Back to index

im-sdk  12.3.91
vkb_layout.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "config.h"
00004 #include "vkb_layout.h"
00005 
00006 #define MAX_LINE_LEN               256
00007 
00008 #define LABEL_STR           "LABEL"
00009 #define TYPE_STR            "TYPE"
00010 #define ENCODE_STR          "ENCODE"
00011 #define STRING_TYPE_STR            "COMMIT_AS_STRING"
00012 #define KEY_TYPE_STR        "COMMIT_AS_KEY"
00013 #define LIST_BEGIN_STR             "LIST_BEGIN"
00014 #define LIST_END_STR        "LIST_END"
00015 
00016 static vkb_layout_t *vkb_layout_new()
00017 {
00018     int i;
00019     vkb_layout_t *vkb_layout = NULL;
00020 
00021     vkb_layout = (vkb_layout_t *) calloc(1, sizeof(vkb_layout_t));
00022     if (vkb_layout == NULL)
00023        return (NULL);
00024 
00025     vkb_layout->ename = NULL;
00026     vkb_layout->name_utf8 = NULL;
00027     vkb_layout->type = KEYBOARD_STRING_TYPE;
00028 
00029     for (i = 0; i < MAX_BASEKEY_NUM; i++) {
00030        vkb_layout->basekey[i].lower_str = NULL;
00031        vkb_layout->basekey[i].upper_str = NULL;
00032     }
00033 
00034     return (vkb_layout);
00035 }
00036 
00037 static void vkb_layout_destroy(vkb_layout_t * vkb_layout)
00038 {
00039     int i;
00040 
00041     if (vkb_layout == NULL)
00042        return;
00043 
00044     if (vkb_layout->ename)
00045        free ((char *)vkb_layout->ename);
00046 
00047     if (vkb_layout->name_utf8)
00048        free ((char *)vkb_layout->name_utf8);
00049 
00050     for (i = 0; i < MAX_BASEKEY_NUM; i++) {
00051        if (vkb_layout->basekey[i].lower_str) {
00052               free((char *)vkb_layout->basekey[i].lower_str);
00053        if (vkb_layout->basekey[i].upper_str)
00054               free((char *)vkb_layout->basekey[i].upper_str);
00055        }
00056     }
00057 
00058     free((char *) vkb_layout);
00059     return;
00060 }
00061 
00062 void vkb_layout_print(vkb_layout_t * vkb_layout)
00063 {
00064     int  i;
00065     char *label_str;
00066 
00067     if (vkb_layout == NULL)
00068        return;
00069 
00070     label_str = KEYLIST_LOWER;
00071     if (vkb_layout->ename)
00072        printf("ename:%s#\n", vkb_layout->ename);
00073 
00074     if (vkb_layout->name_utf8)
00075        printf("name_utf8:%s#\n", vkb_layout->name_utf8);
00076 
00077     for (i = 0; i < MAX_BASEKEY_NUM; i++) {
00078        if (vkb_layout->basekey[i].lower_str) {
00079            printf("%c:#%s#       ", *(label_str + i),
00080                  vkb_layout->basekey[i].lower_str);
00081            if (vkb_layout->basekey[i].upper_str)
00082               printf("#%s#", vkb_layout->basekey[i].upper_str);
00083            printf("\n");
00084        }
00085     }
00086 }
00087 
00088 static void vkb_layout_set_ctrlkey_label(vkb_layout_t * vkb_layout)
00089 {
00090     if (vkb_layout == NULL)
00091        return;
00092 
00093     vkb_layout->ctrlkey[VK_BackSpace].label_str = BACKSPACE_KEY_LABEL;
00094     vkb_layout->ctrlkey[VK_Tab].label_str = TAB_KEY_LABEL;
00095     vkb_layout->ctrlkey[VK_CapsLock].label_str = CAPS_KEY_LABEL;
00096     vkb_layout->ctrlkey[VK_Enter].label_str = ENTER_KEY_LABEL;
00097     vkb_layout->ctrlkey[VK_Shift_L].label_str = SHIFT_KEY_LABEL;
00098     vkb_layout->ctrlkey[VK_Control_L].label_str = CTRL_KEY_LABEL;
00099     vkb_layout->ctrlkey[VK_Alt_L].label_str = ALT_KEY_LABEL;
00100     vkb_layout->ctrlkey[VK_Space].label_str = SPACE_KEY_LABEL;
00101     vkb_layout->ctrlkey[VK_Escape].label_str = ESCAPE_KEY_LABEL;
00102 }
00103 
00104 static void vkb_layout_set_basekey_label(vkb_layout_t * vkb_layout,
00105                                     char *line)
00106 {
00107 
00108     char ch, i, index;
00109     char *ptr, *lower_str, *upper_str, *label_str;
00110 
00111     if (vkb_layout == NULL)
00112        return;
00113 
00114     if (!line || !*line)
00115        return;
00116 
00117     ch = tolower(line[0]);
00118 
00119     label_str = KEYLIST_LOWER;
00120     index = 0;
00121     for (i = 0; i < strlen(label_str); i++) {
00122        if (ch == *(label_str + i)) {
00123            index = i;
00124            break;
00125        }
00126     }
00127 
00128     /* skip spaces */
00129     ptr = line + 1;
00130     while (*ptr && isspace(*ptr))
00131        ptr++;
00132     lower_str = ptr;
00133 
00134     /* skip to space */
00135     while (*ptr && !isspace(*ptr))
00136        ptr++;
00137     if (*ptr) {
00138        *ptr = '\0';
00139        ptr++;
00140     }
00141 
00142     while (*ptr && isspace(*ptr))
00143        ptr++;
00144     upper_str = ptr;
00145 
00146     while (*ptr && !isspace(*ptr))
00147        ptr++;
00148     *ptr = '\0';
00149 
00150     if (*lower_str) {
00151        if (vkb_layout->basekey[index].lower_str)
00152            free((char *)vkb_layout->basekey[index].lower_str);
00153        vkb_layout->basekey[index].lower_str = (char *) strdup(lower_str);
00154     }
00155     if (*upper_str) {
00156        if (vkb_layout->basekey[index].upper_str)
00157            free((char *)vkb_layout->basekey[index].upper_str);
00158        vkb_layout->basekey[index].upper_str = (char *) strdup(upper_str);
00159     }
00160 
00161     return;
00162 }
00163 
00164 static void vkb_layout_set_keyboard_ename(vkb_layout_t * vkb_layout,
00165                                      char *ename)
00166 {
00167     if (vkb_layout == NULL)
00168        return;
00169 
00170     if (!ename || !*ename)
00171        return;
00172 
00173     if (vkb_layout->ename)
00174        free((char *)vkb_layout->ename);
00175     vkb_layout->ename = (char *) strdup(ename);
00176     return;
00177 }
00178 
00179 static void vkb_layout_set_keyboard_label(vkb_layout_t * vkb_layout,
00180                                      char *name)
00181 {
00182     if (vkb_layout == NULL)
00183        return;
00184 
00185     if (!name || !*name)
00186        return;
00187 
00188     if (vkb_layout->name_utf8)
00189        free((char *)vkb_layout->name_utf8);
00190     vkb_layout->name_utf8 = (char *) strdup(name);
00191     return;
00192 }
00193 
00194 #define VKB_LAYOUT_NUM_ALLOC  6 
00195 
00196 static vkb_layout_t **vkb_layout_list_new()
00197 {
00198     vkb_layout_t **vkb_layout_list = NULL;
00199     vkb_layout_list =
00200        (vkb_layout_t **) calloc(VKB_LAYOUT_NUM_ALLOC,
00201                              sizeof(vkb_layout_t *));
00202     return (vkb_layout_list);
00203 }
00204 
00205 static vkb_layout_t **vkb_layout_list_realloc(vkb_layout_t **
00206                                          vkb_layout_list, int num)
00207 {
00208     vkb_layout_t **vkb_layout_list_new1 = NULL;
00209     vkb_layout_list_new1 =
00210        (vkb_layout_t **) realloc(vkb_layout_list,
00211                               num * sizeof(vkb_layout_t *));
00212     return (vkb_layout_list_new1);
00213 }
00214 
00215 void vkb_layout_list_destroy(vkb_layout_t ** vkb_layout_list)
00216 {
00217     vkb_layout_t **p;
00218 
00219     if (vkb_layout_list == NULL)
00220        return;
00221 
00222     for (p = vkb_layout_list; *p; p++) {
00223        vkb_layout_destroy(*p);
00224     }
00225 
00226     free((char *)vkb_layout_list);
00227 }
00228 
00229 void vkb_layout_list_print(vkb_layout_t ** vkb_layout_list)
00230 {
00231     vkb_layout_t **p;
00232 
00233     if (vkb_layout_list == NULL)
00234        return;
00235 
00236     for (p = vkb_layout_list; *p; p++) {
00237        vkb_layout_print(*p);
00238     }
00239 }
00240 
00241 static void vkb_layout_get_line_from_file(FILE * fd, char *line)
00242 {
00243     int line_ptr;
00244     char line_buf[256], *ptr;
00245 
00246     line_ptr = 0;
00247     line[0] = '\0';
00248 
00249     /* get line with no space */
00250     while (fgets(line_buf, 255, fd) != NULL) {
00251        ptr = line_buf;
00252 
00253        /* skip space keys */
00254        while (*ptr && isspace(*ptr))
00255            ptr++;
00256 
00257        /* if is space line, get new line */
00258        if (*ptr == '\n' || *ptr == '\0')
00259            continue;
00260 
00261        while (*ptr != '\n' && *ptr != '\0' && line_ptr < MAX_LINE_LEN)
00262            line[line_ptr++] = *ptr++;
00263 
00264        /* trim right space */
00265        while (isspace(line[line_ptr - 1]))
00266            line_ptr--;
00267        line[line_ptr] = '\0';
00268 
00269        /* if the line end with '\', then continue read the next line */
00270        if (line[line_ptr - 1] == '\\') {
00271            line_ptr--;
00272            line[line_ptr] = '\0';
00273            continue;
00274        }
00275 
00276        break;
00277     }
00278 }
00279 
00280 /*
00281   For keyboard_layout.txt
00282 
00283   Format as follow:
00284 
00285   [ english name ]
00286   LABEL       chinese name
00287   TYPE        COMMIT_AS_STRING/COMMIT_AS_KEY
00288   ENCODE  UTF-8
00289   LIST_BEGIN
00290   a    lower_str     upper_str
00291   LIST_END
00292 
00293 */
00294 vkb_layout_t **vkb_layout_list_read_from_layout_file(char *layout_file_name)
00295 {
00296     FILE *fd;
00297     char line[MAX_LINE_LEN];
00298     char *ptr, *ename_str;
00299     int i, len;
00300     int list_began = 0;
00301 
00302     int num_vkb_layout;
00303     vkb_layout_t *vkb_layout;
00304     vkb_layout_t **vkb_layout_list;
00305 
00306     fd = fopen(layout_file_name, "r");
00307     if (!fd)
00308        return (NULL);
00309 
00310     vkb_layout_list = (vkb_layout_t **) vkb_layout_list_new();
00311     if (vkb_layout_list == NULL)
00312        return (NULL);
00313 
00314     num_vkb_layout = 0;
00315     do {
00316        vkb_layout_get_line_from_file(fd, line);
00317 
00318        if (line[0] == '\0')
00319            break;
00320        if (line[0] == '#')
00321            continue;
00322 
00323        len = strlen(line);
00324        if (line[0] == '[' && line[len - 1] == ']') {
00325            list_began = 0;
00326            if ((num_vkb_layout + 1) % VKB_LAYOUT_NUM_ALLOC == 0) {
00327               vkb_layout_list = vkb_layout_list_realloc(vkb_layout_list,
00328                                                    num_vkb_layout + 1 + VKB_LAYOUT_NUM_ALLOC);
00329               if (vkb_layout_list == NULL) {
00330                   return (NULL);
00331               }
00332               for (i = num_vkb_layout; i < num_vkb_layout + 1 + VKB_LAYOUT_NUM_ALLOC; i++)
00333                   vkb_layout_list[i] = NULL;
00334            }
00335 
00336            vkb_layout = (vkb_layout_t *) vkb_layout_new();
00337            if (vkb_layout == NULL)
00338               break;
00339 
00340            /* get english name of the item */
00341            ptr = line + 1;
00342            while (isspace(*ptr))
00343               ptr++;
00344 
00345            ename_str = ptr;
00346 
00347            ptr = line + len - 2;
00348            while (isspace(*ptr))
00349               ptr--;
00350            *(ptr + 1) = '\0';
00351 
00352            if (*ename_str)
00353                vkb_layout_set_keyboard_ename(vkb_layout, ename_str);
00354            vkb_layout_set_ctrlkey_label(vkb_layout);
00355 
00356            vkb_layout_list[num_vkb_layout] = vkb_layout;
00357            num_vkb_layout++;
00358 
00359            continue;
00360        }
00361 
00362        if (!(strncasecmp(line, LABEL_STR, strlen(LABEL_STR)))) {
00363            ptr = line + strlen(LABEL_STR);
00364            while (*ptr && isspace(*ptr))
00365               ptr++;
00366            if (*ptr)
00367               vkb_layout_set_keyboard_label(vkb_layout, ptr);
00368            continue;
00369        }
00370 
00371        if (!(strncasecmp(line, TYPE_STR, strlen(TYPE_STR)))) {
00372            ptr = line + strlen(TYPE_STR);
00373            while (*ptr && isspace(*ptr))
00374               ptr++;
00375            if (*ptr) {
00376               if (!(strncasecmp(ptr, KEY_TYPE_STR, strlen(KEY_TYPE_STR)))) {
00377                   vkb_layout->type = KEYBOARD_KEY_TYPE;
00378               } else {
00379                   vkb_layout->type = KEYBOARD_STRING_TYPE;
00380               }
00381            }
00382            continue;
00383        }
00384 
00385        if (!(strncasecmp(line, LIST_BEGIN_STR, strlen(LIST_BEGIN_STR)))) {
00386            list_began = 1;
00387            continue;
00388        }
00389 
00390        if (!(strncasecmp(line, LIST_END_STR, strlen(LIST_END_STR)))) {
00391            list_began = 0;
00392            continue;
00393        }
00394 
00395        if (list_began == 1) {
00396            vkb_layout_set_basekey_label(vkb_layout, line);
00397        }
00398     } while (1);
00399 
00400     fclose(fd);
00401     return (vkb_layout_list);
00402 }