| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <gio/gio.h> |
| 5 |
|
| 6 |
#include "internal.h" |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
struct _GpiodglibInfoEvent { |
| 16 |
GObject parent_instance; |
| 17 |
struct gpiod_info_event *handle; |
| 18 |
GpiodglibLineInfo *info; |
| 19 |
}; |
| 20 |
|
| 21 |
typedef enum { |
| 22 |
GPIODGLIB_INFO_EVENT_PROP_EVENT_TYPE = 1, |
| 23 |
GPIODGLIB_INFO_EVENT_PROP_TIMESTAMP, |
| 24 |
GPIODGLIB_INFO_EVENT_PROP_LINE_INFO, |
| 25 |
} GpiodglibInfoEventProp; |
| 26 |
|
| 27 |
G_DEFINE_TYPE(GpiodglibInfoEvent, gpiodglib_info_event, G_TYPE_OBJECT); |
| 28 |
|
| 29 |
static void gpiodglib_info_event_get_property(GObject *obj, guint prop_id, |
| 30 |
GValue *val, GParamSpec *pspec) |
| 31 |
{ |
| 32 |
GpiodglibInfoEvent *self = GPIODGLIB_INFO_EVENT_OBJ(obj); |
| 33 |
struct gpiod_line_info *info, *cpy; |
| 34 |
GpiodglibInfoEventType type; |
| 35 |
|
| 36 |
g_assert(self->handle); |
| 37 |
|
| 38 |
switch ((GpiodglibInfoEventProp)prop_id) { |
| 39 |
case GPIODGLIB_INFO_EVENT_PROP_EVENT_TYPE: |
| 40 |
type = _gpiodglib_info_event_type_from_library( |
| 41 |
gpiod_info_event_get_event_type(self->handle)); |
| 42 |
g_value_set_enum(val, type); |
| 43 |
break; |
| 44 |
case GPIODGLIB_INFO_EVENT_PROP_TIMESTAMP: |
| 45 |
g_value_set_uint64(val, |
| 46 |
gpiod_info_event_get_timestamp_ns(self->handle)); |
| 47 |
break; |
| 48 |
case GPIODGLIB_INFO_EVENT_PROP_LINE_INFO: |
| 49 |
if (!self->info) { |
| 50 |
info = gpiod_info_event_get_line_info(self->handle); |
| 51 |
cpy = gpiod_line_info_copy(info); |
| 52 |
if (!cpy) |
| 53 |
g_error("Failed to allocate memory for line-info object"); |
| 54 |
|
| 55 |
self->info = _gpiodglib_line_info_new(cpy); |
| 56 |
} |
| 57 |
|
| 58 |
g_value_set_object(val, self->info); |
| 59 |
break; |
| 60 |
default: |
| 61 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
static void gpiodglib_info_event_dispose(GObject *obj) |
| 66 |
{ |
| 67 |
GpiodglibInfoEvent *self = GPIODGLIB_INFO_EVENT_OBJ(obj); |
| 68 |
|
| 69 |
g_clear_object(&self->info); |
| 70 |
|
| 71 |
G_OBJECT_CLASS(gpiodglib_info_event_parent_class)->dispose(obj); |
| 72 |
} |
| 73 |
|
| 74 |
static void gpiodglib_info_event_finalize(GObject *obj) |
| 75 |
{ |
| 76 |
GpiodglibInfoEvent *self = GPIODGLIB_INFO_EVENT_OBJ(obj); |
| 77 |
|
| 78 |
g_clear_pointer(&self->handle, gpiod_info_event_free); |
| 79 |
|
| 80 |
G_OBJECT_CLASS(gpiodglib_info_event_parent_class)->finalize(obj); |
| 81 |
} |
| 82 |
|
| 83 |
static void gpiodglib_info_event_class_init(GpiodglibInfoEventClass *info_event_class) |
| 84 |
{ |
| 85 |
GObjectClass *class = G_OBJECT_CLASS(info_event_class); |
| 86 |
|
| 87 |
class->get_property = gpiodglib_info_event_get_property; |
| 88 |
class->dispose = gpiodglib_info_event_dispose; |
| 89 |
class->finalize = gpiodglib_info_event_finalize; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
g_object_class_install_property(class, |
| 99 |
GPIODGLIB_INFO_EVENT_PROP_EVENT_TYPE, |
| 100 |
g_param_spec_enum("event-type", "Event Type", |
| 101 |
"Type of the info event.", |
| 102 |
GPIODGLIB_INFO_EVENT_TYPE_TYPE, |
| 103 |
GPIODGLIB_INFO_EVENT_LINE_REQUESTED, |
| 104 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
g_object_class_install_property(class, |
| 112 |
GPIODGLIB_INFO_EVENT_PROP_TIMESTAMP, |
| 113 |
g_param_spec_uint64("timestamp-ns", |
| 114 |
"Timestamp (in nanoseconds).", |
| 115 |
"Timestamp of the info event expressed in nanoseconds.", |
| 116 |
0, G_MAXUINT64, 0, |
| 117 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
g_object_class_install_property(class, |
| 125 |
GPIODGLIB_INFO_EVENT_PROP_LINE_INFO, |
| 126 |
g_param_spec_object("line-info", "Line Info", |
| 127 |
"New line-info snapshot associated with this info event.", |
| 128 |
GPIODGLIB_LINE_INFO_TYPE, |
| 129 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 130 |
} |
| 131 |
|
| 132 |
static void gpiodglib_info_event_init(GpiodglibInfoEvent *self) |
| 133 |
{ |
| 134 |
self->handle = NULL; |
| 135 |
self->info = NULL; |
| 136 |
} |
| 137 |
|
| 138 |
GpiodglibInfoEventType gpiodglib_info_event_get_event_type(GpiodglibInfoEvent *self) |
| 139 |
{ |
| 140 |
return _gpiodglib_get_prop_enum(G_OBJECT(self), "event-type"); |
| 141 |
} |
| 142 |
|
| 143 |
guint64 gpiodglib_info_event_get_timestamp_ns(GpiodglibInfoEvent *self) |
| 144 |
{ |
| 145 |
return _gpiodglib_get_prop_uint64(G_OBJECT(self), "timestamp-ns"); |
| 146 |
} |
| 147 |
|
| 148 |
GpiodglibLineInfo *gpiodglib_info_event_get_line_info(GpiodglibInfoEvent *self) |
| 149 |
{ |
| 150 |
return GPIODGLIB_LINE_INFO_OBJ( |
| 151 |
_gpiodglib_get_prop_object(G_OBJECT(self), "line-info")); |
| 152 |
} |
| 153 |
|
| 154 |
GpiodglibInfoEvent *_gpiodglib_info_event_new(struct gpiod_info_event *handle) |
| 155 |
{ |
| 156 |
GpiodglibInfoEvent *event; |
| 157 |
|
| 158 |
event = GPIODGLIB_INFO_EVENT_OBJ( |
| 159 |
g_object_new(GPIODGLIB_INFO_EVENT_TYPE, NULL)); |
| 160 |
event->handle = handle; |
| 161 |
|
| 162 |
return event; |
| 163 |
} |