forked from SteveMaddison/cabrio
-
Notifications
You must be signed in to change notification settings - Fork 4
/
emulator.c
272 lines (238 loc) · 6.17 KB
/
emulator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#ifdef __unix__
#include <sys/wait.h>
#endif
#ifdef __WIN32__
#include <windows.h>
#endif
#include "emulator.h"
#include "sdl_wrapper.h"
#include "ogl.h"
#include "platform.h"
#include "bg.h"
#include "menu.h"
#include "submenu.h"
#include "hint.h"
#include "game_sel.h"
#include "sound.h"
#include "font.h"
#include "event.h"
#include "snap.h"
#include "platform.h"
#include <pwd.h>
char filename[CONFIG_FILE_NAME_LENGTH]= ".cabrio/.lock";
char save_filename[CONFIG_FILE_NAME_LENGTH];
void pause_all( void ) {
sound_pause();
game_sel_pause();
game_list_pause();
snap_pause();
submenu_pause();
platform_pause();
menu_pause();
hint_pause();
font_pause();
bg_pause();
event_pause();
}
int resume_all( void ) {
if( event_resume() != 0 )
return -1;
bg_resume();
if( font_resume() != 0 )
return -3;
if( hint_resume() != 0 )
return -4;
if( menu_resume() != 0 )
return -5;
if( platform_resume() != 0 )
return -6;
if( submenu_resume() != 0 )
return -7;
sound_resume();
if( snap_resume() != 0 )
return -9;
if( game_sel_resume() != 0 )
return -10;
return 0;
}
int emulator_exec( struct game *game ) {
char *params[CONFIG_MAX_PARAMS];
struct config_param *param = NULL;
int i = 0;
int count = 0;
char current_dir[CONFIG_FILE_NAME_LENGTH];
#ifdef __WIN32__
PROCESS_INFORMATION pi;
STARTUPINFO si;
char cmdline[CONFIG_MAX_CMD_LENGTH];
#endif
#ifdef __unix__
/* ensuring that argv[0] from the callee's perspective is the executable name,
* so that the first param doesn't get swallowed */
params[count++] = game->emulator->executable;
#endif
if( game->emulator && game->emulator->executable ) {
param = game->emulator->params;
while( param ) {
if( param->name && *param->name && count < CONFIG_MAX_PARAMS - 2 )
params[count++] = param->name;
if( param->value && *param->value && count < CONFIG_MAX_PARAMS - 2 )
params[count++] = param->value;
param = param->next;
}
param = game->params;
while( param ) {
if( param->name && *param->name && count < CONFIG_MAX_PARAMS - 2 )
params[count++] = param->name;
if( param->value && *param->value && count < CONFIG_MAX_PARAMS - 2 )
params[count++] = param->value;
param = param->next;
}
}
else {
fprintf( stderr, "Error: No suitable emulator found in configuration\n" );
return -1;
}
if( game && game->rom_path && game->rom_path[0] ) {
/* If <concat-path> true, concatenates directory and rom string */
if ( game->emulator->concat_path ) {
char tmp[CONFIG_FILE_NAME_LENGTH];
strcpy( tmp, (game->emulator->directory) );
strcat( tmp, (game->rom_path) );
params[count++] = tmp;
}
else
params[count++] = game->rom_path;
}
else {
fprintf( stderr, "Error: No ROM image specified for game\n" );
return -1;
}
/* If emulator provided a directory and if it didn't concatenate with rom_path, go to it. */
if( game->emulator->directory[0] && game->emulator->concat_path == 0 ) {
if ( getcwd( current_dir, CONFIG_FILE_NAME_LENGTH-1 )) {
if (chdir( game->emulator->directory ))
printf( "Changing directory to %s\n", game->emulator->directory );
}
}
#ifdef __unix__
/* Terminate param list */
params[count] = NULL;
printf( "Executing: " );
for( i = 0 ; i < count ; i++ ) {
printf( " %s", params[i] );
}
printf( "\n" );
pid_t pid = fork();
if (pid == 0) {
if( execvp( params[0], params ) != 0 ) {
fprintf( stderr, "Error: Couldn't execute emulator '%s': %s\n", params[0], strerror( errno ) );
exit( 1 );
}
}
else if (pid < 0) {
fprintf(stderr, "Error: failed to fork\n");
return -1;
}
wait( NULL );
#endif
#ifdef __WIN32__
memset( &pi, 0, sizeof(PROCESS_INFORMATION));
memset( &si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(si);
snprintf( cmdline, CONFIG_MAX_CMD_LENGTH, "\"%s\"", game->emulator->executable );
for( i = 0 ; i < count ; i++ ) {
if( strlen(cmdline) + strlen(params[i]) + 4 <= CONFIG_MAX_CMD_LENGTH ) {
strcat( cmdline, " \"" );
strcat( cmdline, params[i] );
strcat( cmdline, "\"" );
}
else {
fprintf( stderr, "Warning: No space for parameter: '%s'\n", params[i] );
break;
}
}
printf( "Executing: %s\n", cmdline );
if( CreateProcess( NULL, cmdline, 0, 0, 0, 0, 0, 0, &si, &pi ) ) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else {
LPTSTR s;
FormatMessage( (FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM),
NULL, GetLastError(), 0, (LPTSTR)&s, 0, NULL );
fprintf( stderr, "Warning: failed to execute: %s\n", s );
}
#endif
if( game->emulator->directory[0] ) {
if (chdir( current_dir ))
fprintf( stderr, "current directory.\n");
}
return 0;
}
int emulator_run( struct game *game ) {
int ret = 0;
struct passwd *passwd = getpwuid(getuid());
if (music){
Mix_HaltMusic();
music = NULL;
}
pause_all();
sdl_free();
FILE* file = NULL;
snprintf( save_filename, CONFIG_FILE_NAME_LENGTH, "%s/%s", passwd->pw_dir, filename );
file = fopen( save_filename, "w+" );
if( file == NULL ) {
fprintf(stderr, "Can't save state: %s\n", save_filename);
}
else {
fprintf(file, "%s", game->name);
fclose( file );
}
emulator_exec( game );
if( sdl_init() != 0 )
return -1;
if( ogl_init() != 0 )
return -1;
ret = resume_all();
if( ret != 0 )
fprintf( stderr, "Warning: resume_all() failed: %d\n", ret );
return ret;
}
struct config_emulator *emulator_get_by_name( const char *name ) {
struct config_emulator *e = config_get()->emulators;
if( name && name[0] ) {
while( e ) {
if( strcasecmp( e->name, name ) == 0 )
break;
e = e->next;
}
}
return e;
}
struct config_emulator *emulator_get_by_platform( const char *platform ) {
struct config_emulator *e = config_get()->emulators;
if( platform && platform[0] ) {
while( e ) {
if( e->platform && e->platform->name && strcasecmp( e->platform->name, platform ) == 0 )
break;
e = e->next;
}
}
return e;
}
struct config_emulator *emulator_get_default( void ) {
struct config_emulator *e = config_get()->emulators;
while( e ) {
if( e->is_default )
break;
e = e->next;
}
if( !e )
e = config_get()->emulators;
return e;
}