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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "gtkmodules.h"
26 #include "gtksettings.h"
28 #include "gtkprivate.h"
29 #include "gtkmodulesprivate.h"
34 typedef struct _GtkModuleInfo GtkModuleInfo;
39 GtkModuleInitFunc init_func;
40 GtkModuleDisplayInitFunc display_init_func;
44 static GSList *gtk_modules = NULL;
46 static gboolean default_display_opened = FALSE;
48 /* Saved argc, argv for delayed module initialization
50 static gint gtk_argc = 0;
51 static gchar **gtk_argv = NULL;
54 get_module_path (void)
56 const gchar *module_path_env;
57 const gchar *exe_prefix;
58 const gchar *home_dir;
59 gchar *home_gtk_dir = NULL;
62 static gchar **result = NULL;
67 home_dir = g_get_home_dir();
69 home_gtk_dir = g_build_filename (home_dir, ".gtk-3.0", NULL);
71 module_path_env = g_getenv ("GTK_PATH");
72 exe_prefix = g_getenv ("GTK_EXE_PREFIX");
75 default_dir = g_build_filename (exe_prefix, "lib", "gtk-3.0", NULL);
77 default_dir = g_build_filename (_gtk_get_libdir (), "gtk-3.0", NULL);
79 if (module_path_env && home_gtk_dir)
80 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
81 module_path_env, home_gtk_dir, default_dir, NULL);
82 else if (module_path_env)
83 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
84 module_path_env, default_dir, NULL);
85 else if (home_gtk_dir)
86 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
87 home_gtk_dir, default_dir, NULL);
89 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
92 g_free (home_gtk_dir);
95 result = pango_split_file_list (module_path);
102 * _gtk_get_module_path:
103 * @type: the type of the module, for instance 'modules', 'engines', immodules'
105 * Determines the search path for a particular type of module.
107 * Return value: the search path for the module type. Free with g_strfreev().
110 _gtk_get_module_path (const gchar *type)
112 gchar **paths = get_module_path();
117 for (path = paths; *path; path++)
120 result = g_new (gchar *, count * 4 + 1);
123 for (path = get_module_path (); *path; path++)
125 gint use_version, use_host;
127 for (use_version = TRUE; use_version >= FALSE; use_version--)
128 for (use_host = TRUE; use_host >= FALSE; use_host--)
132 if (use_version && use_host)
133 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, GTK_HOST, type, NULL);
134 else if (use_version)
135 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, type, NULL);
137 tmp_dir = g_build_filename (*path, GTK_HOST, type, NULL);
139 tmp_dir = g_build_filename (*path, type, NULL);
141 result[count++] = tmp_dir;
145 result[count++] = NULL;
150 /* Like g_module_path, but use .la as the suffix
153 module_build_la_path (const gchar *directory,
154 const gchar *module_name)
159 if (strncmp (module_name, "lib", 3) == 0)
160 filename = (gchar *)module_name;
162 filename = g_strconcat ("lib", module_name, ".la", NULL);
164 if (directory && *directory)
165 result = g_build_filename (directory, filename, NULL);
167 result = g_strdup (filename);
169 if (filename != module_name)
177 * @name: the name of the module
178 * @type: the type of the module, for instance 'modules', 'engines', immodules'
180 * Looks for a dynamically module named @name of type @type in the standard GTK+
181 * module search path.
183 * Return value: the pathname to the found module, or %NULL if it wasn't found.
184 * Free with g_free().
187 _gtk_find_module (const gchar *name,
192 gchar *module_name = NULL;
194 if (g_path_is_absolute (name))
195 return g_strdup (name);
197 paths = _gtk_get_module_path (type);
198 for (path = paths; *path; path++)
202 tmp_name = g_module_build_path (*path, name);
203 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
205 module_name = tmp_name;
210 tmp_name = module_build_la_path (*path, name);
211 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
213 module_name = tmp_name;
225 find_module (const gchar *name)
230 module_name = _gtk_find_module (name, "modules");
233 /* As last resort, try loading without an absolute path (using system
236 module_name = g_module_build_path (NULL, name);
239 module = g_module_open (module_name, G_MODULE_BIND_LOCAL | G_MODULE_BIND_LAZY);
241 if (_gtk_module_has_mixed_deps (module))
243 g_warning ("GTK+ module %s cannot be loaded.\n"
244 "GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported.", module_name);
245 g_module_close (module);
249 g_free (module_name);
255 cmp_module (GtkModuleInfo *info,
258 return info->module != module;
262 module_is_blacklisted (const gchar *name,
265 if (g_str_equal (name, "gail"))
268 g_message ("Not loading module \"gail\": The functionality is provided by GTK natively. Please try to not load it.");
277 load_module (GSList *module_list,
280 GtkModuleInitFunc modinit_func;
281 gpointer modinit_func_ptr;
282 GtkModuleInfo *info = NULL;
283 GModule *module = NULL;
285 gboolean success = FALSE;
287 if (g_module_supported ())
289 for (l = gtk_modules; l; l = l->next)
292 if (g_slist_find_custom (info->names, name,
293 (GCompareFunc)strcmp))
305 module = find_module (name);
309 /* Do the check this late so we only warn about existing modules,
310 * not old modules that are still in the modules path. */
311 if (module_is_blacklisted (name, TRUE))
316 else if (g_module_symbol (module, "gtk_module_init", &modinit_func_ptr))
317 modinit_func = modinit_func_ptr;
322 g_module_close (module);
330 temp = g_slist_find_custom (gtk_modules, module,
331 (GCompareFunc)cmp_module);
337 info = g_new0 (GtkModuleInfo, 1);
339 info->names = g_slist_prepend (info->names, g_strdup (name));
340 info->module = module;
342 info->init_func = modinit_func;
343 g_module_symbol (module, "gtk_module_display_init",
344 (gpointer *) &info->display_init_func);
346 gtk_modules = g_slist_append (gtk_modules, info);
348 /* display_init == NULL indicates a non-multihead aware module.
349 * For these, we delay the call to init_func until first display is
350 * opened, see default_display_notify_cb().
351 * For multihead aware modules, we call init_func immediately,
352 * and also call display_init_func on all opened displays.
354 if (default_display_opened || info->display_init_func)
355 (* info->init_func) (>k_argc, >k_argv);
357 if (info->display_init_func)
359 GSList *displays, *iter;
360 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
361 for (iter = displays; iter; iter = iter->next)
363 GdkDisplay *display = iter->data;
364 (* info->display_init_func) (display);
366 g_slist_free (displays);
371 GTK_NOTE (MODULES, g_print ("Module already loaded, ignoring: %s\n", name));
372 info->names = g_slist_prepend (info->names, g_strdup (name));
374 /* remove new reference count on module, we already have one */
375 g_module_close (module);
384 if (!g_slist_find (module_list, info))
386 module_list = g_slist_prepend (module_list, info);
391 if (!module_is_blacklisted (name, FALSE))
393 const gchar *error = g_module_error ();
395 g_message ("Failed to load module \"%s\"%s%s",
396 name, error ? ": " : "", error ? error : "");
405 gtk_module_info_unref (GtkModuleInfo *info)
411 if (info->ref_count == 0)
414 g_print ("Unloading module: %s\n", g_module_name (info->module)));
416 gtk_modules = g_slist_remove (gtk_modules, info);
417 g_module_close (info->module);
418 for (l = info->names; l; l = l->next)
420 g_slist_free (info->names);
426 load_modules (const char *module_str)
428 gchar **module_names;
429 GSList *module_list = NULL;
432 GTK_NOTE (MODULES, g_print ("Loading module list: %s\n", module_str));
434 module_names = pango_split_file_list (module_str);
435 for (i = 0; module_names[i]; i++)
436 module_list = load_module (module_list, module_names[i]);
438 module_list = g_slist_reverse (module_list);
439 g_strfreev (module_names);
445 default_display_notify_cb (GdkDisplayManager *display_manager)
449 /* Initialize non-multihead-aware modules when the
450 * default display is first set to a non-NULL value.
453 if (!gdk_display_get_default () || default_display_opened)
456 default_display_opened = TRUE;
458 for (slist = gtk_modules; slist; slist = slist->next)
462 GtkModuleInfo *info = slist->data;
464 if (!info->display_init_func)
465 (* info->init_func) (>k_argc, >k_argv);
471 display_closed_cb (GdkDisplay *display,
475 GtkSettings *settings;
478 for (i = 0; i < gdk_display_get_n_screens (display); i++)
480 screen = gdk_display_get_screen (display, i);
482 settings = gtk_settings_get_for_screen (screen);
484 g_object_set_data_full (G_OBJECT (settings),
492 display_opened_cb (GdkDisplayManager *display_manager,
497 GtkSettings *settings;
500 for (slist = gtk_modules; slist; slist = slist->next)
504 GtkModuleInfo *info = slist->data;
506 if (info->display_init_func)
507 (* info->display_init_func) (display);
511 for (i = 0; i < gdk_display_get_n_screens (display); i++)
513 GValue value = G_VALUE_INIT;
515 g_value_init (&value, G_TYPE_STRING);
517 screen = gdk_display_get_screen (display, i);
519 if (gdk_screen_get_setting (screen, "gtk-modules", &value))
521 settings = gtk_settings_get_for_screen (screen);
522 _gtk_modules_settings_changed (settings, g_value_get_string (&value));
523 g_value_unset (&value);
527 /* Since closing display doesn't actually release the resources yet,
528 * we have to connect to the ::closed signal.
530 g_signal_connect (display, "closed", G_CALLBACK (display_closed_cb), NULL);
534 _gtk_modules_init (gint *argc,
536 const gchar *gtk_modules_args)
538 GdkDisplayManager *display_manager;
541 g_assert (gtk_argv == NULL);
545 /* store argc and argv for later use in mod initialization */
547 gtk_argv = g_new (gchar *, *argc + 1);
548 for (i = 0; i < gtk_argc; i++)
549 gtk_argv [i] = g_strdup ((*argv) [i]);
550 gtk_argv [*argc] = NULL;
553 display_manager = gdk_display_manager_get ();
554 default_display_opened = gdk_display_get_default () != NULL;
555 g_signal_connect (display_manager, "notify::default-display",
556 G_CALLBACK (default_display_notify_cb),
558 g_signal_connect (display_manager, "display-opened",
559 G_CALLBACK (display_opened_cb),
562 if (gtk_modules_args) {
563 /* Modules specified in the GTK_MODULES environment variable
564 * or on the command line are always loaded, so we'll just leak
567 g_slist_free (load_modules (gtk_modules_args));
572 settings_destroy_notify (gpointer data)
574 GSList *iter, *modules = data;
576 for (iter = modules; iter; iter = iter->next)
578 GtkModuleInfo *info = iter->data;
579 gtk_module_info_unref (info);
581 g_slist_free (modules);
585 _gtk_modules_settings_changed (GtkSettings *settings,
586 const gchar *modules)
588 GSList *new_modules = NULL;
590 GTK_NOTE (MODULES, g_print ("gtk-modules setting changed to: %s\n", modules));
592 /* load/ref before unreffing existing */
593 if (modules && modules[0])
594 new_modules = load_modules (modules);
596 g_object_set_data_full (G_OBJECT (settings),
599 settings_destroy_notify);
602 /* Return TRUE if module_to_check causes version conflicts.
603 * If module_to_check is NULL, check the main module.
606 _gtk_module_has_mixed_deps (GModule *module_to_check)
612 if (!module_to_check)
613 module = g_module_open (NULL, 0);
615 module = module_to_check;
617 if (g_module_symbol (module, "gtk_progress_get_type", &func))
622 if (!module_to_check)
623 g_module_close (module);