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
|
/* vi:set sw=2 sts=2 ts=2 et ai tw=100: */
/*-
* Copyright (c) 2017 Viktor Odintsev <zakhams@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <string.h>
#include <gdk/gdkx.h>
#include "xfwm-common.h"
void
xfwm_widget_reparent (GtkWidget *widget,
GtkWidget *new_parent)
{
GtkWidget *old_parent;
g_return_if_fail (GTK_IS_WIDGET (widget));
g_return_if_fail (GTK_IS_CONTAINER (new_parent));
old_parent = gtk_widget_get_parent (widget);
g_return_if_fail (old_parent != NULL);
if (old_parent != new_parent)
{
g_object_ref (widget);
gtk_container_remove (GTK_CONTAINER (old_parent), widget);
gtk_container_add (GTK_CONTAINER (new_parent), widget);
g_object_unref (widget);
}
}
void
xfwm_get_screen_dimensions (gint *width, gint *height)
{
#if GTK_CHECK_VERSION(3, 22, 0)
GdkDisplay *display;
GdkMonitor *monitor;
GdkRectangle geometry;
display = gdk_display_get_default ();
monitor = gdk_display_get_primary_monitor (display);
gdk_monitor_get_geometry (monitor, &geometry);
if (width != NULL)
*width = geometry.width;
if (height != NULL)
*height = geometry.height;
#else
if (width != NULL)
*width = gdk_screen_width ();
if (height != NULL)
*height = gdk_screen_height ();
#endif
}
static void
xfwm_geometry_convert_to_device_pixels (GdkRectangle *geometry,
gint scale)
{
if (geometry != NULL)
{
geometry->x *= scale;
geometry->y *= scale;
geometry->width *= scale;
geometry->height *= scale;
}
}
void
xfwm_get_monitor_geometry (GdkScreen *screen,
gint monitor_num,
GdkRectangle *geometry,
gboolean scaled)
{
gint scale;
#if GTK_CHECK_VERSION(3, 22, 0)
GdkDisplay *display;
GdkMonitor *monitor;
display = gdk_screen_get_display (screen);
monitor = gdk_display_get_monitor (display, monitor_num);
scale = gdk_monitor_get_scale_factor (monitor);
gdk_monitor_get_geometry (monitor, geometry);
#else
scale = gdk_screen_get_monitor_scale_factor (screen, monitor_num);
gdk_screen_get_monitor_geometry (screen, monitor_num, geometry);
#endif
if (scaled && scale != 1)
xfwm_geometry_convert_to_device_pixels (geometry, scale);
}
gboolean
xfwm_get_primary_monitor_geometry (GdkScreen *screen,
GdkRectangle *geometry,
gboolean scaled)
{
gint scale;
#if GTK_CHECK_VERSION(3, 22, 0)
GdkDisplay *display;
GdkMonitor *monitor;
display = gdk_screen_get_display (screen);
monitor = gdk_display_get_primary_monitor (display);
if (!monitor)
{
geometry->width = 0;
geometry->height = 0;
return FALSE;
}
scale = gdk_monitor_get_scale_factor (monitor);
gdk_monitor_get_geometry (monitor, geometry);
#else
gint monitor_num;
monitor_num = gdk_screen_get_primary_monitor (screen);
scale = gdk_screen_get_monitor_scale_factor (screen, monitor_num);
gdk_screen_get_monitor_geometry (screen, monitor_num, geometry);
#endif
if (scaled && scale != 1)
xfwm_geometry_convert_to_device_pixels (geometry, scale);
return TRUE;
}
gint
xfwm_get_primary_refresh_rate (GdkScreen *screen)
{
#if GTK_CHECK_VERSION(3, 22, 0)
GdkDisplay *display;
GdkMonitor *monitor;
display = gdk_screen_get_display (screen);
monitor = gdk_display_get_primary_monitor (display);
return gdk_monitor_get_refresh_rate (monitor) / 1000;
#else
return 60;
#endif
}
gboolean
xfwm_monitor_is_primary (GdkScreen *screen,
gint monitor_num)
{
#if GTK_CHECK_VERSION(3, 22, 0)
GdkDisplay *display;
GdkMonitor *monitor;
display = gdk_screen_get_display (screen);
monitor = gdk_display_get_monitor (display, monitor_num);
return gdk_monitor_is_primary (monitor);
#else
return gdk_screen_get_primary_monitor (screen) == monitor_num;
#endif
}
gint
xfwm_get_n_monitors (GdkScreen *screen)
{
#if GTK_CHECK_VERSION(3, 22, 0)
return gdk_display_get_n_monitors (gdk_screen_get_display (screen));
#else
return gdk_screen_get_n_monitors (screen);
#endif
}
static gchar *
substitute_screen_number (const gchar *display_name,
gint screen_number)
{
GString *str;
gchar *p;
str = g_string_new (display_name);
p = strrchr (str->str, '.');
if (p != NULL && p > strchr (str->str, ':'))
{
/* remove screen number from string */
g_string_truncate (str, p - str->str);
}
g_string_append_printf (str, ".%d", screen_number);
return g_string_free (str, FALSE);
}
gchar *
xfwm_make_display_name (GdkScreen *screen)
{
const gchar *name;
gint number;
name = gdk_display_get_name (gdk_screen_get_display (screen));
number = gdk_x11_screen_get_screen_number (screen);
return substitute_screen_number (name, number);
}
|