File: chewing-conf.cpp

package info (click to toggle)
gcin 2.9.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,796 kB
  • sloc: cpp: 34,326; ansic: 9,319; makefile: 658; sh: 556
file content (207 lines) | stat: -rw-r--r-- 6,382 bytes parent folder | download | duplicates (7)
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
#include "chewing.h"

static ChewingConfigData g_chewingConfig;
static gboolean g_bUseDefault = FALSE;
static int g_nFd = -1;
static kbmapping_t g_kbMappingTable[] = 
{ 
    {"zo",        "KB_DEFAULT"},
    {"et",        "KB_ET"},
    {"et26",      "KB_ET26"},
    {"hsu",       "KB_HSU"},
    {"pinyin",    "KB_HANYU_PINYIN"},
    {"pinyin-no-tone", "KB_HANYU_PINYIN"},
    {"dvorak",    "KB_DVORAK"},
    {"ibm",       "KB_IBM"},
    {"mitac",     NULL},
    {"colemak",   NULL},
    {NULL,        NULL},
};

static void gcin_kb_config_set (ChewingContext *pChewingCtx);

void
chewing_config_open (gboolean bWrite)
{
    char *pszChewingConfig;
    char *pszHome;

    pszHome = getenv ("HOME");
    if (!pszHome)
        pszHome = "";

    pszChewingConfig = malloc (strlen (pszHome) + strlen (GCIN_CHEWING_CONFIG) + 1);
    memset (pszChewingConfig, 0x00, strlen (pszHome) + strlen (GCIN_CHEWING_CONFIG) + 1);
    sprintf (pszChewingConfig, "%s%s", pszHome, GCIN_CHEWING_CONFIG);

    g_nFd = open (pszChewingConfig,
                  bWrite == TRUE ? (O_RDWR | O_CREAT) : (O_RDONLY),
                  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

    free (pszChewingConfig);

    if (g_nFd == -1)
        g_bUseDefault = TRUE;
}

void
chewing_config_load (ChewingConfigData *pChewingConfig)
{
    int nReadSize;

    nReadSize = read (g_nFd, &g_chewingConfig, sizeof (g_chewingConfig));
    if (nReadSize == 0 || nReadSize != sizeof (g_chewingConfig))
        g_bUseDefault = TRUE;

    if (g_bUseDefault)
    {
        int nDefaultSelKey[MAX_SELKEY] = {'a', 's', 'd', 'f',
                                          'g', 'h', 'j', 'k',
                                          'l', ';'};

        g_chewingConfig.candPerPage           = 10;
        g_chewingConfig.maxChiSymbolLen       = 16;
        g_chewingConfig.bAddPhraseForward     = 1;
        g_chewingConfig.bSpaceAsSelection     = 1;
        g_chewingConfig.bEscCleanAllBuf       = 0;
        g_chewingConfig.bAutoShiftCur         = 1;
        g_chewingConfig.bEasySymbolInput      = 0;
        g_chewingConfig.bPhraseChoiceRearward = 1;
        g_chewingConfig.hsuSelKeyType         = 0;
        memcpy (&g_chewingConfig.selKey,
                &nDefaultSelKey,
                sizeof (g_chewingConfig.selKey));
    }

    memcpy (pChewingConfig, &g_chewingConfig, sizeof (ChewingConfigData));
}

void
chewing_config_set (ChewingContext *pChewingCtx)
{
    gcin_kb_config_set (pChewingCtx);

    chewing_set_candPerPage (pChewingCtx, g_chewingConfig.candPerPage);
    chewing_set_maxChiSymbolLen (pChewingCtx, g_chewingConfig.maxChiSymbolLen);
    chewing_set_addPhraseDirection (pChewingCtx, g_chewingConfig.bAddPhraseForward);
    chewing_set_spaceAsSelection (pChewingCtx, g_chewingConfig.bSpaceAsSelection);
    chewing_set_escCleanAllBuf (pChewingCtx, g_chewingConfig.bEscCleanAllBuf);
    chewing_set_autoShiftCur (pChewingCtx, g_chewingConfig.bAutoShiftCur);
    chewing_set_easySymbolInput (pChewingCtx, g_chewingConfig.bEasySymbolInput);
    chewing_set_phraseChoiceRearward (pChewingCtx, g_chewingConfig.bPhraseChoiceRearward);
    chewing_set_hsuSelKeyType (pChewingCtx, g_chewingConfig.hsuSelKeyType);
}

void
chewing_config_dump (void)
{
    int nIdx = 0;
    printf ("chewing config:\n");
    printf ("\tcandPerPage: %d\n", g_chewingConfig.candPerPage);
    printf ("\tmaxChiSymbolLen: %d\n", g_chewingConfig.maxChiSymbolLen);
    printf ("\tbAddPhraseForward: %d\n", g_chewingConfig.bAddPhraseForward);
    printf ("\tbSpaceAsSelection: %d\n", g_chewingConfig.bSpaceAsSelection);
    printf ("\tbEscCleanAllBuf: %d\n", g_chewingConfig.bEscCleanAllBuf);
    printf ("\tbAutoShiftCur: %d\n", g_chewingConfig.bAutoShiftCur);
    printf ("\tbEasySymbolInput: %d\n", g_chewingConfig.bEasySymbolInput);
    printf ("\tbPhraseChoiceRearward: %d\n", g_chewingConfig.bPhraseChoiceRearward);
    printf ("\thsuSelKeyType: %d\n", g_chewingConfig.hsuSelKeyType);
    printf ("\tselKey: ");
    for (nIdx = 0; nIdx < MAX_SELKEY; nIdx++)
        printf ("%c ", g_chewingConfig.selKey[nIdx]);
    printf ("\n");
}

void
chewing_config_close (void)
{
    if (g_nFd != -1)
        close (g_nFd);

    g_nFd = -1;
    g_bUseDefault = FALSE;
    memset (&g_chewingConfig, 0x00, sizeof (g_chewingConfig));
}

static void
gcin_kb_config_set (ChewingContext *pChewingCtx)
{
    char *pszHome;
    char *pszGcinKBConfig;
    char szBuf[32];
    char szKbType[16];
    char szKbSelKey[16];
    int  nFd;
    int  nRead;
    int  nIdx = 0;

    memset (szBuf, 0x00, 32);
    memset (szKbType, 0x00, 16);
    memset (szKbSelKey, 0x00, 16);

    pszHome = getenv ("HOME");
    if (!pszHome)
        pszHome = "";

    pszGcinKBConfig = malloc (strlen (pszHome) + strlen (GCIN_KB_CONFIG) + 1);
    memset (pszGcinKBConfig, 0x00, strlen (pszHome) + strlen (GCIN_KB_CONFIG) + 1);
    sprintf (pszGcinKBConfig, "%s%s", pszHome, GCIN_KB_CONFIG);

    nFd = open (pszGcinKBConfig, 
          O_RDONLY, 
          S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

    free (pszGcinKBConfig);

    if (nFd == -1)
        return;

    nRead = read (nFd, szBuf, 32);
    if (nRead == -1)
        return;
    
    sscanf (szBuf, "%s %s ", szKbType, szKbSelKey);

    if (!strlen (szKbType) || !strlen (szKbSelKey))
        return;

    for (nIdx = 0; nIdx < strlen (szKbSelKey); nIdx++)
        g_chewingConfig.selKey[nIdx] = szKbSelKey[nIdx];
    chewing_set_selKey (pChewingCtx, g_chewingConfig.selKey, strlen (szKbSelKey));

    nIdx = 0;
    while (g_kbMappingTable[nIdx].pszGcinKbName)
    {
        if (!strncmp (g_kbMappingTable[nIdx].pszGcinKbName,
	              szKbType,
		      strlen (szKbType)))
        {
            chewing_set_KBType (pChewingCtx, 
	                        chewing_KBStr2Num (g_kbMappingTable[nIdx].pszChewingKbName));
	    break;
	}
        nIdx++;
    }
}

gboolean
chewing_config_save (int nVal[])
{
    int nWriteSize;

    g_chewingConfig.candPerPage       = 
        nVal[0] > MAX_SELKEY ? MAX_SELKEY : nVal[0];
    g_chewingConfig.bSpaceAsSelection = nVal[1];
    g_chewingConfig.bEscCleanAllBuf   = nVal[2];
    g_chewingConfig.bAutoShiftCur     = nVal[3];
    g_chewingConfig.bAddPhraseForward = nVal[4];

    lseek (g_nFd, 0, SEEK_SET);

    nWriteSize = write (g_nFd, &g_chewingConfig, sizeof (g_chewingConfig));
    if (nWriteSize != sizeof (g_chewingConfig))
        return FALSE;

    return TRUE;
}