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