Back to index

im-sdk  12.3.91
le_proc.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 "le.h"
00047 #include "encode.h"
00048 #include "iml.h"
00049 #include "logf.h"
00050 #include "encode_draw.h"
00051 
00052 void ime_buffer_free(IMEBuffer ime_buffer);
00053 
00054 /* ============================================================== */
00055 /*                   IME buffer for every session                 */
00056 /* ============================================================== */
00057 IMEBufferRec *ime_buffer_malloc()
00058 {
00059     int i;
00060     IMEBuffer ime_buffer;
00061 
00062     ime_buffer = (IMEBufferRec *)calloc(1, sizeof(IMEBufferRec));
00063 
00064     ime_buffer->inputkey_buf = (int *)calloc(MAX_INPUT_KEY_NUM, sizeof(int));       
00065     ime_buffer->preedit_buf = (unsigned char *)calloc(MAX_PREEDIT_CHAR_NUM, sizeof(char));
00066     ime_buffer->prev_preedit_buf = (unsigned char *)calloc(MAX_PREEDIT_CHAR_NUM, sizeof(char));
00067     ime_buffer->status_buf = (unsigned char *)calloc(MAX_STATUS_CHAR_NUM, sizeof(char));
00068     ime_buffer->commit_buf = (unsigned char *)calloc(MAX_COMMIT_CHAR_NUM, sizeof(char));
00069 
00070     ime_buffer->lookup_buf = (unsigned char **)calloc(MAX_CANDIDATES_NUM, sizeof(unsigned char *));
00071     ime_buffer->candidates_buf = (unsigned char **)calloc(MAX_CANDIDATES_NUM, sizeof(unsigned char *));
00072     ime_buffer->additions_buf = (unsigned char **)calloc(MAX_CANDIDATES_NUM, sizeof(unsigned char *));
00073 
00074     if (      (ime_buffer == NULL) ||
00075               (ime_buffer->inputkey_buf == NULL) ||
00076               (ime_buffer->preedit_buf == NULL) ||
00077               (ime_buffer->prev_preedit_buf == NULL) ||
00078               (ime_buffer->status_buf == NULL) ||
00079               (ime_buffer->commit_buf == NULL) ||
00080               (ime_buffer->lookup_buf == NULL) ||
00081               (ime_buffer->candidates_buf == NULL) ||
00082               (ime_buffer->additions_buf == NULL) )
00083        goto Error;
00084 
00085     for (i =0; i<MAX_CANDIDATES_NUM; i++) {
00086        ime_buffer->lookup_buf[i] = (unsigned char *)calloc(MAX_CANDIDATE_CHAR_NUM, sizeof(char));
00087        ime_buffer->candidates_buf[i] = (unsigned char *)calloc(MAX_CANDIDATE_CHAR_NUM, sizeof(char));
00088        ime_buffer->additions_buf[i] = (unsigned char *)calloc(MAX_CANDIDATE_CHAR_NUM, sizeof(char));
00089 
00090        if (   (ime_buffer->lookup_buf[i] == NULL) ||
00091               (ime_buffer->candidates_buf[i] == NULL) ||
00092               (ime_buffer->additions_buf[i] == NULL) )
00093            goto Error;
00094     }
00095 
00096     return(ime_buffer);
00097 
00098 Error:
00099     ime_buffer_free(ime_buffer);
00100     return(NULL);
00101 }
00102 
00103 void ime_buffer_free(IMEBuffer ime_buffer)
00104 {
00105     int i;
00106 
00107     if (ime_buffer->inputkey_buf) free(ime_buffer->inputkey_buf);
00108     if (ime_buffer->preedit_buf)  free(ime_buffer->preedit_buf);
00109     if (ime_buffer->prev_preedit_buf)  free(ime_buffer->prev_preedit_buf);
00110     if (ime_buffer->status_buf)   free(ime_buffer->status_buf);
00111     if (ime_buffer->commit_buf)   free(ime_buffer->commit_buf);
00112 
00113     for (i =0; i<MAX_CANDIDATES_NUM; i++) {
00114        if (ime_buffer->lookup_buf[i])
00115            free(ime_buffer->lookup_buf[i]);
00116        if (ime_buffer->candidates_buf[i])
00117            free(ime_buffer->candidates_buf[i]);
00118        if (ime_buffer->additions_buf[i])
00119            free(ime_buffer->additions_buf[i]);
00120     }
00121        
00122     if (ime_buffer->lookup_buf)
00123        free(ime_buffer->lookup_buf);
00124     if (ime_buffer->candidates_buf)
00125        free(ime_buffer->candidates_buf);
00126     if (ime_buffer->additions_buf)
00127        free(ime_buffer->additions_buf);
00128 
00129     free(ime_buffer);
00130 }
00131 
00132 /* ============================================================== */
00133 /*           Change Language Engine Status Information            */
00134 /* ============================================================== */
00135 /* clear input method buffer */
00136 void le_clear_ime_buffer(iml_session_t *s, IMEBuffer ime_buffer)
00137 {
00138     ime_buffer->lookup_num = 0;
00139     iml_lookup_enddraw(s);
00140 
00141     ime_buffer->inputkey_len = 0;
00142     ime_buffer->preedit_len = 0;
00143     ime_buffer->hotkey_flag = IME_HOTKEY_NOT_IN_USE;
00144     ime_buffer->conv_flag = -1;
00145     iml_preedit_enddraw(s);
00146 }
00147 
00148 void le_commit_euro(iml_session_t *s, IMEBuffer ime_buffer)
00149 {
00150     int encode = ime_buffer->encode;
00151     int status = ime_buffer->return_status;
00152     int commit_len = ime_buffer->commit_len;
00153     unsigned char *commit_buf  = ime_buffer->commit_buf;
00154 
00155     if ((status & IME_COMMIT) && (commit_len>0)) {
00156        commit_buf[commit_len] = 0;
00157        log_f("commit_buf:%s\n", commit_buf);
00158        encode_commit(s, encode, commit_buf, commit_len);
00159     }
00160 }
00161 
00162 /* output input method buffer to system */
00163 void le_output_ime_buffer(iml_session_t *s, IMEBuffer ime_buffer)
00164 {
00165 
00166     int encode = ime_buffer->encode;
00167     int status = ime_buffer->return_status;
00168     int preedit_len = ime_buffer->preedit_len;
00169     int commit_len = ime_buffer->commit_len;
00170     int lookup_num = ime_buffer->lookup_num;
00171     int lookup_label_type = ime_buffer->lookup_label_type;
00172     int preedit_caretpos = ime_buffer->preedit_caretpos;
00173     unsigned char *preedit_buf = ime_buffer->preedit_buf;
00174     unsigned char *commit_buf  = ime_buffer->commit_buf;
00175     unsigned char *status_buf  = ime_buffer->status_buf;
00176     unsigned char **lookup_buf = ime_buffer->lookup_buf;
00177 
00178     if (status & IME_STATUS_AREA)
00179        encode_draw_status(s, encode, status_buf);
00180 
00181     if (status & IME_PREEDIT_AREA) {
00182        preedit_buf[preedit_len] = 0;
00183        if (preedit_len <= 0)
00184            iml_preedit_enddraw(s);
00185        else
00186            encode_draw_preedit(s, encode, preedit_buf, preedit_caretpos);
00187     }
00188 
00189 
00190     if (status & IME_LOOKUP_AREA) {
00191        log_f("lookup_num:%d\n", lookup_num);
00192        if (lookup_num <= 0)
00193            iml_lookup_enddraw(s);
00194        else
00195            encode_draw_candidates(s, encode, lookup_buf, lookup_num, lookup_label_type);
00196     }
00197 
00198     if ((status & IME_COMMIT) && (commit_len>0)) {
00199        commit_buf[commit_len] = 0;
00200        log_f("commit_buf:%s\n", commit_buf);
00201        encode_commit(s, encode, commit_buf, commit_len);
00202     }
00203 }
00204 
00205 void le_status_draw(unit_desktop_t *udp, iml_session_t *s)
00206 {
00207     unsigned char status_str[256];
00208     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00209     int engine_id = session_data->paletteaux_status.engine_id;
00210     int on = session_data->paletteaux_status.on;
00211 
00212     memset((char *)&status_str, '\0', sizeof(status_str));
00213 
00214     if (!on || engine_id == 0xff){
00215        log_f("encode_draw_status: ENGLISH \n");
00216        encode_draw_status(s, ENCODE_UTF8, "");
00217     } else {
00218        if (!strcmp(udp->modeList[engine_id]->nlocale_name ,(char *)"ASCII"))
00219            sprintf((char *)status_str, "%s", udp->modeList[engine_id]->nlayout_name);
00220        else if (!strcmp(udp->modeList[engine_id]->nlayout_name, (char *)"ASCII"))
00221            sprintf((char *)status_str, "%s", udp->modeList[engine_id]->nlocale_name);
00222        else
00223            sprintf((char *)status_str, "%s", udp->modeList[engine_id]->nlayout_name);
00224        log_f("encode_draw_status: [%s] \n",status_str);
00225        encode_draw_status(s, udp->gEngine_Info[engine_id]->core.baseinfo.encode_id, status_str);
00226     }
00227 }
00228 
00229 void le_switch_engine(unit_desktop_t *udp, iml_session_t *s, int engine_id, Bool flag)
00230 {
00231     int ret;
00232     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00233 
00234     if (engine_id >= udp->gEngine_Num)  
00235        engine_id = 0xff;
00236 
00237     if (engine_id != 0xff && udp->gEngine_Info[engine_id]->core.baseinfo.status == ENGINE_NOT_INSTALLED)
00238        engine_id = 0xff;
00239 
00240     if (engine_id != 0xff) {
00241        ret = udp->gEngine_Info[engine_id]->so_methods->IME_Open(
00242            &udp->gEngine_Info[engine_id]->core,
00243            session_data->ime_buffer,
00244            udp->gEngine_Info[engine_id]->so_handler);
00245               
00246        log_f("IME_Open return :%d\n", ret);
00247        if (ret == -1)
00248            engine_id = 0xff;
00249     }
00250 
00251     session_data->paletteaux_status.engine_id = engine_id;
00252     if (flag)
00253       le_status_draw(udp, s);
00254 }
00255 
00256 
00257 /* Local Variables: */
00258 /* c-file-style: "iiim-project" */
00259 /* End: */