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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
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
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
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
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
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
|
/* gnome-rr.c
*
* Copyright 2007, 2008, Red Hat, Inc.
*
* This file is part of the Gnome Library.
*
* The Gnome 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.
*
* The Gnome 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 the Gnome Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Soren Sandmann <sandmann@redhat.com>
*/
#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <config.h>
#include <glib/gi18n-lib.h>
#include <string.h>
#include <gtk/gtk.h>
#ifdef HAVE_X11
#include <X11/Xlib.h>
#include <gdk/gdkx.h>
#include <X11/Xatom.h>
#include "gnome-rr-x11.h"
#else
#include "gnome-rr-windows.h"
#endif
#undef GNOME_DISABLE_DEPRECATED
#include "gnome-rr.h"
#include "gnome-rr-config.h"
#include "gnome-rr-private.h"
enum {
SCREEN_PROP_0,
SCREEN_PROP_GDK_SCREEN,
SCREEN_PROP_LAST,
};
enum {
SCREEN_CHANGED,
SCREEN_SIGNAL_LAST,
};
static gint screen_signals[SCREEN_SIGNAL_LAST];
static GParamSpec *screen_properties[SCREEN_PROP_LAST];
/* GnomeRRCrtc */
static GnomeRRCrtc * crtc_copy (const GnomeRRCrtc *from);
static void crtc_free (GnomeRRCrtc *crtc);
static GnomeRROutput *output_copy (const GnomeRROutput *from);
static void output_free (GnomeRROutput *output);
static GnomeRRMode * mode_copy (const GnomeRRMode *from);
static void mode_free (GnomeRRMode *mode);
static void gnome_rr_screen_set_property (GObject*, guint, const GValue*, GParamSpec*);
static void gnome_rr_screen_get_property (GObject*, guint, GValue*, GParamSpec*);
static void gnome_rr_screen_initable_iface_init (GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE (GnomeRRScreen, gnome_rr_screen, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, gnome_rr_screen_initable_iface_init))
G_DEFINE_BOXED_TYPE (GnomeRRCrtc, gnome_rr_crtc, crtc_copy, crtc_free)
G_DEFINE_BOXED_TYPE (GnomeRROutput, gnome_rr_output, output_copy, output_free)
G_DEFINE_BOXED_TYPE (GnomeRRMode, gnome_rr_mode, mode_copy, mode_free)
/* Errors */
/**
* gnome_rr_error_quark:
*
* Returns the #GQuark that will be used for #GError values returned by the
* GnomeRR API.
*
* Return value: a #GQuark used to identify errors coming from the GnomeRR API.
*/
GQuark
gnome_rr_error_quark (void)
{
return g_quark_from_static_string ("gnome-rr-error-quark");
}
/* Screen */
GnomeRROutput *
gnome_rr_output_by_id (ScreenInfo *info, RROutput id)
{
GnomeRROutput **output;
g_return_val_if_fail (info != NULL, NULL);
for (output = info->outputs; *output; ++output)
{
if ((*output)->id == id)
return *output;
}
return NULL;
}
GnomeRRCrtc *
crtc_by_id (ScreenInfo *info, RRCrtc id)
{
GnomeRRCrtc **crtc;
if (!info)
return NULL;
for (crtc = info->crtcs; *crtc; ++crtc)
{
if ((*crtc)->id == id)
return *crtc;
}
return NULL;
}
GnomeRRMode *
mode_by_id (ScreenInfo *info, RRMode id)
{
GnomeRRMode **mode;
g_return_val_if_fail (info != NULL, NULL);
for (mode = info->modes; *mode; ++mode)
{
if ((*mode)->id == id)
return *mode;
}
return NULL;
}
void
screen_info_free (ScreenInfo *info)
{
GnomeRROutput **output;
GnomeRRCrtc **crtc;
GnomeRRMode **mode;
g_return_if_fail (info != NULL);
#ifdef HAVE_RANDR
if (info->resources)
{
XRRFreeScreenResources (info->resources);
info->resources = NULL;
}
#endif
if (info->outputs)
{
for (output = info->outputs; *output; ++output)
output_free (*output);
g_free (info->outputs);
}
if (info->crtcs)
{
for (crtc = info->crtcs; *crtc; ++crtc)
crtc_free (*crtc);
g_free (info->crtcs);
}
if (info->modes)
{
for (mode = info->modes; *mode; ++mode)
mode_free (*mode);
g_free (info->modes);
}
if (info->clone_modes)
{
/* The modes themselves were freed above */
g_free (info->clone_modes);
}
g_free (info);
}
gboolean
has_similar_mode (GnomeRROutput *output, GnomeRRMode *mode)
{
int i;
GnomeRRMode **modes = gnome_rr_output_list_modes (output);
int width = gnome_rr_mode_get_width (mode);
int height = gnome_rr_mode_get_height (mode);
for (i = 0; modes[i] != NULL; ++i)
{
GnomeRRMode *m = modes[i];
if (gnome_rr_mode_get_width (m) == width &&
gnome_rr_mode_get_height (m) == height)
{
return TRUE;
}
}
return FALSE;
}
void
gather_clone_modes (ScreenInfo *info)
{
int i;
GPtrArray *result = g_ptr_array_new ();
for (i = 0; info->outputs[i] != NULL; ++i)
{
int j;
GnomeRROutput *output1, *output2;
output1 = info->outputs[i];
if (!output1->connected)
continue;
for (j = 0; output1->modes[j] != NULL; ++j)
{
GnomeRRMode *mode = output1->modes[j];
gboolean valid;
int k;
valid = TRUE;
for (k = 0; info->outputs[k] != NULL; ++k)
{
output2 = info->outputs[k];
if (!output2->connected)
continue;
if (!has_similar_mode (output2, mode))
{
valid = FALSE;
break;
}
}
if (valid)
g_ptr_array_add (result, mode);
}
}
g_ptr_array_add (result, NULL);
info->clone_modes = (GnomeRRMode **)g_ptr_array_free (result, FALSE);
}
ScreenInfo *
screen_info_new (GnomeRRScreen *screen, gboolean needs_reprobe, GError **error)
{
ScreenInfo *info = g_new0 (ScreenInfo, 1);
GnomeRRScreenPrivate *priv;
g_return_val_if_fail (screen != NULL, NULL);
priv = screen->priv;
info->outputs = NULL;
info->crtcs = NULL;
info->modes = NULL;
info->screen = screen;
if (fill_out_screen_info (screen, info, needs_reprobe, error))
{
return info;
}
else
{
screen_info_free (info);
return NULL;
}
}
gboolean
screen_update (GnomeRRScreen *screen, gboolean force_callback, gboolean needs_reprobe, GError **error)
{
ScreenInfo *info;
gboolean changed = FALSE;
g_return_val_if_fail (screen != NULL, FALSE);
info = screen_info_new (screen, needs_reprobe, error);
if (!info)
return FALSE;
#ifdef HAVE_RANDR
if (info->resources->configTimestamp != screen->priv->info->resources->configTimestamp)
changed = TRUE;
#endif
screen_info_free (screen->priv->info);
screen->priv->info = info;
if (changed || force_callback)
g_signal_emit (G_OBJECT (screen), screen_signals[SCREEN_CHANGED], 0);
return changed;
}
static gboolean
gnome_rr_screen_initable_init (GInitable *initable, GCancellable *canc, GError **error)
{
GnomeRRScreen *self = GNOME_RR_SCREEN (initable);
GnomeRRScreenPrivate *priv = self->priv;
priv->info = screen_info_new (self, TRUE, error);
g_return_val_if_fail (priv->info != NULL, FALSE);
return TRUE;
}
static void
gnome_rr_screen_initable_iface_init (GInitableIface *iface)
{
iface->init = gnome_rr_screen_initable_init;
}
static void
gnome_rr_screen_set_property (GObject *gobject, guint property_id, const GValue *value, GParamSpec *property)
{
GnomeRRScreen *self = GNOME_RR_SCREEN (gobject);
GnomeRRScreenPrivate *priv = self->priv;
switch (property_id)
{
case SCREEN_PROP_GDK_SCREEN:
priv->gdk_screen = g_value_get_object (value);
g_object_notify_by_pspec (gobject, screen_properties[SCREEN_PROP_GDK_SCREEN]);
return;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, property_id, property);
return;
}
}
static void
gnome_rr_screen_get_property (GObject *gobject, guint property_id, GValue *value, GParamSpec *property)
{
GnomeRRScreen *self = GNOME_RR_SCREEN (gobject);
switch (property_id)
{
case SCREEN_PROP_GDK_SCREEN:
g_value_set_object (value, gnome_rr_screen_get_gdk_screen (self));
return;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, property_id, property);
return;
}
}
static void
gnome_rr_screen_finalize (GObject *gobject)
{
GnomeRRScreen *screen = GNOME_RR_SCREEN (gobject);
if (screen->priv->info)
screen_info_free (screen->priv->info);
G_OBJECT_CLASS (gnome_rr_screen_parent_class)->finalize (gobject);
}
void
gnome_rr_screen_class_init (GnomeRRScreenClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (GnomeRRScreenPrivate));
gobject_class->set_property = gnome_rr_screen_set_property;
gobject_class->get_property = gnome_rr_screen_get_property;
gobject_class->finalize = gnome_rr_screen_finalize;
screen_properties[SCREEN_PROP_GDK_SCREEN] = g_param_spec_object (
"gdk-screen",
"GDK Screen",
"The GDK Screen represented by this GnomeRRScreen",
GDK_TYPE_SCREEN,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
g_object_class_install_property(
gobject_class,
SCREEN_PROP_GDK_SCREEN,
screen_properties[SCREEN_PROP_GDK_SCREEN]);
screen_signals[SCREEN_CHANGED] = g_signal_new("changed",
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
G_STRUCT_OFFSET (GnomeRRScreenClass, changed),
NULL,
NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
}
void
gnome_rr_screen_init (GnomeRRScreen *self)
{
GnomeRRScreenPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GNOME_TYPE_RR_SCREEN, GnomeRRScreenPrivate);
self->priv = priv;
priv->gdk_screen = NULL;
priv->info = NULL;
}
/**
* gnome_rr_screen_new:
* Creates a new #GnomeRRScreen instance
*
* @screen: the #GdkScreen on which to operate
* @error: will be set if screen could not be created
*
* Returns: a new #GnomeRRScreen instance or NULL if screen could not be created,
* for instance if the driver does not support Xrandr 1.2
*/
GnomeRRScreen *
gnome_rr_screen_new (GdkScreen *screen,
GError **error)
{
/* FIXME: _gnome_desktop_init_i18n (); */
return g_initable_new (
#ifdef HAVE_X11
GNOME_TYPE_RR_X11_SCREEN,
#else
GNOME_TYPE_RR_WINDOWS_SCREEN,
#endif
NULL, error, "gdk-screen", screen, NULL);
}
/**
* gnome_rr_screen_get_ranges:
*
* Get the ranges of the screen
* @screen: a #GnomeRRScreen
* @min_width: (out): the minimum width
* @max_width: (out): the maximum width
* @min_height: (out): the minimum height
* @max_height: (out): the maximum height
*/
void
gnome_rr_screen_get_ranges (GnomeRRScreen *screen,
int *min_width,
int *max_width,
int *min_height,
int *max_height)
{
GnomeRRScreenPrivate *priv;
g_return_if_fail (GNOME_IS_RR_SCREEN (screen));
priv = screen->priv;
if (min_width)
*min_width = priv->info->min_width;
if (max_width)
*max_width = priv->info->max_width;
if (min_height)
*min_height = priv->info->min_height;
if (max_height)
*max_height = priv->info->max_height;
}
/**
* gnome_rr_screen_get_timestamps:
* @screen: a #GnomeRRScreen
* @change_timestamp_ret: (out): Location in which to store the timestamp at which the RANDR configuration was last changed
* @config_timestamp_ret: (out): Location in which to store the timestamp at which the RANDR configuration was last obtained
*
* Queries the two timestamps that the X RANDR extension maintains. The X
* server will prevent change requests for stale configurations, those whose
* timestamp is not equal to that of the latest request for configuration. The
* X server will also prevent change requests that have an older timestamp to
* the latest change request.
*/
void
gnome_rr_screen_get_timestamps (GnomeRRScreen *screen,
guint32 *change_timestamp_ret,
guint32 *config_timestamp_ret)
{
GnomeRRScreenPrivate *priv;
g_return_if_fail (GNOME_IS_RR_SCREEN (screen));
priv = screen->priv;
#ifdef HAVE_RANDR
if (change_timestamp_ret)
*change_timestamp_ret = priv->info->resources->timestamp;
if (config_timestamp_ret)
*config_timestamp_ret = priv->info->resources->configTimestamp;
#endif
}
/**
* gnome_rr_screen_refresh:
* @screen: a #GnomeRRScreen
* @error: location to store error, or %NULL
*
* Refreshes the screen configuration, and calls the screen's callback if it
* exists and if the screen's configuration changed.
*
* Return value: TRUE if the screen's configuration changed; otherwise, the
* function returns FALSE and a NULL error if the configuration didn't change,
* or FALSE and a non-NULL error if there was an error while refreshing the
* configuration.
*/
gboolean
gnome_rr_screen_refresh (GnomeRRScreen *screen,
GError **error)
{
gboolean refreshed;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
#ifdef HAVE_X11
gdk_x11_display_grab (gdk_screen_get_display (screen->priv->gdk_screen));
#endif
refreshed = screen_update (screen, FALSE, TRUE, error);
#ifdef HAVE_X11
gnome_rr_x11_screen_force_timestamp_update (GNOME_RR_X11_SCREEN (screen)); /* this is to keep other clients from thinking that the X server re-detected things by itself - bgo#621046 */
gdk_x11_display_ungrab (gdk_screen_get_display (screen->priv->gdk_screen));
#endif
return refreshed;
}
/**
* gnome_rr_screen_list_modes:
*
* List available XRandR modes
*
* Returns: (array zero-terminated=1) (transfer none):
*/
GnomeRRMode **
gnome_rr_screen_list_modes (GnomeRRScreen *screen)
{
g_return_val_if_fail (GNOME_IS_RR_SCREEN (screen), NULL);
g_return_val_if_fail (screen->priv->info != NULL, NULL);
return screen->priv->info->modes;
}
/**
* gnome_rr_screen_list_clone_modes:
*
* List available XRandR clone modes
*
* Returns: (array zero-terminated=1) (transfer none):
*/
GnomeRRMode **
gnome_rr_screen_list_clone_modes (GnomeRRScreen *screen)
{
g_return_val_if_fail (GNOME_IS_RR_SCREEN (screen), NULL);
g_return_val_if_fail (screen->priv->info != NULL, NULL);
return screen->priv->info->clone_modes;
}
/**
* gnome_rr_screen_list_crtcs:
*
* List all CRTCs
*
* Returns: (array zero-terminated=1) (transfer none):
*/
GnomeRRCrtc **
gnome_rr_screen_list_crtcs (GnomeRRScreen *screen)
{
g_return_val_if_fail (GNOME_IS_RR_SCREEN (screen), NULL);
g_return_val_if_fail (screen->priv->info != NULL, NULL);
return screen->priv->info->crtcs;
}
/**
* gnome_rr_screen_list_outputs:
*
* List all outputs
*
* Returns: (array zero-terminated=1) (transfer none):
*/
GnomeRROutput **
gnome_rr_screen_list_outputs (GnomeRRScreen *screen)
{
g_return_val_if_fail (GNOME_IS_RR_SCREEN (screen), NULL);
g_return_val_if_fail (screen->priv->info != NULL, NULL);
return screen->priv->info->outputs;
}
/**
* gnome_rr_screen_get_crtc_by_id:
*
* Returns: (transfer none): the CRTC identified by @id
*/
GnomeRRCrtc *
gnome_rr_screen_get_crtc_by_id (GnomeRRScreen *screen,
guint32 id)
{
GnomeRRCrtc **crtcs;
int i;
g_return_val_if_fail (GNOME_IS_RR_SCREEN (screen), NULL);
g_return_val_if_fail (screen->priv->info != NULL, NULL);
crtcs = screen->priv->info->crtcs;
for (i = 0; crtcs[i] != NULL; ++i)
{
if (crtcs[i]->id == id)
return crtcs[i];
}
return NULL;
}
/**
* gnome_rr_screen_get_output_by_id:
*
* Returns: (transfer none): the output identified by @id
*/
GnomeRROutput *
gnome_rr_screen_get_output_by_id (GnomeRRScreen *screen,
guint32 id)
{
GnomeRROutput **outputs;
int i;
g_return_val_if_fail (GNOME_IS_RR_SCREEN (screen), NULL);
g_return_val_if_fail (screen->priv->info != NULL, NULL);
outputs = screen->priv->info->outputs;
for (i = 0; outputs[i] != NULL; ++i)
{
if (outputs[i]->id == id)
return outputs[i];
}
return NULL;
}
/* GnomeRROutput */
GnomeRROutput *
output_new (ScreenInfo *info, RROutput id)
{
GnomeRROutput *output = g_slice_new0 (GnomeRROutput);
output->id = id;
output->info = info;
return output;
}
static GnomeRROutput*
output_copy (const GnomeRROutput *from)
{
GPtrArray *array;
GnomeRRCrtc **p_crtc;
GnomeRROutput **p_output;
GnomeRRMode **p_mode;
GnomeRROutput *output = g_slice_new0 (GnomeRROutput);
output->id = from->id;
output->info = from->info;
output->name = g_strdup (from->name);
output->current_crtc = from->current_crtc;
output->width_mm = from->width_mm;
output->height_mm = from->height_mm;
output->connected = from->connected;
output->n_preferred = from->n_preferred;
output->connector_type = g_strdup (from->connector_type);
array = g_ptr_array_new ();
for (p_crtc = from->possible_crtcs; *p_crtc != NULL; p_crtc++)
{
g_ptr_array_add (array, *p_crtc);
}
output->possible_crtcs = (GnomeRRCrtc**) g_ptr_array_free (array, FALSE);
array = g_ptr_array_new ();
for (p_output = from->clones; *p_output != NULL; p_output++)
{
g_ptr_array_add (array, *p_output);
}
output->clones = (GnomeRROutput**) g_ptr_array_free (array, FALSE);
array = g_ptr_array_new ();
for (p_mode = from->modes; *p_mode != NULL; p_mode++)
{
g_ptr_array_add (array, *p_mode);
}
output->modes = (GnomeRRMode**) g_ptr_array_free (array, FALSE);
output->edid_size = from->edid_size;
output->edid_data = g_memdup (from->edid_data, from->edid_size);
return output;
}
static void
output_free (GnomeRROutput *output)
{
g_free (output->clones);
g_free (output->modes);
g_free (output->possible_crtcs);
g_free (output->edid_data);
g_free (output->name);
g_free (output->connector_type);
g_slice_free (GnomeRROutput, output);
}
guint32
gnome_rr_output_get_id (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, 0);
return output->id;
}
const guint8 *
gnome_rr_output_get_edid_data (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, NULL);
return output->edid_data;
}
/**
* gnome_rr_screen_get_output_by_id:
*
* Returns: (transfer none): the output identified by @name
*/
GnomeRROutput *
gnome_rr_screen_get_output_by_name (GnomeRRScreen *screen,
const char *name)
{
int i;
g_return_val_if_fail (GNOME_IS_RR_SCREEN (screen), NULL);
g_return_val_if_fail (screen->priv->info != NULL, NULL);
for (i = 0; screen->priv->info->outputs[i] != NULL; ++i)
{
GnomeRROutput *output = screen->priv->info->outputs[i];
if (strcmp (output->name, name) == 0)
return output;
}
return NULL;
}
GnomeRRCrtc *
gnome_rr_output_get_crtc (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, NULL);
return output->current_crtc;
}
/* Returns NULL if the ConnectorType property is not available */
const char *
gnome_rr_output_get_connector_type (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, NULL);
return output->connector_type;
}
gboolean
gnome_rr_output_is_laptop (GnomeRROutput *output)
{
const char *connector_type;
g_return_val_if_fail (output != NULL, FALSE);
if (!output->connected)
return FALSE;
/* The ConnectorType property is present in RANDR 1.3 and greater */
connector_type = gnome_rr_output_get_connector_type (output);
if (connector_type && strcmp (connector_type, GNOME_RR_CONNECTOR_TYPE_PANEL) == 0)
return TRUE;
/* Older versions of RANDR - this is a best guess, as @#$% RANDR doesn't have standard output names,
* so drivers can use whatever they like.
*/
if (output->name
&& (strstr (output->name, "lvds") || /* Most drivers use an "LVDS" prefix... */
strstr (output->name, "LVDS") ||
strstr (output->name, "Lvds") ||
strstr (output->name, "LCD"))) /* ... but fglrx uses "LCD" in some versions. Shoot me now, kthxbye. */
return TRUE;
return FALSE;
}
GnomeRRMode *
gnome_rr_output_get_current_mode (GnomeRROutput *output)
{
GnomeRRCrtc *crtc;
g_return_val_if_fail (output != NULL, NULL);
if ((crtc = gnome_rr_output_get_crtc (output)))
return gnome_rr_crtc_get_current_mode (crtc);
return NULL;
}
void
gnome_rr_output_get_position (GnomeRROutput *output,
int *x,
int *y)
{
GnomeRRCrtc *crtc;
g_return_if_fail (output != NULL);
if ((crtc = gnome_rr_output_get_crtc (output)))
gnome_rr_crtc_get_position (crtc, x, y);
}
const char *
gnome_rr_output_get_name (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, "");
return output->name;
}
int
gnome_rr_output_get_width_mm (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, -1);
return output->width_mm;
}
int
gnome_rr_output_get_height_mm (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, -1);
return output->height_mm;
}
GnomeRRMode *
gnome_rr_output_get_preferred_mode (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, NULL);
if (output->n_preferred)
return output->modes[0];
return NULL;
}
GnomeRRMode **
gnome_rr_output_list_modes (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, NULL);
return output->modes;
}
gboolean
gnome_rr_output_is_connected (GnomeRROutput *output)
{
g_return_val_if_fail (output != NULL, FALSE);
return output->connected;
}
gboolean
gnome_rr_output_supports_mode (GnomeRROutput *output,
GnomeRRMode *mode)
{
int i;
g_return_val_if_fail (output != NULL, FALSE);
g_return_val_if_fail (mode != NULL, FALSE);
for (i = 0; output->modes[i] != NULL; ++i)
{
if (output->modes[i] == mode)
return TRUE;
}
return FALSE;
}
gboolean
gnome_rr_output_can_clone (GnomeRROutput *output,
GnomeRROutput *clone)
{
int i;
g_return_val_if_fail (output != NULL, FALSE);
g_return_val_if_fail (clone != NULL, FALSE);
for (i = 0; output->clones[i] != NULL; ++i)
{
if (output->clones[i] == clone)
return TRUE;
}
return FALSE;
}
gboolean
gnome_rr_output_get_is_primary (GnomeRROutput *output)
{
#ifdef HAVE_RANDR
return output->info->primary == output->id;
#else
return FALSE;
#endif
}
void
gnome_rr_screen_set_primary_output (GnomeRRScreen *screen,
GnomeRROutput *output)
{
GnomeRRScreenPrivate *priv;
g_return_if_fail (GNOME_IS_RR_SCREEN (screen));
priv = screen->priv;
#if RANDR_LIBRARY_IS_AT_LEAST_1_3
RROutput id;
if (output)
id = output->id;
else
id = None;
if (SERVERS_RANDR_IS_AT_LEAST_1_3 (priv))
XRRSetOutputPrimary (priv->xdisplay, priv->xroot, id);
#endif
}
#ifndef GNOME_DISABLE_DEPRECATED_SOURCE
gboolean
gnome_rr_crtc_set_config (GnomeRRCrtc *crtc,
int x,
int y,
GnomeRRMode *mode,
GnomeRRRotation rotation,
GnomeRROutput **outputs,
int n_outputs,
GError **error)
{
return gnome_rr_crtc_set_config_with_time (crtc, GDK_CURRENT_TIME, x, y, mode, rotation, outputs, n_outputs, error);
}
#endif
GnomeRRMode *
gnome_rr_crtc_get_current_mode (GnomeRRCrtc *crtc)
{
g_return_val_if_fail (crtc != NULL, NULL);
return crtc->current_mode;
}
guint32
gnome_rr_crtc_get_id (GnomeRRCrtc *crtc)
{
g_return_val_if_fail (crtc != NULL, 0);
return crtc->id;
}
gboolean
gnome_rr_crtc_can_drive_output (GnomeRRCrtc *crtc,
GnomeRROutput *output)
{
int i;
g_return_val_if_fail (crtc != NULL, FALSE);
g_return_val_if_fail (output != NULL, FALSE);
for (i = 0; crtc->possible_outputs[i] != NULL; ++i)
{
if (crtc->possible_outputs[i] == output)
return TRUE;
}
return FALSE;
}
/* FIXME: merge with get_mode()? */
void
gnome_rr_crtc_get_position (GnomeRRCrtc *crtc,
int *x,
int *y)
{
g_return_if_fail (crtc != NULL);
if (x)
*x = crtc->x;
if (y)
*y = crtc->y;
}
/* FIXME: merge with get_mode()? */
GnomeRRRotation
gnome_rr_crtc_get_current_rotation (GnomeRRCrtc *crtc)
{
g_return_val_if_fail (crtc != NULL, GNOME_RR_ROTATION_0);
return crtc->current_rotation;
}
GnomeRRRotation
gnome_rr_crtc_get_rotations (GnomeRRCrtc *crtc)
{
g_return_val_if_fail (crtc != NULL, GNOME_RR_ROTATION_0);
return crtc->rotations;
}
gboolean
gnome_rr_crtc_supports_rotation (GnomeRRCrtc * crtc,
GnomeRRRotation rotation)
{
g_return_val_if_fail (crtc != NULL, FALSE);
return (crtc->rotations & rotation);
}
GnomeRRCrtc *
crtc_new (ScreenInfo *info, RROutput id)
{
GnomeRRCrtc *crtc = g_slice_new0 (GnomeRRCrtc);
crtc->id = id;
crtc->info = info;
return crtc;
}
static GnomeRRCrtc *
crtc_copy (const GnomeRRCrtc *from)
{
GnomeRROutput **p_output;
GPtrArray *array;
GnomeRRCrtc *to = g_slice_new0 (GnomeRRCrtc);
to->info = from->info;
to->id = from->id;
to->current_mode = from->current_mode;
to->x = from->x;
to->y = from->y;
to->current_rotation = from->current_rotation;
to->rotations = from->rotations;
to->gamma_size = from->gamma_size;
array = g_ptr_array_new ();
for (p_output = from->current_outputs; *p_output != NULL; p_output++)
{
g_ptr_array_add (array, *p_output);
}
to->current_outputs = (GnomeRROutput**) g_ptr_array_free (array, FALSE);
array = g_ptr_array_new ();
for (p_output = from->possible_outputs; *p_output != NULL; p_output++)
{
g_ptr_array_add (array, *p_output);
}
to->possible_outputs = (GnomeRROutput**) g_ptr_array_free (array, FALSE);
return to;
}
static void
crtc_free (GnomeRRCrtc *crtc)
{
g_free (crtc->current_outputs);
g_free (crtc->possible_outputs);
g_slice_free (GnomeRRCrtc, crtc);
}
/* GnomeRRMode */
GnomeRRMode *
mode_new (ScreenInfo *info, RRMode id)
{
GnomeRRMode *mode = g_slice_new0 (GnomeRRMode);
mode->id = id;
mode->info = info;
return mode;
}
guint32
gnome_rr_mode_get_id (GnomeRRMode *mode)
{
g_return_val_if_fail (mode != NULL, 0);
return mode->id;
}
guint
gnome_rr_mode_get_width (GnomeRRMode *mode)
{
g_return_val_if_fail (mode != NULL, 0);
return mode->width;
}
int
gnome_rr_mode_get_freq (GnomeRRMode *mode)
{
g_return_val_if_fail (mode != NULL, 0);
return (mode->freq) / 1000;
}
guint
gnome_rr_mode_get_height (GnomeRRMode *mode)
{
g_return_val_if_fail (mode != NULL, 0);
return mode->height;
}
static GnomeRRMode *
mode_copy (const GnomeRRMode *from)
{
GnomeRRMode *to = g_slice_new0 (GnomeRRMode);
to->id = from->id;
to->info = from->info;
to->name = g_strdup (from->name);
to->width = from->width;
to->height = from->height;
to->freq = from->freq;
return to;
}
static void
mode_free (GnomeRRMode *mode)
{
g_free (mode->name);
g_slice_free (GnomeRRMode, mode);
}
GdkScreen *
gnome_rr_screen_get_gdk_screen (GnomeRRScreen *self)
{
g_return_val_if_fail (self != NULL, NULL);
return self->priv->gdk_screen;
}
|