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