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