diff options
author | Vito Caputo <vcaputo@gnugeneration.com> | 2017-02-24 19:43:51 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@gnugeneration.com> | 2017-03-14 19:41:48 -0700 |
commit | 4dc9319239a172eb9f36ca57ea539a4a28edb50f (patch) | |
tree | 98956690d4876f7643760ebad544123fb365ae53 /src/xserver.c | |
parent | a14269b154750450a947af4e8b2ac5b4470f35ed (diff) |
xserver: introduce isolated core xserver api
In preparation for monitoring overlays being shared across vwm and vmon,
adding a common xserver abstraction for both to use and overlay to depend
on.
Diffstat (limited to 'src/xserver.c')
-rw-r--r-- | src/xserver.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/xserver.c b/src/xserver.c new file mode 100644 index 0000000..114a924 --- /dev/null +++ b/src/xserver.c @@ -0,0 +1,67 @@ +#include <X11/Xlib.h> +#include <fcntl.h> +#include <stdlib.h> +#include <unistd.h> + +#include "util.h" +#include "xserver.h" + +/* xserver context shared by vwm and vmon. + * This is the X part monitor.c interfaces with, so monitor.c can avoid depending + * on vwm_t which won't exist in the vmon tool. + */ + +static int errhandler(Display *display, XErrorEvent *err) +{ + /* TODO */ + return 1; +} + +/* open the X display server, initializing *xserver on success. */ +vwm_xserver_t * vwm_xserver_open(void) +{ + vwm_xserver_t *xserver; + + if (!(xserver = calloc(sizeof(vwm_xserver_t), 1))) { + VWM_ERROR("Cannot allocate vwm_xserver_t"); + goto _err; + } + + if (!(xserver->display = XOpenDisplay(NULL))) { + VWM_ERROR("Cannot open display"); + goto _err_free; + } + + /* prevent children from inheriting the X connection */ + if (fcntl(ConnectionNumber(xserver->display), F_SETFD, FD_CLOEXEC) < 0) { + VWM_ERROR("Cannot set FD_CLOEXEC on X connection"); + goto _err_xclose; + } + + XSetErrorHandler(errhandler); /* TODO, this may not belong here. */ + + xserver->screen_num = DefaultScreen(xserver->display); + xserver->gc = XCreateGC(xserver->display, XSERVER_XROOT(xserver), 0, NULL); + xserver->cmap = DefaultColormap(xserver->display, xserver->screen_num); + + return xserver; + + +_err_xclose: + XCloseDisplay(xserver->display); + +_err_free: + free(xserver); + +_err: + return NULL; +} + + +/* close on opened xserver */ +void vwm_xserver_close(vwm_xserver_t *xserver) +{ + XFlush(xserver->display); + XCloseDisplay(xserver->display); + free(xserver); +} |