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;
55 * Private function declarations
58 GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
62 /* Private variable declarations
64 static int gdk_initialized = 0; /* 1 if the library is initialized,
68 static GSList *gdk_error_traps = NULL; /* List of error traps */
69 static GSList *gdk_error_trap_free_list = NULL; /* Free list */
72 static const GDebugKey gdk_debug_keys[] = {
73 {"events", GDK_DEBUG_EVENTS},
74 {"misc", GDK_DEBUG_MISC},
75 {"dnd", GDK_DEBUG_DND},
76 {"color-context", GDK_DEBUG_COLOR_CONTEXT},
77 {"xim", GDK_DEBUG_XIM}
80 static const int gdk_ndebug_keys = sizeof(gdk_debug_keys)/sizeof(GDebugKey);
82 #endif /* G_ENABLE_DEBUG */
85 gdk_arg_context_new (gpointer cb_data)
87 GdkArgContext *result = g_new (GdkArgContext, 1);
88 result->tables = g_ptr_array_new ();
89 result->cb_data = cb_data;
95 gdk_arg_context_destroy (GdkArgContext *context)
97 g_ptr_array_free (context->tables, TRUE);
102 gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
104 g_ptr_array_add (context->tables, table);
108 gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
112 /* Save a copy of the original argc and argv */
115 for (i = 1; i < *argc; i++)
119 if (!(*argv)[i][0] == '-' && (*argv)[i][1] == '-')
122 arg = (*argv)[i] + 2;
124 /* '--' terminates list of arguments */
131 for (j = 0; j < context->tables->len; j++)
133 GdkArgDesc *table = context->tables->pdata[j];
134 for (k = 0; table[k].name; k++)
136 switch (table[k].type)
139 case GDK_ARG_CALLBACK:
142 int len = strlen (table[k].name);
144 if (strncmp (arg, table[k].name, len) == 0 &&
145 (arg[len] == '=' || argc[len] == 0))
152 value = arg + len + 1;
153 else if (i < *argc - 1)
155 value = (*argv)[i + 1];
162 switch (table[k].type)
165 *(gchar **)table[k].location = g_strdup (value);
168 *(gint *)table[k].location = atoi (value);
170 case GDK_ARG_CALLBACK:
171 (*table[k].callback)(table[k].name, value, context->cb_data);
183 if (strcmp (arg, table[k].name) == 0)
187 *(gboolean *)table[k].location = (table[k].type == GDK_ARG_BOOL) ? TRUE : FALSE;
197 for (i = 1; i < *argc; i++)
199 for (k = i; k < *argc; k++)
200 if ((*argv)[k] != NULL)
206 for (j = i + k; j < *argc; j++)
207 (*argv)[j-k] = (*argv)[j];
214 #ifdef G_ENABLE_DEBUG
216 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
218 gdk_debug_flags |= g_parse_debug_string (value,
219 (GDebugKey *) gdk_debug_keys,
224 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
226 gdk_debug_flags &= ~g_parse_debug_string (value,
227 (GDebugKey *) gdk_debug_keys,
230 #endif /* G_ENABLE_DEBUG */
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 { "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;
282 if (g_thread_supported ())
283 gdk_threads_mutex = g_mutex_new ();
289 argv_orig = g_malloc ((argc_orig + 1) * sizeof (char*));
290 for (i = 0; i < argc_orig; i++)
291 argv_orig[i] = g_strdup ((*argv)[i]);
292 argv_orig[argc_orig] = NULL;
298 d = strrchr((*argv)[0], G_DIR_SEPARATOR);
300 g_set_prgname (d + 1);
302 g_set_prgname ((*argv)[0]);
307 #ifdef G_ENABLE_DEBUG
309 gchar *debug_string = getenv("GDK_DEBUG");
310 if (debug_string != NULL)
311 gdk_debug_flags = g_parse_debug_string (debug_string,
312 (GDebugKey *) gdk_debug_keys,
315 #endif /* G_ENABLE_DEBUG */
317 arg_context = gdk_arg_context_new (NULL);
318 gdk_arg_context_add_table (arg_context, gdk_args);
319 gdk_arg_context_add_table (arg_context, _gdk_windowing_args);
320 gdk_arg_context_parse (arg_context, argc, argv);
321 gdk_arg_context_destroy (arg_context);
323 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
327 result = _gdk_windowing_init_check (argc_orig, argv_orig);
329 for (i = 0; i < argc_orig; i++)
330 g_free(argv_orig[i]);
338 _gdk_windowing_window_init ();
339 _gdk_windowing_image_init ();
353 gdk_init (int *argc, char ***argv)
355 if (!gdk_init_check (argc, argv))
357 g_warning ("cannot open display: %s", gdk_get_display ());
363 *--------------------------------------------------------------
366 * Restores the library to an un-itialized state and exits
367 * the program using the "exit" system call.
370 * "errorcode" is the error value to pass to "exit".
373 * Allocated structures are freed and the program exits
378 *--------------------------------------------------------------
382 gdk_exit (gint errorcode)
384 /* de-initialisation is done by the gdk_exit_funct(),
385 no need to do this here (Alex J.) */
391 /* This is disabled, but the code isn't removed, because we might
392 * want to have some sort of explicit way to shut down GDK cleanly
393 * at some point in the future.
397 *--------------------------------------------------------------
400 * This is the "atexit" function that makes sure the
401 * library gets a chance to cleanup.
408 * The library is un-initialized and the program exits.
410 *--------------------------------------------------------------
416 static gboolean in_gdk_exit_func = FALSE;
418 /* This is to avoid an infinite loop if a program segfaults in
419 an atexit() handler (and yes, it does happen, especially if a program
420 has trounced over memory too badly for even g_message to work) */
421 if (in_gdk_exit_func == TRUE)
423 in_gdk_exit_func = TRUE;
435 gdk_key_repeat_restore ();
437 gdk_windowing_exit ();
445 /*************************************************************
446 * gdk_error_trap_push:
447 * Push an error trap. X errors will be trapped until
448 * the corresponding gdk_error_pop(), which will return
449 * the error code, if any.
453 *************************************************************/
456 gdk_error_trap_push (void)
461 if (gdk_error_trap_free_list)
463 node = gdk_error_trap_free_list;
464 gdk_error_trap_free_list = gdk_error_trap_free_list->next;
468 node = g_slist_alloc ();
469 node->data = g_new (GdkErrorTrap, 1);
472 node->next = gdk_error_traps;
473 gdk_error_traps = node;
476 trap->error_code = gdk_error_code;
477 trap->error_warnings = gdk_error_warnings;
480 gdk_error_warnings = 0;
483 /*************************************************************
484 * gdk_error_trap_pop:
485 * Pop an error trap added with gdk_error_push()
489 * 0, if no error occured, otherwise the error code.
490 *************************************************************/
493 gdk_error_trap_pop (void)
499 g_return_val_if_fail (gdk_error_traps != NULL, 0);
501 node = gdk_error_traps;
502 gdk_error_traps = gdk_error_traps->next;
504 node->next = gdk_error_trap_free_list;
505 gdk_error_trap_free_list = node;
507 result = gdk_error_code;
510 gdk_error_code = trap->error_code;
511 gdk_error_warnings = trap->error_warnings;
519 GDK_THREADS_ENTER ();
525 GDK_THREADS_LEAVE ();