summaryrefslogtreecommitdiffstats
path: root/apps/gui/skin_engine/skin_backdrops.c
blob: 3e04beaae0c117eda31393f2036d11a57800e90c (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
188
189
190
191
192
193
194
195
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * 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 "config.h"
#include <stdio.h>
#include <stdlib.h>
#include "string-extra.h"
#include "settings.h"
#include "wps_internals.h"
#include "skin_engine.h"
#include "skin_buffer.h"

#if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1))

#define NB_BDROPS SKINNABLE_SCREENS_COUNT*NB_SCREENS
static struct skin_backdrop {
    char name[MAX_PATH];
    char *buffer;
    enum screen_type screen;
    bool loaded;
} backdrops[NB_BDROPS];

#define NB_BDROPS SKINNABLE_SCREENS_COUNT*NB_SCREENS

void skin_backdrop_init(void)
{
    int i;
    for (i=0; i<NB_BDROPS; i++)
    {
        backdrops[i].name[0] = '\0';
        backdrops[i].buffer = NULL;
        backdrops[i].loaded = false;
    }
}

int skin_backdrop_assign(char* backdrop, char *bmpdir,
                         enum screen_type screen)
{
    char filename[MAX_PATH];
    int i, free = -1;
    if (!backdrop)
        return -1;
    if (backdrop[0] == '-')
    {
        filename[0] = '-';
        filename[1] = '\0';
        filename[2] = '\0'; /* we check this later to see if we actually have an
                               image to load. != '\0' means display the image */
    }
    else
    {
        get_image_filename(backdrop, bmpdir, filename, sizeof(filename));
    }
    for (i=0; i<NB_BDROPS; i++)
    {
        if (!backdrops[i].name[0] && free < 0)
            free = i;
        if (!strcmp(backdrops[i].name, filename) && backdrops[i].screen == screen)
        {
            break;
        }
    }
    if (i < NB_BDROPS)
        return i;
    else if (free >= 0)
    {
        strlcpy(backdrops[free].name, filename,
                sizeof (backdrops[free].name));
        backdrops[free].buffer = NULL;
        backdrops[free].screen = screen;
        return free;
    }
    return -1;
}

bool skin_backdrops_preload(void)
{
    bool retval = true;
    int i;
    char *filename;
    for (i=0; i<NB_BDROPS; i++)
    {
        if (backdrops[i].name[0] && !backdrops[i].buffer)
        {
            size_t buf_size;
            enum screen_type screen = backdrops[i].screen;
#if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
            if (screen == SCREEN_REMOTE)
                buf_size = REMOTE_LCD_BACKDROP_BYTES;
            else
#endif
                buf_size = LCD_BACKDROP_BYTES;

            filename = backdrops[i].name;
            if (screen == SCREEN_MAIN && global_settings.backdrop_file[0] &&
                global_settings.backdrop_file[0] != '-' && filename[0] == '-')
            {
                char* temp = filename+2; /* slightly hacky to get a buffer */
                size_t size = sizeof(backdrops[i].name) - 2;
                snprintf(temp, size, BACKDROP_DIR "/%s.bmp", global_settings.backdrop_file);
                filename = temp;
            }
            if (*filename && *filename != '-')
            {
                backdrops[i].buffer = (char*)skin_buffer_alloc(buf_size);
                backdrops[i].loaded = backdrops[i].buffer && 
                         screens[screen].backdrop_load(filename, backdrops[i].buffer);
                if (!backdrops[i].loaded)
                    retval = false;
            }
            if (backdrops[i].name[0] == '-' && backdrops[i].loaded)
                backdrops[i].name[2] = '.';
        }
    }
    return retval;
}

void skin_backdrop_show(int backdrop_id)
{
    if (backdrop_id < 0)
        return;
    enum screen_type screen = backdrops[backdrop_id].screen;
    if ((backdrops[backdrop_id].loaded == false) ||
        (backdrops[backdrop_id].name[0] == '-' &&
        backdrops[backdrop_id].name[2] == '\0'))
    {
        screens[screen].backdrop_show(NULL);
    }
    else if (backdrops[backdrop_id].buffer)
        screens[screen].backdrop_show(backdrops[backdrop_id].buffer);
}

void skin_backdrop_unload(int backdrop_id)
{
    backdrops[backdrop_id].buffer = NULL;
}

void skin_backdrop_load_setting(void)
{
    int i;
    char filename[MAX_PATH];
    for(i=0;i<SKINNABLE_SCREENS_COUNT*NB_SCREENS;i++)
    {
        if (backdrops[i].name[0] == '-' && backdrops[i].screen == SCREEN_MAIN)
        {
            if (global_settings.backdrop_file[0] &&
                global_settings.backdrop_file[0] != '-')
            {
                if (!backdrops[i].buffer)
                    backdrops[i].buffer = (char*)skin_buffer_alloc(LCD_BACKDROP_BYTES);
                snprintf(filename, sizeof filename, BACKDROP_DIR "/%s.bmp",
                         global_settings.backdrop_file);
                bool loaded = backdrops[i].buffer && 
                              screens[SCREEN_MAIN].backdrop_load(filename,
                                                            backdrops[i].buffer);
                backdrops[i].name[2] = loaded ? '.' : '\0';
                return;
            }
            else
                backdrops[i].name[2] = '\0';
        }
#if NB_SCREENS > 1
        else if (backdrops[i].name[0] == '-')
        {
            backdrops[i].name[2] = '\0';
            return;
        }
#endif
    }
}
    
#else

void skin_backdrop_init(void)
{
}
#endif