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"
40 typedef struct _GdkPredicate GdkPredicate;
48 typedef struct _GdkThreadsDispatch GdkThreadsDispatch;
50 struct _GdkThreadsDispatch
54 GDestroyNotify destroy;
58 /* Private variable declarations
60 static int gdk_initialized = 0; /* 1 if the library is initialized,
64 static gchar *gdk_progclass = NULL;
67 static const GDebugKey gdk_debug_keys[] = {
68 {"events", GDK_DEBUG_EVENTS},
69 {"misc", GDK_DEBUG_MISC},
70 {"dnd", GDK_DEBUG_DND},
71 {"xim", GDK_DEBUG_XIM},
72 {"nograbs", GDK_DEBUG_NOGRABS},
73 {"colormap", GDK_DEBUG_COLORMAP},
75 {"pixmap", GDK_DEBUG_PIXMAP},
76 {"image", GDK_DEBUG_IMAGE},
77 {"input", GDK_DEBUG_INPUT},
78 {"cursor", GDK_DEBUG_CURSOR},
79 {"multihead", GDK_DEBUG_MULTIHEAD},
80 {"xinerama", GDK_DEBUG_XINERAMA},
81 {"draw", GDK_DEBUG_DRAW},
82 {"eventloop", GDK_DEBUG_EVENTLOOP}
85 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
87 #endif /* G_ENABLE_DEBUG */
91 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data, GError **error)
93 guint debug_value = g_parse_debug_string (value,
94 (GDebugKey *) gdk_debug_keys,
97 if (debug_value == 0 && value != NULL && strcmp (value, "") != 0)
100 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
101 _("Error parsing option --gdk-debug"));
105 _gdk_debug_flags |= debug_value;
111 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data, GError **error)
113 guint debug_value = g_parse_debug_string (value,
114 (GDebugKey *) gdk_debug_keys,
117 if (debug_value == 0 && value != NULL && strcmp (value, "") != 0)
120 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
121 _("Error parsing option --gdk-no-debug"));
125 _gdk_debug_flags &= ~debug_value;
129 #endif /* G_ENABLE_DEBUG */
132 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
134 gdk_set_program_class (value);
140 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
142 g_set_prgname (value);
147 static const GOptionEntry gdk_args[] = {
148 { "class", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
149 /* Description of --class=CLASS in --help output */ N_("Program class as used by the window manager"),
150 /* Placeholder in --class=CLASS in --help output */ N_("CLASS") },
151 { "name", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
152 /* Description of --name=NAME in --help output */ N_("Program name as used by the window manager"),
153 /* Placeholder in --name=NAME in --help output */ N_("NAME") },
154 { "display", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &_gdk_display_name,
155 /* Description of --display=DISPLAY in --help output */ N_("X display to use"),
156 /* Placeholder in --display=DISPLAY in --help output */ N_("DISPLAY") },
157 { "screen", 0, 0, G_OPTION_ARG_INT, &_gdk_screen_number,
158 /* Description of --screen=SCREEN in --help output */ N_("X screen to use"),
159 /* Placeholder in --screen=SCREEN in --help output */ N_("SCREEN") },
160 #ifdef G_ENABLE_DEBUG
161 { "gdk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,
162 /* Description of --gdk-debug=FLAGS in --help output */ N_("GDK debugging flags to set"),
163 /* Placeholder in --gdk-debug=FLAGS in --help output */ N_("FLAGS") },
164 { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb,
165 /* Description of --gdk-no-debug=FLAGS in --help output */ N_("GDK debugging flags to unset"),
166 /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
172 * gdk_add_option_entries_libgtk_only:
173 * @group: An option group.
175 * Appends gdk option entries to the passed in option group. This is
176 * not public API and must not be used by applications.
179 gdk_add_option_entries_libgtk_only (GOptionGroup *group)
181 g_option_group_add_entries (group, gdk_args);
182 g_option_group_add_entries (group, _gdk_windowing_args);
186 gdk_pre_parse_libgtk_only (void)
188 gdk_initialized = TRUE;
190 /* We set the fallback program class here, rather than lazily in
191 * gdk_get_program_class, since we don't want -name to override it.
193 gdk_progclass = g_strdup (g_get_prgname ());
194 if (gdk_progclass && gdk_progclass[0])
195 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
197 #ifdef G_ENABLE_DEBUG
199 gchar *debug_string = getenv("GDK_DEBUG");
200 if (debug_string != NULL)
201 _gdk_debug_flags = g_parse_debug_string (debug_string,
202 (GDebugKey *) gdk_debug_keys,
205 #endif /* G_ENABLE_DEBUG */
207 if (getenv ("GDK_NATIVE_WINDOWS"))
209 _gdk_native_windows = TRUE;
210 /* Ensure that this is not propagated
211 to spawned applications */
212 g_unsetenv ("GDK_NATIVE_WINDOWS");
217 /* Do any setup particular to the windowing system
219 _gdk_windowing_init ();
225 * @argc: the number of command line arguments.
226 * @argv: the array of command line arguments.
228 * Parse command line arguments, and store for future
229 * use by calls to gdk_display_open().
231 * Any arguments used by GDK are removed from the array and @argc and @argv are
232 * updated accordingly.
234 * You shouldn't call this function explicitely if you are using
235 * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
240 gdk_parse_args (int *argc,
243 GOptionContext *option_context;
244 GOptionGroup *option_group;
245 GError *error = NULL;
250 gdk_pre_parse_libgtk_only ();
252 option_context = g_option_context_new (NULL);
253 g_option_context_set_ignore_unknown_options (option_context, TRUE);
254 g_option_context_set_help_enabled (option_context, FALSE);
255 option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
256 g_option_context_set_main_group (option_context, option_group);
258 g_option_group_add_entries (option_group, gdk_args);
259 g_option_group_add_entries (option_group, _gdk_windowing_args);
261 if (!g_option_context_parse (option_context, argc, argv, &error))
263 g_warning ("%s", error->message);
264 g_error_free (error);
266 g_option_context_free (option_context);
268 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
272 * gdk_get_display_arg_name:
274 * Gets the display name specified in the command line arguments passed
275 * to gdk_init() or gdk_parse_args(), if any.
277 * Returns: the display name, if specified explicitely, otherwise %NULL
278 * this string is owned by GTK+ and must not be modified or freed.
282 G_CONST_RETURN gchar *
283 gdk_get_display_arg_name (void)
285 if (!_gdk_display_arg_name)
287 if (_gdk_screen_number >= 0)
288 _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
290 _gdk_display_arg_name = g_strdup (_gdk_display_name);
293 return _gdk_display_arg_name;
297 * gdk_display_open_default_libgtk_only:
299 * Opens the default display specified by command line arguments or
300 * environment variables, sets it as the default display, and returns
301 * it. gdk_parse_args must have been called first. If the default
302 * display has previously been set, simply returns that. An internal
303 * function that should not be used by applications.
305 * Return value: the default display, if it could be opened,
309 gdk_display_open_default_libgtk_only (void)
313 g_return_val_if_fail (gdk_initialized, NULL);
315 display = gdk_display_get_default ();
319 display = gdk_display_open (gdk_get_display_arg_name ());
321 if (!display && _gdk_screen_number >= 0)
323 g_free (_gdk_display_arg_name);
324 _gdk_display_arg_name = g_strdup (_gdk_display_name);
326 display = gdk_display_open (_gdk_display_name);
330 gdk_display_manager_set_default_display (gdk_display_manager_get (),
339 * @argv: (array length=argc) (inout):
341 * Initialize the library for use.
344 * "argc" is the number of arguments.
345 * "argv" is an array of strings.
348 * "argc" and "argv" are modified to reflect any arguments
349 * which were not handled. (Such arguments should either
350 * be handled by the application or dismissed). If initialization
351 * fails, returns FALSE, otherwise TRUE.
354 * The library is initialized.
356 *--------------------------------------------------------------
359 gdk_init_check (int *argc,
362 gdk_parse_args (argc, argv);
364 return gdk_display_open_default_libgtk_only () != NULL;
371 * @argv: (array length=argc) (inout):
374 gdk_init (int *argc, char ***argv)
376 if (!gdk_init_check (argc, argv))
378 const char *display_name = gdk_get_display_arg_name ();
379 g_warning ("cannot open display: %s", display_name ? display_name : "");
385 gdk_threads_enter (void)
387 GDK_THREADS_ENTER ();
391 gdk_threads_leave (void)
393 GDK_THREADS_LEAVE ();
397 gdk_threads_impl_lock (void)
399 if (gdk_threads_mutex)
400 g_mutex_lock (gdk_threads_mutex);
404 gdk_threads_impl_unlock (void)
406 if (gdk_threads_mutex)
407 g_mutex_unlock (gdk_threads_mutex);
413 * Initializes GDK so that it can be used from multiple threads
414 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
415 * g_thread_init() must be called previous to this function.
417 * This call must be made before any use of the main loop from
418 * GTK+; to be safe, call it before gtk_init().
421 gdk_threads_init (void)
423 if (!g_thread_supported ())
424 g_error ("g_thread_init() must be called before gdk_threads_init()");
426 gdk_threads_mutex = g_mutex_new ();
427 if (!gdk_threads_lock)
428 gdk_threads_lock = gdk_threads_impl_lock;
429 if (!gdk_threads_unlock)
430 gdk_threads_unlock = gdk_threads_impl_unlock;
434 * gdk_threads_set_lock_functions:
435 * @enter_fn: function called to guard GDK
436 * @leave_fn: function called to release the guard
438 * Allows the application to replace the standard method that
439 * GDK uses to protect its data structures. Normally, GDK
440 * creates a single #GMutex that is locked by gdk_threads_enter(),
441 * and released by gdk_threads_leave(); using this function an
442 * application provides, instead, a function @enter_fn that is
443 * called by gdk_threads_enter() and a function @leave_fn that is
444 * called by gdk_threads_leave().
446 * The functions must provide at least same locking functionality
447 * as the default implementation, but can also do extra application
448 * specific processing.
450 * As an example, consider an application that has its own recursive
451 * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
452 * the GTK+ lock when entering a recursive main loop, the application
453 * must temporarily release its lock as well.
455 * Most threaded GTK+ apps won't need to use this method.
457 * This method must be called before gdk_threads_init(), and cannot
458 * be called multiple times.
463 gdk_threads_set_lock_functions (GCallback enter_fn,
466 g_return_if_fail (gdk_threads_lock == NULL &&
467 gdk_threads_unlock == NULL);
469 gdk_threads_lock = enter_fn;
470 gdk_threads_unlock = leave_fn;
474 gdk_threads_dispatch (gpointer data)
476 GdkThreadsDispatch *dispatch = data;
477 gboolean ret = FALSE;
479 GDK_THREADS_ENTER ();
481 if (!g_source_is_destroyed (g_main_current_source ()))
482 ret = dispatch->func (dispatch->data);
484 GDK_THREADS_LEAVE ();
490 gdk_threads_dispatch_free (gpointer data)
492 GdkThreadsDispatch *dispatch = data;
494 if (dispatch->destroy && dispatch->data)
495 dispatch->destroy (dispatch->data);
497 g_slice_free (GdkThreadsDispatch, data);
502 * gdk_threads_add_idle_full:
503 * @priority: the priority of the idle source. Typically this will be in the
504 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
505 * @function: function to call
506 * @data: data to pass to @function
507 * @notify: (allow-none): function to call when the idle is removed, or %NULL
509 * Adds a function to be called whenever there are no higher priority
510 * events pending. If the function returns %FALSE it is automatically
511 * removed from the list of event sources and will not be called again.
513 * This variant of g_idle_add_full() calls @function with the GDK lock
514 * held. It can be thought of a MT-safe version for GTK+ widgets for the
515 * following use case, where you have to worry about idle_callback()
516 * running in thread A and accessing @self after it has been finalized
521 * idle_callback (gpointer data)
523 * /* gdk_threads_enter(); would be needed for g_idle_add() */
525 * SomeWidget *self = data;
526 * /* do stuff with self */
530 * /* gdk_threads_leave(); would be needed for g_idle_add() */
535 * some_widget_do_stuff_later (SomeWidget *self)
537 * self->idle_id = gdk_threads_add_idle (idle_callback, self)
538 * /* using g_idle_add() here would require thread protection in the callback */
542 * some_widget_finalize (GObject *object)
544 * SomeWidget *self = SOME_WIDGET (object);
546 * g_source_remove (self->idle_id);
547 * G_OBJECT_CLASS (parent_class)->finalize (object);
551 * Return value: the ID (greater than 0) of the event source.
556 gdk_threads_add_idle_full (gint priority,
557 GSourceFunc function,
559 GDestroyNotify notify)
561 GdkThreadsDispatch *dispatch;
563 g_return_val_if_fail (function != NULL, 0);
565 dispatch = g_slice_new (GdkThreadsDispatch);
566 dispatch->func = function;
567 dispatch->data = data;
568 dispatch->destroy = notify;
570 return g_idle_add_full (priority,
571 gdk_threads_dispatch,
573 gdk_threads_dispatch_free);
577 * gdk_threads_add_idle:
578 * @function: function to call
579 * @data: data to pass to @function
581 * A wrapper for the common usage of gdk_threads_add_idle_full()
582 * assigning the default priority, #G_PRIORITY_DEFAULT_IDLE.
584 * See gdk_threads_add_idle_full().
586 * Return value: the ID (greater than 0) of the event source.
591 gdk_threads_add_idle (GSourceFunc function,
594 return gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
595 function, data, NULL);
600 * gdk_threads_add_timeout_full:
601 * @priority: the priority of the timeout source. Typically this will be in the
602 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
603 * @interval: the time between calls to the function, in milliseconds
604 * (1/1000ths of a second)
605 * @function: function to call
606 * @data: data to pass to @function
607 * @notify: (allow-none): function to call when the timeout is removed, or %NULL
609 * Sets a function to be called at regular intervals holding the GDK lock,
610 * with the given priority. The function is called repeatedly until it
611 * returns %FALSE, at which point the timeout is automatically destroyed
612 * and the function will not be called again. The @notify function is
613 * called when the timeout is destroyed. The first call to the
614 * function will be at the end of the first @interval.
616 * Note that timeout functions may be delayed, due to the processing of other
617 * event sources. Thus they should not be relied on for precise timing.
618 * After each call to the timeout function, the time of the next
619 * timeout is recalculated based on the current time and the given interval
620 * (it does not try to 'catch up' time lost in delays).
622 * This variant of g_timeout_add_full() can be thought of a MT-safe version
623 * for GTK+ widgets for the following use case:
626 * static gboolean timeout_callback (gpointer data)
628 * SomeWidget *self = data;
630 * /* do stuff with self */
632 * self->timeout_id = 0;
637 * static void some_widget_do_stuff_later (SomeWidget *self)
639 * self->timeout_id = g_timeout_add (timeout_callback, self)
642 * static void some_widget_finalize (GObject *object)
644 * SomeWidget *self = SOME_WIDGET (object);
646 * if (self->timeout_id)
647 * g_source_remove (self->timeout_id);
649 * G_OBJECT_CLASS (parent_class)->finalize (object);
653 * Return value: the ID (greater than 0) of the event source.
658 gdk_threads_add_timeout_full (gint priority,
660 GSourceFunc function,
662 GDestroyNotify notify)
664 GdkThreadsDispatch *dispatch;
666 g_return_val_if_fail (function != NULL, 0);
668 dispatch = g_slice_new (GdkThreadsDispatch);
669 dispatch->func = function;
670 dispatch->data = data;
671 dispatch->destroy = notify;
673 return g_timeout_add_full (priority,
675 gdk_threads_dispatch,
677 gdk_threads_dispatch_free);
681 * gdk_threads_add_timeout:
682 * @interval: the time between calls to the function, in milliseconds
683 * (1/1000ths of a second)
684 * @function: function to call
685 * @data: data to pass to @function
687 * A wrapper for the common usage of gdk_threads_add_timeout_full()
688 * assigning the default priority, #G_PRIORITY_DEFAULT.
690 * See gdk_threads_add_timeout_full().
692 * Return value: the ID (greater than 0) of the event source.
697 gdk_threads_add_timeout (guint interval,
698 GSourceFunc function,
701 return gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
702 interval, function, data, NULL);
707 * gdk_threads_add_timeout_seconds_full:
708 * @priority: the priority of the timeout source. Typically this will be in the
709 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
710 * @interval: the time between calls to the function, in seconds
711 * @function: function to call
712 * @data: data to pass to @function
713 * @notify: (allow-none): function to call when the timeout is removed, or %NULL
715 * A variant of gdk_threads_add_timout_full() with second-granularity.
716 * See g_timeout_add_seconds_full() for a discussion of why it is
717 * a good idea to use this function if you don't need finer granularity.
719 * Return value: the ID (greater than 0) of the event source.
724 gdk_threads_add_timeout_seconds_full (gint priority,
726 GSourceFunc function,
728 GDestroyNotify notify)
730 GdkThreadsDispatch *dispatch;
732 g_return_val_if_fail (function != NULL, 0);
734 dispatch = g_slice_new (GdkThreadsDispatch);
735 dispatch->func = function;
736 dispatch->data = data;
737 dispatch->destroy = notify;
739 return g_timeout_add_seconds_full (priority,
741 gdk_threads_dispatch,
743 gdk_threads_dispatch_free);
747 * gdk_threads_add_timeout_seconds:
748 * @interval: the time between calls to the function, in seconds
749 * @function: function to call
750 * @data: data to pass to @function
752 * A wrapper for the common usage of gdk_threads_add_timeout_seconds_full()
753 * assigning the default priority, #G_PRIORITY_DEFAULT.
755 * For details, see gdk_threads_add_timeout_full().
757 * Return value: the ID (greater than 0) of the event source.
762 gdk_threads_add_timeout_seconds (guint interval,
763 GSourceFunc function,
766 return gdk_threads_add_timeout_seconds_full (G_PRIORITY_DEFAULT,
767 interval, function, data, NULL);
771 G_CONST_RETURN char *
772 gdk_get_program_class (void)
774 return gdk_progclass;
778 gdk_set_program_class (const char *program_class)
780 g_free (gdk_progclass);
782 gdk_progclass = g_strdup (program_class);
786 * gdk_enable_multidevice:
788 * Enables multidevice support in GDK. This call must happen prior
789 * to gdk_display_open(), gtk_init(), gtk_init_with_args() or
790 * gtk_init_check() in order to take effect.
792 * Note that individual #GdkWindow<!-- -->s still need to explicitly
793 * enable multidevice awareness through gdk_window_set_support_multidevice().
795 * This function must be called before initializing GDK.
800 gdk_enable_multidevice (void)
805 _gdk_enable_multidevice = TRUE;