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