Back to index

im-sdk  12.3.91
thai_keymap.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "thai_keymap.h"
00003 
00004 int ThaiKeymapList_Init(TThaiKeymapList *pThaiKeymapList)
00005 {
00006        pThaiKeymapList->nNum_Keymaps = 0;
00007        pThaiKeymapList->nNum_Keymaps_Alloced = 0;
00008        pThaiKeymapList->pKeymaps = NULL;
00009 
00010        return(THAI_KEYMAP_OK);
00011 }
00012 
00013 int ThaiKeymapList_Free(TThaiKeymapList *pThaiKeymapList)
00014 {
00015        int i;
00016 
00017        if (pThaiKeymapList->pKeymaps == NULL) {
00018               return(0);
00019        }
00020 
00021        for(i=0; i<pThaiKeymapList->nNum_Keymaps_Alloced; i++) {
00022               if (pThaiKeymapList->pKeymaps[i]== NULL)
00023                      continue;
00024 
00025               if (pThaiKeymapList->pKeymaps[i]->pName) {
00026                      free((char *)pThaiKeymapList->pKeymaps[i]->pName);
00027               }
00028 
00029               if (pThaiKeymapList->pKeymaps[i]->pKeymap) {
00030                      free((char *)pThaiKeymapList->pKeymaps[i]->pKeymap);
00031               }
00032 
00033               free((char *)pThaiKeymapList->pKeymaps[i]);
00034        }
00035 
00036        free((char *)pThaiKeymapList->pKeymaps);
00037 
00038        pThaiKeymapList->nNum_Keymaps = 0;
00039        pThaiKeymapList->nNum_Keymaps_Alloced = 0;
00040        pThaiKeymapList->pKeymaps = NULL;
00041 
00042        return(THAI_KEYMAP_OK);
00043 }
00044 
00045 int ThaiKeymapList_Alloc(TThaiKeymapList *pThaiKeymapList, int nNum_Alloced)
00046 {
00047        int i;
00048 
00049        pThaiKeymapList->nNum_Keymaps_Alloced = 0;
00050        pThaiKeymapList->pKeymaps = (PThaiKeymap*)malloc(nNum_Alloced*sizeof(PThaiKeymap));
00051        if (pThaiKeymapList->pKeymaps == NULL) {
00052               return(THAI_KEYMAP_ERROR);
00053        }
00054 
00055        for (i=0; i<nNum_Alloced; i++)
00056               pThaiKeymapList->pKeymaps[i] = NULL;
00057 
00058        pThaiKeymapList->nNum_Keymaps_Alloced = nNum_Alloced;
00059 
00060        return(THAI_KEYMAP_OK);
00061 }
00062 
00063 int ThaiKeymapList_ReAlloc(TThaiKeymapList *pThaiKeymapList, int nNum_ReAlloced)
00064 {
00065        int i;
00066 
00067        pThaiKeymapList->pKeymaps = (PThaiKeymap*)realloc(pThaiKeymapList->pKeymaps,
00068                                    nNum_ReAlloced * sizeof(PThaiKeymap));
00069        if (pThaiKeymapList->pKeymaps == NULL) {
00070               pThaiKeymapList->nNum_Keymaps = 0;
00071               pThaiKeymapList->nNum_Keymaps_Alloced = 0;
00072               return(THAI_KEYMAP_ERROR);
00073        }
00074 
00075        for (i=pThaiKeymapList->nNum_Keymaps_Alloced; i<nNum_ReAlloced; i++)
00076               pThaiKeymapList->pKeymaps[i] = NULL;
00077 
00078        pThaiKeymapList->nNum_Keymaps_Alloced = nNum_ReAlloced;
00079 
00080        return(THAI_KEYMAP_OK);
00081 }
00082 
00083 int ThaiKeymapList_Print(TThaiKeymapList *pThaiKeymapList)
00084 {
00085        int nKeymap_ID, idx, key;
00086 
00087        printf("nNum_Keymaps: %d\n", pThaiKeymapList->nNum_Keymaps);
00088        for(nKeymap_ID = 0; nKeymap_ID < pThaiKeymapList->nNum_Keymaps; nKeymap_ID ++) {
00089               if (pThaiKeymapList->pKeymaps[nKeymap_ID] == NULL) {
00090                      return(THAI_KEYMAP_ERROR);
00091               }
00092 
00093               if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pName == NULL) {
00094                      return(THAI_KEYMAP_ERROR);
00095               }
00096 
00097               if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL) {
00098                      return(THAI_KEYMAP_ERROR);
00099               }
00100 
00101               printf("Name: %s\n", pThaiKeymapList->pKeymaps[nKeymap_ID]->pName);
00102 
00103               printf("Keymap: \n");
00104               for (idx = 1; idx <= THAI_KEYMAP_LEN; idx++) {
00105                      key = pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap[idx];
00106                      if (key != 0) {
00107                             printf("%c:  %c  0x%x\n", idx + 0x20, key, key);
00108                      }
00109               }
00110        }
00111 
00112        return(THAI_KEYMAP_OK);
00113 }
00114 
00115 int ThaiKeymapList_Item_Prepare(TThaiKeymapList *pThaiKeymapList, int nKeymap_ID)
00116 {
00117        int ret;
00118 
00119        if (nKeymap_ID < 0 || nKeymap_ID >= pThaiKeymapList->nNum_Keymaps_Alloced + NUM_KEYMAPS_ALLOC)
00120               return(THAI_KEYMAP_ERROR);
00121 
00122        if (pThaiKeymapList->nNum_Keymaps_Alloced == 0) {
00123               ret = ThaiKeymapList_Alloc(pThaiKeymapList, NUM_KEYMAPS_ALLOC);
00124               if (ret == THAI_KEYMAP_ERROR)
00125                      return(THAI_KEYMAP_ERROR);
00126        }
00127 
00128        if (nKeymap_ID >= pThaiKeymapList->nNum_Keymaps_Alloced) {
00129               int nNum_Alloc = pThaiKeymapList->nNum_Keymaps_Alloced + NUM_KEYMAPS_ALLOC;
00130               ret = ThaiKeymapList_ReAlloc(pThaiKeymapList, nNum_Alloc);
00131               if (ret == THAI_KEYMAP_ERROR)
00132                      return(THAI_KEYMAP_ERROR);
00133        }
00134 
00135        if (pThaiKeymapList->pKeymaps[nKeymap_ID] == NULL) {
00136               pThaiKeymapList->pKeymaps[nKeymap_ID] = (TThaiKeymap *)calloc(1, sizeof(TThaiKeymap));
00137               if (pThaiKeymapList->pKeymaps[nKeymap_ID] == NULL)
00138                      return(THAI_KEYMAP_ERROR);
00139        }
00140 
00141        if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pName != NULL) {
00142               free((char *)pThaiKeymapList->pKeymaps[nKeymap_ID]->pName);
00143               pThaiKeymapList->pKeymaps[nKeymap_ID]->pName = NULL;
00144        }
00145 
00146        if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap != NULL) {
00147               free((char *)pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap);
00148               pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap = NULL;
00149        }
00150               
00151        return(THAI_KEYMAP_OK);
00152 }
00153 
00154 int ThaiKeymapList_Item_Set_Name(TThaiKeymapList *pThaiKeymapList, int nKeymap_ID, unsigned char *pName)
00155 {
00156        if (!pName || !*pName) 
00157               return(THAI_KEYMAP_ERROR);
00158 
00159        if (nKeymap_ID < 0 || nKeymap_ID >= pThaiKeymapList->nNum_Keymaps_Alloced)
00160               return(THAI_KEYMAP_ERROR);
00161 
00162        if (pThaiKeymapList->pKeymaps[nKeymap_ID] == NULL) {
00163               return(THAI_KEYMAP_ERROR);
00164        }
00165 
00166        if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pName != NULL) {
00167               free((char *)pThaiKeymapList->pKeymaps[nKeymap_ID]->pName);
00168        }
00169 
00170        pThaiKeymapList->pKeymaps[nKeymap_ID]->pName = (unsigned char *)strdup((char *)pName);
00171        if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pName == NULL)
00172               return(THAI_KEYMAP_ERROR);
00173 
00174        return(THAI_KEYMAP_OK);
00175 }
00176 
00177 int ThaiKeymapList_Item_Set_KeymapValue(TThaiKeymapList *pThaiKeymapList, int nKeymap_ID, unsigned char key, unsigned char key_value)
00178 {
00179        int idx;
00180 
00181        if (!key_value)
00182               return(THAI_KEYMAP_ERROR);
00183 
00184        if (key < 0x20 || key >= 0x80) 
00185               return(THAI_KEYMAP_ERROR);
00186 
00187        if (nKeymap_ID < 0 || nKeymap_ID >= pThaiKeymapList->nNum_Keymaps_Alloced)
00188               return(THAI_KEYMAP_ERROR);
00189 
00190        if (pThaiKeymapList->pKeymaps == NULL) {
00191               return(THAI_KEYMAP_ERROR);
00192        }
00193 
00194        if (pThaiKeymapList->pKeymaps[nKeymap_ID] == NULL) {
00195               return(THAI_KEYMAP_ERROR);
00196        }
00197 
00198        if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL) {
00199               pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap = (unsigned char *)calloc(THAI_KEYMAP_LEN + 1, sizeof(char));
00200               if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL)
00201                      return(THAI_KEYMAP_ERROR);
00202               for (idx = 0; idx < THAI_KEYMAP_LEN; idx ++) {
00203                      pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap[idx] = idx+0x20;
00204               }
00205        }
00206               
00207        pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap[key - 0x20] = key_value;
00208        return(THAI_KEYMAP_OK);
00209 }
00210 
00211 int ThaiKeymapList_Item_Add_Keymap(TThaiKeymapList *pThaiKeymapList, unsigned char *pName, unsigned char *pKeymap)
00212 {
00213        int idx, ret;
00214        int nKeymap_ID;
00215 
00216        if (!pName || !*pName) 
00217               return(THAI_KEYMAP_ERROR);
00218 
00219        if (!pKeymap) 
00220               return(THAI_KEYMAP_ERROR);
00221 
00222        nKeymap_ID = pThaiKeymapList->nNum_Keymaps;
00223        ret = ThaiKeymapList_Item_Prepare(pThaiKeymapList, nKeymap_ID);
00224        if (ret == THAI_KEYMAP_ERROR)
00225               return(THAI_KEYMAP_ERROR);
00226 
00227        if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pName != NULL) {
00228               free((char *)pThaiKeymapList->pKeymaps[nKeymap_ID]->pName);
00229        }
00230 
00231        pThaiKeymapList->pKeymaps[nKeymap_ID]->pName = (unsigned char *)strdup((char *)pName);
00232        if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pName == NULL)
00233               return(THAI_KEYMAP_ERROR);
00234 
00235        if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL) {
00236               pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap = (unsigned char *)calloc(THAI_KEYMAP_LEN + 1, sizeof(char));
00237               if (pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL)
00238                      return(THAI_KEYMAP_ERROR);
00239               for (idx = 0; idx < THAI_KEYMAP_LEN; idx ++) {
00240                      pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap[idx] = idx+0x20;
00241               }
00242        }
00243               
00244        for (idx = 1; idx <= THAI_KEYMAP_LEN; idx++) {
00245               pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap[idx] = pKeymap[idx];
00246        }
00247 
00248        pThaiKeymapList->nNum_Keymaps ++;
00249 
00250        return(THAI_KEYMAP_OK);
00251 }
00252 
00253 int ThaiKeymapList_Item_Get_Symbol(TThaiKeymapList *pThaiKeymapList, int nKeymap_ID, int key)
00254 {
00255        if (nKeymap_ID < 0 || nKeymap_ID >= pThaiKeymapList->nNum_Keymaps)
00256               return(key);
00257 
00258        if (key <= 0x20 || key >= 0x80) 
00259               return(key);
00260 
00261        if (pThaiKeymapList->pKeymaps == NULL ||
00262            pThaiKeymapList->pKeymaps[nKeymap_ID] == NULL ||
00263            pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap == NULL)
00264               return(key);
00265 
00266        return(pThaiKeymapList->pKeymaps[nKeymap_ID]->pKeymap[key - 0x20]);
00267 }