2016-03-20 18:16:43 +00:00
|
|
|
/*
|
|
|
|
* x11misc.c: miscellaneous stuff for dealing directly with X servers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
|
|
|
|
#ifndef NOT_X_WINDOWS
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
|
|
|
|
#include "x11misc.h"
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
* Error handling mechanism which permits us to ignore specific X11
|
|
|
|
* errors from particular requests. We maintain a list of upcoming
|
|
|
|
* potential error events that we want to not treat as fatal errors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int (*orig_x11_error_handler)(Display *thisdisp, XErrorEvent *err);
|
|
|
|
|
|
|
|
struct x11_err_to_ignore {
|
|
|
|
Display *display;
|
|
|
|
unsigned char error_code;
|
|
|
|
unsigned long serial;
|
|
|
|
};
|
|
|
|
|
2020-01-29 06:22:01 +00:00
|
|
|
static struct x11_err_to_ignore *errs;
|
|
|
|
static size_t nerrs, errsize;
|
2016-03-20 18:16:43 +00:00
|
|
|
|
|
|
|
static int x11_error_handler(Display *thisdisp, XErrorEvent *err)
|
|
|
|
{
|
New array-growing macros: sgrowarray and sgrowarrayn.
The idea of these is that they centralise the common idiom along the
lines of
if (logical_array_len >= physical_array_size) {
physical_array_size = logical_array_len * 5 / 4 + 256;
array = sresize(array, physical_array_size, ElementType);
}
which happens at a zillion call sites throughout this code base, with
different random choices of the geometric factor and additive
constant, sometimes forgetting them completely, and generally doing a
lot of repeated work.
The new macro sgrowarray(array,size,n) has the semantics: here are the
array pointer and its physical size for you to modify, now please
ensure that the nth element exists, so I can write into it. And
sgrowarrayn(array,size,n,m) is the same except that it ensures that
the array has size at least n+m (so sgrowarray is just the special
case where m=1).
Now that this is a single centralised implementation that will be used
everywhere, I've also gone to more effort in the implementation, with
careful overflow checks that would have been painful to put at all the
previous call sites.
This commit also switches over every use of sresize(), apart from a
few where I really didn't think it would gain anything. A consequence
of that is that a lot of array-size variables have to have their types
changed to size_t, because the macros require that (they address-take
the size to pass to the underlying function).
2019-02-28 20:07:30 +00:00
|
|
|
for (size_t i = 0; i < nerrs; i++) {
|
2016-03-20 18:16:43 +00:00
|
|
|
if (thisdisp == errs[i].display &&
|
|
|
|
err->serial == errs[i].serial &&
|
|
|
|
err->error_code == errs[i].error_code) {
|
|
|
|
/* Ok, this is an error we're happy to ignore */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*orig_x11_error_handler)(thisdisp, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
void x11_ignore_error(Display *disp, unsigned char errcode)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Install our error handler, if we haven't already.
|
|
|
|
*/
|
|
|
|
if (!orig_x11_error_handler)
|
|
|
|
orig_x11_error_handler = XSetErrorHandler(x11_error_handler);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is as good a moment as any to winnow the ignore list based
|
|
|
|
* on requests we know to have been processed.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
unsigned long last = LastKnownRequestProcessed(disp);
|
New array-growing macros: sgrowarray and sgrowarrayn.
The idea of these is that they centralise the common idiom along the
lines of
if (logical_array_len >= physical_array_size) {
physical_array_size = logical_array_len * 5 / 4 + 256;
array = sresize(array, physical_array_size, ElementType);
}
which happens at a zillion call sites throughout this code base, with
different random choices of the geometric factor and additive
constant, sometimes forgetting them completely, and generally doing a
lot of repeated work.
The new macro sgrowarray(array,size,n) has the semantics: here are the
array pointer and its physical size for you to modify, now please
ensure that the nth element exists, so I can write into it. And
sgrowarrayn(array,size,n,m) is the same except that it ensures that
the array has size at least n+m (so sgrowarray is just the special
case where m=1).
Now that this is a single centralised implementation that will be used
everywhere, I've also gone to more effort in the implementation, with
careful overflow checks that would have been painful to put at all the
previous call sites.
This commit also switches over every use of sresize(), apart from a
few where I really didn't think it would gain anything. A consequence
of that is that a lot of array-size variables have to have their types
changed to size_t, because the macros require that (they address-take
the size to pass to the underlying function).
2019-02-28 20:07:30 +00:00
|
|
|
size_t i, j;
|
2016-03-20 18:16:43 +00:00
|
|
|
for (i = j = 0; i < nerrs; i++) {
|
|
|
|
if (errs[i].display == disp && errs[i].serial <= last)
|
|
|
|
continue;
|
|
|
|
errs[j++] = errs[i];
|
|
|
|
}
|
|
|
|
nerrs = j;
|
|
|
|
}
|
|
|
|
|
New array-growing macros: sgrowarray and sgrowarrayn.
The idea of these is that they centralise the common idiom along the
lines of
if (logical_array_len >= physical_array_size) {
physical_array_size = logical_array_len * 5 / 4 + 256;
array = sresize(array, physical_array_size, ElementType);
}
which happens at a zillion call sites throughout this code base, with
different random choices of the geometric factor and additive
constant, sometimes forgetting them completely, and generally doing a
lot of repeated work.
The new macro sgrowarray(array,size,n) has the semantics: here are the
array pointer and its physical size for you to modify, now please
ensure that the nth element exists, so I can write into it. And
sgrowarrayn(array,size,n,m) is the same except that it ensures that
the array has size at least n+m (so sgrowarray is just the special
case where m=1).
Now that this is a single centralised implementation that will be used
everywhere, I've also gone to more effort in the implementation, with
careful overflow checks that would have been painful to put at all the
previous call sites.
This commit also switches over every use of sresize(), apart from a
few where I really didn't think it would gain anything. A consequence
of that is that a lot of array-size variables have to have their types
changed to size_t, because the macros require that (they address-take
the size to pass to the underlying function).
2019-02-28 20:07:30 +00:00
|
|
|
sgrowarray(errs, errsize, nerrs);
|
2016-03-20 18:16:43 +00:00
|
|
|
errs[nerrs].display = disp;
|
|
|
|
errs[nerrs].error_code = errcode;
|
|
|
|
errs[nerrs].serial = NextRequest(disp);
|
|
|
|
nerrs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|