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