x11misc.c 2.1 KB

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