aboutsummaryrefslogtreecommitdiffstats
path: root/sway/input/input-manager.c
diff options
context:
space:
mode:
authorLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-10-18 21:20:00 +1000
committerLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-10-20 13:11:43 +1000
commitc006717910e5f30ca65645f701541dfa176c1392 (patch)
treec64452b7f2fe6ab481ad90c424cb14bcb0328eda /sway/input/input-manager.c
parentMerge pull request #2872 from RyanDwyer/cursor-rebase (diff)
downloadsway-c006717910e5f30ca65645f701541dfa176c1392.tar.gz
sway-c006717910e5f30ca65645f701541dfa176c1392.tar.zst
sway-c006717910e5f30ca65645f701541dfa176c1392.zip
Minor refactor of input manager
The input manager is a singleton object. Passing the sway_input_manager argument to each of its functions is unnecessary, while removing the argument makes it obvious to the caller that it's a singleton. This patch removes the argument and makes the input manager use server.input instead. On a similar note: * sway_input_manager.server is removed in favour of using the server global. * seat.input is removed because it can get it from server.input. Due to a circular dependency, creating seat0 is now done directly in server_init rather than in input_manager_create. This is because creating seats must be done after server.input is set. Lastly, it now stores the default seat name using a constant and removes a second reference to seat0 (in input_manager_get_default_seat).
Diffstat (limited to 'sway/input/input-manager.c')
-rw-r--r--sway/input/input-manager.c83
1 files changed, 34 insertions, 49 deletions
diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c
index 671f9a47..5be4143f 100644
--- a/sway/input/input-manager.c
+++ b/sway/input/input-manager.c
@@ -17,32 +17,28 @@
17#include "list.h" 17#include "list.h"
18#include "log.h" 18#include "log.h"
19 19
20static const char *default_seat = "seat0"; 20#define DEFAULT_SEAT "seat0"
21
22// TODO make me not global
23struct sway_input_manager *input_manager;
24 21
25struct input_config *current_input_config = NULL; 22struct input_config *current_input_config = NULL;
26struct seat_config *current_seat_config = NULL; 23struct seat_config *current_seat_config = NULL;
27 24
28struct sway_seat *input_manager_current_seat(struct sway_input_manager *input) { 25struct sway_seat *input_manager_current_seat(void) {
29 struct sway_seat *seat = config->handler_context.seat; 26 struct sway_seat *seat = config->handler_context.seat;
30 if (!seat) { 27 if (!seat) {
31 seat = input_manager_get_default_seat(input_manager); 28 seat = input_manager_get_default_seat();
32 } 29 }
33 return seat; 30 return seat;
34} 31}
35 32
36struct sway_seat *input_manager_get_seat( 33struct sway_seat *input_manager_get_seat(const char *seat_name) {
37 struct sway_input_manager *input, const char *seat_name) {
38 struct sway_seat *seat = NULL; 34 struct sway_seat *seat = NULL;
39 wl_list_for_each(seat, &input->seats, link) { 35 wl_list_for_each(seat, &server.input->seats, link) {
40 if (strcmp(seat->wlr_seat->name, seat_name) == 0) { 36 if (strcmp(seat->wlr_seat->name, seat_name) == 0) {
41 return seat; 37 return seat;
42 } 38 }
43 } 39 }
44 40
45 return seat_create(input, seat_name); 41 return seat_create(seat_name);
46} 42}
47 43
48char *input_device_get_identifier(struct wlr_input_device *device) { 44char *input_device_get_identifier(struct wlr_input_device *device) {
@@ -72,9 +68,9 @@ char *input_device_get_identifier(struct wlr_input_device *device) {
72} 68}
73 69
74static struct sway_input_device *input_sway_device_from_wlr( 70static struct sway_input_device *input_sway_device_from_wlr(
75 struct sway_input_manager *input, struct wlr_input_device *device) { 71 struct wlr_input_device *device) {
76 struct sway_input_device *input_device = NULL; 72 struct sway_input_device *input_device = NULL;
77 wl_list_for_each(input_device, &input->devices, link) { 73 wl_list_for_each(input_device, &server.input->devices, link) {
78 if (input_device->wlr_device == device) { 74 if (input_device->wlr_device == device) {
79 return input_device; 75 return input_device;
80 } 76 }
@@ -82,9 +78,9 @@ static struct sway_input_device *input_sway_device_from_wlr(
82 return NULL; 78 return NULL;
83} 79}
84 80
85static bool input_has_seat_configuration(struct sway_input_manager *input) { 81static bool input_has_seat_configuration(void) {
86 struct sway_seat *seat = NULL; 82 struct sway_seat *seat = NULL;
87 wl_list_for_each(seat, &input->seats, link) { 83 wl_list_for_each(seat, &server.input->seats, link) {
88 struct seat_config *seat_config = seat_get_config(seat); 84 struct seat_config *seat_config = seat_get_config(seat);
89 if (seat_config) { 85 if (seat_config) {
90 return true; 86 return true;
@@ -244,8 +240,7 @@ static void input_manager_libinput_config_pointer(
244static void handle_device_destroy(struct wl_listener *listener, void *data) { 240static void handle_device_destroy(struct wl_listener *listener, void *data) {
245 struct wlr_input_device *device = data; 241 struct wlr_input_device *device = data;
246 242
247 struct sway_input_device *input_device = 243 struct sway_input_device *input_device = input_sway_device_from_wlr(device);
248 input_sway_device_from_wlr(input_manager, device);
249 244
250 if (!sway_assert(input_device, "could not find sway device")) { 245 if (!sway_assert(input_device, "could not find sway device")) {
251 return; 246 return;
@@ -255,7 +250,7 @@ static void handle_device_destroy(struct wl_listener *listener, void *data) {
255 input_device->identifier); 250 input_device->identifier);
256 251
257 struct sway_seat *seat = NULL; 252 struct sway_seat *seat = NULL;
258 wl_list_for_each(seat, &input_manager->seats, link) { 253 wl_list_for_each(seat, &server.input->seats, link) {
259 seat_remove_device(seat, input_device); 254 seat_remove_device(seat, input_device);
260 } 255 }
261 256
@@ -297,9 +292,9 @@ static void handle_new_input(struct wl_listener *listener, void *data) {
297 input_device->device_destroy.notify = handle_device_destroy; 292 input_device->device_destroy.notify = handle_device_destroy;
298 293
299 struct sway_seat *seat = NULL; 294 struct sway_seat *seat = NULL;
300 if (!input_has_seat_configuration(input)) { 295 if (!input_has_seat_configuration()) {
301 wlr_log(WLR_DEBUG, "no seat configuration, using default seat"); 296 wlr_log(WLR_DEBUG, "no seat configuration, using default seat");
302 seat = input_manager_get_seat(input, default_seat); 297 seat = input_manager_get_seat(DEFAULT_SEAT);
303 seat_add_device(seat, input_device); 298 seat_add_device(seat, input_device);
304 return; 299 return;
305 } 300 }
@@ -364,7 +359,7 @@ void handle_virtual_keyboard(struct wl_listener *listener, void *data) {
364 struct wlr_virtual_keyboard_v1 *keyboard = data; 359 struct wlr_virtual_keyboard_v1 *keyboard = data;
365 struct wlr_input_device *device = &keyboard->input_device; 360 struct wlr_input_device *device = &keyboard->input_device;
366 361
367 struct sway_seat *seat = input_manager_get_default_seat(input_manager); 362 struct sway_seat *seat = input_manager_get_default_seat();
368 363
369 // TODO: The user might want this on a different seat 364 // TODO: The user might want this on a different seat
370 struct sway_input_device *input_device = 365 struct sway_input_device *input_device =
@@ -387,21 +382,16 @@ void handle_virtual_keyboard(struct wl_listener *listener, void *data) {
387 seat_add_device(seat, input_device); 382 seat_add_device(seat, input_device);
388} 383}
389 384
390struct sway_input_manager *input_manager_create( 385struct sway_input_manager *input_manager_create(struct sway_server *server) {
391 struct sway_server *server) {
392 struct sway_input_manager *input = 386 struct sway_input_manager *input =
393 calloc(1, sizeof(struct sway_input_manager)); 387 calloc(1, sizeof(struct sway_input_manager));
394 if (!input) { 388 if (!input) {
395 return NULL; 389 return NULL;
396 } 390 }
397 input->server = server;
398 391
399 wl_list_init(&input->devices); 392 wl_list_init(&input->devices);
400 wl_list_init(&input->seats); 393 wl_list_init(&input->seats);
401 394
402 // create the default seat
403 input_manager_get_seat(input, default_seat);
404
405 input->new_input.notify = handle_new_input; 395 input->new_input.notify = handle_new_input;
406 wl_signal_add(&server->backend->events.new_input, &input->new_input); 396 wl_signal_add(&server->backend->events.new_input, &input->new_input);
407 397
@@ -422,10 +412,9 @@ struct sway_input_manager *input_manager_create(
422 return input; 412 return input;
423} 413}
424 414
425bool input_manager_has_focus(struct sway_input_manager *input, 415bool input_manager_has_focus(struct sway_node *node) {
426 struct sway_node *node) {
427 struct sway_seat *seat = NULL; 416 struct sway_seat *seat = NULL;
428 wl_list_for_each(seat, &input->seats, link) { 417 wl_list_for_each(seat, &server.input->seats, link) {
429 if (seat_get_focus(seat) == node) { 418 if (seat_get_focus(seat) == node) {
430 return true; 419 return true;
431 } 420 }
@@ -434,19 +423,17 @@ bool input_manager_has_focus(struct sway_input_manager *input,
434 return false; 423 return false;
435} 424}
436 425
437void input_manager_set_focus(struct sway_input_manager *input, 426void input_manager_set_focus(struct sway_node *node) {
438 struct sway_node *node) {
439 struct sway_seat *seat; 427 struct sway_seat *seat;
440 wl_list_for_each(seat, &input->seats, link) { 428 wl_list_for_each(seat, &server.input->seats, link) {
441 seat_set_focus(seat, node); 429 seat_set_focus(seat, node);
442 } 430 }
443} 431}
444 432
445void input_manager_apply_input_config(struct sway_input_manager *input, 433void input_manager_apply_input_config(struct input_config *input_config) {
446 struct input_config *input_config) {
447 struct sway_input_device *input_device = NULL; 434 struct sway_input_device *input_device = NULL;
448 bool wildcard = strcmp(input_config->identifier, "*") == 0; 435 bool wildcard = strcmp(input_config->identifier, "*") == 0;
449 wl_list_for_each(input_device, &input->devices, link) { 436 wl_list_for_each(input_device, &server.input->devices, link) {
450 if (strcmp(input_device->identifier, input_config->identifier) == 0 437 if (strcmp(input_device->identifier, input_config->identifier) == 0
451 || wildcard) { 438 || wildcard) {
452 if (input_device->wlr_device->type == WLR_INPUT_DEVICE_POINTER || 439 if (input_device->wlr_device->type == WLR_INPUT_DEVICE_POINTER ||
@@ -459,18 +446,17 @@ void input_manager_apply_input_config(struct sway_input_manager *input,
459 } 446 }
460 447
461 struct sway_seat *seat = NULL; 448 struct sway_seat *seat = NULL;
462 wl_list_for_each(seat, &input->seats, link) { 449 wl_list_for_each(seat, &server.input->seats, link) {
463 seat_configure_device(seat, input_device); 450 seat_configure_device(seat, input_device);
464 } 451 }
465 } 452 }
466 } 453 }
467} 454}
468 455
469void input_manager_apply_seat_config(struct sway_input_manager *input, 456void input_manager_apply_seat_config(struct seat_config *seat_config) {
470 struct seat_config *seat_config) {
471 wlr_log(WLR_DEBUG, "applying new seat config for seat %s", 457 wlr_log(WLR_DEBUG, "applying new seat config for seat %s",
472 seat_config->name); 458 seat_config->name);
473 struct sway_seat *seat = input_manager_get_seat(input, seat_config->name); 459 struct sway_seat *seat = input_manager_get_seat(seat_config->name);
474 if (!seat) { 460 if (!seat) {
475 return; 461 return;
476 } 462 }
@@ -480,10 +466,10 @@ void input_manager_apply_seat_config(struct sway_input_manager *input,
480 // for every device, try to add it to a seat and if no seat has it 466 // for every device, try to add it to a seat and if no seat has it
481 // attached, add it to the fallback seats. 467 // attached, add it to the fallback seats.
482 struct sway_input_device *input_device = NULL; 468 struct sway_input_device *input_device = NULL;
483 wl_list_for_each(input_device, &input->devices, link) { 469 wl_list_for_each(input_device, &server.input->devices, link) {
484 list_t *seat_list = create_list(); 470 list_t *seat_list = create_list();
485 struct sway_seat *seat = NULL; 471 struct sway_seat *seat = NULL;
486 wl_list_for_each(seat, &input->seats, link) { 472 wl_list_for_each(seat, &server.input->seats, link) {
487 struct seat_config *seat_config = seat_get_config(seat); 473 struct seat_config *seat_config = seat_get_config(seat);
488 if (!seat_config) { 474 if (!seat_config) {
489 continue; 475 continue;
@@ -496,7 +482,7 @@ void input_manager_apply_seat_config(struct sway_input_manager *input,
496 } 482 }
497 483
498 if (seat_list->length) { 484 if (seat_list->length) {
499 wl_list_for_each(seat, &input->seats, link) { 485 wl_list_for_each(seat, &server.input->seats, link) {
500 bool attached = false; 486 bool attached = false;
501 for (int i = 0; i < seat_list->length; ++i) { 487 for (int i = 0; i < seat_list->length; ++i) {
502 if (seat == seat_list->items[i]) { 488 if (seat == seat_list->items[i]) {
@@ -511,7 +497,7 @@ void input_manager_apply_seat_config(struct sway_input_manager *input,
511 } 497 }
512 } 498 }
513 } else { 499 } else {
514 wl_list_for_each(seat, &input->seats, link) { 500 wl_list_for_each(seat, &server.input->seats, link) {
515 struct seat_config *seat_config = seat_get_config(seat); 501 struct seat_config *seat_config = seat_get_config(seat);
516 if (seat_config && seat_config->fallback == 1) { 502 if (seat_config && seat_config->fallback == 1) {
517 seat_add_device(seat, input_device); 503 seat_add_device(seat, input_device);
@@ -524,18 +510,17 @@ void input_manager_apply_seat_config(struct sway_input_manager *input,
524 } 510 }
525} 511}
526 512
527void input_manager_configure_xcursor(struct sway_input_manager *input) { 513void input_manager_configure_xcursor(void) {
528 struct sway_seat *seat = NULL; 514 struct sway_seat *seat = NULL;
529 wl_list_for_each(seat, &input->seats, link) { 515 wl_list_for_each(seat, &server.input->seats, link) {
530 seat_configure_xcursor(seat); 516 seat_configure_xcursor(seat);
531 } 517 }
532} 518}
533 519
534struct sway_seat *input_manager_get_default_seat( 520struct sway_seat *input_manager_get_default_seat(void) {
535 struct sway_input_manager *input) {
536 struct sway_seat *seat = NULL; 521 struct sway_seat *seat = NULL;
537 wl_list_for_each(seat, &input->seats, link) { 522 wl_list_for_each(seat, &server.input->seats, link) {
538 if (strcmp(seat->wlr_seat->name, "seat0") == 0) { 523 if (strcmp(seat->wlr_seat->name, DEFAULT_SEAT) == 0) {
539 return seat; 524 return seat;
540 } 525 }
541 } 526 }