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;
56 const gchar *home_dir;
57 gchar *home_gtk_dir = NULL;
60 static gchar **result = NULL;
65 home_dir = g_get_home_dir();
67 home_gtk_dir = g_build_filename (home_dir, ".gtk-3.0", NULL);
69 module_path_env = g_getenv ("GTK_PATH");
70 exe_prefix = g_getenv ("GTK_EXE_PREFIX");
73 default_dir = g_build_filename (exe_prefix, "lib", "gtk-3.0", NULL);
75 default_dir = g_build_filename (_gtk_get_libdir (), "gtk-3.0", NULL);
77 if (module_path_env && home_gtk_dir)
78 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
79 module_path_env, home_gtk_dir, default_dir, NULL);
80 else if (module_path_env)
81 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
82 module_path_env, default_dir, NULL);
83 else if (home_gtk_dir)
84 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
85 home_gtk_dir, default_dir, NULL);
87 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
90 g_free (home_gtk_dir);
93 result = pango_split_file_list (module_path);
100 * _gtk_get_module_path:
101 * @type: the type of the module, for instance 'modules', 'engines', immodules'
103 * Determines the search path for a particular type of module.
105 * Return value: the search path for the module type. Free with g_strfreev().
108 _gtk_get_module_path (const gchar *type)
110 gchar **paths = get_module_path();
115 for (path = paths; *path; path++)
118 result = g_new (gchar *, count * 4 + 1);
121 for (path = get_module_path (); *path; path++)
123 gint use_version, use_host;
125 for (use_version = TRUE; use_version >= FALSE; use_version--)
126 for (use_host = TRUE; use_host >= FALSE; use_host--)
130 if (use_version && use_host)
131 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, GTK_HOST, type, NULL);
132 else if (use_version)
133 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, type, NULL);
135 tmp_dir = g_build_filename (*path, GTK_HOST, type, NULL);
137 tmp_dir = g_build_filename (*path, type, NULL);
139 result[count++] = tmp_dir;
143 result[count++] = NULL;
148 /* Like g_module_path, but use .la as the suffix
151 module_build_la_path (const gchar *directory,
152 const gchar *module_name)
157 if (strncmp (module_name, "lib", 3) == 0)
158 filename = (gchar *)module_name;
160 filename = g_strconcat ("lib", module_name, ".la", NULL);
162 if (directory && *directory)
163 result = g_build_filename (directory, filename, NULL);
165 result = g_strdup (filename);
167 if (filename != module_name)
175 * @name: the name of the module
176 * @type: the type of the module, for instance 'modules', 'engines', immodules'
178 * Looks for a dynamically module named @name of type @type in the standard GTK+
179 * module search path.
181 * Return value: the pathname to the found module, or %NULL if it wasn't found.
182 * Free with g_free().
185 _gtk_find_module (const gchar *name,
190 gchar *module_name = NULL;
192 if (g_path_is_absolute (name))
193 return g_strdup (name);
195 paths = _gtk_get_module_path (type);
196 for (path = paths; *path; path++)
200 tmp_name = g_module_build_path (*path, name);
201 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
203 module_name = tmp_name;
208 tmp_name = module_build_la_path (*path, name);
209 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
211 module_name = tmp_name;
223 find_module (const gchar *name)
228 module_name = _gtk_find_module (name, "modules");
231 /* As last resort, try loading without an absolute path (using system
234 module_name = g_module_build_path (NULL, name);
237 module = g_module_open (module_name, G_MODULE_BIND_LOCAL | G_MODULE_BIND_LAZY);
239 if (_gtk_module_has_mixed_deps (module))
241 g_warning ("GTK+ module %s cannot be loaded.\n"
242 "GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported.", module_name);
243 g_module_close (module);
247 g_free (module_name);
253 cmp_module (GtkModuleInfo *info,
256 return info->module != module;
260 module_is_blacklisted (const gchar *name,
263 if (g_str_equal (name, "gail"))
266 g_message ("Not loading module \"gail\": The functionality is provided by GTK natively. Please try to not load it.");
275 load_module (GSList *module_list,
278 GtkModuleInitFunc modinit_func;
279 gpointer modinit_func_ptr;
280 GtkModuleInfo *info = NULL;
281 GModule *module = NULL;
283 gboolean success = FALSE;
285 if (g_module_supported ())
287 for (l = gtk_modules; l; l = l->next)
290 if (g_slist_find_custom (info->names, name,
291 (GCompareFunc)strcmp))
303 module = find_module (name);
307 /* Do the check this late so we only warn about existing modules,
308 * not old modules that are still in the modules path. */
309 if (module_is_blacklisted (name, TRUE))
314 else if (g_module_symbol (module, "gtk_module_init", &modinit_func_ptr))
315 modinit_func = modinit_func_ptr;
320 g_module_close (module);
328 temp = g_slist_find_custom (gtk_modules, module,
329 (GCompareFunc)cmp_module);
335 info = g_new0 (GtkModuleInfo, 1);
337 info->names = g_slist_prepend (info->names, g_strdup (name));
338 info->module = module;
340 info->init_func = modinit_func;
341 g_module_symbol (module, "gtk_module_display_init",
342 (gpointer *) &info->display_init_func);
344 gtk_modules = g_slist_append (gtk_modules, info);
346 /* display_init == NULL indicates a non-multihead aware module.
347 * For these, we delay the call to init_func until first display is
348 * opened, see default_display_notify_cb().
349 * For multihead aware modules, we call init_func immediately,
350 * and also call display_init_func on all opened displays.
352 if (default_display_opened || info->display_init_func)
353 (* info->init_func) (>k_argc, >k_argv);
355 if (info->display_init_func)
357 GSList *displays, *iter;
358 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
359 for (iter = displays; iter; iter = iter->next)
361 GdkDisplay *display = iter->data;
362 (* info->display_init_func) (display);
364 g_slist_free (displays);
369 GTK_NOTE (MODULES, g_print ("Module already loaded, ignoring: %s\n", name));
370 info->names = g_slist_prepend (info->names, g_strdup (name));
372 /* remove new reference count on module, we already have one */
373 g_module_close (module);
382 if (!g_slist_find (module_list, info))
384 module_list = g_slist_prepend (module_list, info);
389 if (!module_is_blacklisted (name, FALSE))
391 const gchar *error = g_module_error ();
393 g_message ("Failed to load module \"%s\"%s%s",
394 name, error ? ": " : "", error ? error : "");
403 gtk_module_info_unref (GtkModuleInfo *info)
409 if (info->ref_count == 0)
412 g_print ("Unloading module: %s\n", g_module_name (info->module)));
414 gtk_modules = g_slist_remove (gtk_modules, info);
415 g_module_close (info->module);
416 for (l = info->names; l; l = l->next)
418 g_slist_free (info->names);
424 load_modules (const char *module_str)
426 gchar **module_names;
427 GSList *module_list = NULL;
430 GTK_NOTE (MODULES, g_print ("Loading module list: %s\n", module_str));
432 module_names = pango_split_file_list (module_str);
433 for (i = 0; module_names[i]; i++)
434 module_list = load_module (module_list, module_names[i]);
436 module_list = g_slist_reverse (module_list);
437 g_strfreev (module_names);
443 default_display_notify_cb (GdkDisplayManager *display_manager)
447 /* Initialize non-multihead-aware modules when the
448 * default display is first set to a non-NULL value.
451 if (!gdk_display_get_default () || default_display_opened)
454 default_display_opened = TRUE;
456 for (slist = gtk_modules; slist; slist = slist->next)
460 GtkModuleInfo *info = slist->data;
462 if (!info->display_init_func)
463 (* info->init_func) (>k_argc, >k_argv);
469 display_closed_cb (GdkDisplay *display,
473 GtkSettings *settings;
476 for (i = 0; i < gdk_display_get_n_screens (display); i++)
478 screen = gdk_display_get_screen (display, i);
480 settings = gtk_settings_get_for_screen (screen);
482 g_object_set_data_full (G_OBJECT (settings),
490 display_opened_cb (GdkDisplayManager *display_manager,
495 GtkSettings *settings;
498 for (slist = gtk_modules; slist; slist = slist->next)
502 GtkModuleInfo *info = slist->data;
504 if (info->display_init_func)
505 (* info->display_init_func) (display);
509 for (i = 0; i < gdk_display_get_n_screens (display); i++)
511 GValue value = G_VALUE_INIT;
513 g_value_init (&value, G_TYPE_STRING);
515 screen = gdk_display_get_screen (display, i);
517 if (gdk_screen_get_setting (screen, "gtk-modules", &value))
519 settings = gtk_settings_get_for_screen (screen);
520 _gtk_modules_settings_changed (settings, g_value_get_string (&value));
521 g_value_unset (&value);
525 /* Since closing display doesn't actually release the resources yet,
526 * we have to connect to the ::closed signal.
528 g_signal_connect (display, "closed", G_CALLBACK (display_closed_cb), NULL);
532 _gtk_modules_init (gint *argc,
534 const gchar *gtk_modules_args)
536 GdkDisplayManager *display_manager;
539 g_assert (gtk_argv == NULL);
543 /* store argc and argv for later use in mod initialization */
545 gtk_argv = g_new (gchar *, *argc + 1);
546 for (i = 0; i < gtk_argc; i++)
547 gtk_argv [i] = g_strdup ((*argv) [i]);
548 gtk_argv [*argc] = NULL;
551 display_manager = gdk_display_manager_get ();
552 default_display_opened = gdk_display_get_default () != NULL;
553 g_signal_connect (display_manager, "notify::default-display",
554 G_CALLBACK (default_display_notify_cb),
556 g_signal_connect (display_manager, "display-opened",
557 G_CALLBACK (display_opened_cb),
560 if (gtk_modules_args) {
561 /* Modules specified in the GTK_MODULES environment variable
562 * or on the command line are always loaded, so we'll just leak
565 g_slist_free (load_modules (gtk_modules_args));
570 settings_destroy_notify (gpointer data)
572 GSList *iter, *modules = data;
574 for (iter = modules; iter; iter = iter->next)
576 GtkModuleInfo *info = iter->data;
577 gtk_module_info_unref (info);
579 g_slist_free (modules);
583 _gtk_modules_settings_changed (GtkSettings *settings,
584 const gchar *modules)
586 GSList *new_modules = NULL;
588 GTK_NOTE (MODULES, g_print ("gtk-modules setting changed to: %s\n", modules));
590 /* load/ref before unreffing existing */
591 if (modules && modules[0])
592 new_modules = load_modules (modules);
594 g_object_set_data_full (G_OBJECT (settings),
597 settings_destroy_notify);
600 /* Return TRUE if module_to_check causes version conflicts.
601 * If module_to_check is NULL, check the main module.
604 _gtk_module_has_mixed_deps (GModule *module_to_check)
610 if (!module_to_check)
611 module = g_module_open (NULL, 0);
613 module = module_to_check;
615 if (g_module_symbol (module, "gtk_progress_get_type", &func))
620 if (!module_to_check)
621 g_module_close (module);