mirror of
https://gitlab.freedesktop.org/libinput/libei.git
synced 2025-12-24 12:20:05 +01:00
tools/demo-server: abstract the event handler
Move this to handle_event function so we can swap out what actually deals with the events. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
parent
5d85ea4c8f
commit
f45a23e466
3 changed files with 129 additions and 73 deletions
|
|
@ -147,8 +147,14 @@ pkgconfig.generate(lib_libreis,
|
|||
|
||||
dep_libxkbcommon = dependency('xkbcommon', required: false)
|
||||
config_h.set10('HAVE_LIBXKBCOMMON', dep_libxkbcommon.found())
|
||||
|
||||
src_eis_demo_server = [
|
||||
'tools/eis-demo-server.h',
|
||||
'tools/eis-demo-server.c',
|
||||
]
|
||||
|
||||
executable('eis-demo-server',
|
||||
'tools/eis-demo-server.c',
|
||||
src_eis_demo_server,
|
||||
dependencies: [dep_libutil, dep_libeis, dep_libxkbcommon])
|
||||
|
||||
executable('ei-demo-client',
|
||||
|
|
|
|||
|
|
@ -36,21 +36,12 @@
|
|||
#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 {
|
||||
const char *layout;
|
||||
#if HAVE_LIBXKBCOMMON
|
||||
struct xkb_context *ctx;
|
||||
struct xkb_keymap *keymap;
|
||||
struct xkb_state *state;
|
||||
#endif
|
||||
};
|
||||
#include "eis-demo-server.h"
|
||||
|
||||
static bool stop = false;
|
||||
|
||||
|
|
@ -97,7 +88,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(struct xkb_state*, xkb_state_unref);
|
|||
#endif
|
||||
|
||||
static void
|
||||
setup_keymap(struct eis_server *server, struct eis_device *device)
|
||||
setup_keymap(struct eis_demo_server *server, struct eis_device *device)
|
||||
{
|
||||
#if HAVE_LIBXKBCOMMON
|
||||
if (server->layout) {
|
||||
|
|
@ -162,7 +153,7 @@ setup_keymap(struct eis_server *server, struct eis_device *device)
|
|||
}
|
||||
|
||||
static void
|
||||
handle_key(struct eis_server *server, uint32_t keycode, bool is_press)
|
||||
handle_key(struct eis_demo_server *server, uint32_t keycode, bool is_press)
|
||||
{
|
||||
char keysym_name[64] = {0};
|
||||
|
||||
|
|
@ -178,6 +169,78 @@ handle_key(struct eis_server *server, uint32_t keycode, bool is_press)
|
|||
keysym_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* The simplest event handler. Connect any client and any device and just
|
||||
* printf the events as the come in. This is an incomplete implementation,
|
||||
* it just does the basics for pointers and keyboards atm.
|
||||
*/
|
||||
static int
|
||||
eis_demo_server_printf_handle_event(struct eis_demo_server *server,
|
||||
struct eis_event *e)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
static void
|
||||
usage(FILE *fp, const char *argv0)
|
||||
{
|
||||
|
|
@ -251,8 +314,9 @@ int main(int argc, char **argv)
|
|||
}
|
||||
|
||||
|
||||
struct eis_server server = {
|
||||
struct eis_demo_server server = {
|
||||
.layout = layout,
|
||||
.handler.handle_event = eis_demo_server_printf_handle_event,
|
||||
};
|
||||
|
||||
_cleanup_eis_ struct eis *eis = eis_new(NULL);
|
||||
|
|
@ -285,66 +349,9 @@ int main(int argc, char **argv)
|
|||
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");
|
||||
int rc = server.handler.handle_event(&server, e);
|
||||
if (rc != 0)
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
43
tools/eis-demo-server.h
Normal file
43
tools/eis-demo-server.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "libeis.h"
|
||||
|
||||
struct eis_demo_server {
|
||||
const char *layout;
|
||||
#if HAVE_LIBXKBCOMMON
|
||||
struct xkb_context *ctx;
|
||||
struct xkb_keymap *keymap;
|
||||
struct xkb_state *state;
|
||||
#endif
|
||||
/* Event handler */
|
||||
struct {
|
||||
int (*handle_event)(struct eis_demo_server *server,
|
||||
struct eis_event *e);
|
||||
void *data;
|
||||
} handler;
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue