e906c010 |
1 | /* |
201c21e2 |
2 | * (C) notaz, 2010-2011 |
e906c010 |
3 | * |
4 | * This work is licensed under the terms of the GNU GPLv2 or later. |
5 | * See the COPYING file in the top-level directory. |
6 | */ |
7 | |
80c2304e |
8 | #include <stdio.h> |
9 | #include <string.h> |
10 | #include <stdarg.h> |
11 | #include <dlfcn.h> |
12 | #include <sys/stat.h> |
13 | #include <sys/types.h> |
14 | #include <unistd.h> |
c5061935 |
15 | #include <signal.h> |
80c2304e |
16 | |
c5061935 |
17 | #include "main.h" |
e906c010 |
18 | #include "plugin.h" |
7c0f51de |
19 | #include "plugin_lib.h" |
14dffdb7 |
20 | #include "pcnt.h" |
3c70c47b |
21 | #include "menu.h" |
80c2304e |
22 | #include "../libpcsxcore/misc.h" |
47bf65ab |
23 | #include "../plugins/cdrcimg/cdrcimg.h" |
69af03a2 |
24 | #include "common/plat.h" |
25 | #include "common/input.h" |
80c2304e |
26 | |
69af03a2 |
27 | int ready_to_go; |
c5061935 |
28 | unsigned long gpuDisp; |
29 | char cfgfile_basename[MAXPATHLEN]; |
b60f2812 |
30 | static char *(*real_getenv)(const char *name); |
80c2304e |
31 | |
32 | static void make_path(char *buf, size_t size, const char *dir, const char *fname) |
33 | { |
34 | if (fname) |
35 | snprintf(buf, size, ".%s%s", dir, fname); |
36 | else |
37 | snprintf(buf, size, ".%s", dir); |
38 | } |
39 | #define MAKE_PATH(buf, dir, fname) \ |
40 | make_path(buf, sizeof(buf), dir, fname) |
41 | |
42 | static void create_profile_dir(const char *directory) { |
43 | char path[MAXPATHLEN]; |
44 | |
45 | MAKE_PATH(path, directory, NULL); |
46 | mkdir(path, S_IRWXU | S_IRWXG); |
47 | } |
48 | |
49 | static void CheckSubDir() { |
50 | // make sure that ~/.pcsx exists |
51 | create_profile_dir(PCSX_DOT_DIR); |
52 | |
53 | create_profile_dir(BIOS_DIR); |
54 | create_profile_dir(MEMCARD_DIR); |
55 | create_profile_dir(STATES_DIR); |
56 | create_profile_dir(PLUGINS_DIR); |
57 | create_profile_dir(PLUGINS_CFG_DIR); |
58 | create_profile_dir(CHEATS_DIR); |
59 | create_profile_dir(PATCHES_DIR); |
cd6e8d0f |
60 | create_profile_dir(PCSX_DOT_DIR "cfg"); |
80c2304e |
61 | } |
62 | |
47bf65ab |
63 | void set_cd_image(const char *fname) |
64 | { |
e16a7e51 |
65 | const char *ext = NULL; |
47bf65ab |
66 | |
e16a7e51 |
67 | if (fname != NULL) { |
68 | int len = strlen(fname); |
69 | ext = fname; |
70 | if (len > 2) |
71 | ext = fname + len - 2; |
72 | } |
47bf65ab |
73 | |
e16a7e51 |
74 | if (ext && strcasecmp(ext, ".z") == 0) { |
47bf65ab |
75 | SetIsoFile(NULL); |
76 | cdrcimg_set_fname(fname); |
77 | strcpy(Config.Cdr, "builtin_cdrcimg"); |
78 | } else { |
79 | SetIsoFile(fname); |
80 | strcpy(Config.Cdr, "builtin_cdr"); |
81 | } |
82 | } |
83 | |
80c2304e |
84 | int main(int argc, char *argv[]) |
85 | { |
86 | char file[MAXPATHLEN] = ""; |
87 | char path[MAXPATHLEN]; |
47bf65ab |
88 | const char *cdfile = NULL; |
80c2304e |
89 | int loadst = 0; |
b60f2812 |
90 | void *tmp; |
80c2304e |
91 | int i; |
92 | |
b60f2812 |
93 | tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY); |
fa9cfe0a |
94 | if (tmp == NULL) |
95 | tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY); |
b60f2812 |
96 | if (tmp != NULL) |
97 | real_getenv = dlsym(tmp, "getenv"); |
98 | if (real_getenv == NULL) { |
99 | fprintf(stderr, "%s\n", dlerror()); |
100 | return 1; |
101 | } |
102 | dlclose(tmp); |
103 | |
80c2304e |
104 | // what is the name of the config file? |
105 | // it may be redefined by -cfg on the command line |
106 | strcpy(cfgfile_basename, "pcsx.cfg"); |
107 | |
e906c010 |
108 | emuLog = stdout; |
80c2304e |
109 | SetIsoFile(NULL); |
80c2304e |
110 | |
111 | // read command line options |
112 | for (i = 1; i < argc; i++) { |
e906c010 |
113 | if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1; |
80c2304e |
114 | else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]); |
115 | else if (!strcmp(argv[i], "-cfg")) { |
116 | if (i+1 >= argc) break; |
117 | strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */ |
118 | printf("Using config file %s.\n", cfgfile_basename); |
119 | } |
120 | else if (!strcmp(argv[i], "-cdfile")) { |
121 | char isofilename[MAXPATHLEN]; |
122 | |
123 | if (i+1 >= argc) break; |
124 | strncpy(isofilename, argv[++i], MAXPATHLEN); |
125 | if (isofilename[0] != '/') { |
126 | getcwd(path, MAXPATHLEN); |
127 | if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) { |
128 | strcat(path, "/"); |
129 | strcat(path, isofilename); |
130 | strcpy(isofilename, path); |
131 | } else |
132 | isofilename[0] = 0; |
133 | } |
134 | |
47bf65ab |
135 | cdfile = isofilename; |
80c2304e |
136 | } |
137 | else if (!strcmp(argv[i], "-h") || |
138 | !strcmp(argv[i], "-help") || |
139 | !strcmp(argv[i], "--help")) { |
140 | printf(PACKAGE_NAME " " PACKAGE_VERSION "\n"); |
141 | printf("%s\n", _( |
142 | " pcsx [options] [file]\n" |
143 | "\toptions:\n" |
144 | "\t-cdfile FILE\tRuns a CD image file\n" |
145 | "\t-nogui\t\tDon't open the GTK GUI\n" |
146 | "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n" |
147 | "\t-psxout\t\tEnable PSX output\n" |
148 | "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n" |
149 | "\t-h -help\tDisplay this message\n" |
150 | "\tfile\t\tLoads file\n")); |
151 | return 0; |
152 | } else { |
153 | strncpy(file, argv[i], MAXPATHLEN); |
154 | if (file[0] != '/') { |
155 | getcwd(path, MAXPATHLEN); |
156 | if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) { |
157 | strcat(path, "/"); |
158 | strcat(path, file); |
159 | strcpy(file, path); |
160 | } else |
161 | file[0] = 0; |
162 | } |
163 | } |
164 | } |
165 | |
166 | memset(&Config, 0, sizeof(PcsxConfig)); |
167 | strcpy(Config.Net, "Disabled"); |
168 | |
169 | CheckSubDir(); |
80c2304e |
170 | |
7e400e1c |
171 | MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd"); |
172 | MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd"); |
e906c010 |
173 | strcpy(Config.Bios, "HLE"); |
e6eb2066 |
174 | strcpy(Config.BiosDir, "bios"); |
e906c010 |
175 | |
176 | strcpy(Config.PluginsDir, "plugins"); |
177 | strcpy(Config.Gpu, "builtin_gpu"); |
178 | strcpy(Config.Spu, "builtin_spu"); |
179 | strcpy(Config.Cdr, "builtin_cdr"); |
180 | strcpy(Config.Pad1, "builtin_pad"); |
181 | strcpy(Config.Pad2, "builtin_pad"); |
4f3639fa |
182 | Config.PsxAuto = 1; |
80c2304e |
183 | |
184 | snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR); |
185 | /* |
186 | // switch to plugin dotdir |
187 | // this lets plugins work without modification! |
188 | gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL); |
189 | chdir(plugin_default_dir); |
190 | g_free(plugin_default_dir); |
191 | */ |
47bf65ab |
192 | |
193 | if (cdfile) |
194 | set_cd_image(cdfile); |
195 | |
69af03a2 |
196 | if (SysInit() == -1) |
197 | return 1; |
80c2304e |
198 | |
69af03a2 |
199 | // frontend stuff |
200 | in_init(); |
201 | in_probe(); |
202 | plat_init(); |
203 | menu_init(); |
80c2304e |
204 | |
69af03a2 |
205 | if (LoadPlugins() == -1) { |
206 | SysMessage("Failed loading plugins!"); |
207 | return 1; |
208 | } |
209 | pcnt_hook_plugins(); |
80c2304e |
210 | |
69af03a2 |
211 | if (OpenPlugins() == -1) { |
212 | return 1; |
213 | } |
201c21e2 |
214 | plugin_call_rearmed_cbs(); |
80c2304e |
215 | |
69af03a2 |
216 | CheckCdrom(); |
bbd837c6 |
217 | SysReset(); |
69af03a2 |
218 | |
219 | if (file[0] != '\0') { |
220 | if (Load(file) != -1) |
221 | ready_to_go = 1; |
222 | } else { |
47bf65ab |
223 | if (cdfile) { |
69af03a2 |
224 | if (LoadCdrom() == -1) { |
225 | ClosePlugins(); |
226 | printf(_("Could not load CD-ROM!\n")); |
227 | return -1; |
80c2304e |
228 | } |
69af03a2 |
229 | ready_to_go = 1; |
80c2304e |
230 | } |
69af03a2 |
231 | } |
80c2304e |
232 | |
69af03a2 |
233 | // If a state has been specified, then load that |
234 | if (loadst) { |
c5061935 |
235 | char state_filename[MAXPATHLEN]; |
236 | int ret = get_state_filename(state_filename, sizeof(state_filename), loadst - 1); |
237 | if (ret == 0) |
238 | ret = LoadState(state_filename); |
69af03a2 |
239 | printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename); |
69af03a2 |
240 | } |
80c2304e |
241 | |
3c70c47b |
242 | if (ready_to_go) |
243 | menu_prepare_emu(); |
244 | else |
69af03a2 |
245 | menu_loop(); |
246 | |
7c0f51de |
247 | pl_start_watchdog(); |
248 | |
69af03a2 |
249 | while (1) |
250 | { |
80c2304e |
251 | psxCpu->Execute(); |
69af03a2 |
252 | menu_loop(); |
80c2304e |
253 | } |
254 | |
255 | return 0; |
256 | } |
257 | |
258 | int SysInit() { |
80c2304e |
259 | if (EmuInit() == -1) { |
260 | printf("PSX emulator couldn't be initialized.\n"); |
261 | return -1; |
262 | } |
263 | |
264 | LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */ |
265 | |
266 | if (Config.Debug) { |
267 | StartDebugger(); |
268 | } |
269 | |
270 | return 0; |
271 | } |
272 | |
273 | void SysRunGui() { |
274 | printf("SysRunGui\n"); |
275 | } |
276 | |
277 | void StartGui() { |
278 | printf("StartGui\n"); |
279 | } |
280 | |
e6eb2066 |
281 | static void dummy_lace() |
282 | { |
283 | } |
284 | |
80c2304e |
285 | void SysReset() { |
e6eb2066 |
286 | // rearmed hack: EmuReset() runs some code when real BIOS is used, |
287 | // but we usually do reset from menu while GPU is not open yet, |
288 | // so we need to prevent updateLace() call.. |
289 | void *real_lace = GPU_updateLace; |
290 | GPU_updateLace = dummy_lace; |
291 | |
80c2304e |
292 | EmuReset(); |
bd6267e6 |
293 | |
294 | // hmh core forgets this |
295 | CDR_stop(); |
e6eb2066 |
296 | |
297 | GPU_updateLace = real_lace; |
80c2304e |
298 | } |
299 | |
300 | void SysClose() { |
301 | EmuShutdown(); |
302 | ReleasePlugins(); |
303 | |
304 | StopDebugger(); |
305 | |
306 | if (emuLog != NULL) fclose(emuLog); |
307 | } |
308 | |
309 | void SysUpdate() { |
80c2304e |
310 | } |
311 | |
312 | void OnFile_Exit() { |
bd6267e6 |
313 | printf("OnFile_Exit\n"); |
201c21e2 |
314 | menu_finish(); |
bd6267e6 |
315 | plat_finish(); |
316 | SysClose(); |
80c2304e |
317 | exit(0); |
318 | } |
319 | |
c5061935 |
320 | int get_state_filename(char *buf, int size, int i) { |
80c2304e |
321 | char trimlabel[33]; |
322 | int j; |
323 | |
324 | strncpy(trimlabel, CdromLabel, 32); |
325 | trimlabel[32] = 0; |
326 | for (j = 31; j >= 0; j--) |
327 | if (trimlabel[j] == ' ') |
328 | trimlabel[j] = 0; |
329 | else |
330 | continue; |
331 | |
c5061935 |
332 | snprintf(buf, size, "." STATES_DIR "%.32s-%.9s.%3.3d", |
80c2304e |
333 | trimlabel, CdromId, i); |
334 | |
c5061935 |
335 | return 0; |
80c2304e |
336 | } |
337 | |
338 | void SysPrintf(const char *fmt, ...) { |
339 | va_list list; |
340 | char msg[512]; |
341 | |
342 | va_start(list, fmt); |
343 | vsprintf(msg, fmt, list); |
344 | va_end(list); |
345 | |
80c2304e |
346 | fprintf(emuLog, "%s", msg); |
347 | } |
348 | |
349 | void SysMessage(const char *fmt, ...) { |
350 | va_list list; |
351 | char msg[512]; |
352 | |
353 | va_start(list, fmt); |
354 | vsprintf(msg, fmt, list); |
355 | va_end(list); |
356 | |
357 | if (msg[strlen(msg) - 1] == '\n') |
358 | msg[strlen(msg) - 1] = 0; |
359 | |
360 | fprintf(stderr, "%s\n", msg); |
361 | } |
362 | |
c5061935 |
363 | static void SignalExit(int sig) { |
364 | ClosePlugins(); |
365 | OnFile_Exit(); |
366 | } |
367 | |
368 | #define PARSEPATH(dst, src) \ |
369 | ptr = src + strlen(src); \ |
370 | while (*ptr != '\\' && ptr != src) ptr--; \ |
371 | if (ptr != src) { \ |
372 | strcpy(dst, ptr+1); \ |
373 | } |
374 | |
375 | static int _OpenPlugins(void) { |
376 | int ret; |
377 | |
378 | signal(SIGINT, SignalExit); |
379 | signal(SIGPIPE, SignalExit); |
380 | |
381 | GPU_clearDynarec(clearDynarec); |
382 | |
383 | ret = CDR_open(); |
384 | if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; } |
385 | ret = SPU_open(); |
386 | if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; } |
387 | SPU_registerCallback(SPUirq); |
388 | // pcsx-rearmed: we handle gpu elsewhere |
389 | //ret = GPU_open(&gpuDisp, "PCSX", NULL); |
390 | //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; } |
391 | ret = PAD1_open(&gpuDisp); |
392 | if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; } |
393 | ret = PAD2_open(&gpuDisp); |
394 | if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; } |
395 | |
396 | if (Config.UseNet && !NetOpened) { |
397 | netInfo info; |
398 | char path[MAXPATHLEN]; |
399 | char dotdir[MAXPATHLEN]; |
400 | |
401 | MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL); |
402 | |
403 | strcpy(info.EmuName, "PCSX " PACKAGE_VERSION); |
404 | strncpy(info.CdromID, CdromId, 9); |
405 | strncpy(info.CdromLabel, CdromLabel, 9); |
406 | info.psxMem = psxM; |
407 | info.GPU_showScreenPic = GPU_showScreenPic; |
408 | info.GPU_displayText = GPU_displayText; |
409 | info.GPU_showScreenPic = GPU_showScreenPic; |
410 | info.PAD_setSensitive = PAD1_setSensitive; |
411 | sprintf(path, "%s%s", Config.BiosDir, Config.Bios); |
412 | strcpy(info.BIOSpath, path); |
413 | strcpy(info.MCD1path, Config.Mcd1); |
414 | strcpy(info.MCD2path, Config.Mcd2); |
415 | sprintf(path, "%s%s", dotdir, Config.Gpu); |
416 | strcpy(info.GPUpath, path); |
417 | sprintf(path, "%s%s", dotdir, Config.Spu); |
418 | strcpy(info.SPUpath, path); |
419 | sprintf(path, "%s%s", dotdir, Config.Cdr); |
420 | strcpy(info.CDRpath, path); |
421 | NET_setInfo(&info); |
422 | |
423 | ret = NET_open(&gpuDisp); |
424 | if (ret < 0) { |
425 | if (ret == -2) { |
426 | // -2 is returned when something in the info |
427 | // changed and needs to be synced |
428 | char *ptr; |
429 | |
430 | PARSEPATH(Config.Bios, info.BIOSpath); |
431 | PARSEPATH(Config.Gpu, info.GPUpath); |
432 | PARSEPATH(Config.Spu, info.SPUpath); |
433 | PARSEPATH(Config.Cdr, info.CDRpath); |
434 | |
435 | strcpy(Config.Mcd1, info.MCD1path); |
436 | strcpy(Config.Mcd2, info.MCD2path); |
437 | return -2; |
438 | } else { |
439 | Config.UseNet = FALSE; |
440 | } |
441 | } else { |
442 | if (NET_queryPlayer() == 1) { |
443 | if (SendPcsxInfo() == -1) Config.UseNet = FALSE; |
444 | } else { |
445 | if (RecvPcsxInfo() == -1) Config.UseNet = FALSE; |
446 | } |
447 | } |
448 | NetOpened = TRUE; |
449 | } else if (Config.UseNet) { |
450 | NET_resume(); |
451 | } |
452 | |
453 | return 0; |
454 | } |
455 | |
456 | int OpenPlugins() { |
457 | int ret; |
458 | |
459 | while ((ret = _OpenPlugins()) == -2) { |
460 | ReleasePlugins(); |
461 | LoadMcds(Config.Mcd1, Config.Mcd2); |
462 | if (LoadPlugins() == -1) return -1; |
463 | } |
464 | return ret; |
465 | } |
466 | |
467 | void ClosePlugins() { |
468 | int ret; |
469 | |
470 | signal(SIGINT, SIG_DFL); |
471 | signal(SIGPIPE, SIG_DFL); |
472 | ret = CDR_close(); |
473 | if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; } |
474 | ret = SPU_close(); |
475 | if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; } |
476 | ret = PAD1_close(); |
477 | if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; } |
478 | ret = PAD2_close(); |
479 | if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; } |
480 | // pcsx-rearmed: we handle gpu elsewhere |
481 | //ret = GPU_close(); |
482 | //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; } |
483 | |
484 | if (Config.UseNet) { |
485 | NET_pause(); |
486 | } |
487 | } |
488 | |
e906c010 |
489 | #if 1 |
490 | /* this is to avoid having to hack every plugin to stop using $HOME */ |
491 | char *getenv(const char *name) |
492 | { |
493 | static char ret[8] = "."; |
494 | |
69af03a2 |
495 | if (name && strcmp(name, "HOME") == 0 && |
496 | ((int)name >> 28) == 0) // HACK: let libs find home |
b60f2812 |
497 | return ret; |
e906c010 |
498 | |
b60f2812 |
499 | return real_getenv(name); |
e906c010 |
500 | } |
501 | #endif |
502 | |
503 | /* we hook statically linked plugins here */ |
504 | static const char *builtin_plugins[] = { |
47bf65ab |
505 | "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad", |
506 | "builtin_cdrcimg", |
e906c010 |
507 | }; |
508 | |
509 | static const int builtin_plugin_ids[] = { |
510 | PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD, |
47bf65ab |
511 | PLUGIN_CDRCIMG, |
e906c010 |
512 | }; |
513 | |
80c2304e |
514 | void *SysLoadLibrary(const char *lib) { |
e906c010 |
515 | const char *tmp = strrchr(lib, '/'); |
bbd837c6 |
516 | void *ret; |
e906c010 |
517 | int i; |
518 | |
bbd837c6 |
519 | printf("plugin: %s\n", lib); |
520 | |
e906c010 |
521 | if (tmp != NULL) { |
522 | tmp++; |
523 | for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++) |
524 | if (strcmp(tmp, builtin_plugins[i]) == 0) |
525 | return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]); |
526 | } |
527 | |
2185e39b |
528 | #if defined(__x86_64__) || defined(__i386__) |
529 | // convenience hack |
530 | char name[MAXPATHLEN]; |
531 | snprintf(name, sizeof(name), "%s.x86", lib); |
532 | lib = name; |
533 | #endif |
534 | |
bbd837c6 |
535 | ret = dlopen(lib, RTLD_NOW); |
536 | if (ret == NULL) |
537 | fprintf(stderr, "dlopen: %s\n", dlerror()); |
538 | return ret; |
80c2304e |
539 | } |
540 | |
541 | void *SysLoadSym(void *lib, const char *sym) { |
e906c010 |
542 | unsigned int plugid = (unsigned int)(long)lib; |
543 | |
544 | if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins)) |
545 | return plugin_link(plugid - PLUGIN_DL_BASE, sym); |
546 | |
80c2304e |
547 | return dlsym(lib, sym); |
548 | } |
549 | |
550 | const char *SysLibError() { |
551 | return dlerror(); |
552 | } |
553 | |
554 | void SysCloseLibrary(void *lib) { |
e906c010 |
555 | unsigned int plugid = (unsigned int)(long)lib; |
556 | |
557 | if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins)) |
558 | return; |
559 | |
80c2304e |
560 | dlclose(lib); |
561 | } |
562 | |