Save bitmaps as PNG
[lsnes.git] / include / library / joyfun.hpp
blob57189047c03b0ebbc8bad765a533e4c288cf0534
1 #ifndef _library__joyfun__hpp__included__
2 #define _library__joyfun__hpp__included__
4 #include <cstdint>
5 #include <string>
6 #include <vector>
7 #include <map>
9 /**
10 * Perform axis calibration correction.
12 * Parameter v: The raw value read.
13 * Parameter low: The low limit.
14 * Parameter high: The high limit.
15 * Returns: The calibrated read value.
17 short calibration_correction(int64_t v, int64_t low, int64_t high);
19 /**
20 * Translate hundredths of degree position into hat bitmask.
22 * 0 is assumed to be up, and values are assumed to be clockwise. Negative values are centered.
24 * Parameter angle: The angle.
25 * Returns: The hat bitmask.
27 short angle_to_bitmask(int angle);
29 /**
30 * If a != b, a <- b and return true. Otherwise return false.
32 * Parameter a: The target.
33 * Parameter b: The source.
34 * Returns: a was not equal to b?
36 template<typename T> bool make_equal(T& a, const T& b)
38 bool r = (a != b);
39 if(r)
40 a = b;
41 return r;
44 /**
45 * Joystick.
47 class joystick_model
49 public:
50 /**
51 * Set name of joystick field.
53 void name(const std::string& newn);
54 /**
55 * Get name of joystick field.
57 const std::string& name();
58 /**
59 * Create a new axis.
61 * Parameter id: The id of the axis.
62 * Parameter minv: The minimal calibration value.
63 * Parameter maxv: The maximal calibration value.
64 * Parameter xname: The name of axis.
65 * Returns: The number of axis.
67 unsigned new_axis(uint64_t id, int64_t minv, int64_t maxv, const std::string& xname);
68 /**
69 * Create a new button.
71 * Parameter id: The id of button.
72 * Parameter xname: The name of button.
73 * Returns: The number of button.
75 unsigned new_button(uint64_t id, const std::string& xname);
76 /**
77 * Create a new hat from pair of axes.
79 * Parameter id_x: The id of x axis of the hat.
80 * Parameter id_y: The id of y axis of the hat.
81 * Parameter min_dev: The smallest deviation from zero to react to.
82 * Parameter xname_x: The name of x axis.
83 * Parameter xname_y: The name of y axis.
84 * Returns: The number of hat.
86 unsigned new_hat(uint64_t id_x, uint64_t id_y, int64_t min_dev, const std::string& xname_x,
87 const std::string& xname_y);
88 /**
89 * Create a new hat from POV control.
91 * Parameter id: The id of POV control.
92 * Parameter xname: The name of POV control.
93 * Returns: The number of hat.
95 unsigned new_hat(uint64_t id, const std::string& xname);
96 /**
97 * Get the number of axes.
99 unsigned axes() { return size_common(_axes); }
101 * Get the number of buttons.
103 unsigned buttons() { return size_common(_buttons); }
105 * Get the number of hats.
107 unsigned hats() { return size_common(_hats); }
109 * Report on specified axis.
111 * Parameter id: The number of axis to report.
112 * Parameter res: Place to write the calibrated axis value to.
113 * Returns: True if value has changed since last call, false otherwise.
115 bool axis(unsigned id, short& res) { return read_common(_axes, id, res); }
117 * Report on specified button.
119 * Parameter id: The number of button to report.
120 * Parameter res: Place to write the value to.
121 * Returns: True if value has changed since last call, false otherwise.
123 bool button(unsigned id, short& res) { return read_common(_buttons, id, res); }
125 * Report on specified hat.
127 * Parameter id: The number of hat to report.
128 * Parameter res: Place to write the value to.
129 * Returns: True if value has changed since last call, false otherwise.
131 bool hat(unsigned id, short& res) { return read_common(_hats, id, res); }
133 * Return name of axis.
135 * Parameter id: The axis number.
136 * Returns: The name of the axis, or "" if not found.
138 std::string axisname(unsigned id);
140 * Return axis calibration data.
142 * Parameter id: The axis number.
143 * Returns: The axis calibration (first minimum, then maximum).
145 std::pair<int64_t, int64_t> axiscalibration(unsigned id);
147 * Return name of button.
149 * Parameter id: The button number.
150 * Returns: The name of the button, or "" if not found.
152 std::string buttonname(unsigned id);
154 * Return name of hat.
156 * Parameter id: The hat number.
157 * Returns: The name of the hat, or "" if not found.
159 std::string hatname(unsigned id);
161 * Report possible change in axis value.
163 * Requests to update unknown axes are ignored.
165 * Parameter id: The ID of axis.
166 * Parameter value: The value.
168 void report_axis(uint64_t id, int64_t value);
170 * Report possible change in button value.
172 * Requests to update unknown buttons are ignored.
174 * Parameter id: The ID of button.
175 * Parameter value: The value.
177 void report_button(uint64_t id, bool value);
179 * Report possible change in POV value.
181 * Requests to update unknown POVs are ignored.
183 * Parameter id: The ID of POV.
184 * Parameter value: The angle in hundredths of degree clockwise from up, or negative if centered.
186 void report_pov(uint64_t id, int angle);
188 * Report on joystick.
190 std::string compose_report(unsigned jnum);
191 private:
192 struct change_info
194 change_info();
195 void update(short newv);
196 bool read(short& val);
197 private:
198 bool has_been_read;
199 short last_read;
200 short last_known;
202 struct hat_info
204 int xstate;
205 int ystate;
207 struct hat_axis_info
209 bool yflag; //Set on y axis, clear on x axis.
210 int64_t mindev;
211 unsigned hnum;
213 bool read_common(std::vector<change_info>& i, unsigned id, short& res);
214 unsigned size_common(std::vector<change_info>& i);
215 std::vector<change_info> _axes;
216 std::vector<change_info> _buttons;
217 std::vector<change_info> _hats;
218 std::map<uint64_t, unsigned> button_map;
219 std::map<uint64_t, unsigned> axis_map; //No hats here!
220 std::map<uint64_t, unsigned> pov_map;
221 std::map<uint64_t, std::pair<int64_t, int64_t>> aranges;
222 std::vector<hat_info> hatinfos;
223 std::map<uint64_t, hat_axis_info> hataxis_map;
224 std::map<unsigned, std::string> axisnames;
225 std::map<unsigned, std::string> buttonnames;
226 std::map<unsigned, std::string> hatnames;
227 std::string joyname;
230 #endif