]> Pileus Git - ~andy/gtk/blob - gtk/gtkimmodule.c
Can't include gtkprivate.h (I added the include yesterday, without really
[~andy/gtk] / gtk / gtkimmodule.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * Themes added by The Rasterman <raster@redhat.com>
5  * 
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <gmodule.h>
33 #include <pango/pango-utils.h>
34 #include "gtkimmodule.h"
35 #include "gtkimcontextsimple.h"
36 #include "gtkrc.h"
37 #include "config.h"
38 #include "gtkintl.h"
39
40 /* Do *not* include "gtkprivate.h" in this file. If you do, the
41  * correct_libdir_prefix() function below will have to move somewhere
42  * else.
43  */
44
45 #define SIMPLE_ID "gtk-im-context-simple"
46
47 typedef struct _GtkIMModule      GtkIMModule;
48 typedef struct _GtkIMModuleClass GtkIMModuleClass;
49
50 #define GTK_TYPE_IM_MODULE          (gtk_im_module_get_type ())
51 #define GTK_IM_MODULE(im_module)    (G_TYPE_CHECK_INSTANCE_CAST ((im_module), GTK_TYPE_IM_MODULE, GtkIMModule))
52 #define GTK_IS_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_TYPE ((im_module), GTK_TYPE_IM_MODULE))
53
54 struct _GtkIMModule
55 {
56   GTypeModule parent_instance;
57   
58   GModule *library;
59
60   void          (*list)   (const GtkIMContextInfo ***contexts,
61                            guint                    *n_contexts);
62   void          (*init)   (GTypeModule              *module);
63   void          (*exit)   (void);
64   GtkIMContext *(*create) (const gchar              *context_id);
65
66   GtkIMContextInfo **contexts;
67   guint n_contexts;
68
69   gchar *path;
70 };
71
72 struct _GtkIMModuleClass 
73 {
74   GTypeModuleClass parent_class;
75 };
76
77 GType gtk_im_module_get_type (void);
78
79 gint n_loaded_contexts = 0;
80 static GHashTable *contexts_hash = NULL;
81 static GSList *modules_list = NULL;
82
83 static GObjectClass *parent_class = NULL;
84
85 static gboolean
86 gtk_im_module_load (GTypeModule *module)
87 {
88   GtkIMModule *im_module = GTK_IM_MODULE (module);
89   
90   im_module->library = g_module_open (im_module->path, 0);
91   if (!im_module->library)
92     {
93       g_warning (g_module_error());
94       return FALSE;
95     }
96   
97   /* extract symbols from the lib */
98   if (!g_module_symbol (im_module->library, "im_module_init",
99                         (gpointer *)&im_module->init) ||
100       !g_module_symbol (im_module->library, "im_module_exit", 
101                         (gpointer *)&im_module->exit) ||
102       !g_module_symbol (im_module->library, "im_module_list", 
103                         (gpointer *)&im_module->list) ||
104       !g_module_symbol (im_module->library, "im_module_create", 
105                         (gpointer *)&im_module->create))
106     {
107       g_warning (g_module_error());
108       g_module_close (im_module->library);
109       
110       return FALSE;
111     }
112             
113   /* call the theme's init (theme_init) function to let it */
114   /* setup anything it needs to set up. */
115   im_module->init (module);
116
117   return TRUE;
118 }
119
120 static void
121 gtk_im_module_unload (GTypeModule *module)
122 {
123   GtkIMModule *im_module = GTK_IM_MODULE (module);
124   
125   im_module->exit();
126
127   g_module_close (im_module->library);
128   im_module->library = NULL;
129
130   im_module->init = NULL;
131   im_module->exit = NULL;
132   im_module->list = NULL;
133   im_module->create = NULL;
134 }
135
136 /* This only will ever be called if an error occurs during
137  * initialization
138  */
139 static void
140 gtk_im_module_finalize (GObject *object)
141 {
142   GtkIMModule *module = GTK_IM_MODULE (object);
143
144   g_free (module->path);
145
146   parent_class->finalize (object);
147 }
148
149 static void
150 gtk_im_module_class_init (GtkIMModuleClass *class)
151 {
152   GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
153   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
154
155   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
156   
157   module_class->load = gtk_im_module_load;
158   module_class->unload = gtk_im_module_unload;
159
160   gobject_class->finalize = gtk_im_module_finalize;
161 }
162
163 GType
164 gtk_im_module_get_type (void)
165 {
166   static GType im_module_type = 0;
167
168   if (!im_module_type)
169     {
170       static const GTypeInfo im_module_info = {
171         sizeof (GtkIMModuleClass),
172         NULL,           /* base_init */
173         NULL,           /* base_finalize */
174         (GClassInitFunc) gtk_im_module_class_init,
175         NULL,           /* class_finalize */
176         NULL,           /* class_data */
177         sizeof (GtkIMModule),
178         0,              /* n_preallocs */
179         NULL,           /* instance_init */
180       };
181
182       im_module_type = g_type_register_static (G_TYPE_TYPE_MODULE, "GtkIMModule", &im_module_info, 0);
183     }
184   
185   return im_module_type;
186 }
187
188 static void
189 free_info (GtkIMContextInfo *info)
190 {
191   g_free ((char *)info->context_id);
192   g_free ((char *)info->context_name);
193   g_free ((char *)info->domain);
194   g_free ((char *)info->domain_dirname);
195   g_free ((char *)info->default_locales);
196   g_free (info);
197 }
198
199 static void
200 add_module (GtkIMModule *module, GSList *infos)
201 {
202   GSList *tmp_list = infos;
203   gint i = 0;
204   gint n = g_slist_length (infos);
205   module->contexts = g_new (GtkIMContextInfo *, n);
206
207   while (tmp_list)
208     {
209       GtkIMContextInfo *info = tmp_list->data;
210   
211       if (g_hash_table_lookup (contexts_hash, info->context_id))
212         {
213           free_info (info);     /* Duplicate */
214         }
215       else
216         {
217           g_hash_table_insert (contexts_hash, (char *)info->context_id, module);
218           module->contexts[i++] = tmp_list->data;
219           n_loaded_contexts++;
220         }
221       
222       tmp_list = tmp_list->next;
223     }
224   g_slist_free (infos);
225   module->n_contexts = i;
226
227   modules_list = g_slist_prepend (modules_list, module);
228 }
229
230 #if defined (G_OS_WIN32) && defined (GTK_LIBDIR)
231 /* This is needes on Win32, but not wanted when compiling with MSVC,
232  * as the makefile.msc doesn't define any GTK_LIBDIR value.
233  */
234
235 #define DO_CORRECT_LIBDIR_PREFIX /* Flag to check below whether to call this */
236
237 static void
238 correct_libdir_prefix (gchar **path)
239 {
240   /* GTK_LIBDIR here is supposed to still have the definition from
241    * Makefile.am, i.e. the build-time value. Do *not* include gtkprivate.h
242    * in this file.
243    */
244   if (strncmp (*path, GTK_LIBDIR, strlen (GTK_LIBDIR)) == 0)
245     {
246       /* This is an entry put there by make install on the
247        * packager's system. On Windows a prebuilt GTK+
248        * package can be installed in a random
249        * location. The gtk.immodules file distributed in
250        * such a package contains paths from the package
251        * builder's machine. Replace the path with the real
252        * one on this machine.
253        */
254       extern const gchar *_gtk_get_libdir ();
255       gchar *tem = *path;
256       *path = g_strconcat (_gtk_get_libdir (), tem + strlen (GTK_LIBDIR), NULL);
257       g_free (tem);
258     }
259 }
260 #endif
261
262
263 static void
264 gtk_im_module_init ()
265 {
266   GString *line_buf = g_string_new (NULL);
267   GString *tmp_buf = g_string_new (NULL);
268   gchar *filename = gtk_rc_get_im_module_file();
269   FILE *file;
270   gboolean have_error = FALSE;
271
272   GtkIMModule *module = NULL;
273   GSList *infos = NULL;
274
275   contexts_hash = g_hash_table_new (g_str_hash, g_str_equal);
276
277   file = fopen (filename, "r");
278   if (!file)
279     {
280       g_warning ("Can not open Input Method module file '%s': %s",
281                  filename, g_strerror (errno));
282       /* We are leaking all kinds of memory here. */
283       return;
284     }
285
286   while (!have_error && pango_read_line (file, line_buf))
287     {
288       const char *p;
289       
290       p = line_buf->str;
291
292       if (!pango_skip_space (&p))
293         {
294           /* Blank line marking the end of a module
295            */
296           if (module && *p != '#')
297             {
298               add_module (module, infos);
299               module = NULL;
300               infos = NULL;
301             }
302           
303           continue;
304         }
305
306       if (!module)
307         {
308           /* Read a module location
309            */
310           module = g_object_new (GTK_TYPE_IM_MODULE, NULL);
311
312           if (!pango_scan_string (&p, tmp_buf) ||
313               pango_skip_space (&p))
314             {
315               g_warning ("Error parsing context info in '%s'\n  %s", 
316                          filename, line_buf->str);
317               have_error = TRUE;
318             }
319
320           module->path = g_strdup (tmp_buf->str);
321 #ifdef DO_CORRECT_LIBDIR_PREFIX
322           correct_libdir_prefix (&module->path);
323 #endif
324           g_type_module_set_name (G_TYPE_MODULE (module), module->path);
325         }
326       else
327         {
328           GtkIMContextInfo *info = g_new0 (GtkIMContextInfo, 1);
329           
330           /* Read information about a context type
331            */
332           if (!pango_scan_string (&p, tmp_buf))
333             goto context_error;
334           info->context_id = g_strdup (tmp_buf->str);
335
336           if (!pango_scan_string (&p, tmp_buf))
337             goto context_error;
338           info->context_name = g_strdup (tmp_buf->str);
339
340           if (!pango_scan_string (&p, tmp_buf))
341             goto context_error;
342           info->domain = g_strdup (tmp_buf->str);
343
344           if (!pango_scan_string (&p, tmp_buf))
345             goto context_error;
346           info->domain_dirname = g_strdup (tmp_buf->str);
347 #ifdef DO_CORRECT_LIBDIR_PREFIX
348           correct_libdir_prefix (&info->domain_dirname);
349 #endif
350
351           if (!pango_scan_string (&p, tmp_buf))
352             goto context_error;
353           info->default_locales = g_strdup (tmp_buf->str);
354
355           if (pango_skip_space (&p))
356             goto context_error;
357
358           infos = g_slist_prepend (infos, info);
359           continue;
360
361         context_error:
362           g_warning ("Error parsing context info in '%s'\n  %s", 
363                      filename, line_buf->str);
364           have_error = TRUE;
365         }
366     }
367
368   if (have_error)
369     {
370       GSList *tmp_list = infos;
371       while (tmp_list)
372         {
373           free_info (tmp_list->data);
374           tmp_list = tmp_list->next;
375         }
376       g_slist_free (infos);
377
378       g_object_unref (G_OBJECT (module));
379     }
380   else if (module)
381     add_module (module, infos);
382
383   fclose (file);
384   g_string_free (line_buf, TRUE);
385   g_string_free (tmp_buf, TRUE);
386   g_free (filename);
387 }
388
389 /**
390  * _gtk_im_module_list:
391  * @contexts: location to store an array of pointers to #GtkIMContextInfo
392  *            this array should be freed with g_free() when you are finished.
393  *            The structures it points are statically allocated and should
394  *            not be modified or freed.
395  * @n_contexts: the length of the array stored in @contexts
396  * 
397  * List all available types of input method context
398  **/
399 void
400 _gtk_im_module_list (const GtkIMContextInfo ***contexts,
401                      guint                    *n_contexts)
402 {
403   int n = 0;
404
405   static const GtkIMContextInfo simple_context_info = {
406     SIMPLE_ID,
407     "Default",
408     "gtk+",
409     NULL,
410     ""
411   };
412
413   if (!contexts_hash)
414     gtk_im_module_init ();
415
416   if (n_contexts)
417     *n_contexts = (n_loaded_contexts + 1);
418
419   if (contexts)
420     {
421       GSList *tmp_list;
422       int i;
423       
424       *contexts = g_new (const GtkIMContextInfo *, n_loaded_contexts + 1);
425
426       (*contexts)[n++] = &simple_context_info;
427
428       tmp_list = modules_list;
429       while (tmp_list)
430         {
431           GtkIMModule *module = tmp_list->data;
432
433           for (i=0; i<module->n_contexts; i++)
434             (*contexts)[n++] = module->contexts[i];
435           
436           tmp_list = tmp_list->next;
437         }
438     }
439 }
440
441 /**
442  * _gtk_im_module_create:
443  * @context_id: the context ID for the context type to create
444  * 
445  * Create an IM context of a type specified by the string
446  * ID @context_id.
447  * 
448  * Return value: a newly created input context of or @context_id, or
449  * if that could not be created, a newly created GtkIMContextSimple.
450  **/
451 GtkIMContext *
452 _gtk_im_module_create (const gchar *context_id)
453 {
454   GtkIMModule *im_module;
455   GtkIMContext *context = NULL;
456   
457   if (!contexts_hash)
458     gtk_im_module_init ();
459
460   if (strcmp (context_id, SIMPLE_ID) != 0)
461     {
462       im_module = g_hash_table_lookup (contexts_hash, context_id);
463       if (!im_module)
464         {
465           g_warning ("Attempt to load unknown IM context type '%s'", context_id);
466         }
467       else
468         {
469           if (g_type_module_use (G_TYPE_MODULE (im_module)))
470             {
471               context = im_module->create (context_id);
472               g_type_module_unuse (G_TYPE_MODULE (im_module));
473             }
474           
475           if (!context)
476             g_warning ("Loading IM context type '%s' failed", context_id);
477         }
478     }
479   
480   if (!context)
481      return gtk_im_context_simple_new ();
482   else
483     return context;
484 }
485
486 /* Match @locale against @against.
487  * 
488  * 'en_US' against 'en_US'       => 4
489  * 'en_US' against 'en'          => 3
490  * 'en', 'en_UK' against 'en_US' => 2
491  *  all locales, against '*'     => 1
492  */
493 static gint
494 match_locale (const gchar *locale,
495               const gchar *against,
496               gint         against_len)
497 {
498   if (strcmp (against, "*") == 0)
499     return 1;
500
501   if (strcmp (locale, against) == 0)
502     return 4;
503
504   if (strncmp (locale, against, 2) == 0)
505     return (against_len == 2) ? 3 : 2;
506
507   return 0;
508 }
509
510 /**
511  * _gtk_im_module_get_default_context_id:
512  * @locale: a locale id in the form 'en_US'
513  * 
514  * Return the context_id of the best IM context type
515  * for the given locale ID.
516  * 
517  * Return value: the context ID (will never be %NULL)
518  *    the value is newly allocated and must be freed
519  *    with g_free().
520  **/
521 const gchar *
522 _gtk_im_module_get_default_context_id (const gchar *locale)
523 {
524   GSList *tmp_list;
525   const gchar *context_id = NULL;
526   gint best_goodness = 0;
527   gint i;
528   gchar *tmp_locale, *tmp;
529   const gchar *envvar;
530       
531   if (!contexts_hash)
532     gtk_im_module_init ();
533
534   envvar = g_getenv ("GTK_IM_MODULE");
535   if (envvar && g_hash_table_lookup (contexts_hash, envvar))
536     return g_strdup (envvar);
537
538   /* Strip the locale code down to the essentials
539    */
540   tmp_locale = g_strdup (locale);
541   tmp = strchr (tmp_locale, '.');
542   if (tmp)
543     *tmp = '\0';
544   tmp = strchr (tmp_locale, '@');
545   if (tmp)
546     *tmp = '\0';
547   
548   tmp_list = modules_list;
549   while (tmp_list)
550     {
551       GtkIMModule *module = tmp_list->data;
552      
553       for (i=0; i<module->n_contexts; i++)
554         {
555           const gchar *p = module->contexts[i]->default_locales;
556           while (p)
557             {
558               const gchar *q = strchr (p, ':');
559               gint goodness = match_locale (tmp_locale, p, q ? q - p : strlen (p));
560
561               if (goodness > best_goodness)
562                 {
563                   context_id = module->contexts[i]->context_id;
564                   best_goodness = goodness;
565                 }
566
567               p = q ? q + 1 : NULL;
568             }
569         }
570       
571       tmp_list = tmp_list->next;
572     }
573
574   g_free (tmp_locale);
575   
576   return g_strdup (context_id ? context_id : SIMPLE_ID);
577 }