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}
86 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
88 #endif /* G_ENABLE_DEBUG */
92 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
94 _gdk_debug_flags |= g_parse_debug_string (value,
95 (GDebugKey *) gdk_debug_keys,
100 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
102 _gdk_debug_flags &= ~g_parse_debug_string (value,
103 (GDebugKey *) gdk_debug_keys,
106 #endif /* G_ENABLE_DEBUG */
109 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
111 gdk_set_program_class (value);
117 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
119 g_set_prgname (value);
124 static const GOptionEntry gdk_args[] = {
125 { "class", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
126 /* Description of --class=CLASS in --help output */ N_("Program class as used by the window manager"),
127 /* Placeholder in --class=CLASS in --help output */ N_("CLASS") },
128 { "name", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
129 /* Description of --name=NAME in --help output */ N_("Program name as used by the window manager"),
130 /* Placeholder in --name=NAME in --help output */ N_("NAME") },
131 { "display", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &_gdk_display_name,
132 /* Description of --display=DISPLAY in --help output */ N_("X display to use"),
133 /* Placeholder in --display=DISPLAY in --help output */ N_("DISPLAY") },
134 { "screen", 0, 0, G_OPTION_ARG_INT, &_gdk_screen_number,
135 /* Description of --screen=SCREEN in --help output */ N_("X screen to use"),
136 /* Placeholder in --screen=SCREEN in --help output */ N_("SCREEN") },
137 #ifdef G_ENABLE_DEBUG
138 { "gdk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,
139 /* Description of --gdk-debug=FLAGS in --help output */ N_("Gdk debugging flags to set"),
140 /* Placeholder in --gdk-debug=FLAGS in --help output */ N_("FLAGS") },
141 { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb,
142 /* Description of --gdk-no-debug=FLAGS in --help output */ N_("Gdk debugging flags to unset"),
143 /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
149 * gdk_add_option_entries_libgtk_only:
150 * @group: An option group.
152 * Appends gdk option entries to the passed in option group. This is
153 * not public API and must not be used by applications.
156 gdk_add_option_entries_libgtk_only (GOptionGroup *group)
158 g_option_group_add_entries (group, gdk_args);
159 g_option_group_add_entries (group, _gdk_windowing_args);
163 gdk_pre_parse_libgtk_only (void)
165 gdk_initialized = TRUE;
167 /* We set the fallback program class here, rather than lazily in
168 * gdk_get_program_class, since we don't want -name to override it.
170 gdk_progclass = g_strdup (g_get_prgname ());
171 if (gdk_progclass && gdk_progclass[0])
172 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
174 #ifdef G_ENABLE_DEBUG
176 gchar *debug_string = getenv("GDK_DEBUG");
177 if (debug_string != NULL)
178 _gdk_debug_flags = g_parse_debug_string (debug_string,
179 (GDebugKey *) gdk_debug_keys,
182 #endif /* G_ENABLE_DEBUG */
186 /* Do any setup particular to the windowing system
188 _gdk_windowing_init ();
194 * @argc: the number of command line arguments.
195 * @argv: the array of command line arguments.
197 * Parse command line arguments, and store for future
198 * use by calls to gdk_display_open().
200 * Any arguments used by GDK are removed from the array and @argc and @argv are
201 * updated accordingly.
203 * You shouldn't call this function explicitely if you are using
204 * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
209 gdk_parse_args (int *argc,
212 GOptionContext *option_context;
213 GOptionGroup *option_group;
214 GError *error = NULL;
219 gdk_pre_parse_libgtk_only ();
221 option_context = g_option_context_new (NULL);
222 g_option_context_set_ignore_unknown_options (option_context, TRUE);
223 g_option_context_set_help_enabled (option_context, FALSE);
224 option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
225 g_option_context_set_main_group (option_context, option_group);
227 g_option_group_add_entries (option_group, gdk_args);
228 g_option_group_add_entries (option_group, _gdk_windowing_args);
230 if (!g_option_context_parse (option_context, argc, argv, &error))
232 g_warning ("%s", error->message);
233 g_error_free (error);
235 g_option_context_free (option_context);
237 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
241 * gdk_get_display_arg_name:
243 * Gets the display name specified in the command line arguments passed
244 * to gdk_init() or gdk_parse_args(), if any.
246 * Returns: the display name, if specified explicitely, otherwise %NULL
247 * this string is owned by GTK+ and must not be modified or freed.
251 G_CONST_RETURN gchar *
252 gdk_get_display_arg_name (void)
254 if (!_gdk_display_arg_name)
256 if (_gdk_screen_number >= 0)
257 _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
259 _gdk_display_arg_name = g_strdup (_gdk_display_name);
262 return _gdk_display_arg_name;
266 * gdk_display_open_default_libgtk_only:
268 * Opens the default display specified by command line arguments or
269 * environment variables, sets it as the default display, and returns
270 * it. gdk_parse_args must have been called first. If the default
271 * display has previously been set, simply returns that. An internal
272 * function that should not be used by applications.
274 * Return value: the default display, if it could be opened,
278 gdk_display_open_default_libgtk_only (void)
282 g_return_val_if_fail (gdk_initialized, NULL);
284 display = gdk_display_get_default ();
288 display = gdk_display_open (gdk_get_display_arg_name ());
290 if (!display && _gdk_screen_number >= 0)
292 g_free (_gdk_display_arg_name);
293 _gdk_display_arg_name = g_strdup (_gdk_display_name);
295 display = gdk_display_open (_gdk_display_name);
299 gdk_display_manager_set_default_display (gdk_display_manager_get (),
306 *--------------------------------------------------------------
309 * Initialize the library for use.
312 * "argc" is the number of arguments.
313 * "argv" is an array of strings.
316 * "argc" and "argv" are modified to reflect any arguments
317 * which were not handled. (Such arguments should either
318 * be handled by the application or dismissed). If initialization
319 * fails, returns FALSE, otherwise TRUE.
322 * The library is initialized.
324 *--------------------------------------------------------------
328 gdk_init_check (int *argc,
331 gdk_parse_args (argc, argv);
333 return gdk_display_open_default_libgtk_only () != NULL;
337 gdk_init (int *argc, char ***argv)
339 if (!gdk_init_check (argc, argv))
341 const char *display_name = gdk_get_display_arg_name ();
342 g_warning ("cannot open display: %s", display_name ? display_name : "");
348 *--------------------------------------------------------------
351 * Restores the library to an un-itialized state and exits
352 * the program using the "exit" system call.
355 * "errorcode" is the error value to pass to "exit".
358 * Allocated structures are freed and the program exits
363 *--------------------------------------------------------------
367 gdk_exit (gint errorcode)
373 gdk_threads_enter (void)
375 GDK_THREADS_ENTER ();
379 gdk_threads_leave (void)
381 GDK_THREADS_LEAVE ();
385 gdk_threads_impl_lock (void)
387 if (gdk_threads_mutex)
388 g_mutex_lock (gdk_threads_mutex);
392 gdk_threads_impl_unlock (void)
394 if (gdk_threads_mutex)
395 g_mutex_unlock (gdk_threads_mutex);
401 * Initializes GDK so that it can be used from multiple threads
402 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
403 * g_thread_init() must be called previous to this function.
405 * This call must be made before any use of the main loop from
406 * GTK+; to be safe, call it before gtk_init().
409 gdk_threads_init (void)
411 if (!g_thread_supported ())
412 g_error ("g_thread_init() must be called before gdk_threads_init()");
414 gdk_threads_mutex = g_mutex_new ();
415 if (!gdk_threads_lock)
416 gdk_threads_lock = gdk_threads_impl_lock;
417 if (!gdk_threads_unlock)
418 gdk_threads_unlock = gdk_threads_impl_unlock;
422 * gdk_threads_set_lock_functions:
423 * @enter_fn: function called to guard GDK
424 * @leave_fn: function called to release the guard
426 * Allows the application to replace the standard method that
427 * GDK uses to protect its data structures. Normally, GDK
428 * creates a single #GMutex that is locked by gdk_threads_enter(),
429 * and released by gdk_threads_leave(); using this function an
430 * application provides, instead, a function @enter_fn that is
431 * called by gdk_threads_enter() and a function @leave_fn that is
432 * called by gdk_threads_leave().
434 * The functions must provide at least same locking functionality
435 * as the default implementation, but can also do extra application
436 * specific processing.
438 * As an example, consider an application that has its own recursive
439 * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
440 * the GTK+ lock when entering a recursive main loop, the application
441 * must temporarily release its lock as well.
443 * Most threaded GTK+ apps won't need to use this method.
445 * This method must be called before gdk_threads_init(), and cannot
446 * be called multiple times.
451 gdk_threads_set_lock_functions (GCallback enter_fn,
454 g_return_if_fail (gdk_threads_lock == NULL &&
455 gdk_threads_unlock == NULL);
457 gdk_threads_lock = enter_fn;
458 gdk_threads_unlock = leave_fn;
462 gdk_threads_dispatch (gpointer data)
464 GdkThreadsDispatch *dispatch = data;
465 gboolean ret = FALSE;
467 GDK_THREADS_ENTER ();
469 if (!g_source_is_destroyed (g_main_current_source ()))
470 ret = dispatch->func (dispatch->data);
472 GDK_THREADS_LEAVE ();
478 gdk_threads_dispatch_free (gpointer data)
480 GdkThreadsDispatch *dispatch = data;
482 if (dispatch->destroy && dispatch->data)
483 dispatch->destroy (dispatch->data);
485 g_slice_free (GdkThreadsDispatch, data);
490 * gdk_threads_add_idle_full:
491 * @priority: the priority of the idle source. Typically this will be in the
492 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
493 * @function: function to call
494 * @data: data to pass to @function
495 * @notify: function to call when the idle is removed, or %NULL
497 * Adds a function to be called whenever there are no higher priority
498 * events pending. If the function returns %FALSE it is automatically
499 * removed from the list of event sources and will not be called again.
501 * This variant of g_idle_add_full() calls @function with the GDK lock
502 * held. It can be thought of a MT-safe version for GTK+ widgets for the
503 * following use case, where you have to worry about idle_callback()
504 * running in thread A and accessing @self after it has been finalized
509 * idle_callback (gpointer data)
511 * /* gdk_threads_enter(); would be needed for g_idle_add() */
513 * SomeWidget *self = data;
514 * /* do stuff with self */
518 * /* gdk_threads_leave(); would be needed for g_idle_add() */
523 * some_widget_do_stuff_later (SomeWidget *self)
525 * self->idle_id = gdk_threads_add_idle (idle_callback, self)
526 * /* using g_idle_add() here would require thread protection in the callback */
530 * some_widget_finalize (GObject *object)
532 * SomeWidget *self = SOME_WIDGET (object);
534 * g_source_remove (self->idle_id);
535 * G_OBJECT_CLASS (parent_class)->finalize (object);
539 * Return value: the ID (greater than 0) of the event source.
544 gdk_threads_add_idle_full (gint priority,
545 GSourceFunc function,
547 GDestroyNotify notify)
549 GdkThreadsDispatch *dispatch;
551 g_return_val_if_fail (function != NULL, 0);
553 dispatch = g_slice_new (GdkThreadsDispatch);
554 dispatch->func = function;
555 dispatch->data = data;
556 dispatch->destroy = notify;
558 return g_idle_add_full (priority,
559 gdk_threads_dispatch,
561 gdk_threads_dispatch_free);
565 * gdk_threads_add_idle:
566 * @function: function to call
567 * @data: data to pass to @function
569 * A wrapper for the common usage of gdk_threads_add_idle_full()
570 * assigning the default priority, #G_PRIORITY_DEFAULT_IDLE.
572 * See gdk_threads_add_idle_full().
574 * Return value: the ID (greater than 0) of the event source.
579 gdk_threads_add_idle (GSourceFunc function,
582 return gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
583 function, data, NULL);
588 * gdk_threads_add_timeout_full:
589 * @priority: the priority of the timeout source. Typically this will be in the
590 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
591 * @interval: the time between calls to the function, in milliseconds
592 * (1/1000ths of a second)
593 * @function: function to call
594 * @data: data to pass to @function
595 * @notify: function to call when the timeout is removed, or %NULL
597 * Sets a function to be called at regular intervals holding the GDK lock,
598 * with the given priority. The function is called repeatedly until it
599 * returns %FALSE, at which point the timeout is automatically destroyed
600 * and the function will not be called again. The @notify function is
601 * called when the timeout is destroyed. The first call to the
602 * function will be at the end of the first @interval.
604 * Note that timeout functions may be delayed, due to the processing of other
605 * event sources. Thus they should not be relied on for precise timing.
606 * After each call to the timeout function, the time of the next
607 * timeout is recalculated based on the current time and the given interval
608 * (it does not try to 'catch up' time lost in delays).
610 * This variant of g_timeout_add_full() can be thought of a MT-safe version
611 * for GTK+ widgets for the following use case:
614 * static gboolean timeout_callback (gpointer data)
616 * SomeWidget *self = data;
618 * /* do stuff with self */
620 * self->timeout_id = 0;
625 * static void some_widget_do_stuff_later (SomeWidget *self)
627 * self->timeout_id = g_timeout_add (timeout_callback, self)
630 * static void some_widget_finalize (GObject *object)
632 * SomeWidget *self = SOME_WIDGET (object);
634 * if (self->timeout_id)
635 * g_source_remove (self->timeout_id);
637 * G_OBJECT_CLASS (parent_class)->finalize (object);
641 * Return value: the ID (greater than 0) of the event source.
646 gdk_threads_add_timeout_full (gint priority,
648 GSourceFunc function,
650 GDestroyNotify notify)
652 GdkThreadsDispatch *dispatch;
654 g_return_val_if_fail (function != NULL, 0);
656 dispatch = g_slice_new (GdkThreadsDispatch);
657 dispatch->func = function;
658 dispatch->data = data;
659 dispatch->destroy = notify;
661 return g_timeout_add_full (priority,
663 gdk_threads_dispatch,
665 gdk_threads_dispatch_free);
669 * gdk_threads_add_timeout:
670 * @interval: the time between calls to the function, in milliseconds
671 * (1/1000ths of a second)
672 * @function: function to call
673 * @data: data to pass to @function
675 * A wrapper for the common usage of gdk_threads_add_timeout_full()
676 * assigning the default priority, #G_PRIORITY_DEFAULT.
678 * See gdk_threads_add_timeout_full().
680 * Return value: the ID (greater than 0) of the event source.
685 gdk_threads_add_timeout (guint interval,
686 GSourceFunc function,
689 return gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
690 interval, function, data, NULL);
694 G_CONST_RETURN char *
695 gdk_get_program_class (void)
697 return gdk_progclass;
701 gdk_set_program_class (const char *program_class)
703 g_free (gdk_progclass);
705 gdk_progclass = g_strdup (program_class);
709 #include "gdkaliasdef.c"