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