libei/tools/eis-socket-server.c
Peter Hutterer d4e274ee6f libeis: flatten the event hierarchy
Same as already done for libei. There's relatively little benefit here since
we won't have a lot of different events and any caller will do the switch
based on the event type anyway. So let's just export a single event type and
have everything contained in that.

Since this required rewriting all getters, let's move the lot to a new file
and streamline things a bit that way.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-18 16:05:52 +10:00

156 lines
4.3 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 "libeis.h"
#include "src/util-mem.h"
#include "src/util-strings.h"
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)
int main(int argc, char **argv)
{
_cleanup_(eis_unrefp) struct eis *eis = eis_new(NULL);
assert(eis);
_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;
}
printf("server: 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);
printf("server: new client: %s\n", eis_client_get_name(client));
/* insert sophisticated authentication here */
eis_client_connect(client);
printf("server: accepting client\n");
break;
}
case EIS_EVENT_CLIENT_DISCONNECT:
{
struct eis_client *client = eis_event_get_client(e);
printf("server: 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);
printf("server: 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" : "");
/* insert sophisticated device checks here */
eis_device_connect(device);
break;
}
case EIS_EVENT_DEVICE_REMOVED:
{
printf("server: device removed\n");
break;
}
case EIS_EVENT_POINTER_MOTION:
{
printf("server: motion by %.2f/%.2f\n",
eis_event_pointer_get_x(e),
eis_event_pointer_get_y(e));
}
break;
case EIS_EVENT_POINTER_BUTTON:
{
printf("server: 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:
{
printf("server: key %d (%s)\n",
eis_event_keyboard_get_key(e),
eis_event_keyboard_get_key_is_press(e) ? "press" : "release");
}
break;
default:
abort();
}
eis_event_unref(e);
}
}
return 0;
}