main.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2010 Michael Buesch <m@bues.ch>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <signal.h>
  19. #include <X11/Xlib.h>
  20. static Display *display;
  21. static void signal_handler(int signal)
  22. {
  23. XUngrabKeyboard(display, CurrentTime);
  24. XUngrabPointer(display, CurrentTime);
  25. XSync(display, True);
  26. printf("pwrtray-xlock: released\n");
  27. exit(0);
  28. }
  29. static int install_sighandler(int signal, void (*handler)(int))
  30. {
  31. struct sigaction act;
  32. memset(&act, 0, sizeof(act));
  33. sigemptyset(&act.sa_mask);
  34. act.sa_flags = 0;
  35. act.sa_handler = handler;
  36. return sigaction(signal, &act, NULL);
  37. }
  38. int main(int argc, char **argv)
  39. {
  40. Window window;
  41. int res;
  42. sigset_t sigset;
  43. display = XOpenDisplay(NULL);
  44. if (!display) {
  45. /* Fallback to the first display. */
  46. display = XOpenDisplay(":0");
  47. if (!display) {
  48. fprintf(stderr, "Failed to open DISPLAY\n");
  49. return 1;
  50. }
  51. }
  52. window = DefaultRootWindow(display);
  53. sigemptyset(&sigset);
  54. res = sigprocmask(SIG_SETMASK, &sigset, NULL);
  55. res |= install_sighandler(SIGINT, signal_handler);
  56. res |= install_sighandler(SIGTERM, signal_handler);
  57. if (res) {
  58. fprintf(stderr, "Failed to setup signal handlers\n");
  59. return 1;
  60. }
  61. res = XGrabPointer(display, window,
  62. False, 0, GrabModeAsync, GrabModeAsync,
  63. None, None, CurrentTime);
  64. if (res != GrabSuccess) {
  65. fprintf(stderr, "Failed to grab pointer (res=%d)\n", res);
  66. return 1;
  67. }
  68. res = XGrabKeyboard(display, window,
  69. False, GrabModeAsync, GrabModeAsync,
  70. CurrentTime);
  71. if (res != GrabSuccess) {
  72. fprintf(stderr, "Failed to grab keyboard (res=%d)\n", res);
  73. XUngrabPointer(display, CurrentTime);
  74. return 1;
  75. }
  76. XFlush(display);
  77. printf("pwrtray-xlock: locked\n");
  78. while (1)
  79. sleep(0xFFFF);
  80. }