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"
35 #ifndef HAVE_XCONVERTCASE
36 #include "gdkkeysyms.h"
39 typedef struct _GdkPredicate GdkPredicate;
47 /* Private variable declarations
49 static int gdk_initialized = 0; /* 1 if the library is initialized,
53 static gchar *gdk_progclass = NULL;
54 static gint gdk_argc = 0;
55 static gchar **gdk_argv = 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 */
79 static GdkArgContext *
80 gdk_arg_context_new (gpointer cb_data)
82 GdkArgContext *result = g_new (GdkArgContext, 1);
83 result->tables = g_ptr_array_new ();
84 result->cb_data = cb_data;
90 gdk_arg_context_destroy (GdkArgContext *context)
92 g_ptr_array_free (context->tables, TRUE);
97 gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
99 g_ptr_array_add (context->tables, table);
103 gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
109 for (i = 1; i < *argc; i++)
113 if (!((*argv)[i][0] == '-' && (*argv)[i][1] == '-'))
116 arg = (*argv)[i] + 2;
118 /* '--' terminates list of arguments */
125 for (j = 0; j < context->tables->len; j++)
127 GdkArgDesc *table = context->tables->pdata[j];
128 for (k = 0; table[k].name; k++)
130 switch (table[k].type)
133 case GDK_ARG_CALLBACK:
136 int len = strlen (table[k].name);
138 if (strncmp (arg, table[k].name, len) == 0 &&
139 (arg[len] == '=' || arg[len] == 0))
146 value = arg + len + 1;
147 else if (i < *argc - 1)
149 value = (*argv)[i + 1];
156 switch (table[k].type)
159 *(gchar **)table[k].location = g_strdup (value);
162 *(gint *)table[k].location = atoi (value);
164 case GDK_ARG_CALLBACK:
165 (*table[k].callback)(table[k].name, value, context->cb_data);
177 if (strcmp (arg, table[k].name) == 0)
181 *(gboolean *)table[k].location = (table[k].type == GDK_ARG_BOOL) ? TRUE : FALSE;
191 for (i = 1; i < *argc; i++)
193 for (k = i; k < *argc; k++)
194 if ((*argv)[k] != NULL)
200 for (j = i + k; j < *argc; j++)
201 (*argv)[j-k] = (*argv)[j];
208 #ifdef G_ENABLE_DEBUG
210 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
212 _gdk_debug_flags |= g_parse_debug_string (value,
213 (GDebugKey *) gdk_debug_keys,
218 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
220 _gdk_debug_flags &= ~g_parse_debug_string (value,
221 (GDebugKey *) gdk_debug_keys,
224 #endif /* G_ENABLE_DEBUG */
227 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data)
229 gdk_set_program_class (value);
233 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data)
235 g_set_prgname (value);
238 static GdkArgDesc gdk_args[] = {
239 { "class" , GDK_ARG_CALLBACK, NULL, gdk_arg_class_cb },
240 { "name", GDK_ARG_CALLBACK, NULL, gdk_arg_name_cb },
241 { "display", GDK_ARG_STRING, &_gdk_display_name, (GdkArgFunc)NULL },
242 { "screen", GDK_ARG_INT, &_gdk_screen_number, (GdkArgFunc)NULL },
244 #ifdef G_ENABLE_DEBUG
245 { "gdk-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_debug_cb },
246 { "gdk-no-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_no_debug_cb },
247 #endif /* G_ENABLE_DEBUG */
253 * _gdk_get_command_line_args:
254 * @argv: location to store argv pointer
257 * Retrieve the command line arguments passed to gdk_init().
258 * The returned argv pointer points to static storage ; no
262 _gdk_get_command_line_args (int *argc,
271 * @argc: the number of command line arguments.
272 * @argv: the array of command line arguments.
274 * Parse command line arguments, and store for future
275 * use by calls to gdk_display_open().
277 * Any arguments used by GDK are removed from the array and @argc and @argv are
278 * updated accordingly.
280 * You shouldn't call this function explicitely if you are using
281 * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
286 gdk_parse_args (int *argc,
289 GdkArgContext *arg_context;
295 gdk_initialized = TRUE;
297 /* Save a copy of the original argc and argv */
302 gdk_argv = g_malloc ((gdk_argc + 1) * sizeof (char*));
303 for (i = 0; i < gdk_argc; i++)
304 gdk_argv[i] = g_strdup ((*argv)[i]);
305 gdk_argv[gdk_argc] = NULL;
308 if (argc && argv && *argc > 0)
312 d = strrchr((*argv)[0], G_DIR_SEPARATOR);
314 g_set_prgname (d + 1);
316 g_set_prgname ((*argv)[0]);
320 g_set_prgname ("<unknown>");
323 /* We set the fallback program class here, rather than lazily in
324 * gdk_get_program_class, since we don't want -name to override it.
326 gdk_progclass = g_strdup (g_get_prgname ());
327 if (gdk_progclass[0])
328 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
330 #ifdef G_ENABLE_DEBUG
332 gchar *debug_string = getenv("GDK_DEBUG");
333 if (debug_string != NULL)
334 _gdk_debug_flags = g_parse_debug_string (debug_string,
335 (GDebugKey *) gdk_debug_keys,
338 #endif /* G_ENABLE_DEBUG */
340 arg_context = gdk_arg_context_new (NULL);
341 gdk_arg_context_add_table (arg_context, gdk_args);
342 gdk_arg_context_add_table (arg_context, _gdk_windowing_args);
343 gdk_arg_context_parse (arg_context, argc, argv);
344 gdk_arg_context_destroy (arg_context);
346 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
350 /* Do any setup particular to the windowing system
352 _gdk_windowing_init (argc, argv);
356 * gdk_get_display_arg_name:
358 * Gets the display name specified in the command line arguments passed
359 * to gdk_init() or gdk_parse_args(), if any.
361 * Returns: the display name, if specified explicitely, otherwise %NULL
362 * this string is owned by GTK+ and must not be modified or freed.
366 G_CONST_RETURN gchar *
367 gdk_get_display_arg_name (void)
369 if (!_gdk_display_arg_name)
371 if (_gdk_screen_number >= 0)
372 _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
374 _gdk_display_arg_name = g_strdup (_gdk_display_name);
377 return _gdk_display_arg_name;
381 * gdk_display_open_default_libgtk_only:
383 * Opens the default display specified by command line arguments or
384 * environment variables, sets it as the default display, and returns
385 * it. gdk_parse_args must have been called first. If the default
386 * display has previously been set, simply returns that. An internal
387 * function that should not be used by applications.
389 * Return value: the default display, if it could be opened,
393 gdk_display_open_default_libgtk_only (void)
397 g_return_val_if_fail (gdk_initialized, NULL);
399 display = gdk_display_get_default ();
403 display = gdk_display_open (gdk_get_display_arg_name ());
405 if (!display && _gdk_screen_number >= 0)
407 g_free (_gdk_display_arg_name);
408 _gdk_display_arg_name = g_strdup (_gdk_display_name);
410 display = gdk_display_open (_gdk_display_name);
414 gdk_display_manager_set_default_display (gdk_display_manager_get (),
421 *--------------------------------------------------------------
424 * Initialize the library for use.
427 * "argc" is the number of arguments.
428 * "argv" is an array of strings.
431 * "argc" and "argv" are modified to reflect any arguments
432 * which were not handled. (Such arguments should either
433 * be handled by the application or dismissed). If initialization
434 * fails, returns FALSE, otherwise TRUE.
437 * The library is initialized.
439 *--------------------------------------------------------------
443 gdk_init_check (int *argc,
446 gdk_parse_args (argc, argv);
448 return gdk_display_open_default_libgtk_only () != NULL;
452 gdk_init (int *argc, char ***argv)
454 if (!gdk_init_check (argc, argv))
456 g_warning ("cannot open display: %s", gdk_get_display_arg_name ());
462 *--------------------------------------------------------------
465 * Restores the library to an un-itialized state and exits
466 * the program using the "exit" system call.
469 * "errorcode" is the error value to pass to "exit".
472 * Allocated structures are freed and the program exits
477 *--------------------------------------------------------------
481 gdk_exit (gint errorcode)
489 GDK_THREADS_ENTER ();
495 GDK_THREADS_LEAVE ();
499 gdk_threads_impl_lock (void)
501 if (gdk_threads_mutex)
502 g_mutex_lock (gdk_threads_mutex);
506 gdk_threads_impl_unlock (void)
508 if (gdk_threads_mutex)
509 g_mutex_unlock (gdk_threads_mutex);
515 * Initializes GDK so that it can be used from multiple threads
516 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
517 * g_thread_init() must be called previous to this function.
519 * This call must be made before any use of the main loop from
520 * GTK+; to be safe, call it before gtk_init().
525 if (!g_thread_supported ())
526 g_error ("g_thread_init() must be called before gdk_threads_init()");
528 gdk_threads_mutex = g_mutex_new ();
529 if (!gdk_threads_lock)
530 gdk_threads_lock = gdk_threads_impl_lock;
531 if (!gdk_threads_unlock)
532 gdk_threads_unlock = gdk_threads_impl_unlock;
536 * gdk_threads_set_lock_functions:
537 * @enter_fn: function called to guard gtk+
538 * @leave_fn: function called to release the guard
540 * Allows the application to replace the standard method that
541 * GDK uses to protect its data structures. Normally, GDK
542 * creates a single #GMutex that is locked by gdk_threads_enter(),
543 * and released by gdk_threads_leave(); using this function an
544 * application provides, instead, a function @enter_fn that is
545 * called by gdk_threads_enter() and a function @leave_fn that is
546 * called by gdk_threads_leave().
548 * The functions must provide at least same locking functionality
549 * as the default implementation, but can also do extra application
550 * specific processing.
552 * As an example, consider an application that has its own recursive
553 * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
554 * the GTK+ lock when entering a recursive main loop, the application
555 * must temporarily release its lock as well.
557 * Most threaded GTK+ apps won't need to use this method.
559 * This method must be called before gdk_threads_init, and cannot
560 * be called multiple times.
563 gdk_threads_set_lock_functions (GCallback enter_fn,
566 g_return_if_fail (gdk_threads_lock == NULL &&
567 gdk_threads_unlock == NULL);
569 gdk_threads_lock = enter_fn;
570 gdk_threads_unlock = leave_fn;
573 G_CONST_RETURN char *
574 gdk_get_program_class (void)
576 return gdk_progclass;
580 gdk_set_program_class (const char *program_class)
583 g_free (gdk_progclass);
585 gdk_progclass = g_strdup (program_class);