SDL  2.0
SDL_haikujoystick.cc
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #ifdef SDL_JOYSTICK_HAIKU
24 
25 /* This is the Haiku implementation of the SDL joystick API */
26 
27 #include <support/String.h>
28 #include <device/Joystick.h>
29 
30 extern "C"
31 {
32 
33 #include "SDL_joystick.h"
34 #include "../SDL_sysjoystick.h"
35 #include "../SDL_joystick_c.h"
36 
37 
38 /* The maximum number of joysticks we'll detect */
39 #define MAX_JOYSTICKS 16
40 
41 /* A list of available joysticks */
42  static char *SDL_joyport[MAX_JOYSTICKS];
43  static char *SDL_joyname[MAX_JOYSTICKS];
44 
45 /* The private structure used to keep track of a joystick */
46  struct joystick_hwdata
47  {
48  BJoystick *stick;
49  uint8 *new_hats;
50  int16 *new_axes;
51  };
52 
53  static int numjoysticks = 0;
54 
55 /* Function to scan the system for joysticks.
56  * Joystick 0 should be the system default joystick.
57  * It should return 0, or -1 on an unrecoverable fatal error.
58  */
59  static int HAIKU_JoystickInit(void)
60  {
61  BJoystick joystick;
62  int i;
63  int32 nports;
64  char name[B_OS_NAME_LENGTH];
65 
66  /* Search for attached joysticks */
67  nports = joystick.CountDevices();
68  numjoysticks = 0;
69  SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport));
70  SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname));
71  for (i = 0; (numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i)
72  {
73  if (joystick.GetDeviceName(i, name) == B_OK) {
74  if (joystick.Open(name) != B_ERROR) {
75  BString stick_name;
76  joystick.GetControllerName(&stick_name);
77  SDL_joyport[numjoysticks] = SDL_strdup(name);
78  SDL_joyname[numjoysticks] = SDL_CreateJoystickName(0, 0, NULL, stick_name.String());
79  numjoysticks++;
80  joystick.Close();
81  }
82  }
83  }
84  return (numjoysticks);
85  }
86 
87  static int HAIKU_JoystickGetCount(void)
88  {
89  return numjoysticks;
90  }
91 
92  static void HAIKU_JoystickDetect(void)
93  {
94  }
95 
96 /* Function to get the device-dependent name of a joystick */
97  static const char *HAIKU_JoystickGetDeviceName(int device_index)
98  {
99  return SDL_joyname[device_index];
100  }
101 
102  static int HAIKU_JoystickGetDevicePlayerIndex(int device_index)
103  {
104  return -1;
105  }
106 
107  static void HAIKU_JoystickSetDevicePlayerIndex(int device_index, int player_index)
108  {
109  }
110 
111 /* Function to perform the mapping from device index to the instance id for this index */
112  static SDL_JoystickID HAIKU_JoystickGetDeviceInstanceID(int device_index)
113  {
114  return device_index;
115  }
116 
117  static void HAIKU_JoystickClose(SDL_Joystick *joystick);
118 
119 /* Function to open a joystick for use.
120  The joystick to open is specified by the device index.
121  This should fill the nbuttons and naxes fields of the joystick structure.
122  It returns 0, or -1 if there is an error.
123  */
124  static int HAIKU_JoystickOpen(SDL_Joystick *joystick, int device_index)
125  {
126  BJoystick *stick;
127 
128  /* Create the joystick data structure */
129  joystick->instance_id = device_index;
130  joystick->hwdata = (struct joystick_hwdata *)
131  SDL_malloc(sizeof(*joystick->hwdata));
132  if (joystick->hwdata == NULL) {
133  return SDL_OutOfMemory();
134  }
135  SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
136  stick = new BJoystick;
137  joystick->hwdata->stick = stick;
138 
139  /* Open the requested joystick for use */
140  if (stick->Open(SDL_joyport[device_index]) == B_ERROR) {
141  HAIKU_JoystickClose(joystick);
142  return SDL_SetError("Unable to open joystick");
143  }
144 
145  /* Set the joystick to calibrated mode */
146  stick->EnableCalibration();
147 
148  /* Get the number of buttons, hats, and axes on the joystick */
149  joystick->nbuttons = stick->CountButtons();
150  joystick->naxes = stick->CountAxes();
151  joystick->nhats = stick->CountHats();
152 
153  joystick->hwdata->new_axes = (int16 *)
154  SDL_malloc(joystick->naxes * sizeof(int16));
155  joystick->hwdata->new_hats = (uint8 *)
156  SDL_malloc(joystick->nhats * sizeof(uint8));
157  if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) {
158  HAIKU_JoystickClose(joystick);
159  return SDL_OutOfMemory();
160  }
161 
162  /* We're done! */
163  return 0;
164  }
165 
166 /* Function to update the state of a joystick - called as a device poll.
167  * This function shouldn't update the joystick structure directly,
168  * but instead should call SDL_PrivateJoystick*() to deliver events
169  * and update joystick device state.
170  */
171  static void HAIKU_JoystickUpdate(SDL_Joystick *joystick)
172  {
173  static const Uint8 hat_map[9] = {
175  SDL_HAT_UP,
179  SDL_HAT_DOWN,
181  SDL_HAT_LEFT,
183  };
184 
185  BJoystick *stick;
186  int i;
187  int16 *axes;
188  uint8 *hats;
189  uint32 buttons;
190 
191  /* Set up data pointers */
192  stick = joystick->hwdata->stick;
193  axes = joystick->hwdata->new_axes;
194  hats = joystick->hwdata->new_hats;
195 
196  /* Get the new joystick state */
197  stick->Update();
198  stick->GetAxisValues(axes);
199  stick->GetHatValues(hats);
200  buttons = stick->ButtonValues();
201 
202  /* Generate axis motion events */
203  for (i = 0; i < joystick->naxes; ++i) {
205  }
206 
207  /* Generate hat change events */
208  for (i = 0; i < joystick->nhats; ++i) {
209  SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
210  }
211 
212  /* Generate button events */
213  for (i = 0; i < joystick->nbuttons; ++i) {
215  buttons >>= 1;
216  }
217  }
218 
219 /* Function to close a joystick after use */
220  static void HAIKU_JoystickClose(SDL_Joystick *joystick)
221  {
222  if (joystick->hwdata) {
223  joystick->hwdata->stick->Close();
224  delete joystick->hwdata->stick;
225  SDL_free(joystick->hwdata->new_hats);
226  SDL_free(joystick->hwdata->new_axes);
227  SDL_free(joystick->hwdata);
228  }
229  }
230 
231 /* Function to perform any system-specific joystick related cleanup */
232  static void HAIKU_JoystickQuit(void)
233  {
234  int i;
235 
236  for (i = 0; i < numjoysticks; ++i) {
237  SDL_free(SDL_joyport[i]);
238  }
239  SDL_joyport[0] = NULL;
240 
241  for (i = 0; i < numjoysticks; ++i) {
242  SDL_free(SDL_joyname[i]);
243  }
244  SDL_joyname[0] = NULL;
245  }
246 
247  static SDL_JoystickGUID HAIKU_JoystickGetDeviceGUID( int device_index )
248  {
250  /* the GUID is just the first 16 chars of the name for now */
251  const char *name = HAIKU_JoystickGetDeviceName( device_index );
252  SDL_zero( guid );
253  SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
254  return guid;
255  }
256 
257  static int HAIKU_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
258  {
259  return SDL_Unsupported();
260  }
261 
262 
263  static int HAIKU_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
264  {
265  return SDL_Unsupported();
266  }
267 
268  static SDL_bool
269  HAIKU_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
270  {
271  return SDL_FALSE;
272  }
273 
274  static SDL_bool HAIKU_JoystickHasLED(SDL_Joystick *joystick)
275  {
276  return SDL_FALSE;
277  }
278 
279  static int HAIKU_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
280  {
281  return SDL_Unsupported();
282  }
283 
284  static int HAIKU_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
285  {
286  return SDL_Unsupported();
287  }
288 
290  {
291  HAIKU_JoystickInit,
292  HAIKU_JoystickGetCount,
293  HAIKU_JoystickDetect,
294  HAIKU_JoystickGetDeviceName,
295  HAIKU_JoystickGetDevicePlayerIndex,
296  HAIKU_JoystickSetDevicePlayerIndex,
297  HAIKU_JoystickGetDeviceGUID,
298  HAIKU_JoystickGetDeviceInstanceID,
299  HAIKU_JoystickOpen,
300  HAIKU_JoystickRumble,
301  HAIKU_JoystickRumbleTriggers,
302  HAIKU_JoystickHasLED,
303  HAIKU_JoystickSetLED,
304  HAIKU_JoystickSetSensorsEnabled,
305  HAIKU_JoystickUpdate,
306  HAIKU_JoystickClose,
307  HAIKU_JoystickQuit,
308  HAIKU_JoystickGetGamepadMapping
309  };
310 
311 } // extern "C"
312 
313 #endif /* SDL_JOYSTICK_HAIKU */
314 
315 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_SetError
#define SDL_memset
#define SDL_malloc
#define SDL_strlen
#define SDL_free
#define SDL_strdup
#define SDL_memcpy
#define SDL_OutOfMemory()
Definition: SDL_error.h:88
#define SDL_Unsupported()
Definition: SDL_error.h:89
const GLubyte GLuint red
Definition: SDL_glfuncs.h:80
int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
char * SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, const char *product_name)
#define SDL_HAT_LEFTDOWN
Definition: SDL_joystick.h:394
#define SDL_HAT_LEFT
Definition: SDL_joystick.h:390
#define SDL_HAT_RIGHT
Definition: SDL_joystick.h:388
#define SDL_HAT_RIGHTUP
Definition: SDL_joystick.h:391
#define SDL_HAT_LEFTUP
Definition: SDL_joystick.h:393
Sint32 SDL_JoystickID
Definition: SDL_joystick.h:81
#define SDL_HAT_DOWN
Definition: SDL_joystick.h:389
#define SDL_HAT_RIGHTDOWN
Definition: SDL_joystick.h:392
#define SDL_HAT_UP
Definition: SDL_joystick.h:387
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:386
static int numjoysticks
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLbyte GLbyte blue
GLuint const GLchar * name
GLbyte green
uint16_t Uint16
Definition: SDL_stdinc.h:197
#define SDL_zero(x)
Definition: SDL_stdinc.h:426
SDL_bool
Definition: SDL_stdinc.h:168
@ SDL_FALSE
Definition: SDL_stdinc.h:169
uint8_t Uint8
Definition: SDL_stdinc.h:185
#define SDL_min(x, y)
Definition: SDL_stdinc.h:412
SDL_JoystickDriver SDL_HAIKU_JoystickDriver
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:163
SDL_JoystickGUID guid
static SDL_Joystick * joystick
Definition: testjoystick.c:37