main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Helper to run programs on X11 keyboard events.
  3. *
  4. * Copyright (C) 2016 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <signal.h>
  21. #include <errno.h>
  22. #include <X11/Xlib.h>
  23. #include <X11/extensions/XInput2.h>
  24. #define PFX "execonkp: "
  25. static void handle_event(char * const * run_prog)
  26. {
  27. printf(PFX "Key event detected. Running %s...\n", run_prog[0]);
  28. execv(run_prog[0], run_prog);
  29. fprintf(stderr, PFX "execv(\"%s\") failed: %s\n",
  30. run_prog[0], strerror(errno));
  31. exit(1);
  32. }
  33. static void signal_handler(int signal)
  34. {
  35. printf(PFX "terminated\n");
  36. exit(0);
  37. }
  38. static int install_sighandler(int signal, void (*handler)(int))
  39. {
  40. struct sigaction act;
  41. memset(&act, 0, sizeof(act));
  42. sigemptyset(&act.sa_mask);
  43. act.sa_flags = 0;
  44. act.sa_handler = handler;
  45. return sigaction(signal, &act, NULL);
  46. }
  47. static void usage(void)
  48. {
  49. printf("Usage: execonkp [OPTIONS] COMMAND [ARGS]\n");
  50. printf("\n");
  51. printf("Options:\n");
  52. printf(" -p|--on-press Run the command on key press.\n");
  53. printf(" -r|--on-release Run the command on key release.\n");
  54. printf("\n");
  55. printf("By default only -p is set.\n");
  56. printf("Setting -r clears -p unless -p is also specified afterwards.\n");
  57. }
  58. int main(int argc, char **argv)
  59. {
  60. Display *display;
  61. Window window;
  62. XIEventMask evmask;
  63. unsigned char evmask_bits[(XI_LASTEVENT + 8) / 8] = { 0, };
  64. int res, tmp0, tmp1, xi_opcode;
  65. sigset_t sigset;
  66. char * const * run_prog;
  67. int on_press = 1, on_release = 0;
  68. argc--;
  69. argv++;
  70. while (argc >= 1 && argv[0][0] == '-') {
  71. if (strcmp(argv[0], "-p") == 0 ||
  72. strcmp(argv[0], "--on-press") == 0) {
  73. on_press = 1;
  74. } else if (strcmp(argv[0], "-r") == 0 ||
  75. strcmp(argv[0], "--on-release") == 0) {
  76. on_press = 0;
  77. on_release = 1;
  78. } else {
  79. usage();
  80. return 1;
  81. }
  82. argc--;
  83. argv++;
  84. }
  85. if (argc < 1) {
  86. usage();
  87. return 1;
  88. }
  89. run_prog = argv;
  90. display = XOpenDisplay(NULL);
  91. if (!display) {
  92. /* Fallback to the first display. */
  93. display = XOpenDisplay(":0");
  94. if (!display) {
  95. fprintf(stderr, PFX "Failed to open DISPLAY\n");
  96. return 1;
  97. }
  98. }
  99. window = DefaultRootWindow(display);
  100. res = XQueryExtension(display, "XInputExtension", &xi_opcode, &tmp0, &tmp1);
  101. if (!res) {
  102. fprintf(stderr, PFX "X Input extension not available.\n");
  103. return 1;
  104. }
  105. sigemptyset(&sigset);
  106. res = sigprocmask(SIG_SETMASK, &sigset, NULL);
  107. res |= install_sighandler(SIGINT, signal_handler);
  108. res |= install_sighandler(SIGTERM, signal_handler);
  109. if (res) {
  110. fprintf(stderr, PFX "Failed to setup signal handlers\n");
  111. return 1;
  112. }
  113. if (on_press)
  114. XISetMask(evmask_bits, XI_KeyPress);
  115. if (on_release)
  116. XISetMask(evmask_bits, XI_KeyRelease);
  117. memset(&evmask, 0, sizeof(evmask));
  118. evmask.deviceid = XIAllDevices;
  119. evmask.mask_len = sizeof(evmask_bits);
  120. evmask.mask = evmask_bits;
  121. res = XISelectEvents(display, window, &evmask, 1);
  122. if (res != Success) {
  123. fprintf(stderr, PFX "Failed to set event mask (%d)\n", res);
  124. return 1;
  125. }
  126. printf(PFX "Waiting for keyboard event...\n");
  127. while (1) {
  128. XEvent ev;
  129. XGenericEventCookie *cookie = &ev.xcookie;
  130. XNextEvent(display, &ev);
  131. if (XGetEventData(display, cookie) &&
  132. cookie->type == GenericEvent &&
  133. cookie->extension == xi_opcode) {
  134. handle_event(run_prog);
  135. }
  136. XFreeEventData(display, cookie);
  137. }
  138. }