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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
36 #ifdef HAVE_SYS_SELECT_H
37 #include <sys/select.h>
38 #endif /* HAVE_SYS_SELECT_H_ */
40 #define XLIB_ILLEGAL_ACCESS
41 #include <X11/Xatom.h>
44 #include <X11/Xutil.h>
45 #include <X11/Xmu/WinUtil.h>
46 #include <X11/cursorfont.h>
51 #include "gdkprivate.h"
52 #include "gdkinputprivate.h"
54 typedef struct _GdkPredicate GdkPredicate;
55 typedef struct _GdkErrorTrap GdkErrorTrap;
70 * Private function declarations
73 #ifndef HAVE_XCONVERTCASE
74 static void gdkx_XConvertCase (KeySym symbol,
77 #define XConvertCase gdkx_XConvertCase
80 static int gdk_x_error (Display *display,
82 static int gdk_x_io_error (Display *display);
84 /* Private variable declarations
86 static int gdk_initialized = 0; /* 1 if the library is initialized,
90 static gint autorepeat;
91 static gboolean gdk_synchronize = FALSE;
94 static const GDebugKey gdk_debug_keys[] = {
95 {"events", GDK_DEBUG_EVENTS},
96 {"misc", GDK_DEBUG_MISC},
97 {"dnd", GDK_DEBUG_DND},
98 {"color-context", GDK_DEBUG_COLOR_CONTEXT},
99 {"xim", GDK_DEBUG_XIM}
102 static const int gdk_ndebug_keys = sizeof(gdk_debug_keys)/sizeof(GDebugKey);
104 #endif /* G_ENABLE_DEBUG */
107 gdk_arg_xim_preedit_cb (const gchar *arg, const gchar *value, gpointer cb_data)
109 if (strcmp ("none", value) == 0)
110 gdk_im_set_best_style (GDK_IM_PREEDIT_NONE);
111 else if (strcmp ("nothing", value) == 0)
112 gdk_im_set_best_style (GDK_IM_PREEDIT_NOTHING);
113 else if (strcmp ("area", value) == 0)
114 gdk_im_set_best_style (GDK_IM_PREEDIT_AREA);
115 else if (strcmp ("position", value) == 0)
116 gdk_im_set_best_style (GDK_IM_PREEDIT_POSITION);
117 else if (strcmp ("callbacks", value) == 0)
118 gdk_im_set_best_style (GDK_IM_PREEDIT_CALLBACKS);
122 gdk_arg_xim_status_cb (const gchar *arg, const gchar *value, gpointer cb_data)
124 if (strcmp ("none", value) == 0)
125 gdk_im_set_best_style (GDK_IM_STATUS_NONE);
126 else if (strcmp ("nothing", value) == 0)
127 gdk_im_set_best_style (GDK_IM_STATUS_NOTHING);
128 else if (strcmp ("area", value) == 0)
129 gdk_im_set_best_style (GDK_IM_STATUS_AREA);
130 else if (strcmp ("callbacks", value) == 0)
131 gdk_im_set_best_style (GDK_IM_STATUS_CALLBACKS);
134 GdkArgDesc _gdk_windowing_args[] = {
135 { "display", GDK_ARG_STRING, &gdk_display_name, (GdkArgFunc)NULL },
136 { "sync", GDK_ARG_BOOL, &gdk_synchronize, (GdkArgFunc)NULL },
137 { "no-xshm", GDK_ARG_NOBOOL, &gdk_use_xshm, (GdkArgFunc)NULL },
138 { "class", GDK_ARG_STRING, &gdk_progclass, (GdkArgFunc)NULL },
139 { "gxid-host", GDK_ARG_STRING, &gdk_input_gxid_host, (GdkArgFunc)NULL },
140 { "gxid-port", GDK_ARG_INT, &gdk_input_gxid_port, (GdkArgFunc)NULL },
141 { "xim-preedit", GDK_ARG_CALLBACK, NULL, gdk_arg_xim_preedit_cb },
142 { "xim-status", GDK_ARG_CALLBACK, NULL, gdk_arg_xim_status_cb },
147 _gdk_windowing_init_check (int argc, char **argv)
149 XKeyboardState keyboard_state;
150 XClassHint *class_hint;
152 XSetErrorHandler (gdk_x_error);
153 XSetIOErrorHandler (gdk_x_io_error);
155 gdk_display = XOpenDisplay (gdk_display_name);
160 XSynchronize (gdk_display, True);
162 gdk_screen = DefaultScreen (gdk_display);
163 gdk_root_window = RootWindow (gdk_display, gdk_screen);
165 gdk_leader_window = XCreateSimpleWindow(gdk_display, gdk_root_window,
166 10, 10, 10, 10, 0, 0 , 0);
167 class_hint = XAllocClassHint();
168 class_hint->res_name = g_get_prgname ();
169 if (gdk_progclass == NULL)
171 gdk_progclass = g_strdup (g_get_prgname ());
172 gdk_progclass[0] = toupper (gdk_progclass[0]);
174 class_hint->res_class = gdk_progclass;
175 XmbSetWMProperties (gdk_display, gdk_leader_window,
176 NULL, NULL, argv, argc,
177 NULL, NULL, class_hint);
180 gdk_wm_delete_window = XInternAtom (gdk_display, "WM_DELETE_WINDOW", False);
181 gdk_wm_take_focus = XInternAtom (gdk_display, "WM_TAKE_FOCUS", False);
182 gdk_wm_protocols = XInternAtom (gdk_display, "WM_PROTOCOLS", False);
183 gdk_wm_window_protocols[0] = gdk_wm_delete_window;
184 gdk_wm_window_protocols[1] = gdk_wm_take_focus;
185 gdk_selection_property = XInternAtom (gdk_display, "GDK_SELECTION", False);
187 XGetKeyboardControl (gdk_display, &keyboard_state);
188 autorepeat = keyboard_state.global_auto_repeat;
194 gdk_set_use_xshm (gint use_xshm)
196 gdk_use_xshm = use_xshm;
200 gdk_get_use_xshm (void)
206 *--------------------------------------------------------------
209 * Grabs the pointer to a specific window
212 * "window" is the window which will receive the grab
213 * "owner_events" specifies whether events will be reported as is,
214 * or relative to "window"
215 * "event_mask" masks only interesting events
216 * "confine_to" limits the cursor movement to the specified window
217 * "cursor" changes the cursor for the duration of the grab
218 * "time" specifies the time
223 * requires a corresponding call to gdk_pointer_ungrab
225 *--------------------------------------------------------------
229 gdk_pointer_grab (GdkWindow * window,
231 GdkEventMask event_mask,
232 GdkWindow * confine_to,
237 GdkCursorPrivate *cursor_private;
244 g_return_val_if_fail (window != NULL, 0);
245 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
246 g_return_val_if_fail (confine_to == NULL || GDK_IS_WINDOW (confine_to), 0);
248 cursor_private = (GdkCursorPrivate*) cursor;
250 xwindow = GDK_DRAWABLE_XID (window);
252 if (!confine_to || GDK_DRAWABLE_DESTROYED (confine_to))
255 xconfine_to = GDK_DRAWABLE_XID (confine_to);
260 xcursor = cursor_private->xcursor;
264 for (i = 0; i < gdk_nevent_masks; i++)
266 if (event_mask & (1 << (i + 1)))
267 xevent_mask |= gdk_event_mask_table[i];
270 if (gdk_input_vtable.grab_pointer)
271 return_val = gdk_input_vtable.grab_pointer (window,
277 return_val = Success;
279 if (return_val == Success)
281 if (!GDK_DRAWABLE_DESTROYED (window))
282 return_val = XGrabPointer (GDK_DRAWABLE_XDISPLAY (window),
286 GrabModeAsync, GrabModeAsync,
291 return_val = AlreadyGrabbed;
294 if (return_val == GrabSuccess)
295 gdk_xgrab_window = (GdkWindowPrivate *)window;
301 *--------------------------------------------------------------
304 * Releases any pointer grab
312 *--------------------------------------------------------------
316 gdk_pointer_ungrab (guint32 time)
318 if (gdk_input_vtable.ungrab_pointer)
319 gdk_input_vtable.ungrab_pointer (time);
321 XUngrabPointer (gdk_display, time);
322 gdk_xgrab_window = NULL;
326 *--------------------------------------------------------------
327 * gdk_pointer_is_grabbed
329 * Tell wether there is an active x pointer grab in effect
337 *--------------------------------------------------------------
341 gdk_pointer_is_grabbed (void)
343 return gdk_xgrab_window != NULL;
347 *--------------------------------------------------------------
350 * Grabs the keyboard to a specific window
353 * "window" is the window which will receive the grab
354 * "owner_events" specifies whether events will be reported as is,
355 * or relative to "window"
356 * "time" specifies the time
361 * requires a corresponding call to gdk_keyboard_ungrab
363 *--------------------------------------------------------------
367 gdk_keyboard_grab (GdkWindow * window,
371 g_return_val_if_fail (window != NULL, 0);
372 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
374 if (!GDK_DRAWABLE_DESTROYED (window))
375 return XGrabKeyboard (GDK_DRAWABLE_XDISPLAY (window),
376 GDK_DRAWABLE_XID (window),
378 GrabModeAsync, GrabModeAsync,
381 return AlreadyGrabbed;
385 *--------------------------------------------------------------
386 * gdk_keyboard_ungrab
388 * Releases any keyboard grab
396 *--------------------------------------------------------------
400 gdk_keyboard_ungrab (guint32 time)
402 XUngrabKeyboard (gdk_display, time);
406 *--------------------------------------------------------------
409 * Return the width of the screen.
417 *--------------------------------------------------------------
421 gdk_screen_width (void)
425 return_val = DisplayWidth (gdk_display, gdk_screen);
431 *--------------------------------------------------------------
434 * Return the height of the screen.
442 *--------------------------------------------------------------
446 gdk_screen_height (void)
450 return_val = DisplayHeight (gdk_display, gdk_screen);
456 *--------------------------------------------------------------
457 * gdk_screen_width_mm
459 * Return the width of the screen in millimeters.
467 *--------------------------------------------------------------
471 gdk_screen_width_mm (void)
475 return_val = DisplayWidthMM (gdk_display, gdk_screen);
481 *--------------------------------------------------------------
484 * Return the height of the screen in millimeters.
492 *--------------------------------------------------------------
496 gdk_screen_height_mm (void)
500 return_val = DisplayHeightMM (gdk_display, gdk_screen);
506 *--------------------------------------------------------------
507 * gdk_set_sm_client_id
509 * Set the SM_CLIENT_ID property on the WM_CLIENT_LEADER window
510 * so that the window manager can save our state using the
511 * X11R6 ICCCM session management protocol. A NULL value should
512 * be set following disconnection from the session manager to
513 * remove the SM_CLIENT_ID property.
517 * "sm_client_id" specifies the client id assigned to us by the
518 * session manager or NULL to remove the property.
524 *--------------------------------------------------------------
528 gdk_set_sm_client_id (const gchar* sm_client_id)
530 if (sm_client_id && strcmp (sm_client_id, ""))
532 XChangeProperty (gdk_display, gdk_leader_window,
533 gdk_atom_intern ("SM_CLIENT_ID", FALSE),
534 XA_STRING, 8, PropModeReplace,
535 sm_client_id, strlen(sm_client_id));
538 XDeleteProperty (gdk_display, gdk_leader_window,
539 gdk_atom_intern ("SM_CLIENT_ID", FALSE));
543 gdk_key_repeat_disable (void)
545 XAutoRepeatOff (gdk_display);
549 gdk_key_repeat_restore (void)
552 XAutoRepeatOn (gdk_display);
554 XAutoRepeatOff (gdk_display);
561 XBell(gdk_display, 0);
565 gdk_windowing_exit (void)
567 XCloseDisplay (gdk_display);
571 *--------------------------------------------------------------
574 * The X error handling routine.
577 * "display" is the X display the error orignated from.
578 * "error" is the XErrorEvent that we are handling.
581 * Either we were expecting some sort of error to occur,
582 * in which case we set the "gdk_error_code" flag, or this
583 * error was unexpected, in which case we will print an
584 * error message and exit. (Since trying to continue will
585 * most likely simply lead to more errors).
589 *--------------------------------------------------------------
593 gdk_x_error (Display *display,
596 if (error->error_code)
598 if (gdk_error_warnings)
602 XGetErrorText (display, error->error_code, buf, 63);
604 #ifdef G_ENABLE_DEBUG
605 g_error ("%s\n serial %ld error_code %d request_code %d minor_code %d\n",
611 #else /* !G_ENABLE_DEBUG */
612 fprintf (stderr, "Gdk-ERROR **: %s\n serial %ld error_code %d request_code %d minor_code %d\n",
620 #endif /* G_ENABLE_DEBUG */
622 gdk_error_code = error->error_code;
629 *--------------------------------------------------------------
632 * The X I/O error handling routine.
635 * "display" is the X display the error orignated from.
638 * An X I/O error basically means we lost our connection
639 * to the X server. There is not much we can do to
640 * continue, so simply print an error message and exit.
644 *--------------------------------------------------------------
648 gdk_x_io_error (Display *display)
650 /* This is basically modelled after the code in XLib. We need
651 * an explicit error handler here, so we can disable our atexit()
652 * which would otherwise cause a nice segfault.
653 * We fprintf(stderr, instead of g_warning() because g_warning()
654 * could possibly be redirected to a dialog
658 fprintf (stderr, "Gdk-ERROR **: X connection to %s broken (explicit kill or server shutdown).\n", gdk_display ? DisplayString (gdk_display) : gdk_get_display());
662 fprintf (stderr, "Gdk-ERROR **: Fatal IO error %d (%s) on X server %s.\n",
663 errno, g_strerror (errno),
664 gdk_display ? DisplayString (gdk_display) : gdk_get_display());
667 /* Disable the atexit shutdown for GDK */
674 gdk_get_display (void)
676 return (gchar *)XDisplayName (gdk_display_name);
680 gdk_send_xevent (Window window, gboolean propagate, glong event_mask,
684 gint old_warnings = gdk_error_warnings;
688 gdk_error_warnings = 0;
689 result = XSendEvent (gdk_display, window, propagate, event_mask, event_send);
690 XSync (gdk_display, False);
691 gdk_error_warnings = old_warnings;
693 return result && !gdk_error_code;
697 gdk_keyval_name (guint keyval)
699 return XKeysymToString (keyval);
703 gdk_keyval_from_name (const gchar *keyval_name)
705 g_return_val_if_fail (keyval_name != NULL, 0);
707 return XStringToKeysym (keyval_name);
710 #ifdef HAVE_XCONVERTCASE
712 gdk_keyval_convert_case (guint symbol,
720 XConvertCase (symbol, &xlower, &xupper);
727 #endif HAVE_XCONVERTCASE