]> Pileus Git - ~andy/gtk/blob - gdk/gdk.c
6e7c4682c4c0f21fb6307b4f621dac7d85172ac7
[~andy/gtk] / gdk / gdk.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "gdk.h"
33 #include "gdkinternals.h"
34
35 #ifndef HAVE_XCONVERTCASE
36 #include "gdkkeysyms.h"
37 #endif
38
39 typedef struct _GdkPredicate  GdkPredicate;
40 typedef struct _GdkErrorTrap  GdkErrorTrap;
41
42 struct _GdkPredicate
43 {
44   GdkEventFunc func;
45   gpointer data;
46 };
47
48 struct _GdkErrorTrap
49 {
50   gint error_warnings;
51   gint error_code;
52 };
53
54 /* Private variable declarations
55  */
56 static int gdk_initialized = 0;                     /* 1 if the library is initialized,
57                                                      * 0 otherwise.
58                                                      */
59
60 static GSList *gdk_error_traps = NULL;               /* List of error traps */
61 static GSList *gdk_error_trap_free_list = NULL;      /* Free list */
62
63 static gchar  *gdk_progclass = NULL;
64
65 #ifdef G_ENABLE_DEBUG
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}
71 };
72
73 static const int gdk_ndebug_keys = sizeof(gdk_debug_keys)/sizeof(GDebugKey);
74
75 #endif /* G_ENABLE_DEBUG */
76
77 GdkArgContext *
78 gdk_arg_context_new (gpointer cb_data)
79 {
80   GdkArgContext *result = g_new (GdkArgContext, 1);
81   result->tables = g_ptr_array_new ();
82   result->cb_data = cb_data;
83
84   return result;
85 }
86
87 void
88 gdk_arg_context_destroy (GdkArgContext *context)
89 {
90   g_ptr_array_free (context->tables, TRUE);
91   g_free (context);
92 }
93
94 void
95 gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
96 {
97   g_ptr_array_add (context->tables, table);
98 }
99
100 void
101 gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
102 {
103   int i, j, k;
104
105   /* Save a copy of the original argc and argv */
106   if (argc && argv)
107     {
108       for (i = 1; i < *argc; i++)
109         {
110           char *arg;
111           
112           if (!(*argv)[i][0] == '-' && (*argv)[i][1] == '-')
113             continue;
114           
115           arg = (*argv)[i] + 2;
116
117           /* '--' terminates list of arguments */
118           if (arg == 0)
119             {
120               (*argv)[i] = NULL;
121               break;
122             }
123               
124           for (j = 0; j < context->tables->len; j++)
125             {
126               GdkArgDesc *table = context->tables->pdata[j];
127               for (k = 0; table[k].name; k++)
128                 {
129                   switch (table[k].type)
130                     {
131                     case GDK_ARG_STRING:
132                     case GDK_ARG_CALLBACK:
133                     case GDK_ARG_INT:
134                       {
135                         int len = strlen (table[k].name);
136                         
137                         if (strncmp (arg, table[k].name, len) == 0 &&
138                             (arg[len] == '=' || argc[len] == 0))
139                           {
140                             char *value = NULL;
141
142                             (*argv)[i] = NULL;
143
144                             if (arg[len] == '=')
145                               value = arg + len + 1;
146                             else if (i < *argc - 1)
147                               {
148                                 value = (*argv)[i + 1];
149                                 (*argv)[i+1] = NULL;
150                                 i++;
151                               }
152                             else
153                               value = "";
154
155                             switch (table[k].type)
156                               {
157                               case GDK_ARG_STRING:
158                                 *(gchar **)table[k].location = g_strdup (value);
159                                 break;
160                               case GDK_ARG_INT:
161                                 *(gint *)table[k].location = atoi (value);
162                                 break;
163                               case GDK_ARG_CALLBACK:
164                                 (*table[k].callback)(table[k].name, value, context->cb_data);
165                                 break;
166                               default:
167                                 ;
168                               }
169
170                             goto next_arg;
171                           }
172                         break;
173                       }
174                     case GDK_ARG_BOOL:
175                     case GDK_ARG_NOBOOL:
176                       if (strcmp (arg, table[k].name) == 0)
177                         {
178                           (*argv)[i] = NULL;
179                           
180                           *(gboolean *)table[k].location = (table[k].type == GDK_ARG_BOOL) ? TRUE : FALSE;
181                           goto next_arg;
182                         }
183                     }
184                 }
185             }
186         next_arg:
187           ;
188         }
189           
190       for (i = 1; i < *argc; i++)
191         {
192           for (k = i; k < *argc; k++)
193             if ((*argv)[k] != NULL)
194               break;
195           
196           if (k > i)
197             {
198               k -= i;
199               for (j = i + k; j < *argc; j++)
200                 (*argv)[j-k] = (*argv)[j];
201               *argc -= k;
202             }
203         }
204     }
205 }
206
207 #ifdef G_ENABLE_DEBUG
208 static void
209 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
210 {
211   _gdk_debug_flags |= g_parse_debug_string (value,
212                                             (GDebugKey *) gdk_debug_keys,
213                                             gdk_ndebug_keys);
214 }
215
216 static void
217 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
218 {
219   _gdk_debug_flags &= ~g_parse_debug_string (value,
220                                              (GDebugKey *) gdk_debug_keys,
221                                              gdk_ndebug_keys);
222 }
223 #endif /* G_ENABLE_DEBUG */
224
225 static void
226 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data)
227 {
228   gdk_set_program_class (value);
229 }
230
231 static void
232 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data)
233 {
234   g_set_prgname (value);
235 }
236
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 */
244   { NULL }
245 };
246
247 /*
248  *--------------------------------------------------------------
249  * gdk_init_check
250  *
251  *   Initialize the library for use.
252  *
253  * Arguments:
254  *   "argc" is the number of arguments.
255  *   "argv" is an array of strings.
256  *
257  * Results:
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.
262  *
263  * Side effects:
264  *   The library is initialized.
265  *
266  *--------------------------------------------------------------
267  */
268
269 gboolean
270 gdk_init_check (int    *argc,
271                 char ***argv)
272 {
273   gchar **argv_orig = NULL;
274   gint argc_orig = 0;
275   GdkArgContext *arg_context;
276   gboolean result;
277   int i;
278   
279   if (gdk_initialized)
280     return TRUE;
281
282   if (argc && argv)
283     {
284       argc_orig = *argc;
285       
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;
290
291       if (*argc > 0)
292         {
293           gchar *d;
294           
295           d = strrchr((*argv)[0], G_DIR_SEPARATOR);
296           if (d != NULL)
297             g_set_prgname (d + 1);
298           else
299             g_set_prgname ((*argv)[0]);
300         }
301     }
302
303   
304 #ifdef G_ENABLE_DEBUG
305   {
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,
310                                               gdk_ndebug_keys);
311   }
312 #endif  /* G_ENABLE_DEBUG */
313   
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);
319   
320   GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
321
322   g_type_init ();
323   
324   result = _gdk_windowing_init_check (argc_orig, argv_orig);
325
326   for (i = 0; i < argc_orig; i++)
327     g_free(argv_orig[i]);
328   g_free(argv_orig);
329
330   if (!result)
331     return FALSE;
332   
333   _gdk_visual_init ();
334   _gdk_windowing_window_init ();
335   _gdk_windowing_image_init ();
336   _gdk_events_init ();
337   _gdk_input_init ();
338   _gdk_dnd_init ();
339
340   gdk_initialized = 1;
341
342   return TRUE;
343 }
344
345 void
346 gdk_init (int *argc, char ***argv)
347 {
348   if (!gdk_init_check (argc, argv))
349     {
350       g_warning ("cannot open display: %s", gdk_get_display ());
351       exit(1);
352     }
353 }
354
355 /*
356  *--------------------------------------------------------------
357  * gdk_exit
358  *
359  *   Restores the library to an un-itialized state and exits
360  *   the program using the "exit" system call.
361  *
362  * Arguments:
363  *   "errorcode" is the error value to pass to "exit".
364  *
365  * Results:
366  *   Allocated structures are freed and the program exits
367  *   cleanly.
368  *
369  * Side effects:
370  *
371  *--------------------------------------------------------------
372  */
373
374 void
375 gdk_exit (gint errorcode)
376 {
377   /* de-initialisation is done by the gdk_exit_funct(),
378      no need to do this here (Alex J.) */
379   exit (errorcode);
380 }
381
382 #if 0
383
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.
387  */
388
389 /*
390  *--------------------------------------------------------------
391  * gdk_exit_func
392  *
393  *   This is the "atexit" function that makes sure the
394  *   library gets a chance to cleanup.
395  *
396  * Arguments:
397  *
398  * Results:
399  *
400  * Side effects:
401  *   The library is un-initialized and the program exits.
402  *
403  *--------------------------------------------------------------
404  */
405
406 static void
407 gdk_exit_func (void)
408 {
409   static gboolean in_gdk_exit_func = FALSE;
410   
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)
415     return;
416   in_gdk_exit_func = TRUE;
417   
418   if (gdk_initialized)
419     {
420       _gdk_image_exit ();
421       _gdk_input_exit ();
422
423       _gdk_windowing_exit ();
424       
425       gdk_initialized = 0;
426     }
427 }
428
429 #endif
430
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.
436  *   arguments:
437  *     
438  *   results:
439  *************************************************************/
440
441 void
442 gdk_error_trap_push (void)
443 {
444   GSList *node;
445   GdkErrorTrap *trap;
446
447   if (gdk_error_trap_free_list)
448     {
449       node = gdk_error_trap_free_list;
450       gdk_error_trap_free_list = gdk_error_trap_free_list->next;
451     }
452   else
453     {
454       node = g_slist_alloc ();
455       node->data = g_new (GdkErrorTrap, 1);
456     }
457
458   node->next = gdk_error_traps;
459   gdk_error_traps = node;
460   
461   trap = node->data;
462   trap->error_code = _gdk_error_code;
463   trap->error_warnings = _gdk_error_warnings;
464
465   _gdk_error_code = 0;
466   _gdk_error_warnings = 0;
467 }
468
469 /*************************************************************
470  * gdk_error_trap_pop:
471  *     Pop an error trap added with gdk_error_push()
472  *   arguments:
473  *     
474  *   results:
475  *     0, if no error occured, otherwise the error code.
476  *************************************************************/
477
478 gint
479 gdk_error_trap_pop (void)
480 {
481   GSList *node;
482   GdkErrorTrap *trap;
483   gint result;
484
485   g_return_val_if_fail (gdk_error_traps != NULL, 0);
486
487   node = gdk_error_traps;
488   gdk_error_traps = gdk_error_traps->next;
489
490   node->next = gdk_error_trap_free_list;
491   gdk_error_trap_free_list = node;
492   
493   result = _gdk_error_code;
494   
495   trap = node->data;
496   _gdk_error_code = trap->error_code;
497   _gdk_error_warnings = trap->error_warnings;
498   
499   return result;
500 }
501
502 void
503 gdk_threads_enter ()
504 {
505   GDK_THREADS_ENTER ();
506 }
507
508 void
509 gdk_threads_leave ()
510 {
511   GDK_THREADS_LEAVE ();
512 }
513
514 /**
515  * gdk_threads_init:
516  * 
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.
520  *
521  * This call must be made before any use of the main loop from
522  * GTK+; to be safe, call it before gtk_init().
523  **/
524 void
525 gdk_threads_init ()
526 {
527   if (!g_thread_supported ())
528     g_error ("g_thread_init() must be called before gdk_threads_init()");
529
530   gdk_threads_mutex = g_mutex_new ();
531 }
532
533 G_CONST_RETURN char *
534 gdk_get_program_class (void)
535 {
536   if (gdk_progclass == NULL)
537     {
538       gdk_progclass = g_strdup (g_get_prgname ());
539       gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
540     }
541   
542   return gdk_progclass;
543 }
544
545 void
546 gdk_set_program_class (const char *program_class)
547 {
548   if (gdk_progclass)
549     g_free (gdk_progclass);
550
551   gdk_progclass = g_strdup (program_class);
552 }