xglcomp

diff src/xerr.cc @ 11:cb636a23f4f2

lifted X error reporting from Xlib
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 04 Feb 2016 04:15:15 +0200
parents be18500d76d1
children 1c0d056ec360
line diff
     1.1 --- a/src/xerr.cc	Wed Feb 03 03:55:52 2016 +0200
     1.2 +++ b/src/xerr.cc	Thu Feb 04 04:15:15 2016 +0200
     1.3 @@ -1,7 +1,13 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6  #include <stack>
     1.7 +#include <X11/Xlib.h>
     1.8 +#include <X11/Xlibint.h>
     1.9  #include "xerr.h"
    1.10  #include "logger.h"
    1.11  
    1.12 +static int _XPrintDefaultError( Display *dpy, XErrorEvent *event, FILE *fp);
    1.13 +
    1.14  static std::stack<xerr_handler_type> func_stack;
    1.15  
    1.16  void push_xerr_handler(xerr_handler_type func)
    1.17 @@ -23,12 +29,7 @@
    1.18  
    1.19  int xerr_debug(Display *dpy, XErrorEvent *err)
    1.20  {
    1.21 -	char errstr[512];
    1.22 -	XGetErrorText(dpy, err->error_code, errstr, sizeof errstr);
    1.23 -	printf("X error caught: \n%s (code: %u)\n", errstr, err->error_code);
    1.24 -	printf("  Failed request number: %lu (%u,%u)\n", err->serial,
    1.25 -			(unsigned int)err->request_code, (unsigned int)err->minor_code);
    1.26 -	printf("  Resource: %lx\n", err->resourceid);
    1.27 +	_XPrintDefaultError(dpy, err, stderr);
    1.28  	return 0;
    1.29  }
    1.30  
    1.31 @@ -36,3 +37,115 @@
    1.32  {
    1.33  	return 0;
    1.34  }
    1.35 +
    1.36 +typedef struct _XExten _XExtension;
    1.37 +
    1.38 +static int _XPrintDefaultError( Display *dpy, XErrorEvent *event, FILE *fp)
    1.39 +{
    1.40 +	char buffer[BUFSIZ];
    1.41 +	char mesg[BUFSIZ];
    1.42 +	char number[32];
    1.43 +	const char *mtype = "XlibMessage";
    1.44 +	_XExtension *ext = (_XExtension *)NULL;
    1.45 +	_XExtension *bext = (_XExtension *)NULL;
    1.46 +
    1.47 +	XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
    1.48 +	XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
    1.49 +	(void) fprintf(fp, "%s:  %s\n  ", mesg, buffer);
    1.50 +	XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d",
    1.51 +			mesg, BUFSIZ);
    1.52 +	(void) fprintf(fp, mesg, event->request_code);
    1.53 +	if (event->request_code < 128) {
    1.54 +		sprintf(number, "%d", event->request_code);
    1.55 +		XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
    1.56 +	} else {
    1.57 +		for (ext = dpy->ext_procs;
    1.58 +				ext && (ext->codes.major_opcode != event->request_code);
    1.59 +				ext = ext->next)
    1.60 +			;
    1.61 +		if (ext) {
    1.62 +			strncpy(buffer, ext->name, BUFSIZ);
    1.63 +			buffer[BUFSIZ - 1] = '\0';
    1.64 +		} else
    1.65 +			buffer[0] = '\0';
    1.66 +	}
    1.67 +	(void) fprintf(fp, " (%s)\n", buffer);
    1.68 +	if (event->request_code >= 128) {
    1.69 +		XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
    1.70 +				mesg, BUFSIZ);
    1.71 +		fputs("  ", fp);
    1.72 +		(void) fprintf(fp, mesg, event->minor_code);
    1.73 +		if (ext) {
    1.74 +			sprintf(mesg, "%s.%d", ext->name, event->minor_code);
    1.75 +			XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
    1.76 +			(void) fprintf(fp, " (%s)", buffer);
    1.77 +		}
    1.78 +		fputs("\n", fp);
    1.79 +	}
    1.80 +	if (event->error_code >= 128) {
    1.81 +		/* kludge, try to find the extension that caused it */
    1.82 +		buffer[0] = '\0';
    1.83 +		for (ext = dpy->ext_procs; ext; ext = ext->next) {
    1.84 +			if (ext->error_string)
    1.85 +				(*ext->error_string)(dpy, event->error_code, &ext->codes,
    1.86 +						buffer, BUFSIZ);
    1.87 +			if (buffer[0]) {
    1.88 +				bext = ext;
    1.89 +				break;
    1.90 +			}
    1.91 +			if (ext->codes.first_error &&
    1.92 +					ext->codes.first_error < (int)event->error_code &&
    1.93 +					(!bext || ext->codes.first_error > bext->codes.first_error))
    1.94 +				bext = ext;
    1.95 +		}
    1.96 +		if (bext)
    1.97 +			sprintf(buffer, "%s.%d", bext->name,
    1.98 +					event->error_code - bext->codes.first_error);
    1.99 +		else
   1.100 +			strcpy(buffer, "Value");
   1.101 +		XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
   1.102 +		if (mesg[0]) {
   1.103 +			fputs("  ", fp);
   1.104 +			(void) fprintf(fp, mesg, event->resourceid);
   1.105 +			fputs("\n", fp);
   1.106 +		}
   1.107 +		/* let extensions try to print the values */
   1.108 +		for (ext = dpy->ext_procs; ext; ext = ext->next) {
   1.109 +			if (ext->error_values)
   1.110 +				(*ext->error_values)(dpy, event, fp);
   1.111 +		}
   1.112 +	} else if ((event->error_code == BadWindow) ||
   1.113 +			(event->error_code == BadPixmap) ||
   1.114 +			(event->error_code == BadCursor) ||
   1.115 +			(event->error_code == BadFont) ||
   1.116 +			(event->error_code == BadDrawable) ||
   1.117 +			(event->error_code == BadColor) ||
   1.118 +			(event->error_code == BadGC) ||
   1.119 +			(event->error_code == BadIDChoice) ||
   1.120 +			(event->error_code == BadValue) ||
   1.121 +			(event->error_code == BadAtom)) {
   1.122 +		if (event->error_code == BadValue)
   1.123 +			XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
   1.124 +					mesg, BUFSIZ);
   1.125 +		else if (event->error_code == BadAtom)
   1.126 +			XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
   1.127 +					mesg, BUFSIZ);
   1.128 +		else
   1.129 +			XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
   1.130 +					mesg, BUFSIZ);
   1.131 +		fputs("  ", fp);
   1.132 +		(void) fprintf(fp, mesg, event->resourceid);
   1.133 +		fputs("\n", fp);
   1.134 +	}
   1.135 +	XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
   1.136 +			mesg, BUFSIZ);
   1.137 +	fputs("  ", fp);
   1.138 +	(void) fprintf(fp, mesg, event->serial);
   1.139 +	XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
   1.140 +			mesg, BUFSIZ);
   1.141 +	fputs("\n  ", fp);
   1.142 +	(void) fprintf(fp, mesg, dpy->request);
   1.143 +	fputs("\n", fp);
   1.144 +	if (event->error_code == BadImplementation) return 0;
   1.145 +	return 1;
   1.146 +}