1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdkdeviceprivate.h"
30 #include "gdkinternals.h"
33 #include "gdkdisplay-x11.h"
34 #include "gdkprivate-x11.h"
36 #include <glib/gprintf.h>
43 #include <X11/Xatom.h>
45 #include <X11/Xutil.h>
48 #include <X11/XKBlib.h>
52 * SECTION:x_interaction
53 * @Short_description: X backend-specific functions
54 * @Title: X Window System Interaction
56 * The functions in this section are specific to the GDK X11 backend.
57 * To use them, you need to include the <literal><gdk/gdkx.h></literal>
58 * header and use the X11-specific pkg-config files to build your
59 * application (either <literal>gdk-x11-3.0</literal> or
60 * <literal>gtk+-x11-3.0</literal>).
62 * To make your code compile with other GDK backends, guard backend-specific
63 * calls by an ifdef as follows. Since GDK may be built with multiple
64 * backends, you should also check for the backend that is in use (e.g. by
65 * using the GDK_IS_X11_DISPLAY() macro).
67 * #ifdef GDK_WINDOWING_X11
68 * if (GDK_IS_X11_DISPLAY (display))
70 * /* make X11-specific calls here */
74 * #ifdef GDK_WINDOWING_QUARTZ
75 * if (GDK_IS_QUARTZ_DISPLAY (display))
77 * /* make Quartz-specific calls here &ast/
81 * g_error ("Unsupported GDK backend");
85 typedef struct _GdkPredicate GdkPredicate;
93 /* non-GDK previous error handler */
94 typedef int (*GdkXErrorHandler) (Display *, XErrorEvent *);
95 static GdkXErrorHandler _gdk_old_error_handler;
96 /* number of times we've pushed the GDK error handler */
97 static int _gdk_error_handler_push_count = 0;
100 * Private function declarations
103 #ifndef HAVE_XCONVERTCASE
104 static void gdkx_XConvertCase (KeySym symbol,
107 #define XConvertCase gdkx_XConvertCase
110 static int gdk_x_error (Display *display,
112 static int gdk_x_io_error (Display *display);
115 _gdk_x11_windowing_init (void)
117 XSetErrorHandler (gdk_x_error);
118 XSetIOErrorHandler (gdk_x_io_error);
122 _gdk_x11_convert_grab_status (gint status)
127 return GDK_GRAB_SUCCESS;
129 return GDK_GRAB_ALREADY_GRABBED;
130 case GrabInvalidTime:
131 return GDK_GRAB_INVALID_TIME;
132 case GrabNotViewable:
133 return GDK_GRAB_NOT_VIEWABLE;
135 return GDK_GRAB_FROZEN;
138 g_assert_not_reached();
144 * _gdk_x11_window_grab_check_unmap:
145 * @window: a #GdkWindow
146 * @serial: serial from Unmap event (or from NextRequest(display)
147 * if the unmap is being done by this client.)
149 * Checks to see if an unmap request or event causes the current
150 * grab window to become not viewable, and if so, clear the
151 * the pointer we keep to it.
154 _gdk_x11_window_grab_check_unmap (GdkWindow *window,
157 GdkDisplay *display = gdk_window_get_display (window);
158 GdkDeviceManager *device_manager;
161 device_manager = gdk_display_get_device_manager (display);
163 /* Get all devices */
164 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
165 devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_SLAVE));
166 devices = g_list_concat (devices, gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING));
168 /* End all grabs on the newly hidden window */
169 for (d = devices; d; d = d->next)
170 _gdk_display_end_device_grab (display, d->data, serial, window, TRUE);
172 g_list_free (devices);
176 * _gdk_x11_window_grab_check_destroy:
177 * @window: a #GdkWindow
179 * Checks to see if window is the current grab window, and if
180 * so, clear the current grab window.
183 _gdk_x11_window_grab_check_destroy (GdkWindow *window)
185 GdkDisplay *display = gdk_window_get_display (window);
186 GdkDeviceManager *device_manager;
187 GdkDeviceGrabInfo *grab;
190 device_manager = gdk_display_get_device_manager (display);
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));
197 for (d = devices; d; d = d->next)
199 /* Make sure there is no lasting grab in this native window */
200 grab = _gdk_display_get_last_device_grab (display, d->data);
202 if (grab && grab->native_window == window)
204 /* We don't know the actual serial to end, but it
205 doesn't really matter as this only happens
206 after we get told of the destroy from the
207 server so we know its ended in the server,
208 just make sure its ended. */
209 grab->serial_end = grab->serial_start;
210 grab->implicit_ungrab = TRUE;
214 g_list_free (devices);
218 *--------------------------------------------------------------
221 * The X I/O error handling routine.
224 * "display" is the X display the error orignated from.
227 * An X I/O error basically means we lost our connection
228 * to the X server. There is not much we can do to
229 * continue, so simply print an error message and exit.
233 *--------------------------------------------------------------
237 gdk_x_io_error (Display *display)
239 /* This is basically modelled after the code in XLib. We need
240 * an explicit error handler here, so we can disable our atexit()
241 * which would otherwise cause a nice segfault.
242 * We fprintf(stderr, instead of g_warning() because g_warning()
243 * could possibly be redirected to a dialog
247 g_warning ("The application '%s' lost its connection to the display %s;\n"
248 "most likely the X server was shut down or you killed/destroyed\n"
249 "the application.\n",
251 display ? DisplayString (display) : gdk_get_display_arg_name ());
255 g_warning ("%s: Fatal IO error %d (%s) on X server %s.\n",
257 errno, g_strerror (errno),
258 display ? DisplayString (display) : gdk_get_display_arg_name ());
264 /* X error handler. Keep the name the same because people are used to
265 * breaking on it in the debugger.
268 gdk_x_error (Display *xdisplay,
271 if (error->error_code)
273 GdkDisplay *error_display;
274 GdkDisplayManager *manager;
277 /* Figure out which GdkDisplay if any got the error. */
278 error_display = NULL;
279 manager = gdk_display_manager_get ();
280 displays = gdk_display_manager_list_displays (manager);
281 while (displays != NULL)
283 GdkX11Display *gdk_display = displays->data;
285 if (xdisplay == gdk_display->xdisplay)
287 error_display = GDK_DISPLAY (gdk_display);
288 g_slist_free (displays);
293 displays = g_slist_delete_link (displays, displays);
297 if (error_display == NULL)
299 /* Error on an X display not opened by GDK. Ignore. */
305 _gdk_x11_display_error_event (error_display, error);
313 _gdk_x11_error_handler_push (void)
315 GdkXErrorHandler previous;
317 previous = XSetErrorHandler (gdk_x_error);
319 if (_gdk_error_handler_push_count > 0)
321 if (previous != gdk_x_error)
322 g_warning ("XSetErrorHandler() called with a GDK error trap pushed. Don't do that.");
326 _gdk_old_error_handler = previous;
329 _gdk_error_handler_push_count += 1;
333 _gdk_x11_error_handler_pop (void)
335 g_return_if_fail (_gdk_error_handler_push_count > 0);
337 _gdk_error_handler_push_count -= 1;
339 if (_gdk_error_handler_push_count == 0)
341 XSetErrorHandler (_gdk_old_error_handler);
342 _gdk_old_error_handler = NULL;
347 _gdk_x11_display_send_xevent (GdkDisplay *display,
355 if (gdk_display_is_closed (display))
358 gdk_x11_display_error_trap_push (display);
359 result = XSendEvent (GDK_DISPLAY_XDISPLAY (display), window,
360 propagate, event_mask, event_send);
361 XSync (GDK_DISPLAY_XDISPLAY (display), False);
363 if (gdk_x11_display_error_trap_pop (display))
370 _gdk_x11_region_get_xrectangles (const cairo_region_t *region,
376 XRectangle *rectangles;
377 cairo_rectangle_int_t box;
380 n = cairo_region_num_rectangles (region);
381 rectangles = g_new (XRectangle, n);
383 for (i = 0; i < n; i++)
385 cairo_region_get_rectangle (region, i, &box);
386 rectangles[i].x = CLAMP (box.x + x_offset, G_MINSHORT, G_MAXSHORT);
387 rectangles[i].y = CLAMP (box.y + y_offset, G_MINSHORT, G_MAXSHORT);
388 rectangles[i].width = CLAMP (box.width, G_MINSHORT, G_MAXSHORT);
389 rectangles[i].height = CLAMP (box.height, G_MINSHORT, G_MAXSHORT);
397 * gdk_x11_grab_server:
399 * Call gdk_x11_display_grab() on the default display.
400 * To ungrab the server again, use gdk_x11_ungrab_server().
402 * gdk_x11_grab_server()/gdk_x11_ungrab_server() calls can be nested.
405 gdk_x11_grab_server (void)
407 gdk_x11_display_grab (gdk_display_get_default ());
411 * gdk_x11_ungrab_server:
413 * Ungrab the default display after it has been grabbed with
414 * gdk_x11_grab_server().
417 gdk_x11_ungrab_server (void)
419 gdk_x11_display_ungrab (gdk_display_get_default ());
423 * gdk_x11_get_default_screen:
425 * Gets the default GTK+ screen number.
427 * Return value: returns the screen number specified by
428 * the --display command line option or the DISPLAY environment
429 * variable when gdk_init() calls XOpenDisplay().
432 gdk_x11_get_default_screen (void)
434 return gdk_screen_get_number (gdk_screen_get_default ());
438 * gdk_x11_get_default_root_xwindow: (skip)
440 * Gets the root window of the default screen
441 * (see gdk_x11_get_default_screen()).
443 * Return value: an Xlib <type>Window</type>.
446 gdk_x11_get_default_root_xwindow (void)
448 return GDK_SCREEN_XROOTWIN (gdk_screen_get_default ());
452 * gdk_x11_get_default_xdisplay: (skip)
454 * Gets the default GTK+ display.
456 * Return value: the Xlib <type>Display*</type> for the display
457 * specified in the <option>--display</option> command line option
458 * or the <envar>DISPLAY</envar> environment variable.
461 gdk_x11_get_default_xdisplay (void)
463 return GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());