diff --git a/meson.build b/meson.build index d2c28c9..9131343 100644 --- a/meson.build +++ b/meson.build @@ -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', diff --git a/src/libei-event.c b/src/libei-event.c new file mode 100644 index 0000000..a970b97 --- /dev/null +++ b/src/libei-event.c @@ -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_; \ + diff --git a/src/libei-private.h b/src/libei-private.h index 3c94ba0..63131aa 100644 --- a/src/libei-private.h +++ b/src/libei-private.h @@ -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); diff --git a/src/libei.c b/src/libei.c index 2e64d8f..ac70e68 100644 --- a/src/libei.c +++ b/src/libei.c @@ -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); diff --git a/src/util-object.h b/src/util-object.h index 336368a..890388d 100644 --- a/src/util-object.h +++ b/src/util-object.h @@ -91,7 +91,6 @@ object_unref(struct object *object) return NULL; } - /** * For a type of "foo", generate * struct foo *foo_ref(struct foo *f);