KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
controller.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/maple/controller.h
4 Copyright (C) 2000-2002 Jordan DeLong
5 Copyright (C) 2000-2002 Megan Potter
6 Copyright (C) 2023 Falco Girgis
7
8 Thanks to Marcus Comstedt for information on the controller.
9*/
10
11/** \file dc/maple/controller.h
12 \brief Definitions for using the controller device.
13 \ingroup controller
14
15 This file contains the definitions needed to access the Maple controller
16 device. Obviously, this corresponds to the MAPLE_FUNC_CONTROLLER function
17 code.
18
19 \author Jordan DeLong
20 \author Megan Potter
21 \author Falco Girgis
22*/
23
24#ifndef __DC_MAPLE_CONTROLLER_H
25#define __DC_MAPLE_CONTROLLER_H
26
27#include <sys/cdefs.h>
28__BEGIN_DECLS
29
30#include <arch/types.h>
31#include <stdint.h>
32
33/** \defgroup controller Controller
34 \brief Controller Maple Device API
35 \ingroup peripherals
36
37 This module contains the public API for the controller
38 maple driver.
39
40 A standard, first-party Dreamcast controller has
41 the following button configuration:
42
43 ___________
44 / | __ | \
45 L trigger ------| / | | | | \ |----- R trigger
46 _|__/ | |__| | \_|__
47 | _ \____/ |
48 Joystick ----|-/ \ (Y) |
49 | \_/ (X) (B)|
50 | _ (A) |
51 | _| |_ |
52 D-Pad ----|-|_ _| |
53 | |_| /\ |
54 \ /__\ /
55 \ _____|_______ /
56 \ / | \ /
57 \/ | \/
58 Start button
59
60 You can grab a pointer to a connected controller by
61 using the following:
62
63 maple_device_t *device = maple_enum_type(N, MAPLE_FUNC_CONTROLLER);
64
65 if(device) printf("Controller found!\n");
66 else printf("Controller not found!\n");
67
68 where N is the controller number. 0 would be the first
69 controller found, which may not necessarily be on port A.
70*/
71
72/** \defgroup controller_inputs Querying Inputs
73 \brief API used to query for input state
74 \ingroup controller
75
76 The following API is used to check for a controller's input state.
77
78 You can grab a controller's state structure, containing the state
79 of all of its inputs by using:
80
81 cont_state_t *state = (cont_state_t *)maple_dev_status(device);
82
83 Next you can check for the state of a particular button with:
84
85 if(state->a) // Check via bitfield
86 printf("Pressed A".);
87
88 or
89
90 if(state->buttons & CONT_A) // Check via applying bitmask
91 printf("Pressed A.")
92*/
93
94/** \defgroup controller_input_masks Inputs
95 \brief Collection of all status masks for checking input
96 \ingroup controller_inputs
97
98 A set of bitmasks representing each input source on a controller, used to
99 check its status.
100
101 @{
102*/
103#define CONT_C (1<<0) /**< \brief C button Mask. */
104#define CONT_B (1<<1) /**< \brief B button Mask. */
105#define CONT_A (1<<2) /**< \brief A button Mask. */
106#define CONT_START (1<<3) /**< \brief Start button Mask. */
107#define CONT_DPAD_UP (1<<4) /**< \brief Main Dpad Up button Mask. */
108#define CONT_DPAD_DOWN (1<<5) /**< \brief Main Dpad Down button Mask. */
109#define CONT_DPAD_LEFT (1<<6) /**< \brief Main Dpad Left button Mask. */
110#define CONT_DPAD_RIGHT (1<<7) /**< \brief Main Dpad right button Mask. */
111#define CONT_Z (1<<8) /**< \brief Z button Mask. */
112#define CONT_Y (1<<9) /**< \brief Y button Mask. */
113#define CONT_X (1<<10) /**< \brief X button Mask. */
114#define CONT_D (1<<11) /**< \brief D button Mask. */
115#define CONT_DPAD2_UP (1<<12) /**< \brief Secondary Dpad Up button Mask. */
116#define CONT_DPAD2_DOWN (1<<13) /**< \brief Secondary Dpad Down button Mask. */
117#define CONT_DPAD2_LEFT (1<<14) /**< \brief Secondary Dpad Left button Mask. */
118#define CONT_DPAD2_RIGHT (1<<15) /**< \brief Secondary Dpad Right button Mask. */
119/** @} */
120
121/** \brief Controller buttons for standard reset action
122 \ingroup controller_inputs
123
124 Convenience macro providing the standard button combination
125 used as a reset mechanism by most retail games.
126*/
127#define CONT_RESET_BUTTONS (CONT_A | CONT_B | CONT_X | CONT_Y | CONT_START)
128
129/** \brief Controller state structure.
130 \ingroup controller_inputs
131
132 This structure contains information about the status of the controller
133 device and can be fetched by casting the result of maple_dev_status() to
134 this structure.
135
136 A 1 bit in the buttons' bitfield indicates that a button is pressed, and the
137 joyx, joyy, joy2x, joy2 values are all 0 based (0 is centered).
138
139 \note
140 Whether a particular field or button is actually used by the controller
141 depends upon its capabilities. See \ref controller_query_caps.
142
143 \sa maple_dev_status
144*/
145typedef struct cont_state {
146 union {
147 /** \brief bit-packed controller button states
148 \sa controller_buttons
149 */
150 uint32_t buttons;
151 struct {
152 uint32_t c: 1; /**< \brief C button value. */
153 uint32_t b: 1; /**< \brief B button value. */
154 uint32_t a: 1; /**< \brief A button value. */
155 uint32_t start: 1; /**< \brief Start button value. */
156 uint32_t dpad_up: 1; /**< \brief Main Dpad Up button value. */
157 uint32_t dpad_down: 1; /**< \brief Main Dpad Down button value. */
158 uint32_t dpad_left: 1; /**< \brief Main Dpad Left button value. */
159 uint32_t dpad_right: 1; /**< \brief Main Dpad Right button value. */
160 uint32_t z: 1; /**< \brief Z button value. */
161 uint32_t y: 1; /**< \brief Y button value. */
162 uint32_t x: 1; /**< \brief X button value. */
163 uint32_t d: 1; /**< \brief D button value. */
164 uint32_t dpad2_up: 1; /**< \brief Secondary Dpad Up button value. */
165 uint32_t dpad2_down: 1; /**< \brief Secondary Dpad Down button value. */
166 uint32_t dpad2_left: 1; /**< \brief Secondary Dpad Left button value. */
167 uint32_t dpad2_right: 1; /**< \brief Secondary Dpad Right button value. */
168 uint32_t: 16;
169 };
170 };
171
172 int ltrig; /**< \brief Left trigger value (0-255). */
173 int rtrig; /**< \brief Right trigger value (0-255). */
174 int joyx; /**< \brief Main joystick x-axis value (-128 - 127). */
175 int joyy; /**< \brief Main joystick y-axis value. */
176 int joy2x; /**< \brief Secondary joystick x-axis value. */
177 int joy2y; /**< \brief Secondary joystick y-axis value. */
179
180/** \brief Controller automatic callback type.
181 \ingroup controller_inputs
182
183 Functions of this type can be set with cont_btn_callback() to respond
184 automatically to the specified set of buttons being pressed. This can be
185 used, for instance, to implement the standard A+B+X+Y+Start method of ending
186 the program running.
187
188 \param addr Maple BUS address to poll for the button mask
189 on, or 0 for all ports.
190 \param btns Mask of all buttons which should be pressed to
191 trigger the callback.
192
193 \sa cont_btn_callback
194*/
195typedef void (*cont_btn_callback_t)(uint8_t addr, uint32_t btns);
196
197/** \brief Set an automatic button press callback.
198 \ingroup controller_inputs
199
200 This function sets a callback function to be called when the specified
201 controller has the set of buttons given pressed.
202
203 \note
204 The callback gets invoked for the given maple port; however, providing
205 an address of '0' will cause it to be invoked for any port with a
206 device pressing the given buttons. Since you are passed back the address
207 of this device, You are free to implement your own filtering logic within
208 your callback. Any callback with addr==0 will be installed to the end of
209 the list of callbacks and will run after callbacks with the same btns but
210 a specified address.
211
212 \param addr The controller to listen on (or 0 for all ports).
213 This value can be obtained by using maple_addr().
214 \param btns The buttons bitmask to match.
215 \param cb The callback to call when the buttons are pressed.
216 Passing NULL will uninstall all callbacks on the
217 addr/btns combination.
218*/
219int cont_btn_callback(uint8_t addr, uint32_t btns, cont_btn_callback_t cb);
220
221/** \defgroup controller_query_caps Querying Capabilities
222 \brief API used to query for a controller's capabilities
223 \ingroup controller
224
225 The following API is used to query for the support of individual
226 or groups of capabilities by a particular device.
227*/
228
229/** \defgroup controller_caps Capabilities
230 \brief Bit masks used to identify controller capabilities
231 \ingroup controller_query_caps
232
233 These bits will be set in the function_data for the controller's deviceinfo
234 if the controller supports the corresponding button/axis capability.
235
236 \note
237 The ordering here is so that they match the order found in
238 \ref controller_input_masks.
239
240 @{
241*/
242#define CONT_CAPABILITY_C (1<<24) /**< \brief C button capability mask. */
243#define CONT_CAPABILITY_B (1<<25) /**< \brief B button capability mask. */
244#define CONT_CAPABILITY_A (1<<26) /**< \brief A button capability mask. */
245#define CONT_CAPABILITY_START (1<<27) /**< \brief Start button capability mask. */
246#define CONT_CAPABILITY_DPAD_UP (1<<28) /**< \brief First Dpad up capability mask. */
247#define CONT_CAPABILITY_DPAD_DOWN (1<<29) /**< \brief First Dpad down capability mask. */
248#define CONT_CAPABILITY_DPAD_LEFT (1<<30) /**< \brief First Dpad left capability mask. */
249#define CONT_CAPABILITY_DPAD_RIGHT (1<<31) /**< \brief First Dpad right capability mask. */
250#define CONT_CAPABILITY_Z (1<<16) /**< \brief Z button capability mask. */
251#define CONT_CAPABILITY_Y (1<<17) /**< \brief Y button capability mask. */
252#define CONT_CAPABILITY_X (1<<18) /**< \brief X button capability mask. */
253#define CONT_CAPABILITY_D (1<<19) /**< \brief D button capability mask. */
254#define CONT_CAPABILITY_DPAD2_UP (1<<20) /**< \brief Second Dpad up capability mask. */
255#define CONT_CAPABILITY_DPAD2_DOWN (1<<21) /**< \brief Second Dpad down capability mask. */
256#define CONT_CAPABILITY_DPAD2_LEFT (1<<22) /**< \brief Second Dpad left capability mask. */
257#define CONT_CAPABILITY_DPAD2_RIGHT (1<<23) /**< \brief Second Dpad right capability mask. */
258#define CONT_CAPABILITY_RTRIG (1<<8) /**< \brief Right trigger capability mask. */
259#define CONT_CAPABILITY_LTRIG (1<<9) /**< \brief Left trigger capability mask. */
260#define CONT_CAPABILITY_ANALOG_X (1<<10) /**< \brief First analog X axis capability mask. */
261#define CONT_CAPABILITY_ANALOG_Y (1<<11) /**< \brief First analog Y axis capability mask. */
262#define CONT_CAPABILITY_ANALOG2_X (1<<12) /**< \brief Second analog X axis capability mask. */
263#define CONT_CAPABILITY_ANALOG2_Y (1<<13) /**< \brief Second analog Y axis capability mask. */
264/** @} */
265
266/** \defgroup controller_caps_groups Capability Groups
267 \brief Bit masks representing common groups of capabilities
268 \ingroup controller_query_caps
269
270 These are a sets of capabilities providing a
271 convenient way to test for high-level features,
272 such as dual-analog sticks or extra buttons.
273
274 @{
275*/
276/** \brief Standard button (A, B, X, Y, Start) controller capabilities */
277#define CONT_CAPABILITIES_STANDARD_BUTTONS (CONT_CAPABILITY_A | \
278 CONT_CAPABILITY_B | \
279 CONT_CAPABILITY_X | \
280 CONT_CAPABILITY_Y | \
281 CONT_CAPABILITY_START)
282
283/** \brief Directional pad (up, down, left right) controller capabilities */
284#define CONT_CAPABILITIES_DPAD (CONT_CAPABILITY_DPAD_UP | \
285 CONT_CAPABILITY_DPAD_DOWN | \
286 CONT_CAPABILITY_DPAD_LEFT | \
287 CONT_CAPABILITY_DPAD_RIGHT)
288
289/** \brief Analog stick (X, Y axes) controller capabilities */
290#define CONT_CAPABILITIES_ANALOG (CONT_CAPABILITY_ANALOG_X | \
291 CONT_CAPABILITY_ANALOG_Y)
292
293/** \brief Trigger (L, R lever) controller capabilities */
294#define CONT_CAPABILITIES_TRIGGERS (CONT_CAPABILITY_LTRIG | \
295 CONT_CAPABILITY_RTRIG)
296
297/** \brief Extended button (C, Z) controller capabilities */
298#define CONT_CAPABILITIES_EXTENDED_BUTTONS (CONT_CAPABILITY_C | \
299 CONT_CAPABILITY_Z)
300
301/** \brief Secondary directional pad (up, down, left, right) controller capabilities */
302#define CONT_CAPABILITIES_SECONDARY_DPAD (CONT_CAPABILITY_DPAD2_UP | \
303 CONT_CAPABILITY_DPAD2_DOWN | \
304 CONT_CAPABILITY_DPAD2_LEFT | \
305 CONT_CAPABILITY_DPAD2_RIGHT)
306
307/** \brief Secondary analog stick (X, Y axes) controller capabilities */
308#define CONT_CAPABILITIES_SECONDARY_ANALOG (CONT_CAPABILITY_ANALOG2_X | \
309 CONT_CAPABILITY_ANALOG2_Y)
310
311/** \brief Both directional pads (up, down, left right) controller capabilities */
312#define CONT_CAPABILITIES_DUAL_DPAD (CONT_CAPABILITIES_DPAD | \
313 CONT_CAPABILITIES_SECONDARY_DPAD)
314
315/** \brief Both analog sticks (X, Y axes) controller capabilities */
316#define CONT_CAPABILITIES_DUAL_ANALOG (CONT_CAPABILITIES_ANALOG | \
317 CONT_CAPABILITIES_SECONDARY_ANALOG)
318
319/* Forward declaration */
320struct maple_device;
321
322/** \brief Check for controller capabilities
323 \ingroup controller_query_caps
324
325 Checks whether or not a controller implements the capabilities
326 associated with the given type.
327
328 \note
329 Controller capability reporting is an extremely generic mechanism,
330 such that many peripherals may implement the same capability in
331 completely different ways. For example, the Samba De Amigo maraca
332 controller will advertise itself as a dual-analog device, with each
333 maraca being an analog stick.
334
335 \param cont Pointer to a Maple device structure which
336 implements the CONTROLLER function.
337 \param capabilities Capability mask the controller is expected
338 to implement
339
340 \retval 1 The controller supports the given capabilities.
341 \retval 0 The controller doesn't support the given capabilities.
342 \retval -1 Invalid controller.
343
344 \sa cont_is_type
345*/
346int cont_has_capabilities(const struct maple_device *cont, uint32_t capabilities);
347/** @} */
348
349/** \defgroup controller_query_types Querying Types
350 \brief API for determining controller types
351 \ingroup controller
352
353 The following API is for detecting between different types
354 of standard controllers. These controllers are not identified
355 by specific model but are instead identified solely by capabilities,
356 so that homebrew software can remain generic and future-proof to later
357 homebrew controllers or exotic, untested 3rd party peripherals.
358
359 \warning
360 Usually you want to check if a controller <i>supports the
361 capabilities</i> of another controller, not whether it is has
362 the <i>exact</i> same capabilities of a controller. For example,
363 a controller that happens to come along supporting a dual analog
364 stick but is otherwise the same layout as a standard controller
365 would not match the standard controller type; however, it would
366 implement its capabilities. There exist 3rd party adapters for
367 connecting dual-analog PS2 controllers to DC which operate
368 like this today.
369
370 \note
371 If you really want to hard-code the detection of a certain
372 exact model or brand of controller, instead of basing your
373 detection upon capabilities, check for its product_name
374 or license within the maple_devinfo structure.
375
376 \sa cont_has_capabilities, maple_devinfo
377*/
378
379/** \defgroup controller_types Types
380 \brief Preconfigured capabilities for standard controllers
381 \ingroup controller_query_types
382
383 Aggregate capability mask containing all capabilities
384 which are implemented for a particular controller type.
385 For example, the standard controller type is simply a
386 combination of the following capabilities:
387 - Standard buttons
388 - Triggers
389 - Dpad
390 - Analog
391
392 \note
393 Because these are technically just capability masks,
394 a type may also be passed to cont_has_capabilities()
395 for detecting whether something has <i>at least</i>
396 the capabilities of a type.
397
398 @{
399*/
400/** \brief Standard controller type */
401#define CONT_TYPE_STANDARD_CONTROLLER (CONT_CAPABILITIES_STANDARD_BUTTONS | \
402 CONT_CAPABILITIES_TRIGGERS | \
403 CONT_CAPABILITIES_DPAD | \
404 CONT_CAPABILITIES_ANALOG)
405
406/** \brief Dual analog controller type */
407#define CONT_TYPE_DUAL_ANALOG_CONTROLLER (CONT_CAPABILITIES_STANDARD_BUTTONS | \
408 CONT_CAPABILITIES_TRIGGERS | \
409 CONT_CAPABILITIES_DPAD | \
410 CONT_CAPABILITIES_DUAL_ANALOG)
411
412/** \brief ASCII fighting pad controller type */
413#define CONT_TYPE_ASCII_PAD (CONT_CAPABILITIES_STANDARD_BUTTONS | \
414 CONT_CAPABILITIES_EXTENDED_BUTTONS | \
415 CONT_CAPABILITIES_DPAD)
416
417/** \brief Arcade stick controller type */
418#define CONT_TYPE_ARCADE_STICK (CONT_CAPABILITIES_STANDARD_BUTTONS | \
419 CONT_CAPABILITIES_EXTENDED_BUTTONS | \
420 CONT_CAPABILITIES_DPAD)
421
422/** \brief Twin stick joystick controller type */
423#define CONT_TYPE_TWIN_STICK (CONT_CAPABILITIES_STANDARD_BUTTONS | \
424 CONT_CAPABILITIES_EXTENDED_BUTTONS | \
425 CONT_CAPABILITY_D | \
426 CONT_CAPABILITIES_DUAL_DPAD)
427
428/** \brief ASCII Mission Stick controller type */
429#define CONT_TYPE_ASCII_MISSION_STICK (CONT_CAPABILITIES_STANDARD_BUTTONS | \
430 CONT_CAPABILITIES_DUAL_DPAD | \
431 CONT_CAPABILITIES_TRIGGERS | \
432 CONT_CAPABILITIES_ANALOG)
433
434/** \brief Racing wheel/controller type */
435#define CONT_TYPE_RACING_CONTROLLER (CONT_CAPABILITY_DPAD_UP | \
436 CONT_CAPABILITY_DPAD_DOWN | \
437 CONT_CAPABILITY_A | \
438 CONT_CAPABILITY_B | \
439 CONT_CAPABILITY_START | \
440 CONT_CAPABILITIES_TRIGGERS | \
441 CONT_CAPABILITY_ANALOG_X \
442 CONT_CAPABILITIES_SECONDARY_ANALOG)
443
444/** \brief Samba De Amigo maraca controller type */
445#define CONT_TYPE_MARACAS (CONT_CAPABILITY_A | \
446 CONT_CAPABILITY_B | \
447 CONT_CAPABILITY_D | \
448 CONT_CAPABILITY_START | \
449 CONT_CAPABILITIES_EXTENDED_BUTTONS | \
450 CONT_CAPABILITIES_DUAL_ANALOG)
451
452/** \brief Dance Dance Revolution mat controller type */
453#define CONT_TYPE_DANCE_MAT (CONT_CAPABILITY_A | \
454 CONT_CAPABILITY_B | \
455 CONT_CAPABILITY_START | \
456 CONT_CAPABILITIES_DPAD)
457
458/** \brief Fishing rod controller type */
459#define CONT_TYPE_FISHING_ROD (CONT_CAPABILITIES_STANDARD_BUTTONS | \
460 CONT_CAPABILITIES_DPAD | \
461 CONT_CAPABILITIES_TRIGGERS | \
462 CONT_CAPABILITIES_DUAL_ANALOG)
463
464/** \brief Pop'n'Music controller type */
465#define CONT_TYPE_POP_N_MUSIC (CONT_CAPABILITIES_STANDARD_BUTTONS | \
466 CONT_CAPABILITY_C | \
467 CONT_CAPABILITIES_DPAD)
468
469/** \brief Densha de Go! controller type */
470#define CONT_TYPE_DENSHA_DE_GO (CONT_CAPABILITIES_STANDARD_BUTTONS | \
471 CONT_CAPABILITIES_EXTENDED_BUTTONS | \
472 CONT_CAPABILITY_D | \
473 CONT_CAPABILITIES_DPAD)
474/** @} */
475
476/** \brief Check for controller type
477 \ingroup controller_query_types
478
479 Checks whether or not a controller has the <i>exact</i>
480 capabilities associated with the given type.
481
482 \warning
483 Just because a controller has all of the same capabilities of a
484 type does not mean that it's that exact type. For example, the
485 ASCII Pad and Arcade Stick both implement the same capabilities,
486 although they are not the same controllers. They would be
487 indistinguishable here, by design, so that you are able to
488 generalize to a collection of 1st or 3rd party controllers
489 easily.
490
491 \param cont Pointer to a Maple device structure which
492 implements the CONTROLLER function.
493 \param type Type identifier or capability mask the
494 controller is expected to match
495
496 \retval 1 The controller matches the given type.
497 \retval 0 The controller doesn't match the given type.
498 \retval -1 Invalid controller.
499
500 \sa cont_has_capabilities
501*/
502int cont_is_type(const struct maple_device *cont, uint32_t type);
503
504/* \cond */
505/* Init / Shutdown */
506void cont_init(void);
507void cont_shutdown(void);
508/* \endcond */
509
510__END_DECLS
511
512#endif /* __DC_MAPLE_CONTROLLER_H */
513
int cont_btn_callback(uint8_t addr, uint32_t btns, cont_btn_callback_t cb)
Set an automatic button press callback.
void(* cont_btn_callback_t)(uint8_t addr, uint32_t btns)
Controller automatic callback type.
Definition controller.h:195
int cont_has_capabilities(const struct maple_device *cont, uint32_t capabilities)
Check for controller capabilities.
int cont_is_type(const struct maple_device *cont, uint32_t type)
Check for controller type.
Controller state structure.
Definition controller.h:145
int joy2y
Secondary joystick y-axis value.
Definition controller.h:177
uint32_t b
B button value.
Definition controller.h:153
uint32_t dpad_down
Main Dpad Down button value.
Definition controller.h:157
uint32_t z
Z button value.
Definition controller.h:160
uint32_t dpad2_down
Secondary Dpad Down button value.
Definition controller.h:165
uint32_t dpad2_up
Secondary Dpad Up button value.
Definition controller.h:164
int ltrig
Left trigger value (0-255).
Definition controller.h:172
uint32_t dpad2_left
Secondary Dpad Left button value.
Definition controller.h:166
uint32_t dpad2_right
Secondary Dpad Right button value.
Definition controller.h:167
uint32_t a
A button value.
Definition controller.h:154
uint32_t c
C button value.
Definition controller.h:152
uint32_t dpad_right
Main Dpad Right button value.
Definition controller.h:159
int joyx
Main joystick x-axis value (-128 - 127).
Definition controller.h:174
uint32_t buttons
bit-packed controller button states
Definition controller.h:150
int joy2x
Secondary joystick x-axis value.
Definition controller.h:176
uint32_t dpad_left
Main Dpad Left button value.
Definition controller.h:158
uint32_t y
Y button value.
Definition controller.h:161
int joyy
Main joystick y-axis value.
Definition controller.h:175
int rtrig
Right trigger value (0-255).
Definition controller.h:173
uint32_t x
X button value.
Definition controller.h:162
uint32_t d
D button value.
Definition controller.h:163
uint32_t start
Start button value.
Definition controller.h:155
uint32_t dpad_up
Main Dpad Up button value.
Definition controller.h:156
Common integer types.