Root/
| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
| 3 | * massimiliano.torromeo@gmail.com * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "debug.h" |
| 22 | #include "inputmanager.h" |
| 23 | #include "utilities.h" |
| 24 | #include "powersaver.h" |
| 25 | |
| 26 | #include <iostream> |
| 27 | #include <fstream> |
| 28 | |
| 29 | static SDL_Joystick *joystick; |
| 30 | |
| 31 | void InputManager::init(const string &conffile) { |
| 32 | if (!readConfFile(conffile)) |
| 33 | ERROR("InputManager initialization from config file failed.\n"); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | InputManager::InputManager() { |
| 38 | initJoystick(); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | InputManager::~InputManager() { |
| 43 | if (SDL_NumJoysticks > 0) |
| 44 | SDL_JoystickClose(joystick); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void InputManager::initJoystick() { |
| 49 | if (SDL_NumJoysticks > 0) |
| 50 | joystick = SDL_JoystickOpen(0); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | bool InputManager::readConfFile(const string &conffile) { |
| 55 | if (!fileExists(conffile)) return false; |
| 56 | |
| 57 | ifstream inf(conffile.c_str(), ios_base::in); |
| 58 | if (!(inf.is_open())) |
| 59 | return false; |
| 60 | |
| 61 | string line, name, source; |
| 62 | string::size_type pos; |
| 63 | buttontype_t button; |
| 64 | |
| 65 | while(getline(inf, line, '\n')) { |
| 66 | pos = line.find("="); |
| 67 | name = trim(line.substr(0,pos)); |
| 68 | line = trim(line.substr(pos+1,line.length())); |
| 69 | |
| 70 | if (name == "up") button = UP; |
| 71 | else if (name == "down") button = DOWN; |
| 72 | else if (name == "left") button = LEFT; |
| 73 | else if (name == "right") button = RIGHT; |
| 74 | else if (name == "accept") button = ACCEPT; |
| 75 | else if (name == "cancel") button = CANCEL; |
| 76 | else if (name == "clear") button = CLEAR; |
| 77 | else if (name == "manual") button = MANUAL; |
| 78 | else if (name == "altleft") button = ALTLEFT; |
| 79 | else if (name == "altright") button = ALTRIGHT; |
| 80 | else if (name == "menu") button = MENU; |
| 81 | else if (name == "settings") button = SETTINGS; |
| 82 | else if (name == "volup") button = VOLUP; |
| 83 | else if (name == "voldown") button = VOLDOWN; |
| 84 | else if (name == "power") button = POWER; |
| 85 | else if (name == "lock") button = LOCK; |
| 86 | else return false; |
| 87 | |
| 88 | pos = line.find(","); |
| 89 | source = trim(line.substr(0,pos)); |
| 90 | line = trim(line.substr(pos+1, line.length())); |
| 91 | |
| 92 | if (source == "keyboard") ButtonMap[button].source = KEYBOARD; |
| 93 | else if (source == "joystick") ButtonMap[button].source = JOYSTICK; |
| 94 | else return false; |
| 95 | |
| 96 | ButtonMap[button].code = atoi(line.c_str()); |
| 97 | } |
| 98 | |
| 99 | inf.close(); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | buttontype_t InputManager::waitForPressedButton() { |
| 105 | return waitForButton(PRESSED); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | buttontype_t InputManager::waitForReleasedButton() { |
| 110 | return waitForButton(RELEASED); |
| 111 | } |
| 112 | |
| 113 | buttontype_t InputManager::waitForButton(enum state_e state) { |
| 114 | bevent_t event; |
| 115 | do { |
| 116 | waitForEvent(&event); |
| 117 | } while(event.state != state); |
| 118 | return event.button; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | void InputManager::waitForEvent(bevent_t *event) { |
| 123 | getEvent(event, true); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | bool InputManager::pollEvent(bevent_t *event) { |
| 128 | return getEvent(event, false); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | bool InputManager::getEvent(bevent_t *bevent, bool wait) { |
| 133 | //TODO: when an event is processed, program a new event |
| 134 | //in some time, and when it occurs, do a key repeat |
| 135 | |
| 136 | SDL_JoystickUpdate(); |
| 137 | SDL_Event event; |
| 138 | |
| 139 | if (wait) |
| 140 | SDL_WaitEvent(&event); |
| 141 | else { |
| 142 | bevent->state = RELEASED; |
| 143 | if (!SDL_PollEvent(&event)) return false; |
| 144 | } |
| 145 | |
| 146 | enum source_type_e source; |
| 147 | |
| 148 | switch(event.type) { |
| 149 | case SDL_KEYDOWN: |
| 150 | bevent->state = PRESSED; |
| 151 | source = KEYBOARD; |
| 152 | break; |
| 153 | case SDL_KEYUP: |
| 154 | bevent->state = RELEASED; |
| 155 | source = KEYBOARD; |
| 156 | break; |
| 157 | case SDL_JOYBUTTONDOWN: |
| 158 | bevent->state = PRESSED; |
| 159 | source = JOYSTICK; |
| 160 | break; |
| 161 | case SDL_JOYBUTTONUP: |
| 162 | bevent->state = RELEASED; |
| 163 | source = JOYSTICK; |
| 164 | break; |
| 165 | default: |
| 166 | return false; |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | if (source == KEYBOARD) { |
| 171 | for (int i=0; i<BUTTONTYPE_T_SIZE; i++) |
| 172 | if (ButtonMap[i].source == KEYBOARD && (unsigned int)event.key.keysym.sym == ButtonMap[i].code) { |
| 173 | bevent->button = (buttontype_t)i; |
| 174 | break; |
| 175 | } |
| 176 | } else { |
| 177 | for (int i=0; i<BUTTONTYPE_T_SIZE; i++) |
| 178 | if (ButtonMap[i].source == JOYSTICK && (unsigned int)event.jbutton.button == ButtonMap[i].code) { |
| 179 | bevent->button = (buttontype_t)i; |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | if ( wait ) { |
| 184 | PowerSaver::getInstance()->resetScreenTimer(); |
| 185 | } |
| 186 | return true; |
| 187 | } |
| 188 |
Branches:
install_locations
master
opkrun
packages
