~rodrigo-moya/ubuntu/oneiric/gnome-desktop3/fix_863038

« back to all changes in this revision

Viewing changes to libgnome-desktop/gnome-rr-labeler.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-01-12 11:08:17 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110112110817-qiyyybvcj2k70sjq
Tags: 2.91.5-0ubuntu1
* New upstream release
* debian/control:
  - Bump build dependency on gsettings-desktop-schemas
* debian/libgnome-desktop-3-0.symbols:
  - Update to match new API

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
#include <config.h>
31
31
#include <glib/gi18n-lib.h>
32
 
#include "gnome-rr-labeler.h"
33
32
#include <gtk/gtk.h>
34
33
 
35
34
#include <X11/Xproto.h>
38
37
#include <X11/Xatom.h>
39
38
#include <gdk/gdkx.h>
40
39
 
41
 
struct _GnomeRRLabeler {
42
 
        GObject parent;
 
40
#include "gnome-rr-labeler.h"
43
41
 
 
42
struct _GnomeRRLabelerPrivate {
44
43
        GnomeRRConfig *config;
45
44
 
46
45
        int num_outputs;
52
51
        Atom        workarea_atom;
53
52
};
54
53
 
55
 
struct _GnomeRRLabelerClass {
56
 
        GObjectClass parent_class;
 
54
enum {
 
55
        PROP_0,
 
56
        PROP_CONFIG,
 
57
        PROP_LAST
57
58
};
58
59
 
59
60
G_DEFINE_TYPE (GnomeRRLabeler, gnome_rr_labeler, G_TYPE_OBJECT);
60
61
 
61
62
static void gnome_rr_labeler_finalize (GObject *object);
62
63
static void create_label_windows (GnomeRRLabeler *labeler);
 
64
static void setup_from_config (GnomeRRLabeler *labeler);
63
65
 
64
66
static gboolean
65
67
get_work_area (GnomeRRLabeler *labeler,
78
80
        int             disp_screen;
79
81
        Display        *display;
80
82
 
81
 
        display = GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (labeler->screen));
 
83
        display = GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (labeler->priv->screen));
82
84
        workarea = XInternAtom (display, "_NET_WORKAREA", True);
83
85
 
84
 
        disp_screen = GDK_SCREEN_XNUMBER (labeler->screen);
 
86
        disp_screen = GDK_SCREEN_XNUMBER (labeler->priv->screen);
85
87
 
86
88
        /* Defaults in case of error */
87
89
        rect->x = 0;
88
90
        rect->y = 0;
89
 
        rect->width = gdk_screen_get_width (labeler->screen);
90
 
        rect->height = gdk_screen_get_height (labeler->screen);
 
91
        rect->width = gdk_screen_get_width (labeler->priv->screen);
 
92
        rect->height = gdk_screen_get_height (labeler->priv->screen);
91
93
 
92
94
        if (workarea == None)
93
95
                return FALSE;
135
137
        xev = (XEvent *) xevent;
136
138
 
137
139
        if (xev->type == PropertyNotify &&
138
 
            xev->xproperty.atom == labeler->workarea_atom) {
 
140
            xev->xproperty.atom == labeler->priv->workarea_atom) {
139
141
                /* update label positions */
140
142
                gnome_rr_labeler_hide (labeler);
141
143
                create_label_windows (labeler);
149
151
{
150
152
        GdkWindow *gdkwindow;
151
153
 
152
 
        labeler->workarea_atom = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
153
 
                                              "_NET_WORKAREA",
154
 
                                              True);
155
 
 
156
 
        labeler->screen = gdk_screen_get_default ();
 
154
        labeler->priv = G_TYPE_INSTANCE_GET_PRIVATE (labeler, GNOME_TYPE_RR_LABELER, GnomeRRLabelerPrivate);
 
155
 
 
156
        labeler->priv->workarea_atom = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
157
                                                    "_NET_WORKAREA",
 
158
                                                    True);
 
159
 
 
160
        labeler->priv->screen = gdk_screen_get_default ();
157
161
        /* code is not really designed to handle multiple screens so *shrug* */
158
 
        gdkwindow = gdk_screen_get_root_window (labeler->screen);
 
162
        gdkwindow = gdk_screen_get_root_window (labeler->priv->screen);
159
163
        gdk_window_add_filter (gdkwindow, (GdkFilterFunc) screen_xevent_filter, labeler);
160
164
        gdk_window_set_events (gdkwindow, gdk_window_get_events (gdkwindow) | GDK_PROPERTY_CHANGE_MASK);
161
165
}
162
166
 
163
167
static void
164
 
gnome_rr_labeler_class_init (GnomeRRLabelerClass *class)
 
168
gnome_rr_labeler_set_property (GObject *gobject, guint property_id, const GValue *value, GParamSpec *param_spec)
 
169
{
 
170
        GnomeRRLabeler *self = GNOME_RR_LABELER (gobject);
 
171
 
 
172
        switch (property_id) {
 
173
        case PROP_CONFIG:
 
174
                self->priv->config = GNOME_RR_CONFIG (g_value_get_object (value));
 
175
                return;
 
176
        default:
 
177
                G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, property_id, param_spec);
 
178
        }
 
179
}
 
180
 
 
181
static GObject *
 
182
gnome_rr_labeler_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_properties)
 
183
{
 
184
        GnomeRRLabeler *self = (GnomeRRLabeler*) G_OBJECT_CLASS (gnome_rr_labeler_parent_class)->constructor (type, n_construct_properties, construct_properties);
 
185
 
 
186
        setup_from_config (self);
 
187
 
 
188
        return (GObject*) self;
 
189
}
 
190
 
 
191
static void
 
192
gnome_rr_labeler_class_init (GnomeRRLabelerClass *klass)
165
193
{
166
194
        GObjectClass *object_class;
167
195
 
168
 
        object_class = (GObjectClass *) class;
169
 
 
 
196
        g_type_class_add_private (klass, sizeof (GnomeRRLabelerPrivate));
 
197
 
 
198
        object_class = (GObjectClass *) klass;
 
199
 
 
200
        object_class->set_property = gnome_rr_labeler_set_property;
170
201
        object_class->finalize = gnome_rr_labeler_finalize;
 
202
        object_class->constructor = gnome_rr_labeler_constructor;
 
203
 
 
204
        g_object_class_install_property (object_class, PROP_CONFIG, g_param_spec_object ("config",
 
205
                                                                                         "Configuration",
 
206
                                                                                         "RandR configuration to label",
 
207
                                                                                         GNOME_TYPE_RR_CONFIG,
 
208
                                                                                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
 
209
                                                                                         G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
171
210
}
172
211
 
173
212
static void
178
217
 
179
218
        labeler = GNOME_RR_LABELER (object);
180
219
 
181
 
        gdkwindow = gdk_screen_get_root_window (labeler->screen);
 
220
        gdkwindow = gdk_screen_get_root_window (labeler->priv->screen);
182
221
        gdk_window_remove_filter (gdkwindow, (GdkFilterFunc) screen_xevent_filter, labeler);
183
222
 
184
 
        /* We don't destroy the labeler->config (a GnomeRRConfig*) here; let our
185
 
         * caller do that instead.
186
 
         */
 
223
        if (labeler->priv->config != NULL) {
 
224
                g_object_unref (labeler->priv->config);
 
225
        }
187
226
 
188
 
        if (labeler->windows != NULL) {
 
227
        if (labeler->priv->windows != NULL) {
189
228
                gnome_rr_labeler_hide (labeler);
190
 
                g_free (labeler->windows);
191
 
                labeler->windows = NULL;
 
229
                g_free (labeler->priv->windows);
192
230
        }
193
231
 
194
 
        g_free (labeler->palette);
195
 
        labeler->palette = NULL;
 
232
        g_free (labeler->priv->palette);
196
233
 
197
234
        G_OBJECT_CLASS (gnome_rr_labeler_parent_class)->finalize (object);
198
235
}
201
238
count_outputs (GnomeRRConfig *config)
202
239
{
203
240
        int i;
 
241
        GnomeRROutputInfo **outputs = gnome_rr_config_get_outputs (config);
204
242
 
205
 
        for (i = 0; config->outputs[i] != NULL; i++)
 
243
        for (i = 0; outputs[i] != NULL; i++)
206
244
                ;
207
245
 
208
246
        return i;
223
261
        double end_hue;
224
262
        int i;
225
263
 
226
 
        g_assert (labeler->num_outputs > 0);
 
264
        g_assert (labeler->priv->num_outputs > 0);
227
265
 
228
 
        labeler->palette = g_new (GdkColor, labeler->num_outputs);
 
266
        labeler->priv->palette = g_new (GdkColor, labeler->priv->num_outputs);
229
267
 
230
268
        start_hue = 0.0; /* red */
231
269
        end_hue   = 2.0/3; /* blue */
232
270
 
233
 
        for (i = 0; i < labeler->num_outputs; i++) {
 
271
        for (i = 0; i < labeler->priv->num_outputs; i++) {
234
272
                double h, s, v;
235
273
                double r, g, b;
236
274
 
237
 
                h = start_hue + (end_hue - start_hue) / labeler->num_outputs * i;
 
275
                h = start_hue + (end_hue - start_hue) / labeler->priv->num_outputs * i;
238
276
                s = 1.0 / 3;
239
277
                v = 1.0;
240
278
 
241
279
                gtk_hsv_to_rgb (h, s, v, &r, &g, &b);
242
280
 
243
 
                labeler->palette[i].red   = (int) (65535 * r + 0.5);
244
 
                labeler->palette[i].green = (int) (65535 * g + 0.5);
245
 
                labeler->palette[i].blue  = (int) (65535 * b + 0.5);
 
281
                labeler->priv->palette[i].red   = (int) (65535 * r + 0.5);
 
282
                labeler->priv->palette[i].green = (int) (65535 * g + 0.5);
 
283
                labeler->priv->palette[i].blue  = (int) (65535 * b + 0.5);
246
284
        }
247
285
}
248
286
 
293
331
        int             monitor_num;
294
332
 
295
333
        get_work_area (labeler, &workarea);
296
 
        monitor_num = gdk_screen_get_monitor_at_point (labeler->screen, x, y);
297
 
        gdk_screen_get_monitor_geometry (labeler->screen,
 
334
        monitor_num = gdk_screen_get_monitor_at_point (labeler->priv->screen, x, y);
 
335
        gdk_screen_get_monitor_geometry (labeler->priv->screen,
298
336
                                         monitor_num,
299
337
                                         &monitor);
300
338
        gdk_rectangle_intersect (&monitor, &workarea, &workarea);
303
341
}
304
342
 
305
343
static GtkWidget *
306
 
create_label_window (GnomeRRLabeler *labeler, GnomeOutputInfo *output, GdkColor *color)
 
344
create_label_window (GnomeRRLabeler *labeler, GnomeRROutputInfo *output, GdkColor *color)
307
345
{
308
346
        GtkWidget *window;
309
347
        GtkWidget *widget;
310
348
        char *str;
311
349
        const char *display_name;
312
350
        GdkColor black = { 0, 0, 0, 0 };
 
351
        int x,y;
313
352
 
314
353
        window = gtk_window_new (GTK_WINDOW_POPUP);
315
354
        gtk_widget_set_app_paintable (window, TRUE);
325
364
        g_signal_connect (window, "draw",
326
365
                          G_CALLBACK (label_window_draw_event_cb), labeler);
327
366
 
328
 
        if (labeler->config->clone) {
 
367
        if (gnome_rr_config_get_clone (labeler->priv->config)) {
329
368
                /* Keep this string in sync with gnome-control-center/capplets/display/xrandr-capplet.c:get_display_name() */
330
369
 
331
370
                /* Translators:  this is the feature where what you see on your laptop's
335
374
                 */
336
375
                display_name = _("Mirror Screens");
337
376
        } else
338
 
                display_name = output->display_name;
 
377
                display_name = gnome_rr_output_info_get_display_name (output);
339
378
 
340
379
        str = g_strdup_printf ("<b>%s</b>", display_name);
341
380
        widget = gtk_label_new (NULL);
351
390
        gtk_container_add (GTK_CONTAINER (window), widget);
352
391
 
353
392
        /* Should we center this at the top edge of the monitor, instead of using the upper-left corner? */
354
 
        position_window (labeler, window, output->x, output->y);
 
393
        gnome_rr_output_info_get_geometry (output, &x, &y, NULL, NULL);
 
394
        position_window (labeler, window, x, y);
355
395
 
356
396
        gtk_widget_show_all (window);
357
397
 
363
403
{
364
404
        int i;
365
405
        gboolean created_window_for_clone;
 
406
        GnomeRROutputInfo **outputs;
366
407
 
367
 
        labeler->windows = g_new (GtkWidget *, labeler->num_outputs);
 
408
        labeler->priv->windows = g_new (GtkWidget *, labeler->priv->num_outputs);
368
409
 
369
410
        created_window_for_clone = FALSE;
370
411
 
371
 
        for (i = 0; i < labeler->num_outputs; i++) {
372
 
                if (!created_window_for_clone && labeler->config->outputs[i]->on) {
373
 
                        labeler->windows[i] = create_label_window (labeler, labeler->config->outputs[i], labeler->palette + i);
374
 
 
375
 
                        if (labeler->config->clone)
 
412
        outputs = gnome_rr_config_get_outputs (labeler->priv->config);
 
413
 
 
414
        for (i = 0; i < labeler->priv->num_outputs; i++) {
 
415
                if (!created_window_for_clone && gnome_rr_output_info_is_active (outputs[i])) {
 
416
                        labeler->priv->windows[i] = create_label_window (labeler, outputs[i], labeler->priv->palette + i);
 
417
 
 
418
                        if (gnome_rr_config_get_clone (labeler->priv->config))
376
419
                                created_window_for_clone = TRUE;
377
420
                } else
378
 
                        labeler->windows[i] = NULL;
 
421
                        labeler->priv->windows[i] = NULL;
379
422
        }
380
423
}
381
424
 
382
425
static void
383
426
setup_from_config (GnomeRRLabeler *labeler)
384
427
{
385
 
        labeler->num_outputs = count_outputs (labeler->config);
 
428
        labeler->priv->num_outputs = count_outputs (labeler->priv->config);
386
429
 
387
430
        make_palette (labeler);
388
431
 
392
435
GnomeRRLabeler *
393
436
gnome_rr_labeler_new (GnomeRRConfig *config)
394
437
{
395
 
        GnomeRRLabeler *labeler;
396
 
 
397
 
        g_return_val_if_fail (config != NULL, NULL);
398
 
 
399
 
        labeler = g_object_new (GNOME_TYPE_RR_LABELER, NULL);
400
 
        labeler->config = config;
401
 
 
402
 
        setup_from_config (labeler);
403
 
 
404
 
        return labeler;
 
438
        g_return_val_if_fail (GNOME_IS_RR_CONFIG (config), NULL);
 
439
 
 
440
        return g_object_new (GNOME_TYPE_RR_LABELER, "config", config, NULL);
405
441
}
406
442
 
407
443
void
408
444
gnome_rr_labeler_hide (GnomeRRLabeler *labeler)
409
445
{
410
446
        int i;
 
447
        GnomeRRLabelerPrivate *priv;
411
448
 
412
449
        g_return_if_fail (GNOME_IS_RR_LABELER (labeler));
413
450
 
414
 
        if (labeler->windows == NULL)
 
451
        priv = labeler->priv;
 
452
 
 
453
        if (priv->windows == NULL)
415
454
                return;
416
455
 
417
 
        for (i = 0; i < labeler->num_outputs; i++)
418
 
                if (labeler->windows[i] != NULL) {
419
 
                        gtk_widget_destroy (labeler->windows[i]);
420
 
                        labeler->windows[i] = NULL;
421
 
                }
422
 
        g_free (labeler->windows);
423
 
        labeler->windows = NULL;
 
456
        for (i = 0; i < priv->num_outputs; i++)
 
457
                if (priv->windows[i] != NULL) {
 
458
                        gtk_widget_destroy (priv->windows[i]);
 
459
                        priv->windows[i] = NULL;
 
460
        }
 
461
        g_free (priv->windows);
 
462
        priv->windows = NULL;
424
463
}
425
464
 
426
465
void
427
 
gnome_rr_labeler_get_color_for_output (GnomeRRLabeler *labeler, GnomeOutputInfo *output, GdkColor *color_out)
 
466
gnome_rr_labeler_get_color_for_output (GnomeRRLabeler *labeler, GnomeRROutputInfo *output, GdkColor *color_out)
428
467
{
429
468
        int i;
 
469
        GnomeRROutputInfo **outputs;
430
470
 
431
471
        g_return_if_fail (GNOME_IS_RR_LABELER (labeler));
432
 
        g_return_if_fail (output != NULL);
 
472
        g_return_if_fail (GNOME_IS_RR_OUTPUT_INFO (output));
433
473
        g_return_if_fail (color_out != NULL);
434
474
 
435
 
        for (i = 0; i < labeler->num_outputs; i++)
436
 
                if (labeler->config->outputs[i] == output) {
437
 
                        *color_out = labeler->palette[i];
 
475
        outputs = gnome_rr_config_get_outputs (labeler->priv->config);
 
476
 
 
477
        for (i = 0; i < labeler->priv->num_outputs; i++)
 
478
                if (outputs[i] == output) {
 
479
                        *color_out = labeler->priv->palette[i];
438
480
                        return;
439
481
                }
440
482