summaryrefslogtreecommitdiffstats
path: root/uisimulator/sdl/uisdl.c
blob: 178090403fff80ed69f6bdc7d5890ca7c0cf3548 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/***************************************************************************
 *             __________               __   ___.                  
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___  
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /  
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <   
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \  
 *                     \/            \/     \/    \/            \/ 
 * $Id$
 *
 * Copyright (C) 2006 by Daniel Everton <dan@iocaine.org>
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdlib.h>
#include <string.h>
#include "autoconf.h"
#include "button.h"
#include "thread.h"
#include "kernel.h"
#include "sound.h"
#include "uisdl.h"
#include "lcd-sdl.h"
#ifdef HAVE_LCD_BITMAP
#include "lcd-bitmap.h"
#elif HAVE_LCD_CHARCELLS
#include "lcd-charcell.h"
#endif
#ifdef HAVE_REMOTE_LCD
#include "lcd-remote.h"
#endif
#include "thread-sdl.h"
#include "SDL_mutex.h"
#include "SDL_thread.h"

/* extern functions */
extern void                 app_main (void *); // mod entry point
extern void                 new_key(int key);
extern void                 sim_tick_tasks(void);

void button_event(int key, bool pressed);

SDL_Surface *gui_surface;
bool background = false;        /* Don't use backgrounds by default */

SDL_Thread *gui_thread;
SDL_TimerID tick_timer_id;
#ifdef ROCKBOX_HAS_SIMSOUND
SDL_Thread *sound_thread;
#endif

bool lcd_display_redraw = true;         /* Used for player simulator */
char having_new_lcd=true;               /* Used for player simulator */

long start_tick;

Uint32 tick_timer(Uint32 interval, void *param)
{
    long new_tick;

    (void) interval;
    (void) param;
    
    new_tick = (SDL_GetTicks() - start_tick) * HZ / 1000;
        
    if (new_tick != current_tick)
    {
        long i;
        for (i = new_tick - current_tick; i > 0; i--)
            sim_tick_tasks();
        current_tick = new_tick;
    }
    
    return 1;
}

void gui_message_loop(void)
{
    SDL_Event event;
    bool done = false;

    while(!done && SDL_WaitEvent(&event))
    {
        switch(event.type)
        {
            case SDL_KEYDOWN:
                button_event(event.key.keysym.sym, true);
                break;
            case SDL_KEYUP:
                button_event(event.key.keysym.sym, false);
                break;
            case SDL_QUIT:
                done = true;
                break;
            default:
                //printf("Unhandled event\n");
                break;
        }
    }
}

bool gui_startup()
{
    SDL_Surface *picture_surface;
    int width, height;

    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)) {
        fprintf(stderr, "fatal: %s", SDL_GetError());
        return false;
    }

    atexit(SDL_Quit);

    /* Try and load the background image. If it fails go without */
    if (background) {
        picture_surface = SDL_LoadBMP("UI256.bmp");
        if (picture_surface == NULL) {
            background = false;
            fprintf(stderr, "warn: %s", SDL_GetError());
        }
    }

    /* Set things up */

    if (background) {
        width = UI_WIDTH;
        height = UI_HEIGHT;
    } else {
#ifdef HAVE_REMOTE_LCD
        width = UI_LCD_WIDTH > UI_REMOTE_WIDTH ? UI_LCD_WIDTH : UI_REMOTE_WIDTH;
        height = UI_LCD_HEIGHT + UI_REMOTE_HEIGHT;
#else
        width = UI_LCD_WIDTH;
        height = UI_LCD_HEIGHT;
#endif
    }
   
    
    if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
        fprintf(stderr, "fatal: %s\n", SDL_GetError());
        return false;
    }

    SDL_WM_SetCaption(UI_TITLE, NULL);

    sim_lcd_init();
#ifdef HAVE_REMOTE_LCD
    sim_lcd_remote_init();
#endif

    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

    if (background && picture_surface != NULL) {
        SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
        SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
    }
    
    start_tick = SDL_GetTicks();

    return true;
}

bool gui_shutdown()
{
    int i;

    SDL_KillThread(gui_thread);
    SDL_RemoveTimer(tick_timer_id);
#ifdef ROCKBOX_HAS_SIMSOUND
    SDL_KillThread(sound_thread);
#endif

    for (i = 0; i < threadCount; i++)
    {
        SDL_KillThread(threads[i]);
    }

    return true;
}

/**
 * Thin wrapper around normal app_main() to stop gcc complaining about types.
 */
int sim_app_main(void *param)
{
    app_main(param);

    return 0;
}

int main(int argc, char *argv[])
{
    if (argc >= 1) {
        int x;
        for (x = 1; x < argc; x++) {
            if (!strcmp("--background", argv[x])) {
                background = true;
                printf("Using background image.\n");
            } else if (!strcmp("--old_lcd", argv[x])) {
                having_new_lcd = false;
                printf("Using old LCD layout.\n");
            } else if (!strcmp("--zoom", argv[x])) {
                x++;
                display_zoom=atoi(argv[x]);
                printf("Window zoom is %d\n", display_zoom);
            } else {
                printf("rockboxui\n");
                printf("Arguments:\n");
                printf("  --background \t Use background image of hardware\n");
                printf("  --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
                printf("  --zoom \t window zoom (will disable backgrounds)\n");
                exit(0);
            }
        }
    }

    if (display_zoom > 1) {
        background = false;
    }

    
    if (!gui_startup())
        return -1;

    gui_thread = SDL_CreateThread(sim_app_main, NULL);
    if (gui_thread == NULL) {
        printf("Error creating GUI thread!\n");
        return -1;
    }

    tick_timer_id = SDL_AddTimer(10, tick_timer, NULL);

#ifdef ROCKBOX_HAS_SIMSOUND
    sound_thread = SDL_CreateThread(sound_playback_thread, NULL);
    if (sound_thread == NULL) {
        printf("Error creating sound thread!\n");
        return -1;
    }
#endif

    gui_message_loop();

    return gui_shutdown();
}