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.

229 lines
5.3KB

  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. using namespace std;
  30. LowLevelJoystick* LowLevelJoystick::m_Singleton = NULL;
  31. int JoystickPlugin::m_RefCount=0;
  32. int JoystickPlugin::m_NoExecuted=0;
  33. extern "C" {
  34. SpiralPlugin* SpiralPlugin_CreateInstance()
  35. {
  36. return new JoystickPlugin;
  37. }
  38. char** SpiralPlugin_GetIcon()
  39. {
  40. return SpiralIcon_xpm;
  41. }
  42. int SpiralPlugin_GetID()
  43. {
  44. return 0x0070;
  45. }
  46. string SpiralPlugin_GetGroupName()
  47. {
  48. return "SpiralSound";
  49. }
  50. }
  51. ///////////////////////////////////////////////////////
  52. JoystickPlugin::JoystickPlugin()
  53. {
  54. m_RefCount++;
  55. m_PluginInfo.Name = "Joystick";
  56. m_PluginInfo.Width = 150;
  57. m_PluginInfo.Height = 60;
  58. m_PluginInfo.NumInputs = 0;
  59. m_PluginInfo.NumOutputs = JP_NUMBER_OF_AXES + JP_NUMBER_OF_BUTTONS;
  60. m_PluginInfo.PortTips.push_back("X-axis CV Out");
  61. m_PluginInfo.PortTips.push_back("Y-axis CV Out");
  62. m_PluginInfo.PortTips.push_back("Z-axis CV Out");
  63. m_PluginInfo.PortTips.push_back("Button 1 trigger Out");
  64. m_PluginInfo.PortTips.push_back("Button 2 trigger Out");
  65. m_PluginInfo.PortTips.push_back("Button 3 trigger Out");
  66. for( int i=0; i<JP_NUMBER_OF_AXES; i++ )
  67. {
  68. m_last_axis[i] = LowLevelJoystick::Get()->GetJoystickAxis( i );
  69. LowLevelJoystick::Get()->m_invert_axis[i] = 0;
  70. }
  71. }
  72. JoystickPlugin::~JoystickPlugin()
  73. {
  74. m_RefCount--;
  75. if (m_RefCount==0)
  76. {
  77. LowLevelJoystick::PackUpAndGoHome();
  78. }
  79. }
  80. PluginInfo &JoystickPlugin::Initialise(const HostInfo *Host)
  81. {
  82. PluginInfo& Info= SpiralPlugin::Initialise(Host);
  83. return Info;
  84. }
  85. SpiralGUIType *JoystickPlugin::CreateGUI()
  86. {
  87. m_GUI = new JoystickPluginGUI(m_PluginInfo.Width,
  88. m_PluginInfo.Height,
  89. this,m_HostInfo);
  90. m_GUI->hide();
  91. return m_GUI;
  92. }
  93. void JoystickPlugin::Execute()
  94. {
  95. // Only Read() once per set of plugins
  96. m_NoExecuted++;
  97. if (m_NoExecuted==m_RefCount)
  98. {
  99. LowLevelJoystick::Get()->Read();
  100. m_NoExecuted=0;
  101. }
  102. float stepsize[JP_NUMBER_OF_AXES];
  103. float newvalue[JP_NUMBER_OF_AXES];
  104. for( int i=0; i<JP_NUMBER_OF_AXES; i++ )
  105. {
  106. newvalue[i] = LowLevelJoystick::Get()->GetJoystickAxis( i );
  107. stepsize[i] = (newvalue[i] - m_last_axis[i]) / m_HostInfo->BUFSIZE;
  108. }
  109. for( int n=0; n<m_HostInfo->BUFSIZE; n++ )
  110. {
  111. for( int i=0; i<JP_NUMBER_OF_AXES; i++ )
  112. SetOutput( i, n, m_last_axis[i] + (n * stepsize[i]) );
  113. for( int i=0; i<JP_NUMBER_OF_BUTTONS; i++ )
  114. SetOutput( i+JP_NUMBER_OF_AXES, n, LowLevelJoystick::Get()->GetJoystickButton( i ) );
  115. }
  116. for( int i=0; i<JP_NUMBER_OF_AXES; i++ )
  117. m_last_axis[i] = newvalue[i];
  118. m_GUI->redraw();
  119. }
  120. //////////////////////////////////////////////////////////////////////
  121. //////////////////////////////////////////////////////////////////////
  122. LowLevelJoystick::LowLevelJoystick() :
  123. m_InputOk(true)
  124. {
  125. OpenRead();
  126. }
  127. //////////////////////////////////////////////////////////////////////
  128. LowLevelJoystick::~LowLevelJoystick()
  129. {
  130. Close();
  131. }
  132. //////////////////////////////////////////////////////////////////////
  133. void LowLevelJoystick::Read()
  134. {
  135. struct js_event js;
  136. if (m_InputOk)
  137. {
  138. int ret = read( m_Joyfd, &js, sizeof(struct js_event) );
  139. if (ret == sizeof(struct js_event))
  140. {
  141. switch ((js.type&3))
  142. {
  143. case JS_EVENT_BUTTON:
  144. m_button[js.number] = (float)js.value;
  145. break;
  146. case JS_EVENT_AXIS:
  147. m_axis[js.number] = ((float)js.value) / 32767;
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. //////////////////////////////////////////////////////////////////////
  154. float LowLevelJoystick::GetJoystickAxis( int i )
  155. {
  156. if( m_invert_axis[i] )
  157. return( -m_axis[i] );
  158. else
  159. return( m_axis[i] );
  160. }
  161. //////////////////////////////////////////////////////////////////////
  162. float LowLevelJoystick::GetJoystickButton( int i )
  163. {
  164. return( m_button[i] );
  165. }
  166. //////////////////////////////////////////////////////////////////////
  167. void LowLevelJoystick::Close()
  168. {
  169. cerr<<"Closing joystick"<<endl;
  170. close(m_Joyfd);
  171. }
  172. //////////////////////////////////////////////////////////////////////
  173. void LowLevelJoystick::OpenRead()
  174. {
  175. int result,val;
  176. cerr<<"Opening joystick"<<endl;
  177. m_Joyfd = open("/dev/js0",O_RDONLY);
  178. if(m_Joyfd<0)
  179. {
  180. fprintf(stderr,"Can't open joystick driver for reading.\n");
  181. m_InputOk=false;
  182. return;
  183. }
  184. fcntl(m_Joyfd, F_SETFL, O_NONBLOCK);
  185. //CHECK_AND_REPORT_ERROR;
  186. }
  187. //////////////////////////////////////////////////////////////////////