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> |
29a8c4f3 |
16 | #include <time.h> |
80c2304e |
17 | |
c5061935 |
18 | #include "main.h" |
e906c010 |
19 | #include "plugin.h" |
7c0f51de |
20 | #include "plugin_lib.h" |
14dffdb7 |
21 | #include "pcnt.h" |
3c70c47b |
22 | #include "menu.h" |
80c2304e |
23 | #include "../libpcsxcore/misc.h" |
799b0b87 |
24 | #include "../libpcsxcore/new_dynarec/new_dynarec.h" |
47bf65ab |
25 | #include "../plugins/cdrcimg/cdrcimg.h" |
69af03a2 |
26 | #include "common/plat.h" |
27 | #include "common/input.h" |
29a8c4f3 |
28 | #include "common/readpng.h" |
80c2304e |
29 | |
fc8145b7 |
30 | // don't include debug.h - it breaks ARM build (R1 redefined) |
31 | void StartDebugger(); |
32 | void StopDebugger(); |
33 | |
69af03a2 |
34 | int ready_to_go; |
c5061935 |
35 | unsigned long gpuDisp; |
36 | char cfgfile_basename[MAXPATHLEN]; |
8f892648 |
37 | int state_slot; |
38 | enum sched_action emu_action, emu_action_old; |
39 | char hud_msg[64]; |
40 | int hud_new_msg; |
41 | |
80c2304e |
42 | static void make_path(char *buf, size_t size, const char *dir, const char *fname) |
43 | { |
44 | if (fname) |
45 | snprintf(buf, size, ".%s%s", dir, fname); |
46 | else |
47 | snprintf(buf, size, ".%s", dir); |
48 | } |
49 | #define MAKE_PATH(buf, dir, fname) \ |
50 | make_path(buf, sizeof(buf), dir, fname) |
51 | |
52 | static void create_profile_dir(const char *directory) { |
53 | char path[MAXPATHLEN]; |
54 | |
55 | MAKE_PATH(path, directory, NULL); |
56 | mkdir(path, S_IRWXU | S_IRWXG); |
57 | } |
58 | |
59 | static void CheckSubDir() { |
60 | // make sure that ~/.pcsx exists |
61 | create_profile_dir(PCSX_DOT_DIR); |
62 | |
63 | create_profile_dir(BIOS_DIR); |
64 | create_profile_dir(MEMCARD_DIR); |
65 | create_profile_dir(STATES_DIR); |
66 | create_profile_dir(PLUGINS_DIR); |
67 | create_profile_dir(PLUGINS_CFG_DIR); |
68 | create_profile_dir(CHEATS_DIR); |
69 | create_profile_dir(PATCHES_DIR); |
cd6e8d0f |
70 | create_profile_dir(PCSX_DOT_DIR "cfg"); |
29a8c4f3 |
71 | create_profile_dir("/screenshots/"); |
72 | } |
73 | |
74 | static int get_gameid_filename(char *buf, int size, const char *fmt, int i) { |
75 | char trimlabel[33]; |
76 | int j; |
77 | |
78 | strncpy(trimlabel, CdromLabel, 32); |
79 | trimlabel[32] = 0; |
80 | for (j = 31; j >= 0; j--) |
81 | if (trimlabel[j] == ' ') |
82 | trimlabel[j] = 0; |
83 | else |
84 | continue; |
85 | |
86 | snprintf(buf, size, fmt, trimlabel, CdromId, i); |
87 | |
88 | return 0; |
80c2304e |
89 | } |
90 | |
47bf65ab |
91 | void set_cd_image(const char *fname) |
92 | { |
e16a7e51 |
93 | const char *ext = NULL; |
47bf65ab |
94 | |
33716956 |
95 | if (fname != NULL) |
96 | ext = strrchr(fname, '.'); |
47bf65ab |
97 | |
33716956 |
98 | if (ext && ( |
99 | strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 || |
9a92bffb |
100 | strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) { |
47bf65ab |
101 | SetIsoFile(NULL); |
102 | cdrcimg_set_fname(fname); |
103 | strcpy(Config.Cdr, "builtin_cdrcimg"); |
104 | } else { |
105 | SetIsoFile(fname); |
106 | strcpy(Config.Cdr, "builtin_cdr"); |
107 | } |
108 | } |
109 | |
6fe1f056 |
110 | static void set_default_paths(void) |
111 | { |
112 | MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd"); |
113 | MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd"); |
114 | strcpy(Config.BiosDir, "bios"); |
115 | |
116 | strcpy(Config.PluginsDir, "plugins"); |
117 | strcpy(Config.Gpu, "builtin_gpu"); |
118 | strcpy(Config.Spu, "builtin_spu"); |
119 | strcpy(Config.Cdr, "builtin_cdr"); |
120 | strcpy(Config.Pad1, "builtin_pad"); |
121 | strcpy(Config.Pad2, "builtin_pad"); |
122 | strcpy(Config.Net, "Disabled"); |
d3f3bf09 |
123 | #if defined(__arm__) && !defined(__ARM_ARCH_7A__) /* XXX */ |
124 | strcpy(Config.Gpu, "gpuPCSX4ALL.so"); |
125 | #endif |
6fe1f056 |
126 | Config.PsxAuto = 1; |
127 | |
128 | snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR); |
129 | } |
130 | |
51f77282 |
131 | static void check_memcards(void) |
132 | { |
133 | char buf[MAXPATHLEN]; |
134 | FILE *f; |
135 | int i; |
136 | |
137 | for (i = 1; i <= 9; i++) { |
138 | snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i); |
139 | |
140 | f = fopen(buf, "rb"); |
141 | if (f == NULL) { |
142 | printf("Creating memcard: %s\n", buf); |
143 | CreateMcd(buf); |
144 | } |
145 | else |
146 | fclose(f); |
147 | } |
148 | } |
149 | |
8f892648 |
150 | void do_emu_action(void) |
151 | { |
29a8c4f3 |
152 | char buf[MAXPATHLEN]; |
8f892648 |
153 | int ret; |
154 | |
155 | emu_action_old = emu_action; |
156 | |
157 | switch (emu_action) { |
8f892648 |
158 | case SACTION_ENTER_MENU: |
159 | menu_loop(); |
160 | return; |
161 | case SACTION_LOAD_STATE: |
162 | ret = emu_load_state(state_slot); |
163 | snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!"); |
164 | break; |
165 | case SACTION_SAVE_STATE: |
166 | ret = emu_save_state(state_slot); |
167 | snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!"); |
168 | break; |
169 | case SACTION_NEXT_SSLOT: |
170 | state_slot++; |
171 | if (state_slot > 9) |
172 | state_slot = 0; |
173 | goto do_state_slot; |
174 | case SACTION_PREV_SSLOT: |
175 | state_slot--; |
176 | if (state_slot < 0) |
177 | state_slot = 9; |
178 | goto do_state_slot; |
179 | case SACTION_TOGGLE_FSKIP: |
ea4a16e7 |
180 | pl_rearmed_cbs.fskip_advice = 0; |
181 | pl_rearmed_cbs.frameskip++; |
182 | if (pl_rearmed_cbs.frameskip > 1) |
183 | pl_rearmed_cbs.frameskip = -1; |
184 | snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s", |
185 | pl_rearmed_cbs.frameskip == -1 ? "AUTO" : |
186 | pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" ); |
187 | plugin_call_rearmed_cbs(); |
8f892648 |
188 | break; |
29a8c4f3 |
189 | case SACTION_SCREENSHOT: |
190 | { |
191 | void *scrbuf; |
192 | int w, h, bpp; |
193 | time_t t = time(NULL); |
194 | struct tm *tb = localtime(&t); |
195 | int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 + |
196 | tb->tm_min * 100 + tb->tm_sec; |
197 | |
198 | scrbuf = pl_prepare_screenshot(&w, &h, &bpp); |
199 | get_gameid_filename(buf, sizeof(buf), |
200 | "screenshots/%.32s-%.9s.%d.png", ti); |
201 | ret = -1; |
202 | if (scrbuf != 0 && bpp == 16) |
203 | ret = writepng(buf, scrbuf, w, h); |
204 | if (ret == 0) |
205 | snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN"); |
206 | break; |
207 | } |
4c08b9e7 |
208 | default: |
209 | return; |
8f892648 |
210 | } |
211 | hud_new_msg = 3; |
212 | return; |
213 | |
214 | do_state_slot: |
215 | snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot, |
216 | emu_check_state(state_slot) == 0 ? "USED" : "FREE"); |
217 | hud_new_msg = 3; |
4c08b9e7 |
218 | printf("* %s\n", hud_msg); |
8f892648 |
219 | } |
220 | |
80c2304e |
221 | int main(int argc, char *argv[]) |
222 | { |
80c2304e |
223 | // what is the name of the config file? |
224 | // it may be redefined by -cfg on the command line |
225 | strcpy(cfgfile_basename, "pcsx.cfg"); |
226 | |
e906c010 |
227 | emuLog = stdout; |
80c2304e |
228 | SetIsoFile(NULL); |
80c2304e |
229 | |
e0c692d9 |
230 | memset(&Config, 0, sizeof(Config)); |
231 | |
232 | CheckSubDir(); |
233 | set_default_paths(); |
51f77282 |
234 | check_memcards(); |
e0c692d9 |
235 | strcpy(Config.Bios, "HLE"); |
236 | |
237 | #ifdef MAEMO |
238 | extern int maemo_main(int argc, char **argv); |
239 | return maemo_main(argc, argv); |
240 | #else |
241 | char file[MAXPATHLEN] = ""; |
242 | char path[MAXPATHLEN]; |
243 | const char *cdfile = NULL; |
de910c6b |
244 | const char *loadst_f = NULL; |
0a151868 |
245 | int psxout = 0; |
e0c692d9 |
246 | int loadst = 0; |
247 | int i; |
248 | |
80c2304e |
249 | // read command line options |
250 | for (i = 1; i < argc; i++) { |
0a151868 |
251 | if (!strcmp(argv[i], "-psxout")) psxout = 1; |
80c2304e |
252 | else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]); |
253 | else if (!strcmp(argv[i], "-cfg")) { |
254 | if (i+1 >= argc) break; |
255 | strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */ |
256 | printf("Using config file %s.\n", cfgfile_basename); |
257 | } |
258 | else if (!strcmp(argv[i], "-cdfile")) { |
259 | char isofilename[MAXPATHLEN]; |
260 | |
261 | if (i+1 >= argc) break; |
262 | strncpy(isofilename, argv[++i], MAXPATHLEN); |
263 | if (isofilename[0] != '/') { |
264 | getcwd(path, MAXPATHLEN); |
265 | if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) { |
266 | strcat(path, "/"); |
267 | strcat(path, isofilename); |
268 | strcpy(isofilename, path); |
269 | } else |
270 | isofilename[0] = 0; |
271 | } |
272 | |
47bf65ab |
273 | cdfile = isofilename; |
80c2304e |
274 | } |
de910c6b |
275 | else if (!strcmp(argv[i], "-loadf")) { |
276 | if (i+1 >= argc) break; |
277 | loadst_f = argv[++i]; |
278 | } |
80c2304e |
279 | else if (!strcmp(argv[i], "-h") || |
280 | !strcmp(argv[i], "-help") || |
281 | !strcmp(argv[i], "--help")) { |
282 | printf(PACKAGE_NAME " " PACKAGE_VERSION "\n"); |
283 | printf("%s\n", _( |
284 | " pcsx [options] [file]\n" |
285 | "\toptions:\n" |
286 | "\t-cdfile FILE\tRuns a CD image file\n" |
287 | "\t-nogui\t\tDon't open the GTK GUI\n" |
288 | "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n" |
289 | "\t-psxout\t\tEnable PSX output\n" |
290 | "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n" |
291 | "\t-h -help\tDisplay this message\n" |
292 | "\tfile\t\tLoads file\n")); |
293 | return 0; |
294 | } else { |
295 | strncpy(file, argv[i], MAXPATHLEN); |
296 | if (file[0] != '/') { |
297 | getcwd(path, MAXPATHLEN); |
298 | if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) { |
299 | strcat(path, "/"); |
300 | strcat(path, file); |
301 | strcpy(file, path); |
302 | } else |
303 | file[0] = 0; |
304 | } |
305 | } |
306 | } |
307 | |
47bf65ab |
308 | if (cdfile) |
309 | set_cd_image(cdfile); |
310 | |
69af03a2 |
311 | if (SysInit() == -1) |
312 | return 1; |
80c2304e |
313 | |
69af03a2 |
314 | // frontend stuff |
315 | in_init(); |
ef94866c |
316 | //in_probe(); |
69af03a2 |
317 | plat_init(); |
0a151868 |
318 | menu_init(); // loads config |
4c08b9e7 |
319 | pl_init(); |
0a151868 |
320 | |
321 | if (psxout) |
322 | Config.PsxOut = 1; |
80c2304e |
323 | |
69af03a2 |
324 | if (LoadPlugins() == -1) { |
6fe1f056 |
325 | // FIXME: this recovery doesn't work, just delete bad config and bail out |
326 | // SysMessage("could not load plugins, retrying with defaults\n"); |
327 | set_default_paths(); |
328 | snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename); |
329 | remove(path); |
69af03a2 |
330 | SysMessage("Failed loading plugins!"); |
331 | return 1; |
332 | } |
333 | pcnt_hook_plugins(); |
80c2304e |
334 | |
69af03a2 |
335 | if (OpenPlugins() == -1) { |
336 | return 1; |
337 | } |
201c21e2 |
338 | plugin_call_rearmed_cbs(); |
80c2304e |
339 | |
69af03a2 |
340 | CheckCdrom(); |
bbd837c6 |
341 | SysReset(); |
69af03a2 |
342 | |
343 | if (file[0] != '\0') { |
344 | if (Load(file) != -1) |
345 | ready_to_go = 1; |
346 | } else { |
47bf65ab |
347 | if (cdfile) { |
69af03a2 |
348 | if (LoadCdrom() == -1) { |
349 | ClosePlugins(); |
350 | printf(_("Could not load CD-ROM!\n")); |
351 | return -1; |
80c2304e |
352 | } |
69af03a2 |
353 | ready_to_go = 1; |
80c2304e |
354 | } |
69af03a2 |
355 | } |
80c2304e |
356 | |
45d45c1e |
357 | if (ready_to_go) { |
3c70c47b |
358 | menu_prepare_emu(); |
45d45c1e |
359 | |
360 | // If a state has been specified, then load that |
361 | if (loadst) { |
362 | int ret = emu_load_state(loadst - 1); |
363 | printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst); |
364 | } |
de910c6b |
365 | if (loadst_f) { |
366 | int ret = LoadState(loadst_f); |
367 | printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f); |
368 | } |
45d45c1e |
369 | } |
3c70c47b |
370 | else |
69af03a2 |
371 | menu_loop(); |
372 | |
7c0f51de |
373 | pl_start_watchdog(); |
374 | |
69af03a2 |
375 | while (1) |
376 | { |
799b0b87 |
377 | stop = 0; |
8f892648 |
378 | emu_action = SACTION_NONE; |
379 | |
80c2304e |
380 | psxCpu->Execute(); |
8f892648 |
381 | if (emu_action != SACTION_NONE) |
382 | do_emu_action(); |
80c2304e |
383 | } |
384 | |
385 | return 0; |
e0c692d9 |
386 | #endif |
80c2304e |
387 | } |
388 | |
389 | int SysInit() { |
80c2304e |
390 | if (EmuInit() == -1) { |
391 | printf("PSX emulator couldn't be initialized.\n"); |
392 | return -1; |
393 | } |
394 | |
51f77282 |
395 | LoadMcds(Config.Mcd1, Config.Mcd2); |
80c2304e |
396 | |
397 | if (Config.Debug) { |
398 | StartDebugger(); |
399 | } |
400 | |
401 | return 0; |
402 | } |
403 | |
404 | void SysRunGui() { |
405 | printf("SysRunGui\n"); |
406 | } |
407 | |
408 | void StartGui() { |
409 | printf("StartGui\n"); |
410 | } |
411 | |
e6eb2066 |
412 | static void dummy_lace() |
413 | { |
414 | } |
415 | |
80c2304e |
416 | void SysReset() { |
e6eb2066 |
417 | // rearmed hack: EmuReset() runs some code when real BIOS is used, |
418 | // but we usually do reset from menu while GPU is not open yet, |
419 | // so we need to prevent updateLace() call.. |
420 | void *real_lace = GPU_updateLace; |
421 | GPU_updateLace = dummy_lace; |
422 | |
80c2304e |
423 | EmuReset(); |
bd6267e6 |
424 | |
425 | // hmh core forgets this |
426 | CDR_stop(); |
e6eb2066 |
427 | |
428 | GPU_updateLace = real_lace; |
80c2304e |
429 | } |
430 | |
431 | void SysClose() { |
432 | EmuShutdown(); |
433 | ReleasePlugins(); |
434 | |
435 | StopDebugger(); |
436 | |
437 | if (emuLog != NULL) fclose(emuLog); |
438 | } |
439 | |
440 | void SysUpdate() { |
80c2304e |
441 | } |
442 | |
443 | void OnFile_Exit() { |
bd6267e6 |
444 | printf("OnFile_Exit\n"); |
e0c692d9 |
445 | #ifndef MAEMO |
201c21e2 |
446 | menu_finish(); |
e0c692d9 |
447 | #endif |
bd6267e6 |
448 | SysClose(); |
2c886904 |
449 | plat_finish(); |
80c2304e |
450 | exit(0); |
451 | } |
452 | |
c5061935 |
453 | int get_state_filename(char *buf, int size, int i) { |
29a8c4f3 |
454 | return get_gameid_filename(buf, size, |
455 | "." STATES_DIR "%.32s-%.9s.%3.3d", i); |
80c2304e |
456 | } |
457 | |
8f892648 |
458 | int emu_check_state(int slot) |
459 | { |
460 | char fname[MAXPATHLEN]; |
461 | int ret; |
462 | |
463 | ret = get_state_filename(fname, sizeof(fname), slot); |
464 | if (ret != 0) |
465 | return ret; |
466 | |
467 | return CheckState(fname); |
468 | } |
469 | |
470 | int emu_save_state(int slot) |
471 | { |
472 | char fname[MAXPATHLEN]; |
473 | int ret; |
474 | |
475 | ret = get_state_filename(fname, sizeof(fname), slot); |
476 | if (ret != 0) |
477 | return ret; |
478 | |
90f1d26c |
479 | ret = SaveState(fname); |
480 | printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot); |
481 | return ret; |
8f892648 |
482 | } |
483 | |
484 | int emu_load_state(int slot) |
485 | { |
486 | char fname[MAXPATHLEN]; |
487 | int ret; |
488 | |
489 | ret = get_state_filename(fname, sizeof(fname), slot); |
490 | if (ret != 0) |
491 | return ret; |
492 | |
493 | return LoadState(fname); |
494 | } |
495 | |
80c2304e |
496 | void SysPrintf(const char *fmt, ...) { |
497 | va_list list; |
498 | char msg[512]; |
499 | |
500 | va_start(list, fmt); |
501 | vsprintf(msg, fmt, list); |
502 | va_end(list); |
503 | |
80c2304e |
504 | fprintf(emuLog, "%s", msg); |
505 | } |
506 | |
507 | void SysMessage(const char *fmt, ...) { |
508 | va_list list; |
509 | char msg[512]; |
510 | |
511 | va_start(list, fmt); |
512 | vsprintf(msg, fmt, list); |
513 | va_end(list); |
514 | |
515 | if (msg[strlen(msg) - 1] == '\n') |
516 | msg[strlen(msg) - 1] = 0; |
517 | |
518 | fprintf(stderr, "%s\n", msg); |
519 | } |
520 | |
c5061935 |
521 | static void SignalExit(int sig) { |
522 | ClosePlugins(); |
523 | OnFile_Exit(); |
524 | } |
525 | |
526 | #define PARSEPATH(dst, src) \ |
527 | ptr = src + strlen(src); \ |
528 | while (*ptr != '\\' && ptr != src) ptr--; \ |
529 | if (ptr != src) { \ |
530 | strcpy(dst, ptr+1); \ |
531 | } |
532 | |
533 | static int _OpenPlugins(void) { |
534 | int ret; |
535 | |
536 | signal(SIGINT, SignalExit); |
537 | signal(SIGPIPE, SignalExit); |
538 | |
539 | GPU_clearDynarec(clearDynarec); |
540 | |
541 | ret = CDR_open(); |
542 | if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; } |
543 | ret = SPU_open(); |
544 | if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; } |
545 | SPU_registerCallback(SPUirq); |
546 | // pcsx-rearmed: we handle gpu elsewhere |
547 | //ret = GPU_open(&gpuDisp, "PCSX", NULL); |
548 | //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; } |
549 | ret = PAD1_open(&gpuDisp); |
550 | if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; } |
551 | ret = PAD2_open(&gpuDisp); |
552 | if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; } |
553 | |
554 | if (Config.UseNet && !NetOpened) { |
555 | netInfo info; |
556 | char path[MAXPATHLEN]; |
557 | char dotdir[MAXPATHLEN]; |
558 | |
559 | MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL); |
560 | |
561 | strcpy(info.EmuName, "PCSX " PACKAGE_VERSION); |
562 | strncpy(info.CdromID, CdromId, 9); |
563 | strncpy(info.CdromLabel, CdromLabel, 9); |
564 | info.psxMem = psxM; |
565 | info.GPU_showScreenPic = GPU_showScreenPic; |
566 | info.GPU_displayText = GPU_displayText; |
567 | info.GPU_showScreenPic = GPU_showScreenPic; |
568 | info.PAD_setSensitive = PAD1_setSensitive; |
569 | sprintf(path, "%s%s", Config.BiosDir, Config.Bios); |
570 | strcpy(info.BIOSpath, path); |
571 | strcpy(info.MCD1path, Config.Mcd1); |
572 | strcpy(info.MCD2path, Config.Mcd2); |
573 | sprintf(path, "%s%s", dotdir, Config.Gpu); |
574 | strcpy(info.GPUpath, path); |
575 | sprintf(path, "%s%s", dotdir, Config.Spu); |
576 | strcpy(info.SPUpath, path); |
577 | sprintf(path, "%s%s", dotdir, Config.Cdr); |
578 | strcpy(info.CDRpath, path); |
579 | NET_setInfo(&info); |
580 | |
581 | ret = NET_open(&gpuDisp); |
582 | if (ret < 0) { |
583 | if (ret == -2) { |
584 | // -2 is returned when something in the info |
585 | // changed and needs to be synced |
586 | char *ptr; |
587 | |
588 | PARSEPATH(Config.Bios, info.BIOSpath); |
589 | PARSEPATH(Config.Gpu, info.GPUpath); |
590 | PARSEPATH(Config.Spu, info.SPUpath); |
591 | PARSEPATH(Config.Cdr, info.CDRpath); |
592 | |
593 | strcpy(Config.Mcd1, info.MCD1path); |
594 | strcpy(Config.Mcd2, info.MCD2path); |
595 | return -2; |
596 | } else { |
597 | Config.UseNet = FALSE; |
598 | } |
599 | } else { |
600 | if (NET_queryPlayer() == 1) { |
601 | if (SendPcsxInfo() == -1) Config.UseNet = FALSE; |
602 | } else { |
603 | if (RecvPcsxInfo() == -1) Config.UseNet = FALSE; |
604 | } |
605 | } |
606 | NetOpened = TRUE; |
607 | } else if (Config.UseNet) { |
608 | NET_resume(); |
609 | } |
610 | |
611 | return 0; |
612 | } |
613 | |
614 | int OpenPlugins() { |
615 | int ret; |
616 | |
617 | while ((ret = _OpenPlugins()) == -2) { |
618 | ReleasePlugins(); |
619 | LoadMcds(Config.Mcd1, Config.Mcd2); |
620 | if (LoadPlugins() == -1) return -1; |
621 | } |
622 | return ret; |
623 | } |
624 | |
625 | void ClosePlugins() { |
626 | int ret; |
627 | |
628 | signal(SIGINT, SIG_DFL); |
629 | signal(SIGPIPE, SIG_DFL); |
630 | ret = CDR_close(); |
631 | if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; } |
632 | ret = SPU_close(); |
633 | if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; } |
634 | ret = PAD1_close(); |
635 | if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; } |
636 | ret = PAD2_close(); |
637 | if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; } |
638 | // pcsx-rearmed: we handle gpu elsewhere |
639 | //ret = GPU_close(); |
640 | //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; } |
641 | |
642 | if (Config.UseNet) { |
643 | NET_pause(); |
644 | } |
645 | } |
646 | |
e906c010 |
647 | /* we hook statically linked plugins here */ |
648 | static const char *builtin_plugins[] = { |
47bf65ab |
649 | "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad", |
650 | "builtin_cdrcimg", |
e906c010 |
651 | }; |
652 | |
653 | static const int builtin_plugin_ids[] = { |
654 | PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD, |
47bf65ab |
655 | PLUGIN_CDRCIMG, |
e906c010 |
656 | }; |
657 | |
80c2304e |
658 | void *SysLoadLibrary(const char *lib) { |
e906c010 |
659 | const char *tmp = strrchr(lib, '/'); |
bbd837c6 |
660 | void *ret; |
e906c010 |
661 | int i; |
662 | |
bbd837c6 |
663 | printf("plugin: %s\n", lib); |
664 | |
e906c010 |
665 | if (tmp != NULL) { |
666 | tmp++; |
667 | for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++) |
668 | if (strcmp(tmp, builtin_plugins[i]) == 0) |
669 | return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]); |
670 | } |
671 | |
2185e39b |
672 | #if defined(__x86_64__) || defined(__i386__) |
673 | // convenience hack |
674 | char name[MAXPATHLEN]; |
675 | snprintf(name, sizeof(name), "%s.x86", lib); |
676 | lib = name; |
677 | #endif |
678 | |
bbd837c6 |
679 | ret = dlopen(lib, RTLD_NOW); |
680 | if (ret == NULL) |
681 | fprintf(stderr, "dlopen: %s\n", dlerror()); |
682 | return ret; |
80c2304e |
683 | } |
684 | |
685 | void *SysLoadSym(void *lib, const char *sym) { |
e906c010 |
686 | unsigned int plugid = (unsigned int)(long)lib; |
687 | |
688 | if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins)) |
689 | return plugin_link(plugid - PLUGIN_DL_BASE, sym); |
690 | |
80c2304e |
691 | return dlsym(lib, sym); |
692 | } |
693 | |
694 | const char *SysLibError() { |
695 | return dlerror(); |
696 | } |
697 | |
698 | void SysCloseLibrary(void *lib) { |
e906c010 |
699 | unsigned int plugid = (unsigned int)(long)lib; |
700 | |
701 | if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins)) |
702 | return; |
703 | |
80c2304e |
704 | dlclose(lib); |
705 | } |
706 | |