]> Pileus Git - ~andy/gtk/blob - gdk/gdk.c
Include "config.h" instead of <config.h> Command used: find -name
[~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 typedef struct _GdkThreadsDispatch GdkThreadsDispatch;
50
51 struct _GdkThreadsDispatch
52 {
53   GSourceFunc func;
54   gpointer data;
55   GDestroyNotify destroy;
56 };
57
58
59 /* Private variable declarations
60  */
61 static int gdk_initialized = 0;                     /* 1 if the library is initialized,
62                                                      * 0 otherwise.
63                                                      */
64
65 static gchar  *gdk_progclass = NULL;
66
67 #ifdef G_ENABLE_DEBUG
68 static const GDebugKey gdk_debug_keys[] = {
69   {"events",        GDK_DEBUG_EVENTS},
70   {"misc",          GDK_DEBUG_MISC},
71   {"dnd",           GDK_DEBUG_DND},
72   {"xim",           GDK_DEBUG_XIM},
73   {"nograbs",       GDK_DEBUG_NOGRABS},
74   {"colormap",      GDK_DEBUG_COLORMAP},
75   {"gdkrgb",        GDK_DEBUG_GDKRGB},
76   {"gc",            GDK_DEBUG_GC},
77   {"pixmap",        GDK_DEBUG_PIXMAP},
78   {"image",         GDK_DEBUG_IMAGE},
79   {"input",         GDK_DEBUG_INPUT},
80   {"cursor",        GDK_DEBUG_CURSOR},
81   {"multihead",     GDK_DEBUG_MULTIHEAD},
82   {"xinerama",      GDK_DEBUG_XINERAMA},
83   {"draw",          GDK_DEBUG_DRAW}
84 };
85
86 static const int gdk_ndebug_keys = G_N_ELEMENTS (gdk_debug_keys);
87
88 #endif /* G_ENABLE_DEBUG */
89
90 #ifdef G_ENABLE_DEBUG
91 static void
92 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
93 {
94   _gdk_debug_flags |= g_parse_debug_string (value,
95                                             (GDebugKey *) gdk_debug_keys,
96                                             gdk_ndebug_keys);
97 }
98
99 static void
100 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
101 {
102   _gdk_debug_flags &= ~g_parse_debug_string (value,
103                                              (GDebugKey *) gdk_debug_keys,
104                                              gdk_ndebug_keys);
105 }
106 #endif /* G_ENABLE_DEBUG */
107
108 static gboolean
109 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
110 {
111   gdk_set_program_class (value);
112
113   return TRUE;
114 }
115
116 static gboolean
117 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
118 {
119   g_set_prgname (value);
120
121   return TRUE;
122 }
123
124 static const GOptionEntry gdk_args[] = {
125   { "class",        0, 0,                     G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
126     /* Description of --class=CLASS in --help output */        N_("Program class as used by the window manager"),
127     /* Placeholder in --class=CLASS in --help output */        N_("CLASS") },
128   { "name",         0, 0,                     G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
129     /* Description of --name=NAME in --help output */          N_("Program name as used by the window manager"),
130     /* Placeholder in --name=NAME in --help output */          N_("NAME") },
131   { "display",      0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,   &_gdk_display_name,
132     /* Description of --display=DISPLAY in --help output */    N_("X display to use"),
133     /* Placeholder in --display=DISPLAY in --help output */    N_("DISPLAY") },
134   { "screen",       0, 0, G_OPTION_ARG_INT,      &_gdk_screen_number,
135     /* Description of --screen=SCREEN in --help output */      N_("X screen to use"),
136     /* Placeholder in --screen=SCREEN in --help output */      N_("SCREEN") },
137 #ifdef G_ENABLE_DEBUG
138   { "gdk-debug",    0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,  
139     /* Description of --gdk-debug=FLAGS in --help output */    N_("Gdk debugging flags to set"),
140     /* Placeholder in --gdk-debug=FLAGS in --help output */    N_("FLAGS") },
141   { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb, 
142     /* Description of --gdk-no-debug=FLAGS in --help output */ N_("Gdk debugging flags to unset"), 
143     /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
144 #endif 
145   { NULL }
146 };
147
148 /**
149  * gdk_add_option_entries_libgtk_only:
150  * @group: An option group.
151  * 
152  * Appends gdk option entries to the passed in option group. This is
153  * not public API and must not be used by applications.
154  **/
155 void
156 gdk_add_option_entries_libgtk_only (GOptionGroup *group)
157 {
158   g_option_group_add_entries (group, gdk_args);
159   g_option_group_add_entries (group, _gdk_windowing_args);
160 }
161
162 void
163 gdk_pre_parse_libgtk_only (void)
164 {
165   gdk_initialized = TRUE;
166
167   /* We set the fallback program class here, rather than lazily in
168    * gdk_get_program_class, since we don't want -name to override it.
169    */
170   gdk_progclass = g_strdup (g_get_prgname ());
171   if (gdk_progclass && gdk_progclass[0])
172     gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
173   
174 #ifdef G_ENABLE_DEBUG
175   {
176     gchar *debug_string = getenv("GDK_DEBUG");
177     if (debug_string != NULL)
178       _gdk_debug_flags = g_parse_debug_string (debug_string,
179                                               (GDebugKey *) gdk_debug_keys,
180                                               gdk_ndebug_keys);
181   }
182 #endif  /* G_ENABLE_DEBUG */
183
184   g_type_init ();
185
186   /* Do any setup particular to the windowing system
187    */
188   _gdk_windowing_init ();  
189 }
190
191   
192 /**
193  * gdk_parse_args:
194  * @argc: the number of command line arguments.
195  * @argv: the array of command line arguments.
196  * 
197  * Parse command line arguments, and store for future
198  * use by calls to gdk_display_open().
199  *
200  * Any arguments used by GDK are removed from the array and @argc and @argv are
201  * updated accordingly.
202  *
203  * You shouldn't call this function explicitely if you are using
204  * gtk_init(), gtk_init_check(), gdk_init(), or gdk_init_check().
205  *
206  * Since: 2.2
207  **/
208 void
209 gdk_parse_args (int    *argc,
210                 char ***argv)
211 {
212   GOptionContext *option_context;
213   GOptionGroup *option_group;
214   GError *error = NULL;
215
216   if (gdk_initialized)
217     return;
218
219   gdk_pre_parse_libgtk_only ();
220   
221   option_context = g_option_context_new (NULL);
222   g_option_context_set_ignore_unknown_options (option_context, TRUE);
223   g_option_context_set_help_enabled (option_context, FALSE);
224   option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
225   g_option_context_set_main_group (option_context, option_group);
226   
227   g_option_group_add_entries (option_group, gdk_args);
228   g_option_group_add_entries (option_group, _gdk_windowing_args);
229
230   if (!g_option_context_parse (option_context, argc, argv, &error))
231     {
232       g_warning ("%s", error->message);
233       g_error_free (error);
234     }
235   g_option_context_free (option_context);
236   
237   GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
238 }
239
240 /** 
241  * gdk_get_display_arg_name:
242  *
243  * Gets the display name specified in the command line arguments passed
244  * to gdk_init() or gdk_parse_args(), if any.
245  *
246  * Returns: the display name, if specified explicitely, otherwise %NULL
247  *   this string is owned by GTK+ and must not be modified or freed.
248  *
249  * Since: 2.2
250  */
251 G_CONST_RETURN gchar *
252 gdk_get_display_arg_name (void)
253 {
254   if (!_gdk_display_arg_name)
255     {
256       if (_gdk_screen_number >= 0)
257         _gdk_display_arg_name = _gdk_windowing_substitute_screen_number (_gdk_display_name, _gdk_screen_number);
258       else
259         _gdk_display_arg_name = g_strdup (_gdk_display_name);
260    }
261
262    return _gdk_display_arg_name;
263 }
264
265 /**
266  * gdk_display_open_default_libgtk_only:
267  * 
268  * Opens the default display specified by command line arguments or
269  * environment variables, sets it as the default display, and returns
270  * it.  gdk_parse_args must have been called first. If the default
271  * display has previously been set, simply returns that. An internal
272  * function that should not be used by applications.
273  * 
274  * Return value: the default display, if it could be opened,
275  *   otherwise %NULL.
276  **/
277 GdkDisplay *
278 gdk_display_open_default_libgtk_only (void)
279 {
280   GdkDisplay *display;
281
282   g_return_val_if_fail (gdk_initialized, NULL);
283   
284   display = gdk_display_get_default ();
285   if (display)
286     return display;
287
288   display = gdk_display_open (gdk_get_display_arg_name ());
289
290   if (!display && _gdk_screen_number >= 0)
291     {
292       g_free (_gdk_display_arg_name);
293       _gdk_display_arg_name = g_strdup (_gdk_display_name);
294       
295       display = gdk_display_open (_gdk_display_name);
296     }
297   
298   if (display)
299     gdk_display_manager_set_default_display (gdk_display_manager_get (),
300                                              display);
301   
302   return display;
303 }
304
305 /*
306  *--------------------------------------------------------------
307  * gdk_init_check
308  *
309  *   Initialize the library for use.
310  *
311  * Arguments:
312  *   "argc" is the number of arguments.
313  *   "argv" is an array of strings.
314  *
315  * Results:
316  *   "argc" and "argv" are modified to reflect any arguments
317  *   which were not handled. (Such arguments should either
318  *   be handled by the application or dismissed). If initialization
319  *   fails, returns FALSE, otherwise TRUE.
320  *
321  * Side effects:
322  *   The library is initialized.
323  *
324  *--------------------------------------------------------------
325  */
326
327 gboolean
328 gdk_init_check (int    *argc,
329                 char ***argv)
330 {
331   gdk_parse_args (argc, argv);
332
333   return gdk_display_open_default_libgtk_only () != NULL;
334 }
335
336 void
337 gdk_init (int *argc, char ***argv)
338 {
339   if (!gdk_init_check (argc, argv))
340     {
341       const char *display_name = gdk_get_display_arg_name ();
342       g_warning ("cannot open display: %s", display_name ? display_name : "");
343       exit(1);
344     }
345 }
346
347 /*
348  *--------------------------------------------------------------
349  * gdk_exit
350  *
351  *   Restores the library to an un-itialized state and exits
352  *   the program using the "exit" system call.
353  *
354  * Arguments:
355  *   "errorcode" is the error value to pass to "exit".
356  *
357  * Results:
358  *   Allocated structures are freed and the program exits
359  *   cleanly.
360  *
361  * Side effects:
362  *
363  *--------------------------------------------------------------
364  */
365
366 void
367 gdk_exit (gint errorcode)
368 {
369   exit (errorcode);
370 }
371
372 void
373 gdk_threads_enter (void)
374 {
375   GDK_THREADS_ENTER ();
376 }
377
378 void
379 gdk_threads_leave (void)
380 {
381   GDK_THREADS_LEAVE ();
382 }
383
384 static void
385 gdk_threads_impl_lock (void)
386 {
387   if (gdk_threads_mutex)
388     g_mutex_lock (gdk_threads_mutex);
389 }
390
391 static void
392 gdk_threads_impl_unlock (void)
393 {
394   if (gdk_threads_mutex)
395     g_mutex_unlock (gdk_threads_mutex);
396 }
397
398 /**
399  * gdk_threads_init:
400  * 
401  * Initializes GDK so that it can be used from multiple threads
402  * in conjunction with gdk_threads_enter() and gdk_threads_leave().
403  * g_thread_init() must be called previous to this function.
404  *
405  * This call must be made before any use of the main loop from
406  * GTK+; to be safe, call it before gtk_init().
407  **/
408 void
409 gdk_threads_init (void)
410 {
411   if (!g_thread_supported ())
412     g_error ("g_thread_init() must be called before gdk_threads_init()");
413
414   gdk_threads_mutex = g_mutex_new ();
415   if (!gdk_threads_lock)
416     gdk_threads_lock = gdk_threads_impl_lock;
417   if (!gdk_threads_unlock)
418     gdk_threads_unlock = gdk_threads_impl_unlock;
419 }
420
421 /**
422  * gdk_threads_set_lock_functions:
423  * @enter_fn:   function called to guard GDK
424  * @leave_fn: function called to release the guard
425  *
426  * Allows the application to replace the standard method that
427  * GDK uses to protect its data structures. Normally, GDK
428  * creates a single #GMutex that is locked by gdk_threads_enter(),
429  * and released by gdk_threads_leave(); using this function an
430  * application provides, instead, a function @enter_fn that is
431  * called by gdk_threads_enter() and a function @leave_fn that is
432  * called by gdk_threads_leave().
433  *
434  * The functions must provide at least same locking functionality
435  * as the default implementation, but can also do extra application
436  * specific processing.
437  *
438  * As an example, consider an application that has its own recursive
439  * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
440  * the GTK+ lock when entering a recursive main loop, the application
441  * must temporarily release its lock as well.
442  *
443  * Most threaded GTK+ apps won't need to use this method.
444  *
445  * This method must be called before gdk_threads_init(), and cannot
446  * be called multiple times.
447  *
448  * Since: 2.4
449  **/
450 void
451 gdk_threads_set_lock_functions (GCallback enter_fn,
452                                 GCallback leave_fn)
453 {
454   g_return_if_fail (gdk_threads_lock == NULL &&
455                     gdk_threads_unlock == NULL);
456
457   gdk_threads_lock = enter_fn;
458   gdk_threads_unlock = leave_fn;
459 }
460
461 static gboolean
462 gdk_threads_dispatch (gpointer data)
463 {
464   GdkThreadsDispatch *dispatch = data;
465   gboolean ret = FALSE;
466
467   GDK_THREADS_ENTER ();
468
469   if (!g_source_is_destroyed (g_main_current_source ()))
470     ret = dispatch->func (dispatch->data);
471
472   GDK_THREADS_LEAVE ();
473
474   return ret;
475 }
476
477 static void
478 gdk_threads_dispatch_free (gpointer data)
479 {
480   GdkThreadsDispatch *dispatch = data;
481
482   if (dispatch->destroy && dispatch->data)
483     dispatch->destroy (dispatch->data);
484
485   g_slice_free (GdkThreadsDispatch, data);
486 }
487
488
489 /**
490  * gdk_threads_add_idle_full:
491  * @priority: the priority of the idle source. Typically this will be in the
492  *            range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
493  * @function: function to call
494  * @data:     data to pass to @function
495  * @notify:   function to call when the idle is removed, or %NULL
496  *
497  * Adds a function to be called whenever there are no higher priority
498  * events pending.  If the function returns %FALSE it is automatically
499  * removed from the list of event sources and will not be called again.
500  *
501  * This variant of g_idle_add_full() calls @function with the GDK lock
502  * held. It can be thought of a MT-safe version for GTK+ widgets for the 
503  * following use case, where you have to worry about idle_callback()
504  * running in thread A and accessing @self after it has been finalized
505  * in thread B:
506  *
507  * |[
508  * static gboolean
509  * idle_callback (gpointer data)
510  * {
511  *    /&ast; gdk_threads_enter(); would be needed for g_idle_add() &ast;/
512  *
513  *    SomeWidget *self = data;
514  *    /&ast; do stuff with self &ast;/
515  *
516  *    self->idle_id = 0;
517  *
518  *    /&ast; gdk_threads_leave(); would be needed for g_idle_add() &ast;/
519  *    return FALSE;
520  * }
521  *
522  * static void
523  * some_widget_do_stuff_later (SomeWidget *self)
524  * {
525  *    self->idle_id = gdk_threads_add_idle (idle_callback, self)
526  *    /&ast; using g_idle_add() here would require thread protection in the callback &ast;/
527  * }
528  *
529  * static void
530  * some_widget_finalize (GObject *object)
531  * {
532  *    SomeWidget *self = SOME_WIDGET (object);
533  *    if (self->idle_id)
534  *      g_source_remove (self->idle_id);
535  *    G_OBJECT_CLASS (parent_class)->finalize (object);
536  * }
537  * ]|
538  *
539  * Return value: the ID (greater than 0) of the event source.
540  *
541  * Since: 2.12
542  */
543 guint
544 gdk_threads_add_idle_full (gint           priority,
545                            GSourceFunc    function,
546                            gpointer       data,
547                            GDestroyNotify notify)
548 {
549   GdkThreadsDispatch *dispatch;
550
551   g_return_val_if_fail (function != NULL, 0);
552
553   dispatch = g_slice_new (GdkThreadsDispatch);
554   dispatch->func = function;
555   dispatch->data = data;
556   dispatch->destroy = notify;
557
558   return g_idle_add_full (priority,
559                           gdk_threads_dispatch,
560                           dispatch,
561                           gdk_threads_dispatch_free);
562 }
563
564 /**
565  * gdk_threads_add_idle:
566  * @function: function to call
567  * @data:     data to pass to @function
568  *
569  * A wrapper for the common usage of gdk_threads_add_idle_full() 
570  * assigning the default priority, #G_PRIORITY_DEFAULT_IDLE.
571  *
572  * See gdk_threads_add_idle_full().
573  *
574  * Return value: the ID (greater than 0) of the event source.
575  * 
576  * Since: 2.12
577  */
578 guint
579 gdk_threads_add_idle (GSourceFunc    function,
580                       gpointer       data)
581 {
582   return gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
583                                     function, data, NULL);
584 }
585
586
587 /**
588  * gdk_threads_add_timeout_full:
589  * @priority: the priority of the timeout source. Typically this will be in the
590  *            range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
591  * @interval: the time between calls to the function, in milliseconds
592  *             (1/1000ths of a second)
593  * @function: function to call
594  * @data:     data to pass to @function
595  * @notify:   function to call when the timeout is removed, or %NULL
596  *
597  * Sets a function to be called at regular intervals holding the GDK lock,
598  * with the given priority.  The function is called repeatedly until it 
599  * returns %FALSE, at which point the timeout is automatically destroyed 
600  * and the function will not be called again.  The @notify function is
601  * called when the timeout is destroyed.  The first call to the
602  * function will be at the end of the first @interval.
603  *
604  * Note that timeout functions may be delayed, due to the processing of other
605  * event sources. Thus they should not be relied on for precise timing.
606  * After each call to the timeout function, the time of the next
607  * timeout is recalculated based on the current time and the given interval
608  * (it does not try to 'catch up' time lost in delays).
609  *
610  * This variant of g_timeout_add_full() can be thought of a MT-safe version 
611  * for GTK+ widgets for the following use case:
612  *
613  * |[
614  * static gboolean timeout_callback (gpointer data)
615  * {
616  *    SomeWidget *self = data;
617  *    
618  *    /&ast; do stuff with self &ast;/
619  *    
620  *    self->timeout_id = 0;
621  *    
622  *    return FALSE;
623  * }
624  *  
625  * static void some_widget_do_stuff_later (SomeWidget *self)
626  * {
627  *    self->timeout_id = g_timeout_add (timeout_callback, self)
628  * }
629  *  
630  * static void some_widget_finalize (GObject *object)
631  * {
632  *    SomeWidget *self = SOME_WIDGET (object);
633  *    
634  *    if (self->timeout_id)
635  *      g_source_remove (self->timeout_id);
636  *    
637  *    G_OBJECT_CLASS (parent_class)->finalize (object);
638  * }
639  * ]|
640  *
641  * Return value: the ID (greater than 0) of the event source.
642  * 
643  * Since: 2.12
644  */
645 guint
646 gdk_threads_add_timeout_full (gint           priority,
647                               guint          interval,
648                               GSourceFunc    function,
649                               gpointer       data,
650                               GDestroyNotify notify)
651 {
652   GdkThreadsDispatch *dispatch;
653
654   g_return_val_if_fail (function != NULL, 0);
655
656   dispatch = g_slice_new (GdkThreadsDispatch);
657   dispatch->func = function;
658   dispatch->data = data;
659   dispatch->destroy = notify;
660
661   return g_timeout_add_full (priority, 
662                              interval,
663                              gdk_threads_dispatch, 
664                              dispatch, 
665                              gdk_threads_dispatch_free);
666 }
667
668 /**
669  * gdk_threads_add_timeout:
670  * @interval: the time between calls to the function, in milliseconds
671  *             (1/1000ths of a second)
672  * @function: function to call
673  * @data:     data to pass to @function
674  *
675  * A wrapper for the common usage of gdk_threads_add_timeout_full() 
676  * assigning the default priority, #G_PRIORITY_DEFAULT.
677  *
678  * See gdk_threads_add_timeout_full().
679  * 
680  * Return value: the ID (greater than 0) of the event source.
681  *
682  * Since: 2.12
683  */
684 guint
685 gdk_threads_add_timeout (guint       interval,
686                          GSourceFunc function,
687                          gpointer    data)
688 {
689   return gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
690                                        interval, function, data, NULL);
691 }
692
693
694 G_CONST_RETURN char *
695 gdk_get_program_class (void)
696 {
697   return gdk_progclass;
698 }
699
700 void
701 gdk_set_program_class (const char *program_class)
702 {
703   g_free (gdk_progclass);
704
705   gdk_progclass = g_strdup (program_class);
706 }
707
708 #define __GDK_C__
709 #include "gdkaliasdef.c"