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/.
33 #include "gdkinternals.h"
36 #ifndef HAVE_XCONVERTCASE
37 #include "gdkkeysyms.h"
41 typedef struct _GdkPredicate GdkPredicate;
49 typedef struct _GdkThreadsDispatch GdkThreadsDispatch;
51 struct _GdkThreadsDispatch
55 GDestroyNotify destroy;
59 /* Private variable declarations
61 static int gdk_initialized = 0; /* 1 if the library is initialized,
65 static gchar *gdk_progclass = NULL;
68 static const GDebugKey gdk_debug_keys[] = {
69 {"events", GDK_DEBUG_EVENTS},
70 {"misc", GDK_DEBUG_MISC},
71 {"dnd", GDK_DEBUG_DND},
72 {"xim", GDK_DEBUG_XIM},
73 {"nograbs", GDK_DEBUG_NOGRABS},
74 {"colormap", GDK_DEBUG_COLORMAP},
75 {"gdkrgb", GDK_DEBUG_GDKRGB},
77 {"pixmap", GDK_DEBUG_PIXMAP},
78 {"image", GDK_DEBUG_IMAGE},
79 {"input", GDK_DEBUG_INPUT},
80 {"cursor", GDK_DEBUG_CURSOR},
81 {"multihead", GDK_DEBUG_MULTIHEAD},
82 {"xinerama", GDK_DEBUG_XINERAMA},
83 {"draw", GDK_DEBUG_DRAW},
84 {"eventloop", GDK_DEBUG_EVENTLOOP}
87 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
89 #endif /* G_ENABLE_DEBUG */
93 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data, GError **error)
95 guint debug_value = g_parse_debug_string (value,
96 (GDebugKey *) gdk_debug_keys,
99 if (debug_value == 0 && value != NULL && strcmp (value, "") != 0)
102 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
103 _("Error parsing option --gdk-debug"));
107 _gdk_debug_flags |= debug_value;
113 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data, GError **error)
115 guint debug_value = g_parse_debug_string (value,
116 (GDebugKey *) gdk_debug_keys,
119 if (debug_value == 0 && value != NULL && strcmp (value, "") != 0)
122 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
123 _("Error parsing option --gdk-no-debug"));
127 _gdk_debug_flags &= ~debug_value;
131 #endif /* G_ENABLE_DEBUG */
134 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
136 gdk_set_program_class (value);
142 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
144 g_set_prgname (value);
149 static const GOptionEntry gdk_args[] = {
150 { "class", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
151 /* Description of --class=CLASS in --help output */ N_("Program class as used by the window manager"),
152 /* Placeholder in --class=CLASS in --help output */ N_("CLASS") },
153 { "name", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
154 /* Description of --name=NAME in --help output */ N_("Program name as used by the window manager"),
155 /* Placeholder in --name=NAME in --help output */ N_("NAME") },
156 { "display", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &_gdk_display_name,
157 /* Description of --display=DISPLAY in --help output */ N_("X display to use"),
158 /* Placeholder in --display=DISPLAY in --help output */ N_("DISPLAY") },
159 { "screen", 0, 0, G_OPTION_ARG_INT, &_gdk_screen_number,
160 /* Description of --screen=SCREEN in --help output */ N_("X screen to use"),
161 /* Placeholder in --screen=SCREEN in --help output */ N_("SCREEN") },
162 #ifdef G_ENABLE_DEBUG
163 { "gdk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,
164 /* Description of --gdk-debug=FLAGS in --help output */ N_("Gdk debugging flags to set"),
165 /* Placeholder in --gdk-debug=FLAGS in --help output */ N_("FLAGS") },
166 { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb,
167 /* Description of --gdk-no-debug=FLAGS in --help output */ N_("Gdk debugging flags to unset"),
168 /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
174 * gdk_add_option_entries_libgtk_only:
175 * @group: An option group.
177 * Appends gdk option entries to the passed in option group. This is
178 * not public API and must not be used by applications.
181 gdk_add_option_entries_libgtk_only (GOptionGroup *group)
183 g_option_group_add_entries (group, gdk_args);
184 g_option_group_add_entries (group, _gdk_windowing_args);
188 gdk_pre_parse_libgtk_only (void)
190 gdk_initialized = TRUE;
192 /* We set the fallback program class here, rather than lazily in
193 * gdk_get_program_class, since we don't want -name to override it.
195 gdk_progclass = g_strdup (g_get_prgname ());
196 if (gdk_progclass && gdk_progclass[0])
197 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
199 #ifdef G_ENABLE_DEBUG
201 gchar *debug_string = getenv("GDK_DEBUG");
202 if (debug_string != NULL)
203 _gdk_debug_flags = g_parse_debug_string (debug_string,
204 (GDebugKey *) gdk_debug_keys,
207 #endif /* G_ENABLE_DEBUG */
209 if (getenv ("GDK_NATIVE_WINDOWS"))
211 _gdk_native_windows = TRUE;
212 /* Ensure that this is not propagated
213 to spawned applications */
214 g_unsetenv ("GDK_NATIVE_WINDOWS");
219 /* Do any setup particular to the windowing system
221 _gdk_windowing_init ();
227 * @argc: the number of command line arguments.
228 * @argv: the array of command line arguments.
230 * Parse command line arguments, and store for future
231 * use by calls to gdk_display_open().
233 * Any arguments used by GDK are removed from the array and @argc and @argv are
234 * updated accordingly.
236 * You shouldn't call this function explicitely if you are using
237 * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
242 gdk_parse_args (int *argc,
245 GOptionContext *option_context;
246 GOptionGroup *option_group;
247 GError *error = NULL;
252 gdk_pre_parse_libgtk_only ();
254 option_context = g_option_context_new (NULL);
255 g_option_context_set_ignore_unknown_options (option_context, TRUE);
256 g_option_context_set_help_enabled (option_context, FALSE);
257 option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
258 g_option_context_set_main_group (option_context, option_group);
260 g_option_group_add_entries (option_group, gdk_args);
261 g_option_group_add_entries (option_group, _gdk_windowing_args);
263 if (!g_option_context_parse (option_context, argc, argv, &error))
265 g_warning ("%s", error->message);
266 g_error_free (error);
268 g_option_context_free (option_context);
270 if (_gdk_debug_flags && GDK_DEBUG_GDKRGB)
271 gdk_rgb_set_verbose (TRUE);
273 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
277 * gdk_get_display_arg_name:
279 * Gets the display name specified in the command line arguments passed
280 * to gdk_init() or gdk_parse_args(), if any.
282 * Returns: the display name, if specified explicitely, otherwise %NULL
283 * this string is owned by GTK+ and must not be modified or freed.
287 G_CONST_RETURN gchar *
288 gdk_get_display_arg_name (void)
290 if (!_gdk_display_arg_name)
292 if (_gdk_screen_number >= 0)
293 _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
295 _gdk_display_arg_name = g_strdup (_gdk_display_name);
298 return _gdk_display_arg_name;
302 * gdk_display_open_default_libgtk_only:
304 * Opens the default display specified by command line arguments or
305 * environment variables, sets it as the default display, and returns
306 * it. gdk_parse_args must have been called first. If the default
307 * display has previously been set, simply returns that. An internal
308 * function that should not be used by applications.
310 * Return value: the default display, if it could be opened,
314 gdk_display_open_default_libgtk_only (void)
318 g_return_val_if_fail (gdk_initialized, NULL);
320 display = gdk_display_get_default ();
324 display = gdk_display_open (gdk_get_display_arg_name ());
326 if (!display && _gdk_screen_number >= 0)
328 g_free (_gdk_display_arg_name);
329 _gdk_display_arg_name = g_strdup (_gdk_display_name);
331 display = gdk_display_open (_gdk_display_name);
335 gdk_display_manager_set_default_display (gdk_display_manager_get (),
344 * @argv: (array length=argc) (inout):
346 * Initialize the library for use.
349 * "argc" is the number of arguments.
350 * "argv" is an array of strings.
353 * "argc" and "argv" are modified to reflect any arguments
354 * which were not handled. (Such arguments should either
355 * be handled by the application or dismissed). If initialization
356 * fails, returns FALSE, otherwise TRUE.
359 * The library is initialized.
361 *--------------------------------------------------------------
364 gdk_init_check (int *argc,
367 gdk_parse_args (argc, argv);
369 return gdk_display_open_default_libgtk_only () != NULL;
376 * @argv: (array length=argc) (inout):
379 gdk_init (int *argc, char ***argv)
381 if (!gdk_init_check (argc, argv))
383 const char *display_name = gdk_get_display_arg_name ();
384 g_warning ("cannot open display: %s", display_name ? display_name : "");
390 *--------------------------------------------------------------
393 * Restores the library to an un-itialized state and exits
394 * the program using the "exit" system call.
397 * "errorcode" is the error value to pass to "exit".
400 * Allocated structures are freed and the program exits
405 *--------------------------------------------------------------
409 gdk_exit (gint errorcode)
415 gdk_threads_enter (void)
417 GDK_THREADS_ENTER ();
421 gdk_threads_leave (void)
423 GDK_THREADS_LEAVE ();
427 gdk_threads_impl_lock (void)
429 if (gdk_threads_mutex)
430 g_mutex_lock (gdk_threads_mutex);
434 gdk_threads_impl_unlock (void)
436 if (gdk_threads_mutex)
437 g_mutex_unlock (gdk_threads_mutex);
443 * Initializes GDK so that it can be used from multiple threads
444 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
445 * g_thread_init() must be called previous to this function.
447 * This call must be made before any use of the main loop from
448 * GTK+; to be safe, call it before gtk_init().
451 gdk_threads_init (void)
453 if (!g_thread_supported ())
454 g_error ("g_thread_init() must be called before gdk_threads_init()");
456 gdk_threads_mutex = g_mutex_new ();
457 if (!gdk_threads_lock)
458 gdk_threads_lock = gdk_threads_impl_lock;
459 if (!gdk_threads_unlock)
460 gdk_threads_unlock = gdk_threads_impl_unlock;
464 * gdk_threads_set_lock_functions:
465 * @enter_fn: function called to guard GDK
466 * @leave_fn: function called to release the guard
468 * Allows the application to replace the standard method that
469 * GDK uses to protect its data structures. Normally, GDK
470 * creates a single #GMutex that is locked by gdk_threads_enter(),
471 * and released by gdk_threads_leave(); using this function an
472 * application provides, instead, a function @enter_fn that is
473 * called by gdk_threads_enter() and a function @leave_fn that is
474 * called by gdk_threads_leave().
476 * The functions must provide at least same locking functionality
477 * as the default implementation, but can also do extra application
478 * specific processing.
480 * As an example, consider an application that has its own recursive
481 * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
482 * the GTK+ lock when entering a recursive main loop, the application
483 * must temporarily release its lock as well.
485 * Most threaded GTK+ apps won't need to use this method.
487 * This method must be called before gdk_threads_init(), and cannot
488 * be called multiple times.
493 gdk_threads_set_lock_functions (GCallback enter_fn,
496 g_return_if_fail (gdk_threads_lock == NULL &&
497 gdk_threads_unlock == NULL);
499 gdk_threads_lock = enter_fn;
500 gdk_threads_unlock = leave_fn;
504 gdk_threads_dispatch (gpointer data)
506 GdkThreadsDispatch *dispatch = data;
507 gboolean ret = FALSE;
509 GDK_THREADS_ENTER ();
511 if (!g_source_is_destroyed (g_main_current_source ()))
512 ret = dispatch->func (dispatch->data);
514 GDK_THREADS_LEAVE ();
520 gdk_threads_dispatch_free (gpointer data)
522 GdkThreadsDispatch *dispatch = data;
524 if (dispatch->destroy && dispatch->data)
525 dispatch->destroy (dispatch->data);
527 g_slice_free (GdkThreadsDispatch, data);
532 * gdk_threads_add_idle_full:
533 * @priority: the priority of the idle source. Typically this will be in the
534 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
535 * @function: function to call
536 * @data: data to pass to @function
537 * @notify: function to call when the idle is removed, or %NULL
539 * Adds a function to be called whenever there are no higher priority
540 * events pending. If the function returns %FALSE it is automatically
541 * removed from the list of event sources and will not be called again.
543 * This variant of g_idle_add_full() calls @function with the GDK lock
544 * held. It can be thought of a MT-safe version for GTK+ widgets for the
545 * following use case, where you have to worry about idle_callback()
546 * running in thread A and accessing @self after it has been finalized
551 * idle_callback (gpointer data)
553 * /* gdk_threads_enter(); would be needed for g_idle_add() */
555 * SomeWidget *self = data;
556 * /* do stuff with self */
560 * /* gdk_threads_leave(); would be needed for g_idle_add() */
565 * some_widget_do_stuff_later (SomeWidget *self)
567 * self->idle_id = gdk_threads_add_idle (idle_callback, self)
568 * /* using g_idle_add() here would require thread protection in the callback */
572 * some_widget_finalize (GObject *object)
574 * SomeWidget *self = SOME_WIDGET (object);
576 * g_source_remove (self->idle_id);
577 * G_OBJECT_CLASS (parent_class)->finalize (object);
581 * Return value: the ID (greater than 0) of the event source.
586 gdk_threads_add_idle_full (gint priority,
587 GSourceFunc function,
589 GDestroyNotify notify)
591 GdkThreadsDispatch *dispatch;
593 g_return_val_if_fail (function != NULL, 0);
595 dispatch = g_slice_new (GdkThreadsDispatch);
596 dispatch->func = function;
597 dispatch->data = data;
598 dispatch->destroy = notify;
600 return g_idle_add_full (priority,
601 gdk_threads_dispatch,
603 gdk_threads_dispatch_free);
607 * gdk_threads_add_idle:
608 * @function: function to call
609 * @data: data to pass to @function
611 * A wrapper for the common usage of gdk_threads_add_idle_full()
612 * assigning the default priority, #G_PRIORITY_DEFAULT_IDLE.
614 * See gdk_threads_add_idle_full().
616 * Return value: the ID (greater than 0) of the event source.
621 gdk_threads_add_idle (GSourceFunc function,
624 return gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
625 function, data, NULL);
630 * gdk_threads_add_timeout_full:
631 * @priority: the priority of the timeout source. Typically this will be in the
632 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
633 * @interval: the time between calls to the function, in milliseconds
634 * (1/1000ths of a second)
635 * @function: function to call
636 * @data: data to pass to @function
637 * @notify: function to call when the timeout is removed, or %NULL
639 * Sets a function to be called at regular intervals holding the GDK lock,
640 * with the given priority. The function is called repeatedly until it
641 * returns %FALSE, at which point the timeout is automatically destroyed
642 * and the function will not be called again. The @notify function is
643 * called when the timeout is destroyed. The first call to the
644 * function will be at the end of the first @interval.
646 * Note that timeout functions may be delayed, due to the processing of other
647 * event sources. Thus they should not be relied on for precise timing.
648 * After each call to the timeout function, the time of the next
649 * timeout is recalculated based on the current time and the given interval
650 * (it does not try to 'catch up' time lost in delays).
652 * This variant of g_timeout_add_full() can be thought of a MT-safe version
653 * for GTK+ widgets for the following use case:
656 * static gboolean timeout_callback (gpointer data)
658 * SomeWidget *self = data;
660 * /* do stuff with self */
662 * self->timeout_id = 0;
667 * static void some_widget_do_stuff_later (SomeWidget *self)
669 * self->timeout_id = g_timeout_add (timeout_callback, self)
672 * static void some_widget_finalize (GObject *object)
674 * SomeWidget *self = SOME_WIDGET (object);
676 * if (self->timeout_id)
677 * g_source_remove (self->timeout_id);
679 * G_OBJECT_CLASS (parent_class)->finalize (object);
683 * Return value: the ID (greater than 0) of the event source.
688 gdk_threads_add_timeout_full (gint priority,
690 GSourceFunc function,
692 GDestroyNotify notify)
694 GdkThreadsDispatch *dispatch;
696 g_return_val_if_fail (function != NULL, 0);
698 dispatch = g_slice_new (GdkThreadsDispatch);
699 dispatch->func = function;
700 dispatch->data = data;
701 dispatch->destroy = notify;
703 return g_timeout_add_full (priority,
705 gdk_threads_dispatch,
707 gdk_threads_dispatch_free);
711 * gdk_threads_add_timeout:
712 * @interval: the time between calls to the function, in milliseconds
713 * (1/1000ths of a second)
714 * @function: function to call
715 * @data: data to pass to @function
717 * A wrapper for the common usage of gdk_threads_add_timeout_full()
718 * assigning the default priority, #G_PRIORITY_DEFAULT.
720 * See gdk_threads_add_timeout_full().
722 * Return value: the ID (greater than 0) of the event source.
727 gdk_threads_add_timeout (guint interval,
728 GSourceFunc function,
731 return gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
732 interval, function, data, NULL);
737 * gdk_threads_add_timeout_seconds_full:
738 * @priority: the priority of the timeout source. Typically this will be in the
739 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
740 * @interval: the time between calls to the function, in seconds
741 * @function: function to call
742 * @data: data to pass to @function
743 * @notify: function to call when the timeout is removed, or %NULL
745 * A variant of gdk_threads_add_timout_full() with second-granularity.
746 * See g_timeout_add_seconds_full() for a discussion of why it is
747 * a good idea to use this function if you don't need finer granularity.
749 * Return value: the ID (greater than 0) of the event source.
754 gdk_threads_add_timeout_seconds_full (gint priority,
756 GSourceFunc function,
758 GDestroyNotify notify)
760 GdkThreadsDispatch *dispatch;
762 g_return_val_if_fail (function != NULL, 0);
764 dispatch = g_slice_new (GdkThreadsDispatch);
765 dispatch->func = function;
766 dispatch->data = data;
767 dispatch->destroy = notify;
769 return g_timeout_add_seconds_full (priority,
771 gdk_threads_dispatch,
773 gdk_threads_dispatch_free);
777 * gdk_threads_add_timeout_seconds:
778 * @interval: the time between calls to the function, in seconds
779 * @function: function to call
780 * @data: data to pass to @function
782 * A wrapper for the common usage of gdk_threads_add_timeout_seconds_full()
783 * assigning the default priority, #G_PRIORITY_DEFAULT.
785 * For details, see gdk_threads_add_timeout_full().
787 * Return value: the ID (greater than 0) of the event source.
792 gdk_threads_add_timeout_seconds (guint interval,
793 GSourceFunc function,
796 return gdk_threads_add_timeout_seconds_full (G_PRIORITY_DEFAULT,
797 interval, function, data, NULL);
801 G_CONST_RETURN char *
802 gdk_get_program_class (void)
804 return gdk_progclass;
808 gdk_set_program_class (const char *program_class)
810 g_free (gdk_progclass);
812 gdk_progclass = g_strdup (program_class);
816 #include "gdkaliasdef.c"