Skip to content

Commit

Permalink
Added <concat-path> tag to concatenates rom path with directory (some…
Browse files Browse the repository at this point in the history
… emulators may need the full path instead cd /your/path/ & ./executable).
  • Loading branch information
sergiobenrocha2 committed Mar 24, 2015
1 parent e2ad9ec commit 6dab21f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
6 changes: 6 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ static const char *tag_position_y = "y-position";
static const char *tag_position_z = "z-position";
static const char *tag_order = "order";
static const char *tag_directory = "directory";
static const char *tag_concat_path = "concat-path";
static const char *tag_color = "color";
static const char *tag_match = "match";
static const char *tag_category = "category";
Expand Down Expand Up @@ -467,6 +468,11 @@ int config_read_emulator( xmlNode *node, struct config_emulator *emulator ) {
strncpy( emulator->directory, content, CONFIG_FILE_NAME_LENGTH );
free( content );
}
else if( strcmp( (char*)node->name, tag_concat_path ) == 0 ) {
char *content = (char*)xmlNodeGetContent(node);
config_read_boolean( (char*)node->name, content, &emulator->concat_path );
free( content );
}
else if( strcmp( (char*)node->name, tag_platform ) == 0 ) {
char *content = (char*)xmlNodeGetContent(node);
emulator->platform = config_platform( content );
Expand Down
63 changes: 37 additions & 26 deletions emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int resume_all( void ) {
return -9;
if( game_sel_resume() != 0 )
return -10;

return 0;
}

Expand All @@ -69,6 +69,7 @@ int emulator_exec( struct game *game ) {
int i = 0;
int count = 0;
char current_dir[CONFIG_FILE_NAME_LENGTH];

#ifdef __WIN32__
PROCESS_INFORMATION pi;
STARTUPINFO si;
Expand All @@ -89,58 +90,68 @@ int emulator_exec( struct game *game ) {
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");
fprintf( stderr, "Error: No suitable emulator found in configuration\n" );
return -1;
}

if( game && game->rom_path && game->rom_path[0] ) {
params[count++] = game->rom_path;
/* 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;
fprintf( stderr, "Error: No ROM image specified for game\n" );
return -1;
}
/* If emulator provided a directory, go to it. */
if( game->emulator->directory[0] ) {

/* 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 ) {
getcwd( current_dir, CONFIG_FILE_NAME_LENGTH-1 );
chdir( game->emulator->directory );
printf( "Changing directory to %s\n", game->emulator->directory );
}

#ifdef __unix__
/* Terminate param list */
params[count] = NULL;

printf( "Executing: %s", game->emulator->executable );
printf( "Executing: " );
for( i = 0 ; i < count ; i++ ) {
printf( " %s", params[i] );
}
printf( "\n" );

pid_t pid = fork();
if (pid == 0) {
if( execvp( game->emulator->executable, params ) != 0 ) {
fprintf( stderr, "Error: Couldn't execute emulator '%s': %s\n", game->emulator->executable, strerror(errno) );
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));
Expand All @@ -159,7 +170,7 @@ int emulator_exec( struct game *game ) {
}
}
printf( "Executing: %s\n", cmdline );

if( CreateProcess( NULL, cmdline, 0, 0, 0, 0, 0, 0, &si, &pi ) ) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
Expand Down Expand Up @@ -191,15 +202,15 @@ int emulator_run( struct game *game ) {
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 );

Expand All @@ -216,44 +227,44 @@ int emulator_run( struct game *game ) {

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;
}

1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct config_emulator {
char display_name[CONFIG_NAME_LENGTH];
char executable[CONFIG_FILE_NAME_LENGTH];
char directory[CONFIG_FILE_NAME_LENGTH];
int concat_path;
int is_default;
struct config_param *params;
struct config_platform *platform;
Expand Down

0 comments on commit 6dab21f

Please sign in to comment.