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