libei/tools/ei-demo-client.c
Peter Hutterer 5adf87cdda tools: hook up absolute pointer motion in the demo client
Prints the regions and sets the pointer to somewhere around 150/150

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2021-08-12 15:39:26 +10:00

387 lines
9.7 KiB
C

/*
* Copyright © 2020 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <poll.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdarg.h>
#include <linux/input-event-codes.h>
#if HAVE_LIBXKBCOMMON
#include <xkbcommon/xkbcommon.h>
#endif
#include "libei.h"
#include "src/util-macros.h"
#include "src/util-mem.h"
#include "src/util-memfile.h"
#include "src/util-color.h"
#include "src/util-strings.h"
DEFINE_UNREF_CLEANUP_FUNC(ei);
DEFINE_UNREF_CLEANUP_FUNC(ei_device);
DEFINE_UNREF_CLEANUP_FUNC(ei_keymap);
DEFINE_UNREF_CLEANUP_FUNC(ei_event);
static inline void
_printf_(1, 2)
colorprint(const char *format, ...)
{
static uint64_t color = 0;
run_only_once {
color = rgb(1, 1, 1) | rgb_bg(230, 0, 230);
}
cprintf(color, "EI socket client: ");
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
#if HAVE_LIBXKBCOMMON
DEFINE_UNREF_CLEANUP_FUNC(xkb_context);
DEFINE_UNREF_CLEANUP_FUNC(xkb_keymap);
DEFINE_UNREF_CLEANUP_FUNC(xkb_state);
DEFINE_UNREF_CLEANUP_FUNC(memfile);
#endif
static void
setup_xkb_keymap(struct ei_keymap *keymap)
{
#if HAVE_LIBXKBCOMMON
if (!keymap)
return;
_unref_(xkb_context) *ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (!ctx)
return;
size_t sz = ei_keymap_get_size(keymap);
_cleanup_free_ char *buf = xalloc(sz + 1);
read(ei_keymap_get_fd(keymap), buf, sz);
buf[sz] = '\0';
_unref_(xkb_keymap) *xkbmap = xkb_keymap_new_from_string(ctx, buf,
XKB_KEYMAP_FORMAT_TEXT_V1,
XKB_KEYMAP_COMPILE_NO_FLAGS);
if (!xkbmap)
return;
_unref_(xkb_state) *xkbstate = xkb_state_new(xkbmap);
if (!xkbstate)
return;
char layout[6 * 7 + 1] = {0}; /* 6 keys, 7 bytes per key min */
for (unsigned int evcode = KEY_Q; evcode <= KEY_Y; evcode++) {
char utf8[7];
xkb_keysym_t keysym = xkb_state_key_get_one_sym(xkbstate, evcode + 8);
xkb_keysym_to_utf8(keysym, utf8, sizeof(utf8));
strcat(layout, utf8);
}
colorprint("XKB keymap: %s\n", layout);
#endif
}
static void
handle_keymap(struct ei_event *event)
{
struct ei_device *device = ei_event_get_device(event);
if (!ei_device_has_capability(device, EI_DEVICE_CAP_KEYBOARD))
return;
struct ei_keymap *keymap = ei_device_keyboard_get_keymap(device);
if (!keymap)
return;
enum ei_keymap_type type = ei_keymap_get_type(keymap);
switch (type) {
case EI_KEYMAP_TYPE_XKB:
setup_xkb_keymap(keymap);
break;
}
}
static void
handle_regions(struct ei_device *device)
{
uint32_t idx = 0;
struct ei_region *r;
while ((r = ei_device_get_region (device, idx++))) {
int x, y, w, h;
x = ei_region_get_x(r);
y = ei_region_get_y(r);
w = ei_region_get_width(r);
h = ei_region_get_height(r);
colorprint("%s has region %dx%d@%d,%d\n",
ei_device_get_name(device), w, h, x, y);
}
}
static void
usage(FILE *fp, const char *argv0)
{
fprintf(fp,
"Usage: %s [--verbose] [--socket|--portal] [--busname=a.b.c.d] [--layout=us]\n"
"\n"
"Start an EI demo client. The client will connect to EIS\n"
"with the chosen backend (default: socket) and emulate pointer\n"
"and keyboard events in a loop.\n"
"\n"
"Options:\n"
" --socket Use the socket backend. The socket path is $LIBEI_SOCKET if set, \n"
" otherwise XDG_RUNTIME/eis-0\n"
" --portal Use the portal backend.\n"
" --busname Use the given busname (default: org.freedesktop.portal.Desktop)\n"
" --verbose Enable debugging output\n"
"",
argv0);
}
int main(int argc, char **argv)
{
enum {
SOCKET,
PORTAL,
} backend = SOCKET;
bool verbose = false;
_cleanup_free_ char *busname = xstrdup("org.freedesktop.portal.Desktop");
while (1) {
enum {
OPT_BACKEND_SOCKET,
OPT_BACKEND_PORTAL,
OPT_BUSNAME,
OPT_VERBOSE,
};
static struct option long_opts[] = {
{"socket", no_argument, 0, OPT_BACKEND_SOCKET},
{ "portal", no_argument, 0, OPT_BACKEND_PORTAL},
{ "busname", required_argument, 0, OPT_BUSNAME},
{ "verbose", no_argument, 0, OPT_VERBOSE},
{ "help", no_argument, 0, 'h'},
{NULL},
};
int optind = 0;
int c = getopt_long(argc, argv, "h", long_opts, &optind);
if (c == -1)
break;
switch(c) {
case 'h':
usage(stdout, argv[0]);
return EXIT_SUCCESS;
case OPT_VERBOSE:
verbose = true;
break;
case OPT_BACKEND_SOCKET:
backend = SOCKET;
break;
case OPT_BACKEND_PORTAL:
backend = PORTAL;
break;
case OPT_BUSNAME:
free(busname);
busname = xstrdup(optarg);
break;
default:
usage(stderr, argv[0]);
return EXIT_FAILURE;
}
}
_unref_(ei) *ei = ei_new(NULL);
assert(ei);
if (verbose)
ei_log_set_priority(ei, EI_LOG_PRIORITY_DEBUG);
ei_configure_name(ei, "ei-demo-client");
int rc = -EINVAL;
if (backend == SOCKET) {
const char SOCKETNAME[] = "eis-0";
colorprint("connecting to %s\n", SOCKETNAME);
rc = ei_setup_backend_socket(ei, getenv("LIBEI_SOCKET") ? NULL : SOCKETNAME);
} else if (backend == PORTAL) {
#if ENABLE_LIBEI_PORTAL
colorprint("connecting to %s\n", busname);
rc = ei_setup_backend_portal_busname(ei, busname);
#endif
}
if (rc != 0) {
fprintf(stderr, "init failed: %s\n", strerror(errno));
return 1;
}
struct pollfd fds = {
.fd = ei_get_fd(ei),
.events = POLLIN,
.revents = 0,
};
_unref_(ei_device) *ptr = NULL;
_unref_(ei_device) *kbd = NULL;
_unref_(ei_device) *abs = NULL;
bool stop = false;
bool have_ptr = false;
bool have_kbd = false;
bool have_abs = false;
struct ei_seat *default_seat = NULL;
while (!stop && poll(&fds, 1, 2000) > -1) {
ei_dispatch(ei);
while (!stop) {
_unref_(ei_event) *e = ei_get_event(ei);
if (!e)
break;
switch(ei_event_get_type(e)) {
case EI_EVENT_CONNECT:
colorprint("connected\n");
break;
case EI_EVENT_DISCONNECT:
{
colorprint("disconnected us\n");
stop = true;
break;
}
case EI_EVENT_SEAT_ADDED:
{
if (default_seat) {
colorprint("ignoring other seats\n");
break;
}
default_seat = ei_seat_ref(ei_event_get_seat(e));
colorprint("seat added: %s\n", ei_seat_get_name(default_seat));
ei_seat_bind(default_seat);
break;
}
case EI_EVENT_SEAT_REMOVED:
/* Don't need to close the devices, libei will
* give us the right events */
if (ei_event_get_seat(e) == default_seat)
default_seat = ei_seat_unref(default_seat);
break;
case EI_EVENT_DEVICE_ADDED:
{
struct ei_device *device = ei_event_get_device(e);
if (ei_device_has_capability(device, EI_DEVICE_CAP_POINTER)) {
colorprint("New pointer device: %s\n", ei_device_get_name(device));
ptr = device;
}
if (ei_device_has_capability(device, EI_DEVICE_CAP_KEYBOARD)) {
colorprint("New keyboard device: %s\n", ei_device_get_name(device));
kbd = device;
handle_keymap(e);
}
if (ei_device_has_capability(device, EI_DEVICE_CAP_POINTER_ABSOLUTE)) {
colorprint("New abs pointer device: %s\n", ei_device_get_name(device));
abs = device;
handle_regions(device);
}
}
break;
case EI_EVENT_DEVICE_RESUMED:
if (ei_event_get_device(e) == ptr) {
colorprint("Pointer device was resumed\n");
have_ptr = true;
}
if (ei_event_get_device(e) == kbd) {
colorprint("Keyboard device was resumed\n");
have_kbd = true;
}
if (ei_event_get_device(e) == abs) {
colorprint("Abs pointer device was resumed\n");
have_abs = true;
}
break;
case EI_EVENT_DEVICE_SUSPENDED:
if (ei_event_get_device(e) == ptr) {
colorprint("Pointer device was resumed\n");
have_ptr = false;
}
if (ei_event_get_device(e) == kbd) {
colorprint("Keyboard device was resumed\n");
have_kbd = false;
}
if (ei_event_get_device(e) == abs) {
colorprint("Abs pointer device was resumed\n");
have_abs = false;
}
break;
case EI_EVENT_DEVICE_REMOVED:
{
colorprint("our device was removed\n");
break;
}
default:
abort();
}
}
if (have_ptr) {
colorprint("sending motion event\n");
ei_device_pointer_motion(ptr, -1, 1);
/* BTN_LEFT */
colorprint("sending button event\n");
ei_device_pointer_button(ptr, BTN_LEFT, true);
ei_device_pointer_button(ptr, BTN_LEFT, false);
colorprint("sending scroll event\n");
ei_device_pointer_scroll(ptr, 1, 1);
ei_device_pointer_scroll_discrete(ptr, 1, 1);
}
if (have_kbd) {
static int key = 0;
colorprint("sending key event\n");
ei_device_keyboard_key(kbd, KEY_Q + key, true); /* KEY_Q */
ei_device_keyboard_key(kbd, KEY_Q + key, false); /* KEY_Q */
key = (key + 1) % 6;
}
if (have_abs) {
static int x, y;
colorprint("sending abs event\n");
ei_device_pointer_motion_absolute(abs, 150 + ++x, 150 - ++y);
}
}
return 0;
}