ff.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ========================
  2. Force feedback for Linux
  3. ========================
  4. :Author: Johann Deneux <[email protected]> on 2001/04/22.
  5. :Updated: Anssi Hannula <[email protected]> on 2006/04/09.
  6. You may redistribute this file. Please remember to include shape.svg and
  7. interactive.svg as well.
  8. Introduction
  9. ~~~~~~~~~~~~
  10. This document describes how to use force feedback devices under Linux. The
  11. goal is not to support these devices as if they were simple input-only devices
  12. (as it is already the case), but to really enable the rendering of force
  13. effects.
  14. This document only describes the force feedback part of the Linux input
  15. interface. Please read joydev/joystick.rst and input.rst before reading further
  16. this document.
  17. Instructions to the user
  18. ~~~~~~~~~~~~~~~~~~~~~~~~
  19. To enable force feedback, you have to:
  20. 1. have your kernel configured with evdev and a driver that supports your
  21. device.
  22. 2. make sure evdev module is loaded and /dev/input/event* device files are
  23. created.
  24. Before you start, let me WARN you that some devices shake violently during the
  25. initialisation phase. This happens for example with my "AVB Top Shot Pegasus".
  26. To stop this annoying behaviour, move your joystick to its limits. Anyway, you
  27. should keep a hand on your device, in order to avoid it to break down if
  28. something goes wrong.
  29. If you have a serial iforce device, you need to start inputattach. See
  30. joydev/joystick.rst for details.
  31. Does it work ?
  32. --------------
  33. There is an utility called fftest that will allow you to test the driver::
  34. % fftest /dev/input/eventXX
  35. Instructions to the developer
  36. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37. All interactions are done using the event API. That is, you can use ioctl()
  38. and write() on /dev/input/eventXX.
  39. This information is subject to change.
  40. Querying device capabilities
  41. ----------------------------
  42. ::
  43. #include <linux/input.h>
  44. #include <sys/ioctl.h>
  45. #define BITS_TO_LONGS(x) \
  46. (((x) + 8 * sizeof (unsigned long) - 1) / (8 * sizeof (unsigned long)))
  47. unsigned long features[BITS_TO_LONGS(FF_CNT)];
  48. int ioctl(int file_descriptor, int request, unsigned long *features);
  49. "request" must be EVIOCGBIT(EV_FF, size of features array in bytes )
  50. Returns the features supported by the device. features is a bitfield with the
  51. following bits:
  52. - FF_CONSTANT can render constant force effects
  53. - FF_PERIODIC can render periodic effects with the following waveforms:
  54. - FF_SQUARE square waveform
  55. - FF_TRIANGLE triangle waveform
  56. - FF_SINE sine waveform
  57. - FF_SAW_UP sawtooth up waveform
  58. - FF_SAW_DOWN sawtooth down waveform
  59. - FF_CUSTOM custom waveform
  60. - FF_RAMP can render ramp effects
  61. - FF_SPRING can simulate the presence of a spring
  62. - FF_FRICTION can simulate friction
  63. - FF_DAMPER can simulate damper effects
  64. - FF_RUMBLE rumble effects
  65. - FF_INERTIA can simulate inertia
  66. - FF_GAIN gain is adjustable
  67. - FF_AUTOCENTER autocenter is adjustable
  68. .. note::
  69. - In most cases you should use FF_PERIODIC instead of FF_RUMBLE. All
  70. devices that support FF_RUMBLE support FF_PERIODIC (square, triangle,
  71. sine) and the other way around.
  72. - The exact syntax FF_CUSTOM is undefined for the time being as no driver
  73. supports it yet.
  74. ::
  75. int ioctl(int fd, EVIOCGEFFECTS, int *n);
  76. Returns the number of effects the device can keep in its memory.
  77. Uploading effects to the device
  78. -------------------------------
  79. ::
  80. #include <linux/input.h>
  81. #include <sys/ioctl.h>
  82. int ioctl(int file_descriptor, int request, struct ff_effect *effect);
  83. "request" must be EVIOCSFF.
  84. "effect" points to a structure describing the effect to upload. The effect is
  85. uploaded, but not played.
  86. The content of effect may be modified. In particular, its field "id" is set
  87. to the unique id assigned by the driver. This data is required for performing
  88. some operations (removing an effect, controlling the playback).
  89. The "id" field must be set to -1 by the user in order to tell the driver to
  90. allocate a new effect.
  91. Effects are file descriptor specific.
  92. See <uapi/linux/input.h> for a description of the ff_effect struct. You
  93. should also find help in a few sketches, contained in files shape.svg
  94. and interactive.svg:
  95. .. kernel-figure:: shape.svg
  96. Shape
  97. .. kernel-figure:: interactive.svg
  98. Interactive
  99. Removing an effect from the device
  100. ----------------------------------
  101. ::
  102. int ioctl(int fd, EVIOCRMFF, effect.id);
  103. This makes room for new effects in the device's memory. Note that this also
  104. stops the effect if it was playing.
  105. Controlling the playback of effects
  106. -----------------------------------
  107. Control of playing is done with write(). Below is an example:
  108. ::
  109. #include <linux/input.h>
  110. #include <unistd.h>
  111. struct input_event play;
  112. struct input_event stop;
  113. struct ff_effect effect;
  114. int fd;
  115. ...
  116. fd = open("/dev/input/eventXX", O_RDWR);
  117. ...
  118. /* Play three times */
  119. play.type = EV_FF;
  120. play.code = effect.id;
  121. play.value = 3;
  122. write(fd, (const void*) &play, sizeof(play));
  123. ...
  124. /* Stop an effect */
  125. stop.type = EV_FF;
  126. stop.code = effect.id;
  127. stop.value = 0;
  128. write(fd, (const void*) &stop, sizeof(stop));
  129. Setting the gain
  130. ----------------
  131. Not all devices have the same strength. Therefore, users should set a gain
  132. factor depending on how strong they want effects to be. This setting is
  133. persistent across access to the driver.
  134. ::
  135. /* Set the gain of the device
  136. int gain; /* between 0 and 100 */
  137. struct input_event ie; /* structure used to communicate with the driver */
  138. ie.type = EV_FF;
  139. ie.code = FF_GAIN;
  140. ie.value = 0xFFFFUL * gain / 100;
  141. if (write(fd, &ie, sizeof(ie)) == -1)
  142. perror("set gain");
  143. Enabling/Disabling autocenter
  144. -----------------------------
  145. The autocenter feature quite disturbs the rendering of effects in my opinion,
  146. and I think it should be an effect, which computation depends on the game
  147. type. But you can enable it if you want.
  148. ::
  149. int autocenter; /* between 0 and 100 */
  150. struct input_event ie;
  151. ie.type = EV_FF;
  152. ie.code = FF_AUTOCENTER;
  153. ie.value = 0xFFFFUL * autocenter / 100;
  154. if (write(fd, &ie, sizeof(ie)) == -1)
  155. perror("set auto-center");
  156. A value of 0 means "no auto-center".
  157. Dynamic update of an effect
  158. ---------------------------
  159. Proceed as if you wanted to upload a new effect, except that instead of
  160. setting the id field to -1, you set it to the wanted effect id.
  161. Normally, the effect is not stopped and restarted. However, depending on the
  162. type of device, not all parameters can be dynamically updated. For example,
  163. the direction of an effect cannot be updated with iforce devices. In this
  164. case, the driver stops the effect, up-load it, and restart it.
  165. Therefore it is recommended to dynamically change direction while the effect
  166. is playing only when it is ok to restart the effect with a replay count of 1.
  167. Information about the status of effects
  168. ---------------------------------------
  169. Every time the status of an effect is changed, an event is sent. The values
  170. and meanings of the fields of the event are as follows::
  171. struct input_event {
  172. /* When the status of the effect changed */
  173. struct timeval time;
  174. /* Set to EV_FF_STATUS */
  175. unsigned short type;
  176. /* Contains the id of the effect */
  177. unsigned short code;
  178. /* Indicates the status */
  179. unsigned int value;
  180. };
  181. FF_STATUS_STOPPED The effect stopped playing
  182. FF_STATUS_PLAYING The effect started to play
  183. .. note::
  184. - Status feedback is only supported by iforce driver. If you have
  185. a really good reason to use this, please contact
  186. [email protected] or [email protected]
  187. so that support for it can be added to the rest of the drivers.