summaryrefslogtreecommitdiff
path: root/common/xfwm-common.c
blob: 673f157efeaf35a666ea8746a806c782e45bb21c (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
/* 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"
#define DEFAULT_REFRESH_RATE 30


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)
{
  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;
}



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;
  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);

  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;
  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);

  if (scaled && scale != 1)
    xfwm_geometry_convert_to_device_pixels (geometry, scale);

  return TRUE;
}



gint
xfwm_get_primary_refresh_rate (GdkScreen *screen)
{
  GdkDisplay *display;
  GdkMonitor *monitor;
  int         refresh_rate;

  display = gdk_screen_get_display (screen);
  monitor = gdk_display_get_primary_monitor (display);
  refresh_rate = 0;

  if (monitor)
    refresh_rate = gdk_monitor_get_refresh_rate (monitor) / 1000;

  if (refresh_rate)
    return refresh_rate;

  return DEFAULT_REFRESH_RATE;
}



gboolean
xfwm_monitor_is_primary (GdkScreen *screen,
                         gint      monitor_num)
{
  GdkDisplay *display;
  GdkMonitor *monitor;

  display = gdk_screen_get_display (screen);
  monitor = gdk_display_get_monitor (display, monitor_num);

  return gdk_monitor_is_primary (monitor);
}



gint
xfwm_get_n_monitors (GdkScreen *screen)
{
  return gdk_display_get_n_monitors (gdk_screen_get_display (screen));
}



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);
}



gboolean
xfwm_is_default_screen (GdkScreen *screen)
{
  return gdk_x11_screen_get_screen_number (screen) == gdk_x11_get_default_screen();
}