aboutsummaryrefslogtreecommitdiff
path: root/gpu-screen-recorder-overlay-daemon
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-03-30 19:14:30 +0200
committerdec05eba <dec05eba@protonmail.com>2022-03-30 19:14:30 +0200
commit900553e6c0120edaf90fa8d28f34bd82af4c5a58 (patch)
treed5353f60e9384729e78603bf794c0dd8c3b601ef /gpu-screen-recorder-overlay-daemon
parenta66516e34ed1037cdc38fce8bd957fd77152c39f (diff)
Add daemon
Diffstat (limited to 'gpu-screen-recorder-overlay-daemon')
-rw-r--r--gpu-screen-recorder-overlay-daemon/main.c68
-rw-r--r--gpu-screen-recorder-overlay-daemon/project.conf8
2 files changed, 76 insertions, 0 deletions
diff --git a/gpu-screen-recorder-overlay-daemon/main.c b/gpu-screen-recorder-overlay-daemon/main.c
new file mode 100644
index 0000000..197d411
--- /dev/null
+++ b/gpu-screen-recorder-overlay-daemon/main.c
@@ -0,0 +1,68 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include <X11/keysym.h>
+
+static int xerror_dummy(Display *dpy, XErrorEvent *ee) {
+ (void)dpy;
+ (void)ee;
+ return 0;
+}
+
+static const KeySym toggle_overlay_key = XK_Z;
+static void grab_keys(Display *display) {
+ unsigned int numlockmask = 0;
+ KeyCode numlock_keycode = XKeysymToKeycode(display, XK_Num_Lock);
+ XModifierKeymap *modmap = XGetModifierMapping(display);
+ for(int i = 0; i < 8; ++i) {
+ for(int j = 0; j < modmap->max_keypermod; ++j) {
+ if(modmap->modifiermap[i * modmap->max_keypermod + j] == numlock_keycode)
+ numlockmask = (1 << i);
+ }
+ }
+ XFreeModifiermap(modmap);
+
+ XErrorHandler prev_error_handler = XSetErrorHandler(xerror_dummy);
+
+ Window root_window = DefaultRootWindow(display);
+ unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
+ for(int i = 0; i < 4; ++i) {
+ XGrabKey(display, XKeysymToKeycode(display, toggle_overlay_key), Mod1Mask|modifiers[i], root_window, False, GrabModeAsync, GrabModeAsync);
+ }
+
+ XSync(display, False);
+ XSetErrorHandler(prev_error_handler);
+}
+
+static Window get_window_with_input_focus(Display *display) {
+ Window window = None;
+ int rev;
+ if(!XGetInputFocus(display, &window, &rev))
+ window = None;
+ return window;
+}
+
+int main() {
+ Display *display = XOpenDisplay(NULL);
+ if(!display) {
+ fprintf(stderr, "Error: XOpenDisplay failed\n");
+ return 1;
+ }
+
+ grab_keys(display);
+ const KeyCode overlay_keycode = XKeysymToKeycode(display, toggle_overlay_key);
+
+ XEvent xev;
+ for(;;) {
+ XNextEvent(display, &xev);
+ if(xev.type == KeyRelease && xev.xkey.keycode == overlay_keycode) {
+ Window window_with_input_focus = get_window_with_input_focus(display);
+ if(window_with_input_focus && window_with_input_focus != DefaultRootWindow(display)) {
+ char cmd[1024];
+ snprintf(cmd, sizeof(cmd), "/home/dec05eba/git/gpu-screen-recorder-overlay/sibs-build/linux_x86_64/debug/gpu-screen-recorder-overlay %ld", window_with_input_focus);
+ system(cmd);
+ }
+ }
+ }
+ return 0;
+}
diff --git a/gpu-screen-recorder-overlay-daemon/project.conf b/gpu-screen-recorder-overlay-daemon/project.conf
new file mode 100644
index 0000000..456b41b
--- /dev/null
+++ b/gpu-screen-recorder-overlay-daemon/project.conf
@@ -0,0 +1,8 @@
+[package]
+name = "gpu-screen-recorder-overlay-daemon"
+type = "executable"
+version = "0.1.0"
+platforms = ["posix"]
+
+[dependencies]
+x11 = ">=1"