cgv
gamepad.h
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include "lib_begin.h"
7 
8 namespace gamepad {
9 
12  struct driver_info
14  {
16  std::string name;
18  bool enabled;
19  };
21  extern CGV_API const std::vector<driver_info>& get_driver_infos();
23  extern CGV_API void set_driver_state(unsigned i, bool enabled);
25 
28  struct device_info
30  {
34  std::string name;
42  size_t driver_index;
44  bool enabled;
47  };
48 
50  extern CGV_API void scan_devices();
52  extern CGV_API const std::vector<device_info>& get_device_infos();
54  extern CGV_API const device_info* get_device_info(void* device_handle);
56  extern CGV_API bool is_connected(void* device_handle);
58  extern CGV_API bool set_device_state(void* device_handle, bool enabled);
59 
61  enum BatteryType {
62  BT_WIRED,
63  BT_ALKALINE,
64  BT_NIMH, // nickel metal hydide batteries
65  BT_UNKNOWN
66  };
68  extern CGV_API bool get_device_battery_info(unsigned device_index, BatteryType& battery_type, float& fill_state);
70 
73  enum GamepadKeys {
75  GPK_UNKNOWN = 512,
76 
77  GPK_A,
78  GPK_B,
79  GPK_X,
80  GPK_Y,
81  GPK_RIGHT_BUMPER,
82  GPK_LEFT_BUMPER,
83  GPK_LEFT_TRIGGER,
84  GPK_RIGHT_TRIGGER,
85  GPK_DPAD_UP,
86  GPK_DPAD_DOWN,
87  GPK_DPAD_LEFT,
88  GPK_DPAD_RIGHT,
89  GPK_START,
90  GPK_BACK,
91 
92  GPK_LEFT_STICK_PRESS,
93  GPK_RIGHT_STICK_PRESS,
94 
95  GPK_LEFT_STICK_UP,
96  GPK_LEFT_STICK_DOWN,
97  GPK_LEFT_STICK_RIGHT,
98  GPK_LEFT_STICK_LEFT,
99  GPK_LEFT_STICK_UPLEFT,
100  GPK_LEFT_STICK_UPRIGHT,
101  GPK_LEFT_STICK_DOWNRIGHT,
102  GPK_LEFT_STICK_DOWNLEFT,
103 
104  GPK_RIGHT_STICK_UP,
105  GPK_RIGHT_STICK_DOWN,
106  GPK_RIGHT_STICK_RIGHT,
107  GPK_RIGHT_STICK_LEFT,
108  GPK_RIGHT_STICK_UPLEFT,
109  GPK_RIGHT_STICK_UPRIGHT,
110  GPK_RIGHT_STICK_DOWNRIGHT,
111  GPK_RIGHT_STICK_DOWNLEFT,
112  GPK_END,
113  GPK_BEGIN = GPK_A
114  };
116  enum KeyAction {
117  KA_RELEASE,
118  KA_PRESS,
119  KA_REPEAT
120  };
122  extern CGV_API std::string get_key_string(unsigned short key);
123 
125 
127  extern CGV_API bool query_key_event(void* device_handle, GamepadKeys& gk, KeyAction& action);
128 
130  enum GamepadButtonStateFlags
131  {
132  GBF_DPAD_UP = 0x0001,
133  GBF_DPAD_DOWN = 0x0002,
134  GBF_DPAD_LEFT = 0x0004,
135  GBF_DPAD_RIGHT = 0x0008,
136  GBF_START = 0x0010,
137  GBF_BACK = 0x0020,
138  GBF_LEFT_STICK = 0x0040,
139  GBF_RIGHT_STICK = 0x0080,
140  GBF_LEFT_BUMPER = 0x0100,
141  GBF_RIGHT_BUMPER = 0x0200,
142  GBF_A = 0x1000,
143  GBF_B = 0x2000,
144  GBF_X = 0x4000,
145  GBF_Y = 0x8000
146  };
148  extern CGV_API std::string convert_flags_to_string(GamepadButtonStateFlags flags);
149 
151  struct CGV_API gamepad_state
152  {
154  unsigned time_stamp;
156  unsigned button_flags;
158  float left_stick_position[2];
160  float right_stick_position[2];
162  float trigger_position[2];
164  gamepad_state();
165  };
166 
168  extern CGV_API bool get_state(void* device_handle, gamepad_state& state);
170  extern CGV_API bool set_vibration(void* device_handle, float low_frequency_strength, float high_frequency_strength);
171 }
172 
173 #include <cgv/config/lib_end.h>
gamepad::device_info::enabled
bool enabled
whether device is enabled
Definition: gamepad.h:44
gamepad::gamepad_state::time_stamp
unsigned time_stamp
time stamp can be used whether a change has happened between two states
Definition: gamepad.h:154
gamepad::device_info::vibration_strength
float vibration_strength[2]
strength values of low and high frequency vibration in the range [0,1]
Definition: gamepad.h:46
gamepad::device_info::force_feedback_support
bool force_feedback_support
whether force feedback is supported
Definition: gamepad.h:36
gamepad::device_info::device_handle
void * device_handle
unique device handle
Definition: gamepad.h:32
gamepad::device_info::is_wireless
bool is_wireless
whether it is wireless
Definition: gamepad.h:38
gamepad::gamepad_state
see https://upload.wikimedia.org/wikipedia/commons/2/2c/360_controller.svg for an explanation of the ...
Definition: gamepad.h:152
gamepad::device_info::no_menu_buttons
bool no_menu_buttons
whether menu buttons (start,back) are missing
Definition: gamepad.h:40
vr::get_key_string
std::string get_key_string(unsigned short key)
convert key to string
Definition: vr_state.cxx:78
gamepad::device_info
information provided per gamepad device
Definition: gamepad.h:30
gamepad::driver_info::enabled
bool enabled
state of driver
Definition: gamepad.h:18
vr::KeyAction
KeyAction
repeated definition from cgv/gui/key_event.h
Definition: vr_state.h:26
gamepad::device_info::driver_index
size_t driver_index
index of driver used to access device
Definition: gamepad.h:42
gamepad::device_info::name
std::string name
name in case driver provides this information (not reliable)
Definition: gamepad.h:34
gamepad::driver_info::name
std::string name
name of driver
Definition: gamepad.h:16
gamepad::gamepad_state::button_flags
unsigned button_flags
combination of flags in GamepadButtonStateFlags combined with the OR operation
Definition: gamepad.h:156