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