4 // Copyright (C) 2005 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #include "joystickkeyboardcontroller.h"
26 #include "lisp/lisp.h"
27 #include "lisp/list_iterator.h"
28 #include "game_session.h"
30 class JoystickKeyboardController::JoystickMenu : public Menu
33 JoystickMenu(JoystickKeyboardController* controller);
34 virtual ~JoystickMenu();
37 std::string get_button_name(int button);
38 virtual void menu_action(MenuItem* item);
39 JoystickKeyboardController* controller;
42 class JoystickKeyboardController::KeyboardMenu : public Menu
45 KeyboardMenu(JoystickKeyboardController* controller);
49 std::string get_key_name(SDLKey key);
50 virtual void menu_action(MenuItem* item);
51 JoystickKeyboardController* controller;
54 JoystickKeyboardController::JoystickKeyboardController()
55 : wait_for_key(-1), wait_for_joybutton(-1), key_options_menu(0),
56 joystick_options_menu(0)
58 memset(last_keys, 0, sizeof(last_keys));
60 // initialize default keyboard map
61 keymap.insert(std::make_pair(SDLK_LEFT, LEFT));
62 keymap.insert(std::make_pair(SDLK_RIGHT, RIGHT));
63 keymap.insert(std::make_pair(SDLK_UP, UP));
64 keymap.insert(std::make_pair(SDLK_DOWN, DOWN));
65 keymap.insert(std::make_pair(SDLK_SPACE, JUMP));
66 keymap.insert(std::make_pair(SDLK_LCTRL, ACTION));
67 keymap.insert(std::make_pair(SDLK_LALT, ACTION));
68 keymap.insert(std::make_pair(SDLK_ESCAPE, PAUSE_MENU));
69 keymap.insert(std::make_pair(SDLK_p, PAUSE_MENU));
70 keymap.insert(std::make_pair(SDLK_PAUSE, PAUSE_MENU));
71 keymap.insert(std::make_pair(SDLK_RETURN, MENU_SELECT));
72 keymap.insert(std::make_pair(SDLK_KP_ENTER, MENU_SELECT));
74 int joystick_count = SDL_NumJoysticks();
77 for(int i = 0; i < joystick_count; ++i) {
78 SDL_Joystick* joystick = SDL_JoystickOpen(i);
80 if(SDL_JoystickNumButtons(joystick) < 2) {
81 std::cerr << "Joystick " << i << " has less than 2 buttons.\n";
84 if(SDL_JoystickNumAxes(joystick) < 2
85 && SDL_JoystickNumHats(joystick) == 0) {
86 std::cerr << "Joystick " << i << " has less than 2 axes and no hat.\n";
90 SDL_JoystickClose(joystick);
94 if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons)
95 min_joybuttons = SDL_JoystickNumButtons(joystick);
96 if(SDL_JoystickNumButtons(joystick) > max_joybuttons) {
97 max_joybuttons = SDL_JoystickNumButtons(joystick);
100 joysticks.push_back(joystick);
109 joy_button_map.insert(std::make_pair(0, JUMP));
110 joy_button_map.insert(std::make_pair(1, ACTION));
111 // map the last 2 buttons to menu and pause
112 if(min_joybuttons > 2)
113 joy_button_map.insert(std::make_pair(min_joybuttons-1, PAUSE_MENU));
114 // map all remaining joystick buttons to MENU_SELECT
115 for(int i = 2; i < max_joybuttons; ++i) {
116 if(i != min_joybuttons-1)
117 joy_button_map.insert(std::make_pair(i, MENU_SELECT));
121 JoystickKeyboardController::~JoystickKeyboardController()
123 for(std::vector<SDL_Joystick*>::iterator i = joysticks.begin();
124 i != joysticks.end(); ++i) {
126 SDL_JoystickClose(*i);
129 delete key_options_menu;
130 delete joystick_options_menu;
134 JoystickKeyboardController::read(const lisp::Lisp& lisp)
136 const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
139 lisp::ListIterator iter(keymap_lisp);
141 if(iter.item() == "map") {
144 const lisp::Lisp* map = iter.lisp();
145 map->get("key", key);
146 map->get("control", control);
147 if(key < SDLK_FIRST || key >= SDLK_LAST) {
148 std::cerr << "Invalid key '" << key << "' in keymap.\n";
153 for(i = 0; controlNames[i] != 0; ++i) {
154 if(control == controlNames[i])
157 if(controlNames[i] == 0) {
158 std::cerr << "Invalid control '" << control << "' in keymap.\n";
161 keymap.insert(std::make_pair((SDLKey) key, (Control) i));
163 std::cerr << "Invalid lisp element '" << iter.item() << "' in keymap.\n";
168 const lisp::Lisp* joystick_lisp = lisp.get_lisp("joystick");
170 joystick_lisp->get("use_hat", use_hat);
171 joystick_lisp->get("axis_x", joyaxis_x);
172 joystick_lisp->get("axis_y", joyaxis_y);
173 joystick_lisp->get("dead_zone_x", dead_zone_x);
174 joystick_lisp->get("dead_zone_y", dead_zone_y);
175 lisp::ListIterator iter(joystick_lisp);
177 if(iter.item() == "map") {
180 const lisp::Lisp* map = iter.lisp();
181 map->get("button", button);
182 map->get("control", control);
183 if(button < 0 || button >= max_joybuttons) {
184 std::cerr << "Invalid button '" << button << "' in buttonmap.\n";
189 for(i = 0; controlNames[i] != 0; ++i) {
190 if(control == controlNames[i])
193 if(controlNames[i] == 0) {
194 std::cerr << "Invalid control '" << control << "' in buttonmap.\n";
197 reset_joybutton(button, (Control) i);
204 JoystickKeyboardController::write(lisp::Writer& writer)
206 writer.start_list("keymap");
207 for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
208 writer.start_list("map");
209 writer.write_int("key", (int) i->first);
210 writer.write_string("control", controlNames[i->second]);
211 writer.end_list("map");
213 writer.end_list("keymap");
214 writer.start_list("joystick");
215 writer.write_bool("use_hat", use_hat);
216 writer.write_int("axis_x", joyaxis_x);
217 writer.write_int("axis_y", joyaxis_y);
218 writer.write_int("dead_zone_x", dead_zone_x);
219 writer.write_int("dead_zone_y", dead_zone_y);
220 for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end();
222 writer.start_list("map");
223 writer.write_int("button", i->first);
224 writer.write_string("control", controlNames[i->second]);
225 writer.end_list("map");
227 writer.end_list("joystick");
231 JoystickKeyboardController::process_event(const SDL_Event& event)
236 // remember ascii keys for cheat codes...
237 if(event.type == SDL_KEYDOWN &&
238 (event.key.keysym.unicode & 0xFF80) == 0) {
239 memmove(last_keys, last_keys+1, sizeof(last_keys)-1);
240 last_keys[sizeof(last_keys)-1] = event.key.keysym.unicode;
241 if(GameSession::current())
242 GameSession::current()->try_cheats();
246 if(Menu::current()) { // menu mode
247 process_menu_key_event(event);
250 // normal mode, find key in keymap
251 KeyMap::iterator i = keymap.find(event.key.keysym.sym);
252 if(i == keymap.end()) {
254 std::cerr << "Pressed key without mapping.\n";
258 Control control = i->second;
259 controls[control] = event.type == SDL_KEYDOWN ? true : false;
263 case SDL_JOYAXISMOTION:
264 if(event.jaxis.axis == joyaxis_x) {
265 if(event.jaxis.value < -dead_zone_x) {
266 controls[LEFT] = true;
267 controls[RIGHT] = false;
268 } else if(event.jaxis.value > dead_zone_x) {
269 controls[LEFT] = false;
270 controls[RIGHT] = true;
272 controls[LEFT] = false;
273 controls[RIGHT] = false;
275 } else if(event.jaxis.axis == joyaxis_y) {
276 if(event.jaxis.value < -dead_zone_y) {
278 controls[DOWN] = false;
279 } else if(event.jaxis.value > dead_zone_y) {
280 controls[UP] = false;
281 controls[DOWN] = true;
283 controls[UP] = false;
284 controls[DOWN] = false;
289 case SDL_JOYHATMOTION:
293 if(event.jhat.value & SDL_HAT_UP) {
295 controls[DOWN] = false;
297 if(event.jhat.value & SDL_HAT_DOWN) {
298 controls[UP] = false;
299 controls[DOWN] = true;
301 if(event.jhat.value & SDL_HAT_LEFT) {
302 controls[LEFT] = true;
303 controls[RIGHT] = false;
305 if(event.jhat.value & SDL_HAT_RIGHT) {
306 controls[LEFT] = false;
307 controls[RIGHT] = true;
309 if(event.jhat.value == SDL_HAT_CENTERED) {
310 controls[UP] = false;
311 controls[DOWN] = false;
312 controls[LEFT] = false;
313 controls[RIGHT] = false;
317 case SDL_JOYBUTTONDOWN:
318 case SDL_JOYBUTTONUP:
320 if(wait_for_joybutton >= 0) {
321 if(event.type == SDL_JOYBUTTONUP)
324 Control c = (Control) wait_for_joybutton;
325 reset_joybutton(event.jbutton.button, c);
327 joystick_options_menu->update();
328 wait_for_joybutton = -1;
332 ButtonMap::iterator i = joy_button_map.find(event.jbutton.button);
333 if(i == joy_button_map.end()) {
335 std::cerr << "Unmapped joybutton " << (int) event.jbutton.button
341 controls[i->second] =
342 event.type == SDL_JOYBUTTONDOWN ? true : false;
352 JoystickKeyboardController::process_menu_key_event(const SDL_Event& event)
354 // wait for key mode?
355 if(wait_for_key >= 0) {
356 if(event.type == SDL_KEYUP)
359 if(event.key.keysym.sym != SDLK_ESCAPE
360 && event.key.keysym.sym != SDLK_PAUSE) {
361 reset_key(event.key.keysym.sym, (Control) wait_for_key);
364 key_options_menu->update();
368 if(wait_for_joybutton >= 0) {
369 if(event.key.keysym.sym == SDLK_ESCAPE) {
371 joystick_options_menu->update();
372 wait_for_joybutton = -1;
378 /* we use default keys when the menu is open (to avoid problems when
379 * redefining keys to invalid settings
381 switch(event.key.keysym.sym) {
397 control = MENU_SELECT;
401 control = PAUSE_MENU;
408 controls[control] = event.type == SDL_KEYDOWN ? true : false;
412 JoystickKeyboardController::reset_joybutton(int button, Control control)
414 // remove all previous mappings for that control and for that key
415 for(ButtonMap::iterator i = joy_button_map.begin();
416 i != joy_button_map.end(); /* no ++i */) {
417 if(i->second == control) {
418 ButtonMap::iterator e = i;
420 joy_button_map.erase(e);
425 ButtonMap::iterator i = joy_button_map.find(button);
426 if(i != joy_button_map.end())
427 joy_button_map.erase(i);
430 joy_button_map.insert(std::make_pair(button, control));
432 // map all unused buttons to MENU_SELECT
433 for(int b = 0; b < max_joybuttons; ++b) {
434 ButtonMap::iterator i = joy_button_map.find(b);
435 if(i != joy_button_map.end())
438 joy_button_map.insert(std::make_pair(b, MENU_SELECT));
443 JoystickKeyboardController::reset_key(SDLKey key, Control control)
445 // remove all previous mappings for that control and for that key
446 for(KeyMap::iterator i = keymap.begin();
447 i != keymap.end(); /* no ++i */) {
448 if(i->second == control) {
449 KeyMap::iterator e = i;
456 KeyMap::iterator i = keymap.find(key);
457 if(i != keymap.end())
461 keymap.insert(std::make_pair(key, control));
465 JoystickKeyboardController::reversemap_key(Control c)
467 for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
476 JoystickKeyboardController::reversemap_joybutton(Control c)
478 for(ButtonMap::iterator i = joy_button_map.begin();
479 i != joy_button_map.end(); ++i) {
488 JoystickKeyboardController::get_key_options_menu()
490 if(key_options_menu == 0) {
491 key_options_menu = new KeyboardMenu(this);
494 return key_options_menu;
498 JoystickKeyboardController::get_joystick_options_menu()
500 if(joystick_options_menu == 0) {
501 joystick_options_menu = new JoystickMenu(this);
504 return joystick_options_menu;
508 JoystickKeyboardController::check_cheatcode(const std::string& cheatcode)
510 if(cheatcode.size() > sizeof(last_keys)) {
512 std::cerr << "Cheat Code too long.\n";
517 for(size_t i = 0; i < cheatcode.size(); ++i) {
518 if(last_keys[sizeof(last_keys)-1 - i] != cheatcode[cheatcode.size()-1-i])
524 //----------------------------------------------------------------------------
526 JoystickKeyboardController::KeyboardMenu::KeyboardMenu(
527 JoystickKeyboardController* _controller)
528 : controller(_controller)
530 add_label(_("Keyboard Setup"));
532 add_controlfield(Controller::UP, _("Up"));
533 add_controlfield(Controller::DOWN, _("Down"));
534 add_controlfield(Controller::LEFT, _("Left"));
535 add_controlfield(Controller::RIGHT, _("Right"));
536 add_controlfield(Controller::JUMP, _("Jump"));
537 add_controlfield(Controller::ACTION, _("Shoot/Run"));
543 JoystickKeyboardController::KeyboardMenu::~KeyboardMenu()
547 JoystickKeyboardController::KeyboardMenu::get_key_name(SDLKey key)
553 return _("Up cursor");
555 return _("Down cursor");
557 return _("Left cursor");
559 return _("Right cursor");
565 return _("Right Shift");
567 return _("Left Shift");
569 return _("Right Control");
571 return _("Left Control");
573 return _("Right Alt");
575 return _("Left Alt");
577 return SDL_GetKeyName((SDLKey) key);
582 JoystickKeyboardController::KeyboardMenu::menu_action(MenuItem* item)
584 assert(item->id >= 0 && item->id < Controller::CONTROLCOUNT);
585 item->change_input(_("Press Key"));
586 controller->wait_for_key = item->id;
590 JoystickKeyboardController::KeyboardMenu::update()
593 get_item_by_id((int) Controller::UP).change_input(get_key_name(
594 controller->reversemap_key(Controller::UP)));
595 get_item_by_id((int) Controller::DOWN).change_input(get_key_name(
596 controller->reversemap_key(Controller::DOWN)));
597 get_item_by_id((int) Controller::LEFT).change_input(get_key_name(
598 controller->reversemap_key(Controller::LEFT)));
599 get_item_by_id((int) Controller::RIGHT).change_input(get_key_name(
600 controller->reversemap_key(Controller::RIGHT)));
601 get_item_by_id((int) Controller::JUMP).change_input(get_key_name(
602 controller->reversemap_key(Controller::JUMP)));
603 get_item_by_id((int) Controller::ACTION).change_input(get_key_name(
604 controller->reversemap_key(Controller::ACTION)));
607 //---------------------------------------------------------------------------
609 JoystickKeyboardController::JoystickMenu::JoystickMenu(
610 JoystickKeyboardController* _controller)
611 : controller(_controller)
613 add_label(_("Joystick Setup"));
615 if(controller->joysticks.size() > 0) {
616 add_controlfield(Controller::JUMP, _("Jump"));
617 add_controlfield(Controller::ACTION, _("Shoot/Run"));
618 add_controlfield(Controller::PAUSE_MENU, _("Pause/Menu"));
620 add_deactive(-1, _("No Joysticks found"));
627 JoystickKeyboardController::JoystickMenu::~JoystickMenu()
631 JoystickKeyboardController::JoystickMenu::get_button_name(int button)
636 std::ostringstream name;
637 name << "Button " << button;
642 JoystickKeyboardController::JoystickMenu::menu_action(MenuItem* item)
644 assert(item->id >= 0 && item->id < Controller::CONTROLCOUNT);
645 item->change_input(_("Press Button"));
646 controller->wait_for_joybutton = item->id;
650 JoystickKeyboardController::JoystickMenu::update()
652 if(controller->joysticks.size() == 0)
656 get_item_by_id((int) Controller::JUMP).change_input(get_button_name(
657 controller->reversemap_joybutton(Controller::JUMP)));
658 get_item_by_id((int) Controller::ACTION).change_input(get_button_name(
659 controller->reversemap_joybutton(Controller::ACTION)));
660 get_item_by_id((int) Controller::PAUSE_MENU).change_input(get_button_name(
661 controller->reversemap_joybutton(Controller::PAUSE_MENU)));