neko/server/internal/xorg/xorg.c

158 lines
3.6 KiB
C
Raw Normal View History

2020-02-11 18:15:59 +13:00
#include "xorg.h"
2020-01-19 12:30:09 +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
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) {
2020-01-19 12:30:09 +13:00
fputs("Could not open main display\n", stderr);
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
}
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
}
2020-01-26 03:29:52 +13:00
void XKey(unsigned long key, int down) {
2020-06-14 02:21:11 +12:00
if (key != 0) {
Display *display = getXDisplay();
KeyCode code = XKeysymToKeycode(display, key);
2020-06-13 23:29:16 +12:00
2020-06-14 02:21:11 +12:00
if (code != 0) {
XTestFakeKeyEvent(display, code, down, CurrentTime);
XSync(display, 0);
}
2020-06-13 23:29:16 +12:00
}
2020-01-19 12:30:09 +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);
}