neko/server/internal/desktop/xorg/xorg.c

317 lines
7.6 KiB
C
Raw Permalink Normal View History

2020-02-11 18:15:59 +13:00
#include "xorg.h"
2020-01-19 12:30:09 +13:00
2020-01-26 03:29:52 +13:00
static Display *DISPLAY = NULL;
2021-04-11 20:35:54 +12:00
2020-01-19 12:30:09 +13:00
Display *getXDisplay(void) {
2020-01-26 03:29:52 +13:00
return DISPLAY;
2020-01-19 12:30:09 +13:00
}
2022-09-14 07:40:40 +12:00
int XDisplayOpen(char *name) {
DISPLAY = XOpenDisplay(name);
return DISPLAY == NULL;
2020-01-19 12:30:09 +13:00
}
2022-09-14 07:40:40 +12:00
void XDisplayClose(void) {
XCloseDisplay(DISPLAY);
2020-01-19 12:30:09 +13:00
}
2020-01-26 03:29:52 +13:00
void XMove(int x, int y) {
2020-01-19 12:30:09 +13:00
Display *display = getXDisplay();
2022-09-14 07:40:40 +12:00
XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, x, y);
2020-01-19 12:30:09 +13:00
XSync(display, 0);
}
2022-09-14 07:40:40 +12:00
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);
}
2020-01-26 03:29:52 +13:00
void XScroll(int x, int y) {
2020-01-19 12:30:09 +13:00
int ydir = 4; /* Button 4 is up, 5 is down. */
int xdir = 6;
Display *display = getXDisplay();
if (y < 0) {
ydir = 5;
}
2020-01-26 03:29:52 +13:00
2020-01-19 12:30:09 +13:00
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);
}
2020-01-26 03:29:52 +13:00
void XButton(unsigned int button, int down) {
2022-09-14 07:40:40 +12:00
if (button == 0)
return;
Display *display = getXDisplay();
XTestFakeButtonEvent(display, button, down, CurrentTime);
XSync(display, 0);
}
static xkeyentry_t *xKeysHead = NULL;
2023-01-15 09:15:52 +13:00
// add keycode->keysym mapping to list
2022-09-14 07:40:40 +12:00
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;
}
2023-01-15 09:15:52 +13:00
// get keycode for keysym from list
2022-09-14 07:40:40 +12:00
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;
2020-06-14 02:21:11 +12:00
}
2022-09-14 07:40:40 +12:00
return 0;
2020-01-19 12:30:09 +13:00
}
2022-09-14 07:40:40 +12:00
// From https://github.com/TigerVNC/tigervnc/blob/0946e298075f8f7b6d63e552297a787c5f84d27c/unix/x0vncserver/XDesktop.cxx#L343-L379
KeyCode XkbKeysymToKeycode(Display* dpy, KeySym keysym) {
XkbDescPtr xkb;
XkbStateRec state;
2021-04-13 02:16:57 +12:00
unsigned int mods;
unsigned keycode;
xkb = XkbGetMap(dpy, XkbAllComponentsMask, XkbUseCoreKbd);
if (!xkb)
return 0;
XkbGetState(dpy, XkbUseCoreKbd, &state);
2021-04-13 02:16:57 +12:00
// 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;
2021-04-13 02:16:57 +12:00
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);
2021-04-13 02:16:57 +12:00
// 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;
}
2023-01-16 04:23:48 +13:00
// From https://github.com/TigerVNC/tigervnc/blob/a434ef3377943e89165ac13c537cd0f28be97f84/unix/x0vncserver/XDesktop.cxx#L401-L453
KeyCode XkbAddKeyKeysym(Display* dpy, KeySym keysym) {
int types[1];
unsigned int key;
XkbDescPtr xkb;
XkbMapChangesRec changes;
KeySym *syms;
KeySym upper, lower;
2023-01-15 09:15:52 +13:00
2023-01-16 04:23:48 +13:00
xkb = XkbGetMap(dpy, XkbAllComponentsMask, XkbUseCoreKbd);
2023-01-15 09:15:52 +13:00
2023-01-16 04:23:48 +13:00
if (!xkb)
return 0;
2023-01-15 09:15:52 +13:00
2023-01-16 04:23:48 +13:00
for (key = xkb->max_key_code; key >= xkb->min_key_code; key--) {
if (XkbKeyNumGroups(xkb, key) == 0)
break;
2023-01-15 09:15:52 +13:00
}
2023-01-16 04:23:48 +13:00
// no free keycodes
if (key < xkb->min_key_code)
return 0;
2023-01-15 09:15:52 +13:00
2023-01-16 04:23:48 +13:00
// assign empty structure
changes = *(XkbMapChangesRec *) malloc(sizeof(XkbMapChangesRec));
for (int i = 0; i < sizeof(changes); i++) ((char *) &changes)[i] = 0;
2023-01-15 09:15:52 +13:00
2023-01-16 04:23:48 +13:00
XConvertCase(keysym, &lower, &upper);
2023-01-15 09:15:52 +13:00
2023-01-16 04:23:48 +13:00
if (upper == lower)
types[XkbGroup1Index] = XkbOneLevelIndex;
else
types[XkbGroup1Index] = XkbAlphabeticIndex;
2023-01-15 09:15:52 +13:00
2023-01-16 04:23:48 +13:00
XkbChangeTypesOfKey(xkb, key, 1, XkbGroup1Mask, types, &changes);
2023-01-15 09:15:52 +13:00
2023-01-16 04:23:48 +13:00
syms = XkbKeySymsPtr(xkb,key);
if (upper == lower)
syms[0] = keysym;
else {
syms[0] = lower;
syms[1] = upper;
2023-01-15 09:15:52 +13:00
}
2023-01-16 04:23:48 +13:00
changes.changed |= XkbKeySymsMask;
changes.first_key_sym = key;
changes.num_key_syms = 1;
2023-01-15 09:15:52 +13:00
2023-01-16 04:23:48 +13:00
if (XkbChangeMap(dpy, xkb, &changes)) {
return key;
2023-01-15 09:15:52 +13:00
}
2023-01-16 04:23:48 +13:00
return 0;
2023-01-15 09:15:52 +13:00
}
2022-09-14 07:40:40 +12:00
void XKey(KeySym keysym, int down) {
if (keysym == 0)
return;
Display *display = getXDisplay();
2022-09-14 07:40:40 +12:00
KeyCode keycode = 0;
2021-04-11 20:35:54 +12:00
2021-04-13 05:38:13 +12:00
if (!down)
2022-09-14 07:40:40 +12:00
keycode = XKeyEntryGet(keysym);
2021-04-11 20:35:54 +12:00
2023-01-15 09:15:52 +13:00
// Try to get keysyms from existing keycodes
2022-09-14 07:40:40 +12:00
if (keycode == 0)
keycode = XkbKeysymToKeycode(display, keysym);
2022-09-14 07:40:40 +12:00
// Map non-existing keysyms to new keycodes
2023-01-15 09:15:52 +13:00
if (keycode == 0)
2023-01-16 04:23:48 +13:00
keycode = XkbAddKeyKeysym(display, keysym);
2021-04-11 22:25:02 +12:00
if (down)
2022-09-14 07:40:40 +12:00
XKeyEntryAdd(keysym, keycode);
2021-04-11 22:25:02 +12:00
2022-09-14 07:40:40 +12:00
XTestFakeKeyEvent(display, keycode, down, CurrentTime);
2021-04-11 11:33:58 +12:00
XSync(display, 0);
2020-01-19 12:30:09 +13:00
}
2020-02-11 18:15:59 +13:00
void XGetScreenConfigurations() {
2022-09-14 07:40:40 +12:00
Display *display = getXDisplay();
Window root = RootWindow(display, 0);
2020-02-11 18:15:59 +13:00
XRRScreenSize *xrrs;
2022-09-14 07:40:40 +12:00
int num_sizes;
2020-02-11 18:15:59 +13:00
xrrs = XRRSizes(display, 0, &num_sizes);
2022-09-14 07:40:40 +12:00
for (int i = 0; i < num_sizes; i++) {
2020-02-11 18:15:59 +13:00
short *rates;
2022-09-14 07:40:40 +12:00
int num_rates;
2020-02-11 18:15:59 +13:00
goCreateScreenSize(i, xrrs[i].width, xrrs[i].height, xrrs[i].mwidth, xrrs[i].mheight);
rates = XRRRates(display, 0, i, &num_rates);
2022-09-14 07:40:40 +12:00
for (int j = 0; j < num_rates; j++) {
2020-02-11 18:15:59 +13:00
goSetScreenRates(i, j, rates[j]);
}
}
}
void XSetScreenConfiguration(int index, short rate) {
Display *display = getXDisplay();
Window root = RootWindow(display, 0);
XRRSetScreenConfigAndRate(display, XRRGetScreenInfo(display, root), root, index, RR_Rotate_0, rate, CurrentTime);
}
int XGetScreenSize() {
Display *display = getXDisplay();
2022-09-14 07:40:40 +12:00
XRRScreenConfiguration *conf = XRRGetScreenInfo(display, RootWindow(display, 0));
2020-02-11 18:15:59 +13:00
Rotation original_rotation;
return XRRConfigCurrentConfiguration(conf, &original_rotation);
}
short XGetScreenRate() {
Display *display = getXDisplay();
2022-09-14 07:40:40 +12:00
XRRScreenConfiguration *conf = XRRGetScreenInfo(display, RootWindow(display, 0));
2020-02-11 18:15:59 +13:00
return XRRConfigCurrentRate(conf);
}
2020-06-16 09:14:23 +12:00
2022-09-14 07:40:40 +12:00
void XSetKeyboardModifier(int mod, int on) {
2020-06-20 12:15:38 +12:00
Display *display = getXDisplay();
2022-09-14 07:40:40 +12:00
XkbLockModifiers(display, XkbUseCoreKbd, mod, on ? mod : 0);
XFlush(display);
}
2020-06-21 13:01:59 +12:00
2022-09-14 07:40:40 +12:00
char XGetKeyboardModifiers() {
XkbStateRec xkbState;
Display *display = getXDisplay();
XkbGetState(display, XkbUseCoreKbd, &xkbState);
return xkbState.locked_mods;
}
2020-06-20 12:15:38 +12:00
2022-09-14 07:40:40 +12:00
XFixesCursorImage *XGetCursorImage(void) {
Display *display = getXDisplay();
return XFixesGetCursorImage(display);
}
char *XGetScreenshot(int *w, int *h) {
Display *display = getXDisplay();
Window root = DefaultRootWindow(display);
XWindowAttributes attr;
XGetWindowAttributes(display, root, &attr);
int width = attr.width;
int height = attr.height;
XImage *ximage = XGetImage(display, root, 0, 0, width, height, AllPlanes, ZPixmap);
2020-06-20 12:15:38 +12:00
2022-09-14 07:40:40 +12:00
*w = width;
*h = height;
char *pixels = (char *)malloc(width * height * 3);
2023-01-15 09:15:52 +13:00
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
int pos = ((row * width) + col) * 3;
2022-09-14 07:40:40 +12:00
unsigned long pixel = XGetPixel(ximage, col, row);
2023-01-15 09:15:52 +13:00
pixels[pos] = (pixel & ximage->red_mask) >> 16;
pixels[pos+1] = (pixel & ximage->green_mask) >> 8;
pixels[pos+2] = pixel & ximage->blue_mask;
2022-09-14 07:40:40 +12:00
}
2020-06-20 12:15:38 +12:00
}
2020-06-21 13:01:59 +12:00
2022-09-14 07:40:40 +12:00
XDestroyImage(ximage);
return pixels;
2020-06-20 12:15:38 +12:00
}