Back to index

im-sdk  12.3.91
hotkey_config.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 "unit_input.h"
00043 
00044 IMKeyEventStruct *parseKey(xmlDocPtr, xmlNodePtr);
00045 IMHotkeyStruct *parseHotkey(xmlDocPtr, xmlNodePtr);
00046 IMHotkeyProfileStruct *parseHotkeyProfile(xmlDocPtr, xmlNodePtr);
00047 IMHotkeyManagerStruct *parse_hotkey_config_file(IMLEName *, char *);
00048 void printHotkeyManager(IMHotkeyManagerStruct *);
00049 
00050 extern int get_key_value(char *);
00051 
00052 IMKeyEventStruct * 
00053 parseKey(
00054     xmlDocPtr doc,
00055     xmlNodePtr cur
00056 )
00057 {
00058    IMKeyEventStruct *key;
00059 
00060    key = (IMKeyEventStruct *) calloc (1, sizeof(IMKeyEventStruct));
00061    if (key == NULL) {
00062      printf("parseKey: calloc error \n");
00063      return (NULL);
00064    }
00065 
00066    cur = cur->xmlChildrenNode;
00067    while (cur != NULL) {
00068      if (!xmlStrcmp(cur->name, (const xmlChar *)"text") || 
00069                    !xmlStrcmp(cur->name, (const xmlChar *)"comment")) {
00070        cur = cur->next;
00071      }
00072      if (!xmlStrcmp(cur->name, (const xmlChar *)"keycode")) {
00073        char *kc = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00074        if (kc && strstr(kc, "IM_")) {
00075          key->keyCode  = get_key_value(kc);
00076        } else {
00077          key->keyCode  = atoi(kc);
00078        }
00079        free(kc);
00080        cur = cur->next;
00081      }
00082      if (!xmlStrcmp(cur->name, (const xmlChar *)"modifier")) {
00083        char *mo = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00084        int modifier = 0;
00085        if (mo && strstr(mo, "IM_")) {
00086          if (strstr(mo, "IM_SHIFT_MASK")) {
00087            modifier += 1;
00088          } 
00089          if (strstr(mo, "IM_CTRL_MASK")) {
00090            modifier += 2;
00091          }  
00092          if (strstr(mo, "IM_META_MASK")) {
00093            modifier += 4;
00094          } 
00095          if (strstr(mo, "IM_ALT_MASK")) {
00096            modifier += 8;
00097          }
00098          if (strstr(mo, "IM_ALT_GRAPH_MASK")) {
00099            modifier += 32;
00100          }
00101          key->modifier = modifier;
00102        } else { 
00103           key->modifier = atoi(mo);
00104        }
00105        free(mo); 
00106        cur = cur->next;
00107      }
00108    }
00109    return key;
00110 }
00111 
00112 IMHotkeyStruct *
00113 parseHotkey(
00114    xmlDocPtr doc,
00115    xmlNodePtr cur
00116 )
00117 {
00118    IMHotkeyStruct *hk = NULL;
00119    IMKeyEventStruct *keys = NULL;
00120    int i;
00121 
00122    hk = (IMHotkeyStruct *) calloc (1, sizeof(IMHotkeyStruct));
00123    if (hk == NULL) {
00124      printf("parseHotkey: calloc error \n");
00125      return (NULL);
00126    }
00127 
00128    cur = cur->xmlChildrenNode;
00129    while (cur != NULL) {
00130      if (!xmlStrcmp(cur->name, (const xmlChar *)"text") || 
00131                    !xmlStrcmp(cur->name, (const xmlChar *)"comment")) {
00132        cur = cur->next;
00133      }
00134      if (!xmlStrcmp(cur->name, (const xmlChar *)"id")) {
00135        /*
00136        int hk_id = atoi(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00137        */
00138        cur = cur->next;
00139      }
00140      if (!xmlStrcmp(cur->name, (const xmlChar *)"state_flag")) {
00141        hk->state_flag = atoi(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00142        cur = cur->next;
00143      }
00144      if (!xmlStrcmp(cur->name, (const xmlChar *)"action_flag")) {
00145        hk->action_flag = atoi(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00146        cur = cur->next;
00147      }
00148      if (!xmlStrcmp(cur->name, (const xmlChar *)"label")) {
00149        hk->label = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00150        cur = cur->next;
00151      }
00152      if (!xmlStrcmp(cur->name, (const xmlChar *)"num_keys")) {
00153        hk->nkeys = atoi(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00154        hk->keys = (IMKeyEventStruct *) calloc(hk->nkeys, sizeof(IMKeyEventStruct));
00155        cur = cur->next;
00156      }
00157      if (!xmlStrcmp(cur->name, (const xmlChar *)"key")) {
00158        for (i=0; i<hk->nkeys; i++) {
00159          keys = parseKey(doc, cur);
00160          hk->keys[i].keyCode = keys->keyCode;
00161          hk->keys[i].modifier = keys->modifier;
00162          if (cur)
00163            cur = cur->next;
00164        }
00165      }
00166    }
00167    return hk;
00168 }
00169 
00170 IMHotkeyProfileStruct *
00171 parseHotkeyProfile(
00172    xmlDocPtr doc,
00173    xmlNodePtr cur
00174 )
00175 {
00176    IMHotkeyProfileStruct *hkp = NULL;
00177    IMHotkeyStruct *hk = NULL;
00178    int i;
00179 
00180    hkp = (IMHotkeyProfileStruct *) calloc (1, sizeof(IMHotkeyProfileStruct));
00181    if (hkp == NULL) {
00182      printf("parseHotkeyProfile: calloc error \n");
00183      return (NULL);
00184    }
00185 
00186    cur = cur->xmlChildrenNode;
00187 
00188    while (cur != NULL) {
00189      if (!xmlStrcmp(cur->name, (const xmlChar *)"id")) {
00190        hkp->profile_id = atoi(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00191        cur = cur->next;
00192      }
00193      if (!xmlStrcmp(cur->name, (const xmlChar *)"text") || 
00194                    !xmlStrcmp(cur->name, (const xmlChar *)"comment")) {
00195        cur = cur->next;
00196      }
00197      if (!xmlStrcmp(cur->name, (const xmlChar *)"scope")) {
00198        char *scope = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00199        if (scope && strstr(scope, "SESSION_SPECIFIC_HOTKEY")) {
00200          hkp->scope = 1;
00201        } else if (scope && strstr(scope, "GLOBAL_HOTKEY")) {
00202          hkp->scope = 0;
00203        } else
00204           hkp->scope  = atoi(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00205        free(scope);
00206        cur = cur->next;
00207      }
00208      if (!xmlStrcmp(cur->name, (const xmlChar *)"num_hotkeys")) {
00209        hkp->num_hotkeys = atoi(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00210        hkp->hks = (IMHotkeyStruct *) calloc(hkp->num_hotkeys, sizeof(IMHotkeyStruct));
00211        cur = cur->next;
00212      }
00213      if (!xmlStrcmp(cur->name, (const xmlChar *)"hotkey")) {
00214        for (i=0; i<hkp->num_hotkeys; i++) {
00215          hk = parseHotkey(doc, cur);
00216          hkp->hks[i].label = hk->label;
00217          hkp->hks[i].state_flag = hk->state_flag;
00218          hkp->hks[i].action_flag = hk->action_flag;
00219          hkp->hks[i].nkeys = hk->nkeys;
00220          hkp->hks[i].keys = hk->keys;
00221          if (cur)
00222            cur = cur->next;
00223        }
00224      }
00225    }
00226    return hkp;
00227 }
00228 
00229 IMHotkeyManagerStruct *
00230 parse_hotkey_config_file(
00231    IMLEName *le_name,
00232    char *filename
00233 ) 
00234 {
00235    IMHotkeyManagerStruct *hkm;
00236    IMHotkeyProfileStruct *hkp;
00237    xmlDocPtr doc;
00238    xmlNodePtr cur;
00239    int i;
00240 
00241    LIBXML_TEST_VERSION
00242 
00243    xmlKeepBlanksDefault(0);
00244 
00245    /*
00246     * build an XML tree from the configuration file
00247     */
00248 
00249    doc = xmlParseFile(filename);
00250    if (doc == NULL) return(NULL);
00251 
00252    /*
00253     * Check if the document is of the right kind
00254     */
00255    
00256    cur = xmlDocGetRootElement(doc);
00257    if (cur == NULL) {
00258      printf("Empty document ..\n");
00259      xmlFreeDoc(doc);
00260      return(NULL);
00261    }
00262 
00263    /*
00264     * Allocate the structure to be returned.
00265     */
00266 
00267    hkm = (IMHotkeyManagerStruct *) calloc(1, sizeof(IMHotkeyManagerStruct));
00268    if (hkm == NULL) {
00269      printf("parse_hotkey_config_file: calloc error \n");
00270      xmlFreeDoc(doc);
00271      return(NULL);
00272    }
00273 
00274    /*
00275     * Now, walk the tree.
00276     */
00277 
00278    /* First level we expect just le */
00279    if (!xmlStrcmp(cur->name, (const xmlChar *)"le")) {
00280      cur = cur->xmlChildrenNode;
00281    } else {
00282      printf("Invalid configuration file ..\n");
00283      xmlFreeDoc(doc);
00284      return(NULL);
00285    }
00286    
00287    while (cur != NULL) {
00288      if (!xmlStrcmp(cur->name, (const xmlChar *)"name")) {
00289        char *le_name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00290        cur = cur->next;
00291      }
00292      if (!xmlStrcmp(cur->name, (const xmlChar *)"text") || !xmlStrcmp(cur->name, (const xmlChar *)"comment")) {
00293        cur = cur->next;
00294      }
00295      if (!xmlStrcmp(cur->name, (const xmlChar *)"num_hotkey_profiles")) {
00296        hkm->num_hotkey_profiles = atoi(xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00297        hkm->hkps = (IMHotkeyProfileStruct *)calloc(hkm->num_hotkey_profiles, sizeof(IMHotkeyProfileStruct));
00298        cur = cur->next;
00299      }
00300      if (!xmlStrcmp(cur->name, (const xmlChar *)"hotkey_profile")) {
00301        for (i=0; i<hkm->num_hotkey_profiles; i++) {
00302          /*
00303          hkm->hkps[i] = (IMHotkeyProfileStruct *)parseHotkeyProfile(doc, cur);
00304          */
00305          hkp = parseHotkeyProfile(doc, cur);
00306          hkm->hkps[i].profile_id = hkp->profile_id;
00307          hkm->hkps[i].scope = hkp->scope;
00308          hkm->hkps[i].name = le_name;
00309          hkm->hkps[i].num_hotkeys = hkp->num_hotkeys;
00310          hkm->hkps[i].hks = hkp->hks;
00311          if (cur)
00312            cur = cur->next;
00313        }
00314      }
00315    }
00316   
00317    return hkm;
00318 }
00319 
00320 void
00321 printHotkeyManager(
00322    IMHotkeyManagerStruct *hkm
00323 )
00324 {
00325    int i, j, k;
00326 
00327    printf("\nprintHotkeyManager: num_hotkey_profiles [%d]\n", hkm->num_hotkey_profiles);
00328    for (i=0; i<hkm->num_hotkey_profiles; i++) {
00329      printf("\n++++ HOTKEY PROFILE: id [%d]\n", hkm->hkps[i].profile_id);
00330      printf("++++ scope [%d]\n", hkm->hkps[i].scope);
00331      printf("++++ num_hotkeys [%d]\n", hkm->hkps[i].num_hotkeys);
00332      for (j=0; j<hkm->hkps[i].num_hotkeys; j++) {
00333        printf("\n++++++ HOTKEY: state_flag [%d]\n", hkm->hkps[i].hks[j].state_flag);
00334        printf("++++++ HOTKEY: action_flag [%d]\n", hkm->hkps[i].hks[j].action_flag);
00335        printf("++++++ HOTKEY: label [%s]\n", hkm->hkps[i].hks[j].label);
00336        printf("++++++ HOTKEY: nkeys [%d]\n", hkm->hkps[i].hks[j].nkeys);
00337        for (k=0; k<hkm->hkps[i].hks[j].nkeys; k++) {
00338          printf("\n++++++++ KEYS: keyCode [%d]\n", hkm->hkps[i].hks[j].keys[k].keyCode);
00339          printf("++++++++ KEYS: modifier [%d]\n", hkm->hkps[i].hks[j].keys[k].modifier);
00340        }
00341      }
00342    }
00343 }
00344 
00345 /* Local Variables: */
00346 /* c-file-style: "iiim-project" */
00347 /* End: */