adding a bunch of code (vesa, keyb, mouse, etc) to the menu
[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 scr_width, scr_height;
12 int fullscr;
13
14 long time_msec;
15
16 struct app_screen *cur_scr;
17
18 unsigned char *framebuf;
19
20 /* available screens */
21 #define MAX_SCREENS     8
22 static struct app_screen *screens[MAX_SCREENS];
23 static int num_screens;
24
25
26 int app_init(void)
27 {
28         int i;
29         char *start_scr_name;
30         static rtk_draw_ops guigfx = {gui_fill, 0, gui_drawtext, gui_textrect};
31
32         rtk_setup(&guigfx);
33
34         /* initialize screens */
35         screens[num_screens++] = &menuscr;
36
37         start_scr_name = getenv("START_SCREEN");
38
39         for(i=0; i<num_screens; i++) {
40                 if(screens[i]->init() == -1) {
41                         return -1;
42                 }
43         }
44
45         time_msec = get_msec();
46
47         for(i=0; i<num_screens; i++) {
48                 if(screens[i]->name && start_scr_name && strcmp(screens[i]->name, start_scr_name) == 0) {
49                         app_chscr(screens[i]);
50                         break;
51                 }
52         }
53         if(!cur_scr) {
54                 app_chscr(&menuscr);
55         }
56
57         return 0;
58 }
59
60 void app_shutdown(void)
61 {
62         int i;
63
64         for(i=0; i<num_screens; i++) {
65                 if(screens[i]->destroy) {
66                         screens[i]->destroy();
67                 }
68         }
69
70         cleanup_logger();
71 }
72
73 void app_display(void)
74 {
75         time_msec = get_msec();
76
77         cur_scr->display();
78 }
79
80 void app_reshape(int x, int y)
81 {
82         int numpix = x * y;
83         int prev_numpix = scr_width * scr_height;
84
85         if(!framebuf || numpix > prev_numpix) {
86                 void *tmp;
87                 if(!(tmp = realloc(framebuf, numpix * sizeof *framebuf))) {
88                         errormsg("failed to resize framebuffer to %dx%d\n", x, y);
89                         return;
90                 }
91                 framebuf = tmp;
92         }
93
94         scr_width = x;
95         scr_height = y;
96
97         if(cur_scr && cur_scr->reshape) {
98                 cur_scr->reshape(x, y);
99         }
100
101         app_invalidate(0, 0, 0, 0);
102 }
103
104 void app_keyboard(int key, int press)
105 {
106         long msec;
107         static long prev_esc;
108
109         if(press) {
110                 switch(key) {
111 #ifdef DBG_ESCQUIT
112                 case 27:
113                         msec = get_msec();
114                         if(msec - prev_esc < 1000) {
115                                 app_quit();
116                                 return;
117                         }
118                         prev_esc = msec;
119                         break;
120 #endif
121
122                 case 'q':
123                         if(modkeys & KEY_MOD_CTRL) {
124                                 app_quit();
125                                 return;
126                         }
127                         break;
128
129                 case '\n':
130                 case '\r':
131                         if(modkeys & KEY_MOD_ALT) {
132                 case KEY_F11:
133                                 app_fullscreen(-1);
134                                 return;
135                         }
136                         break;
137                 }
138         }
139
140         if(cur_scr && cur_scr->keyboard) {
141                 cur_scr->keyboard(key, press);
142         }
143 }
144
145 void app_mouse(int bn, int st, int x, int y)
146 {
147         mouse_x = x;
148         mouse_y = y;
149         if(bn < 3) {
150                 mouse_state[bn] = st;
151         }
152
153         if(cur_scr && cur_scr->mouse) {
154                 cur_scr->mouse(bn, st, x, y);
155         }
156 }
157
158 void app_motion(int x, int y)
159 {
160         if(cur_scr && cur_scr->motion) {
161                 cur_scr->motion(x, y);
162         }
163         mouse_x = x;
164         mouse_y = y;
165 }
166
167 void app_chscr(struct app_screen *scr)
168 {
169         struct app_screen *prev = cur_scr;
170
171         if(!scr) return;
172
173         if(scr->start && scr->start() == -1) {
174                 return;
175         }
176         if(scr->reshape) {
177                 scr->reshape(scr_width, scr_height);
178         }
179
180         if(prev && prev->stop) {
181                 prev->stop();
182         }
183         cur_scr = scr;
184 }
185
186 void gui_fill(rtk_rect *rect, int color)
187 {
188         int i, j;
189         unsigned char *fb;
190
191         if(rect->x < 0) {
192                 rect->width += rect->x;
193                 rect->x = 0;
194         }
195         if(rect->y < 0) {
196                 rect->height += rect->y;
197                 rect->y = 0;
198         }
199         if(rect->x + rect->width >= scr_width) {
200                 rect->width = scr_width - rect->x;
201         }
202         if(rect->y + rect->height >= scr_height) {
203                 rect->height = scr_height - rect->y;
204         }
205
206         fb = framebuf + rect->y * scr_width + rect->x;
207         for(i=0; i<rect->height; i++) {
208                 for(j=0; j<rect->width; j++) {
209                         fb[j] = color;
210                 }
211                 fb += scr_width;
212         }
213 }
214
215 void gui_drawtext(int x, int y, const char *str)
216 {
217 }
218
219 void gui_textrect(const char *str, rtk_rect *rect)
220 {
221 }