From 62f0fb12b1fa946779f0efa406159a355811fdc5 Mon Sep 17 00:00:00 2001
From: Carlos Garnacho <carlosg@gnome.org>
Date: Mon, 19 Feb 2018 16:50:52 +0100
Subject: [PATCH] backends: Monitor changes in active tools' settings

So the changes can be instantly applied while the tool is in proximity.
Before we would just do it on proximity-in, which doesn't provide a
good look&feel while modifying the tool settings in g-c-c.

https://gitlab.gnome.org/GNOME/mutter/issues/38

Closes: #38
---
 src/backends/meta-input-settings.c | 71 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 68 insertions(+), 3 deletions(-)

diff --git a/src/backends/meta-input-settings.c b/src/backends/meta-input-settings.c
index 0658755..ec0fc9f 100644
--- a/src/backends/meta-input-settings.c
+++ b/src/backends/meta-input-settings.c
@@ -41,6 +41,16 @@ static GQuark quark_tool_settings = 0;
 
 typedef struct _MetaInputSettingsPrivate MetaInputSettingsPrivate;
 typedef struct _DeviceMappingInfo DeviceMappingInfo;
+typedef struct _CurrentToolInfo CurrentToolInfo;
+
+struct _CurrentToolInfo
+{
+  MetaInputSettings *input_settings;
+  ClutterInputDevice *device;
+  ClutterInputDeviceTool *tool;
+  GSettings *settings;
+  guint changed_id;
+};
 
 struct _DeviceMappingInfo
 {
@@ -68,6 +78,8 @@ struct _MetaInputSettingsPrivate
 
   GHashTable *mappable_devices;
 
+  GHashTable *current_tools;
+
   ClutterVirtualInputDevice *virtual_pad_keyboard;
 
 #ifdef HAVE_LIBWACOM
@@ -144,6 +156,7 @@ meta_input_settings_dispose (GObject *object)
   g_clear_object (&priv->keyboard_settings);
   g_clear_object (&priv->gsd_settings);
   g_clear_pointer (&priv->mappable_devices, g_hash_table_unref);
+  g_clear_pointer (&priv->current_tools, g_hash_table_unref);
 
   if (priv->monitors_changed_id && priv->monitor_manager)
     {
@@ -1510,22 +1523,71 @@ meta_input_settings_device_removed (ClutterDeviceManager *device_manager,
 
   priv = meta_input_settings_get_instance_private (input_settings);
   g_hash_table_remove (priv->mappable_devices, device);
+  g_hash_table_remove (priv->current_tools, device);
 
   if (g_hash_table_remove (priv->two_finger_devices, device) &&
       g_hash_table_size (priv->two_finger_devices) == 0)
     apply_device_settings (input_settings, NULL);
 }
 
+static void
+current_tool_changed_cb (GSettings  *settings,
+                         const char *key,
+                         gpointer    user_data)
+{
+  CurrentToolInfo *info = user_data;
+
+  apply_stylus_settings (info->input_settings, info->device, info->tool);
+}
+
+static CurrentToolInfo *
+current_tool_info_new (MetaInputSettings      *input_settings,
+                       ClutterInputDevice     *device,
+                       ClutterInputDeviceTool *tool)
+{
+  CurrentToolInfo *info;
+
+  info = g_new0 (CurrentToolInfo, 1);
+  info->input_settings = input_settings;
+  info->device = device;
+  info->tool = tool;
+  info->settings = lookup_tool_settings (tool, device);
+  info->changed_id =
+    g_signal_connect (info->settings, "changed",
+                      G_CALLBACK (current_tool_changed_cb),
+                      info);
+  return info;
+}
+
+static void
+current_tool_info_free (CurrentToolInfo *info)
+{
+  g_signal_handler_disconnect (info->settings, info->changed_id);
+  g_free (info);
+}
+
 static void
 meta_input_settings_tool_changed (ClutterDeviceManager   *device_manager,
                                   ClutterInputDevice     *device,
                                   ClutterInputDeviceTool *tool,
                                   MetaInputSettings      *input_settings)
 {
-  if (!tool)
-    return;
+  MetaInputSettingsPrivate *priv;
 
-  apply_stylus_settings (input_settings, device, tool);
+  priv = meta_input_settings_get_instance_private (input_settings);
+
+  if (tool)
+    {
+      CurrentToolInfo *current_tool;
+
+      current_tool = current_tool_info_new (input_settings, device, tool);
+      g_hash_table_insert (priv->current_tools, device, current_tool);
+      apply_stylus_settings (input_settings, device, tool);
+    }
+  else
+    {
+      g_hash_table_remove (priv->current_tools, device);
+    }
 }
 
 static void
@@ -1616,6 +1678,9 @@ meta_input_settings_init (MetaInputSettings *settings)
   priv->mappable_devices =
     g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) device_mapping_info_free);
 
+  priv->current_tools =
+    g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) current_tool_info_free);
+
   priv->monitor_manager = g_object_ref (meta_monitor_manager_get ());
   g_signal_connect (priv->monitor_manager, "monitors-changed-internal",
                     G_CALLBACK (monitors_changed_cb), settings);
-- 
2.16.1

