summaryrefslogtreecommitdiff
path: root/src/wayland/meta-wayland-tablet-pad-group.c
blob: 28165158815f4a5e5678501d75a56d63faaf060a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Wayland Support
 *
 * Copyright (C) 2016 Red Hat
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Author: Carlos Garnacho <carlosg@gnome.org>
 */

#include "config.h"

#include <wayland-server.h>

#include "compositor/meta-surface-actor-wayland.h"
#include "wayland/meta-wayland-tablet-pad-group.h"
#include "wayland/meta-wayland-tablet-pad-ring.h"
#include "wayland/meta-wayland-tablet-pad-strip.h"
#include "wayland/meta-wayland-tablet-pad.h"
#include "wayland/meta-wayland-tablet-seat.h"

#include "tablet-unstable-v2-server-protocol.h"

static void
unbind_resource (struct wl_resource *resource)
{
  wl_list_remove (wl_resource_get_link (resource));
}

MetaWaylandTabletPadGroup *
meta_wayland_tablet_pad_group_new (MetaWaylandTabletPad *pad)
{
  MetaWaylandTabletPadGroup *group;

  group = g_new0 (MetaWaylandTabletPadGroup, 1);
  wl_list_init (&group->resource_list);
  wl_list_init (&group->focus_resource_list);
  group->pad = pad;

  return group;
}

void
meta_wayland_tablet_pad_group_free (MetaWaylandTabletPadGroup *group)
{
  struct wl_resource *resource, *next;

  wl_resource_for_each_safe (resource, next, &group->resource_list)
    {
      wl_list_remove (wl_resource_get_link (resource));
      wl_list_init (wl_resource_get_link (resource));
    }

  g_list_free (group->rings);
  g_list_free (group->strips);

  g_free (group);
}

static void
tablet_pad_group_destroy (struct wl_client   *client,
                          struct wl_resource *resource)
{
  wl_resource_destroy (resource);
}

static const struct zwp_tablet_pad_group_v2_interface group_interface = {
  tablet_pad_group_destroy
};

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)
{
  struct wl_resource *resource;

  resource = wl_resource_create (client, &zwp_tablet_pad_group_v2_interface,
                                 wl_resource_get_version (pad_resource), id);
  wl_resource_set_implementation (resource, &group_interface,
                                  group, unbind_resource);
  wl_resource_set_user_data (resource, group);
  wl_list_insert (&group->resource_list, wl_resource_get_link (resource));

  return resource;
}

struct wl_resource *
meta_wayland_tablet_pad_group_lookup_resource (MetaWaylandTabletPadGroup *group,
                                               struct wl_client          *client)
{
  struct wl_resource *resource;

  resource = wl_resource_find_for_client (&group->resource_list, client);

  if (!resource)
    resource = wl_resource_find_for_client (&group->focus_resource_list, client);

  return resource;
}

gboolean
meta_wayland_tablet_pad_group_has_button (MetaWaylandTabletPadGroup *group,
                                          guint                      button)
{
  int n_group = g_list_index (group->pad->groups, group);

  if (clutter_input_device_get_pad_feature_group (group->pad->device,
                                                  CLUTTER_PAD_FEATURE_BUTTON,
                                                  button) == n_group)
    return TRUE;

  return FALSE;
}

static void
meta_wayland_tablet_pad_group_send_buttons (MetaWaylandTabletPadGroup *group,
                                            struct wl_resource        *resource)
{
  struct wl_array buttons;
  guint i;

  wl_array_init (&buttons);

  for (i = 0; i < group->pad->n_buttons; i++)
    {
      uint32_t *pos;

      if (!meta_wayland_tablet_pad_group_has_button (group, i))
        continue;

      pos = wl_array_add (&buttons, sizeof (*pos));
      *pos = i;
    }

  zwp_tablet_pad_group_v2_send_buttons (resource, &buttons);
  wl_array_release (&buttons);
}

void
meta_wayland_tablet_pad_group_notify (MetaWaylandTabletPadGroup *group,
                                      struct wl_resource        *resource)
{
  struct wl_client *client = wl_resource_get_client (resource);
  struct wl_array buttons;
  guint n_group, n_modes;
  GList *l;

  wl_array_init (&buttons);

  /* Buttons */
  meta_wayland_tablet_pad_group_send_buttons (group, resource);

  /* Rings */
  for (l = group->rings; l; l = l->next)
    {
      MetaWaylandTabletPadRing *ring = l->data;
      struct wl_resource *ring_resource;

      ring_resource = meta_wayland_tablet_pad_ring_create_new_resource (ring,
                                                                        client,
                                                                        resource,
                                                                        0);
      zwp_tablet_pad_group_v2_send_ring (resource, ring_resource);
    }

  /* Strips */
  for (l = group->strips; l; l = l->next)
    {
      MetaWaylandTabletPadStrip *strip = l->data;
      struct wl_resource *strip_resource;

      strip_resource = meta_wayland_tablet_pad_strip_create_new_resource (strip,
                                                                          client,
                                                                          resource,
                                                                          0);
      zwp_tablet_pad_group_v2_send_strip (resource, strip_resource);
    }

  n_group = g_list_index (group->pad->groups, group);
  n_modes = clutter_input_device_get_group_n_modes (group->pad->device,
                                                    n_group);

  zwp_tablet_pad_group_v2_send_modes (resource, n_modes);
  zwp_tablet_pad_group_v2_send_done (resource);
}

void
meta_wayland_tablet_pad_group_update (MetaWaylandTabletPadGroup *group,
                                      const ClutterEvent        *event)
{
  switch (event->type)
    {
    case CLUTTER_PAD_BUTTON_PRESS:
    case CLUTTER_PAD_BUTTON_RELEASE:
      if (meta_wayland_tablet_pad_group_is_mode_switch_button (group, event->pad_button.button))
        group->current_mode = event->pad_button.mode;
      break;
    default:
      break;
    }
}

static gboolean
handle_pad_ring_event (MetaWaylandTabletPadGroup *group,
                       const ClutterEvent        *event)
{
  MetaWaylandTabletPadRing *ring;

  if (event->type != CLUTTER_PAD_RING)
    return FALSE;

  ring = g_list_nth_data (group->rings, event->pad_ring.ring_number);

  if (!ring)
    return FALSE;

  return meta_wayland_tablet_pad_ring_handle_event (ring, event);
}

static gboolean
handle_pad_strip_event (MetaWaylandTabletPadGroup *group,
                        const ClutterEvent        *event)
{
  MetaWaylandTabletPadStrip *strip;

  if (event->type != CLUTTER_PAD_STRIP)
    return FALSE;

  strip = g_list_nth_data (group->strips, event->pad_strip.strip_number);

  if (!strip)
    return FALSE;

  return meta_wayland_tablet_pad_strip_handle_event (strip, event);
}

static void
broadcast_group_mode (MetaWaylandTabletPadGroup *group,
                      uint32_t                   time)
{
  struct wl_display *display = group->pad->tablet_seat->seat->wl_display;
  struct wl_resource *resource;

  group->mode_switch_serial = wl_display_next_serial (display);

  wl_resource_for_each (resource, &group->focus_resource_list)
    {
      zwp_tablet_pad_group_v2_send_mode_switch (resource, time,
                                                group->mode_switch_serial,
                                                group->current_mode);
    }
}

static void
broadcast_group_buttons (MetaWaylandTabletPadGroup *group)
{
  struct wl_resource *resource;

  wl_resource_for_each (resource, &group->focus_resource_list)
    {
      meta_wayland_tablet_pad_group_send_buttons (group, resource);
    }
}

gboolean
meta_wayland_tablet_pad_group_handle_event (MetaWaylandTabletPadGroup *group,
                                            const ClutterEvent        *event)
{
  switch (clutter_event_type (event))
    {
    case CLUTTER_PAD_BUTTON_PRESS:
    case CLUTTER_PAD_BUTTON_RELEASE:
      if (meta_wayland_tablet_pad_group_is_mode_switch_button (group, event->pad_button.button))
        {
          if (event->type == CLUTTER_PAD_BUTTON_PRESS)
            broadcast_group_mode (group, clutter_event_get_time (event));
          return TRUE;
        }
      else
        {
          return FALSE;
        }
      break;
    case CLUTTER_PAD_RING:
      return handle_pad_ring_event (group, event);
    case CLUTTER_PAD_STRIP:
      return handle_pad_strip_event (group, event);
    default:
      return FALSE;
    }
}

static void
meta_wayland_tablet_pad_group_update_rings_focus (MetaWaylandTabletPadGroup *group)
{
  GList *l;

  for (l = group->rings; l; l = l->next)
    meta_wayland_tablet_pad_ring_sync_focus (l->data);
}

static void
meta_wayland_tablet_pad_group_update_strips_focus (MetaWaylandTabletPadGroup *group)
{
  GList *l;

  for (l = group->strips; l; l = l->next)
    meta_wayland_tablet_pad_strip_sync_focus (l->data);
}

static void
move_resources (struct wl_list *destination, struct wl_list *source)
{
  wl_list_insert_list (destination, source);
  wl_list_init (source);
}

static void
move_resources_for_client (struct wl_list *destination,
			   struct wl_list *source,
			   struct wl_client *client)
{
  struct wl_resource *resource, *tmp;

  wl_resource_for_each_safe (resource, tmp, source)
    {
      if (wl_resource_get_client (resource) == client)
        {
          wl_list_remove (wl_resource_get_link (resource));
          wl_list_insert (destination, wl_resource_get_link (resource));
        }
    }
}

void
meta_wayland_tablet_pad_group_sync_focus (MetaWaylandTabletPadGroup *group)
{
  if (!wl_list_empty (&group->focus_resource_list))
    {
      move_resources (&group->resource_list, &group->focus_resource_list);
    }

  if (group->pad->focus_surface != NULL)
    {
      move_resources_for_client (&group->focus_resource_list,
                                 &group->resource_list,
                                 wl_resource_get_client (group->pad->focus_surface->resource));
    }

  meta_wayland_tablet_pad_group_update_rings_focus (group);
  meta_wayland_tablet_pad_group_update_strips_focus (group);

  if (!wl_list_empty (&group->focus_resource_list))
    {
      broadcast_group_mode (group, clutter_get_current_event_time ());
      broadcast_group_buttons (group);
    }
}

gboolean
meta_wayland_tablet_pad_group_is_mode_switch_button (MetaWaylandTabletPadGroup *group,
                                                     guint                      button)
{
  gint n_group = g_list_index (group->pad->groups, group);

  g_assert (n_group >= 0);

  return clutter_input_device_is_mode_switch_button (group->pad->device,
                                                     n_group, button);
}