foo
[cdmenu] / menu / src / app.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #include "app.h"
6 #include "timer.h"
7 #include "rtk.h"
8
9 int mouse_x, mouse_y, mouse_state[3];
10 unsigned int modkeys;
11 int fullscr;
12
13 long time_msec;
14
15 struct app_screen *cur_scr;
16
17 unsigned char *framebuf;
18
19 /* available screens */
20 #define MAX_SCREENS     8
21 static struct app_screen *screens[MAX_SCREENS];
22 static int num_screens;
23
24
25 int app_init(void)
26 {
27         int i;
28         char *start_scr_name;
29         static rtk_draw_ops guigfx = {gui_fill, 0, gui_drawtext, gui_textrect};
30
31         if(!(framebuf = malloc(SCR_WIDTH * SCR_HEIGHT))) {
32                 errormsg("failed to allocate framebuffer (%dx%d)\n", SCR_WIDTH, SCR_HEIGHT);
33                 return -1;
34         }
35
36         rtk_setup(&guigfx);
37
38         /* initialize screens */
39         screens[num_screens++] = &menuscr;
40
41         start_scr_name = getenv("START_SCREEN");
42
43         for(i=0; i<num_screens; i++) {
44                 if(screens[i]->init() == -1) {
45                         return -1;
46                 }
47         }
48
49         time_msec = get_msec();
50
51         for(i=0; i<num_screens; i++) {
52                 if(screens[i]->name && start_scr_name && strcmp(screens[i]->name, start_scr_name) == 0) {
53                         app_chscr(screens[i]);
54                         break;
55                 }
56         }
57         if(!cur_scr) {
58                 app_chscr(&menuscr);
59         }
60
61         return 0;
62 }
63
64 void app_shutdown(void)
65 {
66         int i;
67
68         for(i=0; i<num_screens; i++) {
69                 if(screens[i]->destroy) {
70                         screens[i]->destroy();
71                 }
72         }
73
74         cleanup_logger();
75         free(framebuf);
76 }
77
78 void app_display(void)
79 {
80         time_msec = get_msec();
81
82         cur_scr->display();
83 }
84
85 void app_keyboard(int key, int press)
86 {
87         long msec;
88         static long prev_esc;
89
90         if(press) {
91                 switch(key) {
92 #ifdef DBG_ESCQUIT
93                 case 27:
94                         msec = get_msec();
95                         if(msec - prev_esc < 1000) {
96                                 app_quit();
97                                 return;
98                         }
99                         prev_esc = msec;
100                         break;
101 #endif
102
103                 case 'q':
104                         if(modkeys & KEY_MOD_CTRL) {
105                                 app_quit();
106                                 return;
107                         }
108                         break;
109                 }
110         }
111
112         if(cur_scr && cur_scr->keyboard) {
113                 cur_scr->keyboard(key, press);
114         }
115 }
116
117 void app_mouse(int bn, int st, int x, int y)
118 {
119         mouse_x = x;
120         mouse_y = y;
121         if(bn < 3) {
122                 mouse_state[bn] = st;
123         }
124
125         if(cur_scr && cur_scr->mouse) {
126                 cur_scr->mouse(bn, st, x, y);
127         }
128 }
129
130 void app_motion(int x, int y)
131 {
132         if(cur_scr && cur_scr->motion) {
133                 cur_scr->motion(x, y);
134         }
135         mouse_x = x;
136         mouse_y = y;
137 }
138
139 void app_chscr(struct app_screen *scr)
140 {
141         struct app_screen *prev = cur_scr;
142
143         if(!scr) return;
144
145         if(scr->start && scr->start() == -1) {
146                 return;
147         }
148
149         if(prev && prev->stop) {
150                 prev->stop();
151         }
152         cur_scr = scr;
153 }
154
155 void gui_fill(rtk_rect *rect, uint32_t color)
156 {
157         int i, j;
158         unsigned char *fb;
159
160         if(rect->x < 0) {
161                 rect->width += rect->x;
162                 rect->x = 0;
163         }
164         if(rect->y < 0) {
165                 rect->height += rect->y;
166                 rect->y = 0;
167         }
168         if(rect->x + rect->width >= SCR_WIDTH) {
169                 rect->width = SCR_WIDTH - rect->x;
170         }
171         if(rect->y + rect->height >= SCR_HEIGHT) {
172                 rect->height = SCR_HEIGHT - rect->y;
173         }
174
175         fb = framebuf + rect->y * SCR_WIDTH + rect->x;
176         for(i=0; i<rect->height; i++) {
177                 for(j=0; j<rect->width; j++) {
178                         fb[j] = color;
179                 }
180                 fb += SCR_WIDTH;
181         }
182 }
183
184 void gui_drawtext(int x, int y, const char *str)
185 {
186 }
187
188 void gui_textrect(const char *str, rtk_rect *rect)
189 {
190 }