summaryrefslogtreecommitdiff
path: root/gdk/x11/gdkscreen-x11.c
blob: eda13614cb9acfb8232be3e2d975cc92d117e7f0 (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
/*
 * gdkscreen-x11.c
 * 
 * Copyright 2001 Sun Microsystems Inc. 
 *
 * Erwann Chenede <erwann.chenede@sun.com>
 *
 * This 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.
 *
 * This 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 this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include "gdkscreen.h"
#include "gdkscreen-x11.h"
#include "gdkdisplay.h"
#include "gdkdisplay-x11.h"
#include "gdkx.h"

static void         gdk_screen_x11_class_init       (GdkScreenX11Class *klass);
static GdkDisplay * gdk_screen_x11_get_display           (GdkScreen             *screen);
static gint         gdk_screen_x11_get_width             (GdkScreen             *screen);
static gint         gdk_screen_x11_get_height            (GdkScreen             *screen);
static gint         gdk_screen_x11_get_width_mm          (GdkScreen             *screen);
static gint         gdk_screen_x11_get_height_mm         (GdkScreen             *screen);
static gint         gdk_screen_x11_get_default_depth     (GdkScreen             *screen);
static GdkWindow *  gdk_screen_x11_get_root_window       (GdkScreen             *screen);
static gint         gdk_screen_x11_get_screen_num        (GdkScreen             *screen);
static GdkColormap *gdk_screen_x11_get_default_colormap  (GdkScreen             *screen);
static void         gdk_screen_x11_set_default_colormap  (GdkScreen             *screen,
							  GdkColormap           *colormap);
static GdkWindow *  gdk_screen_x11_get_window_at_pointer (GdkScreen             *screen,
							  gint                  *win_x,
							  gint                  *win_y);
static void         gdk_screen_x11_finalize		 (GObject		*object);

static gboolean      gdk_screen_x11_use_virtual_screen    (GdkScreen       *screen);
static gint          gdk_screen_x11_get_n_monitors        (GdkScreen       *screen);
static GdkRectangle *gdk_screen_x11_get_monitor_geometry  (GdkScreen       *screen,
							   gint             num_monitor);
static gint          gdk_screen_x11_get_monitor_at_point  (GdkScreen       *screen,
							   gint             x,
							   gint             y);
static gint          gdk_screen_x11_get_monitor_at_window (GdkScreen       *screen,
							   GdkNativeWindow  anid);

GType gdk_screen_x11_get_type ();
static gpointer parent_class = NULL;

GType
gdk_screen_x11_get_type ()
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
	{
	  sizeof (GdkScreenX11Class),
	  (GBaseInitFunc) NULL,
	  (GBaseFinalizeFunc) gdk_screen_x11_finalize,
	  (GClassInitFunc) gdk_screen_x11_class_init,
	  NULL,			/* class_finalize */
	  NULL,			/* class_data */
	  sizeof (GdkScreenX11),
	  0,			/* n_preallocs */
	  (GInstanceInitFunc) NULL,
	};
      object_type = g_type_register_static (GDK_TYPE_SCREEN,
					    "GdkScreenX11",
					    &object_info, 0);
    }
  return object_type;
}

void
gdk_screen_x11_class_init (GdkScreenX11Class * klass)
{
  GdkScreenClass *screen_class = GDK_SCREEN_CLASS (klass);
  
  screen_class->get_display = gdk_screen_x11_get_display;
  screen_class->get_width = gdk_screen_x11_get_width;
  screen_class->get_height = gdk_screen_x11_get_height;
  screen_class->get_width_mm = gdk_screen_x11_get_width_mm;
  screen_class->get_height_mm = gdk_screen_x11_get_height_mm;
  screen_class->get_root_depth = gdk_screen_x11_get_default_depth;
  screen_class->get_screen_num = gdk_screen_x11_get_screen_num;
  screen_class->get_root_window = gdk_screen_x11_get_root_window;
  screen_class->get_default_colormap = gdk_screen_x11_get_default_colormap;
  screen_class->set_default_colormap = gdk_screen_x11_set_default_colormap;
  screen_class->get_window_at_pointer = gdk_screen_x11_get_window_at_pointer;
  screen_class->use_virtual_screen = gdk_screen_x11_use_virtual_screen;
  screen_class->get_n_monitors = gdk_screen_x11_get_n_monitors;
  screen_class->get_monitor_geometry = gdk_screen_x11_get_monitor_geometry;
  screen_class->get_monitor_at_point = gdk_screen_x11_get_monitor_at_point;
  screen_class->get_monitor_at_window = gdk_screen_x11_get_monitor_at_window;
  
  G_OBJECT_CLASS (klass)->finalize = gdk_screen_x11_finalize;
  parent_class = g_type_class_peek_parent (klass);
}

static GdkDisplay *
gdk_screen_x11_get_display (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  return screen_x11->display;
}

static gint
gdk_screen_x11_get_width (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  return WidthOfScreen (screen_x11->xscreen);
}

static gint
gdk_screen_x11_get_height (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  return HeightOfScreen (screen_x11->xscreen);
}

static gint
gdk_screen_x11_get_width_mm (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  return WidthMMOfScreen (screen_x11->xscreen);
}

static gint
gdk_screen_x11_get_height_mm (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  return HeightMMOfScreen (screen_x11->xscreen);
}

static gint
gdk_screen_x11_get_default_depth (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  return DefaultDepthOfScreen (screen_x11->xscreen);
}

static gint
gdk_screen_x11_get_screen_num (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  
  return screen_x11->screen_num;
}

static GdkWindow *
gdk_screen_x11_get_root_window (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  gdk_drawable_ref (screen_x11->root_window);
  return screen_x11->root_window;
}

static GdkColormap *
gdk_screen_x11_get_default_colormap (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  return screen_x11->default_colormap;
}

static void
gdk_screen_x11_set_default_colormap (GdkScreen *screen,
				     GdkColormap * colormap)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  
  screen_x11->default_colormap = colormap;
}

static GdkWindow *
gdk_screen_x11_get_window_at_pointer (GdkScreen *screen,
				      gint      *win_x,
				      gint      *win_y)
{
  GdkWindow *window;
  Window root;
  Window xwindow;
  Window xwindow_last = 0;
  Display *xdisplay;
  int rootx = -1, rooty = -1;
  int winx, winy;
  unsigned int xmask;

  xwindow = GDK_SCREEN_XROOTWIN (screen);
  xdisplay = GDK_SCREEN_XDISPLAY (screen);

  XGrabServer (xdisplay);
  while (xwindow)
    {
      xwindow_last = xwindow;
      XQueryPointer (xdisplay, xwindow,
		     &root, &xwindow, &rootx, &rooty, &winx, &winy, &xmask);
    }
  XUngrabServer (xdisplay);

  window = gdk_window_lookup_for_display (GDK_SCREEN_DISPLAY(screen),
					  xwindow_last);
  if (win_x)
    *win_x = window ? winx : -1;
  if (win_y)
    *win_y = window ? winy : -1;

  return window;
}

static void
gdk_screen_x11_finalize (GObject *object)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (object);
  /* int i; */
  g_object_unref (G_OBJECT (screen_x11->root_window));
  
  /* Visual Part (Need to implement finalize for Visuals for a clean
   * finalize) */
  /* for (i=0;i<screen_x11->nvisuals;i++)
    g_object_unref (G_OBJECT (screen_x11->visuals[i]));*/
  g_free (screen_x11->visuals);
  g_hash_table_destroy (screen_x11->visual_hash);
  /* X settings */
  g_free (screen_x11->xsettings_client);
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gboolean 
gdk_screen_x11_use_virtual_screen (GdkScreen *screen)
{
  g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
  return GDK_SCREEN_X11 (screen)->use_virtual_screen;
}

static gint 
gdk_screen_x11_get_n_monitors (GdkScreen *screen)
{
  g_return_val_if_fail (GDK_IS_SCREEN (screen), 1);
  return GDK_SCREEN_X11 (screen)->num_monitors;
}

static GdkRectangle *
gdk_screen_x11_get_monitor_geometry (GdkScreen *screen, 
				     gint       num_monitor)
{
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
  g_return_val_if_fail (num_monitor < GDK_SCREEN_X11 (screen)->num_monitors, NULL);
  
  return &GDK_SCREEN_X11 (screen)->monitors[num_monitor];
}

static gint 
gdk_screen_x11_get_monitor_at_point (GdkScreen *screen,
				     gint       x,
				     gint       y)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  int i;
  GdkRectangle *monitor;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);

  for (i = 0, monitor = screen_x11->monitors; 
       i < screen_x11->num_monitors; 
       i++, monitor++)
    {
      if (x >= monitor->x &&
	  x < monitor->x + monitor->width &&
	  y >= monitor->y &&
	  y < (monitor->y + monitor->height))
	return i;
    }

  return -1; 
}

static gint 
gdk_screen_x11_get_monitor_at_window (GdkScreen      *screen, 
				      GdkNativeWindow anid)
{
  gint x, y, width, height, depth;
  gint left_monitor, right_monitor, diff_monitor;
  GdkRectangle *left_monitor_rect, *right_monitor_rect;
  
  GdkWindow *window = gdk_window_lookup_for_display (GDK_SCREEN_DISPLAY (screen), anid);
  
  gdk_window_get_geometry (window, &x, &y, &width, &height, &depth);
  gdk_window_get_position (window, &x, &y);

  left_monitor = gdk_screen_x11_get_monitor_at_point (screen, x, y);
  right_monitor = gdk_screen_x11_get_monitor_at_point (screen, x + width, 
						       y + height);
  left_monitor_rect = gdk_screen_x11_get_monitor_geometry (screen,
							   left_monitor);
  right_monitor_rect = gdk_screen_x11_get_monitor_geometry (screen,
							    right_monitor);
  
  diff_monitor = right_monitor - left_monitor;
  if (diff_monitor == 0)
    {
      return left_monitor;
    }
  if (diff_monitor == 1)
    {
      int dist_left, dist_right;
      
      dist_left = left_monitor_rect->x + left_monitor_rect->width - x;
      dist_right = x + width - right_monitor_rect->x;
      
      if (dist_left >= dist_right)
	return left_monitor;

      return right_monitor;
    }
      /* Window window span on at least 3 monitors */
    return left_monitor + 1;
}

Screen *
gdk_x11_screen_get_xscreen (GdkScreen *screen)
{
  return GDK_SCREEN_X11 (screen)->xscreen;
}

int
gdk_x11_screen_get_screen_number (GdkScreen *screen)
{
  return GDK_SCREEN_X11 (screen)->screen_num;
}