| 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/lib/forcefeedback.h $ |
| 21 | * $Revision: 15 $ |
| 22 | * $Date: 7/28/99 3:23p $ |
| 23 | * $Author: Kevin $ |
| 24 | * |
| 25 | * Low-Level ForceFeedback header |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/lib/forcefeedback.h $ |
| 28 | * |
| 29 | * 15 7/28/99 3:23p Kevin |
| 30 | * Macintosh |
| 31 | * |
| 32 | * 14 4/16/99 8:28p Jeff |
| 33 | * moved placement of #endif |
| 34 | * |
| 35 | * 13 4/15/99 1:44a Jeff |
| 36 | * changes for linux compile |
| 37 | * |
| 38 | * 12 1/30/99 11:27p Jeff |
| 39 | * added immersion support |
| 40 | * |
| 41 | * 11 1/28/99 12:09p Jeff |
| 42 | * added force feedback to player shake...fixed spelling error in define |
| 43 | * for forcefeedback |
| 44 | * |
| 45 | * 10 1/13/99 6:48a Jeff |
| 46 | * made file linux friendly (removed dinput specific stuff to ifdef) |
| 47 | * |
| 48 | * 8 11/10/98 5:16p Jeff |
| 49 | * updated forcefeedback system...pretty complete now |
| 50 | * |
| 51 | * 7 11/06/98 7:00p Jeff |
| 52 | * first round of new force feedback installed |
| 53 | * |
| 54 | * 6 11/03/98 6:43p Jeff |
| 55 | * new low-level & high level Force Feedback system implemented, handles |
| 56 | * window losing focus, etc. |
| 57 | * |
| 58 | * 5 10/12/98 3:49p Jeff |
| 59 | * struct changes |
| 60 | * |
| 61 | * 4 9/21/98 11:10a Jeff |
| 62 | * general update, new low level, small high level implementation |
| 63 | * |
| 64 | * 3 9/18/98 7:38p Jeff |
| 65 | * creation of low-level forcefeedback and beginning of high-level |
| 66 | * forcefeedback |
| 67 | * |
| 68 | * 2 9/15/98 12:05p Jeff |
| 69 | * initial creation of low-level forcefeedback |
| 70 | * |
| 71 | * $NoKeywords: $ |
| 72 | */ |
| 73 | #ifndef __DDIO_FORCEFEEDBACK_H_ |
| 74 | #define __DDIO_FORCEFEEDBACK_H_ |
| 75 | #include <cstdint> |
| 76 | #include "pstypes.h" |
| 77 | #include "string.h" |
| 78 | #define kMAX_Str 80 |
| 79 | #define kInfinite_Duration 0xFFFFFF |
| 80 | #define FF_USEENVELOPE 0x01 |
| 81 | #define HZ_to_uS(hz) ((int)(1000000.0 / (double)(hz) + 0.5)) |
| 82 | #define DDIO_FF_MAXEFFECTS 30 |
| 83 | #if defined(WIN32) |
| 84 | // WINDOWS |
| 85 | #include "win/DirectX/dinput.h" |
| 86 | #define FF_DEGREES DI_DEGREES |
| 87 | #define FF_NOMINALMAX DI_FFNOMINALMAX |
| 88 | #define FF_SECONDS DI_SECONDS |
| 89 | #else |
| 90 | #define FF_DEGREES 360 // fake value |
| 91 | #define FF_NOMINALMAX 10000 // fake value |
| 92 | #define FF_SECONDS 1000 // fake value |
| 93 | #endif |
| 94 | #define FORCEPROJECT void * |
| 95 | enum tDevice { |
| 96 | kJoy1 = 0, |
| 97 | kJoy2, |
| 98 | kJoy3, |
| 99 | kJoy4, |
| 100 | kJoy5, |
| 101 | kJoy6, |
| 102 | kJoy7, |
| 103 | kJoy8, |
| 104 | kJoy9, |
| 105 | kJoy10, |
| 106 | kJoy11, |
| 107 | kJoy12, |
| 108 | kJoy13, |
| 109 | kJoy14, |
| 110 | kJoy15, |
| 111 | kJoy16, |
| 112 | kMaxJoy, |
| 113 | kMouse, |
| 114 | kKeyBoard, |
| 115 | kAllDevices |
| 116 | }; |
| 117 | enum tJoyButtons { |
| 118 | kNoButton = -1, |
| 119 | kButton0 = 0, |
| 120 | kButton1, |
| 121 | kButton2, |
| 122 | kButton3, |
| 123 | kButton4, |
| 124 | kButton5, |
| 125 | kButton6, |
| 126 | kButton7, |
| 127 | kButton8, |
| 128 | kButton9, |
| 129 | kButton10, |
| 130 | kButton31 = 31, |
| 131 | kButtonMax |
| 132 | }; |
| 133 | enum tDeviceMask { |
| 134 | kStick = 1 << 0, |
| 135 | kWheel = 1 << 1, |
| 136 | kGamePad = 1 << 2, |
| 137 | }; |
| 138 | struct tFFJoyInfo { |
| 139 | int ButtonMask; |
| 140 | int AxisMask; |
| 141 | tDeviceMask DevType; |
| 142 | char Name[kMAX_Str]; |
| 143 | }; |
| 144 | enum tLoadEffect { |
| 145 | kDontPlayNow = 0, |
| 146 | kPlayNow, |
| 147 | kPlayNowIfModified, |
| 148 | }; |
| 149 | enum tEffType { |
| 150 | kConstant = 0, |
| 151 | kRamp, |
| 152 | kCustom, |
| 153 | kWave_Square, |
| 154 | kWave_Sine, |
| 155 | kWave_Triangle, |
| 156 | kWave_SawUp, |
| 157 | kWave_SawDown, |
| 158 | kCondition_Spring, |
| 159 | kCondition_Damper, |
| 160 | kCondition_Inertia, |
| 161 | kCondition_Friction, |
| 162 | kMaxEffectSubTypes |
| 163 | }; |
| 164 | struct tEffConstant { |
| 165 | int32_t Mag; // +- 10,000 |
| 166 | }; |
| 167 | struct tEffRamp { |
| 168 | int32_t Start; // +- 10,000 |
| 169 | int32_t End; // +- 10,000 |
| 170 | }; |
| 171 | struct tEffWave { |
| 172 | uint32_t Mag; // 0 to 10,000 |
| 173 | int32_t Offset; // +- 10,000 |
| 174 | uint32_t Phase; // 0 to 35,999 |
| 175 | uint32_t Period; |
| 176 | }; |
| 177 | struct tEffCondition { |
| 178 | int32_t Offset; // +- 10,000 |
| 179 | int32_t PositiveCoefficient; // +- 10,000 |
| 180 | int32_t NegativeCoefficient; // +- 10,000 |
| 181 | uint32_t PositiveSaturation; // 0 to 10,000 |
| 182 | uint32_t NegativeSaturation; // 0 to 10,000 |
| 183 | int32_t DeadBand; // 0 to 10,000 |
| 184 | }; |
| 185 | struct tEffCustom { |
| 186 | int Channels; |
| 187 | int Period; |
| 188 | int Samples; |
| 189 | int32_t *ForceData; |
| 190 | }; |
| 191 | union tEffInfo { |
| 192 | tEffConstant Constant; |
| 193 | tEffRamp Ramp; |
| 194 | tEffWave Wave; |
| 195 | tEffCondition Condition; |
| 196 | tEffCustom Custom; |
| 197 | }; |
| 198 | struct tEffEnvelope { |
| 199 | uint32_t AttackLevel; |
| 200 | uint32_t AttackTime; |
| 201 | uint32_t FadeLevel; |
| 202 | uint32_t FadeTime; |
| 203 | }; |
| 204 | enum tEffAxis { kXAxisOnly, kYAxisOnly, kBothAxes }; |
| 205 | struct tFFB_Effect { |
| 206 | int Flags; |
| 207 | tEffType Type; |
| 208 | // tEffInfo TypeInfo[2]; |
| 209 | tEffInfo TypeInfo; |
| 210 | uint32_t Duration; |
| 211 | uint32_t Gain; // 0-10000 -- scales all magnitudes and envelope |
| 212 | uint32_t Period; |
| 213 | tEffAxis Axis; |
| 214 | tJoyButtons Trigger; |
| 215 | uint32_t TriggerRepeatTime; |
| 216 | int32_t Direction; // 0 to 360 deg. |
| 217 | tEffEnvelope Envelope; |
| 218 | }; |
| 219 | extern bool ddForce_found; // a Force Feedback device was found |
| 220 | extern bool ddForce_enabled; // Force Feedback is ready and can be used |
| 221 | // =================================================================== |
| 222 | // Function Prototypes |
| 223 | // =================================================================== |
| 224 | // ------------------------------------------------------------------- |
| 225 | // ddio_ff_AttachForce |
| 226 | // Purpose: |
| 227 | // Attaches variables initialized in the general ddio system to |
| 228 | // the force feedback system. |
| 229 | // ------------------------------------------------------------------- |
| 230 | void ddio_ff_AttachForce(void); |
| 231 | // ------------------------------------------------------------------- |
| 232 | // ddio_ff_DetachForce |
| 233 | // Purpose: |
| 234 | // Detaches variables used by the force-feedback system from the |
| 235 | // ddio system |
| 236 | // ------------------------------------------------------------------- |
| 237 | void ddio_ff_DetachForce(void); |
| 238 | // ------------------------------------------------------------------- |
| 239 | // ddio_ff_Init |
| 240 | // Purpose: |
| 241 | // Initialize force feedback if available. |
| 242 | // ------------------------------------------------------------------- |
| 243 | int ddio_ff_Init(void); |
| 244 | // ------------------------------------------------------------------- |
| 245 | // ddio_ffjoy_Init |
| 246 | // Purpose: |
| 247 | // Creates and acquires all joysticks |
| 248 | // |
| 249 | // Input: |
| 250 | // None |
| 251 | // |
| 252 | // Return: |
| 253 | // # of sticks acquired |
| 254 | // |
| 255 | // Description: |
| 256 | // |
| 257 | // ------------------------------------------------------------------- |
| 258 | int ddio_ffjoy_Init(void); |
| 259 | // ------------------------------------------------------------------- |
| 260 | // ddio_ff_Acquire |
| 261 | // Purpose: |
| 262 | // Acquires a direct input device for use. |
| 263 | // |
| 264 | // Input: |
| 265 | // The device to acquire (use kDI_MaxJoy to acquire all available |
| 266 | // joysticks). |
| 267 | // |
| 268 | // Return: |
| 269 | // # of devices acquired. |
| 270 | // |
| 271 | // Description: |
| 272 | // Call this to gain access to a device after the device has been |
| 273 | // created & after regaining the focus after losing it. |
| 274 | // |
| 275 | // ------------------------------------------------------------------- |
| 276 | int ddio_ff_Acquire(tDevice dev); |
| 277 | // ------------------------------------------------------------------- |
| 278 | // ddio_ff_Unacquire |
| 279 | // Purpose: |
| 280 | // Unacquires a direct input device |
| 281 | // |
| 282 | // Input: |
| 283 | // The device to unacquire (use kDI_MaxJoy to unacquire all available |
| 284 | // joysticks). |
| 285 | // |
| 286 | // Return: |
| 287 | // # of devices unacquired. |
| 288 | // |
| 289 | // Description: |
| 290 | // Call this to lose access to a device after the device has been |
| 291 | // aquired |
| 292 | // |
| 293 | // ------------------------------------------------------------------- |
| 294 | int ddio_ff_Unacquire(tDevice dev); |
| 295 | // ------------------------------------------------------------------- |
| 296 | // ddio_ff_SetCoopLevel |
| 297 | // ------------------------------------------------------------------- |
| 298 | static int ddio_ff_SetCoopLevel(tDevice dev, int coop_level); |
| 299 | // ------------------------------------------------------------------- |
| 300 | // ddio_ffjoy_Query |
| 301 | // Purpose: |
| 302 | // Besides checking what buttons/axis are available, this function |
| 303 | // also checks for force feedback support. |
| 304 | // ------------------------------------------------------------------- |
| 305 | int ddio_ffjoy_Query(int dev, int *but_flags, int *axis_flags); |
| 306 | /* |
| 307 | ======================================================================== |
| 308 | Force Feedback Effect Functions |
| 309 | ======================================================================== |
| 310 | */ |
| 311 | // ------------------------------------------------------------------- |
| 312 | // ddio_ff_GetInfo |
| 313 | // Purpose: |
| 314 | // Returns information about the current state of the low-level |
| 315 | // Force Feedback system. |
| 316 | // ------------------------------------------------------------------- |
| 317 | void ddio_ff_GetInfo(bool *ff_found, bool *ff_enabled); |
| 318 | // ------------------------------------------------------------------- |
| 319 | // ddio_ffb_Pause |
| 320 | // Purpose: |
| 321 | // Pause the FFB output on the given device. Use ddio_ffb_Continue to |
| 322 | // continue where you left off. |
| 323 | // ------------------------------------------------------------------- |
| 324 | void ddio_ffb_Pause(tDevice dev); |
| 325 | // ------------------------------------------------------------------- |
| 326 | // ddio_ffb_Continue |
| 327 | // Purpose: |
| 328 | // Unpause the FFB output on the given device. Complimentary to |
| 329 | // ddio_ffb_Pause. |
| 330 | // ------------------------------------------------------------------- |
| 331 | void ddio_ffb_Continue(tDevice dev); |
| 332 | // ------------------------------------------------------------------- |
| 333 | // ddio_ffb_Enable |
| 334 | // Purpose: |
| 335 | // Must be called after initialization in order to activate the |
| 336 | // device. |
| 337 | // Use ddio_ffb_Pause & ddio_ffb_Continue if you want disable forces |
| 338 | // temporarily and resume later. |
| 339 | // ------------------------------------------------------------------- |
| 340 | void ddio_ffb_Enable(tDevice dev); |
| 341 | // ------------------------------------------------------------------- |
| 342 | // ddio_ffb_Disable |
| 343 | // Purpose: |
| 344 | // Turns off FFB, but effects still play on processor. |
| 345 | // ------------------------------------------------------------------- |
| 346 | void ddio_ffb_Disable(tDevice dev); |
| 347 | // ------------------------------------------------------------------- |
| 348 | // ddio_ffb_effectCreate |
| 349 | // Purpose: |
| 350 | // Create a single effect for future playback. |
| 351 | // Effect is given a logical ID |
| 352 | // ------------------------------------------------------------------- |
| 353 | int ddio_ffb_effectCreate(tDevice dev, tFFB_Effect *eff); |
| 354 | // ------------------------------------------------------------------- |
| 355 | // ddio_ffb_DestroyAll |
| 356 | // Purpose: |
| 357 | // Destroys all created effects |
| 358 | // ------------------------------------------------------------------- |
| 359 | void ddio_ffb_DestroyAll(void); |
| 360 | // ------------------------------------------------------------------- |
| 361 | // ddio_ffb_effectPlay |
| 362 | // Purpose: |
| 363 | // Play an effect that was previously created. |
| 364 | // ------------------------------------------------------------------- |
| 365 | void ddio_ffb_effectPlay(int16_t eID); |
| 366 | // ------------------------------------------------------------------- |
| 367 | // ddio_ffb_effectStop |
| 368 | // Purpose: |
| 369 | // Stop a single effect. |
| 370 | // ------------------------------------------------------------------- |
| 371 | void ddio_ffb_effectStop(int16_t eID); |
| 372 | // ------------------------------------------------------------------- |
| 373 | // ddio_ffb_effectStopAll |
| 374 | // Purpose: |
| 375 | // Stops all forces on the given device. |
| 376 | // ------------------------------------------------------------------- |
| 377 | void ddio_ffb_effectStopAll(tDevice dev); |
| 378 | // ------------------------------------------------------------------- |
| 379 | // ddio_ffb_effectUnload |
| 380 | // Purpose: |
| 381 | // Unload a single effect... Necessary to make room for other |
| 382 | // effects. |
| 383 | // ------------------------------------------------------------------- |
| 384 | void ddio_ffb_effectUnload(int16_t eID); |
| 385 | // ------------------------------------------------------------------- |
| 386 | // ddio_ffb_effectModify |
| 387 | // Purpose: |
| 388 | // Modifies a single effect, only if the given parameters are |
| 389 | // different from what's currently loaded. |
| 390 | // ------------------------------------------------------------------- |
| 391 | void ddio_ffb_effectModify(int16_t eID, int *Direction, uint32_t *Duration, uint32_t *Gain, uint32_t *Period, |
| 392 | tEffInfo *TypeInfo, tEffEnvelope *Envelope); |
| 393 | // ------------------------------------------------------------------- |
| 394 | // ddio_ffb_GetEffectData |
| 395 | // Purpose: |
| 396 | // Retrieves affect data for the given parameters, pass NULL for those you don't want |
| 397 | // ------------------------------------------------------------------- |
| 398 | void ddio_ffb_GetEffectData(int16_t eID, int *Direction, uint32_t *Duration, uint32_t *Gain, uint32_t *Period, |
| 399 | tEffInfo *TypeInfo, tEffEnvelope *Envelope); |
| 400 | // ------------------------------------------------------------------- |
| 401 | // ddio_ffjoy_EnableAutoCenter |
| 402 | // Purpose: |
| 403 | // Disables/Enables the autocentering of the joystick |
| 404 | // ------------------------------------------------------------------- |
| 405 | void ddio_ffjoy_EnableAutoCenter(tDevice dev, bool enable); |
| 406 | // ------------------------------------------------------------------- |
| 407 | // ddio_ffjoy_SetGain |
| 408 | // Purpose: |
| 409 | // Sets the gain for joystick, pass a value of 0-1 |
| 410 | // ------------------------------------------------------------------- |
| 411 | void ddio_ffjoy_SetGain(tDevice dev, float value); |
| 412 | // ------------------------------------------------------------------- |
| 413 | // ddio_ffjoy_IsAutoCentered |
| 414 | // Purpose: |
| 415 | // Returns true if the joystick is set for autocentering |
| 416 | // ------------------------------------------------------------------- |
| 417 | bool ddio_ffjoy_IsAutoCentered(tDevice dev); |
| 418 | // ------------------------------------------------------------------- |
| 419 | // ddio_ffjoy_SupportAutoCenter |
| 420 | // Purpose: |
| 421 | // Returns true if the FF joystick supports auto centering |
| 422 | // ------------------------------------------------------------------- |
| 423 | bool ddio_ffjoy_SupportAutoCenter(tDevice dev); |
| 424 | /* |
| 425 | =========================================================================== |
| 426 | Private Functions |
| 427 | =========================================================================== |
| 428 | */ |
| 429 | #if defined(WIN32) |
| 430 | // ------------------------------------------------------------------- |
| 431 | // ddio_ffjoy_AcquireErr |
| 432 | // Purpose: |
| 433 | // Handle success/err reporting |
| 434 | // ------------------------------------------------------------------- |
| 435 | static int ddio_ffjoy_AcquireErr(HRESULT res, int dev_num); |
| 436 | // ------------------------------------------------------------------- |
| 437 | // FFEnumCallback |
| 438 | // Purpose: |
| 439 | // Initialize all connected joysticks. |
| 440 | // |
| 441 | // Input: |
| 442 | // pdinst info about the current joystick being enumed. |
| 443 | // pvRef the direct input object that was passed in before |
| 444 | // starting the enum process. |
| 445 | // Return: |
| 446 | // DIENUM_CONTINUE continue calling us with any more devices |
| 447 | // DIENUM_STOP all done, don't call us back anymore, go away. |
| 448 | // ------------------------------------------------------------------- |
| 449 | BOOL CALLBACK FFEnumCallback(LPCDIDEVICEINSTANCE pdinst, LPVOID pvRef); |
| 450 | #endif |
| 451 | // Given a filename resource, this loads the file and creates a resource |
| 452 | // for it. It returns a handle to that resource. |
| 453 | // If it returns NULL, then it couldn't load the project. |
| 454 | // Make sure device is aquired before calling. |
| 455 | FORCEPROJECT ddio_ForceLoadProject(char *filename, tDevice dev); |
| 456 | // Unloads a FORCEPROJECT file |
| 457 | void ddio_ForceUnloadProject(FORCEPROJECT prj); |
| 458 | // Given a handle to a resource, and the name of the effect to load |
| 459 | // it will load that effect. Returns the effect ID, or -1 if it couldn't |
| 460 | // be created |
| 461 | int ddio_CreateForceFromProject(FORCEPROJECT project, const char *forcename); |
| 462 | #endif |