1 // -*- mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 /* Unikey Vietnamese Input Method
3 * Copyright (C) 2000-2005 Pham Kim Long
6 * UniKey project: http://unikey.org
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
30 #include "usrkeymap.h"
33 int getLabelIndex(int action);
34 void initKeyMap(int keyMap[256]);
36 #define OPT_COMMENT_CHAR ';'
38 struct UkEventLabelPair
44 UkEventLabelPair UkEvLabelList[] = {
51 {"Roof-All", vneRoofAll},
52 {"Roof-A", vneRoof_a},
53 {"Roof-E", vneRoof_e},
54 {"Roof-O", vneRoof_o},
55 {"Hook-Bowl", vneHookAll},
56 {"Hook-UO", vneHook_uo},
57 {"Hook-U", vneHook_u},
58 {"Hook-O", vneHook_o},
61 {"Telex-W", vne_telex_w},
62 {"Escape", vneEscChar},
63 {"DD", vneCount + vnl_DD},
64 {"dd", vneCount + vnl_dd},
65 {"A^", vneCount + vnl_Ar},
66 {"a^", vneCount + vnl_ar},
67 {"A(", vneCount + vnl_Ab},
68 {"a(", vneCount + vnl_ab},
69 {"E^", vneCount + vnl_Er},
70 {"e^", vneCount + vnl_er},
71 {"O^", vneCount + vnl_Or},
72 {"o^", vneCount + vnl_or},
73 {"O+", vneCount + vnl_Oh},
74 {"o+", vneCount + vnl_oh},
75 {"U+", vneCount + vnl_Uh},
76 {"u+", vneCount + vnl_uh}
79 const int UkEvLabelCount = sizeof(UkEvLabelList)/sizeof(UkEventLabelPair);
81 //--------------------------------------------------
82 static int parseNameValue(char *line, char **name, char **value)
91 p = strchr(line, OPT_COMMENT_CHAR);
96 for (p=line; *p == ' '; p++);
101 mark = p; //mark the last non-space character
103 while ((ch=*p) != '=' && ch!=0) {
111 *(mark+1) = 0; //terminate name with a null character
115 while (*p == ' ') p++;
121 while (*p) { //strip trailing spaces
130 //-----------------------------------------------------
131 DllExport int UkLoadKeyMap(const char *fileName, int keyMap[256])
134 UkKeyMapPair orderMap[256];
135 if (!UkLoadKeyOrderMap(fileName, orderMap, &mapCount))
139 for (i=0; i < mapCount; i++) {
140 keyMap[orderMap[i].key] = orderMap[i].action;
141 if (orderMap[i].action < vneCount) {
142 keyMap[tolower(orderMap[i].key)] = orderMap[i].action;
148 //------------------------------------------------------------------
149 DllExport int UkLoadKeyOrderMap(const char *fileName, UkKeyMapPair *pMap, int *pMapCount)
155 int i, bufSize, lineCount;
160 f = fopen(fileName, "r");
162 cerr << "Failed to open file: " << fileName << endl;
168 buf = new char[bufSize];
173 if (fgets((char *)buf, bufSize, f) == 0)
180 if (buf[len-1] == '\n')
182 if (parseNameValue(buf, (char **)&name, (char **)&value)) {
183 if (strlen(name) == 1) {
184 for (i=0; i < UkEvLabelCount; i++) {
185 if (strcmp(UkEvLabelList[i].label, value) == 0) {
186 c = (unsigned char)name[0];
187 if (keyMap[c] != vneNormal) {
188 //already assigned, don't accept this map
191 //cout << "key: " << c << " value: " << UkEvLabelList[i].ev << endl; //DEBUG
192 keyMap[c] = UkEvLabelList[i].ev;
193 pMap[mapCount].action = UkEvLabelList[i].ev;
194 if (keyMap[c] < vneCount) {
195 pMap[mapCount].key = toupper(c);
196 keyMap[toupper(c)] = UkEvLabelList[i].ev;
199 pMap[mapCount].key = c;
205 if (i == UkEvLabelCount) {
206 cerr << "Error in user key layout, line " << lineCount << ": command not found" << endl;
210 cerr << "Error in user key layout, line " << lineCount
211 << ": key name is not a single character" << endl;
218 *pMapCount = mapCount;
223 //-------------------------------------------
224 void initKeyMap(int keyMap[256])
227 for (c=0; c<256; c++)
228 keyMap[c] = vneNormal;
231 const char *UkKeyMapHeader =
232 "; This is UniKey user-defined key mapping file, generated from UniKey (Windows)\n\n";
234 DllExport int UkStoreKeyOrderMap(const char *fileName, UkKeyMapPair *pMap, int mapCount)
241 f = fopen(fileName, "wt");
243 cerr << "Failed to open file: " << fileName << endl;
247 fputs(UkKeyMapHeader, f);
248 for (i=0; i < mapCount; i++) {
249 labelIndex = getLabelIndex(pMap[i].action);
250 if (labelIndex != -1) {
251 sprintf(line, "%c = %s\n", pMap[i].key, UkEvLabelList[labelIndex].label);
259 int getLabelIndex(int event)
262 for (i = 0; i < UkEvLabelCount; i++) {
263 if (UkEvLabelList[i].ev == event)