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;
40 typedef struct _GdkErrorTrap GdkErrorTrap;
54 /* Private variable declarations
56 static int gdk_initialized = 0; /* 1 if the library is initialized,
60 static GSList *gdk_error_traps = NULL; /* List of error traps */
61 static GSList *gdk_error_trap_free_list = NULL; /* Free list */
63 static gchar *gdk_progclass = NULL;
66 static const GDebugKey gdk_debug_keys[] = {
67 {"events", GDK_DEBUG_EVENTS},
68 {"misc", GDK_DEBUG_MISC},
69 {"dnd", GDK_DEBUG_DND},
70 {"xim", GDK_DEBUG_XIM}
73 static const int gdk_ndebug_keys = sizeof(gdk_debug_keys)/sizeof(GDebugKey);
75 #endif /* G_ENABLE_DEBUG */
78 gdk_arg_context_new (gpointer cb_data)
80 GdkArgContext *result = g_new (GdkArgContext, 1);
81 result->tables = g_ptr_array_new ();
82 result->cb_data = cb_data;
88 gdk_arg_context_destroy (GdkArgContext *context)
90 g_ptr_array_free (context->tables, TRUE);
95 gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
97 g_ptr_array_add (context->tables, table);
101 gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
105 /* Save a copy of the original argc and argv */
108 for (i = 1; i < *argc; i++)
112 if (!(*argv)[i][0] == '-' && (*argv)[i][1] == '-')
115 arg = (*argv)[i] + 2;
117 /* '--' terminates list of arguments */
124 for (j = 0; j < context->tables->len; j++)
126 GdkArgDesc *table = context->tables->pdata[j];
127 for (k = 0; table[k].name; k++)
129 switch (table[k].type)
132 case GDK_ARG_CALLBACK:
135 int len = strlen (table[k].name);
137 if (strncmp (arg, table[k].name, len) == 0 &&
138 (arg[len] == '=' || argc[len] == 0))
145 value = arg + len + 1;
146 else if (i < *argc - 1)
148 value = (*argv)[i + 1];
155 switch (table[k].type)
158 *(gchar **)table[k].location = g_strdup (value);
161 *(gint *)table[k].location = atoi (value);
163 case GDK_ARG_CALLBACK:
164 (*table[k].callback)(table[k].name, value, context->cb_data);
176 if (strcmp (arg, table[k].name) == 0)
180 *(gboolean *)table[k].location = (table[k].type == GDK_ARG_BOOL) ? TRUE : FALSE;
190 for (i = 1; i < *argc; i++)
192 for (k = i; k < *argc; k++)
193 if ((*argv)[k] != NULL)
199 for (j = i + k; j < *argc; j++)
200 (*argv)[j-k] = (*argv)[j];
207 #ifdef G_ENABLE_DEBUG
209 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
211 _gdk_debug_flags |= g_parse_debug_string (value,
212 (GDebugKey *) gdk_debug_keys,
217 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
219 _gdk_debug_flags &= ~g_parse_debug_string (value,
220 (GDebugKey *) gdk_debug_keys,
223 #endif /* G_ENABLE_DEBUG */
226 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data)
228 gdk_set_program_class (value);
232 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data)
234 g_set_prgname (value);
237 static GdkArgDesc gdk_args[] = {
238 { "class" , GDK_ARG_STRING, NULL, gdk_arg_class_cb },
239 { "name", GDK_ARG_STRING, NULL, gdk_arg_name_cb },
240 #ifdef G_ENABLE_DEBUG
241 { "gdk-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_debug_cb },
242 { "gdk-no-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_no_debug_cb },
243 #endif /* G_ENABLE_DEBUG */
248 *--------------------------------------------------------------
251 * Initialize the library for use.
254 * "argc" is the number of arguments.
255 * "argv" is an array of strings.
258 * "argc" and "argv" are modified to reflect any arguments
259 * which were not handled. (Such arguments should either
260 * be handled by the application or dismissed). If initialization
261 * fails, returns FALSE, otherwise TRUE.
264 * The library is initialized.
266 *--------------------------------------------------------------
270 gdk_init_check (int *argc,
273 gchar **argv_orig = NULL;
275 GdkArgContext *arg_context;
286 argv_orig = g_malloc ((argc_orig + 1) * sizeof (char*));
287 for (i = 0; i < argc_orig; i++)
288 argv_orig[i] = g_strdup ((*argv)[i]);
289 argv_orig[argc_orig] = NULL;
295 d = strrchr((*argv)[0], G_DIR_SEPARATOR);
297 g_set_prgname (d + 1);
299 g_set_prgname ((*argv)[0]);
304 #ifdef G_ENABLE_DEBUG
306 gchar *debug_string = getenv("GDK_DEBUG");
307 if (debug_string != NULL)
308 _gdk_debug_flags = g_parse_debug_string (debug_string,
309 (GDebugKey *) gdk_debug_keys,
312 #endif /* G_ENABLE_DEBUG */
314 arg_context = gdk_arg_context_new (NULL);
315 gdk_arg_context_add_table (arg_context, gdk_args);
316 gdk_arg_context_add_table (arg_context, _gdk_windowing_args);
317 gdk_arg_context_parse (arg_context, argc, argv);
318 gdk_arg_context_destroy (arg_context);
320 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
324 result = _gdk_windowing_init_check (argc_orig, argv_orig);
326 for (i = 0; i < argc_orig; i++)
327 g_free(argv_orig[i]);
334 _gdk_windowing_window_init ();
335 _gdk_windowing_image_init ();
346 gdk_init (int *argc, char ***argv)
348 if (!gdk_init_check (argc, argv))
350 g_warning ("cannot open display: %s", gdk_get_display ());
356 *--------------------------------------------------------------
359 * Restores the library to an un-itialized state and exits
360 * the program using the "exit" system call.
363 * "errorcode" is the error value to pass to "exit".
366 * Allocated structures are freed and the program exits
371 *--------------------------------------------------------------
375 gdk_exit (gint errorcode)
377 /* de-initialisation is done by the gdk_exit_funct(),
378 no need to do this here (Alex J.) */
384 /* This is disabled, but the code isn't removed, because we might
385 * want to have some sort of explicit way to shut down GDK cleanly
386 * at some point in the future.
390 *--------------------------------------------------------------
393 * This is the "atexit" function that makes sure the
394 * library gets a chance to cleanup.
401 * The library is un-initialized and the program exits.
403 *--------------------------------------------------------------
409 static gboolean in_gdk_exit_func = FALSE;
411 /* This is to avoid an infinite loop if a program segfaults in
412 an atexit() handler (and yes, it does happen, especially if a program
413 has trounced over memory too badly for even g_message to work) */
414 if (in_gdk_exit_func == TRUE)
416 in_gdk_exit_func = TRUE;
423 _gdk_windowing_exit ();
431 /*************************************************************
432 * gdk_error_trap_push:
433 * Push an error trap. X errors will be trapped until
434 * the corresponding gdk_error_pop(), which will return
435 * the error code, if any.
439 *************************************************************/
442 gdk_error_trap_push (void)
447 if (gdk_error_trap_free_list)
449 node = gdk_error_trap_free_list;
450 gdk_error_trap_free_list = gdk_error_trap_free_list->next;
454 node = g_slist_alloc ();
455 node->data = g_new (GdkErrorTrap, 1);
458 node->next = gdk_error_traps;
459 gdk_error_traps = node;
462 trap->error_code = _gdk_error_code;
463 trap->error_warnings = _gdk_error_warnings;
466 _gdk_error_warnings = 0;
469 /*************************************************************
470 * gdk_error_trap_pop:
471 * Pop an error trap added with gdk_error_push()
475 * 0, if no error occured, otherwise the error code.
476 *************************************************************/
479 gdk_error_trap_pop (void)
485 g_return_val_if_fail (gdk_error_traps != NULL, 0);
487 node = gdk_error_traps;
488 gdk_error_traps = gdk_error_traps->next;
490 node->next = gdk_error_trap_free_list;
491 gdk_error_trap_free_list = node;
493 result = _gdk_error_code;
496 _gdk_error_code = trap->error_code;
497 _gdk_error_warnings = trap->error_warnings;
505 GDK_THREADS_ENTER ();
511 GDK_THREADS_LEAVE ();
517 * Initializes GDK so that it can be used from multiple threads
518 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
519 * g_thread_init() must be called previous to this function.
521 * This call must be made before any use of the main loop from
522 * GTK+; to be safe, call it before gtk_init().
527 if (!g_thread_supported ())
528 g_error ("g_thread_init() must be called before gdk_threads_init()");
530 gdk_threads_mutex = g_mutex_new ();
533 G_CONST_RETURN char *
534 gdk_get_program_class (void)
536 if (gdk_progclass == NULL)
538 gdk_progclass = g_strdup (g_get_prgname ());
539 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
542 return gdk_progclass;
546 gdk_set_program_class (const char *program_class)
549 g_free (gdk_progclass);
551 gdk_progclass = g_strdup (program_class);