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 "export.hpp"
36#include "nomlib/config.hpp"
37
38// Forward declarations (third-party)
39typedef struct _SDL_Joystick SDL_Joystick;
40
41namespace nom {
42
43typedef int32 JoystickIndex;
44
46typedef int32 JoystickID;
47
52const nom::size_type GUID_MAX_LENGTH = 16;
53
59{
60 uint8 data[GUID_MAX_LENGTH];
61};
62
66const int16 JOYSTICK_LEFT_THUMB_DEAD_ZONE = 7849;
67const int16 JOYSTICK_RIGHT_THUMB_DEAD_ZONE = 8689;
68const int16 JOYSTICK_TRIGGER_THRESHOLD = 30;
69
75NOM_EXPORT bool init_joystick_subsystem();
76
78NOM_EXPORT void shutdown_joystick_subsystem();
79
81NOM_EXPORT void JoystickDeleter(SDL_Joystick* dev);
82
84class NOM_EXPORT Joystick
85{
86 public:
88 enum Axis: uint8
89 {
90 AXIS_ZERO = 0,
91 AXIS_ONE,
92 AXIS_TWO,
93 AXIS_THREE,
94 AXIS_FOUR,
95 AXIS_FIVE,
96 AXIS_SIX,
97 AXIS_SEVEN,
98 AXIS_EIGHT,
99 AXIS_MAX,
100 };
101
103 enum Button: uint8
104 {
105 BUTTON_ZERO = 0,
106 BUTTON_ONE,
107 BUTTON_TWO,
108 BUTTON_THREE,
109 BUTTON_FOUR,
110 BUTTON_FIVE,
111 BUTTON_SIX,
112 BUTTON_SEVEN,
113 BUTTON_EIGHT,
114 BUTTON_NINE,
115 BUTTON_TEN,
116 BUTTON_ELEVEN,
117 BUTTON_TWELVE,
118 BUTTON_FOURTEEN,
119 BUTTON_FIFTEEN,
120 BUTTON_SIXTEEN,
121 BUTTON_MAX,
122 };
123
125 enum Hat: uint8
126 {
127 HAT_ZERO = 0,
128 HAT_ONE,
129 HAT_TWO,
130 HAT_THREE,
131 HAT_FOUR,
132 HAT_MAX,
133 };
134
136 enum HatPosition: uint8
137 {
138 HAT_CENTERED = 0x00,
139 HAT_UP = 0x01,
140 HAT_RIGHT = 0x02,
141 HAT_DOWN = 0x04,
142 HAT_LEFT = 0x08,
143 HAT_RIGHTUP = (HAT_RIGHT | HAT_UP),
144 HAT_RIGHTDOWN = (HAT_RIGHT | HAT_DOWN),
145 HAT_LEFTUP = (HAT_LEFT | HAT_UP),
146 HAT_LEFTDOWN = (HAT_LEFT | HAT_DOWN),
147 };
148
149 typedef Joystick self_type;
150
151 Joystick();
152
153 ~Joystick();
154
158 SDL_Joystick* device() const;
159
164 bool attached() const;
165
170 JoystickID device_id() const;
171
176 std::string name() const;
177
187 bool open(JoystickIndex device_index);
188
192 void close();
193
200 int num_axes();
201
209
217
224 int num_hats();
225
240 static int set_event_state(int state);
241
246 static void update();
247
252 static int num_joysticks();
253
266 static std::string name(JoystickIndex device_index);
267
278
290 static JoystickGUID device_guid(JoystickIndex device_index);
291
298 static std::string device_guid_string(JoystickGUID guid);
299
309 std::string device_guid_string() const;
310
322 static JoystickID device_id(JoystickIndex device_index);
323
324 private:
325 typedef std::unique_ptr<SDL_Joystick, void (*)(SDL_Joystick*)> joystick_dev;
326
327 joystick_dev device_;
328};
329
330NOM_EXPORT std::unique_ptr<Joystick> make_unique_joystick();
331NOM_EXPORT std::shared_ptr<Joystick> make_shared_joystick();
332
333NOM_EXPORT JoystickGUID convert_SDL_JoystickGUID(SDL_JoystickGUID dev_guid);
334NOM_EXPORT SDL_JoystickGUID convert_SDL_JoystickGUID(JoystickGUID dev_guid);
335
336} // namespace nom
337
338#endif // include guard defined
339
SDL2 joystick interface.
Definition Joystick.hpp:85
bool open(JoystickIndex device_index)
Open a joystick for use.
Button
Joystick button definitions.
Definition Joystick.hpp:104
static JoystickID device_id(JoystickIndex device_index)
Get the instance ID of a joystick device.
Hat
Joystick hat definitions.
Definition Joystick.hpp:126
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:137
Axis
Joystick axis definitions.
Definition Joystick.hpp:89
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:59