Fixed crash when the ~/.emulationstation folder does not exist.

This commit is contained in:
Aloshi 2013-01-07 20:24:59 -06:00
parent e59c430b89
commit 9da01403a0
8 changed files with 41 additions and 20 deletions

View file

@ -11,6 +11,7 @@ SDL_Event* InputManager::lastEvent = NULL;
std::map<int, InputManager::InputButton> InputManager::joystickButtonMap, InputManager::joystickAxisPosMap, InputManager::joystickAxisNegMap;
std::map<int, int> InputManager::axisState;
InputManager::InputButton InputManager::hatState = InputManager::UNKNOWN;
std::string InputManager::joystickName = "";
int InputManager::deadzone = 28000;
@ -204,7 +205,7 @@ void InputManager::loadConfig()
std::ifstream file(path.c_str());
std::string joystickName = "";
joystickName = "";
while(file.good())
{
@ -257,6 +258,13 @@ void InputManager::loadConfig()
}
LOG(LogDebug) << "Finished loading input config";
openJoystick();
}
void InputManager::openJoystick()
{
//if any joystick is plugged in
if(SDL_NumJoysticks() > 0)
{
@ -282,12 +290,12 @@ void InputManager::loadConfig()
LOG(LogWarning) << " could not find named joystick! You could try manually removing the joystick name entry in the input config file.";
}
}else{
LOG(LogDebug) << " opening first joystick";
LOG(LogDebug) << " opening first joystick (no name saved)";
SDL_JoystickOpen(0); //if we don't have a specific joystick in mind, take the first
}
}else{
LOG(LogDebug) << " no joysticks detected by SDL_NumJoysticks(), input loaded but no joystick opened";
LOG(LogDebug) << " no joysticks detected by SDL_NumJoysticks(), so no joystick opened";
}
}

View file

@ -15,6 +15,7 @@ namespace InputManager {
void unregisterComponent(GuiComponent* comp);
void loadConfig();
void openJoystick();
//enum for identifying input, regardless of configuration
enum InputButton { UNKNOWN, UP, DOWN, LEFT, RIGHT, BUTTON1, BUTTON2, MENU, SELECT, PAGEUP, PAGEDOWN};
@ -30,6 +31,7 @@ namespace InputManager {
extern std::map<int, InputButton> joystickAxisPosMap, joystickAxisNegMap;
extern std::map<int, int> axisState;
extern InputButton hatState;
extern std::string joystickName;
}
#endif

View file

@ -4,7 +4,7 @@
#include <iostream>
LogLevel Log::reportingLevel = LogInfo;
FILE* Log::file = fopen(getLogPath().c_str(), "w");
FILE* Log::file = NULL; //fopen(getLogPath().c_str(), "w");
LogLevel Log::getReportingLevel()
{
@ -22,6 +22,11 @@ void Log::setReportingLevel(LogLevel level)
reportingLevel = level;
}
void Log::open()
{
file = fopen(getLogPath().c_str(), "w");
}
std::ostringstream& Log::get(LogLevel level)
{
os << "lvl" << level << ": \t";
@ -49,6 +54,13 @@ FILE* Log::getOutput()
Log::~Log()
{
os << std::endl;
if(getOutput() == NULL)
{
std::cerr << "ERROR - tried to write to log file before it was open!\n";
return;
}
fprintf(getOutput(), "%s", os.str().c_str());
//if it's an error, also print to console

View file

@ -23,6 +23,7 @@ public:
static std::string getLogPath();
static void flush();
static void open();
static void close();
protected:
std::ostringstream os;

View file

@ -44,10 +44,6 @@ namespace Renderer
return false;
}
//have to reload config to re-open SDL joysticks
InputManager::loadConfig();
LOG(LogInfo) << "Creating surface...";

View file

@ -40,9 +40,6 @@ namespace Renderer
return false;
}
//we need to reload input too since SDL shut down
InputManager::loadConfig();
//usually display width/height are not specified, i.e. zero, which SDL automatically takes as "native resolution"
//so, since other things rely on the size of the screen (damn currently unnormalized coordinate system), we set it here
//even though the system was already initialized

View file

@ -8,6 +8,7 @@
#include "Renderer.h"
#include "AudioManager.h"
#include "Log.h"
#include "InputManager.h"
std::vector<SystemData*> SystemData::sSystemVector;
@ -88,6 +89,7 @@ void SystemData::launchGame(GameData* game)
}
Renderer::init(0, 0);
InputManager::openJoystick();
AudioManager::init();
//re-enable SDL joystick events

View file

@ -82,6 +82,18 @@ int main(int argc, char* argv[])
bool running = true;
//make sure the config directory exists
std::string home = getenv("HOME");
std::string configDir = home + "/.emulationstation";
if(!fs::exists(configDir))
{
std::cout << "Creating config directory \"" << configDir << "\"\n";
fs::create_directory(configDir);
}
//start the logger
Log::open();
//the renderer also takes care of setting up SDL for input and sound
bool renderInit = Renderer::init(width, height);
if(!renderInit)
@ -98,15 +110,6 @@ int main(int argc, char* argv[])
SDL_JoystickEventState(SDL_ENABLE);
//make sure the config directory exists
std::string home = getenv("HOME");
std::string configDir = home + "/.emulationstation";
if(!fs::exists(configDir))
{
std::cout << "Creating config directory \"" << configDir << "\"\n";
fs::create_directory(configDir);
}
//try loading the system config file
if(!fs::exists(SystemData::getConfigPath())) //if it doesn't exist, create the example and quit