Atlas - SDL_haikujoystick.cc
Home / ext / SDL2 / src / joystick / haiku Lines: 1 | Size: 8609 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2018 Sam Lantinga <[email protected]> 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 30extern "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_strdup(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/* Function to perform the mapping from device index to the instance id for this index */ 103 static SDL_JoystickID HAIKU_JoystickGetDeviceInstanceID(int device_index) 104 { 105 return device_index; 106 } 107 108 static void HAIKU_JoystickClose(SDL_Joystick * joystick); 109 110/* Function to open a joystick for use. 111 The joystick to open is specified by the device index. 112 This should fill the nbuttons and naxes fields of the joystick structure. 113 It returns 0, or -1 if there is an error. 114 */ 115 static int HAIKU_JoystickOpen(SDL_Joystick * joystick, int device_index) 116 { 117 BJoystick *stick; 118 119 /* Create the joystick data structure */ 120 joystick->instance_id = device_index; 121 joystick->hwdata = (struct joystick_hwdata *) 122 SDL_malloc(sizeof(*joystick->hwdata)); 123 if (joystick->hwdata == NULL) { 124 return SDL_OutOfMemory(); 125 } 126 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); 127 stick = new BJoystick; 128 joystick->hwdata->stick = stick; 129 130 /* Open the requested joystick for use */ 131 if (stick->Open(SDL_joyport[device_index]) == B_ERROR) { 132 HAIKU_JoystickClose(joystick); 133 return SDL_SetError("Unable to open joystick"); 134 } 135 136 /* Set the joystick to calibrated mode */ 137 stick->EnableCalibration(); 138 139 /* Get the number of buttons, hats, and axes on the joystick */ 140 joystick->nbuttons = stick->CountButtons(); 141 joystick->naxes = stick->CountAxes(); 142 joystick->nhats = stick->CountHats(); 143 144 joystick->hwdata->new_axes = (int16 *) 145 SDL_malloc(joystick->naxes * sizeof(int16)); 146 joystick->hwdata->new_hats = (uint8 *) 147 SDL_malloc(joystick->nhats * sizeof(uint8)); 148 if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) { 149 HAIKU_JoystickClose(joystick); 150 return SDL_OutOfMemory(); 151 } 152 153 /* We're done! */ 154 return 0; 155 } 156 157/* Function to update the state of a joystick - called as a device poll. 158 * This function shouldn't update the joystick structure directly, 159 * but instead should call SDL_PrivateJoystick*() to deliver events 160 * and update joystick device state. 161 */ 162 static void HAIKU_JoystickUpdate(SDL_Joystick * joystick) 163 { 164 static const Uint8 hat_map[9] = { 165 SDL_HAT_CENTERED, 166 SDL_HAT_UP, 167 SDL_HAT_RIGHTUP, 168 SDL_HAT_RIGHT, 169 SDL_HAT_RIGHTDOWN, 170 SDL_HAT_DOWN, 171 SDL_HAT_LEFTDOWN, 172 SDL_HAT_LEFT, 173 SDL_HAT_LEFTUP 174 }; 175 176 BJoystick *stick; 177 int i; 178 int16 *axes; 179 uint8 *hats; 180 uint32 buttons; 181 182 /* Set up data pointers */ 183 stick = joystick->hwdata->stick; 184 axes = joystick->hwdata->new_axes; 185 hats = joystick->hwdata->new_hats; 186 187 /* Get the new joystick state */ 188 stick->Update(); 189 stick->GetAxisValues(axes); 190 stick->GetHatValues(hats); 191 buttons = stick->ButtonValues(); 192 193 /* Generate axis motion events */ 194 for (i = 0; i < joystick->naxes; ++i) { 195 SDL_PrivateJoystickAxis(joystick, i, axes[i]); 196 } 197 198 /* Generate hat change events */ 199 for (i = 0; i < joystick->nhats; ++i) { 200 SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]); 201 } 202 203 /* Generate button events */ 204 for (i = 0; i < joystick->nbuttons; ++i) { 205 SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01)); 206 buttons >>= 1; 207 } 208 } 209 210/* Function to close a joystick after use */ 211 static void HAIKU_JoystickClose(SDL_Joystick * joystick) 212 { 213 if (joystick->hwdata) { 214 joystick->hwdata->stick->Close(); 215 delete joystick->hwdata->stick; 216 SDL_free(joystick->hwdata->new_hats); 217 SDL_free(joystick->hwdata->new_axes); 218 SDL_free(joystick->hwdata); 219 } 220 } 221 222/* Function to perform any system-specific joystick related cleanup */ 223 static void HAIKU_JoystickQuit(void) 224 { 225 int i; 226 227 for (i = 0; i < numjoysticks; ++i) { 228 SDL_free(SDL_joyport[i]); 229 } 230 SDL_joyport[0] = NULL; 231 232 for (i = 0; i < numjoysticks; ++i) { 233 SDL_free(SDL_joyname[i]); 234 } 235 SDL_joyname[0] = NULL; 236 } 237 238 static SDL_JoystickGUID HAIKU_JoystickGetDeviceGUID( int device_index ) 239 { 240 SDL_JoystickGUID guid; 241 /* the GUID is just the first 16 chars of the name for now */ 242 const char *name = HAIKU_JoystickGetDeviceName( device_index ); 243 SDL_zero( guid ); 244 SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) ); 245 return guid; 246 } 247 248 static int HAIKU_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) 249 { 250 return SDL_Unsupported(); 251 } 252 253 SDL_JoystickDriver SDL_HAIKU_JoystickDriver = 254 { 255 HAIKU_JoystickInit, 256 HAIKU_JoystickGetCount, 257 HAIKU_JoystickDetect, 258 HAIKU_JoystickGetDeviceName, 259 HAIKU_JoystickGetDeviceGUID, 260 HAIKU_JoystickGetDeviceInstanceID, 261 HAIKU_JoystickOpen, 262 HAIKU_JoystickRumble, 263 HAIKU_JoystickUpdate, 264 HAIKU_JoystickClose, 265 HAIKU_JoystickQuit, 266 }; 267 268} // extern "C" 269 270#endif /* SDL_JOYSTICK_HAIKU */ 271 272/* vi: set ts=4 sw=4 expandtab: */ 273[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.