397 lines
13 KiB
C
397 lines
13 KiB
C
#define WRASTER_NO_XLIB /* Wayland-only file: no libX11 deps */
|
|
|
|
/*
|
|
* wevent_wl.c — Wayland event backend for WINGs.
|
|
*
|
|
* Events arrive via wlroots input callbacks (keyboard, pointer, touch) in
|
|
* wl_backend.c, which call W_WL_EnqueueEvent(). This backend dequeues them
|
|
* in WMNextEvent / WMMaskEvent without touching any Xlib function.
|
|
*
|
|
* For modal loops (e.g. dialog.c) a dispatch callback is registered via
|
|
* W_WL_SetLoopDispatch() so the compositor event loop can be ticked while
|
|
* waiting for the next WINGs event.
|
|
*/
|
|
|
|
#include "wconfig.h"
|
|
#include "WINGsP.h"
|
|
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
#define WL_EVENT_QUEUE_CAP 512
|
|
|
|
typedef struct {
|
|
WMEvent ev;
|
|
W_View *target; /* direct view routing; NULL = use ev.window */
|
|
} WLQEntry;
|
|
|
|
typedef struct {
|
|
WLQEntry buf[WL_EVENT_QUEUE_CAP];
|
|
int head; /* next read position */
|
|
int tail; /* next write position */
|
|
|
|
int loop_fd; /* wl_event_loop fd for W_HandleInputEvents */
|
|
|
|
void (*dispatch_fn)(int timeout_ms, void *ud);
|
|
void *dispatch_ud;
|
|
} WLEventCtx;
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
static void *
|
|
wl_screen_init(W_Screen *scr)
|
|
{
|
|
(void)scr;
|
|
WLEventCtx *ctx = wmalloc(sizeof(*ctx));
|
|
memset(ctx, 0, sizeof(*ctx));
|
|
ctx->loop_fd = -1;
|
|
return ctx;
|
|
}
|
|
|
|
static void
|
|
wl_screen_fini(void *ectx)
|
|
{
|
|
wfree(ectx);
|
|
}
|
|
|
|
static Bool
|
|
wl_pending(void *ectx)
|
|
{
|
|
WLEventCtx *ctx = ectx;
|
|
return ctx->head != ctx->tail;
|
|
}
|
|
|
|
static void
|
|
wl_fetch(void *ectx, WMEvent *ev)
|
|
{
|
|
WLEventCtx *ctx = ectx;
|
|
WLQEntry *e = &ctx->buf[ctx->head];
|
|
*ev = e->ev;
|
|
ev->_view_target = e->target;
|
|
if (e->target && !W_IsValidView(e->target))
|
|
ev->_view_target = NULL;
|
|
if (e->target) {
|
|
W_Screen *scr = e->target->screen;
|
|
if (scr)
|
|
scr->event_target = e->target;
|
|
}
|
|
ctx->head = (ctx->head + 1) % WL_EVENT_QUEUE_CAP;
|
|
}
|
|
|
|
/* Event mask table for matching — same values as wevent.c's wmeventMasks */
|
|
static unsigned long
|
|
wme_mask(int type)
|
|
{
|
|
switch (type) {
|
|
case WME_KEY_PRESS: return WM_EVMASK_KEY_PRESS;
|
|
case WME_KEY_RELEASE: return WM_EVMASK_KEY_RELEASE;
|
|
case WME_BUTTON_PRESS: return WM_EVMASK_BUTTON_PRESS;
|
|
case WME_BUTTON_RELEASE: return WM_EVMASK_BUTTON_RELEASE;
|
|
case WME_MOTION: return WM_EVMASK_POINTER_MOTION | WM_EVMASK_BUTTON_MOTION;
|
|
case WME_ENTER: return WM_EVMASK_ENTER_WINDOW;
|
|
case WME_LEAVE: return WM_EVMASK_LEAVE_WINDOW;
|
|
case WME_EXPOSE: return WM_EVMASK_EXPOSURE;
|
|
case WME_CONFIGURE: return WM_EVMASK_STRUCTURE_NOTIFY;
|
|
case WME_MAP: return WM_EVMASK_STRUCTURE_NOTIFY;
|
|
case WME_UNMAP: return WM_EVMASK_STRUCTURE_NOTIFY;
|
|
case WME_DESTROY: return WM_EVMASK_STRUCTURE_NOTIFY;
|
|
case WME_FOCUS_IN:
|
|
case WME_FOCUS_OUT: return WM_EVMASK_FOCUS_CHANGE;
|
|
case WME_PROPERTY: return WM_EVMASK_PROPERTY_CHANGE;
|
|
case WME_COLORMAP: return WM_EVMASK_COLORMAP_CHANGE;
|
|
case WME_VISIBILITY: return WM_EVMASK_VISIBILITY_CHANGE;
|
|
default: return 0;
|
|
}
|
|
}
|
|
|
|
static Bool
|
|
wl_check_mask(void *ectx, long mask, WMEvent *ev)
|
|
{
|
|
WLEventCtx *ctx = ectx;
|
|
int i = ctx->head;
|
|
while (i != ctx->tail) {
|
|
unsigned long emask = wme_mask(ctx->buf[i].ev.type);
|
|
if (emask & (unsigned long)mask) {
|
|
*ev = ctx->buf[i].ev;
|
|
if (ev->_view_target && !W_IsValidView(ev->_view_target))
|
|
ev->_view_target = NULL;
|
|
/* Remove entry by shifting */
|
|
int next = (i + 1) % WL_EVENT_QUEUE_CAP;
|
|
while (next != ctx->tail) {
|
|
ctx->buf[i] = ctx->buf[next];
|
|
i = next;
|
|
next = (next + 1) % WL_EVENT_QUEUE_CAP;
|
|
}
|
|
ctx->tail = i;
|
|
return True;
|
|
}
|
|
i = (i + 1) % WL_EVENT_QUEUE_CAP;
|
|
}
|
|
return False;
|
|
}
|
|
|
|
static Bool
|
|
wl_check_typed_window(void *ectx, WNativeWindow w, int type, WMEvent *ev)
|
|
{
|
|
WLEventCtx *ctx = ectx;
|
|
int i = ctx->head;
|
|
while (i != ctx->tail) {
|
|
if (ctx->buf[i].ev.type == type &&
|
|
ctx->buf[i].ev.window == w) {
|
|
*ev = ctx->buf[i].ev;
|
|
if (ev->_view_target && !W_IsValidView(ev->_view_target))
|
|
ev->_view_target = NULL;
|
|
int next = (i + 1) % WL_EVENT_QUEUE_CAP;
|
|
while (next != ctx->tail) {
|
|
ctx->buf[i] = ctx->buf[next];
|
|
i = next;
|
|
next = (next + 1) % WL_EVENT_QUEUE_CAP;
|
|
}
|
|
ctx->tail = i;
|
|
return True;
|
|
}
|
|
i = (i + 1) % WL_EVENT_QUEUE_CAP;
|
|
}
|
|
return False;
|
|
}
|
|
|
|
static void
|
|
wl_flush(void *ectx, Bool discard)
|
|
{
|
|
WLEventCtx *ctx = ectx;
|
|
(void)discard;
|
|
/* Pump the wlroots event loop so pointer/key events arrive in our queue.
|
|
* This is called from waitForEvent/WMMaskEvent when the queue is empty. */
|
|
if (ctx->dispatch_fn)
|
|
ctx->dispatch_fn(16, ctx->dispatch_ud);
|
|
}
|
|
|
|
static int
|
|
wl_fd(void *ectx)
|
|
{
|
|
WLEventCtx *ctx = ectx;
|
|
return ctx->loop_fd;
|
|
}
|
|
|
|
static void
|
|
wl_put_back(void *ectx, WMEvent *ev)
|
|
{
|
|
WLEventCtx *ctx = ectx;
|
|
int prev = (ctx->head - 1 + WL_EVENT_QUEUE_CAP) % WL_EVENT_QUEUE_CAP;
|
|
if (prev == ctx->tail)
|
|
return; /* queue full — drop */
|
|
ctx->buf[prev].ev = *ev;
|
|
ctx->buf[prev].target = NULL;
|
|
ctx->head = prev;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Stubs for new vtable methods (Wayland: not yet implemented) */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
static Bool wl_grab_keyboard(void *ectx, WNativeWindow w)
|
|
{ (void)ectx; (void)w; return False; }
|
|
|
|
static void wl_ungrab_keyboard(void *ectx) { (void)ectx; }
|
|
static void wl_allow_events(void *ectx) { (void)ectx; }
|
|
|
|
static Bool wl_grab_pointer(void *ectx, WNativeWindow w, WMNativePixmap c)
|
|
{ (void)ectx; (void)w; (void)c; return False; }
|
|
|
|
static void wl_ungrab_pointer(void *ectx) { (void)ectx; }
|
|
|
|
static WNativeWindow wl_query_pointer(void *ectx, WNativeWindow w,
|
|
int *rx, int *ry, int *wx, int *wy, unsigned int *m)
|
|
{ (void)ectx; (void)w;
|
|
if (rx) *rx = 0; if (ry) *ry = 0;
|
|
if (wx) *wx = 0; if (wy) *wy = 0; if (m) *m = 0;
|
|
return 0; }
|
|
|
|
static Bool wl_get_window_geometry(void *ectx, WNativeWindow w,
|
|
int *x, int *y, unsigned int *width, unsigned int *height)
|
|
{ (void)ectx; (void)w;
|
|
if (x) *x = 0; if (y) *y = 0;
|
|
if (width) *width = 0; if (height) *height = 0;
|
|
return False; }
|
|
|
|
static void wl_move_window(void *ectx, WNativeWindow w, int x, int y)
|
|
{ (void)ectx; (void)w; (void)x; (void)y; }
|
|
|
|
static WNativeWindow wl_translate_coordinates(void *ectx,
|
|
WNativeWindow src, WNativeWindow dst, int sx, int sy, int *dx, int *dy)
|
|
{ (void)ectx; (void)src; (void)dst; (void)sx; (void)sy;
|
|
if (dx) *dx = 0; if (dy) *dy = 0; return 0; }
|
|
|
|
static WNativeWindow wl_get_root_window(void *ectx)
|
|
{ (void)ectx; return 0; }
|
|
|
|
static Bool wl_query_tree(void *ectx, WNativeWindow w,
|
|
WNativeWindow **children, unsigned int *nc)
|
|
{ (void)ectx; (void)w;
|
|
if (children) *children = NULL; if (nc) *nc = 0; return False; }
|
|
|
|
static void wl_get_pointer_control(void *ectx, int *an, int *ad, int *t)
|
|
{ (void)ectx; if (an) *an = 2; if (ad) *ad = 1; if (t) *t = 4; }
|
|
|
|
static void wl_set_pointer_control(void *ectx, int an, int ad, int t)
|
|
{ (void)ectx; (void)an; (void)ad; (void)t; }
|
|
|
|
static int wl_modifier_from_key(void *ectx, const char *key)
|
|
{
|
|
(void)ectx;
|
|
/* Sensible defaults matching common Wayland compositors */
|
|
if (strcasecmp(key, "SHIFT") == 0) return 0;
|
|
if (strcasecmp(key, "CONTROL") == 0) return 2;
|
|
if (strcasecmp(key, "ALT") == 0) return 3;
|
|
if (strcasecmp(key, "META") == 0) return 3;
|
|
if (strcasecmp(key, "SUPER") == 0) return 6;
|
|
if (strcasecmp(key, "HYPER") == 0) return 0;
|
|
if (strcasecmp(key, "MOD1") == 0) return 3;
|
|
if (strcasecmp(key, "MOD2") == 0) return 4;
|
|
if (strcasecmp(key, "MOD3") == 0) return 5;
|
|
if (strcasecmp(key, "MOD4") == 0) return 6;
|
|
if (strcasecmp(key, "MOD5") == 0) return 7;
|
|
return -1;
|
|
}
|
|
|
|
static const char *wl_keysym_to_string(void *ectx, WMNativeKeysym ks)
|
|
{ (void)ectx; (void)ks; return NULL; /* TODO: xkb_keysym_get_name */ }
|
|
|
|
static int wl_get_modifier_keysyms(void *ectx, WMNativeKeysym *out, int max_per_mod)
|
|
{ (void)ectx; memset(out, 0, 8 * max_per_mod * sizeof(WMNativeKeysym)); return 0; }
|
|
|
|
static unsigned int wl_keysym_to_keycode(void *ectx, WMNativeKeysym ks)
|
|
{ (void)ectx; (void)ks; return 0; }
|
|
|
|
static WMNativeKeysym wl_keycode_to_keysym(void *ectx, unsigned int kc, int idx)
|
|
{ (void)ectx; (void)kc; (void)idx; return 0; }
|
|
|
|
static void wl_keysym_convert_case(void *ectx, WMNativeKeysym sym,
|
|
WMNativeKeysym *lower, WMNativeKeysym *upper)
|
|
{ (void)ectx; if (lower) *lower = sym; if (upper) *upper = sym; }
|
|
|
|
static void wl_send_wm_command(void *ectx, const char *cmd)
|
|
{ (void)ectx; (void)cmd; /* TODO: signal wmaker via PID file */ }
|
|
|
|
static void wl_set_window_hints(void *ectx, WNativeWindow w,
|
|
int minw, int minh, int maxw, int maxh, int bw, int bh)
|
|
{ (void)ectx; (void)w; (void)minw; (void)minh; (void)maxw; (void)maxh; (void)bw; (void)bh; }
|
|
|
|
static void wl_set_window_property_string(void *ectx, WNativeWindow w,
|
|
const char *atom, const char *val)
|
|
{ (void)ectx; (void)w; (void)atom; (void)val; }
|
|
|
|
static void wl_set_window_property_data(void *ectx, WNativeWindow w,
|
|
WMNativeAtom prop, WMNativeAtom type, int fmt, const void *data, int nel)
|
|
{ (void)ectx; (void)w; (void)prop; (void)type; (void)fmt; (void)data; (void)nel; }
|
|
|
|
static void wl_recolor_cursor(void *ectx, WMNativePixmap c,
|
|
unsigned short fr, unsigned short fg, unsigned short fb,
|
|
unsigned short br, unsigned short bg, unsigned short bb)
|
|
{ (void)ectx; (void)c; (void)fr; (void)fg; (void)fb; (void)br; (void)bg; (void)bb; }
|
|
|
|
static void wl_set_error_handler(void *ectx, void *h)
|
|
{ (void)ectx; (void)h; }
|
|
|
|
static void wl_restore_error_handler(void *ectx)
|
|
{ (void)ectx; }
|
|
|
|
const W_EventBackend W_EventBackendWL = {
|
|
.screen_init = wl_screen_init,
|
|
.screen_fini = wl_screen_fini,
|
|
.pending = wl_pending,
|
|
.fetch = wl_fetch,
|
|
.check_mask = wl_check_mask,
|
|
.check_typed_window = wl_check_typed_window,
|
|
.flush = wl_flush,
|
|
.fd = wl_fd,
|
|
.put_back = wl_put_back,
|
|
.post_dispatch = NULL,
|
|
.grab_keyboard = wl_grab_keyboard,
|
|
.ungrab_keyboard = wl_ungrab_keyboard,
|
|
.allow_events = wl_allow_events,
|
|
.grab_pointer = wl_grab_pointer,
|
|
.ungrab_pointer = wl_ungrab_pointer,
|
|
.query_pointer = wl_query_pointer,
|
|
.get_window_geometry = wl_get_window_geometry,
|
|
.move_window = wl_move_window,
|
|
.translate_coordinates = wl_translate_coordinates,
|
|
.get_root_window = wl_get_root_window,
|
|
.query_tree = wl_query_tree,
|
|
.get_pointer_control = wl_get_pointer_control,
|
|
.set_pointer_control = wl_set_pointer_control,
|
|
.modifier_from_key = wl_modifier_from_key,
|
|
.keysym_to_string = wl_keysym_to_string,
|
|
.keysym_to_keycode = wl_keysym_to_keycode,
|
|
.keycode_to_keysym = wl_keycode_to_keysym,
|
|
.keysym_convert_case = wl_keysym_convert_case,
|
|
.get_modifier_keysyms = wl_get_modifier_keysyms,
|
|
.send_wm_command = wl_send_wm_command,
|
|
.set_window_hints = wl_set_window_hints,
|
|
.set_window_property_string = wl_set_window_property_string,
|
|
.set_window_property_data = wl_set_window_property_data,
|
|
.recolor_cursor = wl_recolor_cursor,
|
|
.set_error_handler = wl_set_error_handler,
|
|
.restore_error_handler = wl_restore_error_handler,
|
|
};
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Called from wl_backend.c */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
void
|
|
W_WL_EnqueueEvent(W_Screen *scr, const WMEvent *ev, W_View *target)
|
|
{
|
|
if (!scr || !scr->eventBackendCtx)
|
|
return;
|
|
WLEventCtx *ctx = scr->eventBackendCtx;
|
|
int next_tail = (ctx->tail + 1) % WL_EVENT_QUEUE_CAP;
|
|
if (next_tail == ctx->head)
|
|
return; /* queue full — drop */
|
|
ctx->buf[ctx->tail].ev = *ev;
|
|
ctx->buf[ctx->tail].target = target;
|
|
ctx->tail = next_tail;
|
|
}
|
|
|
|
void
|
|
W_WL_SetLoopDispatch(W_Screen *scr,
|
|
int loop_fd,
|
|
void (*dispatch_fn)(int timeout_ms, void *ud),
|
|
void *userdata)
|
|
{
|
|
if (!scr || !scr->eventBackendCtx)
|
|
return;
|
|
WLEventCtx *ctx = scr->eventBackendCtx;
|
|
ctx->loop_fd = loop_fd;
|
|
ctx->dispatch_fn = dispatch_fn;
|
|
ctx->dispatch_ud = userdata;
|
|
}
|
|
|
|
/* Tick the Wayland compositor event loop once (used by modal loops). */
|
|
void
|
|
W_WL_InvalidateView(W_Screen *scr, W_View *target)
|
|
{
|
|
if (!scr || !scr->eventBackendCtx)
|
|
return;
|
|
WLEventCtx *ctx = scr->eventBackendCtx;
|
|
int i = ctx->head;
|
|
while (i != ctx->tail) {
|
|
if (ctx->buf[i].target == target) {
|
|
ctx->buf[i].target = NULL;
|
|
}
|
|
i = (i + 1) % WL_EVENT_QUEUE_CAP;
|
|
}
|
|
}
|
|
|
|
void
|
|
W_WL_DispatchLoop(W_Screen *scr, int timeout_ms)
|
|
{
|
|
if (!scr || !scr->eventBackendCtx)
|
|
return;
|
|
WLEventCtx *ctx = scr->eventBackendCtx;
|
|
if (ctx->dispatch_fn)
|
|
ctx->dispatch_fn(timeout_ms, ctx->dispatch_ud);
|
|
}
|