977f6a30ff0178133900721a0fcbbfb27dbe9519
[pcsx_rearmed.git] / maemo / main.c
1 /*
2  * (C) notaz, 2010-2011
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 <stdint.h>
11 #include <unistd.h>
12
13 #include "main.h"
14 #include "menu.h"
15 #include "plugin.h"
16 #include "plugin_lib.h"
17 #include "../libpcsxcore/misc.h"
18 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
19 #include "maemo_common.h"
20
21 // sound plugin
22 extern int iUseTimer;
23
24 int g_opts = OPT_SHOWFPS;
25 int g_maemo_opts;
26 char file_name[MAXPATHLEN];
27
28 enum sched_action emu_action;
29 void do_emu_action(void);
30
31 static void ChangeWorkingDirectory(char *exe)
32 {
33         char exepath[1024];
34         char *s;
35         snprintf(exepath, sizeof(exepath), "%s", exe);
36         s = strrchr(exepath, '/');
37         if (s != NULL) {
38                 *s = '\0';
39                 chdir(exepath);
40         }
41 }
42
43 int maemo_main(int argc, char **argv)
44 {
45         ChangeWorkingDirectory("c");
46         char file[MAXPATHLEN] = "";
47         char path[MAXPATHLEN];
48         const char *cdfile = NULL;
49         int loadst = 0;
50         int i;
51
52         strcpy(Config.BiosDir, "/home/user/MyDocs");
53         strcpy(Config.PluginsDir, "/opt/maemo/usr/games/plugins");
54         snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "/opt/maemo/usr/games" PATCHES_DIR);
55         Config.PsxAuto = 1;
56         
57         // read command line options
58         for (i = 1; i < argc; i++) {
59                      if (!strcmp(argv[i], "-psxout")) Config.PsxOut = 1;
60                 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
61                 else if (!strcmp(argv[i], "-cdfile")) {
62                         char isofilename[MAXPATHLEN];
63                         if (i+1 >= argc) break;
64                         strncpy(isofilename, argv[++i], MAXPATHLEN);
65                         if (isofilename[0] != '/') {
66                                 getcwd(path, MAXPATHLEN);
67                                 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
68                                         strcat(path, "/");
69                                         strcat(path, isofilename);
70                                         strcpy(isofilename, path);
71                                 } else
72                                         isofilename[0] = 0;
73                         }
74                         cdfile = isofilename;
75                 }
76                 else if (!strcmp(argv[i],"-frameskip")) {
77                         int tv_reg = atol(argv[++i]);
78                         if (tv_reg > 0)
79                                 pl_rearmed_cbs.frameskip = -1;
80                 }
81                 else if (!strcmp(argv[i],"-fullscreen"))                g_maemo_opts |= 2;
82                 else if (!strcmp(argv[i],"-accel"))                             g_maemo_opts |= 4;
83                 else if (!strcmp(argv[i],"-sputhreaded"))               iUseTimer=1;
84                 else if (!strcmp(argv[i],"-nosound"))           strcpy(Config.Spu, "spunull.so");
85                 /* unworking with r10
86                 else if(!strcmp(argv[i], "-bdir"))                      sprintf(Config.BiosDir, "%s", argv[++i]);
87                 else if(!strcmp(argv[i], "-bios"))                      sprintf(Config.Bios, "%s", argv[++i]);
88                 else if (!strcmp(argv[i],"-gles"))                      strcpy(Config.Gpu, "gpuGLES.so");
89                 */
90                 else if (!strcmp(argv[i], "-cdda"))             Config.Cdda = 1;
91                 else if (!strcmp(argv[i], "-xa"))               Config.Xa = 1;
92                 else if (!strcmp(argv[i], "-rcnt"))             Config.RCntFix = 1 ;
93                 else if (!strcmp(argv[i], "-sio"))              Config.Sio = 1;
94                 else if (!strcmp(argv[i], "-spuirq"))   Config.SpuIrq = 1;
95                 else if (!strcmp(argv[i], "-vsync"))    Config.VSyncWA = 1;
96         }
97
98         hildon_init(&argc, &argv);
99         
100         if (cdfile) {
101                 set_cd_image(cdfile);
102                 strcpy(file_name, strrchr(cdfile,'/'));
103         }
104
105         if (SysInit() == -1)
106                 return 1;
107
108         pl_init();
109
110         if (LoadPlugins() == -1) {
111                 SysMessage("Failed loading plugins!");
112                 return 1;
113         }
114
115         if (OpenPlugins() == -1) {
116                 return 1;
117         }
118         plugin_call_rearmed_cbs();
119
120         CheckCdrom();
121         SysReset();
122
123         if (file[0] != '\0') {
124                 if (Load(file) != -1)
125                         ready_to_go = 1;
126         } else {
127                 if (cdfile) {
128                         if (LoadCdrom() == -1) {
129                                 ClosePlugins();
130                                 printf(_("Could not load CD-ROM!\n"));
131                                 return -1;
132                         }
133                         ready_to_go = 1;
134                 }
135         }
136
137         // If a state has been specified, then load that
138         if (loadst) {
139                 int ret = emu_load_state(loadst - 1);
140                 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
141         }
142
143         if (ready_to_go)
144                 maemo_init(&argc, &argv);
145         else
146         {
147                 printf ("somethings goes wrong, maybe you forgot -cdfile ? \n");
148                 return 0;
149         }
150
151         pl_timing_prepare(Config.PsxType);
152
153         while (1)
154         {
155                 stop = 0;
156                 emu_action = SACTION_NONE;
157
158                 psxCpu->Execute();
159                 if (emu_action != SACTION_NONE)
160                         do_emu_action();
161         }
162
163         return 0;
164 }
165