Some minor code cleanup resulting from a user running code through cppcheck, mostly...
[supertux.git] / src / control / joystickkeyboardcontroller.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
3 //                2007 Ingo Ruhnke <grumbel@gmx.de>
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 3 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, see <http://www.gnu.org/licenses/>.
17
18 #include "control/joystickkeyboardcontroller.hpp"
19
20 #include <iostream>
21
22 #include "lisp/list_iterator.hpp"
23 #include "gui/menu_manager.hpp"
24 #include "supertux/console.hpp"
25 #include "supertux/gameconfig.hpp"
26 #include "supertux/menu/menu_storage.hpp"
27 #include "supertux/menu/joystick_menu.hpp"
28 #include "supertux/menu/keyboard_menu.hpp"
29 #include "util/gettext.hpp"
30 #include "util/writer.hpp"
31 //#include <SDL_keycode.h> // add by giby
32
33 JoystickKeyboardController::JoystickKeyboardController() :
34   controller(),
35   keymap(),
36   joy_button_map(),
37   joy_axis_map(),
38   joy_hat_map(),
39   joysticks(),
40   name(),
41   dead_zone(),
42   min_joybuttons(),
43   max_joybuttons(),
44   max_joyaxis(),
45   max_joyhats(),
46   hat_state(0),
47   jump_with_up_joy(),
48   jump_with_up_kbd(),
49   wait_for_key(-1), 
50   wait_for_joystick(-1)
51 {
52   controller = new Controller;
53
54   // initialize default keyboard map
55   keymap[SDLK_LEFT]     = Controller::LEFT;
56   keymap[SDLK_RIGHT]    = Controller::RIGHT;
57   keymap[SDLK_UP]       = Controller::UP;
58   keymap[SDLK_DOWN]     = Controller::DOWN;
59   keymap[SDLK_SPACE]    = Controller::JUMP;
60   keymap[SDLK_LCTRL]    = Controller::ACTION;
61   keymap[SDLK_LALT]     = Controller::ACTION;
62   keymap[SDLK_ESCAPE]   = Controller::PAUSE_MENU;
63   keymap[SDLK_p]        = Controller::PAUSE_MENU;
64   keymap[SDLK_PAUSE]    = Controller::PAUSE_MENU;
65   keymap[SDLK_RETURN]   = Controller::MENU_SELECT;
66   keymap[SDLK_KP_ENTER] = Controller::MENU_SELECT;
67   keymap[SDLK_CARET]    = Controller::CONSOLE;
68   keymap[SDLK_DELETE]   = Controller::PEEK_LEFT;
69   keymap[SDLK_PAGEDOWN] = Controller::PEEK_RIGHT;
70   keymap[SDLK_HOME]     = Controller::PEEK_UP;
71   keymap[SDLK_END]      = Controller::PEEK_DOWN;
72
73   jump_with_up_joy = false;
74   jump_with_up_kbd = false;
75
76   updateAvailableJoysticks();
77
78   dead_zone = 1000;
79
80   // Default joystick button configuration
81   bind_joybutton(0, 0, Controller::JUMP);
82   bind_joybutton(0, 1, Controller::ACTION);
83   // 6 or more Buttons
84   if( min_joybuttons > 5 ){
85     bind_joybutton(0, 4, Controller::PEEK_LEFT);
86     bind_joybutton(0, 5, Controller::PEEK_RIGHT);
87     // 8 or more
88     if(min_joybuttons > 7)
89       bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU);
90   } else {
91     // map the last 2 buttons to menu and pause
92     if(min_joybuttons > 2)
93       bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU);
94     // map all remaining joystick buttons to MENU_SELECT
95     for(int i = 2; i < max_joybuttons; ++i) {
96       if(i != min_joybuttons-1)
97         bind_joybutton(0, i, Controller::MENU_SELECT);
98     }
99   }
100
101   // Default joystick axis configuration
102   bind_joyaxis(0, -1, Controller::LEFT);
103   bind_joyaxis(0, 1, Controller::RIGHT);
104   bind_joyaxis(0, -2, Controller::UP);
105   bind_joyaxis(0, 2, Controller::DOWN);
106 }
107
108 JoystickKeyboardController::~JoystickKeyboardController()
109 {
110   for(std::vector<SDL_Joystick*>::iterator i = joysticks.begin();
111       i != joysticks.end(); ++i) {
112     if(*i != 0)
113       SDL_JoystickClose(*i);
114   }
115   delete controller;
116 }
117
118 void
119 JoystickKeyboardController::updateAvailableJoysticks()
120 {
121   for(std::vector<SDL_Joystick*>::iterator i = joysticks.begin();
122       i != joysticks.end(); ++i) {
123     if(*i != 0)
124       SDL_JoystickClose(*i);
125   }
126   joysticks.clear();
127   
128   SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
129   SDL_InitSubSystem(SDL_INIT_JOYSTICK);
130
131   int joystick_count = SDL_NumJoysticks();
132   min_joybuttons = -1;
133   max_joybuttons = -1;
134   max_joyaxis    = -1;
135   max_joyhats    = -1;
136
137   if( joystick_count > 0 ){
138     for(int i = 0; i < joystick_count; ++i) {
139       SDL_Joystick* joystick = SDL_JoystickOpen(i);
140       bool good = true;
141       if(SDL_JoystickNumButtons(joystick) < 2) {
142         log_info << _("Joystick ") << i << ": " << SDL_JoystickID(i) << _(" has less than 2 buttons") << std::endl;
143         good = false;
144       }
145       if(SDL_JoystickNumAxes(joystick) < 2
146          && SDL_JoystickNumHats(joystick) == 0) {
147         log_info << _("Joystick ") << i << ": " << SDL_JoystickID(i) << _(" has less than 2 axes and no hat") << std::endl;
148         good = false;
149       }
150       if(!good) {
151         SDL_JoystickClose(joystick);
152         continue;
153       }
154
155       if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons)
156         min_joybuttons = SDL_JoystickNumButtons(joystick);
157
158       if(SDL_JoystickNumButtons(joystick) > max_joybuttons)
159         max_joybuttons = SDL_JoystickNumButtons(joystick);
160
161       if(SDL_JoystickNumAxes(joystick) > max_joyaxis)
162         max_joyaxis = SDL_JoystickNumAxes(joystick);
163
164       if(SDL_JoystickNumHats(joystick) > max_joyhats)
165         max_joyhats = SDL_JoystickNumHats(joystick);
166
167       joysticks.push_back(joystick);
168     }
169   }
170
171   // some joysticks or SDL seem to produce some bogus events after being opened
172   Uint32 ticks = SDL_GetTicks();
173   while(SDL_GetTicks() - ticks < 200) {
174     SDL_Event event;
175     SDL_PollEvent(&event);
176   }
177 }
178
179 Controller*
180 JoystickKeyboardController::get_main_controller()
181 {
182   return controller;
183 }
184
185 void
186 JoystickKeyboardController::read(const Reader& lisp)
187 {
188   const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
189   if(keymap_lisp) {
190     keymap.clear();
191     keymap_lisp->get("jump-with-up", jump_with_up_kbd);
192     lisp::ListIterator iter(keymap_lisp);
193     while(iter.next()) {
194       if(iter.item() == "map") {
195         int key = -1;
196         std::string control;
197         const lisp::Lisp* map = iter.lisp();
198         map->get("key", key);
199         map->get("control", control);
200 //        if(key < SDLK_FIRST || key >= SDLK_LAST) {
201 //          log_info << "Invalid key '" << key << "' in keymap" << std::endl;
202 //          continue;
203 //        }
204
205         int i = 0;
206         for(i = 0; Controller::controlNames[i] != 0; ++i) {
207           if(control == Controller::controlNames[i])
208             break;
209         }
210         if(Controller::controlNames[i] == 0) {
211           log_info << "Invalid control '" << control << "' in keymap" << std::endl;
212           continue;
213         }
214         keymap[SDL_Keycode(key)] = Control(i);
215       }
216     }
217   }
218
219   const lisp::Lisp* joystick_lisp = lisp.get_lisp(_("joystick"));
220   if(joystick_lisp) {
221     joystick_lisp->get("dead-zone", dead_zone);
222     joystick_lisp->get("jump-with-up", jump_with_up_joy);
223     lisp::ListIterator iter(joystick_lisp);
224     while(iter.next()) {
225       if(iter.item() == _("map")) {
226         int button = -1;
227         int axis   = 0;
228         int hat    = -1;
229         std::string control;
230         const lisp::Lisp* map = iter.lisp();
231
232         map->get("control", control);
233         int i = 0;
234         for(i = 0; Controller::controlNames[i] != 0; ++i) {
235           if(control == Controller::controlNames[i])
236             break;
237         }
238         if(Controller::controlNames[i] == 0) {
239           log_info << "Invalid control '" << control << "' in buttonmap" << std::endl;
240           continue;
241         }
242
243         bool js_available = joysticks.size() > 0;
244
245         if (map->get("button", button)) {
246           if(js_available && (button < 0 || button >= max_joybuttons)) {
247             log_info << "Invalid button '" << button << "' in buttonmap" << std::endl;
248             continue;
249           }
250           bind_joybutton(0, button, Control(i));
251         }
252
253         if (map->get("axis",   axis)) {
254           if (js_available && (axis == 0 || abs(axis) > max_joyaxis)) {
255             log_info << "Invalid axis '" << axis << "' in axismap" << std::endl;
256             continue;
257           }
258           bind_joyaxis(0, axis, Control(i));
259         }
260
261         if (map->get("hat",   hat)) {
262           if (js_available        &&
263               hat != SDL_HAT_UP   &&
264               hat != SDL_HAT_DOWN &&
265               hat != SDL_HAT_LEFT &&
266               hat != SDL_HAT_RIGHT) {
267             log_info << "Invalid axis '" << axis << "' in axismap" << std::endl;
268             continue;
269           } else {
270             bind_joyhat(0, hat, Control(i));
271           }
272         }
273       }
274     }
275   }
276 }
277
278 void
279 JoystickKeyboardController::write(Writer& writer)
280 {
281   writer.start_list("keymap");
282   writer.write("jump-with-up", jump_with_up_kbd);
283   for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
284     writer.start_list("map");
285     writer.write("key", (int) i->first);
286     writer.write("control", Controller::controlNames[i->second]);
287     writer.end_list("map");
288   }
289   writer.end_list("keymap");
290
291   writer.start_list("joystick");
292   writer.write("dead-zone", dead_zone);
293   writer.write("jump-with-up", jump_with_up_joy);
294
295   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end();
296       ++i) {
297     writer.start_list("map");
298     writer.write("button", i->first.second);
299     writer.write("control", Controller::controlNames[i->second]);
300     writer.end_list("map");
301   }
302
303   for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) {
304     writer.start_list("map");
305     writer.write("hat", i->first.second);
306     writer.write("control", Controller::controlNames[i->second]);
307     writer.end_list("map");
308   }
309
310   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) {
311     writer.start_list("map");
312     writer.write("axis", i->first.second);
313     writer.write("control", Controller::controlNames[i->second]);
314     writer.end_list("map");
315   }
316
317   writer.end_list("joystick");
318 }
319
320 void
321 JoystickKeyboardController::update()
322 {
323   controller->update();
324 }
325
326 void
327 JoystickKeyboardController::reset()
328 {
329   controller->reset();
330 }
331
332 void
333 JoystickKeyboardController::set_joy_controls(Control id, bool value)
334 {
335   if (jump_with_up_joy && id == Controller::UP)
336     controller->set_control(Controller::JUMP, value);
337
338   controller->set_control(id, value);
339 }
340
341 void
342 JoystickKeyboardController::process_event(const SDL_Event& event)
343 {
344   switch(event.type) {
345     case SDL_KEYUP:
346     case SDL_KEYDOWN:
347       process_key_event(event.key);
348       break;
349
350     case SDL_JOYAXISMOTION:
351       process_axis_event(event.jaxis);
352       break;
353
354     case SDL_JOYHATMOTION:
355       process_hat_event(event.jhat);
356       break;
357
358     case SDL_JOYBUTTONDOWN:
359     case SDL_JOYBUTTONUP:
360       process_button_event(event.jbutton);
361       break;
362
363     default:
364       break;
365   }
366 }
367
368 void
369 JoystickKeyboardController::process_button_event(const SDL_JoyButtonEvent& jbutton)
370 {
371   if(wait_for_joystick >= 0) 
372   {
373     if(jbutton.state == SDL_PRESSED)
374     {
375       bind_joybutton(jbutton.which, jbutton.button, (Control)wait_for_joystick);
376       MenuStorage::get_joystick_options_menu()->update();
377       reset();
378       wait_for_joystick = -1;
379     }
380   } 
381   else 
382   {
383     ButtonMap::iterator i = joy_button_map.find(std::make_pair(jbutton.which, jbutton.button));
384     if(i == joy_button_map.end()) {
385       log_debug << "Unmapped joybutton " << (int)jbutton.button << " pressed" << std::endl;
386     } else {
387       set_joy_controls(i->second, (jbutton.state == SDL_PRESSED));
388     }
389   }
390 }
391
392 void
393 JoystickKeyboardController::process_axis_event(const SDL_JoyAxisEvent& jaxis)
394 {
395   if (wait_for_joystick >= 0)
396   {
397     if (abs(jaxis.value) > dead_zone) {
398       if (jaxis.value < 0)
399         bind_joyaxis(jaxis.which, -(jaxis.axis + 1), Control(wait_for_joystick));
400       else
401         bind_joyaxis(jaxis.which, jaxis.axis + 1, Control(wait_for_joystick));
402
403       MenuStorage::get_joystick_options_menu()->update();
404       wait_for_joystick = -1;
405     }
406   }
407   else
408   {
409     // Split the axis into left and right, so that both can be
410     // mapped separately (needed for jump/down vs up/down)
411     int axis = jaxis.axis + 1;
412
413     AxisMap::iterator left  = joy_axis_map.find(std::make_pair(jaxis.which, -axis));
414     AxisMap::iterator right = joy_axis_map.find(std::make_pair(jaxis.which, axis));
415
416     if(left == joy_axis_map.end()) {
417       // std::cout << "Unmapped joyaxis " << (int)jaxis.axis << " moved" << std::endl;
418     } else {
419       if (jaxis.value < -dead_zone)
420         set_joy_controls(left->second,  true);
421       else
422         set_joy_controls(left->second, false);
423     }
424
425     if(right == joy_axis_map.end()) {
426       // std::cout << "Unmapped joyaxis " << (int)jaxis.axis << " moved" << std::endl;
427     } else {
428       if (jaxis.value > dead_zone)
429         set_joy_controls(right->second, true);
430       else
431         set_joy_controls(right->second, false);
432     }
433   }
434 }
435
436 void
437 JoystickKeyboardController::process_hat_event(const SDL_JoyHatEvent& jhat)
438 {
439   Uint8 changed = hat_state ^ jhat.value;
440
441   if (wait_for_joystick >= 0)
442   {
443     if (changed & SDL_HAT_UP && jhat.value & SDL_HAT_UP)
444       bind_joyhat(jhat.which, SDL_HAT_UP, Control(wait_for_joystick));
445
446     if (changed & SDL_HAT_DOWN && jhat.value & SDL_HAT_DOWN)
447       bind_joyhat(jhat.which, SDL_HAT_DOWN, Control(wait_for_joystick));
448
449     if (changed & SDL_HAT_LEFT && jhat.value & SDL_HAT_LEFT)
450       bind_joyhat(jhat.which, SDL_HAT_LEFT, Control(wait_for_joystick));
451
452     if (changed & SDL_HAT_RIGHT && jhat.value & SDL_HAT_RIGHT)
453       bind_joyhat(jhat.which, SDL_HAT_RIGHT, Control(wait_for_joystick));
454
455     MenuStorage::get_joystick_options_menu()->update();
456     wait_for_joystick = -1;
457   }
458   else
459   {
460     if (changed & SDL_HAT_UP)
461     {
462       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_UP));
463       if (it != joy_hat_map.end())
464         set_joy_controls(it->second, jhat.value & SDL_HAT_UP);
465     }
466
467     if (changed & SDL_HAT_DOWN)
468     {
469       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_DOWN));
470       if (it != joy_hat_map.end())
471         set_joy_controls(it->second, jhat.value & SDL_HAT_DOWN);
472     }
473
474     if (changed & SDL_HAT_LEFT)
475     {
476       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_LEFT));
477       if (it != joy_hat_map.end())
478         set_joy_controls(it->second, jhat.value & SDL_HAT_LEFT);
479     }
480
481     if (changed & SDL_HAT_RIGHT)
482     {
483       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_RIGHT));
484       if (it != joy_hat_map.end())
485         set_joy_controls(it->second, jhat.value & SDL_HAT_RIGHT);
486     }
487   }
488
489   hat_state = jhat.value;
490 }
491
492 void
493 JoystickKeyboardController::process_key_event(const SDL_KeyboardEvent& event)
494 {
495   KeyMap::iterator key_mapping = keymap.find(event.keysym.sym);
496
497   // if console key was pressed: toggle console
498   if ((key_mapping != keymap.end()) && (key_mapping->second == Controller::CONSOLE)) {
499     if (event.type == SDL_KEYDOWN) 
500       Console::instance->toggle();
501   } else {
502     if (Console::instance->hasFocus()) {
503       // if console is open: send key there
504       process_console_key_event(event);
505     } else if (MenuManager::current()) {
506       // if menu mode: send key there
507       process_menu_key_event(event);
508     } else if(key_mapping == keymap.end()) {
509       // default action: update controls
510       //log_debug << "Key " << event.key.SDL_Keycode.sym << " is unbound" << std::endl;
511     } else {
512       Control control = key_mapping->second;
513       bool value = (event.type == SDL_KEYDOWN);
514       controller->set_control(control, value);
515       if (jump_with_up_kbd && control == Controller::UP){
516         controller->set_control(Controller::JUMP, value);
517       }
518     }
519   }
520 }
521
522 void
523 JoystickKeyboardController::process_console_key_event(const SDL_KeyboardEvent& event)
524 {
525   if (event.type != SDL_KEYDOWN) return;
526
527   switch (event.keysym.sym) {
528     case SDLK_RETURN:
529       Console::instance->enter();
530       break;
531     case SDLK_BACKSPACE:
532       Console::instance->backspace();
533       break;
534     case SDLK_TAB:
535       Console::instance->autocomplete();
536       break;
537     case SDLK_PAGEUP:
538       Console::instance->scroll(-1);
539       break;
540     case SDLK_PAGEDOWN:
541       Console::instance->scroll(+1);
542       break;
543     case SDLK_HOME:
544       Console::instance->move_cursor(-65535);
545       break;
546     case SDLK_END:
547       Console::instance->move_cursor(+65535);
548       break;
549     case SDLK_UP:
550       Console::instance->show_history(-1);
551       break;
552     case SDLK_DOWN:
553       Console::instance->show_history(+1);
554       break;
555     case SDLK_LEFT:
556       Console::instance->move_cursor(-1);
557       break;
558     case SDLK_RIGHT:
559       Console::instance->move_cursor(+1);
560       break;
561     default:
562    //   int c = SDL_GetScancodeName(event.keysym);
563  //     if ((c >= 32) && (c <= 126)) { <Xeek> you need to move that "unicode" source we were originaly talkinga bout into a new function that gets called from the SDL_TextInput event..... you'll be adding that event.
564    //     Console::instance->input((char)c);
565      // }
566       break;
567   }
568 }
569
570 void
571 JoystickKeyboardController::process_menu_key_event(const SDL_KeyboardEvent& event)
572 {
573   // wait for key mode?
574   if(wait_for_key >= 0) {
575     if(event.type == SDL_KEYUP)
576       return;
577
578     if(event.keysym.sym != SDLK_ESCAPE
579        && event.keysym.sym != SDLK_PAUSE) {
580       bind_key(event.keysym.sym, Control(wait_for_key));
581     }
582     reset();
583     MenuStorage::get_key_options_menu()->update();
584     wait_for_key = -1;
585     return;
586   }
587   if(wait_for_joystick >= 0) {
588     if(event.keysym.sym == SDLK_ESCAPE) {
589       reset();
590       MenuStorage::get_joystick_options_menu()->update();
591       wait_for_joystick = -1;
592     }
593     return;
594   }
595
596   Control control;
597   /* we use default keys when the menu is open (to avoid problems when
598    * redefining keys to invalid settings
599    */
600   switch(event.keysym.sym) {
601     case SDLK_UP:
602       control = Controller::UP;
603       break;
604     case SDLK_DOWN:
605       control = Controller::DOWN;
606       break;
607     case SDLK_LEFT:
608       control = Controller::LEFT;
609       break;
610     case SDLK_RIGHT:
611       control = Controller::RIGHT;
612       break;
613     case SDLK_SPACE:
614     case SDLK_RETURN:
615     case SDLK_KP_ENTER:
616       control = Controller::MENU_SELECT;
617       break;
618     case SDLK_ESCAPE:
619     case SDLK_PAUSE:
620       control = Controller::PAUSE_MENU;
621       break;
622     default:
623       return;
624       break;
625   }
626
627   controller->set_control(control, (event.type == SDL_KEYDOWN));
628 }
629
630 void
631 JoystickKeyboardController::unbind_joystick_control(Control control)
632 {
633   // remove all previous mappings for that control
634   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); /* no ++i */) {
635     if(i->second == control)
636       joy_axis_map.erase(i++);
637     else
638       ++i;
639   }
640
641   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); /* no ++i */) {
642     if(i->second == control)
643       joy_button_map.erase(i++);
644     else
645       ++i;
646   }
647
648   for(HatMap::iterator i = joy_hat_map.begin();  i != joy_hat_map.end(); /* no ++i */) {
649     if(i->second == control)
650       joy_hat_map.erase(i++);
651     else
652       ++i;
653   }
654 }
655
656 void
657 JoystickKeyboardController::bind_joyaxis(JoyId joy_id, int axis, Control control)
658 {
659   // axis isn't the SDL axis number, but axisnumber + 1 with sign
660   // changed depending on if the positive or negative end is to be
661   // used (negative axis 0 becomes -1, positive axis 2 becomes +3,
662   // etc.)
663
664   unbind_joystick_control(control);
665
666   // add new mapping
667   joy_axis_map[std::make_pair(joy_id, axis)] = control;
668 }
669
670 void
671 JoystickKeyboardController::bind_joyhat(JoyId joy_id, int dir, Control c)
672 {
673   unbind_joystick_control(c);
674
675   // add new mapping
676   joy_hat_map[std::make_pair(joy_id, dir)] = c;
677 }
678
679 void
680 JoystickKeyboardController::bind_joybutton(JoyId joy_id, int button, Control control)
681 {
682   unbind_joystick_control(control);
683
684   // add new mapping
685   joy_button_map[std::make_pair(joy_id, button)] = control;
686 }
687
688 void
689 JoystickKeyboardController::bind_key(SDL_Keycode key, Control control)
690 {
691   // remove all previous mappings for that control and for that key
692   for(KeyMap::iterator i = keymap.begin();
693       i != keymap.end(); /* no ++i */) {
694     if(i->second == control) {
695       KeyMap::iterator e = i;
696       ++i;
697       keymap.erase(e);
698     } else {
699       ++i;
700     }
701   }
702
703   KeyMap::iterator i = keymap.find(key);
704   if(i != keymap.end())
705     keymap.erase(i);
706
707   // add new mapping
708   keymap[key] = control;
709 }
710
711 void
712 JoystickKeyboardController::print_joystick_mappings()
713 {
714   std::cout << _("Joystick Mappings") << std::endl;
715   std::cout << "-----------------" << std::endl;
716   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) {
717     std::cout << "Axis: " << i->first.second << " -> " << i->second << std::endl;
718   }
719
720   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); ++i) {
721     std::cout << "Button: " << i->first.second << " -> " << i->second << std::endl;
722   }
723
724   for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) {
725     std::cout << "Hat: " << i->first.second << " -> " << i->second << std::endl;
726   }
727   std::cout << std::endl;
728 }
729
730 SDL_Keycode
731 JoystickKeyboardController::reversemap_key(Control c)
732 {
733   for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
734     if(i->second == c)
735       return i->first;
736   }
737
738   return SDLK_UNKNOWN;
739 }
740
741 int
742 JoystickKeyboardController::reversemap_joyaxis(Control c)
743 {
744   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) {
745     if(i->second == c)
746       return i->first.second;
747   }
748
749   return 0;
750 }
751
752 int
753 JoystickKeyboardController::reversemap_joybutton(Control c)
754 {
755   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); ++i) {
756     if(i->second == c)
757       return i->first.second;
758   }
759
760   return -1;
761 }
762
763 int
764 JoystickKeyboardController::reversemap_joyhat(Control c)
765 {
766   for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) {
767     if(i->second == c)
768       return i->first.second;
769   }
770
771   return -1;
772 }
773
774 /* EOF */