2020-02-11 18:15:59 +13:00
|
|
|
#include "xorg.h"
|
2020-01-19 12:30:09 +13:00
|
|
|
|
2020-02-04 03:49:27 +13:00
|
|
|
static clipboard_c *CLIPBOARD = NULL;
|
2020-01-26 03:29:52 +13:00
|
|
|
static Display *DISPLAY = NULL;
|
|
|
|
static char *NAME = ":0.0";
|
|
|
|
static int REGISTERED = 0;
|
|
|
|
static int DIRTY = 0;
|
2020-01-19 12:30:09 +13:00
|
|
|
|
2021-04-13 05:30:19 +12:00
|
|
|
xkeys_t *xKeysHead = NULL;
|
2021-04-11 20:35:54 +12:00
|
|
|
|
2021-04-13 05:38:13 +12:00
|
|
|
void XKeysInsert(KeySym keysym, KeyCode keycode) {
|
2021-04-13 05:41:36 +12:00
|
|
|
xkeys_t *node = (xkeys_t *) malloc(sizeof(xkeys_t));
|
2021-04-11 20:35:54 +12:00
|
|
|
|
2021-04-13 05:41:36 +12:00
|
|
|
node->keysym = keysym;
|
|
|
|
node->keycode = keycode;
|
|
|
|
node->next = xKeysHead;
|
|
|
|
xKeysHead = node;
|
2021-04-11 20:35:54 +12:00
|
|
|
}
|
|
|
|
|
2021-04-13 05:38:13 +12:00
|
|
|
KeyCode XKeysPop(KeySym keysym) {
|
|
|
|
KeyCode keycode = 0;
|
2021-04-13 05:41:36 +12:00
|
|
|
xkeys_t *node = xKeysHead,
|
|
|
|
*previous = NULL;
|
2021-04-13 02:16:57 +12:00
|
|
|
int i = 0;
|
2021-04-11 20:35:54 +12:00
|
|
|
|
2021-04-13 05:41:36 +12:00
|
|
|
while (node) {
|
|
|
|
if (node->keysym == keysym) {
|
|
|
|
keycode = node->keycode;
|
2021-04-13 05:38:13 +12:00
|
|
|
|
2021-04-11 22:25:02 +12:00
|
|
|
if (!previous)
|
2021-04-13 05:41:36 +12:00
|
|
|
xKeysHead = node->next;
|
2021-04-11 20:35:54 +12:00
|
|
|
else
|
2021-04-13 05:41:36 +12:00
|
|
|
previous->next = node->next;
|
2021-04-11 20:35:54 +12:00
|
|
|
|
2021-04-13 05:41:36 +12:00
|
|
|
free(node);
|
2021-04-13 05:38:13 +12:00
|
|
|
return keycode;
|
2021-04-11 20:35:54 +12:00
|
|
|
}
|
|
|
|
|
2021-04-13 05:41:36 +12:00
|
|
|
previous = node;
|
|
|
|
node = node->next;
|
2021-04-13 02:16:57 +12:00
|
|
|
if (i++ > 120) {
|
2021-04-13 05:42:58 +12:00
|
|
|
// this should NEVER HAPPEN
|
|
|
|
fprintf(stderr, "[FATAL-ERROR] XKeysPop() in xorg.c: reached maximum loop limit! Something is wrong\n");
|
2021-04-13 02:16:57 +12:00
|
|
|
break;
|
|
|
|
}
|
2021-04-11 20:35:54 +12:00
|
|
|
}
|
|
|
|
|
2021-04-13 05:38:13 +12:00
|
|
|
return 0;
|
2021-04-11 20:35:54 +12:00
|
|
|
}
|
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
Display *getXDisplay(void) {
|
|
|
|
/* Close the display if displayName has changed */
|
2020-01-26 03:29:52 +13:00
|
|
|
if (DIRTY) {
|
2020-02-11 18:15:59 +13:00
|
|
|
XDisplayClose();
|
2020-01-26 03:29:52 +13:00
|
|
|
DIRTY = 0;
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-01-26 03:29:52 +13:00
|
|
|
if (DISPLAY == NULL) {
|
2020-01-19 12:30:09 +13:00
|
|
|
/* First try the user set displayName */
|
2020-01-26 03:29:52 +13:00
|
|
|
DISPLAY = XOpenDisplay(NAME);
|
2020-01-19 12:30:09 +13:00
|
|
|
|
|
|
|
/* Then try using environment variable DISPLAY */
|
2020-01-26 03:29:52 +13:00
|
|
|
if (DISPLAY == NULL) {
|
|
|
|
DISPLAY = XOpenDisplay(NULL);
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-01-26 03:29:52 +13:00
|
|
|
if (DISPLAY == NULL) {
|
2021-04-13 05:42:58 +12:00
|
|
|
fprintf(stderr, "[FATAL-ERROR] XKeysPop() in xorg.c: Could not open main display!");
|
2020-01-26 03:29:52 +13:00
|
|
|
} else if (!REGISTERED) {
|
2020-02-11 18:15:59 +13:00
|
|
|
atexit(&XDisplayClose);
|
2020-01-26 03:29:52 +13:00
|
|
|
REGISTERED = 1;
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 03:29:52 +13:00
|
|
|
return DISPLAY;
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-02-04 03:49:27 +13:00
|
|
|
clipboard_c *getClipboard(void) {
|
|
|
|
if (CLIPBOARD == NULL) {
|
|
|
|
CLIPBOARD = clipboard_new(NULL);
|
|
|
|
}
|
|
|
|
return CLIPBOARD;
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:15:59 +13:00
|
|
|
void XDisplayClose(void) {
|
2020-01-26 03:29:52 +13:00
|
|
|
if (DISPLAY != NULL) {
|
|
|
|
XCloseDisplay(DISPLAY);
|
|
|
|
DISPLAY = NULL;
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:15:59 +13:00
|
|
|
void XDisplaySet(char *input) {
|
2020-01-26 03:29:52 +13:00
|
|
|
NAME = strdup(input);
|
|
|
|
DIRTY = 1;
|
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();
|
|
|
|
XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, x, y);
|
|
|
|
XSync(display, 0);
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-06-14 02:21:11 +12:00
|
|
|
if (button != 0) {
|
|
|
|
Display *display = getXDisplay();
|
|
|
|
XTestFakeButtonEvent(display, button, down, CurrentTime);
|
|
|
|
XSync(display, 0);
|
|
|
|
}
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2021-04-13 05:43:46 +12:00
|
|
|
// From: https://github.com/TigerVNC/tigervnc/blob/0946e298075f8f7b6d63e552297a787c5f84d27c/unix/x0vncserver/XDesktop.cxx#L343-L379
|
2021-04-13 02:16:57 +12:00
|
|
|
KeyCode XkbKeysymToKeycode(Display *dpy, KeySym keysym) {
|
2021-04-10 23:43:04 +12:00
|
|
|
XkbDescPtr xkb;
|
|
|
|
XkbStateRec state;
|
2021-04-13 02:16:57 +12:00
|
|
|
unsigned int mods;
|
2021-04-10 23:43:04 +12:00
|
|
|
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);
|
2021-04-10 23:43:04 +12:00
|
|
|
|
|
|
|
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);
|
2021-04-10 23:43:04 +12:00
|
|
|
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);
|
|
|
|
|
2021-04-10 23:43:04 +12:00
|
|
|
return keycode;
|
|
|
|
}
|
|
|
|
|
2021-04-13 05:22:59 +12:00
|
|
|
void XKey(KeySym key, int down) {
|
2021-04-11 11:17:05 +12:00
|
|
|
Display *display = getXDisplay();
|
2021-04-11 11:48:23 +12:00
|
|
|
KeyCode code = 0;
|
2021-04-11 20:35:54 +12:00
|
|
|
|
2021-04-13 05:38:13 +12:00
|
|
|
if (!down)
|
|
|
|
code = XKeysPop(key);
|
2021-04-11 20:35:54 +12:00
|
|
|
|
2021-04-13 05:38:13 +12:00
|
|
|
if (!code)
|
|
|
|
code = XkbKeysymToKeycode(display, key);
|
2021-04-11 11:17:05 +12:00
|
|
|
|
2021-04-11 11:33:58 +12:00
|
|
|
if (!code) {
|
|
|
|
int min, max, numcodes;
|
|
|
|
XDisplayKeycodes(display, &min, &max);
|
|
|
|
XGetKeyboardMapping(display, min, max-min, &numcodes);
|
|
|
|
|
|
|
|
code = (max-min+1)*numcodes;
|
|
|
|
KeySym keysym_list[numcodes];
|
2021-04-11 22:25:02 +12:00
|
|
|
for (int i=0;i<numcodes;i++) keysym_list[i] = key;
|
|
|
|
XChangeKeyboardMapping(display, code, numcodes, keysym_list, 1);
|
2021-04-11 11:22:37 +12:00
|
|
|
}
|
2021-04-11 22:25:02 +12:00
|
|
|
|
2021-04-11 11:33:58 +12:00
|
|
|
if (!code)
|
2021-04-11 11:48:23 +12:00
|
|
|
return;
|
2021-04-11 20:35:54 +12:00
|
|
|
|
2021-04-11 22:25:02 +12:00
|
|
|
if (down)
|
2021-04-13 05:38:13 +12:00
|
|
|
XKeysInsert(key, code);
|
2021-04-11 22:25:02 +12:00
|
|
|
|
2021-04-11 11:33:58 +12:00
|
|
|
XTestFakeKeyEvent(display, code, down, CurrentTime);
|
|
|
|
XSync(display, 0);
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
2020-02-04 03:49:27 +13:00
|
|
|
|
|
|
|
void XClipboardSet(char *src) {
|
|
|
|
clipboard_c *cb = getClipboard();
|
|
|
|
clipboard_set_text_ex(cb, src, strlen(src), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *XClipboardGet() {
|
|
|
|
clipboard_c *cb = getClipboard();
|
|
|
|
return clipboard_text_ex(cb, NULL, 0);
|
|
|
|
}
|
2020-02-11 18:15:59 +13:00
|
|
|
|
|
|
|
void XGetScreenConfigurations() {
|
|
|
|
Display *display = getXDisplay();
|
|
|
|
Window root = RootWindow(display, 0);
|
|
|
|
XRRScreenSize *xrrs;
|
|
|
|
int num_sizes;
|
|
|
|
|
|
|
|
xrrs = XRRSizes(display, 0, &num_sizes);
|
|
|
|
for(int i = 0; i < num_sizes; i ++) {
|
|
|
|
short *rates;
|
|
|
|
int num_rates;
|
|
|
|
|
|
|
|
goCreateScreenSize(i, xrrs[i].width, xrrs[i].height, xrrs[i].mwidth, xrrs[i].mheight);
|
|
|
|
rates = XRRRates(display, 0, i, &num_rates);
|
|
|
|
for (int j = 0; j < num_rates; j ++) {
|
|
|
|
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();
|
|
|
|
XRRScreenConfiguration *conf = XRRGetScreenInfo(display, RootWindow(display, 0));
|
|
|
|
Rotation original_rotation;
|
|
|
|
return XRRConfigCurrentConfiguration(conf, &original_rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
short XGetScreenRate() {
|
|
|
|
Display *display = getXDisplay();
|
|
|
|
XRRScreenConfiguration *conf = XRRGetScreenInfo(display, RootWindow(display, 0));
|
|
|
|
return XRRConfigCurrentRate(conf);
|
|
|
|
}
|
2020-06-16 09:14:23 +12:00
|
|
|
|
2020-06-20 12:15:38 +12:00
|
|
|
void SetKeyboardModifiers(int num_lock, int caps_lock, int scroll_lock) {
|
|
|
|
Display *display = getXDisplay();
|
2020-06-21 13:01:59 +12:00
|
|
|
|
|
|
|
if (num_lock != -1) {
|
|
|
|
XkbLockModifiers(display, XkbUseCoreKbd, 16, num_lock * 16);
|
2020-06-20 12:15:38 +12:00
|
|
|
}
|
|
|
|
|
2020-06-21 13:01:59 +12:00
|
|
|
if (caps_lock != -1) {
|
|
|
|
XkbLockModifiers(display, XkbUseCoreKbd, 2, caps_lock * 2);
|
2020-06-20 12:15:38 +12:00
|
|
|
}
|
|
|
|
|
2020-06-21 13:01:59 +12:00
|
|
|
if (scroll_lock != -1) {
|
|
|
|
XKeyboardControl values;
|
|
|
|
values.led_mode = scroll_lock ? LedModeOn : LedModeOff;
|
|
|
|
values.led = 3;
|
|
|
|
XChangeKeyboardControl(display, KBLedMode, &values);
|
2020-06-20 12:15:38 +12:00
|
|
|
}
|
2020-06-21 13:01:59 +12:00
|
|
|
|
|
|
|
XFlush(display);
|
2020-06-20 12:15:38 +12:00
|
|
|
}
|