diff options
Diffstat (limited to 'src/mgl.c')
-rw-r--r-- | src/mgl.c | 56 |
1 files changed, 55 insertions, 1 deletions
@@ -1,6 +1,10 @@ #include "../include/mgl/mgl.h" #include <X11/Xutil.h> #include <X11/XKBlib.h> +#include <X11/extensions/Xrender.h> +#include <X11/extensions/Xrandr.h> +#include <stdbool.h> +#include <string.h> #include <stdio.h> #ifndef NDEBUG #include <stdlib.h> @@ -22,9 +26,39 @@ static int ignore_xioerror(Display *display) { return 0; } +static bool xrender_is_supported(Display *display, int *event_base, int *error_base) { + *event_base = 0; + *error_base = 0; + if(!XRenderQueryExtension(display, event_base, error_base)) + return false; + + int major_version = 0; + int minor_version = 0; + if(!XRenderQueryVersion(display, &major_version, &minor_version)) + return false; + + return major_version > 0 || (major_version == 0 && minor_version >= 7); +} + +static bool xrandr_is_supported(Display *display, int *event_base, int *error_base) { + *event_base = 0; + *error_base = 0; + if(!XRRQueryExtension(display, event_base, error_base)) + return false; + + int major_version = 0; + int minor_version = 0; + if(!XRRQueryVersion(display, &major_version, &minor_version)) + return false; + + return major_version > 1 || (major_version == 1 && minor_version >= 2); +} + int mgl_init(void) { ++init_count; if(init_count == 1) { + memset(&context, 0, sizeof(context)); + context.connection = XOpenDisplay(NULL); if(!context.connection) { fprintf(stderr, "mgl error: XOpenDisplay failed\n"); @@ -34,6 +68,22 @@ int mgl_init(void) { prev_xerror = XSetErrorHandler(ignore_xerror); prev_xioerror = XSetIOErrorHandler(ignore_xioerror); + + if(!xrender_is_supported(context.connection, &context.render_event_base, &context.render_error_base)) { + fprintf(stderr, "mgl error: x11 render extension is not supported by your X server\n"); + mgl_deinit(); + return -1; + } + + if(!xrandr_is_supported(context.connection, &context.randr_event_base, &context.randr_error_base)) { + fprintf(stderr, "mgl error: x11 randr extension is not supported by your X server\n"); + mgl_deinit(); + return -1; + } + + fprintf(stderr, "randr event base: %d\n", context.randr_event_base); + XRRSelectInput(context.connection, DefaultRootWindow(context.connection), RRScreenChangeNotifyMask | RRCrtcChangeNotifyMask | RROutputChangeNotifyMask); + XInitThreads(); XkbSetDetectableAutoRepeat(context.connection, True, NULL); @@ -51,13 +101,17 @@ int mgl_init(void) { void mgl_deinit(void) { if(init_count == 1) { - if(context.connection) { + if(prev_xioerror) { XSetIOErrorHandler(prev_xioerror); prev_xioerror = NULL; + } + if(prev_xerror) { XSetErrorHandler(prev_xerror); prev_xerror = NULL; + } + if(context.connection) { XCloseDisplay(context.connection); context.connection = NULL; |