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 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 
00046 #include "unit_input.h"
00047 #include "codetable.h"
00048 #include "ctim.h"
00049 #include "logf.h"
00050 
00051 int  ctim_SetOptions(IMECore, char *);
00052 int  ctim_SetValues(IMECore, IMEArgList, int);
00053 int  ctim_Init(IMECore);
00054 int  ctim_Open(IMECore, IMEBuffer, void *);
00055 int  ctim_Filter(IMECore, IMEKey, IMEArgList, IMEBuffer);
00056 void ctim_Close(IMECore);
00057 
00058 extern int codetable_filter(CodeTableStruct *hztbl, IMEKey key_event, IMEBufferRec *ime_buffer);
00059 
00060 #ifdef WIN32
00061 #define EXPORT extern __declspec(dllexport)
00062 EXPORT
00063 #endif
00064 
00065 /* IF Method */
00066 IMEMethodsRec ime_methods = {
00067   ctim_SetOptions,
00068   ctim_SetValues,
00069   ctim_Init,
00070   ctim_Open,
00071   ctim_Filter,
00072   ctim_Close
00073 };
00074 
00075 #define SUFFIX_DICT_FILE    ".data"
00076 
00077 /* Set options for codetable input method. */
00078 int ctim_SetOptions(IMECore core, char *options)
00079 {
00080   char        data_path[256];
00081 
00082   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);
00083   core->envinfo.data_path = (char *)strdup(data_path);
00084 
00085   log_f("data_path :%s\n", core->envinfo.data_path);
00086   return (0);
00087 }
00088 
00089 int ctim_SetValues(IMECore core, IMEArgList arglist, int option_value)
00090 {
00091   int i;
00092 
00093   log_f("ctim_SetValues === \n");
00094 
00095   if (arglist == NULL) return(-1);
00096 
00097   arglist->args_num = 0;
00098 
00099   if (option_value == -1) {
00100     int              ret;
00101     char             *file_name;
00102     CodeTableStruct ctHeader;
00103 
00104     /* read table from file to memory buffer  */
00105     file_name = core->envinfo.data_path;
00106     log_f("LoadCodeTableHeader: file_name:%s\n", file_name);
00107     ret = LoadCodeTableHeader(file_name, &ctHeader); 
00108     if (ret != -1) {
00109       i = KEYBYKEY_MODE_ID;
00110       arglist->args[i].value = ctHeader.nKeyByKeyMode;
00111       i = HELPINFO_MODE_ID;
00112       arglist->args[i].value = ctHeader.nHelpInfoMode;
00113       i = AUTOSELECT_MODE_ID;
00114       arglist->args[i].value = ctHeader.nAutoSelectMode;
00115       i = KEYPROMPT_MODE_ID;
00116       arglist->args[i].value = ctHeader.nKeyPromptMode;
00117     }
00118 
00119   } else {
00120     for (i = 0; i < CTIM_ARGS_NUM; i++) {
00121       arglist->args[i].value = (option_value >> i) & 1; 
00122     }
00123   }
00124 
00125   return(0);
00126 }
00127 
00128 /* return the name and encode status and status of codetable IME */
00129 int ctim_Init(IMECore core)
00130 {
00131   int                i, ret;
00132   char        *file_name;
00133   CodeTableStruct ctHeader;
00134 
00135   log_f("ctim_Init ====\n");
00136 
00137   /* read table from file to memory buffer  */
00138   file_name = core->envinfo.data_path;
00139   log_f("file name :%s\n",file_name);
00140 
00141   ret = LoadCodeTableHeader(file_name, &ctHeader); 
00142   if (ret == -1) 
00143     return (-1);
00144 
00145   /* Return Information that needed by Input Method Manager */
00146   /* Such as Encode, Cname, Status */
00147   core->baseinfo.status = ENGINE_NOT_INITIATED;
00148   core->baseinfo.lname = (char *)strdup(ctHeader.Lname);
00149   core->baseinfo.cname = (char *)strdup(ctHeader.Cname);
00150   core->baseinfo.encode_id = ctHeader.Encode;
00151        
00152   core->argsinfo.args_num = CTIM_ARGS_NUM;
00153   i = KEYBYKEY_MODE_ID;
00154   core->argsinfo.args[i].name = NULL;
00155   core->argsinfo.args[i].value = ctHeader.nKeyByKeyMode;
00156   i = HELPINFO_MODE_ID;
00157   core->argsinfo.args[i].name = NULL;
00158   core->argsinfo.args[i].value = ctHeader.nHelpInfoMode;
00159   i = AUTOSELECT_MODE_ID;
00160   core->argsinfo.args[i].name = NULL;
00161   core->argsinfo.args[i].value = ctHeader.nAutoSelectMode;
00162   i = KEYPROMPT_MODE_ID;
00163   core->argsinfo.args[i].name = NULL;
00164   core->argsinfo.args[i].value = ctHeader.nKeyPromptMode;
00165 
00166   return(0);
00167 }
00168 
00169 
00170 /* Load the codetable file int memory */
00171 int ctim_Open(IMECore core, IMEBuffer ime_buffer, void *handler)
00172 {
00173   char        *file_name;
00174   int         i, ret;
00175 
00176   CodeTableStruct *ctHeader;
00177 
00178   log_f("ctim_Open ==== \n");
00179 
00180   ime_buffer->encode = 0;
00181   ime_buffer->return_status = 0;
00182   /* 
00183      ime_buffer->preedit_len = 0;
00184      ime_buffer->inputkey_len  = 0;
00185   */
00186   ime_buffer->commit_len  = 0;
00187   ime_buffer->lookup_num  = 0;
00188   ime_buffer->cur_lookup_pos = 0;
00189 
00190   if (core->baseinfo.status == ENGINE_NOT_INSTALLED)
00191     return(-1);
00192 
00193   if (core->baseinfo.status == ENGINE_INITIATED)
00194     return 0;
00195 
00196   /* if IME engine is not initiated, would malloc dictionary buffer */
00197 
00198   core->baseinfo.status = ENGINE_NOT_INSTALLED;
00199 
00200   /* read table from file to memory buffer  */
00201   file_name = core->envinfo.data_path;
00202   log_f("file name :%s\n",file_name);
00203 
00204   ctHeader = (CodeTableStruct *)calloc(1, sizeof(CodeTableStruct));
00205   if (ctHeader == NULL) {
00206     fprintf(stderr, "no memory for CodeTable Input method: %s\n",core->baseinfo.ename);
00207     return(-1);
00208   }
00209 
00210   ret = LoadCodeTable(file_name, ctHeader); 
00211   if (ret == -1) {
00212     UnloadCodeTable(ctHeader);
00213     free((char *)ctHeader);
00214     return(-1);
00215   }
00216 
00217   core->baseinfo.status = ENGINE_INITIATED;
00218   core->envinfo.data_ptr = (char *)ctHeader;
00219 
00220   /* if exist keymap setting */
00221   if (GETBIT(ctHeader->bSectionsFlag, KEYPROMPT_SECTION)) {
00222     core->keymapinfo.bSet = 1;
00223     for (i=0; i<MAX_KEYMAP_KEY_NUM; i++) {
00224       core->keymapinfo.keymap[i] = (char *) strdup(ctHeader->keyprompt[i+0x20].prompt);
00225       /*
00226        log_f("keymap: %d\t%s\n", i, core->keymapinfo.keymap[i]);
00227       */
00228     }
00229   }
00230 
00231   return(0);
00232 }
00233 
00234 /* close IME, and free codetable pointers */
00235 void ctim_Close(IMECore core)
00236 {
00237   CodeTableStruct *ctHeader;
00238   int         i;
00239 
00240   log_f("ctim_Close ==== \n");
00241 
00242   ctHeader = (CodeTableStruct *)(core->envinfo.data_ptr);
00243   UnloadCodeTable(ctHeader);
00244   free((char *)ctHeader);
00245   if (core->baseinfo.lname)
00246     free((char *)core->baseinfo.lname);
00247   if (core->baseinfo.cname)
00248     free((char *)core->baseinfo.cname);
00249 
00250   if (core->envinfo.data_path)
00251     free((char *)core->envinfo.data_path);
00252 
00253   if (core->keymapinfo.bSet == 1) {
00254     for (i=0; i<MAX_KEYMAP_KEY_NUM; i++) {
00255       if (core->keymapinfo.keymap[i])
00256        free((char *)core->keymapinfo.keymap[i]);
00257     }
00258   }
00259 }
00260 
00261 /* process key input event */
00262 /* return value:  IME_NOT_USED_KEY:  if IME not use this key, return this key to systerm directly */
00263 /*                IME_USED_KEY:      if IME has used this key */
00264 int  ctim_Filter(core, key_event, ime_args, ime_buffer)
00265   IMECore core;
00266 IMEKey  key_event;
00267 IMEArgList ime_args;
00268 IMEBuffer ime_buffer;
00269 {
00270   int         ret;
00271   CodeTableStruct *ctHeader;
00272 
00273   log_f("ctim_Filter ==== \n");
00274 
00275   if (core->baseinfo.status != ENGINE_INITIATED)
00276     return(IME_NOT_USED_KEY);
00277               
00278   /* set ctHeader's argument setting */
00279        
00280   /* ctHeader's argument setting are always coveried by user defined data */
00281   ctHeader = (CodeTableStruct *)(core->envinfo.data_ptr);
00282   ctHeader->Output_Encode = core->envinfo.output_encode_id;
00283 
00284   /* if no user defined arguments, set with system arguments */
00285   ctHeader->nKeyByKeyMode = core->argsinfo.args[KEYBYKEY_MODE_ID].value;
00286   ctHeader->nHelpInfoMode = core->argsinfo.args[HELPINFO_MODE_ID].value;
00287   ctHeader->nAutoSelectMode = core->argsinfo.args[AUTOSELECT_MODE_ID].value;
00288   ctHeader->nKeyPromptMode = core->argsinfo.args[KEYPROMPT_MODE_ID].value;
00289 
00290   if (ime_args != NULL && ime_args->args_num > 0) {
00291     /* if has user defined arguments, set with user defined arguments */
00292     ctHeader->nKeyByKeyMode = ime_args->args[KEYBYKEY_MODE_ID].value;
00293     ctHeader->nHelpInfoMode = ime_args->args[HELPINFO_MODE_ID].value;
00294     ctHeader->nAutoSelectMode = ime_args->args[AUTOSELECT_MODE_ID].value;
00295     ctHeader->nKeyPromptMode = ime_args->args[KEYPROMPT_MODE_ID].value;
00296   }
00297               
00298   if (!strncmp(core->baseinfo.cname,(char *)"Latin", strlen(core->baseinfo.cname))) {
00299     if ((key_event->keyCode == IM_VK_F9) && (!key_event->keyChar) && (!key_event->modifier)) {
00300       log_f("HOTKEY for COMPOSE_KEY is pressed \n");
00301       key_event->keyCode = IM_VK_T;
00302       key_event->keyChar = IM_VK_T;
00303       key_event->modifier = IM_CTRL_MASK|IM_SHIFT_MASK ;
00304     }
00305   }
00306   ret = codetable_filter(ctHeader, key_event, ime_buffer);
00307   log_f("codetable_filter : return: %d\n", ret);
00308 
00309   /*  TODO!! Disabling Lookup window Should be done using configuration file */
00310   if (((!strncmp(core->baseinfo.cname,(char *)"Latin", strlen(core->baseinfo.cname))) && (ime_buffer->return_status & IME_LOOKUP_AREA)) || ((!strncmp(core->envinfo.locale_name,(char *)"am_ET", strlen(core->envinfo.locale_name))) && (ime_buffer->return_status & IME_LOOKUP_AREA)) || ((!strncmp(core->envinfo.locale_name,(char *)"ti_ER", strlen(core->envinfo.locale_name))) && (ime_buffer->return_status & IME_LOOKUP_AREA))) {
00311     ime_buffer->return_status &= ~IME_LOOKUP_AREA;
00312   }
00313 
00314   return(ret);
00315 }
00316