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
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];
215 #ifdef G_ENABLE_DEBUG
217 gdk_arg_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,
225 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
227 gdk_debug_flags &= ~g_parse_debug_string (value,
228 (GDebugKey *) gdk_debug_keys,
231 #endif /* G_ENABLE_DEBUG */
234 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data)
236 g_set_prgname (value);
239 static GdkArgDesc gdk_args[] = {
240 { "name", GDK_ARG_STRING, NULL, gdk_arg_name_cb },
241 #ifdef G_ENABLE_DEBUG
242 { "gdk-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_debug_cb },
243 { "gdk-no-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_no_debug_cb },
244 #endif /* G_ENABLE_DEBUG */
249 *--------------------------------------------------------------
252 * Initialize the library for use.
255 * "argc" is the number of arguments.
256 * "argv" is an array of strings.
259 * "argc" and "argv" are modified to reflect any arguments
260 * which were not handled. (Such arguments should either
261 * be handled by the application or dismissed). If initialization
262 * fails, returns FALSE, otherwise TRUE.
265 * The library is initialized.
267 *--------------------------------------------------------------
271 gdk_init_check (int *argc,
274 gchar **argv_orig = NULL;
276 GdkArgContext *arg_context;
283 if (g_thread_supported ())
284 gdk_threads_mutex = g_mutex_new ();
290 argv_orig = g_malloc ((argc_orig + 1) * sizeof (char*));
291 for (i = 0; i < argc_orig; i++)
292 argv_orig[i] = g_strdup ((*argv)[i]);
293 argv_orig[argc_orig] = NULL;
299 d = strrchr((*argv)[0], G_DIR_SEPARATOR);
301 g_set_prgname (d + 1);
303 g_set_prgname ((*argv)[0]);
308 #ifdef G_ENABLE_DEBUG
310 gchar *debug_string = getenv("GDK_DEBUG");
311 if (debug_string != NULL)
312 gdk_debug_flags = g_parse_debug_string (debug_string,
313 (GDebugKey *) gdk_debug_keys,
316 #endif /* G_ENABLE_DEBUG */
318 arg_context = gdk_arg_context_new (NULL);
319 gdk_arg_context_add_table (arg_context, gdk_args);
320 gdk_arg_context_add_table (arg_context, _gdk_windowing_args);
321 gdk_arg_context_parse (arg_context, argc, argv);
322 gdk_arg_context_destroy (arg_context);
324 GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
328 result = _gdk_windowing_init_check (argc_orig, argv_orig);
330 for (i = 0; i < argc_orig; i++)
331 g_free(argv_orig[i]);
337 g_atexit (gdk_exit_func);
341 _gdk_windowing_window_init ();
342 _gdk_windowing_image_init ();
356 gdk_init (int *argc, char ***argv)
358 if (!gdk_init_check (argc, argv))
360 g_warning ("cannot open display: %s", gdk_get_display ());
366 *--------------------------------------------------------------
369 * Restores the library to an un-itialized state and exits
370 * the program using the "exit" system call.
373 * "errorcode" is the error value to pass to "exit".
376 * Allocated structures are freed and the program exits
381 *--------------------------------------------------------------
385 gdk_exit (gint errorcode)
387 /* de-initialisation is done by the gdk_exit_funct(),
388 no need to do this here (Alex J.) */
393 *--------------------------------------------------------------
396 * This is the "atexit" function that makes sure the
397 * library gets a chance to cleanup.
404 * The library is un-initialized and the program exits.
406 *--------------------------------------------------------------
412 static gboolean in_gdk_exit_func = FALSE;
414 /* This is to avoid an infinite loop if a program segfaults in
415 an atexit() handler (and yes, it does happen, especially if a program
416 has trounced over memory too badly for even g_message to work) */
417 if (in_gdk_exit_func == TRUE)
419 in_gdk_exit_func = TRUE;
431 gdk_key_repeat_restore ();
433 gdk_windowing_exit ();
439 /*************************************************************
440 * gdk_error_trap_push:
441 * Push an error trap. X errors will be trapped until
442 * the corresponding gdk_error_pop(), which will return
443 * the error code, if any.
447 *************************************************************/
450 gdk_error_trap_push (void)
455 if (gdk_error_trap_free_list)
457 node = gdk_error_trap_free_list;
458 gdk_error_trap_free_list = gdk_error_trap_free_list->next;
462 node = g_slist_alloc ();
463 node->data = g_new (GdkErrorTrap, 1);
466 node->next = gdk_error_traps;
467 gdk_error_traps = node;
470 trap->error_code = gdk_error_code;
471 trap->error_warnings = gdk_error_warnings;
474 gdk_error_warnings = 0;
477 /*************************************************************
478 * gdk_error_trap_pop:
479 * Pop an error trap added with gdk_error_push()
483 * 0, if no error occured, otherwise the error code.
484 *************************************************************/
487 gdk_error_trap_pop (void)
493 g_return_val_if_fail (gdk_error_traps != NULL, 0);
495 node = gdk_error_traps;
496 gdk_error_traps = gdk_error_traps->next;
498 node->next = gdk_error_trap_free_list;
499 gdk_error_trap_free_list = node;
501 result = gdk_error_code;
504 gdk_error_code = trap->error_code;
505 gdk_error_warnings = trap->error_warnings;
510 #ifndef HAVE_XCONVERTCASE
511 /* compatibility function from X11R6.3, since XConvertCase is not
515 gdk_keyval_convert_case (guint symbol,
519 guint xlower = symbol;
520 guint xupper = symbol;
524 #if defined (GDK_A) && defined (GDK_Ooblique)
525 case 0: /* Latin 1 */
526 if ((symbol >= GDK_A) && (symbol <= GDK_Z))
527 xlower += (GDK_a - GDK_A);
528 else if ((symbol >= GDK_a) && (symbol <= GDK_z))
529 xupper -= (GDK_a - GDK_A);
530 else if ((symbol >= GDK_Agrave) && (symbol <= GDK_Odiaeresis))
531 xlower += (GDK_agrave - GDK_Agrave);
532 else if ((symbol >= GDK_agrave) && (symbol <= GDK_odiaeresis))
533 xupper -= (GDK_agrave - GDK_Agrave);
534 else if ((symbol >= GDK_Ooblique) && (symbol <= GDK_Thorn))
535 xlower += (GDK_oslash - GDK_Ooblique);
536 else if ((symbol >= GDK_oslash) && (symbol <= GDK_thorn))
537 xupper -= (GDK_oslash - GDK_Ooblique);
541 #if defined (GDK_Aogonek) && defined (GDK_tcedilla)
542 case 1: /* Latin 2 */
543 /* Assume the KeySym is a legal value (ignore discontinuities) */
544 if (symbol == GDK_Aogonek)
545 xlower = GDK_aogonek;
546 else if (symbol >= GDK_Lstroke && symbol <= GDK_Sacute)
547 xlower += (GDK_lstroke - GDK_Lstroke);
548 else if (symbol >= GDK_Scaron && symbol <= GDK_Zacute)
549 xlower += (GDK_scaron - GDK_Scaron);
550 else if (symbol >= GDK_Zcaron && symbol <= GDK_Zabovedot)
551 xlower += (GDK_zcaron - GDK_Zcaron);
552 else if (symbol == GDK_aogonek)
553 xupper = GDK_Aogonek;
554 else if (symbol >= GDK_lstroke && symbol <= GDK_sacute)
555 xupper -= (GDK_lstroke - GDK_Lstroke);
556 else if (symbol >= GDK_scaron && symbol <= GDK_zacute)
557 xupper -= (GDK_scaron - GDK_Scaron);
558 else if (symbol >= GDK_zcaron && symbol <= GDK_zabovedot)
559 xupper -= (GDK_zcaron - GDK_Zcaron);
560 else if (symbol >= GDK_Racute && symbol <= GDK_Tcedilla)
561 xlower += (GDK_racute - GDK_Racute);
562 else if (symbol >= GDK_racute && symbol <= GDK_tcedilla)
563 xupper -= (GDK_racute - GDK_Racute);
567 #if defined (GDK_Hstroke) && defined (GDK_Cabovedot)
568 case 2: /* Latin 3 */
569 /* Assume the KeySym is a legal value (ignore discontinuities) */
570 if (symbol >= GDK_Hstroke && symbol <= GDK_Hcircumflex)
571 xlower += (GDK_hstroke - GDK_Hstroke);
572 else if (symbol >= GDK_Gbreve && symbol <= GDK_Jcircumflex)
573 xlower += (GDK_gbreve - GDK_Gbreve);
574 else if (symbol >= GDK_hstroke && symbol <= GDK_hcircumflex)
575 xupper -= (GDK_hstroke - GDK_Hstroke);
576 else if (symbol >= GDK_gbreve && symbol <= GDK_jcircumflex)
577 xupper -= (GDK_gbreve - GDK_Gbreve);
578 else if (symbol >= GDK_Cabovedot && symbol <= GDK_Scircumflex)
579 xlower += (GDK_cabovedot - GDK_Cabovedot);
580 else if (symbol >= GDK_cabovedot && symbol <= GDK_scircumflex)
581 xupper -= (GDK_cabovedot - GDK_Cabovedot);
585 #if defined (GDK_Rcedilla) && defined (GDK_Amacron)
586 case 3: /* Latin 4 */
587 /* Assume the KeySym is a legal value (ignore discontinuities) */
588 if (symbol >= GDK_Rcedilla && symbol <= GDK_Tslash)
589 xlower += (GDK_rcedilla - GDK_Rcedilla);
590 else if (symbol >= GDK_rcedilla && symbol <= GDK_tslash)
591 xupper -= (GDK_rcedilla - GDK_Rcedilla);
592 else if (symbol == GDK_ENG)
594 else if (symbol == GDK_eng)
596 else if (symbol >= GDK_Amacron && symbol <= GDK_Umacron)
597 xlower += (GDK_amacron - GDK_Amacron);
598 else if (symbol >= GDK_amacron && symbol <= GDK_umacron)
599 xupper -= (GDK_amacron - GDK_Amacron);
603 #if defined (GDK_Serbian_DJE) && defined (GDK_Cyrillic_yu)
604 case 6: /* Cyrillic */
605 /* Assume the KeySym is a legal value (ignore discontinuities) */
606 if (symbol >= GDK_Serbian_DJE && symbol <= GDK_Serbian_DZE)
607 xlower -= (GDK_Serbian_DJE - GDK_Serbian_dje);
608 else if (symbol >= GDK_Serbian_dje && symbol <= GDK_Serbian_dze)
609 xupper += (GDK_Serbian_DJE - GDK_Serbian_dje);
610 else if (symbol >= GDK_Cyrillic_YU && symbol <= GDK_Cyrillic_HARDSIGN)
611 xlower -= (GDK_Cyrillic_YU - GDK_Cyrillic_yu);
612 else if (symbol >= GDK_Cyrillic_yu && symbol <= GDK_Cyrillic_hardsign)
613 xupper += (GDK_Cyrillic_YU - GDK_Cyrillic_yu);
615 #endif /* CYRILLIC */
617 #if defined (GDK_Greek_ALPHAaccent) && defined (GDK_Greek_finalsmallsigma)
619 /* Assume the KeySym is a legal value (ignore discontinuities) */
620 if (symbol >= GDK_Greek_ALPHAaccent && symbol <= GDK_Greek_OMEGAaccent)
621 xlower += (GDK_Greek_alphaaccent - GDK_Greek_ALPHAaccent);
622 else if (symbol >= GDK_Greek_alphaaccent && symbol <= GDK_Greek_omegaaccent &&
623 symbol != GDK_Greek_iotaaccentdieresis &&
624 symbol != GDK_Greek_upsilonaccentdieresis)
625 xupper -= (GDK_Greek_alphaaccent - GDK_Greek_ALPHAaccent);
626 else if (symbol >= GDK_Greek_ALPHA && symbol <= GDK_Greek_OMEGA)
627 xlower += (GDK_Greek_alpha - GDK_Greek_ALPHA);
628 else if (symbol >= GDK_Greek_alpha && symbol <= GDK_Greek_omega &&
629 symbol != GDK_Greek_finalsmallsigma)
630 xupper -= (GDK_Greek_alpha - GDK_Greek_ALPHA);
643 gdk_keyval_to_upper (guint keyval)
647 gdk_keyval_convert_case (keyval, NULL, &result);
653 gdk_keyval_to_lower (guint keyval)
657 gdk_keyval_convert_case (keyval, &result, NULL);
663 gdk_keyval_is_upper (guint keyval)
669 gdk_keyval_convert_case (keyval, NULL, &upper_val);
670 return upper_val == keyval;
676 gdk_keyval_is_lower (guint keyval)
682 gdk_keyval_convert_case (keyval, &lower_val, NULL);
683 return lower_val == keyval;
691 GDK_THREADS_ENTER ();
697 GDK_THREADS_LEAVE ();