summaryrefslogtreecommitdiff
path: root/gdk/gdkmonitor.c
blob: 96d7fe4416956bd6c622b6aaac300df26b3e0c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
 * gdkmonitor.c
 *
 * Copyright 2016 Red Hat, Inc.
 *
 * Matthias Clasen <mclasen@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gdkmonitorprivate.h"
#include "gdkdisplay.h"
#include "gdkenumtypes.h"

/**
 * GdkMonitor:
 *
 * `GdkMonitor` objects represent the individual outputs that are
 * associated with a `GdkDisplay`.
 *
 * `GdkDisplay` keeps a `GListModel` to enumerate and monitor
 * monitors with [method@Gdk.Display.get_monitors]. You can use
 * [method@Gdk.Display.get_monitor_at_surface] to find a particular
 * monitor.
 */

enum {
  PROP_0,
  PROP_DESCRIPTION,
  PROP_DISPLAY,
  PROP_MANUFACTURER,
  PROP_MODEL,
  PROP_CONNECTOR,
  PROP_SCALE_FACTOR,
  PROP_GEOMETRY,
  PROP_WIDTH_MM,
  PROP_HEIGHT_MM,
  PROP_REFRESH_RATE,
  PROP_SUBPIXEL_LAYOUT,
  PROP_VALID,
  LAST_PROP
};

static GParamSpec *props[LAST_PROP] = { NULL, };

enum {
  INVALIDATE,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (GdkMonitor, gdk_monitor, G_TYPE_OBJECT)

static void
gdk_monitor_init (GdkMonitor *monitor)
{
  monitor->scale_factor = 1;
  monitor->valid = TRUE;
}

static void
gdk_monitor_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GdkMonitor *monitor = GDK_MONITOR (object);

  switch (prop_id)
    {
    case PROP_DESCRIPTION:
      g_value_set_string (value, monitor->description);
      break;

    case PROP_DISPLAY:
      g_value_set_object (value, monitor->display);
      break;

    case PROP_MANUFACTURER:
      g_value_set_string (value, monitor->manufacturer);
      break;

    case PROP_MODEL:
      g_value_set_string (value, monitor->model);
      break;

    case PROP_CONNECTOR:
      g_value_set_string (value, monitor->connector);
      break;

    case PROP_SCALE_FACTOR:
      g_value_set_int (value, monitor->scale_factor);
      break;

    case PROP_GEOMETRY:
      g_value_set_boxed (value, &monitor->geometry);
      break;

    case PROP_WIDTH_MM:
      g_value_set_int (value, monitor->width_mm);
      break;

    case PROP_HEIGHT_MM:
      g_value_set_int (value, monitor->height_mm);
      break;

    case PROP_REFRESH_RATE:
      g_value_set_int (value, monitor->refresh_rate);
      break;

    case PROP_SUBPIXEL_LAYOUT:
      g_value_set_enum (value, monitor->subpixel_layout);
      break;

    case PROP_VALID:
      g_value_set_boolean (value, monitor->valid);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gdk_monitor_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  GdkMonitor *monitor = GDK_MONITOR (object);

  switch (prop_id)
    {
    case PROP_DISPLAY:
      monitor->display = g_value_get_object (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gdk_monitor_finalize (GObject *object)
{
  GdkMonitor *monitor = GDK_MONITOR (object);

  g_free (monitor->description);
  g_free (monitor->connector);
  g_free (monitor->manufacturer);
  g_free (monitor->model);

  G_OBJECT_CLASS (gdk_monitor_parent_class)->finalize (object);
}

static void
gdk_monitor_class_init (GdkMonitorClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = gdk_monitor_finalize;
  object_class->get_property = gdk_monitor_get_property;
  object_class->set_property = gdk_monitor_set_property;

  /**
   * GdkMonitor:description: (attributes org.gtk.Property.get=gdk_monitor_get_description)
   *
   * A short description of the monitor, meant for display to the user.
   *
   * Since: 4.10
   */
  props[PROP_DESCRIPTION] =
    g_param_spec_string ("description", NULL, NULL,
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:display: (attributes org.gtk.Property.get=gdk_monitor_get_display)
   *
   * The `GdkDisplay` of the monitor.
   */
  props[PROP_DISPLAY] =
    g_param_spec_object ("display", NULL, NULL,
                         GDK_TYPE_DISPLAY,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:manufacturer: (attributes org.gtk.Property.get=gdk_monitor_get_manufacturer)
   *
   * The manufacturer name.
   */
  props[PROP_MANUFACTURER] =
    g_param_spec_string ("manufacturer", NULL, NULL,
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:model: (attributes org.gtk.Property.get=gdk_monitor_get_model)
   *
   * The model name.
   */
  props[PROP_MODEL] =
    g_param_spec_string ("model", NULL, NULL,
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:connector: (attributes org.gtk.Property.get=gdk_monitor_get_connector)
   *
   * The connector name.
   */
  props[PROP_CONNECTOR] =
    g_param_spec_string ("connector", NULL, NULL,
                         NULL,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:scale-factor: (attributes org.gtk.Property.get=gdk_monitor_get_scale_factor)
   *
   * The scale factor.
   */
  props[PROP_SCALE_FACTOR] =
    g_param_spec_int ("scale-factor", NULL, NULL,
                      0, G_MAXINT,
                      1,
                      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:geometry: (attributes org.gtk.Property.get=gdk_monitor_get_geometry)
   *
   * The geometry of the monitor.
   */
  props[PROP_GEOMETRY] =
    g_param_spec_boxed ("geometry", NULL, NULL,
                        GDK_TYPE_RECTANGLE,
                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:width-mm: (attributes org.gtk.Property.get=gdk_monitor_get_width_mm)
   *
   * The width of the monitor, in millimeters.
   */
  props[PROP_WIDTH_MM] =
    g_param_spec_int ("width-mm", NULL, NULL,
                      0, G_MAXINT,
                      0,
                      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:height-mm: (attributes org.gtk.Property.get=gdk_monitor_get_height_mm)
   *
   * The height of the monitor, in millimeters.
   */
  props[PROP_HEIGHT_MM] =
    g_param_spec_int ("height-mm", NULL, NULL,
                      0, G_MAXINT,
                      0,
                      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:refresh-rate: (attributes org.gtk.Property.get=gdk_monitor_get_refresh_rate)
   *
   * The refresh rate, in milli-Hertz.
   */
  props[PROP_REFRESH_RATE] =
    g_param_spec_int ("refresh-rate", NULL, NULL,
                      0, G_MAXINT,
                      0,
                      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:subpixel-layout: (attributes org.gtk.Property.get=gdk_monitor_get_subpixel_layout)
   *
   * The subpixel layout.
   */
  props[PROP_SUBPIXEL_LAYOUT] =
    g_param_spec_enum ("subpixel-layout", NULL, NULL,
                       GDK_TYPE_SUBPIXEL_LAYOUT,
                       GDK_SUBPIXEL_LAYOUT_UNKNOWN,
                       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  /**
   * GdkMonitor:valid: (attributes org.gtk.Property.get=gdk_monitor_is_valid)
   *
   * Whether the object is still valid.
   */
  props[PROP_VALID] =
    g_param_spec_boolean ("valid", NULL, NULL,
                          TRUE,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, LAST_PROP, props);

  /**
   * GdkMonitor::invalidate:
   * @monitor: the object on which this signal was emitted
   *
   * Emitted when the output represented by @monitor gets disconnected.
   */
  signals[INVALIDATE] = g_signal_new (g_intern_static_string ("invalidate"),
                                      G_TYPE_FROM_CLASS (object_class),
                                      G_SIGNAL_RUN_FIRST,
                                      0,
                                      NULL, NULL,
                                      NULL,
                                      G_TYPE_NONE, 0);
}

/**
 * gdk_monitor_get_display: (attributes org.gtk.Method.get_property=display)
 * @monitor: a `GdkMonitor`
 *
 * Gets the display that this monitor belongs to.
 *
 * Returns: (transfer none): the display
 */
GdkDisplay *
gdk_monitor_get_display (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);

  return monitor->display;
}

/**
 * gdk_monitor_get_geometry: (attributes org.gtk.Method.get_property=geometry)
 * @monitor: a `GdkMonitor`
 * @geometry: (out): a `GdkRectangle` to be filled with the monitor geometry
 *
 * Retrieves the size and position of the monitor within the
 * display coordinate space.
 *
 * The returned geometry is in  ”application pixels”, not in
 * ”device pixels” (see [method@Gdk.Monitor.get_scale_factor]).
 */
void
gdk_monitor_get_geometry (GdkMonitor   *monitor,
                          GdkRectangle *geometry)
{
  g_return_if_fail (GDK_IS_MONITOR (monitor));
  g_return_if_fail (geometry != NULL);

  *geometry = monitor->geometry;
}

/**
 * gdk_monitor_get_width_mm: (attributes org.gtk.Method.get_property=width-mm)
 * @monitor: a `GdkMonitor`
 *
 * Gets the width in millimeters of the monitor.
 *
 * Returns: the physical width of the monitor
 */
int
gdk_monitor_get_width_mm (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), 0);

  return monitor->width_mm;
}

/**
 * gdk_monitor_get_height_mm: (attributes org.gtk.Method.get_property=height-mm)
 * @monitor: a `GdkMonitor`
 *
 * Gets the height in millimeters of the monitor.
 *
 * Returns: the physical height of the monitor
 */
int
gdk_monitor_get_height_mm (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), 0);

  return monitor->height_mm;
}

/**
 * gdk_monitor_get_connector: (attributes org.gtk.Method.get_property=connector)
 * @monitor: a `GdkMonitor`
 *
 * Gets the name of the monitor's connector, if available.
 *
 * These are strings such as "eDP-1", or "HDMI-2". They depend
 * on software and hardware configuration, and should not be
 * relied on as stable identifiers of a specific monitor.
 *
 * Returns: (transfer none) (nullable): the name of the connector
 */
const char *
gdk_monitor_get_connector (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);

  return monitor->connector;
}

/**
 * gdk_monitor_get_manufacturer: (attributes org.gtk.Method.get_property=manufacturer)
 * @monitor: a `GdkMonitor`
 *
 * Gets the name or PNP ID of the monitor's manufacturer.
 *
 * Note that this value might also vary depending on actual
 * display backend.
 *
 * The PNP ID registry is located at
 * [https://uefi.org/pnp_id_list](https://uefi.org/pnp_id_list).
 *
 * Returns: (transfer none) (nullable): the name of the manufacturer
 */
const char *
gdk_monitor_get_manufacturer (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);

  return monitor->manufacturer;
}

/**
 * gdk_monitor_get_model: (attributes org.gtk.Method.get_property=model)
 * @monitor: a `GdkMonitor`
 *
 * Gets the string identifying the monitor model, if available.
 *
 * Returns: (transfer none) (nullable): the monitor model
 */
const char *
gdk_monitor_get_model (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);

  return monitor->model;
}

/**
 * gdk_monitor_get_scale_factor: (attributes org.gtk.Method.get_property=scale-factor)
 * @monitor: a `GdkMonitor`
 *
 * Gets the internal scale factor that maps from monitor coordinates
 * to device pixels.
 *
 * On traditional systems this is 1, but on very high density outputs
 * it can be a higher value (often 2).
 *
 * This can be used if you want to create pixel based data for a
 * particular monitor, but most of the time you’re drawing to a surface
 * where it is better to use [method@Gdk.Surface.get_scale_factor] instead.
 *
 * Returns: the scale factor
 */
int
gdk_monitor_get_scale_factor (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), 1);

  return monitor->scale_factor;
}

/**
 * gdk_monitor_get_refresh_rate: (attributes org.gtk.Method.get_property=refresh-rate)
 * @monitor: a `GdkMonitor`
 *
 * Gets the refresh rate of the monitor, if available.
 *
 * The value is in milli-Hertz, so a refresh rate of 60Hz
 * is returned as 60000.
 *
 * Returns: the refresh rate in milli-Hertz, or 0
 */
int
gdk_monitor_get_refresh_rate (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), 0);

  return monitor->refresh_rate;
}

/**
 * gdk_monitor_get_subpixel_layout: (attributes org.gtk.Method.get_property=subpixel-layout)
 * @monitor: a `GdkMonitor`
 *
 * Gets information about the layout of red, green and blue
 * primaries for pixels.
 *
 * Returns: the subpixel layout
 */
GdkSubpixelLayout
gdk_monitor_get_subpixel_layout (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), GDK_SUBPIXEL_LAYOUT_UNKNOWN);

  return monitor->subpixel_layout;
}

GdkMonitor *
gdk_monitor_new (GdkDisplay *display)
{
  return GDK_MONITOR (g_object_new (GDK_TYPE_MONITOR,
                                    "display", display,
                                    NULL));
}

void
gdk_monitor_set_manufacturer (GdkMonitor *monitor,
                              const char *manufacturer)
{
  g_free (monitor->manufacturer);
  monitor->manufacturer = g_strdup (manufacturer);

  g_object_notify (G_OBJECT (monitor), "manufacturer");
}

void
gdk_monitor_set_model (GdkMonitor *monitor,
                       const char *model)
{
  g_free (monitor->model);
  monitor->model = g_strdup (model);

  g_object_notify (G_OBJECT (monitor), "model");
}

void
gdk_monitor_set_connector (GdkMonitor *monitor,
                           const char *connector)
{
  g_free (monitor->connector);
  monitor->connector = g_strdup (connector);

  g_object_notify (G_OBJECT (monitor), "connector");
}

void
gdk_monitor_set_geometry (GdkMonitor *monitor,
                          const GdkRectangle *geometry)
{
  if (gdk_rectangle_equal (&monitor->geometry, geometry))
    return;

  monitor->geometry = *geometry;
  g_object_notify (G_OBJECT (monitor), "geometry");
}

void
gdk_monitor_set_physical_size (GdkMonitor *monitor,
                               int         width_mm,
                               int         height_mm)
{
  g_object_freeze_notify (G_OBJECT (monitor));

  if (monitor->width_mm != width_mm)
    {
      monitor->width_mm = width_mm;
      g_object_notify (G_OBJECT (monitor), "width-mm");
    }

  if (monitor->height_mm != height_mm)
    {
      monitor->height_mm = height_mm;
      g_object_notify (G_OBJECT (monitor), "height-mm");
    }

  g_object_thaw_notify (G_OBJECT (monitor));
}

void
gdk_monitor_set_scale_factor (GdkMonitor *monitor,
                              int         scale_factor)
{
  if (monitor->scale_factor == scale_factor)
    return;

  monitor->scale_factor = scale_factor;

  g_object_notify (G_OBJECT (monitor), "scale-factor");
}

void
gdk_monitor_set_refresh_rate (GdkMonitor *monitor,
                              int         refresh_rate)
{
  if (monitor->refresh_rate == refresh_rate)
    return;

  monitor->refresh_rate = refresh_rate;

  g_object_notify (G_OBJECT (monitor), "refresh-rate");
}

void
gdk_monitor_set_subpixel_layout (GdkMonitor        *monitor,
                                 GdkSubpixelLayout  subpixel_layout)
{
  if (monitor->subpixel_layout == subpixel_layout)
    return;

  monitor->subpixel_layout = subpixel_layout;

  g_object_notify (G_OBJECT (monitor), "subpixel-layout");
}

void
gdk_monitor_invalidate (GdkMonitor *monitor)
{
  monitor->valid = FALSE;
  g_object_notify (G_OBJECT (monitor), "valid");
  g_signal_emit (monitor, signals[INVALIDATE], 0);
}

/**
 * gdk_monitor_is_valid: (attributes org.gtk.Method.get_property=valid)
 * @monitor: a `GdkMonitor`
 *
 * Returns %TRUE if the @monitor object corresponds to a
 * physical monitor.
 *
 * The @monitor becomes invalid when the physical monitor
 * is unplugged or removed.
 *
 * Returns: %TRUE if the object corresponds to a physical monitor
 */
gboolean
gdk_monitor_is_valid (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), FALSE);

  return monitor->valid;
}

/**
 * gdk_monitor_get_description: (attributes org.gtk.Method.get_property=description)
 * @monitor: a `GdkMonitor`
 *
 * Gets a string describing the monitor, if available.
 *
 * This can be used to identify a monitor in the UI.
 *
 * Returns: (transfer none) (nullable): the monitor description
 *
 * Since: 4.10
 */
const char *
gdk_monitor_get_description (GdkMonitor *monitor)
{
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);

  return monitor->description;
}

void
gdk_monitor_set_description (GdkMonitor *monitor,
                             const char *description)
{
  g_free (monitor->description);
  monitor->description = g_strdup (description);
  g_object_notify_by_pspec (G_OBJECT (monitor), props[PROP_DESCRIPTION]);
}