| 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"cxx . . $ `<file>/lib/joystick.h`/ |
| 74 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 75 | |
| 76 | // rcg06182000 need this for specific joystick stuff. |
| 77 | #include "args.h"cxx . . $ `<file>/Descent3/args.h`/ |
| 78 | |
| 79 | // --------------------------------------------------------------------------- |
| 80 | // globals |
| 81 | |
| 82 | static int specificJoycxx . . $ specificJoy. = -1; |
| 83 | static structcxx . . $ $anonymous_type_37abbb6b7a881636_0# { |
| 84 | SDL_Joystick *handlecxx . . $ $anonymous_type_37abbb6b7a881636_0#handle.; |
| 85 | tJoyInfocxx . . $ tJoyInfo# capscxx . . $ $anonymous_type_37abbb6b7a881636_0#caps.; |
| 86 | } Joystickscxx . . $ Joysticks.[MAX_JOYSTICKScxx . . $ `lib/joystick.h:71:9`!]; |
| 87 | void joy_Closecxx . . $ joy_Close(49f6e7a06ebc5aa8).(); |
| 88 | void joy_GetStatecxx . . $ joy_GetState(47f0a1b684eacd68).(tJoystickcxx . . $ tJoystick# stick, tJoyPoscxx . . $ tJoyPos# *pos); |
| 89 | static int joyGetNumDevscxx . . $ joyGetNumDevs(b126dc7c1de90089).(void); |
| 90 | // closes a stick |
| 91 | // closes connection with controller. |
| 92 | static void joy_CloseStickcxx . . $ joy_CloseStick(d4f767463ce0a6b3).(tJoystickcxx . . $ 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_InitStickcxx . . $ joy_InitStick(ceb9eed3194142b2).(tJoystickcxx . . $ tJoystick# joy, char *server_adr); |
| 96 | |
| 97 | // --------------------------------------------------------------------------- |
| 98 | // functions |
| 99 | |
| 100 | // joystick system initialization |
| 101 | bool joy_Initcxx . . $ joy_Init(efaadfb7dc14930a).(bool remote) { |
| 102 | // reinitialize joystick if already initialized. |
| 103 | joy_Closecxx . . $ joy_Close(49f6e7a06ebc5aa8).(); |
| 104 | if (SDL_InitSubSystem(SDL_INIT_JOYSTICKcxx . . $ `<external>/13597568925221565411/SDL.h:86:9`!) < 0) { |
| 105 | // FIXME: report an error? |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | // check if this OS supports joysticks |
| 110 | if (!joyGetNumDevscxx . . $ joyGetNumDevs(b126dc7c1de90089).() && !remote) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | // rcg06182000 specific joystick support. |
| 115 | if (specificJoycxx . . $ specificJoy. >= 0) { |
| 116 | joy_InitStickcxx . . $ joy_InitStick(ceb9eed3194142b2).((tJoystickcxx . . $ tJoystick#)specificJoycxx . . $ specificJoy., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 117 | } // if |
| 118 | else { |
| 119 | // initialize joystick list |
| 120 | for (int i = 0; i < MAX_JOYSTICKScxx . . $ `lib/joystick.h:71:9`!; i++) { |
| 121 | joy_InitStickcxx . . $ joy_InitStick(ceb9eed3194142b2).((tJoystickcxx . . $ tJoystick#)i, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 122 | } |
| 123 | } // else |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | void joy_Closecxx . . $ joy_Close(49f6e7a06ebc5aa8).() { |
| 128 | // initialize joystick list |
| 129 | for (int i = 0; i < MAX_JOYSTICKScxx . . $ `lib/joystick.h:71:9`!; i++) { |
| 130 | joy_CloseStickcxx . . $ joy_CloseStick(d4f767463ce0a6b3).((tJoystickcxx . . $ tJoystick#)i); |
| 131 | } |
| 132 | SDL_QuitSubSystem(SDL_INIT_JOYSTICKcxx . . $ `<external>/13597568925221565411/SDL.h:86:9`!); |
| 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_InitStickcxx . . $ joy_InitStick(ceb9eed3194142b2).(tJoystickcxx . . $ tJoystick# joy, char *server_adr) { |
| 138 | SDL_Joystick *stick; |
| 139 | |
| 140 | // close down already open joystick. |
| 141 | joy_CloseStickcxx . . $ joy_CloseStick(d4f767463ce0a6b3).(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 | Joystickscxx . . $ Joysticks.[joy].handlecxx . . $ $anonymous_type_37abbb6b7a881636_0#handle. = stick; |
| 149 | if (stick) { |
| 150 | tJoyInfocxx . . $ tJoyInfo# caps; |
| 151 | |
| 152 | memset(&caps, 0, (sizeof caps)); |
| 153 | strncpy(caps.namecxx . . $ tJoyInfo#name., SDL_JoystickNameForIndex(joy), sizeof(caps.namecxx . . $ tJoyInfo#name.) - 1); |
| 154 | caps.num_btnscxx . . $ tJoyInfo#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_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_VVALIDcxx . . $ `lib/joystick.h:80:9`!; |
| 161 | caps.minvcxx . . $ tJoyInfo#minv. = -32767; |
| 162 | caps.maxvcxx . . $ tJoyInfo#maxv. = 32768; |
| 163 | case 5: |
| 164 | caps.axes_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_UVALIDcxx . . $ `lib/joystick.h:79:9`!; |
| 165 | caps.minucxx . . $ tJoyInfo#minu. = -32767; |
| 166 | caps.maxucxx . . $ tJoyInfo#maxu. = 32768; |
| 167 | case 4: |
| 168 | caps.axes_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_RVALIDcxx . . $ `lib/joystick.h:78:9`!; |
| 169 | caps.minrcxx . . $ tJoyInfo#minr. = -32767; |
| 170 | caps.maxrcxx . . $ tJoyInfo#maxr. = 32768; |
| 171 | case 3: |
| 172 | caps.axes_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_ZVALIDcxx . . $ `lib/joystick.h:77:9`!; |
| 173 | caps.minzcxx . . $ tJoyInfo#minz. = -32767; |
| 174 | caps.maxzcxx . . $ tJoyInfo#maxz. = 32768; |
| 175 | case 2: |
| 176 | caps.axes_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_YVALIDcxx . . $ `lib/joystick.h:76:9`!; |
| 177 | caps.minycxx . . $ tJoyInfo#miny. = -32767; |
| 178 | caps.maxycxx . . $ tJoyInfo#maxy. = 32768; |
| 179 | case 1: |
| 180 | caps.axes_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_XVALIDcxx . . $ `lib/joystick.h:75:9`!; |
| 181 | caps.minxcxx . . $ tJoyInfo#minx. = -32767; |
| 182 | caps.maxxcxx . . $ tJoyInfo#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_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_POV4VALIDcxx . . $ `lib/joystick.h:84:9`!; |
| 192 | case 3: |
| 193 | caps.axes_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_POV3VALIDcxx . . $ `lib/joystick.h:83:9`!; |
| 194 | case 2: |
| 195 | caps.axes_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_POV2VALIDcxx . . $ `lib/joystick.h:82:9`!; |
| 196 | case 1: |
| 197 | caps.axes_maskcxx . . $ tJoyInfo#axes_mask. |= JOYFLAG_POVVALIDcxx . . $ `lib/joystick.h:81:9`!; |
| 198 | case 0: |
| 199 | break; |
| 200 | } |
| 201 | Joystickscxx . . $ Joysticks.[joy].capscxx . . $ $anonymous_type_37abbb6b7a881636_0#caps. =cxx . . $ tJoyInfo#`operator=`(bef6ddfc1edc6270). caps; |
| 202 | |
| 203 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "JOYSTICK: Initialized stick named [%s].", caps.name); |
| 204 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "JOYSTICK: (%d) axes, (%d) hats, and (%d) buttons.", axes, hats, caps.num_btns); |
| 205 | } |
| 206 | |
| 207 | return (Joystickscxx . . $ Joysticks.[joy].handlecxx . . $ $anonymous_type_37abbb6b7a881636_0#handle. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 208 | } |
| 209 | |
| 210 | // closes a stick |
| 211 | // closes connection with controller. |
| 212 | static void joy_CloseStickcxx . . $ joy_CloseStick(d4f767463ce0a6b3).(tJoystickcxx . . $ tJoystick# joy) { |
| 213 | // CLOSE joystick here |
| 214 | SDL_JoystickClose(Joystickscxx . . $ Joysticks.[joy].handlecxx . . $ $anonymous_type_37abbb6b7a881636_0#handle.); |
| 215 | Joystickscxx . . $ Joysticks.[joy].handlecxx . . $ $anonymous_type_37abbb6b7a881636_0#handle. = 0; |
| 216 | } |
| 217 | |
| 218 | // returns true if joystick valid |
| 219 | bool joy_IsValidcxx . . $ joy_IsValid(83c267b20bac841a).(tJoystickcxx . . $ tJoystick# joy) { |
| 220 | if (specificJoycxx . . $ specificJoy. >= 0) { |
| 221 | if (joy != specificJoycxx . . $ specificJoy.) { |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | return (Joystickscxx . . $ Joysticks.[joy].handlecxx . . $ $anonymous_type_37abbb6b7a881636_0#handle. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 226 | } |
| 227 | |
| 228 | // retreive information about joystick. |
| 229 | void joy_GetJoyInfocxx . . $ joy_GetJoyInfo(6d54e3a34f20aef).(tJoystickcxx . . $ tJoystick# joy, tJoyInfocxx . . $ tJoyInfo# *info) { memcpy(info, &Joystickscxx . . $ Joysticks.[(int)joy].capscxx . . $ $anonymous_type_37abbb6b7a881636_0#caps., sizeof(tJoyInfocxx . . $ tJoyInfo#)); } |
| 230 | |
| 231 | // retreive uncalibrated position of joystick |
| 232 | #define LNX_JOYAXIS_RANGEcxx . . $ `ddio/sdljoy.cpp:232:9`! 65535 |
| 233 | void joy_GetRawPoscxx . . $ joy_GetRawPos(47f0a1b684eacd68).(tJoystickcxx . . $ tJoystick# joy, tJoyPoscxx . . $ tJoyPos# *pos) { |
| 234 | joy_GetPoscxx . . $ joy_GetPos(47f0a1b684eacd68).(joy, pos); |
| 235 | |
| 236 | pos->xcxx . . $ tJoyPos#x. = (pos->xcxx . . $ tJoyPos#x. + 32767); |
| 237 | pos->ycxx . . $ tJoyPos#y. = (pos->ycxx . . $ tJoyPos#y. + 32767); |
| 238 | pos->zcxx . . $ tJoyPos#z. = (pos->zcxx . . $ tJoyPos#z. + 32767); |
| 239 | pos->rcxx . . $ tJoyPos#r. = (pos->rcxx . . $ tJoyPos#r. + 32767); |
| 240 | pos->ucxx . . $ tJoyPos#u. = (pos->ucxx . . $ tJoyPos#u. + 32767); |
| 241 | pos->vcxx . . $ tJoyPos#v. = (pos->vcxx . . $ tJoyPos#v. + 32767); |
| 242 | } |
| 243 | |
| 244 | static inline uint32_tcxx . . $ uint32_t# map_hatcxx . . $ map_hat(ded9d4c05c7b1550).(Uint8cxx . . $ Uint8# value) { |
| 245 | uint32_tcxx . . $ uint32_t# mapped = 0; |
| 246 | |
| 247 | switch (value) { |
| 248 | case SDL_HAT_CENTEREDcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:881:9`!: |
| 249 | mapped = JOYPOV_CENTERcxx . . $ `lib/joystick.h:93:9`!; |
| 250 | break; |
| 251 | case SDL_HAT_UPcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:882:9`!: |
| 252 | mapped = 0x00; |
| 253 | break; |
| 254 | case SDL_HAT_UPcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:882:9`! | SDL_HAT_RIGHTcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:883:9`!: |
| 255 | mapped = 0x20; |
| 256 | break; |
| 257 | case SDL_HAT_RIGHTcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:883:9`!: |
| 258 | mapped = 0x40; |
| 259 | break; |
| 260 | case SDL_HAT_RIGHTcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:883:9`! | SDL_HAT_DOWNcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:884:9`!: |
| 261 | mapped = 0x60; |
| 262 | break; |
| 263 | case SDL_HAT_DOWNcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:884:9`!: |
| 264 | mapped = 0x80; |
| 265 | break; |
| 266 | case SDL_HAT_DOWNcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:884:9`! | SDL_HAT_LEFTcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:885:9`!: |
| 267 | mapped = 0xA0; |
| 268 | break; |
| 269 | case SDL_HAT_LEFTcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:885:9`!: |
| 270 | mapped = 0xC0; |
| 271 | break; |
| 272 | case SDL_HAT_LEFTcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:885:9`! | SDL_HAT_UPcxx . . $ `<external>/17049259561641565683/SDL_joystick.h:882:9`!: |
| 273 | mapped = 0xE0; |
| 274 | break; |
| 275 | } |
| 276 | return mapped; |
| 277 | } |
| 278 | |
| 279 | // returns the state of a stick, remote or otherwise |
| 280 | void joy_GetPoscxx . . $ joy_GetPos(47f0a1b684eacd68).(tJoystickcxx . . $ tJoystick# joy, tJoyPoscxx . . $ 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 = Joystickscxx . . $ Joysticks.[joy].handlecxx . . $ $anonymous_type_37abbb6b7a881636_0#handle.; |
| 288 | if (stick) { |
| 289 | uint32_tcxx . . $ uint32_t# mask; |
| 290 | |
| 291 | mask = Joystickscxx . . $ Joysticks.[joy].capscxx . . $ $anonymous_type_37abbb6b7a881636_0#caps..axes_maskcxx . . $ tJoyInfo#axes_mask.; |
| 292 | pos->xcxx . . $ tJoyPos#x. = SDL_JoystickGetAxis(stick, 0); |
| 293 | pos->ycxx . . $ tJoyPos#y. = SDL_JoystickGetAxis(stick, 1); |
| 294 | if (mask & JOYFLAG_ZVALIDcxx . . $ `lib/joystick.h:77:9`!) { |
| 295 | pos->zcxx . . $ tJoyPos#z. = SDL_JoystickGetAxis(stick, 2); |
| 296 | } |
| 297 | if (mask & JOYFLAG_RVALIDcxx . . $ `lib/joystick.h:78:9`!) { |
| 298 | pos->rcxx . . $ tJoyPos#r. = SDL_JoystickGetAxis(stick, 3); |
| 299 | } |
| 300 | if (mask & JOYFLAG_UVALIDcxx . . $ `lib/joystick.h:79:9`!) { |
| 301 | pos->ucxx . . $ tJoyPos#u. = SDL_JoystickGetAxis(stick, 4); |
| 302 | } |
| 303 | if (mask & JOYFLAG_VVALIDcxx . . $ `lib/joystick.h:80:9`!) { |
| 304 | pos->vcxx . . $ tJoyPos#v. = SDL_JoystickGetAxis(stick, 5); |
| 305 | } |
| 306 | for (i = 0; i < JOYPOV_NUMcxx . . $ `lib/joystick.h:72:9`!; ++i) { |
| 307 | if (mask & (JOYFLAG_POVVALIDcxx . . $ `lib/joystick.h:81:9`! << i)) { |
| 308 | pos->povcxx . . $ tJoyPos#pov.[i] = map_hatcxx . . $ map_hat(ded9d4c05c7b1550).(SDL_JoystickGetHat(stick, i)); |
| 309 | } |
| 310 | } |
| 311 | for (i = Joystickscxx . . $ Joysticks.[joy].capscxx . . $ $anonymous_type_37abbb6b7a881636_0#caps..num_btnscxx . . $ tJoyInfo#num_btns.; i >= 0; --i) { |
| 312 | if (SDL_JoystickGetButton(stick, i)) { |
| 313 | pos->buttonscxx . . $ tJoyPos#buttons. |= (1 << i); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static int joyGetNumDevscxx . . $ joyGetNumDevs(b126dc7c1de90089).(void) { |
| 320 | int found = 0; |
| 321 | |
| 322 | // rcg06182000 add support for specific joydev. |
| 323 | int rc = FindArgCharcxx . . $ FindArgChar(7c0a79fa02e3e627).("-joystick", 'j'); |
| 324 | specificJoycxx . . $ specificJoy. = -1; |
| 325 | if ((rc > 0) && (GameArgscxx . . $ GameArgs.[rc + 1] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)) { |
| 326 | specificJoycxx . . $ specificJoy. = atoicxx . . $ atoi(f5ecf3191ed0cd93).(GameArgscxx . . $ GameArgs.[rc + 1]); |
| 327 | if ((specificJoycxx . . $ specificJoy. >= 0) && (specificJoycxx . . $ specificJoy. < SDL_NumJoysticks())) { |
| 328 | found = 1; |
| 329 | } else { |
| 330 | specificJoycxx . . $ specificJoy. = -1; |
| 331 | } |
| 332 | } |
| 333 | if (specificJoycxx . . $ specificJoy. < 0) { |
| 334 | found = SDL_NumJoysticks(); |
| 335 | } |
| 336 | |
| 337 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Joystick: Found %d joysticks.", found); |
| 338 | return found; |
| 339 | } |
| 340 | |
| 341 | void ddio_InternalJoyFramecxx . . $ ddio_InternalJoyFrame(49f6e7a06ebc5aa8).(void) { |
| 342 | // All the work is done already in SDL_PumpEvents() |
| 343 | } |