[mutter/wip/carlosg/input-thread: 124/130] backends: Seal MetaInputSettings entrypoints with a mutex




commit 6e7feb14adda4ff1132793914f0e68a70cfb2bf8
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Nov 19 16:50:48 2020 +0100

    backends: Seal MetaInputSettings entrypoints with a mutex
    
    This object will have all GSettings signal handlers executed in the
    input thread, but its other entrypoints are API meant to be called
    normally from the main thread. Protect all access with a mutex to
    make it dead sure that everyone gets along.

 src/backends/meta-input-settings.c | 84 +++++++++++++++++++++++++++++++-------
 1 file changed, 70 insertions(+), 14 deletions(-)
---
diff --git a/src/backends/meta-input-settings.c b/src/backends/meta-input-settings.c
index 0b70681aef..4b154927a6 100644
--- a/src/backends/meta-input-settings.c
+++ b/src/backends/meta-input-settings.c
@@ -68,6 +68,7 @@ struct _MetaInputSettingsPrivate
 {
   ClutterSeat *seat;
   gulong monitors_changed_id;
+  GMutex lock;
 
   GSettings *mouse_settings;
   GSettings *touchpad_settings;
@@ -1103,6 +1104,8 @@ meta_input_settings_changed_cb (GSettings  *settings,
   MetaInputSettings *input_settings = META_INPUT_SETTINGS (user_data);
   MetaInputSettingsPrivate *priv = meta_input_settings_get_instance_private (input_settings);
 
+  g_mutex_lock (&priv->lock);
+
   if (settings == priv->mouse_settings)
     {
       if (strcmp (key, "left-handed") == 0)
@@ -1164,6 +1167,8 @@ meta_input_settings_changed_cb (GSettings  *settings,
       else if (strcmp (key, "remember-numlock-state") == 0)
         meta_input_settings_maybe_save_numlock_state (input_settings);
     }
+
+  g_mutex_unlock (&priv->lock);
 }
 
 static void
@@ -1171,14 +1176,22 @@ mapped_device_changed_cb (GSettings         *settings,
                           const gchar       *key,
                           DeviceMappingInfo *info)
 {
+  MetaInputSettings *input_settings = info->input_settings;
+  MetaInputSettingsPrivate *priv =
+    meta_input_settings_get_instance_private (input_settings);
+
+  g_mutex_lock (&priv->lock);
+
   if (strcmp (key, "mapping") == 0)
-    update_tablet_mapping (info->input_settings, settings, info->device);
+    update_tablet_mapping (input_settings, settings, info->device);
   else if (strcmp (key, "area") == 0)
-    update_tablet_area (info->input_settings, settings, info->device);
+    update_tablet_area (input_settings, settings, info->device);
   else if (strcmp (key, "keep-aspect") == 0)
-    update_tablet_keep_aspect (info->input_settings, settings, info->device);
+    update_tablet_keep_aspect (input_settings, settings, info->device);
   else if (strcmp (key, "left-handed") == 0)
-    update_tablet_left_handed (info->input_settings, settings, info->device);
+    update_tablet_left_handed (input_settings, settings, info->device);
+
+  g_mutex_unlock (&priv->lock);
 }
 
 static void
@@ -1275,8 +1288,12 @@ meta_input_keyboard_a11y_settings_changed (GSettings  *settings,
                                            gpointer    user_data)
 {
   MetaInputSettings *input_settings = META_INPUT_SETTINGS (user_data);
+  MetaInputSettingsPrivate *priv =
+    meta_input_settings_get_instance_private (input_settings);
 
+  g_mutex_lock (&priv->lock);
   load_keyboard_a11y_settings (input_settings);
+  g_mutex_unlock (&priv->lock);
 }
 
 struct _pointer_a11y_settings_flags_pair {
@@ -1366,8 +1383,12 @@ meta_input_mouse_a11y_settings_changed (GSettings  *settings,
                                         gpointer    user_data)
 {
   MetaInputSettings *input_settings = META_INPUT_SETTINGS (user_data);
+  MetaInputSettingsPrivate *priv =
+    meta_input_settings_get_instance_private (input_settings);
 
+  g_mutex_lock (&priv->lock);
   load_pointer_a11y_settings (input_settings);
+  g_mutex_unlock (&priv->lock);
 }
 
 static GSettings *
@@ -1637,11 +1658,13 @@ meta_input_settings_add_device (MetaInputSettings  *input_settings,
   if (clutter_input_device_get_device_mode (device) == CLUTTER_INPUT_MODE_LOGICAL)
     return;
 
+  g_mutex_lock (&priv->lock);
   priv->devices = g_list_prepend (priv->devices, device);
   evaluate_two_finger_scrolling (input_settings, device);
 
   apply_device_settings (input_settings, device);
   check_add_mappable_device (input_settings, device);
+  g_mutex_unlock (&priv->lock);
 }
 
 void
@@ -1651,6 +1674,8 @@ meta_input_settings_remove_device (MetaInputSettings  *input_settings,
   MetaInputSettingsPrivate *priv;
 
   priv = meta_input_settings_get_instance_private (input_settings);
+  g_mutex_lock (&priv->lock);
+
   g_hash_table_remove (priv->mappable_devices, device);
   g_hash_table_remove (priv->current_tools, device);
 
@@ -1659,6 +1684,7 @@ meta_input_settings_remove_device (MetaInputSettings  *input_settings,
     apply_device_settings (input_settings, NULL);
 
   priv->devices = g_list_remove (priv->devices, device);
+  g_mutex_unlock (&priv->lock);
 }
 
 static void
@@ -1667,8 +1693,13 @@ current_tool_changed_cb (GSettings  *settings,
                          gpointer    user_data)
 {
   CurrentToolInfo *info = user_data;
+  MetaInputSettings *input_settings = info->input_settings;
+  MetaInputSettingsPrivate *priv =
+    meta_input_settings_get_instance_private (input_settings);
 
+  g_mutex_lock (&priv->lock);
   apply_stylus_settings (info->input_settings, info->device, info->tool);
+  g_mutex_unlock (&priv->lock);
 }
 
 static CurrentToolInfo *
@@ -1798,6 +1829,8 @@ meta_input_settings_init (MetaInputSettings *settings)
 
   priv = meta_input_settings_get_instance_private (settings);
 
+  g_mutex_init (&priv->lock);
+
   priv->mouse_settings = g_settings_new ("org.gnome.desktop.peripherals.mouse");
   g_signal_connect (priv->mouse_settings, "changed",
                     G_CALLBACK (meta_input_settings_changed_cb), settings);
@@ -1846,18 +1879,22 @@ meta_input_settings_maybe_save_numlock_state (MetaInputSettings *input_settings)
   gboolean numlock_state;
 
   priv = meta_input_settings_get_instance_private (input_settings);
+  g_mutex_lock (&priv->lock);
 
   if (!g_settings_get_boolean (priv->keyboard_settings, "remember-numlock-state"))
-    return;
+    goto out;
 
   seat = clutter_backend_get_default_seat (clutter_get_default_backend ());
   keymap = clutter_seat_get_keymap (seat);
   numlock_state = clutter_keymap_get_num_lock_state (keymap);
 
   if (numlock_state == g_settings_get_boolean (priv->keyboard_settings, "numlock-state"))
-    return;
+    goto out;
 
   g_settings_set_boolean (priv->keyboard_settings, "numlock-state", numlock_state);
+
+ out:
+  g_mutex_unlock (&priv->lock);
 }
 
 void
@@ -1867,12 +1904,15 @@ meta_input_settings_maybe_restore_numlock_state (MetaInputSettings *input_settin
   gboolean numlock_state;
 
   priv = meta_input_settings_get_instance_private (input_settings);
+  g_mutex_lock (&priv->lock);
 
-  if (!g_settings_get_boolean (priv->keyboard_settings, "remember-numlock-state"))
-    return;
+  if (g_settings_get_boolean (priv->keyboard_settings, "remember-numlock-state"))
+    {
+      numlock_state = g_settings_get_boolean (priv->keyboard_settings, "numlock-state");
+      meta_backend_set_numlock (meta_get_backend (), numlock_state);
+    }
 
-  numlock_state = g_settings_get_boolean (priv->keyboard_settings, "numlock-state");
-  meta_backend_set_numlock (meta_get_backend (), numlock_state);
+  g_mutex_unlock (&priv->lock);
 }
 
 void
@@ -1880,12 +1920,18 @@ meta_input_settings_set_device_matrix (MetaInputSettings  *input_settings,
                                        ClutterInputDevice *device,
                                        float               matrix[6])
 {
+  MetaInputSettingsPrivate *priv;
+
+  priv = meta_input_settings_get_instance_private (input_settings);
+  g_mutex_lock (&priv->lock);
+
   g_return_if_fail (META_IS_INPUT_SETTINGS (input_settings));
   g_return_if_fail (CLUTTER_IS_INPUT_DEVICE (device));
 
   META_INPUT_SETTINGS_GET_CLASS (input_settings)->set_matrix (input_settings,
                                                               device,
                                                               matrix);
+  g_mutex_unlock (&priv->lock);
 }
 
 void
@@ -1894,10 +1940,14 @@ meta_input_settings_set_device_enabled (MetaInputSettings  *input_settings,
                                         gboolean            enabled)
 {
   GDesktopDeviceSendEvents mode;
+  MetaInputSettingsPrivate *priv;
 
   g_return_if_fail (META_IS_INPUT_SETTINGS (input_settings));
   g_return_if_fail (CLUTTER_IS_INPUT_DEVICE (device));
 
+  priv = meta_input_settings_get_instance_private (input_settings);
+  g_mutex_lock (&priv->lock);
+
   mode = enabled ?
     G_DESKTOP_DEVICE_SEND_EVENTS_ENABLED :
     G_DESKTOP_DEVICE_SEND_EVENTS_DISABLED;
@@ -1905,6 +1955,7 @@ meta_input_settings_set_device_enabled (MetaInputSettings  *input_settings,
   META_INPUT_SETTINGS_GET_CLASS (input_settings)->set_send_events (input_settings,
                                                                    device,
                                                                    mode);
+  g_mutex_unlock (&priv->lock);
 }
 
 void
@@ -1919,13 +1970,16 @@ meta_input_settings_set_device_aspect_ratio (MetaInputSettings  *input_settings,
   g_return_if_fail (CLUTTER_IS_INPUT_DEVICE (device));
 
   priv = meta_input_settings_get_instance_private (input_settings);
+  g_mutex_lock (&priv->lock);
 
   info = g_hash_table_lookup (priv->mappable_devices, device);
-  if (!info)
-    return;
+  if (info)
+    {
+      info->aspect_ratio = aspect_ratio;
+      update_tablet_keep_aspect (input_settings, info->settings, device);
+    }
 
-  info->aspect_ratio = aspect_ratio;
-  update_tablet_keep_aspect (input_settings, info->settings, device);
+  g_mutex_unlock (&priv->lock);
 }
 
 void
@@ -1938,5 +1992,7 @@ meta_input_settings_get_kbd_a11y_settings (MetaInputSettings   *input_settings,
 
   priv = meta_input_settings_get_instance_private (input_settings);
 
+  g_mutex_lock (&priv->lock);
   *a11y_settings = priv->kbd_a11y_settings;
+  g_mutex_unlock (&priv->lock);
 }


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