summaryrefslogtreecommitdiffstats
path: root/gdk/gdktouchcluster.c
blob: e8188f705a5cde7c3f6a57652ac2b5da46ea8610 (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
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 2011 Carlos Garnacho <carlosg@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include "gdktouchcluster.h"
#include "gdkintl.h"

/**
 * SECTION:touchcluster
 * @Short_description: Multitouch handling
 * @Title: Multitouch
 * @See_also: #GdkEventMultiTouch
 *
 * #GdkTouchCluster is an object that gathers touch IDs from a
 * touch-enabled slave #GdkDevice, in order to send
 * #GdkEventMultiTouch<!-- --> events whenever a contained touch ID
 * is updated.
 *
 * #GdkTouchCluster<!-- -->s are always associated to a window,
 * you need to create them through gdk_window_create_touch_cluster(),
 * and free them through gdk_window_remove_touch_cluster().
 *
 * Touch IDs from devices can be obtained from %GDK_TOUCH_PRESS,
 * %GDK_TOUCH_MOTION or %GDK_TOUCH_RELEASE events through
 * gdk_event_get_touch_id(), and then be added via
 * gdk_touch_cluster_add_touch(). Note that touch IDs are
 * highly transitive, even be an incrementable number only
 * identifying the current touch event stream, so touch IDs
 * should always be gotten from these events.
 *
 * Anytime a touch ID is within a cluster, no %GDK_TOUCH_PRESS,
 * %GDK_TOUCH_MOTION or %GDK_TOUCH_RELEASE events will happen
 * for the individual touch. The event will be available instead
 * as part of the #GdkMultitouchEvent that will be emitted. This
 * will hold true until gdk_touch_cluster_remove_touch() is
 * called for it. Note that GTK+ will automatically take a
 * touch ID out of any cluster if %GDK_TOUCH_RELEASE is gotten
 * internally.
 */

typedef struct GdkTouchClusterPrivate GdkTouchClusterPrivate;

struct GdkTouchClusterPrivate
{
  GdkDevice *device;
  GList *touches;
};

enum {
  PROP_0,
  PROP_DEVICE
};

enum {
  TOUCH_ADDED,
  TOUCH_REMOVED,
  LAST_SIGNAL
};

static guint signals [LAST_SIGNAL] = { 0 };

static void   gdk_touch_cluster_finalize     (GObject      *object);
static void   gdk_touch_cluster_set_property (GObject      *object,
                                              guint         prop_id,
                                              const GValue *value,
                                              GParamSpec   *pspec);
static void   gdk_touch_cluster_get_property (GObject      *object,
                                              guint         prop_id,
                                              GValue       *value,
                                              GParamSpec   *pspec);


G_DEFINE_TYPE (GdkTouchCluster, gdk_touch_cluster, G_TYPE_OBJECT)

static void
gdk_touch_cluster_class_init (GdkTouchClusterClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gdk_touch_cluster_finalize;
  object_class->get_property = gdk_touch_cluster_get_property;
  object_class->set_property = gdk_touch_cluster_set_property;

  g_object_class_install_property (object_class,
                                   PROP_DEVICE,
                                   g_param_spec_object ("device",
                                                        P_("Device"),
                                                        P_("Device attached to the cluster"),
                                                        GDK_TYPE_DEVICE,
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));

  signals[TOUCH_ADDED] =
    g_signal_new (g_intern_static_string ("touch-added"),
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GdkTouchClusterClass, touch_added),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__UINT,
                  G_TYPE_NONE, 1, G_TYPE_UINT);
  signals[TOUCH_REMOVED] =
    g_signal_new (g_intern_static_string ("touch-removed"),
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GdkTouchClusterClass, touch_removed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__UINT,
                  G_TYPE_NONE, 1, G_TYPE_UINT);

  g_type_class_add_private (object_class, sizeof (GdkTouchClusterPrivate));
}

static void
gdk_touch_cluster_init (GdkTouchCluster *cluster)
{
  cluster->priv = G_TYPE_INSTANCE_GET_PRIVATE (cluster,
                                               GDK_TYPE_TOUCH_CLUSTER,
                                               GdkTouchClusterPrivate);
}

static void
gdk_touch_cluster_finalize (GObject *object)
{
  GdkTouchClusterPrivate *priv;

  priv = GDK_TOUCH_CLUSTER (object)->priv;
  g_list_free (priv->touches);

  G_OBJECT_CLASS (gdk_touch_cluster_parent_class)->finalize (object);
}

static void
gdk_touch_cluster_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  GdkTouchClusterPrivate *priv;

  priv = GDK_TOUCH_CLUSTER (object)->priv;

  switch (prop_id)
    {
    case PROP_DEVICE:
      gdk_touch_cluster_set_device (GDK_TOUCH_CLUSTER (object),
                                    g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdk_touch_cluster_get_property (GObject      *object,
                                guint         prop_id,
                                GValue       *value,
                                GParamSpec   *pspec)
{
  GdkTouchClusterPrivate *priv;

  priv = GDK_TOUCH_CLUSTER (object)->priv;

  switch (prop_id)
    {
    case PROP_DEVICE:
      g_value_set_object (value, priv->device);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

/**
 * gdk_touch_cluster_add_touch:
 * @cluster: a #GdkTouchCluster
 * @touch_id: a touch ID from a touch event
 *
 * Adds a touch ID to @cluster, so it will generate a
 * %GDK_MULTITOUCH_ADDED event, followed by %GDK_MULTITOUCH_UPDATED
 * events whenever this touch ID is updated.
 *
 * If @touch_id already pertained to another #GdkTouchCluster, it
 * will be removed from it, generating a %GDK_MULTITOUCH_REMOVED
 * for that another cluster.
 **/
void
gdk_touch_cluster_add_touch (GdkTouchCluster *cluster,
                             guint            touch_id)
{
  GdkTouchClusterPrivate *priv;

  g_return_if_fail (GDK_IS_TOUCH_CLUSTER (cluster));

  priv = cluster->priv;

  if (!g_list_find (priv->touches, GUINT_TO_POINTER (touch_id)))
    {
      priv->touches = g_list_prepend (priv->touches, GUINT_TO_POINTER (touch_id));
      g_signal_emit (cluster, signals [TOUCH_ADDED], 0, touch_id);
    }
}

/**
 * gdk_touch_cluster_remove_touch:
 * @cluster: a #GdkTouchCluster
 * @touch_id: a touch ID from a touch event
 *
 * Removes a touch ID from @cluster, generating a %GDK_MULTITOUCH_REMOVED
 * event for @cluster, and causing any further input from @touch_id
 * to be reported trough %GDK_TOUCH_MOTION events.
 *
 * <note><para>
 * Note that GTK+ automatically removes a touch ID from any cluster
 * if a %GDK_TOUCH_RELEASE event is gotten internally.
 * </para></note>
 **/
void
gdk_touch_cluster_remove_touch (GdkTouchCluster *cluster,
                                guint            touch_id)
{
  GdkTouchClusterPrivate *priv;
  GList *link;

  g_return_if_fail (GDK_IS_TOUCH_CLUSTER (cluster));

  priv = cluster->priv;

  link = g_list_find (priv->touches, GUINT_TO_POINTER (touch_id));

  if (link)
    {
      priv->touches = g_list_remove_link (priv->touches, link);
      g_signal_emit (cluster, signals [TOUCH_REMOVED], 0, touch_id);
      g_list_free1 (link);
    }
}

/**
 * gdk_touch_cluster_remove_all:
 * @cluster: a #GdkTouchCluster
 *
 * Removes all touch IDs from @cluster.
 **/
void
gdk_touch_cluster_remove_all (GdkTouchCluster *cluster)
{
  GdkTouchClusterPrivate *priv;
  GList *link;

  g_return_if_fail (GDK_IS_TOUCH_CLUSTER (cluster));

  priv = cluster->priv;
  link = priv->touches;

  while (link)
    {
      priv->touches = g_list_remove_link (priv->touches, link);
      g_signal_emit (cluster, signals [TOUCH_REMOVED], 0, link->data);
    }

  g_list_free (priv->touches);
  priv->touches = NULL;
}


/**
 * gdk_touch_cluster_get_touches:
 * @cluster: a #GdkTouchCluster
 *
 * Returns a const list of touch IDs as #guint.
 *
 * Returns: (transfer none): A list of touch IDs.
 **/
GList *
gdk_touch_cluster_get_touches (GdkTouchCluster *cluster)
{
  GdkTouchClusterPrivate *priv;

  g_return_val_if_fail (GDK_IS_TOUCH_CLUSTER (cluster), NULL);

  priv = cluster->priv;
  return priv->touches;
}

/**
 * gdk_touch_cluster_set_device:
 * @cluster: a #GdkTouchCluster
 * @device: a #GdkDevice
 *
 * Sets the current device associated to @cluster, all contained
 * touch IDs must pertain to this device. As a consequence,
 * gdk_touch_cluster_remove_all() will be called on @cluster
 * if the current device changes.
 **/
void
gdk_touch_cluster_set_device (GdkTouchCluster *cluster,
                              GdkDevice       *device)
{
  GdkTouchClusterPrivate *priv;

  g_return_if_fail (GDK_IS_TOUCH_CLUSTER (cluster));
  g_return_if_fail (!device || GDK_IS_DEVICE (device));

  priv = cluster->priv;

  if (priv->device != device)
    gdk_touch_cluster_remove_all (cluster);

  priv->device = device;
}

/**
 * gdk_touch_cluster_get_device:
 * @cluster: a #GdkTouchCluster
 *
 * Returns the slave/floating device this touch cluster pertains to,
 * only touch IDs from this device can be included in @cluster.
 * the #GdkDevice will typically have the %GDK_SOURCE_TOUCH input source.
 *
 * Returns: (transfer none): The #GdkDevice generating the contained touch IDs
 **/
GdkDevice *
gdk_touch_cluster_get_device (GdkTouchCluster *cluster)
{
  GdkTouchClusterPrivate *priv;

  g_return_val_if_fail (GDK_IS_TOUCH_CLUSTER (cluster), NULL);

  priv = cluster->priv;
  return priv->device;
}