adding a bunch of code (vesa, keyb, mouse, etc) to the menu
[cdmenu] / menu / src / dos / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <time.h>
6 #include "app.h"
7 #include "timer.h"
8 #include "keyb.h"
9 #include "vidsys.h"
10 #include "cdpmi.h"
11 #include "mouse.h"
12 #include "logger.h"
13 #include "rtk.h"
14
15 static unsigned char *vmem;
16 static int quit, dirty_valid;
17 static rtk_rect dirty;
18 static int mx, my, prev_mx, prev_my, use_mouse;
19
20
21 int main(int argc, char **argv)
22 {
23         int i;
24         int vmidx;
25         int mdx, mdy, bnstate, bndiff;
26         static int prev_bnstate;
27         char *env;
28
29 #ifdef __DJGPP__
30         __djgpp_nearptr_enable();
31 #endif
32
33         init_logger();
34         if((env = getenv("MENULOG"))) {
35                 if(tolower(env[0]) == 'c' && tolower(env[1]) == 'o' && tolower(env[2]) == 'm'
36                                 && isdigit(env[3])) {
37                         add_log_console(env);
38                 } else {
39                         add_log_file(env);
40                 }
41         }
42
43         if(!(use_mouse = have_mouse())) {
44                 infomsg("no mouse detected\n");
45         }
46         init_timer(0);
47         kb_init();
48
49         if(vid_init() == -1) {
50                 return 1;
51         }
52
53         scr_width = 640;
54         scr_height = 480;
55         if((vmidx = vid_findmode(scr_width, scr_height, 8)) == -1) {
56                 return 1;
57         }
58         if(!(vmem = vid_setmode(vmidx))) {
59                 return 1;
60         }
61
62         if(app_init() == -1) {
63                 goto break_evloop;
64         }
65         app_invalidate(0, 0, 0, 0);
66
67         app_reshape(scr_width, scr_height);
68         mx = scr_width / 2;
69         my = scr_height / 2;
70         prev_mx = prev_my = -1;
71
72         for(;;) {
73                 int key;
74
75                 modkeys = 0;
76                 if(kb_isdown(KEY_ALT)) {
77                         modkeys |= KEY_MOD_ALT;
78                 }
79                 if(kb_isdown(KEY_CTRL)) {
80                         modkeys |= KEY_MOD_CTRL;
81                 }
82                 if(kb_isdown(KEY_SHIFT)) {
83                         modkeys |= KEY_MOD_SHIFT;
84                 }
85
86                 while((key = kb_getkey()) != -1) {
87                         if(key == 'r' && (modkeys & KEY_MOD_CTRL)) {
88                                 app_invalidate(0, 0, 0, 0);
89                         } else {
90                                 app_keyboard(key, 1);
91                         }
92                         if(quit) goto break_evloop;
93                 }
94
95                 if(use_mouse) {
96                         bnstate = read_mouse_bn();
97                         bndiff = bnstate ^ prev_bnstate;
98                         prev_bnstate = bnstate;
99
100                         read_mouse_rel(&mdx, &mdy);
101                         mx += mdx;
102                         if(mx < 0) mx = 0;
103                         if(mx >= scr_width) mx = scr_width - 1;
104                         my += mdy;
105                         if(my < 0) my = 0;
106                         if(my >= scr_height) my = scr_height - 1;
107                         mdx = mx - prev_mx;
108                         mdy = my - prev_my;
109
110                         if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
111                         if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
112                         if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
113
114                         if((mdx | mdy) != 0) {
115                                 app_motion(mx, my);
116                         }
117                 }
118
119                 app_display();
120                 app_swap_buffers();
121         }
122
123 break_evloop:
124         app_shutdown();
125         vid_cleanup();
126         kb_shutdown();
127         return 0;
128 }
129
130 void app_invalidate(int x, int y, int w, int h)
131 {
132         rtk_rect r;
133
134         if((w | h) == 0) {
135                 r.x = r.y = 0;
136                 r.width = scr_width;
137                 r.height = scr_height;
138         } else {
139                 r.x = x;
140                 r.y = y;
141                 r.width = w;
142                 r.height = h;
143         }
144
145         if(dirty_valid) {
146                 rtk_rect_union(&dirty, &r);
147         } else {
148                 dirty = r;
149         }
150         dirty_valid = 1;
151 }
152
153 void app_swap_buffers(void)
154 {
155         unsigned char *src;
156
157         vid_vsync();
158
159         if(dirty_valid) {
160                 if(dirty.width < scr_width || dirty.height < scr_height) {
161                         src = framebuf + dirty.y * scr_width + dirty.x;
162                         vid_blit8(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
163                 } else {
164                         vid_blitfb8(framebuf, 0);
165                 }
166                 dirty_valid = 0;
167         }
168 }
169
170 void app_quit(void)
171 {
172         quit = 1;
173 }
174
175 void app_resize(int x, int y)
176 {
177 }
178
179 void app_fullscreen(int fs)
180 {
181 }
182
183 void app_vsync(int vsync)
184 {
185 }