Back to index

im-sdk  12.3.91
NewPY.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 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 WIN32
00044 #include <windows.h>
00045 #endif
00046 
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050 #include <sys/types.h>
00051 
00052 #include "SunIM.h"
00053 #include "SunIMMMan.h"
00054 #include "SunIMProt.h"
00055 #include "SunIMMthd.h"
00056 #include "SunIMPriv.h"
00057 #include "SunIMCore.h"
00058 #include "SunIMPub.h"
00059 #include "NewPY.h"
00060 
00061 Bool if_newpy_OpenIF(iml_if_t *);
00062 Bool if_newpy_CloseIF(iml_if_t *);
00063 Bool if_newpy_GetIFValue(iml_if_t *, IMArgList, int);
00064 Bool if_newpy_SetIFValue(iml_if_t *, IMArgList, int);
00065 Bool if_newpy_OpenDesktop(iml_desktop_t *, IMArgList, int);
00066 Bool if_newpy_CloseDesktop(iml_desktop_t *);
00067 Bool if_newpy_CreateSC(iml_session_t *, IMArgList, int);
00068 Bool if_newpy_DestroySC(iml_session_t *);
00069 Bool if_newpy_GetSCValue(iml_session_t *, IMArgList, int);
00070 Bool if_newpy_SetSCValue(iml_session_t *, IMArgList, int);
00071 IMText *if_newpy_ResetSC(iml_session_t *);
00072 void if_newpy_SetSCFocus(iml_session_t *);
00073 void if_newpy_UnsetSCFocus(iml_session_t *);
00074 void if_newpy_SendEvent(iml_session_t *, IMInputEvent * ev);
00075 
00076 /* IF Method */
00077 if_methods_t newpy_methods2 = {
00078     if_newpy_OpenIF,
00079     if_newpy_CloseIF,
00080     if_newpy_GetIFValue,
00081     if_newpy_SetIFValue,
00082     if_newpy_OpenDesktop,
00083     if_newpy_CloseDesktop,
00084     if_newpy_CreateSC,
00085     if_newpy_DestroySC,
00086     if_newpy_GetSCValue,
00087     if_newpy_SetSCValue,
00088     if_newpy_ResetSC,
00089     if_newpy_SetSCFocus,
00090     if_newpy_UnsetSCFocus,
00091     if_newpy_SendEvent
00092 };
00093 
00094 UTFCHAR lename_string[] = {0x7b80, 0x4f53, 0x4e2d, 0x872d, 0x65b0, 0x62fc, 0x97f3, 0x8f93, 0x8815, 0x6cd5, 0x0}; 
00095 UTFCHAR zhhrn_string[] = {0x7b80, 0x4f53, 0x4e2d, 0x872d, 0x0};
00096 
00097 UTFCHAR on_string[16][6] = { /* status string */
00098        { 0x6570,0x5b66,0x7b26,0x53f7,0 },
00099         { 0x6570,0x5b57,0x0031,0 },
00100         { 0x6570,0x5b57,0x0032,0 },
00101         { 0x6570,0x5b57,0x0033,0 },
00102         { 0x7f57,0x9a6c,0x6570,0x5b57,0 },
00103         { 0x5e0c,0x814a,0x5b57,0x6bcd,0 },
00104         { 0x4fc4,0x6587,0x5b57,0x6bcd,0 },
00105         { 0x62fc,0x97f3,0x5b57,0x6bcd,0 },
00106         { 0x5e73,0x5047,0x540d,0 },
00107         { 0x7247,0x5047,0x540d,0 },
00108         { 0x5236,0x8868,0x7b26,0x0031,0 },
00109         { 0x5236,0x8868,0x7b26,0x0032,0 },
00110         { 0x5176,0x4ed6,0x7b26,0x53f7,0 },
00111         { 0x65b0,0x5168,0x62fc,0 },
00112         { 0x65b0,0x53cc,0x62fc,0 },
00113         { 0x5168,0x89d2,0x897f,0x6587,0 }
00114        };
00115 
00116 static IMLEName lename = {
00117     "newpy", lename_string  /* LE id, HRN */
00118 };
00119 
00120 static IMLocale locales[] = {
00121     {"zh_CN", zhhrn_string},       
00122     NULL
00123 };
00124 
00125 IMObjectDescriptorStruct *objects = NULL;
00126 
00127 void init_objects();
00128 void free_objects();
00129 
00130 #ifdef WIN32
00131 #define EXPORT extern __declspec(dllexport)
00132 
00133 EXPORT
00134 
00135 #endif
00136 
00137 void 
00138 if_GetIfInfo(
00139     IMArgList args,
00140     int num_args
00141 )
00142 {
00143     int i;
00144 
00145     init_objects(); 
00146     for (i = 0; i < num_args; i++, args++) {
00147         switch (args->id) {
00148             case IF_VERSION:
00149                 args->value = (IMArgVal) "1.2";
00150                 break;
00151             case IF_METHOD_TABLE:
00152                 args->value = (IMArgVal) & newpy_methods2;
00153                 break;
00154             case IF_LE_NAME:
00155                 args->value = (IMArgVal) & lename;
00156                 break;
00157             case IF_SUPPORTED_LOCALES:
00158                 args->value = (IMArgVal) & locales;
00159                 break;
00160             case IF_SUPPORTED_OBJECTS:
00161                 args->value = (IMArgVal) objects; 
00162                 break;
00163             case IF_NEED_THREAD_LOCK:
00164                 args->value = (IMArgVal) False;
00165                 break;
00166             default:
00167                 break;
00168             }
00169     }
00170 }
00171 
00172 char *class_names[] = {"com.sun.iiim.sample.newpy.NewPYPanel"};
00173 
00174 void aux_start(iml_session_t *);
00175 void aux_draw(iml_session_t *, int, int *, int, UTFCHAR **);
00176 void aux_done(iml_session_t *);
00177 void receive_aux(iml_session_t *, IMAuxDrawCallbackStruct *);
00178 Bool receive_keylist(iml_session_t *, IMKeyListEvent *);
00179 
00180 void my_conversion_on(iml_session_t *);
00181 void my_conversion_off(iml_session_t *);
00182 
00183 void status_draw(iml_session_t *);
00184 void preedit_draw(iml_session_t *);
00185 void lookup_draw(iml_session_t *, UTFCHAR **, int);
00186 void commit(iml_session_t *);
00187 void eval_packet(iml_session_t *, im_data *);
00188 
00189 IMText *make_preedit_imtext(iml_session_t *);
00190 void preedit_buf_print(iml_session_t *);
00191 IMText *make_imtext(iml_session_t *, UTFCHAR *);
00192 
00193 int UTFCHARLen(UTFCHAR *);
00194 int UTFCHARCpy(UTFCHAR *, UTFCHAR *);
00195 
00196 IMFeedbackList *create_feedback(iml_session_t * s, int size);
00197 IMFeedbackList *create_feedback2(iml_session_t * s, int size);
00198 void set_feedback(IMFeedbackList *, int);
00199 int get_feedback(IMFeedbackList *);
00200 
00201 /* data per desktop */
00202 typedef struct {
00203     iml_session_t *auxproxy_session;
00204     int aux_started;
00205     int punc;
00206     int skb;
00207     int gbk_support;
00208 }   MyDataPerDesktop;
00209 
00210 /* data per session */
00211 typedef struct {
00212     int conv_on;
00213     
00214     IMText **luc_candidates;
00215     IMText **luc_labels;
00216     
00217     UTFCHAR *conversion_string;    /* reversed */
00218     UTFCHAR *preedit_string;       /* underlined */
00219     
00220     int luc_top;
00221     int luc_nchoices;
00222     int luc_current_candidate;     /* index of current candidate */
00223     int luc_type;           /* Clinet or IM Master */
00224     int max_candidates;
00225     
00226     UTFCHAR *preedit_buf;   /* preedit */
00227     IMFeedbackList *preedit_feedback;     /* feedback for preedit */
00228     int caret_pos;
00229     
00230     IMFeedbackList *luc_fbs_reverse;
00231     IMFeedbackList *luc_fbs_normal;
00232 
00233     UTFCHAR *commit_buf; /* commit */
00234 
00235     int session_id;
00236     char *username;
00237 
00238 }   MyDataPerSession;
00239 
00240 #define MAX_SESSION 512
00241 #define MAX_PHRASE_LEN 20
00242 static int s_id[MAX_SESSION];
00243 
00244 #define       DEBUG
00245 
00246 void debugprint(iml_session_t *s) {
00247     printf("  s=[%x]\n", s);
00248     printf("  If=[%x]\n", s->desktop->If);
00249     printf("  desktop=[%x]\n", s->desktop);
00250     printf("  locale=[%s]\n", s->desktop->If->locale);
00251     printf("  if_name=[%s]\n", s->desktop->If->if_name);
00252     printf("  USER:%s\n", s->desktop->user_name);
00253     printf("  HOST:%s\n", s->desktop->host_name);
00254     printf("    DISPLAY:%s\n", s->desktop->display_id);
00255 }
00256 
00257 Bool
00258 if_newpy_OpenIF(
00259     iml_if_t * If
00260 )
00261 {
00262 #ifdef DEBUG
00263     printf("if_newpy_OpenIF()\n");
00264     printf("  If=[%x]\n", If);
00265     printf("  locale=[%s]\n", If->locale);
00266     printf("  if_name=[%s]\n", If->if_name);
00267 #endif
00268     init_objects();
00269     return True;
00270 }
00271 
00272 Bool
00273 if_newpy_CloseIF(
00274     iml_if_t * If
00275 )
00276 {
00277 #ifdef DEBUG
00278     printf("if_newpy_CloseIF()\n");
00279     printf("  If=[%x]\n", If);
00280     printf("  locale=[%s]\n", If->locale);
00281     printf("  if_name=[%s]\n", If->if_name);
00282 #endif
00283 
00284 #ifdef  notdef
00285     free_objects();
00286 #endif
00287 
00288     return True;
00289 }
00290 
00291 UTFCHAR aux_name_panel[] = {0x65b0, 0x62fc, 0x97f3, 0x8f85, 0x52a9, 0x7a97, 0x53e3, 0};
00292 UTFCHAR aux_name_aux[] = {0x65b0, 0x62fc, 0x97f3, 0x8f85, 0x52a9, 0x7a97, 0x53e3, 0};
00293 
00294 void
00295 free_objects()
00296 {
00297     IMObjectDescriptorStruct *l = objects;
00298 
00299     while (l->leid) {
00300         free(l->name);
00301         l++;
00302     };
00303     free(objects);
00304     objects = NULL;
00305 }
00306 
00307 void
00308 init_objects()
00309 {
00310     IMObjectDescriptorStruct *l;
00311     objects = (IMObjectDescriptorStruct *) calloc(3, sizeof(IMObjectDescriptorStruct));
00312     l = objects;
00313 
00314     l->leid = "newpy";       /* engine id */
00315     l->type = IM_DOWNLOADINGOBJECT_JARGUI_TYPE; /* object type */
00316     l->name = aux_name_panel;
00317     l->name_length = 7;
00318     l->domain = "com.sun";
00319     l->scope = "newpy";      
00320 
00321 #ifdef  WIN32
00322     l->path = "d:\\newpy_obj.jar";  
00323 #else
00324     l->path = "./newpy/newpy_obj.jar";  /* path */
00325 #endif
00326 
00327     l->signature = "";
00328     l->class_names = class_names;
00329     l->count_names = 1;
00330 
00331     l++;
00332     l->leid = "newpy";                       /* engine id */
00333     l->type = IM_DOWNLOADINGOBJECT_BINGUI_TYPE; /* object type */
00334     l->name = aux_name_panel;                   /* HRN */
00335     l->name_length = 7;
00336     l->domain = "com.sun";
00337     l->scope = "newpy"; 
00338     l->path = "./newpy/aux.so";    /* path for .so from /usr/lib/iiim/le/ */
00339                                    /* this will be
00340                                        /usr/lib/iiim/le/newpy/aux.so
00341                                    */
00342     l->signature = "";
00343     l->basepath = NULL;         /* only for CCDEF */
00344     l->encoding = NULL;         /* only for CCDEF */
00345 }
00346 
00347 Bool
00348 if_newpy_GetIFValue(
00349     iml_if_t * If,
00350     IMArgList args,
00351     int num_args
00352 )
00353 {
00354 
00355 #ifdef DEBUG
00356     printf("if_newpy_GetIFValue()\n");
00357     printf("  If=[%x]\n", If);
00358     printf("  locale=[%s]\n", If->locale);
00359     printf("  if_name=[%s]\n", If->if_name);
00360 #endif
00361 
00362     return True;
00363 }
00364 
00365 Bool
00366 if_newpy_SetIFValue(
00367     iml_if_t * If,
00368     IMArgList args,
00369     int num_args
00370 )
00371 {
00372     int i;
00373 #ifdef DEBUG
00374     printf("if_newpy_SetIFValue()\n");
00375     printf("  If=[%x]\n", If);
00376     printf("  locale=[%s]\n", If->locale);
00377     printf("  if_name=[%s]\n", If->if_name);
00378 #endif
00379     for (i = 0; i < num_args; i++, args++) {
00380         switch (args->id) {
00381         }
00382     }
00383     return True;
00384 }
00385 
00386 Bool
00387 if_newpy_OpenDesktop(
00388     iml_desktop_t * desktop,
00389     IMArgList args,
00390     int num_args
00391 )
00392 {
00393     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) calloc(1, sizeof(MyDataPerDesktop));
00394     int i;
00395 
00396 #ifdef DEBUG
00397     printf("if_newpy_OpenDesktop()\n");
00398     printf("  If=[%x]\n", desktop->If);
00399     printf("  desktop=[%x]\n", desktop);
00400     printf("  locale=[%s]\n", desktop->If->locale);
00401     printf("  if_name=[%s]\n", desktop->If->if_name);
00402     printf("    ifpath_name = [%s]\n", desktop->If->ifpath_name);
00403     printf("  USER:%s\n", desktop->user_name);
00404     printf("  HOST:%s\n", desktop->host_name);
00405     printf("  DISPLAY:%s\n", desktop->display_id);
00406     for (i = 0; i < num_args; i++, args++) {
00407         switch (args->id) {
00408             case UI_USER_NAME:
00409                 if (args->value) {
00410                     printf("    UI_USER_NAME=%s\n", args->value);
00411                 }
00412                 break;
00413             case UI_HOST_NAME:
00414                 if (args->value) {
00415                     printf("    UI_HOST_NAME=%s\n", args->value);
00416                 }
00417                 break;
00418             case UI_DISPLAY_ID:
00419                 if (args->value) {
00420                     printf("    UI_DISPLAY_ID=%s\n", args->value);
00421                 }
00422                 break;
00423             case UI_PROTOCOL_TYPE:
00424                 if (args->value) {
00425                     printf("    UI_PROTOCOL_TYPE=%s\n", args->value);
00426                 }
00427                 break;
00428             case UI_CLIENT_TYPE:
00429                 if (args->value) {
00430                     printf("    UI_CLIENT_TYPE=%s\n", args->value);
00431                 }
00432                 break;
00433             case UI_XSERVER_VENDOR:
00434                 if (args->value) {
00435                     printf("    UI_XSERVER_VENDOR=%s\n", args->value);
00436                 }
00437                 break;
00438             case UI_OS_NAME:
00439                 if (args->value) {
00440                     printf("    UI_OS_NAME=%s\n", args->value);
00441                 }
00442                 break;
00443             case UI_OS_ARCH:
00444                 if (args->value) {
00445                     printf("    UI_OS_ARCH=%s\n", args->value);
00446                 }
00447                 break;
00448             case UI_OS_VERSION:
00449                 if (args->value) {
00450                     printf("    UI_OS_VERSION=%s\n", args->value);
00451                 }
00452                 break;
00453             }
00454        }
00455 #endif
00456 
00457     desktop_data->aux_started = False;
00458     desktop_data->punc = 0;
00459     desktop_data->skb = 13;
00460     desktop_data->gbk_support = 0;
00461     desktop_data->auxproxy_session = NULL;
00462     desktop->specific_data = (void *) desktop_data;
00463     return True;
00464 }
00465 
00466 Bool
00467 if_newpy_CloseDesktop(
00468     iml_desktop_t * desktop
00469 )
00470 {
00471     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) desktop->specific_data;
00472 
00473 #ifdef DEBUG
00474     printf("if_newpy_CloseDesktop()\n");
00475     printf("  If=[%x]\n", desktop->If);
00476     printf("  desktop=[%x]\n", desktop);
00477     printf("  locale=[%s]\n", desktop->If->locale);
00478     printf("  if_name=[%s]\n", desktop->If->if_name);
00479     printf("  USER:%s\n", desktop->user_name);
00480     printf("  HOST:%s\n", desktop->host_name);
00481     printf("  DISPLAY:%s\n", desktop->display_id);
00482 #endif
00483 
00484     free(desktop_data);
00485     return True;
00486 }
00487 
00488 #define       BUFSIZE       256
00489 #define       MAXCANDIDATES 40
00490 
00491 Bool
00492 if_newpy_CreateSC(
00493     iml_session_t *s,
00494     IMArgList args,
00495     int num_args
00496 )
00497 {
00498     static int first_create = 1;
00499     iml_desktop_t *desktop = s->desktop;
00500     int i;
00501 
00502     MyDataPerSession *p = (MyDataPerSession *) calloc(1, sizeof(MyDataPerSession));
00503     p->preedit_buf = (UTFCHAR *) calloc(1, sizeof(UTFCHAR) * BUFSIZE);
00504     p->commit_buf = (UTFCHAR *) calloc(1, sizeof(UTFCHAR) * BUFSIZE);
00505     
00506     p->conversion_string = (UTFCHAR *) calloc(1, sizeof(UTFCHAR) * BUFSIZE);
00507     p->preedit_string = (UTFCHAR *) calloc(1, sizeof(UTFCHAR) * BUFSIZE);
00508     p->luc_candidates = NULL;
00509     p->luc_labels = NULL;
00510     p->luc_nchoices = 36;
00511     p->caret_pos = -1;
00512     p->max_candidates = MAXCANDIDATES - 1;
00513     p->luc_current_candidate = 0;
00514     p->luc_fbs_reverse = create_feedback(0, BUFSIZE);
00515     p->luc_fbs_normal = create_feedback(0, BUFSIZE);
00516     p->preedit_feedback = create_feedback(0, BUFSIZE);
00517     for(i=0;i<BUFSIZE;i++) {
00518        set_feedback(&p->luc_fbs_reverse[i], IMUnderline);
00519        set_feedback(&p->luc_fbs_reverse[i], IMReverse);
00520        set_feedback(&p->luc_fbs_reverse[i], IMNormal);
00521     }
00522 
00523     if(first_create) {
00524         for (i=0;i< MAX_SESSION;i++) s_id[i]=0;
00525         first_create=0;
00526     }
00527 
00528     for (i=0;i< MAX_SESSION;i++)
00529         if (s_id[i]==0) { p->session_id = i ; break; }
00530 
00531     if ( i == MAX_SESSION ) {
00532          perror(" Can't open so many subwindows");
00533          return False;
00534     }
00535     s_id[i]=1;
00536     p->username = (char *)strdup(desktop->user_name);
00537     IM_setValue(USER_NAME, p->username);
00538     IM_setValue(ENGINE_PATH, desktop->If->ifpath_name);
00539 
00540     
00541 #ifdef DEBUG
00542     printf("if_newpy_CreateSC()\n");
00543     printf("    If=[%x]\n", desktop->If);
00544     printf("    desktop=[%x]\n", desktop);
00545     printf("    locale=[%s]\n", desktop->If->locale);
00546     printf("    if_name=[%s]\n", desktop->If->if_name);
00547     printf("    USER:%s\n", desktop->user_name);
00548     printf("    HOST:%s\n", desktop->host_name);
00549     printf("    DISPLAY:%s\n", desktop->display_id);
00550 
00551     for (i = 0; i < num_args; i++, args++) {
00552         switch (args->id) {
00553             case UI_USER_NAME:
00554                 if (args->value) {
00555                     printf("    UI_USER_NAME=%s\n", args->value);
00556                 }
00557                 break;
00558             case UI_HOST_NAME:
00559                 if (args->value) {
00560                     printf("    UI_HOST_NAME=%s\n", args->value);
00561                 }
00562                 break;
00563             case UI_DISPLAY_ID:
00564                 if (args->value) {
00565                     printf("    UI_DISPLAY_ID=%s\n", args->value);
00566                 }
00567                 break;
00568             case UI_PROTOCOL_TYPE:
00569                 if (args->value) {
00570                     printf("    UI_PROTOCOL_TYPE=%s\n", args->value);
00571                 }
00572                 break;
00573             case UI_XSERVER_VENDOR:
00574                 if (args->value) {
00575                     printf("    UI_XSERVER_VENDOR=%s\n", args->value);
00576                 }
00577                 break;
00578             case UI_CLIENT_TYPE:
00579                 if (args->value) {
00580                     printf("    UI_CLIENT_TYPE=%s\n", args->value);
00581                 }
00582                 break;
00583             case UI_OS_NAME:
00584                 if (args->value) {
00585                     printf("    UI_OS_NAME=%s\n", args->value);
00586                 }
00587                 break;
00588             case UI_OS_ARCH:
00589                 if (args->value) {
00590                     printf("    UI_OS_ARCH=%s\n", args->value);
00591                 }
00592                 break;
00593             case UI_OS_VERSION:
00594                 if (args->value) {
00595                     printf("    UI_OS_VERSION=%s\n", args->value);
00596                 }
00597                 break;
00598             }
00599     }
00600 #endif
00601 
00602     s->specific_data = (void*) p;
00603     
00604     return True;
00605 }
00606 
00607 Bool
00608 if_newpy_DestroySC(
00609     iml_session_t * s
00610 )
00611 {
00612     MyDataPerSession *p = (MyDataPerSession *) s->specific_data;
00613     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) s->desktop->specific_data;
00614 
00615     IM_close(p->session_id);
00616     s_id[p->session_id] = 0;
00617 
00618     if ( s == desktop_data->auxproxy_session) {
00619        aux_done(s);
00620        desktop_data->auxproxy_session = NULL;
00621        desktop_data->punc = 0;
00622        desktop_data->skb = 13;
00623        desktop_data->gbk_support = 0;
00624     }
00625 
00626 
00627     if (p->preedit_buf)
00628         free((char *) p->preedit_buf);
00629     if (p->commit_buf)
00630         free((char *) p->commit_buf);
00631     if (p->conversion_string)
00632         free((char *) p->conversion_string);
00633     if (p->preedit_string)
00634         free((char *) p->preedit_string);
00635     if (p->preedit_feedback)
00636         free((char *) p->preedit_feedback);
00637     if (p->luc_fbs_reverse)
00638         free((char *) p->luc_fbs_reverse);
00639     if (p->luc_fbs_normal)
00640         free((char *) p->luc_fbs_normal);
00641     free((char *) p);
00642 #ifdef DEBUG
00643     printf("if_newpy_DestroySC(s=%x)\n", s);
00644     debugprint(s);
00645 #endif
00646     return True;
00647 }
00648 
00649 Bool
00650 if_newpy_GetSCValue(
00651     iml_session_t * s,
00652     IMArgList args,
00653     int num_args
00654 )
00655 {
00656     int i;
00657     IMArg *p = args;
00658     
00659     static int charsubset[] = {
00660         67,                 /* LATIN */
00661         47,                 /* HIRAGANA */
00662         48,                 /* KATAKANA */
00663         71,                 /* KANJI */
00664         0
00665     };
00666     
00667 #ifdef DEBUG
00668     printf("if_newpy_GetSCValue(s=%x)\n", s);
00669     debugprint(s);
00670 #endif
00671     for (i = 0; i < num_args; i++, p++) {
00672         switch (p->id) {
00673             case SC_SUPPORTED_CHARACTER_SUBSETS:
00674                 /* specify CHARACTER_SUBSETS */
00675                 p->value = (IMArgVal) charsubset;
00676                 break;
00677             default:
00678                 break;
00679             }
00680     }
00681     return True;
00682 }
00683 
00684 UTFCHAR off_string[] = {0};
00685 UTFCHAR title_string[] = {0x67e5, 0x627e, 0x8868, 0};
00686 
00687 void
00688 my_conversion_on(
00689     iml_session_t * s
00690 )
00691 {
00692     iml_inst *lp;
00693     iml_inst *rrv = NULL;
00694     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00695     session_data->conv_on = True;
00696     
00697     lp = s->If->m->iml_make_start_conversion_inst(s);
00698     s->If->m->iml_link_inst_tail(&rrv, lp);
00699     
00700     if (!IS_REGION_ACTIVE(s, PREEDIT)) {
00701         lp = s->If->m->iml_make_preedit_start_inst(s);
00702         s->If->m->iml_link_inst_tail(&rrv, lp);
00703     }
00704     lp = s->If->m->iml_execute(s, &rrv);
00705     status_draw(s);
00706 }
00707 
00708 void
00709 my_conversion_off(
00710     iml_session_t * s
00711 )
00712 {
00713     iml_inst *lp;
00714     iml_inst *rrv = NULL;
00715     
00716     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00717     session_data->conv_on = False;
00718 
00719     IM_close(session_data->session_id);
00720     
00721     /* if preedit exists, commit the string */
00722     commit(s);
00723     
00724     if (IS_REGION_ACTIVE(s, LOOKUP)) {
00725         lp = s->If->m->iml_make_lookup_done_inst(s);
00726         s->If->m->iml_link_inst_tail(&rrv, lp);
00727     }
00728     if (IS_REGION_ACTIVE(s, PREEDIT)) {
00729         lp = s->If->m->iml_make_preedit_erase_inst(s);
00730         s->If->m->iml_link_inst_tail(&rrv, lp);
00731         lp = s->If->m->iml_make_preedit_done_inst(s);
00732         s->If->m->iml_link_inst_tail(&rrv, lp);
00733         session_data->caret_pos = -1;
00734     }
00735     lp = s->If->m->iml_make_end_conversion_inst(s);
00736     s->If->m->iml_link_inst_tail(&rrv, lp);
00737     
00738     s->If->m->iml_execute(s, &rrv);
00739     
00740     status_draw(s);
00741 }
00742 
00743 Bool
00744 if_newpy_SetSCValue(
00745     iml_session_t * s,
00746     IMArgList args,
00747     int num_args
00748 )
00749 {
00750     int i;
00751     IMArg *p = args;
00752     UTFCHAR *strs[17];
00753     UTFCHAR state[5];
00754     int aux_int_data_convoff[] = {3};
00755 
00756 #ifdef DEBUG
00757     printf("if_newpy_SetSCValue()\n");
00758     debugprint(s);
00759 #endif
00760 
00761     for (i = 0; i < num_args; i++, p++) {
00762         switch (p->id) {
00763             case SC_TRIGGER_ON_NOTIFY:
00764                 my_conversion_on(s);
00765                 break;
00766                 
00767             case SC_TRIGGER_OFF_NOTIFY:
00768                 my_conversion_off(s);
00769                 break;
00770                 
00771             case SC_REALIZE:
00772               {
00773                   MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) s->desktop->specific_data;
00774                   MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00775                   IM_init(session_data->session_id);
00776                     if(desktop_data->auxproxy_session == NULL) {
00777                       desktop_data->auxproxy_session = s;
00778                      aux_start(s);
00779                      state[0] = 0 + 'a';
00780                      state[1] = desktop_data->punc + 'a';
00781                      state[2] = desktop_data->skb + 'a';
00782                      state[3] = desktop_data->gbk_support + 'a';
00783                      state[4] = 0;
00784                      strs[0] = state;
00785                      for(i = 1; i < 17; i ++) strs[i] = on_string[i - 1];
00786                      aux_draw(s, 1, aux_int_data_convoff, 17, (UTFCHAR **)strs);
00787                   }
00788               }
00789                 break;
00790 
00791            case SC_LOOKUP_LABELTYPE:
00792                 break;
00793 
00794             default:
00795                 break;
00796             }
00797     }
00798     return True;
00799 }
00800 
00801 IMText *
00802 if_newpy_ResetSC(
00803     iml_session_t * s
00804 )
00805 {
00806     int i;
00807     iml_inst *lp;
00808     IMText *p = make_preedit_imtext(s);
00809     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00810     
00811 #ifdef DEBUG
00812     printf("if_newpy_ResetSC(s=%x)\n", s);
00813     debugprint(s);
00814 #endif
00815     
00816     /*
00817     * when you return IMText for commit string, you need to call
00818     * iml_make_preedit_erase_inst() here.
00819     */
00820     lp = s->If->m->iml_make_preedit_erase_inst(s);
00821     s->If->m->iml_execute(s, &lp);
00822     
00823     /*
00824     * reset buffer
00825     */
00826     for(i=0;i<BUFSIZE;i++){
00827        set_feedback(&session_data->preedit_feedback[i], IMUnderline);
00828     }
00829     memset(session_data->preedit_buf, 0, sizeof(UTFCHAR) * BUFSIZE);
00830     memset(session_data->commit_buf, 0, sizeof(UTFCHAR) * BUFSIZE);
00831     memset(session_data->preedit_string, 0, sizeof(UTFCHAR) * BUFSIZE);
00832     memset(session_data->conversion_string, 0, sizeof(UTFCHAR) * BUFSIZE);
00833     session_data->caret_pos = -1;
00834     
00835     /*
00836     * return committed string
00837     */
00838     if (p->char_length) {
00839         return p;
00840     }
00841     return (IMText *) NULL;
00842 }
00843 
00844 void
00845 if_newpy_SetSCFocus(
00846     iml_session_t * s
00847 )
00848 {
00849     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00850     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) s->desktop->specific_data;
00851 #ifdef DEBUG
00852     printf("if_newpy_SetSCFocus()\n");
00853 #endif
00854     status_draw(s);
00855 }
00856 
00857 void
00858 if_newpy_UnsetSCFocus(
00859     iml_session_t * s
00860 )
00861 {
00862 #ifdef DEBUG
00863     printf("if_newpy_UnsetSCFocus()\n");
00864     debugprint(s);
00865 #endif
00866 }
00867 
00868 void
00869 if_newpy_SendEvent(
00870     iml_session_t * s,
00871     IMInputEvent * ev
00872 )
00873 {
00874 #ifdef DEBUG
00875     printf("if_newpy_SendEvent s=%x ev=%x\n", s, ev);
00876 #endif
00877 
00878     if (ev) {
00879         if (ev->type == IM_EventKeyList) {
00880             if (receive_keylist(s, (IMKeyListEvent *) ev) == False) {
00881                 IMKeyListEvent *keylist = (IMKeyListEvent *) ev;
00882                 iml_inst *lp = s->If->m->iml_make_keypress_inst(s, (IMKeyEventStruct *) keylist->keylist);
00883                 s->If->m->iml_execute(s, &lp);
00884             }
00885         } else if (ev->type == IM_EventAux) {
00886             IMAuxEvent *aux = (IMAuxEvent *) ev;
00887             IMAuxDrawCallbackStruct *data = aux->aux;
00888             receive_aux(s, data);
00889         } else if (ev->type == IM_EventString) {
00890             /* String Event */
00891         } else if (ev->type == IM_EventText) {
00892             /* Text Event */
00893         }
00894     }
00895 }
00896 
00897 void im_send_aux_event(s,key,index)
00898 iml_session_t *s;
00899 int key;
00900 int index;
00901 {
00902     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00903     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) s->desktop->specific_data;
00904     im_data *imdata;
00905     extern im_data *IM_setAuxValue();
00906 
00907 /*        XIMAuxDrawCallbackStruct *dcb;*/
00908 
00909     switch(key) {
00910         case '1':
00911             desktop_data->punc = (desktop_data->punc == 0) ? 1 : 0 ;
00912 /*
00913             imdata = (im_data *)IM_setAuxValue(session_data->session_id, PUNCTUATION_STATUS, desktop_data->punc);
00914             if(imdata != NULL) eval_packet(s, imdata);
00915 */
00916             break;
00917 
00918         case '2':
00919            if(index != -1) desktop_data->skb = index;
00920            else {
00921                      desktop_data->skb ++; 
00922               desktop_data->skb = desktop_data->skb % 15;
00923            }
00924 /*
00925             imdata = (im_data *)IM_setAuxValue(session_data->session_id, SOFTKEYBOARD_LAYOUT, desktop_data->skb);
00926             if(imdata != NULL) eval_packet(s, imdata);
00927 */
00928             break;
00929 
00930         case '3':
00931             desktop_data->gbk_support = (desktop_data->gbk_support == 0) ? 1 : 0 ;
00932 /*
00933             imdata = (im_data *)IM_setAuxValue(session_data->session_id, GBKSWITCH_STATUS, desktop_data->skb);
00934             if(imdata != NULL) eval_packet(s, imdata);
00935 */
00936             break;
00937 
00938         default:
00939             break;
00940     }
00941 /*
00942         dcb = (XIMAuxDrawCallbackStruct *)create_aux_struct(s);
00943         im_draw_aux(s,dcb);
00944 */
00945         return;
00946 }
00947 
00948 void eval_packet(s, imdata)
00949 iml_session_t *s ;
00950 im_data *imdata;
00951 {
00952     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00953     iml_inst *lp;
00954     UTFCHAR **luc_tmp;
00955     int k;
00956     int caret;
00957 
00958 /*        if(imdata->operation == CIM_STATUS) im_set_mode(s,imdata->status_str); */
00959        
00960     session_data->caret_pos = imdata->caret_pos;
00961     zh_str_to_utf16(imdata->pre_str, session_data->preedit_buf, &session_data->caret_pos);
00962     preedit_draw(s);
00963        
00964     if( (imdata->operation == CIM_COMMIT) || (imdata->operation == CIM_COMMITSYMBOL) ) {
00965        zh_str_to_utf16(imdata->commit_str, session_data->commit_buf, &caret);
00966        commit(s);
00967     }
00968 
00969     if (imdata->luc_num > 0) {
00970        session_data->luc_nchoices = imdata->luc_num;
00971        luc_tmp = (UTFCHAR**) s->If->m->iml_new(s, session_data->luc_nchoices * sizeof(UTFCHAR*));
00972        for(k = 0; k < session_data->luc_nchoices; k ++) {
00973            luc_tmp[k] = (UTFCHAR*) s->If->m->iml_new(s, MAX_PHRASE_LEN * sizeof(UTFCHAR));
00974            luc_tmp[k][0] = 0;
00975            zh_str_to_utf16(imdata->luc_str[k], luc_tmp[k], &caret);
00976        }
00977        lookup_draw(s, luc_tmp, imdata->luc_num);
00978     }
00979     else if (IS_REGION_ACTIVE(s, LOOKUP)) {
00980         lp = (iml_inst*)s->If->m->iml_make_lookup_done_inst(s);
00981         s->If->m->iml_execute(s, &lp);
00982     }
00983 }
00984 
00985 Bool
00986 receive_keylist(
00987     iml_session_t * s,
00988     IMKeyListEvent * keylist
00989 )
00990 {
00991     int i;
00992     im_data *imdata;
00993     IMKeyEventStruct *k = (IMKeyEventStruct *) keylist->keylist;
00994     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
00995     long kcode, modifier;
00996     unsigned short kchar;
00997     i = UTFCHARLen(session_data->preedit_buf);
00998 
00999     #ifdef DEBUG
01000        printf("keycode %x, keychar %x state %x\n",k->keyCode, k->keyChar, k->modifier);
01001     #endif
01002 
01003     kcode = k->keyCode;
01004     kchar = k->keyChar;
01005     modifier = k->modifier; 
01006     modifyEvent(&kcode, &kchar, &modifier);
01007 /*
01008     if(modifier == 8) {  /aux operation/ 
01009        im_send_aux_event(s, kcode, -1);
01010        return True;
01011     }
01012 */
01013     if((modifier == 4) && (kchar == 0x20)) { /* conversion off */
01014        my_conversion_off(s);
01015        return True;
01016     }
01017     imdata = (im_data *)IM_trans(session_data->session_id, kcode, kchar, modifier);
01018     if(imdata == NULL) return False;
01019 
01020     #ifdef DEBUG
01021     if(imdata != NULL) {
01022         printf("imdata->operation=%d\n",imdata->operation);
01023         printf("imdata->pre_str=%s\n",imdata->pre_str);
01024         printf("imdata->caret_pos=%d\n",imdata->caret_pos);
01025         printf("imdata->luc_str=%s\n",imdata->luc_str[0]);
01026         printf("imdata->luc_num=%d\n",imdata->luc_num);
01027         printf("imdata->commit_str=%s\n",imdata->commit_str);
01028         printf("imdata->status_str=%s\n",imdata->status_str);
01029         printf("imdata->error_num=%d\n",imdata->error_num);
01030     }
01031     #endif
01032 
01033     if(imdata->operation == CIM_BOUNCEKEY) return False;
01034     else {
01035         eval_packet(s, imdata);
01036        return True;
01037     }
01038 }
01039 
01040 UTFCHARCat(
01041     UTFCHAR * dest,
01042     UTFCHAR * str1,
01043     UTFCHAR * str2
01044 )
01045 {
01046     int i;
01047     for (i = 0; *str1; i++) {
01048         *dest++ = *str1++;
01049     }
01050     for (i = 0; *str2; i++) {
01051         *dest++ = *str2++;
01052     }
01053     *dest = 0;
01054     return i;
01055 }
01056 
01057 int
01058 UTFCHARCpy(
01059     UTFCHAR * dest,
01060     UTFCHAR * original
01061 )
01062 {
01063     int i;
01064     for (i = 0; *original; i++) {
01065         *dest++ = *original++;
01066     }
01067     *dest = 0;
01068     return i;
01069 }
01070 
01071 int
01072 UTFCHARLen(
01073     UTFCHAR * p
01074 )
01075 {
01076     int i;
01077     for (i = 0; *p; i++)
01078     p++;
01079     return i;
01080 }
01081 
01082 void
01083 commit(
01084     iml_session_t * s
01085 )
01086 {
01087     int len, i;
01088     iml_inst *lp;
01089     iml_inst *rrv = NULL;
01090     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
01091     IMText *p = (IMText *) s->If->m->iml_new(s, sizeof(IMText));
01092     memset(p, 0, sizeof(IMText));
01093     p->encoding = UTF16_CODESET;
01094     
01095     len = UTFCHARLen(session_data->commit_buf);
01096     if (len != 0) {
01097        p->text.utf_chars = (UTFCHAR *) s->If->m->iml_new(s, sizeof(UTFCHAR) * (len + 1));
01098        UTFCHARCpy(p->text.utf_chars, session_data->commit_buf);
01099        p->char_length = len;
01100        p->feedback = create_feedback(s, p->char_length);
01101        lp = s->If->m->iml_make_commit_inst(s, p);
01102        s->If->m->iml_link_inst_tail(&rrv, lp);
01103        s->If->m->iml_execute(s, &rrv);
01104     }
01105     for(i=0;i<BUFSIZE;i++){
01106        set_feedback(&session_data->preedit_feedback[i], IMUnderline);
01107     }
01108     memset(session_data->preedit_buf, 0, sizeof(UTFCHAR) * BUFSIZE);
01109     memset(session_data->commit_buf, 0, sizeof(UTFCHAR) * BUFSIZE);
01110     memset(session_data->preedit_string, 0, sizeof(UTFCHAR) * BUFSIZE);
01111     memset(session_data->conversion_string, 0, sizeof(UTFCHAR) * BUFSIZE);
01112     
01113     session_data->caret_pos = -1;
01114 }
01115 
01116 
01117 void
01118 status_draw(
01119     iml_session_t * s
01120 )
01121 {
01122     int len, i;
01123     iml_inst *lp;
01124     iml_inst *rrv = NULL;
01125     UTFCHAR *str;
01126     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
01127     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) s->desktop->specific_data;
01128     IMText *p = (IMText *) s->If->m->iml_new(s, sizeof(IMText));
01129     int aux_int_data_convon[] = {2};
01130     int aux_int_data_convoff[] = {3};
01131     im_data *imdata;
01132     extern im_data *IM_setAuxValue();
01133     UTFCHAR *strs[1];
01134     UTFCHAR state[5];
01135 
01136     state[1] = desktop_data->punc + 'a';
01137     state[2] = desktop_data->skb + 'a';
01138     state[3] = desktop_data->gbk_support + 'a';
01139     state[4] = 0;
01140     strs[0] = state;
01141 
01142     memset(p, 0, sizeof(IMText));
01143     p->encoding = UTF16_CODESET;
01144     
01145     if (session_data->conv_on) {
01146         str = on_string[desktop_data->skb];
01147        state[0] = 1 + 'a';
01148        aux_draw(s, 0, NULL, 1, strs);
01149 
01150     } else {
01151         str = off_string;
01152        state[0] = 0 + 'a';
01153        aux_draw(s, 0, NULL, 1, strs);
01154     }
01155     
01156     len = UTFCHARLen(str);
01157     p->text.utf_chars = (UTFCHAR *) s->If->m->iml_new(s, sizeof(UTFCHAR) * (len + 1));
01158     
01159     UTFCHARCpy(p->text.utf_chars, str);
01160     
01161     p->char_length = len;
01162     p->feedback = create_feedback(s, p->char_length);
01163 
01164     if (!IS_REGION_ACTIVE(s, STATUS)) {
01165         lp = s->If->m->iml_make_status_start_inst(s);
01166         s->If->m->iml_link_inst_tail(&rrv, lp);
01167     }
01168     lp = s->If->m->iml_make_status_draw_inst(s, p);
01169     s->If->m->iml_link_inst_tail(&rrv, lp);
01170     
01171     s->If->m->iml_execute(s, &rrv);
01172 
01173     if (!session_data->conv_on) return;
01174     IM_setAuxValue(session_data->session_id, PUNCTUATION_STATUS, desktop_data->punc);
01175 
01176     imdata = (im_data *)IM_setAuxValue(session_data->session_id, SOFTKEYBOARD_LAYOUT, desktop_data->skb);
01177     if(imdata != NULL) eval_packet(s, imdata);
01178 
01179     imdata = IM_setAuxValue(session_data->session_id, GBKSWITCH_STATUS, desktop_data->gbk_support);
01180     if(imdata != NULL) eval_packet(s, imdata);
01181 }
01182 
01183 void
01184 preedit_buf_print(iml_session_t * s)
01185 {
01186     int i;
01187     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
01188     int len = UTFCHARLen(session_data->preedit_buf);
01189     for (i = 0; i <= len; i++) {
01190         printf("Preedit[%d]=%x     %x\n",
01191             i,
01192             session_data->preedit_buf[i],
01193            get_feedback(&session_data->preedit_feedback[i]));
01194     }
01195 }
01196 
01197 IMText *
01198 make_imtext(iml_session_t * s, UTFCHAR * u)
01199 {
01200     int len;
01201     IMText *p = (IMText *) s->If->m->iml_new(s, sizeof(IMText));
01202     memset(p, 0, sizeof(IMText));
01203     p->encoding = UTF16_CODESET;
01204     len = UTFCHARLen(u);
01205     p->text.utf_chars = (UTFCHAR *) s->If->m->iml_new(s, sizeof(UTFCHAR) * (len + 1));
01206     UTFCHARCpy(p->text.utf_chars, u);
01207     p->char_length = len;
01208     p->feedback = create_feedback(s, p->char_length);
01209     return p;
01210 }
01211 
01212 IMText *
01213 make_preedit_imtext(iml_session_t * s)
01214 {
01215     int len, i;
01216     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
01217     IMText *p = (IMText *) s->If->m->iml_new(s, sizeof(IMText));
01218     memset(p, 0, sizeof(IMText));
01219 
01220     p->encoding = UTF16_CODESET;
01221     len = UTFCHARLen(session_data->preedit_buf);
01222     p->text.utf_chars = (UTFCHAR *) s->If->m->iml_new(s, sizeof(UTFCHAR) * (len + 1));
01223     UTFCHARCpy(p->text.utf_chars, session_data->preedit_buf);
01224     p->char_length = len;
01225     p->feedback = create_feedback(s, p->char_length);
01226     for(i = 0; i < session_data->caret_pos; i ++) {
01227        set_feedback(&session_data->preedit_feedback[i], IMReverse);
01228     }
01229     for(i = session_data->caret_pos; i < p->char_length; i ++) {
01230        set_feedback(&session_data->preedit_feedback[i], IMUnderline);
01231     }
01232     for (i = 0; i < p->char_length; i++) {
01233         set_feedback(&p->feedback[i], get_feedback(&session_data->preedit_feedback[i]));
01234     }
01235     return p;
01236 }
01237 
01238 void
01239 preedit_draw(
01240     iml_session_t * s
01241 )
01242 {
01243     iml_inst *lp;
01244     iml_inst *rrv = NULL;
01245     
01246     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
01247     IMText *p = make_preedit_imtext(s);
01248     
01249     if (!IS_REGION_ACTIVE(s, PREEDIT)) {
01250         lp = s->If->m->iml_make_preedit_start_inst(s);
01251         s->If->m->iml_link_inst_tail(&rrv, lp);
01252     }
01253     if(p->char_length == 0) {
01254        lp = s->If->m->iml_make_preedit_erase_inst(s);
01255        s->If->m->iml_link_inst_tail(&rrv, lp);
01256        s->If->m->iml_execute(s, &rrv);
01257        return;
01258     }
01259     lp = s->If->m->iml_make_preedit_draw_inst(s, p);
01260     s->If->m->iml_link_inst_tail(&rrv, lp);
01261     
01262     if(session_data->caret_pos != -1) {
01263        lp = s->If->m->iml_make_preedit_caret_inst(s, session_data->caret_pos); 
01264        s->If->m->iml_link_inst_tail(&rrv, lp);
01265     }
01266     s->If->m->iml_execute(s, &rrv);
01267 }
01268 
01269 void
01270 lookup_draw(iml_session_t * s, UTFCHAR ** luc_tmp, int luc_num)
01271 {
01272     int i;
01273     int j = 0;
01274     iml_inst *lp;
01275     MyDataPerSession *session_data = (MyDataPerSession *) s->specific_data;
01276     IMLookupDrawCallbackStruct *draw;
01277     IMLookupStartCallbackStruct *start;
01278     int max_len = 0;
01279     IMText **candidates;
01280     IMText **labels;
01281     int len;
01282     UTFCHAR buf[BUFSIZE];
01283     
01284     if (session_data->luc_candidates == NULL) {
01285         session_data->luc_candidates = (IMText **) calloc(MAXCANDIDATES, sizeof(IMText *));
01286     }
01287     candidates = session_data->luc_candidates;
01288     
01289     for (i = 0; i < luc_num; i++) {
01290         if (candidates[i]) {
01291             free(candidates[i]->text.utf_chars);
01292             free(candidates[i]->feedback);
01293             free(candidates[i]);
01294         }
01295         candidates[i] = (IMText *) calloc(1, sizeof(IMText));
01296         candidates[i]->encoding = UTF16_CODESET;
01297         candidates[i]->char_length = UTFCHARLen(luc_tmp[i]);
01298         candidates[i]->text.utf_chars = (UTFCHAR *) calloc(1,
01299             sizeof(IMText) * (candidates[i]->char_length + 1));
01300         UTFCHARCpy(candidates[i]->text.utf_chars, luc_tmp[i]);
01301 
01302         candidates[i]->feedback = create_feedback(0, candidates[i]->char_length);
01303     }
01304     
01305     if (session_data->luc_labels == NULL) {
01306         session_data->luc_labels = (IMText **) calloc(MAXCANDIDATES, sizeof(IMText));
01307         labels = session_data->luc_labels;
01308         for (i = '1'; i <= '9'; i++, j++) {
01309             labels[j] = (IMText *) calloc(1, sizeof(IMText));
01310            labels[j]->encoding = UTF16_CODESET;
01311             labels[j]->char_length = 1;
01312             labels[j]->text.utf_chars = (UTFCHAR *) calloc(1,
01313                 sizeof(IMText) * (labels[j]->char_length + 1));
01314             labels[j]->text.utf_chars[0] = (UTFCHAR) i;
01315             labels[j]->feedback = create_feedback(0, labels[j]->char_length);
01316         }
01317     }
01318     labels = session_data->luc_labels;
01319     
01320     if (!IS_REGION_ACTIVE(s, LOOKUP)) {
01321         session_data->luc_top = 0;
01322         start = (IMLookupStartCallbackStruct *) s->If->m->iml_new(s, sizeof(IMLookupStartCallbackStruct));
01323         memset(start, 0, sizeof(IMLookupStartCallbackStruct));
01324         start->whoIsMaster = IMIsMaster;
01325         session_data->luc_type = IMIsMaster;
01326         start->IMPreference = (LayoutInfo *) s->If->m->iml_new(s, sizeof(LayoutInfo));
01327         memset(start->IMPreference, 0, sizeof(LayoutInfo));
01328         start->IMPreference->choice_per_window = 7;
01329         start->IMPreference->ncolumns = 7;
01330         start->IMPreference->nrows = 1;
01331         start->IMPreference->drawUpDirection = DrawUpHorizontally;
01332         start->IMPreference->whoOwnsLabel = IMOwnsLabel;
01333         start->CBPreference = NULL;
01334         lp = s->If->m->iml_make_lookup_start_inst(s, start);
01335         s->If->m->iml_execute(s, &lp);
01336     }
01337     draw = (IMLookupDrawCallbackStruct *) s->If->m->iml_new(s, sizeof(IMLookupDrawCallbackStruct));
01338     memset(draw, 0, sizeof(IMLookupDrawCallbackStruct));
01339     draw->index_of_first_candidate = 0;
01340     draw->index_of_last_candidate = luc_num - 1;
01341     draw->n_choices = draw->index_of_last_candidate - draw->index_of_first_candidate + 1;
01342     
01343     draw->title = (IMText *) s->If->m->iml_new(s, sizeof(IMText));
01344     memset(draw->title, 0, sizeof(IMText));
01345     draw->title->encoding = UTF16_CODESET;
01346     draw->title->char_length = UTFCHARLen(title_string);
01347     draw->title->text.utf_chars = (UTFCHAR *) s->If->m->iml_new(s,
01348         sizeof(IMText) * (draw->title->char_length + 1));
01349     UTFCHARCpy(draw->title->text.utf_chars, title_string);
01350     draw->title->feedback = create_feedback(s, draw->title->char_length);
01351     
01352     draw->choices = (IMChoiceObject *) s->If->m->iml_new(s, draw->n_choices * sizeof(IMChoiceObject));
01353     
01354     for (i = 0; i < draw->n_choices; i++) {
01355         IMText *vt;         /* for value */
01356         IMText *lt;         /* for label */
01357         vt = draw->choices[i].value = candidates[i + session_data->luc_top];
01358         lt = draw->choices[i].label = labels[i];
01359 #ifdef DEBUG        
01360         printf("candidates[%d]=%x\n", i + session_data->luc_top,
01361             candidates[i + session_data->luc_top]);
01362 #endif
01363         
01364         if (max_len < vt->char_length)
01365             max_len = vt->char_length;
01366         
01367         if (i + session_data->luc_top == session_data->max_candidates) {
01368             draw->index_of_first_candidate = 0;
01369             draw->index_of_last_candidate = i;
01370             draw->n_choices = i + 1;
01371             break;
01372         }
01373     }
01374     draw->max_len = max_len;
01375     draw->max_len = 20;
01376     draw->index_of_current_candidate = session_data->luc_current_candidate;
01377     
01378 #ifdef DEBUG        
01379     printf("session_data->luc_top=%x\n", session_data->luc_top);
01380     
01381     printf("draw->index_of_first_candidate=%x\n", draw->index_of_first_candidate);
01382     printf("draw->index_of_last_candidate=%x\n", draw->index_of_last_candidate);
01383     printf("draw->n_choices=%x\n", draw->n_choices);
01384     printf("draw->max_len=%x\n", max_len);
01385     printf("draw->index_of_current_candidate=%x\n", session_data->luc_current_candidate);
01386 #endif    
01387 
01388     lp = s->If->m->iml_make_lookup_draw_inst(s, draw);
01389     s->If->m->iml_execute(s, &lp);
01390 }
01391 
01392 void
01393 receive_aux(
01394     iml_session_t * s,
01395     IMAuxDrawCallbackStruct * aux
01396 )
01397 {
01398     IMText *lts, *lt;
01399     int i, j, index;
01400     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) s->desktop->specific_data;
01401 
01402 #ifdef DEBUG
01403     printf("AUX\n");
01404     
01405     printf("\taux_name=[%s]\n", aux->aux_name);
01406     printf("\tcount_integer_values=[%d]\n", aux->count_integer_values);
01407     printf("\tcount_string_values=[%d]\n", aux->count_string_values);
01408     
01409 #endif
01410 /*
01411     if(aux->count_integer_values == 3) {
01412        desktop_data->punc = aux->integer_values[0];
01413        desktop_data->skb = aux->integer_values[1];
01414        desktop_data->gbk_support = aux->integer_values[2];     
01415     }
01416 */
01417 
01418     lts = aux->string_values;
01419     for (i = 0, lt = lts; i < aux->count_string_values; i++, lt++) {
01420         printf("\t%3d:[%d]\n", i, lt->char_length);
01421               for (j = 0; j < lt->char_length; j++) {
01422             printf("[%x]", lt->text.utf_chars[j]);
01423        }
01424        printf("\n");
01425        desktop_data->punc = lt->text.utf_chars[0] - 'a';
01426        desktop_data->skb = lt->text.utf_chars[1] - 'a';
01427        desktop_data->gbk_support = lt->text.utf_chars[2] - 'a';
01428     }
01429 }
01430 
01431 void
01432 aux_start(
01433     iml_session_t * s
01434 )
01435 {
01436     int i;
01437     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) s->desktop->specific_data;
01438     iml_session_t *s_ = desktop_data->auxproxy_session;
01439 
01440     if(s_ == NULL) {
01441        s_ = desktop_data->auxproxy_session = s;
01442        printf("aux_start: auxproxy_session is NULL, take the responsibility for auxproxy\n");
01443     }
01444 
01445     if (desktop_data->aux_started == False) {
01446         iml_inst *lp;
01447         IMAuxStartCallbackStruct *aux = (IMAuxStartCallbackStruct *) s_->If->m->iml_new(s_, sizeof(IMAuxStartCallbackStruct));
01448        memset(aux, 0, sizeof(IMAuxStartCallbackStruct));
01449         aux->aux_name = class_names[0];
01450         lp = s_->If->m->iml_make_aux_start_inst(s_, aux);
01451         s_->If->m->iml_execute(s_, &lp);
01452 #ifdef DEBUG
01453         printf("Starting AUX [%s]\n", class_names[0]);
01454 #endif
01455         desktop_data->aux_started = True;
01456     } else {
01457 #ifdef DEBUG
01458         printf("AUX[%s] is already started.\n", class_names[0]);
01459 #endif
01460     }
01461 }
01462 
01463 void
01464 aux_done(
01465     iml_session_t * s
01466 )
01467 {
01468     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) s->desktop->specific_data;
01469     iml_session_t *s_ = desktop_data->auxproxy_session;
01470 
01471     if (desktop_data->aux_started == True) {
01472         iml_inst *lp;
01473         IMAuxDoneCallbackStruct *aux = (IMAuxDoneCallbackStruct *) 
01474               s_->If->m->iml_new(s_, sizeof(IMAuxDoneCallbackStruct));
01475        memset(aux, 0, sizeof(IMAuxDoneCallbackStruct));
01476         aux->aux_name = class_names[0];
01477         lp = s_->If->m->iml_make_aux_done_inst(s_, aux);
01478         s_->If->m->iml_execute(s_, &lp);
01479 #ifdef DEBUG
01480         printf("Closing AUX\n");
01481 #endif
01482         desktop_data->aux_started = False;
01483     } else {
01484 #ifdef DEBUG
01485         printf("AUX is already done.\n");
01486 #endif
01487     }
01488 }
01489 
01490 void
01491 aux_draw(
01492     iml_session_t * s,
01493     int count_integers,
01494     int *integers,
01495     int count_strings,
01496     UTFCHAR ** strings
01497 )
01498 {
01499     iml_inst *lp;
01500     int i, j;
01501     int len = 7;
01502     IMText *lts, *lt;
01503     IMAuxDrawCallbackStruct *aux;
01504     MyDataPerDesktop *desktop_data = (MyDataPerDesktop *) s->desktop->specific_data;
01505     iml_session_t *s_ = desktop_data->auxproxy_session;
01506 
01507     if(s_ == NULL) {
01508        s_ = desktop_data->auxproxy_session = s;
01509 #ifdef DEBUG
01510        printf("aux_draw: auxproxy_session is NULL, take the responsibility for auxproxy\n");
01511 #endif
01512     }
01513 
01514     if (desktop_data->aux_started == False) {
01515 #ifdef DEBUG
01516         printf("AUX is not started.\n");
01517 #endif
01518         return;
01519     }
01520     aux = (IMAuxDrawCallbackStruct *) s_->If->m->iml_new(s_, sizeof(IMAuxDrawCallbackStruct));
01521     memset(aux, 0, sizeof(IMAuxDrawCallbackStruct));
01522 
01523     aux->aux_name = class_names[0];
01524 
01525     aux->count_integer_values = count_integers;
01526     if (aux->count_integer_values) {
01527         aux->integer_values = (int *) s_->If->m->iml_new(s_, sizeof(int) * aux->count_integer_values);
01528         memset(aux->integer_values, 0, sizeof(int) * aux->count_integer_values);
01529 
01530         for (i = 0; i < aux->count_integer_values; i++) {
01531             aux->integer_values[i] = integers[i];
01532         }
01533     }
01534     aux->count_string_values = count_strings;
01535     if (aux->count_string_values) {
01536         aux->string_values = lts = (IMText *)
01537                s_->If->m->iml_new(s_, sizeof(IMText) * aux->count_string_values);
01538         memset(aux->string_values, 0, sizeof(IMText) * aux->count_string_values);
01539         aux->string_values->encoding = UTF16_CODESET;
01540 
01541         for (i = 0, lt = lts; i < aux->count_string_values; i++, lt++) {
01542             len = UTFCHARLen(strings[i]);
01543             lt->text.utf_chars = (UTFCHAR *) s_->If->m->iml_new(s_, sizeof(UTFCHAR) * (len + 1));
01544             lt->char_length = len + 1;
01545             UTFCHARCpy(lt->text.utf_chars, strings[i]);
01546         }
01547     }
01548     lp = s_->If->m->iml_make_aux_draw_inst(s_, aux);
01549     s_->If->m->iml_execute(s_, &lp);
01550 }
01551 
01552 IMFeedbackList *
01553 create_feedback2(
01554     iml_session_t * s,
01555     int size
01556 )
01557 {
01558     int i;
01559     IMFeedbackList *feedback, *fbl;
01560     IMFeedback *fb;
01561     feedback = (IMFeedbackList *) s->If->m->iml_new2(s, sizeof(IMFeedbackList) * size);
01562     memset(feedback, 0, sizeof(IMFeedbackList) * size);
01563     for (i = 0; i < size; i++) {
01564         IMFeedbackList *fbl = &feedback[i];
01565         fbl->count_feedbacks = 1;
01566         fb = fbl->feedbacks = (IMFeedback *) s->If->m->iml_new2(s, sizeof(IMFeedback));
01567         memset(fbl->feedbacks, 0, sizeof(IMFeedback));
01568     }
01569     return feedback;
01570 }
01571 
01572 IMFeedbackList *
01573 create_feedback(
01574     iml_session_t * s,
01575     int size
01576 )
01577 {
01578     int i;
01579     IMFeedbackList *feedback, *fbl;
01580     IMFeedback *fb;
01581     
01582     if (s) {
01583         feedback = (IMFeedbackList *) s->If->m->iml_new(s, sizeof(IMFeedbackList) * size);
01584         memset(feedback, 0, sizeof(IMFeedbackList) * size);
01585     } else {
01586         feedback = (IMFeedbackList *) calloc(1, sizeof(IMFeedbackList) * size);
01587     }
01588     for (i = 0; i < size; i++) {
01589         IMFeedbackList *fbl = &feedback[i];
01590         fbl->count_feedbacks = 1;
01591         if (s) {
01592             fb = fbl->feedbacks = (IMFeedback *) s->If->m->iml_new(s, sizeof(IMFeedback));
01593             memset(fbl->feedbacks, 0, sizeof(IMFeedback));
01594         } else {
01595             fb = fbl->feedbacks = (IMFeedback *) calloc(1, sizeof(IMFeedback));
01596         }
01597     }
01598     return feedback;
01599 }
01600 
01601 int
01602 get_feedback(
01603     IMFeedbackList * fbl
01604 )
01605 {
01606     IMFeedback *fb = &fbl->feedbacks[0];
01607     return IM_FEEDBACK_VALUE(fb);
01608 }
01609 
01610 void
01611 set_feedback(
01612     IMFeedbackList * fbl,
01613     int value
01614 )
01615 {
01616     IMFeedback *fb = &fbl->feedbacks[0];
01617     IM_FEEDBACK_TYPE(fb)=IM_DECORATION_FEEDBACK;
01618     IM_FEEDBACK_VALUE(fb)=value;
01619 }