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/.
31 #include "gdkinternals.h"
34 #ifndef HAVE_XCONVERTCASE
35 #include "gdkkeysyms.h"
44 * @Short_description: Library initialization and miscellaneous functions
47 * This section describes the GDK initialization functions and miscellaneous
52 typedef struct _GdkPredicate GdkPredicate;
60 typedef struct _GdkThreadsDispatch GdkThreadsDispatch;
62 struct _GdkThreadsDispatch
66 GDestroyNotify destroy;
70 /* Private variable declarations
72 static int gdk_initialized = 0; /* 1 if the library is initialized,
76 static gchar *gdk_progclass = NULL;
78 static GMutex *gdk_threads_mutex = NULL; /* Global GDK lock */
80 static GCallback gdk_threads_lock = NULL;
81 static GCallback gdk_threads_unlock = NULL;
84 static const GDebugKey gdk_debug_keys[] = {
85 {"events", GDK_DEBUG_EVENTS},
86 {"misc", GDK_DEBUG_MISC},
87 {"dnd", GDK_DEBUG_DND},
88 {"xim", GDK_DEBUG_XIM},
89 {"nograbs", GDK_DEBUG_NOGRABS},
90 {"colormap", GDK_DEBUG_COLORMAP},
91 {"input", GDK_DEBUG_INPUT},
92 {"cursor", GDK_DEBUG_CURSOR},
93 {"multihead", GDK_DEBUG_MULTIHEAD},
94 {"xinerama", GDK_DEBUG_XINERAMA},
95 {"draw", GDK_DEBUG_DRAW},
96 {"eventloop", GDK_DEBUG_EVENTLOOP}
99 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
101 #endif /* G_ENABLE_DEBUG */
103 #ifdef G_ENABLE_DEBUG
105 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data, GError **error)
107 guint debug_value = g_parse_debug_string (value,
108 (GDebugKey *) gdk_debug_keys,
111 if (debug_value == 0 && value != NULL && strcmp (value, "") != 0)
114 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
115 _("Error parsing option --gdk-debug"));
119 _gdk_debug_flags |= debug_value;
125 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data, GError **error)
127 guint debug_value = g_parse_debug_string (value,
128 (GDebugKey *) gdk_debug_keys,
131 if (debug_value == 0 && value != NULL && strcmp (value, "") != 0)
134 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
135 _("Error parsing option --gdk-no-debug"));
139 _gdk_debug_flags &= ~debug_value;
143 #endif /* G_ENABLE_DEBUG */
146 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
148 gdk_set_program_class (value);
154 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
156 g_set_prgname (value);
161 static const GOptionEntry gdk_args[] = {
162 { "class", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
163 /* Description of --class=CLASS in --help output */ N_("Program class as used by the window manager"),
164 /* Placeholder in --class=CLASS in --help output */ N_("CLASS") },
165 { "name", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
166 /* Description of --name=NAME in --help output */ N_("Program name as used by the window manager"),
167 /* Placeholder in --name=NAME in --help output */ N_("NAME") },
168 { "display", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &_gdk_display_name,
169 /* Description of --display=DISPLAY in --help output */ N_("X display to use"),
170 /* Placeholder in --display=DISPLAY in --help output */ N_("DISPLAY") },
171 { "screen", 0, 0, G_OPTION_ARG_INT, &_gdk_screen_number,
172 /* Description of --screen=SCREEN in --help output */ N_("X screen to use"),
173 /* Placeholder in --screen=SCREEN in --help output */ N_("SCREEN") },
174 #ifdef G_ENABLE_DEBUG
175 { "gdk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,
176 /* Description of --gdk-debug=FLAGS in --help output */ N_("GDK debugging flags to set"),
177 /* Placeholder in --gdk-debug=FLAGS in --help output */ N_("FLAGS") },
178 { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb,
179 /* Description of --gdk-no-debug=FLAGS in --help output */ N_("GDK debugging flags to unset"),
180 /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
186 * gdk_add_option_entries_libgtk_only:
187 * @group: An option group.
189 * Appends gdk option entries to the passed in option group. This is
190 * not public API and must not be used by applications.
193 gdk_add_option_entries_libgtk_only (GOptionGroup *group)
195 g_option_group_add_entries (group, gdk_args);
196 g_option_group_add_entries (group, _gdk_windowing_args);
200 gdk_pre_parse_libgtk_only (void)
202 gdk_initialized = TRUE;
204 /* We set the fallback program class here, rather than lazily in
205 * gdk_get_program_class, since we don't want -name to override it.
207 gdk_progclass = g_strdup (g_get_prgname ());
208 if (gdk_progclass && gdk_progclass[0])
209 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
211 #ifdef G_ENABLE_DEBUG
213 gchar *debug_string = getenv("GDK_DEBUG");
214 if (debug_string != NULL)
215 _gdk_debug_flags = g_parse_debug_string (debug_string,
216 (GDebugKey *) gdk_debug_keys,
219 #endif /* G_ENABLE_DEBUG */
221 if (getenv ("GDK_NATIVE_WINDOWS"))
223 _gdk_native_windows = TRUE;
224 /* Ensure that this is not propagated
225 to spawned applications */
226 g_unsetenv ("GDK_NATIVE_WINDOWS");
231 /* Do any setup particular to the windowing system
233 _gdk_windowing_init ();
239 * @argc: the number of command line arguments.
240 * @argv: the array of command line arguments.
242 * Parse command line arguments, and store for future
243 * use by calls to gdk_display_open().
245 * Any arguments used by GDK are removed from the array and @argc and @argv are
246 * updated accordingly.
248 * You shouldn't call this function explicitely if you are using
249 * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
254 gdk_parse_args (int *argc,
257 GOptionContext *option_context;
258 GOptionGroup *option_group;
259 GError *error = NULL;
264 gdk_pre_parse_libgtk_only ();
266 option_context = g_option_context_new (NULL);
267 g_option_context_set_ignore_unknown_options (option_context, TRUE);
268 g_option_context_set_help_enabled (option_context, FALSE);
269 option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
270 g_option_context_set_main_group (option_context, option_group);
272 g_option_group_add_entries (option_group, gdk_args);
273 g_option_group_add_entries (option_group, _gdk_windowing_args);
275 if (!g_option_context_parse (option_context, argc, argv, &error))
277 g_warning ("%s", error->message);
278 g_error_free (error);
280 g_option_context_free (option_context);
282 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
286 * gdk_get_display_arg_name:
288 * Gets the display name specified in the command line arguments passed
289 * to gdk_init() or gdk_parse_args(), if any.
291 * Returns: the display name, if specified explicitely, otherwise %NULL
292 * this string is owned by GTK+ and must not be modified or freed.
296 G_CONST_RETURN gchar *
297 gdk_get_display_arg_name (void)
299 if (!_gdk_display_arg_name)
301 if (_gdk_screen_number >= 0)
302 _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
304 _gdk_display_arg_name = g_strdup (_gdk_display_name);
307 return _gdk_display_arg_name;
311 * gdk_display_open_default_libgtk_only:
313 * Opens the default display specified by command line arguments or
314 * environment variables, sets it as the default display, and returns
315 * it. gdk_parse_args must have been called first. If the default
316 * display has previously been set, simply returns that. An internal
317 * function that should not be used by applications.
319 * Return value: the default display, if it could be opened,
323 gdk_display_open_default_libgtk_only (void)
327 g_return_val_if_fail (gdk_initialized, NULL);
329 display = gdk_display_get_default ();
333 display = gdk_display_open (gdk_get_display_arg_name ());
335 if (!display && _gdk_screen_number >= 0)
337 g_free (_gdk_display_arg_name);
338 _gdk_display_arg_name = g_strdup (_gdk_display_name);
340 display = gdk_display_open (_gdk_display_name);
344 gdk_display_manager_set_default_display (gdk_display_manager_get (),
352 * @argc: (inout): the number of command line arguments.
353 * @argv: (array length=argc) (inout): the array of command line arguments.
355 * Initializes the GDK library and connects to the X server, returning %TRUE on
358 * Any arguments used by GDK are removed from the array and @argc and @argv are
359 * updated accordingly.
361 * GTK+ initializes GDK in gtk_init() and so this function is not usually needed
362 * by GTK+ applications.
364 * Returns: %TRUE if initialization succeeded.
367 gdk_init_check (int *argc,
370 gdk_parse_args (argc, argv);
372 return gdk_display_open_default_libgtk_only () != NULL;
378 * @argc: (inout): the number of command line arguments.
379 * @argv: (array length=argc) (inout): the array of command line arguments.
381 * Initializes the GDK library and connects to the X server.
382 * If initialization fails, a warning message is output and the application
383 * terminates with a call to <literal>exit(1)</literal>.
385 * Any arguments used by GDK are removed from the array and @argc and @argv are
386 * updated accordingly.
388 * GTK+ initializes GDK in gtk_init() and so this function is not usually needed
389 * by GTK+ applications.
392 gdk_init (int *argc, char ***argv)
394 if (!gdk_init_check (argc, argv))
396 const char *display_name = gdk_get_display_arg_name ();
397 g_warning ("cannot open display: %s", display_name ? display_name : "");
403 gdk_threads_enter (void)
405 if (gdk_threads_lock)
406 (*gdk_threads_lock) ();
410 gdk_threads_leave (void)
412 if (gdk_threads_unlock)
413 (*gdk_threads_unlock) ();
417 gdk_threads_impl_lock (void)
419 if (gdk_threads_mutex)
420 g_mutex_lock (gdk_threads_mutex);
424 gdk_threads_impl_unlock (void)
426 if (gdk_threads_mutex)
427 g_mutex_unlock (gdk_threads_mutex);
433 * Initializes GDK so that it can be used from multiple threads
434 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
435 * g_thread_init() must be called previous to this function.
437 * This call must be made before any use of the main loop from
438 * GTK+; to be safe, call it before gtk_init().
441 gdk_threads_init (void)
443 if (!g_thread_supported ())
444 g_error ("g_thread_init() must be called before gdk_threads_init()");
446 gdk_threads_mutex = g_mutex_new ();
447 if (!gdk_threads_lock)
448 gdk_threads_lock = gdk_threads_impl_lock;
449 if (!gdk_threads_unlock)
450 gdk_threads_unlock = gdk_threads_impl_unlock;
454 * gdk_threads_set_lock_functions:
455 * @enter_fn: function called to guard GDK
456 * @leave_fn: function called to release the guard
458 * Allows the application to replace the standard method that
459 * GDK uses to protect its data structures. Normally, GDK
460 * creates a single #GMutex that is locked by gdk_threads_enter(),
461 * and released by gdk_threads_leave(); using this function an
462 * application provides, instead, a function @enter_fn that is
463 * called by gdk_threads_enter() and a function @leave_fn that is
464 * called by gdk_threads_leave().
466 * The functions must provide at least same locking functionality
467 * as the default implementation, but can also do extra application
468 * specific processing.
470 * As an example, consider an application that has its own recursive
471 * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
472 * the GTK+ lock when entering a recursive main loop, the application
473 * must temporarily release its lock as well.
475 * Most threaded GTK+ apps won't need to use this method.
477 * This method must be called before gdk_threads_init(), and cannot
478 * be called multiple times.
483 gdk_threads_set_lock_functions (GCallback enter_fn,
486 g_return_if_fail (gdk_threads_lock == NULL &&
487 gdk_threads_unlock == NULL);
489 gdk_threads_lock = enter_fn;
490 gdk_threads_unlock = leave_fn;
494 gdk_threads_dispatch (gpointer data)
496 GdkThreadsDispatch *dispatch = data;
497 gboolean ret = FALSE;
499 GDK_THREADS_ENTER ();
501 if (!g_source_is_destroyed (g_main_current_source ()))
502 ret = dispatch->func (dispatch->data);
504 GDK_THREADS_LEAVE ();
510 gdk_threads_dispatch_free (gpointer data)
512 GdkThreadsDispatch *dispatch = data;
514 if (dispatch->destroy && dispatch->data)
515 dispatch->destroy (dispatch->data);
517 g_slice_free (GdkThreadsDispatch, data);
522 * gdk_threads_add_idle_full:
523 * @priority: the priority of the idle source. Typically this will be in the
524 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
525 * @function: function to call
526 * @data: data to pass to @function
527 * @notify: (allow-none): function to call when the idle is removed, or %NULL
529 * Adds a function to be called whenever there are no higher priority
530 * events pending. If the function returns %FALSE it is automatically
531 * removed from the list of event sources and will not be called again.
533 * This variant of g_idle_add_full() calls @function with the GDK lock
534 * held. It can be thought of a MT-safe version for GTK+ widgets for the
535 * following use case, where you have to worry about idle_callback()
536 * running in thread A and accessing @self after it has been finalized
541 * idle_callback (gpointer data)
543 * /* gdk_threads_enter(); would be needed for g_idle_add() */
545 * SomeWidget *self = data;
546 * /* do stuff with self */
550 * /* gdk_threads_leave(); would be needed for g_idle_add() */
555 * some_widget_do_stuff_later (SomeWidget *self)
557 * self->idle_id = gdk_threads_add_idle (idle_callback, self)
558 * /* using g_idle_add() here would require thread protection in the callback */
562 * some_widget_finalize (GObject *object)
564 * SomeWidget *self = SOME_WIDGET (object);
566 * g_source_remove (self->idle_id);
567 * G_OBJECT_CLASS (parent_class)->finalize (object);
571 * Return value: the ID (greater than 0) of the event source.
576 gdk_threads_add_idle_full (gint priority,
577 GSourceFunc function,
579 GDestroyNotify notify)
581 GdkThreadsDispatch *dispatch;
583 g_return_val_if_fail (function != NULL, 0);
585 dispatch = g_slice_new (GdkThreadsDispatch);
586 dispatch->func = function;
587 dispatch->data = data;
588 dispatch->destroy = notify;
590 return g_idle_add_full (priority,
591 gdk_threads_dispatch,
593 gdk_threads_dispatch_free);
597 * gdk_threads_add_idle:
598 * @function: function to call
599 * @data: data to pass to @function
601 * A wrapper for the common usage of gdk_threads_add_idle_full()
602 * assigning the default priority, #G_PRIORITY_DEFAULT_IDLE.
604 * See gdk_threads_add_idle_full().
606 * Return value: the ID (greater than 0) of the event source.
611 gdk_threads_add_idle (GSourceFunc function,
614 return gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
615 function, data, NULL);
620 * gdk_threads_add_timeout_full:
621 * @priority: the priority of the timeout source. Typically this will be in the
622 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
623 * @interval: the time between calls to the function, in milliseconds
624 * (1/1000ths of a second)
625 * @function: function to call
626 * @data: data to pass to @function
627 * @notify: (allow-none): function to call when the timeout is removed, or %NULL
629 * Sets a function to be called at regular intervals holding the GDK lock,
630 * with the given priority. The function is called repeatedly until it
631 * returns %FALSE, at which point the timeout is automatically destroyed
632 * and the function will not be called again. The @notify function is
633 * called when the timeout is destroyed. The first call to the
634 * function will be at the end of the first @interval.
636 * Note that timeout functions may be delayed, due to the processing of other
637 * event sources. Thus they should not be relied on for precise timing.
638 * After each call to the timeout function, the time of the next
639 * timeout is recalculated based on the current time and the given interval
640 * (it does not try to 'catch up' time lost in delays).
642 * This variant of g_timeout_add_full() can be thought of a MT-safe version
643 * for GTK+ widgets for the following use case:
646 * static gboolean timeout_callback (gpointer data)
648 * SomeWidget *self = data;
650 * /* do stuff with self */
652 * self->timeout_id = 0;
657 * static void some_widget_do_stuff_later (SomeWidget *self)
659 * self->timeout_id = g_timeout_add (timeout_callback, self)
662 * static void some_widget_finalize (GObject *object)
664 * SomeWidget *self = SOME_WIDGET (object);
666 * if (self->timeout_id)
667 * g_source_remove (self->timeout_id);
669 * G_OBJECT_CLASS (parent_class)->finalize (object);
673 * Return value: the ID (greater than 0) of the event source.
678 gdk_threads_add_timeout_full (gint priority,
680 GSourceFunc function,
682 GDestroyNotify notify)
684 GdkThreadsDispatch *dispatch;
686 g_return_val_if_fail (function != NULL, 0);
688 dispatch = g_slice_new (GdkThreadsDispatch);
689 dispatch->func = function;
690 dispatch->data = data;
691 dispatch->destroy = notify;
693 return g_timeout_add_full (priority,
695 gdk_threads_dispatch,
697 gdk_threads_dispatch_free);
701 * gdk_threads_add_timeout:
702 * @interval: the time between calls to the function, in milliseconds
703 * (1/1000ths of a second)
704 * @function: function to call
705 * @data: data to pass to @function
707 * A wrapper for the common usage of gdk_threads_add_timeout_full()
708 * assigning the default priority, #G_PRIORITY_DEFAULT.
710 * See gdk_threads_add_timeout_full().
712 * Return value: the ID (greater than 0) of the event source.
717 gdk_threads_add_timeout (guint interval,
718 GSourceFunc function,
721 return gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
722 interval, function, data, NULL);
727 * gdk_threads_add_timeout_seconds_full:
728 * @priority: the priority of the timeout source. Typically this will be in the
729 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
730 * @interval: the time between calls to the function, in seconds
731 * @function: function to call
732 * @data: data to pass to @function
733 * @notify: (allow-none): function to call when the timeout is removed, or %NULL
735 * A variant of gdk_threads_add_timout_full() with second-granularity.
736 * See g_timeout_add_seconds_full() for a discussion of why it is
737 * a good idea to use this function if you don't need finer granularity.
739 * Return value: the ID (greater than 0) of the event source.
744 gdk_threads_add_timeout_seconds_full (gint priority,
746 GSourceFunc function,
748 GDestroyNotify notify)
750 GdkThreadsDispatch *dispatch;
752 g_return_val_if_fail (function != NULL, 0);
754 dispatch = g_slice_new (GdkThreadsDispatch);
755 dispatch->func = function;
756 dispatch->data = data;
757 dispatch->destroy = notify;
759 return g_timeout_add_seconds_full (priority,
761 gdk_threads_dispatch,
763 gdk_threads_dispatch_free);
767 * gdk_threads_add_timeout_seconds:
768 * @interval: the time between calls to the function, in seconds
769 * @function: function to call
770 * @data: data to pass to @function
772 * A wrapper for the common usage of gdk_threads_add_timeout_seconds_full()
773 * assigning the default priority, #G_PRIORITY_DEFAULT.
775 * For details, see gdk_threads_add_timeout_full().
777 * Return value: the ID (greater than 0) of the event source.
782 gdk_threads_add_timeout_seconds (guint interval,
783 GSourceFunc function,
786 return gdk_threads_add_timeout_seconds_full (G_PRIORITY_DEFAULT,
787 interval, function, data, NULL);
791 * gdk_get_program_class:
793 * Gets the program class. Unless the program class has explicitly
794 * been set with gdk_set_program_class() or with the <option>--class</option>
795 * commandline option, the default value is the program name (determined
796 * with g_get_prgname()) with the first character converted to uppercase.
798 * Returns: the program class.
800 G_CONST_RETURN char *
801 gdk_get_program_class (void)
803 return gdk_progclass;
807 * gdk_set_program_class:
808 * @program_class: a string.
810 * Sets the program class. The X11 backend uses the program class to set
811 * the class name part of the <literal>WM_CLASS</literal> property on
812 * toplevel windows; see the ICCCM.
815 gdk_set_program_class (const char *program_class)
817 g_free (gdk_progclass);
819 gdk_progclass = g_strdup (program_class);
823 * gdk_enable_multidevice:
825 * Enables multidevice support in GDK. This call must happen prior
826 * to gdk_display_open(), gtk_init(), gtk_init_with_args() or
827 * gtk_init_check() in order to take effect.
829 * Note that individual #GdkWindow<!-- -->s still need to explicitly
830 * enable multidevice awareness through gdk_window_set_support_multidevice().
832 * This function must be called before initializing GDK.
837 gdk_enable_multidevice (void)
842 _gdk_enable_multidevice = TRUE;