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>
216 lines
5.6 KiB
C
216 lines
5.6 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 <stdarg.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_TRIVIAL_CLEANUP_FUNC(struct ei *, ei_unref);
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_device *, ei_device_unref);
|
|
|
|
static inline void
|
|
_printf_(1, 2)
|
|
colorprint(const char *format, ...)
|
|
{
|
|
static const char *color = ANSI_BG_RGB(230, 0, 230);
|
|
static const char *reset = ansi_colorcode[RESET];
|
|
|
|
run_only_once {
|
|
if (!isatty(STDOUT_FILENO))
|
|
color = reset = "";
|
|
}
|
|
|
|
printf("%sEI socket client:%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 ei_device *kbd)
|
|
{
|
|
#if HAVE_LIBXKBCOMMON
|
|
_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 = "fr",
|
|
};
|
|
|
|
_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;
|
|
|
|
ei_device_configure_keymap(kbd, EI_KEYMAP_TYPE_XKB,
|
|
memfile_get_fd(f), memfile_get_size(f));
|
|
memfile_unref(f);
|
|
#endif
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
const char SOCKETNAME[] = "eis-0";
|
|
_cleanup_(ei_unrefp) struct ei *ei = ei_new(NULL);
|
|
assert(ei);
|
|
|
|
ei_log_set_priority(ei, EI_LOG_PRIORITY_DEBUG);
|
|
|
|
ei_configure_name(ei, "ei-socket-client-demo");
|
|
|
|
int rc = ei_setup_backend_socket(ei, getenv("LIBEI_SOCKET") ? NULL : SOCKETNAME);
|
|
|
|
if (rc != 0) {
|
|
fprintf(stderr, "init failed: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
colorprint("connected to %s\n", SOCKETNAME);
|
|
|
|
struct pollfd fds = {
|
|
.fd = ei_get_fd(ei),
|
|
.events = POLLIN,
|
|
.revents = 0,
|
|
};
|
|
|
|
_cleanup_(ei_device_unrefp) struct ei_device *ptr = ei_device_new(ei);
|
|
ei_device_configure_capability(ptr, EI_DEVICE_CAP_POINTER);
|
|
|
|
_cleanup_(ei_device_unrefp) struct ei_device *kbd = ei_device_new(ei);
|
|
ei_device_configure_capability(kbd, EI_DEVICE_CAP_KEYBOARD);
|
|
setup_keymap(kbd);
|
|
|
|
bool stop = false;
|
|
bool have_ptr = false;
|
|
bool have_kbd = false;
|
|
while (!stop && poll(&fds, 1, 2000) > -1) {
|
|
ei_dispatch(ei);
|
|
|
|
while (!stop) {
|
|
struct ei_event *e = ei_get_event(ei);
|
|
if (!e)
|
|
break;
|
|
|
|
switch(ei_event_get_type(e)) {
|
|
case EI_EVENT_CONNECT:
|
|
colorprint("connected\n");
|
|
ei_device_add(ptr);
|
|
ei_device_add(kbd);
|
|
break;
|
|
case EI_EVENT_DISCONNECT:
|
|
{
|
|
colorprint("disconnected us\n");
|
|
stop = true;
|
|
break;
|
|
}
|
|
case EI_EVENT_DEVICE_ADDED:
|
|
colorprint("our device was accepted, waiting for resume\n");
|
|
break;
|
|
case EI_EVENT_DEVICE_RESUMED:
|
|
colorprint("our device was resumed\n");
|
|
if (ei_event_get_device(e) == ptr)
|
|
have_ptr = true;
|
|
if (ei_event_get_device(e) == kbd)
|
|
have_kbd = true;
|
|
break;
|
|
case EI_EVENT_DEVICE_SUSPENDED:
|
|
colorprint("our device was suspended\n");
|
|
if (ei_event_get_device(e) == ptr)
|
|
have_ptr = false;
|
|
if (ei_event_get_device(e) == kbd)
|
|
have_kbd = false;
|
|
break;
|
|
case EI_EVENT_DEVICE_REMOVED:
|
|
{
|
|
colorprint("our device was removed\n");
|
|
if (ei_event_get_device(e) == ptr)
|
|
have_ptr = true;
|
|
if (ei_event_get_device(e) == kbd)
|
|
have_kbd = true;
|
|
break;
|
|
}
|
|
default:
|
|
abort();
|
|
}
|
|
ei_event_unref(e);
|
|
}
|
|
|
|
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, 0x110, true);
|
|
ei_device_pointer_button(ptr, 0x110, false);
|
|
}
|
|
|
|
if (have_kbd) {
|
|
static int key = 0;
|
|
colorprint("sending key event\n");
|
|
ei_device_keyboard_key(kbd, 16 + key, true); /* KEY_Q */
|
|
ei_device_keyboard_key(kbd, 16 + key, false); /* KEY_Q */
|
|
key = (key + 1) % 6;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|