skin engine softlock support for touchscreens:
[maemo-rb.git] / apps / gui / skin_engine / skin_parser.c
blob659d974130cc474f06648e6e51f95e74af189d72
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Nicolas Pennequin, Dan Everton, Matthias Mohr
11 * 2010 Jonathan Gordon
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include "config.h"
27 #include "file.h"
28 #include "misc.h"
29 #include "plugin.h"
30 #include "viewport.h"
32 #include "skin_buffer.h"
33 #include "skin_parser.h"
34 #include "tag_table.h"
36 #ifdef __PCTOOL__
37 #ifdef WPSEDITOR
38 #include "proxy.h"
39 #include "sysfont.h"
40 #else
41 #include "action.h"
42 #include "checkwps.h"
43 #include "audio.h"
44 #define lang_is_rtl() (false)
45 #define DEBUGF printf
46 #endif /*WPSEDITOR*/
47 #else
48 #include "debug.h"
49 #include "language.h"
50 #endif /*__PCTOOL__*/
52 #include <ctype.h>
53 #include <stdbool.h>
54 #include "font.h"
56 #include "wps_internals.h"
57 #include "skin_engine.h"
58 #include "settings.h"
59 #include "settings_list.h"
60 #if CONFIG_TUNER
61 #include "radio.h"
62 #include "tuner.h"
63 #endif
64 #include "skin_fonts.h"
66 #ifdef HAVE_LCD_BITMAP
67 #include "bmp.h"
68 #endif
70 #ifdef HAVE_ALBUMART
71 #include "playback.h"
72 #endif
74 #include "backdrop.h"
75 #include "statusbar-skinned.h"
77 #define WPS_ERROR_INVALID_PARAM -1
80 static bool isdefault(struct skin_tag_parameter *param)
82 return param->type == DEFAULT;
86 /* which screen are we parsing for? */
87 static enum screen_type curr_screen;
89 /* the current viewport */
90 static struct skin_element *curr_viewport_element;
91 static struct skin_viewport *curr_vp;
93 static struct line *curr_line;
95 static int follow_lang_direction = 0;
97 typedef int (*parse_function)(struct skin_element *element,
98 struct wps_token *token,
99 struct wps_data *wps_data);
101 #ifdef HAVE_LCD_BITMAP
102 /* add a skin_token_list item to the list chain. ALWAYS appended because some of the
103 * chains require the order to be kept.
105 static void add_to_ll_chain(struct skin_token_list **list, struct skin_token_list *item)
107 if (*list == NULL)
108 *list = item;
109 else
111 struct skin_token_list *t = *list;
112 while (t->next)
113 t = t->next;
114 t->next = item;
118 #endif
121 void *skin_find_item(const char *label, enum skin_find_what what,
122 struct wps_data *data)
124 const char *itemlabel = NULL;
125 union {
126 struct skin_token_list *linkedlist;
127 struct skin_element *vplist;
128 } list = {NULL};
129 bool isvplist = false;
130 void *ret = NULL;
131 switch (what)
133 case SKIN_FIND_UIVP:
134 case SKIN_FIND_VP:
135 list.vplist = data->tree;
136 isvplist = true;
137 break;
138 #ifdef HAVE_LCD_BITMAP
139 case SKIN_FIND_IMAGE:
140 list.linkedlist = data->images;
141 break;
142 #endif
143 #ifdef HAVE_TOUCHSCREEN
144 case SKIN_FIND_TOUCHREGION:
145 list.linkedlist = data->touchregions;
146 break;
147 #endif
148 #ifdef HAVE_SKIN_VARIABLES
149 case SKIN_VARIABLE:
150 list.linkedlist = data->skinvars;
151 break;
152 #endif
155 while (list.linkedlist)
157 bool skip = false;
158 switch (what)
160 case SKIN_FIND_UIVP:
161 case SKIN_FIND_VP:
162 ret = list.vplist->data;
163 itemlabel = ((struct skin_viewport *)ret)->label;
164 skip = !(((struct skin_viewport *)ret)->is_infovp ==
165 (what==SKIN_FIND_UIVP));
166 break;
167 #ifdef HAVE_LCD_BITMAP
168 case SKIN_FIND_IMAGE:
169 ret = list.linkedlist->token->value.data;
170 itemlabel = ((struct gui_img *)ret)->label;
171 break;
172 #endif
173 #ifdef HAVE_TOUCHSCREEN
174 case SKIN_FIND_TOUCHREGION:
175 ret = list.linkedlist->token->value.data;
176 itemlabel = ((struct touchregion *)ret)->label;
177 break;
178 #endif
179 #ifdef HAVE_SKIN_VARIABLES
180 case SKIN_VARIABLE:
181 ret = list.linkedlist->token->value.data;
182 itemlabel = ((struct skin_var *)ret)->label;
183 break;
184 #endif
187 if (!skip && itemlabel && !strcmp(itemlabel, label))
188 return ret;
190 if (isvplist)
191 list.vplist = list.vplist->next;
192 else
193 list.linkedlist = list.linkedlist->next;
195 return NULL;
198 #ifdef HAVE_LCD_BITMAP
200 /* create and init a new wpsll item.
201 * passing NULL to token will alloc a new one.
202 * You should only pass NULL for the token when the token type (table above)
203 * is WPS_NO_TOKEN which means it is not stored automatically in the skins token array
205 static struct skin_token_list *new_skin_token_list_item(struct wps_token *token,
206 void* token_data)
208 struct skin_token_list *llitem =
209 (struct skin_token_list *)skin_buffer_alloc(sizeof(struct skin_token_list));
210 if (!token)
211 token = (struct wps_token*)skin_buffer_alloc(sizeof(struct wps_token));
212 if (!llitem || !token)
213 return NULL;
214 llitem->next = NULL;
215 llitem->token = token;
216 if (token_data)
217 llitem->token->value.data = token_data;
218 return llitem;
221 static int parse_statusbar_tags(struct skin_element* element,
222 struct wps_token *token,
223 struct wps_data *wps_data)
225 (void)element;
226 if (token->type == SKIN_TOKEN_DRAW_INBUILTBAR)
228 token->value.data = (void*)&curr_vp->vp;
230 else
232 struct skin_element *def_vp = wps_data->tree;
233 struct skin_viewport *default_vp = def_vp->data;
234 if (def_vp->params_count == 0)
236 wps_data->wps_sb_tag = true;
237 wps_data->show_sb_on_wps = (token->type == SKIN_TOKEN_ENABLE_THEME);
239 if (wps_data->show_sb_on_wps)
241 viewport_set_defaults(&default_vp->vp, curr_screen);
243 else
245 viewport_set_fullscreen(&default_vp->vp, curr_screen);
247 #ifdef HAVE_REMOTE_LCD
248 /* viewport_set_defaults() sets the font to FONT_UI+curr_screen.
249 * This parser requires font 1 to always be the UI font,
250 * so force it back to FONT_UI and handle the screen number at the end */
251 default_vp->vp.font = FONT_UI;
252 #endif
254 return 0;
257 static int get_image_id(int c)
259 if(c >= 'a' && c <= 'z')
260 return c - 'a';
261 else if(c >= 'A' && c <= 'Z')
262 return c - 'A' + 26;
263 else
264 return -1;
267 char *get_image_filename(const char *start, const char* bmpdir,
268 char *buf, int buf_size)
270 snprintf(buf, buf_size, "%s/%s", bmpdir, start);
272 return buf;
275 static int parse_image_display(struct skin_element *element,
276 struct wps_token *token,
277 struct wps_data *wps_data)
279 char *label = element->params[0].data.text;
280 char sublabel = '\0';
281 int subimage;
282 struct gui_img *img;
283 struct image_display *id = skin_buffer_alloc(sizeof(struct image_display));
285 if (element->params_count == 1 && strlen(label) <= 2)
287 /* backwards compatability. Allow %xd(Aa) to still work */
288 sublabel = label[1];
289 label[1] = '\0';
291 /* sanity check */
292 img = skin_find_item(label, SKIN_FIND_IMAGE, wps_data);
293 if (!img || !id)
295 return WPS_ERROR_INVALID_PARAM;
297 id->label = label;
298 id->offset = 0;
299 id->token = NULL;
300 if (img->using_preloaded_icons)
302 token->type = SKIN_TOKEN_IMAGE_DISPLAY_LISTICON;
305 if (element->params_count > 1)
307 if (element->params[1].type == CODE)
308 id->token = element->params[1].data.code->data;
309 /* specify a number. 1 being the first subimage (i.e top) NOT 0 */
310 else if (element->params[1].type == INTEGER)
311 id->subimage = element->params[1].data.number - 1;
312 if (element->params_count > 2)
313 id->offset = element->params[2].data.number;
315 else
317 if ((subimage = get_image_id(sublabel)) != -1)
319 if (subimage >= img->num_subimages)
320 return WPS_ERROR_INVALID_PARAM;
321 id->subimage = subimage;
322 } else {
323 id->subimage = 0;
326 token->value.data = id;
327 return 0;
330 static int parse_image_load(struct skin_element *element,
331 struct wps_token *token,
332 struct wps_data *wps_data)
334 const char* filename;
335 const char* id;
336 int x,y;
337 struct gui_img *img;
339 /* format: %x(n,filename.bmp,x,y)
340 or %xl(n,filename.bmp,x,y)
341 or %xl(n,filename.bmp,x,y,num_subimages)
344 id = element->params[0].data.text;
345 filename = element->params[1].data.text;
346 x = element->params[2].data.number;
347 y = element->params[3].data.number;
349 /* check the image number and load state */
350 if(skin_find_item(id, SKIN_FIND_IMAGE, wps_data))
352 /* Invalid image ID */
353 return WPS_ERROR_INVALID_PARAM;
355 img = (struct gui_img*)skin_buffer_alloc(sizeof(struct gui_img));
356 if (!img)
357 return WPS_ERROR_INVALID_PARAM;
358 /* save a pointer to the filename */
359 img->bm.data = (char*)filename;
360 img->label = id;
361 img->x = x;
362 img->y = y;
363 img->num_subimages = 1;
364 img->always_display = false;
365 img->display = -1;
366 img->using_preloaded_icons = false;
368 /* save current viewport */
369 img->vp = &curr_vp->vp;
371 if (token->type == SKIN_TOKEN_IMAGE_DISPLAY)
373 img->always_display = true;
375 else if (element->params_count == 5)
377 img->num_subimages = element->params[4].data.number;
378 if (img->num_subimages <= 0)
379 return WPS_ERROR_INVALID_PARAM;
382 if (!strcmp(img->bm.data, "__list_icons__"))
384 img->num_subimages = Icon_Last_Themeable;
385 img->using_preloaded_icons = true;
388 struct skin_token_list *item =
389 (struct skin_token_list *)new_skin_token_list_item(NULL, img);
390 if (!item)
391 return WPS_ERROR_INVALID_PARAM;
392 add_to_ll_chain(&wps_data->images, item);
394 return 0;
396 struct skin_font {
397 int id; /* the id from font_load */
398 char *name; /* filename without path and extension */
399 int glyphs; /* how many glyphs to reserve room for */
401 static struct skin_font skinfonts[MAXUSERFONTS];
402 static int parse_font_load(struct skin_element *element,
403 struct wps_token *token,
404 struct wps_data *wps_data)
406 (void)wps_data; (void)token;
407 int id = element->params[0].data.number;
408 char *filename = element->params[1].data.text;
409 int glyphs;
410 char *ptr;
412 if(element->params_count > 2)
413 glyphs = element->params[2].data.number;
414 else
415 glyphs = GLYPHS_TO_CACHE;
416 #if defined(DEBUG) || defined(SIMULATOR)
417 if (skinfonts[id-FONT_FIRSTUSERFONT].name != NULL)
419 DEBUGF("font id %d already being used\n", id);
421 #endif
422 /* make sure the filename contains .fnt,
423 * we dont actually use it, but require it anyway */
424 ptr = strchr(filename, '.');
425 if (!ptr || strncmp(ptr, ".fnt", 4))
426 return WPS_ERROR_INVALID_PARAM;
427 skinfonts[id-FONT_FIRSTUSERFONT].id = -1;
428 skinfonts[id-FONT_FIRSTUSERFONT].name = filename;
429 skinfonts[id-FONT_FIRSTUSERFONT].glyphs = glyphs;
431 return 0;
435 #ifdef HAVE_LCD_BITMAP
437 static int parse_playlistview(struct skin_element *element,
438 struct wps_token *token,
439 struct wps_data *wps_data)
441 (void)wps_data;
442 struct playlistviewer *viewer =
443 (struct playlistviewer *)skin_buffer_alloc(sizeof(struct playlistviewer));
444 if (!viewer)
445 return WPS_ERROR_INVALID_PARAM;
446 viewer->vp = &curr_vp->vp;
447 viewer->show_icons = true;
448 viewer->start_offset = element->params[0].data.number;
449 viewer->line = element->params[1].data.code;
451 token->value.data = (void*)viewer;
453 return 0;
455 #endif
457 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1))
459 static int parse_viewportcolour(struct skin_element *element,
460 struct wps_token *token,
461 struct wps_data *wps_data)
463 (void)wps_data;
464 struct skin_tag_parameter *param = element->params;
465 struct viewport_colour *colour =
466 (struct viewport_colour *)skin_buffer_alloc(sizeof(struct viewport_colour));
467 if (!colour)
468 return -1;
469 if (isdefault(param))
471 colour->colour = get_viewport_default_colour(curr_screen,
472 token->type == SKIN_TOKEN_VIEWPORT_FGCOLOUR);
474 else
476 if (!parse_color(curr_screen, param->data.text, &colour->colour))
477 return -1;
479 colour->vp = &curr_vp->vp;
480 token->value.data = colour;
481 if (element->line == curr_viewport_element->line)
483 if (token->type == SKIN_TOKEN_VIEWPORT_FGCOLOUR)
485 curr_vp->start_fgcolour = colour->colour;
486 curr_vp->vp.fg_pattern = colour->colour;
488 else
490 curr_vp->start_bgcolour = colour->colour;
491 curr_vp->vp.bg_pattern = colour->colour;
494 return 0;
497 static int parse_image_special(struct skin_element *element,
498 struct wps_token *token,
499 struct wps_data *wps_data)
501 (void)wps_data; /* kill warning */
502 (void)token;
504 #if LCD_DEPTH > 1
505 char *filename;
506 if (token->type == SKIN_TOKEN_IMAGE_BACKDROP)
508 if (isdefault(&element->params[0]))
510 filename = "-";
512 else
514 filename = element->params[0].data.text;
515 /* format: %X(filename.bmp) or %X(d) */
516 if (!strcmp(filename, "d"))
517 filename = NULL;
519 wps_data->backdrop = filename;
521 #endif
523 return 0;
525 #endif
527 #endif /* HAVE_LCD_BITMAP */
529 static int parse_setting_and_lang(struct skin_element *element,
530 struct wps_token *token,
531 struct wps_data *wps_data)
533 /* NOTE: both the string validations that happen in here will
534 * automatically PASS on checkwps because its too hard to get
535 * settings_list.c and english.lang built for it.
536 * If that ever changes remove the #ifndef __PCTOOL__'s here
538 (void)wps_data;
539 char *temp = element->params[0].data.text;
540 int i;
542 if (token->type == SKIN_TOKEN_TRANSLATEDSTRING)
544 #ifndef __PCTOOL__
545 i = lang_english_to_id(temp);
546 if (i < 0)
547 return WPS_ERROR_INVALID_PARAM;
548 #endif
550 else
552 /* Find the setting */
553 for (i=0; i<nb_settings; i++)
554 if (settings[i].cfg_name &&
555 !strcmp(settings[i].cfg_name, temp))
556 break;
557 #ifndef __PCTOOL__
558 if (i == nb_settings)
559 return WPS_ERROR_INVALID_PARAM;
560 #endif
562 /* Store the setting number */
563 token->value.i = i;
564 return 0;
566 static int parse_logical_if(struct skin_element *element,
567 struct wps_token *token,
568 struct wps_data *wps_data)
570 (void)wps_data;
571 char *op = element->params[1].data.text;
572 struct logical_if *lif = skin_buffer_alloc(sizeof(struct logical_if));
573 if (!lif)
574 return -1;
575 token->value.data = lif;
576 lif->token = element->params[0].data.code->data;
578 if (!strncmp(op, "=", 1))
579 lif->op = IF_EQUALS;
580 else if (!strncmp(op, "!=", 2))
581 lif->op = IF_NOTEQUALS;
582 else if (!strncmp(op, ">=", 2))
583 lif->op = IF_GREATERTHAN_EQ;
584 else if (!strncmp(op, "<=", 2))
585 lif->op = IF_LESSTHAN_EQ;
586 else if (!strncmp(op, ">", 2))
587 lif->op = IF_GREATERTHAN;
588 else if (!strncmp(op, "<", 1))
589 lif->op = IF_LESSTHAN;
591 memcpy(&lif->operand, &element->params[2], sizeof(lif->operand));
592 if (element->params_count > 3)
593 lif->num_options = element->params[3].data.number;
594 else
595 lif->num_options = TOKEN_VALUE_ONLY;
596 return 0;
600 static int parse_timeout_tag(struct skin_element *element,
601 struct wps_token *token,
602 struct wps_data *wps_data)
604 (void)wps_data;
605 int val = 0;
606 if (element->params_count == 0)
608 switch (token->type)
610 case SKIN_TOKEN_SUBLINE_TIMEOUT:
611 return -1;
612 case SKIN_TOKEN_BUTTON_VOLUME:
613 case SKIN_TOKEN_TRACK_STARTING:
614 case SKIN_TOKEN_TRACK_ENDING:
615 val = 10;
616 break;
617 default:
618 break;
621 else
622 val = element->params[0].data.number;
623 token->value.i = val * TIMEOUT_UNIT;
624 return 0;
627 static int parse_progressbar_tag(struct skin_element* element,
628 struct wps_token *token,
629 struct wps_data *wps_data)
631 #ifdef HAVE_LCD_BITMAP
632 struct progressbar *pb;
633 struct viewport *vp = &curr_vp->vp;
634 struct skin_tag_parameter *param = element->params;
635 int curr_param = 0;
636 char *image_filename = NULL;
638 if (element->params_count == 0 &&
639 element->tag->type != SKIN_TOKEN_PROGRESSBAR)
640 return 0; /* nothing to do */
641 pb = (struct progressbar*)skin_buffer_alloc(sizeof(struct progressbar));
643 token->value.data = pb;
645 if (!pb)
646 return WPS_ERROR_INVALID_PARAM;
647 pb->vp = vp;
648 pb->follow_lang_direction = follow_lang_direction > 0;
649 pb->nofill = false;
650 pb->nobar = false;
651 pb->image = NULL;
652 pb->slider = NULL;
653 pb->backdrop = NULL;
654 pb->invert_fill_direction = false;
655 pb->horizontal = true;
657 if (element->params_count == 0)
659 pb->x = 0;
660 pb->width = vp->width;
661 pb->height = SYSFONT_HEIGHT-2;
662 pb->y = -1; /* Will be computed during the rendering */
663 pb->type = element->tag->type;
664 return 0;
667 /* (x, y, width, height, ...) */
668 if (!isdefault(param))
669 pb->x = param->data.number;
670 else
671 pb->x = 0;
672 param++;
674 if (!isdefault(param))
675 pb->y = param->data.number;
676 else
677 pb->y = -1; /* computed at rendering */
678 param++;
680 if (!isdefault(param))
681 pb->width = param->data.number;
682 else
683 pb->width = vp->width - pb->x;
684 param++;
686 if (!isdefault(param))
688 /* A zero height makes no sense - reject it */
689 if (param->data.number == 0)
690 return WPS_ERROR_INVALID_PARAM;
692 pb->height = param->data.number;
694 else
696 if (vp->font > FONT_UI)
697 pb->height = -1; /* calculate at display time */
698 else
700 #ifndef __PCTOOL__
701 pb->height = font_get(vp->font)->height;
702 #else
703 pb->height = 8;
704 #endif
707 /* optional params, first is the image filename if it isnt recognised as a keyword */
709 curr_param = 4;
710 if (isdefault(&element->params[curr_param]))
712 param++;
713 curr_param++;
716 pb->horizontal = pb->width > pb->height;
717 while (curr_param < element->params_count)
719 param++;
720 if (!strcmp(param->data.text, "invert"))
721 pb->invert_fill_direction = true;
722 else if (!strcmp(param->data.text, "nofill"))
723 pb->nofill = true;
724 else if (!strcmp(param->data.text, "nobar"))
725 pb->nobar = true;
726 else if (!strcmp(param->data.text, "slider"))
728 if (curr_param+1 < element->params_count)
730 curr_param++;
731 param++;
732 pb->slider = skin_find_item(param->data.text,
733 SKIN_FIND_IMAGE, wps_data);
735 else /* option needs the next param */
736 return -1;
738 else if (!strcmp(param->data.text, "image"))
740 if (curr_param+1 < element->params_count)
742 curr_param++;
743 param++;
744 image_filename = param->data.text;
747 else /* option needs the next param */
748 return -1;
750 else if (!strcmp(param->data.text, "backdrop"))
752 if (curr_param+1 < element->params_count)
754 curr_param++;
755 param++;
756 pb->backdrop = skin_find_item(param->data.text,
757 SKIN_FIND_IMAGE, wps_data);
760 else /* option needs the next param */
761 return -1;
763 else if (!strcmp(param->data.text, "vertical"))
765 pb->horizontal = false;
766 if (isdefault(&element->params[3]))
767 pb->height = vp->height - pb->y;
769 else if (!strcmp(param->data.text, "horizontal"))
770 pb->horizontal = true;
771 else if (curr_param == 4)
772 image_filename = param->data.text;
774 curr_param++;
777 if (image_filename)
779 pb->image = skin_find_item(image_filename, SKIN_FIND_IMAGE, wps_data);
780 if (!pb->image) /* load later */
782 struct gui_img* img = (struct gui_img*)skin_buffer_alloc(sizeof(struct gui_img));
783 if (!img)
784 return WPS_ERROR_INVALID_PARAM;
785 /* save a pointer to the filename */
786 img->bm.data = (char*)image_filename;
787 img->label = image_filename;
788 img->x = 0;
789 img->y = 0;
790 img->num_subimages = 1;
791 img->always_display = false;
792 img->display = -1;
793 img->using_preloaded_icons = false;
794 img->vp = &curr_vp->vp;
795 struct skin_token_list *item =
796 (struct skin_token_list *)new_skin_token_list_item(NULL, img);
797 if (!item)
798 return WPS_ERROR_INVALID_PARAM;
799 add_to_ll_chain(&wps_data->images, item);
800 pb->image = img;
804 if (token->type == SKIN_TOKEN_VOLUME)
805 token->type = SKIN_TOKEN_VOLUMEBAR;
806 else if (token->type == SKIN_TOKEN_BATTERY_PERCENT)
807 token->type = SKIN_TOKEN_BATTERY_PERCENTBAR;
808 else if (token->type == SKIN_TOKEN_TUNER_RSSI)
809 token->type = SKIN_TOKEN_TUNER_RSSI_BAR;
810 else if (token->type == SKIN_TOKEN_PEAKMETER_LEFT)
811 token->type = SKIN_TOKEN_PEAKMETER_LEFTBAR;
812 else if (token->type == SKIN_TOKEN_PEAKMETER_RIGHT)
813 token->type = SKIN_TOKEN_PEAKMETER_RIGHTBAR;
814 pb->type = token->type;
816 return 0;
818 #else
819 (void)element;
820 if (token->type == SKIN_TOKEN_PROGRESSBAR ||
821 token->type == SKIN_TOKEN_PLAYER_PROGRESSBAR)
823 wps_data->full_line_progressbar =
824 token->type == SKIN_TOKEN_PLAYER_PROGRESSBAR;
826 return 0;
828 #endif
831 #ifdef HAVE_ALBUMART
832 static int parse_albumart_load(struct skin_element* element,
833 struct wps_token *token,
834 struct wps_data *wps_data)
836 struct dim dimensions;
837 int albumart_slot;
838 bool swap_for_rtl = lang_is_rtl() && follow_lang_direction;
839 struct skin_albumart *aa =
840 (struct skin_albumart *)skin_buffer_alloc(sizeof(struct skin_albumart));
841 (void)token; /* silence warning */
842 if (!aa)
843 return -1;
845 /* reset albumart info in wps */
846 aa->width = -1;
847 aa->height = -1;
848 aa->xalign = WPS_ALBUMART_ALIGN_CENTER; /* default */
849 aa->yalign = WPS_ALBUMART_ALIGN_CENTER; /* default */
851 aa->x = element->params[0].data.number;
852 aa->y = element->params[1].data.number;
853 aa->width = element->params[2].data.number;
854 aa->height = element->params[3].data.number;
856 aa->vp = &curr_vp->vp;
857 aa->draw_handle = -1;
859 /* if we got here, we parsed everything ok .. ! */
860 if (aa->width < 0)
861 aa->width = 0;
862 else if (aa->width > LCD_WIDTH)
863 aa->width = LCD_WIDTH;
865 if (aa->height < 0)
866 aa->height = 0;
867 else if (aa->height > LCD_HEIGHT)
868 aa->height = LCD_HEIGHT;
870 if (swap_for_rtl)
871 aa->x = LCD_WIDTH - (aa->x + aa->width);
873 aa->state = WPS_ALBUMART_LOAD;
874 wps_data->albumart = aa;
876 dimensions.width = aa->width;
877 dimensions.height = aa->height;
879 albumart_slot = playback_claim_aa_slot(&dimensions);
881 if (0 <= albumart_slot)
882 wps_data->playback_aa_slot = albumart_slot;
884 if (element->params_count > 4 && !isdefault(&element->params[4]))
886 switch (*element->params[4].data.text)
888 case 'l':
889 case 'L':
890 if (swap_for_rtl)
891 aa->xalign = WPS_ALBUMART_ALIGN_RIGHT;
892 else
893 aa->xalign = WPS_ALBUMART_ALIGN_LEFT;
894 break;
895 case 'c':
896 case 'C':
897 aa->xalign = WPS_ALBUMART_ALIGN_CENTER;
898 break;
899 case 'r':
900 case 'R':
901 if (swap_for_rtl)
902 aa->xalign = WPS_ALBUMART_ALIGN_LEFT;
903 else
904 aa->xalign = WPS_ALBUMART_ALIGN_RIGHT;
905 break;
908 if (element->params_count > 5 && !isdefault(&element->params[5]))
910 switch (*element->params[5].data.text)
912 case 't':
913 case 'T':
914 aa->yalign = WPS_ALBUMART_ALIGN_TOP;
915 break;
916 case 'c':
917 case 'C':
918 aa->yalign = WPS_ALBUMART_ALIGN_CENTER;
919 break;
920 case 'b':
921 case 'B':
922 aa->yalign = WPS_ALBUMART_ALIGN_BOTTOM;
923 break;
926 return 0;
929 #endif /* HAVE_ALBUMART */
930 #ifdef HAVE_SKIN_VARIABLES
931 static struct skin_var* find_or_add_var(const char* label,
932 struct wps_data *data)
934 struct skin_var* ret = skin_find_item(label, SKIN_VARIABLE, data);
935 if (!ret)
937 ret = (struct skin_var*)skin_buffer_alloc(sizeof(struct skin_var));
938 if (!ret)
939 return NULL;
940 ret->label = label;
941 ret->value = 1;
942 ret->last_changed = 0xffff;
943 struct skin_token_list *item = new_skin_token_list_item(NULL, ret);
944 if (!item)
945 return NULL;
946 add_to_ll_chain(&data->skinvars, item);
948 return ret;
950 static int parse_skinvar( struct skin_element *element,
951 struct wps_token *token,
952 struct wps_data *wps_data)
954 const char* label = element->params[0].data.text;
955 struct skin_var* var = find_or_add_var(label, wps_data);
956 if (!var)
957 return WPS_ERROR_INVALID_PARAM;
958 switch (token->type)
960 case SKIN_TOKEN_VAR_GETVAL:
961 token->value.data = var;
962 break;
963 case SKIN_TOKEN_VAR_SET:
965 struct skin_var_changer *data =
966 (struct skin_var_changer*)skin_buffer_alloc(
967 sizeof(struct skin_var_changer));
968 if (!data)
969 return WPS_ERROR_INVALID_PARAM;
970 data->var = var;
971 data->newval = element->params[2].data.number;
972 data->max = 0;
973 if (!strcmp(element->params[1].data.text, "set"))
974 data->direct = true;
975 else if (!strcmp(element->params[1].data.text, "inc"))
977 data->direct = false;
979 else if (!strcmp(element->params[1].data.text, "dec"))
981 data->direct = false;
982 data->newval *= -1;
984 if (element->params_count > 3)
985 data->max = element->params[3].data.number;
986 token->value.data = data;
988 break;
989 case SKIN_TOKEN_VAR_TIMEOUT:
991 struct skin_var_lastchange *data =
992 (struct skin_var_lastchange*)skin_buffer_alloc(
993 sizeof(struct skin_var_lastchange));
994 if (!data)
995 return WPS_ERROR_INVALID_PARAM;
996 data->var = var;
997 data->timeout = 10;
998 if (element->params_count > 1)
999 data->timeout = element->params[1].data.number;
1000 data->timeout *= TIMEOUT_UNIT;
1001 token->value.data = data;
1003 break;
1004 default: /* kill the warning */
1005 break;
1007 return 0;
1009 #endif /* HAVE_SKIN_VARIABLES */
1010 #ifdef HAVE_TOUCHSCREEN
1011 static int parse_lasttouch(struct skin_element *element,
1012 struct wps_token *token,
1013 struct wps_data *wps_data)
1015 struct touchregion_lastpress *data =
1016 (struct touchregion_lastpress*)skin_buffer_alloc(
1017 sizeof(struct touchregion_lastpress));
1018 int i;
1019 if (!data)
1020 return WPS_ERROR_INVALID_PARAM;
1021 data->region = NULL;
1022 data->timeout = 10;
1024 for (i=0; i<element->params_count; i++)
1026 if (element->params[i].type == STRING)
1027 data->region = skin_find_item(element->params[i].data.text,
1028 SKIN_FIND_TOUCHREGION, wps_data);
1029 else if (element->params[i].type == INTEGER ||
1030 element->params[i].type == DECIMAL)
1031 data->timeout = element->params[i].data.number;
1034 data->timeout *= TIMEOUT_UNIT;
1035 token->value.data = data;
1036 return 0;
1039 struct touchaction {const char* s; int action;};
1040 static const struct touchaction touchactions[] = {
1041 /* generic actions, convert to screen actions on use */
1042 {"none", ACTION_TOUCHSCREEN}, {"lock", ACTION_TOUCH_SOFTLOCK },
1043 {"prev", ACTION_STD_PREV }, {"next", ACTION_STD_NEXT },
1044 {"rwd", ACTION_STD_PREVREPEAT }, {"ffwd", ACTION_STD_NEXTREPEAT },
1045 {"hotkey", ACTION_STD_HOTKEY}, {"select", ACTION_STD_OK },
1046 {"menu", ACTION_STD_MENU }, {"cancel", ACTION_STD_CANCEL },
1047 {"contextmenu", ACTION_STD_CONTEXT},{"quickscreen", ACTION_STD_QUICKSCREEN },
1049 /* list/tree actions */
1050 { "resumeplayback", ACTION_TREE_WPS}, /* returns to previous music, WPS/FM */
1051 /* not really WPS specific, but no equivilant ACTION_STD_* */
1052 {"voldown", ACTION_WPS_VOLDOWN}, {"volup", ACTION_WPS_VOLUP},
1053 {"mute", ACTION_TOUCH_MUTE },
1055 /* generic settings changers */
1056 {"setting_inc", ACTION_SETTINGS_INC}, {"setting_dec", ACTION_SETTINGS_DEC},
1057 {"setting_set", ACTION_SETTINGS_SET},
1059 /* WPS specific actions */
1060 {"wps_prev", ACTION_WPS_SKIPPREV }, {"wps_next", ACTION_WPS_SKIPNEXT },
1061 {"browse", ACTION_WPS_BROWSE },
1062 {"play", ACTION_WPS_PLAY }, {"stop", ACTION_WPS_STOP },
1063 {"shuffle", ACTION_TOUCH_SHUFFLE }, {"repmode", ACTION_TOUCH_REPMODE },
1064 {"pitch", ACTION_WPS_PITCHSCREEN}, {"playlist", ACTION_WPS_VIEW_PLAYLIST },
1066 #if CONFIG_TUNER
1067 /* FM screen actions */
1068 /* Also allow browse, play, stop from WPS codes */
1069 {"mode", ACTION_FM_MODE }, {"record", ACTION_FM_RECORD },
1070 {"presets", ACTION_FM_PRESET},
1071 #endif
1074 static int parse_touchregion(struct skin_element *element,
1075 struct wps_token *token,
1076 struct wps_data *wps_data)
1078 (void)token;
1079 unsigned i, imax;
1080 int p;
1081 struct touchregion *region = NULL;
1082 const char *action;
1083 const char pb_string[] = "progressbar";
1084 const char vol_string[] = "volume";
1085 char temp[20];
1087 /* format: %T([label,], x,y,width,height,action[, ...])
1088 * if action starts with & the area must be held to happen
1092 region = (struct touchregion*)skin_buffer_alloc(sizeof(struct touchregion));
1093 if (!region)
1094 return WPS_ERROR_INVALID_PARAM;
1096 /* should probably do some bounds checking here with the viewport... but later */
1097 region->action = ACTION_NONE;
1099 if (element->params[0].type == STRING)
1101 region->label = element->params[0].data.text;
1102 p = 1;
1103 /* "[SI]III[SI]|SS" is the param list. There MUST be 4 numbers
1104 * followed by at least one string. Verify that here */
1105 if (element->params_count < 6 ||
1106 element->params[4].type != INTEGER)
1107 return WPS_ERROR_INVALID_PARAM;
1109 else
1111 region->label = NULL;
1112 p = 0;
1115 region->x = element->params[p++].data.number;
1116 region->y = element->params[p++].data.number;
1117 region->width = element->params[p++].data.number;
1118 region->height = element->params[p++].data.number;
1119 region->wvp = curr_vp;
1120 region->armed = false;
1121 region->reverse_bar = false;
1122 region->value = 0;
1123 region->last_press = 0xffff;
1124 region->press_length = PRESS;
1125 region->allow_while_locked = false;
1126 action = element->params[p++].data.text;
1128 strcpy(temp, action);
1129 action = temp;
1131 switch (*action)
1133 case '!':
1134 region->reverse_bar = true;
1135 action++;
1136 break;
1137 case '^':
1138 action++;
1139 region->allow_while_locked = true;
1140 break;
1142 if(!strcmp(pb_string, action))
1143 region->action = ACTION_TOUCH_SCROLLBAR;
1144 else if(!strcmp(vol_string, action))
1145 region->action = ACTION_TOUCH_VOLUME;
1146 else
1148 if (*action == '*')
1150 action++;
1151 region->press_length = LONG_PRESS;
1153 else if(*action == '&')
1155 action++;
1156 region->press_length = REPEAT;
1158 else
1159 region->press_length = PRESS;
1161 imax = ARRAYLEN(touchactions);
1162 for (i = 0; i < imax; i++)
1164 /* try to match with one of our touchregion screens */
1165 if (!strcmp(touchactions[i].s, action))
1167 region->action = touchactions[i].action;
1168 if (region->action == ACTION_SETTINGS_INC ||
1169 region->action == ACTION_SETTINGS_DEC ||
1170 region->action == ACTION_SETTINGS_SET)
1172 if (element->params_count < p+1)
1174 return WPS_ERROR_INVALID_PARAM;
1176 else
1178 char *name = element->params[p].data.text;
1179 int j;
1180 /* Find the setting */
1181 for (j=0; j<nb_settings; j++)
1182 if (settings[j].cfg_name &&
1183 !strcmp(settings[j].cfg_name, name))
1184 break;
1185 if (j==nb_settings)
1186 return WPS_ERROR_INVALID_PARAM;
1187 region->setting_data.setting = (void*)&settings[j];
1188 if (region->action == ACTION_SETTINGS_SET)
1190 char* text;
1191 int temp;
1192 struct touchsetting *setting =
1193 &region->setting_data;
1194 if (element->params_count < p+2)
1195 return WPS_ERROR_INVALID_PARAM;
1196 #ifndef __PCTOOL__
1197 text = element->params[p+1].data.text;
1198 switch (settings[j].flags&F_T_MASK)
1200 case F_T_CUSTOM:
1201 setting->value.text = text;
1202 break;
1203 case F_T_INT:
1204 case F_T_UINT:
1205 if (settings[j].cfg_vals == NULL)
1207 setting->value.number = atoi(text);
1209 else if (cfg_string_to_int(j, &temp, text))
1211 if (settings[j].flags&F_TABLE_SETTING)
1212 setting->value.number =
1213 settings[j].table_setting->values[temp];
1214 else
1215 setting->value.number = temp;
1217 else
1218 return WPS_ERROR_INVALID_PARAM;
1219 break;
1220 case F_T_BOOL:
1221 if (cfg_string_to_int(j, &temp, text))
1223 setting->value.number = temp;
1225 else
1226 return WPS_ERROR_INVALID_PARAM;
1227 break;
1228 default:
1229 return WPS_ERROR_INVALID_PARAM;
1231 #endif /* __PCTOOL__ */
1235 break;
1238 if (region->action == ACTION_NONE)
1239 return WPS_ERROR_INVALID_PARAM;
1241 struct skin_token_list *item = new_skin_token_list_item(NULL, region);
1242 if (!item)
1243 return WPS_ERROR_INVALID_PARAM;
1244 add_to_ll_chain(&wps_data->touchregions, item);
1246 if (region->action == ACTION_TOUCH_MUTE)
1248 region->value = global_settings.volume;
1252 return 0;
1254 #endif
1256 static bool check_feature_tag(const int type)
1258 switch (type)
1260 case SKIN_TOKEN_RTC_PRESENT:
1261 #if CONFIG_RTC
1262 return true;
1263 #else
1264 return false;
1265 #endif
1266 case SKIN_TOKEN_HAVE_RECORDING:
1267 #ifdef HAVE_RECORDING
1268 return true;
1269 #else
1270 return false;
1271 #endif
1272 case SKIN_TOKEN_HAVE_TUNER:
1273 #if CONFIG_TUNER
1274 if (radio_hardware_present())
1275 return true;
1276 #endif
1277 return false;
1278 case SKIN_TOKEN_HAVE_TOUCH:
1279 #ifdef HAVE_TOUCHSCREEN
1280 return true;
1281 #else
1282 return false;
1283 #endif
1285 #if CONFIG_TUNER
1286 case SKIN_TOKEN_HAVE_RDS:
1287 #ifdef HAVE_RDS_CAP
1288 return true;
1289 #else
1290 return false;
1291 #endif /* HAVE_RDS_CAP */
1292 #endif /* CONFIG_TUNER */
1293 default: /* not a tag we care about, just don't skip */
1294 return true;
1299 * initial setup of wps_data; does reset everything
1300 * except fields which need to survive, i.e.
1303 static void skin_data_reset(struct wps_data *wps_data)
1305 wps_data->tree = NULL;
1306 #ifdef HAVE_LCD_BITMAP
1307 wps_data->images = NULL;
1308 #endif
1309 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
1310 if (wps_data->backdrop_id >= 0)
1311 skin_backdrop_unload(wps_data->backdrop_id);
1312 wps_data->backdrop = NULL;
1313 #endif
1314 #ifdef HAVE_TOUCHSCREEN
1315 wps_data->touchregions = NULL;
1316 #endif
1317 #ifdef HAVE_SKIN_VARIABLES
1318 wps_data->skinvars = NULL;
1319 #endif
1320 #ifdef HAVE_ALBUMART
1321 wps_data->albumart = NULL;
1322 if (wps_data->playback_aa_slot >= 0)
1324 playback_release_aa_slot(wps_data->playback_aa_slot);
1325 wps_data->playback_aa_slot = -1;
1327 #endif
1329 #ifdef HAVE_LCD_BITMAP
1330 wps_data->peak_meter_enabled = false;
1331 wps_data->wps_sb_tag = false;
1332 wps_data->show_sb_on_wps = false;
1333 #else /* HAVE_LCD_CHARCELLS */
1334 /* progress bars */
1335 int i;
1336 for (i = 0; i < 8; i++)
1338 wps_data->wps_progress_pat[i] = 0;
1340 wps_data->full_line_progressbar = false;
1341 #endif
1342 wps_data->wps_loaded = false;
1345 #ifdef HAVE_LCD_BITMAP
1346 static bool load_skin_bmp(struct wps_data *wps_data, struct bitmap *bitmap, char* bmpdir)
1348 (void)wps_data; /* only needed for remote targets */
1349 char img_path[MAX_PATH];
1350 int fd;
1351 get_image_filename(bitmap->data, bmpdir,
1352 img_path, sizeof(img_path));
1354 /* load the image */
1355 int format;
1356 #ifdef HAVE_REMOTE_LCD
1357 if (curr_screen == SCREEN_REMOTE)
1358 format = FORMAT_ANY|FORMAT_REMOTE;
1359 else
1360 #endif
1361 format = FORMAT_ANY|FORMAT_TRANSPARENT;
1363 fd = open(img_path, O_RDONLY);
1364 if (fd < 0)
1366 DEBUGF("Couldn't open %s\n", img_path);
1367 return false;
1369 size_t buf_size = read_bmp_fd(fd, bitmap, 0,
1370 format|FORMAT_RETURN_SIZE, NULL);
1371 char* imgbuf = (char*)skin_buffer_alloc(buf_size);
1372 if (!imgbuf)
1374 #ifndef APPLICATION
1375 DEBUGF("Not enough skin buffer: need %zd more.\n",
1376 buf_size - skin_buffer_freespace());
1377 #endif
1378 close(fd);
1379 return NULL;
1381 lseek(fd, 0, SEEK_SET);
1382 bitmap->data = imgbuf;
1383 int ret = read_bmp_fd(fd, bitmap, buf_size, format, NULL);
1385 close(fd);
1386 if (ret > 0)
1388 return true;
1390 else
1392 /* Abort if we can't load an image */
1393 DEBUGF("Couldn't load '%s'\n", img_path);
1394 return false;
1398 static bool load_skin_bitmaps(struct wps_data *wps_data, char *bmpdir)
1400 struct skin_token_list *list;
1401 bool retval = true; /* return false if a single image failed to load */
1403 /* regular images */
1404 list = wps_data->images;
1405 while (list)
1407 struct gui_img *img = (struct gui_img*)list->token->value.data;
1408 if (img->bm.data)
1410 if (img->using_preloaded_icons)
1412 img->loaded = true;
1413 list->token->type = SKIN_TOKEN_IMAGE_DISPLAY_LISTICON;
1415 else
1417 img->loaded = load_skin_bmp(wps_data, &img->bm, bmpdir);
1418 if (img->loaded)
1419 img->subimage_height = img->bm.height / img->num_subimages;
1420 else
1421 retval = false;
1424 list = list->next;
1427 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1))
1428 wps_data->backdrop_id = skin_backdrop_assign(wps_data->backdrop, bmpdir, curr_screen);
1429 #endif /* has backdrop support */
1430 return retval;
1433 static bool skin_load_fonts(struct wps_data *data)
1435 /* don't spit out after the first failue to aid debugging */
1436 bool success = true;
1437 struct skin_element *vp_list;
1438 int font_id;
1439 /* walk though each viewport and assign its font */
1440 for(vp_list = data->tree; vp_list; vp_list = vp_list->next)
1442 /* first, find the viewports that have a non-sys/ui-font font */
1443 struct skin_viewport *skin_vp =
1444 (struct skin_viewport*)vp_list->data;
1445 struct viewport *vp = &skin_vp->vp;
1448 if (vp->font <= FONT_UI)
1449 { /* the usual case -> built-in fonts */
1450 #ifdef HAVE_REMOTE_LCD
1451 if (vp->font == FONT_UI)
1452 vp->font += curr_screen;
1453 #endif
1454 continue;
1456 font_id = vp->font;
1458 /* now find the corresponding skin_font */
1459 struct skin_font *font = &skinfonts[font_id-FONT_FIRSTUSERFONT];
1460 if (!font->name)
1462 if (success)
1464 DEBUGF("font %d not specified\n", font_id);
1466 success = false;
1467 continue;
1470 /* load the font - will handle loading the same font again if
1471 * multiple viewports use the same */
1472 if (font->id < 0)
1474 char *dot = strchr(font->name, '.');
1475 *dot = '\0';
1476 font->id = skin_font_load(font->name,
1477 skinfonts[font_id-FONT_FIRSTUSERFONT].glyphs);
1480 if (font->id < 0)
1482 DEBUGF("Unable to load font %d: '%s.fnt'\n",
1483 font_id, font->name);
1484 font->name = NULL; /* to stop trying to load it again if we fail */
1485 success = false;
1486 font->name = NULL;
1487 continue;
1490 /* finally, assign the font_id to the viewport */
1491 vp->font = font->id;
1493 return success;
1496 #endif /* HAVE_LCD_BITMAP */
1497 static int convert_viewport(struct wps_data *data, struct skin_element* element)
1499 struct skin_viewport *skin_vp =
1500 (struct skin_viewport *)skin_buffer_alloc(sizeof(struct skin_viewport));
1501 struct screen *display = &screens[curr_screen];
1503 if (!skin_vp)
1504 return CALLBACK_ERROR;
1506 skin_vp->hidden_flags = 0;
1507 skin_vp->label = NULL;
1508 skin_vp->is_infovp = false;
1509 element->data = skin_vp;
1510 curr_vp = skin_vp;
1511 curr_viewport_element = element;
1513 viewport_set_defaults(&skin_vp->vp, curr_screen);
1514 #ifdef HAVE_REMOTE_LCD
1515 /* viewport_set_defaults() sets the font to FONT_UI+curr_screen.
1516 * This parser requires font 1 to always be the UI font,
1517 * so force it back to FONT_UI and handle the screen number at the end */
1518 skin_vp->vp.font = FONT_UI;
1519 #endif
1521 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1))
1522 skin_vp->start_fgcolour = skin_vp->vp.fg_pattern;
1523 skin_vp->start_bgcolour = skin_vp->vp.bg_pattern;
1524 #endif
1527 struct skin_tag_parameter *param = element->params;
1528 if (element->params_count == 0) /* default viewport */
1530 if (!data->tree) /* first viewport in the skin */
1531 data->tree = element;
1532 skin_vp->label = VP_DEFAULT_LABEL;
1533 return CALLBACK_OK;
1536 if (element->params_count == 6)
1538 if (element->tag->type == SKIN_TOKEN_UIVIEWPORT_LOAD)
1540 skin_vp->is_infovp = true;
1541 if (isdefault(param))
1543 skin_vp->hidden_flags = VP_NEVER_VISIBLE;
1544 skin_vp->label = VP_DEFAULT_LABEL;
1546 else
1548 skin_vp->hidden_flags = VP_NEVER_VISIBLE;
1549 skin_vp->label = param->data.text;
1552 else
1554 skin_vp->hidden_flags = VP_DRAW_HIDEABLE|VP_DRAW_HIDDEN;
1555 skin_vp->label = param->data.text;
1557 param++;
1559 /* x */
1560 if (!isdefault(param))
1562 skin_vp->vp.x = param->data.number;
1563 if (param->data.number < 0)
1564 skin_vp->vp.x += display->lcdwidth;
1566 param++;
1567 /* y */
1568 if (!isdefault(param))
1570 skin_vp->vp.y = param->data.number;
1571 if (param->data.number < 0)
1572 skin_vp->vp.y += display->lcdheight;
1574 param++;
1575 /* width */
1576 if (!isdefault(param))
1578 skin_vp->vp.width = param->data.number;
1579 if (param->data.number < 0)
1580 skin_vp->vp.width = (skin_vp->vp.width + display->lcdwidth) - skin_vp->vp.x;
1582 else
1584 skin_vp->vp.width = display->lcdwidth - skin_vp->vp.x;
1586 param++;
1587 /* height */
1588 if (!isdefault(param))
1590 skin_vp->vp.height = param->data.number;
1591 if (param->data.number < 0)
1592 skin_vp->vp.height = (skin_vp->vp.height + display->lcdheight) - skin_vp->vp.y;
1594 else
1596 skin_vp->vp.height = display->lcdheight - skin_vp->vp.y;
1598 param++;
1599 #ifdef HAVE_LCD_BITMAP
1600 /* font */
1601 if (!isdefault(param))
1603 skin_vp->vp.font = param->data.number;
1605 #endif
1606 if ((unsigned) skin_vp->vp.x >= (unsigned) display->lcdwidth ||
1607 skin_vp->vp.width + skin_vp->vp.x > display->lcdwidth ||
1608 (unsigned) skin_vp->vp.y >= (unsigned) display->lcdheight ||
1609 skin_vp->vp.height + skin_vp->vp.y > display->lcdheight)
1610 return CALLBACK_ERROR;
1612 return CALLBACK_OK;
1615 static int skin_element_callback(struct skin_element* element, void* data)
1617 struct wps_data *wps_data = (struct wps_data *)data;
1618 struct wps_token *token;
1619 parse_function function = NULL;
1621 switch (element->type)
1623 /* IMPORTANT: element params are shared, so copy them if needed
1624 * or use then NOW, dont presume they have a long lifespan
1626 case TAG:
1628 token = (struct wps_token*)skin_buffer_alloc(sizeof(struct wps_token));
1629 memset(token, 0, sizeof(*token));
1630 token->type = element->tag->type;
1632 if (element->tag->flags&SKIN_RTC_REFRESH)
1634 #if CONFIG_RTC
1635 curr_line->update_mode |= SKIN_REFRESH_DYNAMIC;
1636 #else
1637 curr_line->update_mode |= SKIN_REFRESH_STATIC;
1638 #endif
1640 else
1641 curr_line->update_mode |= element->tag->flags&SKIN_REFRESH_ALL;
1643 element->data = token;
1645 /* Some tags need special handling for the tag, so add them here */
1646 switch (token->type)
1648 case SKIN_TOKEN_ALIGN_LANGDIRECTION:
1649 follow_lang_direction = 2;
1650 break;
1651 case SKIN_TOKEN_LOGICAL_IF:
1652 function = parse_logical_if;
1653 break;
1654 case SKIN_TOKEN_PROGRESSBAR:
1655 case SKIN_TOKEN_VOLUME:
1656 case SKIN_TOKEN_BATTERY_PERCENT:
1657 case SKIN_TOKEN_PLAYER_PROGRESSBAR:
1658 case SKIN_TOKEN_PEAKMETER_LEFT:
1659 case SKIN_TOKEN_PEAKMETER_RIGHT:
1660 #ifdef HAVE_RADIO_RSSI
1661 case SKIN_TOKEN_TUNER_RSSI:
1662 #endif
1663 function = parse_progressbar_tag;
1664 break;
1665 case SKIN_TOKEN_SUBLINE_TIMEOUT:
1666 case SKIN_TOKEN_BUTTON_VOLUME:
1667 case SKIN_TOKEN_TRACK_STARTING:
1668 case SKIN_TOKEN_TRACK_ENDING:
1669 function = parse_timeout_tag;
1670 break;
1671 #ifdef HAVE_LCD_BITMAP
1672 case SKIN_TOKEN_DISABLE_THEME:
1673 case SKIN_TOKEN_ENABLE_THEME:
1674 case SKIN_TOKEN_DRAW_INBUILTBAR:
1675 function = parse_statusbar_tags;
1676 break;
1677 case SKIN_TOKEN_LIST_TITLE_TEXT:
1678 #ifndef __PCTOOL__
1679 sb_skin_has_title(curr_screen);
1680 #endif
1681 break;
1682 #endif
1683 case SKIN_TOKEN_FILE_DIRECTORY:
1684 token->value.i = element->params[0].data.number;
1685 break;
1686 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1))
1687 case SKIN_TOKEN_VIEWPORT_FGCOLOUR:
1688 case SKIN_TOKEN_VIEWPORT_BGCOLOUR:
1689 function = parse_viewportcolour;
1690 break;
1691 case SKIN_TOKEN_IMAGE_BACKDROP:
1692 function = parse_image_special;
1693 break;
1694 #endif
1695 case SKIN_TOKEN_TRANSLATEDSTRING:
1696 case SKIN_TOKEN_SETTING:
1697 function = parse_setting_and_lang;
1698 break;
1699 #ifdef HAVE_LCD_BITMAP
1700 case SKIN_TOKEN_VIEWPORT_CUSTOMLIST:
1701 function = parse_playlistview;
1702 break;
1703 case SKIN_TOKEN_LOAD_FONT:
1704 function = parse_font_load;
1705 break;
1706 case SKIN_TOKEN_VIEWPORT_ENABLE:
1707 case SKIN_TOKEN_UIVIEWPORT_ENABLE:
1708 token->value.data = element->params[0].data.text;
1709 break;
1710 case SKIN_TOKEN_IMAGE_PRELOAD_DISPLAY:
1711 function = parse_image_display;
1712 break;
1713 case SKIN_TOKEN_IMAGE_PRELOAD:
1714 case SKIN_TOKEN_IMAGE_DISPLAY:
1715 function = parse_image_load;
1716 break;
1717 #endif
1718 #ifdef HAVE_TOUCHSCREEN
1719 case SKIN_TOKEN_TOUCHREGION:
1720 function = parse_touchregion;
1721 break;
1722 case SKIN_TOKEN_LASTTOUCH:
1723 function = parse_lasttouch;
1724 break;
1725 #endif
1726 #ifdef HAVE_ALBUMART
1727 case SKIN_TOKEN_ALBUMART_DISPLAY:
1728 if (wps_data->albumart)
1729 wps_data->albumart->vp = &curr_vp->vp;
1730 break;
1731 case SKIN_TOKEN_ALBUMART_LOAD:
1732 function = parse_albumart_load;
1733 break;
1734 #endif
1735 #ifdef HAVE_SKIN_VARIABLES
1736 case SKIN_TOKEN_VAR_SET:
1737 case SKIN_TOKEN_VAR_GETVAL:
1738 case SKIN_TOKEN_VAR_TIMEOUT:
1739 function = parse_skinvar;
1740 break;
1741 #endif
1742 default:
1743 break;
1745 if (function)
1747 if (function(element, token, wps_data) < 0)
1748 return CALLBACK_ERROR;
1750 /* tags that start with 'F', 'I' or 'D' are for the next file */
1751 if ( *(element->tag->name) == 'I' || *(element->tag->name) == 'F' ||
1752 *(element->tag->name) == 'D')
1753 token->next = true;
1754 if (follow_lang_direction > 0 )
1755 follow_lang_direction--;
1756 break;
1758 case VIEWPORT:
1759 return convert_viewport(wps_data, element);
1760 case LINE:
1762 struct line *line =
1763 (struct line *)skin_buffer_alloc(sizeof(struct line));
1764 line->update_mode = SKIN_REFRESH_STATIC;
1765 curr_line = line;
1766 element->data = line;
1768 break;
1769 case LINE_ALTERNATOR:
1771 struct line_alternator *alternator =
1772 (struct line_alternator *)skin_buffer_alloc(sizeof(struct line_alternator));
1773 alternator->current_line = 0;
1774 #ifndef __PCTOOL__
1775 alternator->next_change_tick = current_tick;
1776 #endif
1777 element->data = alternator;
1779 break;
1780 case CONDITIONAL:
1782 struct conditional *conditional =
1783 (struct conditional *)skin_buffer_alloc(sizeof(struct conditional));
1784 conditional->last_value = -1;
1785 conditional->token = element->data;
1786 element->data = conditional;
1787 if (!check_feature_tag(element->tag->type))
1789 return FEATURE_NOT_AVAILABLE;
1791 return CALLBACK_OK;
1793 case TEXT:
1794 curr_line->update_mode |= SKIN_REFRESH_STATIC;
1795 break;
1796 default:
1797 break;
1799 return CALLBACK_OK;
1802 /* to setup up the wps-data from a format-buffer (isfile = false)
1803 from a (wps-)file (isfile = true)*/
1804 bool skin_data_load(enum screen_type screen, struct wps_data *wps_data,
1805 const char *buf, bool isfile)
1807 char *wps_buffer = NULL;
1808 if (!wps_data || !buf)
1809 return false;
1810 #ifdef HAVE_ALBUMART
1811 int status;
1812 struct mp3entry *curtrack;
1813 long offset;
1814 struct skin_albumart old_aa = {.state = WPS_ALBUMART_NONE};
1815 if (wps_data->albumart)
1817 old_aa.state = wps_data->albumart->state;
1818 old_aa.height = wps_data->albumart->height;
1819 old_aa.width = wps_data->albumart->width;
1821 #endif
1822 #ifdef HAVE_LCD_BITMAP
1823 int i;
1824 for (i=0;i<MAXUSERFONTS;i++)
1826 skinfonts[i].id = -1;
1827 skinfonts[i].name = NULL;
1829 #endif
1830 #ifdef DEBUG_SKIN_ENGINE
1831 if (isfile && debug_wps)
1833 DEBUGF("\n=====================\nLoading '%s'\n=====================\n", buf);
1835 #endif
1838 skin_data_reset(wps_data);
1839 wps_data->wps_loaded = false;
1840 curr_screen = screen;
1841 curr_line = NULL;
1842 curr_vp = NULL;
1843 curr_viewport_element = NULL;
1845 if (isfile)
1847 int fd = open_utf8(buf, O_RDONLY);
1849 if (fd < 0)
1850 return false;
1852 /* get buffer space from the plugin buffer */
1853 size_t buffersize = 0;
1854 wps_buffer = (char *)plugin_get_buffer(&buffersize);
1856 if (!wps_buffer)
1857 return false;
1859 /* copy the file's content to the buffer for parsing,
1860 ensuring that every line ends with a newline char. */
1861 unsigned int start = 0;
1862 while(read_line(fd, wps_buffer + start, buffersize - start) > 0)
1864 start += strlen(wps_buffer + start);
1865 if (start < buffersize - 1)
1867 wps_buffer[start++] = '\n';
1868 wps_buffer[start] = 0;
1871 close(fd);
1872 if (start <= 0)
1873 return false;
1875 else
1877 wps_buffer = (char*)buf;
1879 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
1880 wps_data->backdrop = "-";
1881 wps_data->backdrop_id = -1;
1882 #endif
1883 /* parse the skin source */
1884 #ifndef APPLICATION
1885 skin_buffer_save_position();
1886 #endif
1887 wps_data->tree = skin_parse(wps_buffer, skin_element_callback, wps_data);
1888 if (!wps_data->tree) {
1889 skin_data_reset(wps_data);
1890 #ifndef APPLICATION
1891 skin_buffer_restore_position();
1892 #endif
1893 return false;
1896 #ifdef HAVE_LCD_BITMAP
1897 char bmpdir[MAX_PATH];
1898 if (isfile)
1900 /* get the bitmap dir */
1901 char *dot = strrchr(buf, '.');
1902 strlcpy(bmpdir, buf, dot - buf + 1);
1904 else
1906 snprintf(bmpdir, MAX_PATH, "%s", BACKDROP_DIR);
1908 /* load the bitmaps that were found by the parsing */
1909 if (!load_skin_bitmaps(wps_data, bmpdir) ||
1910 !skin_load_fonts(wps_data))
1912 skin_data_reset(wps_data);
1913 #ifndef APPLICATION
1914 skin_buffer_restore_position();
1915 #endif
1916 return false;
1918 #endif
1919 #if defined(HAVE_ALBUMART) && !defined(__PCTOOL__)
1920 status = audio_status();
1921 if (status & AUDIO_STATUS_PLAY)
1923 struct skin_albumart *aa = wps_data->albumart;
1924 if (aa && ((aa->state && !old_aa.state) ||
1925 (aa->state &&
1926 (((old_aa.height != aa->height) ||
1927 (old_aa.width != aa->width))))))
1929 curtrack = audio_current_track();
1930 offset = curtrack->offset;
1931 audio_stop();
1932 if (!(status & AUDIO_STATUS_PAUSE))
1933 audio_play(offset);
1936 #endif
1937 wps_data->wps_loaded = true;
1938 #ifdef DEBUG_SKIN_ENGINE
1939 // if (isfile && debug_wps)
1940 // debug_skin_usage();
1941 #endif
1942 return true;