~ubuntu-branches/ubuntu/oneiric/libchewing/oneiric

« back to all changes in this revision

Viewing changes to test/gen_keystroke.c

  • Committer: Bazaar Package Importer
  • Author(s): Kanru Chen
  • Date: 2006-05-15 16:17:40 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060515161740-x7jqtejpdwn6jv72
Tags: 0.3.0-1
* New upstream release
* Bump major version to 3
* Change debhelper compatibility to 5
* Update standards-version to 3.7.2.0
* Not install *.la files anymore.
* Install -data files to libchewing3/chewing instead of chewing
  to maintain the compatibility of old -data packages
* Update README.Debian
* Change libchewing3-data architecture to any (Closes: #303000)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
2
 * gen_keystroke.c
3
3
 *
4
 
 * Copyright (c) 2004
 
4
 * Copyright (c) 2004, 2005
5
5
 *      libchewing Core Team. See ChangeLog for details.
6
6
 *
7
7
 * See the file "COPYING" for information on usage and redistribution
13
13
#include <stdio.h>
14
14
#include <stdlib.h>
15
15
#include <string.h>
16
 
#include <ncurses.h>
 
16
#include <ncursesw/ncurses.h>
 
17
#include <locale.h>
17
18
 
18
19
/* Avoid incorrect KEY_ENTER definition */
19
20
#ifdef KEY_ENTER
42
43
/* Spacing */
43
44
#define FILL_LINE  "--------------------------------------------------------"
44
45
#define FILL_BLANK "                                                               "
 
46
 
45
47
static int hasColor = 0;
46
48
static char selKey_define[ 11 ] = "1234567890\0"; /* Default */
47
49
 
51
53
        addstr( FILL_LINE );
52
54
}
53
55
 
54
 
void show_edit_buffer( int x, int y, ChewingOutput *pgo )
 
56
void show_edit_buffer( int x, int y, ChewingContext *ctx )
55
57
{
56
58
        int i;
 
59
        char *buffer_string;
57
60
        move( x, y );
58
61
        addstr( FILL_BLANK );
 
62
        if ( ! chewing_buffer_Check( ctx ) )
 
63
                return;
59
64
        move( x, y );
60
 
        for ( i = 0; i < pgo->chiSymbolBufLen; i++ )
61
 
                addstr( pgo->chiSymbolBuf[ i ].s );
 
65
        buffer_string = chewing_buffer_String( ctx );
 
66
        addstr( buffer_string );
 
67
        free( buffer_string );
62
68
}
63
69
 
64
 
void show_interval_buffer( int x, int y, ChewingOutput *pgo )
 
70
void show_interval_buffer( int x, int y, ChewingContext *ctx )
65
71
{
66
72
        char out_buf[ 100 ];
67
73
        int i, count;
71
77
        addstr( FILL_BLANK );
72
78
        move( x, y );
73
79
 
74
 
        if ( pgo->chiSymbolBufLen == 0 ) {
75
 
                return;
76
 
        }
77
 
 
 
80
        /* Check if buffer is available. */
 
81
        if ( ! chewing_buffer_Check( ctx ) ) {
 
82
                return;
 
83
        }
 
84
        
78
85
        count = 0;
79
 
        for ( i = 0 ;i < pgo->chiSymbolBufLen; i++ ) {
 
86
        for ( i = 0 ;i < ctx->output->chiSymbolBufLen; i++ ) {
80
87
                arrPos[ i ] = count;
81
 
                count += strlen( pgo->chiSymbolBuf[ i ].s );
 
88
                count += strlen( (const char *) ctx->output->chiSymbolBuf[ i ].s ) - 3 < 0 ? 1 : 2;
82
89
        }
83
90
        arrPos[ i ] = count;
84
91
 
85
92
        memset( out_buf, ' ', count * ( sizeof( char ) ) );
86
93
        out_buf[ count ] = '\0';
87
94
 
88
 
        for ( i = 0; i < pgo->nDispInterval; i++ ) {
89
 
                if ( ( pgo->dispInterval[ i ].to - pgo->dispInterval[ i ].from ) == 1 ) {
90
 
                        out_buf[ arrPos[ pgo->dispInterval[ i ].from ] ] = ' ';
91
 
                        out_buf[ arrPos[ pgo->dispInterval[ i ].to ] - 1 ] = ' ';
 
95
        for ( i = 0; i < ctx->output->nDispInterval; i++ ) {
 
96
                if ( ( ctx->output->dispInterval[ i ].to - ctx->output->dispInterval[ i ].from ) == 1 ) {
 
97
                        out_buf[ arrPos[ ctx->output->dispInterval[ i ].from ] ] = ' ';
 
98
                        out_buf[ arrPos[ ctx->output->dispInterval[ i ].to ] - 1 ] = ' ';
92
99
                }
93
100
                else {  
94
 
                        out_buf[ arrPos[ pgo->dispInterval[ i ].from ] ] = '[';
95
 
                        out_buf[ arrPos[ pgo->dispInterval[ i ].to ] - 1 ] =  ']';
 
101
                        out_buf[ arrPos[ ctx->output->dispInterval[ i ].from ] ] = '[';
 
102
                        out_buf[ arrPos[ ctx->output->dispInterval[ i ].to ] - 1 ] =  ']';
96
103
                }
97
104
                memset(
98
 
                        &out_buf[ arrPos[ pgo->dispInterval[ i ].from ] + 1 ], '-',
99
 
                        arrPos[ pgo->dispInterval[ i ].to ] - arrPos[ pgo->dispInterval[ i ].from ] - 2 );
 
105
                        &out_buf[ arrPos[ ctx->output->dispInterval[ i ].from ] + 1 ], '-',
 
106
                        arrPos[ ctx->output->dispInterval[ i ].to ] - arrPos[ ctx->output->dispInterval[ i ].from ] - 2 );
100
107
        }
101
108
        addstr( out_buf );
102
109
}
103
110
 
104
 
void showZuin( ChewingOutput *pgo )
 
111
void showZuin( ChewingContext *ctx )
105
112
{
106
 
        int i, a;
107
 
        if ( pgo->bChiSym )
108
 
                addstr( "[��]" );
 
113
        int i;
 
114
        int zuin_count;
 
115
        char *zuin_string;
 
116
        if ( chewing_get_ChiEngMode( ctx ) )
 
117
                addstr( "[中]" );
109
118
        else
110
 
                addstr( "[�^]" );
111
 
        addstr( "  " );
112
 
        for ( i = 0, a = 2; i < ZUIN_SIZE; i++ ) {
113
 
                if ( pgo->zuinBuf[ i ].s[ 0 ] != '\0' ) {
114
 
                        addstr( pgo->zuinBuf[ i ].s );
115
 
                }
116
 
        }
 
119
                addstr( "[英]" );
 
120
        addstr( "        " );
 
121
        zuin_string = chewing_zuin_String( ctx, &zuin_count );
 
122
        addstr( zuin_string );
 
123
        free( zuin_string );
117
124
}
118
125
 
119
 
void show_zuin_buffer( int x, int y, ChewingOutput *pgo )
 
126
void show_zuin_buffer( int x, int y, ChewingContext *ctx )
120
127
{
121
128
        move( x, y );
122
129
        addstr( FILL_BLANK );
123
130
        move( x, y );
124
131
        if ( hasColor )
125
132
                attron( COLOR_PAIR( 1 ) );
126
 
        showZuin( pgo );
 
133
        showZuin( ctx );
127
134
        if ( hasColor )
128
135
                attroff( COLOR_PAIR( 1 ) );
129
136
}
130
137
 
131
 
void show_userphrase( int x, int y, wch_t showMsg[], int len )
132
 
{
133
 
        char out_buf[ 40 ];
134
 
        int i;
135
 
 
136
 
        memset( out_buf, 0, sizeof( out_buf ) );
137
 
        for ( i = 0; i < len; i++ ) {
138
 
                strcat( out_buf, showMsg[ i ].s );
139
 
        }
 
138
void show_full_shape( int x, int y, ChewingContext *ctx )
 
139
{
 
140
        move( x, y );
 
141
        addstr( "[" );
 
142
        if ( hasColor )
 
143
                attron( COLOR_PAIR( 2 ) );
 
144
        if ( chewing_get_ShapeMode( ctx ) == FULLSHAPE_MODE )
 
145
                addstr( "全形" );
 
146
        else
 
147
                addstr( "半形" );
 
148
        if ( hasColor )
 
149
                attroff( COLOR_PAIR( 2 ) );
 
150
        addstr( "]" );
 
151
}
 
152
 
 
153
void show_userphrase( int x, int y, ChewingContext *ctx )
 
154
{
 
155
        char *aux_string;
 
156
        if ( chewing_aux_Length( ctx ) == 0 )
 
157
                return;
 
158
 
140
159
        move( x, y );
141
160
        addstr( FILL_BLANK );
142
161
        move( x, y );
143
162
        if ( hasColor )
144
163
                attron( COLOR_PAIR( 2 ) );
145
 
        addstr( out_buf );
 
164
        aux_string = chewing_aux_String( ctx );
 
165
        addstr( aux_string );
 
166
        free( aux_string );
146
167
        if ( hasColor )
147
168
                attroff( COLOR_PAIR( 2 ) );
148
169
}
149
170
 
150
 
void show_choose_buffer( int x, int y, ChewingOutput *pgo )
 
171
void show_choose_buffer( int x, int y, ChewingContext *ctx )
151
172
{
152
 
        int i, no, len;
 
173
        int i = 1;
 
174
        int currentPageNo;
153
175
        char str[ 20 ];
 
176
        char *cand_string;
154
177
        move( x, y );
155
178
        addstr( FILL_BLANK );
156
179
        move( x, y );
157
 
 
158
 
        if ( pgo->pci->nPage != 0 ) {
159
 
                no = pgo->pci->pageNo * pgo->pci->nChoicePerPage;
160
 
                len = 0;
161
 
 
162
 
                for ( i = 0; i < pgo->pci->nChoicePerPage; no++, i++ ) {
163
 
                        if ( no >= pgo->pci->nTotalChoice )
164
 
                                break;
165
 
                        sprintf( str, "%d.", i + 1 );
166
 
                        if ( hasColor )
167
 
                                attron( COLOR_PAIR( 3 ) );
168
 
                        addstr( str );
169
 
                        if ( hasColor )
170
 
                                attroff( COLOR_PAIR( 3 ) );
171
 
                        sprintf( str, " %s ", pgo->pci->totalChoiceStr[ no ] );                 
172
 
                        addstr( str );
173
 
                }
174
 
                if ( pgo->pci->nPage != 1 ) {
175
 
                        if ( pgo->pci->pageNo == 0 )
176
 
                                addstr( "  >" );
177
 
                        else if ( pgo->pci->pageNo == ( pgo->pci->nPage - 1 ) )
178
 
                                addstr( "<  " );
179
 
                        else
180
 
                                addstr( "< >" );
181
 
                }
 
180
        
 
181
        if ( chewing_cand_TotalPage( ctx ) == 0 )
 
182
                return;
 
183
        
 
184
        chewing_cand_Enumerate( ctx );
 
185
        while ( chewing_cand_hasNext( ctx ) ) {
 
186
                if ( i == chewing_cand_ChoicePerPage( ctx ) )
 
187
                        break;
 
188
                sprintf( str, "%d.", i );
 
189
                if ( hasColor )
 
190
                        attron( COLOR_PAIR( 3 ) );
 
191
                addstr( str );
 
192
                if ( hasColor )
 
193
                        attroff( COLOR_PAIR( 3 ) );
 
194
                cand_string = chewing_cand_String( ctx );
 
195
                sprintf( str, " %s ", cand_string );
 
196
                addstr( str );
 
197
                free( cand_string );
 
198
                i++;
 
199
        }
 
200
        currentPageNo = chewing_cand_CurrentPage( ctx );
 
201
        if ( chewing_cand_TotalPage( ctx ) != 1 ) {
 
202
                if ( currentPageNo == 0 )
 
203
                        addstr( "  >" );
 
204
                else if ( currentPageNo == ( chewing_cand_TotalPage( ctx ) - 1 ) )
 
205
                        addstr( "<  " );
 
206
                else
 
207
                        addstr( "< >" );
182
208
        }
183
209
}
184
210
 
185
 
void show_commit_string( ChewingOutput *pgo )
 
211
void show_commit_string( ChewingContext *ctx )
186
212
{
187
 
        static int x = 11;
 
213
        static int x = 12;
188
214
        static int y = 0;
189
215
        int i;
 
216
        char *commit_string;
 
217
#if 0
190
218
        if ( pgo->keystrokeRtn & KEYSTROKE_COMMIT ) {
191
219
                for ( i = 0; i < pgo->nCommitStr; i++ ) {
192
 
                        mvaddstr( x, y, pgo->commitStr[ i ].s );
 
220
                        mvaddstr( x, y, (const char *) pgo->commitStr[ i ].s );
193
221
                        y = ( y >= 54 ) ?
194
222
                                0 : 
195
 
                                ( y + strlen( pgo->commitStr[ i ].s ) );
 
223
                                ( y + strlen( (const char *) pgo->commitStr[ i ].s ) - 3 < 0 ? y + 1 : y + 2 );
196
224
                        x = ( y == 0 ) ? ( x + 1 ) : x;
197
225
                }
198
226
        }
 
227
#endif
 
228
        if ( chewing_commit_Check( ctx ) ) {
 
229
                commit_string = chewing_commit_String( ctx );
 
230
                mvaddstr( x, y, FILL_BLANK);
 
231
                mvaddstr( x, y, commit_string );
 
232
                free( commit_string );
 
233
        }
199
234
}
200
235
 
201
 
void set_cursor( int x, ChewingOutput *pgo )
 
236
void set_cursor( int x, ChewingContext *ctx )
202
237
{
203
238
        int i, count;
204
239
 
205
 
        for ( count = 0, i = 0; i < pgo->chiSymbolCursor; i++) {
206
 
                count += strlen( pgo->chiSymbolBuf[ i ].s );
 
240
        for ( count = 0, i = 0; i < ctx->output->chiSymbolCursor; i++) {
 
241
                count += strlen( (const char *) ctx->output->chiSymbolBuf[ i ].s) - 3 < 0 ? 1 : 2;
207
242
        }
208
243
        move( x, count );
209
244
}
210
245
 
211
246
int main( int argc, char *argv[] )
212
247
{
213
 
        ChewingConf *cf = (ChewingConf *) malloc( sizeof( ChewingConf ) );
214
 
        ChewingData *da = (ChewingData *) malloc( sizeof( ChewingData ) );
215
 
        ConfigData config;
216
 
        ChewingOutput gOut;
 
248
        ChewingConfigData config;
 
249
        ChewingContext *ctx;
217
250
        FILE *fout;
218
251
        char *prefix = CHEWING_DATA_PREFIX;
219
252
        int ch;
234
267
        }
235
268
 
236
269
        /* Initialize curses library */
 
270
        setlocale(LC_CTYPE, "");
237
271
        initscr();
238
272
        if ( has_colors() == TRUE ) {
239
273
                start_color();
251
285
        refresh();
252
286
 
253
287
        /* Initialize libchewing */
254
 
        cf->kb_type = KBStr2Num( "KB_DEFAULT" );
255
 
        cf->inp_cname = ( char * ) strdup( "�s�ŭ�" );
256
 
        cf->inp_ename = ( char * ) strdup( "Chewing" );
257
 
        ReadTree( prefix );
258
 
        InitChar( prefix );
259
 
        InitDict( prefix );
260
288
        /* for the sake of testing, we should not change existing hash data */
261
 
        ReadHash( TEST_HASH_DIR );
262
 
        InitChewing( da, cf );
263
 
 
264
 
        config.selectAreaLen = 55;
265
 
        config.maxChiSymbolLen = 16;
 
289
        chewing_Init( prefix, TEST_HASH_DIR );
 
290
 
 
291
        /* Request handle to ChewingContext */
 
292
        ctx = chewing_new();
 
293
 
 
294
        /* Set keyboard type */
 
295
        chewing_set_KBType( ctx, chewing_KBStr2Num( "KB_DEFAULT" ) );
 
296
 
 
297
        /* Fill configuration values */
 
298
        config.selectAreaLen = 20;
 
299
        config.maxChiSymbolLen = 16;
266
300
        config.bAddPhraseForward = 1;
267
301
 
268
 
        for ( i = 0; i < 10; i++ )
269
 
                config.selKey[ i ] = selKey_define[ i ];
270
 
        SetConfig( da, &config );
271
 
 
 
302
        for ( i = 0; i < 10; i++ )
 
303
                config.selKey[ i ] = selKey_define[ i ];
 
304
 
 
305
        /* Enable the configurations */
 
306
        chewing_Configure( ctx, &config );
 
307
 
 
308
        clear();
272
309
        mvaddstr( 0, 0, "Any key to start testing..." );
273
310
 
274
311
        while ( TRUE ) {
275
312
                ch = getch();
276
313
                switch ( ch ) {
277
314
                        case KEY_LEFT:
278
 
                                OnKeyLeft( da, &gOut );
 
315
                                chewing_handle_Left( ctx );
279
316
                                fprintf( fout, "<L>" );
280
317
                                break;
281
318
                        case KEY_SLEFT:
282
 
                                OnKeyShiftLeft( da, &gOut );
 
319
                                chewing_handle_ShiftLeft( ctx );
283
320
                                fprintf( fout, "<SL>" );
284
321
                                break;
285
322
                        case KEY_RIGHT:
286
 
                                OnKeyRight( da, &gOut );
 
323
                                chewing_handle_Right( ctx );
287
324
                                fprintf( fout, "<R>" );
288
325
                                break;
289
326
                        case KEY_SRIGHT:
290
 
                                OnKeyShiftRight( da, &gOut );
 
327
                                chewing_handle_ShiftRight( ctx );
291
328
                                fprintf( fout, "<SR>" );
292
329
                                break;
293
330
                        case KEY_UP:
294
 
                                OnKeyUp( da, &gOut );
 
331
                                chewing_handle_Up( ctx );
295
332
                                fprintf( fout, "<U>" );
296
333
                                break;
297
334
                        case KEY_DOWN:
298
 
                                OnKeyDown( da, &gOut );
 
335
                                chewing_handle_Down( ctx );
299
336
                                fprintf( fout, "<D>" );
300
337
                                break;
301
338
                        case KEY_SPACE:
302
 
                                OnKeySpace( da, &gOut );
 
339
                                chewing_handle_Space( ctx );
303
340
                                fprintf( fout, " " );
304
341
                                break;
305
342
                        case KEY_ENTER:
306
 
                                OnKeyEnter( da, &gOut );
 
343
                                chewing_handle_Enter( ctx );
307
344
                                fprintf( fout, "<E>" );
308
345
                                break;
309
346
                        case KEY_BACKSPACE:
310
 
                                OnKeyBackspace( da, &gOut );
 
347
                                chewing_handle_Backspace( ctx );
311
348
                                fprintf( fout, "<B>" );
312
349
                                break;
313
350
                        case KEY_ESC:
314
 
                                OnKeyEsc( da, &gOut );
 
351
                                chewing_handle_Esc( ctx );
315
352
                                fprintf( fout, "<EE>" );
316
353
                                break;
317
354
                        case KEY_DC:
318
 
                                OnKeyDel( da, &gOut );
 
355
                                chewing_handle_Del( ctx );
319
356
                                fprintf( fout, "<DC>" );
320
357
                                break;
321
358
                        case KEY_HOME:
322
 
                                OnKeyHome( da, &gOut );
 
359
                                chewing_handle_Home( ctx );
323
360
                                fprintf( fout, "<H>" );
324
361
                                break;
325
362
                        case KEY_END:
326
 
                                OnKeyEnd( da, &gOut );
 
363
                                chewing_handle_End( ctx );
327
364
                                fprintf( fout, "<EN>" );
328
365
                                break;
329
366
                        case KEY_TAB:
330
 
                                OnKeyTab( da, &gOut );
 
367
                                chewing_handle_Tab( ctx );
331
368
                                fprintf( fout, "<T>" );
332
369
                                break;
333
370
                        case CTRL_0:
341
378
                        case CTRL_8:
342
379
                        case CTRL_9:
343
380
                                add_phrase_length = ( ch - CTRL_0 + '0' );
344
 
                                OnKeyCtrlNum( 
345
 
                                        da, 
346
 
                                        add_phrase_length, 
347
 
                                        &gOut );
 
381
                                chewing_handle_CtrlNum( ctx, add_phrase_length );
348
382
                                fprintf( fout, "<C%c>", add_phrase_length );
349
383
                                break;
350
 
                        #if 0
351
 
                        case XK_Caps_Lock:
352
 
                                OnKeyCapslock(da, &gOut);
353
 
                                break;
354
 
                        #endif
355
384
                        case KEY_CTRL_('B'): /* emulate CapsLock */
356
 
                                OnKeyCapslock(da, &gOut);
 
385
                                chewing_handle_Capslock( ctx );
357
386
                                fprintf( fout, "<CB>");
358
387
                                break;
359
388
                        case KEY_CTRL_('D'):
360
389
                                goto end;
 
390
                        case KEY_CTRL_('H'): /* emulate Shift */
 
391
                                if ( chewing_get_ShapeMode( ctx ) == FULLSHAPE_MODE )
 
392
                                        chewing_set_ShapeMode( ctx, HALFSHAPE_MODE );
 
393
                                else
 
394
                                        chewing_set_ShapeMode( ctx, FULLSHAPE_MODE );
 
395
                                break;
361
396
                        default:
362
 
                                OnKeyDefault( da, (char) ch, &gOut );
 
397
                                chewing_handle_Default( ctx, (char) ch );
363
398
                                fprintf( fout, "%c", (char) ch );
364
399
                                break;
365
400
                }
366
401
                drawline( 0, 0 );
367
 
                show_edit_buffer( 1, 0, &gOut );
 
402
                show_edit_buffer( 1, 0, ctx );
368
403
                drawline( 2, 0 );
369
 
                show_interval_buffer( 3, 0, &gOut );
 
404
                show_interval_buffer( 3, 0, ctx );
370
405
                drawline( 4, 0 );
371
 
                show_choose_buffer( 5, 0, &gOut );
 
406
                show_choose_buffer( 5, 0, ctx );
372
407
                drawline( 6, 0 );
373
 
                show_zuin_buffer( 7, 0, &gOut );
 
408
                show_zuin_buffer( 7, 0, ctx );
 
409
                show_full_shape( 7, 5, ctx );
374
410
                drawline( 8, 0 );
375
411
                mvaddstr( 9, 0, "Ctrl + d : leave" );
376
 
                mvaddstr( 9, 20, "Ctrl + b : switch Eng/Chi mode" );
 
412
                mvaddstr( 9, 20, "Ctrl + b : toggle Eng/Chi mode" );
377
413
                mvaddstr( 10, 0, "F1, F2, F3, ..., F9 : Add user defined phrase");
378
 
                show_commit_string( &gOut );
379
 
                if ( da->showMsgLen > 0 ) {
380
 
                        show_userphrase( 7, 12, gOut.showMsg, gOut.showMsgLen );
381
 
                        gOut.showMsgLen = 0;
382
 
                }
383
 
                set_cursor( 1, &gOut );
 
414
                mvaddstr( 11, 0, "Crtl + h : toggle Full/Half shape mode" );
 
415
                show_commit_string( ctx );
 
416
                show_userphrase( 7, 12, ctx );
 
417
                set_cursor( 1, ctx );
384
418
        }
385
419
end:
386
420
        endwin();
387
421
 
 
422
        /* Release Chewing context */
 
423
        chewing_free( ctx );
 
424
 
 
425
        /* Termate Chewing services */
 
426
        chewing_Terminate();
 
427
 
388
428
        fprintf( fout, "\n" );
389
429
        fclose( fout );
390
430
        return 0;
391
431
}
392
432
 
393
 
/* vim:tenc=big5:
394
 
 * */