Code

gtk-tpdfv-screen, tpdfview: Implemented "move window to next screen".
[tpdfview.git] / src / gtk-tpdfv-screen.c
1 /*
2  * tpdfview - src/gtk-tpdfv-screen.c
3  * Copyright (C) 2011 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * This module provides an abstraction layer for virtual screens.
30  */
32 #include "gtk-tpdfv.h"
34 #include <assert.h>
35 #include <errno.h>
37 #include <gtk/gtk.h>
39 #include <stdlib.h>
41 typedef struct {
42         GdkScreen *screen;
43         gint n_monitor;
45         gint x;
46         gint y;
47         gint width;
48         gint height;
49 } screen_t;
51 struct gtk_tpdfv_screens {
52         gint      num_screens;
53         screen_t *screens;
54 };
56 /*
57  * Private helper functions.
58  */
61 /*
62  * Public API.
63  */
65 gtk_tpdfv_screens_t *
66 gtk_tpdfv_screens_init(const gchar *display_name)
67 {
68         gtk_tpdfv_screens_t *screens;
70         GdkDisplay *gdk_disp;
71         gint screens_count;
73         gint i;
75         if (display_name)
76                 gdk_disp = gdk_display_open(display_name);
77         else
78                 gdk_disp = gdk_display_get_default();
80         if (! gdk_disp)
81                 return NULL;
83         screens = (gtk_tpdfv_screens_t *)malloc(sizeof(*screens));
84         if (! screens)
85                 return NULL;
87         screens->screens     = NULL;
88         screens->num_screens = 0;
90         screens_count = gdk_display_get_n_screens(gdk_disp);
91         for (i = 0; i < screens_count; ++i) {
92                 GdkScreen *gdk_screen = gdk_display_get_screen(gdk_disp, i);
94                 gint monitors_count = gdk_screen_get_n_monitors(gdk_screen);
95                 gint j;
97                 for (j = 0; j < monitors_count; ++j) {
98                         screen_t *screen;
100                         GdkRectangle geo;
102                         screen = (screen_t *)realloc(screens->screens,
103                                         (size_t)(screens->num_screens + 1) * sizeof(*screen));
104                         if (! screen) {
105                                 if (screens->screens)
106                                         free(screens->screens);
107                                 free(screens);
108                                 return NULL;
109                         }
111                         screens->screens = screen;
112                         screen = screens->screens + screens->num_screens;
113                         ++screens->num_screens;
115                         screen->screen = gdk_screen;
116                         screen->n_monitor = j;
118                         gdk_screen_get_monitor_geometry(gdk_screen, j, &geo);
119                         screen->x = geo.x; screen->y = geo.y;
120                         screen->width = geo.width; screen->height = geo.height;
121                 }
122         }
123         return screens;
124 } /* gtk_tpdfv_screens_init */
126 void
127 gtk_tpdfv_screens_destroy(gtk_tpdfv_screens_t *screens)
129         if (! screens)
130                 return;
132         if (screens->screens)
133                 free(screens->screens);
134         free(screens);
135 } /* gtk_tpdfv_screens_destroy */
137 gint
138 gtk_tpdfv_screens_number(const gtk_tpdfv_screens_t *screens)
140         if (! screens)
141                 return -1;
142         return screens->num_screens;
143 } /* gtk_tpdfv_screens_number */
145 void
146 gtk_tpdfv_screens_window_set(const gtk_tpdfv_screens_t *screens,
147                 GtkWindow *window, gint screen)
149         screen_t *s_old, *s_new;
151         gint x = 0, y = 0;
152         gint current;
154         if ((! screens) || (! window)
155                         || (screen < 0) || (screen >= screens->num_screens))
156                 return;
158         gtk_window_get_position(window, &x, &y);
160         current = gtk_tpdfv_screens_window_get(screens, window);
161         assert((0 <= current) && (current < screens->num_screens));
163         s_old = screens->screens + current;
164         s_new = screens->screens + screen;
166         gtk_window_set_screen(window, s_new->screen);
167         gtk_window_move(window,
168                         s_new->x + (x - s_old->x), s_new->y + (y - s_old->y));
169 } /* gtk_tpdfv_screens_window_set */
171 gint
172 gtk_tpdfv_screens_window_get(const gtk_tpdfv_screens_t *screens,
173                 GtkWindow *window)
175         GdkScreen *gdk_screen;
177         gint x = 0, y = 0;
178         gint i;
180         if ((! screens) || (! window))
181                 return -1;
183         gdk_screen = gtk_window_get_screen(window);
184         gtk_window_get_position(window, &x, &y);
186         for (i = 0; i < screens->num_screens; ++i) {
187                 screen_t *screen = screens->screens + i;
189                 if ((screen->screen == gdk_screen)
190                                 && (screen->x <= x) && (x <= screen->x + screen->width)
191                                 && (screen->y <= y) && (y <= screen->y + screen->height))
192                         return i;
193         }
194         return -1;
195 } /* gtk_tpdfv_screens_window_set */
197 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */