[mutter] backends/x11: Move CRTC code to its own file



commit a4ba38ee78b7dbdcab842daddac026964d2a58bf
Author: Jonas Ådahl <jadahl gmail com>
Date:   Wed Jul 5 01:11:19 2017 +0800

    backends/x11: Move CRTC code to its own file
    
    Move code dealing with Xrandr MetaCrtcs and related functionality to its
    own file. Eventually, MetaCrtcCrtc should be introduced, based on
    MetaCrtc, and this commit is in preparation for that.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=785381

 src/Makefile.am                                |    2 +
 src/backends/x11/meta-crtc-xrandr.c            |  203 ++++++++++++++++++++++++
 src/backends/x11/meta-crtc-xrandr.h            |   46 ++++++
 src/backends/x11/meta-monitor-manager-xrandr.c |  154 +++---------------
 src/backends/x11/meta-monitor-manager-xrandr.h |    4 +
 5 files changed, 280 insertions(+), 129 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 71a846a..02d46c4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -153,6 +153,8 @@ libmutter_@LIBMUTTER_API_VERSION@_la_SOURCES =      \
        backends/x11/meta-barrier-x11.h                 \
        backends/x11/meta-clutter-backend-x11.c         \
        backends/x11/meta-clutter-backend-x11.h         \
+       backends/x11/meta-crtc-xrandr.c                 \
+       backends/x11/meta-crtc-xrandr.h                 \
        backends/x11/meta-cursor-renderer-x11.c         \
        backends/x11/meta-cursor-renderer-x11.h         \
        backends/x11/cm/meta-backend-x11-cm.c           \
diff --git a/src/backends/x11/meta-crtc-xrandr.c b/src/backends/x11/meta-crtc-xrandr.c
new file mode 100644
index 0000000..f59c9e2
--- /dev/null
+++ b/src/backends/x11/meta-crtc-xrandr.c
@@ -0,0 +1,203 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+
+/*
+ * Copyright (C) 2001, 2002 Havoc Pennington
+ * Copyright (C) 2001, 2002 Havoc Pennington
+ * Copyright (C) 2002, 2003 Red Hat Inc.
+ * Some ICCCM manager selection code derived from fvwm2,
+ * Copyright (C) 2001 Dominik Vogt, Matthias Clasen, and fvwm2 team
+ * Copyright (C) 2003 Rob Adams
+ * Copyright (C) 2004-2006 Elijah Newren
+ * Copyright (C) 2002, 2003 Red Hat Inc.
+ * Some ICCCM manager selection code derived from fvwm2,
+ * Copyright (C) 2001 Dominik Vogt, Matthias Clasen, and fvwm2 team
+ * Copyright (C) 2003 Rob Adams
+ * Copyright (C) 2004-2006 Elijah Newren
+ * Copyright (C) 2013-2017 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "backends/x11/meta-crtc-xrandr.h"
+
+#include <stdlib.h>
+#include <X11/Xlib-xcb.h>
+#include <xcb/randr.h>
+
+#include "backends/x11/meta-monitor-manager-xrandr.h"
+
+#define ALL_TRANSFORMS ((1 << (META_MONITOR_TRANSFORM_FLIPPED_270 + 1)) - 1)
+
+gboolean
+meta_crtc_xrandr_set_config (MetaCrtc            *crtc,
+                             xcb_randr_crtc_t     xrandr_crtc,
+                             xcb_timestamp_t      timestamp,
+                             int                  x,
+                             int                  y,
+                             xcb_randr_mode_t     mode,
+                             xcb_randr_rotation_t rotation,
+                             xcb_randr_output_t  *outputs,
+                             int                  n_outputs,
+                             xcb_timestamp_t     *out_timestamp)
+{
+  MetaMonitorManager *monitor_manager = meta_crtc_get_monitor_manager (crtc);
+  MetaMonitorManagerXrandr *monitor_manager_xrandr =
+    META_MONITOR_MANAGER_XRANDR (monitor_manager);
+  Display *xdisplay;
+  XRRScreenResources *resources;
+  xcb_connection_t *xcb_conn;
+  xcb_timestamp_t config_timestamp;
+  xcb_randr_set_crtc_config_cookie_t cookie;
+  xcb_randr_set_crtc_config_reply_t *reply;
+  xcb_generic_error_t *xcb_error = NULL;
+
+  xdisplay = meta_monitor_manager_xrandr_get_xdisplay (monitor_manager_xrandr);
+  xcb_conn = XGetXCBConnection (xdisplay);
+  resources =
+    meta_monitor_manager_xrandr_get_resources (monitor_manager_xrandr);
+  config_timestamp = resources->configTimestamp;
+  cookie = xcb_randr_set_crtc_config (xcb_conn,
+                                      xrandr_crtc,
+                                      timestamp,
+                                      config_timestamp,
+                                      x, y,
+                                      mode,
+                                      rotation,
+                                      n_outputs,
+                                      outputs);
+  reply = xcb_randr_set_crtc_config_reply (xcb_conn,
+                                           cookie,
+                                           &xcb_error);
+  if (xcb_error || !reply)
+    {
+      free (xcb_error);
+      free (reply);
+      return FALSE;
+    }
+
+  *out_timestamp = reply->timestamp;
+  free (reply);
+
+  return TRUE;
+}
+
+static MetaMonitorTransform
+meta_monitor_transform_from_xrandr (Rotation rotation)
+{
+  static const MetaMonitorTransform y_reflected_map[4] = {
+    META_MONITOR_TRANSFORM_FLIPPED_180,
+    META_MONITOR_TRANSFORM_FLIPPED_90,
+    META_MONITOR_TRANSFORM_FLIPPED,
+    META_MONITOR_TRANSFORM_FLIPPED_270
+  };
+  MetaMonitorTransform ret;
+
+  switch (rotation & 0x7F)
+    {
+    default:
+    case RR_Rotate_0:
+      ret = META_MONITOR_TRANSFORM_NORMAL;
+      break;
+    case RR_Rotate_90:
+      ret = META_MONITOR_TRANSFORM_90;
+      break;
+    case RR_Rotate_180:
+      ret = META_MONITOR_TRANSFORM_180;
+      break;
+    case RR_Rotate_270:
+      ret = META_MONITOR_TRANSFORM_270;
+      break;
+    }
+
+  if (rotation & RR_Reflect_X)
+    return ret + 4;
+  else if (rotation & RR_Reflect_Y)
+    return y_reflected_map[ret];
+  else
+    return ret;
+}
+
+#define ALL_ROTATIONS (RR_Rotate_0 | RR_Rotate_90 | RR_Rotate_180 | RR_Rotate_270)
+
+static MetaMonitorTransform
+meta_monitor_transform_from_xrandr_all (Rotation rotation)
+{
+  unsigned ret;
+
+  /* Handle the common cases first (none or all) */
+  if (rotation == 0 || rotation == RR_Rotate_0)
+    return (1 << META_MONITOR_TRANSFORM_NORMAL);
+
+  /* All rotations and one reflection -> all of them by composition */
+  if ((rotation & ALL_ROTATIONS) &&
+      ((rotation & RR_Reflect_X) || (rotation & RR_Reflect_Y)))
+    return ALL_TRANSFORMS;
+
+  ret = 1 << META_MONITOR_TRANSFORM_NORMAL;
+  if (rotation & RR_Rotate_90)
+    ret |= 1 << META_MONITOR_TRANSFORM_90;
+  if (rotation & RR_Rotate_180)
+    ret |= 1 << META_MONITOR_TRANSFORM_180;
+  if (rotation & RR_Rotate_270)
+    ret |= 1 << META_MONITOR_TRANSFORM_270;
+  if (rotation & (RR_Rotate_0 | RR_Reflect_X))
+    ret |= 1 << META_MONITOR_TRANSFORM_FLIPPED;
+  if (rotation & (RR_Rotate_90 | RR_Reflect_X))
+    ret |= 1 << META_MONITOR_TRANSFORM_FLIPPED_90;
+  if (rotation & (RR_Rotate_180 | RR_Reflect_X))
+    ret |= 1 << META_MONITOR_TRANSFORM_FLIPPED_180;
+  if (rotation & (RR_Rotate_270 | RR_Reflect_X))
+    ret |= 1 << META_MONITOR_TRANSFORM_FLIPPED_270;
+
+  return ret;
+}
+
+MetaCrtc *
+meta_create_xrandr_crtc (MetaMonitorManager *monitor_manager,
+                         XRRCrtcInfo        *xrandr_crtc,
+                         RRCrtc              crtc_id,
+                         XRRScreenResources *resources)
+{
+  MetaCrtc *crtc;
+  unsigned int i;
+
+  crtc = g_object_new (META_TYPE_CRTC, NULL);
+
+  crtc->monitor_manager = monitor_manager;
+  crtc->crtc_id = crtc_id;
+  crtc->rect.x = xrandr_crtc->x;
+  crtc->rect.y = xrandr_crtc->y;
+  crtc->rect.width = xrandr_crtc->width;
+  crtc->rect.height = xrandr_crtc->height;
+  crtc->is_dirty = FALSE;
+  crtc->transform =
+    meta_monitor_transform_from_xrandr (xrandr_crtc->rotation);
+  crtc->all_transforms =
+    meta_monitor_transform_from_xrandr_all (xrandr_crtc->rotations);
+
+  for (i = 0; i < (unsigned int) resources->nmode; i++)
+    {
+      if (resources->modes[i].id == xrandr_crtc->mode)
+        {
+          crtc->current_mode = g_list_nth_data (monitor_manager->modes, i);
+          break;
+        }
+    }
+
+  return crtc;
+}
diff --git a/src/backends/x11/meta-crtc-xrandr.h b/src/backends/x11/meta-crtc-xrandr.h
new file mode 100644
index 0000000..e74bf8a
--- /dev/null
+++ b/src/backends/x11/meta-crtc-xrandr.h
@@ -0,0 +1,46 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+
+/*
+ * Copyright (C) 2017 Red Hat
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#ifndef META_CRTC_XRANDR_H
+#define META_CRTC_XRANDR_H
+
+#include <X11/extensions/Xrandr.h>
+#include <xcb/randr.h>
+
+#include "backends/meta-crtc.h"
+
+gboolean meta_crtc_xrandr_set_config (MetaCrtc            *crtc,
+                                      xcb_randr_crtc_t     xrandr_crtc,
+                                      xcb_timestamp_t      timestamp,
+                                      int                  x,
+                                      int                  y,
+                                      xcb_randr_mode_t     mode,
+                                      xcb_randr_rotation_t rotation,
+                                      xcb_randr_output_t  *outputs,
+                                      int                  n_outputs,
+                                      xcb_timestamp_t     *out_timestamp);
+
+MetaCrtc * meta_create_xrandr_crtc (MetaMonitorManager *monitor_manager,
+                                    XRRCrtcInfo        *xrandr_crtc,
+                                    RRCrtc              crtc_id,
+                                    XRRScreenResources *resources);
+
+#endif /* META_CRTC_XRANDR_H */
diff --git a/src/backends/x11/meta-monitor-manager-xrandr.c b/src/backends/x11/meta-monitor-manager-xrandr.c
index f8eec04..2598612 100644
--- a/src/backends/x11/meta-monitor-manager-xrandr.c
+++ b/src/backends/x11/meta-monitor-manager-xrandr.c
@@ -44,10 +44,9 @@
 #include "backends/meta-monitor-config-manager.h"
 #include "backends/meta-logical-monitor.h"
 #include "backends/meta-output.h"
+#include "backends/x11/meta-crtc-xrandr.h"
 #include "backends/x11/meta-output-xrandr.h"
 
-#define ALL_TRANSFORMS ((1 << (META_MONITOR_TRANSFORM_FLIPPED_270 + 1)) - 1)
-
 /* Look for DPI_FALLBACK in:
  * http://git.gnome.org/browse/gnome-settings-daemon/tree/plugins/xsettings/gsd-xsettings-manager.c
  * for the reasoning */
@@ -95,81 +94,16 @@ meta_monitor_manager_xrandr_get_xdisplay (MetaMonitorManagerXrandr *manager_xran
   return manager_xrandr->xdisplay;
 }
 
-gboolean
-meta_monitor_manager_xrandr_has_randr15 (MetaMonitorManagerXrandr *manager_xrandr)
+XRRScreenResources *
+meta_monitor_manager_xrandr_get_resources (MetaMonitorManagerXrandr *manager_xrandr)
 {
-  return manager_xrandr->has_randr15;
+  return manager_xrandr->resources;
 }
 
-static MetaMonitorTransform
-meta_monitor_transform_from_xrandr (Rotation rotation)
-{
-  static const MetaMonitorTransform y_reflected_map[4] = {
-    META_MONITOR_TRANSFORM_FLIPPED_180,
-    META_MONITOR_TRANSFORM_FLIPPED_90,
-    META_MONITOR_TRANSFORM_FLIPPED,
-    META_MONITOR_TRANSFORM_FLIPPED_270
-  };
-  MetaMonitorTransform ret;
-
-  switch (rotation & 0x7F)
-    {
-    default:
-    case RR_Rotate_0:
-      ret = META_MONITOR_TRANSFORM_NORMAL;
-      break;
-    case RR_Rotate_90:
-      ret = META_MONITOR_TRANSFORM_90;
-      break;
-    case RR_Rotate_180:
-      ret = META_MONITOR_TRANSFORM_180;
-      break;
-    case RR_Rotate_270:
-      ret = META_MONITOR_TRANSFORM_270;
-      break;
-    }
-
-  if (rotation & RR_Reflect_X)
-    return ret + 4;
-  else if (rotation & RR_Reflect_Y)
-    return y_reflected_map[ret];
-  else
-    return ret;
-}
-
-#define ALL_ROTATIONS (RR_Rotate_0 | RR_Rotate_90 | RR_Rotate_180 | RR_Rotate_270)
-
-static MetaMonitorTransform
-meta_monitor_transform_from_xrandr_all (Rotation rotation)
+gboolean
+meta_monitor_manager_xrandr_has_randr15 (MetaMonitorManagerXrandr *manager_xrandr)
 {
-  unsigned ret;
-
-  /* Handle the common cases first (none or all) */
-  if (rotation == 0 || rotation == RR_Rotate_0)
-    return (1 << META_MONITOR_TRANSFORM_NORMAL);
-
-  /* All rotations and one reflection -> all of them by composition */
-  if ((rotation & ALL_ROTATIONS) &&
-      ((rotation & RR_Reflect_X) || (rotation & RR_Reflect_Y)))
-    return ALL_TRANSFORMS;
-
-  ret = 1 << META_MONITOR_TRANSFORM_NORMAL;
-  if (rotation & RR_Rotate_90)
-    ret |= 1 << META_MONITOR_TRANSFORM_90;
-  if (rotation & RR_Rotate_180)
-    ret |= 1 << META_MONITOR_TRANSFORM_180;
-  if (rotation & RR_Rotate_270)
-    ret |= 1 << META_MONITOR_TRANSFORM_270;
-  if (rotation & (RR_Rotate_0 | RR_Reflect_X))
-    ret |= 1 << META_MONITOR_TRANSFORM_FLIPPED;
-  if (rotation & (RR_Rotate_90 | RR_Reflect_X))
-    ret |= 1 << META_MONITOR_TRANSFORM_FLIPPED_90;
-  if (rotation & (RR_Rotate_180 | RR_Reflect_X))
-    ret |= 1 << META_MONITOR_TRANSFORM_FLIPPED_180;
-  if (rotation & (RR_Rotate_270 | RR_Reflect_X))
-    ret |= 1 << META_MONITOR_TRANSFORM_FLIPPED_270;
-
-  return ret;
+  return manager_xrandr->has_randr15;
 }
 
 static int
@@ -280,34 +214,13 @@ meta_monitor_manager_xrandr_read_current (MetaMonitorManager *manager)
   for (i = 0; i < (unsigned)resources->ncrtc; i++)
     {
       XRRCrtcInfo *xrandr_crtc;
+      RRCrtc crtc_id;
       MetaCrtc *crtc;
 
-      xrandr_crtc = XRRGetCrtcInfo (manager_xrandr->xdisplay, resources,
-                                    resources->crtcs[i]);
-
-      crtc = g_object_new (META_TYPE_CRTC, NULL);
-
-      crtc->monitor_manager = manager;
-      crtc->crtc_id = resources->crtcs[i];
-      crtc->rect.x = xrandr_crtc->x;
-      crtc->rect.y = xrandr_crtc->y;
-      crtc->rect.width = xrandr_crtc->width;
-      crtc->rect.height = xrandr_crtc->height;
-      crtc->is_dirty = FALSE;
-      crtc->transform =
-        meta_monitor_transform_from_xrandr (xrandr_crtc->rotation);
-      crtc->all_transforms =
-        meta_monitor_transform_from_xrandr_all (xrandr_crtc->rotations);
-
-      for (j = 0; j < (unsigned)resources->nmode; j++)
-       {
-         if (resources->modes[j].id == xrandr_crtc->mode)
-           {
-             crtc->current_mode = g_list_nth_data (manager->modes, j);
-             break;
-           }
-       }
-
+      crtc_id = resources->crtcs[i];
+      xrandr_crtc = XRRGetCrtcInfo (manager_xrandr->xdisplay,
+                                    resources, crtc_id);
+      crtc = meta_create_xrandr_crtc (manager, xrandr_crtc, crtc_id, resources);
       XRRFreeCrtcInfo (xrandr_crtc);
 
       manager->crtcs = g_list_append (manager->crtcs, crtc);
@@ -432,8 +345,9 @@ meta_monitor_transform_to_xrandr (MetaMonitorTransform transform)
 
 static gboolean
 xrandr_set_crtc_config (MetaMonitorManagerXrandr *manager_xrandr,
+                        MetaCrtc                 *crtc,
                         gboolean                  save_timestamp,
-                        xcb_randr_crtc_t          crtc,
+                        xcb_randr_crtc_t          xrandr_crtc,
                         xcb_timestamp_t           timestamp,
                         int                       x,
                         int                       y,
@@ -442,37 +356,16 @@ xrandr_set_crtc_config (MetaMonitorManagerXrandr *manager_xrandr,
                         xcb_randr_output_t       *outputs,
                         int                       n_outputs)
 {
-  xcb_connection_t *xcb_conn;
-  xcb_timestamp_t config_timestamp;
-  xcb_randr_set_crtc_config_cookie_t cookie;
-  xcb_randr_set_crtc_config_reply_t *reply;
-  xcb_generic_error_t *xcb_error = NULL;
-
-  xcb_conn = XGetXCBConnection (manager_xrandr->xdisplay);
-  config_timestamp = manager_xrandr->resources->configTimestamp;
-  cookie = xcb_randr_set_crtc_config (xcb_conn,
-                                      crtc,
-                                      timestamp,
-                                      config_timestamp,
-                                      x, y,
-                                      mode,
-                                      rotation,
-                                      n_outputs,
-                                      outputs);
-  reply = xcb_randr_set_crtc_config_reply (xcb_conn,
-                                           cookie,
-                                           &xcb_error);
-  if (xcb_error || !reply)
-    {
-      free (xcb_error);
-      free (reply);
-      return FALSE;
-    }
+  xcb_timestamp_t new_timestamp;
 
-  if (save_timestamp)
-    manager_xrandr->last_xrandr_set_timestamp = reply->timestamp;
+  if (!meta_crtc_xrandr_set_config (crtc, xrandr_crtc, timestamp,
+                                    x, y, mode, rotation,
+                                    outputs, n_outputs,
+                                    &new_timestamp))
+    return FALSE;
 
-  free (reply);
+  if (save_timestamp)
+    manager_xrandr->last_xrandr_set_timestamp = new_timestamp;
 
   return TRUE;
 }
@@ -654,6 +547,7 @@ apply_crtc_assignments (MetaMonitorManager *manager,
           crtc->rect.y + crtc->rect.height > height)
         {
           xrandr_set_crtc_config (manager_xrandr,
+                                  crtc,
                                   save_timestamp,
                                   (xcb_randr_crtc_t) crtc->crtc_id,
                                   XCB_CURRENT_TIME,
@@ -683,6 +577,7 @@ apply_crtc_assignments (MetaMonitorManager *manager,
         continue;
 
       xrandr_set_crtc_config (manager_xrandr,
+                              crtc,
                               save_timestamp,
                               (xcb_randr_crtc_t) crtc->crtc_id,
                               XCB_CURRENT_TIME,
@@ -740,6 +635,7 @@ apply_crtc_assignments (MetaMonitorManager *manager,
 
           rotation = meta_monitor_transform_to_xrandr (crtc_info->transform);
           if (!xrandr_set_crtc_config (manager_xrandr,
+                                       crtc,
                                        save_timestamp,
                                        (xcb_randr_crtc_t) crtc->crtc_id,
                                        XCB_CURRENT_TIME,
diff --git a/src/backends/x11/meta-monitor-manager-xrandr.h b/src/backends/x11/meta-monitor-manager-xrandr.h
index 9f1d6ff..b898b1a 100644
--- a/src/backends/x11/meta-monitor-manager-xrandr.h
+++ b/src/backends/x11/meta-monitor-manager-xrandr.h
@@ -23,6 +23,8 @@
 #ifndef META_MONITOR_MANAGER_XRANDR_H
 #define META_MONITOR_MANAGER_XRANDR_H
 
+#include <X11/extensions/Xrandr.h>
+
 #include "meta-monitor-manager-private.h"
 
 #define META_TYPE_MONITOR_MANAGER_XRANDR (meta_monitor_manager_xrandr_get_type ())
@@ -31,6 +33,8 @@ G_DECLARE_FINAL_TYPE (MetaMonitorManagerXrandr, meta_monitor_manager_xrandr,
 
 Display * meta_monitor_manager_xrandr_get_xdisplay (MetaMonitorManagerXrandr *manager_xrandr);
 
+XRRScreenResources * meta_monitor_manager_xrandr_get_resources (MetaMonitorManagerXrandr *manager_xrandr);
+
 gboolean meta_monitor_manager_xrandr_has_randr15 (MetaMonitorManagerXrandr *manager_xrandr);
 
 gboolean meta_monitor_manager_xrandr_handle_xevent (MetaMonitorManagerXrandr *manager,


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