neko/internal/desktop/xevent/xevent.c

90 lines
2.4 KiB
C
Raw Normal View History

2021-01-11 03:55:29 +13:00
#include "xevent.h"
static int XEventError(Display *display, XErrorEvent *event) {
char message[100];
int error;
error = XGetErrorText(display, event->error_code, message, sizeof(message));
if (error) {
goXEventError(event, "Could not get error message.");
} else {
goXEventError(event, message);
}
return 1;
}
void XEventLoop(char *name) {
Display *display = XOpenDisplay(name);
Window root = RootWindow(display, 0);
int xfixes_event_base, xfixes_error_base;
if (!XFixesQueryExtension(display, &xfixes_event_base, &xfixes_error_base)) {
return;
}
2021-01-19 09:40:57 +13:00
Atom WM_WINDOW_ROLE = XInternAtom(display, "WM_WINDOW_ROLE", 1);
Atom XA_CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0);
2021-01-12 03:30:53 +13:00
XFixesSelectSelectionInput(display, root, XA_CLIPBOARD, XFixesSetSelectionOwnerNotifyMask);
2021-01-11 03:55:29 +13:00
XFixesSelectCursorInput(display, root, XFixesDisplayCursorNotifyMask);
2021-01-12 12:09:43 +13:00
XSelectInput(display, root, SubstructureNotifyMask);
2021-01-12 03:30:53 +13:00
2021-01-11 03:55:29 +13:00
XSync(display, 0);
XSetErrorHandler(XEventError);
while (goXEventActive()) {
XEvent event;
XNextEvent(display, &event);
// XFixesDisplayCursorNotify
if (event.type == xfixes_event_base + 1) {
XFixesCursorNotifyEvent notifyEvent = *((XFixesCursorNotifyEvent *) &event);
if (notifyEvent.subtype == XFixesDisplayCursorNotify) {
goXEventCursorChanged(notifyEvent);
continue;
}
}
2021-01-12 03:30:53 +13:00
// XFixesSelectionNotifyEvent
if (event.type == xfixes_event_base + XFixesSelectionNotify) {
XFixesSelectionNotifyEvent notifyEvent = *((XFixesSelectionNotifyEvent *) &event);
if (notifyEvent.subtype == XFixesSetSelectionOwnerNotify && notifyEvent.selection == XA_CLIPBOARD) {
goXEventClipboardUpdated();
continue;
}
}
2021-01-12 12:09:43 +13:00
2021-01-19 09:40:57 +13:00
// CreateNotify
2021-01-12 12:09:43 +13:00
if (event.type == CreateNotify) {
2021-01-19 09:40:57 +13:00
Window window = event.xcreatewindow.window;
char *name;
XFetchName(display, window, &name);
XTextProperty role;
XGetTextProperty(display, window, &role, WM_WINDOW_ROLE);
goXEventWindowCreated(window, name, role.value);
XFree(name);
continue;
}
// ConfigureNotify
if (event.type == ConfigureNotify) {
Window window = event.xconfigure.window;
2021-01-12 12:09:43 +13:00
char *name;
2021-01-19 09:40:57 +13:00
XFetchName(display, window, &name);
2021-01-12 12:09:43 +13:00
2021-01-19 09:40:57 +13:00
XTextProperty role;
XGetTextProperty(display, window, &role, WM_WINDOW_ROLE);
2021-01-12 12:09:43 +13:00
2021-01-19 09:40:57 +13:00
goXEventWindowConfigured(window, name, role.value);
2021-01-12 12:09:43 +13:00
XFree(name);
continue;
}
2021-01-11 03:55:29 +13:00
}
XCloseDisplay(display);
}