commit 11c628c

Michael Forney  ·  2019-08-30 02:51:41 +0000 UTC
parent c49dfd7
keyboard: Move away from global state
3 files changed,  +34, -26
+22, -13
 1@@ -175,33 +175,39 @@ error0:
 2 	return false;
 3 }
 4 
 5-bool
 6-keyboard_initialize(struct keyboard *keyboard)
 7+struct keyboard *
 8+keyboard_create(void)
 9 {
10-	struct xkb *xkb = &keyboard->xkb;
11+	struct keyboard *keyboard;
12+	struct xkb *xkb;
13 
14+	keyboard = malloc(sizeof(*keyboard));
15+	if (!keyboard)
16+		goto error0;
17+
18+	xkb = &keyboard->xkb;
19 	if (!(xkb->context = xkb_context_new(0))) {
20 		ERROR("Could not create XKB context\n");
21-		goto error0;
22+		goto error1;
23 	}
24 
25 	if (!(xkb->keymap.map = xkb_keymap_new_from_names(xkb->context, NULL, 0))) {
26 		ERROR("Could not create XKB keymap\n");
27-		goto error1;
28+		goto error2;
29 	}
30 
31 	if (!(xkb->state = xkb_state_new(xkb->keymap.map))) {
32 		ERROR("Could not create XKB state\n");
33-		goto error2;
34+		goto error3;
35 	}
36 
37 	if (!update_keymap(xkb)) {
38 		ERROR("Could not update XKB keymap\n");
39-		goto error3;
40+		goto error4;
41 	}
42 
43 	if (!input_focus_initialize(&keyboard->focus, &keyboard->focus_handler))
44-		goto error3;
45+		goto error4;
46 
47 	keyboard->modifier_state = (struct keyboard_modifier_state){0};
48 	keyboard->modifiers = 0;
49@@ -214,20 +220,22 @@ keyboard_initialize(struct keyboard *keyboard)
50 	wl_list_init(&keyboard->handlers);
51 	wl_list_insert(&keyboard->handlers, &keyboard->client_handler.link);
52 
53-	return true;
54+	return keyboard;
55 
56-error3:
57+error4:
58 	xkb_state_unref(keyboard->xkb.state);
59-error2:
60+error3:
61 	xkb_keymap_unref(keyboard->xkb.keymap.map);
62-error1:
63+error2:
64 	xkb_context_unref(keyboard->xkb.context);
65+error1:
66+	free(keyboard);
67 error0:
68 	return false;
69 }
70 
71 void
72-keyboard_finalize(struct keyboard *keyboard)
73+keyboard_destroy(struct keyboard *keyboard)
74 {
75 	wl_array_release(&keyboard->client_keys);
76 	wl_array_release(&keyboard->keys);
77@@ -237,6 +245,7 @@ keyboard_finalize(struct keyboard *keyboard)
78 	xkb_state_unref(keyboard->xkb.state);
79 	xkb_keymap_unref(keyboard->xkb.keymap.map);
80 	xkb_context_unref(keyboard->xkb.context);
81+	free(keyboard);
82 }
83 
84 bool
+2, -2
 1@@ -83,8 +83,8 @@ struct keyboard {
 2 	uint32_t modifiers;
 3 };
 4 
 5-bool keyboard_initialize(struct keyboard *keyboard);
 6-void keyboard_finalize(struct keyboard *keyboard);
 7+struct keyboard *keyboard_create(void);
 8+void keyboard_destroy(struct keyboard *keyboard);
 9 bool keyboard_reset(struct keyboard *keyboard);
10 void keyboard_set_focus(struct keyboard *keyboard, struct compositor_view *view);
11 struct wl_resource *keyboard_bind(struct keyboard *keyboard, struct wl_client *client, uint32_t version, uint32_t id);
+10, -11
 1@@ -61,7 +61,6 @@ struct seat {
 2 
 3 	struct wl_listener swc_listener;
 4 
 5-	struct keyboard keyboard;
 6 	struct wl_listener keyboard_focus_listener;
 7 	struct pointer pointer;
 8 	struct data_device data_device;
 9@@ -98,8 +97,8 @@ handle_data_device_event(struct wl_listener *listener, void *data)
10 	if (ev->type != DATA_DEVICE_EVENT_SELECTION_CHANGED)
11 		return;
12 
13-	if (seat->keyboard.focus.client)
14-		data_device_offer_selection(&seat->data_device, seat->keyboard.focus.client);
15+	if (seat->base.keyboard->focus.client)
16+		data_device_offer_selection(&seat->data_device, seat->base.keyboard->focus.client);
17 }
18 
19 static void
20@@ -111,7 +110,7 @@ handle_swc_event(struct wl_listener *listener, void *data)
21 	switch (ev->type) {
22 	case SWC_EVENT_DEACTIVATED:
23 		libinput_suspend(seat->libinput);
24-		keyboard_reset(&seat->keyboard);
25+		keyboard_reset(seat->base.keyboard);
26 		break;
27 	case SWC_EVENT_ACTIVATED:
28 		if (libinput_resume(seat->libinput) != 0)
29@@ -134,7 +133,7 @@ get_keyboard(struct wl_client *client, struct wl_resource *resource, uint32_t id
30 {
31 	struct seat *seat = wl_resource_get_user_data(resource);
32 
33-	keyboard_bind(&seat->keyboard, client, wl_resource_get_version(resource), id);
34+	keyboard_bind(seat->base.keyboard, client, wl_resource_get_version(resource), id);
35 }
36 
37 static void
38@@ -260,7 +259,7 @@ handle_libinput_data(int fd, uint32_t mask, void *data)
39 			time = libinput_event_keyboard_get_time(event.k);
40 			key = libinput_event_keyboard_get_key(event.k);
41 			state = libinput_event_keyboard_get_key_state(event.k);
42-			keyboard_handle_key(&seat->keyboard, time, key, state);
43+			keyboard_handle_key(seat->base.keyboard, time, key, state);
44 			break;
45 		case LIBINPUT_EVENT_POINTER_MOTION:
46 			event.p = libinput_event_get_pointer_event(generic_event);
47@@ -394,13 +393,13 @@ seat_create(struct wl_display *display, const char *seat_name)
48 	seat->data_device_listener.notify = &handle_data_device_event;
49 	wl_signal_add(&seat->data_device.event_signal, &seat->data_device_listener);
50 
51-	if (!keyboard_initialize(&seat->keyboard)) {
52+	seat->base.keyboard = keyboard_create();
53+	if (!seat->base.keyboard) {
54 		ERROR("Could not initialize keyboard\n");
55 		goto error4;
56 	}
57-	seat->base.keyboard = &seat->keyboard;
58 	seat->keyboard_focus_listener.notify = handle_keyboard_focus_event;
59-	wl_signal_add(&seat->keyboard.focus.event_signal, &seat->keyboard_focus_listener);
60+	wl_signal_add(&seat->base.keyboard->focus.event_signal, &seat->keyboard_focus_listener);
61 
62 	if (!pointer_initialize(&seat->pointer)) {
63 		ERROR("Could not initialize pointer\n");
64@@ -416,7 +415,7 @@ seat_create(struct wl_display *display, const char *seat_name)
65 error6:
66 	pointer_finalize(&seat->pointer);
67 error5:
68-	keyboard_finalize(&seat->keyboard);
69+	keyboard_destroy(seat->base.keyboard);
70 error4:
71 	data_device_finalize(&seat->data_device);
72 error3:
73@@ -441,7 +440,7 @@ seat_destroy(struct swc_seat *seat_base)
74 #endif
75 
76 	pointer_finalize(&seat->pointer);
77-	keyboard_finalize(&seat->keyboard);
78+	keyboard_destroy(seat->base.keyboard);
79 	data_device_finalize(&seat->data_device);
80 
81 	wl_global_destroy(seat->global);