Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / apps / player / keyboard.c
blob42b93f83bd8b7f5bf1e78e7ec32f997d3580c82e
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 "sprintf.h"
27 #include <string.h>
28 #include "settings.h"
29 #include "statusbar.h"
30 #include "talk.h"
31 #include "misc.h"
32 #include "rbunicode.h"
33 #include "lang.h"
35 #define KBD_BUF_SIZE 64
36 #define KEYBOARD_PAGES 3
38 static unsigned short *kbd_setupkeys(int page, int* len)
40 static unsigned short kbdline[KBD_BUF_SIZE];
41 const unsigned char *p;
42 int i = 0;
44 switch (page)
46 case 0: /* Capitals */
47 p = "ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅ"
48 "ÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ";
49 break;
51 case 1: /* Small */
52 p = "abcdefghijklmnopqrstuvwxyzßàáâãä"
53 "åçèéêëìíîïñòóôõöøùúûüýÿ";
54 break;
56 default: /* Others */
57 p = " !\"#$%&'()*+,-./0123456789:;<=>?@[]_{}~";
58 break;
61 while (*p)
62 p = utf8decode(p, &kbdline[i++]);
64 *len = i;
66 return kbdline;
69 /* Delimiters for highlighting the character selected for insertion */
70 #define KEYBOARD_INSERT_LEFT 0xe110
71 #define KEYBOARD_INSERT_RIGHT 0xe10f
73 #define KEYBOARD_CURSOR 0x7f
74 #define KEYBOARD_ARROW 0xe10c
76 /* helper function to spell a char if voice UI is enabled */
77 static void kbd_spellchar(unsigned short c)
79 if (global_settings.talk_menu) /* voice UI? */
81 unsigned char tmp[5];
82 /* store char to pass to talk_spell */
83 unsigned char* utf8 = utf8encode(c, tmp);
84 *utf8 = 0;
86 if(c == ' ')
87 talk_id(VOICE_BLANK, false);
88 else
89 talk_spell(tmp, false);
93 static void say_edit(void)
95 if (global_settings.talk_menu)
96 talk_id(VOICE_EDIT, false);
99 int kbd_input(char* text, int buflen)
101 bool done = false;
102 bool redraw = true;
103 bool line_edit = false;
104 int page = 0, x = 0;
105 int linelen;
107 int len, len_utf8, i, j;
108 int editpos, curpos, leftpos;
109 unsigned short *line = kbd_setupkeys(page, &linelen);
110 unsigned char temptext[36];
111 unsigned char *utf8;
113 int button, lastbutton = 0;
115 editpos = utf8length(text);
117 if (global_settings.talk_menu) /* voice UI? */
118 talk_spell(text, true); /* spell initial text */
120 while (!done)
122 len = strlen(text);
123 len_utf8 = utf8length(text);
125 if (redraw)
127 if (line_edit)
129 lcd_putc(0, 0, ' ');
130 lcd_putc(0, 1, KEYBOARD_ARROW);
132 else
134 lcd_putc(0, 0, KEYBOARD_ARROW);
135 lcd_putc(0, 1, ' ');
138 lcd_putc(1, 0, KEYBOARD_INSERT_LEFT);
139 lcd_putc(2, 0, line[x]);
140 lcd_putc(3, 0, KEYBOARD_INSERT_RIGHT);
141 for (i = 1; i < 8; i++)
143 lcd_putc(i + 3, 0, line[(x+i)%linelen]);
146 /* write out the text */
147 curpos = MIN(MIN(editpos, 10 - MIN(len_utf8 - editpos, 3)), 9);
148 leftpos = editpos - curpos;
149 if (!leftpos) {
150 utf8 = text + utf8seek(text, leftpos);
151 i = 0;
152 j = 0;
153 } else {
154 temptext[0] = '<';
155 i = 1;
156 j = 1;
157 utf8 = text + utf8seek(text, leftpos+1);
159 while (*utf8 && i < 10) {
160 temptext[j++] = *utf8++;
161 if ((*utf8 & MASK) != COMP)
162 i++;
164 temptext[j] = 0;
167 if (len_utf8 - leftpos > 10) {
168 utf8 = temptext + utf8seek(temptext, 9);
169 *utf8++ = '>';
170 *utf8 = 0;
173 lcd_remove_cursor();
174 lcd_puts(1, 1, temptext);
175 lcd_put_cursor(curpos + 1, 1, KEYBOARD_CURSOR);
177 gui_syncstatusbar_draw(&statusbars, true);
180 /* The default action is to redraw */
181 redraw = true;
183 button = button_get_w_tmo(HZ/2);
184 switch (button)
186 case BUTTON_STOP: /* abort */
187 return -1;
188 break;
190 case BUTTON_MENU: /* page flip */
191 if (++page == KEYBOARD_PAGES)
192 page = 0;
193 line = kbd_setupkeys(page, &linelen);
194 if (x > linelen - 1)
195 x = linelen - 1;
196 kbd_spellchar(line[x]);
197 break;
199 case BUTTON_ON: /* toggle mode */
200 line_edit = !line_edit;
201 if (line_edit)
202 say_edit();
203 else
204 kbd_spellchar(line[x]);
205 break;
207 case BUTTON_RIGHT:
208 case BUTTON_RIGHT | BUTTON_REPEAT:
209 if (line_edit)
211 if (editpos < len_utf8)
213 editpos++;
214 int c = utf8seek(text, editpos);
215 kbd_spellchar(text[c]);
218 else
220 if (++x >= linelen)
221 x = 0;
222 kbd_spellchar(line[x]);
224 break;
226 case BUTTON_LEFT:
227 case BUTTON_LEFT | BUTTON_REPEAT:
228 if (line_edit)
230 if (editpos)
232 editpos--;
233 int c = utf8seek(text, editpos);
234 kbd_spellchar(text[c]);
237 else
239 if (--x < 0)
240 x = linelen - 1;
241 kbd_spellchar(line[x]);
243 break;
245 case BUTTON_PLAY | BUTTON_REPEAT:
246 /* accepts what was entered and continues */
247 done = true;
248 break;
250 case BUTTON_PLAY | BUTTON_REL:
251 if (lastbutton != BUTTON_PLAY)
252 break;
253 if (line_edit) /* backspace in line_edit */
255 if (editpos > 0)
257 utf8 = text + utf8seek(text, editpos);
258 i = 0;
259 do {
260 i++;
261 utf8--;
262 } while ((*utf8 & MASK) == COMP);
263 while (utf8[i]) {
264 *utf8 = utf8[i];
265 utf8++;
267 *utf8 = 0;
268 editpos--;
271 else /* inserts the selected char */
273 utf8 = utf8encode(line[x], temptext);
274 *utf8 = 0;
275 j = strlen(temptext);
276 if (len + j < buflen)
278 int k = len_utf8;
279 for (i = len+j; k >= editpos; i--) {
280 text[i] = text[i-j];
281 if ((text[i] & MASK) != COMP)
282 k--;
284 while (j--)
285 text[i--] = temptext[j];
286 editpos++;
289 if (global_settings.talk_menu) /* voice UI? */
290 talk_spell(text, false); /* speak revised text */
291 break;
293 case BUTTON_NONE:
294 gui_syncstatusbar_draw(&statusbars, false);
295 redraw = false;
296 break;
298 default:
299 default_event_handler(button);
300 break;
302 if (button != BUTTON_NONE)
303 lastbutton = button;
306 return 0;