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;
56 static const GDebugKey gdk_debug_keys[] = {
57 {"events", GDK_DEBUG_EVENTS},
58 {"misc", GDK_DEBUG_MISC},
59 {"dnd", GDK_DEBUG_DND},
60 {"xim", GDK_DEBUG_XIM},
61 {"nograbs", GDK_DEBUG_NOGRABS},
62 {"colormap", GDK_DEBUG_COLORMAP},
63 {"gdkrgb", GDK_DEBUG_GDKRGB},
65 {"pixmap", GDK_DEBUG_PIXMAP},
66 {"image", GDK_DEBUG_IMAGE},
67 {"input", GDK_DEBUG_INPUT},
68 {"cursor", GDK_DEBUG_CURSOR},
71 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
73 #endif /* G_ENABLE_DEBUG */
75 static GdkArgContext *
76 gdk_arg_context_new (gpointer cb_data)
78 GdkArgContext *result = g_new (GdkArgContext, 1);
79 result->tables = g_ptr_array_new ();
80 result->cb_data = cb_data;
86 gdk_arg_context_destroy (GdkArgContext *context)
88 g_ptr_array_free (context->tables, TRUE);
93 gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
95 g_ptr_array_add (context->tables, table);
99 gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
103 /* Save a copy of the original argc and argv */
106 for (i = 1; i < *argc; i++)
110 if (!(*argv)[i][0] == '-' && (*argv)[i][1] == '-')
113 arg = (*argv)[i] + 2;
115 /* '--' terminates list of arguments */
122 for (j = 0; j < context->tables->len; j++)
124 GdkArgDesc *table = context->tables->pdata[j];
125 for (k = 0; table[k].name; k++)
127 switch (table[k].type)
130 case GDK_ARG_CALLBACK:
133 int len = strlen (table[k].name);
135 if (strncmp (arg, table[k].name, len) == 0 &&
136 (arg[len] == '=' || arg[len] == 0))
143 value = arg + len + 1;
144 else if (i < *argc - 1)
146 value = (*argv)[i + 1];
153 switch (table[k].type)
156 *(gchar **)table[k].location = g_strdup (value);
159 *(gint *)table[k].location = atoi (value);
161 case GDK_ARG_CALLBACK:
162 (*table[k].callback)(table[k].name, value, context->cb_data);
174 if (strcmp (arg, table[k].name) == 0)
178 *(gboolean *)table[k].location = (table[k].type == GDK_ARG_BOOL) ? TRUE : FALSE;
188 for (i = 1; i < *argc; i++)
190 for (k = i; k < *argc; k++)
191 if ((*argv)[k] != NULL)
197 for (j = i + k; j < *argc; j++)
198 (*argv)[j-k] = (*argv)[j];
205 #ifdef G_ENABLE_DEBUG
207 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
209 _gdk_debug_flags |= g_parse_debug_string (value,
210 (GDebugKey *) gdk_debug_keys,
215 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
217 _gdk_debug_flags &= ~g_parse_debug_string (value,
218 (GDebugKey *) gdk_debug_keys,
221 #endif /* G_ENABLE_DEBUG */
224 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data)
226 gdk_set_program_class (value);
230 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data)
232 g_set_prgname (value);
235 static GdkArgDesc gdk_args[] = {
236 { "class" , GDK_ARG_CALLBACK, NULL, gdk_arg_class_cb },
237 { "name", GDK_ARG_CALLBACK, NULL, gdk_arg_name_cb },
238 #ifdef G_ENABLE_DEBUG
239 { "gdk-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_debug_cb },
240 { "gdk-no-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_no_debug_cb },
241 #endif /* G_ENABLE_DEBUG */
246 *--------------------------------------------------------------
249 * Initialize the library for use.
252 * "argc" is the number of arguments.
253 * "argv" is an array of strings.
256 * "argc" and "argv" are modified to reflect any arguments
257 * which were not handled. (Such arguments should either
258 * be handled by the application or dismissed). If initialization
259 * fails, returns FALSE, otherwise TRUE.
262 * The library is initialized.
264 *--------------------------------------------------------------
268 gdk_init_check (int *argc,
271 gchar **argv_orig = NULL;
273 GdkArgContext *arg_context;
284 argv_orig = g_malloc ((argc_orig + 1) * sizeof (char*));
285 for (i = 0; i < argc_orig; i++)
286 argv_orig[i] = g_strdup ((*argv)[i]);
287 argv_orig[argc_orig] = NULL;
293 d = strrchr((*argv)[0], G_DIR_SEPARATOR);
295 g_set_prgname (d + 1);
297 g_set_prgname ((*argv)[0]);
302 g_set_prgname ("<unknown>");
305 /* We set the fallback program class here, rather than lazily in
306 * gdk_get_program_class, since we don't want -name to override it.
308 gdk_progclass = g_strdup (g_get_prgname ());
309 if (gdk_progclass[0])
310 gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
312 #ifdef G_ENABLE_DEBUG
314 gchar *debug_string = getenv("GDK_DEBUG");
315 if (debug_string != NULL)
316 _gdk_debug_flags = g_parse_debug_string (debug_string,
317 (GDebugKey *) gdk_debug_keys,
320 #endif /* G_ENABLE_DEBUG */
322 arg_context = gdk_arg_context_new (NULL);
323 gdk_arg_context_add_table (arg_context, gdk_args);
324 gdk_arg_context_add_table (arg_context, _gdk_windowing_args);
325 gdk_arg_context_parse (arg_context, argc, argv);
326 gdk_arg_context_destroy (arg_context);
328 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
332 result = _gdk_windowing_init_check (argc_orig, argv_orig);
334 for (i = 0; i < argc_orig; i++)
335 g_free(argv_orig[i]);
342 _gdk_windowing_window_init ();
343 _gdk_windowing_image_init ();
354 gdk_init (int *argc, char ***argv)
356 if (!gdk_init_check (argc, argv))
358 g_warning ("cannot open display: %s", gdk_get_display ());
364 *--------------------------------------------------------------
367 * Restores the library to an un-itialized state and exits
368 * the program using the "exit" system call.
371 * "errorcode" is the error value to pass to "exit".
374 * Allocated structures are freed and the program exits
379 *--------------------------------------------------------------
383 gdk_exit (gint errorcode)
385 /* de-initialisation is done by the gdk_exit_funct(),
386 no need to do this here (Alex J.) */
392 /* This is disabled, but the code isn't removed, because we might
393 * want to have some sort of explicit way to shut down GDK cleanly
394 * at some point in the future.
398 *--------------------------------------------------------------
401 * This is the "atexit" function that makes sure the
402 * library gets a chance to cleanup.
409 * The library is un-initialized and the program exits.
411 *--------------------------------------------------------------
417 static gboolean in_gdk_exit_func = FALSE;
419 /* This is to avoid an infinite loop if a program segfaults in
420 an atexit() handler (and yes, it does happen, especially if a program
421 has trounced over memory too badly for even g_message to work) */
422 if (in_gdk_exit_func == TRUE)
424 in_gdk_exit_func = TRUE;
431 _gdk_windowing_exit ();
442 GDK_THREADS_ENTER ();
448 GDK_THREADS_LEAVE ();
454 * Initializes GDK so that it can be used from multiple threads
455 * in conjunction with gdk_threads_enter() and gdk_threads_leave().
456 * g_thread_init() must be called previous to this function.
458 * This call must be made before any use of the main loop from
459 * GTK+; to be safe, call it before gtk_init().
464 if (!g_thread_supported ())
465 g_error ("g_thread_init() must be called before gdk_threads_init()");
467 gdk_threads_mutex = g_mutex_new ();
470 G_CONST_RETURN char *
471 gdk_get_program_class (void)
473 return gdk_progclass;
477 gdk_set_program_class (const char *program_class)
480 g_free (gdk_progclass);
482 gdk_progclass = g_strdup (program_class);