blob: 1690455086b8129175194dd4fd02a0f3cf83bc2a [file] [log] [blame]
Jonathan Gordonb4a95642010-06-11 04:47:46 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: skin_parser.c 26752 2010-06-10 21:22:16Z bieber $
9 *
10 * Copyright (C) 2010 Jonathan Gordon
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
Jonathan Gordon21cbdac2010-06-13 13:54:34 +000025#include <stdbool.h>
Jonathan Gordonb4a95642010-06-11 04:47:46 +000026#include <ctype.h>
27
28#include "skin_parser.h"
29#include "skin_debug.h"
30#include "tag_table.h"
31#include "symbols.h"
32#include "skin_scan.h"
Jonathan Gordon21cbdac2010-06-13 13:54:34 +000033#include "skin_structs.h"
Jonathan Gordonb4a95642010-06-11 04:47:46 +000034
Jonathan Gordon21cbdac2010-06-13 13:54:34 +000035#define MAX_LINE 1024
36
37typedef void (*skin_render_func)(struct skin_element* alternator,
38 char* buf, size_t buf_size, int line_number);
39void skin_render_alternator(struct skin_element* alternator,
40 char* buf, size_t buf_size, int line_number);
Jonathan Gordonb4a95642010-06-11 04:47:46 +000041
Jonathan Gordonb49577b2010-06-14 10:00:22 +000042static void do_tags_in_hidden_conditional(struct skin_element* branch)
43{
44 /* Tags here are ones which need to be "turned off" or cleared
45 * if they are in a conditional branch which isnt being used */
46 if (branch->type == SUBLINES)
47 {
48 int i;
49 for (i=0; i<branch->children_count; i++)
50 {
51 do_tags_in_hidden_conditional(branch->children[i]);
52 }
53 }
54 else if (branch->type == LINE)
55 {
56 struct skin_element *child = branch->children[0];
57 while (child)
58 {
59 if (child->type != TAG)
60 {
61 child = child->next;
62 continue;
63 }
64 switch (child->tag->type)
65 {
66 case SKIN_TOKEN_PEAKMETER:
67 /* stop the peak meter */
68 break;
69 case SKIN_TOKEN_ALBUMART_DISPLAY:
70 /* clear the AA image */
71 break;
72 case SKIN_TOKEN_IMAGE_DISPLAY:
73 case SKIN_TOKEN_IMAGE_PRELOAD_DISPLAY:
74 printf("disable image\n");
75 /* clear images */
76 break;
77 default:
78 break;
79 }
80 child = child->next;
81 }
82 }
83}
84
85
Jonathan Gordonb4a95642010-06-11 04:47:46 +000086/* Draw a LINE element onto the display */
Jonathan Gordon21cbdac2010-06-13 13:54:34 +000087void skin_render_line(struct skin_element* line,
88 char* buf, size_t buf_size, int line_number)
Jonathan Gordonb4a95642010-06-11 04:47:46 +000089{
Jonathan Gordonb49577b2010-06-14 10:00:22 +000090 int last_value, value;
Jonathan Gordon3f062732010-06-13 02:27:13 +000091 if (line->children_count == 0)
92 return; /* empty line, do nothing */
Jonathan Gordonb4a95642010-06-11 04:47:46 +000093 struct skin_element *child = line->children[0];
Jonathan Gordon21cbdac2010-06-13 13:54:34 +000094 skin_render_func func = skin_render_line;
95 char tempbuf[128];
Jonathan Gordonb4a95642010-06-11 04:47:46 +000096 while (child)
97 {
Jonathan Gordon21cbdac2010-06-13 13:54:34 +000098 tempbuf[0] = '\0';
Jonathan Gordonb4a95642010-06-11 04:47:46 +000099 switch (child->type)
100 {
101 case CONDITIONAL:
Jonathan Gordonb49577b2010-06-14 10:00:22 +0000102 last_value = ((struct conditional*)(child->data))->last_value;
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000103 value = 0; /* actually get it from the token :p */
104 if (value >= child->children_count)
105 value = child->children_count-1;
Jonathan Gordonb49577b2010-06-14 10:00:22 +0000106
107 /* some tags need handling if they are being disabled */
108 if (value != last_value && last_value < child->children_count)
109 do_tags_in_hidden_conditional(child->children[last_value]);
110 last_value = value;
111
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000112 if (child->children[value]->type == SUBLINES)
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000113 func = skin_render_alternator;
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000114 else if (child->children[value]->type == LINE)
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000115 func = skin_render_line;
116 func(child->children[value], buf, buf_size, line_number);
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000117 break;
118 case TAG:
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000119 snprintf(tempbuf, sizeof(tempbuf), "%%%s", child->tag->name);
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000120 break;
121 case TEXT:
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000122 snprintf(tempbuf, sizeof(tempbuf), "%s", (char*)(child->data));
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000123 break;
124 case COMMENT:
125 default:
126 break;
127 }
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000128 strcat(buf, tempbuf);
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000129 child = child->next;
130 }
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000131}
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000132#define TIME_AFTER(a,b) 1
133void skin_render_alternator(struct skin_element* alternator,
134 char* buf, size_t buf_size, int line_number)
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000135{
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000136 struct subline *subline = (struct subline*)alternator->data;
137 if (TIME_AFTER(subline->last_change_tick + subline->timeout, 0/*FIXME*/))
138 {
139 subline->current_line++;
140 if (subline->current_line >= alternator->children_count)
141 subline->current_line = 0;
142 }
143 skin_render_line(alternator->children[subline->current_line],
144 buf, buf_size, line_number);
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000145}
146
Jonathan Gordon17c34842010-06-13 14:42:09 +0000147void skin_render_viewport(struct skin_element* viewport, bool draw_tags)
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000148{
Jonathan Gordon3f062732010-06-13 02:27:13 +0000149 int line_number = 0;
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000150 char linebuf[MAX_LINE];
151 skin_render_func func = skin_render_line;
Jonathan Gordon17c34842010-06-13 14:42:09 +0000152 struct skin_element* line = viewport;
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000153 while (line)
154 {
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000155 linebuf[0] = '\0';
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000156 if (line->type == SUBLINES)
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000157 func = skin_render_alternator;
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000158 else if (line->type == LINE)
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000159 func = skin_render_line;
160
Jonathan Gordon17c34842010-06-13 14:42:09 +0000161 func(line, linebuf, sizeof(linebuf), line_number);
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000162 if (draw_tags)
163 {
Jonathan Gordon3f5851f2010-06-14 01:08:39 +0000164 printf("[%d]%s", line_number, linebuf);
Jonathan Gordon17c34842010-06-13 14:42:09 +0000165 if (!((struct line*)line->data)->eat_line_ending)
166 {
167 printf("\n");
168 }
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000169 }
Jonathan Gordondfce3572010-06-13 14:42:47 +0000170 if (!((struct line*)line->data)->eat_line_ending)
171 line_number++;
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000172 line = line->next;
173 }
174}
175
176void skin_render(struct skin_element* root)
177{
178 struct skin_element* viewport = root;
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000179 bool draw_tags = viewport->next ? false : true;
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000180 while (viewport)
181 {
Jonathan Gordon21cbdac2010-06-13 13:54:34 +0000182 skin_render_viewport(viewport->children[0], draw_tags);
183 draw_tags = true;
Jonathan Gordonb4a95642010-06-11 04:47:46 +0000184 viewport = viewport->next;
185 }
186}