From c9360566638ce22ec584a4468fa1ea93587c815d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Mon, 28 Nov 2016 20:43:27 +0800 Subject: [PATCH] MetaMonitorManager: Track primary monitor by pointer instead of index Instead of keeping an index in an array to keep track of the current primary logical monitor, just keep a pointer instead. https://bugzilla.gnome.org/show_bug.cgi?id=777732 --- src/backends/meta-backend.c | 13 ++++-------- src/backends/meta-monitor-manager-private.h | 6 +++--- src/backends/meta-monitor-manager.c | 23 ++++++++++++++------- src/core/screen.c | 8 ++++++- src/core/window.c | 11 +--------- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/backends/meta-backend.c b/src/backends/meta-backend.c index c17c123c8..ac6bd308f 100644 --- a/src/backends/meta-backend.c +++ b/src/backends/meta-backend.c @@ -115,16 +115,11 @@ center_pointer (MetaBackend *backend) { MetaBackendPrivate *priv = meta_backend_get_instance_private (backend); MetaMonitorManager *monitor_manager = priv->monitor_manager; - MetaLogicalMonitor *logical_monitors, *primary; - unsigned int n_logical_monitors; - int primary_monitor_index; + MetaLogicalMonitor *primary; + + primary = + meta_monitor_manager_get_primary_logical_monitor (monitor_manager); - logical_monitors = - meta_monitor_manager_get_logical_monitors (monitor_manager, - &n_logical_monitors); - primary_monitor_index = - meta_monitor_manager_get_primary_index (monitor_manager); - primary = &logical_monitors[primary_monitor_index]; meta_backend_warp_pointer (backend, primary->rect.x + primary->rect.width / 2, primary->rect.y + primary->rect.height / 2); diff --git a/src/backends/meta-monitor-manager-private.h b/src/backends/meta-monitor-manager-private.h index 7a35fa58d..f36a7a5d7 100644 --- a/src/backends/meta-monitor-manager-private.h +++ b/src/backends/meta-monitor-manager-private.h @@ -306,7 +306,7 @@ struct _MetaMonitorManager MetaLogicalMonitor *logical_monitors; unsigned int n_logical_monitors; - int primary_monitor_index; + MetaLogicalMonitor *primary_logical_monitor; int dbus_name_id; @@ -368,6 +368,8 @@ int meta_monitor_manager_get_num_logical_monitors (MetaMonitorMa MetaLogicalMonitor *meta_monitor_manager_get_logical_monitors (MetaMonitorManager *manager, unsigned int *n_logical_monitors); +MetaLogicalMonitor *meta_monitor_manager_get_primary_logical_monitor (MetaMonitorManager *manager); + MetaOutput *meta_monitor_manager_get_outputs (MetaMonitorManager *manager, unsigned int *n_outputs); @@ -379,8 +381,6 @@ void meta_monitor_manager_get_resources (MetaMonitorManager MetaOutput **outputs, unsigned int *n_outputs); -int meta_monitor_manager_get_primary_index (MetaMonitorManager *manager); - void meta_monitor_manager_get_screen_size (MetaMonitorManager *manager, int *width, int *height); diff --git a/src/backends/meta-monitor-manager.c b/src/backends/meta-monitor-manager.c index d1d8f0066..e81eb7e6e 100644 --- a/src/backends/meta-monitor-manager.c +++ b/src/backends/meta-monitor-manager.c @@ -247,6 +247,7 @@ make_logical_config (MetaMonitorManager *manager) /* Now walk the list of outputs applying extended properties (primary and presentation) */ + manager->primary_logical_monitor = NULL; for (i = 0; i < manager->n_outputs; i++) { MetaOutput *output; @@ -283,12 +284,20 @@ make_logical_config (MetaMonitorManager *manager) logical_monitor->winsys_id = output->winsys_id; if (logical_monitor->is_primary) - manager->primary_monitor_index = logical_monitor->number; + manager->primary_logical_monitor = logical_monitor; } manager->n_logical_monitors = logical_monitors->len; manager->logical_monitors = (void*)g_array_free (logical_monitors, FALSE); + /* + * If no monitor was marked as primary, fall back on marking the first + * logical monitor the primary one. + */ + if (!manager->primary_logical_monitor && + manager->n_logical_monitors > 0) + manager->primary_logical_monitor = &manager->logical_monitors[0]; + if (manager_class->add_monitor) for (i = 0; i < manager->n_logical_monitors; i++) manager_class->add_monitor (manager, &manager->logical_monitors[i]); @@ -1305,6 +1314,12 @@ meta_monitor_manager_get_logical_monitors (MetaMonitorManager *manager, return manager->logical_monitors; } +MetaLogicalMonitor * +meta_monitor_manager_get_primary_logical_monitor (MetaMonitorManager *manager) +{ + return manager->primary_logical_monitor; +} + MetaOutput * meta_monitor_manager_get_outputs (MetaMonitorManager *manager, unsigned int *n_outputs) @@ -1339,12 +1354,6 @@ meta_monitor_manager_get_resources (MetaMonitorManager *manager, } } -int -meta_monitor_manager_get_primary_index (MetaMonitorManager *manager) -{ - return manager->primary_monitor_index; -} - void meta_monitor_manager_get_screen_size (MetaMonitorManager *manager, int *width, diff --git a/src/core/screen.c b/src/core/screen.c index 6ba1f10d5..1023fd7be 100644 --- a/src/core/screen.c +++ b/src/core/screen.c @@ -1732,10 +1732,16 @@ meta_screen_get_primary_monitor (MetaScreen *screen) MetaBackend *backend = meta_get_backend (); MetaMonitorManager *monitor_manager = meta_backend_get_monitor_manager (backend); + MetaLogicalMonitor *logical_monitor; g_return_val_if_fail (META_IS_SCREEN (screen), 0); - return meta_monitor_manager_get_primary_index (monitor_manager); + logical_monitor = + meta_monitor_manager_get_primary_logical_monitor (monitor_manager); + if (logical_monitor) + return logical_monitor->number; + else + return 0; } /** diff --git a/src/core/window.c b/src/core/window.c index 03a645370..501360926 100644 --- a/src/core/window.c +++ b/src/core/window.c @@ -3565,9 +3565,6 @@ meta_window_update_for_monitors_changed (MetaWindow *window) MetaBackend *backend = meta_get_backend (); MetaMonitorManager *monitor_manager = meta_backend_get_monitor_manager (backend); - MetaLogicalMonitor *logical_monitors; - unsigned int n_logical_monitors; - int primary_monitor_index; const MetaLogicalMonitor *old, *new; if (window->fullscreen_monitors[0] != -1) @@ -3588,15 +3585,9 @@ meta_window_update_for_monitors_changed (MetaWindow *window) if (!new) new = find_monitor_by_winsys_id (window, old->winsys_id); - logical_monitors = - meta_monitor_manager_get_logical_monitors (monitor_manager, - &n_logical_monitors); - primary_monitor_index = - meta_monitor_manager_get_primary_index (monitor_manager); - /* Fall back to primary if everything else failed */ if (!new) - new = &logical_monitors[primary_monitor_index]; + new = meta_monitor_manager_get_primary_logical_monitor (monitor_manager); if (window->tile_mode != META_TILE_NONE) window->tile_monitor_number = new->number;