| 1 |
#include <glib.h> |
| 2 |
#include <glib/gi18n-lib.h> |
| 3 |
#include <dlfcn.h> |
| 4 |
|
| 5 |
#ifndef DEFAULT_IM_MODULE_TYPE |
| 6 |
#define DEFAULT_IM_MODULE_TYPE "gtk3" |
| 7 |
#endif |
| 8 |
|
| 9 |
#define OPTION_TYPE_MESSAGE \ |
| 10 |
N_("Set im-module TYPE to \"gtk2\", \"gtk3\" or \"gtk4\". Default is " \ |
| 11 |
"\"gtk3\".") |
| 12 |
|
| 13 |
typedef const char * (* IBusIMGetContextIdFunc) (int *argc, char ***argv); |
| 14 |
|
| 15 |
static char *im_module_type; |
| 16 |
|
| 17 |
|
| 18 |
char * |
| 19 |
ibus_im_module_get_id (int argc, char *argv[]) |
| 20 |
{ |
| 21 |
static const GOptionEntry options[3] = { |
| 22 |
{ "type", (char)0, (int)0, G_OPTION_ARG_STRING, &im_module_type, |
| 23 |
OPTION_TYPE_MESSAGE, |
| 24 |
"TYPE"}, |
| 25 |
{ NULL } |
| 26 |
}; |
| 27 |
GOptionContext *option; |
| 28 |
GError *error = NULL; |
| 29 |
void *module; |
| 30 |
char *im_context_id; |
| 31 |
IBusIMGetContextIdFunc im_get_context_id; |
| 32 |
|
| 33 |
if (!(option = g_option_context_new (NULL))) { |
| 34 |
g_critical ("malloc GOptionContext is failed."); |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
g_option_context_add_main_entries (option, options, GETTEXT_PACKAGE); |
| 38 |
g_option_context_parse (option, &argc, &argv, &error); |
| 39 |
if (error) { |
| 40 |
g_critical ("%s", error->message); |
| 41 |
g_clear_error (&error); |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
g_option_context_free (option); |
| 45 |
if (!im_module_type) |
| 46 |
im_module_type = g_strdup (DEFAULT_IM_MODULE_TYPE); |
| 47 |
|
| 48 |
if (G_LIKELY (!g_strcmp0 (im_module_type, "gtk3"))) { |
| 49 |
module = dlopen (GTK3_IM_MODULEDIR "/im-ibus.so", |
| 50 |
RTLD_LAZY); |
| 51 |
} else if (!g_strcmp0 (im_module_type, "gtk4")) { |
| 52 |
const char *module_path_env = g_getenv ("GTK_PATH"); |
| 53 |
char *module_path; |
| 54 |
if (module_path_env) { |
| 55 |
module_path = g_build_filename (module_path_env, |
| 56 |
GTK4_IM_MODULEDIR "/libim-ibus.so", |
| 57 |
NULL); |
| 58 |
} else { |
| 59 |
module_path = g_strdup (GTK4_IM_MODULEDIR "/libim-ibus.so"); |
| 60 |
} |
| 61 |
module = dlopen (module_path, RTLD_LAZY); |
| 62 |
g_free (module_path); |
| 63 |
} else if (!g_strcmp0 (im_module_type, "gtk2")) { |
| 64 |
module = dlopen (GTK2_IM_MODULEDIR "/im-ibus.so", |
| 65 |
RTLD_LAZY); |
| 66 |
} else { |
| 67 |
module = dlopen (im_module_type, RTLD_LAZY); |
| 68 |
} |
| 69 |
if (!module) { |
| 70 |
g_warning ("Not found module: %s", dlerror ()); |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
|
| 74 |
im_get_context_id = dlsym (module, "im_get_context_id"); |
| 75 |
if (!im_get_context_id) { |
| 76 |
g_warning ("Not found im_get_context_id: %s", dlerror ()); |
| 77 |
dlclose (module); |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
im_context_id = strdup (im_get_context_id (&argc, &argv)); |
| 82 |
dlclose (module); |
| 83 |
return im_context_id; |
| 84 |
} |
| 85 |
|
| 86 |
#undef DEFAULT_IM_MODULE_TYPE |
| 87 |
|