Add the thread state check into test_codec as used in mpegplayer which makes it shoul...
[Rockbox.git] / apps / player / keyboard.c
blobc89161c8caca47df5af8c417f61841bb588c0150
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"
31 #include "lang.h"
33 #define KBD_BUF_SIZE 64
34 #define KEYBOARD_PAGES 3
36 static unsigned short *kbd_setupkeys(int page, int* len)
38 static unsigned short kbdline[KBD_BUF_SIZE];
39 const unsigned char *p;
40 int i = 0;
42 switch (page)
44 case 0: /* Capitals */
45 p = "ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅ"
46 "ÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ";
47 break;
49 case 1: /* Small */
50 p = "abcdefghijklmnopqrstuvwxyzßàáâãä"
51 "åçèéêëìíîïñòóôõöøùúûüýÿ";
52 break;
54 default: /* Others */
55 p = " !\"#$%&'()*+,-./0123456789:;<=>?@[]_{}~";
56 break;
59 while (*p)
60 p = utf8decode(p, &kbdline[i++]);
62 *len = i;
64 return kbdline;
67 /* Delimiters for highlighting the character selected for insertion */
68 #define KEYBOARD_INSERT_LEFT 0xe110
69 #define KEYBOARD_INSERT_RIGHT 0xe10f
71 #define KEYBOARD_CURSOR 0x7f
72 #define KEYBOARD_ARROW 0xe10c
74 /* helper function to spell a char if voice UI is enabled */
75 static void kbd_spellchar(char c)
77 if (talk_menus_enabled()) /* voice UI? */
79 unsigned char tmp[5];
80 /* store char to pass to talk_spell */
81 unsigned char* utf8 = utf8encode(c, tmp);
82 *utf8 = 0;
84 if(c == ' ')
85 talk_id(VOICE_BLANK, false);
86 else
87 talk_spell(tmp, false);
91 static void say_edit(void)
93 if (talk_menus_enabled())
94 talk_id(VOICE_EDIT, false);
97 int kbd_input(char* text, int buflen)
99 bool done = false;
100 bool redraw = true;
101 bool line_edit = false;
102 int page = 0, x = 0;
103 int linelen;
105 int len, len_utf8, i, j;
106 int editpos, curpos, leftpos;
107 unsigned short *line = kbd_setupkeys(page, &linelen);
108 unsigned char temptext[36];
109 unsigned char *utf8;
111 int button, lastbutton = 0;
113 editpos = utf8length(text);
115 if (talk_menus_enabled()) /* voice UI? */
116 talk_spell(text, true); /* spell initial text */
118 while (!done)
120 len = strlen(text);
121 len_utf8 = utf8length(text);
123 if (redraw)
125 if (line_edit)
127 lcd_putc(0, 0, ' ');
128 lcd_putc(0, 1, KEYBOARD_ARROW);
130 else
132 lcd_putc(0, 0, KEYBOARD_ARROW);
133 lcd_putc(0, 1, ' ');
136 lcd_putc(1, 0, KEYBOARD_INSERT_LEFT);
137 lcd_putc(2, 0, line[x]);
138 lcd_putc(3, 0, KEYBOARD_INSERT_RIGHT);
139 for (i = 1; i < 8; i++)
141 lcd_putc(i + 3, 0, line[(x+i)%linelen]);
144 /* write out the text */
145 curpos = MIN(MIN(editpos, 10 - MIN(len_utf8 - editpos, 3)), 9);
146 leftpos = editpos - curpos;
147 if (!leftpos) {
148 utf8 = text + utf8seek(text, leftpos);
149 i = 0;
150 j = 0;
151 } else {
152 temptext[0] = '<';
153 i = 1;
154 j = 1;
155 utf8 = text + utf8seek(text, leftpos+1);
157 while (*utf8 && i < 10) {
158 temptext[j++] = *utf8++;
159 if ((*utf8 & MASK) != COMP)
160 i++;
162 temptext[j] = 0;
165 if (len_utf8 - leftpos > 10) {
166 utf8 = temptext + utf8seek(temptext, 9);
167 *utf8++ = '>';
168 *utf8 = 0;
171 lcd_remove_cursor();
172 lcd_puts(1, 1, temptext);
173 lcd_put_cursor(curpos + 1, 1, KEYBOARD_CURSOR);
175 gui_syncstatusbar_draw(&statusbars, true);
178 /* The default action is to redraw */
179 redraw = true;
181 button = button_get_w_tmo(HZ/2);
182 switch (button)
184 case BUTTON_STOP: /* abort */
185 return -1;
186 break;
188 case BUTTON_MENU: /* page flip */
189 if (++page == KEYBOARD_PAGES)
190 page = 0;
191 line = kbd_setupkeys(page, &linelen);
192 if (x > linelen - 1)
193 x = linelen - 1;
194 kbd_spellchar(line[x]);
195 break;
197 case BUTTON_ON: /* toggle mode */
198 line_edit = !line_edit;
199 if (line_edit)
200 say_edit();
201 else
202 kbd_spellchar(line[x]);
203 break;
205 case BUTTON_RIGHT:
206 case BUTTON_RIGHT | BUTTON_REPEAT:
207 if (line_edit)
209 if (editpos < len_utf8)
211 editpos++;
212 int c = utf8seek(text, editpos);
213 kbd_spellchar(text[c]);
216 else
218 if (++x >= linelen)
219 x = 0;
220 kbd_spellchar(line[x]);
222 break;
224 case BUTTON_LEFT:
225 case BUTTON_LEFT | BUTTON_REPEAT:
226 if (line_edit)
228 if (editpos)
230 editpos--;
231 int c = utf8seek(text, editpos);
232 kbd_spellchar(text[c]);
235 else
237 if (--x < 0)
238 x = linelen - 1;
239 kbd_spellchar(line[x]);
241 break;
243 case BUTTON_PLAY | BUTTON_REPEAT:
244 /* accepts what was entered and continues */
245 done = true;
246 break;
248 case BUTTON_PLAY | BUTTON_REL:
249 if (lastbutton != BUTTON_PLAY)
250 break;
251 if (line_edit) /* backspace in line_edit */
253 if (editpos > 0)
255 utf8 = text + utf8seek(text, editpos);
256 i = 0;
257 do {
258 i++;
259 utf8--;
260 } while ((*utf8 & MASK) == COMP);
261 while (utf8[i]) {
262 *utf8 = utf8[i];
263 utf8++;
265 *utf8 = 0;
266 editpos--;
269 else /* inserts the selected char */
271 utf8 = utf8encode(line[x], temptext);
272 *utf8 = 0;
273 j = strlen(temptext);
274 if (len + j < buflen)
276 int k = len_utf8;
277 for (i = len+j; k >= editpos; i--) {
278 text[i] = text[i-j];
279 if ((text[i] & MASK) != COMP)
280 k--;
282 while (j--)
283 text[i--] = temptext[j];
284 editpos++;
287 if (talk_menus_enabled()) /* voice UI? */
288 talk_spell(text, false); /* speak revised text */
289 break;
291 case BUTTON_NONE:
292 gui_syncstatusbar_draw(&statusbars, false);
293 redraw = false;
294 break;
296 default:
297 default_event_handler(button);
298 break;
300 if (button != BUTTON_NONE)
301 lastbutton = button;
304 return 0;