]> Pileus Git - ~andy/gtk/blob - gdk/gdk.c
g_set_prgname("<unknown>") if argc is 0 as well, instead of leaving it
[~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
41 struct _GdkPredicate
42 {
43   GdkEventFunc func;
44   gpointer data;
45 };
46
47 /* Private variable declarations
48  */
49 static int gdk_initialized = 0;                     /* 1 if the library is initialized,
50                                                      * 0 otherwise.
51                                                      */
52
53 static gchar  *gdk_progclass = NULL;
54 static gint    gdk_argc = 0;
55 static gchar **gdk_argv = NULL;
56
57 #ifdef G_ENABLE_DEBUG
58 static const GDebugKey gdk_debug_keys[] = {
59   {"events",        GDK_DEBUG_EVENTS},
60   {"misc",          GDK_DEBUG_MISC},
61   {"dnd",           GDK_DEBUG_DND},
62   {"xim",           GDK_DEBUG_XIM},
63   {"nograbs",       GDK_DEBUG_NOGRABS},
64   {"colormap",      GDK_DEBUG_COLORMAP},
65   {"gdkrgb",        GDK_DEBUG_GDKRGB},
66   {"gc",            GDK_DEBUG_GC},
67   {"pixmap",        GDK_DEBUG_PIXMAP},
68   {"image",         GDK_DEBUG_IMAGE},
69   {"input",         GDK_DEBUG_INPUT},
70   {"cursor",        GDK_DEBUG_CURSOR},
71   {"multihead",     GDK_DEBUG_MULTIHEAD},
72 };
73
74 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
75
76 #endif /* G_ENABLE_DEBUG */
77
78 static GdkArgContext *
79 gdk_arg_context_new (gpointer cb_data)
80 {
81   GdkArgContext *result = g_new (GdkArgContext, 1);
82   result->tables = g_ptr_array_new ();
83   result->cb_data = cb_data;
84
85   return result;
86 }
87
88 static void
89 gdk_arg_context_destroy (GdkArgContext *context)
90 {
91   g_ptr_array_free (context->tables, TRUE);
92   g_free (context);
93 }
94
95 static void
96 gdk_arg_context_add_table (GdkArgContext *context, GdkArgDesc *table)
97 {
98   g_ptr_array_add (context->tables, table);
99 }
100
101 static void
102 gdk_arg_context_parse (GdkArgContext *context, gint *argc, gchar ***argv)
103 {
104   int i, j, k;
105
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] == '=' || arg[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_CALLBACK, NULL, gdk_arg_class_cb                   },
239   { "name",         GDK_ARG_CALLBACK, NULL, gdk_arg_name_cb                    },
240   { "display",      GDK_ARG_STRING,   &_gdk_display_name,     (GdkArgFunc)NULL },
241   { "screen",       GDK_ARG_INT,      &_gdk_screen_number,    (GdkArgFunc)NULL },
242
243 #ifdef G_ENABLE_DEBUG
244   { "gdk-debug",    GDK_ARG_CALLBACK, NULL, gdk_arg_debug_cb    },
245   { "gdk-no-debug", GDK_ARG_CALLBACK, NULL, gdk_arg_no_debug_cb },
246 #endif /* G_ENABLE_DEBUG */
247   { NULL }
248 };
249
250
251 /**
252  * _gdk_get_command_line_args:
253  * @argv: location to store argv pointer
254  * @argc: location 
255  * 
256  * Retrieve the command line arguments passed to gdk_init().
257  * The returned argv pointer points to static storage ; no
258  * copy is made.
259  **/
260 void
261 _gdk_get_command_line_args (int    *argc,
262                             char ***argv)
263 {
264   *argc = gdk_argc;
265   *argv = gdk_argv;
266 }
267
268 /**
269  * gdk_parse_args:
270  * @argc: the number of command line arguments.
271  * @argv: the array of command line arguments.
272  * 
273  * Parse command line arguments, and store for future
274  * use by calls to gdk_display_open().
275  *
276  * Any arguments used by GDK are removed from the array and @argc and @argv are
277  * updated accordingly.
278  *
279  * You shouldn't call this function explicitely if you are using
280  * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
281  *
282  * Since: 2.2
283  **/
284 void
285 gdk_parse_args (int    *argc,
286                 char ***argv)
287 {
288   GdkArgContext *arg_context;
289   gint i;
290
291   if (gdk_initialized)
292     return;
293
294   gdk_initialized = TRUE;
295
296   /* Save a copy of the original argc and argv */
297   if (argc && argv)
298     {
299       gdk_argc = *argc;
300       
301       gdk_argv = g_malloc ((gdk_argc + 1) * sizeof (char*));
302       for (i = 0; i < gdk_argc; i++)
303         gdk_argv[i] = g_strdup ((*argv)[i]);
304       gdk_argv[gdk_argc] = NULL;
305     }
306
307   if (argc && argv && *argc > 0)
308     {
309       gchar *d;
310       
311       d = strrchr((*argv)[0], G_DIR_SEPARATOR);
312       if (d != NULL)
313         g_set_prgname (d + 1);
314       else
315         g_set_prgname ((*argv)[0]);
316     }
317   else
318     {
319       g_set_prgname ("<unknown>");
320     }
321
322   /* We set the fallback program class here, rather than lazily in
323    * gdk_get_program_class, since we don't want -name to override it.
324    */
325   gdk_progclass = g_strdup (g_get_prgname ());
326   if (gdk_progclass[0])
327     gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
328   
329 #ifdef G_ENABLE_DEBUG
330   {
331     gchar *debug_string = getenv("GDK_DEBUG");
332     if (debug_string != NULL)
333       _gdk_debug_flags = g_parse_debug_string (debug_string,
334                                               (GDebugKey *) gdk_debug_keys,
335                                               gdk_ndebug_keys);
336   }
337 #endif  /* G_ENABLE_DEBUG */
338   
339   arg_context = gdk_arg_context_new (NULL);
340   gdk_arg_context_add_table (arg_context, gdk_args);
341   gdk_arg_context_add_table (arg_context, _gdk_windowing_args);
342   gdk_arg_context_parse (arg_context, argc, argv);
343   gdk_arg_context_destroy (arg_context);
344   
345   GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
346
347   g_type_init ();
348
349   /* Do any setup particular to the windowing system
350    */
351   _gdk_windowing_init (argc, argv);
352 }
353
354 /** 
355  * gdk_get_display_arg_name:
356  *
357  * Gets the display name specified in the command line arguments passed
358  * to gdk_init() or gdk_parse_args(), if any.
359  *
360  * Returns: the display name, if specified explicitely, otherwise %NULL
361  *   this string is owned by GTK+ and must not be modified or freed.
362  *
363  * Since: 2.2
364  */
365 G_CONST_RETURN gchar *
366 gdk_get_display_arg_name (void)
367 {
368   if (!_gdk_display_arg_name)
369     {
370       if (_gdk_screen_number >= 0)
371         _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
372       else
373         _gdk_display_arg_name = g_strdup (_gdk_display_name);
374    }
375
376    return _gdk_display_arg_name;
377 }
378
379 /**
380  * gdk_display_open_default_libgtk_only:
381  * 
382  * Opens the default display specified by command line arguments or
383  * environment variables, sets it as the default display, and returns
384  * it.  gdk_parse_args must have been called first. If the default
385  * display has previously been set, simply returns that. An internal
386  * function that should not be used by applications.
387  * 
388  * Return value: the default display, if it could be opened,
389  *   otherwise %NULL.
390  **/
391 GdkDisplay *
392 gdk_display_open_default_libgtk_only (void)
393 {
394   GdkDisplay *display;
395
396   g_return_val_if_fail (gdk_initialized, NULL);
397   
398   display = gdk_display_get_default ();
399   if (display)
400     return display;
401
402   display = gdk_display_open (gdk_get_display_arg_name ());
403
404   if (!display && _gdk_screen_number >= 0)
405     {
406       g_free (_gdk_display_arg_name);
407       _gdk_display_arg_name = g_strdup (_gdk_display_name);
408       
409       display = gdk_display_open (_gdk_display_name);
410     }
411   
412   if (display)
413     gdk_display_manager_set_default_display (gdk_display_manager_get (),
414                                              display);
415   
416   return display;
417 }
418
419 /*
420  *--------------------------------------------------------------
421  * gdk_init_check
422  *
423  *   Initialize the library for use.
424  *
425  * Arguments:
426  *   "argc" is the number of arguments.
427  *   "argv" is an array of strings.
428  *
429  * Results:
430  *   "argc" and "argv" are modified to reflect any arguments
431  *   which were not handled. (Such arguments should either
432  *   be handled by the application or dismissed). If initialization
433  *   fails, returns FALSE, otherwise TRUE.
434  *
435  * Side effects:
436  *   The library is initialized.
437  *
438  *--------------------------------------------------------------
439  */
440
441 gboolean
442 gdk_init_check (int    *argc,
443                 char ***argv)
444 {
445   gdk_parse_args (argc, argv);
446
447   return gdk_display_open_default_libgtk_only () != NULL;
448 }
449
450 void
451 gdk_init (int *argc, char ***argv)
452 {
453   if (!gdk_init_check (argc, argv))
454     {
455       g_warning ("cannot open display: %s", gdk_get_display_arg_name ());
456       exit(1);
457     }
458 }
459
460 /*
461  *--------------------------------------------------------------
462  * gdk_exit
463  *
464  *   Restores the library to an un-itialized state and exits
465  *   the program using the "exit" system call.
466  *
467  * Arguments:
468  *   "errorcode" is the error value to pass to "exit".
469  *
470  * Results:
471  *   Allocated structures are freed and the program exits
472  *   cleanly.
473  *
474  * Side effects:
475  *
476  *--------------------------------------------------------------
477  */
478
479 void
480 gdk_exit (gint errorcode)
481 {
482   exit (errorcode);
483 }
484
485 void
486 gdk_threads_enter ()
487 {
488   GDK_THREADS_ENTER ();
489 }
490
491 void
492 gdk_threads_leave ()
493 {
494   GDK_THREADS_LEAVE ();
495 }
496
497 /**
498  * gdk_threads_init:
499  * 
500  * Initializes GDK so that it can be used from multiple threads
501  * in conjunction with gdk_threads_enter() and gdk_threads_leave().
502  * g_thread_init() must be called previous to this function.
503  *
504  * This call must be made before any use of the main loop from
505  * GTK+; to be safe, call it before gtk_init().
506  **/
507 void
508 gdk_threads_init ()
509 {
510   if (!g_thread_supported ())
511     g_error ("g_thread_init() must be called before gdk_threads_init()");
512
513   gdk_threads_mutex = g_mutex_new ();
514 }
515
516 G_CONST_RETURN char *
517 gdk_get_program_class (void)
518 {
519   return gdk_progclass;
520 }
521
522 void
523 gdk_set_program_class (const char *program_class)
524 {
525   if (gdk_progclass)
526     g_free (gdk_progclass);
527
528   gdk_progclass = g_strdup (program_class);
529 }