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},
74 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
76 #endif /* G_ENABLE_DEBUG */
78 static GdkArgContext *
79 gdk_arg_context_new (gpointer cb_data)
81 GdkArgContext *result = g_new (GdkArgContext, 1);
82 result->tables = g_ptr_array_new ();
83 result->cb_data = cb_data;
89 gdk_arg_context_destroy (GdkArgContext *context)
91 g_ptr_array_free (context->tables, TRUE);
96 gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
98 g_ptr_array_add (context->tables, table);
102 gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
106 /* Save a copy of the original argc and 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 },
243 #ifdef G_ENABLE_DEBUG
244 { "gdk-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_debug_cb },
245 { "gdk-no-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_no_debug_cb },
246 #endif /* G_ENABLE_DEBUG */
252 * _gdk_get_command_line_args:
253 * @argv: location to store argv pointer
256 * Retrieve the command line arguments passed to gdk_init().
257 * The returned argv pointer points to static storage ; no
261 _gdk_get_command_line_args (int *argc,
270 * @argc: the number of command line arguments.
271 * @argv: the array of command line arguments.
273 * Parse command line arguments, and store for future
274 * use by calls to gdk_display_open().
276 * Any arguments used by GDK are removed from the array and @argc and @argv are
277 * updated accordingly.
279 * You shouldn't call this function explicitely if you are using
280 * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
283 gdk_parse_args (int *argc,
286 GdkArgContext *arg_context;
292 gdk_initialized = TRUE;
298 gdk_argv = g_malloc ((gdk_argc + 1) * sizeof (char*));
299 for (i = 0; i < gdk_argc; i++)
300 gdk_argv[i] = g_strdup ((*argv)[i]);
301 gdk_argv[gdk_argc] = NULL;
307 d = strrchr((*argv)[0], G_DIR_SEPARATOR);
309 g_set_prgname (d + 1);
311 g_set_prgname ((*argv)[0]);
316 g_set_prgname ("<unknown>");
319 /* We set the fallback program class here, rather than lazily in
320 * gdk_get_program_class, since we don't want -name to override it.
322 gdk_progclass = g_strdup (g_get_prgname ());
323 if (gdk_progclass[0])
324 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
326 #ifdef G_ENABLE_DEBUG
328 gchar *debug_string = getenv("GDK_DEBUG");
329 if (debug_string != NULL)
330 _gdk_debug_flags = g_parse_debug_string (debug_string,
331 (GDebugKey *) gdk_debug_keys,
334 #endif /* G_ENABLE_DEBUG */
336 arg_context = gdk_arg_context_new (NULL);
337 gdk_arg_context_add_table (arg_context, gdk_args);
338 gdk_arg_context_add_table (arg_context, _gdk_windowing_args);
339 gdk_arg_context_parse (arg_context, argc, argv);
340 gdk_arg_context_destroy (arg_context);
342 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
346 /* Do any setup particular to the windowing system
348 _gdk_windowing_init ();
352 * gdk_get_display_arg_name:
354 * Gets the display name specified in the command line arguments passed
355 * to gdk_init() or gdk_parse_args(), if any.
357 * Returns: the display name, if specified explicitely, otherwise %NULL
360 gdk_get_display_arg_name (void)
362 return _gdk_display_name;
366 *--------------------------------------------------------------
369 * Initialize the library for use.
372 * "argc" is the number of arguments.
373 * "argv" is an array of strings.
376 * "argc" and "argv" are modified to reflect any arguments
377 * which were not handled. (Such arguments should either
378 * be handled by the application or dismissed). If initialization
379 * fails, returns FALSE, otherwise TRUE.
382 * The library is initialized.
384 *--------------------------------------------------------------
388 gdk_init_check (int *argc,
393 gdk_parse_args (argc, argv);
395 if (gdk_display_get_default ())
398 display = gdk_display_open (_gdk_display_name);
402 gdk_display_manager_set_default_display (gdk_display_manager_get (),
411 gdk_init (int *argc, char ***argv)
413 if (!gdk_init_check (argc, argv))
415 g_warning ("cannot open display: %s", gdk_get_display_arg_name ());
421 *--------------------------------------------------------------
424 * Restores the library to an un-itialized state and exits
425 * the program using the "exit" system call.
428 * "errorcode" is the error value to pass to "exit".
431 * Allocated structures are freed and the program exits
436 *--------------------------------------------------------------
440 gdk_exit (gint errorcode)
442 /* de-initialisation is done by the gdk_exit_funct(),
443 no need to do this here (Alex J.) */
449 /* This is disabled, but the code isn't removed, because we might
450 * want to have some sort of explicit way to shut down GDK cleanly
451 * at some point in the future.
455 *--------------------------------------------------------------
458 * This is the "atexit" function that makes sure the
459 * library gets a chance to cleanup.
466 * The library is un-initialized and the program exits.
468 *--------------------------------------------------------------
474 static gboolean in_gdk_exit_func = FALSE;
476 /* This is to avoid an infinite loop if a program segfaults in
477 an atexit() handler (and yes, it does happen, especially if a program
478 has trounced over memory too badly for even g_message to work) */
479 if (in_gdk_exit_func == TRUE)
481 in_gdk_exit_func = TRUE;
488 _gdk_windowing_exit ();
499 GDK_THREADS_ENTER ();
505 GDK_THREADS_LEAVE ();
511 * Initializes GDK so that it can be used from multiple threads
512 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
513 * g_thread_init() must be called previous to this function.
515 * This call must be made before any use of the main loop from
516 * GTK+; to be safe, call it before gtk_init().
521 if (!g_thread_supported ())
522 g_error ("g_thread_init() must be called before gdk_threads_init()");
524 gdk_threads_mutex = g_mutex_new ();
527 G_CONST_RETURN char *
528 gdk_get_program_class (void)
530 return gdk_progclass;
534 gdk_set_program_class (const char *program_class)
537 g_free (gdk_progclass);
539 gdk_progclass = g_strdup (program_class);