~elementary-os/elementaryos/os-patch-mutter-bionic

« back to all changes in this revision

Viewing changes to src/wayland/meta-wayland-tablet-manager.c

  • Committer: RabbitBot
  • Date: 2018-04-11 14:49:36 UTC
  • Revision ID: rabbitbot@elementary.io-20180411144936-hgymqa9d8d1xfpbh
Initial import, version 3.28.0-2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Wayland Support
 
3
 *
 
4
 * Copyright (C) 2015 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
#define _GNU_SOURCE
 
25
 
 
26
#include "config.h"
 
27
 
 
28
#include <glib.h>
 
29
 
 
30
#include <wayland-server.h>
 
31
#include "tablet-unstable-v2-server-protocol.h"
 
32
 
 
33
#include "meta-wayland-private.h"
 
34
#include "meta-wayland-tablet-manager.h"
 
35
#include "meta-wayland-tablet-seat.h"
 
36
#include "meta-wayland-tablet-tool.h"
 
37
 
 
38
static void
 
39
unbind_resource (struct wl_resource *resource)
 
40
{
 
41
  wl_list_remove (wl_resource_get_link (resource));
 
42
}
 
43
 
 
44
static gboolean
 
45
is_tablet_device (ClutterInputDevice *device)
 
46
{
 
47
  ClutterInputDeviceType device_type;
 
48
 
 
49
  if (clutter_input_device_get_device_mode (device) == CLUTTER_INPUT_MODE_MASTER)
 
50
    return FALSE;
 
51
 
 
52
  device_type = clutter_input_device_get_device_type (device);
 
53
 
 
54
  return (device_type == CLUTTER_TABLET_DEVICE ||
 
55
          device_type == CLUTTER_PEN_DEVICE ||
 
56
          device_type == CLUTTER_ERASER_DEVICE ||
 
57
          device_type == CLUTTER_CURSOR_DEVICE ||
 
58
          device_type == CLUTTER_PAD_DEVICE);
 
59
}
 
60
 
 
61
static void
 
62
tablet_manager_get_tablet_seat (struct wl_client   *client,
 
63
                                struct wl_resource *resource,
 
64
                                guint32             id,
 
65
                                struct wl_resource *seat_resource)
 
66
{
 
67
  MetaWaylandTabletManager *tablet_manager = wl_resource_get_user_data (resource);
 
68
  MetaWaylandSeat *seat = wl_resource_get_user_data (seat_resource);
 
69
  MetaWaylandTabletSeat *tablet_seat;
 
70
 
 
71
  tablet_seat = meta_wayland_tablet_manager_ensure_seat (tablet_manager, seat);
 
72
  meta_wayland_tablet_seat_create_new_resource (tablet_seat, client,
 
73
                                                resource, id);
 
74
}
 
75
 
 
76
static void
 
77
tablet_manager_destroy (struct wl_client   *client,
 
78
                        struct wl_resource *resource)
 
79
{
 
80
  wl_resource_destroy (resource);
 
81
}
 
82
 
 
83
static const struct zwp_tablet_manager_v2_interface tablet_manager_interface = {
 
84
  tablet_manager_get_tablet_seat,
 
85
  tablet_manager_destroy
 
86
};
 
87
 
 
88
static void
 
89
bind_tablet_manager (struct wl_client *client,
 
90
                     void             *data,
 
91
                     uint32_t          version,
 
92
                     uint32_t          id)
 
93
{
 
94
  MetaWaylandCompositor *compositor = data;
 
95
  MetaWaylandTabletManager *tablet_manager = compositor->tablet_manager;
 
96
  struct wl_resource *resource;
 
97
 
 
98
  resource = wl_resource_create (client, &zwp_tablet_manager_v2_interface,
 
99
                                 MIN (version, 1), id);
 
100
  wl_resource_set_implementation (resource, &tablet_manager_interface,
 
101
                                  tablet_manager, unbind_resource);
 
102
  wl_resource_set_user_data (resource, tablet_manager);
 
103
  wl_list_insert (&tablet_manager->resource_list,
 
104
                  wl_resource_get_link (resource));
 
105
}
 
106
 
 
107
static MetaWaylandTabletManager *
 
108
meta_wayland_tablet_manager_new (MetaWaylandCompositor *compositor)
 
109
{
 
110
  MetaWaylandTabletManager *tablet_manager;
 
111
 
 
112
  tablet_manager = g_slice_new0 (MetaWaylandTabletManager);
 
113
  tablet_manager->compositor = compositor;
 
114
  tablet_manager->wl_display = compositor->wayland_display;
 
115
  tablet_manager->seats = g_hash_table_new_full (NULL, NULL, NULL,
 
116
                                                 (GDestroyNotify) meta_wayland_tablet_seat_free);
 
117
  wl_list_init (&tablet_manager->resource_list);
 
118
 
 
119
  wl_global_create (tablet_manager->wl_display,
 
120
                    &zwp_tablet_manager_v2_interface, 1,
 
121
                    compositor, bind_tablet_manager);
 
122
 
 
123
  return tablet_manager;
 
124
}
 
125
 
 
126
void
 
127
meta_wayland_tablet_manager_init (MetaWaylandCompositor *compositor)
 
128
{
 
129
  compositor->tablet_manager = meta_wayland_tablet_manager_new (compositor);
 
130
}
 
131
 
 
132
void
 
133
meta_wayland_tablet_manager_free (MetaWaylandTabletManager *tablet_manager)
 
134
{
 
135
  ClutterDeviceManager *device_manager;
 
136
 
 
137
  device_manager = clutter_device_manager_get_default ();
 
138
  g_signal_handlers_disconnect_by_data (device_manager, tablet_manager);
 
139
 
 
140
  g_hash_table_destroy (tablet_manager->seats);
 
141
  g_slice_free (MetaWaylandTabletManager, tablet_manager);
 
142
}
 
143
 
 
144
static MetaWaylandTabletSeat *
 
145
meta_wayland_tablet_manager_lookup_seat (MetaWaylandTabletManager *manager,
 
146
                                         ClutterInputDevice       *device)
 
147
{
 
148
  MetaWaylandTabletSeat *tablet_seat;
 
149
  MetaWaylandSeat *seat;
 
150
  GHashTableIter iter;
 
151
 
 
152
  if (!device || !is_tablet_device (device))
 
153
    return NULL;
 
154
 
 
155
  g_hash_table_iter_init (&iter, manager->seats);
 
156
 
 
157
  while (g_hash_table_iter_next (&iter, (gpointer*) &seat, (gpointer*) &tablet_seat))
 
158
    {
 
159
      if (meta_wayland_tablet_seat_lookup_tablet (tablet_seat, device) ||
 
160
          meta_wayland_tablet_seat_lookup_pad (tablet_seat, device))
 
161
        return tablet_seat;
 
162
    }
 
163
 
 
164
  return NULL;
 
165
}
 
166
 
 
167
gboolean
 
168
meta_wayland_tablet_manager_consumes_event (MetaWaylandTabletManager *manager,
 
169
                                            const ClutterEvent       *event)
 
170
{
 
171
  ClutterInputDevice *device = clutter_event_get_source_device (event);
 
172
 
 
173
  return meta_wayland_tablet_manager_lookup_seat (manager, device) != NULL;
 
174
}
 
175
 
 
176
void
 
177
meta_wayland_tablet_manager_update (MetaWaylandTabletManager *manager,
 
178
                                    const ClutterEvent       *event)
 
179
{
 
180
  ClutterInputDevice *device = clutter_event_get_source_device (event);
 
181
  MetaWaylandTabletSeat *tablet_seat;
 
182
 
 
183
  tablet_seat = meta_wayland_tablet_manager_lookup_seat (manager, device);
 
184
 
 
185
  if (!tablet_seat)
 
186
    return;
 
187
 
 
188
  switch (event->type)
 
189
    {
 
190
    case CLUTTER_PROXIMITY_IN:
 
191
    case CLUTTER_PROXIMITY_OUT:
 
192
    case CLUTTER_BUTTON_PRESS:
 
193
    case CLUTTER_BUTTON_RELEASE:
 
194
    case CLUTTER_MOTION:
 
195
    case CLUTTER_PAD_BUTTON_PRESS:
 
196
    case CLUTTER_PAD_BUTTON_RELEASE:
 
197
    case CLUTTER_PAD_RING:
 
198
    case CLUTTER_PAD_STRIP:
 
199
      meta_wayland_tablet_seat_update (tablet_seat, event);
 
200
      break;
 
201
    default:
 
202
      break;
 
203
    }
 
204
}
 
205
 
 
206
gboolean
 
207
meta_wayland_tablet_manager_handle_event (MetaWaylandTabletManager *manager,
 
208
                                          const ClutterEvent       *event)
 
209
{
 
210
  ClutterInputDevice *device = clutter_event_get_source_device (event);
 
211
  MetaWaylandTabletSeat *tablet_seat;
 
212
 
 
213
  tablet_seat = meta_wayland_tablet_manager_lookup_seat (manager, device);
 
214
 
 
215
  if (!tablet_seat)
 
216
    return CLUTTER_EVENT_PROPAGATE;
 
217
 
 
218
  switch (event->type)
 
219
    {
 
220
    case CLUTTER_PROXIMITY_IN:
 
221
    case CLUTTER_PROXIMITY_OUT:
 
222
    case CLUTTER_BUTTON_PRESS:
 
223
    case CLUTTER_BUTTON_RELEASE:
 
224
    case CLUTTER_MOTION:
 
225
    case CLUTTER_PAD_BUTTON_PRESS:
 
226
    case CLUTTER_PAD_BUTTON_RELEASE:
 
227
    case CLUTTER_PAD_RING:
 
228
    case CLUTTER_PAD_STRIP:
 
229
      return meta_wayland_tablet_seat_handle_event (tablet_seat, event);
 
230
    default:
 
231
      return CLUTTER_EVENT_PROPAGATE;
 
232
    }
 
233
}
 
234
 
 
235
MetaWaylandTabletSeat *
 
236
meta_wayland_tablet_manager_ensure_seat (MetaWaylandTabletManager *manager,
 
237
                                         MetaWaylandSeat          *seat)
 
238
{
 
239
  MetaWaylandTabletSeat *tablet_seat;
 
240
 
 
241
  tablet_seat = g_hash_table_lookup (manager->seats, seat);
 
242
 
 
243
  if (!tablet_seat)
 
244
    {
 
245
      tablet_seat = meta_wayland_tablet_seat_new (manager, seat);
 
246
      g_hash_table_insert (manager->seats, seat, tablet_seat);
 
247
    }
 
248
 
 
249
  return tablet_seat;
 
250
}
 
251
 
 
252
void
 
253
meta_wayland_tablet_manager_update_cursor_position (MetaWaylandTabletManager *manager,
 
254
                                                    const ClutterEvent       *event)
 
255
{
 
256
  MetaWaylandTabletSeat *tablet_seat = NULL;
 
257
  MetaWaylandTabletTool *tool = NULL;
 
258
  ClutterInputDeviceTool *device_tool;
 
259
  ClutterInputDevice *device;
 
260
 
 
261
  device = clutter_event_get_source_device (event);
 
262
  device_tool = clutter_event_get_device_tool (event);
 
263
 
 
264
  if (device)
 
265
    tablet_seat = meta_wayland_tablet_manager_lookup_seat (manager, device);
 
266
 
 
267
  if (tablet_seat && device_tool)
 
268
    tool = meta_wayland_tablet_seat_lookup_tool (tablet_seat, device_tool);
 
269
 
 
270
  if (tool)
 
271
    {
 
272
      gfloat new_x, new_y;
 
273
 
 
274
      clutter_event_get_coords (event, &new_x, &new_y);
 
275
      meta_wayland_tablet_tool_set_cursor_position (tool, new_x, new_y);
 
276
    }
 
277
}