Oops: when adding a new platform I should also add it to the platform list ...
[Rockbox.git] / apps / player / keyboard.c
blobdcff7e7d622ec0d22de2edd5a75a62bd2aa5072f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "lcd.h"
20 #include "button.h"
21 #include "kernel.h"
22 #include "system.h"
23 #include "version.h"
24 #include "sprintf.h"
25 #include <string.h>
26 #include "settings.h"
27 #include "statusbar.h"
28 #include "talk.h"
29 #include "misc.h"
30 #include "rbunicode.h"
32 #define KBD_BUF_SIZE 64
33 #define KEYBOARD_PAGES 3
35 static unsigned short *kbd_setupkeys(int page, int* len)
37 static unsigned short kbdline[KBD_BUF_SIZE];
38 const unsigned char *p;
39 int i = 0;
41 switch (page)
43 case 0: /* Capitals */
44 p = "ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅ"
45 "ÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ";
46 break;
48 case 1: /* Small */
49 p = "abcdefghijklmnopqrstuvwxyzßàáâãä"
50 "åçèéêëìíîïñòóôõöøùúûüýÿ";
51 break;
53 default: /* Others */
54 p = " !\"#$%&'()*+,-./0123456789:;<=>?@[]_{}~";
55 break;
58 while (*p)
59 p = utf8decode(p, &kbdline[i++]);
61 *len = i;
63 return kbdline;
66 /* Delimiters for highlighting the character selected for insertion */
67 #define KEYBOARD_INSERT_LEFT 0xe110
68 #define KEYBOARD_INSERT_RIGHT 0xe10f
70 #define KEYBOARD_CURSOR 0x7f
71 #define KEYBOARD_ARROW 0xe10c
73 /* helper function to spell a char if voice UI is enabled */
74 static void kbd_spellchar(char c)
76 static char spell_char[2] = "\0\0"; /* store char to pass to talk_spell */
78 if (talk_menus_enabled()) /* voice UI? */
80 spell_char[0] = c;
81 talk_spell(spell_char, false);
85 int kbd_input(char* text, int buflen)
87 bool done = false;
88 bool redraw = true;
89 bool line_edit = false;
90 int page = 0, x = 0;
91 int linelen;
93 int len, len_utf8, i, j;
94 int editpos, curpos, leftpos;
95 unsigned short *line = kbd_setupkeys(page, &linelen);
96 unsigned char temptext[36];
97 unsigned char *utf8;
99 int button, lastbutton = 0;
101 editpos = utf8length(text);
103 if (talk_menus_enabled()) /* voice UI? */
104 talk_spell(text, true); /* spell initial text */
106 while (!done)
108 len = strlen(text);
109 len_utf8 = utf8length(text);
111 if (redraw)
113 if (line_edit)
115 lcd_putc(0, 0, ' ');
116 lcd_putc(0, 1, KEYBOARD_ARROW);
118 else
120 lcd_putc(0, 0, KEYBOARD_ARROW);
121 lcd_putc(0, 1, ' ');
124 lcd_putc(1, 0, KEYBOARD_INSERT_LEFT);
125 lcd_putc(2, 0, line[x]);
126 lcd_putc(3, 0, KEYBOARD_INSERT_RIGHT);
127 for (i = 1; i < 8; i++)
129 lcd_putc(i + 3, 0, line[(x+i)%linelen]);
132 /* write out the text */
133 curpos = MIN(MIN(editpos, 10 - MIN(len_utf8 - editpos, 3)), 9);
134 leftpos = editpos - curpos;
135 if (!leftpos) {
136 utf8 = text + utf8seek(text, leftpos);
137 i = 0;
138 j = 0;
139 } else {
140 temptext[0] = '<';
141 i = 1;
142 j = 1;
143 utf8 = text + utf8seek(text, leftpos+1);
145 while (*utf8 && i < 10) {
146 temptext[j++] = *utf8++;
147 if ((*utf8 & MASK) != COMP)
148 i++;
150 temptext[j] = 0;
153 if (len_utf8 - leftpos > 10) {
154 utf8 = temptext + utf8seek(temptext, 9);
155 *utf8++ = '>';
156 *utf8 = 0;
159 lcd_remove_cursor();
160 lcd_puts(1, 1, temptext);
161 lcd_put_cursor(curpos + 1, 1, KEYBOARD_CURSOR);
163 gui_syncstatusbar_draw(&statusbars, true);
166 /* The default action is to redraw */
167 redraw = true;
169 button = button_get_w_tmo(HZ/2);
170 switch (button)
172 case BUTTON_STOP: /* abort */
173 return -1;
174 break;
176 case BUTTON_MENU: /* page flip */
177 if (++page == KEYBOARD_PAGES)
178 page = 0;
179 line = kbd_setupkeys(page, &linelen);
180 if (x > linelen - 1)
181 x = linelen - 1;
182 kbd_spellchar(line[x]);
183 break;
185 case BUTTON_ON: /* toggle mode */
186 line_edit = !line_edit;
187 if (!line_edit)
188 kbd_spellchar(line[x]);
189 break;
191 case BUTTON_RIGHT:
192 case BUTTON_RIGHT | BUTTON_REPEAT:
193 if (line_edit)
195 if (editpos < len_utf8)
197 editpos++;
198 int c = utf8seek(text, editpos);
199 kbd_spellchar(text[c]);
202 else
204 if (++x >= linelen)
205 x = 0;
206 kbd_spellchar(line[x]);
208 break;
210 case BUTTON_LEFT:
211 case BUTTON_LEFT | BUTTON_REPEAT:
212 if (line_edit)
214 if (editpos)
216 editpos--;
217 int c = utf8seek(text, editpos);
218 kbd_spellchar(text[c]);
221 else
223 if (--x < 0)
224 x = linelen - 1;
225 kbd_spellchar(line[x]);
227 break;
229 case BUTTON_PLAY | BUTTON_REPEAT:
230 /* accepts what was entered and continues */
231 done = true;
232 break;
234 case BUTTON_PLAY | BUTTON_REL:
235 if (lastbutton != BUTTON_PLAY)
236 break;
237 if (line_edit) /* backspace in line_edit */
239 if (editpos > 0)
241 utf8 = text + utf8seek(text, editpos);
242 i = 0;
243 do {
244 i++;
245 utf8--;
246 } while ((*utf8 & MASK) == COMP);
247 while (utf8[i]) {
248 *utf8 = utf8[i];
249 utf8++;
251 *utf8 = 0;
252 editpos--;
255 else /* inserts the selected char */
257 utf8 = utf8encode(line[x], temptext);
258 *utf8 = 0;
259 j = strlen(temptext);
260 if (len + j < buflen)
262 int k = len_utf8;
263 for (i = len+j; k >= editpos; i--) {
264 text[i] = text[i-j];
265 if ((text[i] & MASK) != COMP)
266 k--;
268 while (j--)
269 text[i--] = temptext[j];
270 editpos++;
273 if (talk_menus_enabled()) /* voice UI? */
274 talk_spell(text, false); /* speak revised text */
275 break;
277 case BUTTON_NONE:
278 gui_syncstatusbar_draw(&statusbars, false);
279 redraw = false;
280 break;
282 default:
283 default_event_handler(button);
284 break;
286 if (button != BUTTON_NONE)
287 lastbutton = button;
290 return 0;