Back to index

im-sdk  12.3.91
component.c
Go to the documentation of this file.
00001 /*
00002   component.c
00003 */
00004 
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include "iiimcfint.h"
00008 
00009 /********************************************************************************
00010                           Predefined Component
00011 ********************************************************************************/
00012 
00013 static IIIMF_status
00014 iiimcf_component_UI(
00015     IIIMCF_context context,
00016     IIIMCF_event event,
00017     IIIMCF_component current,
00018     IIIMCF_component parent
00019 )
00020 {
00021     IIIMF_status st;
00022     IIIMCF_event_type type;
00023 
00024     st = iiimcf_get_event_type(event, &type);
00025     if (st != IIIMF_STATUS_SUCCESS) return st;
00026        
00027     if ((type >= IIIMCF_EVENT_TYPE_UI)
00028        && (type < IIIMCF_EVENT_TYPE_UI_END))
00029        return iiimcf_dispatch_event(context, event);
00030 
00031     return IIIMF_STATUS_COMPONENT_INDIFFERENT;
00032 }
00033 
00034 static IIIMF_status
00035 iiimcf_component_UI_preedit(
00036     IIIMCF_context context,
00037     IIIMCF_event event,
00038     IIIMCF_component current,
00039     IIIMCF_component parent
00040 )
00041 {
00042     IIIMF_status st;
00043     IIIMCF_event_type type;
00044 
00045     st = iiimcf_get_event_type(event, &type);
00046     if (st != IIIMF_STATUS_SUCCESS) return st;
00047        
00048     if ((type >= IIIMCF_EVENT_TYPE_UI_PREEDIT)
00049        && (type < IIIMCF_EVENT_TYPE_UI_PREEDIT_END))
00050        return iiimcf_dispatch_event(context, event);
00051 
00052     return IIIMF_STATUS_COMPONENT_INDIFFERENT;
00053 }
00054 
00055 static IIIMF_status
00056 iiimcf_component_UI_status(
00057     IIIMCF_context context,
00058     IIIMCF_event event,
00059     IIIMCF_component current,
00060     IIIMCF_component parent
00061 )
00062 {
00063     IIIMF_status st;
00064     IIIMCF_event_type type;
00065 
00066     st = iiimcf_get_event_type(event, &type);
00067     if (st != IIIMF_STATUS_SUCCESS) return st;
00068        
00069     if ((type >= IIIMCF_EVENT_TYPE_UI_STATUS)
00070        && (type < IIIMCF_EVENT_TYPE_UI_STATUS_END))
00071        return iiimcf_dispatch_event(context, event);
00072 
00073     return IIIMF_STATUS_COMPONENT_INDIFFERENT;
00074 }
00075 
00076 static IIIMF_status
00077 iiimcf_component_UI_lookup_choice(
00078     IIIMCF_context context,
00079     IIIMCF_event event,
00080     IIIMCF_component current,
00081     IIIMCF_component parent
00082 )
00083 {
00084     IIIMF_status st;
00085     IIIMCF_event_type type;
00086 
00087     st = iiimcf_get_event_type(event, &type);
00088     if (st != IIIMF_STATUS_SUCCESS) return st;
00089        
00090     if ((type >= IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE)
00091        && (type < IIIMCF_EVENT_TYPE_UI_LOOKUP_CHOICE_END))
00092        return iiimcf_dispatch_event(context, event);
00093 
00094     return IIIMF_STATUS_COMPONENT_INDIFFERENT;
00095 }
00096 
00097 static IIIMF_status
00098 iiimcf_component_UI_commit(
00099     IIIMCF_context context,
00100     IIIMCF_event event,
00101     IIIMCF_component current,
00102     IIIMCF_component parent
00103 )
00104 {
00105     IIIMF_status st;
00106     IIIMCF_event_type type;
00107 
00108     st = iiimcf_get_event_type(event, &type);
00109     if (st != IIIMF_STATUS_SUCCESS) return st;
00110        
00111     if ((type >= IIIMCF_EVENT_TYPE_UI_COMMIT)
00112        && (type < IIIMCF_EVENT_TYPE_UI_COMMIT_END))
00113        return iiimcf_dispatch_event(context, event);
00114 
00115     return IIIMF_STATUS_COMPONENT_INDIFFERENT;
00116 }
00117 
00118 static IIIMF_status
00119 iiimcf_component_event(
00120     IIIMCF_context context,
00121     IIIMCF_event event,
00122     IIIMCF_component current,
00123     IIIMCF_component parent
00124 )
00125 {
00126     IIIMF_status st;
00127     IIIMCF_event_type type;
00128 
00129     st = iiimcf_get_event_type(event, &type);
00130     if (st != IIIMF_STATUS_SUCCESS) return st;
00131        
00132     if ((type >= IIIMCF_EVENT_TYPE_EVENTLIKE)
00133        && (type < IIIMCF_EVENT_TYPE_EVENTLIKE_END))
00134        return iiimcf_dispatch_event(context, event);
00135 
00136     return IIIMF_STATUS_COMPONENT_INDIFFERENT;
00137 }
00138 
00139 static IIIMF_status
00140 iiimcf_component_event_key(
00141     IIIMCF_context context,
00142     IIIMCF_event event,
00143     IIIMCF_component current,
00144     IIIMCF_component parent
00145 )
00146 {
00147     IIIMF_status st;
00148     IIIMCF_event_type type;
00149 
00150     st = iiimcf_get_event_type(event, &type);
00151     if ((type >= IIIMCF_EVENT_TYPE_KEYEVENT)
00152        && (type < IIIMCF_EVENT_TYPE_KEYEVENT_END))
00153        return iiimcf_dispatch_event(context, event);
00154 
00155     return IIIMF_STATUS_COMPONENT_INDIFFERENT;
00156 }
00157 
00158 static IIIMF_status
00159 iiimcf_component_event_trigger_notify(
00160     IIIMCF_context context,
00161     IIIMCF_event event,
00162     IIIMCF_component current,
00163     IIIMCF_component parent
00164 )
00165 {
00166     IIIMF_status st;
00167     IIIMCF_event_type type;
00168 
00169     st = iiimcf_get_event_type(event, &type);
00170     if ((type >= IIIMCF_EVENT_TYPE_TRIGGER_NOTIFY)
00171        && (type < IIIMCF_EVENT_TYPE_TRIGGER_NOTIFY_END))
00172        return iiimcf_dispatch_event(context, event);
00173 
00174     return IIIMF_STATUS_COMPONENT_INDIFFERENT;
00175 }
00176 
00177 static IIIMF_status
00178 iiimcf_component_AUX(
00179     IIIMCF_context context,
00180     IIIMCF_event event,
00181     IIIMCF_component current,
00182     IIIMCF_component parent
00183 )
00184 {
00185     IIIMF_status st;
00186     IIIMCF_event_type type;
00187 
00188     st = iiimcf_get_event_type(event, &type);
00189     if ((type >= IIIMCF_EVENT_TYPE_AUX)
00190        && (type < IIIMCF_EVENT_TYPE_AUX_END))
00191        return iiimcf_dispatch_event(context, event);
00192 
00193     return IIIMF_STATUS_COMPONENT_INDIFFERENT;
00194 }
00195 
00196 /********************************************************************************
00197                            Internal functions.
00198 ********************************************************************************/
00199 
00200 static IIIMCF_component_rec*
00201 lookup_component(
00202     const char *name,
00203     IIIMCF_component_rec *pc
00204 )
00205 {
00206     IIIMCF_component_rec *pc2;
00207 
00208     for (; pc; pc = pc->pnext) {
00209        if (strcmp(pc->name, name) == 0) return pc;
00210        pc2 = lookup_component(name, pc->pchild);
00211        if (pc2) return pc2;
00212     }
00213 
00214     return NULL;
00215 }
00216 
00217 typedef struct IIIMCF_component_tree IIIMCF_component_tree;
00218 struct IIIMCF_component_tree {
00219     char *name;
00220     IIIMCF_component_interface pint;
00221     IIIMCF_component_tree *pdec;
00222 };
00223 static IIIMCF_component_tree predefined_root[];
00224 static IIIMCF_component_tree predefined_UI[];
00225 static IIIMCF_component_tree predefined_event[];
00226 
00227 static IIIMCF_component_tree predefined_root[] = {
00228        {"org.OpenI18N.IIIMCF.UI", iiimcf_component_UI, predefined_UI},
00229        {"org.OpenI18N.IIIMCF.event", iiimcf_component_event, predefined_event},
00230        {"org.OpenI18N.IIIMCF.AUX", iiimcf_component_AUX, NULL},
00231        {NULL, NULL, NULL}
00232 };
00233 static IIIMCF_component_tree predefined_UI[] = {
00234        {"org.OpenI18N.IIIMCF.UI.preedit", iiimcf_component_UI_preedit, NULL},
00235        {"org.OpenI18N.IIIMCF.UI.status", iiimcf_component_UI_status, NULL},
00236        {"org.OpenI18N.IIIMCF.UI.lookup_choice", iiimcf_component_UI_lookup_choice, NULL},
00237        {NULL, NULL, NULL}
00238 };
00239 static IIIMCF_component_tree predefined_event[] = {
00240        {"org.OpenI18N.IIIMCF.event.key", iiimcf_component_event_key, NULL},
00241        {"org.OpenI18N.IIIMCF.event.trigger_notify", iiimcf_component_event_trigger_notify, NULL},
00242        {NULL, NULL, NULL}
00243 };
00244 
00245 static IIIMF_status
00246 register_component_tree(
00247     IIIMCF_handle_rec *ph,
00248     IIIMCF_component parent_comp,
00249     IIIMCF_component_tree *pt
00250 )
00251 {
00252     IIIMF_status st;
00253     IIIMCF_component comp;
00254 
00255     for (; pt->name; pt++) {
00256        st = iiimcf_register_component((IIIMCF_handle) ph,
00257                                    pt->name,
00258                                    pt->pint,
00259                                    parent_comp,
00260                                    &comp);
00261        if (st != IIIMF_STATUS_SUCCESS) return st;
00262        if (pt->pdec) {
00263            st = register_component_tree(ph, comp, pt->pdec);
00264            if (st != IIIMF_STATUS_SUCCESS) return st;
00265        }
00266     }
00267 
00268     return IIIMF_STATUS_SUCCESS;
00269 }
00270 
00271 IIIMF_status
00272 iiimcf_register_predefined_components(
00273     IIIMCF_handle_rec *ph
00274 )
00275 {
00276     return register_component_tree(ph, IIIMCF_ROOT_COMPONENT, predefined_root);
00277 }
00278 
00279 void
00280 iiimcf_delete_all_components(
00281     IIIMCF_handle_rec *ph
00282 )
00283 {
00284     IIIMCF_component_rec *pcom, *pcom2;
00285 
00286     pcom = ph->proot_component;
00287     while (pcom) {
00288        pcom2 = pcom->pnext;
00289        iiimcf_unregister_component(pcom);
00290        pcom = pcom2;
00291     }
00292     ph->proot_component = NULL;
00293 }
00294 
00295 /********************************************************************************
00296                                  APIs
00297 ********************************************************************************/
00298 
00299 IIIMF_status
00300 iiimcf_register_component(
00301     IIIMCF_handle handle,
00302     const char* name,
00303     IIIMCF_component_interface interface,
00304     IIIMCF_component parent,
00305     IIIMCF_component* pcomp
00306 )
00307 {
00308     IIIMCF_handle_rec *ph = (IIIMCF_handle_rec*) handle;
00309     IIIMCF_component_rec *pcom_parent = (IIIMCF_component_rec*) parent;
00310     IIIMCF_component_rec *pc;
00311 
00312     pc = lookup_component(name, ph->proot_component);
00313     if (pc) return IIIMF_STATUS_COMPONENT_DUPLICATED_NAME;
00314 
00315     pc = (IIIMCF_component_rec*) malloc(sizeof(*pc));
00316     if (!pc) return IIIMF_STATUS_MALLOC;
00317     memset(pc, 0, sizeof(*pc));
00318     pc->name = strdup(name);
00319     if (!pc->name) {
00320        free(pc);
00321        return IIIMF_STATUS_MALLOC;
00322     }
00323 
00324     pc->ph = ph;
00325     pc->func = interface;
00326     pc->pparent = pcom_parent;
00327     if (pcom_parent) {
00328        pc->pnext = pcom_parent->pchild;
00329        pcom_parent->pchild = pc;
00330     } else {
00331        pc->pnext = ph->proot_component;
00332        ph->proot_component = pc;
00333     }
00334     if (pc->pnext) {
00335        pc->pnext->pprev = pc;
00336     }
00337 
00338     *pcomp = pc;
00339 
00340     return IIIMF_STATUS_SUCCESS;
00341 }
00342 
00343 IIIMF_status
00344 iiimcf_unregister_component(
00345     IIIMCF_component component
00346 )
00347 {
00348     IIIMCF_component_rec *pc = (IIIMCF_component_rec*) component;
00349     IIIMCF_component_rec *pc2, *pc3;
00350 
00351     for (pc2 = pc->pchild; pc2; ) {
00352        pc3 = pc2->pnext;
00353        if (pc2->pchild) {
00354            iiimcf_unregister_component(pc2);
00355        } else {
00356            free(pc2->name);
00357            if (pc2->attr) iiimcf_destroy_attr(pc2->attr);
00358            free(pc2);
00359        }
00360        pc2 = pc3;
00361     }
00362     if (pc->attr) iiimcf_destroy_attr(pc->attr);
00363     if (pc->pprev) {
00364        pc->pprev->pnext = pc->pnext;
00365     }
00366     if (pc->pnext) {
00367        pc->pnext->pprev = pc->pprev;
00368     }
00369 
00370     free(pc->name);
00371     free(pc);
00372 
00373     return IIIMF_STATUS_SUCCESS;
00374 }
00375 
00376 IIIMF_status
00377 iiimcf_get_component(
00378     IIIMCF_handle handle,
00379     const char *name,
00380     IIIMCF_component* pcomponent
00381 )
00382 {
00383     IIIMCF_handle_rec *ph = (IIIMCF_handle_rec*) handle;
00384     IIIMCF_component_rec *pc = ph->proot_component;
00385 
00386     pc = lookup_component(name, pc);
00387     if (!pc) return IIIMF_STATUS_COMPONENT_DUPLICATED_NAME;
00388 
00389     *pcomponent = pc;
00390 
00391     return IIIMF_STATUS_SUCCESS;
00392 }
00393 
00394 IIIMF_status
00395 iiimcf_component_get_attr(
00396     IIIMCF_component component,
00397     IIIMCF_attr* pattr
00398 )
00399 {
00400     IIIMF_status st;
00401     IIIMCF_component_rec *pc = (IIIMCF_component_rec*) component;
00402 
00403     if (pc->attr) {
00404        *pattr = pc->attr;
00405        return IIIMF_STATUS_SUCCESS;
00406     }
00407     st = iiimcf_create_attr(pattr);
00408     if (st != IIIMF_STATUS_SUCCESS) return st;
00409     pc->attr = *pattr;
00410 
00411     return IIIMF_STATUS_SUCCESS;
00412 }
00413 
00414 /* Local Variables: */
00415 /* c-file-style: "iiim-project" */
00416 /* End: */
00417