2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
2011-10-23 08:49:43 +03:00
|
|
|
* Copyright (C) 2006 by Massimiliano Torromeo *
|
|
|
|
* massimiliano.torromeo@gmail.com *
|
2010-02-04 13:33:47 +02:00
|
|
|
* *
|
|
|
|
* 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
|
|
|
|
2011-10-23 09:15:23 +03:00
|
|
|
using namespace std;
|
|
|
|
|
2010-09-17 23:34:26 +03:00
|
|
|
void InputManager::init(const string &conffile) {
|
2011-10-23 11:38:39 +03:00
|
|
|
for (int i = 0; i < BUTTON_TYPE_SIZE; i++) {
|
|
|
|
buttonMap[i].source = UNMAPPED;
|
2011-10-23 08:49:43 +03:00
|
|
|
}
|
2011-10-23 11:38:39 +03:00
|
|
|
readConfFile(conffile);
|
2010-09-17 23:34:26 +03:00
|
|
|
}
|
|
|
|
|
2011-10-23 10:59:22 +03:00
|
|
|
InputManager::InputManager()
|
|
|
|
{
|
2011-10-23 11:12:11 +03:00
|
|
|
#ifndef SDL_JOYSTICK_DISABLED
|
2011-10-23 08:49:43 +03:00
|
|
|
if (SDL_NumJoysticks() > 0) {
|
2011-10-23 10:59:22 +03:00
|
|
|
joystick = SDL_JoystickOpen(0);
|
2011-10-23 11:12:11 +03:00
|
|
|
} else {
|
|
|
|
joystick = NULL;
|
2011-10-23 08:49:43 +03:00
|
|
|
}
|
2011-10-23 11:12:11 +03:00
|
|
|
#endif
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 10:59:22 +03:00
|
|
|
InputManager::~InputManager() {
|
2011-10-23 11:12:11 +03:00
|
|
|
#ifndef SDL_JOYSTICK_DISABLED
|
2011-10-23 10:59:22 +03:00
|
|
|
if (joystick) {
|
|
|
|
SDL_JoystickClose(joystick);
|
2011-10-23 08:49:43 +03:00
|
|
|
}
|
2011-10-23 11:12:11 +03:00
|
|
|
#endif
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 11:38:39 +03:00
|
|
|
void InputManager::readConfFile(const string &conffile) {
|
2011-10-23 08:49:43 +03:00
|
|
|
ifstream inf(conffile.c_str(), ios_base::in);
|
2011-10-23 11:38:39 +03:00
|
|
|
if (inf.fail()) {
|
|
|
|
ERROR("InputManager: failed to open config file\n");
|
|
|
|
return;
|
2011-10-23 08:49:43 +03:00
|
|
|
}
|
|
|
|
|
2011-10-23 11:38:39 +03:00
|
|
|
string line;
|
|
|
|
while (getline(inf, line, '\n')) {
|
|
|
|
string::size_type pos = line.find("=");
|
|
|
|
string name = trim(line.substr(0,pos));
|
2011-10-23 08:49:43 +03:00
|
|
|
line = trim(line.substr(pos+1,line.length()));
|
|
|
|
|
2011-10-23 11:38:39 +03:00
|
|
|
Button button;
|
2011-10-23 08:49:43 +03:00
|
|
|
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 == "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;
|
2011-10-23 11:38:39 +03:00
|
|
|
else {
|
|
|
|
WARNING("InputManager: Ignoring unknown button name \"%s\"\n",
|
|
|
|
name.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-23 08:49:43 +03:00
|
|
|
|
|
|
|
pos = line.find(",");
|
2011-10-23 11:38:39 +03:00
|
|
|
string sourceStr = trim(line.substr(0,pos));
|
2011-10-23 08:49:43 +03:00
|
|
|
line = trim(line.substr(pos+1, line.length()));
|
|
|
|
|
2011-10-23 11:38:39 +03:00
|
|
|
ButtonSource source;
|
|
|
|
if (sourceStr == "keyboard") {
|
|
|
|
source = KEYBOARD;
|
|
|
|
#ifndef SDL_JOYSTICK_DISABLED
|
|
|
|
} else if (sourceStr == "joystick") {
|
|
|
|
source = JOYSTICK;
|
2011-10-23 11:12:11 +03:00
|
|
|
#endif
|
|
|
|
} else {
|
2011-10-23 11:38:39 +03:00
|
|
|
WARNING("InputManager: Ignoring unknown button source \"%s\"\n",
|
|
|
|
sourceStr.c_str());
|
|
|
|
continue;
|
2011-10-23 11:12:11 +03:00
|
|
|
}
|
2011-10-23 11:38:39 +03:00
|
|
|
buttonMap[button].source = source;
|
2011-10-23 09:57:52 +03:00
|
|
|
buttonMap[button].code = atoi(line.c_str());
|
2011-10-23 08:49:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inf.close();
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 09:57:52 +03:00
|
|
|
InputManager::Button InputManager::waitForPressedButton() {
|
2011-10-23 08:49:43 +03:00
|
|
|
return waitForButton(PRESSED);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 09:57:52 +03:00
|
|
|
InputManager::Button InputManager::waitForReleasedButton() {
|
2011-10-23 08:49:43 +03:00
|
|
|
return waitForButton(RELEASED);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 09:57:52 +03:00
|
|
|
InputManager::Button InputManager::waitForButton(ButtonState state) {
|
|
|
|
ButtonEvent event;
|
2012-01-22 22:49:52 +02:00
|
|
|
while (!waitForEvent(&event) || event.state != state);
|
2011-10-23 08:49:43 +03:00
|
|
|
return event.button;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2012-01-22 22:49:52 +02:00
|
|
|
bool InputManager::waitForEvent(ButtonEvent *event) {
|
|
|
|
return getEvent(event, true);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 09:57:52 +03:00
|
|
|
bool InputManager::pollEvent(ButtonEvent *event) {
|
2011-10-23 08:49:43 +03:00
|
|
|
return getEvent(event, false);
|
2010-09-17 23:34:26 +03:00
|
|
|
}
|
|
|
|
|
2011-10-23 09:57:52 +03:00
|
|
|
bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
|
2011-10-23 08:49:43 +03:00
|
|
|
//TODO: when an event is processed, program a new event
|
|
|
|
//in some time, and when it occurs, do a key repeat
|
|
|
|
|
2012-01-22 22:49:52 +02:00
|
|
|
int i;
|
|
|
|
|
2011-10-23 11:12:11 +03:00
|
|
|
#ifndef SDL_JOYSTICK_DISABLED
|
2011-10-23 10:59:22 +03:00
|
|
|
if (joystick) {
|
|
|
|
SDL_JoystickUpdate();
|
|
|
|
}
|
2011-10-23 11:12:11 +03:00
|
|
|
#endif
|
2011-10-23 08:49:43 +03:00
|
|
|
SDL_Event event;
|
|
|
|
if (wait) {
|
|
|
|
SDL_WaitEvent(&event);
|
|
|
|
} else {
|
|
|
|
bevent->state = RELEASED;
|
|
|
|
if (!SDL_PollEvent(&event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-23 09:57:52 +03:00
|
|
|
ButtonSource source;
|
2011-10-23 08:49:43 +03:00
|
|
|
switch(event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
bevent->state = PRESSED;
|
|
|
|
source = KEYBOARD;
|
|
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
|
|
|
bevent->state = RELEASED;
|
|
|
|
source = KEYBOARD;
|
|
|
|
break;
|
2011-10-23 11:12:11 +03:00
|
|
|
#ifndef SDL_JOYSTICK_DISABLED
|
2011-10-23 08:49:43 +03:00
|
|
|
case SDL_JOYBUTTONDOWN:
|
|
|
|
bevent->state = PRESSED;
|
|
|
|
source = JOYSTICK;
|
|
|
|
break;
|
|
|
|
case SDL_JOYBUTTONUP:
|
|
|
|
bevent->state = RELEASED;
|
|
|
|
source = JOYSTICK;
|
|
|
|
break;
|
2011-10-23 11:12:11 +03:00
|
|
|
#endif
|
2011-10-23 08:49:43 +03:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source == KEYBOARD) {
|
2012-01-22 22:49:52 +02:00
|
|
|
for (i = 0; i < BUTTON_TYPE_SIZE; i++) {
|
2011-10-23 09:57:52 +03:00
|
|
|
if (buttonMap[i].source == KEYBOARD
|
|
|
|
&& (unsigned int)event.key.keysym.sym == buttonMap[i].code) {
|
|
|
|
bevent->button = static_cast<Button>(i);
|
2011-10-23 08:49:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-10-23 11:12:11 +03:00
|
|
|
#ifndef SDL_JOYSTICK_DISABLED
|
|
|
|
} else if (source == JOYSTICK) {
|
2012-01-22 22:49:52 +02:00
|
|
|
for (i = 0; i < BUTTON_TYPE_SIZE; i++) {
|
2011-10-23 09:57:52 +03:00
|
|
|
if (buttonMap[i].source == JOYSTICK
|
|
|
|
&& (unsigned int)event.jbutton.button == buttonMap[i].code) {
|
|
|
|
bevent->button = static_cast<Button>(i);
|
2011-10-23 08:49:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-10-23 11:12:11 +03:00
|
|
|
#endif
|
2011-10-23 08:49:43 +03:00
|
|
|
}
|
2012-01-22 22:49:52 +02:00
|
|
|
|
|
|
|
if (i == BUTTON_TYPE_SIZE)
|
|
|
|
return false;
|
|
|
|
|
2011-10-23 08:49:43 +03:00
|
|
|
if (wait && PowerSaver::isRunning()) {
|
|
|
|
PowerSaver::getInstance()->resetScreenTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|