summaryrefslogtreecommitdiff
path: root/penxortouch.c
blob: 362ff724899fb7eaf1a886b38958543d40e1be44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* penxortouch

Copyright (c) 2015 Jakob Kaivo <jakob@kaivo.net>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/XInput.h>
#include <X11/Xatom.h>

#define PENDEVICE	"Wacom ISDv4 EC Pen stylus"
#define TOUCHDEVICE	"ELAN Touchscreen"

#define DEVPROP		"Device Enabled"

#define USAGE		"Usage: %s [-h] [-p pen-device] [-t touch-device]\n"

XDevice *
open_dev(Display *dpy, const char *devname)
{
	XDeviceInfo *devs = NULL;
	XDeviceInfo *devinfo = NULL;
	XDevice *dev = NULL;
	int i, ndevs;

	devs = XListInputDevices(dpy, &ndevs);
	for (i = 0; i < ndevs; i++) {
		if (!strcmp(devname, devs[i].name)) {
			devinfo = &devs[i];
		}
	}

	if (devinfo) {
		dev = XOpenDevice(dpy, devinfo->id);
	}
	if (!dev) {
		fprintf(stderr, "Couldn't open device %s.\n", devname);
	}
	return dev;
}

Display *
open_display(void)
{
	int ev, error, xi;
	Display *dpy = XOpenDisplay(NULL);
	if (!dpy) {
		fprintf(stderr, "Couldn't open display.\n");
	} else if (!XQueryExtension(dpy, "XInputExtension", &xi, &ev, &error)) {
		fprintf(stderr, "X Input extension not supported.\n");
		XCloseDisplay(dpy);
		dpy = NULL;
	}
	return dpy;
}

int
enable(Display *dpy, XDevice *dev, int en)
{
	unsigned char data = (en ? 1 : 0);
	Atom prop = XInternAtom(dpy, DEVPROP, False);
	XChangeDeviceProperty(dpy, dev, prop, XA_INTEGER, 8, PropModeReplace, (char*)&data, 1);
	return 0;
}

void
watch(Display *dpy, XDevice *pen, XDevice *touch)
{
	XEventClass evlist[2];
	int i, proxin = -1, proxout = -1;
	XEvent ev;

	for (i = 0; i < pen->num_classes; i++) {
		if (pen->classes[i].input_class == ValuatorClass) {
			ProximityIn(pen, proxin, evlist[0]);
			ProximityOut(pen, proxout, evlist[1]);
		}
	}
	if(XSelectExtensionEvent(dpy, RootWindow(dpy, DefaultScreen(dpy)), evlist, 2) != 0) {
		fprintf(stderr, "Couldn't select events.\n");
		return;
	}

	while (1) {
		XNextEvent(dpy, &ev);
		if (ev.type == proxin) {
			enable(dpy, touch, 0);
		} else if (ev.type == proxout) {
			enable(dpy, touch, 1);
		}
	}
}

int
main(int argc, char **argv)
{
	XDevice *pen = NULL, *touch = NULL;
	Display *dpy = NULL;
	char *pendev = PENDEVICE, *touchdev = TOUCHDEVICE;
	int opt;

	while ((opt = getopt(argc, argv, "?hp:t:")) != -1) {
		switch (opt) {
		case 'p':
			pendev = optarg;
			break;
		case 't':
			touchdev = optarg;
			break;
		case 'h':
			printf(USAGE, argv[0]);
			printf("\n");
			printf("  This command monitors a pen device for proxmity to the screen.\n");
			printf("  When the pen device comes near, the touch device will be disabled.\n");
			printf("  The touch device will be reenabled when the pen leaves the screen.\n");
			printf("  The following options are available:\n");
			printf("\n");
			printf("  -h               Prints out this usage information and exits\n");
			printf("  -p pen-device    Specifies the device to take precedence\n");
			printf("                   (default: %s)\n", PENDEVICE);
			printf("  -t touch-device  Specifies the device to be disabled\n");
			printf("                   (default: %s)\n", TOUCHDEVICE);
			printf("\n");
			return 0;
		default:
			fprintf(stderr, USAGE, argv[0]);
			return 1;
		}
	}

	if ((dpy = open_display()) == NULL) goto error;
	if ((pen = open_dev(dpy, pendev)) == NULL) goto error;
	if ((touch = open_dev(dpy, touchdev)) == NULL) goto error;
	watch(dpy, pen, touch);

	/* Only get here on error conditions */
error:
	if (dpy) XCloseDisplay(dpy);
	return 1;
}