2020-07-28 14:19:28 +10:00
|
|
|
/*
|
|
|
|
|
* 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>
|
2020-08-25 09:20:31 +10:00
|
|
|
#include <getopt.h>
|
2020-07-28 14:19:28 +10:00
|
|
|
#include <poll.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <string.h>
|
2020-08-21 16:57:41 +10:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
|
|
#if HAVE_LIBXKBCOMMON
|
|
|
|
|
#include <xkbcommon/xkbcommon.h>
|
|
|
|
|
#endif
|
2020-07-28 14:19:28 +10:00
|
|
|
|
|
|
|
|
#include "libeis.h"
|
|
|
|
|
|
2020-08-20 12:52:22 +10:00
|
|
|
#include "src/util-color.h"
|
2020-07-28 14:19:28 +10:00
|
|
|
#include "src/util-mem.h"
|
2020-08-21 16:57:41 +10:00
|
|
|
#include "src/util-memfile.h"
|
2020-07-28 14:19:28 +10:00
|
|
|
#include "src/util-strings.h"
|
|
|
|
|
|
2020-08-21 16:57:41 +10:00
|
|
|
struct eis_server {
|
2020-08-25 09:36:38 +10:00
|
|
|
const char *layout;
|
2020-08-21 16:57:41 +10:00
|
|
|
#if HAVE_LIBXKBCOMMON
|
|
|
|
|
struct xkb_context *ctx;
|
|
|
|
|
struct xkb_keymap *keymap;
|
|
|
|
|
struct xkb_state *state;
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-29 11:55:02 +10:00
|
|
|
static bool stop = false;
|
|
|
|
|
|
|
|
|
|
static void sighandler(int signal) {
|
|
|
|
|
stop = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 21:07:28 +10:00
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis *, eis_unref);
|
2020-08-27 10:10:40 +10:00
|
|
|
#define _cleanup_eis_ _cleanup_(eis_unrefp)
|
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_event *, eis_event_unref);
|
|
|
|
|
#define _cleanup_eis_event_ _cleanup_(eis_event_unrefp)
|
2020-08-05 21:07:28 +10:00
|
|
|
|
2020-08-13 15:18:15 +10:00
|
|
|
static void unlink_free(char **path) {
|
|
|
|
|
if (*path) {
|
|
|
|
|
unlink(*path);
|
|
|
|
|
free(*path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#define _cleanup_unlink_free_ _cleanup_(unlink_free)
|
|
|
|
|
|
2020-08-20 12:52:22 +10:00
|
|
|
static inline void
|
|
|
|
|
_printf_(1, 2)
|
|
|
|
|
colorprint(const char *format, ...)
|
|
|
|
|
{
|
2020-08-27 11:16:05 +10:00
|
|
|
static uint64_t color = 0;
|
2020-08-20 12:52:22 +10:00
|
|
|
run_only_once {
|
2020-08-27 11:16:05 +10:00
|
|
|
color = rgb(1, 1, 1) | rgb_bg(255, 127, 0);
|
2020-08-20 12:52:22 +10:00
|
|
|
}
|
|
|
|
|
|
2020-08-27 11:16:05 +10:00
|
|
|
cprintf(color, "EIS socket server: ");
|
2020-08-20 12:52:22 +10:00
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
vprintf(format, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 16:57:41 +10:00
|
|
|
#if HAVE_LIBXKBCOMMON
|
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_context*, xkb_context_unref);
|
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_keymap*, xkb_keymap_unref);
|
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_state*, xkb_state_unref);
|
|
|
|
|
#define _cleanup_xkb_context_ _cleanup_(xkb_context_unrefp)
|
|
|
|
|
#define _cleanup_xkb_keymap_ _cleanup_(xkb_keymap_unrefp)
|
|
|
|
|
#define _cleanup_xkb_state_ _cleanup_(xkb_state_unrefp)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
setup_keymap(struct eis_server *server, struct eis_device *device)
|
|
|
|
|
{
|
|
|
|
|
#if HAVE_LIBXKBCOMMON
|
2020-08-25 09:36:38 +10:00
|
|
|
if (server->layout) {
|
|
|
|
|
colorprint("Using server layout: %s\n", server->layout);
|
|
|
|
|
_cleanup_xkb_context_ struct xkb_context *ctx =
|
|
|
|
|
xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
|
|
|
if (!ctx)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
struct xkb_rule_names names = {
|
|
|
|
|
.rules = "evdev",
|
|
|
|
|
.model = "pc105",
|
|
|
|
|
.layout = server->layout,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_cleanup_xkb_keymap_ struct xkb_keymap *keymap =
|
|
|
|
|
xkb_keymap_new_from_names(ctx, &names, 0);
|
|
|
|
|
if (!keymap)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const char *str = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1);
|
|
|
|
|
size_t len = strlen(str) - 1;
|
|
|
|
|
|
|
|
|
|
struct memfile *f = memfile_new(str, len);
|
|
|
|
|
if (!f)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
eis_device_set_keymap(device, EIS_KEYMAP_TYPE_XKB,
|
|
|
|
|
memfile_get_fd(f), memfile_get_size(f));
|
|
|
|
|
memfile_unref(f);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 16:57:41 +10:00
|
|
|
if (eis_device_get_keymap_type(device) == EIS_KEYMAP_TYPE_NONE)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
colorprint("Using client keymap\n");
|
|
|
|
|
int fd = eis_device_get_keymap(device);
|
|
|
|
|
size_t size = eis_device_get_keymap_size(device);
|
|
|
|
|
char *str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
|
if (str == MAP_FAILED)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_cleanup_xkb_context_ struct xkb_context *ctx =
|
|
|
|
|
xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
|
|
|
if (!ctx)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_cleanup_xkb_keymap_ struct xkb_keymap *keymap =
|
|
|
|
|
xkb_keymap_new_from_string(ctx, str,
|
|
|
|
|
XKB_KEYMAP_FORMAT_TEXT_V1, 0);
|
|
|
|
|
if (!keymap)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_cleanup_xkb_state_ struct xkb_state *state = xkb_state_new(keymap);
|
|
|
|
|
if (!state)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
server->ctx = steal(&ctx);
|
|
|
|
|
server->keymap = steal(&keymap);
|
|
|
|
|
server->state = steal(&state);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_key(struct eis_server *server, uint32_t keycode, bool is_press)
|
|
|
|
|
{
|
|
|
|
|
char keysym_name[64] = {0};
|
|
|
|
|
|
|
|
|
|
#if HAVE_LIBXKBCOMMON
|
|
|
|
|
if (server->state) {
|
|
|
|
|
uint32_t xkbkc = keycode + 8;
|
|
|
|
|
xkb_state_update_key(server->state, xkbkc, is_press ? XKB_KEY_DOWN : XKB_KEY_UP);
|
|
|
|
|
xkb_state_key_get_utf8(server->state, xkbkc, keysym_name, sizeof(keysym_name));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
colorprint("key %d (%s) [%s]\n",
|
|
|
|
|
keycode, is_press ? "press" : "release",
|
|
|
|
|
keysym_name);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 09:20:31 +10:00
|
|
|
static void
|
|
|
|
|
usage(FILE *fp, const char *argv0)
|
|
|
|
|
{
|
|
|
|
|
fprintf(fp,
|
|
|
|
|
"Usage: %s [--verbose] [--socketpath=/path/to/socket]\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Start an EIS demo server. The server accepts all client connections\n"
|
|
|
|
|
"and devices and prints any events from the client to stdout.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Options:\n"
|
|
|
|
|
" --socketpath Use the given socket path. Default: $XDG_RUNTIME/eis-0\n"
|
2020-08-25 09:36:38 +10:00
|
|
|
" --layout Use the given XKB layout (requires libxkbcommon). Default:\n"
|
|
|
|
|
" use the client-supplied keymap, if any\n"
|
2020-08-25 09:20:31 +10:00
|
|
|
" --verbose Enable debugging output\n"
|
|
|
|
|
"",
|
|
|
|
|
argv0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 14:19:28 +10:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2020-08-25 09:20:31 +10:00
|
|
|
bool verbose = false;
|
2020-08-25 09:36:38 +10:00
|
|
|
const char *layout = NULL;
|
2020-08-25 09:20:31 +10:00
|
|
|
|
|
|
|
|
_cleanup_unlink_free_ char *socketpath = NULL;
|
|
|
|
|
const char *xdg = getenv("XDG_RUNTIME_DIR");
|
|
|
|
|
if (xdg)
|
|
|
|
|
socketpath = xaprintf("%s/eis-0", xdg);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
enum {
|
|
|
|
|
OPT_VERBOSE,
|
2020-08-25 09:36:38 +10:00
|
|
|
OPT_LAYOUT,
|
2020-08-25 09:20:31 +10:00
|
|
|
OPT_SOCKETPATH,
|
|
|
|
|
};
|
|
|
|
|
static struct option long_opts[] = {
|
|
|
|
|
{"socketpath", required_argument, 0, OPT_SOCKETPATH},
|
2020-08-25 09:36:38 +10:00
|
|
|
{"layout", required_argument, 0, OPT_LAYOUT},
|
2020-08-25 09:20:31 +10:00
|
|
|
{"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_SOCKETPATH:
|
|
|
|
|
free(socketpath);
|
|
|
|
|
socketpath = xstrdup(optarg);
|
|
|
|
|
break;
|
2020-08-25 09:36:38 +10:00
|
|
|
case OPT_LAYOUT:
|
|
|
|
|
layout = optarg;
|
|
|
|
|
break;
|
2020-08-25 09:20:31 +10:00
|
|
|
case OPT_VERBOSE:
|
|
|
|
|
verbose = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
usage(stderr, argv[0]);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (socketpath == NULL) {
|
|
|
|
|
fprintf(stderr, "No socketpath given and $XDG_RUNTIME_DIR is not set\n");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-25 09:36:38 +10:00
|
|
|
struct eis_server server = {
|
|
|
|
|
.layout = layout,
|
|
|
|
|
};
|
2020-08-21 16:57:41 +10:00
|
|
|
|
2020-08-27 10:10:40 +10:00
|
|
|
_cleanup_eis_ struct eis *eis = eis_new(NULL);
|
2020-07-28 14:19:28 +10:00
|
|
|
assert(eis);
|
|
|
|
|
|
2020-08-25 09:20:31 +10:00
|
|
|
if (verbose)
|
|
|
|
|
eis_log_set_priority(eis, EIS_LOG_PRIORITY_DEBUG);
|
2020-07-28 14:19:28 +10:00
|
|
|
|
2020-07-29 11:55:02 +10:00
|
|
|
signal(SIGINT, sighandler);
|
2020-07-28 14:19:28 +10:00
|
|
|
|
2020-08-05 16:25:43 +10:00
|
|
|
int rc = eis_setup_backend_socket(eis, socketpath);
|
2020-07-28 14:19:28 +10:00
|
|
|
if (rc != 0) {
|
|
|
|
|
fprintf(stderr, "init failed: %s\n", strerror(errno));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 12:52:22 +10:00
|
|
|
colorprint("waiting on %s\n", socketpath);
|
2020-07-29 09:43:06 +10:00
|
|
|
|
2020-07-28 14:19:28 +10:00
|
|
|
struct pollfd fds = {
|
|
|
|
|
.fd = eis_get_fd(eis),
|
|
|
|
|
.events = POLLIN,
|
|
|
|
|
.revents = 0,
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-29 11:55:02 +10:00
|
|
|
while (!stop && poll(&fds, 1, -1) > -1) {
|
2020-07-28 14:19:28 +10:00
|
|
|
eis_dispatch(eis);
|
|
|
|
|
|
2020-07-29 09:44:45 +10:00
|
|
|
while (true) {
|
2020-08-27 10:10:40 +10:00
|
|
|
_cleanup_eis_event_ struct eis_event *e = eis_get_event(eis);
|
2020-07-29 09:44:45 +10:00
|
|
|
if (!e)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
switch(eis_event_get_type(e)) {
|
|
|
|
|
case EIS_EVENT_CLIENT_CONNECT:
|
|
|
|
|
{
|
|
|
|
|
struct eis_client *client = eis_event_get_client(e);
|
2020-08-20 12:52:22 +10:00
|
|
|
colorprint("new client: %s\n", eis_client_get_name(client));
|
2020-07-29 09:44:45 +10:00
|
|
|
/* insert sophisticated authentication here */
|
|
|
|
|
eis_client_connect(client);
|
2020-08-20 12:52:22 +10:00
|
|
|
colorprint("accepting client\n");
|
2020-07-29 09:44:45 +10:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case EIS_EVENT_CLIENT_DISCONNECT:
|
|
|
|
|
{
|
|
|
|
|
struct eis_client *client = eis_event_get_client(e);
|
2020-08-20 12:52:22 +10:00
|
|
|
colorprint("client %s disconnected\n", eis_client_get_name(client));
|
2020-07-29 09:44:45 +10:00
|
|
|
eis_client_disconnect(client);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case EIS_EVENT_DEVICE_ADDED:
|
|
|
|
|
{
|
|
|
|
|
struct eis_device *device = eis_event_get_device(e);
|
2020-08-20 12:52:22 +10:00
|
|
|
colorprint("new device, caps:%s%s%s%s\n",
|
2020-07-29 09:44:45 +10:00
|
|
|
eis_device_has_capability(device, EIS_DEVICE_CAP_POINTER) ? " ptr" : "",
|
|
|
|
|
eis_device_has_capability(device, EIS_DEVICE_CAP_KEYBOARD) ? " kbd" : "",
|
|
|
|
|
eis_device_has_capability(device, EIS_DEVICE_CAP_POINTER_ABSOLUTE) ? " abs" : "",
|
|
|
|
|
eis_device_has_capability(device, EIS_DEVICE_CAP_TOUCH) ? " touch" : "");
|
2020-08-21 16:57:41 +10:00
|
|
|
setup_keymap(&server, device);
|
2020-07-29 09:44:45 +10:00
|
|
|
/* insert sophisticated device checks here */
|
|
|
|
|
eis_device_connect(device);
|
2020-08-19 13:39:32 +10:00
|
|
|
eis_device_resume(device);
|
2020-07-29 09:44:45 +10:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case EIS_EVENT_DEVICE_REMOVED:
|
|
|
|
|
{
|
2020-08-20 12:52:22 +10:00
|
|
|
colorprint("device removed\n");
|
2020-07-29 09:44:45 +10:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case EIS_EVENT_POINTER_MOTION:
|
|
|
|
|
{
|
2020-08-20 12:52:22 +10:00
|
|
|
colorprint("motion by %.2f/%.2f\n",
|
2020-08-19 11:35:06 +10:00
|
|
|
eis_event_pointer_get_dx(e),
|
|
|
|
|
eis_event_pointer_get_dy(e));
|
2020-07-29 09:44:45 +10:00
|
|
|
}
|
|
|
|
|
break;
|
2020-08-03 12:00:31 +10:00
|
|
|
case EIS_EVENT_POINTER_BUTTON:
|
|
|
|
|
{
|
2020-08-20 12:52:22 +10:00
|
|
|
colorprint("button %d (%s)\n",
|
2020-08-18 15:45:01 +10:00
|
|
|
eis_event_pointer_get_button(e),
|
|
|
|
|
eis_event_pointer_get_button_is_press(e) ? "press" : "release");
|
2020-08-03 12:00:31 +10:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case EIS_EVENT_KEYBOARD_KEY:
|
|
|
|
|
{
|
2020-08-21 16:57:41 +10:00
|
|
|
handle_key(&server,
|
|
|
|
|
eis_event_keyboard_get_key(e),
|
|
|
|
|
eis_event_keyboard_get_key_is_press(e));
|
2020-08-03 12:00:31 +10:00
|
|
|
}
|
|
|
|
|
break;
|
2020-07-29 09:44:45 +10:00
|
|
|
default:
|
|
|
|
|
abort();
|
2020-07-28 14:19:28 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|