Back to index

im-sdk  12.3.91
common.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <time.h>
00004 #include <iiimcf.h>
00005 #include "common.h"
00006 
00007 IIIMCF_handle handle;
00008 
00009 void
00010 test_iiimf_error(
00011     IIIMF_status st
00012 )
00013 {
00014     fprintf(stderr, "IIIMF error:%d\n", st);
00015     exit(255);
00016 }
00017 
00018 void
00019 check(
00020     IIIMF_status st
00021 )
00022 {
00023     if (st != IIIMF_STATUS_SUCCESS)
00024        test_iiimf_error(st);
00025     return;
00026 }
00027 
00028 void
00029 test_assert(
00030     int b
00031 )
00032 {
00033     if (!b) abort();
00034 }
00035 
00036 char*
00037 test_format_iiimcf_string(
00038     const IIIMP_card16 *pu16
00039 )
00040 {
00041     char *p, *pr, *pr2;
00042     int i, rest;
00043     int initlen = 256;
00044 
00045     pr = (char*) malloc(sizeof(*p) * initlen);
00046     rest = initlen - 1;
00047     for (p = pr; *pu16; pu16++) {
00048        if (*pu16 < 128) {
00049            if (rest < 1) {
00050               rest += initlen;
00051               initlen *= 2;
00052               pr2 = (char*) realloc(pr, sizeof(*p) * initlen);
00053               p = pr2 + (p - pr);
00054               pr = pr2;
00055            }
00056            *p++ = *pu16;
00057            rest--;
00058        } else {
00059            if (rest < 16) {
00060               rest += initlen;
00061               initlen *= 2;
00062               pr2 = (char*) realloc(pr, sizeof(*p) * initlen);
00063               p = pr2 + (p - pr);
00064               pr = pr2;
00065            }
00066            i = snprintf(p, 15, " U+%.4X", *pu16);
00067            p += i;
00068            rest -= i ;
00069        }
00070     }
00071     *p = '\0';
00072 
00073     return pr;
00074 }
00075 
00076 void
00077 test_output_iiimcf_string(
00078     const IIIMP_card16 *pu16
00079 )
00080 {
00081     char *str;
00082     str = test_format_iiimcf_string(pu16);
00083     if (!str) return;
00084     fputs(str, stderr);
00085     free(str);
00086 }    
00087 
00088 void
00089 test_init()
00090 {
00091     IIIMF_status st;
00092     IIIMCF_attr attr;
00093 
00094     st = iiimcf_initialize(IIIMCF_ATTR_NULL);
00095     if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00096     st = iiimcf_create_attr(&attr);
00097     if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00098     st = iiimcf_attr_put_string_value(attr, IIIMCF_ATTR_CLIENT_TYPE,
00099                                   "IIIMCF basic test");
00100     if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00101     st = iiimcf_create_handle(attr, &handle);
00102     if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00103     st = iiimcf_destroy_attr(attr);
00104     if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00105 }
00106 
00107 void
00108 test_show_objects()
00109 {
00110     IIIMF_status st;
00111     int i, n;
00112     const IIIMP_card16 *pu16;
00113     const IIIMCF_object_descriptor *pdesc;
00114     IIIMCF_downloaded_object objs[1];
00115     char *domain, *hrn, *signature, *user, *objtype, *filename;
00116 
00117     st = iiimcf_get_object_descriptor_list(handle, &n, &pdesc);
00118     if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00119 
00120     for (i = 0; i < n; i++, pdesc++) {
00121        domain = test_format_iiimcf_string(pdesc->domain);
00122        hrn = test_format_iiimcf_string(pdesc->hrn);
00123        signature = test_format_iiimcf_string(pdesc->signature);
00124        user = test_format_iiimcf_string(pdesc->user);
00125        filename = NULL;
00126 
00127        objtype = NULL;
00128        switch (pdesc->predefined_id) {
00129          case IIIMP_IMATTRIBUTE_CHARACTER_CONVERSION_DEFINITION_RULE:
00130           objtype = "CCDEF";
00131           break;
00132 
00133          case IIIMP_IMATTRIBUTE_BINARY_GUI_OBJECT:
00134           objtype = "Binary GUI";
00135          case IIIMP_IMATTRIBUTE_BINARY_LIGHT_WEIGHT_ENGINE_OBJECT:
00136           if (!objtype) objtype = "Binary LWE";
00137          case IIIMP_IMATTRIBUTE_SCRIPT_GUI_OBJECT:
00138           if (!objtype) objtype = "Script GUI";
00139          case IIIMP_IMATTRIBUTE_SCRIPT_LIGHT_WEIGHT_ENGINE_OBJECT:
00140           if (!objtype) objtype = "Script LWE";
00141 
00142           st =  iiimcf_get_downloaded_objects(handle, 1, &pdesc, objs);
00143           if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00144           st = iiimcf_get_downloaded_object_filename(*objs, &pu16);
00145           if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00146           filename = test_format_iiimcf_string(pu16);
00147           break;
00148 
00149          case IIIMP_IMATTRIBUTE_JAR_GUI_OBJECT:
00150           objtype = "Jar GUI";
00151           break;
00152          case IIIMP_IMATTRIBUTE_JAR_LIGHT_WEIGHT_ENGINE_OBJECT:
00153           objtype = "Jar LWE";
00154           break;
00155          default:
00156           objtype = "Unknown";
00157           break;
00158        }
00159 
00160        fprintf(stderr,
00161               "Object %d ----------------------------------\n"
00162               "  category: %d\n"
00163               "  size: %d\n"
00164               "  objtype type: %s(%d)\n"
00165               "  dynamic_id: %d\n"
00166               "  reverse domain name: %s\n"
00167               "  HRN: %s\n"
00168               "  signature: %s\n"
00169               "  user: %s\n"
00170               "  filename: %s\n"
00171               "--------------------------------------------\n",
00172               i,
00173               pdesc->category,
00174               pdesc->size,
00175               objtype, pdesc->predefined_id,
00176               pdesc->dynamic_id,
00177               domain,
00178               hrn,
00179               signature,
00180               user,
00181               filename ? filename : "");
00182 
00183        free(domain);
00184        free(hrn);
00185        free(signature);
00186        free(user);
00187        if (filename) free(filename);
00188     }
00189 }
00190 
00191 void
00192 test_show_input_methods()
00193 {
00194     IIIMF_status st;
00195     int i, n, j, nlangs;
00196     IIIMCF_input_method *pims;
00197     const IIIMP_card16 *u16idname, *u16hrn, *u16domain;
00198     char *idname, *hrn, *domain;
00199     const char *langid;
00200     IIIMCF_language *plangs;
00201 
00202     st = iiimcf_get_supported_input_methods(handle, &n, &pims);
00203     if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00204 
00205     if (n == 0) {
00206        fprintf(stderr, "The server offers no INPUTMETHOD_DESCRIPTOR.\n");
00207        return;
00208     }
00209 
00210     for (i = 0; i < n; i++) {
00211        check(iiimcf_get_input_method_desc(pims[i], &u16idname, &u16hrn, &u16domain));
00212        check(iiimcf_get_input_method_languages(pims[i], &nlangs, &plangs));
00213 
00214        idname = test_format_iiimcf_string(u16idname);
00215        hrn = test_format_iiimcf_string(u16hrn);
00216        domain = test_format_iiimcf_string(u16domain);
00217 
00218        fprintf(stderr,
00219               "Input Method %d ----------------------------------\n"
00220               "  idname: %s\n"
00221               "  HRN: %s\n"
00222               "  domain: %s\n"
00223               "  langs: ",
00224               i, idname, hrn, domain);
00225 
00226        for (j = 0; j < nlangs; j++) {
00227            check(iiimcf_get_language_id(plangs[j], &langid));
00228            fprintf(stderr, "%s, ", langid);
00229        }
00230        fprintf(stderr,      "\n--------------------------------------------\n");
00231 
00232        free(idname);
00233        free(hrn);
00234        free(domain);
00235     }
00236 }
00237 
00238 
00239 void
00240 test_uninit()
00241 {
00242     IIIMF_status st;
00243 
00244     st = iiimcf_destroy_handle(handle);
00245     if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00246     st = iiimcf_finalize();
00247     if (st != IIIMF_STATUS_SUCCESS) test_iiimf_error(st);
00248 }
00249 
00250 void
00251 test_forwardevent(
00252     IIIMCF_context c,
00253     IIIMCF_event ev
00254 )
00255 {
00256     IIIMF_status st;
00257 
00258     st = iiimcf_forward_event(c, ev);
00259     if (st == IIIMF_STATUS_SUCCESS) {
00260        fprintf(stderr, "--- successfully forwarded.\n");
00261     } else if (st == IIIMF_STATUS_EVENT_NOT_FORWARDED) {
00262        fprintf(stderr, "--- not forwarded.\n");
00263     } else {
00264        test_iiimf_error(st);
00265     }
00266 }
00267 
00268 void
00269 test_forwardkey(
00270     IIIMCF_context c,
00271     int keycode,
00272     int keychar,
00273     int mod
00274 )
00275 {
00276     IIIMCF_event ev;
00277     IIIMCF_keyevent kev;
00278 
00279     kev.keycode = keycode;
00280     kev.keychar = keychar;
00281     kev.modifier = mod;
00282     kev.time_stamp = time(NULL);
00283 
00284     fprintf(stderr, "forward keyevent (%02X, %02X, %02X) ",
00285            keycode, keychar, mod);
00286     check(iiimcf_create_keyevent(&kev, &ev));
00287     test_forwardevent(c, ev);
00288 }
00289 
00290 void
00291 test_seticfocus(
00292     IIIMCF_context c
00293 )
00294 {
00295     IIIMCF_event ev;
00296 
00297     check(iiimcf_create_seticfocus_event(&ev));
00298     fprintf(stderr, "forward SETICFOCUS ");
00299     test_forwardevent(c, ev);
00300 }
00301 
00302 void
00303 test_unseticfocus(
00304     IIIMCF_context c
00305 )
00306 {
00307     IIIMCF_event ev;
00308 
00309     check(iiimcf_create_unseticfocus_event(&ev));
00310     fprintf(stderr, "forward UNSETICFOCUS ");
00311     test_forwardevent(c, ev);
00312 }
00313 
00314 void
00315 test_trigger(
00316     IIIMCF_context c,
00317     int flag
00318 )
00319 {
00320     IIIMF_status st;
00321     IIIMCF_event ev;
00322 
00323     fprintf(stderr, "forward trigger notify event (%d) ", flag);
00324     check(iiimcf_create_trigger_notify_event(flag, &ev));
00325     st = iiimcf_forward_event(c, ev);
00326     if (st == IIIMF_STATUS_SUCCESS) {
00327        fprintf(stderr, "--- successfully forwarded.\n");
00328     } else if (st == IIIMF_STATUS_EVENT_NOT_FORWARDED) {
00329        fprintf(stderr, "--- not forwarded.\n");
00330     } else {
00331        test_iiimf_error(st);
00332     }
00333 }
00334 
00335 char*
00336 test_format_iiimcf_text(
00337     IIIMCF_text t
00338 )
00339 {
00340     char *p, *pr, *pr2;
00341     int i, j, n, l, rest;
00342     int initlen = 256;
00343     IIIMP_card16 ch;
00344     int nfb;
00345     const IIIMP_card32 *pids, *pfbs;
00346 
00347     pr = p = (char*) malloc(sizeof(*p) * initlen);
00348     rest = initlen - 1;
00349 
00350     check(iiimcf_get_text_length(t, &n));
00351 
00352     for (i = 0; i < n; i++) {
00353        check(iiimcf_get_char_with_feedback(t, i, &ch, &nfb, &pids, &pfbs));
00354        if (rest <= (16 + 2 + 20 * nfb)) {
00355            rest += initlen;
00356            initlen *= 2;
00357            pr2 = (char*) realloc(pr, sizeof(*p) * initlen);
00358            p = pr2 + (p - pr);
00359            pr = pr2;
00360        }
00361        if (ch < 128) {
00362            *p++ = ch;
00363            rest--;
00364        } else {
00365            l = snprintf(p, 15, " U+%04X", ch);
00366            p += l;
00367            rest -= l;
00368        }
00369        if (nfb > 0) {
00370            *p++ = '[';
00371            rest--;
00372            for (j = 0; j < nfb; j++, pids++, pfbs++) {
00373               l = snprintf(p, 20, "(%d,%d)", *pids, *pfbs);
00374               p += l;
00375               rest -= l;
00376            }
00377            rest--;
00378            *p++ = ']';
00379        }
00380     }
00381     *p = '\0';
00382 
00383     return pr;
00384 }
00385 
00386 void
00387 test_show_preedit_info(
00388     IIIMCF_context c
00389 )
00390 {
00391     int cpos;
00392     IIIMF_status st;
00393     IIIMCF_text text;
00394     
00395     st = iiimcf_get_preedit_text(c, &text, &cpos);
00396     if (st == IIIMF_STATUS_SUCCESS) {
00397        char *str;
00398 
00399        str = test_format_iiimcf_text(text);
00400        fprintf(stderr, "Preedit(%d):%s\n", cpos, str);
00401        free(str);
00402     } else if (st == IIIMF_STATUS_NO_PREEDIT) {
00403        fprintf(stderr, "Preedit is disabled.\n");
00404     } else {
00405        test_iiimf_error(st);
00406     }
00407 }
00408 
00409 void
00410 test_show_status_info(
00411     IIIMCF_context c
00412 )
00413 {
00414     IIIMF_status st;
00415     IIIMCF_text text;
00416     
00417     st = iiimcf_get_status_text(c, &text);
00418     if (st == IIIMF_STATUS_SUCCESS) {
00419        char *str;
00420 
00421        str = test_format_iiimcf_text(text);
00422        fprintf(stderr, "Status:%s\n", str);
00423        free(str);
00424     } else if (st == IIIMF_STATUS_NO_STATUS_TEXT) {
00425        fprintf(stderr, "Status is disabled.\n");
00426     } else {
00427        test_iiimf_error(st);
00428     }
00429 }
00430 
00431 void
00432 test_show_lookup_choice_info(
00433     IIIMCF_context c
00434 )
00435 {
00436     IIIMF_status st;
00437     IIIMCF_lookup_choice ilc;
00438 
00439     st = iiimcf_get_lookup_choice(c, &ilc);
00440     if (st == IIIMF_STATUS_SUCCESS) {
00441        char *itemstr, *labelstr;
00442        int i, flag;
00443        int size, idx_first, idx_last, idx_current;
00444        IIIMCF_text cand, label;
00445 
00446        check(iiimcf_get_lookup_choice_size(ilc, &size,
00447                                        &idx_first,
00448                                        &idx_last,
00449                                        &idx_current));
00450 
00451        for (i = 0; i < size; i++) {
00452            check(iiimcf_get_lookup_choice_item(ilc, i, &cand, &label, &flag));
00453            if (flag & IIIMCF_LOOKUP_CHOICE_ITEM_ENABLED) {
00454               itemstr = test_format_iiimcf_text(cand);
00455               labelstr = test_format_iiimcf_text(label);
00456               fprintf(stderr, "LC[%d](%s): %s\n", i, labelstr, itemstr);
00457               free(labelstr);
00458               free(itemstr);
00459            }
00460        }
00461     } else if (st == IIIMF_STATUS_NO_LOOKUP_CHOICE) {
00462        fprintf(stderr, "Lookup choice is disabled.\n");
00463     } else {
00464        test_iiimf_error(st);
00465     }
00466 }
00467 
00468 void
00469 test_show_commit_info(
00470     IIIMCF_context c
00471 )
00472 {
00473     IIIMF_status st;
00474     IIIMCF_text text;
00475     
00476     st = iiimcf_get_committed_text(c, &text);
00477     if (st == IIIMF_STATUS_SUCCESS) {
00478        char *str;
00479 
00480        str = test_format_iiimcf_text(text);
00481        fprintf(stderr, "Committed text:%s\n", str);
00482        free(str);
00483     } else if (st == IIIMF_STATUS_NO_COMMITTED_TEXT) {
00484        fprintf(stderr, "No committed text.\n");
00485     } else {
00486        test_iiimf_error(st);
00487     }
00488 }
00489 
00490 void
00491 test_show_version_info(
00492     IIIMCF_handle h
00493 )
00494 {
00495     IIIMF_status st;
00496     int v, lib_major, lib_minor, proto_major, proto_minor;
00497 
00498     check(iiimcf_get_version_number(h, IIIMCF_LIBRARY_VERSION | IIIMCF_MAJOR_VERSION,
00499                                 &lib_major));
00500     check(iiimcf_get_version_number(h, IIIMCF_LIBRARY_VERSION | IIIMCF_MINOR_VERSION,
00501                                 &lib_minor));
00502 
00503     check(iiimcf_get_version_number(h, IIIMCF_PROTOCOL_VERSION | IIIMCF_MAJOR_VERSION,
00504                                 &proto_major));
00505     check(iiimcf_get_version_number(h, IIIMCF_PROTOCOL_VERSION | IIIMCF_MINOR_VERSION,
00506                                 &proto_minor));
00507 
00508     fprintf(stderr, "Version: LIB:%d.%d, PROTOCOL:%d.%d\n",
00509            lib_major, lib_minor,
00510            proto_major, proto_minor);
00511 
00512     st = iiimcf_get_version_number(h, IIIMCF_PROTOCOL_VERSION, &v);
00513     test_assert(st == IIIMF_STATUS_ARGUMENT);
00514     st = iiimcf_get_version_number(h, IIIMCF_LIBRARY_VERSION, &v);
00515     test_assert(st == IIIMF_STATUS_ARGUMENT);
00516     st = iiimcf_get_version_number(h, IIIMCF_MAJOR_VERSION, &v);
00517     test_assert(st == IIIMF_STATUS_ARGUMENT);
00518     st = iiimcf_get_version_number(h, IIIMCF_MINOR_VERSION, &v);
00519     test_assert(st == IIIMF_STATUS_ARGUMENT);
00520 }
00521 
00522 static void
00523 show_aux(
00524     const char *name,
00525     IIIMCF_event ev
00526 )
00527 {
00528     int i;
00529     char *str;
00530     const IIIMP_card16 *aux_name;
00531     IIIMP_card32 idx;
00532     int num_intvals, num_strvals;
00533     const IIIMP_card32 *pints;
00534     const IIIMP_card16 **pstrs;
00535        
00536     check(iiimcf_get_aux_event_value(ev, &aux_name, &idx,
00537                                  &num_intvals, &pints,
00538                                  &num_strvals, &pstrs));
00539     str = test_format_iiimcf_string(aux_name);
00540     fprintf(stderr, "Event[%s/%s:%d]: ", name, str, idx);
00541     free(str);
00542     if (num_intvals > 0) {
00543        fprintf(stderr, "(%d", *pints);
00544        for (i = 1; i < num_intvals; i++) {
00545            fprintf(stderr, ",%d", pints[i]);
00546        }
00547        fprintf(stderr, ")");
00548     }
00549     if (num_strvals > 0) {
00550        str = test_format_iiimcf_string(*pstrs);
00551        fprintf(stderr, "{%s", str);
00552        free(str);
00553        for (i = 1; i < num_strvals; i++) {
00554            str = test_format_iiimcf_string(pstrs[i]);
00555            fprintf(stderr, ",%s", str);
00556            free(str);
00557        }
00558        fprintf(stderr, "}");
00559     }
00560     fprintf(stderr, "\n");
00561 }
00562 
00563 void
00564 test_show_incoming_events(
00565     IIIMCF_context c,
00566     int dispatchp
00567 )
00568 {
00569     IIIMF_status st;
00570     IIIMCF_event ev;
00571     IIIMCF_event_type et;
00572 
00573     while ((st = iiimcf_get_next_event(c, &ev))
00574           == IIIMF_STATUS_SUCCESS) {
00575        check(iiimcf_get_event_type(ev, &et));
00576        switch (et) {
00577          case IIIMCF_EVENT_TYPE_KEYEVENT:
00578          {
00579              IIIMCF_keyevent kev;
00580 
00581              check(iiimcf_get_keyevent_value(ev, &kev));
00582              fprintf(stderr, "Event[KEY]: %d, %d, %d, %d\n",
00583                     kev.keycode, kev.keychar, kev.modifier, kev.time_stamp);
00584              break;
00585          }
00586          case IIIMCF_EVENT_TYPE_TRIGGER_NOTIFY:
00587          {
00588              int flag;
00589 
00590              check(iiimcf_get_trigger_notify_flag(ev, &flag));
00591              fprintf(stderr, "Event[TRIGGER-NOTIFY]: %d\n", flag);
00592              break;
00593          }
00594          case IIIMCF_EVENT_TYPE_UI_PREEDIT_START:
00595           fprintf(stderr, "Event[PREEDIT-START]\n");
00596           break;
00597          case IIIMCF_EVENT_TYPE_UI_PREEDIT_CHANGE:
00598           fprintf(stderr, "Event[PREEDIT-CHANGE]\n");
00599           break;
00600          case IIIMCF_EVENT_TYPE_UI_PREEDIT_DONE:
00601           fprintf(stderr, "Event[PREEDIT-DONE]\n");
00602           break;
00603 
00604          case IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_START:
00605           fprintf(stderr, "Event[LOOKUP-CHOICE-START]\n");
00606           break;
00607          case IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_CHANGE:
00608           fprintf(stderr, "Event[LOOKUP-CHOICE-CHANGE]\n");
00609           break;
00610          case IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_DONE:
00611           fprintf(stderr, "Event[LOOKUP-CHOICE-DONE]\n");
00612           break;
00613 
00614          case IIIMCF_EVENT_TYPE_UI_STATUS_START:
00615           fprintf(stderr, "Event[STATUS-START]\n");
00616           break;
00617          case IIIMCF_EVENT_TYPE_UI_STATUS_CHANGE:
00618           fprintf(stderr, "Event[STATUS-CHANGE]\n");
00619           break;
00620          case IIIMCF_EVENT_TYPE_UI_STATUS_DONE:
00621           fprintf(stderr, "Event[STATUS-DONE]\n");
00622           break;
00623 
00624          case IIIMCF_EVENT_TYPE_UI_COMMIT:
00625           fprintf(stderr, "Event[COMMIT]\n");
00626           break;
00627 
00628          case IIIMCF_EVENT_TYPE_AUX_START:
00629           show_aux("AUX-START", ev);
00630           break;
00631 
00632          case IIIMCF_EVENT_TYPE_AUX_DRAW:
00633           show_aux("AUX-DRAW", ev);
00634           break;
00635          case IIIMCF_EVENT_TYPE_AUX_SETVALUES:
00636           show_aux("AUX-SETVALUES", ev);
00637           break;
00638          case IIIMCF_EVENT_TYPE_AUX_DONE:
00639           show_aux("AUX-DONE", ev);
00640           break;
00641           
00642          default:
00643           fprintf(stderr, "Event[Unknown:%d]\n", et);
00644           break;
00645        }
00646        if (dispatchp) {
00647            st = iiimcf_dispatch_event(c, ev);
00648            if (st != IIIMF_STATUS_SUCCESS) {
00649               if (st == IIIMF_STATUS_COMPONENT_FAIL) {
00650                   fprintf(stderr, "at least one component reported failure.\n");
00651               } else if (st == IIIMF_STATUS_COMPONENT_INDIFFERENT) {
00652                   fprintf(stderr, "none of the components deal with the event.\n");
00653               } else {
00654                   fprintf(stderr, "fail to dispatch");
00655                   test_iiimf_error(st);
00656               }
00657            }
00658        }
00659        check(iiimcf_ignore_event(ev));
00660     }
00661     if (st != IIIMF_STATUS_NO_EVENT) {
00662        test_iiimf_error(st);
00663     }
00664 
00665 }
00666 
00667 /* Local Variables: */
00668 /* c-file-style: "iiim-project" */
00669 /* End: */
00670