2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
3 // 2007,2014 Ingo Ruhnke <grumbel@gmail.com>
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.
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.
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/>.
18 #include "control/input_manager.hpp"
22 #include "control/game_controller_manager.hpp"
23 #include "control/joystick_manager.hpp"
24 #include "control/keyboard_manager.hpp"
25 #include "gui/menu_manager.hpp"
26 #include "lisp/list_iterator.hpp"
27 #include "supertux/gameconfig.hpp"
28 #include "util/gettext.hpp"
29 #include "util/log.hpp"
30 #include "util/writer.hpp"
32 InputManager::InputManager() :
33 controller(new Controller),
34 m_use_game_controller(true),
35 keyboard_manager(new KeyboardManager(this)),
36 joystick_manager(new JoystickManager(this)),
37 game_controller_manager(new GameControllerManager(this))
41 InputManager::~InputManager()
46 InputManager::get_controller()
48 return controller.get();
52 InputManager::use_game_controller(bool v)
54 m_use_game_controller = v;
58 InputManager::read(const Reader& lisp)
60 const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
63 keyboard_manager->read(keymap_lisp);
66 const lisp::Lisp* joystick_lisp = lisp.get_lisp(_("joystick"));
69 joystick_manager->read(joystick_lisp);
74 InputManager::write(Writer& writer)
76 writer.start_list("keymap");
77 keyboard_manager->write(writer);
78 writer.end_list("keymap");
80 writer.start_list("joystick");
81 joystick_manager->write(writer);
82 writer.end_list("joystick");
86 InputManager::update()
98 InputManager::process_event(const SDL_Event& event)
102 keyboard_manager->process_text_input_event(event.text);
107 keyboard_manager->process_key_event(event.key);
110 case SDL_JOYAXISMOTION:
111 if (!m_use_game_controller) joystick_manager->process_axis_event(event.jaxis);
114 case SDL_JOYHATMOTION:
115 if (!m_use_game_controller) joystick_manager->process_hat_event(event.jhat);
118 case SDL_JOYBUTTONDOWN:
119 case SDL_JOYBUTTONUP:
120 if (!m_use_game_controller) joystick_manager->process_button_event(event.jbutton);
123 case SDL_JOYDEVICEADDED:
124 joystick_manager->on_joystick_added(event.jdevice.which);
127 case SDL_JOYDEVICEREMOVED:
128 joystick_manager->on_joystick_removed(event.jdevice.which);
131 case SDL_CONTROLLERAXISMOTION:
132 if (m_use_game_controller) game_controller_manager->process_axis_event(event.caxis);
135 case SDL_CONTROLLERBUTTONDOWN:
136 if (m_use_game_controller) game_controller_manager->process_button_event(event.cbutton);
139 case SDL_CONTROLLERBUTTONUP:
140 if (m_use_game_controller) game_controller_manager->process_button_event(event.cbutton);
143 case SDL_CONTROLLERDEVICEADDED:
144 log_debug << "SDL_CONTROLLERDEVICEADDED" << std::endl;
145 game_controller_manager->on_controller_added(event.cdevice.which);
148 case SDL_CONTROLLERDEVICEREMOVED:
149 log_debug << "SDL_CONTROLLERDEVICEREMOVED" << std::endl;
150 game_controller_manager->on_controller_removed(event.cdevice.which);
153 case SDL_CONTROLLERDEVICEREMAPPED:
154 log_debug << "SDL_CONTROLLERDEVICEREMAPPED" << std::endl;