1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 #include <glib/gstdio.h>
35 #include "gtkimmodule.h"
36 #include "gtkimcontextsimple.h"
37 #include "gtksettings.h"
43 /* Do *not* include "gtkprivate.h" in this file. If you do, the
44 * correct_libdir_prefix() and correct_localedir_prefix() functions
45 * below will have to move somewhere else.
48 #ifdef __GTK_PRIVATE_H__
49 #error gtkprivate.h should not be included in this file
52 #define SIMPLE_ID "gtk-im-context-simple"
56 * @context_id: The unique identification string of the input method.
57 * @context_name: The human-readable name of the input method.
58 * @domain: Translation domain to be used with dgettext()
59 * @domain_dirname: Name of locale directory for use with bindtextdomain()
60 * @default_locales: A colon-separated list of locales where this input method
61 * should be the default. The asterisk "*" sets the default for all locales.
63 * Bookkeeping information about a loadable input method.
66 typedef struct _GtkIMModule GtkIMModule;
67 typedef struct _GtkIMModuleClass GtkIMModuleClass;
69 #define GTK_TYPE_IM_MODULE (gtk_im_module_get_type ())
70 #define GTK_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_CAST ((im_module), GTK_TYPE_IM_MODULE, GtkIMModule))
71 #define GTK_IS_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_TYPE ((im_module), GTK_TYPE_IM_MODULE))
75 GTypeModule parent_instance;
81 void (*list) (const GtkIMContextInfo ***contexts,
83 void (*init) (GTypeModule *module);
85 GtkIMContext *(*create) (const gchar *context_id);
87 GtkIMContextInfo **contexts;
93 struct _GtkIMModuleClass
95 GTypeModuleClass parent_class;
98 static GType gtk_im_module_get_type (void);
100 static gint n_loaded_contexts = 0;
101 static GHashTable *contexts_hash = NULL;
102 static GSList *modules_list = NULL;
104 static GObjectClass *parent_class = NULL;
107 gtk_im_module_load (GTypeModule *module)
109 GtkIMModule *im_module = GTK_IM_MODULE (module);
111 if (!im_module->builtin)
113 im_module->library = g_module_open (im_module->path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
114 if (!im_module->library)
116 g_warning ("%s", g_module_error());
120 /* extract symbols from the lib */
121 if (!g_module_symbol (im_module->library, "im_module_init",
122 (gpointer *)&im_module->init) ||
123 !g_module_symbol (im_module->library, "im_module_exit",
124 (gpointer *)&im_module->exit) ||
125 !g_module_symbol (im_module->library, "im_module_list",
126 (gpointer *)&im_module->list) ||
127 !g_module_symbol (im_module->library, "im_module_create",
128 (gpointer *)&im_module->create))
130 g_warning ("%s", g_module_error());
131 g_module_close (im_module->library);
137 /* call the module's init function to let it */
138 /* setup anything it needs to set up. */
139 im_module->init (module);
145 gtk_im_module_unload (GTypeModule *module)
147 GtkIMModule *im_module = GTK_IM_MODULE (module);
151 if (!im_module->builtin)
153 g_module_close (im_module->library);
154 im_module->library = NULL;
156 im_module->init = NULL;
157 im_module->exit = NULL;
158 im_module->list = NULL;
159 im_module->create = NULL;
163 /* This only will ever be called if an error occurs during
167 gtk_im_module_finalize (GObject *object)
169 GtkIMModule *module = GTK_IM_MODULE (object);
171 g_free (module->path);
173 parent_class->finalize (object);
176 G_DEFINE_TYPE (GtkIMModule, gtk_im_module, G_TYPE_TYPE_MODULE)
179 gtk_im_module_class_init (GtkIMModuleClass *class)
181 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
182 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
184 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
186 module_class->load = gtk_im_module_load;
187 module_class->unload = gtk_im_module_unload;
189 gobject_class->finalize = gtk_im_module_finalize;
193 gtk_im_module_init (GtkIMModule* object)
198 free_info (GtkIMContextInfo *info)
200 g_free ((char *)info->context_id);
201 g_free ((char *)info->context_name);
202 g_free ((char *)info->domain);
203 g_free ((char *)info->domain_dirname);
204 g_free ((char *)info->default_locales);
209 add_module (GtkIMModule *module, GSList *infos)
211 GSList *tmp_list = infos;
213 gint n = g_slist_length (infos);
214 module->contexts = g_new (GtkIMContextInfo *, n);
218 GtkIMContextInfo *info = tmp_list->data;
220 if (g_hash_table_lookup (contexts_hash, info->context_id))
222 free_info (info); /* Duplicate */
226 g_hash_table_insert (contexts_hash, (char *)info->context_id, module);
227 module->contexts[i++] = tmp_list->data;
231 tmp_list = tmp_list->next;
233 g_slist_free (infos);
234 module->n_contexts = i;
236 modules_list = g_slist_prepend (modules_list, module);
242 correct_libdir_prefix (gchar **path)
244 /* GTK_LIBDIR here is supposed to still have the definition from
245 * Makefile.am, i.e. the build-time value. Do *not* include gtkprivate.h
248 if (strncmp (*path, GTK_LIBDIR, strlen (GTK_LIBDIR)) == 0)
250 /* This is an entry put there by make install on the
251 * packager's system. On Windows a prebuilt GTK+
252 * package can be installed in a random
253 * location. The gtk.immodules file distributed in
254 * such a package contains paths from the package
255 * builder's machine. Replace the path with the real
256 * one on this machine.
258 extern const gchar *_gtk_get_libdir ();
260 *path = g_strconcat (_gtk_get_libdir (), tem + strlen (GTK_LIBDIR), NULL);
266 correct_localedir_prefix (gchar **path)
268 /* As above, but for GTK_LOCALEDIR. Use separate function in case
269 * GTK_LOCALEDIR isn't a subfolder of GTK_LIBDIR.
271 if (strncmp (*path, GTK_LOCALEDIR, strlen (GTK_LOCALEDIR)) == 0)
273 extern const gchar *_gtk_get_localedir ();
275 *path = g_strconcat (_gtk_get_localedir (), tem + strlen (GTK_LOCALEDIR), NULL);
282 G_GNUC_UNUSED static GtkIMModule *
283 add_builtin_module (const gchar *module_name,
284 const GtkIMContextInfo **contexts,
287 GtkIMModule *module = g_object_new (GTK_TYPE_IM_MODULE, NULL);
288 GSList *infos = NULL;
291 for (i = 0; i < n_contexts; i++)
293 GtkIMContextInfo *info = g_new (GtkIMContextInfo, 1);
294 info->context_id = g_strdup (contexts[i]->context_id);
295 info->context_name = g_strdup (contexts[i]->context_name);
296 info->domain = g_strdup (contexts[i]->domain);
297 info->domain_dirname = g_strdup (contexts[i]->domain_dirname);
299 correct_localedir_prefix ((char **) &info->domain_dirname);
301 info->default_locales = g_strdup (contexts[i]->default_locales);
302 infos = g_slist_prepend (infos, info);
305 module->builtin = TRUE;
306 g_type_module_set_name (G_TYPE_MODULE (module), module_name);
307 add_module (module, infos);
313 gtk_im_module_initialize (void)
315 GString *line_buf = g_string_new (NULL);
316 GString *tmp_buf = g_string_new (NULL);
317 gchar *filename = gtk_rc_get_im_module_file();
319 gboolean have_error = FALSE;
321 GtkIMModule *module = NULL;
322 GSList *infos = NULL;
324 contexts_hash = g_hash_table_new (g_str_hash, g_str_equal);
326 #define do_builtin(m) \
328 const GtkIMContextInfo **contexts; \
330 extern void _gtk_immodule_ ## m ## _list (const GtkIMContextInfo ***contexts, \
331 guint *n_contexts); \
332 extern void _gtk_immodule_ ## m ## _init (GTypeModule *module); \
333 extern void _gtk_immodule_ ## m ## _exit (void); \
334 extern GtkIMContext *_gtk_immodule_ ## m ## _create (const gchar *context_id); \
336 _gtk_immodule_ ## m ## _list (&contexts, &n_contexts); \
337 module = add_builtin_module (#m, contexts, n_contexts); \
338 module->init = _gtk_immodule_ ## m ## _init; \
339 module->exit = _gtk_immodule_ ## m ## _exit; \
340 module->create = _gtk_immodule_ ## m ## _create; \
344 #ifdef INCLUDE_IM_am_et
347 #ifdef INCLUDE_IM_cedilla
348 do_builtin (cedilla);
350 #ifdef INCLUDE_IM_cyrillic_translit
351 do_builtin (cyrillic_translit);
353 #ifdef INCLUDE_IM_ime
356 #ifdef INCLUDE_IM_inuktitut
357 do_builtin (inuktitut);
359 #ifdef INCLUDE_IM_ipa
362 #ifdef INCLUDE_IM_multipress
363 do_builtin (multipress);
365 #ifdef INCLUDE_IM_thai
368 #ifdef INCLUDE_IM_ti_er
371 #ifdef INCLUDE_IM_ti_et
374 #ifdef INCLUDE_IM_viqr
377 #ifdef INCLUDE_IM_xim
383 file = g_fopen (filename, "r");
386 /* In case someone wants only the default input method,
387 * we allow no file at all.
389 g_string_free (line_buf, TRUE);
390 g_string_free (tmp_buf, TRUE);
395 while (!have_error && pango_read_line (file, line_buf))
401 if (!pango_skip_space (&p))
403 /* Blank line marking the end of a module
405 if (module && *p != '#')
407 add_module (module, infos);
417 /* Read a module location
419 module = g_object_new (GTK_TYPE_IM_MODULE, NULL);
421 if (!pango_scan_string (&p, tmp_buf) ||
422 pango_skip_space (&p))
424 g_warning ("Error parsing context info in '%s'\n %s",
425 filename, line_buf->str);
429 module->path = g_strdup (tmp_buf->str);
431 correct_libdir_prefix (&module->path);
433 g_type_module_set_name (G_TYPE_MODULE (module), module->path);
437 GtkIMContextInfo *info = g_new0 (GtkIMContextInfo, 1);
439 /* Read information about a context type
441 if (!pango_scan_string (&p, tmp_buf))
443 info->context_id = g_strdup (tmp_buf->str);
445 if (!pango_scan_string (&p, tmp_buf))
447 info->context_name = g_strdup (tmp_buf->str);
449 if (!pango_scan_string (&p, tmp_buf))
451 info->domain = g_strdup (tmp_buf->str);
453 if (!pango_scan_string (&p, tmp_buf))
455 info->domain_dirname = g_strdup (tmp_buf->str);
457 correct_localedir_prefix ((char **) &info->domain_dirname);
460 if (!pango_scan_string (&p, tmp_buf))
462 info->default_locales = g_strdup (tmp_buf->str);
464 if (pango_skip_space (&p))
467 infos = g_slist_prepend (infos, info);
471 g_warning ("Error parsing context info in '%s'\n %s",
472 filename, line_buf->str);
479 GSList *tmp_list = infos;
482 free_info (tmp_list->data);
483 tmp_list = tmp_list->next;
485 g_slist_free (infos);
487 g_object_unref (module);
490 add_module (module, infos);
493 g_string_free (line_buf, TRUE);
494 g_string_free (tmp_buf, TRUE);
499 compare_gtkimcontextinfo_name(const GtkIMContextInfo **a,
500 const GtkIMContextInfo **b)
502 return g_utf8_collate ((*a)->context_name, (*b)->context_name);
506 * _gtk_im_module_list:
507 * @contexts: location to store an array of pointers to #GtkIMContextInfo
508 * this array should be freed with g_free() when you are finished.
509 * The structures it points are statically allocated and should
510 * not be modified or freed.
511 * @n_contexts: the length of the array stored in @contexts
513 * List all available types of input method context
516 _gtk_im_module_list (const GtkIMContextInfo ***contexts,
525 GtkIMContextInfo simple_context_info = {
538 static gboolean beenhere = FALSE;
542 gtk_im_module_initialize ();
548 /* correct_localedir_prefix() requires its parameter to be a
551 simple_context_info.domain_dirname = g_strdup (simple_context_info.domain_dirname);
552 correct_localedir_prefix ((char **) &simple_context_info.domain_dirname);
557 *n_contexts = (n_loaded_contexts + 1);
564 *contexts = g_new (const GtkIMContextInfo *, n_loaded_contexts + 1);
566 (*contexts)[n++] = &simple_context_info;
568 tmp_list = modules_list;
571 GtkIMModule *module = tmp_list->data;
573 for (i=0; i<module->n_contexts; i++)
574 (*contexts)[n++] = module->contexts[i];
576 tmp_list = tmp_list->next;
579 /* fisrt element (Default) should always be at top */
580 qsort ((*contexts)+1, n-1, sizeof (GtkIMContextInfo *), (GCompareFunc)compare_gtkimcontextinfo_name);
585 * _gtk_im_module_create:
586 * @context_id: the context ID for the context type to create
588 * Create an IM context of a type specified by the string
591 * Return value: a newly created input context of or @context_id, or
592 * if that could not be created, a newly created GtkIMContextSimple.
595 _gtk_im_module_create (const gchar *context_id)
597 GtkIMModule *im_module;
598 GtkIMContext *context = NULL;
601 gtk_im_module_initialize ();
603 if (strcmp (context_id, SIMPLE_ID) != 0)
605 im_module = g_hash_table_lookup (contexts_hash, context_id);
608 g_warning ("Attempt to load unknown IM context type '%s'", context_id);
612 if (g_type_module_use (G_TYPE_MODULE (im_module)))
614 context = im_module->create (context_id);
615 g_type_module_unuse (G_TYPE_MODULE (im_module));
619 g_warning ("Loading IM context type '%s' failed", context_id);
624 return gtk_im_context_simple_new ();
629 /* Match @locale against @against.
631 * 'en_US' against 'en_US' => 4
632 * 'en_US' against 'en' => 3
633 * 'en', 'en_UK' against 'en_US' => 2
634 * all locales, against '*' => 1
637 match_locale (const gchar *locale,
638 const gchar *against,
641 if (strcmp (against, "*") == 0)
644 if (g_ascii_strcasecmp (locale, against) == 0)
647 if (g_ascii_strncasecmp (locale, against, 2) == 0)
648 return (against_len == 2) ? 3 : 2;
654 * _gtk_im_module_get_default_context_id:
655 * @client_window: a window
657 * Return the context_id of the best IM context type
658 * for the given window.
660 * Return value: the context ID (will never be %NULL)
663 _gtk_im_module_get_default_context_id (GdkWindow *client_window)
666 const gchar *context_id = NULL;
667 gint best_goodness = 0;
669 gchar *tmp_locale, *tmp;
672 GtkSettings *settings;
675 gtk_im_module_initialize ();
677 envvar = g_getenv ("GTK_IM_MODULE");
679 (strcmp (envvar, SIMPLE_ID) == 0 ||
680 g_hash_table_lookup (contexts_hash, envvar)))
683 /* Check if the certain immodule is set in XSETTINGS.
685 if (GDK_IS_DRAWABLE (client_window))
687 screen = gdk_drawable_get_screen (GDK_DRAWABLE (client_window));
688 settings = gtk_settings_get_for_screen (screen);
689 g_object_get (G_OBJECT (settings), "gtk-im-module", &tmp, NULL);
692 if (strcmp (tmp, SIMPLE_ID) == 0)
693 context_id = SIMPLE_ID;
697 module = g_hash_table_lookup (contexts_hash, tmp);
699 context_id = module->contexts[0]->context_id;
708 /* Strip the locale code down to the essentials
710 tmp_locale = _gtk_get_lc_ctype ();
711 tmp = strchr (tmp_locale, '.');
714 tmp = strchr (tmp_locale, '@');
718 tmp_list = modules_list;
721 GtkIMModule *module = tmp_list->data;
723 for (i = 0; i < module->n_contexts; i++)
725 const gchar *p = module->contexts[i]->default_locales;
728 const gchar *q = strchr (p, ':');
729 gint goodness = match_locale (tmp_locale, p, q ? q - p : strlen (p));
731 if (goodness > best_goodness)
733 context_id = module->contexts[i]->context_id;
734 best_goodness = goodness;
737 p = q ? q + 1 : NULL;
741 tmp_list = tmp_list->next;
746 return context_id ? context_id : SIMPLE_ID;