x11_ignore_error.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Error handling mechanism which permits us to ignore specific X11
  3. * errors from particular requests. We maintain a list of upcoming
  4. * potential error events that we want to not treat as fatal errors.
  5. */
  6. #include <ctype.h>
  7. #include <unistd.h>
  8. #include <assert.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include "putty.h"
  12. #ifndef NOT_X_WINDOWS
  13. #include <X11/Xlib.h>
  14. #include <X11/Xutil.h>
  15. #include <X11/Xatom.h>
  16. #include "x11misc.h"
  17. static int (*orig_x11_error_handler)(Display *thisdisp, XErrorEvent *err);
  18. struct x11_err_to_ignore {
  19. Display *display;
  20. unsigned char error_code;
  21. unsigned long serial;
  22. };
  23. static struct x11_err_to_ignore *errs;
  24. static size_t nerrs, errsize;
  25. static int x11_error_handler(Display *thisdisp, XErrorEvent *err)
  26. {
  27. for (size_t i = 0; i < nerrs; i++) {
  28. if (thisdisp == errs[i].display &&
  29. err->serial == errs[i].serial &&
  30. err->error_code == errs[i].error_code) {
  31. /* Ok, this is an error we're happy to ignore */
  32. return 0;
  33. }
  34. }
  35. return (*orig_x11_error_handler)(thisdisp, err);
  36. }
  37. void x11_ignore_error(Display *disp, unsigned char errcode)
  38. {
  39. /*
  40. * Install our error handler, if we haven't already.
  41. */
  42. if (!orig_x11_error_handler)
  43. orig_x11_error_handler = XSetErrorHandler(x11_error_handler);
  44. /*
  45. * This is as good a moment as any to winnow the ignore list based
  46. * on requests we know to have been processed.
  47. */
  48. {
  49. unsigned long last = LastKnownRequestProcessed(disp);
  50. size_t i, j;
  51. for (i = j = 0; i < nerrs; i++) {
  52. if (errs[i].display == disp && errs[i].serial <= last)
  53. continue;
  54. errs[j++] = errs[i];
  55. }
  56. nerrs = j;
  57. }
  58. sgrowarray(errs, errsize, nerrs);
  59. errs[nerrs].display = disp;
  60. errs[nerrs].error_code = errcode;
  61. errs[nerrs].serial = NextRequest(disp);
  62. nerrs++;
  63. }
  64. #else /* NOT_X_WINDOWS */
  65. /*
  66. * Include _something_ in this file to prevent an annoying compiler
  67. * warning, and to avoid having to condition out this file in
  68. * CMakeLists. It's in a library, so this variable shouldn't end up in
  69. * any actual program, because nothing will refer to it.
  70. */
  71. const int x11_ignore_error_dummy_variable = 0;
  72. #endif /* NOT_X_WINDOWS */