1 /* GTK - The GIMP Toolkit
2 * Copyright 1998-2002 Tim Janik, Red Hat, Inc., and others.
3 * Copyright (C) 2003 Alex Graveley
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include "gtkmodules.h"
24 #include "gtksettings.h"
26 #include "gtkprivate.h"
27 #include "gtkmodulesprivate.h"
32 typedef struct _GtkModuleInfo GtkModuleInfo;
37 GtkModuleInitFunc init_func;
38 GtkModuleDisplayInitFunc display_init_func;
42 static GSList *gtk_modules = NULL;
44 static gboolean default_display_opened = FALSE;
46 /* Saved argc, argv for delayed module initialization
48 static gint gtk_argc = 0;
49 static gchar **gtk_argv = NULL;
52 get_module_path (void)
54 const gchar *module_path_env;
55 const gchar *exe_prefix;
58 static gchar **result = NULL;
63 module_path_env = g_getenv ("GTK_PATH");
64 exe_prefix = g_getenv ("GTK_EXE_PREFIX");
67 default_dir = g_build_filename (exe_prefix, "lib", "gtk-3.0", NULL);
69 default_dir = g_build_filename (_gtk_get_libdir (), "gtk-3.0", NULL);
72 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
73 module_path_env, default_dir, NULL);
75 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
80 result = pango_split_file_list (module_path);
87 * _gtk_get_module_path:
88 * @type: the type of the module, for instance 'modules', 'engines', immodules'
90 * Determines the search path for a particular type of module.
92 * Return value: the search path for the module type. Free with g_strfreev().
95 _gtk_get_module_path (const gchar *type)
97 gchar **paths = get_module_path();
102 for (path = paths; *path; path++)
105 result = g_new (gchar *, count * 4 + 1);
108 for (path = get_module_path (); *path; path++)
110 gint use_version, use_host;
112 for (use_version = TRUE; use_version >= FALSE; use_version--)
113 for (use_host = TRUE; use_host >= FALSE; use_host--)
117 if (use_version && use_host)
118 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, GTK_HOST, type, NULL);
119 else if (use_version)
120 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, type, NULL);
122 tmp_dir = g_build_filename (*path, GTK_HOST, type, NULL);
124 tmp_dir = g_build_filename (*path, type, NULL);
126 result[count++] = tmp_dir;
130 result[count++] = NULL;
135 /* Like g_module_path, but use .la as the suffix
138 module_build_la_path (const gchar *directory,
139 const gchar *module_name)
144 if (strncmp (module_name, "lib", 3) == 0)
145 filename = (gchar *)module_name;
147 filename = g_strconcat ("lib", module_name, ".la", NULL);
149 if (directory && *directory)
150 result = g_build_filename (directory, filename, NULL);
152 result = g_strdup (filename);
154 if (filename != module_name)
162 * @name: the name of the module
163 * @type: the type of the module, for instance 'modules', 'engines', immodules'
165 * Looks for a dynamically module named @name of type @type in the standard GTK+
166 * module search path.
168 * Return value: the pathname to the found module, or %NULL if it wasn't found.
169 * Free with g_free().
172 _gtk_find_module (const gchar *name,
177 gchar *module_name = NULL;
179 if (g_path_is_absolute (name))
180 return g_strdup (name);
182 paths = _gtk_get_module_path (type);
183 for (path = paths; *path; path++)
187 tmp_name = g_module_build_path (*path, name);
188 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
190 module_name = tmp_name;
195 tmp_name = module_build_la_path (*path, name);
196 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
198 module_name = tmp_name;
210 find_module (const gchar *name)
215 module_name = _gtk_find_module (name, "modules");
218 /* As last resort, try loading without an absolute path (using system
221 module_name = g_module_build_path (NULL, name);
224 module = g_module_open (module_name, G_MODULE_BIND_LOCAL | G_MODULE_BIND_LAZY);
226 if (_gtk_module_has_mixed_deps (module))
228 g_warning ("GTK+ module %s cannot be loaded.\n"
229 "GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported.", module_name);
230 g_module_close (module);
234 g_free (module_name);
240 cmp_module (GtkModuleInfo *info,
243 return info->module != module;
247 module_is_blacklisted (const gchar *name,
250 if (g_str_equal (name, "gail") ||
251 g_str_equal (name, "atk-bridge"))
254 g_message ("Not loading module \"%s\": The functionality is provided by GTK natively. Please try to not load it.", name);
263 load_module (GSList *module_list,
266 GtkModuleInitFunc modinit_func;
267 gpointer modinit_func_ptr;
268 GtkModuleInfo *info = NULL;
269 GModule *module = NULL;
271 gboolean success = FALSE;
273 if (g_module_supported ())
275 for (l = gtk_modules; l; l = l->next)
278 if (g_slist_find_custom (info->names, name,
279 (GCompareFunc)strcmp))
291 module = find_module (name);
295 /* Do the check this late so we only warn about existing modules,
296 * not old modules that are still in the modules path. */
297 if (module_is_blacklisted (name, TRUE))
302 else if (g_module_symbol (module, "gtk_module_init", &modinit_func_ptr))
303 modinit_func = modinit_func_ptr;
308 g_module_close (module);
316 temp = g_slist_find_custom (gtk_modules, module,
317 (GCompareFunc)cmp_module);
323 info = g_new0 (GtkModuleInfo, 1);
325 info->names = g_slist_prepend (info->names, g_strdup (name));
326 info->module = module;
328 info->init_func = modinit_func;
329 g_module_symbol (module, "gtk_module_display_init",
330 (gpointer *) &info->display_init_func);
332 gtk_modules = g_slist_append (gtk_modules, info);
334 /* display_init == NULL indicates a non-multihead aware module.
335 * For these, we delay the call to init_func until first display is
336 * opened, see default_display_notify_cb().
337 * For multihead aware modules, we call init_func immediately,
338 * and also call display_init_func on all opened displays.
340 if (default_display_opened || info->display_init_func)
341 (* info->init_func) (>k_argc, >k_argv);
343 if (info->display_init_func)
345 GSList *displays, *iter;
346 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
347 for (iter = displays; iter; iter = iter->next)
349 GdkDisplay *display = iter->data;
350 (* info->display_init_func) (display);
352 g_slist_free (displays);
357 GTK_NOTE (MODULES, g_print ("Module already loaded, ignoring: %s\n", name));
358 info->names = g_slist_prepend (info->names, g_strdup (name));
360 /* remove new reference count on module, we already have one */
361 g_module_close (module);
370 if (!g_slist_find (module_list, info))
372 module_list = g_slist_prepend (module_list, info);
377 if (!module_is_blacklisted (name, FALSE))
379 const gchar *error = g_module_error ();
381 g_message ("Failed to load module \"%s\"%s%s",
382 name, error ? ": " : "", error ? error : "");
391 gtk_module_info_unref (GtkModuleInfo *info)
397 if (info->ref_count == 0)
400 g_print ("Unloading module: %s\n", g_module_name (info->module)));
402 gtk_modules = g_slist_remove (gtk_modules, info);
403 g_module_close (info->module);
404 for (l = info->names; l; l = l->next)
406 g_slist_free (info->names);
412 load_modules (const char *module_str)
414 gchar **module_names;
415 GSList *module_list = NULL;
418 GTK_NOTE (MODULES, g_print ("Loading module list: %s\n", module_str));
420 module_names = pango_split_file_list (module_str);
421 for (i = 0; module_names[i]; i++)
422 module_list = load_module (module_list, module_names[i]);
424 module_list = g_slist_reverse (module_list);
425 g_strfreev (module_names);
431 default_display_notify_cb (GdkDisplayManager *display_manager)
435 /* Initialize non-multihead-aware modules when the
436 * default display is first set to a non-NULL value.
439 if (!gdk_display_get_default () || default_display_opened)
442 default_display_opened = TRUE;
444 for (slist = gtk_modules; slist; slist = slist->next)
448 GtkModuleInfo *info = slist->data;
450 if (!info->display_init_func)
451 (* info->init_func) (>k_argc, >k_argv);
457 display_closed_cb (GdkDisplay *display,
461 GtkSettings *settings;
464 for (i = 0; i < gdk_display_get_n_screens (display); i++)
466 screen = gdk_display_get_screen (display, i);
468 settings = gtk_settings_get_for_screen (screen);
470 g_object_set_data_full (G_OBJECT (settings),
478 display_opened_cb (GdkDisplayManager *display_manager,
483 GtkSettings *settings;
486 for (slist = gtk_modules; slist; slist = slist->next)
490 GtkModuleInfo *info = slist->data;
492 if (info->display_init_func)
493 (* info->display_init_func) (display);
497 for (i = 0; i < gdk_display_get_n_screens (display); i++)
499 GValue value = G_VALUE_INIT;
501 g_value_init (&value, G_TYPE_STRING);
503 screen = gdk_display_get_screen (display, i);
505 if (gdk_screen_get_setting (screen, "gtk-modules", &value))
507 settings = gtk_settings_get_for_screen (screen);
508 _gtk_modules_settings_changed (settings, g_value_get_string (&value));
509 g_value_unset (&value);
513 /* Since closing display doesn't actually release the resources yet,
514 * we have to connect to the ::closed signal.
516 g_signal_connect (display, "closed", G_CALLBACK (display_closed_cb), NULL);
520 _gtk_modules_init (gint *argc,
522 const gchar *gtk_modules_args)
524 GdkDisplayManager *display_manager;
527 g_assert (gtk_argv == NULL);
531 /* store argc and argv for later use in mod initialization */
533 gtk_argv = g_new (gchar *, *argc + 1);
534 for (i = 0; i < gtk_argc; i++)
535 gtk_argv [i] = g_strdup ((*argv) [i]);
536 gtk_argv [*argc] = NULL;
539 display_manager = gdk_display_manager_get ();
540 default_display_opened = gdk_display_get_default () != NULL;
541 g_signal_connect (display_manager, "notify::default-display",
542 G_CALLBACK (default_display_notify_cb),
544 g_signal_connect (display_manager, "display-opened",
545 G_CALLBACK (display_opened_cb),
548 if (gtk_modules_args) {
549 /* Modules specified in the GTK_MODULES environment variable
550 * or on the command line are always loaded, so we'll just leak
553 g_slist_free (load_modules (gtk_modules_args));
558 settings_destroy_notify (gpointer data)
560 GSList *iter, *modules = data;
562 for (iter = modules; iter; iter = iter->next)
564 GtkModuleInfo *info = iter->data;
565 gtk_module_info_unref (info);
567 g_slist_free (modules);
571 _gtk_modules_settings_changed (GtkSettings *settings,
572 const gchar *modules)
574 GSList *new_modules = NULL;
576 GTK_NOTE (MODULES, g_print ("gtk-modules setting changed to: %s\n", modules));
578 /* load/ref before unreffing existing */
579 if (modules && modules[0])
580 new_modules = load_modules (modules);
582 g_object_set_data_full (G_OBJECT (settings),
585 settings_destroy_notify);
588 /* Return TRUE if module_to_check causes version conflicts.
589 * If module_to_check is NULL, check the main module.
592 _gtk_module_has_mixed_deps (GModule *module_to_check)
598 if (!module_to_check)
599 module = g_module_open (NULL, 0);
601 module = module_to_check;
603 if (g_module_symbol (module, "gtk_progress_get_type", &func))
608 if (!module_to_check)
609 g_module_close (module);