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