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