Oops from previous commit
[lsnes.git] / include / library / gamepad.hpp
blobf46c9875518bad944a718268ce55a72f6dd36a3d
1 #ifndef _library__gamepad__hpp__included__
2 #define _library__gamepad__hpp__included__
4 #include <cstdint>
5 #include <set>
6 #include "json.hpp"
7 #include "threads.hpp"
9 namespace gamepad
11 class pad
13 public:
14 pad(const JSON::node& state, unsigned jnum);
15 pad(const std::string& _name, unsigned jnum);
16 ~pad();
17 unsigned add_axis(uint64_t id, int64_t min, int64_t max, bool pressure, const std::string& name);
18 unsigned add_button(uint64_t id, const std::string& name);
19 unsigned add_hat(uint64_t id, const std::string& name);
20 unsigned add_hat(uint64_t idx, uint64_t idy, int64_t mindev, const std::string& namex,
21 const std::string& namey);
22 unsigned axes() { return next_axis; }
23 unsigned buttons() { return next_button; }
24 unsigned hats() { return next_hat; }
25 void calibrate_axis(unsigned num, int64_t minus, int64_t zero, int64_t plus, int64_t neutral,
26 double threshold, bool pressure, bool disabled);
27 void get_calibration(unsigned num, int64_t& minus, int64_t& zero, int64_t& plus, int64_t& neutral,
28 double& threshold, bool& pressure, bool& disabled);
29 double get_tolerance(unsigned num);
30 int get_mode(unsigned num);
31 std::set<unsigned> online_axes();
32 std::set<unsigned> online_buttons();
33 std::set<unsigned> online_hats();
34 void axis_status(unsigned num, int64_t& raw, int16_t& scaled);
35 int button_status(unsigned num);
36 int hat_status(unsigned num);
37 void report_axis(uint64_t id, int64_t val);
38 void report_button(uint64_t id, bool val);
39 void report_hat(uint64_t id, int angle);
40 void set_online(bool status);
41 bool online() { return online_flag; }
42 const std::string& name() { return _name; }
43 void set_axis_cb(std::function<void(unsigned jnum, unsigned num, int16_t val)> fn)
45 axis_fn = fn;
47 void set_button_cb(std::function<void(unsigned jnum, unsigned num, bool val)> fn) { button_fn = fn; }
48 void set_hat_cb(std::function<void(unsigned jnum, unsigned num, unsigned val)> fn) { hat_fn = fn; }
49 void set_axismode_cb(std::function<void(unsigned jnum, unsigned num, int mode, double tolerance)> fn)
51 amode_fn = fn;
53 void set_newitem_cb(std::function<void(unsigned jnum, unsigned num, int type)> fn) { newitem_fn = fn; }
54 void load(const JSON::node& state);
55 JSON::node save();
56 std::string get_summary();
57 private:
58 struct axis_info
60 uint64_t id;
61 unsigned num;
62 int64_t minus;
63 int64_t zero;
64 int64_t plus;
65 int64_t neutral;
66 double threshold;
67 bool pressure;
68 bool disabled;
69 bool online;
70 std::string name;
71 int16_t state;
72 int64_t rstate;
74 struct button_info
76 uint64_t id;
77 unsigned num;
78 std::string name;
79 bool online;
80 bool state;
82 struct hat_info
84 bool axis;
85 uint64_t id;
86 uint64_t id2;
87 unsigned num;
88 int64_t mindev;
89 std::string name;
90 std::string name2;
91 unsigned state;
92 bool online;
94 pad(const pad&);
95 pad& operator=(const pad&);
96 std::function<void(unsigned jnum, unsigned num, int16_t val)> axis_fn;
97 std::function<void(unsigned jnum, unsigned num, bool val)> button_fn;
98 std::function<void(unsigned jnum, unsigned num, unsigned val)> hat_fn;
99 std::function<void(unsigned jnum, unsigned num, int mode, double tolerance)> amode_fn;
100 std::function<void(unsigned jnum, unsigned num, int type)> newitem_fn;
101 bool online_flag;
102 std::string _name;
103 std::map<uint64_t, axis_info> _axes;
104 std::map<uint64_t, hat_info*> _axes_hat;
105 std::map<uint64_t, button_info> _buttons;
106 std::map<uint64_t, hat_info> _hats;
107 unsigned next_axis;
108 unsigned next_button;
109 unsigned next_hat;
110 unsigned jid;
111 threads::lock mlock;
114 class set
116 public:
117 set();
118 ~set();
119 void load(const JSON::node& state);
120 JSON::node save();
121 unsigned gamepads();
122 pad& operator[](unsigned gpnum);
123 unsigned add(const std::string& name);
124 void set_axis_cb(std::function<void(unsigned jnum, unsigned num, int16_t val)> fn);
125 void set_button_cb(std::function<void(unsigned jnum, unsigned num, bool val)> fn);
126 void set_hat_cb(std::function<void(unsigned jnum, unsigned num, unsigned val)> fn);
127 void set_axismode_cb(std::function<void(unsigned jnum, unsigned num, int mode, double tolerance)> fn);
128 void set_newitem_cb(std::function<void(unsigned jnum, unsigned num, int type)> fn);
129 std::string get_summary();
130 void offline_all();
131 private:
132 set(const set&);
133 set& operator=(const set&);
134 std::function<void(unsigned jnum, unsigned num, int16_t val)> axis_fn;
135 std::function<void(unsigned jnum, unsigned num, bool val)> button_fn;
136 std::function<void(unsigned jnum, unsigned num, unsigned val)> hat_fn;
137 std::function<void(unsigned jnum, unsigned num, int mode, double tolerance)> amode_fn;
138 std::function<void(unsigned jnum, unsigned num, int type)> newitem_fn;
139 std::vector<pad*> _gamepads;
140 threads::lock mlock;
144 #endif