Remove plugin-specific defines from the pdbox makefile, and add them to m_pd.h, which...
[kugel-rb.git] / apps / plugins / zxbox / zxbox_keyb.c
blobbc7e3aca2ce44f8588284b48a7b706a9c13bcb2e
1 #include "zxconfig.h"
3 #ifndef O_BINARY
4 #define O_BINARY 0
5 #endif
7 #if CONFIG_KEYPAD == RECORDER_PAD
8 #define BUTTONBAR_HEIGHT 8
9 #else
10 #define BUTTONBAR_HEIGHT 0
11 #endif
13 #define DEFAULT_MARGIN 6
14 #define KBD_BUF_SIZE 500
15 #define kbd_loaded false
16 #define statusbar_size 0
18 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
19 (CONFIG_KEYPAD == IRIVER_H300_PAD)
20 #define KBD_SELECT BUTTON_SELECT
21 #define KBD_ABORT BUTTON_OFF
22 #define KBD_LEFT BUTTON_LEFT
23 #define KBD_RIGHT BUTTON_RIGHT
24 #define KBD_UP BUTTON_UP
25 #define KBD_DOWN BUTTON_DOWN
27 #elif CONFIG_KEYPAD == RECORDER_PAD
28 #define KBD_SELECT BUTTON_PLAY
29 #define KBD_ABORT BUTTON_OFF
30 #define KBD_LEFT BUTTON_LEFT
31 #define KBD_RIGHT BUTTON_RIGHT
32 #define KBD_UP BUTTON_UP
33 #define KBD_DOWN BUTTON_DOWN
35 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
36 #define KBD_SELECT BUTTON_SELECT
37 #define KBD_ABORT BUTTON_OFF
38 #define KBD_LEFT BUTTON_LEFT
39 #define KBD_RIGHT BUTTON_RIGHT
40 #define KBD_UP BUTTON_UP
41 #define KBD_DOWN BUTTON_DOWN
43 #elif CONFIG_KEYPAD == ONDIO_PAD /* restricted Ondio keypad */
44 #define KBD_SELECT BUTTON_MENU
45 #define KBD_ABORT BUTTON_OFF
46 #define KBD_LEFT BUTTON_LEFT
47 #define KBD_RIGHT BUTTON_RIGHT
48 #define KBD_UP BUTTON_UP
49 #define KBD_DOWN BUTTON_DOWN
51 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
52 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
53 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
55 #define KBD_SELECT BUTTON_SELECT
56 #define KBD_ABORT BUTTON_MENU
57 #define KBD_LEFT BUTTON_LEFT
58 #define KBD_RIGHT BUTTON_RIGHT
59 #define KBD_UP BUTTON_SCROLL_BACK
60 #define KBD_DOWN BUTTON_SCROLL_FWD
62 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
64 /* TODO: Check keyboard mappings */
66 #define KBD_SELECT BUTTON_SELECT
67 #define KBD_ABORT BUTTON_PLAY
68 #define KBD_LEFT BUTTON_LEFT
69 #define KBD_RIGHT BUTTON_RIGHT
70 #define KBD_UP BUTTON_UP
71 #define KBD_DOWN BUTTON_DOWN
73 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
75 /* TODO: Check keyboard mappings */
77 #define KBD_SELECT BUTTON_SELECT
78 #define KBD_ABORT BUTTON_REC
79 #define KBD_LEFT BUTTON_LEFT
80 #define KBD_RIGHT BUTTON_RIGHT
81 #define KBD_UP BUTTON_UP
82 #define KBD_DOWN BUTTON_DOWN
84 #elif CONFIG_KEYPAD == GIGABEAT_PAD
86 #define KBD_SELECT BUTTON_SELECT
87 #define KBD_ABORT BUTTON_POWER
88 #define KBD_LEFT BUTTON_LEFT
89 #define KBD_RIGHT BUTTON_RIGHT
90 #define KBD_UP BUTTON_UP
91 #define KBD_DOWN BUTTON_DOWN
93 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
95 #define KBD_SELECT BUTTON_SELECT
96 #define KBD_ABORT BUTTON_POWER
97 #define KBD_LEFT BUTTON_LEFT
98 #define KBD_RIGHT BUTTON_RIGHT
99 #define KBD_UP BUTTON_UP
100 #define KBD_DOWN BUTTON_DOWN
102 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
104 /* TODO: Check keyboard mappings */
106 #define KBD_SELECT BUTTON_REW
107 #define KBD_ABORT BUTTON_FF
108 #define KBD_LEFT BUTTON_LEFT
109 #define KBD_RIGHT BUTTON_RIGHT
110 #define KBD_UP BUTTON_SCROLL_UP
111 #define KBD_DOWN BUTTON_SCROLL_DOWN
113 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
114 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
115 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
116 (CONFIG_KEYPAD == SANSA_M200_PAD) || \
117 (CONFIG_KEYPAD == SANSA_FUZE_PAD) || \
118 (CONFIG_KEYPAD == MROBE100_PAD)
120 /* TODO: Check keyboard mappings */
122 #define KBD_SELECT BUTTON_SELECT
123 #define KBD_ABORT BUTTON_POWER
124 #define KBD_LEFT BUTTON_LEFT
125 #define KBD_RIGHT BUTTON_RIGHT
126 #define KBD_UP BUTTON_UP
127 #define KBD_DOWN BUTTON_DOWN
129 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
131 #define KBD_SELECT BUTTON_RC_PLAY
132 #define KBD_ABORT BUTTON_RC_REC
133 #define KBD_LEFT BUTTON_RC_REW
134 #define KBD_RIGHT BUTTON_RC_FF
135 #define KBD_UP BUTTON_RC_VOL_UP
136 #define KBD_DOWN BUTTON_RC_VOL_DOWN
138 #elif CONFIG_KEYPAD == COWON_D2_PAD
140 #define KBD_ABORT BUTTON_POWER
142 #elif CONFIG_KEYPAD == IAUDIO67_PAD
144 #define KBD_SELECT BUTTON_MENU
145 #define KBD_ABORT BUTTON_POWER
146 #define KBD_LEFT BUTTON_LEFT
147 #define KBD_RIGHT BUTTON_RIGHT
148 #define KBD_UP BUTTON_STOP
149 #define KBD_DOWN BUTTON_PLAY
151 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
153 #define KBD_SELECT BUTTON_SELECT
154 #define KBD_ABORT BUTTON_BACK
155 #define KBD_LEFT BUTTON_LEFT
156 #define KBD_RIGHT BUTTON_RIGHT
157 #define KBD_UP BUTTON_UP
158 #define KBD_DOWN BUTTON_DOWN
160 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
162 #define KBD_SELECT BUTTON_MENU
163 #define KBD_ABORT BUTTON_POWER
164 #define KBD_LEFT BUTTON_LEFT
165 #define KBD_RIGHT BUTTON_RIGHT
166 #define KBD_UP BUTTON_UP
167 #define KBD_DOWN BUTTON_DOWN
169 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
171 #define KBD_SELECT BUTTON_PLAY
172 #define KBD_ABORT BUTTON_REW
173 #define KBD_LEFT BUTTON_LEFT
174 #define KBD_RIGHT BUTTON_RIGHT
175 #define KBD_UP BUTTON_UP
176 #define KBD_DOWN BUTTON_DOWN
178 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
180 #define KBD_SELECT BUTTON_OK
181 #define KBD_ABORT BUTTON_CANCEL
182 #define KBD_LEFT BUTTON_PREV
183 #define KBD_RIGHT BUTTON_NEXT
184 #define KBD_UP BUTTON_UP
185 #define KBD_DOWN BUTTON_DOWN
187 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
189 #define KBD_SELECT BUTTON_SELECT
190 #define KBD_ABORT BUTTON_REC
191 #define KBD_LEFT BUTTON_VOL_DOWN
192 #define KBD_RIGHT BUTTON_VOL_UP
193 #define KBD_UP BUTTON_PREV
194 #define KBD_DOWN BUTTON_NEXT
196 #endif
198 #ifdef HAVE_TOUCHSCREEN
199 #ifndef KBD_SELECT
200 #define KBD_SELECT BUTTON_CENTER
201 #endif
202 #ifndef KBD_ABORT
203 #define KBD_ABORT BUTTON_TOPLEFT
204 #endif
205 #ifndef KBD_LEFT
206 #define KBD_LEFT BUTTON_MIDLEFT
207 #endif
208 #ifndef KBD_RIGHT
209 #define KBD_RIGHT BUTTON_MIDRIGHT
210 #endif
211 #ifndef KBD_UP
212 #define KBD_UP BUTTON_TOPMIDDLE
213 #endif
214 #ifndef KBD_DOWN
215 #define KBD_DOWN BUTTON_BOTTOMMIDDLE
216 #endif
217 #endif
219 struct keyboard_parameters {
220 const unsigned char* default_kbd;
221 int DEFAULT_LINES;
222 unsigned short kbd_buf[KBD_BUF_SIZE];
223 int nchars;
224 int font_w;
225 int font_h;
226 struct font* font;
227 int curfont;
228 int main_x;
229 int main_y;
230 int max_chars;
231 int max_chars_text;
232 int lines;
233 int pages;
234 int keyboard_margin;
235 int old_main_y;
236 int curpos;
237 int leftpos;
238 int page;
239 int x;
240 int y;
243 struct keyboard_parameters param[NB_SCREENS];
245 int zx_kbd_input(char* text/*, int buflen*/)
247 bool done = false;
248 int i, j, k, w, l;
249 int text_w = 0;
250 int len_utf8/*, c = 0*/;
251 int editpos;
252 /* int statusbar_size = global_settings.statusbar ? STATUSBAR_HEIGHT : 0;*/
253 unsigned short ch/*, tmp, hlead = 0, hvowel = 0, htail = 0*/;
254 /*bool hangul = false;*/
255 unsigned char *utf8;
256 const unsigned char *p;
257 bool cur_blink = true;
258 int char_screen = 0;
260 FOR_NB_SCREENS(l)
262 param[l].default_kbd =
263 "1234567890\n"
264 "qwertyuiop\n"
265 "asdfghjkl\n"
266 "zxcvbnm\n"
267 "S\n"
268 "E";
270 param[l].DEFAULT_LINES = 7;
273 char outline[256];
274 int button, lastbutton = 0;
275 FOR_NB_SCREENS(l)
277 /* Copy default keyboard to buffer */
278 i = 0;
279 param[l].curfont = FONT_SYSFIXED;
280 p = param[l].default_kbd;
281 while (*p != 0)
282 p = rb->utf8decode(p, &param[l].kbd_buf[i++]);
283 param[l].nchars = i;
284 /* param[l].curfont = FONT_UI;*/
286 FOR_NB_SCREENS(l)
288 param[l].font = rb->font_get(param[l].curfont);
289 param[l].font_h = param[l].font->height;
291 /* check if FONT_UI fits the screen */
292 if (2*param[l].font_h+3 + BUTTONBAR_HEIGHT >
293 rb->screens[l]->getheight()) {
294 param[l].font = rb->font_get(FONT_SYSFIXED);
295 param[l].font_h = param[l].font->height;
296 param[l].curfont = FONT_SYSFIXED;
299 rb->screens[l]->setfont(param[l].curfont);
300 /* find max width of keyboard glyphs */
301 for (i=0; i<param[l].nchars; i++) {
302 w = rb->font_get_width(param[l].font, param[l].kbd_buf[i]);
303 if (w > param[l].font_w)
304 param[l].font_w = w;
306 /* Since we're going to be adding spaces, make sure that we check
307 * their width too */
308 w = rb->font_get_width( param[l].font, ' ' );
309 if( w > param[l].font_w )
310 param[l].font_w = w;
312 FOR_NB_SCREENS(l)
314 i = 0;
315 /* Pad lines with spaces */
316 while( i < param[l].nchars )
318 if( param[l].kbd_buf[i] == '\n' )
320 k = ( rb->screens[l]->getwidth() / param[l].font_w ) -
321 i % ( rb->screens[l]->getwidth() / param[l].font_w ) - 1;
322 if( k == ( rb->screens[l]->getwidth() / param[l].font_w ) - 1 )
324 param[l].nchars--;
325 for( j = i; j < param[l].nchars; j++ )
327 param[l].kbd_buf[j] = param[l].kbd_buf[j+1];
330 else
332 if( param[l].nchars + k - 1 >= KBD_BUF_SIZE )
333 { /* We don't want to overflow the buffer */
334 k = KBD_BUF_SIZE - param[l].nchars;
336 for( j = param[l].nchars + k - 1; j > i+k; j-- )
338 param[l].kbd_buf[j] = param[l].kbd_buf[j-k];
340 param[l].nchars += k;
341 k++;
342 while( k-- )
344 param[l].kbd_buf[i++] = ' ';
348 else
349 i++;
353 /* find max width for text string */
354 utf8 = text;
355 FOR_NB_SCREENS(l)
357 text_w = param[l].font_w;
358 while (*utf8) {
359 utf8 = (unsigned char*)rb->utf8decode(utf8, &ch);
360 w = rb->font_get_width(param[l].font, ch);
361 if (w > text_w)
362 text_w = w;
364 param[l].max_chars_text = rb->screens[l]->getwidth() / text_w - 2;
366 /* calculate keyboard grid size */
367 param[l].max_chars = rb->screens[l]->getwidth() / param[l].font_w;
368 if (!kbd_loaded) {
369 param[l].lines = param[l].DEFAULT_LINES;
370 param[l].keyboard_margin = DEFAULT_MARGIN;
371 } else {
372 param[l].lines = (rb->screens[l]->lcdheight - BUTTONBAR_HEIGHT -
373 statusbar_size) / param[l].font_h - 1;
374 param[l].keyboard_margin = rb->screens[l]->lcdheight -
375 BUTTONBAR_HEIGHT - statusbar_size -
376 (param[l].lines+1)*param[l].font_h;
377 if (param[l].keyboard_margin < 3) {
378 param[l].lines--;
379 param[l].keyboard_margin += param[l].font_h;
381 if (param[l].keyboard_margin > 6)
382 param[l].keyboard_margin = 6;
385 param[l].pages = (param[l].nchars + (param[l].lines*param[l].max_chars-1))
386 /(param[l].lines*param[l].max_chars);
387 if (param[l].pages == 1 && kbd_loaded)
388 param[l].lines = (param[l].nchars + param[l].max_chars - 1) / param[l].max_chars;
390 param[l].main_y = param[l].font_h*param[l].lines + param[l].keyboard_margin + statusbar_size;
391 param[l].main_x = 0;
392 param[l].keyboard_margin -= param[l].keyboard_margin/2;
395 editpos = rb->utf8length(text);
399 while(!done)
401 len_utf8 = rb->utf8length(text);
402 FOR_NB_SCREENS(l)
403 rb->screens[l]->clear_display();
406 /* draw page */
407 FOR_NB_SCREENS(l)
409 rb->screens[l]->setfont(param[l].curfont);
410 k = param[l].page*param[l].max_chars*param[l].lines;
411 for (i=j=0; j < param[l].lines && k < param[l].nchars; k++) {
412 utf8 = rb->utf8encode(param[l].kbd_buf[k], outline);
413 *utf8 = 0;
414 rb->screens[l]->getstringsize(outline, &w, NULL);
415 rb->screens[l]->putsxy(i*param[l].font_w + (param[l].font_w-w)/2, j*param[l].font_h
416 + statusbar_size, outline);
417 if (++i == param[l].max_chars) {
418 i = 0;
419 j++;
425 /* separator */
426 FOR_NB_SCREENS(l)
428 rb->screens[l]->hline(0, rb->screens[l]->getwidth() - 1,
429 param[l].main_y - param[l].keyboard_margin);
431 /* write out the text */
432 #if 0
433 rb->screens[l]->setfont(param[l].curfont);
435 i=j=0;
436 param[l].curpos = MIN(editpos, param[l].max_chars_text
437 - MIN(len_utf8 - editpos, 2));
438 param[l].leftpos = editpos - param[l].curpos;
439 utf8 = text + rb->utf8seek(text, param[l].leftpos);
441 text_w = param[l].font_w;
442 while (*utf8 && i < param[l].max_chars_text) {
443 outline[j++] = *utf8++;
444 if ((*utf8 & MASK) != COMP) {
445 outline[j] = 0;
446 j=0;
447 i++;
448 rb->screens[l]->getstringsize(outline, &w, NULL);
449 rb->screens[l]->putsxy(i*text_w + (text_w-w)/2, param[l].main_y, outline);
453 if (param[l].leftpos) {
454 rb->screens[l]->getstringsize("<", &w, NULL);
455 rb->screens[l]->putsxy(text_w - w, param[l].main_y, "<");
457 if (len_utf8 - param[l].leftpos > param[l].max_chars_text)
458 rb->screens[l]->putsxy(rb->screens[l]->width - text_w, param[l].main_y, ">");
460 /* cursor */
461 i = (param[l].curpos + 1) * text_w;
462 if (cur_blink)
463 rb->screens[l]->vline(i, param[l].main_y, param[l].main_y + param[l].font_h-1);
465 if (hangul) /* draw underbar */
466 rb->screens[l]->hline(param[l].curpos*text_w, (param[l].curpos+1)*text_w,
467 param[l].main_y+param[l].font_h-1);
468 #endif
470 cur_blink = !cur_blink;
473 /* highlight the key that has focus */
474 FOR_NB_SCREENS(l)
476 rb->screens[l]->set_drawmode(DRMODE_COMPLEMENT);
477 rb->screens[l]->fillrect(param[l].font_w * param[l].x,
478 statusbar_size + param[l].font_h * param[l].y,
479 param[l].font_w, param[l].font_h);
480 rb->screens[l]->set_drawmode(DRMODE_SOLID);
483 FOR_NB_SCREENS(l)
484 rb->screens[l]->update();
486 button = rb->button_get_w_tmo(HZ/2);
488 switch ( button ) {
490 case KBD_ABORT:
491 FOR_NB_SCREENS(l)
492 rb->screens[l]->setfont(FONT_UI);
494 return -1;
495 break;
497 case KBD_RIGHT:
498 case KBD_RIGHT | BUTTON_REPEAT:
500 FOR_NB_SCREENS(l)
502 if (++param[l].x == param[l].max_chars) {
503 param[l].x = 0;
504 /* no dedicated flip key - flip page on wrap */
505 if (++param[l].page == param[l].pages)
506 param[l].page = 0;
508 k = (param[l].page*param[l].lines + param[l].y)*param[l].max_chars + param[l].x;
509 /*kbd_spellchar(param[l].kbd_buf[k]);*/
512 break;
513 case KBD_LEFT:
514 case KBD_LEFT | BUTTON_REPEAT:
516 FOR_NB_SCREENS(l)
518 if (param[l].x)
519 param[l].x--;
520 else
522 /* no dedicated flip key - flip page on wrap */
523 if (--param[l].page < 0)
524 param[l].page = (param[l].pages-1);
525 param[l].x = param[l].max_chars - 1;
527 k = (param[l].page*param[l].lines +
528 param[l].y)*param[l].max_chars + param[l].x;
529 /* kbd_spellchar(param[l].kbd_buf[k]);*/
532 break;
534 case KBD_DOWN:
535 case KBD_DOWN | BUTTON_REPEAT:
537 FOR_NB_SCREENS(l)
539 if (param[l].y < param[l].lines - 1)
540 param[l].y++;
541 else
542 param[l].y=0;
544 FOR_NB_SCREENS(l)
546 k = (param[l].page*param[l].lines + param[l].y)*
547 param[l].max_chars + param[l].x;
548 /*kbd_spellchar(param[l].kbd_buf[k]);*/
551 break;
553 case KBD_UP:
554 case KBD_UP | BUTTON_REPEAT:
556 FOR_NB_SCREENS(l)
558 if (param[l].y)
559 param[l].y--;
560 else
561 param[l].y = param[l].lines - 1;
563 FOR_NB_SCREENS(l)
565 k = (param[l].page*param[l].lines + param[l].y)*
566 param[l].max_chars + param[l].x;
567 /*kbd_spellchar(param[l].kbd_buf[k]);*/
570 break;
572 case KBD_SELECT:
574 if (button == KBD_SELECT)
575 char_screen = 0;
577 /* inserts the selected char */
579 /* find input char */
580 k = (param[char_screen].page*param[char_screen].lines +
581 param[char_screen].y)*param[char_screen].max_chars +
582 param[char_screen].x;
583 if (k < param[char_screen].nchars)
584 ch = param[char_screen].kbd_buf[k];
585 else
586 ch = ' ';
587 text[0]=ch;
588 done = true;
590 break;
593 default:
594 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
595 FOR_NB_SCREENS(l)
596 rb->screens[l]->setfont(FONT_SYSFIXED);
597 break;
600 if (button != BUTTON_NONE)
602 lastbutton = button;
603 cur_blink = true;
606 FOR_NB_SCREENS(l)
607 rb->screens[l]->setfont(FONT_UI);
608 return 0;