#include "xorg.h" static Display *DISPLAY = NULL; Display *getXDisplay(void) { return DISPLAY; } int XDisplayOpen(char *name) { DISPLAY = XOpenDisplay(name); return DISPLAY == NULL; } void XDisplayClose(void) { XCloseDisplay(DISPLAY); } void XMove(int x, int y) { Display *display = getXDisplay(); XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, x, y); XSync(display, 0); } void XCursorPosition(int *x, int *y) { Display *display = getXDisplay(); Window root = DefaultRootWindow(display); Window window; int i; unsigned mask; XQueryPointer(display, root, &root, &window, x, y, &i, &i, &mask); } void XScroll(int x, int y) { int ydir = 4; /* Button 4 is up, 5 is down. */ int xdir = 6; Display *display = getXDisplay(); if (y < 0) { ydir = 5; } if (x < 0) { xdir = 7; } int xi; int yi; for (xi = 0; xi < abs(x); xi++) { XTestFakeButtonEvent(display, xdir, 1, CurrentTime); XTestFakeButtonEvent(display, xdir, 0, CurrentTime); } for (yi = 0; yi < abs(y); yi++) { XTestFakeButtonEvent(display, ydir, 1, CurrentTime); XTestFakeButtonEvent(display, ydir, 0, CurrentTime); } XSync(display, 0); } void XButton(unsigned int button, int down) { if (button == 0) return; Display *display = getXDisplay(); XTestFakeButtonEvent(display, button, down, CurrentTime); XSync(display, 0); } static xkeyentry_t *xKeysHead = NULL; // add keycode->keysym mapping to list void XKeyEntryAdd(KeySym keysym, KeyCode keycode) { xkeyentry_t *entry = (xkeyentry_t *) malloc(sizeof(xkeyentry_t)); if (entry == NULL) return; entry->keysym = keysym; entry->keycode = keycode; entry->next = xKeysHead; xKeysHead = entry; } // get keycode for keysym from list KeyCode XKeyEntryGet(KeySym keysym) { xkeyentry_t *prev = NULL; xkeyentry_t *curr = xKeysHead; KeyCode keycode = 0; while (curr != NULL) { if (curr->keysym == keysym) { keycode = curr->keycode; if (prev == NULL) { xKeysHead = curr->next; } else { prev->next = curr->next; } free(curr); return keycode; } prev = curr; curr = curr->next; } return 0; } // From https://github.com/TigerVNC/tigervnc/blob/0946e298075f8f7b6d63e552297a787c5f84d27c/unix/x0vncserver/XDesktop.cxx#L343-L379 KeyCode XkbKeysymToKeycode(Display* dpy, KeySym keysym) { XkbDescPtr xkb; XkbStateRec state; unsigned int mods; unsigned keycode; xkb = XkbGetMap(dpy, XkbAllComponentsMask, XkbUseCoreKbd); if (!xkb) return 0; XkbGetState(dpy, XkbUseCoreKbd, &state); // XkbStateFieldFromRec() doesn't work properly because // state.lookup_mods isn't properly updated, so we do this manually mods = XkbBuildCoreState(XkbStateMods(&state), state.group); for (keycode = xkb->min_key_code; keycode <= xkb->max_key_code; keycode++) { KeySym cursym; unsigned int out_mods; XkbTranslateKeyCode(xkb, keycode, mods, &out_mods, &cursym); if (cursym == keysym) break; } if (keycode > xkb->max_key_code) keycode = 0; XkbFreeKeyboard(xkb, XkbAllComponentsMask, True); // Shift+Tab is usually ISO_Left_Tab, but RFB hides this fact. Do // another attempt if we failed the initial lookup if ((keycode == 0) && (keysym == XK_Tab) && (mods & ShiftMask)) return XkbKeysymToKeycode(dpy, XK_ISO_Left_Tab); return keycode; } static xkeycode_t *xFreeKeycodesHead = NULL; int xNumcodes = 0; void XFreeKeycodesInit(Display* dpy) { if (xFreeKeycodesHead != NULL) return; KeyCode keycode; KeySym *keysyms; int min, max, numcodes; // get full keyboard mapping XDisplayKeycodes(dpy, &min, &max); keysyms = XGetKeyboardMapping(dpy, min, max-min, &numcodes); xNumcodes = numcodes; // loop through all keycodes xkeycode_t *last = NULL; for (int i = min; i <= max; ++i) { // check if keycode is empty int isEmpty = 1; for (int j = 0; j < numcodes; ++j) { int symindex = ((i - min) * numcodes) + j; if (keysyms[symindex] != 0) { isEmpty = 0; break; } } if (!isEmpty) continue; xkeycode_t *entry = (xkeycode_t *) malloc(sizeof(xkeycode_t)); if (entry == NULL) return; entry->keycode = i; if (last == NULL) { xFreeKeycodesHead = entry; } else { last->next = entry; } last = entry; } if (last != NULL) { // make as circular list last->next = xFreeKeycodesHead; } XFree(keysyms); } // get free keycode from list KeyCode XFreeKeycodeGet() { if (xFreeKeycodesHead == NULL) return 0; // move head to next entry xkeycode_t *entry = xFreeKeycodesHead; xFreeKeycodesHead = entry->next; return entry->keycode; } // map keysym to new keycode KeyCode XKeysymMapNew(Display* dpy, KeySym keysym) { // initialize free keycodes list if (xFreeKeycodesHead == NULL) { XFreeKeycodesInit(dpy); } KeyCode keycode = XFreeKeycodeGet(); // no free keycodes, cannot map keysym if (keycode != 0) { KeySym keysym_list[xNumcodes]; for(int i=0;ired_mask) >> 16; pixels[pos+1] = (pixel & ximage->green_mask) >> 8; pixels[pos+2] = pixel & ximage->blue_mask; } } XDestroyImage(ximage); return pixels; }