penxortouch.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* penxortouch
  2. Copyright (c) 2015 Jakob Kaivo <jakob@kaivo.net>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <X11/Xlib.h>
  23. #include <X11/extensions/XInput.h>
  24. #include <X11/Xatom.h>
  25. #define PENDEVICE "Wacom ISDv4 EC Pen stylus"
  26. #define TOUCHDEVICE "ELAN Touchscreen"
  27. #define DEVPROP "Device Enabled"
  28. #define USAGE "Usage: %s [-h] [-p pen-device] [-t touch-device]\n"
  29. XDevice *
  30. open_dev(Display *dpy, const char *devname)
  31. {
  32. XDeviceInfo *devs = NULL;
  33. XDeviceInfo *devinfo = NULL;
  34. XDevice *dev = NULL;
  35. int i, ndevs;
  36. devs = XListInputDevices(dpy, &ndevs);
  37. for (i = 0; i < ndevs; i++) {
  38. if (!strcmp(devname, devs[i].name)) {
  39. devinfo = &devs[i];
  40. }
  41. }
  42. if (devinfo) {
  43. dev = XOpenDevice(dpy, devinfo->id);
  44. }
  45. if (!dev) {
  46. fprintf(stderr, "Couldn't open device %s.\n", devname);
  47. }
  48. return dev;
  49. }
  50. Display *
  51. open_display(void)
  52. {
  53. int ev, error, xi;
  54. Display *dpy = XOpenDisplay(NULL);
  55. if (!dpy) {
  56. fprintf(stderr, "Couldn't open display.\n");
  57. } else if (!XQueryExtension(dpy, "XInputExtension", &xi, &ev, &error)) {
  58. fprintf(stderr, "X Input extension not supported.\n");
  59. XCloseDisplay(dpy);
  60. dpy = NULL;
  61. }
  62. return dpy;
  63. }
  64. int
  65. enable(Display *dpy, XDevice *dev, int en)
  66. {
  67. unsigned char data = (en ? 1 : 0);
  68. Atom prop = XInternAtom(dpy, DEVPROP, False);
  69. XChangeDeviceProperty(dpy, dev, prop, XA_INTEGER, 8, PropModeReplace, (char*)&data, 1);
  70. return 0;
  71. }
  72. void
  73. watch(Display *dpy, XDevice *pen, XDevice *touch)
  74. {
  75. XEventClass evlist[2];
  76. int i, proxin = -1, proxout = -1;
  77. XEvent ev;
  78. for (i = 0; i < pen->num_classes; i++) {
  79. if (pen->classes[i].input_class == ValuatorClass) {
  80. ProximityIn(pen, proxin, evlist[0]);
  81. ProximityOut(pen, proxout, evlist[1]);
  82. }
  83. }
  84. if(XSelectExtensionEvent(dpy, RootWindow(dpy, DefaultScreen(dpy)), evlist, 2) != 0) {
  85. fprintf(stderr, "Couldn't select events.\n");
  86. return;
  87. }
  88. while (1) {
  89. XNextEvent(dpy, &ev);
  90. if (ev.type == proxin) {
  91. enable(dpy, touch, 0);
  92. } else if (ev.type == proxout) {
  93. enable(dpy, touch, 1);
  94. }
  95. }
  96. }
  97. int
  98. main(int argc, char **argv)
  99. {
  100. XDevice *pen = NULL, *touch = NULL;
  101. Display *dpy = NULL;
  102. char *pendev = PENDEVICE, *touchdev = TOUCHDEVICE;
  103. int opt;
  104. while ((opt = getopt(argc, argv, "?hp:t:")) != -1) {
  105. switch (opt) {
  106. case 'p':
  107. pendev = optarg;
  108. break;
  109. case 't':
  110. touchdev = optarg;
  111. break;
  112. case 'h':
  113. printf(USAGE, argv[0]);
  114. printf("\n");
  115. printf(" This command monitors a pen device for proxmity to the screen.\n");
  116. printf(" When the pen device comes near, the touch device will be disabled.\n");
  117. printf(" The touch device will be reenabled when the pen leaves the screen.\n");
  118. printf(" The following options are available:\n");
  119. printf("\n");
  120. printf(" -h Prints out this usage information and exits\n");
  121. printf(" -p pen-device Specifies the device to take precedence\n");
  122. printf(" (default: %s)\n", PENDEVICE);
  123. printf(" -t touch-device Specifies the device to be disabled\n");
  124. printf(" (default: %s)\n", TOUCHDEVICE);
  125. printf("\n");
  126. return 0;
  127. default:
  128. fprintf(stderr, USAGE, argv[0]);
  129. return 1;
  130. }
  131. }
  132. if ((dpy = open_display()) == NULL) goto error;
  133. if ((pen = open_dev(dpy, pendev)) == NULL) goto error;
  134. if ((touch = open_dev(dpy, touchdev)) == NULL) goto error;
  135. watch(dpy, pen, touch);
  136. /* Only get here on error conditions */
  137. error:
  138. if (dpy) XCloseDisplay(dpy);
  139. return 1;
  140. }