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