summaryrefslogtreecommitdiffstats
path: root/utils/newparser/skin_render.c
blob: d8facb03f686458da4533ac778809c133c3dd944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: skin_parser.c 26752 2010-06-10 21:22:16Z bieber $
 *
 * Copyright (C) 2010 Jonathan Gordon
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

#include "skin_parser.h"
#include "skin_debug.h"
#include "tag_table.h"
#include "symbols.h"
#include "skin_scan.h"
#include "skin_structs.h"

#define MAX_LINE 1024

typedef void (*skin_render_func)(struct skin_element* alternator, 
                            char* buf, size_t buf_size, int line_number);
void skin_render_alternator(struct skin_element* alternator, 
                            char* buf, size_t buf_size, int line_number);

static void do_tags_in_hidden_conditional(struct skin_element* branch)
{
    /* Tags here are ones which need to be "turned off" or cleared 
     * if they are in a conditional branch which isnt being used */
    if (branch->type == SUBLINES)
    {
        int i;
        for (i=0; i<branch->children_count; i++)
        {
            do_tags_in_hidden_conditional(branch->children[i]);
        }
    }
    else if (branch->type == LINE)
    {
        struct skin_element *child = branch->children[0];
        while (child)
        {
            if (child->type != TAG)
            {
                child = child->next;
                continue;
            }
            switch (child->tag->type)
            {
                case SKIN_TOKEN_PEAKMETER:
                    /* stop the peak meter */
                    break;
                case SKIN_TOKEN_ALBUMART_DISPLAY:
                    /* clear the AA image */
                    break;
                case SKIN_TOKEN_IMAGE_DISPLAY:
                case SKIN_TOKEN_IMAGE_PRELOAD_DISPLAY:
                    printf("disable image\n");
                    /* clear images */
                    break;
                default:
                    break;
            }
            child = child->next;
        }
    }
}
        
    
/* Draw a LINE element onto the display */
void skin_render_line(struct skin_element* line, 
                      char* buf, size_t buf_size, int line_number)
{
    int last_value, value;
    if (line->children_count == 0)
        return; /* empty line, do nothing */
    struct skin_element *child = line->children[0];
    skin_render_func func = skin_render_line;
    char tempbuf[128];
    while (child)
    {
        tempbuf[0] = '\0';
        switch (child->type)
        {
            case CONDITIONAL:
                last_value = ((struct conditional*)(child->data))->last_value;
                value = 0; /* actually get it from the token :p */
                if (value >= child->children_count)
                    value = child->children_count-1;
                    
                /* some tags need handling if they are being disabled */
                if (value != last_value && last_value < child->children_count)
                    do_tags_in_hidden_conditional(child->children[last_value]);
                last_value = value;
                
                if (child->children[value]->type == SUBLINES)
                    func = skin_render_alternator;
                else if (child->children[value]->type == LINE)
                    func = skin_render_line;
                func(child->children[value], buf, buf_size, line_number);
                break;
            case TAG:
                snprintf(tempbuf, sizeof(tempbuf), "%%%s", child->tag->name);
                break;
            case TEXT:
                snprintf(tempbuf, sizeof(tempbuf), "%s", (char*)(child->data));
                break;
            case COMMENT:
            default:
                break;
        }
        strcat(buf, tempbuf);
        child = child->next;
    }
}
#define TIME_AFTER(a,b) 1
void skin_render_alternator(struct skin_element* alternator, 
                            char* buf, size_t buf_size, int line_number)
{
    struct subline *subline = (struct subline*)alternator->data;
    if (TIME_AFTER(subline->last_change_tick + subline->timeout, 0/*FIXME*/))
    {
        subline->current_line++;
        if (subline->current_line >= alternator->children_count)
            subline->current_line = 0;
    }
    skin_render_line(alternator->children[subline->current_line],
                     buf, buf_size, line_number);
}

void skin_render_viewport(struct skin_element* viewport, bool draw_tags)
{
    int line_number = 0;
    char linebuf[MAX_LINE];
    skin_render_func func = skin_render_line;
    struct skin_element* line = viewport;
    while (line)
    {
        linebuf[0] = '\0';
        if (line->type == SUBLINES)
            func = skin_render_alternator;
        else if (line->type == LINE)
            func = skin_render_line;
        
        func(line, linebuf, sizeof(linebuf), line_number);
        if (draw_tags)
        {
            printf("[%d]%s", line_number, linebuf);
            if (!((struct line*)line->data)->eat_line_ending)
            {
                printf("\n");
            }
        }
        if (!((struct line*)line->data)->eat_line_ending)
            line_number++;
        line = line->next;
    }
}

void skin_render(struct skin_element* root)
{
    struct skin_element* viewport = root;
    bool draw_tags = viewport->next ? false : true;
    while (viewport)
    {
        skin_render_viewport(viewport->children[0], draw_tags);
        draw_tags = true;
        viewport = viewport->next;
    }
}