[gtk+/osx-monitor] quartz: Add monitor support



commit 9159e70473d576053ec5d4fd37cf9567d3d0aac5
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Mar 10 08:48:36 2017 -0500

    quartz: Add monitor support
    
    This is a backport of a patch by Tom Schoonjans,
    
    https://bugzilla.gnome.org/show_bug.cgi?id=779184

 gdk/quartz/Makefile.am         |    4 +
 gdk/quartz/gdkdisplay-quartz.c |   51 ++++++++--
 gdk/quartz/gdkmonitor-quartz.c |   58 +++++++++++
 gdk/quartz/gdkmonitor-quartz.h |   41 ++++++++
 gdk/quartz/gdkprivate-quartz.h |   11 --
 gdk/quartz/gdkquartz.h         |    1 +
 gdk/quartz/gdkscreen-quartz.c  |  204 ++++++++++------------------------------
 gdk/quartz/gdkscreen-quartz.h  |    3 -
 8 files changed, 196 insertions(+), 177 deletions(-)
---
diff --git a/gdk/quartz/Makefile.am b/gdk/quartz/Makefile.am
index 18fdbd3..3ba0393 100644
--- a/gdk/quartz/Makefile.am
+++ b/gdk/quartz/Makefile.am
@@ -29,6 +29,7 @@ libgdk_quartz_la_SOURCES =            \
        gdkdevicemanager-core-quartz.c  \
        gdkdevicemanager-core-quartz.h  \
        gdkdisplay-quartz.c     \
+       gdkdisplay-quartz.h     \
        gdkdisplaymanager-quartz.c      \
        gdkdnd-quartz.c         \
        gdkdnd-quartz.h         \
@@ -38,6 +39,8 @@ libgdk_quartz_la_SOURCES =            \
        gdkglcontext-quartz.h   \
        gdkglobals-quartz.c     \
        gdkkeys-quartz.c        \
+       gdkmonitor-quartz.c     \
+       gdkmonitor-quartz.h     \
        gdkprivate-quartz.h     \
        gdkproperty-quartz.c    \
        gdkquartz.h             \
@@ -62,6 +65,7 @@ libgdkquartzinclude_HEADERS =         \
        gdkquartzdisplaymanager.h       \
        gdkquartzdnd.h                  \
        gdkquartzkeys.h                 \
+       gdkquartzmonitor.h              \
        gdkquartzscreen.h               \
        gdkquartzutils.h                \
        gdkquartzvisual.h               \
diff --git a/gdk/quartz/gdkdisplay-quartz.c b/gdk/quartz/gdkdisplay-quartz.c
index 2bea570..b309908 100644
--- a/gdk/quartz/gdkdisplay-quartz.c
+++ b/gdk/quartz/gdkdisplay-quartz.c
@@ -26,18 +26,11 @@
 #include "gdkquartzwindow.h"
 #include "gdkquartzdisplay.h"
 #include "gdkquartzdevicemanager-core.h"
+#include "gdkscreen.h"
+#include "gdkmonitorprivate.h"
+#include "gdkdisplay-quartz.h"
 
 
-struct _GdkQuartzDisplay
-{
-  GdkDisplay display;
-};
-
-struct _GdkQuartzDisplayClass
-{
-  GdkDisplayClass display_class;
-};
-
 static GdkWindow *
 gdk_quartz_display_get_default_group (GdkDisplay *display)
 {
@@ -196,12 +189,45 @@ gdk_quartz_display_notify_startup_complete (GdkDisplay  *display,
   /* FIXME: Implement? */
 }
 
+static int
+gdk_quartz_display_get_n_monitors (GdkDisplay *display)
+{
+  GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display);
+
+  return quartz_display->monitors->len;
+}
+
+
+static GdkMonitor *
+gdk_quartz_display_get_monitor (GdkDisplay *display,
+                                int         monitor_num)
+{
+  GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display);
+
+  if (0 <= monitor_num || monitor_num < quartz_display->monitors->len)
+    return (GdkMonitor *)quartz_display->monitors->pdata[monitor_num];
+
+  return NULL;
+}
+
+static GdkMonitor *
+gdk_quartz_display_get_primary_monitor (GdkDisplay *display)
+{
+  GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display);
+
+  return quartz_display->monitors->pdata[0];
+}
 
 G_DEFINE_TYPE (GdkQuartzDisplay, gdk_quartz_display, GDK_TYPE_DISPLAY)
 
 static void
 gdk_quartz_display_init (GdkQuartzDisplay *display)
 {
+  GDK_QUARTZ_ALLOC_POOL;
+
+  display->monitors = g_ptr_array_new_with_free_func (g_object_unref);
+
+  GDK_QUARTZ_RELEASE_POOL;
 }
 
 static void
@@ -209,6 +235,8 @@ gdk_quartz_display_dispose (GObject *object)
 {
   GdkQuartzDisplay *display_quartz = GDK_QUARTZ_DISPLAY (object);
 
+  g_ptr_array_free (display_quartz->monitors, TRUE);
+
   G_OBJECT_CLASS (gdk_quartz_display_parent_class)->dispose (object);
 }
 
@@ -268,6 +296,9 @@ gdk_quartz_display_class_init (GdkQuartzDisplayClass *class)
   display_class->convert_selection = _gdk_quartz_display_convert_selection;
   display_class->text_property_to_utf8_list = _gdk_quartz_display_text_property_to_utf8_list;
   display_class->utf8_to_string_target = _gdk_quartz_display_utf8_to_string_target;
+  display_class->get_n_monitors = gdk_quartz_display_get_n_monitors;
+  display_class->get_monitor = gdk_quartz_display_get_monitor;
+  display_class->get_primary_monitor = gdk_quartz_display_get_primary_monitor;
 
   ProcessSerialNumber psn = { 0, kCurrentProcess };
 
diff --git a/gdk/quartz/gdkmonitor-quartz.c b/gdk/quartz/gdkmonitor-quartz.c
new file mode 100644
index 0000000..ec36b23
--- /dev/null
+++ b/gdk/quartz/gdkmonitor-quartz.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright © 2017 Tom Schoonjans
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include "gdkmonitor-quartz.h"
+#include "gdkscreen-quartz.h"
+
+
+G_DEFINE_TYPE (GdkQuartzMonitor, gdk_quartz_monitor, GDK_TYPE_MONITOR)
+
+static void
+gdk_quartz_monitor_get_workarea (GdkMonitor   *monitor,
+                                 GdkRectangle *dest)
+{
+  GdkQuartzScreen *quartz_screen = GDK_QUARTZ_SCREEN(gdk_display_get_default_screen (monitor->display));
+  GdkQuartzMonitor *quartz_monitor = GDK_QUARTZ_MONITOR(monitor);
+
+  GDK_QUARTZ_ALLOC_POOL;
+
+  NSRect rect = [quartz_monitor->nsscreen visibleFrame];
+
+  dest->x = rect.origin.x - quartz_screen->min_x;
+  dest->y = quartz_screen->height - (rect.origin.y + rect.size.height) + quartz_screen->min_y;
+  dest->width = rect.size.width;
+  dest->height = rect.size.height;
+
+  GDK_QUARTZ_RELEASE_POOL;
+}
+
+static void
+gdk_quartz_monitor_init (GdkQuartzMonitor *monitor)
+{
+}
+
+static void
+gdk_quartz_monitor_class_init (GdkQuartzMonitorClass *class)
+{
+  GDK_MONITOR_CLASS (class)->get_workarea = gdk_quartz_monitor_get_workarea;
+}
+
diff --git a/gdk/quartz/gdkmonitor-quartz.h b/gdk/quartz/gdkmonitor-quartz.h
new file mode 100644
index 0000000..448c483
--- /dev/null
+++ b/gdk/quartz/gdkmonitor-quartz.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright © 2017 Tom Schoonjans
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GDK_QUARTZ_MONITOR_PRIVATE_H__
+#define __GDK_QUARTZ_MONITOR_PRIVATE_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include "gdkmonitorprivate.h"
+
+#include "gdkquartzmonitor.h"
+#include "gdkprivate-quartz.h"
+
+struct _GdkQuartzMonitor
+{
+  GdkMonitor parent;
+
+  NSScreen *nsscreen;
+};
+
+struct _GdkQuartzMonitorClass {
+  GdkMonitorClass parent_class;
+};
+
+#endif
+
diff --git a/gdk/quartz/gdkprivate-quartz.h b/gdk/quartz/gdkprivate-quartz.h
index 1a7410c..8fca430 100644
--- a/gdk/quartz/gdkprivate-quartz.h
+++ b/gdk/quartz/gdkprivate-quartz.h
@@ -116,17 +116,6 @@ void       _gdk_quartz_display_get_maximal_cursor_size (GdkDisplay *display,
                                                         guint      *width,
                                                         guint      *height);
 
-/* Display methods - window */
-void       _gdk_quartz_display_before_process_all_updates (GdkDisplay *display);
-void       _gdk_quartz_display_after_process_all_updates  (GdkDisplay *display);
-void       _gdk_quartz_display_create_window_impl (GdkDisplay    *display,
-                                                   GdkWindow     *window,
-                                                   GdkWindow     *real_parent,
-                                                   GdkScreen     *screen,
-                                                   GdkEventMask   event_mask,
-                                                   GdkWindowAttr *attributes,
-                                                   gint           attributes_mask);
-
 /* Display methods - keymap */
 GdkKeymap * _gdk_quartz_display_get_keymap (GdkDisplay *display);
 
diff --git a/gdk/quartz/gdkquartz.h b/gdk/quartz/gdkquartz.h
index e933708..b07b02b 100644
--- a/gdk/quartz/gdkquartz.h
+++ b/gdk/quartz/gdkquartz.h
@@ -71,6 +71,7 @@ G_END_DECLS
 #include <gdk/quartz/gdkquartzdisplaymanager.h>
 #include <gdk/quartz/gdkquartzdnd.h>
 #include <gdk/quartz/gdkquartzkeys.h>
+#include <gdk/quartz/gdkquartzmonitor.h>
 #include <gdk/quartz/gdkquartzscreen.h>
 #include <gdk/quartz/gdkquartzutils.h>
 #include <gdk/quartz/gdkquartzvisual.h>
diff --git a/gdk/quartz/gdkscreen-quartz.c b/gdk/quartz/gdkscreen-quartz.c
index 6f57627..b7ee27c 100644
--- a/gdk/quartz/gdkscreen-quartz.c
+++ b/gdk/quartz/gdkscreen-quartz.c
@@ -22,6 +22,8 @@
 #include <gdk/gdk.h>
 
 #include "gdkprivate-quartz.h"
+#include "gdkdisplay-quartz.h"
+#include "gdkmonitor-quartz.h"
  
 
 /* A couple of notes about this file are in order.  In GDK, a
@@ -66,6 +68,8 @@ static void display_reconfiguration_callback (CGDirectDisplayID            displ
                                               CGDisplayChangeSummaryFlags  flags,
                                               void                        *userInfo);
 
+static gint get_mm_from_pixels (NSScreen *screen, int pixels);
+
 G_DEFINE_TYPE (GdkQuartzScreen, gdk_quartz_screen, GDK_TYPE_SCREEN);
 
 static void
@@ -104,20 +108,17 @@ gdk_quartz_screen_dispose (GObject *object)
 }
 
 static void
-gdk_quartz_screen_screen_rects_free (GdkQuartzScreen *screen)
-{
-  screen->n_screens = 0;
-  g_clear_pointer (&screen->screen_rects, g_free);
-}
-
-static void
 gdk_quartz_screen_finalize (GObject *object)
 {
   GdkQuartzScreen *screen = GDK_QUARTZ_SCREEN (object);
 
-  gdk_quartz_screen_screen_rects_free (screen);
+  G_OBJECT_CLASS (gdk_quartz_screen_parent_class)->finalize (object);
 }
 
+/* Protocol to build cleanly for OSX < 10.7 */
+@protocol ScaleFactor
+- (CGFloat) backingScaleFactor;
+@end
 
 static void
 gdk_quartz_screen_calculate_layout (GdkQuartzScreen *screen)
@@ -125,10 +126,13 @@ gdk_quartz_screen_calculate_layout (GdkQuartzScreen *screen)
   NSArray *array;
   int i;
   int max_x, max_y;
+  GdkDisplay *display = gdk_screen_get_display (GDK_SCREEN (screen));
+  GdkQuartzDisplay *display_quartz = GDK_QUARTZ_DISPLAY (display);
 
-  GDK_QUARTZ_ALLOC_POOL;
+  g_ptr_array_free (display_quartz->monitors, TRUE);
+  display_quartz->monitors = g_ptr_array_new_with_free_func (g_object_unref);
 
-  gdk_quartz_screen_screen_rects_free (screen);
+  GDK_QUARTZ_ALLOC_POOL;
 
   array = [NSScreen screens];
 
@@ -144,6 +148,12 @@ gdk_quartz_screen_calculate_layout (GdkQuartzScreen *screen)
    */
   for (i = 0; i < [array count]; i++)
     {
+      GdkQuartzMonitor *monitor = g_object_new (GDK_TYPE_QUARTZ_MONITOR,
+                                                "display", display,
+                                                NULL);
+      g_ptr_array_add (display_quartz->monitors, monitor);
+      monitor->nsscreen = [array objectAtIndex:i];
+
       NSRect rect = [[array objectAtIndex:i] frame];
 
       screen->min_x = MIN (screen->min_x, rect.origin.x);
@@ -156,22 +166,31 @@ gdk_quartz_screen_calculate_layout (GdkQuartzScreen *screen)
   screen->width = max_x - screen->min_x;
   screen->height = max_y - screen->min_y;
 
-  screen->n_screens = [array count];
-  screen->screen_rects = g_new0 (GdkRectangle, screen->n_screens);
-
-  for (i = 0; i < screen->n_screens; i++)
+  for (i = 0; i < [array count] ; i++)
     {
       NSScreen *nsscreen;
       NSRect rect;
+      GdkMonitor *monitor;
 
+      monitor = GDK_MONITOR(display_quartz->monitors->pdata[i]);
       nsscreen = [array objectAtIndex:i];
       rect = [nsscreen frame];
 
-      screen->screen_rects[i].x = rect.origin.x - screen->min_x;
-      screen->screen_rects[i].y
+      monitor->geometry.x = rect.origin.x - screen->min_x;
+      monitor->geometry.y
           = screen->height - (rect.origin.y + rect.size.height) + screen->min_y;
-      screen->screen_rects[i].width = rect.size.width;
-      screen->screen_rects[i].height = rect.size.height;
+      monitor->geometry.width = rect.size.width;
+      monitor->geometry.height = rect.size.height;
+      if (gdk_quartz_osx_version() >= GDK_OSX_LION)
+        monitor->scale_factor = [(id <ScaleFactor>) nsscreen backingScaleFactor];
+      else
+        monitor->scale_factor = 1;
+      monitor->width_mm = get_mm_from_pixels(nsscreen, monitor->geometry.width);
+      monitor->height_mm = get_mm_from_pixels(nsscreen, monitor->geometry.height);
+      monitor->refresh_rate = 0; // unknown
+      monitor->manufacturer = NULL; // unknown
+      monitor->model = NULL; // unknown
+      monitor->subpixel_layout = GDK_SUBPIXEL_LAYOUT_UNKNOWN; // unknown
     }
 
   GDK_QUARTZ_RELEASE_POOL;
@@ -331,133 +350,6 @@ get_mm_from_pixels (NSScreen *screen, int pixels)
   return (pixels / dpi) * 25.4;
 }
 
-static NSScreen *
-get_nsscreen_for_monitor (gint monitor_num)
-{
-  NSArray *array;
-  NSScreen *screen;
-
-  GDK_QUARTZ_ALLOC_POOL;
-
-  array = [NSScreen screens];
-  screen = [array objectAtIndex:monitor_num];
-
-  GDK_QUARTZ_RELEASE_POOL;
-
-  return screen;
-}
-
-static gint
-gdk_quartz_screen_get_width_mm (GdkScreen *screen)
-{
-  return get_mm_from_pixels (get_nsscreen_for_monitor (0),
-                             GDK_QUARTZ_SCREEN (screen)->width);
-}
-
-static gint
-gdk_quartz_screen_get_height_mm (GdkScreen *screen)
-{
-  return get_mm_from_pixels (get_nsscreen_for_monitor (0),
-                             GDK_QUARTZ_SCREEN (screen)->height);
-}
-
-static gint
-gdk_quartz_screen_get_n_monitors (GdkScreen *screen)
-{
-  return GDK_QUARTZ_SCREEN (screen)->n_screens;
-}
-
-static gint
-gdk_quartz_screen_get_primary_monitor (GdkScreen *screen)
-{
-  return 0;
-}
-
-static gint
-gdk_quartz_screen_get_monitor_width_mm (GdkScreen *screen,
-                                        gint       monitor_num)
-{
-  return get_mm_from_pixels (get_nsscreen_for_monitor (monitor_num),
-                             GDK_QUARTZ_SCREEN (screen)->screen_rects[monitor_num].width);
-}
-
-static gint
-gdk_quartz_screen_get_monitor_height_mm (GdkScreen *screen,
-                                         gint       monitor_num)
-{
-  return get_mm_from_pixels (get_nsscreen_for_monitor (monitor_num),
-                             GDK_QUARTZ_SCREEN (screen)->screen_rects[monitor_num].height);
-}
-
-static gchar *
-gdk_quartz_screen_get_monitor_plug_name (GdkScreen *screen,
-                                         gint       monitor_num)
-{
-  /* FIXME: Is there some useful name we could use here? */
-  return NULL;
-}
-
-static void
-gdk_quartz_screen_get_monitor_geometry (GdkScreen    *screen,
-                                        gint          monitor_num,
-                                        GdkRectangle *dest)
-{
-  *dest = GDK_QUARTZ_SCREEN (screen)->screen_rects[monitor_num];
-}
-
-static void
-gdk_quartz_screen_get_monitor_workarea (GdkScreen    *screen,
-                                        gint          monitor_num,
-                                        GdkRectangle *dest)
-{
-  GdkQuartzScreen *quartz_screen = GDK_QUARTZ_SCREEN (screen);
-  NSArray *array;
-  NSScreen *nsscreen;
-  NSRect rect;
-
-  GDK_QUARTZ_ALLOC_POOL;
-
-  array = [NSScreen screens];
-  nsscreen = [array objectAtIndex:monitor_num];
-  rect = [nsscreen visibleFrame];
-
-  dest->x = rect.origin.x - quartz_screen->min_x;
-  dest->y = quartz_screen->height - (rect.origin.y + rect.size.height) + quartz_screen->min_y;
-  dest->width = rect.size.width;
-  dest->height = rect.size.height;
-
-  GDK_QUARTZ_RELEASE_POOL;
-}
-
-/* Protocol to build cleanly for OSX < 10.7 */
-@protocol ScaleFactor
-- (CGFloat) backingScaleFactor;
-@end
-
-gint
-_gdk_quartz_screen_get_monitor_scale_factor (GdkScreen *screen,
-                                             gint       monitor_num)
-{
-  GdkQuartzScreen *quartz_screen;
-  NSArray *array;
-  NSScreen *nsscreen;
-  gint scale_factor = 1;
-
-  quartz_screen = GDK_QUARTZ_SCREEN (screen);
-
-  GDK_QUARTZ_ALLOC_POOL;
-
-  array = [NSScreen screens];
-  nsscreen = [array objectAtIndex:monitor_num];
-
-  if (gdk_quartz_osx_version() >= GDK_OSX_LION)
-    scale_factor = [(id <ScaleFactor>) nsscreen backingScaleFactor];
-
-  GDK_QUARTZ_RELEASE_POOL;
-
-  return scale_factor;
-}
-
 static gchar *
 gdk_quartz_screen_make_display_name (GdkScreen *screen)
 {
@@ -482,6 +374,20 @@ gdk_quartz_screen_is_composited (GdkScreen *screen)
   return TRUE;
 }
 
+static gint
+gdk_quartz_screen_get_width_mm (GdkScreen *screen)
+{
+  return get_mm_from_pixels (get_nsscreen_for_monitor (0),
+                             GDK_QUARTZ_SCREEN (screen)->width);
+}
+
+static gint
+gdk_quartz_screen_get_height_mm (GdkScreen *screen)
+{
+  return get_mm_from_pixels (get_nsscreen_for_monitor (0),
+                             GDK_QUARTZ_SCREEN (screen)->height);
+}
+
 static void
 gdk_quartz_screen_class_init (GdkQuartzScreenClass *klass)
 {
@@ -498,13 +404,6 @@ gdk_quartz_screen_class_init (GdkQuartzScreenClass *klass)
   screen_class->get_height_mm = gdk_quartz_screen_get_height_mm;
   screen_class->get_number = gdk_quartz_screen_get_number;
   screen_class->get_root_window = gdk_quartz_screen_get_root_window;
-  screen_class->get_n_monitors = gdk_quartz_screen_get_n_monitors;
-  screen_class->get_primary_monitor = gdk_quartz_screen_get_primary_monitor;
-  screen_class->get_monitor_width_mm = gdk_quartz_screen_get_monitor_width_mm;
-  screen_class->get_monitor_height_mm = gdk_quartz_screen_get_monitor_height_mm;
-  screen_class->get_monitor_plug_name = gdk_quartz_screen_get_monitor_plug_name;
-  screen_class->get_monitor_geometry = gdk_quartz_screen_get_monitor_geometry;
-  screen_class->get_monitor_workarea = gdk_quartz_screen_get_monitor_workarea;
   screen_class->is_composited = gdk_quartz_screen_is_composited;
   screen_class->make_display_name = gdk_quartz_screen_make_display_name;
   screen_class->get_active_window = gdk_quartz_screen_get_active_window;
@@ -522,5 +421,4 @@ gdk_quartz_screen_class_init (GdkQuartzScreenClass *klass)
   screen_class->query_depths = _gdk_quartz_screen_query_depths;
   screen_class->query_visual_types = _gdk_quartz_screen_query_visual_types;
   screen_class->list_visuals = _gdk_quartz_screen_list_visuals;
-  screen_class->get_monitor_scale_factor = _gdk_quartz_screen_get_monitor_scale_factor;
 }
diff --git a/gdk/quartz/gdkscreen-quartz.h b/gdk/quartz/gdkscreen-quartz.h
index fb4e4b2..5a1cfa3 100644
--- a/gdk/quartz/gdkscreen-quartz.h
+++ b/gdk/quartz/gdkscreen-quartz.h
@@ -36,9 +36,6 @@ struct _GdkQuartzScreen
   gint width;
   gint height;
 
-  int n_screens;
-  GdkRectangle *screen_rects;
-
   guint screen_changed_id;
 
   guint emit_monitors_changed : 1;


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