mirror of
https://gitlab.freedesktop.org/libinput/libei.git
synced 2025-12-30 02:30:08 +01:00
Client sets layout fr, server accepts it and prints azerty for the keys pressed. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
258 lines
6.8 KiB
C
258 lines
6.8 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 <poll.h>
|
|
#include <stdio.h>
|
|
#include <signal.h>
|
|
#include <string.h>
|
|
#include <sys/mman.h>
|
|
|
|
#if HAVE_LIBXKBCOMMON
|
|
#include <xkbcommon/xkbcommon.h>
|
|
#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 {
|
|
uint32_t pad; /* avoid warning with missing libxkbcommon */
|
|
#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);
|
|
|
|
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 const char *color = ANSI_BG_RGB(255, 127, 0);
|
|
static const char *reset = ansi_colorcode[RESET];
|
|
|
|
run_only_once {
|
|
if (!isatty(STDOUT_FILENO))
|
|
color = reset = "";
|
|
}
|
|
|
|
printf("%sEIS socket server:%s ", color, reset);
|
|
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 (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);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
struct eis_server server = {0};
|
|
|
|
_cleanup_(eis_unrefp) struct eis *eis = eis_new(NULL);
|
|
assert(eis);
|
|
|
|
eis_log_set_priority(eis, EIS_LOG_PRIORITY_DEBUG);
|
|
|
|
_cleanup_unlink_free_ char *socketpath = NULL;
|
|
if (argc < 2) {
|
|
const char SOCKETNAME[] = "eis-0";
|
|
const char *xdgdir = getenv("XDG_RUNTIME_DIR");
|
|
assert(xdgdir != NULL);
|
|
socketpath = xaprintf("%s/%s", xdgdir, SOCKETNAME);
|
|
} else {
|
|
socketpath = xstrdup(argv[1]);
|
|
}
|
|
|
|
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) {
|
|
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();
|
|
}
|
|
eis_event_unref(e);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|