1
/* Monitor Settings. A preference panel for configuring monitors
3
* Copyright (C) 2007, 2008 Red Hat, Inc.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* Author: Soren Sandmann <sandmann@redhat.com>
28
#include "scrollarea.h"
29
#define GNOME_DESKTOP_USE_UNSTABLE_API
30
#include <libgnome-desktop/gnome-rr.h>
31
#include <libgnome-desktop/gnome-rr-config.h>
32
#include <libgnome-desktop/gnome-rr-labeler.h>
35
#include <glib/gi18n.h>
36
#include <dbus/dbus-glib.h>
37
#include <dbus/dbus-glib-bindings.h>
38
#include <gdesktop-enums.h>
40
#include "xrandr-capplet.h"
42
#define TOP_BAR_HEIGHT 10
44
#define CLOCK_SCHEMA "org.gnome.desktop.interface"
45
#define CLOCK_FORMAT_KEY "clock-format"
47
/* The minimum supported size for the panel, see:
48
* http://live.gnome.org/Design/SystemSettings */
49
#define MINIMUM_WIDTH 675
50
#define MINIMUM_HEIGHT 530
52
typedef struct App App;
53
typedef struct GrabInfo GrabInfo;
57
GnomeRRScreen *screen;
58
GnomeRRConfig *current_configuration;
59
GnomeRRLabeler *labeler;
60
GnomeRROutputInfo *current_output;
62
GSettings *clock_settings;
66
GtkWidget *current_monitor_event_box;
67
GtkWidget *current_monitor_label;
68
GtkWidget *monitor_switch;
69
GtkListStore *resolution_store;
70
GtkWidget *resolution_combo;
71
GtkWidget *rotation_combo;
72
GtkWidget *clone_checkbox;
73
GtkWidget *clone_label;
74
GtkWidget *show_icon_checkbox;
76
/* We store the event timestamp when the Apply button is clicked */
77
guint32 apply_button_clicked_timestamp;
80
gboolean ignore_gui_changes;
81
gboolean dragging_top_bar;
83
/* These are used while we are waiting for the ApplyConfiguration method to be executed over D-bus */
84
DBusGConnection *connection;
86
DBusGProxyCall *proxy_call;
89
static void rebuild_gui (App *app);
90
static void on_clone_changed (GtkWidget *box, gpointer data);
91
static gboolean output_overlaps (GnomeRROutputInfo *output, GnomeRRConfig *config);
92
static void select_current_output_from_dialog_position (App *app);
93
static void monitor_switch_active_cb (GObject *object, GParamSpec *pspec, gpointer data);
94
static void get_geometry (GnomeRROutputInfo *output, int *w, int *h);
95
static void apply_configuration_returned_cb (DBusGProxy *proxy, DBusGProxyCall *call_id, void *data);
96
static gboolean get_clone_size (GnomeRRScreen *screen, int *width, int *height);
97
static gboolean output_info_supports_mode (App *app, GnomeRROutputInfo *info, int width, int height);
100
error_message (App *app, const char *primary_text, const char *secondary_text)
105
if (app && app->panel)
106
toplevel = gtk_widget_get_toplevel (app->panel);
110
dialog = gtk_message_dialog_new (GTK_WINDOW (toplevel),
111
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
117
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", secondary_text);
119
gtk_dialog_run (GTK_DIALOG (dialog));
120
gtk_widget_destroy (dialog);
124
do_free (gpointer data)
133
g_idle_add (do_free, s);
139
should_show_resolution (gint output_width,
144
if (width >= MIN (output_width, MINIMUM_WIDTH) &&
145
height >= MIN (output_height, MINIMUM_HEIGHT))
153
on_screen_changed (GnomeRRScreen *scr,
156
GnomeRRConfig *current;
159
current = gnome_rr_config_new_current (app->screen, NULL);
160
gnome_rr_config_ensure_primary (current);
162
if (app->current_configuration)
163
g_object_unref (app->current_configuration);
165
app->current_configuration = current;
166
app->current_output = NULL;
169
gnome_rr_labeler_hide (app->labeler);
170
g_object_unref (app->labeler);
173
app->labeler = gnome_rr_labeler_new (app->current_configuration);
175
select_current_output_from_dialog_position (app);
179
on_viewport_changed (FooScrollArea *scroll_area,
180
GdkRectangle *old_viewport,
181
GdkRectangle *new_viewport)
183
foo_scroll_area_set_size (scroll_area,
185
new_viewport->height);
187
foo_scroll_area_invalidate (scroll_area);
191
layout_set_font (PangoLayout *layout, const char *font)
193
PangoFontDescription *desc =
194
pango_font_description_from_string (font);
198
pango_layout_set_font_description (layout, desc);
200
pango_font_description_free (desc);
205
clear_combo (GtkWidget *widget)
207
GtkComboBox *box = GTK_COMBO_BOX (widget);
208
GtkTreeModel *model = gtk_combo_box_get_model (box);
209
GtkListStore *store = GTK_LIST_STORE (model);
211
gtk_list_store_clear (store);
222
foreach (GtkTreeModel *model,
227
ForeachInfo *info = data;
230
gtk_tree_model_get (model, iter, 0, &text, -1);
232
g_assert (text != NULL);
234
if (strcmp (info->text, text) == 0)
245
add_key (GtkWidget *widget,
247
int width, int height, int rate,
248
GnomeRRRotation rotation)
251
GtkComboBox *box = GTK_COMBO_BOX (widget);
252
GtkTreeModel *model = gtk_combo_box_get_model (box);
253
GtkListStore *store = GTK_LIST_STORE (model);
258
gtk_tree_model_foreach (model, foreach, &info);
263
gtk_list_store_insert_with_values (store, &iter, -1,
276
combo_select (GtkWidget *widget, const char *text)
278
GtkComboBox *box = GTK_COMBO_BOX (widget);
279
GtkTreeModel *model = gtk_combo_box_get_model (box);
285
gtk_tree_model_foreach (model, foreach, &info);
290
gtk_combo_box_set_active_iter (box, &info.iter);
294
static GnomeRRMode **
295
get_current_modes (App *app)
297
GnomeRROutput *output;
299
if (gnome_rr_config_get_clone (app->current_configuration))
301
return gnome_rr_screen_list_clone_modes (app->screen);
305
if (!app->current_output)
308
output = gnome_rr_screen_get_output_by_name (app->screen,
309
gnome_rr_output_info_get_name (app->current_output));
314
return gnome_rr_output_list_modes (output);
319
rebuild_rotation_combo (App *app)
323
GnomeRRRotation rotation;
326
static const RotationInfo rotations[] = {
327
{ GNOME_RR_ROTATION_0, N_("Normal") },
328
{ GNOME_RR_ROTATION_90, N_("Anti-Clockwise") },
329
{ GNOME_RR_ROTATION_270, N_("Clockwise") },
330
{ GNOME_RR_ROTATION_180, N_("180 Degrees") },
332
const char *selection;
333
GnomeRRRotation current;
336
clear_combo (app->rotation_combo);
338
gtk_widget_set_sensitive (app->rotation_combo,
339
app->current_output && gnome_rr_output_info_is_active (app->current_output));
341
if (!app->current_output)
344
current = gnome_rr_output_info_get_rotation (app->current_output);
347
for (i = 0; i < G_N_ELEMENTS (rotations); ++i)
349
const RotationInfo *info = &(rotations[i]);
351
gnome_rr_output_info_set_rotation (app->current_output, info->rotation);
353
/* NULL-GError --- FIXME: we should say why this rotation is not available! */
354
if (gnome_rr_config_applicable (app->current_configuration, app->screen, NULL))
356
add_key (app->rotation_combo, _(info->name), 0, 0, 0, info->rotation);
358
if (info->rotation == current)
359
selection = _(info->name);
363
gnome_rr_output_info_set_rotation (app->current_output, current);
365
if (!(selection && combo_select (app->rotation_combo, selection)))
366
combo_select (app->rotation_combo, _("Normal"));
370
count_active_outputs (App *app)
373
GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (app->current_configuration);
375
for (i = 0; outputs[i] != NULL; ++i)
377
if (gnome_rr_output_info_is_active (outputs[i]))
386
count_all_outputs (GnomeRRConfig *config)
389
GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (config);
391
for (i = 0; outputs[i] != NULL; i++)
398
/* Computes whether "Mirror displays" (clone mode) is supported based on these criteria:
400
* 1. There is an available size for cloning.
402
* 2. There are 2 or more connected outputs that support that size.
405
mirror_screens_is_supported (App *app)
407
int clone_width, clone_height;
408
gboolean have_clone_size;
409
gboolean mirror_is_supported;
411
mirror_is_supported = FALSE;
413
have_clone_size = get_clone_size (app->screen, &clone_width, &clone_height);
415
if (have_clone_size) {
417
int num_outputs_with_clone_size;
418
GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (app->current_configuration);
420
num_outputs_with_clone_size = 0;
422
for (i = 0; outputs[i] != NULL; i++)
424
/* We count the connected outputs that support the clone size. It
425
* doesn't matter if those outputs aren't actually On currently; we
426
* will turn them on in on_clone_changed().
428
if (gnome_rr_output_info_is_connected (outputs[i]) && output_info_supports_mode (app, outputs[i], clone_width, clone_height))
429
num_outputs_with_clone_size++;
432
if (num_outputs_with_clone_size >= 2)
433
mirror_is_supported = TRUE;
436
return mirror_is_supported;
440
rebuild_mirror_screens (App *app)
442
gboolean mirror_is_active;
443
gboolean mirror_is_supported;
445
g_signal_handlers_block_by_func (app->clone_checkbox, G_CALLBACK (on_clone_changed), app);
447
mirror_is_active = app->current_configuration && gnome_rr_config_get_clone (app->current_configuration);
449
/* If mirror_is_active, then it *must* be possible to turn mirroring off */
450
mirror_is_supported = mirror_is_active || mirror_screens_is_supported (app);
452
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->clone_checkbox), mirror_is_active);
453
gtk_widget_set_sensitive (app->clone_checkbox, mirror_is_supported);
454
gtk_widget_set_sensitive (app->clone_label, mirror_is_supported);
456
g_signal_handlers_unblock_by_func (app->clone_checkbox, G_CALLBACK (on_clone_changed), app);
460
mirror_monitor_name (void)
462
/* Translators: this is the feature where what you see on your laptop's
463
* screen is the same as your external monitor. Here, "Mirror" is being
464
* used as an adjective, not as a verb. For example, the Spanish
465
* translation could be "Pantallas en Espejo", *not* "Espejar Pantallas".
467
return g_strdup (_("Mirror Displays"));
471
rebuild_current_monitor_label (App *app)
477
if (app->current_output)
479
if (gnome_rr_config_get_clone (app->current_configuration))
480
tmp = mirror_monitor_name ();
482
tmp = g_strdup (gnome_rr_output_info_get_display_name (app->current_output));
484
str = g_strdup_printf ("<b>%s</b>", tmp);
485
gnome_rr_labeler_get_color_for_output (app->labeler, app->current_output, &color);
491
str = g_strdup_printf ("<b>%s</b>", _("Monitor"));
495
gtk_label_set_markup (GTK_LABEL (app->current_monitor_label), str);
500
GdkRGBA black = { 0, 0, 0, 1.0 };
503
light.red = color.red / 65535.0;
504
light.green = color.green / 65535.0;
505
light.blue = color.blue / 65535.0;
507
gtk_widget_override_background_color (app->current_monitor_event_box,
508
gtk_widget_get_state_flags (app->current_monitor_event_box),
511
/* Make the label explicitly black. We don't want it to follow the
512
* theme's colors, since the label is always shown against a light
513
* pastel background. See bgo#556050
515
gtk_widget_override_color (app->current_monitor_label,
516
gtk_widget_get_state_flags (app->current_monitor_label),
521
/* Remove any modifications we did on the label's color */
522
gtk_widget_override_color (app->current_monitor_label,
523
gtk_widget_get_state_flags (app->current_monitor_label),
527
gtk_event_box_set_visible_window (GTK_EVENT_BOX (app->current_monitor_event_box), use_color);
531
rebuild_on_off_radios (App *app)
536
g_signal_handlers_block_by_func (app->monitor_switch, G_CALLBACK (monitor_switch_active_cb), app);
541
if (!gnome_rr_config_get_clone (app->current_configuration) && app->current_output)
543
if (count_active_outputs (app) > 1 || !gnome_rr_output_info_is_active (app->current_output))
548
on_active = gnome_rr_output_info_is_active (app->current_output);
551
gtk_widget_set_sensitive (app->monitor_switch, sensitive);
553
gtk_switch_set_active (GTK_SWITCH (app->monitor_switch), on_active);
555
g_signal_handlers_unblock_by_func (app->monitor_switch, G_CALLBACK (monitor_switch_active_cb), app);
559
make_resolution_string (int width, int height)
562
const char *aspect = NULL;
564
if (width && height) {
566
ratio = width * 10 / height;
568
ratio = height * 10 / width;
583
/* This catches 1.5625 as well (1600x1024) when maybe it shouldn't. */
597
return g_strdup_printf (_("%d x %d (%s)"), width, height, aspect);
599
return g_strdup_printf (_("%d x %d"), width, height);
603
find_best_mode (GnomeRRMode **modes, int *out_width, int *out_height)
610
for (i = 0; modes[i] != NULL; i++)
614
w = gnome_rr_mode_get_width (modes[i]);
615
h = gnome_rr_mode_get_height (modes[i]);
617
if (w * h > *out_width * *out_height)
626
rebuild_resolution_combo (App *app)
631
int output_width, output_height;
633
clear_combo (app->resolution_combo);
635
if (!(modes = get_current_modes (app))
636
|| !app->current_output
637
|| !gnome_rr_output_info_is_active (app->current_output))
639
gtk_widget_set_sensitive (app->resolution_combo, FALSE);
643
g_assert (app->current_output != NULL);
645
gnome_rr_output_info_get_geometry (app->current_output, NULL, NULL, &output_width, &output_height);
646
g_assert (output_width != 0 && output_height != 0);
648
gtk_widget_set_sensitive (app->resolution_combo, TRUE);
650
for (i = 0; modes[i] != NULL; ++i)
654
width = gnome_rr_mode_get_width (modes[i]);
655
height = gnome_rr_mode_get_height (modes[i]);
657
if (should_show_resolution (output_width, output_height, width, height))
659
add_key (app->resolution_combo,
660
idle_free (make_resolution_string (width, height)),
661
width, height, 0, -1);
665
current = idle_free (make_resolution_string (output_width, output_height));
667
if (!combo_select (app->resolution_combo, current))
671
find_best_mode (modes, &best_w, &best_h);
672
combo_select (app->resolution_combo, idle_free (make_resolution_string (best_w, best_h)));
677
rebuild_gui (App *app)
679
/* We would break spectacularly if we recursed, so
680
* just assert if that happens
682
g_assert (app->ignore_gui_changes == FALSE);
684
app->ignore_gui_changes = TRUE;
686
rebuild_mirror_screens (app);
687
rebuild_current_monitor_label (app);
688
rebuild_on_off_radios (app);
689
rebuild_resolution_combo (app);
690
rebuild_rotation_combo (app);
692
app->ignore_gui_changes = FALSE;
696
get_mode (GtkWidget *widget, int *width, int *height, int *freq, GnomeRRRotation *rot)
700
GtkComboBox *box = GTK_COMBO_BOX (widget);
703
if (!gtk_combo_box_get_active_iter (box, &iter))
716
rot = (GnomeRRRotation *)&dummy;
718
model = gtk_combo_box_get_model (box);
719
gtk_tree_model_get (model, &iter,
731
on_rotation_changed (GtkComboBox *box, gpointer data)
734
GnomeRRRotation rotation;
736
if (!app->current_output)
739
if (get_mode (app->rotation_combo, NULL, NULL, NULL, &rotation))
740
gnome_rr_output_info_set_rotation (app->current_output, rotation);
742
foo_scroll_area_invalidate (FOO_SCROLL_AREA (app->area));
746
select_resolution_for_current_output (App *app)
751
gnome_rr_output_info_get_geometry (app->current_output, &x, &y, NULL, NULL);
753
width = gnome_rr_output_info_get_preferred_width (app->current_output);
754
height = gnome_rr_output_info_get_preferred_height (app->current_output);
756
if (width != 0 && height != 0)
758
gnome_rr_output_info_set_geometry (app->current_output, x, y, width, height);
762
modes = get_current_modes (app);
766
find_best_mode (modes, &width, &height);
768
gnome_rr_output_info_set_geometry (app->current_output, x, y, width, height);
772
monitor_switch_active_cb (GObject *object,
779
if (!app->current_output)
782
value = gtk_switch_get_active (GTK_SWITCH (object));
786
gnome_rr_output_info_set_active (app->current_output, TRUE);
787
select_resolution_for_current_output (app);
791
gnome_rr_output_info_set_active (app->current_output, FALSE);
792
gnome_rr_config_ensure_primary (app->current_configuration);
796
foo_scroll_area_invalidate (FOO_SCROLL_AREA (app->area));
800
realign_outputs_after_resolution_change (App *app, GnomeRROutputInfo *output_that_changed, int old_width, int old_height)
802
/* We find the outputs that were below or to the right of the output that
803
* changed, and realign them; we also do that for outputs that shared the
804
* right/bottom edges with the output that changed. The outputs that are
805
* above or to the left of that output don't need to change.
809
int old_right_edge, old_bottom_edge;
811
int x, y, width, height;
812
GnomeRROutputInfo **outputs;
814
g_assert (app->current_configuration != NULL);
816
gnome_rr_output_info_get_geometry (output_that_changed, &x, &y, &width, &height);
818
if (width == old_width && height == old_height)
821
old_right_edge = x + old_width;
822
old_bottom_edge = y + old_height;
824
dx = width - old_width;
825
dy = height - old_height;
827
outputs = gnome_rr_config_get_outputs (app->current_configuration);
829
for (i = 0; outputs[i] != NULL; i++)
831
int output_x, output_y;
832
int output_width, output_height;
834
if (outputs[i] == output_that_changed || !gnome_rr_output_info_is_connected (outputs[i]))
837
gnome_rr_output_info_get_geometry (outputs[i], &output_x, &output_y, &output_width, &output_height);
839
if (output_x >= old_right_edge)
841
else if (output_x + output_width == old_right_edge)
842
output_x = x + width - output_width;
844
if (output_y >= old_bottom_edge)
846
else if (output_y + output_height == old_bottom_edge)
847
output_y = y + height - output_height;
849
gnome_rr_output_info_set_geometry (outputs[i], output_x, output_y, output_width, output_height);
854
on_resolution_changed (GtkComboBox *box, gpointer data)
857
int old_width, old_height;
862
if (!app->current_output)
865
gnome_rr_output_info_get_geometry (app->current_output, &x, &y, &old_width, &old_height);
867
if (get_mode (app->resolution_combo, &width, &height, NULL, NULL))
869
gnome_rr_output_info_set_geometry (app->current_output, x, y, width, height);
871
if (width == 0 || height == 0)
872
gnome_rr_output_info_set_active (app->current_output, FALSE);
874
gnome_rr_output_info_set_active (app->current_output, TRUE);
877
realign_outputs_after_resolution_change (app, app->current_output, old_width, old_height);
879
rebuild_rotation_combo (app);
881
foo_scroll_area_invalidate (FOO_SCROLL_AREA (app->area));
885
lay_out_outputs_horizontally (App *app)
889
GnomeRROutputInfo **outputs;
891
/* Lay out all the monitors horizontally when "mirror screens" is turned
892
* off, to avoid having all of them overlapped initially. We put the
893
* outputs turned off on the right-hand side.
898
/* First pass, all "on" outputs */
899
outputs = gnome_rr_config_get_outputs (app->current_configuration);
901
for (i = 0; outputs[i]; ++i)
904
if (gnome_rr_output_info_is_connected (outputs[i]) && gnome_rr_output_info_is_active (outputs[i]))
906
gnome_rr_output_info_get_geometry (outputs[i], NULL, NULL, &width, &height);
907
gnome_rr_output_info_set_geometry (outputs[i], x, 0, width, height);
912
/* Second pass, all the black screens */
914
for (i = 0; outputs[i]; ++i)
917
if (!(gnome_rr_output_info_is_connected (outputs[i]) && gnome_rr_output_info_is_active (outputs[i])))
919
gnome_rr_output_info_get_geometry (outputs[i], NULL, NULL, &width, &height);
920
gnome_rr_output_info_set_geometry (outputs[i], x, 0, width, height);
927
/* FIXME: this function is copied from gnome-settings-daemon/plugins/xrandr/gsd-xrandr-manager.c.
928
* Do we need to put this function in gnome-desktop for public use?
931
get_clone_size (GnomeRRScreen *screen, int *width, int *height)
933
GnomeRRMode **modes = gnome_rr_screen_list_clone_modes (screen);
940
for (i = 0; modes[i] != NULL; ++i) {
941
GnomeRRMode *mode = modes[i];
944
w = gnome_rr_mode_get_width (mode);
945
h = gnome_rr_mode_get_height (mode);
947
if (w * h > best_w * best_h) {
953
if (best_w > 0 && best_h > 0) {
966
output_info_supports_mode (App *app, GnomeRROutputInfo *info, int width, int height)
968
GnomeRROutput *output;
972
if (!gnome_rr_output_info_is_connected (info))
975
output = gnome_rr_screen_get_output_by_name (app->screen, gnome_rr_output_info_get_name (info));
979
modes = gnome_rr_output_list_modes (output);
981
for (i = 0; modes[i]; i++) {
982
if (gnome_rr_mode_get_width (modes[i]) == width
983
&& gnome_rr_mode_get_height (modes[i]) == height)
991
on_clone_changed (GtkWidget *box, gpointer data)
995
gnome_rr_config_set_clone (app->current_configuration, gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (app->clone_checkbox)));
997
if (gnome_rr_config_get_clone (app->current_configuration))
1001
GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (app->current_configuration);
1003
for (i = 0; outputs[i]; ++i)
1005
if (gnome_rr_output_info_is_connected (outputs[i]))
1007
app->current_output = outputs[i];
1012
/* Turn on all the connected screens that support the best clone mode.
1013
* The user may hit "Mirror displays", but he shouldn't have to turn on
1014
* all the required outputs as well.
1017
get_clone_size (app->screen, &width, &height);
1019
for (i = 0; outputs[i]; i++) {
1021
if (output_info_supports_mode (app, outputs[i], width, height)) {
1022
gnome_rr_output_info_set_active (outputs[i], TRUE);
1023
gnome_rr_output_info_get_geometry (outputs[i], &x, &y, NULL, NULL);
1024
gnome_rr_output_info_set_geometry (outputs[i], x, y, width, height);
1030
if (output_overlaps (app->current_output, app->current_configuration))
1031
lay_out_outputs_horizontally (app);
1038
apply_rotation_to_geometry (GnomeRROutputInfo *output, int *w, int *h)
1040
GnomeRRRotation rotation;
1042
rotation = gnome_rr_output_info_get_rotation (output);
1043
if ((rotation & GNOME_RR_ROTATION_90) || (rotation & GNOME_RR_ROTATION_270))
1053
get_geometry (GnomeRROutputInfo *output, int *w, int *h)
1055
if (gnome_rr_output_info_is_active (output))
1057
gnome_rr_output_info_get_geometry (output, NULL, NULL, w, h);
1061
*h = gnome_rr_output_info_get_preferred_height (output);
1062
*w = gnome_rr_output_info_get_preferred_width (output);
1065
apply_rotation_to_geometry (output, w, h);
1072
list_connected_outputs (App *app, int *total_w, int *total_h)
1075
GList *result = NULL;
1076
GnomeRROutputInfo **outputs;
1086
outputs = gnome_rr_config_get_outputs (app->current_configuration);
1087
for (i = 0; outputs[i] != NULL; ++i)
1089
if (gnome_rr_output_info_is_connected (outputs[i]))
1093
result = g_list_prepend (result, outputs[i]);
1095
get_geometry (outputs[i], &w, &h);
1102
return g_list_reverse (result);
1106
get_n_connected (App *app)
1108
GList *connected_outputs = list_connected_outputs (app, NULL, NULL);
1109
int n = g_list_length (connected_outputs);
1111
g_list_free (connected_outputs);
1117
compute_scale (App *app)
1119
int available_w, available_h;
1120
int total_w, total_h;
1122
GdkRectangle viewport;
1123
GList *connected_outputs;
1125
foo_scroll_area_get_viewport (FOO_SCROLL_AREA (app->area), &viewport);
1127
connected_outputs = list_connected_outputs (app, &total_w, &total_h);
1129
n_monitors = g_list_length (connected_outputs);
1131
g_list_free (connected_outputs);
1133
available_w = viewport.width - 2 * MARGIN - (n_monitors - 1) * SPACE;
1134
available_h = viewport.height - 2 * MARGIN - (n_monitors - 1) * SPACE;
1136
return MIN ((double)available_w / total_w, (double)available_h / total_h);
1141
GnomeRROutputInfo *output;
1148
Edge *snapper; /* Edge that should be snapped */
1154
add_edge (GnomeRROutputInfo *output, int x1, int y1, int x2, int y2, GArray *edges)
1164
g_array_append_val (edges, e);
1168
list_edges_for_output (GnomeRROutputInfo *output, GArray *edges)
1172
gnome_rr_output_info_get_geometry (output, &x, &y, &w, &h);
1174
apply_rotation_to_geometry (output, &w, &h);
1176
/* Top, Bottom, Left, Right */
1177
add_edge (output, x, y, x + w, y, edges);
1178
add_edge (output, x, y + h, x + w, y + h, edges);
1179
add_edge (output, x, y, x, y + h, edges);
1180
add_edge (output, x + w, y, x + w, y + h, edges);
1184
list_edges (GnomeRRConfig *config, GArray *edges)
1187
GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (config);
1189
for (i = 0; outputs[i]; ++i)
1191
if (gnome_rr_output_info_is_connected (outputs[i]))
1192
list_edges_for_output (outputs[i], edges);
1197
overlap (int s1, int e1, int s2, int e2)
1199
return (!(e1 < s2 || s1 >= e2));
1203
horizontal_overlap (Edge *snapper, Edge *snappee)
1205
if (snapper->y1 != snapper->y2 || snappee->y1 != snappee->y2)
1208
return overlap (snapper->x1, snapper->x2, snappee->x1, snappee->x2);
1212
vertical_overlap (Edge *snapper, Edge *snappee)
1214
if (snapper->x1 != snapper->x2 || snappee->x1 != snappee->x2)
1217
return overlap (snapper->y1, snapper->y2, snappee->y1, snappee->y2);
1221
add_snap (GArray *snaps, Snap snap)
1223
if (ABS (snap.dx) <= 200 || ABS (snap.dy) <= 200)
1224
g_array_append_val (snaps, snap);
1228
add_edge_snaps (Edge *snapper, Edge *snappee, GArray *snaps)
1232
snap.snapper = snapper;
1233
snap.snappee = snappee;
1235
if (horizontal_overlap (snapper, snappee))
1238
snap.dy = snappee->y1 - snapper->y1;
1240
add_snap (snaps, snap);
1242
else if (vertical_overlap (snapper, snappee))
1245
snap.dx = snappee->x1 - snapper->x1;
1247
add_snap (snaps, snap);
1252
snap.dx = snappee->x1 - snapper->x1;
1253
snap.dy = snappee->y1 - snapper->y1;
1255
add_snap (snaps, snap);
1258
snap.dx = snappee->x2 - snapper->x1;
1259
snap.dy = snappee->y2 - snapper->y1;
1261
add_snap (snaps, snap);
1264
snap.dx = snappee->x2 - snapper->x2;
1265
snap.dy = snappee->y2 - snapper->y2;
1267
add_snap (snaps, snap);
1270
snap.dx = snappee->x1 - snapper->x2;
1271
snap.dy = snappee->y1 - snapper->y2;
1273
add_snap (snaps, snap);
1277
list_snaps (GnomeRROutputInfo *output, GArray *edges, GArray *snaps)
1281
for (i = 0; i < edges->len; ++i)
1283
Edge *output_edge = &(g_array_index (edges, Edge, i));
1285
if (output_edge->output == output)
1289
for (j = 0; j < edges->len; ++j)
1291
Edge *edge = &(g_array_index (edges, Edge, j));
1293
if (edge->output != output)
1294
add_edge_snaps (output_edge, edge, snaps);
1302
print_edge (Edge *edge)
1304
g_debug ("(%d %d %d %d)", edge->x1, edge->y1, edge->x2, edge->y2);
1309
corner_on_edge (int x, int y, Edge *e)
1311
if (x == e->x1 && x == e->x2 && y >= e->y1 && y <= e->y2)
1314
if (y == e->y1 && y == e->y2 && x >= e->x1 && x <= e->x2)
1321
edges_align (Edge *e1, Edge *e2)
1323
if (corner_on_edge (e1->x1, e1->y1, e2))
1326
if (corner_on_edge (e2->x1, e2->y1, e1))
1333
output_is_aligned (GnomeRROutputInfo *output, GArray *edges)
1335
gboolean result = FALSE;
1338
for (i = 0; i < edges->len; ++i)
1340
Edge *output_edge = &(g_array_index (edges, Edge, i));
1342
if (output_edge->output == output)
1346
for (j = 0; j < edges->len; ++j)
1348
Edge *edge = &(g_array_index (edges, Edge, j));
1350
/* We are aligned if an output edge matches
1351
* an edge of another output
1353
if (edge->output != output_edge->output)
1355
if (edges_align (output_edge, edge))
1370
get_output_rect (GnomeRROutputInfo *output, GdkRectangle *rect)
1372
gnome_rr_output_info_get_geometry (output, &rect->x, &rect->y, &rect->width, &rect->height);
1374
apply_rotation_to_geometry (output, &rect->width, &rect->height);
1378
output_overlaps (GnomeRROutputInfo *output, GnomeRRConfig *config)
1381
GdkRectangle output_rect;
1382
GnomeRROutputInfo **outputs;
1384
g_assert (output != NULL);
1386
get_output_rect (output, &output_rect);
1388
outputs = gnome_rr_config_get_outputs (config);
1389
for (i = 0; outputs[i]; ++i)
1391
if (outputs[i] != output && gnome_rr_output_info_is_connected (outputs[i]))
1393
GdkRectangle other_rect;
1395
get_output_rect (outputs[i], &other_rect);
1396
if (gdk_rectangle_intersect (&output_rect, &other_rect, NULL))
1405
gnome_rr_config_is_aligned (GnomeRRConfig *config, GArray *edges)
1408
gboolean result = TRUE;
1409
GnomeRROutputInfo **outputs;
1411
outputs = gnome_rr_config_get_outputs (config);
1412
for (i = 0; outputs[i]; ++i)
1414
if (gnome_rr_output_info_is_connected (outputs[i]))
1416
if (!output_is_aligned (outputs[i], edges))
1419
if (output_overlaps (outputs[i], config))
1436
is_corner_snap (const Snap *s)
1438
return s->dx != 0 && s->dy != 0;
1442
compare_snaps (gconstpointer v1, gconstpointer v2)
1444
const Snap *s1 = v1;
1445
const Snap *s2 = v2;
1446
int sv1 = MAX (ABS (s1->dx), ABS (s1->dy));
1447
int sv2 = MAX (ABS (s2->dx), ABS (s2->dy));
1452
/* This snapping algorithm is good enough for rock'n'roll, but
1453
* this is probably a better:
1455
* First do a horizontal/vertical snap, then
1456
* with the new coordinates from that snap,
1459
* Right now, it's confusing that corner snapping
1460
* depends on the distance in an axis that you can't actually see.
1465
if (is_corner_snap (s1) && !is_corner_snap (s2))
1467
else if (is_corner_snap (s2) && !is_corner_snap (s1))
1478
/* Sets a mouse cursor for a widget's window. As a hack, you can pass
1479
* GDK_BLANK_CURSOR to mean "set the cursor to NULL" (i.e. reset the widget's
1480
* window's cursor to its default).
1483
set_cursor (GtkWidget *widget, GdkCursorType type)
1488
if (type == GDK_BLANK_CURSOR)
1491
cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), type);
1493
window = gtk_widget_get_window (widget);
1496
gdk_window_set_cursor (window, cursor);
1499
g_object_unref (cursor);
1503
set_top_bar_tooltip (App *app, gboolean is_dragging)
1510
text = _("Drag to change primary display.");
1512
gtk_widget_set_tooltip_text (app->area, text);
1516
on_top_bar_event (FooScrollArea *area,
1517
FooScrollAreaEvent *event,
1521
if (event->type == FOO_DROP)
1524
/* If the mouse is inside the top bar, set the cursor to "you can move me". See
1525
* on_canvas_event() for where we reset the cursor to the default if it
1526
* exits the outputs' area.
1528
if (!gnome_rr_config_get_clone (app->current_configuration) && get_n_connected (app) > 1)
1529
set_cursor (GTK_WIDGET (area), GDK_HAND1);
1531
if (event->type == FOO_BUTTON_PRESS)
1534
set_top_bar_tooltip (app, TRUE);
1536
if (!gnome_rr_config_get_clone (app->current_configuration) && get_n_connected (app) > 1)
1538
app->dragging_top_bar = TRUE;
1539
foo_scroll_area_begin_grab (area, (FooScrollAreaEventFunc) on_top_bar_event, app);
1542
foo_scroll_area_invalidate (area);
1546
if (foo_scroll_area_is_grabbed (area))
1548
if (event->type == FOO_BUTTON_RELEASE)
1550
foo_scroll_area_end_grab (area, event);
1551
app->dragging_top_bar = FALSE;
1552
set_top_bar_tooltip (app, FALSE);
1555
foo_scroll_area_invalidate (area);
1561
set_monitors_tooltip (App *app, gboolean is_dragging)
1568
text = _("Select a monitor to change its properties; drag it to rearrange its placement.");
1570
gtk_widget_set_tooltip_text (app->area, text);
1574
set_primary_output (App *app,
1575
GnomeRROutputInfo *output)
1578
GnomeRROutputInfo **outputs;
1580
outputs = gnome_rr_config_get_outputs (app->current_configuration);
1581
for (i = 0; outputs[i] != NULL; ++i)
1582
gnome_rr_output_info_set_primary (outputs[i], outputs[i] == output);
1586
on_output_event (FooScrollArea *area,
1587
FooScrollAreaEvent *event,
1590
GnomeRROutputInfo *output = data;
1591
App *app = g_object_get_data (G_OBJECT (area), "app");
1593
if (event->type == FOO_DRAG_HOVER)
1595
if (gnome_rr_output_info_is_active (output) && app->dragging_top_bar)
1596
set_primary_output (app, output);
1599
if (event->type == FOO_DROP)
1601
/* Activate new primary? */
1605
/* If the mouse is inside the outputs, set the cursor to "you can move me". See
1606
* on_canvas_event() for where we reset the cursor to the default if it
1607
* exits the outputs' area.
1609
if (!gnome_rr_config_get_clone (app->current_configuration) && get_n_connected (app) > 1)
1610
set_cursor (GTK_WIDGET (area), GDK_FLEUR);
1612
if (event->type == FOO_BUTTON_PRESS)
1616
app->current_output = output;
1619
set_monitors_tooltip (app, TRUE);
1621
if (!gnome_rr_config_get_clone (app->current_configuration) && get_n_connected (app) > 1)
1623
int output_x, output_y;
1624
gnome_rr_output_info_get_geometry (output, &output_x, &output_y, NULL, NULL);
1626
foo_scroll_area_begin_grab (area, on_output_event, data);
1628
info = g_new0 (GrabInfo, 1);
1629
info->grab_x = event->x;
1630
info->grab_y = event->y;
1631
info->output_x = output_x;
1632
info->output_y = output_y;
1634
g_object_set_data (G_OBJECT (output), "grab-info", info);
1636
foo_scroll_area_invalidate (area);
1640
if (foo_scroll_area_is_grabbed (area))
1642
GrabInfo *info = g_object_get_data (G_OBJECT (output), "grab-info");
1643
double scale = compute_scale (app);
1648
GArray *edges, *snaps, *new_edges;
1650
gnome_rr_output_info_get_geometry (output, &old_x, &old_y, &width, &height);
1651
new_x = info->output_x + (event->x - info->grab_x) / scale;
1652
new_y = info->output_y + (event->y - info->grab_y) / scale;
1654
gnome_rr_output_info_set_geometry (output, new_x, new_y, width, height);
1656
edges = g_array_new (TRUE, TRUE, sizeof (Edge));
1657
snaps = g_array_new (TRUE, TRUE, sizeof (Snap));
1658
new_edges = g_array_new (TRUE, TRUE, sizeof (Edge));
1660
list_edges (app->current_configuration, edges);
1661
list_snaps (output, edges, snaps);
1663
g_array_sort (snaps, compare_snaps);
1665
gnome_rr_output_info_set_geometry (output, new_x, new_y, width, height);
1667
for (i = 0; i < snaps->len; ++i)
1669
Snap *snap = &(g_array_index (snaps, Snap, i));
1670
GArray *new_edges = g_array_new (TRUE, TRUE, sizeof (Edge));
1672
gnome_rr_output_info_set_geometry (output, new_x + snap->dx, new_y + snap->dy, width, height);
1674
g_array_set_size (new_edges, 0);
1675
list_edges (app->current_configuration, new_edges);
1677
if (gnome_rr_config_is_aligned (app->current_configuration, new_edges))
1679
g_array_free (new_edges, TRUE);
1684
gnome_rr_output_info_set_geometry (output, info->output_x, info->output_y, width, height);
1688
g_array_free (new_edges, TRUE);
1689
g_array_free (snaps, TRUE);
1690
g_array_free (edges, TRUE);
1692
if (event->type == FOO_BUTTON_RELEASE)
1694
foo_scroll_area_end_grab (area, event);
1695
set_monitors_tooltip (app, FALSE);
1697
g_free (g_object_get_data (G_OBJECT (output), "grab-info"));
1698
g_object_set_data (G_OBJECT (output), "grab-info", NULL);
1701
g_debug ("new position: %d %d %d %d", output->x, output->y, output->width, output->height);
1705
foo_scroll_area_invalidate (area);
1711
on_canvas_event (FooScrollArea *area,
1712
FooScrollAreaEvent *event,
1715
/* If the mouse exits the outputs, reset the cursor to the default. See
1716
* on_output_event() for where we set the cursor to the movement cursor if
1717
* it is over one of the outputs.
1719
set_cursor (GTK_WIDGET (area), GDK_BLANK_CURSOR);
1722
static PangoLayout *
1723
get_display_name (App *app,
1724
GnomeRROutputInfo *output)
1726
PangoLayout *layout;
1729
if (gnome_rr_config_get_clone (app->current_configuration))
1730
text = mirror_monitor_name ();
1732
text = g_strdup (gnome_rr_output_info_get_display_name (output));
1734
layout = gtk_widget_create_pango_layout (GTK_WIDGET (app->area), text);
1736
pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
1742
paint_background (FooScrollArea *area,
1745
GdkRectangle viewport;
1747
GtkStyleContext *context;
1750
widget = GTK_WIDGET (area);
1752
foo_scroll_area_get_viewport (area, &viewport);
1753
context = gtk_widget_get_style_context (widget);
1754
gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &fg);
1755
gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
1757
cairo_set_source_rgba (cr,
1758
(fg.red + bg.red) / 2,
1759
(fg.green + bg.green) / 2,
1760
(fg.blue + bg.blue) / 2,
1761
(fg.alpha + bg.alpha) / 2);
1763
cairo_rectangle (cr,
1764
viewport.x, viewport.y,
1765
viewport.width, viewport.height);
1767
cairo_fill_preserve (cr);
1769
foo_scroll_area_add_input_from_fill (area, cr, on_canvas_event, NULL);
1771
cairo_set_source_rgba (cr,
1781
color_shade (double *r,
1788
gtk_rgb_to_hsv (*r, *g, *b, &h, &s, &v);
1802
gtk_hsv_to_rgb (h, s, v, r, g, b);
1806
paint_output (App *app, cairo_t *cr, int i)
1809
double scale = compute_scale (app);
1811
int output_x, output_y;
1812
GnomeRRRotation rotation;
1813
int total_w, total_h;
1814
GList *connected_outputs = list_connected_outputs (app, &total_w, &total_h);
1815
GnomeRROutputInfo *output = g_list_nth (connected_outputs, i)->data;
1816
PangoLayout *layout = get_display_name (app, output);
1817
PangoRectangle ink_extent, log_extent;
1818
GdkRectangle viewport;
1819
GdkColor output_color;
1826
foo_scroll_area_get_viewport (FOO_SCROLL_AREA (app->area), &viewport);
1827
get_geometry (output, &w, &h);
1830
g_debug ("%s (%p) geometry %d %d %d primary=%d", output->name, output->name,
1831
w, h, output->rate, output->primary);
1834
viewport.height -= 2 * MARGIN;
1835
viewport.width -= 2 * MARGIN;
1837
gnome_rr_output_info_get_geometry (output, &output_x, &output_y, NULL, NULL);
1838
x = output_x * scale + MARGIN + (viewport.width - total_w * scale) / 2.0;
1839
y = output_y * scale + MARGIN + (viewport.height - total_h * scale) / 2.0;
1842
g_debug ("scaled: %f %f", x, y);
1844
g_debug ("scale: %f", scale);
1846
g_debug ("%f %f %f %f", x, y, w * scale + 0.5, h * scale + 0.5);
1849
cairo_translate (cr,
1850
x + (w * scale + 0.5) / 2,
1851
y + (h * scale + 0.5) / 2);
1853
/* rotation is already applied in get_geometry */
1855
rotation = gnome_rr_output_info_get_rotation (output);
1856
if (rotation & GNOME_RR_REFLECT_X)
1857
cairo_scale (cr, -1, 1);
1859
if (rotation & GNOME_RR_REFLECT_Y)
1860
cairo_scale (cr, 1, -1);
1862
cairo_translate (cr,
1863
- x - (w * scale + 0.5) / 2,
1864
- y - (h * scale + 0.5) / 2);
1866
if (output == app->current_output)
1868
GtkStyleContext *context;
1871
context = gtk_widget_get_style_context (app->area);
1872
gtk_style_context_get_background_color (context, GTK_STATE_FLAG_SELECTED, &color);
1874
cairo_rectangle (cr, x - 2, y - 2, w * scale + 0.5 + 4, h * scale + 0.5 + 4);
1876
cairo_set_line_width (cr, 4);
1877
cairo_set_source_rgba (cr, color.red, color.green, color.blue, 0.5);
1881
cairo_rectangle (cr, x, y, w * scale + 0.5, h * scale + 0.5);
1882
cairo_clip_preserve (cr);
1884
gnome_rr_labeler_get_color_for_output (app->labeler, output, &output_color);
1885
r = output_color.red / 65535.0;
1886
g = output_color.green / 65535.0;
1887
b = output_color.blue / 65535.0;
1889
if (!gnome_rr_output_info_is_active (output))
1891
/* If the output is turned off, just darken the selected color */
1892
color_shade (&r, &g, &b, 0.4);
1895
cairo_set_source_rgba (cr, r, g, b, 1.0);
1897
foo_scroll_area_add_input_from_fill (FOO_SCROLL_AREA (app->area),
1898
cr, on_output_event, output);
1901
cairo_rectangle (cr, x + 0.5, y + 0.5, w * scale + 0.5 - 1, h * scale + 0.5 - 1);
1903
cairo_set_line_width (cr, 1);
1904
cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
1907
cairo_set_line_width (cr, 2);
1911
layout_set_font (layout, "Sans 10");
1912
pango_layout_get_pixel_extents (layout, &ink_extent, &log_extent);
1914
available_w = w * scale + 0.5 - 6; /* Same as the inner rectangle's width, minus 1 pixel of padding on each side */
1915
if (available_w < ink_extent.width)
1916
factor = available_w / ink_extent.width;
1921
x + ((w * scale + 0.5) - factor * log_extent.width) / 2,
1922
y + ((h * scale + 0.5) - factor * log_extent.height) / 2);
1924
cairo_scale (cr, factor, factor);
1925
if (gnome_rr_output_info_is_active (output))
1926
cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
1928
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
1930
pango_cairo_show_layout (cr, layout);
1931
g_object_unref (layout);
1934
if (gnome_rr_output_info_get_primary (output))
1936
const char *clock_format;
1940
GDesktopClockFormat value;
1943
cairo_rectangle (cr, x, y, w * scale + 0.5, TOP_BAR_HEIGHT);
1944
cairo_set_source_rgb (cr, 0, 0, 0);
1945
foo_scroll_area_add_input_from_fill (FOO_SCROLL_AREA (app->area),
1947
(FooScrollAreaEventFunc) on_top_bar_event,
1953
/* FIXME: set 12/24 hour */
1954
value = g_settings_get_enum (app->clock_settings, CLOCK_FORMAT_KEY);
1955
use_24 = value == G_DESKTOP_CLOCK_FORMAT_24H;
1957
clock_format = _("%a %R");
1959
clock_format = _("%a %l:%M %p");
1961
dt = g_date_time_new_now_local ();
1962
text = g_date_time_format (dt, clock_format);
1963
g_date_time_unref (dt);
1965
layout = gtk_widget_create_pango_layout (GTK_WIDGET (app->area), text);
1967
pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
1969
layout_set_font (layout, "Sans 4");
1970
pango_layout_get_pixel_extents (layout, &ink_extent, &log_extent);
1972
if (available_w < ink_extent.width)
1973
factor = available_w / ink_extent.width;
1978
x + ((w * scale + 0.5) - factor * log_extent.width) / 2,
1979
y + (TOP_BAR_HEIGHT - factor * log_extent.height) / 2);
1981
cairo_scale (cr, factor, factor);
1983
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
1985
pango_cairo_show_layout (cr, layout);
1986
g_object_unref (layout);
1993
on_area_paint (FooScrollArea *area,
1998
GList *connected_outputs = NULL;
2001
paint_background (area, cr);
2003
if (!app->current_configuration)
2006
connected_outputs = list_connected_outputs (app, NULL, NULL);
2008
for (list = connected_outputs; list != NULL; list = list->next)
2010
paint_output (app, cr, g_list_position (connected_outputs, list));
2012
if (gnome_rr_config_get_clone (app->current_configuration))
2018
make_text_combo (GtkWidget *widget, int sort_column)
2020
GtkComboBox *box = GTK_COMBO_BOX (widget);
2021
GtkListStore *store = gtk_list_store_new (
2023
G_TYPE_STRING, /* Text */
2024
G_TYPE_INT, /* Width */
2025
G_TYPE_INT, /* Height */
2026
G_TYPE_INT, /* Frequency */
2027
G_TYPE_INT, /* Width * Height */
2028
G_TYPE_INT); /* Rotation */
2030
GtkCellRenderer *cell;
2032
gtk_cell_layout_clear (GTK_CELL_LAYOUT (widget));
2034
gtk_combo_box_set_model (box, GTK_TREE_MODEL (store));
2036
cell = gtk_cell_renderer_text_new ();
2037
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (box), cell, TRUE);
2038
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (box), cell,
2042
if (sort_column != -1)
2044
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
2046
GTK_SORT_DESCENDING);
2051
compute_virtual_size_for_configuration (GnomeRRConfig *config, int *ret_width, int *ret_height)
2055
int output_x, output_y, output_width, output_height;
2056
GnomeRROutputInfo **outputs;
2060
outputs = gnome_rr_config_get_outputs (config);
2061
for (i = 0; outputs[i] != NULL; i++)
2063
if (gnome_rr_output_info_is_active (outputs[i]))
2065
gnome_rr_output_info_get_geometry (outputs[i], &output_x, &output_y, &output_width, &output_height);
2066
width = MAX (width, output_x + output_width);
2067
height = MAX (height, output_y + output_height);
2072
*ret_height = height;
2076
check_required_virtual_size (App *app)
2078
int req_width, req_height;
2079
int min_width, max_width;
2080
int min_height, max_height;
2082
compute_virtual_size_for_configuration (app->current_configuration, &req_width, &req_height);
2084
gnome_rr_screen_get_ranges (app->screen, &min_width, &max_width, &min_height, &max_height);
2087
g_debug ("X Server supports:");
2088
g_debug ("min_width = %d, max_width = %d", min_width, max_width);
2089
g_debug ("min_height = %d, max_height = %d", min_height, max_height);
2091
g_debug ("Requesting size of %dx%d", req_width, req_height);
2094
if (!(min_width <= req_width && req_width <= max_width
2095
&& min_height <= req_height && req_height <= max_height))
2097
/* FIXME: present a useful dialog, maybe even before the user tries to Apply */
2099
g_debug ("Your X server needs a larger Virtual size!");
2105
begin_version2_apply_configuration (App *app, GdkWindow *parent_window, guint32 timestamp)
2107
XID parent_window_xid;
2109
parent_window_xid = GDK_WINDOW_XID (parent_window);
2111
app->proxy = dbus_g_proxy_new_for_name (app->connection,
2112
"org.gnome.SettingsDaemon",
2113
"/org/gnome/SettingsDaemon/XRANDR",
2114
"org.gnome.SettingsDaemon.XRANDR_2");
2115
g_assert (app->proxy != NULL); /* that call does not fail unless we pass bogus names */
2117
app->proxy_call = dbus_g_proxy_begin_call (app->proxy, "ApplyConfiguration",
2118
apply_configuration_returned_cb, app,
2120
G_TYPE_INT64, (gint64) parent_window_xid,
2121
G_TYPE_INT64, (gint64) timestamp,
2124
/* FIXME: we don't check for app->proxy_call == NULL, which could happen if
2125
* the connection was disconnected. This is left as an exercise for the
2131
ensure_current_configuration_is_saved (void)
2133
GnomeRRScreen *rr_screen;
2134
GnomeRRConfig *rr_config;
2136
/* Normally, gnome_rr_config_save() creates a backup file based on the
2137
* old monitors.xml. However, if *that* file didn't exist, there is
2138
* nothing from which to create a backup. So, here we'll save the
2139
* current/unchanged configuration and then let our caller call
2140
* gnome_rr_config_save() again with the new/changed configuration, so
2141
* that there *will* be a backup file in the end.
2144
rr_screen = gnome_rr_screen_new (gdk_screen_get_default (), NULL); /* NULL-GError */
2148
rr_config = gnome_rr_config_new_current (rr_screen, NULL);
2149
gnome_rr_config_ensure_primary (rr_config);
2150
gnome_rr_config_save (rr_config, NULL); /* NULL-GError */
2152
g_object_unref (rr_config);
2153
g_object_unref (rr_screen);
2156
/* Callback for dbus_g_proxy_begin_call() */
2158
apply_configuration_returned_cb (DBusGProxy *proxy,
2159
DBusGProxyCall *call_id,
2166
g_assert (call_id == app->proxy_call);
2169
success = dbus_g_proxy_end_call (proxy, call_id, &error, G_TYPE_INVALID);
2172
/* We don't pop up an error message; gnome-settings-daemon already does that
2173
* in case the selected RANDR configuration could not be applied.
2175
g_error_free (error);
2178
g_object_unref (app->proxy);
2181
dbus_g_connection_unref (app->connection);
2182
app->connection = NULL;
2183
app->proxy_call = NULL;
2185
gtk_widget_set_sensitive (app->panel, TRUE);
2189
sanitize_and_save_configuration (App *app)
2193
gnome_rr_config_sanitize (app->current_configuration);
2194
gnome_rr_config_ensure_primary (app->current_configuration);
2196
check_required_virtual_size (app);
2198
foo_scroll_area_invalidate (FOO_SCROLL_AREA (app->area));
2200
ensure_current_configuration_is_saved ();
2203
if (!gnome_rr_config_save (app->current_configuration, &error))
2205
error_message (app, _("Could not save the monitor configuration"), error->message);
2206
g_error_free (error);
2216
GError *error = NULL;
2219
app->apply_button_clicked_timestamp = gtk_get_current_event_time ();
2221
if (!sanitize_and_save_configuration (app))
2224
g_assert (app->connection == NULL);
2225
g_assert (app->proxy == NULL);
2226
g_assert (app->proxy_call == NULL);
2228
app->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
2229
if (app->connection == NULL) {
2230
error_message (app, _("Could not get session bus while applying display configuration"), error->message);
2231
g_error_free (error);
2235
gtk_widget_set_sensitive (app->panel, FALSE);
2237
window = gtk_widget_get_window (gtk_widget_get_toplevel (app->panel));
2239
begin_version2_apply_configuration (app, window,
2240
app->apply_button_clicked_timestamp);
2244
/* Returns whether the graphics driver doesn't advertise RANDR 1.2 features, and just 1.0 */
2246
driver_is_randr_10 (GnomeRRConfig *config)
2248
/* In the Xorg code, see xserver/randr/rrinfo.c:RRScanOldConfig(). It gets
2249
* called when the graphics driver doesn't support RANDR 1.2 yet, just 1.0.
2250
* In that case, the X server's base code (which supports RANDR 1.2) will
2251
* simulate having a single output called "default". For drivers that *do*
2252
* support RANDR 1.2, the separate outputs will be named differently, we
2255
* This heuristic is courtesy of Dirk Mueller <dmueller@suse.de>
2257
* FIXME: however, we don't even check for XRRQueryVersion() returning 1.2, neither
2258
* here nor in gnome-desktop/libgnomedesktop*.c. Do we need to check for that,
2259
* or is gnome_rr_screen_new()'s return value sufficient?
2262
return (count_all_outputs (config) == 1 && strcmp (gnome_rr_output_info_get_name (gnome_rr_config_get_outputs (config)[0]), "default") == 0);
2267
on_detect_displays (GtkWidget *widget, gpointer data)
2273
if (!gnome_rr_screen_refresh (app->screen, &error)) {
2275
error_message (app, _("Could not detect displays"), error->message);
2276
g_error_free (error);
2281
static GnomeRROutputInfo *
2282
get_nearest_output (GnomeRRConfig *configuration, int x, int y)
2287
GnomeRROutputInfo **outputs;
2290
nearest_dist = G_MAXINT;
2292
outputs = gnome_rr_config_get_outputs (configuration);
2293
for (i = 0; outputs[i] != NULL; i++)
2296
int output_x, output_y, output_width, output_height;
2298
if (!(gnome_rr_output_info_is_connected (outputs[i]) && gnome_rr_output_info_is_active (outputs[i])))
2301
gnome_rr_output_info_get_geometry (outputs[i], &output_x, &output_y, &output_width, &output_height);
2304
dist_x = output_x - x;
2305
else if (x >= output_x + output_width)
2306
dist_x = x - (output_x + output_width) + 1;
2311
dist_y = output_y - y;
2312
else if (y >= output_y + output_height)
2313
dist_y = y - (output_y + output_height) + 1;
2317
if (MIN (dist_x, dist_y) < nearest_dist)
2319
nearest_dist = MIN (dist_x, dist_y);
2324
if (nearest_index != -1)
2325
return outputs[nearest_index];
2330
/* Gets the output that contains the largest intersection with the window.
2331
* Logic stolen from gdk_screen_get_monitor_at_window().
2333
static GnomeRROutputInfo *
2334
get_output_for_window (GnomeRRConfig *configuration, GdkWindow *window)
2336
GdkRectangle win_rect;
2340
GnomeRROutputInfo **outputs;
2342
gdk_window_get_geometry (window, &win_rect.x, &win_rect.y, &win_rect.width, &win_rect.height);
2343
gdk_window_get_origin (window, &win_rect.x, &win_rect.y);
2348
outputs = gnome_rr_config_get_outputs (configuration);
2349
for (i = 0; outputs[i] != NULL; i++)
2351
GdkRectangle output_rect, intersection;
2353
gnome_rr_output_info_get_geometry (outputs[i], &output_rect.x, &output_rect.y, &output_rect.width, &output_rect.height);
2355
if (gnome_rr_output_info_is_connected (outputs[i]) && gdk_rectangle_intersect (&win_rect, &output_rect, &intersection))
2359
area = intersection.width * intersection.height;
2360
if (area > largest_area)
2362
largest_area = area;
2368
if (largest_index != -1)
2369
return outputs[largest_index];
2371
return get_nearest_output (configuration,
2372
win_rect.x + win_rect.width / 2,
2373
win_rect.y + win_rect.height / 2);
2377
on_toplevel_realized (GtkWidget *widget,
2380
app->current_output = get_output_for_window (app->current_configuration,
2381
gtk_widget_get_window (widget));
2385
/* We select the current output, i.e. select the one being edited, based on
2386
* which output is showing the configuration dialog.
2389
select_current_output_from_dialog_position (App *app)
2391
GtkWidget *toplevel;
2393
toplevel = gtk_widget_get_toplevel (app->panel);
2395
if (gtk_widget_get_realized (toplevel)) {
2396
app->current_output = get_output_for_window (app->current_configuration,
2397
gtk_widget_get_window (toplevel));
2400
g_signal_connect (toplevel, "realize", G_CALLBACK (on_toplevel_realized), app);
2401
app->current_output = NULL;
2405
/* This is a GtkWidget::map-event handler. We wait for the display-properties
2406
* dialog to be mapped, and then we select the output which corresponds to the
2407
* monitor on which the dialog is being shown.
2410
dialog_map_event_cb (GtkWidget *widget, GdkEventAny *event, gpointer data)
2414
select_current_output_from_dialog_position (app);
2420
_gtk_builder_get_widget (GtkBuilder *builder, const gchar *name)
2422
return GTK_WIDGET (gtk_builder_get_object (builder, name));
2426
destroy_app (App *app)
2428
g_object_unref (app->screen);
2429
g_object_unref (app->builder);
2431
if (app->clock_settings != NULL)
2432
g_object_unref (app->clock_settings);
2434
gnome_rr_labeler_hide (app->labeler);
2435
g_object_unref (app->labeler);
2441
run_application (void)
2446
#define UI_FILE UIDIR "/display-capplet.ui"
2447
GtkBuilder *builder;
2450
gchar *objects[] = {"display-panel", "rotation-liststore", NULL};
2453
app = g_new0 (App, 1);
2456
app->builder = builder = gtk_builder_new ();
2458
if (!gtk_builder_add_objects_from_file (builder, UI_FILE, objects, &error))
2460
g_warning ("Could not parse UI definition: %s", error->message);
2461
g_error_free (error);
2462
g_object_unref (builder);
2466
app->screen = gnome_rr_screen_new (gdk_screen_get_default (), &error);
2467
g_signal_connect (app->screen, "changed", G_CALLBACK (on_screen_changed), app);
2470
error_message (NULL, _("Could not get screen information"), error->message);
2471
g_error_free (error);
2472
g_object_unref (builder);
2476
app->clock_settings = g_settings_new (CLOCK_SCHEMA);
2478
app->panel = _gtk_builder_get_widget (builder, "display-panel");
2481
g_warning ("Missing display-panel object");
2482
g_signal_connect_after (app->panel, "show",
2483
G_CALLBACK (dialog_map_event_cb), app);
2485
app->current_monitor_event_box = _gtk_builder_get_widget (builder,
2486
"current_monitor_event_box");
2487
app->current_monitor_label = _gtk_builder_get_widget (builder,
2488
"current_monitor_label");
2490
app->monitor_switch = _gtk_builder_get_widget (builder,
2492
g_signal_connect (app->monitor_switch, "notify::active",
2493
G_CALLBACK (monitor_switch_active_cb), app);
2495
app->resolution_combo = _gtk_builder_get_widget (builder,
2496
"resolution_combo");
2497
g_signal_connect (app->resolution_combo, "changed",
2498
G_CALLBACK (on_resolution_changed), app);
2500
app->rotation_combo = _gtk_builder_get_widget (builder, "rotation_combo");
2501
g_signal_connect (app->rotation_combo, "changed",
2502
G_CALLBACK (on_rotation_changed), app);
2504
app->clone_checkbox = _gtk_builder_get_widget (builder, "clone_checkbox");
2505
g_signal_connect (app->clone_checkbox, "toggled",
2506
G_CALLBACK (on_clone_changed), app);
2508
app->clone_label = _gtk_builder_get_widget (builder, "clone_resolution_warning_label");
2510
g_signal_connect (_gtk_builder_get_widget (builder, "detect_displays_button"),
2511
"clicked", G_CALLBACK (on_detect_displays), app);
2513
make_text_combo (app->resolution_combo, 4);
2514
make_text_combo (app->rotation_combo, -1);
2517
app->area = (GtkWidget *)foo_scroll_area_new ();
2519
g_object_set_data (G_OBJECT (app->area), "app", app);
2521
set_monitors_tooltip (app, FALSE);
2523
/* FIXME: this should be computed dynamically */
2524
foo_scroll_area_set_min_size (FOO_SCROLL_AREA (app->area), 0, 200);
2525
gtk_widget_show (app->area);
2526
g_signal_connect (app->area, "paint",
2527
G_CALLBACK (on_area_paint), app);
2528
g_signal_connect (app->area, "viewport_changed",
2529
G_CALLBACK (on_viewport_changed), app);
2531
align = _gtk_builder_get_widget (builder, "align");
2533
gtk_container_add (GTK_CONTAINER (align), app->area);
2535
on_screen_changed (app->screen, app);
2537
g_signal_connect_swapped (_gtk_builder_get_widget (builder, "apply_button"),
2538
"clicked", G_CALLBACK (apply), app);
2540
g_object_weak_ref (G_OBJECT (app->panel), (GWeakNotify) destroy_app, app);