cgv
Loading...
Searching...
No Matches
gamepad.cxx
1#include "gamepad.h"
2#include "gamepad_driver.h"
3#include <cassert>
4
5namespace gamepad {
6
7 std::string get_key_string(unsigned short key)
8 {
9 static const char* gamepad_key_names[] = {
10 "UNKNOWN",
11
12 "A",
13 "B",
14 "X",
15 "Y",
16
17 "RIGHT_BUMPER",
18 "LEFT_BUMPER",
19 "LEFT_TRIGGER",
20 "RIGHT_TRIGGER",
21
22 "DPAD_UP",
23 "DPAD_DOWN",
24 "DPAD_LEFT",
25 "DPAD_RIGHT",
26
27 "START",
28 "BACK",
29
30 "LEFT_STICK_PRESS",
31 "RIGHT_STICK_PRESS",
32
33 "LEFT_STICK_UP",
34 "LEFT_STICK_DOWN",
35 "LEFT_STICK_RIGHT",
36 "LEFT_STICK_LEFT",
37 "LEFT_STICK_UPLEFT",
38 "LEFT_STICK_UPRIGHT",
39 "LEFT_STICK_DOWNRIGHT",
40 "LEFT_STICK_DOWNLEFT",
41
42 "RIGHT_STICK_UP",
43 "RIGHT_STICK_DOWN",
44 "RIGHT_STICK_RIGHT",
45 "RIGHT_STICK_LEFT",
46 "RIGHT_STICK_UPLEFT",
47 "RIGHT_STICK_UPRIGHT",
48 "RIGHT_STICK_DOWNRIGHT",
49 "RIGHT_STICK_DOWNLEFT"
50 };
51 int index = key - (int)GPK_UNKNOWN;
52 return index < 33 ? gamepad_key_names[index] : "";
53 }
54
56 std::string convert_flags_to_string(GamepadButtonStateFlags flags)
57 {
58 static const char* flag_names[] = {
59 "DPAD_UP" ,
60 "DPAD_DOWN" ,
61 "DPAD_LEFT" ,
62 "DPAD_RIGHT" ,
63 "START" ,
64 "BACK" ,
65 "LEFT_STICK" ,
66 "RIGHT_STICK" ,
67 "LEFT_BUMPER" ,
68 "RIGHT_BUMPER",
69 "A",
70 "B",
71 "X",
72 "Y"
73 };
74 static const GamepadButtonStateFlags flag_values[] = {
75 GBF_DPAD_UP ,
76 GBF_DPAD_DOWN ,
77 GBF_DPAD_LEFT ,
78 GBF_DPAD_RIGHT ,
79 GBF_START ,
80 GBF_BACK ,
81 GBF_LEFT_STICK ,
82 GBF_RIGHT_STICK ,
83 GBF_LEFT_BUMPER ,
84 GBF_RIGHT_BUMPER ,
85 GBF_A ,
86 GBF_B ,
87 GBF_X ,
88 GBF_Y
89 };
90 std::string result;
91 for (unsigned i = 0; i < 14; ++i)
92 if ((flags & flag_values[i]) != 0) {
93 if (result.empty())
94 result = flag_names[i];
95 else
96 result += std::string("+") + flag_names[i];
97 }
98 return result;
99 }
109
111 const std::vector<driver_info>& get_driver_infos()
112 {
113 return ref_driver_infos();
114 }
116 void set_driver_state(unsigned i, bool enabled)
117 {
118 assert(i < ref_drivers().size());
119 ref_drivers()[i]->set_driver_state(enabled);
120 ref_driver_infos()[i].enabled = enabled;
121 }
122
124 void scan_devices()
125 {
126 ref_device_infos().clear();
127 for (auto driver_ptr : ref_drivers())
128 driver_ptr->scan_devices(ref_device_infos());
129 }
131 const std::vector<device_info>& get_device_infos()
132 {
133 return ref_device_infos();
134 }
136 const device_info* get_device_info(void* device_handle)
137 {
138 return ref_device_info(device_handle);
139 }
140
141 bool get_driver_index(void* device_handle, unsigned& driver_index)
142 {
143 // check if device_index is valid
144 auto* dip = get_device_info(device_handle);
145 if (!dip)
146 return false;
147 driver_index = (unsigned)dip->driver_index;
148 if (driver_index >= ref_drivers().size())
149 return false;
150 return true;
151 }
153 bool is_connected(void* device_handle)
154 {
155 gamepad_state state;
156 unsigned driver_index;
157 if (!get_driver_index(device_handle, driver_index))
158 return false;
159 return ref_drivers()[driver_index]->get_device_state(device_handle, state);
160 }
162 bool set_device_state(void* device_handle, bool enabled)
163 {
164 unsigned driver_index;
165 if (!get_driver_index(device_handle, driver_index))
166 return false;
167 ref_drivers()[driver_index]->set_device_state(device_handle, enabled);
168 ref_device_info(device_handle)->enabled = enabled;
169 return true;
170 }
171
172 bool get_device_battery_info(void* device_handle, BatteryType& battery_type, float& fill_state)
173 {
174 unsigned driver_index;
175 if (!get_driver_index(device_handle, driver_index))
176 return false;
177 return ref_drivers()[driver_index]->get_device_battery_info(device_handle, battery_type, fill_state);
178 }
179 bool query_key_event(void* device_handle, GamepadKeys& gk, KeyAction& action)
180 {
181 unsigned driver_index;
182 if (!get_driver_index(device_handle, driver_index))
183 return false;
184 return ref_drivers()[driver_index]->query_device_key_event(device_handle, gk, action);
185 }
187 bool get_state(void* device_handle, gamepad_state& state)
188 {
189 unsigned driver_index;
190 if (!get_driver_index(device_handle, driver_index))
191 return false;
192 return ref_drivers()[driver_index]->get_device_state(device_handle, state);
193 }
195 bool set_vibration(void* device_handle, float low_frequency_strength, float high_frequency_strength)
196 {
197 unsigned driver_index;
198 if (!get_driver_index(device_handle, driver_index))
199 return false;
200 return ref_drivers()[driver_index]->set_device_vibration(device_handle, low_frequency_strength, high_frequency_strength);
201 }
202}
std::vector< vr_driver * > & ref_drivers()
return registered drivers
std::string get_key_string(unsigned short key)
convert key to string
Definition vr_state.cxx:78
bool enabled
whether device is enabled
Definition gamepad.h:44
float right_stick_position[2]
x and y position of left thumb in the range [-1,1]
Definition gamepad.h:160
unsigned time_stamp
time stamp can be used whether a change has happened between two states
Definition gamepad.h:154
float left_stick_position[2]
x and y position of left thumb in the range [-1,1]
Definition gamepad.h:158
unsigned button_flags
combination of flags in GamepadButtonStateFlags combined with the OR operation
Definition gamepad.h:156
gamepad_state()
initialize state
Definition gamepad.cxx:101
float trigger_position[2]
values of left and right triggers in the range [0,1]
Definition gamepad.h:162