chiark / gitweb /
terminal: sysview: don't return uninitialized error codes
[elogind.git] / src / libsystemd-terminal / idev-internal.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #pragma once
23
24 #include <inttypes.h>
25 #include <libudev.h>
26 #include <linux/input.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <systemd/sd-bus.h>
30 #include <systemd/sd-event.h>
31 #include <xkbcommon/xkbcommon.h>
32 #include "hashmap.h"
33 #include "idev.h"
34 #include "list.h"
35 #include "util.h"
36
37 typedef struct idev_link                idev_link;
38 typedef struct idev_device_vtable       idev_device_vtable;
39 typedef struct idev_element             idev_element;
40 typedef struct idev_element_vtable      idev_element_vtable;
41
42 /*
43  * Evdev Elements
44  */
45
46 bool idev_is_evdev(idev_element *e);
47 idev_element *idev_find_evdev(idev_session *s, dev_t devnum);
48 int idev_evdev_new(idev_element **out, idev_session *s, struct udev_device *ud);
49
50 /*
51  * Keyboard Devices
52  */
53
54 bool idev_is_keyboard(idev_device *d);
55 idev_device *idev_find_keyboard(idev_session *s, const char *name);
56 int idev_keyboard_new(idev_device **out, idev_session *s, const char *name);
57
58 /*
59  * Element Links
60  */
61
62 struct idev_link {
63         /* element-to-device connection */
64         LIST_FIELDS(idev_link, links_by_element);
65         idev_element *element;
66
67         /* device-to-element connection */
68         LIST_FIELDS(idev_link, links_by_device);
69         idev_device *device;
70 };
71
72 /*
73  * Devices
74  */
75
76 struct idev_device_vtable {
77         void (*free) (idev_device *d);
78         void (*attach) (idev_device *d, idev_link *l);
79         void (*detach) (idev_device *d, idev_link *l);
80         int (*feed) (idev_device *d, idev_data *data);
81 };
82
83 struct idev_device {
84         const idev_device_vtable *vtable;
85         idev_session *session;
86         char *name;
87
88         LIST_HEAD(idev_link, links);
89
90         bool public : 1;
91         bool enabled : 1;
92 };
93
94 #define IDEV_DEVICE_INIT(_vtable, _session) ((idev_device){ \
95                 .vtable = (_vtable), \
96                 .session = (_session), \
97         })
98
99 idev_device *idev_find_device(idev_session *s, const char *name);
100
101 int idev_device_add(idev_device *d, const char *name);
102 idev_device *idev_device_free(idev_device *d);
103
104 DEFINE_TRIVIAL_CLEANUP_FUNC(idev_device*, idev_device_free);
105
106 int idev_device_feed(idev_device *d, idev_data *data);
107 void idev_device_feedback(idev_device *d, idev_data *data);
108
109 /*
110  * Elements
111  */
112
113 struct idev_element_vtable {
114         void (*free) (idev_element *e);
115         void (*enable) (idev_element *e);
116         void (*disable) (idev_element *e);
117         void (*open) (idev_element *e);
118         void (*close) (idev_element *e);
119         void (*feedback) (idev_element *e, idev_data *data);
120 };
121
122 struct idev_element {
123         const idev_element_vtable *vtable;
124         idev_session *session;
125         unsigned long n_open;
126         char *name;
127
128         LIST_HEAD(idev_link, links);
129
130         bool enabled : 1;
131         bool readable : 1;
132         bool writable : 1;
133 };
134
135 #define IDEV_ELEMENT_INIT(_vtable, _session) ((idev_element){ \
136                 .vtable = (_vtable), \
137                 .session = (_session), \
138         })
139
140 idev_element *idev_find_element(idev_session *s, const char *name);
141
142 int idev_element_add(idev_element *e, const char *name);
143 idev_element *idev_element_free(idev_element *e);
144
145 DEFINE_TRIVIAL_CLEANUP_FUNC(idev_element*, idev_element_free);
146
147 int idev_element_feed(idev_element *e, idev_data *data);
148 void idev_element_feedback(idev_element *e, idev_data *data);
149
150 /*
151  * Sessions
152  */
153
154 struct idev_session {
155         idev_context *context;
156         char *name;
157         char *path;
158
159         Hashmap *element_map;
160         Hashmap *device_map;
161
162         idev_event_fn event_fn;
163         void *userdata;
164
165         bool custom : 1;
166         bool managed : 1;
167         bool enabled : 1;
168 };
169
170 idev_session *idev_find_session(idev_context *c, const char *name);
171 int idev_session_raise_device_data(idev_session *s, idev_device *d, idev_data *data);
172
173 /*
174  * Contexts
175  */
176
177 struct idev_context {
178         unsigned long ref;
179         sd_event *event;
180         sd_bus *sysbus;
181
182         Hashmap *session_map;
183         Hashmap *data_map;
184 };