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