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

/*
 * Copyright (C) 2016 Red Hat
5
 * Copyright (c) 2018 DisplayLink (UK) Ltd.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * 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/meta-monitor-config-manager.h"

27
#include "backends/meta-backend-private.h"
28
#include "backends/meta-monitor-config-migration.h"
29
#include "backends/meta-monitor-config-store.h"
30
#include "backends/meta-monitor-manager-private.h"
Jonas Ådahl's avatar
Jonas Ådahl committed
31
#include "backends/meta-output.h"
32
#include "core/boxes-private.h"
33

34
35
#define CONFIG_HISTORY_MAX_SIZE 3

36
37
38
39
40
41
struct _MetaMonitorConfigManager
{
  GObject parent;

  MetaMonitorManager *monitor_manager;

42
43
  MetaMonitorConfigStore *config_store;

44
  MetaMonitorsConfig *current_config;
45
  GQueue config_history;
46
47
48
49
50
51
52
53
};

G_DEFINE_TYPE (MetaMonitorConfigManager, meta_monitor_config_manager,
               G_TYPE_OBJECT)

G_DEFINE_TYPE (MetaMonitorsConfig, meta_monitors_config,
               G_TYPE_OBJECT)

54
55
56
57
58
59
static void
meta_crtc_info_free (MetaCrtcInfo *info);

static void
meta_output_info_free (MetaOutputInfo *info);

60
61
62
63
64
65
66
MetaMonitorConfigManager *
meta_monitor_config_manager_new (MetaMonitorManager *monitor_manager)
{
  MetaMonitorConfigManager *config_manager;

  config_manager = g_object_new (META_TYPE_MONITOR_CONFIG_MANAGER, NULL);
  config_manager->monitor_manager = monitor_manager;
67
68
  config_manager->config_store =
    meta_monitor_config_store_new (monitor_manager);
69
70
71
72

  return config_manager;
}

73
74
75
76
77
78
MetaMonitorConfigStore *
meta_monitor_config_manager_get_store (MetaMonitorConfigManager *config_manager)
{
  return config_manager->config_store;
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
static gboolean
is_crtc_reserved (MetaCrtc *crtc,
                  GArray   *reserved_crtcs)
{
  unsigned int i;

  for (i = 0; i < reserved_crtcs->len; i++)
    {
       glong id = g_array_index (reserved_crtcs, glong, i);
       if (id == crtc->crtc_id)
         return TRUE;
    }

  return FALSE;
}

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
static gboolean
is_crtc_assigned (MetaCrtc  *crtc,
                  GPtrArray *crtc_infos)
{
  unsigned int i;

  for (i = 0; i < crtc_infos->len; i++)
    {
      MetaCrtcInfo *assigned_crtc_info = g_ptr_array_index (crtc_infos, i);

      if (assigned_crtc_info->crtc == crtc)
        return TRUE;
    }

  return FALSE;
}

static MetaCrtc *
find_unassigned_crtc (MetaOutput *output,
114
115
                      GPtrArray  *crtc_infos,
                      GArray     *reserved_crtcs)
116
{
117
  MetaCrtc *crtc;
118
119
  unsigned int i;

120
121
122
123
  crtc = meta_output_get_assigned_crtc (output);
  if (crtc && !is_crtc_assigned (crtc, crtc_infos))
    return crtc;

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  /* then try to assign a CRTC that wasn't used */
  for (i = 0; i < output->n_possible_crtcs; i++)
    {
      crtc = output->possible_crtcs[i];

      if (is_crtc_assigned (crtc, crtc_infos))
        continue;

      if (is_crtc_reserved (crtc, reserved_crtcs))
        continue;

      return crtc;
    }

  /* finally just give a CRTC that we haven't assigned */
139
140
  for (i = 0; i < output->n_possible_crtcs; i++)
    {
141
      crtc = output->possible_crtcs[i];
142
143
144
145
146
147
148
149
150
151
152
153

      if (is_crtc_assigned (crtc, crtc_infos))
        continue;

      return crtc;
    }

  return NULL;
}

typedef struct
{
154
  MetaMonitorManager *monitor_manager;
155
  MetaMonitorsConfig *config;
156
157
158
159
  MetaLogicalMonitorConfig *logical_monitor_config;
  MetaMonitorConfig *monitor_config;
  GPtrArray *crtc_infos;
  GPtrArray *output_infos;
160
  GArray *reserved_crtcs;
161
162
163
164
165
166
167
168
169
170
171
172
} MonitorAssignmentData;

static gboolean
assign_monitor_crtc (MetaMonitor         *monitor,
                     MetaMonitorMode     *mode,
                     MetaMonitorCrtcMode *monitor_crtc_mode,
                     gpointer             user_data,
                     GError             **error)
{
  MonitorAssignmentData *data = user_data;
  MetaOutput *output;
  MetaCrtc *crtc;
173
174
  MetaMonitorTransform transform;
  MetaMonitorTransform crtc_transform;
175
  MetaMonitorTransform crtc_hw_transform;
176
  int crtc_x, crtc_y;
177
  float x_offset, y_offset;
178
  float scale = 0.0;
179
180
181
  float width, height;
  MetaCrtcMode *crtc_mode;
  graphene_rect_t crtc_layout;
182
183
184
185
186
187
188
189
  MetaCrtcInfo *crtc_info;
  MetaOutputInfo *output_info;
  MetaMonitorConfig *first_monitor_config;
  gboolean assign_output_as_primary;
  gboolean assign_output_as_presentation;

  output = monitor_crtc_mode->output;

190
191
  crtc = find_unassigned_crtc (output, data->crtc_infos, data->reserved_crtcs);

192
193
194
195
196
197
198
199
200
201
  if (!crtc)
    {
      MetaMonitorSpec *monitor_spec = meta_monitor_get_spec (monitor);

      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "No available CRTC for monitor '%s %s' not found",
                   monitor_spec->vendor, monitor_spec->product);
      return FALSE;
    }

202
  transform = data->logical_monitor_config->transform;
203
  crtc_transform = meta_monitor_logical_to_crtc_transform (monitor, transform);
204
205
206
207
208
209
  if (meta_monitor_manager_is_transform_handled (data->monitor_manager,
                                                 crtc,
                                                 crtc_transform))
    crtc_hw_transform = crtc_transform;
  else
    crtc_hw_transform = META_MONITOR_TRANSFORM_NORMAL;
210
211

  meta_monitor_calculate_crtc_pos (monitor, mode, output, crtc_transform,
212
213
                                   &crtc_x, &crtc_y);

214
215
  x_offset = data->logical_monitor_config->layout.x;
  y_offset = data->logical_monitor_config->layout.y;
216
217
218
219
220
221
222
223
224
225
226

  switch (data->config->layout_mode)
    {
    case META_LOGICAL_MONITOR_LAYOUT_MODE_LOGICAL:
      scale = data->logical_monitor_config->scale;
      break;
    case META_LOGICAL_MONITOR_LAYOUT_MODE_PHYSICAL:
      scale = 1.0;
      break;
    }

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  crtc_mode = monitor_crtc_mode->crtc_mode;

  if (meta_monitor_transform_is_rotated (crtc_transform))
    {
      width = crtc_mode->height / scale;
      height = crtc_mode->width / scale;
    }
  else
    {
      width = crtc_mode->width / scale;
      height = crtc_mode->height / scale;
    }

  crtc_layout = GRAPHENE_RECT_INIT (x_offset + (crtc_x / scale),
                                    y_offset + (crtc_y / scale),
                                    width,
                                    height);

245
246
247
  crtc_info = g_slice_new0 (MetaCrtcInfo);
  *crtc_info = (MetaCrtcInfo) {
    .crtc = crtc,
248
249
    .mode = crtc_mode,
    .layout = crtc_layout,
250
    .transform = crtc_hw_transform,
251
252
253
254
255
256
257
258
259
260
    .outputs = g_ptr_array_new ()
  };
  g_ptr_array_add (crtc_info->outputs, output);

  /*
   * Only one output can be marked as primary (due to Xrandr limitation),
   * so only mark the main output of the first monitor in the logical monitor
   * as such.
   */
  first_monitor_config = data->logical_monitor_config->monitor_configs->data;
261
262
  if (data->logical_monitor_config->is_primary &&
      data->monitor_config == first_monitor_config &&
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
      meta_monitor_get_main_output (monitor) == output)
    assign_output_as_primary = TRUE;
  else
    assign_output_as_primary = FALSE;

  if (data->logical_monitor_config->is_presentation)
    assign_output_as_presentation = TRUE;
  else
    assign_output_as_presentation = FALSE;

  output_info = g_slice_new0 (MetaOutputInfo);
  *output_info = (MetaOutputInfo) {
    .output = output,
    .is_primary = assign_output_as_primary,
    .is_presentation = assign_output_as_presentation,
278
    .is_underscanning = data->monitor_config->enable_underscanning
279
280
281
282
283
284
285
286
287
288
  };

  g_ptr_array_add (data->crtc_infos, crtc_info);
  g_ptr_array_add (data->output_infos, output_info);

  return TRUE;
}

static gboolean
assign_monitor_crtcs (MetaMonitorManager       *manager,
289
                      MetaMonitorsConfig       *config,
290
291
292
293
                      MetaLogicalMonitorConfig *logical_monitor_config,
                      MetaMonitorConfig        *monitor_config,
                      GPtrArray                *crtc_infos,
                      GPtrArray                *output_infos,
294
                      GArray                   *reserved_crtcs,
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
                      GError                  **error)
{
  MetaMonitorSpec *monitor_spec = monitor_config->monitor_spec;
  MetaMonitorModeSpec *monitor_mode_spec = monitor_config->mode_spec;
  MetaMonitor *monitor;
  MetaMonitorMode *monitor_mode;
  MonitorAssignmentData data;

  monitor = meta_monitor_manager_get_monitor_from_spec (manager, monitor_spec);
  if (!monitor)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Configured monitor '%s %s' not found",
                   monitor_spec->vendor, monitor_spec->product);
      return FALSE;
    }

  monitor_mode = meta_monitor_get_mode_from_spec (monitor, monitor_mode_spec);
  if (!monitor_mode)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Invalid mode %dx%d (%f) for monitor '%s %s'",
                   monitor_mode_spec->width, monitor_mode_spec->height,
                   monitor_mode_spec->refresh_rate,
                   monitor_spec->vendor, monitor_spec->product);
      return FALSE;
    }

  data = (MonitorAssignmentData) {
324
    .monitor_manager = manager,
325
    .config = config,
326
327
328
    .logical_monitor_config = logical_monitor_config,
    .monitor_config = monitor_config,
    .crtc_infos = crtc_infos,
329
330
    .output_infos = output_infos,
    .reserved_crtcs = reserved_crtcs
331
332
333
334
335
336
337
338
339
340
341
342
  };
  if (!meta_monitor_mode_foreach_crtc (monitor, monitor_mode,
                                       assign_monitor_crtc,
                                       &data,
                                       error))
    return FALSE;

  return TRUE;
}

static gboolean
assign_logical_monitor_crtcs (MetaMonitorManager       *manager,
343
                              MetaMonitorsConfig       *config,
344
345
346
                              MetaLogicalMonitorConfig *logical_monitor_config,
                              GPtrArray                *crtc_infos,
                              GPtrArray                *output_infos,
347
                              GArray                   *reserved_crtcs,
348
349
350
351
352
353
354
355
356
                              GError                  **error)
{
  GList *l;

  for (l = logical_monitor_config->monitor_configs; l; l = l->next)
    {
      MetaMonitorConfig *monitor_config = l->data;

      if (!assign_monitor_crtcs (manager,
357
                                 config,
358
359
360
                                 logical_monitor_config,
                                 monitor_config,
                                 crtc_infos, output_infos,
361
                                 reserved_crtcs, error))
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
        return FALSE;
    }

  return TRUE;
}

gboolean
meta_monitor_config_manager_assign (MetaMonitorManager *manager,
                                    MetaMonitorsConfig *config,
                                    GPtrArray         **out_crtc_infos,
                                    GPtrArray         **out_output_infos,
                                    GError            **error)
{
  GPtrArray *crtc_infos;
  GPtrArray *output_infos;
377
  GArray *reserved_crtcs;
378
379
380
381
382
383
  GList *l;

  crtc_infos =
    g_ptr_array_new_with_free_func ((GDestroyNotify) meta_crtc_info_free);
  output_infos =
    g_ptr_array_new_with_free_func ((GDestroyNotify) meta_output_info_free);
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
  reserved_crtcs = g_array_new (FALSE, FALSE, sizeof (glong));

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

      for (k = logical_monitor_config->monitor_configs; k; k = k->next)
        {
          MetaMonitorConfig *monitor_config = k->data;
          MetaMonitorSpec *monitor_spec = monitor_config->monitor_spec;
          MetaMonitor *monitor;
          GList *o;

          monitor = meta_monitor_manager_get_monitor_from_spec (manager, monitor_spec);

          for (o = meta_monitor_get_outputs (monitor); o; o = o->next)
            {
              MetaOutput *output = o->data;
              MetaCrtc *crtc;

              crtc = meta_output_get_assigned_crtc (output);
              if (crtc)
                g_array_append_val (reserved_crtcs, crtc->crtc_id);
            }
        }
    }
411
412
413
414
415

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

416
417
      if (!assign_logical_monitor_crtcs (manager,
                                         config, logical_monitor_config,
418
                                         crtc_infos, output_infos,
419
                                         reserved_crtcs, error))
420
421
422
        {
          g_ptr_array_free (crtc_infos, TRUE);
          g_ptr_array_free (output_infos, TRUE);
423
          g_array_free (reserved_crtcs, TRUE);
424
425
426
427
          return FALSE;
        }
    }

428
429
  g_array_free (reserved_crtcs, TRUE);

430
431
432
433
434
435
  *out_crtc_infos = crtc_infos;
  *out_output_infos = output_infos;

  return TRUE;
}

436
437
438
439
440
441
442
443
444
static gboolean
is_lid_closed (MetaMonitorManager *monitor_manager)
{
    MetaBackend *backend;

    backend = meta_monitor_manager_get_backend (monitor_manager);
    return meta_backend_is_lid_closed (backend);
}

445
446
MetaMonitorsConfigKey *
meta_create_monitors_config_key_for_current_state (MetaMonitorManager *monitor_manager)
447
448
449
450
451
452
453
454
455
456
457
458
{
  MetaMonitorsConfigKey *config_key;
  GList *l;
  GList *monitor_specs;

  monitor_specs = NULL;
  for (l = monitor_manager->monitors; l; l = l->next)
    {
      MetaMonitor *monitor = l->data;
      MetaMonitorSpec *monitor_spec;

      if (meta_monitor_is_laptop_panel (monitor) &&
459
          is_lid_closed (monitor_manager))
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
        continue;

      monitor_spec = meta_monitor_spec_clone (meta_monitor_get_spec (monitor));
      monitor_specs = g_list_prepend (monitor_specs, monitor_spec);
    }

  if (!monitor_specs)
    return NULL;

  monitor_specs = g_list_sort (monitor_specs,
                               (GCompareFunc) meta_monitor_spec_compare);

  config_key = g_new0 (MetaMonitorsConfigKey, 1);
  *config_key = (MetaMonitorsConfigKey) {
    .monitor_specs = monitor_specs
  };

  return config_key;
}

MetaMonitorsConfig *
meta_monitor_config_manager_get_stored (MetaMonitorConfigManager *config_manager)
{
483
  MetaMonitorManager *monitor_manager = config_manager->monitor_manager;
484
485
  MetaMonitorsConfigKey *config_key;
  MetaMonitorsConfig *config;
486
  GError *error = NULL;
487

488
489
  config_key =
    meta_create_monitors_config_key_for_current_state (monitor_manager);
490
491
492
493
494
495
496
  if (!config_key)
    return NULL;

  config = meta_monitor_config_store_lookup (config_manager->config_store,
                                             config_key);
  meta_monitors_config_key_free (config_key);

497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
  if (!config)
    return NULL;

  if (config->flags & META_MONITORS_CONFIG_FLAG_MIGRATED)
    {
      if (!meta_finish_monitors_config_migration (monitor_manager, config,
                                                  &error))
        {
          g_warning ("Failed to finish monitors config migration: %s",
                     error->message);
          g_error_free (error);
          meta_monitor_config_store_remove (config_manager->config_store, config);
          return NULL;
        }
    }

513
514
515
  return config;
}

516
517
518
519
520
521
typedef enum _MonitorMatchRule
{
  MONITOR_MATCH_ALL = 0,
  MONITOR_MATCH_EXTERNAL = (1 << 0)
} MonitorMatchRule;

522
static MetaMonitor *
523
524
find_monitor_with_highest_preferred_resolution (MetaMonitorManager *monitor_manager,
                                                MonitorMatchRule    match_rule)
525
526
527
528
529
530
531
532
533
534
535
536
537
538
{
  GList *monitors;
  GList *l;
  int largest_area = 0;
  MetaMonitor *largest_monitor = NULL;

  monitors = meta_monitor_manager_get_monitors (monitor_manager);
  for (l = monitors; l; l = l->next)
    {
      MetaMonitor *monitor = l->data;
      MetaMonitorMode *mode;
      int width, height;
      int area;

539
540
541
542
543
544
      if (match_rule & MONITOR_MATCH_EXTERNAL)
        {
          if (meta_monitor_is_laptop_panel (monitor))
            continue;
        }

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
      mode = meta_monitor_get_preferred_mode (monitor);
      meta_monitor_mode_get_resolution (mode, &width, &height);
      area = width * height;

      if (area > largest_area)
        {
          largest_area = area;
          largest_monitor = monitor;
        }
    }

  return largest_monitor;
}

/*
560
561
562
563
564
565
566
567
 * Try to find the primary monitor. The priority of classification is:
 *
 * 1. Find the primary monitor as reported by the underlying system,
 * 2. Find the laptop panel
 * 3. Find the external monitor with highest resolution
 *
 * If the laptop lid is closed, exclude the laptop panel from possible
 * alternatives, except if no other alternatives exist.
568
569
570
571
572
573
 */
static MetaMonitor *
find_primary_monitor (MetaMonitorManager *monitor_manager)
{
  MetaMonitor *monitor;

574
  if (is_lid_closed (monitor_manager))
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
    {
      monitor = meta_monitor_manager_get_primary_monitor (monitor_manager);
      if (monitor && !meta_monitor_is_laptop_panel (monitor))
        return monitor;

      monitor =
        find_monitor_with_highest_preferred_resolution (monitor_manager,
                                                        MONITOR_MATCH_EXTERNAL);
      if (monitor)
        return monitor;

      return find_monitor_with_highest_preferred_resolution (monitor_manager,
                                                             MONITOR_MATCH_ALL);
    }
  else
    {
      monitor = meta_monitor_manager_get_primary_monitor (monitor_manager);
      if (monitor)
        return monitor;
594

595
596
597
      monitor = meta_monitor_manager_get_laptop_panel (monitor_manager);
      if (monitor)
        return monitor;
598

599
600
601
      return find_monitor_with_highest_preferred_resolution (monitor_manager,
                                                             MONITOR_MATCH_ALL);
    }
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
}

static MetaMonitorConfig *
create_monitor_config (MetaMonitor     *monitor,
                       MetaMonitorMode *mode)
{
  MetaMonitorSpec *monitor_spec;
  MetaMonitorModeSpec *mode_spec;
  MetaMonitorConfig *monitor_config;

  monitor_spec = meta_monitor_get_spec (monitor);
  mode_spec = meta_monitor_mode_get_spec (mode);

  monitor_config = g_new0 (MetaMonitorConfig, 1);
  *monitor_config = (MetaMonitorConfig) {
    .monitor_spec = meta_monitor_spec_clone (monitor_spec),
618
    .mode_spec = g_memdup (mode_spec, sizeof (MetaMonitorModeSpec)),
619
    .enable_underscanning = meta_monitor_is_underscanning (monitor)
620
621
622
623
624
  };

  return monitor_config;
}

625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
static MetaMonitorTransform
get_monitor_transform (MetaMonitorManager *monitor_manager,
                       MetaMonitor        *monitor)
{
  MetaOrientationManager *orientation_manager;
  MetaBackend *backend;

  if (!meta_monitor_is_laptop_panel (monitor))
    return META_MONITOR_TRANSFORM_NORMAL;

  backend = meta_monitor_manager_get_backend (monitor_manager);
  orientation_manager = meta_backend_get_orientation_manager (backend);

  switch (meta_orientation_manager_get_orientation (orientation_manager))
    {
    case META_ORIENTATION_BOTTOM_UP:
      return META_MONITOR_TRANSFORM_180;
    case META_ORIENTATION_LEFT_UP:
      return META_MONITOR_TRANSFORM_90;
    case META_ORIENTATION_RIGHT_UP:
      return META_MONITOR_TRANSFORM_270;
    case META_ORIENTATION_UNDEFINED:
    case META_ORIENTATION_NORMAL:
    default:
      return META_MONITOR_TRANSFORM_NORMAL;
    }
}

653
static MetaLogicalMonitorConfig *
654
655
656
657
create_preferred_logical_monitor_config (MetaMonitorManager          *monitor_manager,
                                         MetaMonitor                 *monitor,
                                         int                          x,
                                         int                          y,
658
                                         MetaLogicalMonitorConfig    *primary_logical_monitor_config,
659
                                         MetaLogicalMonitorLayoutMode layout_mode)
660
661
662
{
  MetaMonitorMode *mode;
  int width, height;
663
  float scale;
664
  MetaMonitorTransform transform;
665
666
667
668
669
  MetaMonitorConfig *monitor_config;
  MetaLogicalMonitorConfig *logical_monitor_config;

  mode = meta_monitor_get_preferred_mode (monitor);
  meta_monitor_mode_get_resolution (mode, &width, &height);
670
671
672
673
674
675
676
677
678

  if ((meta_monitor_manager_get_capabilities (monitor_manager) &
       META_MONITOR_MANAGER_CAPABILITY_GLOBAL_SCALE_REQUIRED) &&
      primary_logical_monitor_config)
    scale = primary_logical_monitor_config->scale;
  else
    scale = meta_monitor_manager_calculate_monitor_mode_scale (monitor_manager,
                                                               monitor,
                                                               mode);
679

680
681
682
  switch (layout_mode)
    {
    case META_LOGICAL_MONITOR_LAYOUT_MODE_LOGICAL:
683
684
      width = (int) roundf (width / scale);
      height = (int) roundf (height / scale);
685
686
687
688
689
      break;
    case META_LOGICAL_MONITOR_LAYOUT_MODE_PHYSICAL:
      break;
    }

690
691
  monitor_config = create_monitor_config (monitor, mode);

692
693
694
695
696
697
698
699
  transform = get_monitor_transform (monitor_manager, monitor);
  if (meta_monitor_transform_is_rotated (transform))
    {
      int temp = width;
      width = height;
      height = temp;
    }

700
701
702
703
704
705
706
707
  logical_monitor_config = g_new0 (MetaLogicalMonitorConfig, 1);
  *logical_monitor_config = (MetaLogicalMonitorConfig) {
    .layout = (MetaRectangle) {
      .x = x,
      .y = y,
      .width = width,
      .height = height
    },
708
    .transform = transform,
709
    .scale = scale,
710
711
712
713
714
715
716
717
718
719
    .monitor_configs = g_list_append (NULL, monitor_config)
  };

  return logical_monitor_config;
}

MetaMonitorsConfig *
meta_monitor_config_manager_create_linear (MetaMonitorConfigManager *config_manager)
{
  MetaMonitorManager *monitor_manager = config_manager->monitor_manager;
720
  GList *logical_monitor_configs;
721
  MetaMonitor *primary_monitor;
722
  MetaLogicalMonitorLayoutMode layout_mode;
723
724
725
726
727
728
  MetaLogicalMonitorConfig *primary_logical_monitor_config;
  int x;
  GList *monitors;
  GList *l;

  primary_monitor = find_primary_monitor (monitor_manager);
729
730
  if (!primary_monitor)
    return NULL;
731

732
733
  layout_mode = meta_monitor_manager_get_default_layout_mode (monitor_manager);

734
  primary_logical_monitor_config =
735
736
    create_preferred_logical_monitor_config (monitor_manager,
                                             primary_monitor,
737
                                             0, 0,
738
                                             NULL,
739
                                             layout_mode);
740
  primary_logical_monitor_config->is_primary = TRUE;
741
742
  logical_monitor_configs = g_list_append (NULL,
                                           primary_logical_monitor_config);
743
744
745
746
747
748
749
750
751
752
753

  x = primary_logical_monitor_config->layout.width;
  monitors = meta_monitor_manager_get_monitors (monitor_manager);
  for (l = monitors; l; l = l->next)
    {
      MetaMonitor *monitor = l->data;
      MetaLogicalMonitorConfig *logical_monitor_config;

      if (monitor == primary_monitor)
        continue;

754
      if (meta_monitor_is_laptop_panel (monitor) &&
755
          is_lid_closed (monitor_manager))
756
757
        continue;

758
      logical_monitor_config =
759
760
        create_preferred_logical_monitor_config (monitor_manager,
                                                 monitor,
761
                                                 x, 0,
762
                                                 primary_logical_monitor_config,
763
                                                 layout_mode);
764
765
      logical_monitor_configs = g_list_append (logical_monitor_configs,
                                               logical_monitor_config);
766
767
768
769

      x += logical_monitor_config->layout.width;
    }

770
771
772
  return meta_monitors_config_new (monitor_manager,
                                   logical_monitor_configs,
                                   layout_mode,
773
                                   META_MONITORS_CONFIG_FLAG_NONE);
774
775
776
777
778
779
780
}

MetaMonitorsConfig *
meta_monitor_config_manager_create_fallback (MetaMonitorConfigManager *config_manager)
{
  MetaMonitorManager *monitor_manager = config_manager->monitor_manager;
  MetaMonitor *primary_monitor;
781
  GList *logical_monitor_configs;
782
  MetaLogicalMonitorLayoutMode layout_mode;
783
784
785
  MetaLogicalMonitorConfig *primary_logical_monitor_config;

  primary_monitor = find_primary_monitor (monitor_manager);
786
787
  if (!primary_monitor)
    return NULL;
788

789
790
  layout_mode = meta_monitor_manager_get_default_layout_mode (monitor_manager);

791
  primary_logical_monitor_config =
792
793
    create_preferred_logical_monitor_config (monitor_manager,
                                             primary_monitor,
794
                                             0, 0,
795
                                             NULL,
796
                                             layout_mode);
797
  primary_logical_monitor_config->is_primary = TRUE;
798
799
  logical_monitor_configs = g_list_append (NULL,
                                           primary_logical_monitor_config);
800

801
802
803
  return meta_monitors_config_new (monitor_manager,
                                   logical_monitor_configs,
                                   layout_mode,
804
                                   META_MONITORS_CONFIG_FLAG_NONE);
805
806
}

807
808
809
810
811
812
MetaMonitorsConfig *
meta_monitor_config_manager_create_suggested (MetaMonitorConfigManager *config_manager)
{
  MetaMonitorManager *monitor_manager = config_manager->monitor_manager;
  MetaLogicalMonitorConfig *primary_logical_monitor_config = NULL;
  MetaMonitor *primary_monitor;
813
  MetaLogicalMonitorLayoutMode layout_mode;
814
815
  GList *logical_monitor_configs;
  GList *region;
816
  int x, y;
817
818
819
820
821
822
823
  GList *monitors;
  GList *l;

  primary_monitor = find_primary_monitor (monitor_manager);
  if (!primary_monitor)
    return NULL;

824
825
826
  if (!meta_monitor_get_suggested_position (primary_monitor, &x, &y))
    return NULL;

827
828
  layout_mode = meta_monitor_manager_get_default_layout_mode (monitor_manager);

829
830
831
832
833
834
835
836
837
838
839
  primary_logical_monitor_config =
    create_preferred_logical_monitor_config (monitor_manager,
                                             primary_monitor,
                                             x, y,
                                             NULL,
                                             layout_mode);
  primary_logical_monitor_config->is_primary = TRUE;
  logical_monitor_configs = g_list_append (NULL,
                                           primary_logical_monitor_config);
  region = g_list_prepend (NULL, &primary_logical_monitor_config->layout);

840
841
842
843
844
  monitors = meta_monitor_manager_get_monitors (monitor_manager);
  for (l = monitors; l; l = l->next)
    {
      MetaMonitor *monitor = l->data;
      MetaLogicalMonitorConfig *logical_monitor_config;
845
846
847

      if (monitor == primary_monitor)
        continue;
848
849
850
851
852

      if (!meta_monitor_get_suggested_position (monitor, &x, &y))
        continue;

      logical_monitor_config =
853
854
        create_preferred_logical_monitor_config (monitor_manager,
                                                 monitor,
855
                                                 x, y,
856
                                                 primary_logical_monitor_config,
857
                                                 layout_mode);
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
      logical_monitor_configs = g_list_append (logical_monitor_configs,
                                               logical_monitor_config);

      if (meta_rectangle_overlaps_with_region (region,
                                               &logical_monitor_config->layout))
        {
          g_warning ("Suggested monitor config has overlapping region, rejecting");
          g_list_free (region);
          g_list_free_full (logical_monitor_configs,
                            (GDestroyNotify) meta_logical_monitor_config_free);
          return NULL;
        }

      region = g_list_prepend (region, &logical_monitor_config->layout);
    }

  g_list_free (region);

  if (!logical_monitor_configs)
    return NULL;

879
880
881
  return meta_monitors_config_new (monitor_manager,
                                   logical_monitor_configs,
                                   layout_mode,
882
                                   META_MONITORS_CONFIG_FLAG_NONE);
883
884
}

885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
static GList *
clone_monitor_config_list (GList *monitor_configs_in)
{
  MetaMonitorConfig *monitor_config_in;
  MetaMonitorConfig *monitor_config_out;
  GList *monitor_configs_out = NULL;
  GList *l;

  for (l = monitor_configs_in; l; l = l->next)
    {
      monitor_config_in = l->data;
      monitor_config_out = g_new0 (MetaMonitorConfig, 1);
      *monitor_config_out = (MetaMonitorConfig) {
        .monitor_spec = meta_monitor_spec_clone (monitor_config_in->monitor_spec),
        .mode_spec = g_memdup (monitor_config_in->mode_spec,
                               sizeof (MetaMonitorModeSpec)),
        .enable_underscanning = monitor_config_in->enable_underscanning
      };
      monitor_configs_out =
        g_list_append (monitor_configs_out, monitor_config_out);
    }

  return monitor_configs_out;
}

static GList *
clone_logical_monitor_config_list (GList *logical_monitor_configs_in)
{
  MetaLogicalMonitorConfig *logical_monitor_config_in;
  MetaLogicalMonitorConfig *logical_monitor_config_out;
  GList *logical_monitor_configs_out = NULL;
  GList *l;

  for (l = logical_monitor_configs_in; l; l = l->next)
    {
      logical_monitor_config_in = l->data;

      logical_monitor_config_out =
        g_memdup (logical_monitor_config_in, sizeof (MetaLogicalMonitorConfig));
      logical_monitor_config_out->monitor_configs =
        clone_monitor_config_list (logical_monitor_config_in->monitor_configs);

      logical_monitor_configs_out =
        g_list_append (logical_monitor_configs_out, logical_monitor_config_out);
    }

  return logical_monitor_configs_out;
}

934
935
936
937
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
static MetaLogicalMonitorConfig *
find_logical_config_for_builtin_display_rotation (MetaMonitorConfigManager *config_manager,
                                                  GList                    *logical_monitor_configs)
{
  MetaLogicalMonitorConfig *logical_monitor_config;
  MetaMonitorConfig *monitor_config;
  MetaMonitor *panel;
  GList *l;

  panel = meta_monitor_manager_get_laptop_panel (config_manager->monitor_manager);
  if (panel && meta_monitor_is_active (panel))
    {
      for (l = logical_monitor_configs; l; l = l->next)
        {
          logical_monitor_config = l->data;
          /*
           * We only want to return the config for the panel if it is
           * configured on its own, so we skip configs which contain clones.
           */
          if (g_list_length (logical_monitor_config->monitor_configs) != 1)
            continue;

          monitor_config = logical_monitor_config->monitor_configs->data;
          if (meta_monitor_spec_equals (meta_monitor_get_spec (panel),
                                        monitor_config->monitor_spec))
            return logical_monitor_config;
        }
    }

  return NULL;
}

966
967
968
969
970
static MetaMonitorsConfig *
create_for_builtin_display_rotation (MetaMonitorConfigManager *config_manager,
                                     gboolean                  rotate,
                                     MetaMonitorTransform      transform)
{
971
  MetaMonitorManager *monitor_manager = config_manager->monitor_manager;
972
973
  MetaLogicalMonitorConfig *logical_monitor_config;
  MetaLogicalMonitorConfig *current_logical_monitor_config;
974
  GList *logical_monitor_configs, *current_configs;
975
  MetaLogicalMonitorLayoutMode layout_mode;
976
977
978
979

  if (!config_manager->current_config)
    return NULL;

980
981
982
983
984
  current_configs = config_manager->current_config->logical_monitor_configs;
  current_logical_monitor_config =
    find_logical_config_for_builtin_display_rotation (config_manager,
                                                      current_configs);
  if (!current_logical_monitor_config)
985
986
987
988
    return NULL;

  if (rotate)
    transform = (current_logical_monitor_config->transform + 1) % META_MONITOR_TRANSFORM_FLIPPED;
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
  else
    {
      /*
       * The transform coming from the accelerometer should be applied to
       * the crtc as is, without taking panel-orientation into account, this
       * is done so that non panel-orientation aware desktop environments do the
       * right thing. Mutter corrects for panel-orientation when applying the
       * transform from a logical-monitor-config, so we must convert here.
       */
      MetaMonitor *panel =
        meta_monitor_manager_get_laptop_panel (config_manager->monitor_manager);

      transform = meta_monitor_crtc_to_logical_transform (panel, transform);
    }
1003
1004
1005
1006

  if (current_logical_monitor_config->transform == transform)
    return NULL;

1007
1008
  logical_monitor_configs =
    clone_logical_monitor_config_list (config_manager->current_config->logical_monitor_configs);
1009
1010
  logical_monitor_config =
    find_logical_config_for_builtin_display_rotation (config_manager, logical_monitor_configs);
1011
1012
  logical_monitor_config->transform = transform;

1013
1014
1015
1016
1017
1018
1019
1020
  if (meta_monitor_transform_is_rotated (current_logical_monitor_config->transform) !=
      meta_monitor_transform_is_rotated (logical_monitor_config->transform))
    {
      int temp = logical_monitor_config->layout.width;
      logical_monitor_config->layout.width = logical_monitor_config->layout.height;
      logical_monitor_config->layout.height = temp;
    }

1021
1022
1023
1024
  layout_mode = config_manager->current_config->layout_mode;
  return meta_monitors_config_new (monitor_manager,
                                   logical_monitor_configs,
                                   layout_mode,
1025
                                   META_MONITORS_CONFIG_FLAG_NONE);
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
}

MetaMonitorsConfig *
meta_monitor_config_manager_create_for_orientation (MetaMonitorConfigManager *config_manager,
                                                    MetaMonitorTransform      transform)
{
  return create_for_builtin_display_rotation (config_manager, FALSE, transform);
}

MetaMonitorsConfig *
meta_monitor_config_manager_create_for_rotate_monitor (MetaMonitorConfigManager *config_manager)
{
  return create_for_builtin_display_rotation (config_manager, TRUE, META_MONITOR_TRANSFORM_NORMAL);
}

1041
1042
1043
1044
static MetaMonitorsConfig *
create_for_switch_config_all_mirror (MetaMonitorConfigManager *config_manager)
{
  MetaMonitorManager *monitor_manager = config_manager->monitor_manager;
1045
  MetaLogicalMonitorLayoutMode layout_mode;
1046
  MetaLogicalMonitorConfig *logical_monitor_config = NULL;
1047
  GList *logical_monitor_configs;
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
  GList *monitor_configs = NULL;
  gint common_mode_w = 0, common_mode_h = 0;
  float best_scale = 1.0;
  MetaMonitor *monitor;
  GList *modes;
  GList *monitors;
  GList *l;

  monitors = meta_monitor_manager_get_monitors (monitor_manager);
  monitor = monitors->data;
  modes = meta_monitor_get_modes (monitor);
  for (l = modes; l; l = l->next)
    {
      MetaMonitorMode *mode = l->data;
      gboolean common_mode_size = TRUE;
      gint mode_w, mode_h;
      GList *ll;

      meta_monitor_mode_get_resolution (mode, &mode_w, &mode_h);

      for (ll = monitors->next; ll; ll = ll->next)
        {
          MetaMonitor *monitor_b = ll->data;
          gboolean have_same_mode_size = FALSE;
          GList *mm;

          for (mm = meta_monitor_get_modes (monitor_b); mm; mm = mm->next)
            {
              MetaMonitorMode *mode_b = mm->data;
              gint mode_b_w, mode_b_h;

              meta_monitor_mode_get_resolution (mode_b, &mode_b_w, &mode_b_h);

              if (mode_w == mode_b_w &&
                  mode_h == mode_b_h)
                {
                  have_same_mode_size = TRUE;
                  break;
                }
            }

          if (!have_same_mode_size)
            {
              common_mode_size = FALSE;
              break;
            }
        }

      if (common_mode_size &&
          common_mode_w * common_mode_h < mode_w * mode_h)
        {
          common_mode_w = mode_w;
          common_mode_h = mode_h;
        }
    }

  if (common_mode_w == 0 || common_mode_h == 0)
    return NULL;

  for (l = monitors; l; l = l->next)
    {
      MetaMonitor *monitor = l->data;
      MetaMonitorMode *mode = NULL;
      GList *ll;
      float scale;

      for (ll = meta_monitor_get_modes (monitor); ll; ll = ll->next)
        {
          gint mode_w, mode_h;

          mode = ll->data;
          meta_monitor_mode_get_resolution (mode, &mode_w, &mode_h);

          if (mode_w == common_mode_w && mode_h == common_mode_h)
            break;
        }

      if (!mode)
        continue;

      scale = meta_monitor_manager_calculate_monitor_mode_scale (monitor_manager, monitor, mode);
      best_scale = MAX (best_scale, scale);
      monitor_configs = g_list_prepend (monitor_configs, create_monitor_config (monitor, mode));
    }

  logical_monitor_config = g_new0 (MetaLogicalMonitorConfig, 1);
  *logical_monitor_config = (MetaLogicalMonitorConfig) {
    .layout = (MetaRectangle) {
      .x = 0,
      .y = 0,
      .width = common_mode_w,
      .height = common_mode_h
    },
    .scale = best_scale,
    .monitor_configs = monitor_configs
  };

1145
  logical_monitor_configs = g_list_append (NULL, logical_monitor_config);
1146
  layout_mode = meta_monitor_manager_get_default_layout_mode (monitor_manager);
1147
1148
  return meta_monitors_config_new (monitor_manager,
                                   logical_monitor_configs,
1149
1150
                                   layout_mode,
                                   META_MONITORS_CONFIG_FLAG_NONE);
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
}

static MetaMonitorsConfig *
create_for_switch_config_external (MetaMonitorConfigManager *config_manager)
{
  MetaMonitorManager *monitor_manager = config_manager->monitor_manager;
  GList *logical_monitor_configs = NULL;
  int x = 0;
  MetaLogicalMonitorLayoutMode layout_mode;
  GList *monitors;
  GList *l;

  layout_mode = meta_monitor_manager_get_default_layout_mode (monitor_manager);

  monitors = meta_monitor_manager_get_monitors (monitor_manager);
  for (l = monitors; l; l = l->next)
    {
      MetaMonitor *monitor = l->data;
      MetaLogicalMonitorConfig *logical_monitor_config;

      if (meta_monitor_is_laptop_panel (monitor))
        continue;

      logical_monitor_config =
        create_preferred_logical_monitor_config (monitor_manager,
                                                 monitor,
                                                 x, 0,
                                                 NULL,
                                                 layout_mode);
      logical_monitor_configs = g_list_append (logical_monitor_configs,
                                               logical_monitor_config);

      if (x == 0)
        logical_monitor_config->is_primary = TRUE;

      x += logical_monitor_config->layout.width;
    }

1189
1190
1191
  return meta_monitors_config_new (monitor_manager,
                                   logical_monitor_configs,
                                   layout_mode,
1192
                                   META_MONITORS_CONFIG_FLAG_NONE);
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
1219
}

static MetaMonitorsConfig *
create_for_switch_config_builtin (MetaMonitorConfigManager *config_manager)
{
  MetaMonitorManager *monitor_manager = config_manager->monitor_manager;
  MetaLogicalMonitorLayoutMode layout_mode;
  GList *logical_monitor_configs;
  MetaLogicalMonitorConfig *primary_logical_monitor_config;
  MetaMonitor *monitor;

  monitor = meta_monitor_manager_get_laptop_panel (monitor_manager);
  if (!monitor)
    return NULL;

  layout_mode = meta_monitor_manager_get_default_layout_mode (monitor_manager);

  primary_logical_monitor_config =
    create_preferred_logical_monitor_config (monitor_manager,
                                             monitor,
                                             0, 0,
                                             NULL,
                                             layout_mode);
  primary_logical_monitor_config->is_primary = TRUE;
  logical_monitor_configs = g_list_append (NULL,
                                           primary_logical_monitor_config);

1220
1221
1222
  return meta_monitors_config_new (monitor_manager,
                                   logical_monitor_configs,
                                   layout_mode,
1223
                                   META_MONITORS_CONFIG_FLAG_NONE);
1224
1225
1226
1227
1228
1229
1230
}

MetaMonitorsConfig *
meta_monitor_config_manager_create_for_switch_config (MetaMonitorConfigManager    *config_manager,
                                                      MetaMonitorSwitchConfigType  config_type)
{
  MetaMonitorManager *monitor_manager = config_manager->monitor_manager;
1231
  MetaMonitorsConfig *config;
1232
1233
1234
1235
1236
1237
1238

  if (!meta_monitor_manager_can_switch_config (monitor_manager))
    return NULL;

  switch (config_type)
    {
    case META_MONITOR_SWITCH_CONFIG_ALL_MIRROR:
1239
1240
      config = create_for_switch_config_all_mirror (config_manager);
      break;
1241
    case META_MONITOR_SWITCH_CONFIG_ALL_LINEAR:
1242
1243
      config = meta_monitor_config_manager_create_linear (config_manager);
      break;
1244
    case META_MONITOR_SWITCH_CONFIG_EXTERNAL:
1245
1246
      config = create_for_switch_config_external (config_manager);
      break;
1247
    case META_MONITOR_SWITCH_CONFIG_BUILTIN:
1248
1249
      config = create_for_switch_config_builtin (config_manager);
      break;
1250
    case META_MONITOR_SWITCH_CONFIG_UNKNOWN:
1251
    default:
1252
      g_warn_if_reached ();
1253
      return NULL;
1254
    }
1255
1256
1257
1258
1259

  if (config)
    meta_monitors_config_set_switch_config (config, config_type);

  return config;
1260
1261
}

1262
1263
1264
1265
void
meta_monitor_config_manager_set_current (MetaMonitorConfigManager *config_manager,
                                         MetaMonitorsConfig       *config)
{
1266
1267
1268
1269
1270
1271
1272
1273
1274
  if (config_manager->current_config)
    {
      g_queue_push_head (&config_manager->config_history,
                         g_object_ref (config_manager->current_config));
      if (g_queue_get_length (&config_manager->config_history) >
          CONFIG_HISTORY_MAX_SIZE)
        g_object_unref (g_queue_pop_tail (&config_manager->config_history));
    }

1275
1276
1277
  g_set_object (&config_manager->current_config, config);
}

1278
1279
1280
1281
1282
1283
1284
1285
1286
void
meta_monitor_config_manager_save_current (MetaMonitorConfigManager *config_manager)
{
  g_return_if_fail (config_manager->current_config);

  meta_monitor_config_store_add (config_manager->config_store,
                                 config_manager->current_config);
}

1287
1288
1289
1290
1291
1292
MetaMonitorsConfig *
meta_monitor_config_manager_get_current (MetaMonitorConfigManager *config_manager)
{
  return config_manager->current_config;
}

1293
1294
1295
1296
1297
1298
MetaMonitorsConfig *
meta_monitor_config_manager_pop_previous (MetaMonitorConfigManager *config_manager)
{
  return g_queue_pop_head (&config_manager->config_history);
}

1299
1300
1301
MetaMonitorsConfig *
meta_monitor_config_manager_get_previous (MetaMonitorConfigManager *config_manager)
{
1302
1303
1304
1305
1306
1307
1308
1309
  return g_queue_peek_head (&config_manager->config_history);
}

void
meta_monitor_config_manager_clear_history (MetaMonitorConfigManager *config_manager)
{
  g_queue_foreach (&config_manager->config_history, (GFunc) g_object_unref, NULL);
  g_queue_clear (&config_manager->config_history);
1310
1311
}

1312
1313
1314
1315
1316
1317
1318
static void
meta_monitor_config_manager_dispose (GObject *object)
{
  MetaMonitorConfigManager *config_manager =
    META_MONITOR_CONFIG_MANAGER (object);

  g_clear_object (&config_manager->current_config);
1319
  meta_monitor_config_manager_clear_history (config_manager);
1320
1321
1322
1323
1324
1325
1326

  G_OBJECT_CLASS (meta_monitor_config_manager_parent_class)->dispose (object);
}

static void
meta_monitor_config_manager_init (MetaMonitorConfigManager *config_manager)
{
1327
  g_queue_init (&config_manager->config_history);
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
}

static void
meta_monitor_config_manager_class_init (MetaMonitorConfigManagerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = meta_monitor_config_manager_dispose;
}

1338
void
1339
1340
1341
1342
1343
1344
1345
meta_monitor_config_free (MetaMonitorConfig *monitor_config)
{
  meta_monitor_spec_free (monitor_config->monitor_spec);
  g_free (monitor_config->mode_spec);
  g_free (monitor_config);
}

1346
void
1347
1348
1349
1350
1351
1352
1353
meta_logical_monitor_config_free (MetaLogicalMonitorConfig *logical_monitor_config)
{
  g_list_free_full (logical_monitor_config->monitor_configs,
                    (GDestroyNotify) meta_monitor_config_free);
  g_free (logical_monitor_config);
}

1354
static MetaMonitorsConfigKey *
1355
1356
meta_monitors_config_key_new (GList *logical_monitor_configs,
                              GList *disabled_monitor_specs)
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
{
  MetaMonitorsConfigKey *config_key;
  GList *monitor_specs;
  GList *l;

  monitor_specs = NULL;
  for (l = logical_monitor_configs; l; l = l->next)
    {
      MetaLogicalMonitorConfig *logical_monitor_config = l->data;
      GList *k;

      for (k = logical_monitor_config->monitor_configs; k; k = k->next)
        {
          MetaMonitorConfig *monitor_config = k->data;
          MetaMonitorSpec *monitor_spec;

          monitor_spec = meta_monitor_spec_clone (monitor_config->monitor_spec);
          monitor_specs = g_list_prepend (monitor_specs, monitor_spec);
        }
    }

1378
1379
1380
1381
1382
1383
1384
1385
  for (l = disabled_monitor_specs; l; l = l->next)
    {
      MetaMonitorSpec *monitor_spec = l->data;

      monitor_spec = meta_monitor_spec_clone (monitor_spec);
      monitor_specs = g_list_prepend (monitor_specs, monitor_spec);
    }

1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
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
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
  monitor_specs = g_list_sort (monitor_specs,
                               (GCompareFunc) meta_monitor_spec_compare);

  config_key = g_new0 (MetaMonitorsConfigKey, 1);
  *config_key = (MetaMonitorsConfigKey) {
    .monitor_specs = monitor_specs
  };

  return config_key;
}

void
meta_monitors_config_key_free (MetaMonitorsConfigKey *config_key)
{
  g_list_free_full (config_key->monitor_specs,
                    (GDestroyNotify) meta_monitor_spec_free);
  g_free (config_key);
}

unsigned int
meta_monitors_config_key_hash (gconstpointer data)
{
  const MetaMonitorsConfigKey *config_key = data;
  GList *l;
  unsigned long hash;

  hash = 0;
  for (l = config_key->monitor_specs; l; l = l->next)
    {
      MetaMonitorSpec *monitor_spec = l->data;

      hash ^= (g_str_hash (monitor_spec->connector) ^
               g_str_hash (monitor_spec->vendor) ^
               g_str_hash (monitor_spec->product) ^
               g_str_hash (monitor_spec->serial));
    }

  return hash;
}

gboolean
meta_monitors_config_key_equal (gconstpointer data_a,
                                gconstpointer data_b)
{
  const MetaMonitorsConfigKey *config_key_a = data_a;
  const MetaMonitorsConfigKey *config_key_b = data_b;
  GList *l_a, *l_b;

  for (l_a = config_key_a->monitor_specs, l_b = config_key_b->monitor_specs;
       l_a && l_b;
       l_a = l_a->next, l_b = l_b->next)
    {
      MetaMonitorSpec *monitor_spec_a = l_a->data;
      MetaMonitorSpec *monitor_spec_b = l_b->data;

      if (!meta_monitor_spec_equals (monitor_spec_a, monitor_spec_b))
        return FALSE;
    }

1445
  if (l_a || l_b)
1446
1447
1448
1449
1450
    return FALSE;

  return TRUE;
}

1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
MetaMonitorSwitchConfigType
meta_monitors_config_get_switch_config (MetaMonitorsConfig *config)
{
  return config->switch_config;
}

void
meta_monitors_config_set_switch_config (MetaMonitorsConfig          *config,
                                        MetaMonitorSwitchConfigType  switch_config)
{
  config->switch_config = switch_config;
}

1464
MetaMonitorsConfig *
1465
1466
1467
1468
meta_monitors_config_new_full (GList                        *logical_monitor_configs,
                               GList                        *disabled_monitor_specs,
                               MetaLogicalMonitorLayoutMode  layout_mode,
                               MetaMonitorsConfigFlag        flags)
1469
1470
1471
1472
1473
{
  MetaMonitorsConfig *config;

  config = g_object_new (META_TYPE_MONITORS_CONFIG, NULL);
  config->logical_monitor_configs = logical_monitor_configs;
1474
1475
1476
  config->disabled_monitor_specs = disabled_monitor_specs;
  config->key = meta_monitors_config_key_new (logical_monitor_configs,
                                              disabled_monitor_specs);
1477
  config->layout_mode = layout_mode;
1478
  config->flags = flags;
1479
  config->switch_config = META_MONITOR_SWITCH_CONFIG_UNKNOWN;
1480
1481
1482
1483

  return config;
}

1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
MetaMonitorsConfig *
meta_monitors_config_new (MetaMonitorManager           *monitor_manager,
                          GList                        *logical_monitor_configs,
                          MetaLogicalMonitorLayoutMode  layout_mode,
                          MetaMonitorsConfigFlag        flags)
{
  GList *disabled_monitor_specs = NULL;
  GList *monitors;
  GList *l;

  monitors = meta_monitor_manager_get_monitors (monitor_manager);
  for (l = monitors; l; l = l->next)
    {
      MetaMonitor *monitor = l->data;
      MetaMonitorSpec *monitor_spec;

1500
      if (is_lid_closed (monitor_manager) &&
1501
1502
1503
          meta_monitor_is_laptop_panel (monitor))
        continue;

1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
      monitor_spec = meta_monitor_get_spec (monitor);
      if (meta_logical_monitor_configs_have_monitor (logical_monitor_configs,
                                                     monitor_spec))
        continue;

      disabled_monitor_specs =
        g_list_prepend (disabled_monitor_specs,
                        meta_monitor_spec_clone (monitor_spec));
    }

  return meta_monitors_config_new_full (logical_monitor_configs,
                                        disabled_monitor_specs,
                                        layout_mode,
                                        flags);
}

1520
1521
1522
1523
1524
static void
meta_monitors_config_finalize (GObject *object)
{
  MetaMonitorsConfig *config = META_MONITORS_CONFIG (object);

1525
  meta_monitors_config_key_free (config->key);
1526
1527
  g_list_free_full (config->logical_monitor_configs,
                    (GDestroyNotify) meta_logical_monitor_config_free);
1528
1529
  g_list_free_full (config->disabled_monitor_specs,
                    (GDestroyNotify) meta_monitor_spec_free);
1530
1531

  G_OBJECT_CLASS (meta_monitors_config_parent_class)->finalize (object);
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
}

static void
meta_monitors_config_init (MetaMonitorsConfig *config)
{
}

static void
meta_monitors_config_class_init (MetaMonitorsConfigClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = meta_monitors_config_finalize;
}
1546

1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
static void
meta_crtc_info_free (MetaCrtcInfo *info)
{
  g_ptr_array_free (info->outputs, TRUE);
  g_slice_free (MetaCrtcInfo, info);
}

static void
meta_output_info_free (MetaOutputInfo *info)
{
  g_slice_free (MetaOutputInfo, info);
}

1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
gboolean
meta_verify_monitor_mode_spec (MetaMonitorModeSpec *monitor_mode_spec,
                               GError             **error)
{
  if (monitor_mode_spec->width > 0 &&
      monitor_mode_spec->height > 0 &&
      monitor_mode_spec->refresh_rate > 0.0f)
    {
      return TRUE;
    }
  else
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Monitor mode invalid");
      return FALSE;
    }
}

gboolean
meta_verify_monitor_spec (MetaMonitorSpec *monitor_spec,
                          GError         **error)
{
  if (monitor_spec->connector &&
      monitor_spec->vendor &&
      monitor_spec->product &&
      monitor_spec->serial)
    {
      return TRUE;
    }
  else
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Monitor spec incomplete");
      return FALSE;
    }
}

gboolean
meta_verify_monitor_config (MetaMonitorConfig *monitor_config,
                            GError           **error)
{
  if (monitor_config->monitor_spec && monitor_config->mode_spec)
    {
      return TRUE;
    }
  else
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Monitor config incomplete");
      return FALSE;
    }
}

gboolean
1614
1615
meta_verify_logical_monitor_config (MetaLogicalMonitorConfig    *logical_monitor_config,
                                    MetaLogicalMonitorLayoutMode layout_mode,
1616
                                    MetaMonitorManager          *monitor_manager,
1617
                                    GError                     **error)
1618
{
1619
  GList *l;
1620
1621
  int expected_mode_width = 0;
  int expected_mode_height = 0;
1622

1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
  if (logical_monitor_config->layout.x < 0 ||
      logical_monitor_config->layout.y < 0)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Invalid logical monitor position (%d, %d)",
                   logical_monitor_config->layout.x,
                   logical_monitor_config->layout.y);
      return FALSE;
    }

  if (!logical_monitor_config->monitor_configs)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Logical monitor is empty");
      return FALSE;
    }

1640
1641
1642
1643
1644
1645
1646
1647
1648
1649