123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /* 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;
- }
|