Skip to content

Commit

Permalink
Emulator selection based on platform
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveMaddison committed Nov 26, 2009
1 parent 1b9ef52 commit 00a6ddf
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 18 deletions.
17 changes: 16 additions & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static char config_filename[CONFIG_FILE_NAME_LENGTH] = "";
/* Specific XML tags */
static const char *tag_root = "cabrio-config";
static const char *tag_emulators = "emulators";
static const char *tag_emulator = "emulator";
static const char *tag_emulator_executable = "executable";
static const char *tag_game_list = "game-list";
static const char *tag_games = "games";
Expand Down Expand Up @@ -152,6 +151,7 @@ static const char *tag_theme_sounds_sound = "sound";
static const char *tag_theme_sounds_sound_file = "sound-file";
static const char *tag_theme_snap = "snap";
static const char *tag_theme_snap_fix_ar = "fix-aspect-ratio";
static const char *tag_theme_snap_platform_icons = "platform-icons";
static const char *tag_theme_hints = "hints";
static const char *tag_theme_hints_pulse = "pulse";
static const char *tag_theme_hints_image_back = "back-image";
Expand Down Expand Up @@ -202,6 +202,8 @@ static const char *tag_directory = "directory";
static const char *tag_color = "color";
static const char *tag_match = "match";
static const char *tag_category = "category";
static const char *tag_emulator = "emulator";
static const char *tag_default = "default";

/* Common values */
static const char *config_empty = "";
Expand Down Expand Up @@ -438,9 +440,15 @@ int config_read_emulator( xmlNode *node, struct config_emulator *emulator ) {
else if( strcmp( (char*)node->name, tag_directory ) == 0 ) {
strncpy( emulator->directory, (char*)xmlNodeGetContent(node), CONFIG_FILE_NAME_LENGTH );
}
else if( strcmp( (char*)node->name, tag_platform ) == 0 ) {
emulator->platform = config_platform( (char*)xmlNodeGetContent(node) );
}
else if( strcmp( (char*)node->name, tag_params ) == 0 ) {
config_read_emulator_params( node->children, emulator );
}
else if( strcmp( (char*)node->name, tag_default ) == 0 ) {
config_read_boolean( (char*)node->name, (char*)xmlNodeGetContent(node), &emulator->is_default );
}
else {
fprintf( stderr, warn_skip, tag_emulator, node->name );
}
Expand Down Expand Up @@ -738,6 +746,9 @@ int config_read_game( xmlNode *node, struct config_game *game, const char *game_
else if( strcmp( (char*)node->name, tag_game_video ) == 0 ) {
strncpy( game->video, (char*)xmlNodeGetContent(node), CONFIG_FILE_NAME_LENGTH );
}
else if( strcmp( (char*)node->name, tag_emulator ) == 0 ) {
strncpy( game->emulator, (char*)xmlNodeGetContent(node), CONFIG_NAME_LENGTH );
}
else {
fprintf( stderr, warn_skip, tag_game, node->name );
}
Expand Down Expand Up @@ -1083,6 +1094,9 @@ int config_read_snap( xmlNode *node, struct config_snap *snap ) {
else if( strcmp( (char*)node->name, tag_auto_hide ) == 0 ) {
config_read_boolean( (char*)node->name, (char*)xmlNodeGetContent(node), &snap->auto_hide );
}
else if( strcmp( (char*)node->name, tag_theme_snap_platform_icons ) == 0 ) {
config_read_boolean( (char*)node->name, (char*)xmlNodeGetContent(node), &snap->platform_icons );
}
else {
fprintf( stderr, warn_skip, tag_theme_snap, node->name );
}
Expand Down Expand Up @@ -2220,6 +2234,7 @@ int config_new( void ) {
default_theme.snap.size = 1.0;
default_theme.snap.fix_aspect_ratio = 1;
default_theme.snap.auto_hide = 1;
default_theme.snap.platform_icons = 1;

default_theme.hints.offset1 = -2.0;
default_theme.hints.offset2 = -1.2;
Expand Down
69 changes: 58 additions & 11 deletions emulator.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#ifdef __unix__
#include <sys/wait.h>
#endif
Expand Down Expand Up @@ -52,11 +53,11 @@ int resume_all( void ) {
return -7;
if( game_list_resume() != 0 )
return -8;
sound_resume();
if( snap_resume() != 0 )
return -9;
if( game_sel_resume() != 0 )
return -10;
sound_resume();

return 0;
}
Expand All @@ -66,16 +67,15 @@ int emulator_exec( struct game *game ) {
struct config_param *param = NULL;
int i = 0;
int count = 0;
const struct config *config = config_get();
char current_dir[CONFIG_FILE_NAME_LENGTH];
#ifdef __WIN32__
PROCESS_INFORMATION pi;
STARTUPINFO si;
char cmdline[CONFIG_MAX_CMD_LENGTH];
#endif

if( config->emulators && config->emulators->executable ) {
param = config->emulators->params;
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;
Expand Down Expand Up @@ -106,27 +106,30 @@ int emulator_exec( struct game *game ) {
}

/* If emulator provided a directory, go to it. */
if( config->emulators->directory[0] ) {
if( game->emulator->directory[0] ) {
getcwd( current_dir, CONFIG_FILE_NAME_LENGTH-1 );
chdir( config->emulators->directory );
chdir( game->emulator->directory );
}

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

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

pid_t pid = fork();
if (pid == 0) {
execvp( config->emulators->executable, params );
if( execvp( game->emulator->executable, params ) != 0 ) {
fprintf( stderr, "Error: Couldn't execute emulator '%s': %s\n", game->emulator->executable, strerror(errno) );
exit( 1 );
}
}
else if (pid < 0) {
fprintf(stderr, "Warning: failed to fork\n");
fprintf(stderr, "Error: failed to fork\n");
return -1;
}
wait( NULL );
Expand All @@ -136,7 +139,7 @@ int emulator_exec( struct game *game ) {
memset( &si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(si);

snprintf( cmdline, CONFIG_MAX_CMD_LENGTH, "\"%s\"", config->emulators->executable );
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, " \"" );
Expand All @@ -163,7 +166,7 @@ int emulator_exec( struct game *game ) {
}
#endif

if( config->emulators->directory[0] )
if( game->emulator->directory[0] )
chdir( current_dir );

return 0;
Expand All @@ -187,3 +190,47 @@ int emulator_run( struct game *game ) {

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

18 changes: 18 additions & 0 deletions game.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "sdl_ogl.h"
#include "font.h"
#include "media.h"
#include "emulator.h"
#include "location.h"
#include "lookup.h"

Expand Down Expand Up @@ -188,6 +189,23 @@ int game_list_create( void ) {
game->platform = platform_get( NULL );
}

if( config_game->emulator && config_game->emulator[0] ) {
game->emulator = emulator_get_by_name( config_game->emulator );
if( !game->emulator )
fprintf( stderr, "Warning: Emulator '%s' defined for game '%s' not found\n", config_game->emulator, game->name );
}
if( !game->emulator && config_game->platform ) {
game->emulator = emulator_get_by_platform( config_game->platform->name );
}
if( !game->emulator ) {
game->emulator = emulator_get_default();
}
if( !game->emulator ) {
fprintf( stderr, "Warning: No emulator found for game '%s'\n", game->name );
free( game );
continue;
}

/* Add game categories. */
game->categories = NULL;
while( config_game_category ) {
Expand Down
14 changes: 9 additions & 5 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,21 @@ struct config_param {
char value[CONFIG_PARAM_LENGTH];
};

struct config_platform {
struct config_platform *next;
char name[CONFIG_NAME_LENGTH];
};

struct config_emulator {
struct config_emulator *next;
int id;
char name[CONFIG_NAME_LENGTH];
char display_name[CONFIG_NAME_LENGTH];
char executable[CONFIG_FILE_NAME_LENGTH];
char directory[CONFIG_FILE_NAME_LENGTH];
int is_default;
struct config_param *params;
};

struct config_platform {
struct config_platform *next;
char name[CONFIG_NAME_LENGTH];
struct config_platform *platform;
};

struct config_lookup {
Expand Down Expand Up @@ -94,6 +96,7 @@ struct config_game {
char name[CONFIG_NAME_LENGTH];
char rom_image[CONFIG_FILE_NAME_LENGTH];
char video[CONFIG_FILE_NAME_LENGTH];
char emulator[CONFIG_NAME_LENGTH];
struct config_game_category *categories;
struct config_param *params;
struct config_platform *platform;
Expand Down Expand Up @@ -160,6 +163,7 @@ struct config_snap {
float size;
int fix_aspect_ratio;
int auto_hide;
int platform_icons;
};

struct config_hints {
Expand Down
4 changes: 4 additions & 0 deletions include/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
#define _EMULATOR_H_ 1

#include "game.h"
#include "config.h"

int emulator_run( struct game *game );
struct config_emulator *emulator_get_by_name( const char *name );
struct config_emulator *emulator_get_by_platform( const char *platform );
struct config_emulator *emulator_get_default( void );

#endif

1 change: 1 addition & 0 deletions include/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct game {
struct texture *texture;
struct config_param *params;
struct game_media *media;
struct config_emulator *emulator;
char *name;
char *rom_path;
};
Expand Down
2 changes: 1 addition & 1 deletion snap.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void snap_draw( void ) {
glTexCoord2f(1.0, 0.0); glVertex3f( xsize, ysize, 0.0);
glEnd();

if( platform_count() > 1 && platform_texture ) {
if( config->platform_icons && platform_count() > 1 && platform_texture ) {
GLfloat platform_xsize = platform_texture->width;
GLfloat platform_ysize = platform_texture->height;

Expand Down

0 comments on commit 00a6ddf

Please sign in to comment.