1 #ifndef _controllerdata__hpp__included__
2 #define _controllerdata__hpp__included__
4 #include "fieldsplit.hpp"
9 * \brief What version to write as control version?
11 #define WRITE_CONTROL_VERSION 0
13 * \brief System control: Frame sync flag
15 #define CONTROL_FRAME_SYNC 0
17 * \brief System control: System reset button
19 #define CONTROL_SYSTEM_RESET 1
21 * \brief High part of cycle count for system reset (multiplier 10000).
23 #define CONTROL_SYSTEM_RESET_CYCLES_HI 2
25 * \brief Low part of cycle count for system reset (multiplier 1).
27 #define CONTROL_SYSTEM_RESET_CYCLES_LO 3
29 * \brief Number of system controls.
31 #define MAX_SYSTEM_CONTROLS 4
33 * \brief SNES has 2 controller ports.
37 * \brief Multitap can connect 4 controllers to a single port.
39 #define MAX_CONTROLLERS_PER_PORT 4
41 * \brief Ordinary gamepad has 12 buttons/axis total (more than anything else supported).
43 #define CONTROLLER_CONTROLS 12
45 * \brief The total number of controls (currently 100).
47 #define TOTAL_CONTROLS (MAX_SYSTEM_CONTROLS + MAX_PORTS * CONTROLLER_CONTROLS * MAX_CONTROLLERS_PER_PORT)
53 * \brief Subfield information
55 * Information about single subfield in control field. Subfields can be either single buttons or single axes.
57 struct control_subfield
60 * \brief Index used for subfield in control field
62 * This is the index this subfield is accessed as in operator[] method of class controlfield.
67 * \brief Type of subfield
69 * This type gives the type of the subfield, if it is a single button or single axis.
71 enum control_subfield_type
{
73 * \brief The subfield is a button.
78 * \brief The subfield is an axis.
84 * \brief Type of subfield
86 * This gives the type of the subfield.
88 enum control_subfield_type type
;
91 * \brief Make subfield structure
93 * Make subfield structure having specified index and type.
95 * \param _index The index to give to the subfield.
96 * \param _type The type to to give to the subfield.
98 control_subfield(unsigned _index
, enum control_subfield_type _type
) throw();
102 * \brief Field parser
104 * This class handles parsing fields consisting of subfields.
110 * \brief Create new field parser
112 * This constructor creates new field parser parsing field consisting of specified subfields.
114 * \param _subfields The subfields forming the field to parse.
115 * \throws std::bad_alloc Not enough memory to complete the operation.
117 controlfield(std::vector
<control_subfield
> _subfields
) throw(std::bad_alloc
);
120 * \brief Update values of subfields
122 * Read in string representation of a field and update values of all subfields.
124 * \param str The string representation
125 * \throws std::bad_alloc Not enough memory to complete the operation.
127 void set_field(const std::string
& str
) throw(std::bad_alloc
);
130 * \brief Read data in specified subfield.
132 * This method takes in a subfield index and gives the current data for said subfield.
134 * \param index The subfield index to read (0-based indexing).
135 * \throws std::logic_error Trying to access subfield index out of range
136 * \return The current value for that subfield
137 * \note If subfield index is in range but not mapped to any subfield, then 0 is returned.
139 short operator[](unsigned index
) throw(std::logic_error
);
142 * \brief Get number of valid subfield indices
144 * Obtain number of valid subfield indices. The indices are [0, N), where N is the return value of this method.
146 * \return The amount of accessable subfield indices.
148 unsigned indices() throw();
150 std::vector
<control_subfield
> subfields
;
151 std::vector
<short> values
;
155 * \brief System control field parser
157 * Subtype of controlfield (field parser) with fixed subfields of system field.
159 class controlfield_system
: public controlfield
163 * \brief Create new field parser
165 * This creates new parser for system fields.
167 * \param version The version to parse, currently must be 0.
168 * \throws std::bad_alloc Not enough memory.
169 * \throws std::runtime_error Unknown version.
171 controlfield_system(unsigned version
) throw(std::bad_alloc
, std::runtime_error
);
175 * \brief Gamepad control field parser
177 * Subtype of controlfield (field parser) with fixed subfields of gamepad controller (12 buttons).
179 class controlfield_gamepad
: public controlfield
183 * \brief Create new field parser
185 * This creates new parser for gamepad fields.
187 * \throws std::bad_alloc Not enough memory.
189 controlfield_gamepad() throw(std::bad_alloc
);
193 * \brief Mouse/Justifier control field parser
195 * Subtype of controlfield (field parser) with fixed subfields of mouse or justfier controller (2 axes, 2 buttons).
197 class controlfield_mousejustifier
: public controlfield
201 * \brief Create new field parser
203 * This creates new parser for mouse or justifier fields.
205 * \throws std::bad_alloc Not enough memory.
207 controlfield_mousejustifier() throw(std::bad_alloc
);
211 * \brief Superscope control field parser
213 * Subtype of controlfield (field parser) with fixed subfields of superscope controller (2 axes, 4 buttons).
215 class controlfield_superscope
: public controlfield
219 * \brief Create new field parser
221 * This creates new parser for superscope fields.
223 * \throws std::bad_alloc Not enough memory.
225 controlfield_superscope() throw(std::bad_alloc
);
235 * \brief Port decoder type
237 * This is type of functions that perform decoding of port fields.
239 * \param port The number of port to decode.
240 * \param line Field splitter, positioned so the first field of that port is read first.
241 * \param controls Buffer to place the read controls to.
242 * \throws std::bad_alloc Not enough memory.
243 * \throws std::runtime_error Bad input.
244 * \note This method needs to read all fields associated with the port.
246 typedef void (*fn_t
)(unsigned port
, fieldsplitter
& line
, short* controls
);
249 * \brief System field decoder
251 * This is a decoder for the system field.
253 * \param line The line to decode from. Assumed to be positioned on the system field.
254 * \param controls The buffer for controls read.
255 * \param version The version of control structure to read.
256 * \throws std::bad_alloc Not enough memory.
257 * \throws std::runtime_error Invalid field or invalid version.
258 * \note This can't be put into decoder_type as parameters are bit diffrent.
260 static void system(fieldsplitter
& line
, short* controls
, unsigned version
) throw(std::bad_alloc
,
264 * \brief Port decoder for type none.
266 * This is a port decoder for port type none.
268 * \param port The port number
269 * \param line The line to decode. Assumed to be positioned on start of port data.
270 * \param controls Buffer to decode the controls to.
271 * \throws std::bad_alloc Can't happen.
272 * \throws std::runtime_error Can't happen.
274 static void none(unsigned port
, fieldsplitter
& line
, short* controls
) throw(std::bad_alloc
,
278 * \brief Port decoder for type gamepad.
280 * This is a port decoder for port type gamepad.
282 * \param port The port number
283 * \param line The line to decode. Assumed to be positioned on start of port data.
284 * \param controls Buffer to decode the controls to.
285 * \throws std::bad_alloc Out of memory.
286 * \throws std::runtime_error Invalid controller data.
288 static void gamepad(unsigned port
, fieldsplitter
& line
, short* controls
) throw(std::bad_alloc
,
292 * \brief Port decoder for type multitap.
294 * This is a port decoder for port type multitap.
296 * \param port The port number
297 * \param line The line to decode. Assumed to be positioned on start of port data.
298 * \param controls Buffer to decode the controls to.
299 * \throws std::bad_alloc Out of memory.
300 * \throws std::runtime_error Invalid controller data.
302 static void multitap(unsigned port
, fieldsplitter
& line
, short* controls
) throw(std::bad_alloc
,
306 * \brief Port decoder for type mouse.
308 * This is a port decoder for port type mouse.
310 * \param port The port number
311 * \param line The line to decode. Assumed to be positioned on start of port data.
312 * \param controls Buffer to decode the controls to.
313 * \throws std::bad_alloc Out of memory.
314 * \throws std::runtime_error Invalid controller data.
316 static void mouse(unsigned port
, fieldsplitter
& line
, short* controls
) throw(std::bad_alloc
,
320 * \brief Port decoder for type superscope.
322 * This is a port decoder for port type superscope.
324 * \param port The port number
325 * \param line The line to decode. Assumed to be positioned on start of port data.
326 * \param controls Buffer to decode the controls to.
327 * \throws std::bad_alloc Out of memory.
328 * \throws std::runtime_error Invalid controller data.
330 static void superscope(unsigned port
, fieldsplitter
& line
, short* controls
) throw(std::bad_alloc
,
334 * \brief Port decoder for type justifier.
336 * This is a port decoder for port type justifier.
338 * \param port The port number
339 * \param line The line to decode. Assumed to be positioned on start of port data.
340 * \param controls Buffer to decode the controls to.
341 * \throws std::bad_alloc Out of memory.
342 * \throws std::runtime_error Invalid controller data.
344 static void justifier(unsigned port
, fieldsplitter
& line
, short* controls
) throw(std::bad_alloc
,
348 * \brief Port decoder for type justifiers.
350 * This is a port decoder for port type justifiers.
352 * \param port The port number
353 * \param line The line to decode. Assumed to be positioned on start of port data.
354 * \param controls Buffer to decode the controls to.
355 * \throws std::bad_alloc Out of memory.
356 * \throws std::runtime_error Invalid controller data.
358 static void justifiers(unsigned port
, fieldsplitter
& line
, short* controls
) throw(std::bad_alloc
,
369 * \brief Port encoder type
371 * This is the type of functions that perform encoding of port fields.
373 * \param port To number of port to encode.
374 * \param controls Buffer to read the controls from.
375 * \throws std::bad_alloc Not enough memory.
377 typedef std::string (*fn_t
)(unsigned port
, const short* controls
);
380 * \brief Encoder for system field
382 * This is encoder for the system field.
384 * \param controls The controls to encode.
385 * \throws std::bad_alloc Out of memory.
386 * \note This can't be put into encoder_type as parameters are bit diffrent.
388 static std::string
system(const short* controls
) throw(std::bad_alloc
);
391 * \brief Port encoder for type none.
393 * This is a port encoder for port type none.
395 * \param port The port number
396 * \param controls The controls to encode.
397 * \return The encoded fields.
398 * \throws std::bad_alloc Out of memory.
400 static std::string
none(unsigned port
, const short* controls
) throw(std::bad_alloc
);
403 * \brief Port encoder for type gamepad.
405 * This is a port encoder for port type gamepad.
407 * \param port The port number
408 * \param controls The controls to encode.
409 * \return The encoded fields.
410 * \throws std::bad_alloc Out of memory.
412 static std::string
gamepad(unsigned port
, const short* controls
) throw(std::bad_alloc
);
415 * \brief Port encoder for type multitap.
417 * This is a port encoder for port type multitap.
419 * \param port The port number
420 * \param controls The controls to encode.
421 * \return The encoded fields.
422 * \throws std::bad_alloc Out of memory.
424 static std::string
multitap(unsigned port
, const short* controls
) throw(std::bad_alloc
);
427 * \brief Port encoder for type mouse.
429 * This is a port encoder for port type mouse.
431 * \param port The port number
432 * \param controls The controls to encode.
433 * \return The encoded fields.
434 * \throws std::bad_alloc Out of memory.
436 static std::string
mouse(unsigned port
, const short* controls
) throw(std::bad_alloc
);
439 * \brief Port encoder for type superscope.
441 * This is a port encoder for port type superscope.
443 * \param port The port number
444 * \param controls The controls to encode.
445 * \return The encoded fields.
446 * \throws std::bad_alloc Out of memory.
448 static std::string
superscope(unsigned port
, const short* controls
) throw(std::bad_alloc
);
451 * \brief Port encoder for type justifier.
453 * This is a port encoder for port type justifier.
455 * \param port The port number
456 * \param controls The controls to encode.
457 * \return The encoded fields.
458 * \throws std::bad_alloc Out of memory.
460 static std::string
justifier(unsigned port
, const short* controls
) throw(std::bad_alloc
);
463 * \brief Port encoder for type justifiers.
465 * This is a port encoder for port type justifiers.
467 * \param port The port number
468 * \param controls The controls to encode.
469 * \return The encoded fields.
470 * \throws std::bad_alloc Out of memory.
472 static std::string
justifiers(unsigned port
, const short* controls
) throw(std::bad_alloc
);
476 * \brief Controls for single (sub)frame
478 * This structure holds controls for single (sub)frame or instant of time.
483 * \brief Create new controls structure
485 * Creates new controls structure. All buttons are released and all axes are 0 (neutral).
487 * \param sync If true, write 1 (pressed) to frame sync subfield, else write 0 (released). Default false.
489 controls_t(bool sync
= false) throw();
492 * \brief Read in a line using specified decoders
494 * This constructor takes in a line of input, port decoders and system field version and decodes the controls.
496 * \param line The line to decode.
497 * \param decoders The decoders for each port.
498 * \param version Version for the system field.
499 * \throws std::bad_alloc Not enough memory.
500 * \throws std::runtime_error Invalid input line.
502 controls_t(const std::string
& line
, const std::vector
<cdecode::fn_t
>& decoders
, unsigned version
)
503 throw(std::bad_alloc
, std::runtime_error
);
507 * This method takes in port encoders and encodes the controls.
509 * \param encoders The encoders for each port.
510 * \throws std::bad_alloc Not enough memory.
512 std::string
tostring(const std::vector
<cencode::fn_t
>& encoders
) const throw(std::bad_alloc
);
515 * \brief Read control value by (port, controller, control) tuple.
517 * This method takes in controller (port, controller, control) tuple and returns reference to the value of that
520 * \param port The port number
521 * \param controller The controller number within that port.
522 * \param control The control number within that controller.
523 * \return Reference to control value.
524 * \throws std::logic_error port, controller or control is invalid.
526 const short& operator()(unsigned port
, unsigned controller
, unsigned control
) const throw(std::logic_error
);
529 * \brief Read control value by system index.
531 * This method takes in system index and returns reference to the value of that control.
533 * \param control The system index of control.
534 * \return Reference to control value.
535 * \throws std::logic_error Invalid control index.
537 const short& operator()(unsigned control
) const throw(std::logic_error
);
540 * \brief Read control value by (port, controller, control) tuple.
542 * This method takes in controller (port, controller, control) tuple and returns reference to the value of that
545 * \param port The port number
546 * \param controller The controller number within that port.
547 * \param control The control number within that controller.
548 * \return Reference to control value.
549 * \throws std::logic_error port, controller or control is invalid.
551 short& operator()(unsigned port
, unsigned controller
, unsigned control
) throw(std::logic_error
);
554 * \brief Read control value by system index.
556 * This method takes in system index and returns reference to the value of that control.
558 * \param control The system index of control.
559 * \return Reference to control value.
560 * \throws std::logic_error Invalid control index.
562 short& operator()(unsigned control
) throw(std::logic_error
);
564 controls_t
operator^(controls_t other
) throw();
567 * \brief Raw controller data
569 * This field contains the raw controller data. Avoid manipulating directly.
571 short controls
[TOTAL_CONTROLS
];
576 bool operator==(const controls_t
& c
) const throw();
580 * \brief Type of port
582 * This enumeration gives the type of port.
589 PT_NONE
= 0, //Nothing connected to port.
595 * \brief Multitap (with 4 gamepads connected)
603 * \brief Superscope (only allowed for port 2).
607 * \brief Justifier (only allowed for port 2).
611 * \brief 2 Justifiers (only allowed for port 2).
615 * \brief Number of controller types.
619 * \brief Invalid controller type.
625 * \brief Type of port
627 * This enumeration gives the type of device.
654 * \brief Information about port type.
659 cdecode::fn_t decoder
;
660 cencode::fn_t encoder
;
665 static const port_type
& lookup(const std::string
& name
, bool port2
= true) throw(std::bad_alloc
,
671 * \brief Information about port types.
673 extern port_type port_types
[];
676 * \brief Translate controller index to system index.
678 * This method takes in controller (port, controller, control) tuple and returns the system index corresponding to
681 * \param port The port number
682 * \param controller The controller number within that port.
683 * \param control The control number within that controller.
684 * \return System control index.
685 * \throws std::logic_error port, controller or control is invalid.
687 unsigned ccindex2(unsigned port
, unsigned controller
, unsigned control
) throw(std::logic_error
);