libei/src/libeis-device.c
Peter Hutterer f7a24b2fbd Add the minimal implementation for a UNIX socket libeis server
This is the minimum framework to support new clients, added devices and
pointer relative motion events. It's missing a bunch of checks and
verification, most of the server hooks aren't there yet, the only
implementation is a UNIX socket and the protocol is plain text (but at least
the last two makes it netcat-compatible).

Protocol is plain text for now and interaction is like this (S is server, C is client):
S: hello
C: connect myclientname
S: connected
C: add 2 4
S: accept 2
C: rel 2 -1 1
C: rel 2 5 4

Where the last two lines are: add device with id 2 and capability mask 0x4,
send a relative pointer motion event for device 2 with coordinates -1/1, then
5/4.

The implementation relies heavily on some abstraction and macros galore, see
the various util-* files. These are largely copied from libinput, with a few
parts removed and a few other parts added.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-28 19:33:34 +10:00

102 lines
2.7 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 <errno.h>
#include "util-macros.h"
#include "util-bits.h"
#include "libeis-private.h"
static void
eis_device_destroy(struct eis_device *device)
{
}
OBJECT_DECLARE_REF(eis_device);
OBJECT_DECLARE_UNREF(eis_device);
OBJECT_DECLARE_CREATE(eis_device);
static
OBJECT_DECLARE_PARENT(eis_device, eis_client);
_public_ struct eis_client *
eis_device_get_client(struct eis_device *device)
{
return eis_device_parent(device);
}
struct eis_device *
eis_device_new(struct eis_client *client,
uint32_t id,
uint32_t capabilities)
{
struct eis_device *device = eis_device_create(&client->object);
device->capabilities = capabilities;
device->id = id;
device->state = EIS_DEVICE_STATE_NEW;
return device;
}
_public_
bool
eis_device_has_capability(struct eis_device *device,
enum eis_device_capability cap)
{
switch (cap) {
case EIS_DEVICE_CAP_POINTER:
case EIS_DEVICE_CAP_POINTER_ABSOLUTE:
case EIS_DEVICE_CAP_KEYBOARD:
case EIS_DEVICE_CAP_TOUCH:
return flag_is_set(device->capabilities, cap);
}
return false;
}
int
eis_device_pointer_rel(struct eis_device *device,
int x, int y)
{
if (!eis_device_has_capability(device, EIS_DEVICE_CAP_POINTER))
return -EINVAL;
if (device->state != EIS_DEVICE_STATE_ACCEPTED)
return -EINVAL;
eis_queue_pointer_rel_event(device, x, y);
return 0;
}
_public_ void
eis_device_connect(struct eis_device *device)
{
if (device->state != EIS_DEVICE_STATE_NEW)
return;
device->state = EIS_DEVICE_STATE_ACCEPTED;
eis_client_connect_device(eis_device_get_client(device), device);
}