2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
11 #include "ecore_imf_private.h"
13 static void _ecore_imf_module_load_all(void);
14 static void _ecore_imf_module_append(Ecore_Plugin *plugin, const Ecore_IMF_Context_Info *info, Ecore_IMF_Context *(*imf_module_create)(void));
15 static void _ecore_imf_module_free(Ecore_IMF_Module *module);
16 static int _ecore_imf_modules_exists(const char *ctx_id);
18 static Ecore_Path_Group *ecore_imf_modules_path = NULL;
19 static Ecore_Hash *modules = NULL;
22 ecore_imf_module_init(void)
24 char pathname[PATH_MAX];
27 ecore_imf_modules_path = ecore_path_group_new();
28 snprintf(pathname, sizeof(pathname), "%s/ecore/immodules/",
30 ecore_path_group_add(ecore_imf_modules_path, pathname);
32 homedir = getenv("HOME");
35 snprintf(pathname, sizeof(pathname), "%s/.ecore/immodules/",
37 ecore_path_group_add(ecore_imf_modules_path, pathname);
41 _ecore_imf_module_load_all();
45 ecore_imf_module_shutdown(void)
49 ecore_hash_destroy(modules);
53 ecore_path_group_del(ecore_imf_modules_path);
54 ecore_imf_modules_path = NULL;
58 ecore_imf_module_available_get(void)
63 if (!modules) return NULL;
65 /* ecore_hash_values() */
66 values = ecore_list_new();
67 while (i < ecore_prime_table[modules->size])
69 if (modules->buckets[i])
71 Ecore_Hash_Node *node;
73 for (node = modules->buckets[i]; node; node = node->next)
74 ecore_list_append(values, node->value);
78 ecore_list_first_goto(values);
84 ecore_imf_module_get(const char *ctx_id)
86 if (!modules) return NULL;
87 return ecore_hash_get(modules, ctx_id);
91 ecore_imf_module_context_create(const char *ctx_id)
93 Ecore_IMF_Module *module;
94 Ecore_IMF_Context *ctx = NULL;
96 if (!modules) return NULL;
97 module = ecore_hash_get(modules, ctx_id);
100 ctx = module->create();
101 if (!ECORE_MAGIC_CHECK(ctx, ECORE_MAGIC_CONTEXT))
103 ECORE_MAGIC_FAIL(ctx, ECORE_MAGIC_CONTEXT,
104 "ecore_imf_module_context_create");
107 ctx->module = module;
113 ecore_imf_module_context_ids_get(void)
115 if (!modules) return NULL;
116 return ecore_hash_keys(modules);
120 ecore_imf_module_context_ids_by_canvas_type_get(const char *canvas_type)
125 if (!modules) return NULL;
128 return ecore_imf_module_context_ids_get();
130 values = ecore_list_new();
131 while (i < ecore_prime_table[modules->size])
133 if (modules->buckets[i])
135 Ecore_Hash_Node *node;
137 for (node = modules->buckets[i]; node; node = node->next)
139 Ecore_IMF_Module *module = node->value;
140 if (strcmp(module->info->canvas_type, canvas_type) == 0)
141 ecore_list_append(values, (void *) module->info->id);
146 ecore_list_first_goto(values);
152 _ecore_imf_module_load_all(void)
156 Ecore_Plugin *plugin;
157 const Ecore_IMF_Context_Info *info = NULL;
158 int (*imf_module_init)(const Ecore_IMF_Context_Info **info);
159 Ecore_IMF_Context *(*imf_module_create)(void);
161 avail = ecore_plugin_available_get(ecore_imf_modules_path);
164 ecore_list_first_goto(avail);
165 while ((filename = ecore_list_next(avail)))
167 plugin = ecore_plugin_load(ecore_imf_modules_path, filename, NULL);
170 fprintf(stderr, "** ecore_imf: Error loading input method plugin %s!\n",
175 imf_module_init = ecore_plugin_symbol_get(plugin, "imf_module_init");
176 if (!imf_module_init || !imf_module_init(&info) || !info)
178 fprintf(stderr, "** ecore_imf: Error initializing input method plugin %s! "
179 "'imf_module_init' is missing or failed to run!",
181 ecore_plugin_unload(plugin);
185 if (_ecore_imf_modules_exists(info->id))
187 fprintf(stderr, "** ecore_imf: Error loading input method plugin %s! "
188 "Plugin with id='%s' already exists!",
190 ecore_plugin_unload(plugin);
194 imf_module_create = ecore_plugin_symbol_get(plugin, "imf_module_create");
195 if (!imf_module_create)
197 fprintf(stderr, "** ecore_imf: Error setting up input method plugin %s! "
198 "'imf_module_create' is missing!",
200 ecore_plugin_unload(plugin);
204 _ecore_imf_module_append(plugin, info, imf_module_create);
207 ecore_list_destroy(avail);
211 _ecore_imf_module_append(Ecore_Plugin *plugin,
212 const Ecore_IMF_Context_Info *info,
213 Ecore_IMF_Context *(*imf_module_create)(void))
215 Ecore_IMF_Module *module;
219 modules = ecore_hash_new(ecore_str_hash, ecore_str_compare);
220 ecore_hash_free_key_cb_set(modules, free);
221 ecore_hash_free_value_cb_set(modules, (Ecore_Free_Cb) _ecore_imf_module_free);
224 module = malloc(sizeof(Ecore_IMF_Module));
225 module->plugin = plugin;
227 /* cache imf_module_create as it may be used several times */
228 module->create = imf_module_create;
230 ecore_hash_set(modules, strdup(info->id), module);
234 _ecore_imf_module_free(Ecore_IMF_Module *module)
236 int (*imf_module_exit)(void);
238 imf_module_exit = ecore_plugin_symbol_get(module->plugin, "imf_module_exit");
239 if (imf_module_exit) imf_module_exit();
240 ecore_plugin_unload(module->plugin);
245 _ecore_imf_modules_exists(const char *ctx_id)
247 if (!modules) return 0;
248 return (ecore_hash_get(modules, ctx_id) != NULL);