]> Pileus Git - ~andy/gtk/blob - gdk/gdk.c
Fix an oversight
[~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 #include "gdkintl.h"
35
36 #ifndef HAVE_XCONVERTCASE
37 #include "gdkkeysyms.h"
38 #endif
39 #include "gdkalias.h"
40
41 typedef struct _GdkPredicate  GdkPredicate;
42
43 struct _GdkPredicate
44 {
45   GdkEventFunc func;
46   gpointer data;
47 };
48
49 /* Private variable declarations
50  */
51 static int gdk_initialized = 0;                     /* 1 if the library is initialized,
52                                                      * 0 otherwise.
53                                                      */
54
55 static gchar  *gdk_progclass = 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   {"xinerama",      GDK_DEBUG_XINERAMA},
73   {"draw",          GDK_DEBUG_DRAW}
74 };
75
76 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
77
78 #endif /* G_ENABLE_DEBUG */
79
80 #ifdef G_ENABLE_DEBUG
81 static void
82 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
83 {
84   _gdk_debug_flags |= g_parse_debug_string (value,
85                                             (GDebugKey *) gdk_debug_keys,
86                                             gdk_ndebug_keys);
87 }
88
89 static void
90 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
91 {
92   _gdk_debug_flags &= ~g_parse_debug_string (value,
93                                              (GDebugKey *) gdk_debug_keys,
94                                              gdk_ndebug_keys);
95 }
96 #endif /* G_ENABLE_DEBUG */
97
98 static gboolean
99 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
100 {
101   gdk_set_program_class (value);
102
103   return TRUE;
104 }
105
106 static gboolean
107 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
108 {
109   g_set_prgname (value);
110
111   return TRUE;
112 }
113
114 static GOptionEntry gdk_args[] = {
115   { "class",        0, 0,                     G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
116     /* Description of --class=CLASS in --help output */        N_("Program class as used by the window manager"),
117     /* Placeholder in --class=CLASS in --help output */        N_("CLASS") },
118   { "name",         0, 0,                     G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
119     /* Description of --name=NAME in --help output */          N_("Program name as used by the window manager"),
120     /* Placeholder in --name=NAME in --help output */          N_("NAME") },
121   { "display",      0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,   &_gdk_display_name,
122     /* Description of --display=DISPLAY in --help output */    N_("X display to use"),
123     /* Placeholder in --display=DISPLAY in --help output */    N_("DISPLAY") },
124   { "screen",       0, 0, G_OPTION_ARG_INT,      &_gdk_screen_number,
125     /* Description of --screen=SCREEN in --help output */      N_("X screen to use"),
126     /* Placeholder in --screen=SCREEN in --help output */      N_("SCREEN") },
127 #ifdef G_ENABLE_DEBUG
128   { "gdk-debug",    0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,  
129     /* Description of --gdk-debug=FLAGS in --help output */    N_("Gdk debugging flags to set"),
130     /* Placeholder in --gdk-debug=FLAGS in --help output */    N_("FLAGS") },
131   { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb, 
132     /* Description of --gdk-no-debug=FLAGS in --help output */ N_("Gdk debugging flags to unset"), 
133     /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
134 #endif 
135   { NULL }
136 };
137
138 /**
139  * gdk_add_option_entries_libgtk_only:
140  * @group: An option group.
141  * 
142  * Appends gdk option entries to the passed in option group. This is
143  * not public API and must not be used by applications.
144  **/
145 void
146 gdk_add_option_entries_libgtk_only (GOptionGroup *group)
147 {
148   g_option_group_add_entries (group, gdk_args);
149   g_option_group_add_entries (group, _gdk_windowing_args);
150 }
151
152 void
153 gdk_pre_parse_libgtk_only (void)
154 {
155   gdk_initialized = TRUE;
156
157   /* We set the fallback program class here, rather than lazily in
158    * gdk_get_program_class, since we don't want -name to override it.
159    */
160   gdk_progclass = g_strdup (g_get_prgname ());
161   if (gdk_progclass && gdk_progclass[0])
162     gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
163   
164 #ifdef G_ENABLE_DEBUG
165   {
166     gchar *debug_string = getenv("GDK_DEBUG");
167     if (debug_string != NULL)
168       _gdk_debug_flags = g_parse_debug_string (debug_string,
169                                               (GDebugKey *) gdk_debug_keys,
170                                               gdk_ndebug_keys);
171   }
172 #endif  /* G_ENABLE_DEBUG */
173
174   g_type_init ();
175
176   /* Do any setup particular to the windowing system
177    */
178   _gdk_windowing_init ();  
179 }
180
181   
182 /**
183  * gdk_parse_args:
184  * @argc: the number of command line arguments.
185  * @argv: the array of command line arguments.
186  * 
187  * Parse command line arguments, and store for future
188  * use by calls to gdk_display_open().
189  *
190  * Any arguments used by GDK are removed from the array and @argc and @argv are
191  * updated accordingly.
192  *
193  * You shouldn't call this function explicitely if you are using
194  * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
195  *
196  * Since: 2.2
197  **/
198 void
199 gdk_parse_args (int    *argc,
200                 char ***argv)
201 {
202   GOptionContext *option_context;
203   GOptionGroup *option_group;
204   GError *error = NULL;
205
206   if (gdk_initialized)
207     return;
208
209   gdk_pre_parse_libgtk_only ();
210   
211   option_context = g_option_context_new (NULL);
212   g_option_context_set_ignore_unknown_options (option_context, TRUE);
213   g_option_context_set_help_enabled (option_context, FALSE);
214   option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
215   g_option_context_set_main_group (option_context, option_group);
216   
217   g_option_group_add_entries (option_group, gdk_args);
218   g_option_group_add_entries (option_group, _gdk_windowing_args);
219
220   if (!g_option_context_parse (option_context, argc, argv, &error))
221     {
222       g_warning ("%s", error->message);
223       g_error_free (error);
224     }
225   g_option_context_free (option_context);
226   
227   GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
228 }
229
230 /** 
231  * gdk_get_display_arg_name:
232  *
233  * Gets the display name specified in the command line arguments passed
234  * to gdk_init() or gdk_parse_args(), if any.
235  *
236  * Returns: the display name, if specified explicitely, otherwise %NULL
237  *   this string is owned by GTK+ and must not be modified or freed.
238  *
239  * Since: 2.2
240  */
241 G_CONST_RETURN gchar *
242 gdk_get_display_arg_name (void)
243 {
244   if (!_gdk_display_arg_name)
245     {
246       if (_gdk_screen_number >= 0)
247         _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
248       else
249         _gdk_display_arg_name = g_strdup (_gdk_display_name);
250    }
251
252    return _gdk_display_arg_name;
253 }
254
255 /**
256  * gdk_display_open_default_libgtk_only:
257  * 
258  * Opens the default display specified by command line arguments or
259  * environment variables, sets it as the default display, and returns
260  * it.  gdk_parse_args must have been called first. If the default
261  * display has previously been set, simply returns that. An internal
262  * function that should not be used by applications.
263  * 
264  * Return value: the default display, if it could be opened,
265  *   otherwise %NULL.
266  **/
267 GdkDisplay *
268 gdk_display_open_default_libgtk_only (void)
269 {
270   GdkDisplay *display;
271
272   g_return_val_if_fail (gdk_initialized, NULL);
273   
274   display = gdk_display_get_default ();
275   if (display)
276     return display;
277
278   display = gdk_display_open (gdk_get_display_arg_name ());
279
280   if (!display && _gdk_screen_number >= 0)
281     {
282       g_free (_gdk_display_arg_name);
283       _gdk_display_arg_name = g_strdup (_gdk_display_name);
284       
285       display = gdk_display_open (_gdk_display_name);
286     }
287   
288   if (display)
289     gdk_display_manager_set_default_display (gdk_display_manager_get (),
290                                              display);
291   
292   return display;
293 }
294
295 /*
296  *--------------------------------------------------------------
297  * gdk_init_check
298  *
299  *   Initialize the library for use.
300  *
301  * Arguments:
302  *   "argc" is the number of arguments.
303  *   "argv" is an array of strings.
304  *
305  * Results:
306  *   "argc" and "argv" are modified to reflect any arguments
307  *   which were not handled. (Such arguments should either
308  *   be handled by the application or dismissed). If initialization
309  *   fails, returns FALSE, otherwise TRUE.
310  *
311  * Side effects:
312  *   The library is initialized.
313  *
314  *--------------------------------------------------------------
315  */
316
317 gboolean
318 gdk_init_check (int    *argc,
319                 char ***argv)
320 {
321   gdk_parse_args (argc, argv);
322
323   return gdk_display_open_default_libgtk_only () != NULL;
324 }
325
326 void
327 gdk_init (int *argc, char ***argv)
328 {
329   if (!gdk_init_check (argc, argv))
330     {
331       g_warning ("cannot open display: %s", gdk_get_display_arg_name ());
332       exit(1);
333     }
334 }
335
336 /*
337  *--------------------------------------------------------------
338  * gdk_exit
339  *
340  *   Restores the library to an un-itialized state and exits
341  *   the program using the "exit" system call.
342  *
343  * Arguments:
344  *   "errorcode" is the error value to pass to "exit".
345  *
346  * Results:
347  *   Allocated structures are freed and the program exits
348  *   cleanly.
349  *
350  * Side effects:
351  *
352  *--------------------------------------------------------------
353  */
354
355 void
356 gdk_exit (gint errorcode)
357 {
358   exit (errorcode);
359 }
360
361 void
362 gdk_threads_enter (void)
363 {
364   GDK_THREADS_ENTER ();
365 }
366
367 void
368 gdk_threads_leave (void)
369 {
370   GDK_THREADS_LEAVE ();
371 }
372
373 static void
374 gdk_threads_impl_lock (void)
375 {
376   if (gdk_threads_mutex)
377     g_mutex_lock (gdk_threads_mutex);
378 }
379
380 static void
381 gdk_threads_impl_unlock (void)
382 {
383   if (gdk_threads_mutex)
384     g_mutex_unlock (gdk_threads_mutex);
385 }
386
387 /**
388  * gdk_threads_init:
389  * 
390  * Initializes GDK so that it can be used from multiple threads
391  * in conjunction with gdk_threads_enter() and gdk_threads_leave().
392  * g_thread_init() must be called previous to this function.
393  *
394  * This call must be made before any use of the main loop from
395  * GTK+; to be safe, call it before gtk_init().
396  **/
397 void
398 gdk_threads_init (void)
399 {
400   if (!g_thread_supported ())
401     g_error ("g_thread_init() must be called before gdk_threads_init()");
402
403   gdk_threads_mutex = g_mutex_new ();
404   if (!gdk_threads_lock)
405     gdk_threads_lock = gdk_threads_impl_lock;
406   if (!gdk_threads_unlock)
407     gdk_threads_unlock = gdk_threads_impl_unlock;
408 }
409
410 /**
411  * gdk_threads_set_lock_functions:
412  * @enter_fn:   function called to guard GDK
413  * @leave_fn: function called to release the guard
414  *
415  * Allows the application to replace the standard method that
416  * GDK uses to protect its data structures. Normally, GDK
417  * creates a single #GMutex that is locked by gdk_threads_enter(),
418  * and released by gdk_threads_leave(); using this function an
419  * application provides, instead, a function @enter_fn that is
420  * called by gdk_threads_enter() and a function @leave_fn that is
421  * called by gdk_threads_leave().
422  *
423  * The functions must provide at least same locking functionality
424  * as the default implementation, but can also do extra application
425  * specific processing.
426  *
427  * As an example, consider an application that has its own recursive
428  * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
429  * the GTK+ lock when entering a recursive main loop, the application
430  * must temporarily release its lock as well.
431  *
432  * Most threaded GTK+ apps won't need to use this method.
433  *
434  * This method must be called before gdk_threads_init(), and cannot
435  * be called multiple times.
436  *
437  * Since: 2.4
438  **/
439 void
440 gdk_threads_set_lock_functions (GCallback enter_fn,
441                                 GCallback leave_fn)
442 {
443   g_return_if_fail (gdk_threads_lock == NULL &&
444                     gdk_threads_unlock == NULL);
445
446   gdk_threads_lock = enter_fn;
447   gdk_threads_unlock = leave_fn;
448 }
449
450 G_CONST_RETURN char *
451 gdk_get_program_class (void)
452 {
453   return gdk_progclass;
454 }
455
456 void
457 gdk_set_program_class (const char *program_class)
458 {
459   if (gdk_progclass)
460     g_free (gdk_progclass);
461
462   gdk_progclass = g_strdup (program_class);
463 }
464
465 #define __GDK_C__
466 #include "gdkaliasdef.c"