You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
5.2KB

  1. /* JoystickPlugin
  2. * Copyleft (C) 2002 William Bland <wjb@abstractnonsense.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <sys/types.h>
  19. #include <stdio.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <limits.h>
  23. #include <sys/ioctl.h>
  24. #include <limits.h>
  25. #include "JoystickPlugin.h"
  26. #include "JoystickPluginGUI.h"
  27. #include <FL/Fl_Button.h>
  28. #include "SpiralIcon.xpm"
  29. LowLevelJoystick* LowLevelJoystick::m_Singleton = NULL;
  30. int JoystickPlugin::m_RefCount=0;
  31. int JoystickPlugin::m_NoExecuted=0;
  32. extern "C" {
  33. SpiralPlugin* CreateInstance()
  34. {
  35. return new JoystickPlugin;
  36. }
  37. char** GetIcon()
  38. {
  39. return SpiralIcon_xpm;
  40. }
  41. int GetID()
  42. {
  43. return 0x0070;
  44. }
  45. }
  46. ///////////////////////////////////////////////////////
  47. JoystickPlugin::JoystickPlugin()
  48. {
  49. m_RefCount++;
  50. m_PluginInfo.Name = "Joystick";
  51. m_PluginInfo.Width = 150;
  52. m_PluginInfo.Height = 60;
  53. m_PluginInfo.NumInputs = 0;
  54. m_PluginInfo.NumOutputs = JP_NUMBER_OF_AXES + JP_NUMBER_OF_BUTTONS;
  55. m_PluginInfo.PortTips.push_back("X-axis CV Out");
  56. m_PluginInfo.PortTips.push_back("Y-axis CV Out");
  57. m_PluginInfo.PortTips.push_back("Z-axis CV Out");
  58. m_PluginInfo.PortTips.push_back("Button 1 trigger Out");
  59. m_PluginInfo.PortTips.push_back("Button 2 trigger Out");
  60. m_PluginInfo.PortTips.push_back("Button 3 trigger Out");
  61. for( int i=0; i<JP_NUMBER_OF_AXES; i++ )
  62. {
  63. m_last_axis[i] = LowLevelJoystick::Get()->GetJoystickAxis( i );
  64. LowLevelJoystick::Get()->m_invert_axis[i] = 0;
  65. }
  66. }
  67. JoystickPlugin::~JoystickPlugin()
  68. {
  69. m_RefCount--;
  70. if (m_RefCount==0)
  71. {
  72. LowLevelJoystick::PackUpAndGoHome();
  73. }
  74. }
  75. PluginInfo &JoystickPlugin::Initialise(const HostInfo *Host)
  76. {
  77. PluginInfo& Info= SpiralPlugin::Initialise(Host);
  78. return Info;
  79. }
  80. SpiralGUIType *JoystickPlugin::CreateGUI()
  81. {
  82. m_GUI = new JoystickPluginGUI(m_PluginInfo.Width,
  83. m_PluginInfo.Height,
  84. this,m_HostInfo);
  85. m_GUI->hide();
  86. return m_GUI;
  87. }
  88. void JoystickPlugin::Execute()
  89. {
  90. // Only Read() once per set of plugins
  91. m_NoExecuted++;
  92. if (m_NoExecuted==m_RefCount)
  93. {
  94. LowLevelJoystick::Get()->Read();
  95. m_NoExecuted=0;
  96. }
  97. float stepsize[JP_NUMBER_OF_AXES];
  98. float newvalue[JP_NUMBER_OF_AXES];
  99. for( int i=0; i<JP_NUMBER_OF_AXES; i++ )
  100. {
  101. newvalue[i] = LowLevelJoystick::Get()->GetJoystickAxis( i );
  102. stepsize[i] = (newvalue[i] - m_last_axis[i]) / m_HostInfo->BUFSIZE;
  103. }
  104. for( int n=0; n<m_HostInfo->BUFSIZE; n++ )
  105. {
  106. for( int i=0; i<JP_NUMBER_OF_AXES; i++ )
  107. SetOutput( i, n, m_last_axis[i] + (n * stepsize[i]) );
  108. for( int i=0; i<JP_NUMBER_OF_BUTTONS; i++ )
  109. SetOutput( i+JP_NUMBER_OF_AXES, n, LowLevelJoystick::Get()->GetJoystickButton( i ) );
  110. }
  111. for( int i=0; i<JP_NUMBER_OF_AXES; i++ )
  112. m_last_axis[i] = newvalue[i];
  113. m_GUI->redraw();
  114. }
  115. //////////////////////////////////////////////////////////////////////
  116. //////////////////////////////////////////////////////////////////////
  117. LowLevelJoystick::LowLevelJoystick() :
  118. m_InputOk(true)
  119. {
  120. OpenRead();
  121. }
  122. //////////////////////////////////////////////////////////////////////
  123. LowLevelJoystick::~LowLevelJoystick()
  124. {
  125. Close();
  126. }
  127. //////////////////////////////////////////////////////////////////////
  128. void LowLevelJoystick::Read()
  129. {
  130. struct js_event js;
  131. if (m_InputOk)
  132. {
  133. int ret = read( m_Joyfd, &js, sizeof(struct js_event) );
  134. if (ret == sizeof(struct js_event))
  135. {
  136. switch ((js.type&3))
  137. {
  138. case JS_EVENT_BUTTON:
  139. m_button[js.number] = (float)js.value;
  140. break;
  141. case JS_EVENT_AXIS:
  142. m_axis[js.number] = ((float)js.value) / 32767;
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. //////////////////////////////////////////////////////////////////////
  149. float LowLevelJoystick::GetJoystickAxis( int i )
  150. {
  151. if( m_invert_axis[i] )
  152. return( -m_axis[i] );
  153. else
  154. return( m_axis[i] );
  155. }
  156. //////////////////////////////////////////////////////////////////////
  157. float LowLevelJoystick::GetJoystickButton( int i )
  158. {
  159. return( m_button[i] );
  160. }
  161. //////////////////////////////////////////////////////////////////////
  162. void LowLevelJoystick::Close()
  163. {
  164. cerr<<"Closing joystick"<<endl;
  165. close(m_Joyfd);
  166. }
  167. //////////////////////////////////////////////////////////////////////
  168. void LowLevelJoystick::OpenRead()
  169. {
  170. int result,val;
  171. cerr<<"Opening joystick"<<endl;
  172. m_Joyfd = open("/dev/js0",O_RDONLY);
  173. if(m_Joyfd<0)
  174. {
  175. fprintf(stderr,"Can't open joystick driver for reading.\n");
  176. m_InputOk=false;
  177. return;
  178. }
  179. fcntl(m_Joyfd, F_SETFL, O_NONBLOCK);
  180. //CHECK_AND_REPORT_ERROR;
  181. }
  182. //////////////////////////////////////////////////////////////////////