summaryrefslogtreecommitdiffstats
path: root/apps/recorder/keyboard.c
blob: 126a30cc83e3c3e20515b44be45b84b59395dd99 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Björn Stenberg
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "lcd.h"
#include "button.h"
#include "kernel.h"
#include "version.h"
#include "debug_menu.h"
#include "sprintf.h"
#include <string.h>
#include "font.h"
#include "screens.h"
#include "status.h"
#include "talk.h"
#include "settings.h"
#include "misc.h"

#define KEYBOARD_LINES 4
#define KEYBOARD_PAGES 3


#if CONFIG_KEYPAD == RECORDER_PAD
#define KBD_CURSOR_RIGHT (BUTTON_ON | BUTTON_RIGHT)
#define KBD_CURSOR_LEFT (BUTTON_ON | BUTTON_LEFT)
#define KBD_SELECT BUTTON_PLAY
#define KBD_PAGE_FLIP BUTTON_F1
#define KBD_DONE BUTTON_F2
#define KBD_ABORT BUTTON_OFF
#define KBD_BACKSPACE BUTTON_F3
#elif CONFIG_KEYPAD == ONDIO_PAD /* restricted Ondio keypad */
#define KBD_CURSOR_RIGHT (BUTTON_MENU | BUTTON_RIGHT)
#define KBD_CURSOR_LEFT (BUTTON_MENU | BUTTON_LEFT)
#define KBD_SELECT (BUTTON_MENU | BUTTON_REL)
#define KBD_SELECT_PRE BUTTON_MENU
#define KBD_PAGE_FLIP (BUTTON_MENU | BUTTON_UP)
#define KBD_DONE (BUTTON_OFF | BUTTON_REL)
#define KBD_ABORT (BUTTON_OFF | BUTTON_REPEAT)
#define KBD_BACKSPACE (BUTTON_MENU | BUTTON_DOWN)
#endif


static void kbd_setupkeys(const char* line[KEYBOARD_LINES], int page)
{
    switch (page) {
    case 0:
        line[0] = "ABCDEFG !?\" @#$%+'";
        line[1] = "HIJKLMN 789 &_()-`";
        line[2] = "OPQRSTU 456 §|{}/<";
        line[3] = "VWXYZ.,0123 ~=[]*>";
        break;

    case 1:
        line[0] = "abcdefg ¢£¤¥¦§©®¬";
        line[1] = "hijklmn «»°ºª¹²³¶";
        line[2] = "opqrstu ¯±×÷¡¿µ·¨";
        line[3] = "vwxyz., ¼½¾      ";
        break;

    case 2:
        line[0] = "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË";
        line[1] = "àáâãäåæ ìíîï èéêë";
        line[2] = "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ";
        line[3] = "òóôõöø çðþýÿ ùúûü";
        break;
    }
}

/* helper function to spell a char if voice UI is enabled */
void kbd_spellchar(char c)
{
    char spell_char[2]; /* store char to pass to talk_spell */

    if (global_settings.talk_menu) /* voice UI? */
    {
        spell_char[0] = c; 
        spell_char[1] = '\0'; /* mark end of char string */ 

        talk_spell(spell_char, false); 
    }
}

int kbd_input(char* text, int buflen)
{
    bool done = false;
    int page = 0;

    int font_w = 0, font_h = 0, i;
    int x = 0, y = 0;
    int main_x, main_y, max_chars, margin;
    int status_y1, status_y2, curpos;
    int len;
    int editpos;
    bool redraw = true;
    const char* line[KEYBOARD_LINES];

    char outline[256];
    char c = 0;
    struct font* font = font_get(FONT_SYSFIXED);
    int button, lastbutton = 0;

    lcd_setfont(FONT_SYSFIXED);
    font_w = font->maxwidth;
    font_h = font->height;

    margin = 3;
    main_y = (KEYBOARD_LINES + 1) * font_h + margin*2;
    main_x = 0;
    status_y1 = LCD_HEIGHT - font_h;
    status_y2 = LCD_HEIGHT;

    editpos = strlen(text);

    max_chars = LCD_WIDTH / font_w;
    kbd_setupkeys(line, page);

    if (global_settings.talk_menu) /* voice UI? */
        talk_spell(text, true); /* spell initial text */ 

    while(!done)
    {
        len = strlen(text);
            
        if(redraw)
        {
            lcd_clear_display();
            
            lcd_setfont(FONT_SYSFIXED);
            
            /* draw page */
            for (i=0; i < KEYBOARD_LINES; i++)
                lcd_putsxy(0, 8+i * font_h, line[i]);
            
            /* separator */
            lcd_drawline(0, main_y - margin, LCD_WIDTH, main_y - margin);
            
            /* write out the text */
            if (editpos < max_chars - 3 )
            {
                strncpy(outline, text, max_chars - 2);
                if (len > max_chars - 2)
                    lcd_putsxy(LCD_WIDTH - font_w, main_y, ">");
                curpos = (1 + editpos) * font_w;
            }
            else
            {
                /* not room for all text, cut left, right or both */
                if (editpos == len )
                {
                    if ( max_chars - 3 == len)
                    {
                        strncpy(outline, text, max_chars - 2);
                        curpos  = (1 + editpos) * font_w;
                    }
                    else
                    {
                        strncpy(outline, text + editpos - max_chars + 2,
                                max_chars - 2);
                        if (len > max_chars - 2)
                            lcd_putsxy(0, main_y, "<");
                        curpos = ( max_chars - 1) * font_w;
                    }
                }
                else
                {
                    if (len - 1 == editpos)
                    {
                        strncpy(outline, text + editpos - max_chars + 3,
                                max_chars - 2);
                        curpos = ( max_chars - 2) * font_w;
                    }
                    else
                    {
                        strncpy(outline, text + editpos - max_chars + 4,
                                max_chars - 2);
                        curpos = ( max_chars - 3) * font_w;
                    }
                    lcd_putsxy(LCD_WIDTH - font_w, main_y, ">");
                    lcd_putsxy(0, main_y, "<");
                }
            }

            /* Zero terminate the string */
            outline[max_chars - 2] = '\0';
            
            lcd_putsxy(font_w,main_y,outline);
            
            /* cursor */
            lcd_drawline(curpos, main_y, curpos, main_y + font_h);
            
#if CONFIG_KEYPAD == RECORDER_PAD
            /* draw the status bar */
            buttonbar_set("Shift", "OK", "Del");
            buttonbar_draw();
#endif            
            /* highlight the key that has focus */
            lcd_invertrect(font_w * x, 8 + font_h * y, font_w, font_h);
            
            status_draw(true);
        
            lcd_update();
        }

        /* The default action is to redraw */
        redraw = true;

        button = button_get_w_tmo(HZ/2);
        switch ( button ) {

            case KBD_ABORT:
                lcd_setfont(FONT_UI);
                return -1;
                break;

            case KBD_PAGE_FLIP:
                if (++page == KEYBOARD_PAGES)
                    page = 0;
                kbd_setupkeys(line, page);
                kbd_spellchar(line[y][x]);
                break;

            case BUTTON_RIGHT:
            case BUTTON_RIGHT | BUTTON_REPEAT:
                if (x < (int)strlen(line[y]) - 1)
                    x++;
                else
                    x = 0;
                kbd_spellchar(line[y][x]);
                break;

            case BUTTON_LEFT:
            case BUTTON_LEFT | BUTTON_REPEAT:
                if (x)
                    x--;
                else
                    x = strlen(line[y]) - 1;
                kbd_spellchar(line[y][x]);
                break;

            case BUTTON_DOWN:
            case BUTTON_DOWN | BUTTON_REPEAT:
                if (y < KEYBOARD_LINES - 1)
                    y++;
                else
                    y=0;
                kbd_spellchar(line[y][x]);
                break;

            case BUTTON_UP:
            case BUTTON_UP | BUTTON_REPEAT:
                if (y)
                    y--;
                else
                    y = KEYBOARD_LINES - 1;
                kbd_spellchar(line[y][x]);
                break;

            case KBD_BACKSPACE:
            case KBD_BACKSPACE | BUTTON_REPEAT:
                if (editpos > 0)
                {
                    for (i = editpos; i <= (len - 1);i++)
                    {
                        text[i-1] = text[i];
                    }
                    text[i-1]='\0';
                    editpos--;
                    if (editpos < 0)
                        editpos=0;
                }
                break;

            case KBD_DONE:
                /* accepts what was entered and continues */
                done = true;
                break;

            case KBD_SELECT:
                /* inserts the selected char */
#ifdef KBD_SELECT_PRE
                if (lastbutton != KBD_SELECT_PRE)
                    break;
#endif
                if (len<buflen)
                {
                    c = line[y][x];
                    if ( editpos == len )
                    {
                        text[len] = c;
                        text[len+1] = 0;
                    }
                    else
                    {
                        for (i = len ; i + 1 > editpos; i--)
                            text[i+1] = text[i];
                        text[editpos] = c;
                    }
                    editpos++;
                }
                if (global_settings.talk_menu) /* voice UI? */
                    talk_spell(text, false); /* speak revised text */
                break;

            case KBD_CURSOR_RIGHT:
            case KBD_CURSOR_RIGHT | BUTTON_REPEAT:
                editpos++;
                if (editpos > len)
                    editpos = len;
                else
                    kbd_spellchar(text[editpos]);
                break;

            case KBD_CURSOR_LEFT:
            case KBD_CURSOR_LEFT | BUTTON_REPEAT:
                editpos--;
                if (editpos < 0)
                    editpos = 0;
                else
                    kbd_spellchar(text[editpos]);
                break;

            case BUTTON_NONE:
                status_draw(false);
                redraw = false;
                break;

            default:
                if(default_event_handler(button) == SYS_USB_CONNECTED)
                    lcd_setfont(FONT_SYSFIXED);
                break;

        }
        if (button != BUTTON_NONE)
            lastbutton = button;
    }
    lcd_setfont(FONT_UI);

    return 0;
}