/* * 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 #include #include #include #include #include #include #include #if HAVE_LIBXKBCOMMON #include #endif #include "libeis.h" #include "src/util-color.h" #include "src/util-mem.h" #include "src/util-memfile.h" #include "src/util-strings.h" struct eis_server { const char *layout; #if HAVE_LIBXKBCOMMON struct xkb_context *ctx; struct xkb_keymap *keymap; struct xkb_state *state; #endif }; static bool stop = false; static void sighandler(int signal) { stop = true; } DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis *, eis_unref); #define _cleanup_eis_ _cleanup_(eis_unrefp) DEFINE_TRIVIAL_CLEANUP_FUNC(struct eis_event *, eis_event_unref); #define _cleanup_eis_event_ _cleanup_(eis_event_unrefp) static void unlink_free(char **path) { if (*path) { unlink(*path); free(*path); } } #define _cleanup_unlink_free_ _cleanup_(unlink_free) 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(255, 127, 0); } cprintf(color, "EIS socket server: "); va_list args; va_start(args, format); vprintf(format, args); va_end(args); } #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 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); } 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); } 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" " --layout Use the given XKB layout (requires libxkbcommon). Default:\n" " use the client-supplied keymap, if any\n" " --verbose Enable debugging output\n" "", argv0); } int main(int argc, char **argv) { bool verbose = false; const char *layout = NULL; _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, OPT_LAYOUT, OPT_SOCKETPATH, }; static struct option long_opts[] = { {"socketpath", required_argument, 0, OPT_SOCKETPATH}, {"layout", required_argument, 0, OPT_LAYOUT}, {"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; case OPT_LAYOUT: layout = optarg; break; 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; } struct eis_server server = { .layout = layout, }; _cleanup_eis_ struct eis *eis = eis_new(NULL); assert(eis); if (verbose) eis_log_set_priority(eis, EIS_LOG_PRIORITY_DEBUG); signal(SIGINT, sighandler); int rc = eis_setup_backend_socket(eis, socketpath); if (rc != 0) { fprintf(stderr, "init failed: %s\n", strerror(errno)); return 1; } colorprint("waiting on %s\n", socketpath); struct pollfd fds = { .fd = eis_get_fd(eis), .events = POLLIN, .revents = 0, }; while (!stop && poll(&fds, 1, -1) > -1) { eis_dispatch(eis); while (true) { _cleanup_eis_event_ struct eis_event *e = eis_get_event(eis); if (!e) break; switch(eis_event_get_type(e)) { case EIS_EVENT_CLIENT_CONNECT: { struct eis_client *client = eis_event_get_client(e); colorprint("new client: %s\n", eis_client_get_name(client)); /* insert sophisticated authentication here */ eis_client_connect(client); colorprint("accepting client\n"); break; } case EIS_EVENT_CLIENT_DISCONNECT: { struct eis_client *client = eis_event_get_client(e); colorprint("client %s disconnected\n", eis_client_get_name(client)); eis_client_disconnect(client); break; } case EIS_EVENT_DEVICE_ADDED: { struct eis_device *device = eis_event_get_device(e); colorprint("new device, caps:%s%s%s%s\n", 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" : ""); setup_keymap(&server, device); /* insert sophisticated device checks here */ eis_device_connect(device); eis_device_resume(device); break; } case EIS_EVENT_DEVICE_REMOVED: { colorprint("device removed\n"); break; } case EIS_EVENT_POINTER_MOTION: { colorprint("motion by %.2f/%.2f\n", eis_event_pointer_get_dx(e), eis_event_pointer_get_dy(e)); } break; case EIS_EVENT_POINTER_BUTTON: { colorprint("button %d (%s)\n", eis_event_pointer_get_button(e), eis_event_pointer_get_button_is_press(e) ? "press" : "release"); } break; case EIS_EVENT_KEYBOARD_KEY: { handle_key(&server, eis_event_keyboard_get_key(e), eis_event_keyboard_get_key_is_press(e)); } break; default: abort(); } } } return 0; }