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"
|
2013-07-16 20:36:18 +03:00
|
|
|
#include "menu.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;
|
|
|
|
|
2013-07-16 20:36:18 +03:00
|
|
|
void InputManager::init(const string &conffile, Menu *menu) {
|
|
|
|
this->menu = menu;
|
|
|
|
|
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
|
2013-07-29 08:28:52 +03:00
|
|
|
int i;
|
2013-07-29 23:54:12 +03:00
|
|
|
|
|
|
|
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
|
|
|
|
ERROR("Unable to init joystick subsystem\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-29 08:28:52 +03:00
|
|
|
for (i = 0; i < SDL_NumJoysticks(); i++)
|
|
|
|
joysticks.push_back(SDL_JoystickOpen(i));
|
2013-07-29 23:54:12 +03:00
|
|
|
DEBUG("Opening %i joysticks\n", i);
|
2011-10-23 11:12:11 +03:00
|
|
|
#endif
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 08:28:52 +03:00
|
|
|
InputManager::~InputManager()
|
|
|
|
{
|
2011-10-23 11:12:11 +03:00
|
|
|
#ifndef SDL_JOYSTICK_DISABLED
|
2013-07-29 08:28:52 +03:00
|
|
|
for (std::vector<SDL_Joystick *>::iterator it = joysticks.begin();
|
|
|
|
it < joysticks.end(); it++)
|
|
|
|
SDL_JoystickClose(*it);
|
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;
|
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() {
|
2013-09-07 18:00:57 +03:00
|
|
|
Button button;
|
|
|
|
while (!getButton(&button, true));
|
|
|
|
return button;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 18:00:57 +03:00
|
|
|
bool InputManager::pollButton(Button *button) {
|
|
|
|
return getButton(button, false);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 18:00:57 +03:00
|
|
|
bool InputManager::getButton(Button *button, 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
|
2013-07-29 08:28:52 +03:00
|
|
|
if (joysticks.size() > 0)
|
2011-10-23 10:59:22 +03:00
|
|
|
SDL_JoystickUpdate();
|
2011-10-23 11:12:11 +03:00
|
|
|
#endif
|
2013-07-29 08:28:52 +03:00
|
|
|
|
2011-10-23 08:49:43 +03:00
|
|
|
SDL_Event event;
|
2013-09-07 18:00:57 +03:00
|
|
|
if (wait)
|
2011-10-23 08:49:43 +03:00
|
|
|
SDL_WaitEvent(&event);
|
2013-09-07 18:00:57 +03:00
|
|
|
else if (!SDL_PollEvent(&event))
|
|
|
|
return false;
|
2011-10-23 08:49:43 +03:00
|
|
|
|
2011-10-23 09:57:52 +03:00
|
|
|
ButtonSource source;
|
2011-10-23 08:49:43 +03:00
|
|
|
switch(event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
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:
|
|
|
|
source = JOYSTICK;
|
|
|
|
break;
|
2011-10-23 11:12:11 +03:00
|
|
|
#endif
|
2013-07-16 20:36:18 +03:00
|
|
|
case SDL_USEREVENT:
|
2013-07-22 06:52:35 +03:00
|
|
|
switch ((enum EventCode) event.user.code) {
|
|
|
|
case REMOVE_LINKS:
|
|
|
|
menu->removePackageLink((const char *) event.user.data1);
|
|
|
|
break;
|
|
|
|
case OPEN_PACKAGE:
|
|
|
|
menu->openPackage((const char *) event.user.data1);
|
|
|
|
break;
|
|
|
|
case OPEN_PACKAGES_FROM_DIR:
|
|
|
|
menu->openPackagesFromDir(
|
|
|
|
((string) (const char *) event.user.data1
|
|
|
|
+ "/apps").c_str());
|
|
|
|
break;
|
|
|
|
case REPAINT_MENU:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.user.data1)
|
|
|
|
free(event.user.data1);
|
2013-09-07 18:00:57 +03:00
|
|
|
*button = REPAINT;
|
2013-07-16 20:36:18 +03:00
|
|
|
return true;
|
2013-07-22 06:52:35 +03:00
|
|
|
|
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) {
|
2013-09-07 18:00:57 +03:00
|
|
|
*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) {
|
2013-09-07 18:00:57 +03:00
|
|
|
*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
|
|
|
}
|