Back to index

im-sdk  12.3.91
ime_module.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <dlfcn.h>
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include "config.h"
00006 #endif
00007 
00008 #include "imbean.h"
00009 #include "ime_module.h"
00010 
00011 extern struct ImmServicesRec le_services;
00012 
00013 /*************************************************************/
00014 /*                       ImeModule                           */
00015 /*************************************************************/
00016 ImeModuleRec *ime_module_new()
00017 {
00018     ImeModuleRec *ime_module = NULL;
00019 
00020     ime_module = (ImeModuleRec *) calloc(1, sizeof(ImeModuleRec));
00021     if (ime_module != NULL) {
00022         ime_module->pc_cursor_x = ime_module->pc_cursor_y = 0x80000000;
00023     }
00024 
00025     return (ime_module);
00026 }
00027 
00028 ImeResult ime_module_destroy(ImeModuleRec * ime_module)
00029 {
00030     if (ime_module == NULL)
00031        return (IME_OK);
00032 
00033     if (ime_module->methods && ime_module->methods->ImeDestroy)
00034        ime_module->methods->ImeDestroy(ime_module->info);
00035 
00036     if (ime_module->object_file_path)
00037        free((char *) ime_module->object_file_path);
00038 
00039     if (ime_module->args)
00040        free((char *) ime_module->args);
00041 
00042     if (ime_module->module_handler)
00043        dlclose(ime_module->module_handler);
00044 
00045     free((char *) ime_module);
00046     return (IME_OK);
00047 }
00048 
00049 ImeResult ime_module_print(ImeModuleRec * ime_module)
00050 {
00051 #if DEBUG
00052     if (ime_module == NULL)
00053        return (IME_OK);
00054 
00055     printf("\nImeModule: %p\n", ime_module);
00056     if (ime_module->object_file_path)
00057        printf("    object_file_path: %s\n", ime_module->object_file_path);
00058 
00059     if (ime_module->args)
00060        printf("    args: %s\n", ime_module->args);
00061 
00062     if (ime_module->info) {
00063        if (ime_module->info->version)
00064            printf("    version: %d\n", ime_module->info->version);
00065 
00066        if (ime_module->info->uuid)
00067            printf("    uuid: %s\n", (char *) ime_module->info->uuid);
00068 
00069        if (ime_module->info->name)
00070            printf("    name: %s\n", (char *) ime_module->info->name);
00071 
00072        if (ime_module->info->author)
00073            printf("    author: %s\n", (char *) ime_module->info->author);
00074 
00075        if (ime_module->info->copyright)
00076            printf("    copyright: %s\n",
00077                  (char *) ime_module->info->copyright);
00078 
00079        if (ime_module->info->hinting)
00080            printf("    hinting: %s\n",
00081                  (char *) ime_module->info->hinting);
00082 
00083        if (ime_module->info->icon_file)
00084            printf("    icon_file: %s\n",
00085                  (char *) ime_module->info->icon_file);
00086 
00087        if (ime_module->info->support_locales)
00088            printf("    support_locales: %s\n",
00089                  (char *) ime_module->info->support_locales);
00090     }
00091 
00092     return (IME_OK);
00093 #endif
00094 }
00095 
00096 ImeResult ime_module_load_object_file(ImeModuleRec * ime_module,
00097                                   char *basedir, char *ime_object_file,
00098                                   char *ime_args)
00099 {
00100     void *module_handler = NULL;
00101     ImeResult (*ime_register_method) ();
00102     ImeResult result;
00103     int i, argc = 0;
00104     char **argv = NULL;
00105 
00106     if (ime_module == NULL)
00107        return (IME_FAIL);
00108 
00109     module_handler = (void *) dlopen(ime_object_file, RTLD_LAZY);
00110     if (module_handler == NULL) {
00111        fprintf(stderr, "Warning: could not open module file: %s\n",
00112               ime_object_file);
00113        goto Error;
00114     }
00115 
00116     ime_module->module_handler = module_handler;
00117 
00118     ime_register_method = dlsym(module_handler, "RegisterIME");
00119     if (ime_register_method == NULL) {
00120        fprintf(stderr, "Warning: no 'RegisterIME' method: %s\n",
00121               ime_object_file);
00122        goto Error;
00123     }
00124 
00125     if (ime_args || *ime_args) {
00126        char *ptr = ime_args;
00127 
00128        if (ime_module->args)
00129            free((char *) ime_module->args);
00130        ime_module->args = (char *) strdup(ime_args);
00131 
00132        /* get the value of argc */
00133        while (*ptr) {
00134            while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
00135               ptr++;
00136            if (*ptr)
00137               argc++;
00138            while (*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '\n')
00139               ptr++;
00140        }
00141 
00142        /* get the value of argv */
00143 #if 0
00144        argc += 2;
00145 #endif
00146        DEBUG_printf("ime_args: %s, argc: %d\n", ime_module->args, argc);
00147        argv = (char **) calloc(argc, sizeof(char *));
00148        if (argv == NULL) {
00149            argc = 0;
00150        } else {
00151            i = 0;
00152 #if 0
00153            argv[i++] = "-basedir";
00154            argv[i++] = basedir;
00155 #endif
00156            ptr = ime_args;
00157            while (*ptr) {
00158               while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
00159                   ptr++;
00160               if (*ptr)
00161                   argv[i++] = ptr;
00162               while (*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '\n')
00163                   ptr++;
00164               if (*ptr)
00165                   *(ptr++) = '\0';
00166            }
00167        }
00168     }
00169 
00170     result = ime_register_method(&le_services, &(ime_module->info),
00171                              &(ime_module->methods), argc, argv);
00172     if (argv != NULL)
00173        free((char *) argv);
00174 
00175     if (result != IME_OK) {
00176        fprintf(stderr, "Warning: call 'RegisterIME' Failed: %s\n",
00177               ime_object_file);
00178        goto Error;
00179     }
00180 
00181     if (ime_module->info == NULL || ime_module->methods == NULL) {
00182        fprintf(stderr,
00183               "Warning: 'RegisterIME' return wrong informations: %s\n",
00184               ime_object_file);
00185        goto Error;
00186     }
00187 
00188     if (ime_module->methods->ImeInitialize != NULL) {
00189        result = ime_module->methods->ImeInitialize(ime_module->info);
00190        if (result != IME_OK) {
00191            fprintf(stderr, "Warning: call 'ImeInitialize' Failed: %s\n",
00192                   ime_object_file);
00193            goto Error;
00194        }
00195     }
00196 
00197     if (ime_module->object_file_path)
00198        free((char *) ime_module->object_file_path);
00199     ime_module->object_file_path = (char *) strdup(ime_object_file);
00200 
00201     return (IME_OK);
00202 
00203   Error:
00204     if (ime_module->object_file_path) {
00205        free((char *) ime_module->object_file_path);
00206        ime_module->object_file_path = NULL;
00207     }
00208 
00209     if (ime_module->args) {
00210        free((char *) ime_module->args);
00211        ime_module->args = NULL;
00212     }
00213 
00214     if (module_handler)
00215        dlclose(module_handler);
00216     ime_module->module_handler = NULL;
00217 
00218     return (IME_FAIL);
00219 }
00220 
00221 char *standard_error_message[] = {
00222     "OK",
00223     "Fail",
00224     "Register Fail",
00225 };
00226 
00227 char *ime_module_get_error_message(ImeModuleRec * ime_module, int error_id)
00228 {
00229 
00230     if (ime_module == NULL)
00231        return (NULL);
00232 
00233     if (error_id <= IME_OK) {
00234        return (NULL);
00235     }
00236 
00237 /*
00238     if (error_id > IME_FAIL && error_id <= IME_ERROR_NUM) {
00239        return (standard_error_messages[error_id]);
00240     }
00241 */
00242 
00243     if (ime_module->methods == NULL)
00244        return (NULL);
00245     if (ime_module->methods->ImeGetErrorMessage == NULL)
00246        return (NULL);
00247 
00248     return ((char *) ime_module->methods->ImeGetErrorMessage(error_id));
00249 }
00250 
00251 /*************************************************************/
00252 /*                   ImeModuleContext                        */
00253 /*************************************************************/
00254 ImeModuleContextRec *ime_module_context_new()
00255 {
00256     ImeModuleContextRec *ime_module_context = NULL;
00257 
00258     ime_module_context =
00259         (ImeModuleContextRec *) calloc(1, sizeof(ImeModuleContextRec));
00260     return (ime_module_context);
00261 }
00262 
00263 ImeResult ime_module_context_destroy(ImeModuleContextRec *ime_module_context)
00264 {
00265     if (ime_module_context == NULL)
00266         return (IME_OK);
00267 
00268     ime_module_destroy(ime_module_context->ime_module);
00269     free((char *) ime_module_context);
00270 
00271     return (IME_OK);
00272 }
00273 
00274 ImeResult ime_module_context_print(ImeModuleContextRec *ime_module_context)
00275 {
00276 #if DEBUG
00277     if (ime_module_context == NULL)
00278         return (IME_OK);
00279 
00280     ime_module_print(ime_module_context->ime_module);
00281 
00282     printf("    enabled: %d\n", ime_module_context->enabled);
00283 
00284     return (IME_OK);
00285 #endif
00286 }