2020-10-23 03:54:50 +13:00
|
|
|
#include "xorg.h"
|
|
|
|
|
|
|
|
static Display *DISPLAY = NULL;
|
|
|
|
|
|
|
|
Display *getXDisplay(void) {
|
|
|
|
return DISPLAY;
|
|
|
|
}
|
|
|
|
|
2020-11-04 12:09:52 +13:00
|
|
|
int XDisplayOpen(char *name) {
|
|
|
|
DISPLAY = XOpenDisplay(name);
|
|
|
|
return DISPLAY == NULL;
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-04 12:09:52 +13:00
|
|
|
void XDisplayClose(void) {
|
|
|
|
XCloseDisplay(DISPLAY);
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
void XMove(int x, int y) {
|
|
|
|
Display *display = getXDisplay();
|
2020-10-30 06:07:04 +13:00
|
|
|
XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, x, y);
|
2020-10-23 03:54:50 +13:00
|
|
|
XSync(display, 0);
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:55:11 +13: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-10-23 03:54:50 +13:00
|
|
|
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) {
|
2021-04-12 05:06:56 +12:00
|
|
|
if (button == 0)
|
|
|
|
return;
|
2021-01-18 07:10:49 +13:00
|
|
|
|
|
|
|
Display *display = getXDisplay();
|
|
|
|
XTestFakeButtonEvent(display, button, down, CurrentTime);
|
|
|
|
XSync(display, 0);
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-04-12 02:48:59 +12:00
|
|
|
static xkeyentry_t *xKeysHead = NULL;
|
2021-04-10 11:10:14 +12:00
|
|
|
|
|
|
|
void XKeyEntryAdd(KeySym keysym, KeyCode keycode) {
|
|
|
|
xkeyentry_t *entry = (xkeyentry_t *) malloc(sizeof(xkeyentry_t));
|
2021-04-12 05:06:56 +12:00
|
|
|
if (entry == NULL)
|
2021-04-10 11:10:14 +12:00
|
|
|
return;
|
|
|
|
|
|
|
|
entry->keysym = keysym;
|
|
|
|
entry->keycode = keycode;
|
2021-04-12 02:48:59 +12:00
|
|
|
entry->next = xKeysHead;
|
|
|
|
xKeysHead = entry;
|
2021-04-10 11:10:14 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
KeyCode XKeyEntryGet(KeySym keysym) {
|
|
|
|
xkeyentry_t *prev = NULL;
|
2021-04-12 02:48:59 +12:00
|
|
|
xkeyentry_t *curr = xKeysHead;
|
2021-04-10 11:10:14 +12:00
|
|
|
|
|
|
|
KeyCode keycode = 0;
|
|
|
|
while (curr != NULL) {
|
|
|
|
if (curr->keysym == keysym) {
|
|
|
|
keycode = curr->keycode;
|
|
|
|
|
|
|
|
if (prev == NULL) {
|
2021-04-12 02:48:59 +12:00
|
|
|
xKeysHead = curr->next;
|
2021-04-10 11:10:14 +12:00
|
|
|
} 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
|
2021-04-13 01:25:10 +12:00
|
|
|
KeyCode XkbKeysymToKeycode(Display* dpy, KeySym keysym) {
|
2021-04-10 11:10:14 +12:00
|
|
|
XkbDescPtr xkb;
|
|
|
|
XkbStateRec state;
|
|
|
|
unsigned int mods;
|
2021-04-13 01:25:10 +12:00
|
|
|
unsigned keycode;
|
2021-04-10 11:10:14 +12:00
|
|
|
|
|
|
|
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))
|
2021-04-13 01:25:10 +12:00
|
|
|
return XkbKeysymToKeycode(dpy, XK_ISO_Left_Tab);
|
2021-04-10 11:10:14 +12:00
|
|
|
|
|
|
|
return keycode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XKey(KeySym keysym, int down) {
|
|
|
|
if (keysym == 0)
|
|
|
|
return;
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-01-18 07:10:49 +13:00
|
|
|
Display *display = getXDisplay();
|
2021-04-10 11:10:14 +12:00
|
|
|
KeyCode keycode = 0;
|
|
|
|
|
|
|
|
if (!down)
|
|
|
|
keycode = XKeyEntryGet(keysym);
|
|
|
|
|
|
|
|
if (keycode == 0)
|
2021-04-13 01:25:10 +12:00
|
|
|
keycode = XkbKeysymToKeycode(display, keysym);
|
2021-01-18 07:10:49 +13:00
|
|
|
|
|
|
|
// Map non-existing keysyms to new keycodes
|
2021-04-10 11:10:14 +12:00
|
|
|
if (keycode == 0) {
|
2021-01-18 07:10:49 +13:00
|
|
|
int min, max, numcodes;
|
|
|
|
XDisplayKeycodes(display, &min, &max);
|
|
|
|
XGetKeyboardMapping(display, min, max-min, &numcodes);
|
|
|
|
|
2021-04-10 11:10:14 +12:00
|
|
|
keycode = (max-min+1)*numcodes;
|
2021-01-18 07:10:49 +13:00
|
|
|
KeySym keysym_list[numcodes];
|
2021-04-10 11:10:14 +12:00
|
|
|
for(int i=0;i<numcodes;i++) keysym_list[i] = keysym;
|
|
|
|
XChangeKeyboardMapping(display, keycode, numcodes, keysym_list, 1);
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2021-01-18 07:10:49 +13:00
|
|
|
|
2021-04-10 11:10:14 +12:00
|
|
|
if (down)
|
|
|
|
XKeyEntryAdd(keysym, keycode);
|
|
|
|
|
|
|
|
XTestFakeKeyEvent(display, keycode, down, CurrentTime);
|
2021-01-18 07:10:49 +13:00
|
|
|
XSync(display, 0);
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
void XGetScreenConfigurations() {
|
2020-10-30 06:07:04 +13:00
|
|
|
Display *display = getXDisplay();
|
|
|
|
Window root = RootWindow(display, 0);
|
2020-10-23 03:54:50 +13:00
|
|
|
XRRScreenSize *xrrs;
|
2020-10-30 06:07:04 +13:00
|
|
|
int num_sizes;
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
xrrs = XRRSizes(display, 0, &num_sizes);
|
2020-10-30 06:07:04 +13:00
|
|
|
for (int i = 0; i < num_sizes; i++) {
|
2020-10-23 03:54:50 +13:00
|
|
|
short *rates;
|
2020-10-30 06:07:04 +13:00
|
|
|
int num_rates;
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
goCreateScreenSize(i, xrrs[i].width, xrrs[i].height, xrrs[i].mwidth, xrrs[i].mheight);
|
|
|
|
rates = XRRRates(display, 0, i, &num_rates);
|
2020-10-30 06:07:04 +13:00
|
|
|
for (int j = 0; j < num_rates; j++) {
|
2020-10-23 03:54:50 +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();
|
2020-10-30 06:07:04 +13:00
|
|
|
XRRScreenConfiguration *conf = XRRGetScreenInfo(display, RootWindow(display, 0));
|
2020-10-23 03:54:50 +13:00
|
|
|
Rotation original_rotation;
|
|
|
|
return XRRConfigCurrentConfiguration(conf, &original_rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
short XGetScreenRate() {
|
|
|
|
Display *display = getXDisplay();
|
2020-10-30 06:07:04 +13:00
|
|
|
XRRScreenConfiguration *conf = XRRGetScreenInfo(display, RootWindow(display, 0));
|
2020-10-23 03:54:50 +13:00
|
|
|
return XRRConfigCurrentRate(conf);
|
|
|
|
}
|
|
|
|
|
2021-01-13 10:54:13 +13:00
|
|
|
void XSetKeyboardModifier(int mod, int on) {
|
2020-10-23 03:54:50 +13:00
|
|
|
Display *display = getXDisplay();
|
2021-01-13 10:54:13 +13:00
|
|
|
XkbLockModifiers(display, XkbUseCoreKbd, mod, on ? mod : 0);
|
2020-10-23 03:54:50 +13:00
|
|
|
XFlush(display);
|
|
|
|
}
|
2021-01-10 10:58:18 +13:00
|
|
|
|
2021-01-13 11:35:11 +13:00
|
|
|
char XGetKeyboardModifiers() {
|
2021-01-13 10:54:13 +13:00
|
|
|
XkbStateRec xkbState;
|
|
|
|
Display *display = getXDisplay();
|
|
|
|
XkbGetState(display, XkbUseCoreKbd, &xkbState);
|
2021-01-13 11:35:11 +13:00
|
|
|
return xkbState.locked_mods;
|
2021-01-13 10:54:13 +13:00
|
|
|
}
|
|
|
|
|
2021-01-10 10:58:18 +13:00
|
|
|
XFixesCursorImage *XGetCursorImage(void) {
|
|
|
|
Display *display = getXDisplay();
|
|
|
|
return XFixesGetCursorImage(display);
|
|
|
|
}
|
2021-01-22 08:44:09 +13:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
*w = width;
|
|
|
|
*h = height;
|
|
|
|
char *pixels = (char *)malloc(width * height * 3);
|
|
|
|
|
2022-09-14 08:21:08 +12:00
|
|
|
for (int row = 0; row < height; row++) {
|
|
|
|
for (int col = 0; col < width; col++) {
|
|
|
|
int pos = ((row * width) + col) * 3;
|
2021-01-22 08:44:09 +13:00
|
|
|
unsigned long pixel = XGetPixel(ximage, col, row);
|
|
|
|
|
2022-09-14 08:21:08 +12:00
|
|
|
pixels[pos] = (pixel & ximage->red_mask) >> 16;
|
|
|
|
pixels[pos+1] = (pixel & ximage->green_mask) >> 8;
|
|
|
|
pixels[pos+2] = pixel & ximage->blue_mask;
|
2021-01-22 08:44:09 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XDestroyImage(ximage);
|
|
|
|
return pixels;
|
|
|
|
}
|