Debian Codegraph @

gdk/x11/gdkmain-x11.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25 #include "config.h"
26
27 #include "gdkdeviceprivate.h"
28 #include "gdkinternals.h"
29 #include "gdkintl.h"
30 #include "gdkasync.h"
31 #include "gdkdisplay-x11.h"
32 #include "gdkprivate-x11.h"
33
34 #include <glib/gprintf.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <limits.h>
39 #include <errno.h>
40
41 #include <X11/Xatom.h>
42 #include <X11/Xlib.h>
43 #include <X11/Xutil.h>
44
45 #ifdef HAVE_XKB
46 #include <X11/XKBlib.h>
47 #endif
48
49 /**
50 * SECTION:x_interaction
51 * @Short_description: X backend-specific functions
52 * @Title: X Window System Interaction
53 *
54 * The functions in this section are specific to the GDK X11 backend.
55 * To use them, you need to include the `<gdk/gdkx.h>` header and use
56 * the X11-specific pkg-config files to build your application (either
57 * `gdk-x11-3.0` or `gtk+-x11-3.0`).
58 *
59 * To make your code compile with other GDK backends, guard backend-specific
60 * calls by an ifdef as follows. Since GDK may be built with multiple
61 * backends, you should also check for the backend that is in use (e.g. by
62 * using the GDK_IS_X11_DISPLAY() macro).
63 * |[
64 * #ifdef GDK_WINDOWING_X11
65 * if (GDK_IS_X11_DISPLAY (display))
66 * {
67 * // make X11-specific calls here
68 * }
69 * else
70 * #endif
71 * #ifdef GDK_WINDOWING_QUARTZ
72 * if (GDK_IS_QUARTZ_DISPLAY (display))
73 * {
74 * // make Quartz-specific calls here
75 * }
76 * else
77 * #endif
78 * g_error ("Unsupported GDK backend");
79 * ]|
80 */
81
82 typedef struct _GdkPredicate GdkPredicate;
83
84 struct _GdkPredicate
85 {
86 GdkEventFunc func;
87 gpointer data;
88 };
89
90 /* non-GDK previous error handler */
91 typedef int (*GdkXErrorHandler) (Display *, XErrorEvent *);
92 static GdkXErrorHandler _gdk_old_error_handler;
93 /* number of times we've pushed the GDK error handler */
94 static int _gdk_error_handler_push_count = 0;
95
96 /*
97 * Private function declarations
98 */
99
100 static int gdk_x_error (Display *display,
101 XErrorEvent *error);
102 static int gdk_x_io_error (Display *display);
103
104 void
105 _gdk_x11_windowing_init (void)
106 {
107 XSetErrorHandler (gdk_x_error);
108 XSetIOErrorHandler (gdk_x_io_error);
109
110 gdk_window_add_filter (NULL,
111 _gdk_wm_protocols_filter,
112 NULL);
113 gdk_window_add_filter (NULL,
114 _gdk_x11_dnd_filter,
115 NULL);
116 }
117
118 GdkGrabStatus
119 _gdk_x11_convert_grab_status (gint status)
120 {
121 switch (status)
122 {
123 case GrabSuccess:
124 return GDK_GRAB_SUCCESS;
125 case AlreadyGrabbed:
126 return GDK_GRAB_ALREADY_GRABBED;
127 case GrabInvalidTime:
128 return GDK_GRAB_INVALID_TIME;
129 case GrabNotViewable:
130 return GDK_GRAB_NOT_VIEWABLE;
131 case GrabFrozen:
132 return GDK_GRAB_FROZEN;
133 }
134
135 g_assert_not_reached();
136
137 return 0;
138 }
139
140 /*
141 * _gdk_x11_window_grab_check_unmap:
142 * @window: a #GdkWindow
143 * @serial: serial from Unmap event (or from NextRequest(display)
144 * if the unmap is being done by this client.)
145 *
146 * Checks to see if an unmap request or event causes the current
147 * grab window to become not viewable, and if so, clear the
148 * the pointer we keep to it.
149 **/
150 void
151 _gdk_x11_window_grab_check_unmap (GdkWindow *window,
152 gulong serial)
153 {
154 GdkDisplay *display = gdk_window_get_display (window);
155 GdkDeviceManager *device_manager;
156 GList *devices, *d;
157
158 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
159 device_manager = gdk_display_get_device_manager (display);
160
161 /* Get all devices */
162 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
163 devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_SLAVE));
164 devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING));
165 G_GNUC_END_IGNORE_DEPRECATIONS;
166
167 /* End all grabs on the newly hidden window */
168 for (d = devices; d; d = d->next)
169 _gdk_display_end_device_grab (display, d->data, serial, window, TRUE);
170
171 g_list_free (devices);
172 }
173
174 /*
175 * _gdk_x11_window_grab_check_destroy:
176 * @window: a #GdkWindow
177 *
178 * Checks to see if window is the current grab window, and if
179 * so, clear the current grab window.
180 **/
181 void
182 _gdk_x11_window_grab_check_destroy (GdkWindow *window)
183 {
184 GdkDisplay *display = gdk_window_get_display (window);
185 GdkDeviceManager *device_manager;
186 GdkDeviceGrabInfo *grab;
187 GList *devices, *d;
188
189 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
190 device_manager = gdk_display_get_device_manager (display);
191
192 /* Get all devices */
193 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
194 devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_SLAVE));
195 devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING));
196 G_GNUC_END_IGNORE_DEPRECATIONS;
197
198 for (d = devices; d; d = d->next)
199 {
200 /* Make sure there is no lasting grab in this native window */
201 grab = _gdk_display_get_last_device_grab (display, d->data);
202
203 if (grab && grab->native_window == window)
204 {
205 /* We don't know the actual serial to end, but it
206 doesn't really matter as this only happens
207 after we get told of the destroy from the
208 server so we know its ended in the server,
209 just make sure its ended. */
210 grab->serial_end = grab->serial_start;
211 grab->implicit_ungrab = TRUE;
212 }
213 }
214
215 g_list_free (devices);
216 }
217
218 /*
219 *--------------------------------------------------------------
220 * gdk_x_io_error
221 *
222 * The X I/O error handling routine.
223 *
224 * Arguments:
225 * "display" is the X display the error orignated from.
226 *
227 * Results:
228 * An X I/O error basically means we lost our connection
229 * to the X server. There is not much we can do to
230 * continue, so simply print an error message and exit.
231 *
232 * Side effects:
233 *
234 *--------------------------------------------------------------
235 */
236
237 static int
238 gdk_x_io_error (Display *display)
239 {
240 /* This is basically modelled after the code in XLib. We need
241 * an explicit error handler here, so we can disable our atexit()
242 * which would otherwise cause a nice segfault.
243 * We g_debug() instead of g_warning(), because g_warning()
244 * could possibly be redirected to the log
245 */
246 g_debug ("%s: Fatal IO error %d (%s) on X server %s.\n",
247 g_get_prgname (),
248 errno, g_strerror (errno),
249 display ? DisplayString (display) : gdk_get_display_arg_name ());
250
251 _exit (1);
252 }
253
254 /* X error handler. Keep the name the same because people are used to
255 * breaking on it in the debugger.
256 */
257 static int
258 gdk_x_error (Display *xdisplay,
259 XErrorEvent *error)
260 {
261 if (error->error_code)
262 {
263 GdkDisplay *error_display;
264 GdkDisplayManager *manager;
265 GSList *displays;
266
267 /* Figure out which GdkDisplay if any got the error. */
268 error_display = NULL;
269 manager = gdk_display_manager_get ();
270 displays = gdk_display_manager_list_displays (manager);
271 while (displays != NULL)
272 {
273 GdkX11Display *gdk_display = displays->data;
274
275 if (GDK_IS_X11_DISPLAY (gdk_display) &&
276 xdisplay == gdk_display->xdisplay)
277 {
278 error_display = GDK_DISPLAY (gdk_display);
279 g_slist_free (displays);
280 displays = NULL;
281 }
282 else
283 {
284 displays = g_slist_delete_link (displays, displays);
285 }
286 }
287
288 if (error_display == NULL)
289 {
290 /* Error on an X display not opened by GDK. Ignore. */
291
292 return 0;
293 }
294 else
295 {
296 _gdk_x11_display_error_event (error_display, error);
297 }
298 }
299
300 return 0;
301 }
302
303 void
304 _gdk_x11_error_handler_push (void)
305 {
306 GdkXErrorHandler previous;
307
308 previous = XSetErrorHandler (gdk_x_error);
309
310 if (_gdk_error_handler_push_count > 0)
311 {
312 if (previous != gdk_x_error)
313 g_warning ("XSetErrorHandler() called with a GDK error trap pushed. Don't do that.");
314 }
315 else
316 {
317 _gdk_old_error_handler = previous;
318 }
319
320 _gdk_error_handler_push_count += 1;
321 }
322
323 void
324 _gdk_x11_error_handler_pop (void)
325 {
326 g_return_if_fail (_gdk_error_handler_push_count > 0);
327
328 _gdk_error_handler_push_count -= 1;
329
330 if (_gdk_error_handler_push_count == 0)
331 {
332 XSetErrorHandler (_gdk_old_error_handler);
333 _gdk_old_error_handler = NULL;
334 }
335 }
336
337 gint
338 _gdk_x11_display_send_xevent (GdkDisplay *display,
339 Window window,
340 gboolean propagate,
341 glong event_mask,
342 XEvent *event_send)
343 {
344 gboolean result;
345
346 if (gdk_display_is_closed (display))
347 return FALSE;
348
349 gdk_x11_display_error_trap_push (display);
350 result = XSendEvent (GDK_DISPLAY_XDISPLAY (display), window,
351 propagate, event_mask, event_send);
352 XSync (GDK_DISPLAY_XDISPLAY (display), False);
353
354 if (gdk_x11_display_error_trap_pop (display))
355 return FALSE;
356
357 return result;
358 }
359
360 void
361 _gdk_x11_region_get_xrectangles (const cairo_region_t *region,
362 gint x_offset,
363 gint y_offset,
364 gint scale,
365 XRectangle **rects,
366 gint *n_rects)
367 {
368 XRectangle *rectangles;
369 cairo_rectangle_int_t box;
370 gint i, n;
371
372 n = cairo_region_num_rectangles (region);
373 rectangles = g_new (XRectangle, n);
374
375 for (i = 0; i < n; i++)
376 {
377 cairo_region_get_rectangle (region, i, &box);
378 rectangles[i].x = CLAMP ((box.x + x_offset) * scale, G_MINSHORT, G_MAXSHORT);
379 rectangles[i].y = CLAMP ((box.y + y_offset) * scale, G_MINSHORT, G_MAXSHORT);
380 rectangles[i].width = CLAMP (box.width * scale, G_MINSHORT, G_MAXSHORT);
381 rectangles[i].height = CLAMP (box.height * scale, G_MINSHORT, G_MAXSHORT);
382 }
383
384 *n_rects = n;
385 *rects = rectangles;
386 }
387
388 /**
389 * gdk_x11_grab_server:
390 *
391 * Call gdk_x11_display_grab() on the default display.
392 * To ungrab the server again, use gdk_x11_ungrab_server().
393 *
394 * gdk_x11_grab_server()/gdk_x11_ungrab_server() calls can be nested.
395 **/
396 void
397 gdk_x11_grab_server (void)
398 {
399 gdk_x11_display_grab (gdk_display_get_default ());
400 }
401
402 /**
403 * gdk_x11_ungrab_server:
404 *
405 * Ungrab the default display after it has been grabbed with
406 * gdk_x11_grab_server().
407 **/
408 void
409 gdk_x11_ungrab_server (void)
410 {
411 gdk_x11_display_ungrab (gdk_display_get_default ());
412 }
413
414 /**
415 * gdk_x11_get_default_screen:
416 *
417 * Gets the default GTK+ screen number.
418 *
419 * Returns: returns the screen number specified by
420 * the --display command line option or the DISPLAY environment
421 * variable when gdk_init() calls XOpenDisplay().
422 **/
423 gint
424 gdk_x11_get_default_screen (void)
425 {
426 return gdk_x11_screen_get_number (gdk_screen_get_default ());
427 }
428
429 /**
430 * gdk_x11_get_default_root_xwindow:
431 *
432 * Gets the root window of the default screen
433 * (see gdk_x11_get_default_screen()).
434 *
435 * Returns: an Xlib Window.
436 **/
437 Window
438 gdk_x11_get_default_root_xwindow (void)
439 {
440 return GDK_SCREEN_XROOTWIN (gdk_screen_get_default ());
441 }
442
443 /**
444 * gdk_x11_get_default_xdisplay:
445 *
446 * Gets the default GTK+ display.
447 *
448 * Returns: (transfer none): the Xlib Display* for
449 * the display specified in the `--display` command
450 * line option or the `DISPLAY` environment variable.
451 **/
452 Display *
453 gdk_x11_get_default_xdisplay (void)
454 {
455 return GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
456 }
457
458 /**
459 * gdk_x11_get_parent_relative_pattern:
460 *
461 * Used with gdk_window_set_background_pattern() to inherit background from
462 * parent window. Useful for imitating transparency when compositing is not
463 * available. Otherwise behaves like a transparent pattern.
464 *
465 * Since: 3.24.2
466 *
467 * Deprecated: 3.24: Don't use this function
468 **/
469 cairo_pattern_t *
470 gdk_x11_get_parent_relative_pattern (void)
471 {
472 static cairo_pattern_t *parent_relative_pattern = NULL;
473
474 if (G_UNLIKELY (parent_relative_pattern == NULL))
475 parent_relative_pattern = cairo_pattern_create_rgba (0.0, 0.0, 0.0, 0.0);
476
477 return parent_relative_pattern;
478 }