ff.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. Force feedback for Linux.
  2. By Johann Deneux <deneux@ifrance.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. unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
  44. int ioctl(int file_descriptor, int request, unsigned long *features);
  45. "request" must be EVIOCGBIT(EV_FF, size of features array in bytes )
  46. Returns the features supported by the device. features is a bitfield with the
  47. following bits:
  48. - FF_CONSTANT can render constant force effects
  49. - FF_PERIODIC can render periodic effects with the following waveforms:
  50. - FF_SQUARE square waveform
  51. - FF_TRIANGLE triangle waveform
  52. - FF_SINE sine waveform
  53. - FF_SAW_UP sawtooth up waveform
  54. - FF_SAW_DOWN sawtooth down waveform
  55. - FF_CUSTOM custom waveform
  56. - FF_RAMP can render ramp effects
  57. - FF_SPRING can simulate the presence of a spring
  58. - FF_FRICTION can simulate friction
  59. - FF_DAMPER can simulate damper effects
  60. - FF_RUMBLE rumble effects
  61. - FF_INERTIA can simulate inertia
  62. - FF_GAIN gain is adjustable
  63. - FF_AUTOCENTER autocenter is adjustable
  64. Note: In most cases you should use FF_PERIODIC instead of FF_RUMBLE. All
  65. devices that support FF_RUMBLE support FF_PERIODIC (square, triangle,
  66. sine) and the other way around.
  67. Note: The exact syntax FF_CUSTOM is undefined for the time being as no driver
  68. supports it yet.
  69. int ioctl(int fd, EVIOCGEFFECTS, int *n);
  70. Returns the number of effects the device can keep in its memory.
  71. 3.2 Uploading effects to the device
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. #include <linux/input.h>
  74. #include <sys/ioctl.h>
  75. int ioctl(int file_descriptor, int request, struct ff_effect *effect);
  76. "request" must be EVIOCSFF.
  77. "effect" points to a structure describing the effect to upload. The effect is
  78. uploaded, but not played.
  79. The content of effect may be modified. In particular, its field "id" is set
  80. to the unique id assigned by the driver. This data is required for performing
  81. some operations (removing an effect, controlling the playback).
  82. This if field must be set to -1 by the user in order to tell the driver to
  83. allocate a new effect.
  84. Effects are file descriptor specific.
  85. See <linux/input.h> for a description of the ff_effect struct. You should also
  86. find help in a few sketches, contained in files shape.fig and interactive.fig.
  87. You need xfig to visualize these files.
  88. 3.3 Removing an effect from the device
  89. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. int ioctl(int fd, EVIOCRMFF, effect.id);
  91. This makes room for new effects in the device's memory. Note that this also
  92. stops the effect if it was playing.
  93. 3.4 Controlling the playback of effects
  94. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. Control of playing is done with write(). Below is an example:
  96. #include <linux/input.h>
  97. #include <unistd.h>
  98. struct input_event play;
  99. struct input_event stop;
  100. struct ff_effect effect;
  101. int fd;
  102. ...
  103. fd = open("/dev/input/eventXX", O_RDWR);
  104. ...
  105. /* Play three times */
  106. play.type = EV_FF;
  107. play.code = effect.id;
  108. play.value = 3;
  109. write(fd, (const void*) &play, sizeof(play));
  110. ...
  111. /* Stop an effect */
  112. stop.type = EV_FF;
  113. stop.code = effect.id;
  114. stop.value = 0;
  115. write(fd, (const void*) &play, sizeof(stop));
  116. 3.5 Setting the gain
  117. ~~~~~~~~~~~~~~~~~~~~
  118. Not all devices have the same strength. Therefore, users should set a gain
  119. factor depending on how strong they want effects to be. This setting is
  120. persistent across access to the driver.
  121. /* Set the gain of the device
  122. int gain; /* between 0 and 100 */
  123. struct input_event ie; /* structure used to communicate with the driver */
  124. ie.type = EV_FF;
  125. ie.code = FF_GAIN;
  126. ie.value = 0xFFFFUL * gain / 100;
  127. if (write(fd, &ie, sizeof(ie)) == -1)
  128. perror("set gain");
  129. 3.6 Enabling/Disabling autocenter
  130. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  131. The autocenter feature quite disturbs the rendering of effects in my opinion,
  132. and I think it should be an effect, which computation depends on the game
  133. type. But you can enable it if you want.
  134. int autocenter; /* between 0 and 100 */
  135. struct input_event ie;
  136. ie.type = EV_FF;
  137. ie.code = FF_AUTOCENTER;
  138. ie.value = 0xFFFFUL * autocenter / 100;
  139. if (write(fd, &ie, sizeof(ie)) == -1)
  140. perror("set auto-center");
  141. A value of 0 means "no auto-center".
  142. 3.7 Dynamic update of an effect
  143. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  144. Proceed as if you wanted to upload a new effect, except that instead of
  145. setting the id field to -1, you set it to the wanted effect id.
  146. Normally, the effect is not stopped and restarted. However, depending on the
  147. type of device, not all parameters can be dynamically updated. For example,
  148. the direction of an effect cannot be updated with iforce devices. In this
  149. case, the driver stops the effect, up-load it, and restart it.
  150. Therefore it is recommended to dynamically change direction while the effect
  151. is playing only when it is ok to restart the effect with a replay count of 1.
  152. 3.8 Information about the status of effects
  153. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154. Every time the status of an effect is changed, an event is sent. The values
  155. and meanings of the fields of the event are as follows:
  156. struct input_event {
  157. /* When the status of the effect changed */
  158. struct timeval time;
  159. /* Set to EV_FF_STATUS */
  160. unsigned short type;
  161. /* Contains the id of the effect */
  162. unsigned short code;
  163. /* Indicates the status */
  164. unsigned int value;
  165. };
  166. FF_STATUS_STOPPED The effect stopped playing
  167. FF_STATUS_PLAYING The effect started to play
  168. NOTE: Status feedback is only supported by iforce driver. If you have
  169. a really good reason to use this, please contact
  170. linux-joystick@atrey.karlin.mff.cuni.cz or anssi.hannula@gmail.com
  171. so that support for it can be added to the rest of the drivers.