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/.
34 #include "gdkinternals.h"
37 #ifndef HAVE_XCONVERTCASE
38 #include "gdkkeysyms.h"
41 typedef struct _GdkPredicate GdkPredicate;
49 /* Private variable declarations
51 static int gdk_initialized = 0; /* 1 if the library is initialized,
55 static gchar *gdk_progclass = NULL;
58 static const GDebugKey gdk_debug_keys[] = {
59 {"events", GDK_DEBUG_EVENTS},
60 {"misc", GDK_DEBUG_MISC},
61 {"dnd", GDK_DEBUG_DND},
62 {"xim", GDK_DEBUG_XIM},
63 {"nograbs", GDK_DEBUG_NOGRABS},
64 {"colormap", GDK_DEBUG_COLORMAP},
65 {"gdkrgb", GDK_DEBUG_GDKRGB},
67 {"pixmap", GDK_DEBUG_PIXMAP},
68 {"image", GDK_DEBUG_IMAGE},
69 {"input", GDK_DEBUG_INPUT},
70 {"cursor", GDK_DEBUG_CURSOR},
71 {"multihead", GDK_DEBUG_MULTIHEAD},
72 {"xinerama", GDK_DEBUG_XINERAMA}
75 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
77 #endif /* G_ENABLE_DEBUG */
81 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
83 _gdk_debug_flags |= g_parse_debug_string (value,
84 (GDebugKey *) gdk_debug_keys,
89 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
91 _gdk_debug_flags &= ~g_parse_debug_string (value,
92 (GDebugKey *) gdk_debug_keys,
95 #endif /* G_ENABLE_DEBUG */
98 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
100 gdk_set_program_class (value);
106 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
108 g_set_prgname (value);
113 static GOptionEntry gdk_args[] = {
114 { "class", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
115 /* Description of --class=CLASS in --help output */ N_("Program class as used by the window manager"),
116 /* Placeholder in --class=CLASS in --help output */ N_("CLASS") },
117 { "name", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
118 /* Description of --name=NAME in --help output */ N_("Program name as used by the window manager"),
119 /* Placeholder in --name=NAME in --help output */ N_("NAME") },
120 { "display", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &_gdk_display_name,
121 /* Description of --display=DISPLAY in --help output */ N_("X display to use"),
122 /* Placeholder in --display=DISPLAY in --help output */ N_("DISPLAY") },
123 { "screen", 0, 0, G_OPTION_ARG_INT, &_gdk_screen_number,
124 /* Description of --screen=SCREEN in --help output */ N_("X screen to use"),
125 /* Placeholder in --screen=SCREEN in --help output */ N_("SCREEN") },
126 #ifdef G_ENABLE_DEBUG
127 { "gdk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,
128 /* Description of --gdk-debug=FLAGS in --help output */ N_("Gdk debugging flags to set"),
129 /* Placeholder in --gdk-debug=FLAGS in --help output */ N_("FLAGS") },
130 { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb,
131 /* Description of --gdk-no-debug=FLAGS in --help output */ N_("Gdk debugging flags to unset"),
132 /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
138 * gdk_add_option_entries_libgtk_only:
139 * @group: An option group.
141 * Appends gdk option entries to the passed in option group. This is
142 * not public API and must not be used by applications.
145 gdk_add_option_entries_libgtk_only (GOptionGroup *group)
147 g_option_group_add_entries (group, gdk_args);
148 g_option_group_add_entries (group, _gdk_windowing_args);
152 gdk_pre_parse_libgtk_only (void)
154 gdk_initialized = TRUE;
156 /* We set the fallback program class here, rather than lazily in
157 * gdk_get_program_class, since we don't want -name to override it.
159 gdk_progclass = g_strdup (g_get_prgname ());
160 if (gdk_progclass && gdk_progclass[0])
161 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
163 #ifdef G_ENABLE_DEBUG
165 gchar *debug_string = getenv("GDK_DEBUG");
166 if (debug_string != NULL)
167 _gdk_debug_flags = g_parse_debug_string (debug_string,
168 (GDebugKey *) gdk_debug_keys,
171 #endif /* G_ENABLE_DEBUG */
175 /* Do any setup particular to the windowing system
177 _gdk_windowing_init ();
183 * @argc: the number of command line arguments.
184 * @argv: the array of command line arguments.
186 * Parse command line arguments, and store for future
187 * use by calls to gdk_display_open().
189 * Any arguments used by GDK are removed from the array and @argc and @argv are
190 * updated accordingly.
192 * You shouldn't call this function explicitely if you are using
193 * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
198 gdk_parse_args (int *argc,
201 GOptionContext *option_context;
202 GOptionGroup *option_group;
207 gdk_pre_parse_libgtk_only ();
209 option_context = g_option_context_new (NULL);
210 g_option_context_set_ignore_unknown_options (option_context, TRUE);
211 g_option_context_set_help_enabled (option_context, FALSE);
212 option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
213 g_option_context_set_main_group (option_context, option_group);
215 g_option_group_add_entries (option_group, gdk_args);
216 g_option_group_add_entries (option_group, _gdk_windowing_args);
218 g_option_context_parse (option_context, argc, argv, NULL);
219 g_option_context_free (option_context);
221 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
225 * gdk_get_display_arg_name:
227 * Gets the display name specified in the command line arguments passed
228 * to gdk_init() or gdk_parse_args(), if any.
230 * Returns: the display name, if specified explicitely, otherwise %NULL
231 * this string is owned by GTK+ and must not be modified or freed.
235 G_CONST_RETURN gchar *
236 gdk_get_display_arg_name (void)
238 if (!_gdk_display_arg_name)
240 if (_gdk_screen_number >= 0)
241 _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
243 _gdk_display_arg_name = g_strdup (_gdk_display_name);
246 return _gdk_display_arg_name;
250 * gdk_display_open_default_libgtk_only:
252 * Opens the default display specified by command line arguments or
253 * environment variables, sets it as the default display, and returns
254 * it. gdk_parse_args must have been called first. If the default
255 * display has previously been set, simply returns that. An internal
256 * function that should not be used by applications.
258 * Return value: the default display, if it could be opened,
262 gdk_display_open_default_libgtk_only (void)
266 g_return_val_if_fail (gdk_initialized, NULL);
268 display = gdk_display_get_default ();
272 display = gdk_display_open (gdk_get_display_arg_name ());
274 if (!display && _gdk_screen_number >= 0)
276 g_free (_gdk_display_arg_name);
277 _gdk_display_arg_name = g_strdup (_gdk_display_name);
279 display = gdk_display_open (_gdk_display_name);
283 gdk_display_manager_set_default_display (gdk_display_manager_get (),
290 *--------------------------------------------------------------
293 * Initialize the library for use.
296 * "argc" is the number of arguments.
297 * "argv" is an array of strings.
300 * "argc" and "argv" are modified to reflect any arguments
301 * which were not handled. (Such arguments should either
302 * be handled by the application or dismissed). If initialization
303 * fails, returns FALSE, otherwise TRUE.
306 * The library is initialized.
308 *--------------------------------------------------------------
312 gdk_init_check (int *argc,
315 gdk_parse_args (argc, argv);
317 return gdk_display_open_default_libgtk_only () != NULL;
321 gdk_init (int *argc, char ***argv)
323 if (!gdk_init_check (argc, argv))
325 g_warning ("cannot open display: %s", gdk_get_display_arg_name ());
331 *--------------------------------------------------------------
334 * Restores the library to an un-itialized state and exits
335 * the program using the "exit" system call.
338 * "errorcode" is the error value to pass to "exit".
341 * Allocated structures are freed and the program exits
346 *--------------------------------------------------------------
350 gdk_exit (gint errorcode)
356 gdk_threads_enter (void)
358 GDK_THREADS_ENTER ();
362 gdk_threads_leave (void)
364 GDK_THREADS_LEAVE ();
368 gdk_threads_impl_lock (void)
370 if (gdk_threads_mutex)
371 g_mutex_lock (gdk_threads_mutex);
375 gdk_threads_impl_unlock (void)
377 if (gdk_threads_mutex)
378 g_mutex_unlock (gdk_threads_mutex);
384 * Initializes GDK so that it can be used from multiple threads
385 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
386 * g_thread_init() must be called previous to this function.
388 * This call must be made before any use of the main loop from
389 * GTK+; to be safe, call it before gtk_init().
392 gdk_threads_init (void)
394 if (!g_thread_supported ())
395 g_error ("g_thread_init() must be called before gdk_threads_init()");
397 gdk_threads_mutex = g_mutex_new ();
398 if (!gdk_threads_lock)
399 gdk_threads_lock = gdk_threads_impl_lock;
400 if (!gdk_threads_unlock)
401 gdk_threads_unlock = gdk_threads_impl_unlock;
405 * gdk_threads_set_lock_functions:
406 * @enter_fn: function called to guard GDK
407 * @leave_fn: function called to release the guard
409 * Allows the application to replace the standard method that
410 * GDK uses to protect its data structures. Normally, GDK
411 * creates a single #GMutex that is locked by gdk_threads_enter(),
412 * and released by gdk_threads_leave(); using this function an
413 * application provides, instead, a function @enter_fn that is
414 * called by gdk_threads_enter() and a function @leave_fn that is
415 * called by gdk_threads_leave().
417 * The functions must provide at least same locking functionality
418 * as the default implementation, but can also do extra application
419 * specific processing.
421 * As an example, consider an application that has its own recursive
422 * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
423 * the GTK+ lock when entering a recursive main loop, the application
424 * must temporarily release its lock as well.
426 * Most threaded GTK+ apps won't need to use this method.
428 * This method must be called before gdk_threads_init(), and cannot
429 * be called multiple times.
434 gdk_threads_set_lock_functions (GCallback enter_fn,
437 g_return_if_fail (gdk_threads_lock == NULL &&
438 gdk_threads_unlock == NULL);
440 gdk_threads_lock = enter_fn;
441 gdk_threads_unlock = leave_fn;
444 G_CONST_RETURN char *
445 gdk_get_program_class (void)
447 return gdk_progclass;
451 gdk_set_program_class (const char *program_class)
454 g_free (gdk_progclass);
456 gdk_progclass = g_strdup (program_class);