2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Massimiliano Torromeo *
|
|
|
|
* massimiliano.torromeo@gmail.com *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
#include "debug.h"
|
|
|
|
#include "inputmanager.h"
|
|
|
|
#include "utilities.h"
|
2011-06-02 06:04:35 +03:00
|
|
|
#include "powersaver.h"
|
2010-09-17 23:34:26 +03:00
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2010-09-17 23:31:09 +03:00
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
static SDL_Joystick *joystick;
|
|
|
|
|
|
|
|
void InputManager::init(const string &conffile) {
|
|
|
|
if (!readConfFile(conffile))
|
|
|
|
ERROR("InputManager initialization from config file failed.\n");
|
|
|
|
}
|
|
|
|
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
InputManager::InputManager() {
|
|
|
|
initJoystick();
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
InputManager::~InputManager() {
|
2011-10-23 08:22:55 +03:00
|
|
|
if (SDL_NumJoysticks() > 0) {
|
2010-09-17 23:34:26 +03:00
|
|
|
SDL_JoystickClose(joystick);
|
2011-10-23 08:22:55 +03:00
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
|
|
|
|
void InputManager::initJoystick() {
|
2011-10-23 08:22:55 +03:00
|
|
|
if (SDL_NumJoysticks() > 0) {
|
2010-09-17 23:34:26 +03:00
|
|
|
joystick = SDL_JoystickOpen(0);
|
2011-10-23 08:22:55 +03:00
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
|
|
|
|
bool InputManager::readConfFile(const string &conffile) {
|
|
|
|
if (!fileExists(conffile)) return false;
|
|
|
|
|
|
|
|
ifstream inf(conffile.c_str(), ios_base::in);
|
|
|
|
if (!(inf.is_open()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
string line, name, source;
|
|
|
|
string::size_type pos;
|
|
|
|
buttontype_t button;
|
|
|
|
|
|
|
|
while(getline(inf, line, '\n')) {
|
|
|
|
pos = line.find("=");
|
|
|
|
name = trim(line.substr(0,pos));
|
|
|
|
line = trim(line.substr(pos+1,line.length()));
|
|
|
|
|
|
|
|
if (name == "up") button = UP;
|
|
|
|
else if (name == "down") button = DOWN;
|
|
|
|
else if (name == "left") button = LEFT;
|
|
|
|
else if (name == "right") button = RIGHT;
|
|
|
|
else if (name == "accept") button = ACCEPT;
|
|
|
|
else if (name == "cancel") button = CANCEL;
|
|
|
|
else if (name == "clear") button = CLEAR;
|
|
|
|
else if (name == "manual") button = MANUAL;
|
|
|
|
else if (name == "altleft") button = ALTLEFT;
|
|
|
|
else if (name == "altright") button = ALTRIGHT;
|
|
|
|
else if (name == "menu") button = MENU;
|
|
|
|
else if (name == "settings") button = SETTINGS;
|
|
|
|
else if (name == "volup") button = VOLUP;
|
|
|
|
else if (name == "voldown") button = VOLDOWN;
|
|
|
|
else if (name == "power") button = POWER;
|
|
|
|
else if (name == "lock") button = LOCK;
|
|
|
|
else return false;
|
|
|
|
|
|
|
|
pos = line.find(",");
|
|
|
|
source = trim(line.substr(0,pos));
|
|
|
|
line = trim(line.substr(pos+1, line.length()));
|
|
|
|
|
|
|
|
if (source == "keyboard") ButtonMap[button].source = KEYBOARD;
|
|
|
|
else if (source == "joystick") ButtonMap[button].source = JOYSTICK;
|
|
|
|
else return false;
|
|
|
|
|
|
|
|
ButtonMap[button].code = atoi(line.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
inf.close();
|
|
|
|
return true;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
|
|
|
|
buttontype_t InputManager::waitForPressedButton() {
|
|
|
|
return waitForButton(PRESSED);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
|
|
|
|
buttontype_t InputManager::waitForReleasedButton() {
|
|
|
|
return waitForButton(RELEASED);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
buttontype_t InputManager::waitForButton(enum state_e state) {
|
|
|
|
bevent_t event;
|
|
|
|
do {
|
|
|
|
waitForEvent(&event);
|
|
|
|
} while(event.state != state);
|
|
|
|
return event.button;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
|
|
|
|
void InputManager::waitForEvent(bevent_t *event) {
|
|
|
|
getEvent(event, true);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
|
|
|
|
bool InputManager::pollEvent(bevent_t *event) {
|
|
|
|
return getEvent(event, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InputManager::getEvent(bevent_t *bevent, bool wait) {
|
|
|
|
//TODO: when an event is processed, program a new event
|
|
|
|
//in some time, and when it occurs, do a key repeat
|
2011-10-23 08:22:55 +03:00
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
SDL_JoystickUpdate();
|
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
if (wait)
|
|
|
|
SDL_WaitEvent(&event);
|
|
|
|
else {
|
|
|
|
bevent->state = RELEASED;
|
|
|
|
if (!SDL_PollEvent(&event)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum source_type_e source;
|
|
|
|
|
|
|
|
switch(event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
bevent->state = PRESSED;
|
|
|
|
source = KEYBOARD;
|
|
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
|
|
|
bevent->state = RELEASED;
|
|
|
|
source = KEYBOARD;
|
|
|
|
break;
|
|
|
|
case SDL_JOYBUTTONDOWN:
|
|
|
|
bevent->state = PRESSED;
|
|
|
|
source = JOYSTICK;
|
|
|
|
break;
|
|
|
|
case SDL_JOYBUTTONUP:
|
|
|
|
bevent->state = RELEASED;
|
|
|
|
source = JOYSTICK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source == KEYBOARD) {
|
|
|
|
for (int i=0; i<BUTTONTYPE_T_SIZE; i++)
|
|
|
|
if (ButtonMap[i].source == KEYBOARD && (unsigned int)event.key.keysym.sym == ButtonMap[i].code) {
|
|
|
|
bevent->button = (buttontype_t)i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i=0; i<BUTTONTYPE_T_SIZE; i++)
|
|
|
|
if (ButtonMap[i].source == JOYSTICK && (unsigned int)event.jbutton.button == ButtonMap[i].code) {
|
|
|
|
bevent->button = (buttontype_t)i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-08-17 01:46:43 +03:00
|
|
|
if ( wait && PowerSaver::isRunning())
|
2011-06-02 06:04:35 +03:00
|
|
|
PowerSaver::getInstance()->resetScreenTimer();
|
2011-08-17 01:46:43 +03:00
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
return true;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|