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 "gtkimmoduleprivate.h"
36 #include "gtkimcontextsimple.h"
37 #include "gtksettings.h"
38 #include "gtkprivate.h"
41 #include "deprecated/gtkrc.h"
43 #define SIMPLE_ID "gtk-im-context-simple"
47 * @context_id: The unique identification string of the input method.
48 * @context_name: The human-readable name of the input method.
49 * @domain: Translation domain to be used with dgettext()
50 * @domain_dirname: Name of locale directory for use with bindtextdomain()
51 * @default_locales: A colon-separated list of locales where this input method
52 * should be the default. The asterisk "*" sets the default for all locales.
54 * Bookkeeping information about a loadable input method.
57 typedef struct _GtkIMModule GtkIMModule;
58 typedef struct _GtkIMModuleClass GtkIMModuleClass;
60 #define GTK_TYPE_IM_MODULE (gtk_im_module_get_type ())
61 #define GTK_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_CAST ((im_module), GTK_TYPE_IM_MODULE, GtkIMModule))
62 #define GTK_IS_IM_MODULE(im_module) (G_TYPE_CHECK_INSTANCE_TYPE ((im_module), GTK_TYPE_IM_MODULE))
66 GTypeModule parent_instance;
72 void (*list) (const GtkIMContextInfo ***contexts,
74 void (*init) (GTypeModule *module);
76 GtkIMContext *(*create) (const gchar *context_id);
78 GtkIMContextInfo **contexts;
84 struct _GtkIMModuleClass
86 GTypeModuleClass parent_class;
89 static GType gtk_im_module_get_type (void);
91 static gint n_loaded_contexts = 0;
92 static GHashTable *contexts_hash = NULL;
93 static GSList *modules_list = NULL;
95 static GObjectClass *parent_class = NULL;
98 gtk_im_module_load (GTypeModule *module)
100 GtkIMModule *im_module = GTK_IM_MODULE (module);
102 if (!im_module->builtin)
104 im_module->library = g_module_open (im_module->path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
105 if (!im_module->library)
107 g_warning ("%s", g_module_error());
111 /* extract symbols from the lib */
112 if (!g_module_symbol (im_module->library, "im_module_init",
113 (gpointer *)&im_module->init) ||
114 !g_module_symbol (im_module->library, "im_module_exit",
115 (gpointer *)&im_module->exit) ||
116 !g_module_symbol (im_module->library, "im_module_list",
117 (gpointer *)&im_module->list) ||
118 !g_module_symbol (im_module->library, "im_module_create",
119 (gpointer *)&im_module->create))
121 g_warning ("%s", g_module_error());
122 g_module_close (im_module->library);
128 /* call the module's init function to let it */
129 /* setup anything it needs to set up. */
130 im_module->init (module);
136 gtk_im_module_unload (GTypeModule *module)
138 GtkIMModule *im_module = GTK_IM_MODULE (module);
142 if (!im_module->builtin)
144 g_module_close (im_module->library);
145 im_module->library = NULL;
147 im_module->init = NULL;
148 im_module->exit = NULL;
149 im_module->list = NULL;
150 im_module->create = NULL;
154 /* This only will ever be called if an error occurs during
158 gtk_im_module_finalize (GObject *object)
160 GtkIMModule *module = GTK_IM_MODULE (object);
162 g_free (module->path);
164 parent_class->finalize (object);
167 G_DEFINE_TYPE (GtkIMModule, gtk_im_module, G_TYPE_TYPE_MODULE)
170 gtk_im_module_class_init (GtkIMModuleClass *class)
172 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
173 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
175 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (class));
177 module_class->load = gtk_im_module_load;
178 module_class->unload = gtk_im_module_unload;
180 gobject_class->finalize = gtk_im_module_finalize;
184 gtk_im_module_init (GtkIMModule* object)
189 free_info (GtkIMContextInfo *info)
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);
200 add_module (GtkIMModule *module, GSList *infos)
202 GSList *tmp_list = infos;
204 gint n = g_slist_length (infos);
205 module->contexts = g_new (GtkIMContextInfo *, n);
209 GtkIMContextInfo *info = tmp_list->data;
211 if (g_hash_table_lookup (contexts_hash, info->context_id))
213 free_info (info); /* Duplicate */
217 g_hash_table_insert (contexts_hash, (char *)info->context_id, module);
218 module->contexts[i++] = tmp_list->data;
222 tmp_list = tmp_list->next;
224 g_slist_free (infos);
225 module->n_contexts = i;
227 modules_list = g_slist_prepend (modules_list, module);
233 correct_libdir_prefix (gchar **path)
235 /* GTK_LIBDIR is the build-time libdir */
236 if (strncmp (*path, GTK_LIBDIR, strlen (GTK_LIBDIR)) == 0)
238 /* This is an entry put there by make install on the
239 * packager's system. On Windows a prebuilt GTK+
240 * package can be installed in a random
241 * location. The gtk.immodules file distributed in
242 * such a package contains paths from the package
243 * builder's machine. Replace the path with the real
244 * one on this machine.
247 *path = g_strconcat (_gtk_get_libdir (), tem + strlen (GTK_LIBDIR), NULL);
253 correct_localedir_prefix (gchar **path)
256 if (strncmp (*path, GTK_LOCALEDIR, strlen (GTK_LOCALEDIR)) == 0)
259 *path = g_strconcat (_gtk_get_localedir (), tem + strlen (GTK_LOCALEDIR), NULL);
266 G_GNUC_UNUSED static GtkIMModule *
267 add_builtin_module (const gchar *module_name,
268 const GtkIMContextInfo **contexts,
271 GtkIMModule *module = g_object_new (GTK_TYPE_IM_MODULE, NULL);
272 GSList *infos = NULL;
275 for (i = 0; i < n_contexts; i++)
277 GtkIMContextInfo *info = g_new (GtkIMContextInfo, 1);
278 info->context_id = g_strdup (contexts[i]->context_id);
279 info->context_name = g_strdup (contexts[i]->context_name);
280 info->domain = g_strdup (contexts[i]->domain);
281 info->domain_dirname = g_strdup (contexts[i]->domain_dirname);
283 correct_localedir_prefix ((char **) &info->domain_dirname);
285 info->default_locales = g_strdup (contexts[i]->default_locales);
286 infos = g_slist_prepend (infos, info);
289 module->builtin = TRUE;
290 g_type_module_set_name (G_TYPE_MODULE (module), module_name);
291 add_module (module, infos);
297 gtk_im_module_initialize (void)
299 GString *line_buf = g_string_new (NULL);
300 GString *tmp_buf = g_string_new (NULL);
301 gchar *filename = gtk_rc_get_im_module_file();
303 gboolean have_error = FALSE;
305 GtkIMModule *module = NULL;
306 GSList *infos = NULL;
308 contexts_hash = g_hash_table_new (g_str_hash, g_str_equal);
310 #define do_builtin(m) \
312 const GtkIMContextInfo **contexts; \
314 extern void _gtk_immodule_ ## m ## _list (const GtkIMContextInfo ***contexts, \
315 guint *n_contexts); \
316 extern void _gtk_immodule_ ## m ## _init (GTypeModule *module); \
317 extern void _gtk_immodule_ ## m ## _exit (void); \
318 extern GtkIMContext *_gtk_immodule_ ## m ## _create (const gchar *context_id); \
320 _gtk_immodule_ ## m ## _list (&contexts, &n_contexts); \
321 module = add_builtin_module (#m, contexts, n_contexts); \
322 module->init = _gtk_immodule_ ## m ## _init; \
323 module->exit = _gtk_immodule_ ## m ## _exit; \
324 module->create = _gtk_immodule_ ## m ## _create; \
328 #ifdef INCLUDE_IM_am_et
331 #ifdef INCLUDE_IM_cedilla
332 do_builtin (cedilla);
334 #ifdef INCLUDE_IM_cyrillic_translit
335 do_builtin (cyrillic_translit);
337 #ifdef INCLUDE_IM_ime
340 #ifdef INCLUDE_IM_inuktitut
341 do_builtin (inuktitut);
343 #ifdef INCLUDE_IM_ipa
346 #ifdef INCLUDE_IM_multipress
347 do_builtin (multipress);
349 #ifdef INCLUDE_IM_thai
352 #ifdef INCLUDE_IM_ti_er
355 #ifdef INCLUDE_IM_ti_et
358 #ifdef INCLUDE_IM_viqr
361 #ifdef INCLUDE_IM_xim
367 file = g_fopen (filename, "r");
370 /* In case someone wants only the default input method,
371 * we allow no file at all.
373 g_string_free (line_buf, TRUE);
374 g_string_free (tmp_buf, TRUE);
379 while (!have_error && pango_read_line (file, line_buf))
385 if (!pango_skip_space (&p))
387 /* Blank line marking the end of a module
389 if (module && *p != '#')
391 add_module (module, infos);
401 /* Read a module location
403 module = g_object_new (GTK_TYPE_IM_MODULE, NULL);
405 if (!pango_scan_string (&p, tmp_buf) ||
406 pango_skip_space (&p))
408 g_warning ("Error parsing context info in '%s'\n %s",
409 filename, line_buf->str);
413 module->path = g_strdup (tmp_buf->str);
415 correct_libdir_prefix (&module->path);
417 g_type_module_set_name (G_TYPE_MODULE (module), module->path);
421 GtkIMContextInfo *info = g_new0 (GtkIMContextInfo, 1);
423 /* Read information about a context type
425 if (!pango_scan_string (&p, tmp_buf))
427 info->context_id = g_strdup (tmp_buf->str);
429 if (!pango_scan_string (&p, tmp_buf))
431 info->context_name = g_strdup (tmp_buf->str);
433 if (!pango_scan_string (&p, tmp_buf))
435 info->domain = g_strdup (tmp_buf->str);
437 if (!pango_scan_string (&p, tmp_buf))
439 info->domain_dirname = g_strdup (tmp_buf->str);
441 correct_localedir_prefix ((char **) &info->domain_dirname);
444 if (!pango_scan_string (&p, tmp_buf))
446 info->default_locales = g_strdup (tmp_buf->str);
448 if (pango_skip_space (&p))
451 infos = g_slist_prepend (infos, info);
455 g_warning ("Error parsing context info in '%s'\n %s",
456 filename, line_buf->str);
463 GSList *tmp_list = infos;
466 free_info (tmp_list->data);
467 tmp_list = tmp_list->next;
469 g_slist_free (infos);
471 g_object_unref (module);
474 add_module (module, infos);
477 g_string_free (line_buf, TRUE);
478 g_string_free (tmp_buf, TRUE);
483 compare_gtkimcontextinfo_name(const GtkIMContextInfo **a,
484 const GtkIMContextInfo **b)
486 return g_utf8_collate ((*a)->context_name, (*b)->context_name);
490 * _gtk_im_module_list:
491 * @contexts: location to store an array of pointers to #GtkIMContextInfo
492 * this array should be freed with g_free() when you are finished.
493 * The structures it points are statically allocated and should
494 * not be modified or freed.
495 * @n_contexts: the length of the array stored in @contexts
497 * List all available types of input method context
500 _gtk_im_module_list (const GtkIMContextInfo ***contexts,
509 GtkIMContextInfo simple_context_info = {
522 static gboolean beenhere = FALSE;
526 gtk_im_module_initialize ();
532 /* correct_localedir_prefix() requires its parameter to be a
535 simple_context_info.domain_dirname = g_strdup (simple_context_info.domain_dirname);
536 correct_localedir_prefix ((char **) &simple_context_info.domain_dirname);
541 *n_contexts = (n_loaded_contexts + 1);
548 *contexts = g_new (const GtkIMContextInfo *, n_loaded_contexts + 1);
550 (*contexts)[n++] = &simple_context_info;
552 tmp_list = modules_list;
555 GtkIMModule *module = tmp_list->data;
557 for (i=0; i<module->n_contexts; i++)
558 (*contexts)[n++] = module->contexts[i];
560 tmp_list = tmp_list->next;
563 /* fisrt element (Default) should always be at top */
564 qsort ((*contexts)+1, n-1, sizeof (GtkIMContextInfo *), (GCompareFunc)compare_gtkimcontextinfo_name);
569 * _gtk_im_module_create:
570 * @context_id: the context ID for the context type to create
572 * Create an IM context of a type specified by the string
575 * Return value: a newly created input context of or @context_id, or
576 * if that could not be created, a newly created GtkIMContextSimple.
579 _gtk_im_module_create (const gchar *context_id)
581 GtkIMModule *im_module;
582 GtkIMContext *context = NULL;
585 gtk_im_module_initialize ();
587 if (strcmp (context_id, SIMPLE_ID) != 0)
589 im_module = g_hash_table_lookup (contexts_hash, context_id);
592 g_warning ("Attempt to load unknown IM context type '%s'", context_id);
596 if (g_type_module_use (G_TYPE_MODULE (im_module)))
598 context = im_module->create (context_id);
599 g_type_module_unuse (G_TYPE_MODULE (im_module));
603 g_warning ("Loading IM context type '%s' failed", context_id);
608 return gtk_im_context_simple_new ();
613 /* Match @locale against @against.
615 * 'en_US' against 'en_US' => 4
616 * 'en_US' against 'en' => 3
617 * 'en', 'en_UK' against 'en_US' => 2
618 * all locales, against '*' => 1
621 match_locale (const gchar *locale,
622 const gchar *against,
625 if (strcmp (against, "*") == 0)
628 if (g_ascii_strcasecmp (locale, against) == 0)
631 if (g_ascii_strncasecmp (locale, against, 2) == 0)
632 return (against_len == 2) ? 3 : 2;
638 lookup_immodule (gchar **immodules_list)
640 while (immodules_list && *immodules_list)
642 if (g_strcmp0 (*immodules_list, SIMPLE_ID) == 0)
647 module = g_hash_table_lookup (contexts_hash, *immodules_list);
649 return module->contexts[0]->context_id;
658 * _gtk_im_module_get_default_context_id:
659 * @client_window: a window
661 * Return the context_id of the best IM context type
662 * for the given window.
664 * Return value: the context ID (will never be %NULL)
667 _gtk_im_module_get_default_context_id (GdkWindow *client_window)
670 const gchar *context_id = NULL;
671 gint best_goodness = 0;
673 gchar *tmp_locale, *tmp, **immodules;
676 GtkSettings *settings;
679 gtk_im_module_initialize ();
681 envvar = g_getenv("GTK_IM_MODULE");
684 immodules = g_strsplit(envvar, ":", 0);
685 context_id = lookup_immodule(immodules);
686 g_strfreev(immodules);
692 /* Check if the certain immodule is set in XSETTINGS.
694 if (GDK_IS_WINDOW (client_window))
696 screen = gdk_window_get_screen (client_window);
697 settings = gtk_settings_get_for_screen (screen);
698 g_object_get (G_OBJECT (settings), "gtk-im-module", &tmp, NULL);
701 immodules = g_strsplit(tmp, ":", 0);
702 context_id = lookup_immodule(immodules);
703 g_strfreev(immodules);
711 /* Strip the locale code down to the essentials
713 tmp_locale = _gtk_get_lc_ctype ();
714 tmp = strchr (tmp_locale, '.');
717 tmp = strchr (tmp_locale, '@');
721 tmp_list = modules_list;
724 GtkIMModule *module = tmp_list->data;
726 for (i = 0; i < module->n_contexts; i++)
728 const gchar *p = module->contexts[i]->default_locales;
731 const gchar *q = strchr (p, ':');
732 gint goodness = match_locale (tmp_locale, p, q ? q - p : strlen (p));
734 if (goodness > best_goodness)
736 context_id = module->contexts[i]->context_id;
737 best_goodness = goodness;
740 p = q ? q + 1 : NULL;
744 tmp_list = tmp_list->next;
749 return context_id ? context_id : SIMPLE_ID;