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.

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