File: usrkeymap.cpp

package info (click to toggle)
fcitx-unikey 0.2.7%2Bgit20220410-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 684 kB
  • sloc: cpp: 6,904; makefile: 8
file content (267 lines) | stat: -rw-r--r-- 7,143 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// -*- mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
/* Unikey Vietnamese Input Method
 * Copyright (C) 2000-2005 Pham Kim Long
 * Contact:
 *   unikey@gmail.com
 *   UniKey project: http://unikey.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor
 * Boston, MA 02110-1301, USA.
 */

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

#include <ctype.h>
#include "usrkeymap.h"


int getLabelIndex(int action);
void initKeyMap(int keyMap[256]);

#define OPT_COMMENT_CHAR ';'

struct UkEventLabelPair
{
    char label[32];
    int ev;
};

UkEventLabelPair UkEvLabelList[] = {
    {"Tone0", vneTone0},
    {"Tone1", vneTone1},
    {"Tone2", vneTone2},
    {"Tone3", vneTone3},
    {"Tone4", vneTone4},
    {"Tone5", vneTone5},
    {"Roof-All", vneRoofAll},
    {"Roof-A", vneRoof_a},
    {"Roof-E", vneRoof_e}, 
    {"Roof-O", vneRoof_o},
    {"Hook-Bowl", vneHookAll},
    {"Hook-UO", vneHook_uo},
    {"Hook-U", vneHook_u},
    {"Hook-O", vneHook_o},
    {"Bowl", vneBowl},
    {"D-Mark", vneDd},
    {"Telex-W", vne_telex_w},
    {"Escape", vneEscChar},
    {"DD", vneCount + vnl_DD},
    {"dd", vneCount + vnl_dd},
    {"A^", vneCount + vnl_Ar},
    {"a^", vneCount + vnl_ar},
    {"A(", vneCount + vnl_Ab},
    {"a(", vneCount + vnl_ab},
    {"E^", vneCount + vnl_Er},
    {"e^", vneCount + vnl_er},
    {"O^", vneCount + vnl_Or},
    {"o^", vneCount + vnl_or},
    {"O+", vneCount + vnl_Oh},
    {"o+", vneCount + vnl_oh},
    {"U+", vneCount + vnl_Uh},
    {"u+", vneCount + vnl_uh}
};

const int UkEvLabelCount = sizeof(UkEvLabelList)/sizeof(UkEventLabelPair);

//--------------------------------------------------
static int parseNameValue(char *line, char **name, char **value)
{
    char *p, *mark;
    char ch;

    if (line == 0)
        return 0;

    // get rid of comment
    p = strchr(line, OPT_COMMENT_CHAR);
    if (p)
        *p = 0;

    //get option name
    for (p=line; *p == ' '; p++);
    if (*p == 0)
        return 0;

    *name = p;
    mark = p; //mark the last non-space character
    p++;
    while ((ch=*p) != '=' && ch!=0) {
        if (ch != ' ')
            mark = p;
        p++;
    }

    if (ch == 0)
        return 0;
    *(mark+1) = 0; //terminate name with a null character

    //get option value
    p++;
    while (*p == ' ') p++;
    if (*p == 0)
        return 0;

    *value = p;
    mark = p;
    while (*p) { //strip trailing spaces
        if (*p != ' ')
            mark = p;
        p++;
    }
    *++mark = 0;
    return 1;
}

//-----------------------------------------------------
DllExport int UkLoadKeyMap(const char *fileName, int keyMap[256])
{
    int i, mapCount;
    UkKeyMapPair orderMap[256];
    if (!UkLoadKeyOrderMap(fileName, orderMap, &mapCount))
        return 0;

    initKeyMap(keyMap);
    for (i=0; i < mapCount; i++) {
        keyMap[orderMap[i].key] = orderMap[i].action;
        if (orderMap[i].action < vneCount) {
            keyMap[tolower(orderMap[i].key)] = orderMap[i].action;
        }
    }
    return 1;
}

//------------------------------------------------------------------
DllExport int UkLoadKeyOrderMap(const char *fileName, UkKeyMapPair *pMap, int *pMapCount)
{
    FILE *f;
    char *buf;
    char *name, *value;
    size_t len;
    int i, bufSize, lineCount;
    unsigned char c;
    int mapCount;
    int keyMap[256];

    f = fopen(fileName, "r");
    if (f == 0) {
        cerr << "Failed to open file: " << fileName << endl;
        return 0;
    }

    initKeyMap(keyMap);
    bufSize = 256;
    buf = new char[bufSize];

    lineCount = 0;
    mapCount = 0;
    while (!feof(f)) {
        if (fgets((char *)buf, bufSize, f) == 0)
            break;
        lineCount++;
        len = strlen(buf);
        if (len == 0)
            break;

        if (buf[len-1] == '\n')
            buf[len-1] = 0;
        if (parseNameValue(buf, (char **)&name, (char **)&value)) {
            if (strlen(name) == 1) {
                for (i=0; i < UkEvLabelCount; i++) {
                    if (strcmp(UkEvLabelList[i].label, value) == 0) {
                        c = (unsigned char)name[0];
                        if (keyMap[c] != vneNormal) {
                            //already assigned, don't accept this map
                            break;
                        }
                        //cout << "key: " << c << " value: " << UkEvLabelList[i].ev << endl; //DEBUG
                        keyMap[c] = UkEvLabelList[i].ev;
                        pMap[mapCount].action = UkEvLabelList[i].ev;
                        if (keyMap[c] < vneCount) {
                            pMap[mapCount].key = toupper(c);
                            keyMap[toupper(c)] = UkEvLabelList[i].ev;
                        }
                        else {
                            pMap[mapCount].key = c;
                        }
                        mapCount++;
                        break;
                    }
                }
                if (i == UkEvLabelCount) {
                    cerr << "Error in user key layout, line " << lineCount << ": command not found" << endl;
                }
            }
            else {
                cerr << "Error in user key layout, line " << lineCount 
                     << ": key name is not a single character" << endl;	
            }
        }
    }
    delete [] buf;
    fclose(f);

    *pMapCount = mapCount;

    return 1;
}

//-------------------------------------------
void initKeyMap(int keyMap[256])
{
    unsigned int c;
    for (c=0; c<256; c++)
        keyMap[c] = vneNormal;
}

const char *UkKeyMapHeader = 
    "; This is UniKey user-defined key mapping file, generated from UniKey (Windows)\n\n";

DllExport int UkStoreKeyOrderMap(const char *fileName, UkKeyMapPair *pMap, int mapCount)
{
    FILE *f;
    int i;
    int labelIndex;
    char line[128];

    f = fopen(fileName, "wt");
    if (f == 0) {
        cerr << "Failed to open file: " << fileName << endl;
        return 0;
    }

    fputs(UkKeyMapHeader, f);
    for (i=0; i < mapCount; i++) {
        labelIndex = getLabelIndex(pMap[i].action);
        if (labelIndex != -1) {
            sprintf(line, "%c = %s\n", pMap[i].key, UkEvLabelList[labelIndex].label);
            fputs(line, f);
        }
    }
    fclose(f);
    return 1;
}

int getLabelIndex(int event)
{
    int i;
    for (i = 0; i < UkEvLabelCount; i++) {
        if (UkEvLabelList[i].ev == event)
            return i;
    }
    return -1;
}