aboutsummaryrefslogtreecommitdiff
path: root/tools/gsr-global-hotkeys/main.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2025-01-09 13:08:12 +0100
committerdec05eba <dec05eba@protonmail.com>2025-01-09 13:08:12 +0100
commit77dd32a3ff82180b15447e1d9ab4e9a7c622f531 (patch)
tree85c84a13dddde9a6858fa9734fdcdcc4fdb34539 /tools/gsr-global-hotkeys/main.c
parentb0def958c705ffadeca365753f18aa24ebe85e01 (diff)
Revert "Dont use dlopen in gsr-global-hotkeys"
This reverts commit b0def958c705ffadeca365753f18aa24ebe85e01.
Diffstat (limited to 'tools/gsr-global-hotkeys/main.c')
-rw-r--r--tools/gsr-global-hotkeys/main.c74
1 files changed, 67 insertions, 7 deletions
diff --git a/tools/gsr-global-hotkeys/main.c b/tools/gsr-global-hotkeys/main.c
index 9d7dd68..b64d60f 100644
--- a/tools/gsr-global-hotkeys/main.c
+++ b/tools/gsr-global-hotkeys/main.c
@@ -46,12 +46,76 @@ static void usage(void) {
fprintf(stderr, " --virtual Grab all virtual devices only.\n");
}
-static int x_ignore_error(Display *display, XErrorEvent *ee) {
+typedef void* (*XOpenDisplay_FUNC)(const char*);
+typedef int (*XErrorHandler_FUNC)(void *display, void* error_event);
+typedef XErrorHandler_FUNC (*XSetErrorHandler_FUNC)(XErrorHandler_FUNC handler);
+
+static int x_ignore_error(void *display, void *ee) {
(void)display;
(void)ee;
return 0;
}
+static x11_context setup_x11_context(void) {
+ x11_context x_context = {0};
+ XSetErrorHandler_FUNC XSetErrorHandler = NULL;
+
+ void *x11_lib = dlopen("libX11.so.6", RTLD_LAZY);
+ if(!x11_lib) {
+ fprintf(stderr, "Warning: dlopen libX11.so.6 failed\n");
+ return x_context;
+ }
+
+ XOpenDisplay_FUNC XOpenDisplay = dlsym(x11_lib, "XOpenDisplay");
+ if(!XOpenDisplay) {
+ fprintf(stderr, "Warning: dlsym XOpenDisplay failed\n");
+ goto fail;
+ }
+
+ x_context.XKeycodeToKeysym = dlsym(x11_lib, "XKeycodeToKeysym");
+ if(!x_context.XKeycodeToKeysym) {
+ fprintf(stderr, "Warning: dlsym XKeycodeToKeysym failed\n");
+ goto fail;
+ }
+
+ x_context.XPending = dlsym(x11_lib, "XPending");
+ if(!x_context.XPending) {
+ fprintf(stderr, "Warning: dlsym XPending failed\n");
+ goto fail;
+ }
+
+ x_context.XNextEvent = dlsym(x11_lib, "XNextEvent");
+ if(!x_context.XNextEvent) {
+ fprintf(stderr, "Warning: dlsym XNextEvent failed\n");
+ goto fail;
+ }
+
+ x_context.XRefreshKeyboardMapping = dlsym(x11_lib, "XRefreshKeyboardMapping");
+ if(!x_context.XRefreshKeyboardMapping) {
+ fprintf(stderr, "Warning: dlsym XRefreshKeyboardMapping failed\n");
+ goto fail;
+ }
+
+ x_context.display = XOpenDisplay(NULL);
+ if(!x_context.display) {
+ fprintf(stderr, "Warning: XOpenDisplay failed\n");
+ goto fail;
+ }
+
+ XSetErrorHandler = dlsym(x11_lib, "XSetErrorHandler");
+ if(XSetErrorHandler)
+ XSetErrorHandler(x_ignore_error);
+ else
+ fprintf(stderr, "Warning: dlsym XSetErrorHandler failed\n");
+
+ return x_context;
+
+ fail:
+ memset(&x_context, 0, sizeof(x_context));
+ dlclose(x11_lib);
+ return x_context;
+}
+
int main(int argc, char **argv) {
keyboard_grab_type grab_type = KEYBOARD_GRAB_TYPE_ALL;
if(argc == 2) {
@@ -71,11 +135,7 @@ int main(int argc, char **argv) {
return 1;
}
- Display *display = XOpenDisplay(NULL);
- if(display)
- XSetErrorHandler(x_ignore_error);
- else
- fprintf(stderr, "Warning: XOpenDisplay failed\n");
+ x11_context x_context = setup_x11_context();
const uid_t user_id = getuid();
if(geteuid() != 0) {
@@ -86,7 +146,7 @@ int main(int argc, char **argv) {
}
keyboard_event keyboard_ev;
- if(!keyboard_event_init(&keyboard_ev, true, true, grab_type, display)) {
+ if(!keyboard_event_init(&keyboard_ev, true, true, grab_type, x_context)) {
fprintf(stderr, "Error: failed to setup hotplugging and no keyboard input devices were found\n");
setuid(user_id);
return 1;