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.hpp"
25 #include "gui/menu.hpp"
26 #include "gettext.hpp"
27 #include "lisp/lisp.hpp"
28 #include "lisp/list_iterator.hpp"
29 #include "game_session.hpp"
31 class JoystickKeyboardController::JoystickMenu : public Menu
34 JoystickMenu(JoystickKeyboardController* controller);
35 virtual ~JoystickMenu();
38 std::string get_button_name(int button);
39 virtual void menu_action(MenuItem* item);
40 JoystickKeyboardController* controller;
43 class JoystickKeyboardController::KeyboardMenu : public Menu
46 KeyboardMenu(JoystickKeyboardController* controller);
50 std::string get_key_name(SDLKey key);
51 virtual void menu_action(MenuItem* item);
52 JoystickKeyboardController* controller;
55 JoystickKeyboardController::JoystickKeyboardController()
56 : wait_for_key(-1), wait_for_joybutton(-1), key_options_menu(0),
57 joystick_options_menu(0)
59 memset(last_keys, 0, sizeof(last_keys));
61 // initialize default keyboard map
62 keymap.insert(std::make_pair(SDLK_LEFT, LEFT));
63 keymap.insert(std::make_pair(SDLK_RIGHT, RIGHT));
64 keymap.insert(std::make_pair(SDLK_UP, UP));
65 keymap.insert(std::make_pair(SDLK_DOWN, DOWN));
66 keymap.insert(std::make_pair(SDLK_SPACE, JUMP));
67 keymap.insert(std::make_pair(SDLK_LCTRL, ACTION));
68 keymap.insert(std::make_pair(SDLK_LALT, ACTION));
69 keymap.insert(std::make_pair(SDLK_ESCAPE, PAUSE_MENU));
70 keymap.insert(std::make_pair(SDLK_p, PAUSE_MENU));
71 keymap.insert(std::make_pair(SDLK_PAUSE, PAUSE_MENU));
72 keymap.insert(std::make_pair(SDLK_RETURN, MENU_SELECT));
73 keymap.insert(std::make_pair(SDLK_KP_ENTER, MENU_SELECT));
75 int joystick_count = SDL_NumJoysticks();
78 for(int i = 0; i < joystick_count; ++i) {
79 SDL_Joystick* joystick = SDL_JoystickOpen(i);
81 if(SDL_JoystickNumButtons(joystick) < 2) {
82 msg_warning("Joystick " << i << " has less than 2 buttons");
85 if(SDL_JoystickNumAxes(joystick) < 2
86 && SDL_JoystickNumHats(joystick) == 0) {
87 msg_warning("Joystick " << i << " has less than 2 axes and no hat");
91 SDL_JoystickClose(joystick);
95 if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons)
96 min_joybuttons = SDL_JoystickNumButtons(joystick);
97 if(SDL_JoystickNumButtons(joystick) > max_joybuttons) {
98 max_joybuttons = SDL_JoystickNumButtons(joystick);
101 joysticks.push_back(joystick);
110 joy_button_map.insert(std::make_pair(0, JUMP));
111 joy_button_map.insert(std::make_pair(1, ACTION));
112 // map the last 2 buttons to menu and pause
113 if(min_joybuttons > 2)
114 joy_button_map.insert(std::make_pair(min_joybuttons-1, PAUSE_MENU));
115 // map all remaining joystick buttons to MENU_SELECT
116 for(int i = 2; i < max_joybuttons; ++i) {
117 if(i != min_joybuttons-1)
118 joy_button_map.insert(std::make_pair(i, MENU_SELECT));
121 // some joysticks or SDL seem to produce some bogus events after being opened
122 Uint32 ticks = SDL_GetTicks();
123 while(SDL_GetTicks() - ticks < 200) {
125 SDL_PollEvent(&event);
129 JoystickKeyboardController::~JoystickKeyboardController()
131 for(std::vector<SDL_Joystick*>::iterator i = joysticks.begin();
132 i != joysticks.end(); ++i) {
134 SDL_JoystickClose(*i);
137 delete key_options_menu;
138 delete joystick_options_menu;
142 JoystickKeyboardController::read(const lisp::Lisp& lisp)
144 const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
147 lisp::ListIterator iter(keymap_lisp);
149 if(iter.item() == "map") {
152 const lisp::Lisp* map = iter.lisp();
153 map->get("key", key);
154 map->get("control", control);
155 if(key < SDLK_FIRST || key >= SDLK_LAST) {
156 msg_warning("Invalid key '" << key << "' in keymap");
161 for(i = 0; controlNames[i] != 0; ++i) {
162 if(control == controlNames[i])
165 if(controlNames[i] == 0) {
166 msg_warning("Invalid control '" << control << "' in keymap");
169 keymap.insert(std::make_pair((SDLKey) key, (Control) i));
171 msg_warning("Invalid lisp element '" << iter.item() << "' in keymap");
176 const lisp::Lisp* joystick_lisp = lisp.get_lisp("joystick");
178 joystick_lisp->get("use_hat", use_hat);
179 joystick_lisp->get("axis_x", joyaxis_x);
180 joystick_lisp->get("axis_y", joyaxis_y);
181 joystick_lisp->get("dead_zone_x", dead_zone_x);
182 joystick_lisp->get("dead_zone_y", dead_zone_y);
183 lisp::ListIterator iter(joystick_lisp);
185 if(iter.item() == "map") {
188 const lisp::Lisp* map = iter.lisp();
189 map->get("button", button);
190 map->get("control", control);
191 if(button < 0 || button >= max_joybuttons) {
192 msg_warning("Invalid button '" << button << "' in buttonmap");
197 for(i = 0; controlNames[i] != 0; ++i) {
198 if(control == controlNames[i])
201 if(controlNames[i] == 0) {
202 msg_warning("Invalid control '" << control << "' in buttonmap");
205 reset_joybutton(button, (Control) i);
212 JoystickKeyboardController::write(lisp::Writer& writer)
214 writer.start_list("keymap");
215 for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
216 writer.start_list("map");
217 writer.write_int("key", (int) i->first);
218 writer.write_string("control", controlNames[i->second]);
219 writer.end_list("map");
221 writer.end_list("keymap");
222 writer.start_list("joystick");
223 writer.write_bool("use_hat", use_hat);
224 writer.write_int("axis_x", joyaxis_x);
225 writer.write_int("axis_y", joyaxis_y);
226 writer.write_int("dead_zone_x", dead_zone_x);
227 writer.write_int("dead_zone_y", dead_zone_y);
228 for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end();
230 writer.start_list("map");
231 writer.write_int("button", i->first);
232 writer.write_string("control", controlNames[i->second]);
233 writer.end_list("map");
235 writer.end_list("joystick");
239 JoystickKeyboardController::reset()
242 for(size_t i = 0; i < sizeof(last_keys); ++i)
247 JoystickKeyboardController::process_event(const SDL_Event& event)
252 // remember ascii keys for cheat codes...
253 if(event.type == SDL_KEYDOWN &&
254 (event.key.keysym.unicode & 0xFF80) == 0) {
255 memmove(last_keys, last_keys+1, sizeof(last_keys)-1);
256 last_keys[sizeof(last_keys)-1] = event.key.keysym.unicode;
257 if(GameSession::current() != NULL)
258 GameSession::current()->try_cheats();
262 if(Menu::current()) { // menu mode
263 process_menu_key_event(event);
266 // normal mode, find key in keymap
267 KeyMap::iterator i = keymap.find(event.key.keysym.sym);
268 if(i == keymap.end()) {
269 msg_debug("Pressed key without mapping");
272 Control control = i->second;
273 controls[control] = event.type == SDL_KEYDOWN ? true : false;
277 case SDL_JOYAXISMOTION:
278 if(event.jaxis.axis == joyaxis_x) {
279 if(event.jaxis.value < -dead_zone_x) {
280 controls[LEFT] = true;
281 controls[RIGHT] = false;
282 } else if(event.jaxis.value > dead_zone_x) {
283 controls[LEFT] = false;
284 controls[RIGHT] = true;
286 controls[LEFT] = false;
287 controls[RIGHT] = false;
289 } else if(event.jaxis.axis == joyaxis_y) {
290 if(event.jaxis.value < -dead_zone_y) {
292 controls[DOWN] = false;
293 } else if(event.jaxis.value > dead_zone_y) {
294 controls[UP] = false;
295 controls[DOWN] = true;
297 controls[UP] = false;
298 controls[DOWN] = false;
303 case SDL_JOYHATMOTION:
307 if(event.jhat.value & SDL_HAT_UP) {
309 controls[DOWN] = false;
311 if(event.jhat.value & SDL_HAT_DOWN) {
312 controls[UP] = false;
313 controls[DOWN] = true;
315 if(event.jhat.value & SDL_HAT_LEFT) {
316 controls[LEFT] = true;
317 controls[RIGHT] = false;
319 if(event.jhat.value & SDL_HAT_RIGHT) {
320 controls[LEFT] = false;
321 controls[RIGHT] = true;
323 if(event.jhat.value == SDL_HAT_CENTERED) {
324 controls[UP] = false;
325 controls[DOWN] = false;
326 controls[LEFT] = false;
327 controls[RIGHT] = false;
331 case SDL_JOYBUTTONDOWN:
332 case SDL_JOYBUTTONUP:
334 if(wait_for_joybutton >= 0) {
335 if(event.type == SDL_JOYBUTTONUP)
338 Control c = (Control) wait_for_joybutton;
339 reset_joybutton(event.jbutton.button, c);
341 joystick_options_menu->update();
342 wait_for_joybutton = -1;
346 ButtonMap::iterator i = joy_button_map.find(event.jbutton.button);
347 if(i == joy_button_map.end()) {
348 msg_debug("Unmapped joybutton " << (int) event.jbutton.button
353 controls[i->second] =
354 event.type == SDL_JOYBUTTONDOWN ? true : false;
364 JoystickKeyboardController::process_menu_key_event(const SDL_Event& event)
366 // wait for key mode?
367 if(wait_for_key >= 0) {
368 if(event.type == SDL_KEYUP)
371 if(event.key.keysym.sym != SDLK_ESCAPE
372 && event.key.keysym.sym != SDLK_PAUSE) {
373 reset_key(event.key.keysym.sym, (Control) wait_for_key);
376 key_options_menu->update();
380 if(wait_for_joybutton >= 0) {
381 if(event.key.keysym.sym == SDLK_ESCAPE) {
383 joystick_options_menu->update();
384 wait_for_joybutton = -1;
390 /* we use default keys when the menu is open (to avoid problems when
391 * redefining keys to invalid settings
393 switch(event.key.keysym.sym) {
409 control = MENU_SELECT;
413 control = PAUSE_MENU;
420 controls[control] = event.type == SDL_KEYDOWN ? true : false;
424 JoystickKeyboardController::reset_joybutton(int button, Control control)
426 // remove all previous mappings for that control and for that key
427 for(ButtonMap::iterator i = joy_button_map.begin();
428 i != joy_button_map.end(); /* no ++i */) {
429 if(i->second == control) {
430 ButtonMap::iterator e = i;
432 joy_button_map.erase(e);
437 ButtonMap::iterator i = joy_button_map.find(button);
438 if(i != joy_button_map.end())
439 joy_button_map.erase(i);
442 joy_button_map.insert(std::make_pair(button, control));
444 // map all unused buttons to MENU_SELECT
445 for(int b = 0; b < max_joybuttons; ++b) {
446 ButtonMap::iterator i = joy_button_map.find(b);
447 if(i != joy_button_map.end())
450 joy_button_map.insert(std::make_pair(b, MENU_SELECT));
455 JoystickKeyboardController::reset_key(SDLKey key, Control control)
457 // remove all previous mappings for that control and for that key
458 for(KeyMap::iterator i = keymap.begin();
459 i != keymap.end(); /* no ++i */) {
460 if(i->second == control) {
461 KeyMap::iterator e = i;
468 KeyMap::iterator i = keymap.find(key);
469 if(i != keymap.end())
473 keymap.insert(std::make_pair(key, control));
477 JoystickKeyboardController::reversemap_key(Control c)
479 for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
488 JoystickKeyboardController::reversemap_joybutton(Control c)
490 for(ButtonMap::iterator i = joy_button_map.begin();
491 i != joy_button_map.end(); ++i) {
500 JoystickKeyboardController::get_key_options_menu()
502 if(key_options_menu == 0) {
503 key_options_menu = new KeyboardMenu(this);
506 return key_options_menu;
510 JoystickKeyboardController::get_joystick_options_menu()
512 if(joystick_options_menu == 0) {
513 joystick_options_menu = new JoystickMenu(this);
516 return joystick_options_menu;
520 JoystickKeyboardController::check_cheatcode(const std::string& cheatcode)
522 if(cheatcode.size() > sizeof(last_keys)) {
523 msg_debug("Cheat Code too long");
527 for(size_t i = 0; i < cheatcode.size(); ++i) {
528 if(last_keys[sizeof(last_keys)-1 - i] != cheatcode[cheatcode.size()-1-i])
534 //----------------------------------------------------------------------------
536 JoystickKeyboardController::KeyboardMenu::KeyboardMenu(
537 JoystickKeyboardController* _controller)
538 : controller(_controller)
540 add_label(_("Keyboard Setup"));
542 add_controlfield(Controller::UP, _("Up"));
543 add_controlfield(Controller::DOWN, _("Down"));
544 add_controlfield(Controller::LEFT, _("Left"));
545 add_controlfield(Controller::RIGHT, _("Right"));
546 add_controlfield(Controller::JUMP, _("Jump"));
547 add_controlfield(Controller::ACTION, _("Shoot/Run"));
553 JoystickKeyboardController::KeyboardMenu::~KeyboardMenu()
557 JoystickKeyboardController::KeyboardMenu::get_key_name(SDLKey key)
563 return _("Up cursor");
565 return _("Down cursor");
567 return _("Left cursor");
569 return _("Right cursor");
575 return _("Right Shift");
577 return _("Left Shift");
579 return _("Right Control");
581 return _("Left Control");
583 return _("Right Alt");
585 return _("Left Alt");
587 return SDL_GetKeyName((SDLKey) key);
592 JoystickKeyboardController::KeyboardMenu::menu_action(MenuItem* item)
594 assert(item->id >= 0 && item->id < Controller::CONTROLCOUNT);
595 item->change_input(_("Press Key"));
596 controller->wait_for_key = item->id;
600 JoystickKeyboardController::KeyboardMenu::update()
603 get_item_by_id((int) Controller::UP).change_input(get_key_name(
604 controller->reversemap_key(Controller::UP)));
605 get_item_by_id((int) Controller::DOWN).change_input(get_key_name(
606 controller->reversemap_key(Controller::DOWN)));
607 get_item_by_id((int) Controller::LEFT).change_input(get_key_name(
608 controller->reversemap_key(Controller::LEFT)));
609 get_item_by_id((int) Controller::RIGHT).change_input(get_key_name(
610 controller->reversemap_key(Controller::RIGHT)));
611 get_item_by_id((int) Controller::JUMP).change_input(get_key_name(
612 controller->reversemap_key(Controller::JUMP)));
613 get_item_by_id((int) Controller::ACTION).change_input(get_key_name(
614 controller->reversemap_key(Controller::ACTION)));
617 //---------------------------------------------------------------------------
619 JoystickKeyboardController::JoystickMenu::JoystickMenu(
620 JoystickKeyboardController* _controller)
621 : controller(_controller)
623 add_label(_("Joystick Setup"));
625 if(controller->joysticks.size() > 0) {
626 add_controlfield(Controller::JUMP, _("Jump"));
627 add_controlfield(Controller::ACTION, _("Shoot/Run"));
628 add_controlfield(Controller::PAUSE_MENU, _("Pause/Menu"));
630 add_deactive(-1, _("No Joysticks found"));
637 JoystickKeyboardController::JoystickMenu::~JoystickMenu()
641 JoystickKeyboardController::JoystickMenu::get_button_name(int button)
646 std::ostringstream name;
647 name << "Button " << button;
652 JoystickKeyboardController::JoystickMenu::menu_action(MenuItem* item)
654 assert(item->id >= 0 && item->id < Controller::CONTROLCOUNT);
655 item->change_input(_("Press Button"));
656 controller->wait_for_joybutton = item->id;
660 JoystickKeyboardController::JoystickMenu::update()
662 if(controller->joysticks.size() == 0)
666 get_item_by_id((int) Controller::JUMP).change_input(get_button_name(
667 controller->reversemap_joybutton(Controller::JUMP)));
668 get_item_by_id((int) Controller::ACTION).change_input(get_button_name(
669 controller->reversemap_joybutton(Controller::ACTION)));
670 get_item_by_id((int) Controller::PAUSE_MENU).change_input(get_button_name(
671 controller->reversemap_joybutton(Controller::PAUSE_MENU)));