xglcomp

changeset 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
files src/main.cc src/xerr.cc
diffstat 2 files changed, 121 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/src/main.cc	Wed Feb 03 03:55:52 2016 +0200
     1.2 +++ b/src/main.cc	Thu Feb 04 04:15:15 2016 +0200
     1.3 @@ -237,6 +237,8 @@
     1.4  	push_xerr_handler(xerr_debug);
     1.5  	XDamageSubtract(dpy, cwin->damage, None, region);
     1.6  	pop_xerr_handler();
     1.7 +
     1.8 +	XFixesDestroyRegion(dpy, region);
     1.9  }
    1.10  
    1.11  static void redraw()
     2.1 --- a/src/xerr.cc	Wed Feb 03 03:55:52 2016 +0200
     2.2 +++ b/src/xerr.cc	Thu Feb 04 04:15:15 2016 +0200
     2.3 @@ -1,7 +1,13 @@
     2.4 +#include <stdio.h>
     2.5 +#include <string.h>
     2.6  #include <stack>
     2.7 +#include <X11/Xlib.h>
     2.8 +#include <X11/Xlibint.h>
     2.9  #include "xerr.h"
    2.10  #include "logger.h"
    2.11  
    2.12 +static int _XPrintDefaultError( Display *dpy, XErrorEvent *event, FILE *fp);
    2.13 +
    2.14  static std::stack<xerr_handler_type> func_stack;
    2.15  
    2.16  void push_xerr_handler(xerr_handler_type func)
    2.17 @@ -23,12 +29,7 @@
    2.18  
    2.19  int xerr_debug(Display *dpy, XErrorEvent *err)
    2.20  {
    2.21 -	char errstr[512];
    2.22 -	XGetErrorText(dpy, err->error_code, errstr, sizeof errstr);
    2.23 -	printf("X error caught: \n%s (code: %u)\n", errstr, err->error_code);
    2.24 -	printf("  Failed request number: %lu (%u,%u)\n", err->serial,
    2.25 -			(unsigned int)err->request_code, (unsigned int)err->minor_code);
    2.26 -	printf("  Resource: %lx\n", err->resourceid);
    2.27 +	_XPrintDefaultError(dpy, err, stderr);
    2.28  	return 0;
    2.29  }
    2.30  
    2.31 @@ -36,3 +37,115 @@
    2.32  {
    2.33  	return 0;
    2.34  }
    2.35 +
    2.36 +typedef struct _XExten _XExtension;
    2.37 +
    2.38 +static int _XPrintDefaultError( Display *dpy, XErrorEvent *event, FILE *fp)
    2.39 +{
    2.40 +	char buffer[BUFSIZ];
    2.41 +	char mesg[BUFSIZ];
    2.42 +	char number[32];
    2.43 +	const char *mtype = "XlibMessage";
    2.44 +	_XExtension *ext = (_XExtension *)NULL;
    2.45 +	_XExtension *bext = (_XExtension *)NULL;
    2.46 +
    2.47 +	XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
    2.48 +	XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
    2.49 +	(void) fprintf(fp, "%s:  %s\n  ", mesg, buffer);
    2.50 +	XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d",
    2.51 +			mesg, BUFSIZ);
    2.52 +	(void) fprintf(fp, mesg, event->request_code);
    2.53 +	if (event->request_code < 128) {
    2.54 +		sprintf(number, "%d", event->request_code);
    2.55 +		XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
    2.56 +	} else {
    2.57 +		for (ext = dpy->ext_procs;
    2.58 +				ext && (ext->codes.major_opcode != event->request_code);
    2.59 +				ext = ext->next)
    2.60 +			;
    2.61 +		if (ext) {
    2.62 +			strncpy(buffer, ext->name, BUFSIZ);
    2.63 +			buffer[BUFSIZ - 1] = '\0';
    2.64 +		} else
    2.65 +			buffer[0] = '\0';
    2.66 +	}
    2.67 +	(void) fprintf(fp, " (%s)\n", buffer);
    2.68 +	if (event->request_code >= 128) {
    2.69 +		XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
    2.70 +				mesg, BUFSIZ);
    2.71 +		fputs("  ", fp);
    2.72 +		(void) fprintf(fp, mesg, event->minor_code);
    2.73 +		if (ext) {
    2.74 +			sprintf(mesg, "%s.%d", ext->name, event->minor_code);
    2.75 +			XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ);
    2.76 +			(void) fprintf(fp, " (%s)", buffer);
    2.77 +		}
    2.78 +		fputs("\n", fp);
    2.79 +	}
    2.80 +	if (event->error_code >= 128) {
    2.81 +		/* kludge, try to find the extension that caused it */
    2.82 +		buffer[0] = '\0';
    2.83 +		for (ext = dpy->ext_procs; ext; ext = ext->next) {
    2.84 +			if (ext->error_string)
    2.85 +				(*ext->error_string)(dpy, event->error_code, &ext->codes,
    2.86 +						buffer, BUFSIZ);
    2.87 +			if (buffer[0]) {
    2.88 +				bext = ext;
    2.89 +				break;
    2.90 +			}
    2.91 +			if (ext->codes.first_error &&
    2.92 +					ext->codes.first_error < (int)event->error_code &&
    2.93 +					(!bext || ext->codes.first_error > bext->codes.first_error))
    2.94 +				bext = ext;
    2.95 +		}
    2.96 +		if (bext)
    2.97 +			sprintf(buffer, "%s.%d", bext->name,
    2.98 +					event->error_code - bext->codes.first_error);
    2.99 +		else
   2.100 +			strcpy(buffer, "Value");
   2.101 +		XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
   2.102 +		if (mesg[0]) {
   2.103 +			fputs("  ", fp);
   2.104 +			(void) fprintf(fp, mesg, event->resourceid);
   2.105 +			fputs("\n", fp);
   2.106 +		}
   2.107 +		/* let extensions try to print the values */
   2.108 +		for (ext = dpy->ext_procs; ext; ext = ext->next) {
   2.109 +			if (ext->error_values)
   2.110 +				(*ext->error_values)(dpy, event, fp);
   2.111 +		}
   2.112 +	} else if ((event->error_code == BadWindow) ||
   2.113 +			(event->error_code == BadPixmap) ||
   2.114 +			(event->error_code == BadCursor) ||
   2.115 +			(event->error_code == BadFont) ||
   2.116 +			(event->error_code == BadDrawable) ||
   2.117 +			(event->error_code == BadColor) ||
   2.118 +			(event->error_code == BadGC) ||
   2.119 +			(event->error_code == BadIDChoice) ||
   2.120 +			(event->error_code == BadValue) ||
   2.121 +			(event->error_code == BadAtom)) {
   2.122 +		if (event->error_code == BadValue)
   2.123 +			XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
   2.124 +					mesg, BUFSIZ);
   2.125 +		else if (event->error_code == BadAtom)
   2.126 +			XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
   2.127 +					mesg, BUFSIZ);
   2.128 +		else
   2.129 +			XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
   2.130 +					mesg, BUFSIZ);
   2.131 +		fputs("  ", fp);
   2.132 +		(void) fprintf(fp, mesg, event->resourceid);
   2.133 +		fputs("\n", fp);
   2.134 +	}
   2.135 +	XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
   2.136 +			mesg, BUFSIZ);
   2.137 +	fputs("  ", fp);
   2.138 +	(void) fprintf(fp, mesg, event->serial);
   2.139 +	XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
   2.140 +			mesg, BUFSIZ);
   2.141 +	fputs("\n  ", fp);
   2.142 +	(void) fprintf(fp, mesg, dpy->request);
   2.143 +	fputs("\n", fp);
   2.144 +	if (event->error_code == BadImplementation) return 0;
   2.145 +	return 1;
   2.146 +}