Commit 261f1dc3 authored by John Ralls's avatar John Ralls

Switch quartz monitors collection from hash table to ptr array.

It's easier to find in a ptr array than to iterate over a hash table.
parent 3bd9a345
...@@ -342,13 +342,22 @@ display_rect (GdkQuartzDisplay *display) ...@@ -342,13 +342,22 @@ display_rect (GdkQuartzDisplay *display)
display->size = NSMakeSize (max_x_mm - min_x_mm, max_y_mm - min_y_mm); display->size = NSMakeSize (max_x_mm - min_x_mm, max_y_mm - min_y_mm);
} }
static gboolean
same_monitor (gconstpointer a, gconstpointer b)
{
GdkQuartzMonitor *mon_a = GDK_QUARTZ_MONITOR (a);
CGDirectDisplayID disp_id = (CGDirectDisplayID)GPOINTER_TO_INT (b);
if (!mon_a)
return FALSE;
return mon_a->id == disp_id;
}
static void static void
display_reconfiguration_callback (CGDirectDisplayID cg_display, display_reconfiguration_callback (CGDirectDisplayID cg_display,
CGDisplayChangeSummaryFlags flags, CGDisplayChangeSummaryFlags flags,
void *data) void *data)
{ {
GdkQuartzDisplay *display = data; GdkQuartzDisplay *display = data;
GdkQuartzMonitor *monitor;
/* Ignore the begin configuration signal. */ /* Ignore the begin configuration signal. */
if (flags & kCGDisplayBeginConfigurationFlag) if (flags & kCGDisplayBeginConfigurationFlag)
...@@ -358,28 +367,44 @@ display_reconfiguration_callback (CGDirectDisplayID cg_display, ...@@ -358,28 +367,44 @@ display_reconfiguration_callback (CGDirectDisplayID cg_display,
kCGDisplaySetMainFlag | kCGDisplayDesktopShapeChangedFlag | kCGDisplaySetMainFlag | kCGDisplayDesktopShapeChangedFlag |
kCGDisplayMirrorFlag | kCGDisplayUnMirrorFlag)) kCGDisplayMirrorFlag | kCGDisplayUnMirrorFlag))
{ {
monitor = g_hash_table_lookup (display->monitors, GdkQuartzMonitor *monitor = NULL;
GINT_TO_POINTER (cg_display)); guint index;
if (!monitor)
if (!g_ptr_array_find_with_equal_func (display->monitors,
GINT_TO_POINTER (cg_display),
same_monitor,
&index))
{ {
monitor = g_object_new (GDK_TYPE_QUARTZ_MONITOR, monitor = g_object_new (GDK_TYPE_QUARTZ_MONITOR,
"display", display, NULL); "display", display, NULL);
monitor->id = cg_display; monitor->id = cg_display;
g_hash_table_insert (display->monitors, GINT_TO_POINTER (monitor->id), g_ptr_array_add (display->monitors, monitor);
monitor);
gdk_display_monitor_added (GDK_DISPLAY (display), gdk_display_monitor_added (GDK_DISPLAY (display),
GDK_MONITOR (monitor)); GDK_MONITOR (monitor));
} }
else
{
monitor = g_ptr_array_index (display->monitors, index);
}
display_rect (display); display_rect (display);
configure_monitor (GDK_MONITOR (monitor), display); configure_monitor (GDK_MONITOR (monitor), display);
} }
else if (flags & (kCGDisplayRemoveFlag | kCGDisplayDisabledFlag)) else if (flags & (kCGDisplayRemoveFlag | kCGDisplayDisabledFlag))
{ {
GdkMonitor *monitor = g_hash_table_lookup (display->monitors, guint index;
GINT_TO_POINTER (cg_display));
gdk_display_monitor_removed (GDK_DISPLAY (display), if (g_ptr_array_find_with_equal_func (display->monitors,
GDK_MONITOR (monitor)); GINT_TO_POINTER (cg_display),
g_hash_table_remove (display->monitors, GINT_TO_POINTER (cg_display)); same_monitor,
&index))
{
GdkQuartzMonitor *monitor = g_ptr_array_index (display->monitors,
index);
gdk_display_monitor_removed (GDK_DISPLAY (display),
GDK_MONITOR (monitor));
g_ptr_array_remove_fast (display->monitors, monitor);
}
} }
g_signal_emit (display, MONITORS_CHANGED, 0); g_signal_emit (display, MONITORS_CHANGED, 0);
...@@ -389,7 +414,8 @@ display_reconfiguration_callback (CGDirectDisplayID cg_display, ...@@ -389,7 +414,8 @@ display_reconfiguration_callback (CGDirectDisplayID cg_display,
static int static int
gdk_quartz_display_get_n_monitors (GdkDisplay *display) gdk_quartz_display_get_n_monitors (GdkDisplay *display)
{ {
return get_active_displays (NULL); GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display);
return quartz_display->monitors->len;
} }
static GdkMonitor * static GdkMonitor *
...@@ -397,16 +423,12 @@ gdk_quartz_display_get_monitor (GdkDisplay *display, ...@@ -397,16 +423,12 @@ gdk_quartz_display_get_monitor (GdkDisplay *display,
int monitor_num) int monitor_num)
{ {
GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display); GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display);
CGDirectDisplayID *screens = NULL; int count = gdk_quartz_display_get_n_monitors (display);
int count = get_active_displays (&screens);
GdkMonitor *monitor = NULL;
if (monitor_num >= 0 && monitor_num < count) if (monitor_num >= 0 && monitor_num < count)
monitor = g_hash_table_lookup (quartz_display->monitors, return g_ptr_array_index (quartz_display->monitors, monitor_num);
GINT_TO_POINTER (screens[monitor_num]));
g_free (screens); return NULL;
return monitor;
} }
static GdkMonitor * static GdkMonitor *
...@@ -414,9 +436,15 @@ gdk_quartz_display_get_primary_monitor (GdkDisplay *display) ...@@ -414,9 +436,15 @@ gdk_quartz_display_get_primary_monitor (GdkDisplay *display)
{ {
GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display); GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display);
CGDirectDisplayID primary_id = CGMainDisplayID (); CGDirectDisplayID primary_id = CGMainDisplayID ();
GdkMonitor *monitor = NULL;
guint index;
return g_hash_table_lookup (quartz_display->monitors, if (g_ptr_array_find_with_equal_func (quartz_display->monitors,
GINT_TO_POINTER (primary_id)); GINT_TO_POINTER (primary_id),
same_monitor, &index))
monitor = g_ptr_array_index (quartz_display->monitors, index);
return monitor;
} }
static GdkMonitor * static GdkMonitor *
...@@ -426,18 +454,27 @@ gdk_quartz_display_get_monitor_at_window (GdkDisplay *display, ...@@ -426,18 +454,27 @@ gdk_quartz_display_get_monitor_at_window (GdkDisplay *display,
GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl); GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl);
NSWindow *nswindow = impl->toplevel; NSWindow *nswindow = impl->toplevel;
NSScreen *screen = [nswindow screen]; NSScreen *screen = [nswindow screen];
GdkMonitor *monitor = NULL;
if (screen) if (screen)
{ {
CGDirectDisplayID disp_id = GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display);
[[[screen deviceDescription] guint index;
objectForKey: @"NSScreenNumber"] unsignedIntValue]; CGDirectDisplayID disp_id =
return g_hash_table_lookup (GDK_QUARTZ_DISPLAY (display)->monitors, [[[screen deviceDescription]
GINT_TO_POINTER (disp_id)); objectForKey: @"NSScreenNumber"] unsignedIntValue];
if (g_ptr_array_find_with_equal_func (quartz_display->monitors,
GINT_TO_POINTER (disp_id),
same_monitor, &index))
monitor = g_ptr_array_index (quartz_display->monitors, index);
} }
GdkRectangle rect = cgrect_to_gdkrect ([nswindow frame]); if (!monitor)
return gdk_display_get_monitor_at_point (display, {
rect.x + rect.width/2, GdkRectangle rect = cgrect_to_gdkrect ([nswindow frame]);
rect.y + rect.height /2); monitor = gdk_display_get_monitor_at_point (display,
rect.x + rect.width/2,
rect.y + rect.height /2);
}
return monitor;
} }
G_DEFINE_TYPE (GdkQuartzDisplay, gdk_quartz_display, GDK_TYPE_DISPLAY) G_DEFINE_TYPE (GdkQuartzDisplay, gdk_quartz_display, GDK_TYPE_DISPLAY)
...@@ -450,15 +487,13 @@ gdk_quartz_display_init (GdkQuartzDisplay *display) ...@@ -450,15 +487,13 @@ gdk_quartz_display_init (GdkQuartzDisplay *display)
display_rect(display); /* Initialize the overall display coordinates. */ display_rect(display); /* Initialize the overall display coordinates. */
count = get_active_displays (&displays); count = get_active_displays (&displays);
display->monitors = g_hash_table_new_full (g_direct_hash, NULL, display->monitors = g_ptr_array_new_full (max_displays, g_object_unref);
NULL, g_object_unref);
for (disp = 0; disp < count; ++disp) for (disp = 0; disp < count; ++disp)
{ {
GdkQuartzMonitor *monitor = g_object_new (GDK_TYPE_QUARTZ_MONITOR, GdkQuartzMonitor *monitor = g_object_new (GDK_TYPE_QUARTZ_MONITOR,
"display", display, NULL); "display", display, NULL);
monitor->id = displays[disp]; monitor->id = displays[disp];
g_hash_table_insert (display->monitors, GINT_TO_POINTER (monitor->id), g_ptr_array_add (display->monitors, monitor);
monitor);
configure_monitor (GDK_MONITOR (monitor), display); configure_monitor (GDK_MONITOR (monitor), display);
} }
g_free (displays); g_free (displays);
...@@ -471,11 +506,11 @@ gdk_quartz_display_init (GdkQuartzDisplay *display) ...@@ -471,11 +506,11 @@ gdk_quartz_display_init (GdkQuartzDisplay *display)
static void static void
gdk_quartz_display_dispose (GObject *object) gdk_quartz_display_dispose (GObject *object)
{ {
GdkQuartzDisplay *display_quartz = GDK_QUARTZ_DISPLAY (object); GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (object);
g_hash_table_destroy (display_quartz->monitors); g_ptr_array_free (quartz_display->monitors, TRUE);
CGDisplayRemoveReconfigurationCallback (display_reconfiguration_callback, CGDisplayRemoveReconfigurationCallback (display_reconfiguration_callback,
display_quartz); quartz_display);
G_OBJECT_CLASS (gdk_quartz_display_parent_class)->dispose (object); G_OBJECT_CLASS (gdk_quartz_display_parent_class)->dispose (object);
} }
......
...@@ -34,7 +34,7 @@ struct _GdkQuartzDisplay ...@@ -34,7 +34,7 @@ struct _GdkQuartzDisplay
GdkDisplay parent_instance; GdkDisplay parent_instance;
NSRect geometry; /* In AppKit coordinates. */ NSRect geometry; /* In AppKit coordinates. */
NSSize size; /* Aggregate size of displays in millimeters. */ NSSize size; /* Aggregate size of displays in millimeters. */
GHashTable *monitors; GPtrArray *monitors;
}; };
struct _GdkQuartzDisplayClass struct _GdkQuartzDisplayClass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment