Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / player / keyboard.c
blob1c6dc9bb0d311bab6babffeb083306533faae4e7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "lcd.h"
22 #include "button.h"
23 #include "kernel.h"
24 #include "system.h"
25 #include "version.h"
26 #include <string.h>
27 #include "settings.h"
28 #include "statusbar.h"
29 #include "talk.h"
30 #include "misc.h"
31 #include "rbunicode.h"
32 #include "lang.h"
33 #include "keyboard.h"
34 #include "splash.h"
36 #define KBD_BUF_SIZE 64
37 #define KEYBOARD_PAGES 3
39 static unsigned short *kbd_setupkeys(int page, int* len)
41 static unsigned short kbdline[KBD_BUF_SIZE];
42 const unsigned char *p;
43 int i = 0;
45 switch (page)
47 case 0: /* Capitals */
48 p = "ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅ"
49 "ÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ";
50 break;
52 case 1: /* Small */
53 p = "abcdefghijklmnopqrstuvwxyzßàáâãä"
54 "åçèéêëìíîïñòóôõöøùúûüýÿ";
55 break;
57 default: /* Others */
58 p = " !\"#$%&'()*+,-./0123456789:;<=>?@[]_{}~";
59 break;
62 while (*p)
63 p = utf8decode(p, &kbdline[i++]);
65 *len = i;
67 return kbdline;
70 /* Delimiters for highlighting the character selected for insertion */
71 #define KEYBOARD_INSERT_LEFT 0xe110
72 #define KEYBOARD_INSERT_RIGHT 0xe10f
74 #define KEYBOARD_CURSOR 0x7f
75 #define KEYBOARD_ARROW 0xe10c
77 /* helper function to spell a char if voice UI is enabled */
78 static void kbd_spellchar(unsigned short c)
80 if (global_settings.talk_menu) /* voice UI? */
82 unsigned char tmp[5];
83 /* store char to pass to talk_spell */
84 unsigned char* utf8 = utf8encode(c, tmp);
85 *utf8 = 0;
87 if(c == ' ')
88 talk_id(VOICE_BLANK, false);
89 else
90 talk_spell(tmp, false);
94 static void say_edit(void)
96 if (global_settings.talk_menu)
97 talk_id(VOICE_EDIT, false);
100 int kbd_input(char* text, int buflen)
102 bool done = false;
103 bool redraw = true;
104 bool line_edit = false;
105 int page = 0, x = 0;
106 int linelen;
108 int len, len_utf8, i, j;
109 int editpos, curpos, leftpos;
110 unsigned short *line = kbd_setupkeys(page, &linelen);
111 unsigned char temptext[36];
112 unsigned char *utf8;
114 int button, lastbutton = 0;
115 int ret = 0; /* assume success */
117 editpos = utf8length(text);
119 if (global_settings.talk_menu) /* voice UI? */
120 talk_spell(text, true); /* spell initial text */
122 while (!done)
124 len = strlen(text);
125 len_utf8 = utf8length(text);
127 if (redraw)
129 if (line_edit)
131 lcd_putc(0, 0, ' ');
132 lcd_putc(0, 1, KEYBOARD_ARROW);
134 else
136 lcd_putc(0, 0, KEYBOARD_ARROW);
137 lcd_putc(0, 1, ' ');
140 lcd_putc(1, 0, KEYBOARD_INSERT_LEFT);
141 lcd_putc(2, 0, line[x]);
142 lcd_putc(3, 0, KEYBOARD_INSERT_RIGHT);
143 for (i = 1; i < 8; i++)
145 lcd_putc(i + 3, 0, line[(x+i)%linelen]);
148 /* write out the text */
149 curpos = MIN(MIN(editpos, 10 - MIN(len_utf8 - editpos, 3)), 9);
150 leftpos = editpos - curpos;
151 if (!leftpos) {
152 utf8 = text + utf8seek(text, leftpos);
153 i = 0;
154 j = 0;
155 } else {
156 temptext[0] = '<';
157 i = 1;
158 j = 1;
159 utf8 = text + utf8seek(text, leftpos+1);
161 while (*utf8 && i < 10) {
162 temptext[j++] = *utf8++;
163 if ((*utf8 & MASK) != COMP)
164 i++;
166 temptext[j] = 0;
169 if (len_utf8 - leftpos > 10) {
170 utf8 = temptext + utf8seek(temptext, 9);
171 *utf8++ = '>';
172 *utf8 = 0;
175 lcd_remove_cursor();
176 lcd_puts(1, 1, temptext);
177 lcd_put_cursor(curpos + 1, 1, KEYBOARD_CURSOR);
179 gui_syncstatusbar_draw(&statusbars, true);
182 /* The default action is to redraw */
183 redraw = true;
185 button = button_get_w_tmo(HZ/2);
186 switch (button)
188 case BUTTON_STOP: /* abort */
189 ret = -1; done = true;
190 break;
192 case BUTTON_MENU: /* page flip */
193 if (++page == KEYBOARD_PAGES)
194 page = 0;
195 line = kbd_setupkeys(page, &linelen);
196 if (x > linelen - 1)
197 x = linelen - 1;
198 kbd_spellchar(line[x]);
199 break;
201 case BUTTON_ON: /* toggle mode */
202 line_edit = !line_edit;
203 if (line_edit)
204 say_edit();
205 else
206 kbd_spellchar(line[x]);
207 break;
209 case BUTTON_RIGHT:
210 case BUTTON_RIGHT | BUTTON_REPEAT:
211 if (line_edit)
213 if (editpos < len_utf8)
215 editpos++;
216 int c = utf8seek(text, editpos);
217 kbd_spellchar(text[c]);
220 else
222 if (++x >= linelen)
223 x = 0;
224 kbd_spellchar(line[x]);
226 break;
228 case BUTTON_LEFT:
229 case BUTTON_LEFT | BUTTON_REPEAT:
230 if (line_edit)
232 if (editpos)
234 editpos--;
235 int c = utf8seek(text, editpos);
236 kbd_spellchar(text[c]);
239 else
241 if (--x < 0)
242 x = linelen - 1;
243 kbd_spellchar(line[x]);
245 break;
247 case BUTTON_PLAY | BUTTON_REPEAT:
248 /* accepts what was entered and continues */
249 ret = 0; done = true;
250 break;
252 case BUTTON_PLAY | BUTTON_REL:
253 if (lastbutton != BUTTON_PLAY)
254 break;
255 if (line_edit) /* backspace in line_edit */
257 if (editpos > 0)
259 utf8 = text + utf8seek(text, editpos);
260 i = 0;
261 do {
262 i++;
263 utf8--;
264 } while ((*utf8 & MASK) == COMP);
265 while (utf8[i]) {
266 *utf8 = utf8[i];
267 utf8++;
269 *utf8 = 0;
270 editpos--;
273 else /* inserts the selected char */
275 utf8 = utf8encode(line[x], temptext);
276 *utf8 = 0;
277 j = strlen(temptext);
278 if (len + j < buflen)
280 int k = len_utf8;
281 for (i = len+j; k >= editpos; i--) {
282 text[i] = text[i-j];
283 if ((text[i] & MASK) != COMP)
284 k--;
286 while (j--)
287 text[i--] = temptext[j];
288 editpos++;
291 if (global_settings.talk_menu) /* voice UI? */
292 talk_spell(text, false); /* speak revised text */
293 break;
295 case BUTTON_NONE:
296 gui_syncstatusbar_draw(&statusbars, false);
297 redraw = false;
298 break;
300 default:
301 default_event_handler(button);
302 break;
304 if (button != BUTTON_NONE)
305 lastbutton = button;
308 if (ret < 0)
309 splash(HZ/2, ID2P(LANG_CANCEL));
310 return ret;