ff.txt 7.4 KB

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