haptic.nim 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. import "../sdl2"
  2. import "joystick"
  3. #
  4. # Simple DirectMedia Layer
  5. # Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  6. #
  7. # This software is provided 'as-is', without any express or implied
  8. # warranty. In no event will the authors be held liable for any damages
  9. # arising from the use of this software.
  10. #
  11. # Permission is granted to anyone to use this software for any purpose,
  12. # including commercial applications, and to alter it and redistribute it
  13. # freely, subject to the following restrictions:
  14. #
  15. # 1. The origin of this software must not be misrepresented; you must not
  16. # claim that you wrote the original software. If you use this software
  17. # in a product, an acknowledgment in the product documentation would be
  18. # appreciated but is not required.
  19. # 2. Altered source versions must be plainly marked as such, and must not be
  20. # misrepresented as being the original software.
  21. # 3. This notice may not be removed or altered from any source distribution.
  22. #
  23. #
  24. # \file SDL_haptic.h
  25. #
  26. # \brief The SDL Haptic subsystem allows you to control haptic (force feedback)
  27. # devices.
  28. #
  29. # The basic usage is as follows:
  30. # - Initialize the Subsystem (::SDL_INIT_HAPTIC).
  31. # - Open a Haptic Device.
  32. # - SDL_HapticOpen() to open from index.
  33. # - SDL_HapticOpenFromJoystick() to open from an existing joystick.
  34. # - Create an effect (::SDL_HapticEffect).
  35. # - Upload the effect with SDL_HapticNewEffect().
  36. # - Run the effect with SDL_HapticRunEffect().
  37. # - (optional) Free the effect with SDL_HapticDestroyEffect().
  38. # - Close the haptic device with SDL_HapticClose().
  39. #
  40. # \par Simple rumble example:
  41. # \code
  42. # haptic: HapticPtr;
  43. #
  44. # // Open the device
  45. # haptic = SDL_HapticOpen( 0 );
  46. # if (haptic == NULL)
  47. # return -1;
  48. #
  49. # // Initialize simple rumble
  50. # if (SDL_HapticRumbleInit( haptic ) != 0)
  51. # return -1;
  52. #
  53. # // Play effect at 50% strength for 2 seconds
  54. # if (SDL_HapticRumblePlay( haptic, 0.5, 2000 ) != 0)
  55. # return -1;
  56. # SDL_Delay( 2000 );
  57. #
  58. # // Clean up
  59. # SDL_HapticClose( haptic );
  60. # \endcode
  61. #
  62. # \par Complete example:
  63. # \code
  64. # int test_haptic( joystick: Joystick ) {
  65. # haptic: HapticPtr;
  66. # SDL_HapticEffect effect;
  67. # effect: cint_id;
  68. #
  69. # // Open the device
  70. # haptic = SDL_HapticOpenFromJoystick( joystick );
  71. # if (haptic == NULL) return -1; // Most likely joystick isn't haptic
  72. #
  73. # // See if it can do sine waves
  74. # if ((SDL_HapticQuery(haptic) & SDL_HAPTIC_SINE)==0) {
  75. # SDL_HapticClose(haptic); // No sine effect
  76. # return -1;
  77. # }
  78. #
  79. # // Create the effect
  80. # memset( &effect, 0, sizeof(SDL_HapticEffect) ); // 0 is safe default
  81. # effect.type = SDL_HAPTIC_SINE;
  82. # effect.periodic.direction.type = SDL_HAPTIC_POLAR; // Polar coordinates
  83. # effect.periodic.direction.dir[0] = 18000; // Force comes from south
  84. # effect.periodic.period = 1000; // 1000 ms
  85. # effect.periodic.magnitude = 20000; // 20000/32767 strength
  86. # effect.periodic.length = 5000; // 5 seconds long
  87. # effect.periodic.attack_length = 1000; // Takes 1 second to get max strength
  88. # effect.periodic.fade_length = 1000; // Takes 1 second to fade away
  89. #
  90. # // Upload the effect
  91. # effect_id = SDL_HapticNewEffect( haptic, &effect );
  92. #
  93. # // Test the effect
  94. # SDL_HapticRunEffect( haptic, effect_id, 1 );
  95. # SDL_Delay( 5000); // Wait for the effect to finish
  96. #
  97. # // We destroy the effect, although closing the device also does this
  98. # SDL_HapticDestroyEffect( haptic, effect_id );
  99. #
  100. # // Close the device
  101. # SDL_HapticClose(haptic);
  102. #
  103. # return 0; // Success
  104. # }
  105. # \endcode
  106. #
  107. # You can also find out more information on my blog:
  108. # http://bobbens.dyndns.org/journal/2010/sdl_haptic/
  109. #
  110. # \author Edgar Simo Serra
  111. # /
  112. #
  113. # \typedef SDL_Haptic
  114. #
  115. # \brief The haptic structure used to identify an SDL haptic.
  116. #
  117. # \sa SDL_HapticOpen
  118. # \sa SDL_HapticOpenFromJoystick
  119. # \sa SDL_HapticClose
  120. type
  121. Haptic* = object
  122. HapticPtr* = ptr Haptic
  123. #
  124. # \name Haptic features
  125. #
  126. # Different haptic features a device can have.
  127. # @{
  128. #
  129. # \name Haptic effects
  130. # @{
  131. #
  132. # \brief Constant effect supported.
  133. #
  134. # Constant haptic effect.
  135. #
  136. # \sa SDL_HapticCondition
  137. const SDL_HAPTIC_CONSTANT* = (1 shl 0)
  138. #
  139. # \brief Sine wave effect supported.
  140. #
  141. # Periodic haptic effect that simulates sine waves.
  142. #
  143. # \sa SDL_HapticPeriodic
  144. const SDL_HAPTIC_SINE* = (1 shl 1)
  145. #
  146. # \brief Left/Right effect supported.
  147. #
  148. # Haptic effect for direct control over high/low frequency motors.
  149. #
  150. # \sa SDL_HapticLeftRight
  151. # \warning this value was SDL_HAPTIC_SQUARE right before 2.0.0 shipped. Sorry,
  152. # we ran out of bits, and this is important for XInput devices.
  153. const SDL_HAPTIC_LEFTRIGHT* = (1 shl 2)
  154. # !!! FIXME: put this back when we have more bits in 2.1
  155. # const SDL_HAPTIC_SQUARE* = (1 shl 2)
  156. #
  157. # \brief Triangle wave effect supported.
  158. #
  159. # Periodic haptic effect that simulates triangular waves.
  160. #
  161. # \sa SDL_HapticPeriodic
  162. const SDL_HAPTIC_TRIANGLE* = (1 shl 3)
  163. #
  164. # \brief Sawtoothup wave effect supported.
  165. #
  166. # Periodic haptic effect that simulates saw tooth up waves.
  167. #
  168. # \sa SDL_HapticPeriodic
  169. const SDL_HAPTIC_SAWTOOTHUP* = (1 shl 4)
  170. #
  171. # \brief Sawtoothdown wave effect supported.
  172. #
  173. # Periodic haptic effect that simulates saw tooth down waves.
  174. #
  175. # \sa SDL_HapticPeriodic
  176. const SDL_HAPTIC_SAWTOOTHDOWN* = (1 shl 5)
  177. #
  178. # \brief Ramp effect supported.
  179. #
  180. # Ramp haptic effect.
  181. #
  182. # \sa SDL_HapticRamp
  183. const SDL_HAPTIC_RAMP* = (1 shl 6)
  184. #
  185. # \brief Spring effect supported - uses axes position.
  186. #
  187. # Condition haptic effect that simulates a spring. Effect is based on the
  188. # axes position.
  189. #
  190. # \sa SDL_HapticCondition
  191. const SDL_HAPTIC_SPRING* = (1 shl 7)
  192. #
  193. # \brief Damper effect supported - uses axes velocity.
  194. #
  195. # Condition haptic effect that simulates dampening. Effect is based on the
  196. # axes velocity.
  197. #
  198. # \sa SDL_HapticCondition
  199. const SDL_HAPTIC_DAMPER* = (1 shl 8)
  200. #
  201. # \brief Inertia effect supported - uses axes acceleration.
  202. #
  203. # Condition haptic effect that simulates inertia. Effect is based on the axes
  204. # acceleration.
  205. #
  206. # \sa SDL_HapticCondition
  207. const SDL_HAPTIC_INERTIA* = (1 shl 9)
  208. #
  209. # \brief Friction effect supported - uses axes movement.
  210. #
  211. # Condition haptic effect that simulates friction. Effect is based on the
  212. # axes movement.
  213. #
  214. # \sa SDL_HapticCondition
  215. const SDL_HAPTIC_FRICTION* = (1 shl 10)
  216. #
  217. # \brief Custom effect is supported.
  218. #
  219. # User defined custom haptic effect.
  220. const SDL_HAPTIC_CUSTOM* = (1 shl 11)
  221. # @} # Haptic effects
  222. # These last few are features the device has, not effects
  223. #
  224. # \brief Device can set global gain.
  225. #
  226. # Device supports setting the global gain.
  227. #
  228. # \sa SDL_HapticSetGain
  229. const SDL_HAPTIC_GAIN* = (1 shl 12)
  230. #
  231. # \brief Device can set autocenter.
  232. #
  233. # Device supports setting autocenter.
  234. #
  235. # \sa SDL_HapticSetAutocenter
  236. const SDL_HAPTIC_AUTOCENTER* = (1 shl 13)
  237. #
  238. # \brief Device can be queried for effect status.
  239. #
  240. # Device can be queried for effect status.
  241. #
  242. # \sa SDL_HapticGetEffectStatus
  243. const SDL_HAPTIC_STATUS* = (1 shl 14)
  244. #
  245. # \brief Device can be paused.
  246. #
  247. # \sa SDL_HapticPause
  248. # \sa SDL_HapticUnpause
  249. const SDL_HAPTIC_PAUSE* = (1 shl 15)
  250. #
  251. # \name Direction encodings
  252. # @{
  253. #
  254. # \brief Uses polar coordinates for the direction.
  255. #
  256. # \sa HapticDirection
  257. const SDL_HAPTIC_POLAR* = 0
  258. #
  259. # \brief Uses cartesian coordinates for the direction.
  260. #
  261. # \sa HapticDirection
  262. const SDL_HAPTIC_CARTESIAN* = 1
  263. #
  264. # \brief Uses spherical coordinates for the direction.
  265. #
  266. # \sa HapticDirection
  267. const SDL_HAPTIC_SPHERICAL* = 2
  268. # @} # Direction encodings
  269. # @} # Haptic features
  270. #
  271. # Misc defines.
  272. #
  273. # \brief Used to play a device an infinite number of times.
  274. #
  275. # \sa SDL_HapticRunEffect
  276. const SDL_HAPTIC_INFINITY* = 4294967295'u
  277. #
  278. # \brief Structure that represents a haptic direction.
  279. #
  280. # Directions can be specified by:
  281. # - ::SDL_HAPTIC_POLAR : Specified by polar coordinates.
  282. # - ::SDL_HAPTIC_CARTESIAN : Specified by cartesian coordinates.
  283. # - ::SDL_HAPTIC_SPHERICAL : Specified by spherical coordinates.
  284. #
  285. # Cardinal directions of the haptic device are relative to the positioning
  286. # of the device. North is considered to be away from the user.
  287. #
  288. # The following diagram represents the cardinal directions:
  289. # \verbatim
  290. # .--.
  291. # |__| .-------.
  292. # |=.| |.-----.|
  293. # |--| || ||
  294. # | | |'-----'|
  295. # |__|~')_____('
  296. # [ COMPUTER ]
  297. #
  298. #
  299. # North (0,-1)
  300. # ^
  301. # |
  302. # | = (1,0) West <----[ HAPTIC ]----> East (-1,0)
  303. # |
  304. # |
  305. # v
  306. # South (0,1)
  307. #
  308. #
  309. # [ USER ]
  310. # \|||/ = (o o)
  311. # ---ooO-(_)-Ooo---
  312. # \endverbatim
  313. #
  314. # If type is ::SDL_HAPTIC_POLAR, direction is encoded by hundredths of a
  315. # degree starting north and turning clockwise. ::SDL_HAPTIC_POLAR only uses
  316. # the first \c dir parameter. The cardinal directions would be:
  317. # - North: 0 (0 degrees)
  318. # - East: 9000 (90 degrees)
  319. # - South: 18000 (180 degrees)
  320. # - West: 27000 (270 degrees)
  321. #
  322. # If type is ::SDL_HAPTIC_CARTESIAN, direction is encoded by three positions
  323. # = (X axis, Y axis and Z axis (with 3 axes)). ::SDL_HAPTIC_CARTESIAN uses
  324. # the first three \c dir parameters. The cardinal directions would be:
  325. # - North: 0,-1, 0
  326. # - East: -1, 0, 0
  327. # - South: 0, 1, 0
  328. # - West: 1, 0, 0
  329. #
  330. # The Z axis represents the height of the effect if supported, otherwise
  331. # it's unused. In cartesian encoding (1, 2) would be the same as (2, 4), you
  332. # can use any multiple you want, only the direction matters.
  333. #
  334. # If type is ::SDL_HAPTIC_SPHERICAL, direction is encoded by two rotations.
  335. # The first two \c dir parameters are used. The \c dir parameters are as
  336. # follows (all values are in hundredths of degrees):
  337. # - Degrees from (1, 0) rotated towards (0, 1).
  338. # - Degrees towards (0, 0, 1) (device needs at least 3 axes).
  339. #
  340. #
  341. # Example of force coming from the south with all encodings (force coming
  342. # from the south means the user will have to pull the stick to counteract):
  343. # \code
  344. # HapticDirection direction;
  345. #
  346. # // Cartesian directions
  347. # direction.type = SDL_HAPTIC_CARTESIAN; // Using cartesian direction encoding.
  348. # direction.dir[0] = 0; // X position
  349. # direction.dir[1] = 1; // Y position
  350. # // Assuming the device has 2 axes, we don't need to specify third parameter.
  351. #
  352. # // Polar directions
  353. # direction.type = SDL_HAPTIC_POLAR; // We'll be using polar direction encoding.
  354. # direction.dir[0] = 18000; // Polar only uses first parameter
  355. #
  356. # // Spherical coordinates
  357. # direction.type = SDL_HAPTIC_SPHERICAL; // Spherical encoding
  358. # direction.dir[0] = 9000; // Since we only have two axes we don't need more parameters.
  359. # \endcode
  360. #
  361. # \sa SDL_HAPTIC_POLAR
  362. # \sa SDL_HAPTIC_CARTESIAN
  363. # \sa SDL_HAPTIC_SPHERICAL
  364. # \sa SDL_HapticEffect
  365. # \sa SDL_HapticNumAxes
  366. type
  367. HapticDirection* = object
  368. kind: uint8 # < The type of encoding.
  369. dir: array[3, int32] # < The encoded direction.
  370. #
  371. # \brief A structure containing a template for a Constant effect.
  372. #
  373. # The struct is exclusive to the ::SDL_HAPTIC_CONSTANT effect.
  374. #
  375. # A constant effect applies a constant force in the specified direction
  376. # to the joystick.
  377. #
  378. # \sa SDL_HAPTIC_CONSTANT
  379. # \sa SDL_HapticEffect
  380. type
  381. HapticConstant* = object
  382. # Header
  383. kind: uint16 # < ::SDL_HAPTIC_CONSTANT
  384. direction: HapticDirection # < Direction of the effect.
  385. # Replay
  386. length: uint32 # < Duration of the effect.
  387. delay: uint16 # < Delay before starting the effect.
  388. # Trigger
  389. button: uint16 # < Button that triggers the effect.
  390. interval: uint16 # < How soon it can be triggered again after button.
  391. # Constant
  392. level: int16 # < Strength of the constant effect.
  393. # Envelope
  394. attack_length: uint16 # < Duration of the attack.
  395. attack_level: uint16 # < Level at the start of the attack.
  396. fade_length: uint16 # < Duration of the fade.
  397. fade_level: uint16 # < Level at the end of the fade.
  398. #
  399. # \brief A structure containing a template for a Periodic effect.
  400. #
  401. # The struct handles the following effects:
  402. # - ::SDL_HAPTIC_SINE
  403. # - ::SDL_HAPTIC_LEFTRIGHT
  404. # - ::SDL_HAPTIC_TRIANGLE
  405. # - ::SDL_HAPTIC_SAWTOOTHUP
  406. # - ::SDL_HAPTIC_SAWTOOTHDOWN
  407. #
  408. # A periodic effect consists in a wave-shaped effect that repeats itself
  409. # over time. The type determines the shape of the wave and the parameters
  410. # determine the dimensions of the wave.
  411. #
  412. # Phase is given by hundredth of a cycle meaning that giving the phase a value
  413. # of 9000 will displace it 25% of its period. Here are sample values:
  414. # - 0: No phase displacement.
  415. # - 9000: Displaced 25% of its period.
  416. # - 18000: Displaced 50% of its period.
  417. # - 27000: Displaced 75% of its period.
  418. # - 36000: Displaced 100% of its period, same as 0, but 0 is preferred.
  419. #
  420. # Examples:
  421. # \verbatim
  422. # SDL_HAPTIC_SINE
  423. # __ __ __ __
  424. # / \ / \ / \ /
  425. # / \__/ \__/ \__/
  426. #
  427. # SDL_HAPTIC_SQUARE
  428. # __ __ __ __ __
  429. # | | | | | | | | | |
  430. # | |__| |__| |__| |__| |
  431. #
  432. # SDL_HAPTIC_TRIANGLE
  433. # /\ /\ /\ /\ /\
  434. # / \ / \ / \ / \ /
  435. # / \/ \/ \/ \/
  436. #
  437. # SDL_HAPTIC_SAWTOOTHUP
  438. # /| /| /| /| /| /| /|
  439. # / | / | / | / | / | / | / |
  440. # / |/ |/ |/ |/ |/ |/ |
  441. #
  442. # SDL_HAPTIC_SAWTOOTHDOWN
  443. # \ |\ |\ |\ |\ |\ |\ |
  444. # \ | \ | \ | \ | \ | \ | \ |
  445. # \| \| \| \| \| \| \|
  446. # \endverbatim
  447. #
  448. # \sa SDL_HAPTIC_SINE
  449. # \sa SDL_HAPTIC_LEFTRIGHT
  450. # \sa SDL_HAPTIC_TRIANGLE
  451. # \sa SDL_HAPTIC_SAWTOOTHUP
  452. # \sa SDL_HAPTIC_SAWTOOTHDOWN
  453. # \sa SDL_HapticEffect
  454. type
  455. HapticPeriodic* = object
  456. # Header
  457. kind: uint16 # < ::SDL_HAPTIC_SINE, ::SDL_HAPTIC_LEFTRIGHT,
  458. # ::SDL_HAPTIC_TRIANGLE, ::SDL_HAPTIC_SAWTOOTHUP or
  459. # ::SDL_HAPTIC_SAWTOOTHDOWN
  460. direction: HapticDirection # < Direction of the effect.
  461. # Replay
  462. length: uint32 # < Duration of the effect.
  463. delay: uint16 # < Delay before starting the effect.
  464. # Trigger
  465. button: uint16 # < Button that triggers the effect.
  466. interval: uint16 # < How soon it can be triggered again after button.
  467. # Periodic
  468. period: uint16 # < Period of the wave.
  469. magnitude: int16 # < Peak value.
  470. offset: int16 # < Mean value of the wave.
  471. phase: uint16 # < Horizontal shift given by hundredth of a cycle.
  472. # Envelope
  473. attack_length: uint16 # < Duration of the attack.
  474. attack_level: uint16 # < Level at the start of the attack.
  475. fade_length: uint16 # < Duration of the fade.
  476. fade_level: uint16 # < Level at the end of the fade.
  477. #
  478. # \brief A structure containing a template for a Condition effect.
  479. #
  480. # The struct handles the following effects:
  481. # - ::SDL_HAPTIC_SPRING: Effect based on axes position.
  482. # - ::SDL_HAPTIC_DAMPER: Effect based on axes velocity.
  483. # - ::SDL_HAPTIC_INERTIA: Effect based on axes acceleration.
  484. # - ::SDL_HAPTIC_FRICTION: Effect based on axes movement.
  485. #
  486. # Direction is handled by condition internals instead of a direction member.
  487. # The condition effect specific members have three parameters. The first
  488. # refers to the X axis, the second refers to the Y axis and the third
  489. # refers to the Z axis. The right terms refer to the positive side of the
  490. # axis and the left terms refer to the negative side of the axis. Please
  491. # refer to the ::HapticDirection diagram for which side is positive and
  492. # which is negative.
  493. #
  494. # \sa HapticDirection
  495. # \sa SDL_HAPTIC_SPRING
  496. # \sa SDL_HAPTIC_DAMPER
  497. # \sa SDL_HAPTIC_INERTIA
  498. # \sa SDL_HAPTIC_FRICTION
  499. # \sa SDL_HapticEffect
  500. type
  501. HapticCondition* = object
  502. # Header
  503. kind: uint16 # < ::SDL_HAPTIC_SPRING, ::SDL_HAPTIC_DAMPER,
  504. # ::SDL_HAPTIC_INERTIA or ::SDL_HAPTIC_FRICTION
  505. direction: HapticDirection # < Direction of the effect - Not used ATM.
  506. # Replay
  507. length: uint32 # < Duration of the effect.
  508. delay: uint16 # < Delay before starting the effect.
  509. # Trigger
  510. button: uint16 # < Button that triggers the effect.
  511. interval: uint16 # < How soon it can be triggered again after button.
  512. # Condition
  513. right_sat: array[3, uint16] # < Level when joystick is to the positive side.
  514. left_sat: array[3, uint16] # < Level when joystick is to the negative side.
  515. right_coeff: array[3, int16] # < How fast to increase the force towards the positive side.
  516. left_coeff: array[3, int16] # < How fast to increase the force towards the negative side.
  517. deadband: array[3, uint16] # < Size of the dead zone.
  518. center: array[3, int16] # < Position of the dead zone.
  519. #
  520. # \brief A structure containing a template for a Ramp effect.
  521. #
  522. # This struct is exclusively for the ::SDL_HAPTIC_RAMP effect.
  523. #
  524. # The ramp effect starts at start strength and ends at end strength.
  525. # It augments in linear fashion. If you use attack and fade with a ramp
  526. # the effects get added to the ramp effect making the effect become
  527. # quadratic instead of linear.
  528. #
  529. # \sa SDL_HAPTIC_RAMP
  530. # \sa SDL_HapticEffect
  531. type
  532. HapticRamp* = object
  533. # Header
  534. kind: uint16 # < ::SDL_HAPTIC_RAMP
  535. direction: HapticDirection # < Direction of the effect.
  536. # Replay
  537. length: uint32 # < Duration of the effect.
  538. delay: uint16 # < Delay before starting the effect.
  539. # Trigger
  540. button: uint16 # < Button that triggers the effect.
  541. interval: uint16 # < How soon it can be triggered again after button.
  542. # Ramp
  543. start: int16 # < Beginning strength level.
  544. fin: int16 # < Ending strength level.
  545. # Envelope
  546. attack_length: uint16 # < Duration of the attack.
  547. attack_level: uint16 # < Level at the start of the attack.
  548. fade_length: uint16 # < Duration of the fade.
  549. fade_level: uint16 # < Level at the end of the fade.
  550. #
  551. # \brief A structure containing a template for a Left/Right effect.
  552. #
  553. # This struct is exclusively for the ::SDL_HAPTIC_LEFTRIGHT effect.
  554. #
  555. # The Left/Right effect is used to explicitly control the large and small
  556. # motors, commonly found in modern game controllers. One motor is high
  557. # frequency, the other is low frequency.
  558. #
  559. # \sa SDL_HAPTIC_LEFTRIGHT
  560. # \sa SDL_HapticEffect
  561. type
  562. HapticLeftRight* = object
  563. # Header
  564. kind: uint16 # < ::SDL_HAPTIC_LEFTRIGHT
  565. # Replay
  566. length: uint32 # < Duration of the effect.
  567. # Rumble
  568. large_magnitude: uint16 # < Control of the large controller motor.
  569. small_magnitude: uint16 # < Control of the small controller motor.
  570. #
  571. # \brief A structure containing a template for the ::SDL_HAPTIC_CUSTOM effect.
  572. #
  573. # A custom force feedback effect is much like a periodic effect, where the
  574. # application can define its exact shape. You will have to allocate the
  575. # data yourself. Data should consist of channels# samples uint16 samples.
  576. #
  577. # If channels is one, the effect is rotated using the defined direction.
  578. # Otherwise it uses the samples in data for the different axes.
  579. #
  580. # \sa SDL_HAPTIC_CUSTOM
  581. # \sa SDL_HapticEffect
  582. type
  583. HapticCustom* = object
  584. # Header
  585. kind: uint16 # < ::SDL_HAPTIC_CUSTOM
  586. direction: HapticDirection # < Direction of the effect.
  587. # Replay
  588. length: uint32 # < Duration of the effect.
  589. delay: uint16 # < Delay before starting the effect.
  590. # Trigger
  591. button: uint16 # < Button that triggers the effect.
  592. interval: uint16 # < How soon it can be triggered again after button.
  593. # Custom
  594. channels: uint8 # < Axes to use, minimum of one.
  595. period: uint16 # < Sample periods.
  596. samples: uint16 # < Amount of samples.
  597. data: ptr uint16 # < Should contain channels*samples items.
  598. # Envelope
  599. attack_length: uint16 # < Duration of the attack.
  600. attack_level: uint16 # < Level at the start of the attack.
  601. fade_length: uint16 # < Duration of the fade.
  602. fade_level: uint16 # < Level at the end of the fade.
  603. #
  604. # \brief The generic template for any haptic effect.
  605. #
  606. # All values max at 32767 (0x7FFF). Signed values also can be negative.
  607. # Time values unless specified otherwise are in milliseconds.
  608. #
  609. # You can also pass ::SDL_HAPTIC_INFINITY to length instead of a 0-32767
  610. # value. Neither delay, interval, attack_length nor fade_length support
  611. # ::SDL_HAPTIC_INFINITY. Fade will also not be used since effect never ends.
  612. #
  613. # Additionally, the ::SDL_HAPTIC_RAMP effect does not support a duration of
  614. # ::SDL_HAPTIC_INFINITY.
  615. #
  616. # Button triggers may not be supported on all devices, it is advised to not
  617. # use them if possible. Buttons start at index 1 instead of index 0 like
  618. # the joystick.
  619. #
  620. # If both attack_length and fade_level are 0, the envelope is not used,
  621. # otherwise both values are used.
  622. #
  623. # Common parts:
  624. # \code
  625. # // Replay - All effects have this
  626. # uint32 length; // Duration of effect (ms).
  627. # uint16 delay; // Delay before starting effect.
  628. #
  629. # // Trigger - All effects have this
  630. # uint16 button; // Button that triggers effect.
  631. # uint16 interval; // How soon before effect can be triggered again.
  632. #
  633. # // Envelope - All effects except condition effects have this
  634. # uint16 attack_length; // Duration of the attack (ms).
  635. # uint16 attack_level; // Level at the start of the attack.
  636. # uint16 fade_length; // Duration of the fade out (ms).
  637. # uint16 fade_level; // Level at the end of the fade.
  638. # \endcode
  639. #
  640. #
  641. # Here we have an example of a constant effect evolution in time:
  642. # \verbatim
  643. # Strength
  644. # ^
  645. # |
  646. # | effect level --> _________________
  647. # | / \
  648. # | / \
  649. # | / \
  650. # | / \
  651. # | attack_level --> | \
  652. # | | | <--- fade_level
  653. # |
  654. # +--------------------------------------------------> Time
  655. # [--] [---]
  656. # attack_length fade_length
  657. #
  658. # [------------------][-----------------------]
  659. # delay length
  660. # \endverbatim
  661. #
  662. # Note either the attack_level or the fade_level may be above the actual
  663. # effect level.
  664. #
  665. # \sa SDL_HapticConstant
  666. # \sa SDL_HapticPeriodic
  667. # \sa SDL_HapticCondition
  668. # \sa SDL_HapticRamp
  669. # \sa SDL_HapticLeftRight
  670. # \sa SDL_HapticCustom
  671. type
  672. HapticEffect* = object {.union.}
  673. # Common for all force feedback effects
  674. kind: uint16 # < Effect type.
  675. constant: HapticConstant # < Constant effect.
  676. periodic: HapticPeriodic # < Periodic effect.
  677. condition: HapticCondition # < Condition effect.
  678. ramp: HapticRamp # < Ramp effect.
  679. leftright: HapticLeftRight # < Left/Right effect.
  680. custom: HapticCustom # < Custom effect.
  681. when defined(SDL_Static):
  682. static: echo "SDL_Static option is deprecated and will soon be removed. Instead please use --dynlibOverride:SDL2."
  683. else:
  684. {.push callConv: cdecl, dynlib: LibName.}
  685. # Function prototypes
  686. #
  687. # \brief Count the number of haptic devices attached to the system.
  688. #
  689. # \return Number of haptic devices detected on the system.
  690. proc numHaptics*():cint {.importc: "SDL_NumHaptics".}
  691. #
  692. # \brief Get the implementation dependent name of a Haptic device.
  693. #
  694. # This can be called before any joysticks are opened.
  695. # If no name can be found, this function returns NULL.
  696. #
  697. # \param device_index Index of the device to get its name.
  698. # \return Name of the device or NULL on error.
  699. #
  700. # \sa SDL_NumHaptics
  701. proc hapticName*(device_index: cint):cstring {.importc: "SDL_HapticName".}
  702. #
  703. # \brief Opens a Haptic device for usage.
  704. #
  705. # The index passed as an argument refers to the N'th Haptic device on this
  706. # system.
  707. #
  708. # When opening a haptic device, its gain will be set to maximum and
  709. # autocenter will be disabled. To modify these values use
  710. # SDL_HapticSetGain() and SDL_HapticSetAutocenter().
  711. #
  712. # \param device_index Index of the device to open.
  713. # \return Device identifier or NULL on error.
  714. #
  715. # \sa SDL_HapticIndex
  716. # \sa SDL_HapticOpenFromMouse
  717. # \sa SDL_HapticOpenFromJoystick
  718. # \sa SDL_HapticClose
  719. # \sa SDL_HapticSetGain
  720. # \sa SDL_HapticSetAutocenter
  721. # \sa SDL_HapticPause
  722. # \sa SDL_HapticStopAll
  723. proc hapticOpen*(device_index: cint):HapticPtr {.importc: "SDL_HapticOpen".}
  724. #
  725. # \brief Checks if the haptic device at index has been opened.
  726. #
  727. # \param device_index Index to check to see if it has been opened.
  728. # \return 1 if it has been opened or 0 if it hasn't.
  729. #
  730. # \sa SDL_HapticOpen
  731. # \sa SDL_HapticIndex
  732. proc hapticOpened*(device_index: cint):cint {.importc: "SDL_HapticOpened".}
  733. #
  734. # \brief Gets the index of a haptic device.
  735. #
  736. # \param haptic Haptic device to get the index of.
  737. # \return The index of the haptic device or -1 on error.
  738. #
  739. # \sa SDL_HapticOpen
  740. # \sa SDL_HapticOpened
  741. proc index*(haptic: HapticPtr):cint {.importc: "SDL_HapticIndex".}
  742. #
  743. # \brief Gets whether or not the current mouse has haptic capabilities.
  744. #
  745. # \return SDL_TRUE if the mouse is haptic, SDL_FALSE if it isn't.
  746. #
  747. # \sa SDL_HapticOpenFromMouse
  748. proc mouseIsHaptic*():cint {.importc: "SDL_MouseIsHaptic".}
  749. #
  750. # \brief Tries to open a haptic device from the current mouse.
  751. #
  752. # \return The haptic device identifier or NULL on error.
  753. #
  754. # \sa SDL_MouseIsHaptic
  755. # \sa SDL_HapticOpen
  756. proc hapticOpenFromMouse*():HapticPtr {.importc: "SDL_HapticOpenFromMouse".}
  757. #
  758. # \brief Checks to see if a joystick has haptic features.
  759. #
  760. # \param joystick Joystick to test for haptic capabilities.
  761. # \return 1 if the joystick is haptic, 0 if it isn't
  762. # or -1 if an error ocurred.
  763. #
  764. # \sa SDL_HapticOpenFromJoystick
  765. proc joystickIsHaptic*(joystick: Joystick):cint {.importc: "SDL_JoystickIsHaptic".}
  766. #
  767. # \brief Opens a Haptic device for usage from a Joystick device.
  768. #
  769. # You must still close the haptic device seperately. It will not be closed
  770. # with the joystick.
  771. #
  772. # When opening from a joystick you should first close the haptic device before
  773. # closing the joystick device. If not, on some implementations the haptic
  774. # device will also get unallocated and you'll be unable to use force feedback
  775. # on that device.
  776. #
  777. # \param joystick Joystick to create a haptic device from.
  778. # \return A valid haptic device identifier on success or NULL on error.
  779. #
  780. # \sa SDL_HapticOpen
  781. # \sa SDL_HapticClose
  782. proc hapticOpenFromJoystick*(joystick: JoystickPtr):HapticPtr {.importc: "SDL_HapticOpenFromJoystick".}
  783. #
  784. # \brief Closes a Haptic device previously opened with SDL_HapticOpen().
  785. #
  786. # \param haptic Haptic device to close.
  787. proc close*(haptic: HapticPtr) {.importc: "SDL_HapticClose".}
  788. #
  789. # \brief Returns the number of effects a haptic device can store.
  790. #
  791. # On some platforms this isn't fully supported, and therefore is an
  792. # approximation. Always check to see if your created effect was actually
  793. # created and do not rely solely on SDL_HapticNumEffects().
  794. #
  795. # \param haptic The haptic device to query effect max.
  796. # \return The number of effects the haptic device can store or
  797. # -1 on error.
  798. #
  799. # \sa SDL_HapticNumEffectsPlaying
  800. # \sa SDL_HapticQuery
  801. proc numEffects*(haptic: HapticPtr):cint {.importc: "SDL_HapticNumEffects".}
  802. #
  803. # \brief Returns the number of effects a haptic device can play at the same
  804. # time.
  805. #
  806. # This is not supported on all platforms, but will always return a value.
  807. # Added here for the sake of completeness.
  808. #
  809. # \param haptic The haptic device to query maximum playing effects.
  810. # \return The number of effects the haptic device can play at the same time
  811. # or -1 on error.
  812. #
  813. # \sa SDL_HapticNumEffects
  814. # \sa SDL_HapticQuery
  815. proc numEffectsPlaying*(haptic: HapticPtr):cint {.importc: "SDL_HapticNumEffectsPlaying".}
  816. #
  817. # \brief Gets the haptic devices supported features in bitwise matter.
  818. #
  819. # Example:
  820. # \code
  821. # if (SDL_HapticQuery(haptic) & SDL_HAPTIC_CONSTANT) {
  822. # printf("We have constant haptic effect!");
  823. # }
  824. # \endcode
  825. #
  826. # \param haptic The haptic device to query.
  827. # \return Haptic features in bitwise manner (OR'd).
  828. #
  829. # \sa SDL_HapticNumEffects
  830. # \sa SDL_HapticEffectSupported
  831. proc query*(haptic: HapticPtr):uint {.importc: "SDL_HapticQuery".}
  832. #
  833. # \brief Gets the number of haptic axes the device has.
  834. #
  835. # \sa HapticDirection
  836. proc numAxes*(haptic: HapticPtr):cint {.importc: "SDL_HapticNumAxes".}
  837. #
  838. # \brief Checks to see if effect is supported by haptic.
  839. #
  840. # \param haptic Haptic device to check on.
  841. # \param effect Effect to check to see if it is supported.
  842. # \return SDL_TRUE if effect is supported, SDL_FALSE if it isn't or -1 on error.
  843. #
  844. # \sa SDL_HapticQuery
  845. # \sa SDL_HapticNewEffect
  846. proc effectSupported*(haptic: HapticPtr, effect: ptr HapticEffect):cint {.importc: "SDL_HapticEffectSupported".}
  847. #
  848. # \brief Creates a new haptic effect on the device.
  849. #
  850. # \param haptic Haptic device to create the effect on.
  851. # \param effect Properties of the effect to create.
  852. # \return The id of the effect on success or -1 on error.
  853. #
  854. # \sa SDL_HapticUpdateEffect
  855. # \sa SDL_HapticRunEffect
  856. # \sa SDL_HapticDestroyEffect
  857. proc newEffect*(haptic: HapticPtr, effect: ptr HapticEffect):cint {.importc: "SDL_HapticNewEffect".}
  858. #
  859. # \brief Updates the properties of an effect.
  860. #
  861. # Can be used dynamically, although behaviour when dynamically changing
  862. # direction may be strange. Specifically the effect may reupload itself
  863. # and start playing from the start. You cannot change the type either when
  864. # running SDL_HapticUpdateEffect().
  865. #
  866. # \param haptic Haptic device that has the effect.
  867. # \param effect Effect to update.
  868. # \param data New effect properties to use.
  869. # \return 0 on success or -1 on error.
  870. #
  871. # \sa SDL_HapticNewEffect
  872. # \sa SDL_HapticRunEffect
  873. # \sa SDL_HapticDestroyEffect
  874. proc updateEffect*(haptic: HapticPtr, effect: cint, data: ptr HapticEffect):cint {.importc: "SDL_HapticUpdateEffect".}
  875. #
  876. # \brief Runs the haptic effect on its associated haptic device.
  877. #
  878. # If iterations are ::SDL_HAPTIC_INFINITY, it'll run the effect over and over
  879. # repeating the envelope (attack and fade) every time. If you only want the
  880. # effect to last forever, set ::SDL_HAPTIC_INFINITY in the effect's length
  881. # parameter.
  882. #
  883. # \param haptic Haptic device to run the effect on.
  884. # \param effect Identifier of the haptic effect to run.
  885. # \param iterations Number of iterations to run the effect. Use
  886. # ::SDL_HAPTIC_INFINITY for infinity.
  887. # \return 0 on success or -1 on error.
  888. #
  889. # \sa SDL_HapticStopEffect
  890. # \sa SDL_HapticDestroyEffect
  891. # \sa SDL_HapticGetEffectStatus
  892. proc runEffect*(haptic: HapticPtr, effect: cint, iterations: uint32):cint {.importc: "SDL_HapticRunEffect".}
  893. #
  894. # \brief Stops the haptic effect on its associated haptic device.
  895. #
  896. # \param haptic Haptic device to stop the effect on.
  897. # \param effect Identifier of the effect to stop.
  898. # \return 0 on success or -1 on error.
  899. #
  900. # \sa SDL_HapticRunEffect
  901. # \sa SDL_HapticDestroyEffect
  902. proc stopEffect*(haptic: HapticPtr, effect: cint):cint {.importc: "SDL_HapticStopEffect".}
  903. #
  904. # \brief Destroys a haptic effect on the device.
  905. #
  906. # This will stop the effect if it's running. Effects are automatically
  907. # destroyed when the device is closed.
  908. #
  909. # \param haptic Device to destroy the effect on.
  910. # \param effect Identifier of the effect to destroy.
  911. #
  912. # \sa SDL_HapticNewEffect
  913. proc destroyEffect*(haptic: HapticPtr, effect: cint) {.importc: "SDL_HapticDestroyEffect".}
  914. #
  915. # \brief Gets the status of the current effect on the haptic device.
  916. #
  917. # Device must support the ::SDL_HAPTIC_STATUS feature.
  918. #
  919. # \param haptic Haptic device to query the effect status on.
  920. # \param effect Identifier of the effect to query its status.
  921. # \return 0 if it isn't playing, 1 if it is playing or -1 on error.
  922. #
  923. # \sa SDL_HapticRunEffect
  924. # \sa SDL_HapticStopEffect
  925. proc getEffectStatus*(haptic: HapticPtr, effect: cint):cint {.importc: "SDL_HapticGetEffectStatus".}
  926. #
  927. # \brief Sets the global gain of the device.
  928. #
  929. # Device must support the ::SDL_HAPTIC_GAIN feature.
  930. #
  931. # The user may specify the maximum gain by setting the environment variable
  932. # SDL_HAPTIC_GAIN_MAX which should be between 0 and 100. All calls to
  933. # SDL_HapticSetGain() will scale linearly using SDL_HAPTIC_GAIN_MAX as the
  934. # maximum.
  935. #
  936. # \param haptic Haptic device to set the gain on.
  937. # \param gain Value to set the gain to, should be between 0 and 100.
  938. # \return 0 on success or -1 on error.
  939. #
  940. # \sa SDL_HapticQuery
  941. proc setGain*(haptic: HapticPtr, gain: int ):cint {.importc: "SDL_HapticSetGain".}
  942. #
  943. # \brief Sets the global autocenter of the device.
  944. #
  945. # Autocenter should be between 0 and 100. Setting it to 0 will disable
  946. # autocentering.
  947. #
  948. # Device must support the ::SDL_HAPTIC_AUTOCENTER feature.
  949. #
  950. # \param haptic Haptic device to set autocentering on.
  951. # \param autocenter Value to set autocenter to, 0 disables autocentering.
  952. # \return 0 on success or -1 on error.
  953. #
  954. # \sa SDL_HapticQuery
  955. proc setAutocenter*(haptic: HapticPtr, autocenter: int ):cint {.importc: "SDL_HapticSetAutocenter".}
  956. #
  957. # \brief Pauses a haptic device.
  958. #
  959. # Device must support the ::SDL_HAPTIC_PAUSE feature. Call
  960. # SDL_HapticUnpause() to resume playback.
  961. #
  962. # Do not modify the effects nor add new ones while the device is paused.
  963. # That can cause all sorts of weird errors.
  964. #
  965. # \param haptic Haptic device to pause.
  966. # \return 0 on success or -1 on error.
  967. #
  968. # \sa SDL_HapticUnpause
  969. proc pause*(haptic: HapticPtr):cint {.importc: "SDL_HapticPause".}
  970. #
  971. # \brief Unpauses a haptic device.
  972. #
  973. # Call to unpause after SDL_HapticPause().
  974. #
  975. # \param haptic Haptic device to pause.
  976. # \return 0 on success or -1 on error.
  977. #
  978. # \sa SDL_HapticPause
  979. proc unpause*(haptic: HapticPtr):cint {.importc: "SDL_HapticUnpause".}
  980. #
  981. # \brief Stops all the currently playing effects on a haptic device.
  982. #
  983. # \param haptic Haptic device to stop.
  984. # \return 0 on success or -1 on error.
  985. proc stopAll*(haptic: HapticPtr):cint {.importc: "SDL_HapticStopAll".}
  986. #
  987. # \brief Checks to see if rumble is supported on a haptic device.
  988. #
  989. # \param haptic Haptic device to check to see if it supports rumble.
  990. # \return SDL_TRUE if effect is supported, SDL_FALSE if it isn't or -1 on error.
  991. #
  992. # \sa SDL_HapticRumbleInit
  993. # \sa SDL_HapticRumblePlay
  994. # \sa SDL_HapticRumbleStop
  995. proc rumbleSupported*(haptic: HapticPtr):cint {.importc: "SDL_HapticRumbleSupported".}
  996. #
  997. # \brief Initializes the haptic device for simple rumble playback.
  998. #
  999. # \param haptic Haptic device to initialize for simple rumble playback.
  1000. # \return 0 on success or -1 on error.
  1001. #
  1002. # \sa SDL_HapticOpen
  1003. # \sa SDL_HapticRumbleSupported
  1004. # \sa SDL_HapticRumblePlay
  1005. # \sa SDL_HapticRumbleStop
  1006. proc rumbleInit*(haptic: HapticPtr):cint {.importc: "SDL_HapticRumbleInit".}
  1007. #
  1008. # \brief Runs simple rumble on a haptic device
  1009. #
  1010. # \param haptic Haptic device to play rumble effect on.
  1011. # \param strength Strength of the rumble to play as a 0-1 float value.
  1012. # \param length Length of the rumble to play in milliseconds.
  1013. # \return 0 on success or -1 on error.
  1014. #
  1015. # \sa SDL_HapticRumbleSupported
  1016. # \sa SDL_HapticRumbleInit
  1017. # \sa SDL_HapticRumbleStop
  1018. proc rumblePlay*(haptic: HapticPtr, strength: float, length: uint32 ):cint {.importc: "SDL_HapticRumblePlay".}
  1019. #
  1020. # \brief Stops the simple rumble on a haptic device.
  1021. #
  1022. # \param haptic Haptic to stop the rumble on.
  1023. # \return 0 on success or -1 on error.
  1024. #
  1025. # \sa SDL_HapticRumbleSupported
  1026. # \sa SDL_HapticRumbleInit
  1027. # \sa SDL_HapticRumblePlay
  1028. proc rumbleStop*(haptic: HapticPtr):cint {.importc: "SDL_HapticRumbleStop".}
  1029. when not defined(SDL_Static):
  1030. {.pop.}