Jo Engine  2024.04.28
Jo Sega Saturn Engine
input.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2024, Johannes Fetz (johannesfetz@gmail.com)
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are met:
8 ** * Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** * Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** * Neither the name of the Johannes Fetz nor the
14 ** names of its contributors may be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 ** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
36 #ifndef __JO_INPUT_H__
37 # define __JO_INPUT_H__
38 
39 # define JO_INPUT_MAX_DEVICE (12)
40 
41 // This device ID is not present in SGL header files
42 #define PER_ID_NightsPad 0x16 /* Nights 3D pad */
43 
44 #if JO_COMPILE_USING_SGL
45 extern PerDigital jo_inputs[JO_INPUT_MAX_DEVICE];
46 # define __JO_KEY_PRESSED(PORT, KEY) ((jo_inputs[PORT].data & KEY) == 0)
47 #else
48 # define __JO_KEY_PRESSED(PORT, KEY) ((jo_inputs[PORT].pressed & KEY) == KEY)
49 #endif
50 
51 #if !JO_COMPILE_USING_SGL
52 
56 typedef struct
57 {
58  unsigned char id;
59  unsigned short pressed;
60  unsigned short on_keydown;
61  unsigned short on_keyup;
62 } jo_input;
63 
67 extern jo_input jo_inputs[JO_INPUT_MAX_DEVICE];
68 
69 #endif
70 
73 #if JO_COMPILE_USING_SGL
74 typedef enum
75 {
76  JO_KEY_RIGHT = 1 << 15,
77  JO_KEY_LEFT = 1 << 14,
78  JO_KEY_DOWN = 1 << 13,
79  JO_KEY_UP = 1 << 12,
80  JO_KEY_START = 1 << 11,
81  JO_KEY_A = 1 << 10,
82  JO_KEY_B = 1 << 8,
83  JO_KEY_C = 1 << 9,
84  JO_KEY_X = 1 << 6,
85  JO_KEY_Y = 1 << 5,
86  JO_KEY_Z = 1 << 4,
87  JO_KEY_L = 1 << 3,
88  JO_KEY_R = 1 << 7,
89 
93  JO_MOUSE_RIGHT_BUTTON = 1 << 9
95 #else
96 typedef enum
97 {
98  JO_KEY_RIGHT = 1 << 7,
99  JO_KEY_LEFT = 1 << 6,
100  JO_KEY_DOWN = 1 << 5,
101  JO_KEY_UP = 1 << 4,
102  JO_KEY_START = 1 << 11,
103  JO_KEY_A = 1 << 10,
104  JO_KEY_B = 1 << 8,
105  JO_KEY_C = 1 << 9,
106  JO_KEY_X = 1 << 2,
107  JO_KEY_Y = 1 << 1,
108  JO_KEY_Z = 1 << 0,
109  JO_KEY_L = 1 << 15,
110  JO_KEY_R = 1 << 3,
111 
112  JO_MOUSE_START_BUTTON = 1 << 11,
113  JO_MOUSE_LEFT_BUTTON = 1 << 10,
114  JO_MOUSE_MIDDLE_BUTTON = 1 << 8,
115  JO_MOUSE_RIGHT_BUTTON = 1 << 9
117 #endif
118 
121 typedef enum
122 {
129 
132  JoNightsPad = 5
134 
137 typedef enum
138 {
139  JoAxis1 = 0,
140  JoAxis2 = 1,
141  JoAxis3 = 2,
142  JoAxis4 = 3,
143  JoAxis5 = 4,
144  JoAxis6 = 5,
145 } jo_input_axis;
146 
152 
157 static __jo_force_inline bool jo_is_input_analog(const int port)
158 {
159  // TODO: Add other analog devices (wheel, mission stick)
160  return (jo_inputs[port].id == PER_ID_NightsPad);
161 }
162 
167 static __jo_force_inline bool jo_is_input_available(const int port)
168 {
169  return (jo_inputs[port].id != PER_ID_NotConnect);
170 }
171 
176 
182 unsigned char jo_get_input_axis(const int port, const jo_input_axis axis);
183 
189 static __jo_force_inline bool jo_is_input_key_pressed(const int port, const jo_gamepad_keys key)
190 {
191  return __JO_KEY_PRESSED(port, key);
192 }
193 
199 {
200  if (__JO_KEY_PRESSED(port, JO_KEY_UP))
201  {
202  if (__JO_KEY_PRESSED(port, JO_KEY_LEFT))
203  return (UP_LEFT);
204  if (__JO_KEY_PRESSED(port, JO_KEY_RIGHT))
205  return (UP_RIGHT);
206  return (UP);
207  }
208  if (__JO_KEY_PRESSED(port, JO_KEY_DOWN))
209  {
210  if (__JO_KEY_PRESSED(port, JO_KEY_LEFT))
211  return (DOWN_LEFT);
212  if (__JO_KEY_PRESSED(port, JO_KEY_RIGHT))
213  return (DOWN_RIGHT);
214  return (DOWN);
215  }
216  if (__JO_KEY_PRESSED(port, JO_KEY_LEFT))
217  return (LEFT);
218  if (__JO_KEY_PRESSED(port, JO_KEY_RIGHT))
219  return (RIGHT);
220  return (NONE);
221 }
222 
227 static __jo_force_inline unsigned short jo_get_raw_input_key_pressed(const int port)
228 {
229 #if JO_COMPILE_USING_SGL
230  return ((unsigned short)jo_inputs[port].data);
231 #else
232  return ((unsigned short)jo_inputs[port].pressed);
233 #endif
234 }
235 
241 static __jo_force_inline bool jo_is_input_key_down(const int port, const jo_gamepad_keys key)
242 {
243 #if JO_COMPILE_USING_SGL
244  return ((jo_inputs[port].push & key) == 0);
245 #else
246  return ((jo_inputs[port].on_keydown & key) == key);
247 #endif
248 }
249 
250 #if !JO_COMPILE_USING_SGL
251 
257 static __jo_force_inline bool jo_is_input_key_up(const int port, const jo_gamepad_keys key)
258 {
259  return ((jo_inputs[port].on_keyup & key) == key);
260 }
261 #endif
262 
263 #if JO_COMPILE_USING_SGL
264 
269 static __jo_force_inline int jo_get_mouse_pos_x(const int port)
270 {
271  return (JO_DIV_BY_65536(jo_inputs[port].dummy2[0]));
272 }
273 
278 static __jo_force_inline int jo_get_mouse_pos_y(const int port)
279 {
280  return (JO_DIV_BY_65536((JO_MULT_BY_65536(jo_inputs[port].dummy2[0]))));
281 }
282 
283 #endif
284 
285 /*
286 ** HELPER
287 */
288 
293 {
294  return jo_is_input_available(0);
295 }
296 
301 {
302  return jo_is_input_available(1);
303 }
304 
310 {
311  return (jo_is_input_key_pressed(0, key));
312 }
313 
319 {
320  return (jo_is_input_key_down(0, key));
321 }
322 
328 {
329  return (jo_is_input_key_pressed(1, key));
330 }
331 
337 {
338  return (jo_is_input_key_down(1, key));
339 }
340 
341 #endif /* !__JO_INPUT_H__ */
342 
343 /*
344 ** END OF FILE
345 */
JO_MOUSE_MIDDLE_BUTTON
@ JO_MOUSE_MIDDLE_BUTTON
Definition: input.h:92
JoAxis3
@ JoAxis3
Definition: input.h:141
JO_KEY_START
@ JO_KEY_START
Definition: input.h:80
jo_is_pad2_key_pressed
static __jo_force_inline bool jo_is_pad2_key_pressed(jo_gamepad_keys key)
Check if the key is pressed on the second gamepad.
Definition: input.h:327
RIGHT
@ RIGHT
Definition: types.h:305
JoRegularKeyboard
@ JoRegularKeyboard
Definition: input.h:128
__JO_KEY_PRESSED
#define __JO_KEY_PRESSED(PORT, KEY)
Definition: input.h:46
JO_MOUSE_LEFT_BUTTON
@ JO_MOUSE_LEFT_BUTTON
Definition: input.h:91
jo_is_input_available
static __jo_force_inline bool jo_is_input_available(const int port)
Check if the input is available for the given port.
Definition: input.h:167
JO_KEY_L
@ JO_KEY_L
Definition: input.h:87
UP_RIGHT
@ UP_RIGHT
Definition: types.h:309
JO_DIV_BY_65536
#define JO_DIV_BY_65536(X)
Devide a variable by 65536.
Definition: math.h:192
UP_LEFT
@ UP_LEFT
Definition: types.h:308
jo_get_input_type
jo_gamepad_type jo_get_input_type(const int port)
Get input type for the given port.
jo_get_input_count
int jo_get_input_count(void)
Get the number of input available.
DOWN_RIGHT
@ DOWN_RIGHT
Definition: types.h:311
JoAxis5
@ JoAxis5
Definition: input.h:143
jo_is_pad2_key_down
static __jo_force_inline bool jo_is_pad2_key_down(jo_gamepad_keys key)
Check if the key is down on the second gamepad.
Definition: input.h:336
UP
@ UP
Definition: types.h:306
jo_get_raw_input_key_pressed
static __jo_force_inline unsigned short jo_get_raw_input_key_pressed(const int port)
Get raw input data for the given port.
Definition: input.h:227
JO_KEY_LEFT
@ JO_KEY_LEFT
Definition: input.h:77
JO_MULT_BY_65536
#define JO_MULT_BY_65536(X)
Multiply a variable by 65536.
Definition: math.h:138
jo_get_mouse_pos_x
static __jo_force_inline int jo_get_mouse_pos_x(const int port)
Get mouse X position.
Definition: input.h:269
jo_is_pad1_available
static __jo_force_inline bool jo_is_pad1_available(void)
Check if the first gamepad is plugged.
Definition: input.h:292
jo_is_input_key_down
static __jo_force_inline bool jo_is_input_key_down(const int port, const jo_gamepad_keys key)
Check if the key is down for the given port.
Definition: input.h:241
PER_ID_NightsPad
#define PER_ID_NightsPad
Definition: input.h:42
JO_INPUT_MAX_DEVICE
#define JO_INPUT_MAX_DEVICE
Definition: input.h:39
JO_KEY_X
@ JO_KEY_X
Definition: input.h:84
JoNightsPad
@ JoNightsPad
Nights 3D pad, Axis1 and Axis2 are analog stick, Axis3 is right trigger and Axis4 is left trigger.
Definition: input.h:132
jo_is_input_analog
static __jo_force_inline bool jo_is_input_analog(const int port)
Check if connected input is analog.
Definition: input.h:157
JoRegularGamepad
@ JoRegularGamepad
Definition: input.h:125
jo_gamepad_keys
jo_gamepad_keys
Input keys.
Definition: input.h:75
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: types.h:39
JO_KEY_B
@ JO_KEY_B
Definition: input.h:82
JO_MOUSE_START_BUTTON
@ JO_MOUSE_START_BUTTON
Definition: input.h:90
jo_gamepad_type
jo_gamepad_type
Input type.
Definition: input.h:122
JO_KEY_DOWN
@ JO_KEY_DOWN
Definition: input.h:78
NONE
@ NONE
Definition: types.h:303
jo_is_input_key_pressed
static __jo_force_inline bool jo_is_input_key_pressed(const int port, const jo_gamepad_keys key)
Check if the key is pressed for the given port.
Definition: input.h:189
jo_is_pad1_key_pressed
static __jo_force_inline bool jo_is_pad1_key_pressed(jo_gamepad_keys key)
Check if the key is pressed on the first gamepad.
Definition: input.h:309
JO_KEY_R
@ JO_KEY_R
Definition: input.h:88
jo_get_input_direction_pressed
static __jo_force_inline jo_8_directions jo_get_input_direction_pressed(const int port)
Get the direction pressed for the given port.
Definition: input.h:198
JO_MOUSE_RIGHT_BUTTON
@ JO_MOUSE_RIGHT_BUTTON
Definition: input.h:93
JoAxis2
@ JoAxis2
Definition: input.h:140
JoShuttleMouse
@ JoShuttleMouse
Definition: input.h:127
jo_inputs
PerDigital jo_inputs[(12)]
JO_KEY_Y
@ JO_KEY_Y
Definition: input.h:85
JO_KEY_C
@ JO_KEY_C
Definition: input.h:83
JoAxis6
@ JoAxis6
Definition: input.h:144
DOWN
@ DOWN
Definition: types.h:307
JoUnsupportedGamepad
@ JoUnsupportedGamepad
Definition: input.h:124
JoNotConnectedGamepad
@ JoNotConnectedGamepad
Definition: input.h:123
JO_KEY_A
@ JO_KEY_A
Definition: input.h:81
JoAxis1
@ JoAxis1
Definition: input.h:139
jo_input_axis
jo_input_axis
Input axis type.
Definition: input.h:138
JO_KEY_UP
@ JO_KEY_UP
Definition: input.h:79
JoAxis4
@ JoAxis4
Definition: input.h:142
jo_8_directions
jo_8_directions
8 directions definition
Definition: types.h:302
jo_is_pad2_available
static __jo_force_inline bool jo_is_pad2_available(void)
Check if the second gamepad is plugged.
Definition: input.h:300
JO_KEY_RIGHT
@ JO_KEY_RIGHT
Definition: input.h:76
DOWN_LEFT
@ DOWN_LEFT
Definition: types.h:310
JoRegularMouse
@ JoRegularMouse
Definition: input.h:126
JO_KEY_Z
@ JO_KEY_Z
Definition: input.h:86
jo_get_input_axis
unsigned char jo_get_input_axis(const int port, const jo_input_axis axis)
Get analog axis value.
jo_is_pad1_key_down
static __jo_force_inline bool jo_is_pad1_key_down(jo_gamepad_keys key)
Check if the key is down on the first gamepad.
Definition: input.h:318
LEFT
@ LEFT
Definition: types.h:304
jo_get_mouse_pos_y
static __jo_force_inline int jo_get_mouse_pos_y(const int port)
Get mouse Y position.
Definition: input.h:278