summaryrefslogtreecommitdiff
path: root/libgnome-desktop/gnome-rr-output-info.c
blob: 692e7ef201c162ced95a7532063396feaa3ab27e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* gnome-rr-output-info.c
 * -*- c-basic-offset: 4 -*-
 *
 * Copyright 2010 Giovanni Campagna
 *
 * This file is part of the Gnome Desktop Library.
 *
 * The Gnome Desktop 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 Desktop Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#define GNOME_DESKTOP_USE_UNSTABLE_API

#include <config.h>

#include "gnome-rr-config.h"

#include "gnome-rr-private.h"

G_DEFINE_TYPE (GnomeRROutputInfo, gnome_rr_output_info, G_TYPE_OBJECT)

static void
gnome_rr_output_info_init (GnomeRROutputInfo *self)
{
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GNOME_TYPE_RR_OUTPUT_INFO, GnomeRROutputInfoPrivate);

    self->priv->name = NULL;
    self->priv->on = FALSE;
    self->priv->rotation = GNOME_RR_ROTATION_0;
    self->priv->display_name = NULL;
}

static void
gnome_rr_output_info_finalize (GObject *gobject)
{
    GnomeRROutputInfo *self = GNOME_RR_OUTPUT_INFO (gobject);

    g_free (self->priv->name);
    g_free (self->priv->display_name);
    g_free (self->priv->product);
    g_free (self->priv->serial);
    g_free (self->priv->vendor);

    G_OBJECT_CLASS (gnome_rr_output_info_parent_class)->finalize (gobject);
}

static void
gnome_rr_output_info_class_init (GnomeRROutputInfoClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (klass, sizeof (GnomeRROutputInfoPrivate));

    gobject_class->finalize = gnome_rr_output_info_finalize;
}

/**
 * gnome_rr_output_info_get_name:
 *
 * Returns: (transfer none): the output name
 */
char *gnome_rr_output_info_get_name (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), NULL);

    return self->priv->name;
}

/**
 * gnome_rr_output_info_is_active:
 *
 * Returns: whether there is a CRTC assigned to this output (i.e. a signal is being sent to it)
 */
gboolean gnome_rr_output_info_is_active (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), FALSE);

    return self->priv->on;
}

void gnome_rr_output_info_set_active (GnomeRROutputInfo *self, gboolean active)
{
    g_return_if_fail (GNOME_IS_RR_OUTPUT_INFO (self));

    self->priv->on = active;
}

static void gnome_rr_output_info_get_tiled_geometry (GnomeRROutputInfo *self,
                                                     int *x,
                                                     int *y,
                                                     int *width,
                                                     int *height)
{
    GnomeRROutputInfo **outputs;
    gboolean active;
    int i;
    int ht, vt;
    int total_w = 0, total_h = 0;

    outputs = gnome_rr_config_get_outputs (self->priv->config);

    /*
     * iterate over all the outputs from 0,0 -> h,v
     * find the output for each tile,
     * if it is the 0 tile, store the x/y offsets.
     * if the tile is active, add the tile to the total w/h
     * for the output if the tile is in the 0 row or 0 column.
     */
    for (ht = 0; ht < self->priv->tile.max_horiz_tiles; ht++)
    {
        for (vt = 0; vt < self->priv->tile.max_vert_tiles; vt++)
        {
            for (i = 0; outputs[i]; i++)
            {
                GnomeRRTile *this_tile = &outputs[i]->priv->tile;

                if (!outputs[i]->priv->is_tiled)
                    continue;

                if (this_tile->group_id != self->priv->tile.group_id)
                    continue;

                if (this_tile->loc_horiz != ht ||
                    this_tile->loc_vert != vt)
                    continue;

                if (vt == 0 && ht == 0)
                {
                    if (x)
                        *x = outputs[i]->priv->x;
                    if (y)
                        *y = outputs[i]->priv->y;
                }

                active = gnome_rr_output_info_is_active (outputs[i]);
                if (!active)
                    continue;

                if (this_tile->loc_horiz == 0)
                    total_h += outputs[i]->priv->height;

                if (this_tile->loc_vert == 0)
                    total_w += outputs[i]->priv->width;
            }
        }
    }

    if (width)
        *width = total_w;
    if (height)
        *height = total_h;
}

/**
 * gnome_rr_output_info_get_geometry:
 * @self: a #GnomeRROutputInfo
 * @x: (out) (allow-none):
 * @y: (out) (allow-none):
 * @width: (out) (allow-none):
 * @height: (out) (allow-none):
 *
 * Get the geometry for the monitor connected to the specified output.
 * If the monitor is a tiled monitor, it returns the geometry for the complete monitor.
 */
void gnome_rr_output_info_get_geometry (GnomeRROutputInfo *self, int *x, int *y, int *width, int *height)
{
    g_return_if_fail (GNOME_IS_RR_OUTPUT_INFO (self));

    if (self->priv->is_tiled)
    {
        gnome_rr_output_info_get_tiled_geometry (self, x, y, width, height);
        return;
    }

    if (x)
	*x = self->priv->x;
    if (y)
	*y = self->priv->y;
    if (width)
	*width = self->priv->width;
    if (height)
	*height = self->priv->height;
}

static void gnome_rr_output_info_set_tiled_geometry (GnomeRROutputInfo *self, int x, int y, int width, int height)
{
    GnomeRROutputInfo **outputs;
    gboolean primary_tile_only = FALSE;
    int ht, vt, i;
    int x_off;

    primary_tile_only = TRUE;

    if (width == self->priv->total_tiled_width &&
        height == self->priv->total_tiled_height)
        primary_tile_only = FALSE;

    outputs = gnome_rr_config_get_outputs (self->priv->config);
    /*
     * iterate over all the outputs from 0,0 -> h,v
     * find the output for each tile,
     * if only the primary tile is being set, disable
     * the non-primary tiles, and set the output up
     * for tile 0 only.
     * if all tiles are being set, then store the
     * dimensions for this tile, and increase the offsets.
     * y_off is reset per column of tiles,
     * addx is incremented for the first row of tiles
     * to set the correct x offset.
     */
    x_off = 0;
    for (ht = 0; ht < self->priv->tile.max_horiz_tiles; ht++)
    {
        int y_off = 0;
        int addx = 0;
        for (vt = 0; vt < self->priv->tile.max_vert_tiles; vt++)
        {
            for (i = 0; outputs[i]; i++)
            {
                GnomeRRTile *this_tile = &outputs[i]->priv->tile;

                if (!outputs[i]->priv->is_tiled)
                    continue;

                if (this_tile->group_id != self->priv->tile.group_id)
                    continue;

                if (this_tile->loc_horiz != ht ||
                    this_tile->loc_vert != vt)
                    continue;

                /* for primary tile only configs turn off non-primary
                   tiles - turn them on for tiled ones */
                if (ht != 0 || vt != 0)
                    outputs[i]->priv->on = !primary_tile_only;

                if (primary_tile_only)
                {
                        if (ht == 0 && vt == 0)
                        {
                            outputs[i]->priv->x = x;
                            outputs[i]->priv->y = y;
                            outputs[i]->priv->width = width;
                            outputs[i]->priv->height = height;
                        }
                }
                else
                {
                    outputs[i]->priv->x = x + x_off;
                    outputs[i]->priv->y = y + y_off;
                    outputs[i]->priv->width = this_tile->width;
                    outputs[i]->priv->height = this_tile->height;

                    y_off += this_tile->height;
                    if (vt == 0)
                        addx = this_tile->width;
                }
            }
        }
        x_off += addx;
    }
}

/**
 * gnome_rr_output_info_set_geometry:
 * @self: a #GnomeRROutputInfo
 * @x: x offset for monitor
 * @y: y offset for monitor
 * @width: monitor width
 * @height: monitor height
 *
 * Set the geometry for the monitor connected to the specified output.
 * If the monitor is a tiled monitor, it sets the geometry for the complete monitor.
 */

void gnome_rr_output_info_set_geometry (GnomeRROutputInfo *self, int x, int y, int width, int height)
{
    g_return_if_fail (GNOME_IS_RR_OUTPUT_INFO (self));

    if (self->priv->is_tiled)
    {
        gnome_rr_output_info_set_tiled_geometry (self, x, y, width, height);
        return;
    }
    self->priv->x = x;
    self->priv->y = y;
    self->priv->width = width;
    self->priv->height = height;
}

int gnome_rr_output_info_get_refresh_rate (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), 0);

    return self->priv->rate;
}

void gnome_rr_output_info_set_refresh_rate (GnomeRROutputInfo *self, int rate)
{
    g_return_if_fail (GNOME_IS_RR_OUTPUT_INFO (self));

    self->priv->rate = rate;
}

GnomeRRRotation gnome_rr_output_info_get_rotation (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), GNOME_RR_ROTATION_0);

    return self->priv->rotation;
}

static void gnome_rr_output_info_set_tiled_rotation (GnomeRROutputInfo *self, GnomeRRRotation rotation)
{
    GnomeRROutputInfo **outputs;
    int x_off;
    int base_x = 0, base_y = 0;
    int ht, vt;
    int i;

    outputs = gnome_rr_config_get_outputs (self->priv->config);
    x_off = 0;
    /*
     * iterate over all the outputs from 0,0 -> h,v
     * find the output for each tile,
     * for all tiles set the rotation,
     * for the 0 tile use the base X/Y offsets
     * for non-0 tile, rotate the offsets of each
     * tile so things display correctly.
     */
    for (ht = 0; ht < self->priv->tile.max_horiz_tiles; ht++)
    {
        int y_off = 0;
        int addx = 0;
        for (vt = 0; vt < self->priv->tile.max_vert_tiles; vt++)
        {
            for (i = 0; outputs[i] != NULL; i++)
            {
                GnomeRRTile *this_tile = &outputs[i]->priv->tile;
                int new_x, new_y;

                if (!outputs[i]->priv->is_tiled)
                    continue;

                if (this_tile->group_id != self->priv->tile.group_id)
                    continue;

                if (this_tile->loc_horiz != ht ||
                    this_tile->loc_vert != vt)
                    continue;

                /* set tile rotation */
                outputs[i]->priv->rotation = rotation;

                /* for non-zero tiles - change the offsets */
                if (ht == 0 && vt == 0)
                {
                    base_x = outputs[i]->priv->x;
                    base_y = outputs[i]->priv->y;
                }
                else
                {
                    if ((rotation & GNOME_RR_ROTATION_90) || (rotation & GNOME_RR_ROTATION_270))
                    {
                        new_x = base_x + y_off;
                        new_y = base_y + x_off;
                    }
                    else
                    {
                        new_x = base_x + x_off;
                        new_y = base_y + y_off;
                    }
                    outputs[i]->priv->x = new_x;
                    outputs[i]->priv->y = new_y;
                    outputs[i]->priv->width = this_tile->width;
                    outputs[i]->priv->height = this_tile->height;
                }

                y_off += this_tile->height;
                if (vt == 0)
                    addx = this_tile->width;
            }
        }
        x_off += addx;
    }
}

void gnome_rr_output_info_set_rotation (GnomeRROutputInfo *self, GnomeRRRotation rotation)
{
    g_return_if_fail (GNOME_IS_RR_OUTPUT_INFO (self));

    if (self->priv->is_tiled)
    {
        gnome_rr_output_info_set_tiled_rotation (self, rotation);
        return;
    }
    self->priv->rotation = rotation;
}

gboolean gnome_rr_output_info_supports_rotation (GnomeRROutputInfo *self, GnomeRRRotation rotation)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), FALSE);

    return (self->priv->available_rotations & rotation);
}

/**
 * gnome_rr_output_info_is_connected:
 *
 * Returns: whether the output is physically connected to a monitor
 */
gboolean gnome_rr_output_info_is_connected (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), FALSE);

    return self->priv->connected;
}

/**
 * gnome_rr_output_info_get_vendor:
 * @self: a #GnomeRROutputInfo
 */
const char *
gnome_rr_output_info_get_vendor (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), NULL);

    return self->priv->vendor;
}

const char *
gnome_rr_output_info_get_product (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), NULL);

    return self->priv->product;
}

const char *
gnome_rr_output_info_get_serial (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), NULL);

    return self->priv->serial;
}

double gnome_rr_output_info_get_aspect_ratio (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), 0);

    return self->priv->aspect;
}

/**
 * gnome_rr_output_info_get_display_name:
 *
 * Returns: (transfer none): the display name of this output
 */
char *gnome_rr_output_info_get_display_name (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), NULL);

    return self->priv->display_name;
}

gboolean gnome_rr_output_info_get_primary (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), FALSE);

    return self->priv->primary;
}

void gnome_rr_output_info_set_primary (GnomeRROutputInfo *self, gboolean primary)
{
    g_return_if_fail (GNOME_IS_RR_OUTPUT_INFO (self));

    self->priv->primary = primary;
}

int gnome_rr_output_info_get_preferred_width (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), 0);

    return self->priv->pref_width;
}

int gnome_rr_output_info_get_preferred_height (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), 0);

    return self->priv->pref_height;
}

gboolean gnome_rr_output_info_get_underscanning (GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), FALSE);

    return self->priv->underscanning;
}

void gnome_rr_output_info_set_underscanning (GnomeRROutputInfo *self,
                                             gboolean underscanning)
{
    g_return_if_fail (GNOME_IS_RR_OUTPUT_INFO (self));

    self->priv->underscanning = underscanning;
}

/**
 * gnome_rr_output_info_is_primary_tile
 * @self: a #GnomeRROutputInfo
 *
 * Returns: %TRUE if the specified output is connected to
 * the primary tile of a monitor or to an untiled monitor,
 * %FALSE if the output is connected to a secondary tile.
 */
gboolean gnome_rr_output_info_is_primary_tile(GnomeRROutputInfo *self)
{
    g_return_val_if_fail (GNOME_IS_RR_OUTPUT_INFO (self), FALSE);

    if (!self->priv->is_tiled)
        return TRUE;

    if (self->priv->tile.loc_horiz == 0 &&
        self->priv->tile.loc_vert == 0)
        return TRUE;

    return FALSE;
}