[Add] More input error checking and safety crap.

-- This also fixes a segmentation fault on application close.
This commit is contained in:
Allanis 2013-02-10 21:45:06 +00:00
parent 3db7241663
commit 34b01c7545
4 changed files with 39 additions and 25 deletions

View File

@ -80,14 +80,7 @@ int main(int argc, char** argv) {
// input. // input.
input_init(); input_init();
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0); input_setDefault(); // Call input crap from player.c
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0);
input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, 0);
input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, 0);
input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_r, 0);
input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0);
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0);
// Use Lua to parse configuration file. // Use Lua to parse configuration file.
lua_State* L = luaL_newstate(); lua_State* L = luaL_newstate();
@ -208,7 +201,7 @@ int main(int argc, char** argv) {
if(gl_init()) { if(gl_init()) {
// Initializes video output. // Initializes video output.
WARN("Error initializing video output, exiting..."); ERR("Initializing video output failed, exiting...");
SDL_Quit(); SDL_Quit();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -231,12 +224,18 @@ int main(int argc, char** argv) {
WARN("Error initializing joystick input"); WARN("Error initializing joystick input");
if(namjoystick != NULL) { if(namjoystick != NULL) {
// Use a joystick name to find joystick. // Use a joystick name to find joystick.
joystick_use(joystick_get(namjoystick)); if(joystick_use(joystick_get(namjoystick))) {
WARN("Failure to open any joystick, falling back to default keybinds");
input_setDefault();
}
free(namjoystick); free(namjoystick);
} }
else if(indjoystick >= 0) else if(indjoystick >= 0)
// Must be using an id instead. // Must be using an id instead.
joystick_use(indjoystick); if(joystick_use(indjoystick)) {
WARN("Failure to open any joystick, falling back to default keybinds");
input_setDefault();
}
} }
// Misc. // Misc.

View File

@ -578,7 +578,7 @@ int gl_init(void) {
// Get available fullscreen modes. // Get available fullscreen modes.
if(gl_screen.fullscreen) { if(gl_screen.fullscreen) {
modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN); modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
if(modes == NULL) { if(modes == NULL) { // Could happen, but rare.
WARN("No fullscreen modes available"); WARN("No fullscreen modes available");
if(flags & SDL_FULLSCREEN) { if(flags & SDL_FULLSCREEN) {
WARN("Disabling fullscreen mode"); WARN("Disabling fullscreen mode");
@ -592,14 +592,14 @@ int gl_init(void) {
for(i = 0; modes[i]; ++i) { for(i = 0; modes[i]; ++i) {
DEBUG("\t%dx%d", modes[i]->w, modes[i]->h); DEBUG("\t%dx%d", modes[i]->w, modes[i]->h);
if((flags & SDL_FULLSCREEN) && (modes[i]->w == gl_screen.w) && (modes[i]->h == gl_screen.h)) if((flags & SDL_FULLSCREEN) && (modes[i]->w == gl_screen.w) && (modes[i]->h == gl_screen.h))
supported = 1; supported = 1; // Mode we asked for is supported.
} }
} }
// Make sure fullscreen mode is supported. // Make sure fullscreen mode is supported.
if(flags & SDL_FULLSCREEN && !supported) { if(flags & SDL_FULLSCREEN && !supported) {
WARN("Fullscreen mode %dx%d is not supported by your current setup, switching to another mode", WARN("Fullscreen mode %dx%d is not supported by your current setup, attempting %dx%d",
gl_screen.w, gl_screen.h); gl_screen.w, gl_screen.h, modes[0]->w, modes[0]->h);
gl_screen.w = modes[0]->w; gl_screen.w = modes[0]->w;
gl_screen.h = modes[0]->h; gl_screen.h = modes[0]->h;
} }
@ -618,7 +618,7 @@ int gl_init(void) {
// Actually creating the screen. // Actually creating the screen.
if(SDL_SetVideoMode(gl_screen.w, gl_screen.h, gl_screen.depth, flags) == NULL) { if(SDL_SetVideoMode(gl_screen.w, gl_screen.h, gl_screen.depth, flags) == NULL) {
WARN("Unable to create OpenGL window: %s", SDL_GetError()); ERR("Unable to create OpenGL window: %s", SDL_GetError());
SDL_Quit(); SDL_Quit();
return -1; return -1;
} }

View File

@ -386,11 +386,25 @@ void player_think(Pilot* player) {
// ================ // ================
// INPUT! // INPUT!
// ================ // ================
// Set the default input keys.
void input_setDefault(void) {
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0);
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0);
input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, 0);
input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, 0);
input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_r, 0);
input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0);
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0);
}
// Initialization/exit functions (does not assign keys). // Initialization/exit functions (does not assign keys).
void input_init(void) { void input_init(void) {
Keybind* tmp; Keybind* tmp;
int i; int i;
for(i = 0; keybindNames[i]; i++); // Get number of bindings. for(i = 0; strcmp(keybindNames[i], "end"); i++); // Get number of bindings.
player_input = malloc(i*sizeof(Keybind*)); player_input = malloc(i*sizeof(Keybind*));
// Create a null keybinding for each. // Create a null keybinding for each.
@ -406,7 +420,7 @@ void input_init(void) {
void input_exit(void) { void input_exit(void) {
int i; int i;
for(i = 0; keybindNames[i]; i++) for(i = 0; strcmp(keybindNames[i], "end"); i++)
free(player_input[i]); free(player_input[i]);
free(player_input); free(player_input);
} }
@ -414,14 +428,14 @@ void input_exit(void) {
// Binds key of type [type] to action keybind. // Binds key of type [type] to action keybind.
void input_setKeybind(char* keybind, KeybindType type, int key, int reverse) { void input_setKeybind(char* keybind, KeybindType type, int key, int reverse) {
int i; int i;
for(i = 0; keybindNames[i]; i++) { for(i = 0; strcmp(keybindNames[i], "end"); i++)
if(strcmp(keybind, player_input[i]->name)==0) { if(strcmp(keybind, player_input[i]->name)==0) {
player_input[i]->type = type; player_input[i]->type = type;
player_input[i]->key = key; player_input[i]->key = key;
player_input[i]->reverse = reverse ? -1. : 1.; player_input[i]->reverse = reverse ? -1. : 1.;
return; return;
} }
} WARN("Unable to set keybind '%s', That command does not exist.", keybind);
} }
// == Run input method. ================================================ // == Run input method. ================================================
@ -485,7 +499,7 @@ static void input_keyup(SDLKey key);
// Axis. // Axis.
static void input_joyaxis(const unsigned int axis, const int value) { static void input_joyaxis(const unsigned int axis, const int value) {
int i; int i;
for(i = 0; keybindNames[i]; i++) for(i = 0; strcmp(keybindNames[i], "end"); i++)
if(player_input[i]->type == KEYBIND_JAXIS && player_input[i]->key == axis) { if(player_input[i]->type == KEYBIND_JAXIS && player_input[i]->key == axis) {
input_key(i, -(player_input[i]->reverse) * (double)value / 32767., 1); input_key(i, -(player_input[i]->reverse) * (double)value / 32767., 1);
return; return;
@ -495,7 +509,7 @@ static void input_joyaxis(const unsigned int axis, const int value) {
// Joystick button down. // Joystick button down.
static void input_joydown(const unsigned int button) { static void input_joydown(const unsigned int button) {
int i; int i;
for(i = 0; keybindNames[i]; i++) for(i = 0; strcmp(keybindNames[i], "end");i++)
if(player_input[i]->type == KEYBIND_JBUTTON && player_input[i]->key == button) { if(player_input[i]->type == KEYBIND_JBUTTON && player_input[i]->key == button) {
input_key(i, KEY_RELEASE, 0); input_key(i, KEY_RELEASE, 0);
return; return;
@ -505,7 +519,7 @@ static void input_joydown(const unsigned int button) {
// Joystick button up. // Joystick button up.
static void input_joyup(const unsigned int button) { static void input_joyup(const unsigned int button) {
int i; int i;
for(i = 0; keybindNames[i]; i++) for(i = 0; strcmp(keybindNames[i], "end"); i++)
if(player_input[i]->type == KEYBIND_JBUTTON && player_input[i]->key == button) { if(player_input[i]->type == KEYBIND_JBUTTON && player_input[i]->key == button) {
input_key(i, KEY_RELEASE, 0); input_key(i, KEY_RELEASE, 0);
return; return;
@ -517,7 +531,7 @@ static void input_joyup(const unsigned int button) {
// Key down. // Key down.
static void input_keydown(SDLKey key) { static void input_keydown(SDLKey key) {
int i; int i;
for(i = 0; keybindNames[i]; i++) for(i = 0; strcmp(keybindNames[i], "end"); i++)
if(player_input[i]->type == KEYBIND_KEYBOARD && player_input[i]->key == key) { if(player_input[i]->type == KEYBIND_KEYBOARD && player_input[i]->key == key) {
input_key(i, KEY_PRESS, 0); input_key(i, KEY_PRESS, 0);
return; return;
@ -534,7 +548,7 @@ static void input_keydown(SDLKey key) {
// Key up. // Key up.
static void input_keyup(SDLKey key) { static void input_keyup(SDLKey key) {
int i; int i;
for(i = 0; keybindNames[i]; i++) for(i = 0; strcmp(keybindNames[i], "end"); i++)
if(player_input[i]->type == KEYBIND_KEYBOARD && player_input[i]->key == key) { if(player_input[i]->type == KEYBIND_KEYBOARD && player_input[i]->key == key) {
input_key(i, KEY_RELEASE, 0); input_key(i, KEY_RELEASE, 0);
return; return;

View File

@ -18,6 +18,7 @@ void player_rmFlag(unsigned int flag);
// Input. // Input.
void input_init(void); void input_init(void);
void input_exit(void); void input_exit(void);
void input_setDefault(void);
void input_setKeybind(char* keybind, KeybindType type, int key, int reverse); void input_setKeybind(char* keybind, KeybindType type, int key, int reverse);
void input_handle(SDL_Event* event); void input_handle(SDL_Event* event);