AtlatestRepositorysigil-ui
1
/*2
* A virtual keyboard and pointer for the Wayland surface tests.3
*4
* Reads one command per line on stdin, sends it through5
* zwp_virtual_keyboard_v1 or zwlr_virtual_pointer_v1, waits for the6
* compositor to process it (a roundtrip), and prints "ACK <n> <command>".7
* The devices live as long as this process, so the surface keeps the8
* keyboard between commands.9
*10
* keymap LAYOUT [VARIANT] upload an xkb keymap for LAYOUT (default us)11
* key CODE press and release an evdev key code12
* press CODE / release CODE13
* mods BITS depressed modifiers: 1 shift, 4 control, 8 alt14
* move X Y pointer to output coordinates15
* button CODE 0|1 evdev button (272 left) up or down16
* scroll DY STEPS vertical wheel: DY logical pixels, STEPS clicks17
* scroll-x DX STEPS horizontal wheel: DX logical pixels, STEPS clicks18
* quit19
*20
* With --probe it exits 0 at once.21
*22
* Like the library, it loads libwayland-client and libxkbcommon with23
* dlopen (SIGIL_UI_LIBWAYLAND_CLIENT, SIGIL_UI_LIBXKBCOMMON), so it builds24
* from the headers in native/wayland alone.25
*/26
#define _GNU_SOURCE27
#include <dlfcn.h>28
#include <stdint.h>29
#include <stdio.h>30
#include <stdlib.h>31
#include <string.h>32
#include <time.h>33
#include <unistd.h>35
#define wl_display_connect (*sui_wl_display_connect)36
#define wl_display_disconnect (*sui_wl_display_disconnect)37
#define wl_display_roundtrip (*sui_wl_display_roundtrip)38
#define wl_display_flush (*sui_wl_display_flush)39
#define wl_proxy_marshal_flags (*sui_wl_proxy_marshal_flags)40
#define wl_proxy_add_listener (*sui_wl_proxy_add_listener)41
#define wl_proxy_get_version (*sui_wl_proxy_get_version)42
#define wl_proxy_destroy (*sui_wl_proxy_destroy)43
#define wl_proxy_set_user_data (*sui_wl_proxy_set_user_data)44
#define wl_proxy_get_user_data (*sui_wl_proxy_get_user_data)46
#pragma GCC visibility push(hidden)47
#include "wayland-client.h"48
#include "virtual-keyboard-unstable-v1-client-protocol.h"49
#include "wlr-virtual-pointer-unstable-v1-client-protocol.h"50
#include "wayland-protocol.c"51
#include "virtual-keyboard-unstable-v1-protocol.c"52
#include "wlr-virtual-pointer-unstable-v1-protocol.c"53
#pragma GCC visibility pop55
#include "xkb.h"57
struct xkb_rule_names { const char *rules, *model, *layout, *variant, *options; };58
static struct xkb_context *(*xkb_context_new)(int);59
static struct xkb_keymap *(*xkb_keymap_new_from_names)(struct xkb_context *, const struct xkb_rule_names *, int);60
static char *(*xkb_keymap_get_as_string)(struct xkb_keymap *, int);61
static void (*xkb_keymap_unref)(struct xkb_keymap *);63
static void *load(const char *env, const char *soname)64
{65
const char *path = getenv(env);66
void *h = dlopen(path && *path ? path : soname, RTLD_NOW | RTLD_LOCAL);67
if (!h) { fprintf(stderr, "input-driver: %s\n", dlerror()); exit(2); }68
return h;69
}70
#define BIND(h, name, slot) do { *(void **)&(slot) = dlsym(h, name); \71
if (!(slot)) { fprintf(stderr, "input-driver: no %s\n", name); exit(2); } } while (0)73
struct input {74
struct wl_seat *seat;75
struct wl_output *output;76
struct zwp_virtual_keyboard_manager_v1 *keyboards;77
struct zwlr_virtual_pointer_manager_v1 *pointers;78
int width, height;79
};81
static void geometry(void *d, struct wl_output *o, int32_t x, int32_t y, int32_t pw, int32_t ph,82
int32_t sub, const char *make, const char *model, int32_t t) {}83
static void mode(void *d, struct wl_output *o, uint32_t flags, int32_t w, int32_t h, int32_t r)84
{85
struct input *in = d;86
if (flags & WL_OUTPUT_MODE_CURRENT) { in->width = w; in->height = h; }87
}88
static void done(void *d, struct wl_output *o) {}89
static void scale(void *d, struct wl_output *o, int32_t s) {}90
static const struct wl_output_listener output_listener = { geometry, mode, done, scale, NULL, NULL };92
static void global(void *d, struct wl_registry *r, uint32_t id, const char *iface, uint32_t version)93
{94
struct input *in = d;95
if (!strcmp(iface, "wl_seat") && !in->seat)96
in->seat = wl_registry_bind(r, id, &wl_seat_interface, 1);97
else if (!strcmp(iface, "wl_output") && !in->output) {98
in->output = wl_registry_bind(r, id, &wl_output_interface, 2);99
wl_output_add_listener(in->output, &output_listener, in);100
} else if (!strcmp(iface, "zwp_virtual_keyboard_manager_v1"))101
in->keyboards = wl_registry_bind(r, id, &zwp_virtual_keyboard_manager_v1_interface, 1);102
else if (!strcmp(iface, "zwlr_virtual_pointer_manager_v1"))103
in->pointers = wl_registry_bind(r, id, &zwlr_virtual_pointer_manager_v1_interface, 1);104
}105
static void global_remove(void *d, struct wl_registry *r, uint32_t id) {}106
static const struct wl_registry_listener registry_listener = { global, global_remove };108
static uint32_t now(void)109
{110
struct timespec t;111
clock_gettime(CLOCK_MONOTONIC, &t);112
return (uint32_t)(t.tv_sec * 1000 + t.tv_nsec / 1000000);113
}115
static int upload_keymap(struct xkb_context *ctx, struct zwp_virtual_keyboard_v1 *kb,116
const char *layout, const char *variant)117
{118
struct xkb_rule_names names = { NULL, NULL, layout, variant, NULL };119
struct xkb_keymap *map = xkb_keymap_new_from_names(ctx, &names, 0);120
if (!map) return 0;121
char *text = xkb_keymap_get_as_string(map, XKB_KEYMAP_FORMAT_TEXT_V1);122
xkb_keymap_unref(map);123
if (!text) return 0;124
char path[] = "/tmp/sigil-ui-keymap.XXXXXX";125
int fd = mkstemp(path);126
if (fd < 0) { free(text); return 0; }127
unlink(path);128
size_t size = strlen(text) + 1, off = 0;129
while (off < size) {130
ssize_t n = write(fd, text + off, size - off);131
if (n <= 0) { close(fd); free(text); return 0; }132
off += (size_t)n;133
}134
free(text);135
zwp_virtual_keyboard_v1_keymap(kb, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, fd, (uint32_t)size);136
close(fd);137
return 1;138
}140
int main(int argc, char **argv)141
{142
/* --probe: exit at once; the test runner uses it to see whether the143
* binary starts at all on this system. */144
if (argc > 1 && !strcmp(argv[1], "--probe")) return 0;145
void *wl = load("SIGIL_UI_LIBWAYLAND_CLIENT", "libwayland-client.so.0");146
BIND(wl, "wl_display_connect", sui_wl_display_connect);147
BIND(wl, "wl_display_disconnect", sui_wl_display_disconnect);148
BIND(wl, "wl_display_roundtrip", sui_wl_display_roundtrip);149
BIND(wl, "wl_display_flush", sui_wl_display_flush);150
BIND(wl, "wl_proxy_marshal_flags", sui_wl_proxy_marshal_flags);151
BIND(wl, "wl_proxy_add_listener", sui_wl_proxy_add_listener);152
BIND(wl, "wl_proxy_get_version", sui_wl_proxy_get_version);153
BIND(wl, "wl_proxy_destroy", sui_wl_proxy_destroy);154
BIND(wl, "wl_proxy_set_user_data", sui_wl_proxy_set_user_data);155
BIND(wl, "wl_proxy_get_user_data", sui_wl_proxy_get_user_data);156
void *xkb = load("SIGIL_UI_LIBXKBCOMMON", "libxkbcommon.so.0");157
BIND(xkb, "xkb_context_new", xkb_context_new);158
BIND(xkb, "xkb_keymap_new_from_names", xkb_keymap_new_from_names);159
BIND(xkb, "xkb_keymap_get_as_string", xkb_keymap_get_as_string);160
BIND(xkb, "xkb_keymap_unref", xkb_keymap_unref);162
struct input in = {0};163
struct wl_display *display = wl_display_connect(NULL);164
if (!display) { fprintf(stderr, "input-driver: no compositor\n"); return 3; }165
struct wl_registry *registry = wl_display_get_registry(display);166
wl_registry_add_listener(registry, ®istry_listener, &in);167
if (wl_display_roundtrip(display) < 0 || wl_display_roundtrip(display) < 0) return 3;168
if (!in.seat || !in.keyboards || !in.pointers || !in.width) {169
fprintf(stderr, "input-driver: the compositor lacks a seat, an output or virtual input\n");170
return 4;171
}172
struct zwp_virtual_keyboard_v1 *kb =173
zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(in.keyboards, in.seat);174
struct zwlr_virtual_pointer_v1 *ptr =175
zwlr_virtual_pointer_manager_v1_create_virtual_pointer(in.pointers, in.seat);176
struct xkb_context *ctx = xkb_context_new(0);177
if (!ctx || !upload_keymap(ctx, kb, "us", NULL)) { fprintf(stderr, "input-driver: keymap\n"); return 5; }178
if (wl_display_roundtrip(display) < 0) return 6;179
printf("INPUT-READY %dx%d\n", in.width, in.height);180
fflush(stdout);182
char line[256], op[32], s1[64], s2[64];183
unsigned seq = 0;184
while (fgets(line, sizeof line, stdin)) {185
int a = 0, b = 0;186
s1[0] = s2[0] = 0;187
if (sscanf(line, "%31s", op) != 1) continue;188
uint32_t t = now();189
if (!strcmp(op, "keymap")) {190
sscanf(line, "%*s %63s %63s", s1, s2);191
if (!upload_keymap(ctx, kb, s1[0] ? s1 : "us", s2[0] ? s2 : NULL)) {192
fprintf(stderr, "input-driver: no keymap for %s\n", s1);193
return 7;194
}195
} else if (!strcmp(op, "key") && sscanf(line, "%*s %d", &a) == 1) {196
zwp_virtual_keyboard_v1_key(kb, t, (uint32_t)a, WL_KEYBOARD_KEY_STATE_PRESSED);197
zwp_virtual_keyboard_v1_key(kb, t, (uint32_t)a, WL_KEYBOARD_KEY_STATE_RELEASED);198
} else if (!strcmp(op, "press") && sscanf(line, "%*s %d", &a) == 1) {199
zwp_virtual_keyboard_v1_key(kb, t, (uint32_t)a, WL_KEYBOARD_KEY_STATE_PRESSED);200
} else if (!strcmp(op, "release") && sscanf(line, "%*s %d", &a) == 1) {201
zwp_virtual_keyboard_v1_key(kb, t, (uint32_t)a, WL_KEYBOARD_KEY_STATE_RELEASED);202
} else if (!strcmp(op, "mods") && sscanf(line, "%*s %d", &a) == 1) {203
zwp_virtual_keyboard_v1_modifiers(kb, (uint32_t)a, 0, 0, 0);204
} else if (!strcmp(op, "move") && sscanf(line, "%*s %d %d", &a, &b) == 2) {205
zwlr_virtual_pointer_v1_motion_absolute(ptr, t, (uint32_t)a, (uint32_t)b,206
(uint32_t)in.width, (uint32_t)in.height);207
zwlr_virtual_pointer_v1_frame(ptr);208
} else if (!strcmp(op, "button") && sscanf(line, "%*s %d %d", &a, &b) == 2) {209
zwlr_virtual_pointer_v1_button(ptr, t, (uint32_t)a, (uint32_t)b);210
zwlr_virtual_pointer_v1_frame(ptr);211
} else if ((!strcmp(op, "scroll") || !strcmp(op, "scroll-x")) &&212
sscanf(line, "%*s %d %d", &a, &b) == 2) {213
uint32_t axis = !strcmp(op, "scroll-x") ? WL_POINTER_AXIS_HORIZONTAL_SCROLL214
: WL_POINTER_AXIS_VERTICAL_SCROLL;215
zwlr_virtual_pointer_v1_axis_source(ptr, WL_POINTER_AXIS_SOURCE_WHEEL);216
zwlr_virtual_pointer_v1_axis_discrete(ptr, t, axis,217
wl_fixed_from_int(a), b);218
zwlr_virtual_pointer_v1_frame(ptr);219
} else if (!strcmp(op, "quit")) {220
break;221
} else {222
fprintf(stderr, "input-driver: unknown command: %s", line);223
return 8;224
}225
if (wl_display_roundtrip(display) < 0) return 9;226
printf("ACK %u %s", ++seq, line);227
fflush(stdout);228
}229
zwp_virtual_keyboard_v1_destroy(kb);230
zwlr_virtual_pointer_v1_destroy(ptr);231
wl_display_roundtrip(display);232
wl_display_disconnect(display);233
return 0;234
}