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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 /*
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/.
25 */
26
27 #include "config.h"
28
29 #include <glib/gprintf.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <errno.h>
35
36 #include <X11/Xatom.h>
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39
40 #ifdef HAVE_XKB
41 #include <X11/XKBlib.h>
42 #endif
43
44 #include "gdk.h"
45
46 #include "gdkx.h"
47 #include "gdkasync.h"
48 #include "gdkdisplay-x11.h"
49 #include "gdkinternals.h"
50 #include "gdkintl.h"
51 #include "gdkregion-generic.h"
52 #include "gdkinputprivate.h"
53 #include "gdkalias.h"
54
55 typedef struct _GdkPredicate GdkPredicate;
56 typedef struct _GdkErrorTrap GdkErrorTrap;
57
58 struct _GdkPredicate
59 {
60 GdkEventFunc func;
61 gpointer data;
62 };
63
64 struct _GdkErrorTrap
65 {
66 int (*old_handler) (Display *, XErrorEvent *);
67 gint error_warnings;
68 gint error_code;
69 };
70
71 /*
72 * Private function declarations
73 */
74
75 #ifndef HAVE_XCONVERTCASE
76 static void gdkx_XConvertCase (KeySym symbol,
77 KeySym *lower,
78 KeySym *upper);
79 #define XConvertCase gdkx_XConvertCase
80 #endif
81
82 static int gdk_x_error (Display *display,
83 XErrorEvent *error);
84 static int gdk_x_io_error (Display *display);
85
86 /* Private variable declarations
87 */
88 static GSList *gdk_error_traps = NULL; /* List of error traps */
89 static GSList *gdk_error_trap_free_list = NULL; /* Free list */
90
91 const GOptionEntry _gdk_windowing_args[] = {
92 { "sync", 0, 0, G_OPTION_ARG_NONE, &_gdk_synchronize,
93 /* Description of --sync in --help output */ N_("Make X calls synchronous"), NULL },
94 { NULL }
95 };
96
97 void
98 _gdk_windowing_init (void)
99 {
100 _gdk_x11_initialize_locale ();
101
102 XSetErrorHandler (gdk_x_error);
103 XSetIOErrorHandler (gdk_x_io_error);
104
105 _gdk_selection_property = gdk_atom_intern_static_string ("GDK_SELECTION");
106 }
107
108 void
109 gdk_set_use_xshm (gboolean use_xshm)
110 {
111 }
112
113 gboolean
114 gdk_get_use_xshm (void)
115 {
116 return GDK_DISPLAY_X11 (gdk_display_get_default ())->use_xshm;
117 }
118
119 static GdkGrabStatus
120 gdk_x11_convert_grab_status (gint status)
121 {
122 switch (status)
123 {
124 case GrabSuccess:
125 return GDK_GRAB_SUCCESS;
126 case AlreadyGrabbed:
127 return GDK_GRAB_ALREADY_GRABBED;
128 case GrabInvalidTime:
129 return GDK_GRAB_INVALID_TIME;
130 case GrabNotViewable:
131 return GDK_GRAB_NOT_VIEWABLE;
132 case GrabFrozen:
133 return GDK_GRAB_FROZEN;
134 }
135
136 g_assert_not_reached();
137
138 return 0;
139 }
140
141 static void
142 has_pointer_grab_callback (GdkDisplay *display,
143 gpointer data,
144 gulong serial)
145 {
146 _gdk_display_pointer_grab_update (display, serial);
147 }
148
149 GdkGrabStatus
150 _gdk_windowing_pointer_grab (GdkWindow *window,
151 GdkWindow *native,
152 gboolean owner_events,
153 GdkEventMask event_mask,
154 GdkWindow *confine_to,
155 GdkCursor *cursor,
156 guint32 time)
157 {
158 gint return_val;
159 GdkCursorPrivate *cursor_private;
160 GdkDisplayX11 *display_x11;
161 guint xevent_mask;
162 Window xwindow;
163 Window xconfine_to;
164 Cursor xcursor;
165 int i;
166
167 if (confine_to)
168 confine_to = _gdk_window_get_impl_window (confine_to);
169
170 display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (native));
171
172 cursor_private = (GdkCursorPrivate*) cursor;
173
174 xwindow = GDK_WINDOW_XID (native);
175
176 if (!confine_to || GDK_WINDOW_DESTROYED (confine_to))
177 xconfine_to = None;
178 else
179 xconfine_to = GDK_WINDOW_XID (confine_to);
180
181 if (!cursor)
182 xcursor = None;
183 else
184 {
185 _gdk_x11_cursor_update_theme (cursor);
186 xcursor = cursor_private->xcursor;
187 }
188
189 xevent_mask = 0;
190 for (i = 0; i < _gdk_nenvent_masks; i++)
191 {
192 if (event_mask & (1 << (i + 1)))
193 xevent_mask |= _gdk_event_mask_table[i];
194 }
195
196 /* We don't want to set a native motion hint mask, as we're emulating motion
197 * hints. If we set a native one we just wouldn't get any events.
198 */
199 xevent_mask &= ~PointerMotionHintMask;
200
201 return_val = _gdk_input_grab_pointer (window,
202 native,
203 owner_events,
204 event_mask,
205 confine_to,
206 time);
207
208 if (return_val == GrabSuccess ||
209 G_UNLIKELY (!display_x11->trusted_client && return_val == AlreadyGrabbed))
210 {
211 if (!GDK_WINDOW_DESTROYED (native))
212 {
213 #ifdef G_ENABLE_DEBUG
214 if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
215 return_val = GrabSuccess;
216 else
217 #endif
218 return_val = XGrabPointer (GDK_WINDOW_XDISPLAY (native),
219 xwindow,
220 owner_events,
221 xevent_mask,
222 GrabModeAsync, GrabModeAsync,
223 xconfine_to,
224 xcursor,
225 time);
226 }
227 else
228 return_val = AlreadyGrabbed;
229 }
230
231 if (return_val == GrabSuccess)
232 _gdk_x11_roundtrip_async (GDK_DISPLAY_OBJECT (display_x11),
233 has_pointer_grab_callback,
234 NULL);
235
236 return gdk_x11_convert_grab_status (return_val);
237 }
238
239 /*
240 *--------------------------------------------------------------
241 * gdk_keyboard_grab
242 *
243 * Grabs the keyboard to a specific window
244 *
245 * Arguments:
246 * "window" is the window which will receive the grab
247 * "owner_events" specifies whether events will be reported as is,
248 * or relative to "window"
249 * "time" specifies the time
250 *
251 * Results:
252 *
253 * Side effects:
254 * requires a corresponding call to gdk_keyboard_ungrab
255 *
256 *--------------------------------------------------------------
257 */
258
259 GdkGrabStatus
260 gdk_keyboard_grab (GdkWindow * window,
261 gboolean owner_events,
262 guint32 time)
263 {
264 gint return_val;
265 unsigned long serial;
266 GdkDisplay *display;
267 GdkDisplayX11 *display_x11;
268 GdkWindow *native;
269
270 g_return_val_if_fail (window != NULL, 0);
271 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
272
273 native = gdk_window_get_toplevel (window);
274
275 /* TODO: What do we do for offscreens and children? We need to proxy the grab somehow */
276 if (!GDK_IS_WINDOW_IMPL_X11 (GDK_WINDOW_OBJECT (native)->impl))
277 return GDK_GRAB_SUCCESS;
278
279 display = GDK_WINDOW_DISPLAY (native);
280 display_x11 = GDK_DISPLAY_X11 (display);
281
282 serial = NextRequest (GDK_WINDOW_XDISPLAY (native));
283
284 if (!GDK_WINDOW_DESTROYED (native))
285 {
286 #ifdef G_ENABLE_DEBUG
287 if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
288 return_val = GrabSuccess;
289 else
290 #endif
291 return_val = XGrabKeyboard (GDK_WINDOW_XDISPLAY (native),
292 GDK_WINDOW_XID (native),
293 owner_events,
294 GrabModeAsync, GrabModeAsync,
295 time);
296 if (G_UNLIKELY (!display_x11->trusted_client &&
297 return_val == AlreadyGrabbed))
298 /* we can't grab the keyboard, but we can do a GTK-local grab */
299 return_val = GrabSuccess;
300 }
301 else
302 return_val = AlreadyGrabbed;
303
304 if (return_val == GrabSuccess)
305 _gdk_display_set_has_keyboard_grab (display,
306 window, native,
307 owner_events,
308 serial, time);
309
310 return gdk_x11_convert_grab_status (return_val);
311 }
312
313 /**
314 * _gdk_xgrab_check_unmap:
315 * @window: a #GdkWindow
316 * @serial: serial from Unmap event (or from NextRequest(display)
317 * if the unmap is being done by this client.)
318 *
319 * Checks to see if an unmap request or event causes the current
320 * grab window to become not viewable, and if so, clear the
321 * the pointer we keep to it.
322 **/
323 void
324 _gdk_xgrab_check_unmap (GdkWindow *window,
325 gulong serial)
326 {
327 GdkDisplay *display = gdk_drawable_get_display (window);
328
329 _gdk_display_end_pointer_grab (display, serial, window, TRUE);
330
331 if (display->keyboard_grab.window &&
332 serial >= display->keyboard_grab.serial)
333 {
334 GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
335 GdkWindowObject *tmp = GDK_WINDOW_OBJECT (display->keyboard_grab.window);
336
337 while (tmp && tmp != private)
338 tmp = tmp->parent;
339
340 if (tmp)
341 _gdk_display_unset_has_keyboard_grab (display, TRUE);
342 }
343 }
344
345 /**
346 * _gdk_xgrab_check_destroy:
347 * @window: a #GdkWindow
348 *
349 * Checks to see if window is the current grab window, and if
350 * so, clear the current grab window.
351 **/
352 void
353 _gdk_xgrab_check_destroy (GdkWindow *window)
354 {
355 GdkDisplay *display = gdk_drawable_get_display (window);
356 GdkPointerGrabInfo *grab;
357
358 /* Make sure there is no lasting grab in this native
359 window */
360 grab = _gdk_display_get_last_pointer_grab (display);
361 if (grab && grab->native_window == window)
362 {
363 /* We don't know the actual serial to end, but it
364 doesn't really matter as this only happens
365 after we get told of the destroy from the
366 server so we know its ended in the server,
367 just make sure its ended. */
368 grab->serial_end = grab->serial_start;
369 grab->implicit_ungrab = TRUE;
370 }
371
372 if (window == display->keyboard_grab.native_window &&
373 display->keyboard_grab.window != NULL)
374 _gdk_display_unset_has_keyboard_grab (display, TRUE);
375 }
376
377 void
378 _gdk_windowing_display_set_sm_client_id (GdkDisplay *display,
379 const gchar *sm_client_id)
380 {
381 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
382
383 if (display->closed)
384 return;
385
386 if (sm_client_id && strcmp (sm_client_id, ""))
387 {
388 XChangeProperty (display_x11->xdisplay, display_x11->leader_window,
389 gdk_x11_get_xatom_by_name_for_display (display, "SM_CLIENT_ID"),
390 XA_STRING, 8, PropModeReplace, (guchar *)sm_client_id,
391 strlen (sm_client_id));
392 }
393 else
394 XDeleteProperty (display_x11->xdisplay, display_x11->leader_window,
395 gdk_x11_get_xatom_by_name_for_display (display, "SM_CLIENT_ID"));
396 }
397
398 /**
399 * gdk_x11_set_sm_client_id:
400 * @sm_client_id: the client id assigned by the session manager when the
401 * connection was opened, or %NULL to remove the property.
402 *
403 * Sets the <literal>SM_CLIENT_ID</literal> property on the application's leader window so that
404 * the window manager can save the application's state using the X11R6 ICCCM
405 * session management protocol.
406 *
407 * See the X Session Management Library documentation for more information on
408 * session management and the Inter-Client Communication Conventions Manual
409 *
410 * Since: 2.24
411 */
412 void
413 gdk_x11_set_sm_client_id (const gchar *sm_client_id)
414 {
415 gdk_set_sm_client_id (sm_client_id);
416 }
417
418 /* Close all open displays
419 */
420 void
421 _gdk_windowing_exit (void)
422 {
423 GSList *tmp_list = _gdk_displays;
424
425 while (tmp_list)
426 {
427 XCloseDisplay (GDK_DISPLAY_XDISPLAY (tmp_list->data));
428
429 tmp_list = tmp_list->next;
430 }
431 }
432
433 /*
434 *--------------------------------------------------------------
435 * gdk_x_error
436 *
437 * The X error handling routine.
438 *
439 * Arguments:
440 * "display" is the X display the error orignated from.
441 * "error" is the XErrorEvent that we are handling.
442 *
443 * Results:
444 * Either we were expecting some sort of error to occur,
445 * in which case we set the "_gdk_error_code" flag, or this
446 * error was unexpected, in which case we will print an
447 * error message and exit. (Since trying to continue will
448 * most likely simply lead to more errors).
449 *
450 * Side effects:
451 *
452 *--------------------------------------------------------------
453 */
454
455 static int
456 gdk_x_error (Display *display,
457 XErrorEvent *error)
458 {
459 if (error->error_code)
460 {
461 if (_gdk_error_warnings)
462 {
463 gchar buf[64];
464 gchar *msg;
465
466 XGetErrorText (display, error->error_code, buf, 63);
467
468 msg =
469 g_strdup_printf ("The program '%s' received an X Window System error.\n"
470 "This probably reflects a bug in the program.\n"
471 "The error was '%s'.\n"
472 " (Details: serial %ld error_code %d request_code %d minor_code %d)\n"
473 " (Note to programmers: normally, X errors are reported asynchronously;\n"
474 " that is, you will receive the error a while after causing it.\n"
475 " To debug your program, run it with the --sync command line\n"
476 " option to change this behavior. You can then get a meaningful\n"
477 " backtrace from your debugger if you break on the gdk_x_error() function.)",
478 g_get_prgname (),
479 buf,
480 error->serial,
481 error->error_code,
482 error->request_code,
483 error->minor_code);
484
485 #ifdef G_ENABLE_DEBUG
486 g_error ("%s", msg);
487 #else /* !G_ENABLE_DEBUG */
488 g_fprintf (stderr, "%s\n", msg);
489
490 exit (1);
491 #endif /* G_ENABLE_DEBUG */
492 }
493 _gdk_error_code = error->error_code;
494 }
495
496 return 0;
497 }
498
499 /*
500 *--------------------------------------------------------------
501 * gdk_x_io_error
502 *
503 * The X I/O error handling routine.
504 *
505 * Arguments:
506 * "display" is the X display the error orignated from.
507 *
508 * Results:
509 * An X I/O error basically means we lost our connection
510 * to the X server. There is not much we can do to
511 * continue, so simply print an error message and exit.
512 *
513 * Side effects:
514 *
515 *--------------------------------------------------------------
516 */
517
518 static int
519 gdk_x_io_error (Display *display)
520 {
521 /* This is basically modelled after the code in XLib. We need
522 * an explicit error handler here, so we can disable our atexit()
523 * which would otherwise cause a nice segfault.
524 * We fprintf(stderr, instead of g_warning() because g_warning()
525 * could possibly be redirected to a dialog
526 */
527 if (errno == EPIPE)
528 {
529 g_fprintf (stderr,
530 "The application '%s' lost its connection to the display %s;\n"
531 "most likely the X server was shut down or you killed/destroyed\n"
532 "the application.\n",
533 g_get_prgname (),
534 display ? DisplayString (display) : gdk_get_display_arg_name ());
535 }
536 else
537 {
538 g_fprintf (stderr, "%s: Fatal IO error %d (%s) on X server %s.\n",
539 g_get_prgname (),
540 errno, g_strerror (errno),
541 display ? DisplayString (display) : gdk_get_display_arg_name ());
542 }
543
544 exit(1);
545 }
546
547 /*************************************************************
548 * gdk_error_trap_push:
549 * Push an error trap. X errors will be trapped until
550 * the corresponding gdk_error_pop(), which will return
551 * the error code, if any.
552 * arguments:
553 *
554 * results:
555 *************************************************************/
556
557 void
558 gdk_error_trap_push (void)
559 {
560 GSList *node;
561 GdkErrorTrap *trap;
562
563 if (gdk_error_trap_free_list)
564 {
565 node = gdk_error_trap_free_list;
566 gdk_error_trap_free_list = gdk_error_trap_free_list->next;
567 }
568 else
569 {
570 node = g_slist_alloc ();
571 node->data = g_new (GdkErrorTrap, 1);
572 }
573
574 node->next = gdk_error_traps;
575 gdk_error_traps = node;
576
577 trap = node->data;
578 trap->old_handler = XSetErrorHandler (gdk_x_error);
579 trap->error_code = _gdk_error_code;
580 trap->error_warnings = _gdk_error_warnings;
581
582 _gdk_error_code = 0;
583 _gdk_error_warnings = 0;
584 }
585
586 /*************************************************************
587 * gdk_error_trap_pop:
588 * Pop an error trap added with gdk_error_push()
589 * arguments:
590 *
591 * results:
592 * 0, if no error occured, otherwise the error code.
593 *************************************************************/
594
595 gint
596 gdk_error_trap_pop (void)
597 {
598 GSList *node;
599 GdkErrorTrap *trap;
600 gint result;
601
602 g_return_val_if_fail (gdk_error_traps != NULL, 0);
603
604 node = gdk_error_traps;
605 gdk_error_traps = gdk_error_traps->next;
606
607 node->next = gdk_error_trap_free_list;
608 gdk_error_trap_free_list = node;
609
610 result = _gdk_error_code;
611
612 trap = node->data;
613 _gdk_error_code = trap->error_code;
614 _gdk_error_warnings = trap->error_warnings;
615 XSetErrorHandler (trap->old_handler);
616
617 return result;
618 }
619
620 gchar *
621 gdk_get_display (void)
622 {
623 return g_strdup (gdk_display_get_name (gdk_display_get_default ()));
624 }
625
626 /**
627 * _gdk_send_xevent:
628 * @display: #GdkDisplay which @window is on
629 * @window: window ID to which to send the event
630 * @propagate: %TRUE if the event should be propagated if the target window
631 * doesn't handle it.
632 * @event_mask: event mask to match against, or 0 to send it to @window
633 * without regard to event masks.
634 * @event_send: #XEvent to send
635 *
636 * Send an event, like XSendEvent(), but trap errors and check
637 * the result.
638 *
639 * Return value: %TRUE if sending the event succeeded.
640 **/
641 gint
642 _gdk_send_xevent (GdkDisplay *display,
643 Window window,
644 gboolean propagate,
645 glong event_mask,
646 XEvent *event_send)
647 {
648 gboolean result;
649
650 if (display->closed)
651 return FALSE;
652
653 gdk_error_trap_push ();
654 result = XSendEvent (GDK_DISPLAY_XDISPLAY (display), window,
655 propagate, event_mask, event_send);
656 XSync (GDK_DISPLAY_XDISPLAY (display), False);
657
658 if (gdk_error_trap_pop ())
659 return FALSE;
660
661 return result;
662 }
663
664 void
665 _gdk_region_get_xrectangles (const GdkRegion *region,
666 gint x_offset,
667 gint y_offset,
668 XRectangle **rects,
669 gint *n_rects)
670 {
671 XRectangle *rectangles = g_new (XRectangle, region->numRects);
672 GdkRegionBox *boxes = region->rects;
673 gint i;
674
675 for (i = 0; i < region->numRects; i++)
676 {
677 rectangles[i].x = CLAMP (boxes[i].x1 + x_offset, G_MINSHORT, G_MAXSHORT);
678 rectangles[i].y = CLAMP (boxes[i].y1 + y_offset, G_MINSHORT, G_MAXSHORT);
679 rectangles[i].width = CLAMP (boxes[i].x2 + x_offset, G_MINSHORT, G_MAXSHORT) - rectangles[i].x;
680 rectangles[i].height = CLAMP (boxes[i].y2 + y_offset, G_MINSHORT, G_MAXSHORT) - rectangles[i].y;
681 }
682
683 *rects = rectangles;
684 *n_rects = region->numRects;
685 }
686
687 /**
688 * gdk_x11_grab_server:
689 *
690 * Call gdk_x11_display_grab() on the default display.
691 * To ungrab the server again, use gdk_x11_ungrab_server().
692 *
693 * gdk_x11_grab_server()/gdk_x11_ungrab_server() calls can be nested.
694 **/
695 void
696 gdk_x11_grab_server (void)
697 {
698 gdk_x11_display_grab (gdk_display_get_default ());
699 }
700
701 /**
702 * gdk_x11_ungrab_server:
703 *
704 * Ungrab the default display after it has been grabbed with
705 * gdk_x11_grab_server().
706 **/
707 void
708 gdk_x11_ungrab_server (void)
709 {
710 gdk_x11_display_ungrab (gdk_display_get_default ());
711 }
712
713 /**
714 * gdk_x11_get_default_screen:
715 *
716 * Gets the default GTK+ screen number.
717 *
718 * Return value: returns the screen number specified by
719 * the --display command line option or the DISPLAY environment
720 * variable when gdk_init() calls XOpenDisplay().
721 **/
722 gint
723 gdk_x11_get_default_screen (void)
724 {
725 return gdk_screen_get_number (gdk_screen_get_default ());
726 }
727
728 /**
729 * gdk_x11_get_default_root_xwindow:
730 *
731 * Gets the root window of the default screen
732 * (see gdk_x11_get_default_screen()).
733 *
734 * Return value: an Xlib <type>Window</type>.
735 **/
736 Window
737 gdk_x11_get_default_root_xwindow (void)
738 {
739 return GDK_SCREEN_XROOTWIN (gdk_screen_get_default ());
740 }
741
742 /**
743 * gdk_x11_get_default_xdisplay:
744 *
745 * Gets the default GTK+ display.
746 *
747 * Return value: the Xlib <type>Display*</type> for the display
748 * specified in the <option>--display</option> command line option
749 * or the <envar>DISPLAY</envar> environment variable.
750 **/
751 Display *
752 gdk_x11_get_default_xdisplay (void)
753 {
754 return GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
755 }
756
757 #define __GDK_MAIN_X11_C__
758 #include "gdkaliasdef.c"