chiark / gitweb /
3301ebf6e41f0cc372c6dfe4b3b3af116f56587f
[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 "hashmap.h"
32 #include "idev.h"
33 #include "list.h"
34 #include "util.h"
35
36 typedef struct idev_link                idev_link;
37 typedef struct idev_device_vtable       idev_device_vtable;
38 typedef struct idev_element             idev_element;
39 typedef struct idev_element_vtable      idev_element_vtable;
40
41 /*
42  * Evdev Elements
43  */
44
45 bool idev_is_evdev(idev_element *e);
46 idev_element *idev_find_evdev(idev_session *s, dev_t devnum);
47 int idev_evdev_new(idev_element **out, idev_session *s, struct udev_device *ud);
48
49 /*
50  * Element Links
51  */
52
53 struct idev_link {
54         /* element-to-device connection */
55         LIST_FIELDS(idev_link, links_by_element);
56         idev_element *element;
57
58         /* device-to-element connection */
59         LIST_FIELDS(idev_link, links_by_device);
60         idev_device *device;
61 };
62
63 /*
64  * Devices
65  */
66
67 struct idev_device_vtable {
68         void (*free) (idev_device *d);
69         void (*attach) (idev_device *d, idev_link *l);
70         void (*detach) (idev_device *d, idev_link *l);
71         int (*feed) (idev_device *d, idev_data *data);
72 };
73
74 struct idev_device {
75         const idev_device_vtable *vtable;
76         idev_session *session;
77         char *name;
78
79         LIST_HEAD(idev_link, links);
80
81         bool public : 1;
82         bool enabled : 1;
83 };
84
85 #define IDEV_DEVICE_INIT(_vtable, _session) ((idev_device){ \
86                 .vtable = (_vtable), \
87                 .session = (_session), \
88         })
89
90 idev_device *idev_find_device(idev_session *s, const char *name);
91
92 int idev_device_add(idev_device *d, const char *name);
93 idev_device *idev_device_free(idev_device *d);
94
95 DEFINE_TRIVIAL_CLEANUP_FUNC(idev_device*, idev_device_free);
96
97 int idev_device_feed(idev_device *d, idev_data *data);
98 void idev_device_feedback(idev_device *d, idev_data *data);
99
100 /*
101  * Elements
102  */
103
104 struct idev_element_vtable {
105         void (*free) (idev_element *e);
106         void (*enable) (idev_element *e);
107         void (*disable) (idev_element *e);
108         void (*open) (idev_element *e);
109         void (*close) (idev_element *e);
110         void (*feedback) (idev_element *e, idev_data *data);
111 };
112
113 struct idev_element {
114         const idev_element_vtable *vtable;
115         idev_session *session;
116         unsigned long n_open;
117         char *name;
118
119         LIST_HEAD(idev_link, links);
120
121         bool enabled : 1;
122         bool readable : 1;
123         bool writable : 1;
124 };
125
126 #define IDEV_ELEMENT_INIT(_vtable, _session) ((idev_element){ \
127                 .vtable = (_vtable), \
128                 .session = (_session), \
129         })
130
131 idev_element *idev_find_element(idev_session *s, const char *name);
132
133 int idev_element_add(idev_element *e, const char *name);
134 idev_element *idev_element_free(idev_element *e);
135
136 DEFINE_TRIVIAL_CLEANUP_FUNC(idev_element*, idev_element_free);
137
138 int idev_element_feed(idev_element *e, idev_data *data);
139 void idev_element_feedback(idev_element *e, idev_data *data);
140
141 /*
142  * Sessions
143  */
144
145 struct idev_session {
146         idev_context *context;
147         char *name;
148         char *path;
149
150         Hashmap *element_map;
151         Hashmap *device_map;
152
153         idev_event_fn event_fn;
154         void *userdata;
155
156         bool custom : 1;
157         bool managed : 1;
158         bool enabled : 1;
159 };
160
161 idev_session *idev_find_session(idev_context *c, const char *name);
162 int idev_session_raise_device_data(idev_session *s, idev_device *d, idev_data *data);
163
164 /*
165  * Contexts
166  */
167
168 struct idev_context {
169         unsigned long ref;
170         sd_event *event;
171         sd_bus *sysbus;
172
173         Hashmap *session_map;
174         Hashmap *data_map;
175 };