aboutsummaryrefslogtreecommitdiffstats
path: root/sway/input/input-manager.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2017-12-20 11:56:39 -0500
committerLibravatar GitHub <noreply@github.com>2017-12-20 11:56:39 -0500
commit373def44468e0c919031a6ffe3049f91680e05ca (patch)
treee40dfdcc9292a71fcc2160bc5d643a66996664dc /sway/input/input-manager.c
parentMerge pull request #1527 from martinetd/mesondep (diff)
parentcleanup (diff)
downloadsway-373def44468e0c919031a6ffe3049f91680e05ca.tar.gz
sway-373def44468e0c919031a6ffe3049f91680e05ca.tar.zst
sway-373def44468e0c919031a6ffe3049f91680e05ca.zip
Merge pull request #1505 from acrisci/feature/input
input management and seat
Diffstat (limited to 'sway/input/input-manager.c')
-rw-r--r--sway/input/input-manager.c293
1 files changed, 293 insertions, 0 deletions
diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c
new file mode 100644
index 00000000..4459c43b
--- /dev/null
+++ b/sway/input/input-manager.c
@@ -0,0 +1,293 @@
1#define _XOPEN_SOURCE 700
2#include <ctype.h>
3#include <float.h>
4#include <limits.h>
5#include <stdio.h>
6#include <string.h>
7#include <libinput.h>
8#include <math.h>
9#include "sway/config.h"
10#include "sway/input/input-manager.h"
11#include "sway/input/seat.h"
12#include "sway/server.h"
13#include "stringop.h"
14#include "list.h"
15#include "log.h"
16
17static const char *default_seat = "seat0";
18
19// TODO make me not global
20struct sway_input_manager *input_manager;
21
22struct input_config *current_input_config = NULL;
23struct seat_config *current_seat_config = NULL;
24
25static struct sway_seat *input_manager_get_seat(
26 struct sway_input_manager *input, const char *seat_name) {
27 struct sway_seat *seat = NULL;
28 wl_list_for_each(seat, &input->seats, link) {
29 if (strcmp(seat->wlr_seat->name, seat_name) == 0) {
30 return seat;
31 }
32 }
33
34 return sway_seat_create(input, seat_name);
35}
36
37static char *get_device_identifier(struct wlr_input_device *device) {
38 int vendor = device->vendor;
39 int product = device->product;
40 char *name = strdup(device->name);
41 name = strip_whitespace(name);
42
43 char *p = name;
44 for (; *p; ++p) {
45 if (*p == ' ') {
46 *p = '_';
47 }
48 }
49
50 const char *fmt = "%d:%d:%s";
51 int len = snprintf(NULL, 0, fmt, vendor, product, name) + 1;
52 char *identifier = malloc(len);
53 if (!identifier) {
54 sway_log(L_ERROR, "Unable to allocate unique input device name");
55 return NULL;
56 }
57
58 snprintf(identifier, len, fmt, vendor, product, name);
59 free(name);
60 return identifier;
61}
62
63static struct sway_input_device *input_sway_device_from_wlr(
64 struct sway_input_manager *input, struct wlr_input_device *device) {
65 struct sway_input_device *input_device = NULL;
66 wl_list_for_each(input_device, &input->devices, link) {
67 if (input_device->wlr_device == device) {
68 return input_device;
69 }
70 }
71 return NULL;
72}
73
74static bool input_has_seat_configuration(struct sway_input_manager *input) {
75 struct sway_seat *seat = NULL;
76 wl_list_for_each(seat, &input->seats, link) {
77 if (seat->config) {
78 return true;
79 }
80 }
81
82 return false;
83}
84
85static void input_add_notify(struct wl_listener *listener, void *data) {
86 struct sway_input_manager *input =
87 wl_container_of(listener, input, input_add);
88 struct wlr_input_device *device = data;
89
90 struct sway_input_device *input_device =
91 calloc(1, sizeof(struct sway_input_device));
92 if (!sway_assert(input_device, "could not allocate input device")) {
93 return;
94 }
95
96 input_device->wlr_device = device;
97 input_device->identifier = get_device_identifier(device);
98 wl_list_insert(&input->devices, &input_device->link);
99
100 sway_log(L_DEBUG, "adding device: '%s'",
101 input_device->identifier);
102
103 // find config
104 for (int i = 0; i < config->input_configs->length; ++i) {
105 struct input_config *input_config = config->input_configs->items[i];
106 if (strcmp(input_config->identifier, input_device->identifier) == 0) {
107 input_device->config = input_config;
108 break;
109 }
110 }
111
112 struct sway_seat *seat = NULL;
113 if (!input_has_seat_configuration(input)) {
114 sway_log(L_DEBUG, "no seat configuration, using default seat");
115 seat = input_manager_get_seat(input, default_seat);
116 sway_seat_add_device(seat, input_device);
117 return;
118 }
119
120 bool added = false;
121 wl_list_for_each(seat, &input->seats, link) {
122 bool has_attachment = seat->config &&
123 (seat_config_get_attachment(seat->config, input_device->identifier) ||
124 seat_config_get_attachment(seat->config, "*"));
125
126 if (has_attachment) {
127 sway_seat_add_device(seat, input_device);
128 added = true;
129 }
130 }
131
132 if (!added) {
133 wl_list_for_each(seat, &input->seats, link) {
134 if (seat->config && seat->config->fallback == 1) {
135 sway_seat_add_device(seat, input_device);
136 added = true;
137 }
138 }
139 }
140
141 if (!added) {
142 sway_log(L_DEBUG,
143 "device '%s' is not configured on any seats",
144 input_device->identifier);
145 }
146}
147
148static void input_remove_notify(struct wl_listener *listener, void *data) {
149 struct sway_input_manager *input =
150 wl_container_of(listener, input, input_remove);
151 struct wlr_input_device *device = data;
152
153 struct sway_input_device *input_device =
154 input_sway_device_from_wlr(input, device);
155
156 if (!sway_assert(input_device, "could not find sway device")) {
157 return;
158 }
159
160 sway_log(L_DEBUG, "removing device: '%s'",
161 input_device->identifier);
162
163 struct sway_seat *seat = NULL;
164 wl_list_for_each(seat, &input->seats, link) {
165 sway_seat_remove_device(seat, input_device);
166 }
167
168 wl_list_remove(&input_device->link);
169 free(input_device->identifier);
170 free(input_device);
171}
172
173struct sway_input_manager *sway_input_manager_create(
174 struct sway_server *server) {
175 struct sway_input_manager *input =
176 calloc(1, sizeof(struct sway_input_manager));
177 if (!input) {
178 return NULL;
179 }
180 input->server = server;
181
182 wl_list_init(&input->devices);
183 wl_list_init(&input->seats);
184
185 // create the default seat
186 input_manager_get_seat(input, default_seat);
187
188 input->input_add.notify = input_add_notify;
189 wl_signal_add(&server->backend->events.input_add, &input->input_add);
190
191 input->input_remove.notify = input_remove_notify;
192 wl_signal_add(&server->backend->events.input_remove, &input->input_remove);
193
194 return input;
195}
196
197bool sway_input_manager_has_focus(struct sway_input_manager *input,
198 swayc_t *container) {
199 struct sway_seat *seat = NULL;
200 wl_list_for_each(seat, &input->seats, link) {
201 if (seat->focus == container) {
202 return true;
203 }
204 }
205
206 return false;
207}
208
209void sway_input_manager_set_focus(struct sway_input_manager *input,
210 swayc_t *container) {
211 struct sway_seat *seat ;
212 wl_list_for_each(seat, &input->seats, link) {
213 sway_seat_set_focus(seat, container);
214 }
215}
216
217void sway_input_manager_apply_input_config(struct sway_input_manager *input,
218 struct input_config *input_config) {
219 struct sway_input_device *input_device = NULL;
220 wl_list_for_each(input_device, &input->devices, link) {
221 if (strcmp(input_device->identifier, input_config->identifier) == 0) {
222 input_device->config = input_config;
223
224 struct sway_seat *seat = NULL;
225 wl_list_for_each(seat, &input->seats, link) {
226 sway_seat_configure_device(seat, input_device);
227 }
228 }
229 }
230}
231
232void sway_input_manager_apply_seat_config(struct sway_input_manager *input,
233 struct seat_config *seat_config) {
234 sway_log(L_DEBUG, "applying new seat config for seat %s",
235 seat_config->name);
236 struct sway_seat *seat = input_manager_get_seat(input, seat_config->name);
237 if (!seat) {
238 return;
239 }
240
241 sway_seat_set_config(seat, seat_config);
242
243 // for every device, try to add it to a seat and if no seat has it
244 // attached, add it to the fallback seats.
245 struct sway_input_device *input_device = NULL;
246 wl_list_for_each(input_device, &input->devices, link) {
247 list_t *seat_list = create_list();
248 struct sway_seat *seat = NULL;
249 wl_list_for_each(seat, &input->seats, link) {
250 if (!seat->config) {
251 continue;
252 }
253 if (seat_config_get_attachment(seat->config, "*") ||
254 seat_config_get_attachment(seat->config,
255 input_device->identifier)) {
256 list_add(seat_list, seat);
257 }
258 }
259
260 if (seat_list->length) {
261 wl_list_for_each(seat, &input->seats, link) {
262 bool attached = false;
263 for (int i = 0; i < seat_list->length; ++i) {
264 if (seat == seat_list->items[i]) {
265 attached = true;
266 break;
267 }
268 }
269 if (attached) {
270 sway_seat_add_device(seat, input_device);
271 } else {
272 sway_seat_remove_device(seat, input_device);
273 }
274 }
275 } else {
276 wl_list_for_each(seat, &input->seats, link) {
277 if (seat->config && seat->config->fallback == 1) {
278 sway_seat_add_device(seat, input_device);
279 } else {
280 sway_seat_remove_device(seat, input_device);
281 }
282 }
283 }
284 list_free(seat_list);
285 }
286}
287
288void sway_input_manager_configure_xcursor(struct sway_input_manager *input) {
289 struct sway_seat *seat = NULL;
290 wl_list_for_each(seat, &input->seats, link) {
291 sway_seat_configure_xcursor(seat);
292 }
293}