forked from DC-SWAT/DreamShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
339 lines (254 loc) · 6.88 KB
/
Copy pathmain.c
File metadata and controls
339 lines (254 loc) · 6.88 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/****************************
* DreamShell ##version## *
* main.c *
* DreamShell main *
* Created by SWAT *
* http://www.dc-swat.ru *
***************************/
#include "ds.h"
#include "fs.h"
//#define EMU
//#define DEBUG
#ifdef EMU
# define DEBUG 1
#endif
extern uint8 romdisk[];
KOS_INIT_FLAGS(INIT_IRQ | INIT_THD_PREEMPT/* | INIT_NET | INIT_MALLOCSTATS | INIT_NO_DCLOAD*/);
KOS_INIT_ROMDISK(romdisk);
static uint32 ver_int = 0;
static const char *build_str[4] = {"Alpha", "Beta", "RC", "Release"};
static mutex_t input_mutex = MUTEX_INITIALIZER;
void gdb_init();
uint32 GetVersion() {
return ver_int;
}
void SetVersion(uint32 ver) {
char ver_str[32];
uint ver_bld;
if(ver == 0) {
ver_int = DS_MAKE_VER(VER_MAJOR, VER_MINOR, VER_MICRO, VER_BUILD);
} else {
ver_int = ver;
}
ver_bld = (uint)DS_VER_BUILD(ver_int);
if(DS_VER_BUILD_TYPE(ver_bld) == DS_VER_BUILD_TYPE_RELEASE) {
snprintf(ver_str, sizeof(ver_str), "%d.%d.%d %s",
(uint)DS_VER_MAJOR(ver_int),
(uint)DS_VER_MINOR(ver_int),
(uint)DS_VER_MICRO(ver_int),
DS_VER_BUILD_TYPE_STR(ver_bld));
} else {
snprintf(ver_str, sizeof(ver_str), "%d.%d.%d.%s.%d",
(uint)DS_VER_MAJOR(ver_int),
(uint)DS_VER_MINOR(ver_int),
(uint)DS_VER_MICRO(ver_int),
DS_VER_BUILD_TYPE_STR(ver_bld),
DS_VER_BUILD_NUM(ver_bld));
}
setenv("VERSION", ver_str, 1);
}
char *GetVersionString() {
return getenv("VERSION");
}
const char *GetVersionBuildTypeString(int type) {
return build_str[type];
}
static int is_emulator() {
#ifndef EMU
return 0;
#else
return 1;
#endif
}
static uint8 *get_board_id() {
uint8 *(*sc)(int, int, int, int);
*((uint32 *)&sc) = *((uint32 *)0x8c0000b0);
return sc(0, 0, 0, 3);
}
int InitDS() {
char fn[MAX_FN_LEN], bf[32];
int tmpi = 0, emu = 0;
uint8 *tmpb = NULL;
Settings_t *settings;
SetVersion(0);
settings = GetSettings();
InitVideoHardware();
ShowLogo();
setenv("HOST", "DreamShell", 1);
setenv("OS", getenv("HOST"), 1);
setenv("USER", getenv("HOST"), 1);
setenv("ARCH", hardware_sys_mode(&tmpi) == HW_TYPE_SET5 ? "Set5.xx" : "Dreamcast", 1);
emu = is_emulator();
if(emu) {
setenv("EMU", "Unknown", 1); // TODO Emu name
}
setenv("SDL_DEBUG", "0", 1);
setenv("SDL_VIDEODRIVER", "dcvideo", 1);
//setenv("SDL_VIDEO_YUV_DIRECT", "1", 1);
//setenv("SDL_VIDEO_YUV_HWACCEL", "1", 1);
//#if !defined(EMU) && defined(DEBUG)
// gdb_init();
//#endif
vmu_draw_string(getenv("HOST"));
dbglog(DBG_INFO, "Initializing DreamShell Core...\n");
#ifdef DEBUG
uint64 t_start = timer_ms_gettime64();
dbglog_set_level(DBG_KDEBUG);
#endif
expt_init();
SetConsoleDebug(1);
if(!emu) {
tmpi = 1;
if(!InitIDE()) {
tmpi = 0;
}
if(!InitSDCard()) {
tmpi = 0;
}
if(tmpi && is_custom_bios()) {
InitRomdisk();
}
}
if(settings->root[0] != 0 && DirExists(settings->root)) {
setenv("PATH", settings->root, 1);
} else {
SearchRoot(0);
}
setenv("HOME", getenv("PATH"), 1);
setenv("$PATH", getenv("PATH"), 1);
setenv("LUA_PATH", getenv("PATH"), 1);
setenv("LUA_CPATH", getenv("PATH"), 1);
setenv("PWD", fs_getwd(), 1);
setenv("APP", (settings->app[0] != 0 ? settings->app : "Main"), 1);
/* If used custom BIOS and syscalls is not installed, setting up it */
if(is_custom_bios() && is_no_syscalls()) {
// if(!setup_syscalls()) {
// tmpb = get_board_id();
// } else {
tmpb = (uint8 *)0x8c000068;
// }
} else {
/* Getting board ID */
tmpb = get_board_id();
if(!tmpi && strncasecmp(getenv("PATH"), "/cd", 3)) {
/* Relax GD drive =) */
cdrom_spin_down();
}
}
memset(fn, 0, MAX_FN_LEN);
for(tmpi = 0; tmpi < 8; tmpi++) {
sprintf(bf, "%02X", tmpb[tmpi]);
strcat(fn, bf);
}
setenv("BOARD_ID", fn, 1);
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
InitEvents();
/* Initializing video */
if(!InitVideo(settings->video.tex_width, settings->video.tex_height, settings->video.bpp)) {
dbglog(DBG_ERROR, "Can't init video: %dx%dx\n", settings->video.tex_width, settings->video.tex_height);
arch_reboot();
}
SetScreenMode(settings->video.virt_width, settings->video.virt_height, 0.0f, 0.0f, 1.0f);
snprintf(fn, MAX_FN_LEN, "%s/gui/cursors/default.png", getenv("PATH"));
SetActiveMouseCursor(CreateMouseCursor(fn, NULL));
TTF_Init();
InitGUI();
snd_stream_init();
snprintf(fn, MAX_FN_LEN, "%s/fonts/bitmap/console.png", getenv("PATH"));
InitCmd();
InitConsole(fn, NULL, 40, 0, 0, GetScreenWidth(), GetScreenHeight(), 255);
if(InitModules()) {
dbglog(DBG_ERROR, "Can't init modules\n");
return -1;
}
if(InitApps()) {
dbglog(DBG_ERROR, "Can't init apps\n");
return -1;
}
if(InitLua()) {
dbglog(DBG_ERROR, "Can't init lua library\n");
return -1;
}
#ifdef DEBUG
uint64 t_end = timer_ms_gettime64();
dbglog(DBG_INFO, "Initializing time: %ld ms\n", (uint32)(t_end - t_start));
#else
if(!emu) {
SetConsoleDebug(0);
dbgio_set_dev_ds();
}
#endif
if(settings->startup[0] == '/') {
snprintf(fn, MAX_FN_LEN, "%s%s", getenv("PATH"), settings->startup);
LuaDo(LUA_DO_FILE, fn, GetLuaState());
} else if(settings->startup[0] == '#') {
dsystem_buff(settings->startup);
} else if(settings->startup[0] != 0) {
LuaDo(LUA_DO_STRING, settings->startup, GetLuaState());
} else {
snprintf(fn, MAX_FN_LEN, "%s/lua/startup.lua", getenv("PATH"));
LuaDo(LUA_DO_FILE, fn, GetLuaState());
}
#ifdef DEBUG
t_end = timer_ms_gettime64();
dbglog(DBG_INFO, "Startup time: %ld ms\n", (uint32)(t_end - t_start));
#endif
HideLogo();
return 0;
}
void ShutdownDS() {
char fn[MAX_FN_LEN];
snprintf(fn, MAX_FN_LEN, "%s/lua/shutdown.lua", getenv("PATH"));
LuaDo(LUA_DO_FILE, fn, GetLuaState());
ShutdownCmd();
ShutdownVideoThread();
ShutdownApps();
ShutdownConsole();
ShutdownModules();
ShutdownEvents();
ShutdownLua();
expt_shutdown();
}
void LockInput() {
mutex_lock(&input_mutex);
}
void UnlockInput() {
mutex_unlock(&input_mutex);
}
int InputIsLocked() {
return mutex_is_locked(&input_mutex);
}
int InputMustLock() {
kthread_t *ct = thd_get_current();
return ct->tid != 1;
}
int main(int argc, char **argv) {
SDL_Event event;
maple_device_t *dev;
int key;
if(InitDS()) {
return -1;
}
while(1) {
LockInput();
while(SDL_PollEvent(&event)) {
if((dev = maple_enum_type(0, MAPLE_FUNC_KEYBOARD))) {
key = kbd_queue_pop(dev, 1);
/* ASCII? */
if(!(key & 0xFFFFFF00)) {
event.key.keysym.unicode = key;
}
}
ProcessInputEvents(&event);
}
UnLoadOldApps();
GUI_ClearTrash();
if(event.type == SDL_QUIT) {
UnlockInput();
break;
}
UnlockInput();
}
ShutdownDS();
return 0;
}