| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/ddio_lnx/lnxjoy.cpp $ |
| 21 | * $Revision: 1.3 $ |
| 22 | * $Date: 2001/02/07 09:16:45 $ |
| 23 | * $Author: icculus $ |
| 24 | * |
| 25 | * Linux joystick routines |
| 26 | * |
| 27 | * $Log: sdljoy.cpp,v $ |
| 28 | * Revision 1.3 2001/02/07 09:16:45 icculus |
| 29 | * More robust debugging information. |
| 30 | * |
| 31 | * Revision 1.2 2000/06/29 22:15:25 hercules |
| 32 | * Fixed hat motion |
| 33 | * |
| 34 | * Revision 1.1 2000/06/29 09:53:00 hercules |
| 35 | * Use SDL joystick support (hats off to you! :) |
| 36 | * |
| 37 | * Revision 1.3 2000/06/24 01:15:15 icculus |
| 38 | * patched to compile. |
| 39 | * |
| 40 | * Revision 1.2 2000/05/29 05:21:09 icculus |
| 41 | * Changed a fprintf(stderr, ...) to an mprintf()... |
| 42 | * |
| 43 | * Revision 1.1.1.1 2000/04/18 00:00:33 icculus |
| 44 | * initial checkin |
| 45 | * |
| 46 | * |
| 47 | * 9 8/22/99 5:55p Jeff |
| 48 | * fixed assert |
| 49 | * |
| 50 | * 8 8/19/99 3:46p Jeff |
| 51 | * removed mprintfs |
| 52 | * |
| 53 | * 7 8/19/99 3:22p Jeff |
| 54 | * added support for joystick driver version pre 1.0 |
| 55 | * |
| 56 | * 6 8/18/99 9:47p Jeff |
| 57 | * joystick support! for kernel 2..2+ clients...need to handle before that |
| 58 | * still. |
| 59 | * |
| 60 | * 5 8/17/99 2:32p Jeff |
| 61 | * fixed joy_GetPos |
| 62 | * |
| 63 | * 4 7/14/99 9:06p Jeff |
| 64 | * added comment header |
| 65 | * |
| 66 | * $NoKeywords: $ |
| 67 | */ |
| 68 | |
| 69 | #include <cstdlib> |
| 70 | #include <cstring> |
| 71 | #include <SDL.h> |
| 72 | |
| 73 | #include "joystick.h" |
| 74 | #include "pserror.h" |
| 75 | |
| 76 | // rcg06182000 need this for specific joystick stuff. |
| 77 | #include "args.h" |
| 78 | |
| 79 | // --------------------------------------------------------------------------- |
| 80 | // globals |
| 81 | |
| 82 | static int specificJoy = -1; |
| 83 | static struct { |
| 84 | SDL_Joystick *handle; |
| 85 | tJoyInfo caps; |
| 86 | } Joysticks[MAX_JOYSTICKS]; |
| 87 | void joy_Close(); |
| 88 | void joy_GetState(tJoystick stick, tJoyPos *pos); |
| 89 | static int joyGetNumDevs(void); |
| 90 | // closes a stick |
| 91 | // closes connection with controller. |
| 92 | static void joy_CloseStick(tJoystick joy); |
| 93 | // initializes a joystick |
| 94 | // if server_adr is valid, a link is opened to another machine with a controller. |
| 95 | static bool joy_InitStick(tJoystick joy, char *server_adr); |
| 96 | |
| 97 | // --------------------------------------------------------------------------- |
| 98 | // functions |
| 99 | |
| 100 | // joystick system initialization |
| 101 | bool joy_Init(bool remote) { |
| 102 | // reinitialize joystick if already initialized. |
| 103 | joy_Close(); |
| 104 | if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) { |
| 105 | // FIXME: report an error? |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | // check if this OS supports joysticks |
| 110 | if (!joyGetNumDevs() && !remote) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | // rcg06182000 specific joystick support. |
| 115 | if (specificJoy >= 0) { |
| 116 | joy_InitStick((tJoystick)specificJoy, NULL); |
| 117 | } // if |
| 118 | else { |
| 119 | // initialize joystick list |
| 120 | for (int i = 0; i < MAX_JOYSTICKS; i++) { |
| 121 | joy_InitStick((tJoystick)i, NULL); |
| 122 | } |
| 123 | } // else |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | void joy_Close() { |
| 128 | // initialize joystick list |
| 129 | for (int i = 0; i < MAX_JOYSTICKS; i++) { |
| 130 | joy_CloseStick((tJoystick)i); |
| 131 | } |
| 132 | SDL_QuitSubSystem(SDL_INIT_JOYSTICK); |
| 133 | } |
| 134 | |
| 135 | // initializes a joystick |
| 136 | // if server_adr is valid, a link is opened to another machine with a controller. |
| 137 | static bool joy_InitStick(tJoystick joy, char *server_adr) { |
| 138 | SDL_Joystick *stick; |
| 139 | |
| 140 | // close down already open joystick. |
| 141 | joy_CloseStick(joy); |
| 142 | |
| 143 | // okay, now if this is a remote joystick, open it |
| 144 | if (server_adr) { |
| 145 | return false; |
| 146 | } |
| 147 | stick = SDL_JoystickOpen(joy); |
| 148 | Joysticks[joy].handle = stick; |
| 149 | if (stick) { |
| 150 | tJoyInfo caps; |
| 151 | |
| 152 | memset(&caps, 0, (sizeof caps)); |
| 153 | strncpy(caps.name, SDL_JoystickNameForIndex(joy), sizeof(caps.name) - 1); |
| 154 | caps.num_btns = SDL_JoystickNumButtons(stick); |
| 155 | int axes = SDL_JoystickNumAxes(stick); |
| 156 | switch (axes) { |
| 157 | default: |
| 158 | // Fall through to 6 axes |
| 159 | case 6: |
| 160 | caps.axes_mask |= JOYFLAG_VVALID; |
| 161 | caps.minv = -32767; |
| 162 | caps.maxv = 32768; |
| 163 | case 5: |
| 164 | caps.axes_mask |= JOYFLAG_UVALID; |
| 165 | caps.minu = -32767; |
| 166 | caps.maxu = 32768; |
| 167 | case 4: |
| 168 | caps.axes_mask |= JOYFLAG_RVALID; |
| 169 | caps.minr = -32767; |
| 170 | caps.maxr = 32768; |
| 171 | case 3: |
| 172 | caps.axes_mask |= JOYFLAG_ZVALID; |
| 173 | caps.minz = -32767; |
| 174 | caps.maxz = 32768; |
| 175 | case 2: |
| 176 | caps.axes_mask |= JOYFLAG_YVALID; |
| 177 | caps.miny = -32767; |
| 178 | caps.maxy = 32768; |
| 179 | case 1: |
| 180 | caps.axes_mask |= JOYFLAG_XVALID; |
| 181 | caps.minx = -32767; |
| 182 | caps.maxx = 32768; |
| 183 | case 0: |
| 184 | break; |
| 185 | } |
| 186 | int hats = SDL_JoystickNumHats(stick); |
| 187 | switch (hats) { |
| 188 | default: |
| 189 | // Fall through to 4 hats |
| 190 | case 4: |
| 191 | caps.axes_mask |= JOYFLAG_POV4VALID; |
| 192 | case 3: |
| 193 | caps.axes_mask |= JOYFLAG_POV3VALID; |
| 194 | case 2: |
| 195 | caps.axes_mask |= JOYFLAG_POV2VALID; |
| 196 | case 1: |
| 197 | caps.axes_mask |= JOYFLAG_POVVALID; |
| 198 | case 0: |
| 199 | break; |
| 200 | } |
| 201 | Joysticks[joy].caps = caps; |
| 202 | |
| 203 | mprintf(0, "JOYSTICK: Initialized stick named [%s].", caps.name); |
| 204 | mprintf(0, "JOYSTICK: (%d) axes, (%d) hats, and (%d) buttons.", axes, hats, caps.num_btns); |
| 205 | } |
| 206 | |
| 207 | return (Joysticks[joy].handle != NULL); |
| 208 | } |
| 209 | |
| 210 | // closes a stick |
| 211 | // closes connection with controller. |
| 212 | static void joy_CloseStick(tJoystick joy) { |
| 213 | // CLOSE joystick here |
| 214 | SDL_JoystickClose(Joysticks[joy].handle); |
| 215 | Joysticks[joy].handle = 0; |
| 216 | } |
| 217 | |
| 218 | // returns true if joystick valid |
| 219 | bool joy_IsValid(tJoystick joy) { |
| 220 | if (specificJoy >= 0) { |
| 221 | if (joy != specificJoy) { |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | return (Joysticks[joy].handle != NULL); |
| 226 | } |
| 227 | |
| 228 | // retreive information about joystick. |
| 229 | void joy_GetJoyInfo(tJoystick joy, tJoyInfo *info) { memcpy(info, &Joysticks[(int)joy].caps, sizeof(tJoyInfo)); } |
| 230 | |
| 231 | // retreive uncalibrated position of joystick |
| 232 | #define LNX_JOYAXIS_RANGE 65535 |
| 233 | void joy_GetRawPos(tJoystick joy, tJoyPos *pos) { |
| 234 | joy_GetPos(joy, pos); |
| 235 | |
| 236 | pos->x = (pos->x + 32767); |
| 237 | pos->y = (pos->y + 32767); |
| 238 | pos->z = (pos->z + 32767); |
| 239 | pos->r = (pos->r + 32767); |
| 240 | pos->u = (pos->u + 32767); |
| 241 | pos->v = (pos->v + 32767); |
| 242 | } |
| 243 | |
| 244 | static inline uint32_t map_hat(Uint8 value) { |
| 245 | uint32_t mapped = 0; |
| 246 | |
| 247 | switch (value) { |
| 248 | case SDL_HAT_CENTERED: |
| 249 | mapped = JOYPOV_CENTER; |
| 250 | break; |
| 251 | case SDL_HAT_UP: |
| 252 | mapped = 0x00; |
| 253 | break; |
| 254 | case SDL_HAT_UP | SDL_HAT_RIGHT: |
| 255 | mapped = 0x20; |
| 256 | break; |
| 257 | case SDL_HAT_RIGHT: |
| 258 | mapped = 0x40; |
| 259 | break; |
| 260 | case SDL_HAT_RIGHT | SDL_HAT_DOWN: |
| 261 | mapped = 0x60; |
| 262 | break; |
| 263 | case SDL_HAT_DOWN: |
| 264 | mapped = 0x80; |
| 265 | break; |
| 266 | case SDL_HAT_DOWN | SDL_HAT_LEFT: |
| 267 | mapped = 0xA0; |
| 268 | break; |
| 269 | case SDL_HAT_LEFT: |
| 270 | mapped = 0xC0; |
| 271 | break; |
| 272 | case SDL_HAT_LEFT | SDL_HAT_UP: |
| 273 | mapped = 0xE0; |
| 274 | break; |
| 275 | } |
| 276 | return mapped; |
| 277 | } |
| 278 | |
| 279 | // returns the state of a stick, remote or otherwise |
| 280 | void joy_GetPos(tJoystick joy, tJoyPos *pos) { |
| 281 | SDL_Joystick *stick; |
| 282 | int i; |
| 283 | |
| 284 | memset(pos, 0, (sizeof *pos)); |
| 285 | |
| 286 | // retrieve joystick info from the net, or locally. |
| 287 | stick = Joysticks[joy].handle; |
| 288 | if (stick) { |
| 289 | uint32_t mask; |
| 290 | |
| 291 | mask = Joysticks[joy].caps.axes_mask; |
| 292 | pos->x = SDL_JoystickGetAxis(stick, 0); |
| 293 | pos->y = SDL_JoystickGetAxis(stick, 1); |
| 294 | if (mask & JOYFLAG_ZVALID) { |
| 295 | pos->z = SDL_JoystickGetAxis(stick, 2); |
| 296 | } |
| 297 | if (mask & JOYFLAG_RVALID) { |
| 298 | pos->r = SDL_JoystickGetAxis(stick, 3); |
| 299 | } |
| 300 | if (mask & JOYFLAG_UVALID) { |
| 301 | pos->u = SDL_JoystickGetAxis(stick, 4); |
| 302 | } |
| 303 | if (mask & JOYFLAG_VVALID) { |
| 304 | pos->v = SDL_JoystickGetAxis(stick, 5); |
| 305 | } |
| 306 | for (i = 0; i < JOYPOV_NUM; ++i) { |
| 307 | if (mask & (JOYFLAG_POVVALID << i)) { |
| 308 | pos->pov[i] = map_hat(SDL_JoystickGetHat(stick, i)); |
| 309 | } |
| 310 | } |
| 311 | for (i = Joysticks[joy].caps.num_btns; i >= 0; --i) { |
| 312 | if (SDL_JoystickGetButton(stick, i)) { |
| 313 | pos->buttons |= (1 << i); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static int joyGetNumDevs(void) { |
| 320 | int found = 0; |
| 321 | |
| 322 | // rcg06182000 add support for specific joydev. |
| 323 | int rc = FindArgChar("-joystick", 'j'); |
| 324 | specificJoy = -1; |
| 325 | if ((rc > 0) && (GameArgs[rc + 1] != NULL)) { |
| 326 | specificJoy = atoi(GameArgs[rc + 1]); |
| 327 | if ((specificJoy >= 0) && (specificJoy < SDL_NumJoysticks())) { |
| 328 | found = 1; |
| 329 | } else { |
| 330 | specificJoy = -1; |
| 331 | } |
| 332 | } |
| 333 | if (specificJoy < 0) { |
| 334 | found = SDL_NumJoysticks(); |
| 335 | } |
| 336 | |
| 337 | mprintf(0, "Joystick: Found %d joysticks.", found); |
| 338 | return found; |
| 339 | } |
| 340 | |
| 341 | void ddio_InternalJoyFrame(void) { |
| 342 | // All the work is done already in SDL_PumpEvents() |
| 343 | } |