More tab fixes
[kugel-rb.git] / apps / gui / skin_engine / skin_parser.c
blob4158165f53479632a8956baf3cde36aa4fe8f6c5
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 data->timeout = element->params[i].data.number;
1033 data->timeout *= TIMEOUT_UNIT;
1034 token->value.data = data;
1035 return 0;
1038 struct touchaction {const char* s; int action;};
1039 static const struct touchaction touchactions[] = {
1040 /* generic actions, convert to screen actions on use */
1041 {"none", ACTION_TOUCHSCREEN},
1042 {"prev", ACTION_STD_PREV }, {"next", ACTION_STD_NEXT },
1043 {"rwd", ACTION_STD_PREVREPEAT }, {"ffwd", ACTION_STD_NEXTREPEAT },
1044 {"hotkey", ACTION_STD_HOTKEY}, {"select", ACTION_STD_OK },
1045 {"menu", ACTION_STD_MENU }, {"cancel", ACTION_STD_CANCEL },
1046 {"contextmenu", ACTION_STD_CONTEXT},{"quickscreen", ACTION_STD_QUICKSCREEN },
1048 /* list/tree actions */
1049 { "resumeplayback", ACTION_TREE_WPS}, /* returns to previous music, WPS/FM */
1050 /* not really WPS specific, but no equivilant ACTION_STD_* */
1051 {"voldown", ACTION_WPS_VOLDOWN}, {"volup", ACTION_WPS_VOLUP},
1052 {"mute", ACTION_TOUCH_MUTE },
1054 /* generic settings changers */
1055 {"setting_inc", ACTION_SETTINGS_INC}, {"setting_dec", ACTION_SETTINGS_DEC},
1056 {"setting_set", ACTION_SETTINGS_SET},
1058 /* WPS specific actions */
1059 {"wps_prev", ACTION_WPS_SKIPPREV }, {"wps_next", ACTION_WPS_SKIPNEXT },
1060 {"browse", ACTION_WPS_BROWSE },
1061 {"play", ACTION_WPS_PLAY }, {"stop", ACTION_WPS_STOP },
1062 {"shuffle", ACTION_TOUCH_SHUFFLE }, {"repmode", ACTION_TOUCH_REPMODE },
1063 {"pitch", ACTION_WPS_PITCHSCREEN}, {"playlist", ACTION_WPS_VIEW_PLAYLIST },
1065 #if CONFIG_TUNER
1066 /* FM screen actions */
1067 /* Also allow browse, play, stop from WPS codes */
1068 {"mode", ACTION_FM_MODE }, {"record", ACTION_FM_RECORD },
1069 {"presets", ACTION_FM_PRESET},
1070 #endif
1073 static int parse_touchregion(struct skin_element *element,
1074 struct wps_token *token,
1075 struct wps_data *wps_data)
1077 (void)token;
1078 unsigned i, imax;
1079 int p;
1080 struct touchregion *region = NULL;
1081 const char *action;
1082 const char pb_string[] = "progressbar";
1083 const char vol_string[] = "volume";
1084 char temp[20];
1086 /* format: %T([label,], x,y,width,height,action[, ...])
1087 * if action starts with & the area must be held to happen
1091 region = (struct touchregion*)skin_buffer_alloc(sizeof(struct touchregion));
1092 if (!region)
1093 return WPS_ERROR_INVALID_PARAM;
1095 /* should probably do some bounds checking here with the viewport... but later */
1096 region->action = ACTION_NONE;
1098 if (element->params[0].type == STRING)
1100 region->label = element->params[0].data.text;
1101 p = 1;
1102 /* "[SI]III[SI]|SS" is the param list. There MUST be 4 numbers
1103 * followed by at least one string. Verify that here */
1104 if (element->params_count < 6 ||
1105 element->params[4].type != INTEGER)
1106 return WPS_ERROR_INVALID_PARAM;
1108 else
1110 region->label = NULL;
1111 p = 0;
1114 region->x = element->params[p++].data.number;
1115 region->y = element->params[p++].data.number;
1116 region->width = element->params[p++].data.number;
1117 region->height = element->params[p++].data.number;
1118 region->wvp = curr_vp;
1119 region->armed = false;
1120 region->reverse_bar = false;
1121 region->value = 0;
1122 region->last_press = 0xffff;
1123 region->press_length = PRESS;
1124 action = element->params[p++].data.text;
1126 strcpy(temp, action);
1127 action = temp;
1129 if (*action == '!')
1131 region->reverse_bar = true;
1132 action++;
1135 if(!strcmp(pb_string, action))
1136 region->action = ACTION_TOUCH_SCROLLBAR;
1137 else if(!strcmp(vol_string, action))
1138 region->action = ACTION_TOUCH_VOLUME;
1139 else
1141 if (*action == '&')
1143 action++;
1144 region->press_length = LONG_PRESS;
1146 else if(*action == '*')
1148 action++;
1149 region->press_length = REPEAT;
1151 else
1152 region->press_length = PRESS;
1154 imax = ARRAYLEN(touchactions);
1155 for (i = 0; i < imax; i++)
1157 /* try to match with one of our touchregion screens */
1158 if (!strcmp(touchactions[i].s, action))
1160 region->action = touchactions[i].action;
1161 if (region->action == ACTION_SETTINGS_INC ||
1162 region->action == ACTION_SETTINGS_DEC ||
1163 region->action == ACTION_SETTINGS_SET)
1165 if (element->params_count < p+1)
1167 return WPS_ERROR_INVALID_PARAM;
1169 else
1171 char *name = element->params[p].data.text;
1172 int j;
1173 /* Find the setting */
1174 for (j=0; j<nb_settings; j++)
1175 if (settings[j].cfg_name &&
1176 !strcmp(settings[j].cfg_name, name))
1177 break;
1178 if (j==nb_settings)
1179 return WPS_ERROR_INVALID_PARAM;
1180 region->setting_data.setting = (void*)&settings[j];
1181 if (region->action == ACTION_SETTINGS_SET)
1183 char* text;
1184 int temp;
1185 struct touchsetting *setting =
1186 &region->setting_data;
1187 if (element->params_count < p+2)
1188 return WPS_ERROR_INVALID_PARAM;
1189 #ifndef __PCTOOL__
1190 text = element->params[p+1].data.text;
1191 switch (settings[j].flags&F_T_MASK)
1193 case F_T_CUSTOM:
1194 setting->value.text = text;
1195 break;
1196 case F_T_INT:
1197 case F_T_UINT:
1198 if (settings[j].cfg_vals == NULL)
1200 setting->value.number = atoi(text);
1202 else if (cfg_string_to_int(j, &temp, text))
1204 if (settings[j].flags&F_TABLE_SETTING)
1205 setting->value.number =
1206 settings[j].table_setting->values[temp];
1207 else
1208 setting->value.number = temp;
1210 else
1211 return WPS_ERROR_INVALID_PARAM;
1212 break;
1213 case F_T_BOOL:
1214 if (cfg_string_to_int(j, &temp, text))
1216 setting->value.number = temp;
1218 else
1219 return WPS_ERROR_INVALID_PARAM;
1220 break;
1221 default:
1222 return WPS_ERROR_INVALID_PARAM;
1224 #endif /* __PCTOOL__ */
1228 break;
1231 if (region->action == ACTION_NONE)
1232 return WPS_ERROR_INVALID_PARAM;
1234 struct skin_token_list *item = new_skin_token_list_item(NULL, region);
1235 if (!item)
1236 return WPS_ERROR_INVALID_PARAM;
1237 add_to_ll_chain(&wps_data->touchregions, item);
1239 if (region->action == ACTION_TOUCH_MUTE)
1241 region->value = global_settings.volume;
1245 return 0;
1247 #endif
1249 static bool check_feature_tag(const int type)
1251 switch (type)
1253 case SKIN_TOKEN_RTC_PRESENT:
1254 #if CONFIG_RTC
1255 return true;
1256 #else
1257 return false;
1258 #endif
1259 case SKIN_TOKEN_HAVE_RECORDING:
1260 #ifdef HAVE_RECORDING
1261 return true;
1262 #else
1263 return false;
1264 #endif
1265 case SKIN_TOKEN_HAVE_TUNER:
1266 #if CONFIG_TUNER
1267 if (radio_hardware_present())
1268 return true;
1269 #endif
1270 return false;
1271 case SKIN_TOKEN_HAVE_TOUCH:
1272 #ifdef HAVE_TOUCHSCREEN
1273 return true;
1274 #else
1275 return false;
1276 #endif
1278 #if CONFIG_TUNER
1279 case SKIN_TOKEN_HAVE_RDS:
1280 #ifdef HAVE_RDS_CAP
1281 return true;
1282 #else
1283 return false;
1284 #endif /* HAVE_RDS_CAP */
1285 #endif /* CONFIG_TUNER */
1286 default: /* not a tag we care about, just don't skip */
1287 return true;
1292 * initial setup of wps_data; does reset everything
1293 * except fields which need to survive, i.e.
1296 static void skin_data_reset(struct wps_data *wps_data)
1298 wps_data->tree = NULL;
1299 #ifdef HAVE_LCD_BITMAP
1300 wps_data->images = NULL;
1301 #endif
1302 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
1303 if (wps_data->backdrop_id >= 0)
1304 skin_backdrop_unload(wps_data->backdrop_id);
1305 wps_data->backdrop = NULL;
1306 #endif
1307 #ifdef HAVE_TOUCHSCREEN
1308 wps_data->touchregions = NULL;
1309 #endif
1310 #ifdef HAVE_SKIN_VARIABLES
1311 wps_data->skinvars = NULL;
1312 #endif
1313 #ifdef HAVE_ALBUMART
1314 wps_data->albumart = NULL;
1315 if (wps_data->playback_aa_slot >= 0)
1317 playback_release_aa_slot(wps_data->playback_aa_slot);
1318 wps_data->playback_aa_slot = -1;
1320 #endif
1322 #ifdef HAVE_LCD_BITMAP
1323 wps_data->peak_meter_enabled = false;
1324 wps_data->wps_sb_tag = false;
1325 wps_data->show_sb_on_wps = false;
1326 #else /* HAVE_LCD_CHARCELLS */
1327 /* progress bars */
1328 int i;
1329 for (i = 0; i < 8; i++)
1331 wps_data->wps_progress_pat[i] = 0;
1333 wps_data->full_line_progressbar = false;
1334 #endif
1335 wps_data->wps_loaded = false;
1338 #ifdef HAVE_LCD_BITMAP
1339 static bool load_skin_bmp(struct wps_data *wps_data, struct bitmap *bitmap, char* bmpdir)
1341 (void)wps_data; /* only needed for remote targets */
1342 char img_path[MAX_PATH];
1343 int fd;
1344 get_image_filename(bitmap->data, bmpdir,
1345 img_path, sizeof(img_path));
1347 /* load the image */
1348 int format;
1349 #ifdef HAVE_REMOTE_LCD
1350 if (curr_screen == SCREEN_REMOTE)
1351 format = FORMAT_ANY|FORMAT_REMOTE;
1352 else
1353 #endif
1354 format = FORMAT_ANY|FORMAT_TRANSPARENT;
1356 fd = open(img_path, O_RDONLY);
1357 if (fd < 0)
1359 DEBUGF("Couldn't open %s\n", img_path);
1360 return false;
1362 size_t buf_size = read_bmp_fd(fd, bitmap, 0,
1363 format|FORMAT_RETURN_SIZE, NULL);
1364 char* imgbuf = (char*)skin_buffer_alloc(buf_size);
1365 if (!imgbuf)
1367 #ifndef APPLICATION
1368 DEBUGF("Not enough skin buffer: need %zd more.\n",
1369 buf_size - skin_buffer_freespace());
1370 #endif
1371 close(fd);
1372 return NULL;
1374 lseek(fd, 0, SEEK_SET);
1375 bitmap->data = imgbuf;
1376 int ret = read_bmp_fd(fd, bitmap, buf_size, format, NULL);
1378 close(fd);
1379 if (ret > 0)
1381 return true;
1383 else
1385 /* Abort if we can't load an image */
1386 DEBUGF("Couldn't load '%s'\n", img_path);
1387 return false;
1391 static bool load_skin_bitmaps(struct wps_data *wps_data, char *bmpdir)
1393 struct skin_token_list *list;
1394 bool retval = true; /* return false if a single image failed to load */
1396 /* regular images */
1397 list = wps_data->images;
1398 while (list)
1400 struct gui_img *img = (struct gui_img*)list->token->value.data;
1401 if (img->bm.data)
1403 if (img->using_preloaded_icons)
1405 img->loaded = true;
1406 list->token->type = SKIN_TOKEN_IMAGE_DISPLAY_LISTICON;
1408 else
1410 img->loaded = load_skin_bmp(wps_data, &img->bm, bmpdir);
1411 if (img->loaded)
1412 img->subimage_height = img->bm.height / img->num_subimages;
1413 else
1414 retval = false;
1417 list = list->next;
1420 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1))
1421 wps_data->backdrop_id = skin_backdrop_assign(wps_data->backdrop, bmpdir, curr_screen);
1422 #endif /* has backdrop support */
1423 return retval;
1426 static bool skin_load_fonts(struct wps_data *data)
1428 /* don't spit out after the first failue to aid debugging */
1429 bool success = true;
1430 struct skin_element *vp_list;
1431 int font_id;
1432 /* walk though each viewport and assign its font */
1433 for(vp_list = data->tree; vp_list; vp_list = vp_list->next)
1435 /* first, find the viewports that have a non-sys/ui-font font */
1436 struct skin_viewport *skin_vp =
1437 (struct skin_viewport*)vp_list->data;
1438 struct viewport *vp = &skin_vp->vp;
1441 if (vp->font <= FONT_UI)
1442 { /* the usual case -> built-in fonts */
1443 #ifdef HAVE_REMOTE_LCD
1444 if (vp->font == FONT_UI)
1445 vp->font += curr_screen;
1446 #endif
1447 continue;
1449 font_id = vp->font;
1451 /* now find the corresponding skin_font */
1452 struct skin_font *font = &skinfonts[font_id-FONT_FIRSTUSERFONT];
1453 if (!font->name)
1455 if (success)
1457 DEBUGF("font %d not specified\n", font_id);
1459 success = false;
1460 continue;
1463 /* load the font - will handle loading the same font again if
1464 * multiple viewports use the same */
1465 if (font->id < 0)
1467 char *dot = strchr(font->name, '.');
1468 *dot = '\0';
1469 font->id = skin_font_load(font->name,
1470 skinfonts[font_id-FONT_FIRSTUSERFONT].glyphs);
1473 if (font->id < 0)
1475 DEBUGF("Unable to load font %d: '%s.fnt'\n",
1476 font_id, font->name);
1477 font->name = NULL; /* to stop trying to load it again if we fail */
1478 success = false;
1479 font->name = NULL;
1480 continue;
1483 /* finally, assign the font_id to the viewport */
1484 vp->font = font->id;
1486 return success;
1489 #endif /* HAVE_LCD_BITMAP */
1490 static int convert_viewport(struct wps_data *data, struct skin_element* element)
1492 struct skin_viewport *skin_vp =
1493 (struct skin_viewport *)skin_buffer_alloc(sizeof(struct skin_viewport));
1494 struct screen *display = &screens[curr_screen];
1496 if (!skin_vp)
1497 return CALLBACK_ERROR;
1499 skin_vp->hidden_flags = 0;
1500 skin_vp->label = NULL;
1501 skin_vp->is_infovp = false;
1502 element->data = skin_vp;
1503 curr_vp = skin_vp;
1504 curr_viewport_element = element;
1506 viewport_set_defaults(&skin_vp->vp, curr_screen);
1507 #ifdef HAVE_REMOTE_LCD
1508 /* viewport_set_defaults() sets the font to FONT_UI+curr_screen.
1509 * This parser requires font 1 to always be the UI font,
1510 * so force it back to FONT_UI and handle the screen number at the end */
1511 skin_vp->vp.font = FONT_UI;
1512 #endif
1514 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1))
1515 skin_vp->start_fgcolour = skin_vp->vp.fg_pattern;
1516 skin_vp->start_bgcolour = skin_vp->vp.bg_pattern;
1517 #endif
1520 struct skin_tag_parameter *param = element->params;
1521 if (element->params_count == 0) /* default viewport */
1523 if (!data->tree) /* first viewport in the skin */
1524 data->tree = element;
1525 skin_vp->label = VP_DEFAULT_LABEL;
1526 return CALLBACK_OK;
1529 if (element->params_count == 6)
1531 if (element->tag->type == SKIN_TOKEN_UIVIEWPORT_LOAD)
1533 skin_vp->is_infovp = true;
1534 if (isdefault(param))
1536 skin_vp->hidden_flags = VP_NEVER_VISIBLE;
1537 skin_vp->label = VP_DEFAULT_LABEL;
1539 else
1541 skin_vp->hidden_flags = VP_NEVER_VISIBLE;
1542 skin_vp->label = param->data.text;
1545 else
1547 skin_vp->hidden_flags = VP_DRAW_HIDEABLE|VP_DRAW_HIDDEN;
1548 skin_vp->label = param->data.text;
1550 param++;
1552 /* x */
1553 if (!isdefault(param))
1555 skin_vp->vp.x = param->data.number;
1556 if (param->data.number < 0)
1557 skin_vp->vp.x += display->lcdwidth;
1559 param++;
1560 /* y */
1561 if (!isdefault(param))
1563 skin_vp->vp.y = param->data.number;
1564 if (param->data.number < 0)
1565 skin_vp->vp.y += display->lcdheight;
1567 param++;
1568 /* width */
1569 if (!isdefault(param))
1571 skin_vp->vp.width = param->data.number;
1572 if (param->data.number < 0)
1573 skin_vp->vp.width = (skin_vp->vp.width + display->lcdwidth) - skin_vp->vp.x;
1575 else
1577 skin_vp->vp.width = display->lcdwidth - skin_vp->vp.x;
1579 param++;
1580 /* height */
1581 if (!isdefault(param))
1583 skin_vp->vp.height = param->data.number;
1584 if (param->data.number < 0)
1585 skin_vp->vp.height = (skin_vp->vp.height + display->lcdheight) - skin_vp->vp.y;
1587 else
1589 skin_vp->vp.height = display->lcdheight - skin_vp->vp.y;
1591 param++;
1592 #ifdef HAVE_LCD_BITMAP
1593 /* font */
1594 if (!isdefault(param))
1596 skin_vp->vp.font = param->data.number;
1598 #endif
1599 if ((unsigned) skin_vp->vp.x >= (unsigned) display->lcdwidth ||
1600 skin_vp->vp.width + skin_vp->vp.x > display->lcdwidth ||
1601 (unsigned) skin_vp->vp.y >= (unsigned) display->lcdheight ||
1602 skin_vp->vp.height + skin_vp->vp.y > display->lcdheight)
1603 return CALLBACK_ERROR;
1605 return CALLBACK_OK;
1608 static int skin_element_callback(struct skin_element* element, void* data)
1610 struct wps_data *wps_data = (struct wps_data *)data;
1611 struct wps_token *token;
1612 parse_function function = NULL;
1614 switch (element->type)
1616 /* IMPORTANT: element params are shared, so copy them if needed
1617 * or use then NOW, dont presume they have a long lifespan
1619 case TAG:
1621 token = (struct wps_token*)skin_buffer_alloc(sizeof(struct wps_token));
1622 memset(token, 0, sizeof(*token));
1623 token->type = element->tag->type;
1625 if (element->tag->flags&SKIN_RTC_REFRESH)
1627 #if CONFIG_RTC
1628 curr_line->update_mode |= SKIN_REFRESH_DYNAMIC;
1629 #else
1630 curr_line->update_mode |= SKIN_REFRESH_STATIC;
1631 #endif
1633 else
1634 curr_line->update_mode |= element->tag->flags&SKIN_REFRESH_ALL;
1636 element->data = token;
1638 /* Some tags need special handling for the tag, so add them here */
1639 switch (token->type)
1641 case SKIN_TOKEN_ALIGN_LANGDIRECTION:
1642 follow_lang_direction = 2;
1643 break;
1644 case SKIN_TOKEN_LOGICAL_IF:
1645 function = parse_logical_if;
1646 break;
1647 case SKIN_TOKEN_PROGRESSBAR:
1648 case SKIN_TOKEN_VOLUME:
1649 case SKIN_TOKEN_BATTERY_PERCENT:
1650 case SKIN_TOKEN_PLAYER_PROGRESSBAR:
1651 case SKIN_TOKEN_PEAKMETER_LEFT:
1652 case SKIN_TOKEN_PEAKMETER_RIGHT:
1653 #ifdef HAVE_RADIO_RSSI
1654 case SKIN_TOKEN_TUNER_RSSI:
1655 #endif
1656 function = parse_progressbar_tag;
1657 break;
1658 case SKIN_TOKEN_SUBLINE_TIMEOUT:
1659 case SKIN_TOKEN_BUTTON_VOLUME:
1660 case SKIN_TOKEN_TRACK_STARTING:
1661 case SKIN_TOKEN_TRACK_ENDING:
1662 function = parse_timeout_tag;
1663 break;
1664 #ifdef HAVE_LCD_BITMAP
1665 case SKIN_TOKEN_DISABLE_THEME:
1666 case SKIN_TOKEN_ENABLE_THEME:
1667 case SKIN_TOKEN_DRAW_INBUILTBAR:
1668 function = parse_statusbar_tags;
1669 break;
1670 case SKIN_TOKEN_LIST_TITLE_TEXT:
1671 #ifndef __PCTOOL__
1672 sb_skin_has_title(curr_screen);
1673 #endif
1674 break;
1675 #endif
1676 case SKIN_TOKEN_FILE_DIRECTORY:
1677 token->value.i = element->params[0].data.number;
1678 break;
1679 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1))
1680 case SKIN_TOKEN_VIEWPORT_FGCOLOUR:
1681 case SKIN_TOKEN_VIEWPORT_BGCOLOUR:
1682 function = parse_viewportcolour;
1683 break;
1684 case SKIN_TOKEN_IMAGE_BACKDROP:
1685 function = parse_image_special;
1686 break;
1687 #endif
1688 case SKIN_TOKEN_TRANSLATEDSTRING:
1689 case SKIN_TOKEN_SETTING:
1690 function = parse_setting_and_lang;
1691 break;
1692 #ifdef HAVE_LCD_BITMAP
1693 case SKIN_TOKEN_VIEWPORT_CUSTOMLIST:
1694 function = parse_playlistview;
1695 break;
1696 case SKIN_TOKEN_LOAD_FONT:
1697 function = parse_font_load;
1698 break;
1699 case SKIN_TOKEN_VIEWPORT_ENABLE:
1700 case SKIN_TOKEN_UIVIEWPORT_ENABLE:
1701 token->value.data = element->params[0].data.text;
1702 break;
1703 case SKIN_TOKEN_IMAGE_PRELOAD_DISPLAY:
1704 function = parse_image_display;
1705 break;
1706 case SKIN_TOKEN_IMAGE_PRELOAD:
1707 case SKIN_TOKEN_IMAGE_DISPLAY:
1708 function = parse_image_load;
1709 break;
1710 #endif
1711 #ifdef HAVE_TOUCHSCREEN
1712 case SKIN_TOKEN_TOUCHREGION:
1713 function = parse_touchregion;
1714 break;
1715 case SKIN_TOKEN_LASTTOUCH:
1716 function = parse_lasttouch;
1717 break;
1718 #endif
1719 #ifdef HAVE_ALBUMART
1720 case SKIN_TOKEN_ALBUMART_DISPLAY:
1721 if (wps_data->albumart)
1722 wps_data->albumart->vp = &curr_vp->vp;
1723 break;
1724 case SKIN_TOKEN_ALBUMART_LOAD:
1725 function = parse_albumart_load;
1726 break;
1727 #endif
1728 #ifdef HAVE_SKIN_VARIABLES
1729 case SKIN_TOKEN_VAR_SET:
1730 case SKIN_TOKEN_VAR_GETVAL:
1731 case SKIN_TOKEN_VAR_TIMEOUT:
1732 function = parse_skinvar;
1733 break;
1734 #endif
1735 default:
1736 break;
1738 if (function)
1740 if (function(element, token, wps_data) < 0)
1741 return CALLBACK_ERROR;
1743 /* tags that start with 'F', 'I' or 'D' are for the next file */
1744 if ( *(element->tag->name) == 'I' || *(element->tag->name) == 'F' ||
1745 *(element->tag->name) == 'D')
1746 token->next = true;
1747 if (follow_lang_direction > 0 )
1748 follow_lang_direction--;
1749 break;
1751 case VIEWPORT:
1752 return convert_viewport(wps_data, element);
1753 case LINE:
1755 struct line *line =
1756 (struct line *)skin_buffer_alloc(sizeof(struct line));
1757 line->update_mode = SKIN_REFRESH_STATIC;
1758 curr_line = line;
1759 element->data = line;
1761 break;
1762 case LINE_ALTERNATOR:
1764 struct line_alternator *alternator =
1765 (struct line_alternator *)skin_buffer_alloc(sizeof(struct line_alternator));
1766 alternator->current_line = 0;
1767 #ifndef __PCTOOL__
1768 alternator->next_change_tick = current_tick;
1769 #endif
1770 element->data = alternator;
1772 break;
1773 case CONDITIONAL:
1775 struct conditional *conditional =
1776 (struct conditional *)skin_buffer_alloc(sizeof(struct conditional));
1777 conditional->last_value = -1;
1778 conditional->token = element->data;
1779 element->data = conditional;
1780 if (!check_feature_tag(element->tag->type))
1782 return FEATURE_NOT_AVAILABLE;
1784 return CALLBACK_OK;
1786 case TEXT:
1787 curr_line->update_mode |= SKIN_REFRESH_STATIC;
1788 break;
1789 default:
1790 break;
1792 return CALLBACK_OK;
1795 /* to setup up the wps-data from a format-buffer (isfile = false)
1796 from a (wps-)file (isfile = true)*/
1797 bool skin_data_load(enum screen_type screen, struct wps_data *wps_data,
1798 const char *buf, bool isfile)
1800 char *wps_buffer = NULL;
1801 if (!wps_data || !buf)
1802 return false;
1803 #ifdef HAVE_ALBUMART
1804 int status;
1805 struct mp3entry *curtrack;
1806 long offset;
1807 struct skin_albumart old_aa = {.state = WPS_ALBUMART_NONE};
1808 if (wps_data->albumart)
1810 old_aa.state = wps_data->albumart->state;
1811 old_aa.height = wps_data->albumart->height;
1812 old_aa.width = wps_data->albumart->width;
1814 #endif
1815 #ifdef HAVE_LCD_BITMAP
1816 int i;
1817 for (i=0;i<MAXUSERFONTS;i++)
1819 skinfonts[i].id = -1;
1820 skinfonts[i].name = NULL;
1822 #endif
1823 #ifdef DEBUG_SKIN_ENGINE
1824 if (isfile && debug_wps)
1826 DEBUGF("\n=====================\nLoading '%s'\n=====================\n", buf);
1828 #endif
1831 skin_data_reset(wps_data);
1832 wps_data->wps_loaded = false;
1833 curr_screen = screen;
1834 curr_line = NULL;
1835 curr_vp = NULL;
1836 curr_viewport_element = NULL;
1838 if (isfile)
1840 int fd = open_utf8(buf, O_RDONLY);
1842 if (fd < 0)
1843 return false;
1845 /* get buffer space from the plugin buffer */
1846 size_t buffersize = 0;
1847 wps_buffer = (char *)plugin_get_buffer(&buffersize);
1849 if (!wps_buffer)
1850 return false;
1852 /* copy the file's content to the buffer for parsing,
1853 ensuring that every line ends with a newline char. */
1854 unsigned int start = 0;
1855 while(read_line(fd, wps_buffer + start, buffersize - start) > 0)
1857 start += strlen(wps_buffer + start);
1858 if (start < buffersize - 1)
1860 wps_buffer[start++] = '\n';
1861 wps_buffer[start] = 0;
1864 close(fd);
1865 if (start <= 0)
1866 return false;
1868 else
1870 wps_buffer = (char*)buf;
1872 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
1873 wps_data->backdrop = "-";
1874 wps_data->backdrop_id = -1;
1875 #endif
1876 /* parse the skin source */
1877 #ifndef APPLICATION
1878 skin_buffer_save_position();
1879 #endif
1880 wps_data->tree = skin_parse(wps_buffer, skin_element_callback, wps_data);
1881 if (!wps_data->tree) {
1882 skin_data_reset(wps_data);
1883 #ifndef APPLICATION
1884 skin_buffer_restore_position();
1885 #endif
1886 return false;
1889 #ifdef HAVE_LCD_BITMAP
1890 char bmpdir[MAX_PATH];
1891 if (isfile)
1893 /* get the bitmap dir */
1894 char *dot = strrchr(buf, '.');
1895 strlcpy(bmpdir, buf, dot - buf + 1);
1897 else
1899 snprintf(bmpdir, MAX_PATH, "%s", BACKDROP_DIR);
1901 /* load the bitmaps that were found by the parsing */
1902 if (!load_skin_bitmaps(wps_data, bmpdir) ||
1903 !skin_load_fonts(wps_data))
1905 skin_data_reset(wps_data);
1906 #ifndef APPLICATION
1907 skin_buffer_restore_position();
1908 #endif
1909 return false;
1911 #endif
1912 #if defined(HAVE_ALBUMART) && !defined(__PCTOOL__)
1913 status = audio_status();
1914 if (status & AUDIO_STATUS_PLAY)
1916 struct skin_albumart *aa = wps_data->albumart;
1917 if (aa && ((aa->state && !old_aa.state) ||
1918 (aa->state &&
1919 (((old_aa.height != aa->height) ||
1920 (old_aa.width != aa->width))))))
1922 curtrack = audio_current_track();
1923 offset = curtrack->offset;
1924 audio_stop();
1925 if (!(status & AUDIO_STATUS_PAUSE))
1926 audio_play(offset);
1929 #endif
1930 wps_data->wps_loaded = true;
1931 #ifdef DEBUG_SKIN_ENGINE
1932 // if (isfile && debug_wps)
1933 // debug_skin_usage();
1934 #endif
1935 return true;