xglcomp

view src/xerr.cc @ 12:1c0d056ec360

moving slowly
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 05 Feb 2016 03:33:18 +0200
parents cb636a23f4f2
children
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <stack>
4 #include <X11/Xlib.h>
5 #include <X11/Xlibint.h>
6 #include "xerr.h"
7 #include "logger.h"
9 static int _XPrintDefaultError( Display *dpy, XErrorEvent *event);
11 static std::stack<xerr_handler_type> func_stack;
13 void push_xerr_handler(xerr_handler_type func)
14 {
15 xerr_handler_type prev = XSetErrorHandler(func);
16 if(func_stack.empty()) {
17 func_stack.push(prev);
18 }
19 func_stack.push(func);
20 }
22 void pop_xerr_handler()
23 {
24 if(func_stack.empty()) {
25 log_error("attempt to pop_xerr_handler with an empty stack\n");
26 return;
27 }
28 xerr_handler_type prev = func_stack.top();
29 func_stack.pop();
30 XSetErrorHandler(prev);
31 }
33 int xerr_debug(Display *dpy, XErrorEvent *err)
34 {
35 _XPrintDefaultError(dpy, err);
36 return 0;
37 }
39 int xerr_ignore(Display *dpy, XErrorEvent *err)
40 {
41 return 0;
42 }
44 typedef struct _XExten _XExtension;
46 static int _XPrintDefaultError(Display *dpy, XErrorEvent *event)
47 {
48 char buffer[BUFSIZ];
49 char mesg[BUFSIZ];
50 char number[32];
51 const char *mtype = "XlibMessage";
52 _XExtension *ext = (_XExtension *)NULL;
53 _XExtension *bext = (_XExtension *)NULL;
55 XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
56 XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
57 log_error("%s: %s\n ", mesg, buffer);
58 XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d",
59 mesg, BUFSIZ);
60 log_error(mesg, event->request_code);
61 if (event->request_code < 128) {
62 sprintf(number, "%d", event->request_code);
63 XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
64 } else {
65 for (ext = dpy->ext_procs;
66 ext && (ext->codes.major_opcode != event->request_code);
67 ext = ext->next)
68 ;
69 if (ext) {
70 strncpy(buffer, ext->name, BUFSIZ);
71 buffer[BUFSIZ - 1] = '\0';
72 } else
73 buffer[0] = '\0';
74 }
75 log_error(" (%s)\n", buffer);
76 if (event->request_code >= 128) {
77 XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
78 mesg, BUFSIZ);
79 log_error(" ");
80 log_error(mesg, event->minor_code);
81 if (ext) {
82 sprintf(mesg, "%s.%d", ext->name, event->minor_code);
83 XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
84 log_error(" (%s)", buffer);
85 }
86 log_error("\n");
87 }
88 if (event->error_code >= 128) {
89 /* kludge, try to find the extension that caused it */
90 buffer[0] = '\0';
91 for (ext = dpy->ext_procs; ext; ext = ext->next) {
92 if (ext->error_string)
93 (*ext->error_string)(dpy, event->error_code, &ext->codes,
94 buffer, BUFSIZ);
95 if (buffer[0]) {
96 bext = ext;
97 break;
98 }
99 if (ext->codes.first_error &&
100 ext->codes.first_error < (int)event->error_code &&
101 (!bext || ext->codes.first_error > bext->codes.first_error))
102 bext = ext;
103 }
104 if (bext)
105 sprintf(buffer, "%s.%d", bext->name,
106 event->error_code - bext->codes.first_error);
107 else
108 strcpy(buffer, "Value");
109 XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
110 if (mesg[0]) {
111 log_error(" ");
112 log_error(mesg, event->resourceid);
113 log_error("\n");
114 }
115 /* let extensions try to print the values */
116 for (ext = dpy->ext_procs; ext; ext = ext->next) {
117 if (ext->error_values)
118 (*ext->error_values)(dpy, event, stderr);
119 }
120 } else if ((event->error_code == BadWindow) ||
121 (event->error_code == BadPixmap) ||
122 (event->error_code == BadCursor) ||
123 (event->error_code == BadFont) ||
124 (event->error_code == BadDrawable) ||
125 (event->error_code == BadColor) ||
126 (event->error_code == BadGC) ||
127 (event->error_code == BadIDChoice) ||
128 (event->error_code == BadValue) ||
129 (event->error_code == BadAtom)) {
130 if (event->error_code == BadValue)
131 XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
132 mesg, BUFSIZ);
133 else if (event->error_code == BadAtom)
134 XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
135 mesg, BUFSIZ);
136 else
137 XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
138 mesg, BUFSIZ);
139 log_error(" ");
140 log_error(mesg, event->resourceid);
141 log_error("\n");
142 }
143 XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
144 mesg, BUFSIZ);
145 log_error(" ");
146 log_error(mesg, event->serial);
147 XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
148 mesg, BUFSIZ);
149 log_error("\n ");
150 log_error(mesg, dpy->request);
151 log_error("\n");
152 if (event->error_code == BadImplementation) return 0;
153 return 1;
154 }