libei/tools/ei-socket-client.c
Peter Hutterer cb976a2f95 tools: fix a few memleaks on errors
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:12:19 +10:00

134 lines
3.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 "libei.h"
#include "src/util-mem.h"
#include "src/util-strings.h"
DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei *, ei_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct ei_device *, ei_device_unref);
int main(int argc, char **argv)
{
const char SOCKETNAME[] = "eis.socket";
_cleanup_(ei_unrefp) struct ei *ei = ei_new(NULL);
assert(ei);
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;
}
printf("client: 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);
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:
printf("client: connected\n");
ei_device_add(ptr);
ei_device_add(kbd);
break;
case EI_EVENT_DISCONNECT:
{
printf("client: disconnected us\n");
stop = true;
break;
}
case EI_EVENT_DEVICE_ADDED:
{
printf("client: our device was accepted\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_REMOVED:
{
printf("client: 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) {
printf("client: sending motion event\n");
ei_device_pointer_motion(ptr, -1, 1);
/* BTN_LEFT */
printf("client: sending button event\n");
ei_device_pointer_button(ptr, 0x110, true);
ei_device_pointer_button(ptr, 0x110, false);
}
if (have_kbd) {
printf("client: sending key event\n");
ei_device_keyboard_key(kbd, 57, true); /* KEY_SPACE */
ei_device_keyboard_key(kbd, 57, false); /* KEY_SPACE */
}
}
return 0;
}