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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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
57 static void gdk_exit_func (void);
59 GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
63 /* Private variable declarations
65 static int gdk_initialized = 0; /* 1 if the library is initialized,
69 static GSList *gdk_error_traps = NULL; /* List of error traps */
70 static GSList *gdk_error_trap_free_list = NULL; /* Free list */
73 static const GDebugKey gdk_debug_keys[] = {
74 {"events", GDK_DEBUG_EVENTS},
75 {"misc", GDK_DEBUG_MISC},
76 {"dnd", GDK_DEBUG_DND},
77 {"color-context", GDK_DEBUG_COLOR_CONTEXT},
78 {"xim", GDK_DEBUG_XIM}
81 static const int gdk_ndebug_keys = sizeof(gdk_debug_keys)/sizeof(GDebugKey);
83 #endif /* G_ENABLE_DEBUG */
86 gdk_arg_context_new (gpointer cb_data)
88 GdkArgContext *result = g_new (GdkArgContext, 1);
89 result->tables = g_ptr_array_new ();
90 result->cb_data = cb_data;
96 gdk_arg_context_destroy (GdkArgContext *context)
98 g_ptr_array_free (context->tables, TRUE);
103 gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
105 g_ptr_array_add (context->tables, table);
109 gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
113 /* Save a copy of the original argc and argv */
116 for (i = 1; i < *argc; i++)
120 if (!(*argv)[i][0] == '-' && (*argv)[i][1] == '-')
123 arg = (*argv)[i] + 2;
125 /* '--' terminates list of arguments */
132 for (j = 0; j < context->tables->len; j++)
134 GdkArgDesc *table = context->tables->pdata[j];
135 for (k = 0; table[k].name; k++)
137 switch (table[k].type)
140 case GDK_ARG_CALLBACK:
143 int len = strlen (table[k].name);
145 if (strncmp (arg, table[k].name, len) == 0 &&
146 (arg[len] == '=' || argc[len] == 0))
153 value = arg + len + 1;
154 else if (i < *argc - 1)
156 value = (*argv)[i + 1];
163 switch (table[k].type)
166 *(gchar **)table[k].location = g_strdup (value);
169 *(gint *)table[k].location = atoi (value);
171 case GDK_ARG_CALLBACK:
172 (*table[k].callback)(table[k].name, value, context->cb_data);
184 if (strcmp (arg, table[k].name) == 0)
188 *(gboolean *)table[k].location = (table[k].type == GDK_ARG_BOOL) ? TRUE : FALSE;
198 for (i = 1; i < *argc; i++)
200 for (k = i; k < *argc; k++)
201 if ((*argv)[k] != NULL)
207 for (j = i + k; j < *argc; j++)
208 (*argv)[j-k] = (*argv)[j];
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,
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 { "name", GDK_ARG_STRING, NULL, gdk_arg_name_cb },
239 #ifdef G_ENABLE_DEBUG
240 { "gdk-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_debug_cb },
241 { "gdk-no-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_no_debug_cb },
242 #endif /* G_ENABLE_DEBUG */
247 *--------------------------------------------------------------
250 * Initialize the library for use.
253 * "argc" is the number of arguments.
254 * "argv" is an array of strings.
257 * "argc" and "argv" are modified to reflect any arguments
258 * which were not handled. (Such arguments should either
259 * be handled by the application or dismissed). If initialization
260 * fails, returns FALSE, otherwise TRUE.
263 * The library is initialized.
265 *--------------------------------------------------------------
269 gdk_init_check (int *argc,
272 gchar **argv_orig = NULL;
274 GdkArgContext *arg_context;
281 if (g_thread_supported ())
282 gdk_threads_mutex = g_mutex_new ();
288 argv_orig = g_malloc ((argc_orig + 1) * sizeof (char*));
289 for (i = 0; i < argc_orig; i++)
290 argv_orig[i] = g_strdup ((*argv)[i]);
291 argv_orig[argc_orig] = NULL;
297 d = strrchr((*argv)[0], G_DIR_SEPARATOR);
299 g_set_prgname (d + 1);
301 g_set_prgname ((*argv)[0]);
306 #ifdef G_ENABLE_DEBUG
308 gchar *debug_string = getenv("GDK_DEBUG");
309 if (debug_string != NULL)
310 gdk_debug_flags = g_parse_debug_string (debug_string,
311 (GDebugKey *) gdk_debug_keys,
314 #endif /* G_ENABLE_DEBUG */
316 arg_context = gdk_arg_context_new (NULL);
317 gdk_arg_context_add_table (arg_context, gdk_args);
318 gdk_arg_context_add_table (arg_context, _gdk_windowing_args);
319 gdk_arg_context_parse (arg_context, argc, argv);
320 gdk_arg_context_destroy (arg_context);
322 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
326 result = _gdk_windowing_init_check (argc_orig, argv_orig);
328 for (i = 0; i < argc_orig; i++)
329 g_free(argv_orig[i]);
335 g_atexit (gdk_exit_func);
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.) */
391 *--------------------------------------------------------------
394 * This is the "atexit" function that makes sure the
395 * library gets a chance to cleanup.
402 * The library is un-initialized and the program exits.
404 *--------------------------------------------------------------
410 static gboolean in_gdk_exit_func = FALSE;
412 /* This is to avoid an infinite loop if a program segfaults in
413 an atexit() handler (and yes, it does happen, especially if a program
414 has trounced over memory too badly for even g_message to work) */
415 if (in_gdk_exit_func == TRUE)
417 in_gdk_exit_func = TRUE;
429 gdk_key_repeat_restore ();
431 gdk_windowing_exit ();
437 /*************************************************************
438 * gdk_error_trap_push:
439 * Push an error trap. X errors will be trapped until
440 * the corresponding gdk_error_pop(), which will return
441 * the error code, if any.
445 *************************************************************/
448 gdk_error_trap_push (void)
453 if (gdk_error_trap_free_list)
455 node = gdk_error_trap_free_list;
456 gdk_error_trap_free_list = gdk_error_trap_free_list->next;
460 node = g_slist_alloc ();
461 node->data = g_new (GdkErrorTrap, 1);
464 node->next = gdk_error_traps;
465 gdk_error_traps = node;
468 trap->error_code = gdk_error_code;
469 trap->error_warnings = gdk_error_warnings;
472 gdk_error_warnings = 0;
475 /*************************************************************
476 * gdk_error_trap_pop:
477 * Pop an error trap added with gdk_error_push()
481 * 0, if no error occured, otherwise the error code.
482 *************************************************************/
485 gdk_error_trap_pop (void)
491 g_return_val_if_fail (gdk_error_traps != NULL, 0);
493 node = gdk_error_traps;
494 gdk_error_traps = gdk_error_traps->next;
496 node->next = gdk_error_trap_free_list;
497 gdk_error_trap_free_list = node;
499 result = gdk_error_code;
502 gdk_error_code = trap->error_code;
503 gdk_error_warnings = trap->error_warnings;
508 #ifndef HAVE_XCONVERTCASE
509 /* compatibility function from X11R6.3, since XConvertCase is not
513 gdk_keyval_convert_case (guint symbol,
517 guint xlower = symbol;
518 guint xupper = symbol;
522 #if defined (GDK_A) && defined (GDK_Ooblique)
523 case 0: /* Latin 1 */
524 if ((symbol >= GDK_A) && (symbol <= GDK_Z))
525 xlower += (GDK_a - GDK_A);
526 else if ((symbol >= GDK_a) && (symbol <= GDK_z))
527 xupper -= (GDK_a - GDK_A);
528 else if ((symbol >= GDK_Agrave) && (symbol <= GDK_Odiaeresis))
529 xlower += (GDK_agrave - GDK_Agrave);
530 else if ((symbol >= GDK_agrave) && (symbol <= GDK_odiaeresis))
531 xupper -= (GDK_agrave - GDK_Agrave);
532 else if ((symbol >= GDK_Ooblique) && (symbol <= GDK_Thorn))
533 xlower += (GDK_oslash - GDK_Ooblique);
534 else if ((symbol >= GDK_oslash) && (symbol <= GDK_thorn))
535 xupper -= (GDK_oslash - GDK_Ooblique);
539 #if defined (GDK_Aogonek) && defined (GDK_tcedilla)
540 case 1: /* Latin 2 */
541 /* Assume the KeySym is a legal value (ignore discontinuities) */
542 if (symbol == GDK_Aogonek)
543 xlower = GDK_aogonek;
544 else if (symbol >= GDK_Lstroke && symbol <= GDK_Sacute)
545 xlower += (GDK_lstroke - GDK_Lstroke);
546 else if (symbol >= GDK_Scaron && symbol <= GDK_Zacute)
547 xlower += (GDK_scaron - GDK_Scaron);
548 else if (symbol >= GDK_Zcaron && symbol <= GDK_Zabovedot)
549 xlower += (GDK_zcaron - GDK_Zcaron);
550 else if (symbol == GDK_aogonek)
551 xupper = GDK_Aogonek;
552 else if (symbol >= GDK_lstroke && symbol <= GDK_sacute)
553 xupper -= (GDK_lstroke - GDK_Lstroke);
554 else if (symbol >= GDK_scaron && symbol <= GDK_zacute)
555 xupper -= (GDK_scaron - GDK_Scaron);
556 else if (symbol >= GDK_zcaron && symbol <= GDK_zabovedot)
557 xupper -= (GDK_zcaron - GDK_Zcaron);
558 else if (symbol >= GDK_Racute && symbol <= GDK_Tcedilla)
559 xlower += (GDK_racute - GDK_Racute);
560 else if (symbol >= GDK_racute && symbol <= GDK_tcedilla)
561 xupper -= (GDK_racute - GDK_Racute);
565 #if defined (GDK_Hstroke) && defined (GDK_Cabovedot)
566 case 2: /* Latin 3 */
567 /* Assume the KeySym is a legal value (ignore discontinuities) */
568 if (symbol >= GDK_Hstroke && symbol <= GDK_Hcircumflex)
569 xlower += (GDK_hstroke - GDK_Hstroke);
570 else if (symbol >= GDK_Gbreve && symbol <= GDK_Jcircumflex)
571 xlower += (GDK_gbreve - GDK_Gbreve);
572 else if (symbol >= GDK_hstroke && symbol <= GDK_hcircumflex)
573 xupper -= (GDK_hstroke - GDK_Hstroke);
574 else if (symbol >= GDK_gbreve && symbol <= GDK_jcircumflex)
575 xupper -= (GDK_gbreve - GDK_Gbreve);
576 else if (symbol >= GDK_Cabovedot && symbol <= GDK_Scircumflex)
577 xlower += (GDK_cabovedot - GDK_Cabovedot);
578 else if (symbol >= GDK_cabovedot && symbol <= GDK_scircumflex)
579 xupper -= (GDK_cabovedot - GDK_Cabovedot);
583 #if defined (GDK_Rcedilla) && defined (GDK_Amacron)
584 case 3: /* Latin 4 */
585 /* Assume the KeySym is a legal value (ignore discontinuities) */
586 if (symbol >= GDK_Rcedilla && symbol <= GDK_Tslash)
587 xlower += (GDK_rcedilla - GDK_Rcedilla);
588 else if (symbol >= GDK_rcedilla && symbol <= GDK_tslash)
589 xupper -= (GDK_rcedilla - GDK_Rcedilla);
590 else if (symbol == GDK_ENG)
592 else if (symbol == GDK_eng)
594 else if (symbol >= GDK_Amacron && symbol <= GDK_Umacron)
595 xlower += (GDK_amacron - GDK_Amacron);
596 else if (symbol >= GDK_amacron && symbol <= GDK_umacron)
597 xupper -= (GDK_amacron - GDK_Amacron);
601 #if defined (GDK_Serbian_DJE) && defined (GDK_Cyrillic_yu)
602 case 6: /* Cyrillic */
603 /* Assume the KeySym is a legal value (ignore discontinuities) */
604 if (symbol >= GDK_Serbian_DJE && symbol <= GDK_Serbian_DZE)
605 xlower -= (GDK_Serbian_DJE - GDK_Serbian_dje);
606 else if (symbol >= GDK_Serbian_dje && symbol <= GDK_Serbian_dze)
607 xupper += (GDK_Serbian_DJE - GDK_Serbian_dje);
608 else if (symbol >= GDK_Cyrillic_YU && symbol <= GDK_Cyrillic_HARDSIGN)
609 xlower -= (GDK_Cyrillic_YU - GDK_Cyrillic_yu);
610 else if (symbol >= GDK_Cyrillic_yu && symbol <= GDK_Cyrillic_hardsign)
611 xupper += (GDK_Cyrillic_YU - GDK_Cyrillic_yu);
613 #endif /* CYRILLIC */
615 #if defined (GDK_Greek_ALPHAaccent) && defined (GDK_Greek_finalsmallsigma)
617 /* Assume the KeySym is a legal value (ignore discontinuities) */
618 if (symbol >= GDK_Greek_ALPHAaccent && symbol <= GDK_Greek_OMEGAaccent)
619 xlower += (GDK_Greek_alphaaccent - GDK_Greek_ALPHAaccent);
620 else if (symbol >= GDK_Greek_alphaaccent && symbol <= GDK_Greek_omegaaccent &&
621 symbol != GDK_Greek_iotaaccentdieresis &&
622 symbol != GDK_Greek_upsilonaccentdieresis)
623 xupper -= (GDK_Greek_alphaaccent - GDK_Greek_ALPHAaccent);
624 else if (symbol >= GDK_Greek_ALPHA && symbol <= GDK_Greek_OMEGA)
625 xlower += (GDK_Greek_alpha - GDK_Greek_ALPHA);
626 else if (symbol >= GDK_Greek_alpha && symbol <= GDK_Greek_omega &&
627 symbol != GDK_Greek_finalsmallsigma)
628 xupper -= (GDK_Greek_alpha - GDK_Greek_ALPHA);
641 gdk_keyval_to_upper (guint keyval)
645 gdk_keyval_convert_case (keyval, NULL, &result);
651 gdk_keyval_to_lower (guint keyval)
655 gdk_keyval_convert_case (keyval, &result, NULL);
661 gdk_keyval_is_upper (guint keyval)
667 gdk_keyval_convert_case (keyval, NULL, &upper_val);
668 return upper_val == keyval;
674 gdk_keyval_is_lower (guint keyval)
680 gdk_keyval_convert_case (keyval, &lower_val, NULL);
681 return lower_val == keyval;
689 GDK_THREADS_ENTER ();
695 GDK_THREADS_LEAVE ();