[gtk+/wip/wayland-tablet: 1251/1271] gdkdevice: Add GdkDeviceTool to identify device tools



commit d51914ec6baf20dd969d29528d564e5dccc3b580
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue Jan 6 14:38:14 2015 +0100

    gdkdevice: Add GdkDeviceTool to identify device tools
    
    GdkDeviceTool is an opaque typedef that can be used to identify a given
    tool (eg. pens on tablets) during the app/device lifetime. Tools are only
    set on non-master devices, and are owned by these.
    
    The accounting functions are made private, the only public call on
    GdkDeviceTool so far is gdk_device_tool_get_serial(), useful to identify
    the tool across runs.

 docs/reference/gdk/gdk3-sections.txt |    3 +
 gdk/gdkdevice.c                      |   93 ++++++++++++++++++++++++++++++++++
 gdk/gdkdevice.h                      |    9 +++
 gdk/gdkdeviceprivate.h               |   19 +++++++
 4 files changed, 124 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gdk/gdk3-sections.txt b/docs/reference/gdk/gdk3-sections.txt
index 0231530..f27c45a 100644
--- a/docs/reference/gdk/gdk3-sections.txt
+++ b/docs/reference/gdk/gdk3-sections.txt
@@ -752,6 +752,9 @@ gdk_device_list_axes
 gdk_device_get_axis_value
 gdk_device_get_last_event_window
 
+<SUBSECTION>
+gdk_device_tool_get_serial
+
 <SUBSECTION Standard>
 GDK_TYPE_AXIS_USE
 GDK_TYPE_EXTENSION_MODE
diff --git a/gdk/gdkdevice.c b/gdk/gdkdevice.c
index 8d5706d..e47844a 100644
--- a/gdk/gdkdevice.c
+++ b/gdk/gdkdevice.c
@@ -337,6 +337,12 @@ gdk_device_dispose (GObject *object)
       device->axes = NULL;
     }
 
+  if (device->tools)
+    {
+      g_ptr_array_free (device->tools, TRUE);
+      device->tools = NULL;
+    }
+
   g_free (device->name);
   g_free (device->keys);
 
@@ -1912,3 +1918,90 @@ gdk_device_get_axes (GdkDevice *device)
 
   return device->axis_flags;
 }
+
+GdkDeviceTool *
+gdk_device_tool_ref (GdkDeviceTool *tool)
+{
+  tool->ref_count++;
+  return tool;
+}
+
+void
+gdk_device_tool_unref (GdkDeviceTool *tool)
+{
+  tool->ref_count--;
+
+  if (tool->ref_count == 0)
+    g_free (tool);
+}
+
+G_DEFINE_BOXED_TYPE (GdkDeviceTool, gdk_device_tool,
+                     gdk_device_tool_ref, gdk_device_tool_unref);
+
+GdkDeviceTool *
+gdk_device_tool_new (guint serial)
+{
+  GdkDeviceTool *tool;
+
+  tool = g_new0 (GdkDeviceTool, 1);
+  tool->serial = serial;
+
+  return tool;
+}
+
+void
+gdk_device_add_tool (GdkDevice     *device,
+                     GdkDeviceTool *tool)
+{
+  g_return_if_fail (GDK_IS_DEVICE (device));
+  g_return_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER);
+  g_return_if_fail (tool != NULL);
+
+  if (!device->tools)
+    device->tools = g_ptr_array_new_with_free_func ((GDestroyNotify) gdk_device_tool_unref);
+
+  g_ptr_array_add (device->tools, gdk_device_tool_ref (tool));
+}
+
+GdkDeviceTool *
+gdk_device_lookup_tool (GdkDevice *device,
+                        guint      serial)
+{
+  GdkDeviceTool *tool;
+  guint i;
+
+  g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
+  g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
+
+  if (!device->tools)
+    return NULL;
+
+  for (i = 0; i < device->tools->len; i++)
+    {
+      tool = g_ptr_array_index (device->tools, i);
+
+      if (tool->serial == serial)
+        return tool;
+    }
+
+  return NULL;
+}
+
+/**
+ * gdk_device_tool_get_serial:
+ * @tool: a #GdkDeviceTool
+ *
+ * Gets the serial of this tool, this value can be used to identify a
+ * physical tool (eg. a tablet pen) across program executions.
+ *
+ * Returns: The serial ID for this tool
+ *
+ * Since: 3.18
+ **/
+guint
+gdk_device_tool_get_serial (GdkDeviceTool *tool)
+{
+  g_return_val_if_fail (tool != NULL, 0);
+
+  return tool->serial;
+}
diff --git a/gdk/gdkdevice.h b/gdk/gdkdevice.h
index 45e0f0e..3c57d6e 100644
--- a/gdk/gdkdevice.h
+++ b/gdk/gdkdevice.h
@@ -32,7 +32,10 @@ G_BEGIN_DECLS
 #define GDK_DEVICE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GDK_TYPE_DEVICE, GdkDevice))
 #define GDK_IS_DEVICE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GDK_TYPE_DEVICE))
 
+#define GDK_TYPE_DEVICE_TOOL    (gdk_device_tool_get_type ())
+
 typedef struct _GdkTimeCoord GdkTimeCoord;
+typedef struct _GdkDeviceTool GdkDeviceTool;
 
 /**
  * GdkInputSource:
@@ -295,6 +298,12 @@ const gchar *gdk_device_get_product_id      (GdkDevice *device);
 GDK_AVAILABLE_IN_3_18
 GdkAxisFlags gdk_device_get_axes            (GdkDevice *device);
 
+GDK_AVAILABLE_IN_3_18
+GType gdk_device_tool_get_type (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_3_18
+guint gdk_device_tool_get_serial (GdkDeviceTool *tool);
+
 G_END_DECLS
 
 #endif /* __GDK_DEVICE_H__ */
diff --git a/gdk/gdkdeviceprivate.h b/gdk/gdkdeviceprivate.h
index 123fa2e..bd3dd50 100644
--- a/gdk/gdkdeviceprivate.h
+++ b/gdk/gdkdeviceprivate.h
@@ -31,6 +31,12 @@ G_BEGIN_DECLS
 typedef struct _GdkDeviceClass GdkDeviceClass;
 typedef struct _GdkDeviceKey GdkDeviceKey;
 
+struct _GdkDeviceTool
+{
+  guint serial;
+  gint ref_count;
+};
+
 struct _GdkDeviceKey
 {
   guint keyval;
@@ -60,6 +66,8 @@ struct _GdkDevice
 
   gchar *vendor_id;
   gchar *product_id;
+
+  GPtrArray *tools;
 };
 
 struct _GdkDeviceClass
@@ -177,6 +185,17 @@ GdkWindow * _gdk_device_window_at_position    (GdkDevice        *device,
                                                GdkModifierType  *mask,
                                                gboolean          get_toplevel);
 
+/* Device tools */
+GdkDeviceTool *gdk_device_tool_new    (guint          serial);
+GdkDeviceTool *gdk_device_lookup_tool (GdkDevice     *device,
+                                       guint          serial);
+void           gdk_device_add_tool    (GdkDevice     *device,
+                                       GdkDeviceTool *tool);
+
+GdkDeviceTool *gdk_device_tool_ref    (GdkDeviceTool *tool);
+void           gdk_device_tool_unref  (GdkDeviceTool *tool);
+
+
 G_END_DECLS
 
 #endif /* __GDK_DEVICE_PRIVATE_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]