meta-monitor-manager.c 96.2 KB
Newer Older
1 2
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

3
/*
4 5 6 7 8 9 10
 * 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) 2013 Red Hat Inc.
11
 *
12 13 14 15 16 17 18 19 20
 * 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.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 25 26 27
 */

#include "config.h"

28
#include "meta-monitor-manager-private.h"
29

30 31
#include <string.h>
#include <math.h>
32
#include <stdlib.h>
33 34
#include <clutter/clutter.h>

35
#include <meta/main.h>
36
#include "util-private.h"
37
#include <meta/errors.h>
38
#include "edid.h"
39
#include "backends/meta-crtc.h"
40
#include "backends/meta-logical-monitor.h"
41
#include "backends/meta-monitor.h"
42
#include "backends/meta-monitor-config-manager.h"
43
#include "backends/meta-orientation-manager.h"
Jonas Ådahl's avatar
Jonas Ådahl committed
44
#include "backends/meta-output.h"
45
#include "backends/x11/meta-monitor-manager-xrandr.h"
46
#include "meta-backend-private.h"
47

48 49
#define DEFAULT_DISPLAY_CONFIGURATION_TIMEOUT 20

50 51 52 53 54 55 56 57 58 59 60
enum
{
  PROP_0,

  PROP_BACKEND,

  PROP_LAST
};

static GParamSpec *obj_props[PROP_LAST];

61
enum {
62
  MONITORS_CHANGED_INTERNAL,
63
  CONFIRM_DISPLAY_CHANGE,
64 65 66
  SIGNALS_LAST
};

67 68 69 70 71 72 73 74 75 76 77 78
/* Array index matches MetaMonitorTransform */
static gfloat transform_matrices[][6] = {
  {  1,  0,  0,  0,  1,  0 }, /* normal */
  {  0, -1,  1,  1,  0,  0 }, /* 90° */
  { -1,  0,  1,  0, -1,  1 }, /* 180° */
  {  0,  1,  0, -1,  0,  1 }, /* 270° */
  { -1,  0,  1,  0,  1,  0 }, /* normal flipped */
  {  0,  1,  0,  1,  0,  0 }, /* 90° flipped */
  {  1,  0,  0,  0, -1,  1 }, /* 180° flipped */
  {  0, -1,  1, -1,  0,  1 }, /* 270° flipped */
};

79 80
static int signals[SIGNALS_LAST];

81 82
static void meta_monitor_manager_display_config_init (MetaDBusDisplayConfigIface *iface);

83 84
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (MetaMonitorManager, meta_monitor_manager, META_DBUS_TYPE_DISPLAY_CONFIG_SKELETON,
                                  G_IMPLEMENT_INTERFACE (META_DBUS_TYPE_DISPLAY_CONFIG, meta_monitor_manager_display_config_init));
85

86
static void initialize_dbus_interface (MetaMonitorManager *manager);
87

88 89 90 91
static gboolean
meta_monitor_manager_is_config_complete (MetaMonitorManager *manager,
                                         MetaMonitorsConfig *config);

92 93 94 95 96 97
MetaBackend *
meta_monitor_manager_get_backend (MetaMonitorManager *manager)
{
  return manager->backend;
}

98 99 100 101 102
static void
meta_monitor_manager_init (MetaMonitorManager *manager)
{
}

103 104 105 106 107 108 109 110 111
static void
meta_monitor_manager_set_primary_logical_monitor (MetaMonitorManager *manager,
                                                  MetaLogicalMonitor *logical_monitor)
{
  manager->primary_logical_monitor = logical_monitor;
  if (logical_monitor)
    meta_logical_monitor_make_primary (logical_monitor);
}

112 113 114 115 116 117
static gboolean
is_main_tiled_monitor_output (MetaOutput *output)
{
  return output->tile_info.loc_h_tile == 0 && output->tile_info.loc_v_tile == 0;
}

118 119 120 121
static MetaLogicalMonitor *
logical_monitor_from_layout (MetaMonitorManager *manager,
                             GList              *logical_monitors,
                             MetaRectangle      *layout)
122
{
123
  GList *l;
124

125
  for (l = logical_monitors; l; l = l->next)
126
    {
127
      MetaLogicalMonitor *logical_monitor = l->data;
128

129 130
      if (meta_rectangle_equal (layout, &logical_monitor->rect))
        return logical_monitor;
131 132
    }

133
  return NULL;
134 135
}

136 137 138 139
static void
meta_monitor_manager_rebuild_logical_monitors (MetaMonitorManager *manager,
                                               MetaMonitorsConfig *config)
{
140
  GList *logical_monitor_configs;
141 142 143 144 145
  GList *logical_monitors = NULL;
  GList *l;
  int monitor_number = 0;
  MetaLogicalMonitor *primary_logical_monitor = NULL;

146 147
  logical_monitor_configs = config ? config->logical_monitor_configs : NULL;
  for (l = logical_monitor_configs; l; l = l->next)
148 149 150 151
    {
      MetaLogicalMonitorConfig *logical_monitor_config = l->data;
      MetaLogicalMonitor *logical_monitor;

152 153 154
      logical_monitor = meta_logical_monitor_new (manager,
                                                  logical_monitor_config,
                                                  monitor_number);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      monitor_number++;

      if (logical_monitor_config->is_primary)
        primary_logical_monitor = logical_monitor;

      logical_monitors = g_list_append (logical_monitors, logical_monitor);
    }

  /*
   * If no monitor was marked as primary, fall back on marking the first
   * logical monitor the primary one.
   */
  if (!primary_logical_monitor && logical_monitors)
    primary_logical_monitor = g_list_first (logical_monitors)->data;

  manager->logical_monitors = logical_monitors;
  meta_monitor_manager_set_primary_logical_monitor (manager,
                                                    primary_logical_monitor);
}

175
static float
176 177
derive_configured_global_scale (MetaMonitorManager *manager,
                                MetaMonitorsConfig *config)
178 179 180 181 182 183 184 185
{
  MetaLogicalMonitorConfig *logical_monitor_config;

  logical_monitor_config = config->logical_monitor_configs->data;

  return logical_monitor_config->scale;
}

186
static float
187 188 189 190 191 192 193 194 195 196 197
calculate_monitor_scale (MetaMonitorManager *manager,
                         MetaMonitor        *monitor)
{
  MetaMonitorMode *monitor_mode;

  monitor_mode = meta_monitor_get_current_mode (monitor);
  return meta_monitor_manager_calculate_monitor_mode_scale (manager,
                                                            monitor,
                                                            monitor_mode);
}

198
static float
199 200 201 202 203 204
derive_calculated_global_scale (MetaMonitorManager *manager)
{
  MetaMonitor *primary_monitor;

  primary_monitor = meta_monitor_manager_get_primary_monitor (manager);
  if (!primary_monitor)
205
    return 1.0;
206 207 208 209

  return calculate_monitor_scale (manager, primary_monitor);
}

210
static float
211
derive_scale_from_config (MetaMonitorManager *manager,
212
                          MetaMonitorsConfig *config,
213 214 215 216 217 218 219 220 221 222 223 224 225
                          MetaRectangle      *layout)
{
  GList *l;

  for (l = config->logical_monitor_configs; l; l = l->next)
    {
      MetaLogicalMonitorConfig *logical_monitor_config = l->data;

      if (meta_rectangle_equal (layout, &logical_monitor_config->layout))
        return logical_monitor_config->scale;
    }

  g_warning ("Missing logical monitor, using scale 1");
226
  return 1.0;
227 228
}

229
static void
230 231
meta_monitor_manager_rebuild_logical_monitors_derived (MetaMonitorManager *manager,
                                                       MetaMonitorsConfig *config)
232
{
233
  GList *logical_monitors = NULL;
234
  GList *l;
235
  int monitor_number;
236
  MetaLogicalMonitor *primary_logical_monitor = NULL;
237
  gboolean use_global_scale;
238
  float global_scale = 0.0;
239
  MetaMonitorManagerCapability capabilities;
240

241
  monitor_number = 0;
242

243 244 245 246 247 248
  capabilities = meta_monitor_manager_get_capabilities (manager);
  use_global_scale =
    !!(capabilities & META_MONITOR_MANAGER_CAPABILITY_GLOBAL_SCALE_REQUIRED);

  if (use_global_scale)
    {
249 250
      if (config)
        global_scale = derive_configured_global_scale (manager, config);
251 252 253 254
      else
        global_scale = derive_calculated_global_scale (manager);
    }

255
  for (l = manager->monitors; l; l = l->next)
256
    {
257 258 259
      MetaMonitor *monitor = l->data;
      MetaLogicalMonitor *logical_monitor;
      MetaRectangle layout;
260

261
      if (!meta_monitor_is_active (monitor))
262 263
        continue;

264
      meta_monitor_derive_layout (monitor, &layout);
265 266 267
      logical_monitor = logical_monitor_from_layout (manager, logical_monitors,
                                                     &layout);
      if (logical_monitor)
268
        {
269
          meta_logical_monitor_add_monitor (logical_monitor, monitor);
270
        }
271
      else
272
        {
273
          float scale;
274 275 276

          if (use_global_scale)
            scale = global_scale;
277 278
          else if (config)
            scale = derive_scale_from_config (manager, config, &layout);
279 280 281 282 283
          else
            scale = calculate_monitor_scale (manager, monitor);

          g_assert (scale > 0);

284 285 286
          logical_monitor = meta_logical_monitor_new_derived (manager,
                                                              monitor,
                                                              &layout,
287
                                                              scale,
288
                                                              monitor_number);
289 290
          logical_monitors = g_list_append (logical_monitors, logical_monitor);
          monitor_number++;
291 292
        }

293 294
      if (meta_monitor_is_primary (monitor))
        primary_logical_monitor = logical_monitor;
295 296
    }

297
  manager->logical_monitors = logical_monitors;
298

299 300 301 302
  /*
   * If no monitor was marked as primary, fall back on marking the first
   * logical monitor the primary one.
   */
303 304 305 306 307
  if (!primary_logical_monitor && manager->logical_monitors)
    primary_logical_monitor = g_list_first (manager->logical_monitors)->data;

  meta_monitor_manager_set_primary_logical_monitor (manager,
                                                    primary_logical_monitor);
308 309
}

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
static void
power_save_mode_changed (MetaMonitorManager *manager,
                         GParamSpec         *pspec,
                         gpointer            user_data)
{
  MetaMonitorManagerClass *klass;
  int mode = meta_dbus_display_config_get_power_save_mode (META_DBUS_DISPLAY_CONFIG (manager));

  if (mode == META_POWER_SAVE_UNSUPPORTED)
    return;

  /* If DPMS is unsupported, force the property back. */
  if (manager->power_save_mode == META_POWER_SAVE_UNSUPPORTED)
    {
      meta_dbus_display_config_set_power_save_mode (META_DBUS_DISPLAY_CONFIG (manager), META_POWER_SAVE_UNSUPPORTED);
      return;
    }

  klass = META_MONITOR_MANAGER_GET_CLASS (manager);
  if (klass->set_power_save_mode)
    klass->set_power_save_mode (manager, mode);

  manager->power_save_mode = mode;
}

335 336 337
void
meta_monitor_manager_lid_is_closed_changed (MetaMonitorManager *manager)
{
338
  meta_monitor_manager_ensure_configured (manager);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
}

static void
lid_is_closed_changed (UpClient   *client,
                       GParamSpec *pspec,
                       gpointer    user_data)
{
  MetaMonitorManager *manager = user_data;

  meta_monitor_manager_lid_is_closed_changed (manager);
}

static gboolean
meta_monitor_manager_real_is_lid_closed (MetaMonitorManager *manager)
{
354 355 356
  if (!manager->up_client)
    return FALSE;

357 358 359 360 361 362 363 364 365
  return up_client_get_lid_is_closed (manager->up_client);
}

gboolean
meta_monitor_manager_is_lid_closed (MetaMonitorManager *manager)
{
  return META_MONITOR_MANAGER_GET_CLASS (manager)->is_lid_closed (manager);
}

366 367 368
gboolean
meta_monitor_manager_is_headless (MetaMonitorManager *manager)
{
369
  return !manager->logical_monitors;
370 371
}

372
float
373 374 375 376 377 378 379 380 381 382 383 384
meta_monitor_manager_calculate_monitor_mode_scale (MetaMonitorManager *manager,
                                                   MetaMonitor        *monitor,
                                                   MetaMonitorMode    *monitor_mode)
{
  MetaMonitorManagerClass *manager_class =
    META_MONITOR_MANAGER_GET_CLASS (manager);

  return manager_class->calculate_monitor_mode_scale (manager,
                                                      monitor,
                                                      monitor_mode);
}

385 386 387 388 389 390
float *
meta_monitor_manager_calculate_supported_scales (MetaMonitorManager          *manager,
                                                 MetaLogicalMonitorLayoutMode layout_mode,
                                                 MetaMonitor                 *monitor,
                                                 MetaMonitorMode             *monitor_mode,
                                                 int                         *n_supported_scales)
391 392 393 394
{
  MetaMonitorManagerClass *manager_class =
    META_MONITOR_MANAGER_GET_CLASS (manager);

395 396 397 398 399
  return manager_class->calculate_supported_scales (manager,
                                                    layout_mode,
                                                    monitor,
                                                    monitor_mode,
                                                    n_supported_scales);
400 401
}

402
MetaMonitorManagerCapability
403 404 405 406 407 408 409 410
meta_monitor_manager_get_capabilities (MetaMonitorManager *manager)
{
  MetaMonitorManagerClass *manager_class =
    META_MONITOR_MANAGER_GET_CLASS (manager);

  return manager_class->get_capabilities (manager);
}

411 412 413 414 415 416 417 418 419 420 421
gboolean
meta_monitor_manager_get_max_screen_size (MetaMonitorManager *manager,
                                          int                *max_width,
                                          int                *max_height)
{
  MetaMonitorManagerClass *manager_class =
    META_MONITOR_MANAGER_GET_CLASS (manager);

  return manager_class->get_max_screen_size (manager, max_width, max_height);
}

422 423 424 425 426 427 428 429 430 431

MetaLogicalMonitorLayoutMode
meta_monitor_manager_get_default_layout_mode (MetaMonitorManager *manager)
{
  MetaMonitorManagerClass *manager_class =
    META_MONITOR_MANAGER_GET_CLASS (manager);

  return manager_class->get_default_layout_mode (manager);
}

432 433 434 435 436 437
static void
meta_monitor_manager_ensure_initial_config (MetaMonitorManager *manager)
{
  META_MONITOR_MANAGER_GET_CLASS (manager)->ensure_initial_config (manager);
}

438
static gboolean
439 440 441 442
meta_monitor_manager_apply_monitors_config (MetaMonitorManager      *manager,
                                            MetaMonitorsConfig      *config,
                                            MetaMonitorsConfigMethod method,
                                            GError                 **error)
443 444 445 446
{
  MetaMonitorManagerClass *manager_class =
    META_MONITOR_MANAGER_GET_CLASS (manager);

447 448 449
  g_assert (!config ||
            !(config->flags & META_MONITORS_CONFIG_FLAG_MIGRATED));

450 451 452
  if (!manager_class->apply_monitors_config (manager, config, method, error))
    return FALSE;

453 454 455 456 457 458 459 460 461
  switch (method)
    {
    case META_MONITORS_CONFIG_METHOD_TEMPORARY:
    case META_MONITORS_CONFIG_METHOD_PERSISTENT:
      meta_monitor_config_manager_set_current (manager->config_manager, config);
      break;
    case META_MONITORS_CONFIG_METHOD_VERIFY:
      break;
    }
462 463

  return TRUE;
464 465
}

466 467 468
gboolean
meta_monitor_manager_has_hotplug_mode_update (MetaMonitorManager *manager)
{
Jonas Ådahl's avatar
Jonas Ådahl committed
469
  GList *l;
470

471
  for (l = manager->gpus; l; l = l->next)
472
    {
473
      MetaGpu *gpu = l->data;
474

475
      if (meta_gpu_has_hotplug_mode_update (gpu))
476 477 478 479 480 481
        return TRUE;
    }

  return FALSE;
}

482 483 484
static gboolean
should_use_stored_config (MetaMonitorManager *manager)
{
485 486
  return (manager->in_init ||
          !meta_monitor_manager_has_hotplug_mode_update (manager));
487 488
}

489
MetaMonitorsConfig *
490 491
meta_monitor_manager_ensure_configured (MetaMonitorManager *manager)
{
492 493
  MetaMonitorsConfig *config = NULL;
  GError *error = NULL;
494 495 496 497
  gboolean use_stored_config;
  MetaMonitorsConfigMethod method;
  MetaMonitorsConfigMethod fallback_method =
    META_MONITORS_CONFIG_METHOD_TEMPORARY;
498

499 500 501 502 503 504 505
  use_stored_config = should_use_stored_config (manager);
  if (use_stored_config)
    method = META_MONITORS_CONFIG_METHOD_PERSISTENT;
  else
    method = META_MONITORS_CONFIG_METHOD_TEMPORARY;

  if (use_stored_config)
506 507 508 509
    {
      config = meta_monitor_config_manager_get_stored (manager->config_manager);
      if (config)
        {
510 511 512
          if (!meta_monitor_manager_apply_monitors_config (manager,
                                                           config,
                                                           method,
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
                                                           &error))
            {
              config = NULL;
              g_warning ("Failed to use stored monitor configuration: %s",
                         error->message);
              g_clear_error (&error);
            }
          else
            {
              g_object_ref (config);
              goto done;
            }
        }
    }

528 529 530
  config = meta_monitor_config_manager_create_suggested (manager->config_manager);
  if (config)
    {
531 532 533 534
      if (!meta_monitor_manager_apply_monitors_config (manager,
                                                       config,
                                                       method,
                                                       &error))
535 536 537 538 539 540 541 542 543 544 545 546
        {
          g_clear_object (&config);
          g_warning ("Failed to use suggested monitor configuration: %s",
                     error->message);
          g_clear_error (&error);
        }
      else
        {
          goto done;
        }
    }

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
  config = meta_monitor_config_manager_get_previous (manager->config_manager);
  if (config)
    {
      config = g_object_ref (config);

      if (meta_monitor_manager_is_config_complete (manager, config))
        {
          if (!meta_monitor_manager_apply_monitors_config (manager,
                                                           config,
                                                           method,
                                                           &error))
            {
              g_warning ("Failed to use suggested monitor configuration: %s",
                         error->message);
              g_clear_error (&error);
            }
          else
            {
              goto done;
            }
        }

      g_clear_object (&config);
    }

572
  config = meta_monitor_config_manager_create_linear (manager->config_manager);
573
  if (config)
574
    {
575 576 577 578
      if (!meta_monitor_manager_apply_monitors_config (manager,
                                                       config,
                                                       method,
                                                       &error))
579 580 581 582 583 584 585 586 587 588
        {
          g_clear_object (&config);
          g_warning ("Failed to use linear monitor configuration: %s",
                     error->message);
          g_clear_error (&error);
        }
      else
        {
          goto done;
        }
589 590 591
    }

  config = meta_monitor_config_manager_create_fallback (manager->config_manager);
592
  if (config)
593
    {
594 595 596 597
      if (!meta_monitor_manager_apply_monitors_config (manager,
                                                       config,
                                                       fallback_method,
                                                       &error))
598 599 600
        {
          g_clear_object (&config);
          g_warning ("Failed to use fallback monitor configuration: %s",
601
                 error->message);
602 603 604 605 606 607
          g_clear_error (&error);
        }
      else
        {
          goto done;
        }
608 609 610 611
    }

done:
  if (!config)
612
    {
613 614 615 616
      meta_monitor_manager_apply_monitors_config (manager,
                                                  NULL,
                                                  fallback_method,
                                                  &error);
617 618
      return NULL;
    }
619 620 621 622

  g_object_unref (config);

  return config;
623 624
}

625 626 627 628 629
static void
orientation_changed (MetaOrientationManager *orientation_manager,
                     MetaMonitorManager     *manager)
{
  MetaMonitorTransform transform;
630 631
  GError *error = NULL;
  MetaMonitorsConfig *config;
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

  switch (meta_orientation_manager_get_orientation (orientation_manager))
    {
    case META_ORIENTATION_NORMAL:
      transform = META_MONITOR_TRANSFORM_NORMAL;
      break;
    case META_ORIENTATION_BOTTOM_UP:
      transform = META_MONITOR_TRANSFORM_180;
      break;
    case META_ORIENTATION_LEFT_UP:
      transform = META_MONITOR_TRANSFORM_90;
      break;
    case META_ORIENTATION_RIGHT_UP:
      transform = META_MONITOR_TRANSFORM_270;
      break;

    case META_ORIENTATION_UNDEFINED:
649
    default:
650 651 652
      return;
    }

653 654 655 656 657
  config =
    meta_monitor_config_manager_create_for_orientation (manager->config_manager,
                                                        transform);
  if (!config)
    return;
658

659 660 661 662 663 664 665 666
  if (!meta_monitor_manager_apply_monitors_config (manager,
                                                   config,
                                                   META_MONITORS_CONFIG_METHOD_TEMPORARY,
                                                   &error))
    {
      g_warning ("Failed to use orientation monitor configuration: %s",
                 error->message);
      g_error_free (error);
667
    }
668
  g_object_unref (config);
669 670
}

671
static void
672
experimental_features_changed (MetaSettings           *settings,
673 674
                               MetaExperimentalFeature old_experimental_features,
                               MetaMonitorManager     *manager)
675
{
676 677 678
  gboolean was_stage_views_scaled;
  gboolean is_stage_views_scaled;
  gboolean should_reconfigure = FALSE;
679

680 681 682 683
  was_stage_views_scaled =
    !!(old_experimental_features &
       META_EXPERIMENTAL_FEATURE_SCALE_MONITOR_FRAMEBUFFER);
  is_stage_views_scaled =
684 685
    meta_settings_is_experimental_feature_enabled (
      settings,
686 687
      META_EXPERIMENTAL_FEATURE_SCALE_MONITOR_FRAMEBUFFER);

688
  if (is_stage_views_scaled != was_stage_views_scaled)
689 690 691 692 693
    should_reconfigure = TRUE;

  if (should_reconfigure)
    meta_monitor_manager_on_hotplug (manager);

694
  meta_settings_update_ui_scaling_factor (settings);
695 696
}

697 698 699 700 701 702 703 704 705 706 707 708 709 710
void
meta_monitor_manager_setup (MetaMonitorManager *manager)
{
  manager->in_init = TRUE;

  manager->config_manager = meta_monitor_config_manager_new (manager);

  meta_monitor_manager_read_current_state (manager);

  meta_monitor_manager_ensure_initial_config (manager);

  manager->in_init = FALSE;
}

711 712 713 714
static void
meta_monitor_manager_constructed (GObject *object)
{
  MetaMonitorManager *manager = META_MONITOR_MANAGER (object);
715 716
  MetaMonitorManagerClass *manager_class =
    META_MONITOR_MANAGER_GET_CLASS (manager);
717
  MetaBackend *backend = manager->backend;
718
  MetaSettings *settings = meta_backend_get_settings (backend);
719

720
  manager->experimental_features_changed_handler_id =
721
    g_signal_connect (settings,
722 723 724 725
                      "experimental-features-changed",
                      G_CALLBACK (experimental_features_changed),
                      manager);

726 727 728 729 730 731
  if (manager_class->is_lid_closed == meta_monitor_manager_real_is_lid_closed)
    {
      manager->up_client = up_client_new ();
      g_signal_connect_object (manager->up_client, "notify::lid-is-closed",
                               G_CALLBACK (lid_is_closed_changed), manager, 0);
    }
732

733 734 735
  g_signal_connect_object (manager, "notify::power-save-mode",
                           G_CALLBACK (power_save_mode_changed), manager, 0);

736 737 738 739 740
  g_signal_connect_object (meta_backend_get_orientation_manager (backend),
                           "orientation-changed",
                           G_CALLBACK (orientation_changed),
                           manager, 0);

741
  manager->current_switch_config = META_MONITOR_SWITCH_CONFIG_UNKNOWN;
742

743
  initialize_dbus_interface (manager);
744 745 746 747 748 749 750
}

static void
meta_monitor_manager_finalize (GObject *object)
{
  MetaMonitorManager *manager = META_MONITOR_MANAGER (object);

751
  g_list_free_full (manager->gpus, g_object_unref);
752
  g_list_free_full (manager->logical_monitors, g_object_unref);
753

754
  g_signal_handler_disconnect (manager->backend,
755 756
                               manager->experimental_features_changed_handler_id);

757 758 759
  G_OBJECT_CLASS (meta_monitor_manager_parent_class)->finalize (object);
}

760 761 762 763 764 765 766 767 768 769 770
static void
meta_monitor_manager_dispose (GObject *object)
{
  MetaMonitorManager *manager = META_MONITOR_MANAGER (object);

  if (manager->dbus_name_id != 0)
    {
      g_bus_unown_name (manager->dbus_name_id);
      manager->dbus_name_id = 0;
    }

771
  g_clear_object (&manager->config_manager);
772
  g_clear_object (&manager->up_client);
773

774 775 776
  G_OBJECT_CLASS (meta_monitor_manager_parent_class)->dispose (object);
}

777 778 779 780 781 782 783 784 785 786 787 788 789 790
static GBytes *
meta_monitor_manager_real_read_edid (MetaMonitorManager *manager,
                                     MetaOutput         *output)
{
  return NULL;
}

static char *
meta_monitor_manager_real_get_edid_file (MetaMonitorManager *manager,
                                         MetaOutput         *output)
{
  return NULL;
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
static void
meta_monitor_manager_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  MetaMonitorManager *manager = META_MONITOR_MANAGER (object);

  switch (prop_id)
    {
    case PROP_BACKEND:
      manager->backend = g_value_get_object (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
meta_monitor_manager_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  MetaMonitorManager *manager = META_MONITOR_MANAGER (object);

  switch (prop_id)
    {
    case PROP_BACKEND:
      g_value_set_object (value, manager->backend);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

827 828 829 830 831
static void
meta_monitor_manager_class_init (MetaMonitorManagerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

832
  object_class->constructed = meta_monitor_manager_constructed;
833
  object_class->dispose = meta_monitor_manager_dispose;
834
  object_class->finalize = meta_monitor_manager_finalize;
835 836
  object_class->get_property = meta_monitor_manager_get_property;
  object_class->set_property = meta_monitor_manager_set_property;
837

838 839
  klass->get_edid_file = meta_monitor_manager_real_get_edid_file;
  klass->read_edid = meta_monitor_manager_real_read_edid;
840
  klass->is_lid_closed = meta_monitor_manager_real_is_lid_closed;
841

842 843 844 845 846 847 848 849
  signals[MONITORS_CHANGED_INTERNAL] =
    g_signal_new ("monitors-changed-internal",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

850 851 852 853 854 855 856
  signals[CONFIRM_DISPLAY_CHANGE] =
    g_signal_new ("confirm-display-change",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  0,
                  NULL, NULL, NULL,
		  G_TYPE_NONE, 0);
857 858 859 860 861 862 863 864 865 866

  obj_props[PROP_BACKEND] =
    g_param_spec_object ("backend",
                         "backend",
                         "MetaBackend",
                         META_TYPE_BACKEND,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);
  g_object_class_install_properties (object_class, PROP_LAST, obj_props);
867 868
}

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
static const double known_diagonals[] = {
    12.1,
    13.3,
    15.6
};

static char *
diagonal_to_str (double d)
{
  unsigned int i;

  for (i = 0; i < G_N_ELEMENTS (known_diagonals); i++)
    {
      double delta;

      delta = fabs(known_diagonals[i] - d);
      if (delta < 0.1)
        return g_strdup_printf ("%0.1lf\"", known_diagonals[i]);
    }

  return g_strdup_printf ("%d\"", (int) (d + 0.5));
}

static char *
893 894
make_display_name (MetaMonitorManager *manager,
                   MetaOutput         *output)
895
{
896 897
  g_autofree char *inches = NULL;
  g_autofree char *vendor_name = NULL;
898

899
  if (meta_output_is_laptop (output))
900
      return g_strdup (_("Built-in display"));
901

902
  if (output->width_mm > 0 && output->height_mm > 0)
903 904 905
    {
      double d = sqrt (output->width_mm * output->width_mm +
                       output->height_mm * output->height_mm);
906 907
      inches = diagonal_to_str (d / 25.4);
    }
908

909 910 911 912
  if (g_strcmp0 (output->vendor, "unknown") != 0)
    {
      if (!manager->pnp_ids)
        manager->pnp_ids = gnome_pnp_ids_new ();
913

914 915
      vendor_name = gnome_pnp_ids_get_pnp_id (manager->pnp_ids,
                                              output->vendor);
916

917 918 919 920 921 922 923
      if (!vendor_name)
        vendor_name = g_strdup (output->vendor);
    }
  else
    {
      if (inches != NULL)
        vendor_name = g_strdup (_("Unknown"));
924
      else
925 926
        vendor_name = g_strdup (_("Unknown Display"));
    }
927

928 929 930 931 932
  if (inches != NULL)
    {
      /* TRANSLATORS: this is a monitor vendor name, followed by a
       * size in inches, like 'Dell 15"'
       */
933
      return g_strdup_printf (_("%s %s"), vendor_name, inches);
934 935 936
    }
  else
    {
937
      return g_strdup (vendor_name);
938 939 940
    }
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
static const char *
get_connector_type_name (MetaConnectorType connector_type)
{
  switch (connector_type)
    {
    case META_CONNECTOR_TYPE_Unknown: return "Unknown";
    case META_CONNECTOR_TYPE_VGA: return "VGA";
    case META_CONNECTOR_TYPE_DVII: return "DVII";
    case META_CONNECTOR_TYPE_DVID: return "DVID";
    case META_CONNECTOR_TYPE_DVIA: return "DVIA";
    case META_CONNECTOR_TYPE_Composite: return "Composite";
    case META_CONNECTOR_TYPE_SVIDEO: return "SVIDEO";
    case META_CONNECTOR_TYPE_LVDS: return "LVDS";
    case META_CONNECTOR_TYPE_Component: return "Component";
    case META_CONNECTOR_TYPE_9PinDIN: return "9PinDIN";
    case META_CONNECTOR_TYPE_DisplayPort: return "DisplayPort";
    case META_CONNECTOR_TYPE_HDMIA: return "HDMIA";
    case META_CONNECTOR_TYPE_HDMIB: return "HDMIB";
    case META_CONNECTOR_TYPE_TV: return "TV";
    case META_CONNECTOR_TYPE_eDP: return "eDP";
    case META_CONNECTOR_TYPE_VIRTUAL: return "VIRTUAL";
    case META_CONNECTOR_TYPE_DSI: return "DSI";
    default: g_assert_not_reached ();
    }
}

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
static GList *
combine_gpu_lists (MetaMonitorManager    *manager,
                   GList              * (*list_getter) (MetaGpu *gpu))
{
  GList *list = NULL;
  GList *l;

  for (l = manager->gpus; l; l = l->next)
    {
      MetaGpu *gpu = l->data;

      list = g_list_concat (list, g_list_copy (list_getter (gpu)));
    }

  return list;
}

984
static gboolean
985 986
meta_monitor_manager_handle_get_resources (MetaDBusDisplayConfig *skeleton,
                                           GDBusMethodInvocation *invocation)
987
{
988
  MetaMonitorManager *manager = META_MONITOR_MANAGER (skeleton);
989
  MetaMonitorManagerClass *manager_class = META_MONITOR_MANAGER_GET_CLASS (skeleton);
990 991 992
  GList *combined_modes;
  GList *combined_outputs;
  GList *combined_crtcs;
993
  GVariantBuilder crtc_builder, output_builder, mode_builder;
Jonas Ådahl's avatar
Jonas Ådahl committed
994
  GList *l;
995
  unsigned int i, j;
996 997
  int max_screen_width;
  int max_screen_height;
998

999 1000 1001 1002
  combined_modes = combine_gpu_lists (manager, meta_gpu_get_modes);
  combined_outputs = combine_gpu_lists (manager, meta_gpu_get_outputs);
  combined_crtcs = combine_gpu_lists (manager, meta_gpu_get_crtcs);

1003
  g_variant_builder_init (&crtc_builder, G_VARIANT_TYPE ("a(uxiiiiiuaua{sv})"));
1004
  g_variant_builder_init (&output_builder, G_VARIANT_TYPE ("a(uxiausauaua{sv})"));
1005
  g_variant_builder_init (&mode_builder, G_VARIANT_TYPE ("a(uxuudu)"));
1006

1007
  for (l = combined_crtcs, i = 0; l; l = l->next, i++)
1008
    {
1009
      MetaCrtc *crtc = l->data;
1010
      GVariantBuilder transforms;
1011
      int current_mode_index;
1012 1013

      g_variant_builder_init (&transforms, G_VARIANT_TYPE ("au"));
1014
      for (j = 0; j <= META_MONITOR_TRANSFORM_FLIPPED_270; j++)
1015 1016
        if (crtc->all_transforms & (1 << j))
          g_variant_builder_add (&transforms, "u", j);
1017

1018
      if (crtc->current_mode)
1019
        current_mode_index = g_list_index (combined_modes, crtc->current_mode);
1020 1021
      else
        current_mode_index = -1;
1022 1023
      g_variant_builder_add (&crtc_builder, "(uxiiiiiuaua{sv})",
                             i, /* ID */
1024
                             (gint64)crtc->crtc_id,
1025 1026 1027 1028
                             (int)crtc->rect.x,
                             (int)crtc->rect.y,
                             (int)crtc->rect.width,
                             (int)crtc->rect.height,
1029
                             current_mode_index,
1030
                             (guint32)crtc->transform,
1031 1032 1033 1034
                             &transforms,
                             NULL /* properties */);
    }

1035
  for (l = combined_outputs; l; l = l->next)
1036
    {
Jonas Ådahl's avatar
Jonas Ådahl committed
1037
      MetaOutput *output = l->data;
1038
      GVariantBuilder crtcs, modes, clones, properties;
1039 1040
      GBytes *edid;
      char *edid_file;
1041
      int crtc_index;
1042 1043 1044

      g_variant_builder_init (&crtcs, G_VARIANT_TYPE ("au"));
      for (j = 0; j < output->n_possible_crtcs; j++)
1045 1046 1047 1048
        {
          MetaCrtc *possible_crtc = output->possible_crtcs[j];
          unsigned possible_crtc_index;

1049
          possible_crtc_index = g_list_index (combined_crtcs, possible_crtc);
1050 1051
          g_variant_builder_add (&crtcs, "u", possible_crtc_index);
        }
1052 1053 1054

      g_variant_builder_init (&modes, G_VARIANT_TYPE ("au"));
      for (j = 0; j < output->n_modes; j++)
1055 1056 1057
        {
          unsigned mode_index;

1058
          mode_index = g_list_index (combined_modes, output->modes[j]);
1059 1060
          g_variant_builder_add (&modes, "u", mode_index);
        }
1061

1062 1063
      g_variant_builder_init (&clones, G_VARIANT_TYPE ("au"));
      for (j = 0; j < output->n_possible_clones; j++)
Jonas Ådahl's avatar
Jonas Ådahl committed
1064 1065 1066
        {
          unsigned int possible_clone_index;

1067
          possible_clone_index = g_list_index (combined_outputs,
Jonas Ådahl's avatar
Jonas Ådahl committed
1068 1069 1070
                                               output->possible_clones[j]);
          g_variant_builder_add (&clones, "u", possible_clone_index);
        }
1071

1072 1073 1074 1075 1076 1077 1078
      g_variant_builder_init (&properties, G_VARIANT_TYPE ("a{sv}"));
      g_variant_builder_add (&properties, "{sv}", "vendor",
                             g_variant_new_string (output->vendor));
      g_variant_builder_add (&properties, "{sv}", "product",
                             g_variant_new_string (output->product));
      g_variant_builder_add (&properties, "{sv}", "serial",
                             g_variant_new_string (output->serial));
1079 1080 1081 1082
      g_variant_builder_add (&properties, "{sv}", "width-mm",
                             g_variant_new_int32 (output->width_mm));
      g_variant_builder_add (&properties, "{sv}", "height-mm",
                             g_variant_new_int32 (output->height_mm));
1083
      g_variant_builder_add (&properties, "{sv}", "display-name",
1084
                             g_variant_new_take_string (make_display_name (manager, output)));
1085 1086
      g_variant_builder_add (&properties, "{sv}", "backlight",
                             g_variant_new_int32 (output->backlight));
1087 1088 1089
      g_variant_builder_add (&properties, "{sv}", "min-backlight-step",
                             g_variant_new_int32 ((output->backlight_max - output->backlight_min) ?
                                                  100 / (output->backlight_max - output->backlight_min) : -1));
1090 1091 1092 1093
      g_variant_builder_add (&properties, "{sv}", "primary",
                             g_variant_new_boolean (output->is_primary));
      g_variant_builder_add (&properties, "{sv}", "presentation",
                             g_variant_new_boolean (output->is_presentation));
1094 1095
      g_variant_builder_add (&properties, "{sv}", "connector-type",
                             g_variant_new_string (get_connector_type_name (output->connector_type)));
1096 1097
      g_variant_builder_add (&properties, "{sv}", "underscanning",
                             g_variant_new_boolean (output->is_underscanning));
1098 1099
      g_variant_builder_add (&properties, "{sv}", "supports-underscanning",
                             g_variant_new_boolean (output->supports_underscanning));
1100

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
      edid_file = manager_class->get_edid_file (manager, output);
      if (edid_file)
        {
          g_variant_builder_add (&properties, "{sv}", "edid-file",
                                 g_variant_new_take_string (edid_file));
        }
      else
        {
          edid = manager_class->read_edid (manager, output);

          if (edid)
            {
              g_variant_builder_add (&properties, "{sv}", "edid",
                                     g_variant_new_from_bytes (G_VARIANT_TYPE ("ay"),
                                                               edid, TRUE));
              g_bytes_unref (edid);
            }
        }

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
      if (output->tile_info.group_id)
        {
          g_variant_builder_add (&properties, "{sv}", "tile",
                                 g_variant_new ("(uuuuuuuu)",
                                                output->tile_info.group_id,
                                                output->tile_info.flags,
                                                output->tile_info.max_h_tiles,
                                                output->tile_info.max_v_tiles,
                                                output->tile_info.loc_h_tile,
                                                output->tile_info.loc_v_tile,
                                                output->tile_info.tile_w,
                                                output->tile_info.tile_h));
        }

1134
      crtc_index = output->crtc ? g_list_index (combined_crtcs, output->crtc)
1135
                                : -1;
1136
      g_variant_builder_add (&output_builder, "(uxiausauaua{sv})",
1137
                             i, /* ID */
1138
                             (gint64)output->winsys_id,
1139
                             crtc_index,
1140 1141 1142
                             &crtcs,
                             output->name,
                             &modes,
1143
                             &clones,
1144 1145 1146
                             &properties);
    }

1147
  for (l = combined_modes, i = 0; l; l = l->next, i++)
1148
    {
1149
      MetaCrtcMode *mode = l->data;
1150

1151
      g_variant_builder_add (&mode_builder, "(uxuudu)",
1152
                             i, /* ID */
1153 1154 1155
                             (gint64)mode->mode_id,
                             (guint32)mode->width,
                             (guint32)mode->height,
1156 1157
                             (double)mode->refresh_rate,
                             (guint32)mode->flags);
1158 1159
    }

1160 1161 1162 1163 1164 1165 1166 1167 1168
  if (!meta_monitor_manager_get_max_screen_size (manager,
                                                 &max_screen_width,
                                                 &max_screen_height))
    {
      /* No max screen size, just send something large */
      max_screen_width = 65535;
      max_screen_height = 65535;
    }

1169 1170 1171 1172 1173
  meta_dbus_display_config_complete_get_resources (skeleton,
                                                   invocation,
                                                   manager->serial,
                                                   g_variant_builder_end (&crtc_builder),
                                                   g_variant_builder_end (&output_builder),
1174
                                                   g_variant_builder_end (&mode_builder),
1175 1176
                                                   max_screen_width,
                                                   max_screen_height);
1177 1178 1179 1180 1181

  g_list_free (combined_modes);
  g_list_free (combined_outputs);
  g_list_free (combined_crtcs);

1182 1183 1184
  return TRUE;
}

1185 1186 1187 1188 1189 1190 1191
static void
restore_previous_config (MetaMonitorManager *manager)
{
  MetaMonitorsConfig *previous_config;
  GError *error = NULL;

  previous_config =
1192
    meta_monitor_config_manager_pop_previous (manager->config_manager);
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

  if (previous_config)
    {
      MetaMonitorsConfigMethod method;

      method = META_MONITORS_CONFIG_METHOD_TEMPORARY;
      if (meta_monitor_manager_apply_monitors_config (manager,
                                                      previous_config,
                                                      method,
                                                      &error))
        {
          g_object_unref (previous_config);
          return;
        }
      else
        {
          g_object_unref (previous_config);
          g_warning ("Failed to restore previous configuration: %s",
                     error->message);
          g_error_free (error);
        }
    }

  meta_monitor_manager_ensure_configured (manager);
}

1219 1220 1221 1222 1223 1224
gint
meta_monitor_manager_get_display_configuration_timeout (void)
{
  return DEFAULT_DISPLAY_CONFIGURATION_TIMEOUT;
}

1225 1226 1227 1228 1229
static gboolean
save_config_timeout (gpointer user_data)
{
  MetaMonitorManager *manager = user_data;

1230
  restore_previous_config (manager);
1231
  manager->persistent_timeout_id = 0;
1232

1233 1234 1235
  return G_SOURCE_REMOVE;
}

1236 1237 1238 1239 1240 1241 1242 1243
static void
cancel_persistent_confirmation (MetaMonitorManager *manager)
{
  g_source_remove (manager->persistent_timeout_id);
  manager->persistent_timeout_id = 0;
}

static void
1244
request_persistent_confirmation (MetaMonitorManager *manager)
1245
{
1246
  manager->persistent_timeout_id = g_timeout_add_seconds (meta_monitor_manager_get_display_configuration_timeout (),
1247 1248 1249 1250 1251 1252 1253 1254
                                                          save_config_timeout,
                                                          manager);
  g_source_set_name_by_id (manager->persistent_timeout_id,
                           "[mutter] save_config_timeout");

  g_signal_emit (manager, signals[CONFIRM_DISPLAY_CHANGE], 0);
}

1255 1256 1257
#define META_DISPLAY_CONFIG_MODE_FLAGS_PREFERRED (1 << 0)
#define META_DISPLAY_CONFIG_MODE_FLAGS_CURRENT (1 << 1)

1258
#define MODE_FORMAT "(siiddada{sv})"
1259 1260 1261 1262 1263 1264
#define MODES_FORMAT "a" MODE_FORMAT
#define MONITOR_SPEC_FORMAT "(ssss)"
#define MONITOR_FORMAT "(" MONITOR_SPEC_FORMAT MODES_FORMAT "a{sv})"
#define MONITORS_FORMAT "a" MONITOR_FORMAT

#define LOGICAL_MONITOR_MONITORS_FORMAT "a" MONITOR_SPEC_FORMAT
1265
#define LOGICAL_MONITOR_FORMAT "(iidub" LOGICAL_MONITOR_MONITORS_FORMAT "a{sv})"
1266 1267 1268 1269 1270 1271 1272
#define LOGICAL_MONITORS_FORMAT "a" LOGICAL_MONITOR_FORMAT

static gboolean
meta_monitor_manager_handle_get_current_state (MetaDBusDisplayConfig *skeleton,
                                               GDBusMethodInvocation *invocation)
{
  MetaMonitorManager *manager = META_MONITOR_MANAGER (skeleton);
1273
  MetaSettings *settings = meta_backend_get_settings (manager->backend);
1274 1275
  GVariantBuilder monitors_builder;
  GVariantBuilder logical_monitors_builder;
1276
  GVariantBuilder properties_builder;
1277
  GList *l;
1278
  int i;
1279
  MetaMonitorManagerCapability capabilities;
1280
  int ui_scaling_factor;
1281
  int max_screen_width, max_screen_height;
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294

  g_variant_builder_init (&monitors_builder,
                          G_VARIANT_TYPE (MONITORS_FORMAT));
  g_variant_builder_init (&logical_monitors_builder,
                          G_VARIANT_TYPE (LOGICAL_MONITORS_FORMAT));

  for (l = manager->monitors; l; l = l->next)
    {
      MetaMonitor *monitor = l->data;
      MetaMonitorSpec *monitor_spec = meta_monitor_get_spec (monitor);
      MetaMonitorMode *current_mode;
      MetaMonitorMode *preferred_mode;
      GVariantBuilder modes_builder;
1295
      GVariantBuilder monitor_properties_builder;
1296
      GList *k;
1297
      gboolean is_builtin;
1298 1299
      MetaOutput *main_output;
      char *display_name;
1300 1301 1302 1303 1304 1305 1306 1307

      current_mode = meta_monitor_get_current_mode (monitor);
      preferred_mode = meta_monitor_get_preferred_mode (monitor);

      g_variant_builder_init (&modes_builder, G_VARIANT_TYPE (MODES_FORMAT));
      for (k = meta_monitor_get_modes (monitor); k; k = k->next)
        {
          MetaMonitorMode *monitor_mode = k->data;
1308
          GVariantBuilder supported_scales_builder;
1309 1310 1311
          const char *mode_id;
          int mode_width, mode_height;
          float refresh_rate;
1312 1313 1314
          float preferred_scale;
          float *supported_scales;
          int n_supported_scales;
1315
          GVariantBuilder mode_properties_builder;
1316
          MetaCrtcModeFlag mode_flags;
1317

1318 1319 1320 1321 1322
          mode_id = meta_monitor_mode_get_id (monitor_mode);
          meta_monitor_mode_get_resolution (monitor_mode,
                                            &mode_width, &mode_height);
          refresh_rate = meta_monitor_mode_get_refresh_rate (monitor_mode);

1323 1324 1325 1326
          preferred_scale =
            meta_monitor_manager_calculate_monitor_mode_scale (manager,
                                                               monitor,
                                                               monitor_mode);
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340

          g_variant_builder_init (&supported_scales_builder,
                                  G_VARIANT_TYPE ("ad"));
          supported_scales =
            meta_monitor_manager_calculate_supported_scales (manager,
                                                             manager->layout_mode,
                                                             monitor,
                                                             monitor_mode,
                                                             &n_supported_scales);
          for (i = 0; i < n_supported_scales; i++)
            g_variant_builder_add (&supported_scales_builder, "d",
                                   (double) supported_scales[i]);
          g_free (supported_scales);

1341 1342
          mode_flags = meta_monitor_mode_get_flags (monitor_mode);

1343 1344
          g_variant_builder_init (&mode_properties_builder,
                                  G_VARIANT_TYPE ("a{sv}"));
1345
          if (monitor_mode == current_mode)
1346 1347 1348
            g_variant_builder_add (&mode_properties_builder, "{sv}",
                                   "is-current",
                                   g_variant_new_boolean (TRUE));
1349
          if (monitor_mode == preferred_mode)
1350 1351 1352
            g_variant_builder_add (&mode_properties_builder, "{sv}",
                                   "is-preferred",
                                   g_variant_new_boolean (TRUE));
1353 1354 1355 1356
          if (mode_flags & META_CRTC_MODE_FLAG_INTERLACE)
            g_variant_builder_add (&mode_properties_builder, "{sv}",
                                   "is-interlaced",
                                   g_variant_new_boolean (TRUE));
1357 1358

          g_variant_builder_add (&modes_builder, MODE_FORMAT,
1359 1360 1361 1362
                                 mode_id,
                                 mode_width,
                                 mode_height,
                                 refresh_rate,
1363
                                 (double) preferred_scale,
1364
                                 &supported_scales_builder,
1365
                                 &mode_properties_builder);
1366 1367
        }

1368 1369
      g_variant_builder_init (&monitor_properties_builder,
                              G_VARIANT_TYPE ("a{sv}"));
1370 1371 1372 1373
      if (meta_monitor_supports_underscanning (monitor))
        {
          gboolean is_underscanning = meta_monitor_is_underscanning (monitor);

1374
          g_variant_builder_add (&monitor_properties_builder, "{sv}",
1375
                                 "is-underscanning",
1376 1377 1378
                                 g_variant_new_boolean (is_underscanning));
        }

1379 1380
      is_builtin = meta_monitor_is_laptop_panel (monitor);
      g_variant_builder_add (&monitor_properties_builder, "{sv}",
1381
                             "is-builtin",
1382 1383
                             g_variant_new_boolean (is_builtin));

1384 1385 1386 1387 1388 1389
      main_output = meta_monitor_get_main_output (monitor);
      display_name = make_display_name (manager, main_output);
      g_variant_builder_add (&monitor_properties_builder, "{sv}",
                             "display-name",
                             g_variant_new_take_string (display_name));

1390 1391 1392 1393 1394 1395
      g_variant_builder_add (&monitors_builder, MONITOR_FORMAT,
                             monitor_spec->connector,
                             monitor_spec->vendor,
                             monitor_spec->product,
                             monitor_spec->serial,
                             &modes_builder,
1396
                             &monitor_properties_builder);
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
    }

  for (l = manager->logical_monitors; l; l = l->next)
    {
      MetaLogicalMonitor *logical_monitor = l->data;
      GVariantBuilder logical_monitor_monitors_builder;
      GList *k;

      g_variant_builder_init (&logical_monitor_monitors_builder,
                              G_VARIANT_TYPE (LOGICAL_MONITOR_MONITORS_FORMAT));

      for (k = logical_monitor->monitors; k; k = k->next)
        {
          MetaMonitor *monitor = k->data;
          MetaMonitorSpec *monitor_spec = meta_monitor_get_spec (monitor);

          g_variant_builder_add (&logical_monitor_monitors_builder,
                                 MONITOR_SPEC_FORMAT,
                                 monitor_spec->connector,
                                 monitor_spec->vendor,
                                 monitor_spec->product,
                                 monitor_spec->serial);
        }

      g_variant_builder_add (&logical_monitors_builder,
                             LOGICAL_MONITOR_FORMAT,
                             logical_monitor->rect.x,
                             logical_monitor->rect.y,
1425
                             (double) logical_monitor->scale,
1426
                             logical_monitor->transform,
1427
                             logical_monitor->is_primary,
1428
                             &logical_monitor_monitors_builder,
1429 1430 1431
                             NULL);
    }

1432 1433 1434 1435 1436 1437 1438 1439 1440
  g_variant_builder_init (&properties_builder, G_VARIANT_TYPE ("a{sv}"));
  capabilities = meta_monitor_manager_get_capabilities (manager);
  if ((capabilities & META_MONITOR_MANAGER_CAPABILITY_MIRRORING) == 0)
    {
      g_variant_builder_add (&properties_builder, "{sv}",
                             "supports-mirroring",
                             g_variant_new_boolean (FALSE));
    }

1441 1442 1443
  g_variant_builder_add (&properties_builder, "{sv}",
                         "layout-mode",
                         g_variant_new_uint32 (manager->layout_mode));
1444 1445 1446
  if (capabilities & META_MONITOR_MANAGER_CAPABILITY_LAYOUT_MODE)
    {
      g_variant_builder_add (&properties_builder, "{sv}",
1447 1448
                             "supports-changing-layout-mode",
                             g_variant_new_boolean (TRUE));
1449 1450
    }

1451 1452 1453 1454 1455 1456 1457
  if (capabilities & META_MONITOR_MANAGER_CAPABILITY_GLOBAL_SCALE_REQUIRED)
    {
      g_variant_builder_add (&properties_builder, "{sv}",
                             "global-scale-required",
                             g_variant_new_boolean (TRUE));
    }

1458 1459 1460 1461 1462
  ui_scaling_factor = meta_settings_get_ui_scaling_factor (settings);
  g_variant_builder_add (&properties_builder, "{sv}",
                         "legacy-ui-scaling-factor",
                         g_variant_new_int32 (ui_scaling_factor));

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
  if (meta_monitor_manager_get_max_screen_size (manager,
                                                &max_screen_width,
                                                &max_screen_height))
    {
      GVariantBuilder max_screen_size_builder;

      g_variant_builder_init (&max_screen_size_builder,
                              G_VARIANT_TYPE ("(ii)"));
      g_variant_builder_add (&max_screen_size_builder, "i",
                             max_screen_width);
      g_variant_builder_add (&max_screen_size_builder, "i",
                             max_screen_height);

      g_variant_builder_add (&properties_builder, "{sv}",
1477
                             "max-screen-size",
1478 1479 1480
                             g_variant_builder_end (&max_screen_size_builder));
    }

1481 1482 1483 1484 1485 1486
  meta_dbus_display_config_complete_get_current_state (
    skeleton,
    invocation,
    manager->serial,
    g_variant_builder_end (&monitors_builder),
    g_variant_builder_end (&logical_monitors_builder),
1487
    g_variant_builder_end (&properties_builder));
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500

  return TRUE;
}

#undef MODE_FORMAT
#undef MODES_FORMAT
#undef MONITOR_SPEC_FORMAT
#undef MONITOR_FORMAT
#undef MONITORS_FORMAT
#undef LOGICAL_MONITOR_MONITORS_FORMAT
#undef LOGICAL_MONITOR_FORMAT
#undef LOGICAL_MONITORS_FORMAT

1501 1502 1503
gboolean
meta_monitor_manager_is_scale_supported (MetaMonitorManager          *manager,
                                         MetaLogicalMonitorLayoutMode layout_mode,
1504 1505
                                         MetaMonitor                 *monitor,
                                         MetaMonitorMode             *monitor_mode,
1506
                                         float                        scale)
1507
{
1508
  g_autofree float *supported_scales = NULL;
1509 1510 1511
  int n_supported_scales;
  int i;