1 /*
2 * Wayland Support
3 *
4 * Copyright (C) 2016 Red Hat
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 *
21 * Author: Carlos Garnacho <carlosg@gnome.org>
22 */
23
24 #include "config.h"
25
26 #include <wayland-server.h>
27
28 #include "compositor/meta-surface-actor-wayland.h"
29 #include "wayland/meta-wayland-tablet-pad-group.h"
30 #include "wayland/meta-wayland-tablet-pad-ring.h"
31 #include "wayland/meta-wayland-tablet-pad-strip.h"
32 #include "wayland/meta-wayland-tablet-pad.h"
33 #include "wayland/meta-wayland-tablet-seat.h"
34
35 #include "tablet-unstable-v2-server-protocol.h"
36
37 static void
unbind_resource(struct wl_resource * resource)38 unbind_resource (struct wl_resource *resource)
39 {
40 wl_list_remove (wl_resource_get_link (resource));
41 }
42
43 MetaWaylandTabletPadGroup *
meta_wayland_tablet_pad_group_new(MetaWaylandTabletPad * pad)44 meta_wayland_tablet_pad_group_new (MetaWaylandTabletPad *pad)
45 {
46 MetaWaylandTabletPadGroup *group;
47
48 group = g_new0 (MetaWaylandTabletPadGroup, 1);
49 wl_list_init (&group->resource_list);
50 wl_list_init (&group->focus_resource_list);
51 group->pad = pad;
52
53 return group;
54 }
55
56 void
meta_wayland_tablet_pad_group_free(MetaWaylandTabletPadGroup * group)57 meta_wayland_tablet_pad_group_free (MetaWaylandTabletPadGroup *group)
58 {
59 struct wl_resource *resource, *next;
60
61 wl_resource_for_each_safe (resource, next, &group->resource_list)
62 {
63 wl_list_remove (wl_resource_get_link (resource));
64 wl_list_init (wl_resource_get_link (resource));
65 }
66
67 g_list_free (group->rings);
68 g_list_free (group->strips);
69
70 g_free (group);
71 }
72
73 static void
tablet_pad_group_destroy(struct wl_client * client,struct wl_resource * resource)74 tablet_pad_group_destroy (struct wl_client *client,
75 struct wl_resource *resource)
76 {
77 wl_resource_destroy (resource);
78 }
79
80 static const struct zwp_tablet_pad_group_v2_interface group_interface = {
81 tablet_pad_group_destroy
82 };
83
84 struct wl_resource *
meta_wayland_tablet_pad_group_create_new_resource(MetaWaylandTabletPadGroup * group,struct wl_client * client,struct wl_resource * pad_resource,uint32_t id)85 meta_wayland_tablet_pad_group_create_new_resource (MetaWaylandTabletPadGroup *group,
86 struct wl_client *client,
87 struct wl_resource *pad_resource,
88 uint32_t id)
89 {
90 struct wl_resource *resource;
91
92 resource = wl_resource_create (client, &zwp_tablet_pad_group_v2_interface,
93 wl_resource_get_version (pad_resource), id);
94 wl_resource_set_implementation (resource, &group_interface,
95 group, unbind_resource);
96 wl_resource_set_user_data (resource, group);
97 wl_list_insert (&group->resource_list, wl_resource_get_link (resource));
98
99 return resource;
100 }
101
102 struct wl_resource *
meta_wayland_tablet_pad_group_lookup_resource(MetaWaylandTabletPadGroup * group,struct wl_client * client)103 meta_wayland_tablet_pad_group_lookup_resource (MetaWaylandTabletPadGroup *group,
104 struct wl_client *client)
105 {
106 struct wl_resource *resource;
107
108 resource = wl_resource_find_for_client (&group->resource_list, client);
109
110 if (!resource)
111 resource = wl_resource_find_for_client (&group->focus_resource_list, client);
112
113 return resource;
114 }
115
116 gboolean
meta_wayland_tablet_pad_group_has_button(MetaWaylandTabletPadGroup * group,guint button)117 meta_wayland_tablet_pad_group_has_button (MetaWaylandTabletPadGroup *group,
118 guint button)
119 {
120 int n_group = g_list_index (group->pad->groups, group);
121
122 if (clutter_input_device_get_pad_feature_group (group->pad->device,
123 CLUTTER_PAD_FEATURE_BUTTON,
124 button) == n_group)
125 return TRUE;
126
127 return FALSE;
128 }
129
130 static void
meta_wayland_tablet_pad_group_send_buttons(MetaWaylandTabletPadGroup * group,struct wl_resource * resource)131 meta_wayland_tablet_pad_group_send_buttons (MetaWaylandTabletPadGroup *group,
132 struct wl_resource *resource)
133 {
134 struct wl_array buttons;
135 guint i;
136
137 wl_array_init (&buttons);
138
139 for (i = 0; i < group->pad->n_buttons; i++)
140 {
141 uint32_t *pos;
142
143 if (!meta_wayland_tablet_pad_group_has_button (group, i))
144 continue;
145
146 pos = wl_array_add (&buttons, sizeof (*pos));
147 *pos = i;
148 }
149
150 zwp_tablet_pad_group_v2_send_buttons (resource, &buttons);
151 wl_array_release (&buttons);
152 }
153
154 void
meta_wayland_tablet_pad_group_notify(MetaWaylandTabletPadGroup * group,struct wl_resource * resource)155 meta_wayland_tablet_pad_group_notify (MetaWaylandTabletPadGroup *group,
156 struct wl_resource *resource)
157 {
158 struct wl_client *client = wl_resource_get_client (resource);
159 struct wl_array buttons;
160 guint n_group, n_modes;
161 GList *l;
162
163 wl_array_init (&buttons);
164
165 /* Buttons */
166 meta_wayland_tablet_pad_group_send_buttons (group, resource);
167
168 /* Rings */
169 for (l = group->rings; l; l = l->next)
170 {
171 MetaWaylandTabletPadRing *ring = l->data;
172 struct wl_resource *ring_resource;
173
174 ring_resource = meta_wayland_tablet_pad_ring_create_new_resource (ring,
175 client,
176 resource,
177 0);
178 zwp_tablet_pad_group_v2_send_ring (resource, ring_resource);
179 }
180
181 /* Strips */
182 for (l = group->strips; l; l = l->next)
183 {
184 MetaWaylandTabletPadStrip *strip = l->data;
185 struct wl_resource *strip_resource;
186
187 strip_resource = meta_wayland_tablet_pad_strip_create_new_resource (strip,
188 client,
189 resource,
190 0);
191 zwp_tablet_pad_group_v2_send_strip (resource, strip_resource);
192 }
193
194 n_group = g_list_index (group->pad->groups, group);
195 n_modes = clutter_input_device_get_group_n_modes (group->pad->device,
196 n_group);
197
198 zwp_tablet_pad_group_v2_send_modes (resource, n_modes);
199 zwp_tablet_pad_group_v2_send_done (resource);
200 }
201
202 void
meta_wayland_tablet_pad_group_update(MetaWaylandTabletPadGroup * group,const ClutterEvent * event)203 meta_wayland_tablet_pad_group_update (MetaWaylandTabletPadGroup *group,
204 const ClutterEvent *event)
205 {
206 switch (event->type)
207 {
208 case CLUTTER_PAD_BUTTON_PRESS:
209 case CLUTTER_PAD_BUTTON_RELEASE:
210 if (meta_wayland_tablet_pad_group_is_mode_switch_button (group, event->pad_button.button))
211 group->current_mode = event->pad_button.mode;
212 break;
213 default:
214 break;
215 }
216 }
217
218 static gboolean
handle_pad_ring_event(MetaWaylandTabletPadGroup * group,const ClutterEvent * event)219 handle_pad_ring_event (MetaWaylandTabletPadGroup *group,
220 const ClutterEvent *event)
221 {
222 MetaWaylandTabletPadRing *ring;
223
224 if (event->type != CLUTTER_PAD_RING)
225 return FALSE;
226
227 ring = g_list_nth_data (group->rings, event->pad_ring.ring_number);
228
229 if (!ring)
230 return FALSE;
231
232 return meta_wayland_tablet_pad_ring_handle_event (ring, event);
233 }
234
235 static gboolean
handle_pad_strip_event(MetaWaylandTabletPadGroup * group,const ClutterEvent * event)236 handle_pad_strip_event (MetaWaylandTabletPadGroup *group,
237 const ClutterEvent *event)
238 {
239 MetaWaylandTabletPadStrip *strip;
240
241 if (event->type != CLUTTER_PAD_STRIP)
242 return FALSE;
243
244 strip = g_list_nth_data (group->strips, event->pad_strip.strip_number);
245
246 if (!strip)
247 return FALSE;
248
249 return meta_wayland_tablet_pad_strip_handle_event (strip, event);
250 }
251
252 static void
broadcast_group_mode(MetaWaylandTabletPadGroup * group,uint32_t time)253 broadcast_group_mode (MetaWaylandTabletPadGroup *group,
254 uint32_t time)
255 {
256 struct wl_display *display = group->pad->tablet_seat->seat->wl_display;
257 struct wl_resource *resource;
258
259 group->mode_switch_serial = wl_display_next_serial (display);
260
261 wl_resource_for_each (resource, &group->focus_resource_list)
262 {
263 zwp_tablet_pad_group_v2_send_mode_switch (resource, time,
264 group->mode_switch_serial,
265 group->current_mode);
266 }
267 }
268
269 static void
broadcast_group_buttons(MetaWaylandTabletPadGroup * group)270 broadcast_group_buttons (MetaWaylandTabletPadGroup *group)
271 {
272 struct wl_resource *resource;
273
274 wl_resource_for_each (resource, &group->focus_resource_list)
275 {
276 meta_wayland_tablet_pad_group_send_buttons (group, resource);
277 }
278 }
279
280 gboolean
meta_wayland_tablet_pad_group_handle_event(MetaWaylandTabletPadGroup * group,const ClutterEvent * event)281 meta_wayland_tablet_pad_group_handle_event (MetaWaylandTabletPadGroup *group,
282 const ClutterEvent *event)
283 {
284 switch (clutter_event_type (event))
285 {
286 case CLUTTER_PAD_BUTTON_PRESS:
287 case CLUTTER_PAD_BUTTON_RELEASE:
288 if (meta_wayland_tablet_pad_group_is_mode_switch_button (group, event->pad_button.button))
289 {
290 if (event->type == CLUTTER_PAD_BUTTON_PRESS)
291 broadcast_group_mode (group, clutter_event_get_time (event));
292 return TRUE;
293 }
294 else
295 {
296 return FALSE;
297 }
298 break;
299 case CLUTTER_PAD_RING:
300 return handle_pad_ring_event (group, event);
301 case CLUTTER_PAD_STRIP:
302 return handle_pad_strip_event (group, event);
303 default:
304 return FALSE;
305 }
306 }
307
308 static void
meta_wayland_tablet_pad_group_update_rings_focus(MetaWaylandTabletPadGroup * group)309 meta_wayland_tablet_pad_group_update_rings_focus (MetaWaylandTabletPadGroup *group)
310 {
311 GList *l;
312
313 for (l = group->rings; l; l = l->next)
314 meta_wayland_tablet_pad_ring_sync_focus (l->data);
315 }
316
317 static void
meta_wayland_tablet_pad_group_update_strips_focus(MetaWaylandTabletPadGroup * group)318 meta_wayland_tablet_pad_group_update_strips_focus (MetaWaylandTabletPadGroup *group)
319 {
320 GList *l;
321
322 for (l = group->strips; l; l = l->next)
323 meta_wayland_tablet_pad_strip_sync_focus (l->data);
324 }
325
326 static void
move_resources(struct wl_list * destination,struct wl_list * source)327 move_resources (struct wl_list *destination, struct wl_list *source)
328 {
329 wl_list_insert_list (destination, source);
330 wl_list_init (source);
331 }
332
333 static void
move_resources_for_client(struct wl_list * destination,struct wl_list * source,struct wl_client * client)334 move_resources_for_client (struct wl_list *destination,
335 struct wl_list *source,
336 struct wl_client *client)
337 {
338 struct wl_resource *resource, *tmp;
339
340 wl_resource_for_each_safe (resource, tmp, source)
341 {
342 if (wl_resource_get_client (resource) == client)
343 {
344 wl_list_remove (wl_resource_get_link (resource));
345 wl_list_insert (destination, wl_resource_get_link (resource));
346 }
347 }
348 }
349
350 void
meta_wayland_tablet_pad_group_sync_focus(MetaWaylandTabletPadGroup * group)351 meta_wayland_tablet_pad_group_sync_focus (MetaWaylandTabletPadGroup *group)
352 {
353 if (!wl_list_empty (&group->focus_resource_list))
354 {
355 move_resources (&group->resource_list, &group->focus_resource_list);
356 }
357
358 if (group->pad->focus_surface != NULL)
359 {
360 move_resources_for_client (&group->focus_resource_list,
361 &group->resource_list,
362 wl_resource_get_client (group->pad->focus_surface->resource));
363 }
364
365 meta_wayland_tablet_pad_group_update_rings_focus (group);
366 meta_wayland_tablet_pad_group_update_strips_focus (group);
367
368 if (!wl_list_empty (&group->focus_resource_list))
369 {
370 broadcast_group_mode (group, clutter_get_current_event_time ());
371 broadcast_group_buttons (group);
372 }
373 }
374
375 gboolean
meta_wayland_tablet_pad_group_is_mode_switch_button(MetaWaylandTabletPadGroup * group,guint button)376 meta_wayland_tablet_pad_group_is_mode_switch_button (MetaWaylandTabletPadGroup *group,
377 guint button)
378 {
379 gint n_group = g_list_index (group->pad->groups, group);
380
381 g_assert (n_group >= 0);
382
383 return clutter_input_device_is_mode_switch_button (group->pad->device,
384 n_group, button);
385 }
386