]> Pileus Git - ~andy/gtk/blob - gtk/gtkmodules.c
Bug 457086 - numpad does not work when the Thai-Lao input method is used
[~andy/gtk] / gtk / gtkmodules.c
1 /* GTK - The GIMP Toolkit
2  * Copyright 1998-2002 Tim Janik, Red Hat, Inc., and others.
3  * Copyright (C) 2003 Alex Graveley
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24
25 #include "gtkmodules.h"
26 #include "gtksettings.h"
27 #include "gtkdebug.h"
28 #include "gtkprivate.h" /* GTK_LIBDIR */
29 #include "gtkintl.h" 
30 #include "gtkalias.h"
31
32 #include <gmodule.h>
33
34 typedef struct _GtkModuleInfo GtkModuleInfo;
35 struct _GtkModuleInfo
36 {
37   GModule                 *module;
38   gint                     ref_count;
39   GtkModuleInitFunc        init_func;
40   GtkModuleDisplayInitFunc display_init_func;
41   GSList                  *names;
42 };
43
44 static GSList *gtk_modules = NULL;
45
46 static gboolean default_display_opened = FALSE;
47
48 /* Saved argc, argv for delayed module initialization
49  */
50 static gint    gtk_argc = 0;
51 static gchar **gtk_argv = NULL;
52
53 static gchar **
54 get_module_path (void)
55 {
56   const gchar *module_path_env;
57   const gchar *exe_prefix;
58   const gchar *home_dir;
59   gchar *home_gtk_dir = NULL;
60   gchar *module_path;
61   gchar *default_dir;
62   static gchar **result = NULL;
63
64   if (result)
65     return result;
66
67   home_dir = g_get_home_dir();
68   if (home_dir)
69     home_gtk_dir = g_build_filename (home_dir, ".gtk-2.0", NULL);
70
71   module_path_env = g_getenv ("GTK_PATH");
72   exe_prefix = g_getenv ("GTK_EXE_PREFIX");
73
74   if (exe_prefix)
75     default_dir = g_build_filename (exe_prefix, "lib", "gtk-2.0", NULL);
76   else
77     default_dir = g_build_filename (GTK_LIBDIR, "gtk-2.0", NULL);
78
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);
88   else
89     module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
90                                 default_dir, NULL);
91
92   g_free (home_gtk_dir);
93   g_free (default_dir);
94
95   result = pango_split_file_list (module_path);
96   g_free (module_path);
97
98   return result;
99 }
100
101 /**
102  * _gtk_get_module_path:
103  * @type: the type of the module, for instance 'modules', 'engines', immodules'
104  * 
105  * Determines the search path for a particular type of module.
106  * 
107  * Return value: the search path for the module type. Free with g_strfreev().
108  **/
109 gchar **
110 _gtk_get_module_path (const gchar *type)
111 {
112   gchar **paths = get_module_path();
113   gchar **path;
114   gchar **result;
115   gint count = 0;
116
117   for (path = paths; *path; path++)
118     count++;
119
120   result = g_new (gchar *, count * 4 + 1);
121
122   count = 0;
123   for (path = get_module_path (); *path; path++)
124     {
125       gint use_version, use_host;
126       
127       for (use_version = TRUE; use_version >= FALSE; use_version--)
128         for (use_host = TRUE; use_host >= FALSE; use_host--)
129           {
130             gchar *tmp_dir;
131             
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);
136             else if (use_host)
137               tmp_dir = g_build_filename (*path, GTK_HOST, type, NULL);
138             else
139               tmp_dir = g_build_filename (*path, type, NULL);
140
141             result[count++] = tmp_dir;
142           }
143     }
144
145   result[count++] = NULL;
146
147   return result;
148 }
149
150 /* Like g_module_path, but use .la as the suffix
151  */
152 static gchar*
153 module_build_la_path (const gchar *directory,
154                       const gchar *module_name)
155 {
156   gchar *filename;
157   gchar *result;
158         
159   if (strncmp (module_name, "lib", 3) == 0)
160     filename = (gchar *)module_name;
161   else
162     filename =  g_strconcat ("lib", module_name, ".la", NULL);
163
164   if (directory && *directory)
165     result = g_build_filename (directory, filename, NULL);
166   else
167     result = g_strdup (filename);
168
169   if (filename != module_name)
170     g_free (filename);
171
172   return result;
173 }
174
175 /**
176  * _gtk_find_module:
177  * @name: the name of the module
178  * @type: the type of the module, for instance 'modules', 'engines', immodules'
179  * 
180  * Looks for a dynamically module named @name of type @type in the standard GTK+
181  *  module search path.
182  * 
183  * Return value: the pathname to the found module, or %NULL if it wasn't found.
184  *  Free with g_free().
185  **/
186 gchar *
187 _gtk_find_module (const gchar *name,
188                   const gchar *type)
189 {
190   gchar **paths;
191   gchar **path;
192   gchar *module_name = NULL;
193
194   if (g_path_is_absolute (name))
195     return g_strdup (name);
196
197   paths = _gtk_get_module_path (type);
198   for (path = paths; *path; path++)
199     {
200       gchar *tmp_name;
201
202       tmp_name = g_module_build_path (*path, name);
203       if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
204         {
205           module_name = tmp_name;
206           goto found;
207         }
208       g_free(tmp_name);
209
210       tmp_name = module_build_la_path (*path, name);
211       if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
212         {
213           module_name = tmp_name;
214           goto found;
215         }
216       g_free(tmp_name);
217     }
218
219  found:
220   g_strfreev (paths);
221   return module_name;
222 }
223
224 static GModule *
225 find_module (const gchar *name)
226 {
227   GModule *module;
228   gchar *module_name;
229
230   module_name = _gtk_find_module (name, "modules");
231   if (!module_name)
232     {
233       /* As last resort, try loading without an absolute path (using system
234        * library path)
235        */
236       module_name = g_module_build_path (NULL, name);
237     }
238
239   module = g_module_open (module_name, G_MODULE_BIND_LOCAL | G_MODULE_BIND_LAZY);
240   g_free(module_name);
241
242   return module;
243 }
244
245 static gint
246 cmp_module (GtkModuleInfo *info,
247             GModule       *module)
248 {
249   return info->module != module;
250 }
251
252 static GSList *
253 load_module (GSList      *module_list,
254              const gchar *name)
255 {
256   GtkModuleInitFunc modinit_func;
257   gpointer modinit_func_ptr;
258   GtkModuleInfo *info = NULL;
259   GModule *module = NULL;
260   GSList *l;
261   gboolean success = FALSE;
262   
263   if (g_module_supported ())
264     {
265       for (l = gtk_modules; l; l = l->next)
266         {
267           info = l->data;
268           if (g_slist_find_custom (info->names, name, 
269                                    (GCompareFunc)strcmp))
270             {
271               info->ref_count++;
272               
273               success = TRUE;
274             }
275         }
276
277       if (!success) 
278         {
279           module = find_module (name);
280
281           if (module)
282             {
283               if (g_module_symbol (module, "gtk_module_init", &modinit_func_ptr))
284                 modinit_func = modinit_func_ptr;
285               else
286                 modinit_func = NULL;
287
288               if (!modinit_func)
289                 g_module_close (module);
290               else
291                 {
292                   success = TRUE;
293                   info = (GtkModuleInfo *) g_slist_find_custom (gtk_modules, module,
294                                                                 (GCompareFunc)cmp_module);
295                   if (!info)
296                     {
297                       info = g_new0 (GtkModuleInfo, 1);
298                       
299                       info->names = g_slist_prepend (info->names, g_strdup (name));
300                       info->module = module;
301                       info->ref_count = 1;
302                       info->init_func = modinit_func;
303                       g_module_symbol (module, "gtk_module_display_init",
304                                        (gpointer *) &info->display_init_func);
305                       
306                       gtk_modules = g_slist_append (gtk_modules, info);
307                       
308                       /* display_init == NULL indicates a non-multihead aware module.
309                        * For these, we delay the call to init_func until first display is 
310                        * opened, see default_display_notify_cb().
311                        * For multihead aware modules, we call init_func immediately,
312                        * and also call display_init_func on all opened displays.
313                        */
314                       if (default_display_opened || info->display_init_func)
315                         (* info->init_func) (&gtk_argc, &gtk_argv);
316                       
317                       if (info->display_init_func) 
318                         {
319                           GSList *displays, *iter;                
320                           displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
321                           for (iter = displays; iter; iter = iter->next)
322                             {
323                               GdkDisplay *display = iter->data;
324                           (* info->display_init_func) (display);
325                             }
326                           g_slist_free (displays);
327                         }
328                     }
329                   else
330                     {
331                       GTK_NOTE (MODULES, g_print ("Module already loaded, ignoring: %s\n", name));
332                       info->names = g_slist_prepend (info->names, g_strdup (name));
333                       info->ref_count++;
334                       /* remove new reference count on module, we already have one */
335                       g_module_close (module);
336                     }
337                 }
338             }
339         }
340     }
341
342   if (success)
343     {
344       if (!g_slist_find (module_list, info))
345         {
346           module_list = g_slist_prepend (module_list, info);
347         }
348     }
349   else
350     g_message ("Failed to load module \"%s\": %s", name, g_module_error ());
351   
352   return module_list;
353 }
354
355
356 static void
357 gtk_module_info_unref (GtkModuleInfo *info)
358 {
359   GSList *l;
360
361   info->ref_count--;
362
363   if (info->ref_count == 0) 
364     {
365       GTK_NOTE (MODULES, 
366                 g_print ("Unloading module: %s\n", g_module_name (info->module)));
367
368       gtk_modules = g_slist_remove (gtk_modules, info);
369       g_module_close (info->module);
370       for (l = info->names; l; l = l->next)
371         g_free (l->data);
372       g_slist_free (info->names);
373       g_free (info);
374     }
375 }
376
377 static GSList *
378 load_modules (const char *module_str)
379 {
380   gchar **module_names;
381   GSList *module_list = NULL;
382   gint i;
383
384   GTK_NOTE (MODULES, g_print ("Loading module list: %s\n", module_str));
385
386   module_names = pango_split_file_list (module_str);
387   for (i = 0; module_names[i]; i++) 
388     module_list = load_module (module_list, module_names[i]);
389
390   module_list = g_slist_reverse (module_list);
391   g_strfreev (module_names);
392
393   return module_list;
394 }
395
396 static void
397 default_display_notify_cb (GdkDisplayManager *display_manager)
398 {
399   GSList *slist;
400
401   /* Initialize non-multihead-aware modules when the
402    * default display is first set to a non-NULL value.
403    */
404
405   if (!gdk_display_get_default () || default_display_opened)
406     return;
407
408   default_display_opened = TRUE;
409
410   for (slist = gtk_modules; slist; slist = slist->next)
411     {
412       if (slist->data)
413         {
414           GtkModuleInfo *info = slist->data;
415
416           if (!info->display_init_func)
417             (* info->init_func) (&gtk_argc, &gtk_argv);
418         }
419     }
420 }
421
422 static void
423 display_closed_cb (GdkDisplay *display,
424                    gboolean    is_error)
425 {
426   GdkScreen *screen;
427   GtkSettings *settings;
428   gint i;
429
430   for (i = 0; i < gdk_display_get_n_screens (display); i++)
431     {
432       screen = gdk_display_get_screen (display, i);
433
434       settings = gtk_settings_get_for_screen (screen);
435
436       g_object_set_data_full (G_OBJECT (settings),
437                               I_("gtk-modules"),
438                               NULL, NULL);
439     }  
440 }
441                    
442
443 static void
444 display_opened_cb (GdkDisplayManager *display_manager,
445                    GdkDisplay        *display)
446 {
447   GSList *slist;
448   GdkScreen *screen;
449   GtkSettings *settings;
450   gint i;
451
452   for (slist = gtk_modules; slist; slist = slist->next)
453     {
454       if (slist->data)
455         {
456           GtkModuleInfo *info = slist->data;
457
458           if (info->display_init_func)
459             (* info->display_init_func) (display);
460         }
461     }
462   
463   for (i = 0; i < gdk_display_get_n_screens (display); i++)
464     {
465       GValue value = { 0, };
466
467       g_value_init (&value, G_TYPE_STRING);
468
469       screen = gdk_display_get_screen (display, i);
470
471       if (gdk_screen_get_setting (screen, "gtk-modules", &value))
472         {
473           settings = gtk_settings_get_for_screen (screen);
474           _gtk_modules_settings_changed (settings, g_value_get_string (&value));
475           g_value_unset (&value);
476         }
477     }
478
479   /* Since closing display doesn't actually release the resources yet,
480    * we have to connect to the ::closed signal.
481    */
482   g_signal_connect (display, "closed", G_CALLBACK (display_closed_cb), NULL);
483 }
484
485 void
486 _gtk_modules_init (gint        *argc, 
487                    gchar     ***argv, 
488                    const gchar *gtk_modules_args)
489 {
490   GdkDisplayManager *display_manager;
491   gint i;
492
493   g_assert (gtk_argv == NULL);
494
495   if (argc && argv) 
496     {
497       /* store argc and argv for later use in mod initialization */
498       gtk_argc = *argc;
499       gtk_argv = g_new (gchar *, *argc + 1);
500       for (i = 0; i < gtk_argc; i++)
501         gtk_argv [i] = g_strdup ((*argv) [i]);
502       gtk_argv [*argc] = NULL;
503     }
504
505   display_manager = gdk_display_manager_get ();
506   default_display_opened = gdk_display_get_default () != NULL;
507   g_signal_connect (display_manager, "notify::default-display",
508                     G_CALLBACK (default_display_notify_cb), 
509                     NULL);
510   g_signal_connect (display_manager, "display-opened",
511                     G_CALLBACK (display_opened_cb), 
512                     NULL);
513
514   if (gtk_modules_args) {
515     /* Modules specified in the GTK_MODULES environment variable
516      * or on the command line are always loaded, so we'll just leak 
517      * the refcounts.
518      */
519     g_slist_free (load_modules (gtk_modules_args));
520   }
521 }
522
523 static void
524 settings_destroy_notify (gpointer data)
525 {
526   GSList *iter, *modules = data;
527
528   for (iter = modules; iter; iter = iter->next) 
529     {
530       GtkModuleInfo *info = iter->data;
531       gtk_module_info_unref (info);
532     }
533   g_slist_free (modules);
534 }
535
536 void
537 _gtk_modules_settings_changed (GtkSettings *settings, 
538                                const gchar *modules)
539 {
540   GSList *new_modules = NULL;
541
542   GTK_NOTE (MODULES, g_print ("gtk-modules setting changed to: %s\n", modules));
543
544   /* load/ref before unreffing existing */
545   if (modules && modules[0])
546     new_modules = load_modules (modules);
547
548   g_object_set_data_full (G_OBJECT (settings),
549                           I_("gtk-modules"),
550                           new_modules,
551                           settings_destroy_notify);
552 }