Merge branch 'feature/menu-cleanup'
[supertux.git] / src / control / joystick_manager.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //                2014 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 #ifndef HEADER_SUPERTUX_CONTROL_JOYSTICK_MANAGER_HPP
19 #define HEADER_SUPERTUX_CONTROL_JOYSTICK_MANAGER_HPP
20
21 #include <map>
22 #include <vector>
23
24 #include "SDL.h"
25
26 #include "control/controller.hpp"
27 #include "util/reader_fwd.hpp"
28 #include "util/writer_fwd.hpp"
29
30 class InputManager;
31
32 class JoystickManager final
33 {
34 private:
35   typedef Uint8 JoyId;
36
37   typedef std::map<std::pair<JoyId, int>, Controller::Control> ButtonMap;
38   typedef std::map<std::pair<JoyId, int>, Controller::Control> AxisMap;
39   typedef std::map<std::pair<JoyId, int>, Controller::Control> HatMap;
40
41 private:
42   InputManager* parent;
43
44   ButtonMap joy_button_map;
45   AxisMap joy_axis_map;
46   HatMap joy_hat_map;
47
48   int dead_zone;
49
50   /// the number of buttons all joysticks have
51   int min_joybuttons;
52
53   /// the max number of buttons a joystick has
54   int max_joybuttons;
55
56   int max_joyaxis;
57   int max_joyhats;
58
59   Uint8 hat_state;
60
61 public:
62   bool jump_with_up_joy;
63
64 public:
65   int wait_for_joystick;
66
67 public:
68   std::vector<SDL_Joystick*> joysticks;
69
70 public:
71   JoystickManager(InputManager* parent);
72   ~JoystickManager();
73
74   void process_hat_event(const SDL_JoyHatEvent& jhat);
75   void process_axis_event(const SDL_JoyAxisEvent& jaxis);
76   void process_button_event(const SDL_JoyButtonEvent& jbutton);
77
78   void print_joystick_mappings();
79
80   int reversemap_joybutton(Controller::Control c);
81   int reversemap_joyaxis(Controller::Control c);
82   int reversemap_joyhat(Controller::Control c);
83
84   void unbind_joystick_control(Controller::Control c);
85
86   void bind_joybutton(JoyId joy_id, int button, Controller::Control c);
87   void bind_joyaxis(JoyId joy_id, int axis, Controller::Control c);
88   void bind_joyhat(JoyId joy_id, int dir, Controller::Control c);
89
90   void set_joy_controls(Controller::Control id, bool value);
91
92   void on_joystick_added(int joystick_index);
93   void on_joystick_removed(int instance_id);
94
95   void read(const lisp::Lisp* joystick_lisp);
96   void write(Writer& writer);
97
98 private:
99   JoystickManager(const JoystickManager&) = delete;
100   JoystickManager& operator=(const JoystickManager&) = delete;
101 };
102
103 #endif
104
105 /* EOF */