]> Pileus Git - ~andy/gtk/blob - gdk/gdk.c
[GI] Add missing (out) and (array) annotations
[~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 "gdkmain.h"
30
31 #include "gdkinternals.h"
32 #include "gdkintl.h"
33
34 #ifndef HAVE_XCONVERTCASE
35 #include "gdkkeysyms.h"
36 #endif
37
38 #include <string.h>
39 #include <stdlib.h>
40
41
42 /**
43  * SECTION:general
44  * @Short_description: Library initialization and miscellaneous functions
45  * @Title: General
46  *
47  * This section describes the GDK initialization functions and miscellaneous
48  * utility functions.
49  */
50
51 /**
52  * GDK_WINDOWING_X11:
53  *
54  * The #GDK_WINDOWING_X11 macro is defined if the X11 backend
55  * is supported.
56  *
57  * Use this macro to guard code that is specific to the X11-backend.
58  * Since GDK may be configured with multiple backends, an additional
59  * runtime check for the used backend is recommended:
60  *
61  * |[
62  * #ifdef GDK_WINDOWING_X11
63  *   if (GDK_IS_X11_DISPLAY (display))
64  *     {
65  *       /&ast; make X11-specific calls here &ast;/
66  *     }
67  *   else
68  * #endif
69  * #ifdef GDK_WINDOWING_QUARTZ
70  *   if (GDK_IS_QUARTZ_DISPLAY (display))
71  *     {
72  *       /&ast; make Quartz-specific calls here &ast/
73  *     }
74  *   else
75  * #endif
76  *   g_error ("Unsupported GDK backend");
77  * ]|
78  */
79
80 /**
81  * GDK_WINDOWING_WIN32:
82  *
83  * The #GDK_WINDOWING_WIN32 macro is defined if the Win32 backend
84  * is supported.
85  */
86
87 /**
88  * GDK_WINDOWING_QUARTZ:
89  *
90  * The #GDK_WINDOWING_QUARTZ macro is defined if the Quartz backend
91  * is supported.
92  */
93
94 typedef struct _GdkPredicate  GdkPredicate;
95
96 struct _GdkPredicate
97 {
98   GdkEventFunc func;
99   gpointer data;
100 };
101
102 typedef struct _GdkThreadsDispatch GdkThreadsDispatch;
103
104 struct _GdkThreadsDispatch
105 {
106   GSourceFunc func;
107   gpointer data;
108   GDestroyNotify destroy;
109 };
110
111
112 /* Private variable declarations
113  */
114 static int gdk_initialized = 0;                     /* 1 if the library is initialized,
115                                                      * 0 otherwise.
116                                                      */
117
118 static gchar  *gdk_progclass = NULL;
119
120 static GMutex *gdk_threads_mutex = NULL;            /* Global GDK lock */
121
122 static GCallback gdk_threads_lock = NULL;
123 static GCallback gdk_threads_unlock = NULL;
124
125 #ifdef G_ENABLE_DEBUG
126 static const GDebugKey gdk_debug_keys[] = {
127   {"events",        GDK_DEBUG_EVENTS},
128   {"misc",          GDK_DEBUG_MISC},
129   {"dnd",           GDK_DEBUG_DND},
130   {"xim",           GDK_DEBUG_XIM},
131   {"nograbs",       GDK_DEBUG_NOGRABS},
132   {"colormap",      GDK_DEBUG_COLORMAP},
133   {"input",         GDK_DEBUG_INPUT},
134   {"cursor",        GDK_DEBUG_CURSOR},
135   {"multihead",     GDK_DEBUG_MULTIHEAD},
136   {"xinerama",      GDK_DEBUG_XINERAMA},
137   {"draw",          GDK_DEBUG_DRAW},
138   {"eventloop",     GDK_DEBUG_EVENTLOOP}
139 };
140
141 static gboolean
142 gdk_arg_debug_cb (const char *key, const char *value, gpointer user_data, GError **error)
143 {
144   guint debug_value = g_parse_debug_string (value,
145                                             (GDebugKey *) gdk_debug_keys,
146                                             G_N_ELEMENTS (gdk_debug_keys));
147
148   if (debug_value == 0 && value != NULL && strcmp (value, "") != 0)
149     {
150       g_set_error (error,
151                    G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
152                    _("Error parsing option --gdk-debug"));
153       return FALSE;
154     }
155
156   _gdk_debug_flags |= debug_value;
157
158   return TRUE;
159 }
160
161 static gboolean
162 gdk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data, GError **error)
163 {
164   guint debug_value = g_parse_debug_string (value,
165                                             (GDebugKey *) gdk_debug_keys,
166                                             G_N_ELEMENTS (gdk_debug_keys));
167
168   if (debug_value == 0 && value != NULL && strcmp (value, "") != 0)
169     {
170       g_set_error (error,
171                    G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
172                    _("Error parsing option --gdk-no-debug"));
173       return FALSE;
174     }
175
176   _gdk_debug_flags &= ~debug_value;
177
178   return TRUE;
179 }
180 #endif /* G_ENABLE_DEBUG */
181
182 static gboolean
183 gdk_arg_class_cb (const char *key, const char *value, gpointer user_data, GError **error)
184 {
185   gdk_set_program_class (value);
186
187   return TRUE;
188 }
189
190 static gboolean
191 gdk_arg_name_cb (const char *key, const char *value, gpointer user_data, GError **error)
192 {
193   g_set_prgname (value);
194
195   return TRUE;
196 }
197
198 static const GOptionEntry gdk_args[] = {
199   { "class",        0, 0,                     G_OPTION_ARG_CALLBACK, gdk_arg_class_cb,
200     /* Description of --class=CLASS in --help output */        N_("Program class as used by the window manager"),
201     /* Placeholder in --class=CLASS in --help output */        N_("CLASS") },
202   { "name",         0, 0,                     G_OPTION_ARG_CALLBACK, gdk_arg_name_cb,
203     /* Description of --name=NAME in --help output */          N_("Program name as used by the window manager"),
204     /* Placeholder in --name=NAME in --help output */          N_("NAME") },
205   { "display",      0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,   &_gdk_display_name,
206     /* Description of --display=DISPLAY in --help output */    N_("X display to use"),
207     /* Placeholder in --display=DISPLAY in --help output */    N_("DISPLAY") },
208 #ifdef G_ENABLE_DEBUG
209   { "gdk-debug",    0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_debug_cb,  
210     /* Description of --gdk-debug=FLAGS in --help output */    N_("GDK debugging flags to set"),
211     /* Placeholder in --gdk-debug=FLAGS in --help output */    N_("FLAGS") },
212   { "gdk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gdk_arg_no_debug_cb, 
213     /* Description of --gdk-no-debug=FLAGS in --help output */ N_("GDK debugging flags to unset"),
214     /* Placeholder in --gdk-no-debug=FLAGS in --help output */ N_("FLAGS") },
215 #endif 
216   { NULL }
217 };
218
219 /**
220  * gdk_add_option_entries_libgtk_only:
221  * @group: An option group.
222  *
223  * Appends gdk option entries to the passed in option group. This is
224  * not public API and must not be used by applications.
225  */
226 void
227 gdk_add_option_entries_libgtk_only (GOptionGroup *group)
228 {
229   g_option_group_add_entries (group, gdk_args);
230 }
231
232 void
233 gdk_pre_parse_libgtk_only (void)
234 {
235   gdk_initialized = TRUE;
236
237   /* We set the fallback program class here, rather than lazily in
238    * gdk_get_program_class, since we don't want -name to override it.
239    */
240   gdk_progclass = g_strdup (g_get_prgname ());
241   if (gdk_progclass && gdk_progclass[0])
242     gdk_progclass[0] = g_ascii_toupper (gdk_progclass[0]);
243   
244 #ifdef G_ENABLE_DEBUG
245   {
246     gchar *debug_string = getenv("GDK_DEBUG");
247     if (debug_string != NULL)
248       _gdk_debug_flags = g_parse_debug_string (debug_string,
249                                               (GDebugKey *) gdk_debug_keys,
250                                               G_N_ELEMENTS (gdk_debug_keys));
251   }
252 #endif  /* G_ENABLE_DEBUG */
253
254   if (getenv ("GDK_NATIVE_WINDOWS"))
255     {
256       _gdk_native_windows = TRUE;
257       /* Ensure that this is not propagated to spawned applications */
258       g_unsetenv ("GDK_NATIVE_WINDOWS");
259     }
260
261   g_type_init ();
262
263   /* Do any setup particular to the windowing system */
264   gdk_display_manager_get ();
265 }
266
267   
268 /**
269  * gdk_parse_args:
270  * @argc: the number of command line arguments.
271  * @argv: (inout) (array length=argc): 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   GOptionContext *option_context;
289   GOptionGroup *option_group;
290   GError *error = NULL;
291
292   if (gdk_initialized)
293     return;
294
295   gdk_pre_parse_libgtk_only ();
296
297   option_context = g_option_context_new (NULL);
298   g_option_context_set_ignore_unknown_options (option_context, TRUE);
299   g_option_context_set_help_enabled (option_context, FALSE);
300   option_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
301   g_option_context_set_main_group (option_context, option_group);
302
303   g_option_group_add_entries (option_group, gdk_args);
304
305   if (!g_option_context_parse (option_context, argc, argv, &error))
306     {
307       g_warning ("%s", error->message);
308       g_error_free (error);
309     }
310   g_option_context_free (option_context);
311
312   GDK_NOTE (MISC, g_message ("progname: \"%s\"", g_get_prgname ()));
313 }
314
315 /**
316  * gdk_get_display_arg_name:
317  *
318  * Gets the display name specified in the command line arguments passed
319  * to gdk_init() or gdk_parse_args(), if any.
320  *
321  * Returns: the display name, if specified explicitely, otherwise %NULL
322  *   this string is owned by GTK+ and must not be modified or freed.
323  *
324  * Since: 2.2
325  */
326 G_CONST_RETURN gchar *
327 gdk_get_display_arg_name (void)
328 {
329   if (!_gdk_display_arg_name)
330     _gdk_display_arg_name = g_strdup (_gdk_display_name);
331
332    return _gdk_display_arg_name;
333 }
334
335 /**
336  * gdk_display_open_default_libgtk_only:
337  *
338  * Opens the default display specified by command line arguments or
339  * environment variables, sets it as the default display, and returns
340  * it.  gdk_parse_args must have been called first. If the default
341  * display has previously been set, simply returns that. An internal
342  * function that should not be used by applications.
343  *
344  * Return value: (transfer none): the default display, if it could be
345  *   opened, otherwise %NULL.
346  **/
347 GdkDisplay *
348 gdk_display_open_default_libgtk_only (void)
349 {
350   GdkDisplay *display;
351
352   g_return_val_if_fail (gdk_initialized, NULL);
353
354   display = gdk_display_get_default ();
355   if (display)
356     return display;
357
358   display = gdk_display_open (gdk_get_display_arg_name ());
359
360   return display;
361 }
362
363 /**
364  * gdk_init_check:
365  * @argc: (inout): the number of command line arguments.
366  * @argv: (array length=argc) (inout): the array of command line arguments.
367  *
368  * Initializes the GDK library and connects to the windowing system,
369  * returning %TRUE on success.
370  *
371  * Any arguments used by GDK are removed from the array and @argc and @argv
372  * are updated accordingly.
373  *
374  * GTK+ initializes GDK in gtk_init() and so this function is not usually
375  * needed by GTK+ applications.
376  *
377  * Returns: %TRUE if initialization succeeded.
378  */
379 gboolean
380 gdk_init_check (int    *argc,
381                 char ***argv)
382 {
383   gdk_parse_args (argc, argv);
384
385   return gdk_display_open_default_libgtk_only () != NULL;
386 }
387
388
389 /**
390  * gdk_init:
391  * @argc: (inout): the number of command line arguments.
392  * @argv: (array length=argc) (inout): the array of command line arguments.
393  *
394  * Initializes the GDK library and connects to the windowing system.
395  * If initialization fails, a warning message is output and the application
396  * terminates with a call to <literal>exit(1)</literal>.
397  *
398  * Any arguments used by GDK are removed from the array and @argc and @argv
399  * are updated accordingly.
400  *
401  * GTK+ initializes GDK in gtk_init() and so this function is not usually
402  * needed by GTK+ applications.
403  */
404 void
405 gdk_init (int *argc, char ***argv)
406 {
407   if (!gdk_init_check (argc, argv))
408     {
409       const char *display_name = gdk_get_display_arg_name ();
410       g_warning ("cannot open display: %s", display_name ? display_name : "");
411       exit(1);
412     }
413 }
414
415
416
417 /**
418  * SECTION:threads
419  * @Short_description: Functions for using GDK in multi-threaded programs
420  * @Title: Threads
421  *
422  * For thread safety, GDK relies on the thread primitives in GLib,
423  * and on the thread-safe GLib main loop.
424  *
425  * GLib is completely thread safe (all global data is automatically
426  * locked), but individual data structure instances are not automatically
427  * locked for performance reasons. So e.g. you must coordinate
428  * accesses to the same #GHashTable from multiple threads.
429  *
430  * GTK+ is "thread aware" but not thread safe &mdash; it provides a
431  * global lock controlled by gdk_threads_enter()/gdk_threads_leave()
432  * which protects all use of GTK+. That is, only one thread can use GTK+
433  * at any given time.
434  *
435  * Unfortunately the above holds with the X11 backend only. With the
436  * Win32 backend, GDK calls should not be attempted from multiple threads
437  * at all.
438  *
439  * You must call g_thread_init() and gdk_threads_init() before executing
440  * any other GTK+ or GDK functions in a threaded GTK+ program.
441  *
442  * Idles, timeouts, and input functions from GLib, such as g_idle_add(), are
443  * executed outside of the main GTK+ lock.
444  * So, if you need to call GTK+ inside of such a callback, you must surround
445  * the callback with a gdk_threads_enter()/gdk_threads_leave() pair or use
446  * gdk_threads_add_idle_full() which does this for you.
447  * However, event dispatching from the mainloop is still executed within
448  * the main GTK+ lock, so callback functions connected to event signals
449  * like #GtkWidget::button-press-event, do not need thread protection.
450  *
451  * In particular, this means, if you are writing widgets that might
452  * be used in threaded programs, you <emphasis>must</emphasis> surround
453  * timeouts and idle functions in this matter.
454  *
455  * As always, you must also surround any calls to GTK+ not made within
456  * a signal handler with a gdk_threads_enter()/gdk_threads_leave() pair.
457  *
458  * Before calling gdk_threads_leave() from a thread other
459  * than your main thread, you probably want to call gdk_flush()
460  * to send all pending commands to the windowing system.
461  * (The reason you don't need to do this from the main thread
462  * is that GDK always automatically flushes pending commands
463  * when it runs out of incoming events to process and has
464  * to sleep while waiting for more events.)
465  *
466  * A minimal main program for a threaded GTK+ application
467  * looks like:
468  * <informalexample>
469  * <programlisting role="C">
470  * int
471  * main (int argc, char *argv[])
472  * {
473  *   GtkWidget *window;
474  *
475  *   g_thread_init (NULL);
476  *   gdk_threads_init (<!-- -->);
477  *   gdk_threads_enter (<!-- -->);
478  *
479  *   gtk_init (&argc, &argv);
480  *
481  *   window = create_window (<!-- -->);
482  *   gtk_widget_show (window);
483  *
484  *   gtk_main (<!-- -->);
485  *   gdk_threads_leave (<!-- -->);
486  *
487  *   return 0;
488  * }
489  * </programlisting>
490  * </informalexample>
491  *
492  * Callbacks require a bit of attention. Callbacks from GTK+ signals
493  * are made within the GTK+ lock. However callbacks from GLib (timeouts,
494  * IO callbacks, and idle functions) are made outside of the GTK+
495  * lock. So, within a signal handler you do not need to call
496  * gdk_threads_enter(), but within the other types of callbacks, you
497  * do.
498  *
499  * Erik Mouw contributed the following code example to
500  * illustrate how to use threads within GTK+ programs.
501  * <informalexample>
502  * <programlisting role="C">
503  * /<!---->*-------------------------------------------------------------------------
504  *  * Filename:      gtk-thread.c
505  *  * Version:       0.99.1
506  *  * Copyright:     Copyright (C) 1999, Erik Mouw
507  *  * Author:        Erik Mouw &lt;J.A.K.Mouw@its.tudelft.nl&gt;
508  *  * Description:   GTK threads example.
509  *  * Created at:    Sun Oct 17 21:27:09 1999
510  *  * Modified by:   Erik Mouw &lt;J.A.K.Mouw@its.tudelft.nl&gt;
511  *  * Modified at:   Sun Oct 24 17:21:41 1999
512  *  *-----------------------------------------------------------------------*<!---->/
513  * /<!---->*
514  *  * Compile with:
515  *  *
516  *  * cc -o gtk-thread gtk-thread.c `gtk-config --cflags --libs gthread`
517  *  *
518  *  * Thanks to Sebastian Wilhelmi and Owen Taylor for pointing out some
519  *  * bugs.
520  *  *
521  *  *<!---->/
522  *
523  * #include <stdio.h>
524  * #include <stdlib.h>
525  * #include <unistd.h>
526  * #include <time.h>
527  * #include <gtk/gtk.h>
528  * #include <glib.h>
529  * #include <pthread.h>
530  *
531  * #define YES_IT_IS    (1)
532  * #define NO_IT_IS_NOT (0)
533  *
534  * typedef struct
535  * {
536  *   GtkWidget *label;
537  *   int what;
538  * } yes_or_no_args;
539  *
540  * G_LOCK_DEFINE_STATIC (yes_or_no);
541  * static volatile int yes_or_no = YES_IT_IS;
542  *
543  * void destroy (GtkWidget *widget, gpointer data)
544  * {
545  *   gtk_main_quit (<!-- -->);
546  * }
547  *
548  * void *argument_thread (void *args)
549  * {
550  *   yes_or_no_args *data = (yes_or_no_args *)args;
551  *   gboolean say_something;
552  *
553  *   for (;;)
554  *     {
555  *       /<!---->* sleep a while *<!---->/
556  *       sleep(rand(<!-- -->) / (RAND_MAX / 3) + 1);
557  *
558  *       /<!---->* lock the yes_or_no_variable *<!---->/
559  *       G_LOCK(yes_or_no);
560  *
561  *       /<!---->* do we have to say something? *<!---->/
562  *       say_something = (yes_or_no != data->what);
563  *
564  *       if(say_something)
565  *      {
566  *        /<!---->* set the variable *<!---->/
567  *        yes_or_no = data->what;
568  *      }
569  *
570  *       /<!---->* Unlock the yes_or_no variable *<!---->/
571  *       G_UNLOCK (yes_or_no);
572  *
573  *       if (say_something)
574  *      {
575  *        /<!---->* get GTK thread lock *<!---->/
576  *        gdk_threads_enter (<!-- -->);
577  *
578  *        /<!---->* set label text *<!---->/
579  *        if(data->what == YES_IT_IS)
580  *          gtk_label_set_text (GTK_LABEL (data->label), "O yes, it is!");
581  *        else
582  *          gtk_label_set_text (GTK_LABEL (data->label), "O no, it isn't!");
583  *
584  *        /<!---->* release GTK thread lock *<!---->/
585  *        gdk_threads_leave (<!-- -->);
586  *      }
587  *     }
588  *
589  *   return NULL;
590  * }
591  *
592  * int main (int argc, char *argv[])
593  * {
594  *   GtkWidget *window;
595  *   GtkWidget *label;
596  *   yes_or_no_args yes_args, no_args;
597  *   pthread_t no_tid, yes_tid;
598  *
599  *   /<!---->* init threads *<!---->/
600  *   g_thread_init (NULL);
601  *   gdk_threads_init (<!-- -->);
602  *   gdk_threads_enter (<!-- -->);
603  *
604  *   /<!---->* init gtk *<!---->/
605  *   gtk_init(&argc, &argv);
606  *
607  *   /<!---->* init random number generator *<!---->/
608  *   srand ((unsigned int) time (NULL));
609  *
610  *   /<!---->* create a window *<!---->/
611  *   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
612  *
613  *   g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
614  *
615  *   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
616  *
617  *   /<!---->* create a label *<!---->/
618  *   label = gtk_label_new ("And now for something completely different ...");
619  *   gtk_container_add (GTK_CONTAINER (window), label);
620  *
621  *   /<!---->* show everything *<!---->/
622  *   gtk_widget_show (label);
623  *   gtk_widget_show (window);
624  *
625  *   /<!---->* create the threads *<!---->/
626  *   yes_args.label = label;
627  *   yes_args.what = YES_IT_IS;
628  *   pthread_create (&yes_tid, NULL, argument_thread, &yes_args);
629  *
630  *   no_args.label = label;
631  *   no_args.what = NO_IT_IS_NOT;
632  *   pthread_create (&no_tid, NULL, argument_thread, &no_args);
633  *
634  *   /<!---->* enter the GTK main loop *<!---->/
635  *   gtk_main (<!-- -->);
636  *   gdk_threads_leave (<!-- -->);
637  *
638  *   return 0;
639  * }
640  * </programlisting>
641  * </informalexample>
642  */
643
644
645 /**
646  * gdk_threads_enter:
647  *
648  * This macro marks the beginning of a critical section in which GDK and
649  * GTK+ functions can be called safely and without causing race
650  * conditions.  Only one thread at a time can be in such a critial
651  * section.
652  */
653 void
654 gdk_threads_enter (void)
655 {
656   if (gdk_threads_lock)
657     (*gdk_threads_lock) ();
658 }
659
660 /**
661  * gdk_threads_leave:
662  *
663  * Leaves a critical region begun with gdk_threads_enter().
664  */
665 void
666 gdk_threads_leave (void)
667 {
668   if (gdk_threads_unlock)
669     (*gdk_threads_unlock) ();
670 }
671
672 static void
673 gdk_threads_impl_lock (void)
674 {
675   if (gdk_threads_mutex)
676     g_mutex_lock (gdk_threads_mutex);
677 }
678
679 static void
680 gdk_threads_impl_unlock (void)
681 {
682   if (gdk_threads_mutex)
683     g_mutex_unlock (gdk_threads_mutex);
684 }
685
686 /**
687  * gdk_threads_init:
688  *
689  * Initializes GDK so that it can be used from multiple threads
690  * in conjunction with gdk_threads_enter() and gdk_threads_leave().
691  * g_thread_init() must be called previous to this function.
692  *
693  * This call must be made before any use of the main loop from
694  * GTK+; to be safe, call it before gtk_init().
695  **/
696 void
697 gdk_threads_init (void)
698 {
699   if (!g_thread_supported ())
700     g_error ("g_thread_init() must be called before gdk_threads_init()");
701
702   gdk_threads_mutex = g_mutex_new ();
703   if (!gdk_threads_lock)
704     gdk_threads_lock = gdk_threads_impl_lock;
705   if (!gdk_threads_unlock)
706     gdk_threads_unlock = gdk_threads_impl_unlock;
707 }
708
709 /**
710  * gdk_threads_set_lock_functions:
711  * @enter_fn:   function called to guard GDK
712  * @leave_fn: function called to release the guard
713  *
714  * Allows the application to replace the standard method that
715  * GDK uses to protect its data structures. Normally, GDK
716  * creates a single #GMutex that is locked by gdk_threads_enter(),
717  * and released by gdk_threads_leave(); using this function an
718  * application provides, instead, a function @enter_fn that is
719  * called by gdk_threads_enter() and a function @leave_fn that is
720  * called by gdk_threads_leave().
721  *
722  * The functions must provide at least same locking functionality
723  * as the default implementation, but can also do extra application
724  * specific processing.
725  *
726  * As an example, consider an application that has its own recursive
727  * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
728  * the GTK+ lock when entering a recursive main loop, the application
729  * must temporarily release its lock as well.
730  *
731  * Most threaded GTK+ apps won't need to use this method.
732  *
733  * This method must be called before gdk_threads_init(), and cannot
734  * be called multiple times.
735  *
736  * Since: 2.4
737  **/
738 void
739 gdk_threads_set_lock_functions (GCallback enter_fn,
740                                 GCallback leave_fn)
741 {
742   g_return_if_fail (gdk_threads_lock == NULL &&
743                     gdk_threads_unlock == NULL);
744
745   gdk_threads_lock = enter_fn;
746   gdk_threads_unlock = leave_fn;
747 }
748
749 static gboolean
750 gdk_threads_dispatch (gpointer data)
751 {
752   GdkThreadsDispatch *dispatch = data;
753   gboolean ret = FALSE;
754
755   GDK_THREADS_ENTER ();
756
757   if (!g_source_is_destroyed (g_main_current_source ()))
758     ret = dispatch->func (dispatch->data);
759
760   GDK_THREADS_LEAVE ();
761
762   return ret;
763 }
764
765 static void
766 gdk_threads_dispatch_free (gpointer data)
767 {
768   GdkThreadsDispatch *dispatch = data;
769
770   if (dispatch->destroy && dispatch->data)
771     dispatch->destroy (dispatch->data);
772
773   g_slice_free (GdkThreadsDispatch, data);
774 }
775
776
777 /**
778  * gdk_threads_add_idle_full:
779  * @priority: the priority of the idle source. Typically this will be in the
780  *            range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
781  * @function: function to call
782  * @data:     data to pass to @function
783  * @notify: (allow-none):   function to call when the idle is removed, or %NULL
784  *
785  * Adds a function to be called whenever there are no higher priority
786  * events pending.  If the function returns %FALSE it is automatically
787  * removed from the list of event sources and will not be called again.
788  *
789  * This variant of g_idle_add_full() calls @function with the GDK lock
790  * held. It can be thought of a MT-safe version for GTK+ widgets for the 
791  * following use case, where you have to worry about idle_callback()
792  * running in thread A and accessing @self after it has been finalized
793  * in thread B:
794  *
795  * |[
796  * static gboolean
797  * idle_callback (gpointer data)
798  * {
799  *    /&ast; gdk_threads_enter(); would be needed for g_idle_add() &ast;/
800  *
801  *    SomeWidget *self = data;
802  *    /&ast; do stuff with self &ast;/
803  *
804  *    self->idle_id = 0;
805  *
806  *    /&ast; gdk_threads_leave(); would be needed for g_idle_add() &ast;/
807  *    return FALSE;
808  * }
809  *
810  * static void
811  * some_widget_do_stuff_later (SomeWidget *self)
812  * {
813  *    self->idle_id = gdk_threads_add_idle (idle_callback, self)
814  *    /&ast; using g_idle_add() here would require thread protection in the callback &ast;/
815  * }
816  *
817  * static void
818  * some_widget_finalize (GObject *object)
819  * {
820  *    SomeWidget *self = SOME_WIDGET (object);
821  *    if (self->idle_id)
822  *      g_source_remove (self->idle_id);
823  *    G_OBJECT_CLASS (parent_class)->finalize (object);
824  * }
825  * ]|
826  *
827  * Return value: the ID (greater than 0) of the event source.
828  *
829  * Since: 2.12
830  */
831 guint
832 gdk_threads_add_idle_full (gint           priority,
833                            GSourceFunc    function,
834                            gpointer       data,
835                            GDestroyNotify notify)
836 {
837   GdkThreadsDispatch *dispatch;
838
839   g_return_val_if_fail (function != NULL, 0);
840
841   dispatch = g_slice_new (GdkThreadsDispatch);
842   dispatch->func = function;
843   dispatch->data = data;
844   dispatch->destroy = notify;
845
846   return g_idle_add_full (priority,
847                           gdk_threads_dispatch,
848                           dispatch,
849                           gdk_threads_dispatch_free);
850 }
851
852 /**
853  * gdk_threads_add_idle:
854  * @function: function to call
855  * @data:     data to pass to @function
856  *
857  * A wrapper for the common usage of gdk_threads_add_idle_full() 
858  * assigning the default priority, #G_PRIORITY_DEFAULT_IDLE.
859  *
860  * See gdk_threads_add_idle_full().
861  *
862  * Return value: the ID (greater than 0) of the event source.
863  * 
864  * Since: 2.12
865  */
866 guint
867 gdk_threads_add_idle (GSourceFunc    function,
868                       gpointer       data)
869 {
870   return gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
871                                     function, data, NULL);
872 }
873
874
875 /**
876  * gdk_threads_add_timeout_full:
877  * @priority: the priority of the timeout source. Typically this will be in the
878  *            range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
879  * @interval: the time between calls to the function, in milliseconds
880  *             (1/1000ths of a second)
881  * @function: function to call
882  * @data:     data to pass to @function
883  * @notify: (allow-none):   function to call when the timeout is removed, or %NULL
884  *
885  * Sets a function to be called at regular intervals holding the GDK lock,
886  * with the given priority.  The function is called repeatedly until it 
887  * returns %FALSE, at which point the timeout is automatically destroyed 
888  * and the function will not be called again.  The @notify function is
889  * called when the timeout is destroyed.  The first call to the
890  * function will be at the end of the first @interval.
891  *
892  * Note that timeout functions may be delayed, due to the processing of other
893  * event sources. Thus they should not be relied on for precise timing.
894  * After each call to the timeout function, the time of the next
895  * timeout is recalculated based on the current time and the given interval
896  * (it does not try to 'catch up' time lost in delays).
897  *
898  * This variant of g_timeout_add_full() can be thought of a MT-safe version 
899  * for GTK+ widgets for the following use case:
900  *
901  * |[
902  * static gboolean timeout_callback (gpointer data)
903  * {
904  *    SomeWidget *self = data;
905  *    
906  *    /&ast; do stuff with self &ast;/
907  *    
908  *    self->timeout_id = 0;
909  *    
910  *    return FALSE;
911  * }
912  *  
913  * static void some_widget_do_stuff_later (SomeWidget *self)
914  * {
915  *    self->timeout_id = g_timeout_add (timeout_callback, self)
916  * }
917  *  
918  * static void some_widget_finalize (GObject *object)
919  * {
920  *    SomeWidget *self = SOME_WIDGET (object);
921  *    
922  *    if (self->timeout_id)
923  *      g_source_remove (self->timeout_id);
924  *    
925  *    G_OBJECT_CLASS (parent_class)->finalize (object);
926  * }
927  * ]|
928  *
929  * Return value: the ID (greater than 0) of the event source.
930  * 
931  * Since: 2.12
932  */
933 guint
934 gdk_threads_add_timeout_full (gint           priority,
935                               guint          interval,
936                               GSourceFunc    function,
937                               gpointer       data,
938                               GDestroyNotify notify)
939 {
940   GdkThreadsDispatch *dispatch;
941
942   g_return_val_if_fail (function != NULL, 0);
943
944   dispatch = g_slice_new (GdkThreadsDispatch);
945   dispatch->func = function;
946   dispatch->data = data;
947   dispatch->destroy = notify;
948
949   return g_timeout_add_full (priority, 
950                              interval,
951                              gdk_threads_dispatch, 
952                              dispatch, 
953                              gdk_threads_dispatch_free);
954 }
955
956 /**
957  * gdk_threads_add_timeout:
958  * @interval: the time between calls to the function, in milliseconds
959  *             (1/1000ths of a second)
960  * @function: function to call
961  * @data:     data to pass to @function
962  *
963  * A wrapper for the common usage of gdk_threads_add_timeout_full() 
964  * assigning the default priority, #G_PRIORITY_DEFAULT.
965  *
966  * See gdk_threads_add_timeout_full().
967  * 
968  * Return value: the ID (greater than 0) of the event source.
969  *
970  * Since: 2.12
971  */
972 guint
973 gdk_threads_add_timeout (guint       interval,
974                          GSourceFunc function,
975                          gpointer    data)
976 {
977   return gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
978                                        interval, function, data, NULL);
979 }
980
981
982 /**
983  * gdk_threads_add_timeout_seconds_full:
984  * @priority: the priority of the timeout source. Typically this will be in the
985  *            range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
986  * @interval: the time between calls to the function, in seconds
987  * @function: function to call
988  * @data:     data to pass to @function
989  * @notify: (allow-none): function to call when the timeout is removed, or %NULL
990  *
991  * A variant of gdk_threads_add_timeout_full() with second-granularity.
992  * See g_timeout_add_seconds_full() for a discussion of why it is
993  * a good idea to use this function if you don't need finer granularity.
994  *
995  *  Return value: the ID (greater than 0) of the event source.
996  * 
997  * Since: 2.14
998  */
999 guint
1000 gdk_threads_add_timeout_seconds_full (gint           priority,
1001                                       guint          interval,
1002                                       GSourceFunc    function,
1003                                       gpointer       data,
1004                                       GDestroyNotify notify)
1005 {
1006   GdkThreadsDispatch *dispatch;
1007
1008   g_return_val_if_fail (function != NULL, 0);
1009
1010   dispatch = g_slice_new (GdkThreadsDispatch);
1011   dispatch->func = function;
1012   dispatch->data = data;
1013   dispatch->destroy = notify;
1014
1015   return g_timeout_add_seconds_full (priority, 
1016                                      interval,
1017                                      gdk_threads_dispatch, 
1018                                      dispatch, 
1019                                      gdk_threads_dispatch_free);
1020 }
1021
1022 /**
1023  * gdk_threads_add_timeout_seconds:
1024  * @interval: the time between calls to the function, in seconds
1025  * @function: function to call
1026  * @data:     data to pass to @function
1027  *
1028  * A wrapper for the common usage of gdk_threads_add_timeout_seconds_full() 
1029  * assigning the default priority, #G_PRIORITY_DEFAULT.
1030  *
1031  * For details, see gdk_threads_add_timeout_full().
1032  * 
1033  * Return value: the ID (greater than 0) of the event source.
1034  *
1035  * Since: 2.14
1036  */
1037 guint
1038 gdk_threads_add_timeout_seconds (guint       interval,
1039                                  GSourceFunc function,
1040                                  gpointer    data)
1041 {
1042   return gdk_threads_add_timeout_seconds_full (G_PRIORITY_DEFAULT,
1043                                                interval, function, data, NULL);
1044 }
1045
1046 /**
1047  * gdk_get_program_class:
1048  *
1049  * Gets the program class. Unless the program class has explicitly
1050  * been set with gdk_set_program_class() or with the <option>--class</option>
1051  * commandline option, the default value is the program name (determined
1052  * with g_get_prgname()) with the first character converted to uppercase.
1053  *
1054  * Returns: the program class.
1055  */
1056 G_CONST_RETURN char *
1057 gdk_get_program_class (void)
1058 {
1059   return gdk_progclass;
1060 }
1061
1062 /**
1063  * gdk_set_program_class:
1064  * @program_class: a string.
1065  *
1066  * Sets the program class. The X11 backend uses the program class to set
1067  * the class name part of the <literal>WM_CLASS</literal> property on
1068  * toplevel windows; see the ICCCM.
1069  */
1070 void
1071 gdk_set_program_class (const char *program_class)
1072 {
1073   g_free (gdk_progclass);
1074
1075   gdk_progclass = g_strdup (program_class);
1076 }
1077
1078 /**
1079  * gdk_disable_multidevice:
1080  *
1081  * Disables multidevice support in GDK. This call must happen prior
1082  * to gdk_display_open(), gtk_init(), gtk_init_with_args() or
1083  * gtk_init_check() in order to take effect.
1084  *
1085  * Most common GTK+ applications won't ever need to call this. Only
1086  * applications that do mixed GDK/Xlib calls could want to disable
1087  * multidevice support if such Xlib code deals with input devices in
1088  * any way and doesn't observe the presence of XInput 2.
1089  *
1090  * Since: 3.0
1091  */
1092 void
1093 gdk_disable_multidevice (void)
1094 {
1095   if (gdk_initialized)
1096     return;
1097
1098   _gdk_disable_multidevice = TRUE;
1099 }