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