update inputs on vsync
[pcsx_rearmed.git] / frontend / main.c
CommitLineData
e906c010 1/*
2 * (C) notaz, 2010
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>
15
e906c010 16#include "plugin.h"
14dffdb7 17#include "pcnt.h"
3c70c47b 18#include "menu.h"
80c2304e 19#include "../gui/Linux.h"
20#include "../libpcsxcore/misc.h"
47bf65ab 21#include "../plugins/cdrcimg/cdrcimg.h"
69af03a2 22#include "common/plat.h"
23#include "common/input.h"
80c2304e 24
69af03a2 25int ready_to_go;
80c2304e 26int UseGui;
b60f2812 27static char *(*real_getenv)(const char *name);
80c2304e 28
29static void make_path(char *buf, size_t size, const char *dir, const char *fname)
30{
31 if (fname)
32 snprintf(buf, size, ".%s%s", dir, fname);
33 else
34 snprintf(buf, size, ".%s", dir);
35}
36#define MAKE_PATH(buf, dir, fname) \
37 make_path(buf, sizeof(buf), dir, fname)
38
39static void create_profile_dir(const char *directory) {
40 char path[MAXPATHLEN];
41
42 MAKE_PATH(path, directory, NULL);
43 mkdir(path, S_IRWXU | S_IRWXG);
44}
45
46static void CheckSubDir() {
47 // make sure that ~/.pcsx exists
48 create_profile_dir(PCSX_DOT_DIR);
49
50 create_profile_dir(BIOS_DIR);
51 create_profile_dir(MEMCARD_DIR);
52 create_profile_dir(STATES_DIR);
53 create_profile_dir(PLUGINS_DIR);
54 create_profile_dir(PLUGINS_CFG_DIR);
55 create_profile_dir(CHEATS_DIR);
56 create_profile_dir(PATCHES_DIR);
57}
58
59static void CreateMemcard(char *filename, char *conf_mcd) {
60 struct stat buf;
61
62 make_path(conf_mcd, MAXPATHLEN, MEMCARD_DIR, filename);
63
64 /* Only create a memory card if an existing one does not exist */
65 if (stat(conf_mcd, &buf) == -1) {
66 SysPrintf(_("Creating memory card: %s\n"), conf_mcd);
67 CreateMcd(conf_mcd);
68 }
69}
70
47bf65ab 71void set_cd_image(const char *fname)
72{
73 const char *ext;
74 int len;
75
76 len = strlen(fname);
77 ext = fname;
78 if (len > 2)
79 ext = fname + len - 2;
80
81 if (strcasecmp(ext, ".z") == 0) {
82 SetIsoFile(NULL);
83 cdrcimg_set_fname(fname);
84 strcpy(Config.Cdr, "builtin_cdrcimg");
85 } else {
86 SetIsoFile(fname);
87 strcpy(Config.Cdr, "builtin_cdr");
88 }
89}
90
80c2304e 91int main(int argc, char *argv[])
92{
93 char file[MAXPATHLEN] = "";
94 char path[MAXPATHLEN];
47bf65ab 95 const char *cdfile = NULL;
80c2304e 96 int loadst = 0;
b60f2812 97 void *tmp;
80c2304e 98 int i;
99
b60f2812 100 tmp = dlopen("/lib/libdl.so.2", RTLD_LAZY);
fa9cfe0a 101 if (tmp == NULL)
102 tmp = dlopen("/lib32/libdl.so.2", RTLD_LAZY);
b60f2812 103 if (tmp != NULL)
104 real_getenv = dlsym(tmp, "getenv");
105 if (real_getenv == NULL) {
106 fprintf(stderr, "%s\n", dlerror());
107 return 1;
108 }
109 dlclose(tmp);
110
80c2304e 111 // what is the name of the config file?
112 // it may be redefined by -cfg on the command line
113 strcpy(cfgfile_basename, "pcsx.cfg");
114
e906c010 115 emuLog = stdout;
80c2304e 116 SetIsoFile(NULL);
80c2304e 117
118 // read command line options
119 for (i = 1; i < argc; i++) {
e906c010 120 if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
80c2304e 121 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
122 else if (!strcmp(argv[i], "-cfg")) {
123 if (i+1 >= argc) break;
124 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
125 printf("Using config file %s.\n", cfgfile_basename);
126 }
127 else if (!strcmp(argv[i], "-cdfile")) {
128 char isofilename[MAXPATHLEN];
129
130 if (i+1 >= argc) break;
131 strncpy(isofilename, argv[++i], MAXPATHLEN);
132 if (isofilename[0] != '/') {
133 getcwd(path, MAXPATHLEN);
134 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
135 strcat(path, "/");
136 strcat(path, isofilename);
137 strcpy(isofilename, path);
138 } else
139 isofilename[0] = 0;
140 }
141
47bf65ab 142 cdfile = isofilename;
80c2304e 143 }
144 else if (!strcmp(argv[i], "-h") ||
145 !strcmp(argv[i], "-help") ||
146 !strcmp(argv[i], "--help")) {
147 printf(PACKAGE_NAME " " PACKAGE_VERSION "\n");
148 printf("%s\n", _(
149 " pcsx [options] [file]\n"
150 "\toptions:\n"
151 "\t-cdfile FILE\tRuns a CD image file\n"
152 "\t-nogui\t\tDon't open the GTK GUI\n"
153 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
154 "\t-psxout\t\tEnable PSX output\n"
155 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
156 "\t-h -help\tDisplay this message\n"
157 "\tfile\t\tLoads file\n"));
158 return 0;
159 } else {
160 strncpy(file, argv[i], MAXPATHLEN);
161 if (file[0] != '/') {
162 getcwd(path, MAXPATHLEN);
163 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
164 strcat(path, "/");
165 strcat(path, file);
166 strcpy(file, path);
167 } else
168 file[0] = 0;
169 }
170 }
171 }
172
173 memset(&Config, 0, sizeof(PcsxConfig));
174 strcpy(Config.Net, "Disabled");
175
176 CheckSubDir();
177// ScanAllPlugins();
178
e906c010 179 strcpy(Config.Bios, "HLE");
180 strcpy(Config.BiosDir, "./");
181
182 strcpy(Config.PluginsDir, "plugins");
183 strcpy(Config.Gpu, "builtin_gpu");
184 strcpy(Config.Spu, "builtin_spu");
185 strcpy(Config.Cdr, "builtin_cdr");
186 strcpy(Config.Pad1, "builtin_pad");
187 strcpy(Config.Pad2, "builtin_pad");
188
80c2304e 189 // try to load config
190 // if the config file doesn't exist
191 if (LoadConfig() == -1) {
192 // Uh oh, no config file found, use some defaults
193 Config.PsxAuto = 1;
194
80c2304e 195 // create & load default memcards if they don't exist
196 CreateMemcard("card1.mcd", Config.Mcd1);
197 CreateMemcard("card2.mcd", Config.Mcd2);
198
199 LoadMcds(Config.Mcd1, Config.Mcd2);
200
201 SaveConfig();
202 }
203
204 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
205/*
206 // switch to plugin dotdir
207 // this lets plugins work without modification!
208 gchar *plugin_default_dir = g_build_filename(getenv("HOME"), PLUGINS_DIR, NULL);
209 chdir(plugin_default_dir);
210 g_free(plugin_default_dir);
211*/
47bf65ab 212
213 if (cdfile)
214 set_cd_image(cdfile);
215
69af03a2 216 if (SysInit() == -1)
217 return 1;
80c2304e 218
69af03a2 219 // frontend stuff
220 in_init();
221 in_probe();
222 plat_init();
223 menu_init();
80c2304e 224
69af03a2 225 if (LoadPlugins() == -1) {
226 SysMessage("Failed loading plugins!");
227 return 1;
228 }
229 pcnt_hook_plugins();
80c2304e 230
69af03a2 231 if (OpenPlugins() == -1) {
232 return 1;
233 }
80c2304e 234
69af03a2 235 SysReset();
236 CheckCdrom();
237
238 if (file[0] != '\0') {
239 if (Load(file) != -1)
240 ready_to_go = 1;
241 } else {
47bf65ab 242 if (cdfile) {
69af03a2 243 if (LoadCdrom() == -1) {
244 ClosePlugins();
245 printf(_("Could not load CD-ROM!\n"));
246 return -1;
80c2304e 247 }
69af03a2 248 ready_to_go = 1;
80c2304e 249 }
69af03a2 250 }
80c2304e 251
69af03a2 252 // If a state has been specified, then load that
253 if (loadst) {
254 StatesC = loadst - 1;
255 char *state_filename = get_state_filename(StatesC);
256 int ret = LoadState(state_filename);
257 printf("%s state %s\n", ret ? "failed to load" : "loaded", state_filename);
258 free(state_filename);
259 }
80c2304e 260
3c70c47b 261 if (ready_to_go)
262 menu_prepare_emu();
263 else
69af03a2 264 menu_loop();
265
266 while (1)
267 {
80c2304e 268 psxCpu->Execute();
69af03a2 269 menu_loop();
80c2304e 270 }
271
272 return 0;
273}
274
275int SysInit() {
80c2304e 276 if (EmuInit() == -1) {
277 printf("PSX emulator couldn't be initialized.\n");
278 return -1;
279 }
280
281 LoadMcds(Config.Mcd1, Config.Mcd2); /* TODO Do we need to have this here, or in the calling main() function?? */
282
283 if (Config.Debug) {
284 StartDebugger();
285 }
286
287 return 0;
288}
289
290void SysRunGui() {
291 printf("SysRunGui\n");
292}
293
294void StartGui() {
295 printf("StartGui\n");
296}
297
298void SysReset() {
299 EmuReset();
300}
301
302void SysClose() {
303 EmuShutdown();
304 ReleasePlugins();
305
306 StopDebugger();
307
308 if (emuLog != NULL) fclose(emuLog);
309}
310
311void SysUpdate() {
312 PADhandleKey(PAD1_keypressed());
313 PADhandleKey(PAD2_keypressed());
314}
315
316void UpdateMenuSlots() {
317}
318
319void OnFile_Exit() {
320 printf("OnFile_Exit\n");
321 exit(0);
322}
323
324void state_save(gchar *state_filename) {
325 char Text[MAXPATHLEN + 20];
326
327 GPU_updateLace();
328
329 if (SaveState(state_filename) == 0)
330 sprintf(Text, _("Saved state %s."), state_filename);
331 else
332 sprintf(Text, _("Error saving state %s!"), state_filename);
333
334 GPU_displayText(Text);
335}
336
337void state_load(gchar *state_filename) {
338 int ret;
339 char Text[MAXPATHLEN + 20];
340 FILE *fp;
341
342 // check if the state file actually exists
343 fp = fopen(state_filename, "rb");
344 if (fp == NULL) {
345 // file does not exist
346 return;
347 }
348
349 fclose(fp);
350
351 ret = CheckState(state_filename);
352
353 if (ret == 0) {
354 SysReset();
355 ret = LoadState(state_filename);
356 }
357
358 if (ret == 0) {
359 // Check the CD-ROM is valid
360 if (CheckCdrom() == -1) {
361 ClosePlugins();
362 SysRunGui();
363 return;
364 }
365
366 sprintf(Text, _("Loaded state %s."), state_filename);
367 } else {
368 sprintf(Text, _("Error loading state %s!"), state_filename);
369 }
370 GPU_displayText(Text);
371}
372
373char *get_state_filename(int i) {
374 char SStateFile[256];
375 char trimlabel[33];
376 int j;
377
378 strncpy(trimlabel, CdromLabel, 32);
379 trimlabel[32] = 0;
380 for (j = 31; j >= 0; j--)
381 if (trimlabel[j] == ' ')
382 trimlabel[j] = 0;
383 else
384 continue;
385
386 snprintf(SStateFile, sizeof(SStateFile), "." STATES_DIR "%.32s-%.9s.%3.3d",
387 trimlabel, CdromId, i);
388
389 return strdup(SStateFile);
390}
391
392void SysPrintf(const char *fmt, ...) {
393 va_list list;
394 char msg[512];
395
396 va_start(list, fmt);
397 vsprintf(msg, fmt, list);
398 va_end(list);
399
80c2304e 400 fprintf(emuLog, "%s", msg);
401}
402
403void SysMessage(const char *fmt, ...) {
404 va_list list;
405 char msg[512];
406
407 va_start(list, fmt);
408 vsprintf(msg, fmt, list);
409 va_end(list);
410
411 if (msg[strlen(msg) - 1] == '\n')
412 msg[strlen(msg) - 1] = 0;
413
414 fprintf(stderr, "%s\n", msg);
415}
416
e906c010 417#if 1
418/* this is to avoid having to hack every plugin to stop using $HOME */
419char *getenv(const char *name)
420{
421 static char ret[8] = ".";
422
69af03a2 423 if (name && strcmp(name, "HOME") == 0 &&
424 ((int)name >> 28) == 0) // HACK: let libs find home
b60f2812 425 return ret;
e906c010 426
b60f2812 427 return real_getenv(name);
e906c010 428}
429#endif
430
431/* we hook statically linked plugins here */
432static const char *builtin_plugins[] = {
47bf65ab 433 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
434 "builtin_cdrcimg",
e906c010 435};
436
437static const int builtin_plugin_ids[] = {
438 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
47bf65ab 439 PLUGIN_CDRCIMG,
e906c010 440};
441
80c2304e 442void *SysLoadLibrary(const char *lib) {
e906c010 443 const char *tmp = strrchr(lib, '/');
444 int i;
445
446 printf("dlopen %s\n", lib);
447 if (tmp != NULL) {
448 tmp++;
449 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
450 if (strcmp(tmp, builtin_plugins[i]) == 0)
451 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
452 }
453
80c2304e 454 return dlopen(lib, RTLD_NOW);
455}
456
457void *SysLoadSym(void *lib, const char *sym) {
e906c010 458 unsigned int plugid = (unsigned int)(long)lib;
459
460 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
461 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
462
80c2304e 463 return dlsym(lib, sym);
464}
465
466const char *SysLibError() {
467 return dlerror();
468}
469
470void SysCloseLibrary(void *lib) {
e906c010 471 unsigned int plugid = (unsigned int)(long)lib;
472
473 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
474 return;
475
80c2304e 476 dlclose(lib);
477}
478
479