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



commit 69a787582dd133ed4392abad9102d0f7238cfc44
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                      |  119 ++++++++++++++++++++++++++++-----
 gdk/gdkdevice.h                      |    9 +++
 gdk/gdkdeviceprivate.h               |   17 +++++
 4 files changed, 130 insertions(+), 18 deletions(-)
---
diff --git a/docs/reference/gdk/gdk3-sections.txt b/docs/reference/gdk/gdk3-sections.txt
index b99607b..8e86ffc 100644
--- a/docs/reference/gdk/gdk3-sections.txt
+++ b/docs/reference/gdk/gdk3-sections.txt
@@ -761,6 +761,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 641e598..45b783a 100644
--- a/gdk/gdkdevice.c
+++ b/gdk/gdkdevice.c
@@ -344,6 +344,13 @@ gdk_device_finalize (GObject *object)
 
   g_clear_pointer (&device->name, g_free);
   g_clear_pointer (&device->keys, g_free);
+
+  if (device->tools)
+    {
+      g_ptr_array_free (device->tools, TRUE);
+      device->tools = NULL;
+    }
+
   g_clear_pointer (&device->vendor_id, g_free);
   g_clear_pointer (&device->product_id, g_free);
 
@@ -464,13 +471,11 @@ gdk_device_get_property (GObject    *object,
     case PROP_PRODUCT_ID:
       g_value_set_string (value, device->product_id);
       break;
-<<<<<<< 676016cb4a06add2bc4cfe46f5fcb38f2a9b19af
     case PROP_SEAT:
       g_value_set_object (value, device->seat);
-=======
+      break;
     case PROP_AXES:
       g_value_set_flags (value, device->axis_flags);
->>>>>>> device: Add gdk_device_get_axes(), and ::axes property
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -1510,14 +1515,10 @@ _gdk_device_reset_axes (GdkDevice *device)
   for (i = device->axes->len - 1; i >= 0; i--)
     g_array_remove_index (device->axes, i);
 
-<<<<<<< 676016cb4a06add2bc4cfe46f5fcb38f2a9b19af
-  g_object_notify_by_pspec (G_OBJECT (device), device_props[PROP_N_AXES]);
-=======
   device->axis_flags = 0;
 
-  g_object_notify (G_OBJECT (device), "n-axes");
-  g_object_notify (G_OBJECT (device), "axes");
->>>>>>> device: Add gdk_device_get_axes(), and ::axes property
+  g_object_notify_by_pspec (G_OBJECT (device), device_props[PROP_N_AXES]);
+  g_object_notify_by_pspec (G_OBJECT (device), device_props[PROP_AXES]);
 }
 
 guint
@@ -1558,14 +1559,10 @@ _gdk_device_add_axis (GdkDevice   *device,
   device->axes = g_array_append_val (device->axes, axis_info);
   pos = device->axes->len - 1;
 
-<<<<<<< 676016cb4a06add2bc4cfe46f5fcb38f2a9b19af
-  g_object_notify_by_pspec (G_OBJECT (device), device_props[PROP_N_AXES]);
-=======
   device->axis_flags |= (1 << use);
 
-  g_object_notify (G_OBJECT (device), "n-axes");
-  g_object_notify (G_OBJECT (device), "axes");
->>>>>>> device: Add gdk_device_get_axes(), and ::axes property
+  g_object_notify_by_pspec (G_OBJECT (device), device_props[PROP_N_AXES]);
+  g_object_notify_by_pspec (G_OBJECT (device), device_props[PROP_AXES]);
 
   return pos;
 }
@@ -1941,7 +1938,6 @@ gdk_device_get_product_id (GdkDevice *device)
   return device->product_id;
 }
 
-<<<<<<< 676016cb4a06add2bc4cfe46f5fcb38f2a9b19af
 void
 gdk_device_set_seat (GdkDevice *device,
                      GdkSeat   *seat)
@@ -1973,7 +1969,8 @@ gdk_device_get_seat (GdkDevice *device)
   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
 
   return device->seat;
-=======
+}
+
 /**
  * gdk_device_get_axes:
  * @device: a #GdkDevice
@@ -1988,5 +1985,91 @@ gdk_device_get_axes (GdkDevice *device)
   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
 
   return device->axis_flags;
->>>>>>> device: Add gdk_device_get_axes(), and ::axes property
+}
+
+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 d150cf9..00adc5e 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:
@@ -298,6 +301,12 @@ GdkSeat     *gdk_device_get_seat            (GdkDevice *device);
 GDK_AVAILABLE_IN_3_20
 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 c933f3a..ca67372 100644
--- a/gdk/gdkdeviceprivate.h
+++ b/gdk/gdkdeviceprivate.h
@@ -32,6 +32,12 @@ G_BEGIN_DECLS
 typedef struct _GdkDeviceClass GdkDeviceClass;
 typedef struct _GdkDeviceKey GdkDeviceKey;
 
+struct _GdkDeviceTool
+{
+  guint serial;
+  gint ref_count;
+};
+
 struct _GdkDeviceKey
 {
   guint keyval;
@@ -63,6 +69,7 @@ struct _GdkDevice
   gchar *product_id;
 
   GdkSeat *seat;
+  GPtrArray *tools;
 };
 
 struct _GdkDeviceClass
@@ -183,6 +190,16 @@ GdkWindow * _gdk_device_window_at_position    (GdkDevice        *device,
 void  gdk_device_set_seat  (GdkDevice *device,
                             GdkSeat   *seat);
 
+/* 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]