input: Remove keyboard listener keyboard focus activation
[src/agl-compositor.git] / src / input.c
1 /*
2  * Copyright © 2020 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include <assert.h>
27 #include <linux/input.h>
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <string.h>
31
32 #include "ivi-compositor.h"
33 #include "shared/helpers.h"
34
35 struct ivi_shell_seat {
36         struct weston_seat *seat;
37         struct weston_surface *focused_surface;
38
39         bool hide_cursor;
40         bool new_caps_sent;
41
42         struct wl_listener seat_destroy_listener;
43         struct wl_listener caps_changed_listener;
44         struct wl_listener keyboard_focus_listener;
45         struct wl_listener pointer_focus_listener;
46 };
47
48 static struct ivi_surface *
49 get_ivi_shell_surface(struct weston_surface *surface)
50 {
51         struct weston_desktop_surface *desktop_surface =
52                 weston_surface_get_desktop_surface(surface);
53
54         if (desktop_surface)
55                 return weston_desktop_surface_get_user_data(desktop_surface);
56
57         return NULL;
58 }
59
60 static void
61 ivi_shell_seat_handle_destroy(struct wl_listener *listener, void *data)
62 {
63         struct ivi_shell_seat *shseat =
64                 container_of(listener,
65                              struct ivi_shell_seat, seat_destroy_listener);
66
67         wl_list_remove(&shseat->keyboard_focus_listener.link);
68         wl_list_remove(&shseat->caps_changed_listener.link);
69         wl_list_remove(&shseat->pointer_focus_listener.link);
70         wl_list_remove(&shseat->seat_destroy_listener.link);
71
72         free(shseat);
73 }
74
75 static struct ivi_shell_seat *
76 get_ivi_shell_seat(struct weston_seat *seat)
77 {
78         struct wl_listener *listener;
79
80         listener = wl_signal_get(&seat->destroy_signal,
81                                  ivi_shell_seat_handle_destroy);
82         assert(listener != NULL);
83
84         return container_of(listener,
85                             struct ivi_shell_seat, seat_destroy_listener);
86 }
87
88 static void
89 ivi_shell_seat_handle_pointer_focus(struct wl_listener *listener, void *data)
90 {
91         struct weston_pointer *pointer = data;
92         struct ivi_shell_seat *shseat = get_ivi_shell_seat(pointer->seat);
93         struct wl_resource *resource;
94         int resources = 0;
95
96         /* FIXME: should probably query it and not assume all caps */
97         uint32_t caps = (WL_SEAT_CAPABILITY_POINTER |
98                          WL_SEAT_CAPABILITY_TOUCH |
99                          WL_SEAT_CAPABILITY_KEYBOARD);
100
101         wl_resource_for_each(resource, &pointer->seat->base_resource_list)
102                 resources++;
103
104         /* remove the POINTER capability such that the client will not install
105          * a cursor surface */
106         if (shseat->hide_cursor && !shseat->new_caps_sent && resources) {
107                 caps &= ~WL_SEAT_CAPABILITY_POINTER;
108                 wl_resource_for_each(resource, &pointer->seat->base_resource_list) {
109                         wl_seat_send_capabilities(resource, caps);
110                 }
111                 shseat->new_caps_sent = true;
112         }
113 }
114
115 static void
116 ivi_shell_seat_handle_caps_changed(struct wl_listener *listener, void *data)
117 {
118         struct weston_pointer *pointer;
119         struct ivi_shell_seat *shseat;
120
121         shseat = container_of(listener, struct ivi_shell_seat,
122                               caps_changed_listener);
123         pointer = weston_seat_get_pointer(shseat->seat);
124
125         if (pointer && wl_list_empty(&shseat->pointer_focus_listener.link)) {
126                 wl_signal_add(&pointer->focus_signal,
127                               &shseat->pointer_focus_listener);
128         } else {
129                 wl_list_remove(&shseat->pointer_focus_listener.link);
130                 wl_list_init(&shseat->pointer_focus_listener.link);
131         }
132 }
133
134 static struct ivi_shell_seat *
135 ivi_shell_seat_create(struct weston_seat *seat, bool hide_cursor)
136 {
137         struct ivi_shell_seat *shseat;
138
139         shseat = zalloc(sizeof(*shseat));
140         if (!shseat) {
141                 weston_log("no memory to allocate shell seat\n");
142                 return NULL;
143         }
144
145         shseat->seat = seat;
146         shseat->hide_cursor = hide_cursor;
147         shseat->new_caps_sent = false;
148
149         shseat->seat_destroy_listener.notify = ivi_shell_seat_handle_destroy;
150         wl_signal_add(&seat->destroy_signal, &shseat->seat_destroy_listener);
151
152         wl_list_init(&shseat->keyboard_focus_listener.link);
153
154         shseat->pointer_focus_listener.notify =
155                 ivi_shell_seat_handle_pointer_focus;
156         wl_list_init(&shseat->pointer_focus_listener.link);
157
158         shseat->caps_changed_listener.notify =
159                 ivi_shell_seat_handle_caps_changed;
160         wl_signal_add(&seat->updated_caps_signal, &shseat->caps_changed_listener);
161
162         ivi_shell_seat_handle_caps_changed(&shseat->caps_changed_listener, NULL);
163
164         return shseat;
165 }
166
167
168 static void
169 ivi_shell_handle_seat_created(struct wl_listener *listener, void *data)
170 {
171         struct weston_seat *seat = data;
172         struct ivi_compositor *ivi =
173                 container_of(listener, struct ivi_compositor, seat_created_listener);
174
175         weston_log("Cursor is %s\n", ivi->hide_cursor ? "set" : "not set");
176         ivi_shell_seat_create(seat, ivi->hide_cursor);
177 }
178
179 /*
180  * useful to reset the fact that 'new' capabilities have seent
181  */
182 void
183 ivi_seat_reset_caps_sent(struct ivi_compositor *ivi)
184 {
185         struct weston_compositor *ec = ivi->compositor;
186         struct weston_seat *seat;
187
188         wl_list_for_each(seat, &ec->seat_list, link) {
189                 struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(seat);
190                 ivi_seat->new_caps_sent = false;
191         }
192 }
193
194 void
195 ivi_seat_init(struct ivi_compositor *ivi)
196 {
197         struct weston_compositor *ec = ivi->compositor;
198         struct weston_seat *seat;
199
200         wl_list_for_each(seat, &ec->seat_list, link) {
201                 weston_log("Seat %p, cursor is %s\n", seat, ivi->hide_cursor ?
202                                 "set" : "not set");
203                 ivi_shell_seat_create(seat, ivi->hide_cursor);
204         }
205
206         ivi->seat_created_listener.notify = ivi_shell_handle_seat_created;
207         wl_signal_add(&ec->seat_created_signal, &ivi->seat_created_listener);
208 }