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 /* 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},
73 {"draw", GDK_DEBUG_DRAW}
76 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
78 #endif /* G_ENABLE_DEBUG */
82 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
84 _gdk_debug_flags |= g_parse_debug_string (value,
85 (GDebugKey *) gdk_debug_keys,
90 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
92 _gdk_debug_flags &= ~g_parse_debug_string (value,
93 (GDebugKey *) gdk_debug_keys,
96 #endif /* G_ENABLE_DEBUG */
99 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
101 gdk_set_program_class (value);
107 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
109 g_set_prgname (value);
114 static GOptionEntry gdk_args[] = {
115 { "class", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
116 /* Description of --class=CLASS in --help output */ N_("Program class as used by the window manager"),
117 /* Placeholder in --class=CLASS in --help output */ N_("CLASS") },
118 { "name", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
119 /* Description of --name=NAME in --help output */ N_("Program name as used by the window manager"),
120 /* Placeholder in --name=NAME in --help output */ N_("NAME") },
121 { "display", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &_gdk_display_name,
122 /* Description of --display=DISPLAY in --help output */ N_("X display to use"),
123 /* Placeholder in --display=DISPLAY in --help output */ N_("DISPLAY") },
124 { "screen", 0, 0, G_OPTION_ARG_INT, &_gdk_screen_number,
125 /* Description of --screen=SCREEN in --help output */ N_("X screen to use"),
126 /* Placeholder in --screen=SCREEN in --help output */ N_("SCREEN") },
127 #ifdef G_ENABLE_DEBUG
128 { "gdk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,
129 /* Description of --gdk-debug=FLAGS in --help output */ N_("Gdk debugging flags to set"),
130 /* Placeholder in --gdk-debug=FLAGS in --help output */ N_("FLAGS") },
131 { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb,
132 /* Description of --gdk-no-debug=FLAGS in --help output */ N_("Gdk debugging flags to unset"),
133 /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
139 * gdk_add_option_entries_libgtk_only:
140 * @group: An option group.
142 * Appends gdk option entries to the passed in option group. This is
143 * not public API and must not be used by applications.
146 gdk_add_option_entries_libgtk_only (GOptionGroup *group)
148 g_option_group_add_entries (group, gdk_args);
149 g_option_group_add_entries (group, _gdk_windowing_args);
153 gdk_pre_parse_libgtk_only (void)
155 gdk_initialized = TRUE;
157 /* We set the fallback program class here, rather than lazily in
158 * gdk_get_program_class, since we don't want -name to override it.
160 gdk_progclass = g_strdup (g_get_prgname ());
161 if (gdk_progclass && gdk_progclass[0])
162 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
164 #ifdef G_ENABLE_DEBUG
166 gchar *debug_string = getenv("GDK_DEBUG");
167 if (debug_string != NULL)
168 _gdk_debug_flags = g_parse_debug_string (debug_string,
169 (GDebugKey *) gdk_debug_keys,
172 #endif /* G_ENABLE_DEBUG */
176 /* Do any setup particular to the windowing system
178 _gdk_windowing_init ();
184 * @argc: the number of command line arguments.
185 * @argv: the array of command line arguments.
187 * Parse command line arguments, and store for future
188 * use by calls to gdk_display_open().
190 * Any arguments used by GDK are removed from the array and @argc and @argv are
191 * updated accordingly.
193 * You shouldn't call this function explicitely if you are using
194 * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
199 gdk_parse_args (int *argc,
202 GOptionContext *option_context;
203 GOptionGroup *option_group;
204 GError *error = NULL;
209 gdk_pre_parse_libgtk_only ();
211 option_context = g_option_context_new (NULL);
212 g_option_context_set_ignore_unknown_options (option_context, TRUE);
213 g_option_context_set_help_enabled (option_context, FALSE);
214 option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
215 g_option_context_set_main_group (option_context, option_group);
217 g_option_group_add_entries (option_group, gdk_args);
218 g_option_group_add_entries (option_group, _gdk_windowing_args);
220 if (!g_option_context_parse (option_context, argc, argv, &error))
222 g_warning ("%s", error->message);
223 g_error_free (error);
225 g_option_context_free (option_context);
227 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
231 * gdk_get_display_arg_name:
233 * Gets the display name specified in the command line arguments passed
234 * to gdk_init() or gdk_parse_args(), if any.
236 * Returns: the display name, if specified explicitely, otherwise %NULL
237 * this string is owned by GTK+ and must not be modified or freed.
241 G_CONST_RETURN gchar *
242 gdk_get_display_arg_name (void)
244 if (!_gdk_display_arg_name)
246 if (_gdk_screen_number >= 0)
247 _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
249 _gdk_display_arg_name = g_strdup (_gdk_display_name);
252 return _gdk_display_arg_name;
256 * gdk_display_open_default_libgtk_only:
258 * Opens the default display specified by command line arguments or
259 * environment variables, sets it as the default display, and returns
260 * it. gdk_parse_args must have been called first. If the default
261 * display has previously been set, simply returns that. An internal
262 * function that should not be used by applications.
264 * Return value: the default display, if it could be opened,
268 gdk_display_open_default_libgtk_only (void)
272 g_return_val_if_fail (gdk_initialized, NULL);
274 display = gdk_display_get_default ();
278 display = gdk_display_open (gdk_get_display_arg_name ());
280 if (!display && _gdk_screen_number >= 0)
282 g_free (_gdk_display_arg_name);
283 _gdk_display_arg_name = g_strdup (_gdk_display_name);
285 display = gdk_display_open (_gdk_display_name);
289 gdk_display_manager_set_default_display (gdk_display_manager_get (),
296 *--------------------------------------------------------------
299 * Initialize the library for use.
302 * "argc" is the number of arguments.
303 * "argv" is an array of strings.
306 * "argc" and "argv" are modified to reflect any arguments
307 * which were not handled. (Such arguments should either
308 * be handled by the application or dismissed). If initialization
309 * fails, returns FALSE, otherwise TRUE.
312 * The library is initialized.
314 *--------------------------------------------------------------
318 gdk_init_check (int *argc,
321 gdk_parse_args (argc, argv);
323 return gdk_display_open_default_libgtk_only () != NULL;
327 gdk_init (int *argc, char ***argv)
329 if (!gdk_init_check (argc, argv))
331 g_warning ("cannot open display: %s", gdk_get_display_arg_name ());
337 *--------------------------------------------------------------
340 * Restores the library to an un-itialized state and exits
341 * the program using the "exit" system call.
344 * "errorcode" is the error value to pass to "exit".
347 * Allocated structures are freed and the program exits
352 *--------------------------------------------------------------
356 gdk_exit (gint errorcode)
362 gdk_threads_enter (void)
364 GDK_THREADS_ENTER ();
368 gdk_threads_leave (void)
370 GDK_THREADS_LEAVE ();
374 gdk_threads_impl_lock (void)
376 if (gdk_threads_mutex)
377 g_mutex_lock (gdk_threads_mutex);
381 gdk_threads_impl_unlock (void)
383 if (gdk_threads_mutex)
384 g_mutex_unlock (gdk_threads_mutex);
390 * Initializes GDK so that it can be used from multiple threads
391 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
392 * g_thread_init() must be called previous to this function.
394 * This call must be made before any use of the main loop from
395 * GTK+; to be safe, call it before gtk_init().
398 gdk_threads_init (void)
400 if (!g_thread_supported ())
401 g_error ("g_thread_init() must be called before gdk_threads_init()");
403 gdk_threads_mutex = g_mutex_new ();
404 if (!gdk_threads_lock)
405 gdk_threads_lock = gdk_threads_impl_lock;
406 if (!gdk_threads_unlock)
407 gdk_threads_unlock = gdk_threads_impl_unlock;
411 * gdk_threads_set_lock_functions:
412 * @enter_fn: function called to guard GDK
413 * @leave_fn: function called to release the guard
415 * Allows the application to replace the standard method that
416 * GDK uses to protect its data structures. Normally, GDK
417 * creates a single #GMutex that is locked by gdk_threads_enter(),
418 * and released by gdk_threads_leave(); using this function an
419 * application provides, instead, a function @enter_fn that is
420 * called by gdk_threads_enter() and a function @leave_fn that is
421 * called by gdk_threads_leave().
423 * The functions must provide at least same locking functionality
424 * as the default implementation, but can also do extra application
425 * specific processing.
427 * As an example, consider an application that has its own recursive
428 * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
429 * the GTK+ lock when entering a recursive main loop, the application
430 * must temporarily release its lock as well.
432 * Most threaded GTK+ apps won't need to use this method.
434 * This method must be called before gdk_threads_init(), and cannot
435 * be called multiple times.
440 gdk_threads_set_lock_functions (GCallback enter_fn,
443 g_return_if_fail (gdk_threads_lock == NULL &&
444 gdk_threads_unlock == NULL);
446 gdk_threads_lock = enter_fn;
447 gdk_threads_unlock = leave_fn;
450 G_CONST_RETURN char *
451 gdk_get_program_class (void)
453 return gdk_progclass;
457 gdk_set_program_class (const char *program_class)
460 g_free (gdk_progclass);
462 gdk_progclass = g_strdup (program_class);
466 #include "gdkaliasdef.c"