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