ei: split event handling into its own source file

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2021-08-23 09:17:33 +10:00
parent f8c2078bd3
commit b08ec3cea4
5 changed files with 143 additions and 62 deletions

View file

@ -45,6 +45,7 @@ src_libei = [
'src/libei.h',
'src/libei.c',
'src/libei-device.c',
'src/libei-event.c',
'src/libei-log.c',
'src/libei-seat.c',
'src/libei-socket.c',

123
src/libei-event.c Normal file
View file

@ -0,0 +1,123 @@
/*
* Copyright © 2021 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 "libei-private.h"
OBJECT_IMPLEMENT_REF(ei_event);
_public_
OBJECT_IMPLEMENT_UNREF(ei_event);
_public_
OBJECT_IMPLEMENT_GETTER(ei_event, type, enum ei_event_type);
_public_
OBJECT_IMPLEMENT_GETTER(ei_event, device, struct ei_device*);
_public_
OBJECT_IMPLEMENT_GETTER(ei_event, seat, struct ei_seat*);
const char *
ei_event_type_to_string(enum ei_event_type type)
{
switch(type) {
CASE_RETURN_STRING(EI_EVENT_CONNECT);
CASE_RETURN_STRING(EI_EVENT_DISCONNECT);
CASE_RETURN_STRING(EI_EVENT_SEAT_ADDED);
CASE_RETURN_STRING(EI_EVENT_SEAT_REMOVED);
CASE_RETURN_STRING(EI_EVENT_DEVICE_ADDED);
CASE_RETURN_STRING(EI_EVENT_DEVICE_REMOVED);
CASE_RETURN_STRING(EI_EVENT_DEVICE_PAUSED);
CASE_RETURN_STRING(EI_EVENT_DEVICE_RESUMED);
}
assert(!"Unhandled event type");
}
static void
ei_event_destroy(struct ei_event *event)
{
switch (event->type) {
case EI_EVENT_CONNECT:
case EI_EVENT_DISCONNECT:
case EI_EVENT_SEAT_ADDED:
case EI_EVENT_SEAT_REMOVED:
case EI_EVENT_DEVICE_ADDED:
case EI_EVENT_DEVICE_REMOVED:
case EI_EVENT_DEVICE_PAUSED:
case EI_EVENT_DEVICE_RESUMED:
break;
}
ei_device_unref(event->device);
ei_seat_unref(event->seat);
}
static
OBJECT_IMPLEMENT_CREATE(ei_event);
static
OBJECT_IMPLEMENT_PARENT(ei_event, ei);
struct ei_event *
ei_event_new(struct ei *ei)
{
return ei_event_create(&ei->object);
}
static struct ei *
ei_event_get_context(struct ei_event *event)
{
return ei_event_parent(event);
}
static inline bool
check_event_type(struct ei_event *event,
const char *function_name,
...)
{
bool rc = false;
va_list args;
unsigned int type_permitted;
enum ei_event_type type = ei_event_get_type(event);
va_start(args, function_name);
type_permitted = va_arg(args, unsigned int);
while (type_permitted != (unsigned int)-1) {
if (type_permitted == type) {
rc = true;
break;
}
type_permitted = va_arg(args, unsigned int);
}
va_end(args);
if (!rc)
log_bug_client(ei_event_get_context(event),
"Invalid event type %d passed to %s()\n",
type, function_name);
return rc;
}
#define require_event_type(event_, retval_, ...) \
if (!check_event_type(event_, __func__, __VA_ARGS__, -1)) \
return retval_; \

View file

@ -154,13 +154,22 @@ struct ei_touch {
};
struct ei_event {
struct object object;
struct object object; /* Parent is struct ei */
enum ei_event_type type;
struct list link;
struct ei_seat *seat; /* NULL if device is non-NULL */
struct ei_device *device;
};
struct ei_event *
ei_event_new(struct ei *ei);
struct ei_event *
ei_event_ref(struct ei_event *event);
const char *
ei_event_type_to_string(enum ei_event_type type);
int
ei_set_connection(struct ei *ei, int fd);

View file

@ -39,57 +39,6 @@
#include "libei-proto.h"
#include "brei-shared.h"
static const char *
ei_event_type_to_string(enum ei_event_type type)
{
switch(type) {
CASE_RETURN_STRING(EI_EVENT_CONNECT);
CASE_RETURN_STRING(EI_EVENT_DISCONNECT);
CASE_RETURN_STRING(EI_EVENT_SEAT_ADDED);
CASE_RETURN_STRING(EI_EVENT_SEAT_REMOVED);
CASE_RETURN_STRING(EI_EVENT_DEVICE_ADDED);
CASE_RETURN_STRING(EI_EVENT_DEVICE_REMOVED);
CASE_RETURN_STRING(EI_EVENT_DEVICE_PAUSED);
CASE_RETURN_STRING(EI_EVENT_DEVICE_RESUMED);
}
assert(!"Unhandled event type");
}
static void
ei_event_destroy(struct ei_event *event)
{
switch (event->type) {
case EI_EVENT_CONNECT:
case EI_EVENT_DISCONNECT:
case EI_EVENT_SEAT_ADDED:
case EI_EVENT_SEAT_REMOVED:
case EI_EVENT_DEVICE_ADDED:
case EI_EVENT_DEVICE_REMOVED:
case EI_EVENT_DEVICE_PAUSED:
case EI_EVENT_DEVICE_RESUMED:
break;
default:
assert(!"destroy not implemented for this type");
}
ei_device_unref(event->device);
ei_seat_unref(event->seat);
}
static
OBJECT_IMPLEMENT_CREATE(ei_event);
static
OBJECT_IMPLEMENT_REF(ei_event);
_public_
OBJECT_IMPLEMENT_UNREF(ei_event);
_public_
OBJECT_IMPLEMENT_GETTER(ei_event, type, enum ei_event_type);
_public_
OBJECT_IMPLEMENT_GETTER(ei_event, device, struct ei_device*);
_public_
OBJECT_IMPLEMENT_GETTER(ei_event, seat, struct ei_seat*);
static struct ei_seat *
ei_find_seat(struct ei *ei, uint32_t seatid)
{
@ -203,7 +152,7 @@ insert_event(struct ei *ei, struct ei_event *event)
static void
queue_connect_event(struct ei *ei)
{
struct ei_event *e = ei_event_create(&ei->object);
struct ei_event *e = ei_event_new(ei);
e->type = EI_EVENT_CONNECT;
queue_event(ei, e);
@ -212,7 +161,7 @@ queue_connect_event(struct ei *ei)
static void
queue_disconnect_event(struct ei *ei)
{
struct ei_event *e = ei_event_create(&ei->object);
struct ei_event *e = ei_event_new(ei);
e->type = EI_EVENT_DISCONNECT;
queue_event(ei, e);
@ -223,7 +172,7 @@ queue_seat_added_event(struct ei_seat *seat)
{
struct ei *ei= ei_seat_get_context(seat);
struct ei_event *e = ei_event_create(&ei->object);
struct ei_event *e = ei_event_new(ei);
e->type = EI_EVENT_SEAT_ADDED;
e->seat = ei_seat_ref(seat);
@ -235,7 +184,7 @@ queue_seat_removed_event(struct ei_seat *seat)
{
struct ei *ei= ei_seat_get_context(seat);
struct ei_event *e = ei_event_create(&ei->object);
struct ei_event *e = ei_event_new(ei);
e->type = EI_EVENT_SEAT_REMOVED;
e->seat = ei_seat_ref(seat);
@ -253,7 +202,7 @@ queue_device_added_event(struct ei_device *device)
{
struct ei *ei= ei_device_get_context(device);
struct ei_event *e = ei_event_create(&ei->object);
struct ei_event *e = ei_event_new(ei);
e->type = EI_EVENT_DEVICE_ADDED;
e->seat = ei_seat_ref(ei_device_get_seat(device));
e->device = ei_device_ref(device);
@ -266,7 +215,7 @@ queue_device_removed_event(struct ei_device *device)
{
struct ei *ei= ei_device_get_context(device);
struct ei_event *e = ei_event_create(&ei->object);
struct ei_event *e = ei_event_new(ei);
e->type = EI_EVENT_DEVICE_REMOVED;
e->seat = ei_seat_ref(ei_device_get_seat(device));
e->device = ei_device_ref(device);
@ -279,7 +228,7 @@ insert_device_removed_event(struct ei_device *device)
{
struct ei *ei= ei_device_get_context(device);
struct ei_event *e = ei_event_create(&ei->object);
struct ei_event *e = ei_event_new(ei);
e->type = EI_EVENT_DEVICE_REMOVED;
e->seat = ei_seat_ref(ei_device_get_seat(device));
e->device = ei_device_ref(device);
@ -292,7 +241,7 @@ queue_paused_event(struct ei_device *device)
{
struct ei *ei= ei_device_get_context(device);
struct ei_event *e = ei_event_create(&ei->object);
struct ei_event *e = ei_event_new(ei);
e->type = EI_EVENT_DEVICE_PAUSED;
e->seat = ei_seat_ref(ei_device_get_seat(device));
e->device = ei_device_ref(device);
@ -305,7 +254,7 @@ queue_resumed_event(struct ei_device *device)
{
struct ei *ei= ei_device_get_context(device);
struct ei_event *e = ei_event_create(&ei->object);
struct ei_event *e = ei_event_new(ei);
e->type = EI_EVENT_DEVICE_RESUMED;
e->seat = ei_seat_ref(ei_device_get_seat(device));
e->device = ei_device_ref(device);

View file

@ -91,7 +91,6 @@ object_unref(struct object *object)
return NULL;
}
/**
* For a type of "foo", generate
* struct foo *foo_ref(struct foo *f);