add xevent c bindings.

This commit is contained in:
Miroslav Šedivý 2021-01-10 15:55:29 +01:00
parent a7d4511037
commit 0f19b6ed57
3 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,45 @@
#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;
}
XFixesSelectCursorInput(display, root, XFixesDisplayCursorNotifyMask);
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;
}
}
}
XCloseDisplay(display);
}

View File

@ -0,0 +1,55 @@
package xevent
/*
#cgo linux CFLAGS: -I/usr/src -I/usr/local/include/
#cgo linux LDFLAGS: -L/usr/src -L/usr/local/lib -lX11 -lxcb -lXfixes
#include "xevent.h"
*/
import "C"
import (
"unsafe"
"github.com/kataras/go-events"
)
var emmiter events.EventEmmiter
func init() {
emmiter = events.New()
}
func EventLoop(display string) {
displayUnsafe := C.CString(display)
defer C.free(unsafe.Pointer(displayUnsafe))
C.XEventLoop(displayUnsafe)
}
func OnCursorChanged(listener func(serial uint64)) {
emmiter.On("cursor-changed", func(payload ...interface{}) {
listener(payload[0].(uint64))
})
}
func OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) {
emmiter.On("event-error", func(payload ...interface{}) {
listener(payload[0].(uint8), payload[1].(string), payload[2].(uint8), payload[3].(uint8))
})
}
//export goXEventCursorChanged
func goXEventCursorChanged(event C.XFixesCursorNotifyEvent) {
emmiter.Emit("cursor-changed", uint64(event.cursor_serial))
}
//export goXEventError
func goXEventError(event *C.XErrorEvent, message *C.char) {
emmiter.Emit("event-error", uint8(event.error_code), C.GoString(message), uint8(event.request_code), uint8(event.minor_code))
}
//export goXEventActive
func goXEventActive() C.int {
return C.int(1)
}

View File

@ -0,0 +1,15 @@
#pragma once
#include <X11/Xlib.h>
#include <X11/extensions/Xfixes.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
extern void goXEventCursorChanged(XFixesCursorNotifyEvent event);
extern void goXEventError(XErrorEvent *event, char *message);
extern int goXEventActive();
static int XEventError(Display *display, XErrorEvent *event);
void XEventLoop(char *display);