Back to index

im-sdk  12.3.91
ctim.c
Go to the documentation of this file.
00001 /*
00002   Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004   Permission is hereby granted, free of charge, to any person obtaining a
00005   copy of this software and associated documentation files (the
00006   "Software"), to deal in the Software without restriction, including
00007   without limitation the rights to use, copy, modify, merge, publish,
00008   distribute, sublicense, and/or sell copies of the Software, and to
00009   permit persons to whom the Software is furnished to do so, subject to
00010   the following conditions: The above copyright notice and this
00011   permission notice shall be included in all copies or substantial
00012   portions of the Software.
00013 
00014 
00015   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018   IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020   CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021   THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022   ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025   Except as contained in this notice, the names of The Open Group and/or
00026   Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027   promote the sale, use or other dealings in this Software without prior
00028   written authorization from The Open Group and/or Sun Microsystems,
00029   Inc., as applicable.
00030 
00031 
00032   X Window System is a trademark of The Open Group
00033 
00034   OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035   logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036   Group. All other trademarks and registered trademarks mentioned herein
00037   are the property of their respective owners. No right, title or
00038   interest in or to any trademark, service mark, logo or trade name of
00039   Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 #ifdef HAVE_CONFIG_H
00044 #include <config.h>
00045 #endif
00046 
00047 #ifdef HAVE_STRING_H
00048 #include <string.h>
00049 #endif
00050 #ifdef HAVE_MEMORY_H
00051 #include <memory.h>
00052 #endif
00053 #ifdef HAVE_STDLIB_H
00054 #include <stdlib.h>
00055 #endif
00056 #include <stdio.h>
00057 
00058 #include "unit_input.h"
00059 #include "codetable.h"
00060 #include "logf.h"
00061 #include "ctim.h"
00062 
00063 extern int codetable_filter(CodeTableStruct *hztbl, IMEKey key_event, IMEBufferRec *ime_buffer);
00064 
00065 int  ctim_SetOptions(IMECore, char *);
00066 int  ctim_SetValues(IMECore, IMEArgList, int);
00067 int  ctim_Init(IMECore);
00068 int  ctim_Open(IMECore, IMEBuffer, void *handler);
00069 int  ctim_Filter(IMECore, IMEKey, IMEArgList, IMEBuffer);
00070 void ctim_Close(IMECore);
00071 
00072 #ifdef WIN32
00073 #define EXPORT extern __declspec(dllexport)
00074 EXPORT
00075 #endif
00076 
00077 /* IF Method */
00078 IMEMethodsRec ime_methods = {
00079   ctim_SetOptions,
00080   ctim_SetValues,
00081   ctim_Init,
00082   ctim_Open,
00083   ctim_Filter,
00084   ctim_Close
00085 };
00086 
00087 #define SUFFIX_DICT_FILE    ".data"
00088 
00089 /* Set options for codetable input method. */
00090 int ctim_SetOptions(IMECore core, char *options)
00091 {
00092   char        data_path[256];
00093 
00094   sprintf(data_path, "%s/%s/%s/%s/%s%s", DEFAULT_ENGINE_PATH, core->envinfo.lang_name, options, DEFAULT_DICT_PATH, core->baseinfo.ename, SUFFIX_DICT_FILE);
00095   core->envinfo.data_path = (char *)strdup(data_path);
00096 
00097   log_f("data_path :%s\n", core->envinfo.data_path);
00098   return (0);
00099 }
00100 
00101 int ctim_SetValues(IMECore core, IMEArgList arglist, int option_value)
00102 {
00103   int i;
00104 
00105   log_f("ctim_SetValues === \n");
00106 
00107   if (arglist == NULL) return(-1);
00108 
00109   arglist->args_num = 0;
00110 
00111   if (option_value == -1) {
00112     int              ret;
00113     char             *file_name;
00114     CodeTableStruct ctHeader;
00115 
00116     /* read table from file to memory buffer  */
00117     file_name = core->envinfo.data_path;
00118     log_f("LoadCodeTableHeader: file_name:%s\n", file_name);
00119     ret = LoadCodeTableHeader(file_name, &ctHeader); 
00120     if (ret != -1) {
00121       i = KEYBYKEY_MODE_ID;
00122       arglist->args[i].value = ctHeader.nKeyByKeyMode;
00123       i = HELPINFO_MODE_ID;
00124       arglist->args[i].value = ctHeader.nHelpInfoMode;
00125       i = AUTOSELECT_MODE_ID;
00126       arglist->args[i].value = ctHeader.nAutoSelectMode;
00127       i = KEYPROMPT_MODE_ID;
00128       arglist->args[i].value = ctHeader.nKeyPromptMode;
00129     }
00130 
00131   } else {
00132     for (i = 0; i < CTIM_ARGS_NUM; i++) {
00133       arglist->args[i].value = (option_value >> i) & 1; 
00134     }
00135   }
00136 
00137   return(0);
00138 }
00139 
00140 /* return the name and encode status and status of codetable IME */
00141 int ctim_Init(IMECore core)
00142 {
00143   int                i, ret;
00144   char        *file_name;
00145   CodeTableStruct ctHeader;
00146 
00147   log_f("ctim_Init ====\n");
00148 
00149   /* read table from file to memory buffer  */
00150   file_name = core->envinfo.data_path;
00151   log_f("file name :%s\n",file_name);
00152 
00153   ret = LoadCodeTableHeader(file_name, &ctHeader); 
00154   if (ret == -1) 
00155     return (-1);
00156 
00157   /* Return Information that needed by Input Method Manager */
00158   /* Such as Encode, Cname, Status */
00159   core->baseinfo.status = ENGINE_NOT_INITIATED;
00160   core->baseinfo.lname = (char *)strdup(ctHeader.Lname);
00161   core->baseinfo.cname = (char *)strdup(ctHeader.Cname);
00162   core->baseinfo.encode_id = ctHeader.Encode;
00163        
00164   core->argsinfo.args_num = CTIM_ARGS_NUM;
00165   i = KEYBYKEY_MODE_ID;
00166   core->argsinfo.args[i].name = NULL;
00167   core->argsinfo.args[i].value = ctHeader.nKeyByKeyMode;
00168   i = HELPINFO_MODE_ID;
00169   core->argsinfo.args[i].name = NULL;
00170   core->argsinfo.args[i].value = ctHeader.nHelpInfoMode;
00171   i = AUTOSELECT_MODE_ID;
00172   core->argsinfo.args[i].name = NULL;
00173   core->argsinfo.args[i].value = ctHeader.nAutoSelectMode;
00174   i = KEYPROMPT_MODE_ID;
00175   core->argsinfo.args[i].name = NULL;
00176   core->argsinfo.args[i].value = ctHeader.nKeyPromptMode;
00177 
00178   return(0);
00179 }
00180 
00181 
00182 /* Load the codetable file int memory */
00183 int ctim_Open(IMECore core, IMEBuffer ime_buffer, void *handler)
00184 {
00185   char        *file_name;
00186   int         i, ret;
00187 
00188   CodeTableStruct *ctHeader;
00189 
00190   log_f("ctim_Open ==== \n");
00191 
00192   ime_buffer->encode = 0;
00193   ime_buffer->return_status = 0;
00194   ime_buffer->preedit_len = 0;
00195   ime_buffer->commit_len  = 0;
00196   ime_buffer->lookup_num  = 0;
00197   ime_buffer->inputkey_len  = 0;
00198   ime_buffer->cur_lookup_pos = 0;
00199 
00200   if (core->baseinfo.status == ENGINE_NOT_INSTALLED)
00201     return(-1);
00202 
00203   if (core->baseinfo.status == ENGINE_INITIATED)
00204     return 0;
00205 
00206   /* if IME engine is not initiated, would malloc dictionary buffer */
00207 
00208   core->baseinfo.status = ENGINE_NOT_INSTALLED;
00209 
00210   /* read table from file to memory buffer  */
00211   file_name = core->envinfo.data_path;
00212   log_f("file name :%s\n",file_name);
00213 
00214   ctHeader = (CodeTableStruct *)calloc(1, sizeof(CodeTableStruct));
00215   if (ctHeader == NULL) {
00216     fprintf(stderr, "no memory for CodeTable Input method: %s\n",core->baseinfo.ename);
00217     return(-1);
00218   }
00219 
00220   ret = LoadCodeTable(file_name, ctHeader); 
00221   if (ret == -1) {
00222     UnloadCodeTable(ctHeader);
00223     free((char *)ctHeader);
00224     return(-1);
00225   }
00226 
00227   core->baseinfo.status = ENGINE_INITIATED;
00228   core->envinfo.data_ptr = (char *)ctHeader;
00229 
00230   /* if exist keymap setting */
00231   if (GETBIT(ctHeader->bSectionsFlag, KEYPROMPT_SECTION)) {
00232     core->keymapinfo.bSet = 1;
00233     for (i=0; i<MAX_KEYMAP_KEY_NUM; i++) {
00234       core->keymapinfo.keymap[i] = (char *) strdup(ctHeader->keyprompt[i+0x20].prompt);
00235       /*
00236        log_f("keymap: %d\t%s\n", i, core->keymapinfo.keymap[i]);
00237       */
00238     }
00239   }
00240 
00241   return(0);
00242 }
00243 
00244 /* close IME, and free codetable pointers */
00245 void ctim_Close(IMECore core)
00246 {
00247   CodeTableStruct *ctHeader;
00248   int         i;
00249 
00250   log_f("ctim_Close ==== \n");
00251 
00252   ctHeader = (CodeTableStruct *)(core->envinfo.data_ptr);
00253   UnloadCodeTable(ctHeader);
00254   free((char *)ctHeader);
00255   if (core->baseinfo.lname)
00256     free((char *)core->baseinfo.lname);
00257   if (core->baseinfo.cname)
00258     free((char *)core->baseinfo.cname);
00259 
00260   if (core->envinfo.data_path)
00261     free((char *)core->envinfo.data_path);
00262 
00263   if (core->keymapinfo.bSet == 1) {
00264     for (i=0; i<MAX_KEYMAP_KEY_NUM; i++) {
00265       if (core->keymapinfo.keymap[i])
00266        free((char *)core->keymapinfo.keymap[i]);
00267     }
00268   }
00269 }
00270 
00271 /* process key input event */
00272 /* return value:  IME_NOT_USED_KEY:  if IME not use this key, return this key to systerm directly */
00273 /*                IME_USED_KEY:      if IME has used this key */
00274 int  ctim_Filter(core, key_event, ime_args, ime_buffer)
00275      IMECore core;
00276      IMEKey  key_event;
00277      IMEArgList ime_args;
00278      IMEBuffer ime_buffer;
00279 {
00280   int         ret;
00281   CodeTableStruct *ctHeader;
00282 
00283   log_f("ctim_Filter ==== \n");
00284 
00285   if (core->baseinfo.status != ENGINE_INITIATED)
00286     return(IME_NOT_USED_KEY);
00287               
00288   /* set ctHeader's argument setting */
00289        
00290   /* ctHeader's argument setting are always coveried by user defined data */
00291   ctHeader = (CodeTableStruct *)(core->envinfo.data_ptr);
00292   ctHeader->Output_Encode = core->envinfo.output_encode_id;
00293 
00294   /* if no user defined arguments, set with system arguments */
00295   ctHeader->nKeyByKeyMode = core->argsinfo.args[KEYBYKEY_MODE_ID].value;
00296   ctHeader->nHelpInfoMode = core->argsinfo.args[HELPINFO_MODE_ID].value;
00297   ctHeader->nAutoSelectMode = core->argsinfo.args[AUTOSELECT_MODE_ID].value;
00298   ctHeader->nKeyPromptMode = core->argsinfo.args[KEYPROMPT_MODE_ID].value;
00299 
00300   if (ime_args != NULL && ime_args->args_num > 0) {
00301     /* if has user defined arguments, set with user defined arguments */
00302     ctHeader->nKeyByKeyMode = ime_args->args[KEYBYKEY_MODE_ID].value;
00303     ctHeader->nHelpInfoMode = ime_args->args[HELPINFO_MODE_ID].value;
00304     ctHeader->nAutoSelectMode = ime_args->args[AUTOSELECT_MODE_ID].value;
00305     ctHeader->nKeyPromptMode = ime_args->args[KEYPROMPT_MODE_ID].value;
00306   }
00307               
00308   ret = codetable_filter(ctHeader, key_event, ime_buffer);
00309   log_f("codetable_filter : return: %d\n", ret);
00310   return(ret);
00311 }
00312