]> Pileus Git - ~andy/gtk/blob - gdk/gdk.c
file makefile.mingw was initially added on branch gtk-1-3-win32-production.
[~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 /* 
55  * Private function declarations
56  */
57
58 GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
59                                          GdkEvent  *event,
60                                          gpointer   data);
61
62 /* Private variable declarations
63  */
64 static int gdk_initialized = 0;                     /* 1 if the library is initialized,
65                                                      * 0 otherwise.
66                                                      */
67
68 static GSList *gdk_error_traps = NULL;               /* List of error traps */
69 static GSList *gdk_error_trap_free_list = NULL;      /* Free list */
70
71 #ifdef G_ENABLE_DEBUG
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}
78 };
79
80 static const int gdk_ndebug_keys = sizeof(gdk_debug_keys)/sizeof(GDebugKey);
81
82 #endif /* G_ENABLE_DEBUG */
83
84 GdkArgContext *
85 gdk_arg_context_new (gpointer cb_data)
86 {
87   GdkArgContext *result = g_new (GdkArgContext, 1);
88   result->tables = g_ptr_array_new ();
89   result->cb_data = cb_data;
90
91   return result;
92 }
93
94 void
95 gdk_arg_context_destroy (GdkArgContext *context)
96 {
97   g_ptr_array_free (context->tables, TRUE);
98   g_free (context);
99 }
100
101 void
102 gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
103 {
104   g_ptr_array_add (context->tables, table);
105 }
106
107 void
108 gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
109 {
110   int i, j, k;
111
112   /* Save a copy of the original argc and argv */
113   if (argc && argv)
114     {
115       for (i = 1; i < *argc; i++)
116         {
117           char *arg;
118           
119           if (!(*argv)[i][0] == '-' && (*argv)[i][1] == '-')
120             continue;
121           
122           arg = (*argv)[i] + 2;
123
124           /* '--' terminates list of arguments */
125           if (arg == 0)
126             {
127               (*argv)[i] = NULL;
128               break;
129             }
130               
131           for (j = 0; j < context->tables->len; j++)
132             {
133               GdkArgDesc *table = context->tables->pdata[j];
134               for (k = 0; table[k].name; k++)
135                 {
136                   switch (table[k].type)
137                     {
138                     case GDK_ARG_STRING:
139                     case GDK_ARG_CALLBACK:
140                     case GDK_ARG_INT:
141                       {
142                         int len = strlen (table[k].name);
143                         
144                         if (strncmp (arg, table[k].name, len) == 0 &&
145                             (arg[len] == '=' || argc[len] == 0))
146                           {
147                             char *value = NULL;
148
149                             (*argv)[i] = NULL;
150
151                             if (arg[len] == '=')
152                               value = arg + len + 1;
153                             else if (i < *argc - 1)
154                               {
155                                 value = (*argv)[i + 1];
156                                 (*argv)[i+1] = NULL;
157                                 i++;
158                               }
159                             else
160                               value = "";
161
162                             switch (table[k].type)
163                               {
164                               case GDK_ARG_STRING:
165                                 *(gchar **)table[k].location = g_strdup (value);
166                                 break;
167                               case GDK_ARG_INT:
168                                 *(gint *)table[k].location = atoi (value);
169                                 break;
170                               case GDK_ARG_CALLBACK:
171                                 (*table[k].callback)(table[k].name, value, context->cb_data);
172                                 break;
173                               default:
174                                 ;
175                               }
176
177                             goto next_arg;
178                           }
179                         break;
180                       }
181                     case GDK_ARG_BOOL:
182                     case GDK_ARG_NOBOOL:
183                       if (strcmp (arg, table[k].name) == 0)
184                         {
185                           (*argv)[i] = NULL;
186                           
187                           *(gboolean *)table[k].location = (table[k].type == GDK_ARG_BOOL) ? TRUE : FALSE;
188                           goto next_arg;
189                         }
190                     }
191                 }
192             }
193         next_arg:
194           ;
195         }
196           
197       for (i = 1; i < *argc; i++)
198         {
199           for (k = i; k < *argc; k++)
200             if ((*argv)[k] != NULL)
201               break;
202           
203           if (k > i)
204             {
205               k -= i;
206               for (j = i + k; j < *argc; j++)
207                 (*argv)[j-k] = (*argv)[j];
208               *argc -= k;
209             }
210         }
211     }
212 }
213
214 #ifdef G_ENABLE_DEBUG
215 static void
216 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
217 {
218   gdk_debug_flags |= g_parse_debug_string (value,
219                                            (GDebugKey *) gdk_debug_keys,
220                                            gdk_ndebug_keys);
221 }
222
223 static void
224 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
225 {
226   gdk_debug_flags &= ~g_parse_debug_string (value,
227                                             (GDebugKey *) gdk_debug_keys,
228                                             gdk_ndebug_keys);
229 }
230 #endif /* G_ENABLE_DEBUG */
231
232 static void
233 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data)
234 {
235   g_set_prgname (value);
236 }
237
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 */
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 (g_thread_supported ())
283     gdk_threads_mutex = g_mutex_new ();
284   
285   if (argc && argv)
286     {
287       argc_orig = *argc;
288       
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;
293
294       if (*argc > 0)
295         {
296           gchar *d;
297           
298           d = strrchr((*argv)[0], G_DIR_SEPARATOR);
299           if (d != NULL)
300             g_set_prgname (d + 1);
301           else
302             g_set_prgname ((*argv)[0]);
303         }
304     }
305
306   
307 #ifdef G_ENABLE_DEBUG
308   {
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,
313                                               gdk_ndebug_keys);
314   }
315 #endif  /* G_ENABLE_DEBUG */
316   
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);
322   
323   GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
324
325   g_type_init (0);
326   
327   result = _gdk_windowing_init_check (argc_orig, argv_orig);
328
329   for (i = 0; i < argc_orig; i++)
330     g_free(argv_orig[i]);
331   g_free(argv_orig);
332
333   if (!result)
334     return FALSE;
335   
336   gdk_events_init ();
337   gdk_visual_init ();
338   _gdk_windowing_window_init ();
339   _gdk_windowing_image_init ();
340   gdk_input_init ();
341   gdk_dnd_init ();
342
343 #ifdef USE_XIM
344   gdk_im_open ();
345 #endif
346   
347   gdk_initialized = 1;
348
349   return TRUE;
350 }
351
352 void
353 gdk_init (int *argc, char ***argv)
354 {
355   if (!gdk_init_check (argc, argv))
356     {
357       g_warning ("cannot open display: %s", gdk_get_display ());
358       exit(1);
359     }
360 }
361
362 /*
363  *--------------------------------------------------------------
364  * gdk_exit
365  *
366  *   Restores the library to an un-itialized state and exits
367  *   the program using the "exit" system call.
368  *
369  * Arguments:
370  *   "errorcode" is the error value to pass to "exit".
371  *
372  * Results:
373  *   Allocated structures are freed and the program exits
374  *   cleanly.
375  *
376  * Side effects:
377  *
378  *--------------------------------------------------------------
379  */
380
381 void
382 gdk_exit (gint errorcode)
383 {
384   /* de-initialisation is done by the gdk_exit_funct(),
385      no need to do this here (Alex J.) */
386   exit (errorcode);
387 }
388
389 #if 0
390
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.
394  */
395
396 /*
397  *--------------------------------------------------------------
398  * gdk_exit_func
399  *
400  *   This is the "atexit" function that makes sure the
401  *   library gets a chance to cleanup.
402  *
403  * Arguments:
404  *
405  * Results:
406  *
407  * Side effects:
408  *   The library is un-initialized and the program exits.
409  *
410  *--------------------------------------------------------------
411  */
412
413 static void
414 gdk_exit_func (void)
415 {
416   static gboolean in_gdk_exit_func = FALSE;
417   
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)
422     return;
423   in_gdk_exit_func = TRUE;
424   
425   if (gdk_initialized)
426     {
427 #ifdef USE_XIM
428       /* cleanup IC */
429       gdk_ic_cleanup ();
430       /* close IM */
431       gdk_im_close ();
432 #endif
433       gdk_image_exit ();
434       gdk_input_exit ();
435       gdk_key_repeat_restore ();
436
437       gdk_windowing_exit ();
438       
439       gdk_initialized = 0;
440     }
441 }
442
443 #endif
444
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.
450  *   arguments:
451  *     
452  *   results:
453  *************************************************************/
454
455 void
456 gdk_error_trap_push (void)
457 {
458   GSList *node;
459   GdkErrorTrap *trap;
460
461   if (gdk_error_trap_free_list)
462     {
463       node = gdk_error_trap_free_list;
464       gdk_error_trap_free_list = gdk_error_trap_free_list->next;
465     }
466   else
467     {
468       node = g_slist_alloc ();
469       node->data = g_new (GdkErrorTrap, 1);
470     }
471
472   node->next = gdk_error_traps;
473   gdk_error_traps = node;
474   
475   trap = node->data;
476   trap->error_code = gdk_error_code;
477   trap->error_warnings = gdk_error_warnings;
478
479   gdk_error_code = 0;
480   gdk_error_warnings = 0;
481 }
482
483 /*************************************************************
484  * gdk_error_trap_pop:
485  *     Pop an error trap added with gdk_error_push()
486  *   arguments:
487  *     
488  *   results:
489  *     0, if no error occured, otherwise the error code.
490  *************************************************************/
491
492 gint
493 gdk_error_trap_pop (void)
494 {
495   GSList *node;
496   GdkErrorTrap *trap;
497   gint result;
498
499   g_return_val_if_fail (gdk_error_traps != NULL, 0);
500
501   node = gdk_error_traps;
502   gdk_error_traps = gdk_error_traps->next;
503
504   node->next = gdk_error_trap_free_list;
505   gdk_error_trap_free_list = node;
506   
507   result = gdk_error_code;
508   
509   trap = node->data;
510   gdk_error_code = trap->error_code;
511   gdk_error_warnings = trap->error_warnings;
512   
513   return result;
514 }
515
516 void
517 gdk_threads_enter ()
518 {
519   GDK_THREADS_ENTER ();
520 }
521
522 void
523 gdk_threads_leave ()
524 {
525   GDK_THREADS_LEAVE ();
526 }
527