Last active
March 10, 2022 08:44
-
-
Save nkrapivin/452c1509559cff0945f141910aa098a8 to your computer and use it in GitHub Desktop.
libnikXfix - a small hack to fix keyboard input in Linux GameMaker games.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Compile me as: | |
Release version - `gcc -fPIC -ldl -shared nikXfix.c -o libnikXfix.so` | |
Debug version - `gcc -fPIC -g -O0 -DDEBUG -ldl -shared nikXfix.c -o libnikXfix.so` | |
Then load with LD_PRELOAD="./assets/libnikXfix.so" (.so is to be placed in Included Files) | |
Enjoy! | |
*/ | |
#define _GNU_SOURCE 1 | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <X11/Xlib.h> | |
#include <X11/Xutil.h> | |
#include <dlfcn.h> | |
typedef int (*XLookupString_type)(XKeyEvent*, char*, int, KeySym*, XComposeStatus*); | |
static XLookupString_type XLookupString_orig = NULL; | |
#define NPRINT(...) printf("[nikXfix]: "); printf(__VA_ARGS__); printf("\n"); fflush(stdout) | |
int XLookupString( | |
XKeyEvent* event_struct, | |
char* buffer_return, | |
int bytes_buffer, | |
KeySym* keysym_return, | |
XComposeStatus* status_in_out | |
) { | |
int rc; /* return code */ | |
if (!XLookupString_orig) { | |
NPRINT("Trying to resolve XLookupString..."); | |
XLookupString_orig = dlsym(RTLD_NEXT, "XLookupString"); | |
if (!XLookupString_orig) { | |
NPRINT("Unable to resolve XLookupString!"); | |
abort(); | |
} | |
NPRINT("XLookupString is resolved to 0x%p", XLookupString_orig); | |
} | |
/* restricts the bitmask to 8bit range only, excluding all locale info, change if this breaks things! */ | |
if (event_struct) event_struct->state &= 0xFFu; | |
rc = XLookupString_orig(event_struct, buffer_return, bytes_buffer, keysym_return, status_in_out); | |
/* big fun */ | |
#ifdef DEBUG | |
NPRINT("rc=%d,keycode=0x%X,state=0x%X", rc, event_struct->keycode, event_struct->state); | |
#endif | |
return rc; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment