nomlib
Loading...
Searching...
No Matches
Joystick.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SYSTEM_JOYSTICK_HPP
30#define NOMLIB_SYSTEM_JOYSTICK_HPP
31
32#include <string>
33#include <memory>
34
35#include "nomlib/config.hpp"
36
37// Forward declarations (third-party)
38typedef struct _SDL_Joystick SDL_Joystick;
39
40namespace nom {
41
42typedef int32 JoystickIndex;
43
45typedef int32 JoystickID;
46
51const nom::size_type GUID_MAX_LENGTH = 16;
52
58{
59 uint8 data[GUID_MAX_LENGTH];
60};
61
65const int16 JOYSTICK_LEFT_THUMB_DEAD_ZONE = 7849;
66const int16 JOYSTICK_RIGHT_THUMB_DEAD_ZONE = 8689;
67const int16 JOYSTICK_TRIGGER_THRESHOLD = 30;
68
74bool init_joystick_subsystem();
75
77void shutdown_joystick_subsystem();
78
80void JoystickDeleter(SDL_Joystick* dev);
81
83class Joystick
84{
85 public:
87 enum Axis: uint8
88 {
89 AXIS_ZERO = 0,
90 AXIS_ONE,
91 AXIS_TWO,
92 AXIS_THREE,
93 AXIS_FOUR,
94 AXIS_FIVE,
95 AXIS_SIX,
96 AXIS_SEVEN,
97 AXIS_EIGHT,
98 AXIS_MAX,
99 };
100
102 enum Button: uint8
103 {
104 BUTTON_ZERO = 0,
105 BUTTON_ONE,
106 BUTTON_TWO,
107 BUTTON_THREE,
108 BUTTON_FOUR,
109 BUTTON_FIVE,
110 BUTTON_SIX,
111 BUTTON_SEVEN,
112 BUTTON_EIGHT,
113 BUTTON_NINE,
114 BUTTON_TEN,
115 BUTTON_ELEVEN,
116 BUTTON_TWELVE,
117 BUTTON_FOURTEEN,
118 BUTTON_FIFTEEN,
119 BUTTON_SIXTEEN,
120 BUTTON_MAX,
121 };
122
124 enum Hat: uint8
125 {
126 HAT_ZERO = 0,
127 HAT_ONE,
128 HAT_TWO,
129 HAT_THREE,
130 HAT_FOUR,
131 HAT_MAX,
132 };
133
135 enum HatPosition: uint8
136 {
137 HAT_CENTERED = 0x00,
138 HAT_UP = 0x01,
139 HAT_RIGHT = 0x02,
140 HAT_DOWN = 0x04,
141 HAT_LEFT = 0x08,
142 HAT_RIGHTUP = (HAT_RIGHT | HAT_UP),
143 HAT_RIGHTDOWN = (HAT_RIGHT | HAT_DOWN),
144 HAT_LEFTUP = (HAT_LEFT | HAT_UP),
145 HAT_LEFTDOWN = (HAT_LEFT | HAT_DOWN),
146 };
147
148 typedef Joystick self_type;
149
150 Joystick();
151
152 ~Joystick();
153
157 SDL_Joystick* device() const;
158
163 bool attached() const;
164
169 JoystickID device_id() const;
170
175 std::string name() const;
176
186 bool open(JoystickIndex device_index);
187
191 void close();
192
199 int num_axes();
200
208
216
223 int num_hats();
224
239 static int set_event_state(int state);
240
245 static void update();
246
251 static int num_joysticks();
252
265 static std::string name(JoystickIndex device_index);
266
277
289 static JoystickGUID device_guid(JoystickIndex device_index);
290
297 static std::string device_guid_string(JoystickGUID guid);
298
308 std::string device_guid_string() const;
309
321 static JoystickID device_id(JoystickIndex device_index);
322
323 private:
324 typedef std::unique_ptr<SDL_Joystick, void (*)(SDL_Joystick*)> joystick_dev;
325
326 joystick_dev device_;
327};
328
329std::unique_ptr<Joystick> make_unique_joystick();
330std::shared_ptr<Joystick> make_shared_joystick();
331
332JoystickGUID convert_SDL_JoystickGUID(SDL_JoystickGUID dev_guid);
333SDL_JoystickGUID convert_SDL_JoystickGUID(JoystickGUID dev_guid);
334
335} // namespace nom
336
337#endif // include guard defined
338
SDL2 joystick interface.
Definition Joystick.hpp:84
bool open(JoystickIndex device_index)
Open a joystick for use.
Button
Joystick button definitions.
Definition Joystick.hpp:103
static JoystickID device_id(JoystickIndex device_index)
Get the instance ID of a joystick device.
Hat
Joystick hat definitions.
Definition Joystick.hpp:125
JoystickID device_id() const
Get the instance ID of an opened joystick.
JoystickGUID device_guid() const
Get the implementation-dependent GUID for this joystick instance.
HatPosition
Joystick hat position definitions.
Definition Joystick.hpp:136
Axis
Joystick axis definitions.
Definition Joystick.hpp:88
static int set_event_state(int state)
Toggle the joystick event polling state.
void close()
Close an opened joystick.
static std::string device_guid_string(JoystickGUID guid)
Get the ASCII string representation of a joystick GUID.
bool attached() const
Get the status of the joystick.
SDL_Joystick * device() const
Get the underlying implementation pointer.
int num_buttons()
Get the number of buttons for this joystick.
static std::string name(JoystickIndex device_index)
Get the implementation-dependent name of the joystick.
int num_hats()
Get the number of POV hats for this joystick.
int num_axes()
Get the number of axes for this joystick.
static JoystickGUID device_guid(JoystickIndex device_index)
Get the implementation-dependent GUID for a joystick.
static void update()
Update the current state of the opened joysticks.
int num_track_balls()
Get the number of track balls for this joystick.
std::string name() const
Get the implementation-dependent name of the joystick.
std::string device_guid_string() const
Get the ASCII string representation of this joystick instance GUID.
static int num_joysticks()
Get the number of joysticks attached to the system.
Internal representation of a stable, fixed identifier – 128 bits wide – for a joystick device....
Definition Joystick.hpp:58