From 37f398160ba387b84e811b2f2d69d467d793e3da Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 26 Jun 2021 14:10:25 +0200 Subject: Add xmonad-autofocus-output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- xmonad/xmonad-autofocus-output.c | 93 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 xmonad/xmonad-autofocus-output.c (limited to 'xmonad/xmonad-autofocus-output.c') diff --git a/xmonad/xmonad-autofocus-output.c b/xmonad/xmonad-autofocus-output.c new file mode 100644 index 0000000..853e18b --- /dev/null +++ b/xmonad/xmonad-autofocus-output.c @@ -0,0 +1,93 @@ +/* -lX11 -lXtst */ +#include +#include +#include + +#define XK_MISCELLANY +#define XK_LATIN1 +#include +#include +#include + +static Display *disp; +static KeyCode mod, key[2]; + +static inline int +get_output(int x, int y) +{ + return x < 1920; +} + +static inline void +set_output(int output) +{ + XTestFakeKeyEvent(disp, mod, 1, 0); + XTestFakeKeyEvent(disp, key[output], 1, 0); + XTestFakeKeyEvent(disp, key[output], 0, 0); + XTestFakeKeyEvent(disp, mod, 0, 0); + XFlush(disp); +} + +int +main(int argc, char *argv[]) +{ + int fd, scrn; + Window root; + fd_set fds; + XEvent ev; + int output, prev_output = -1; + + XSetLocaleModifiers(""); + if (!(disp = XOpenDisplay(NULL))) { + fprintf(stderr, "%s: cannot open display\n", *argv); + return 1; + } + + fd = XConnectionNumber(disp); + scrn = XDefaultScreen(disp); + root = XRootWindow(disp, scrn); + XSelectInput(disp, root, PointerMotionMask | EnterWindowMask); + XSync(disp, False); + + mod = XKeysymToKeycode(disp, XK_Super_L); + key[0] = XKeysymToKeycode(disp, XK_W); + key[1] = XKeysymToKeycode(disp, XK_E); + + for (;;) { + FD_ZERO(&fds); + FD_SET(fd, &fds); + + if (select(fd + 1, &fds, NULL, NULL, NULL) < 0) { + if (errno == EINTR) + continue; + perror(*argv); + return 1; + } + + if (!FD_ISSET(fd, &fds)) + continue; + + while (XPending(disp)) { + XNextEvent(disp, &ev); + if (XFilterEvent(&ev, None)) + continue; + switch (ev.type) { + case MotionNotify: + output = get_output(ev.xmotion.x_root, ev.xmotion.y_root); + if (output == prev_output) + continue; + prev_output = output; + set_output(output); + break; + case EnterNotify: + prev_output = -1; + break; + default: + break; + } + } + } + + return 0; + (void) argc; +} -- cgit v1.2.3-70-g09d2