Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. Index: trunk/dev/MSDK/MGui/Includes/X11/MJoystickLinux.h
  2. ===================================================================
  3. --- trunk/dev/MSDK/MGui/Includes/X11/MJoystickLinux.h (Revision 0)
  4. +++ trunk/dev/MSDK/MGui/Includes/X11/MJoystickLinux.h (Arbeitskopie)
  5. @@ -0,0 +1,62 @@
  6. +/////////////////////////////////////////////////////////////////////////////////////////////////////////
  7. +// MGui
  8. +// MJoystickLinux.h
  9. +/////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. +
  11. +//========================================================================
  12. +// Copyright (c) 2013 Yannick Pflanzer <yp1995@live.de>
  13. +//
  14. +// This software is provided 'as-is', without any express or implied
  15. +// warranty. In no event will the authors be held liable for any damages
  16. +// arising from the use of this software.
  17. +//
  18. +// Permission is granted to anyone to use this software for any purpose,
  19. +// including commercial applications, and to alter it and redistribute it
  20. +// freely, subject to the following restrictions:
  21. +//
  22. +// 1. The origin of this software must not be misrepresented; you must not
  23. +// claim that you wrote the original software. If you use this software
  24. +// in a product, an acknowledgment in the product documentation would
  25. +// be appreciated but is not required.
  26. +//
  27. +// 2. Altered source versions must be plainly marked as such, and must not
  28. +// be misrepresented as being the original software.
  29. +//
  30. +// 3. This notice may not be removed or altered from any source
  31. +// distribution.
  32. +//
  33. +//========================================================================
  34. +
  35. +#ifndef MJOYSTICK_LINUX_H
  36. +#define MJOYSTICK_LINUX_H
  37. +
  38. +#include <linux/joystick.h>
  39. +
  40. +#define JOY1_DEV "/dev/input/js0"
  41. +#define JOY2_DEV "/dev/input/js1"
  42. +
  43. +#define JOY_AXIS_MIN -32767
  44. +#define JOY_AXIS_MAX 32767
  45. +
  46. +class MJoystickLinux
  47. +{
  48. +public:
  49. + MJoystickLinux(const char* joydev);
  50. + ~MJoystickLinux();
  51. + void updateData();
  52. +
  53. + int getAxis(int idx);
  54. + float getDeltaAxis(int idx);
  55. + bool getButton(int idx);
  56. +
  57. +private:
  58. + int m_file, m_numAxes, m_numButtons;
  59. + int* m_axis;
  60. + float* m_dAxis;
  61. +
  62. + char* m_button, m_joystickName[80];
  63. +
  64. + struct js_event m_js;
  65. +};
  66. +
  67. +#endif
  68. Index: trunk/dev/MSDK/MGui/Sources/X11/MJoystickLinux.cpp
  69. ===================================================================
  70. --- trunk/dev/MSDK/MGui/Sources/X11/MJoystickLinux.cpp (Revision 0)
  71. +++ trunk/dev/MSDK/MGui/Sources/X11/MJoystickLinux.cpp (Arbeitskopie)
  72. @@ -0,0 +1,136 @@
  73. +/////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. +// MGui
  75. +// MJoystickLinux.cpp
  76. +/////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. +
  78. +//========================================================================
  79. +// Copyright (c) 2013 Yannick Pflanzer <yp1995@live.de>
  80. +//
  81. +// This software is provided 'as-is', without any express or implied
  82. +// warranty. In no event will the authors be held liable for any damages
  83. +// arising from the use of this software.
  84. +//
  85. +// Permission is granted to anyone to use this software for any purpose,
  86. +// including commercial applications, and to alter it and redistribute it
  87. +// freely, subject to the following restrictions:
  88. +//
  89. +// 1. The origin of this software must not be misrepresented; you must not
  90. +// claim that you wrote the original software. If you use this software
  91. +// in a product, an acknowledgment in the product documentation would
  92. +// be appreciated but is not required.
  93. +//
  94. +// 2. Altered source versions must be plainly marked as such, and must not
  95. +// be misrepresented as being the original software.
  96. +//
  97. +// 3. This notice may not be removed or altered from any source
  98. +// distribution.
  99. +//
  100. +//========================================================================
  101. +
  102. +#include "../../Includes/X11/MJoystickLinux.h"
  103. +
  104. +#include <cstdio>
  105. +#include <cstdlib>
  106. +#include <fcntl.h>
  107. +#include <unistd.h>
  108. +#include <sys/ioctl.h>
  109. +
  110. +MJoystickLinux::MJoystickLinux(const char* joydev)
  111. +{
  112. + if(joydev == NULL)
  113. + {
  114. + return;
  115. + }
  116. +
  117. + m_numAxes = 0;
  118. + m_numButtons = 0;
  119. + m_axis = NULL;
  120. + m_button = NULL;
  121. + m_dAxis = NULL;
  122. +
  123. + if((m_file = open(joydev, O_RDONLY)) == -1)
  124. + {
  125. + return;
  126. + }
  127. +
  128. + ioctl(m_file, JSIOCGAXES, &m_numAxes);
  129. + ioctl(m_file, JSIOCGBUTTONS, &m_numButtons);
  130. + ioctl(m_file, JSIOCGNAME(80), &m_joystickName);
  131. +
  132. + m_axis = (int*) calloc(m_numAxes, sizeof(int));
  133. + m_dAxis = (float*) calloc(m_numAxes, sizeof(float));
  134. + m_button = (char*) calloc(m_numButtons, sizeof(char));
  135. +
  136. + fcntl(m_file, F_SETFL, O_NONBLOCK);
  137. +}
  138. +
  139. +MJoystickLinux::~MJoystickLinux()
  140. +{
  141. +
  142. +}
  143. +
  144. +int MJoystickLinux::getAxis(int idx)
  145. +{
  146. + if(idx < m_numAxes)
  147. + {
  148. + return m_axis[idx];
  149. + }
  150. +
  151. + return 0;
  152. +}
  153. +
  154. +float MJoystickLinux::getDeltaAxis(int idx)
  155. +{
  156. + if(idx < m_numAxes)
  157. + {
  158. + return m_dAxis[idx];
  159. + }
  160. +
  161. + return 0;
  162. +}
  163. +
  164. +bool MJoystickLinux::getButton(int idx)
  165. +{
  166. + if(idx < m_numButtons)
  167. + {
  168. + return (bool) m_button[idx];
  169. + }
  170. +
  171. + return false;
  172. +}
  173. +
  174. +void MJoystickLinux::updateData()
  175. +{
  176. + if(m_file == -1)
  177. + return;
  178. +
  179. + read(m_file, &m_js, sizeof(struct js_event));
  180. +
  181. + switch(m_js.type & ~JS_EVENT_INIT)
  182. + {
  183. + case JS_EVENT_AXIS:
  184. + //m_dAxis[m_js.number] = m_axis[m_js.number];
  185. + m_axis[m_js.number] = m_js.value;
  186. + m_dAxis[m_js.number] = m_js.value;
  187. + m_dAxis[m_js.number] /= (float) JOY_AXIS_MAX;
  188. + break;
  189. + case JS_EVENT_BUTTON:
  190. + m_button[m_js.number] = m_js.value;
  191. + break;
  192. + }
  193. +
  194. +
  195. +}
  196. +
  197. +int main(int argc, char* argv[])
  198. +{
  199. + MJoystickLinux joystick(JOY1_DEV);
  200. +
  201. + while(1)
  202. + {
  203. + joystick.updateData();
  204. +
  205. + printf("Axis x = %f\n", joystick.getDeltaAxis(0));
  206. + fflush(stdout);
  207. + }
  208. +}
  209. \ Kein Zeilenumbruch am Ende der Datei
  210. Index: trunk/dev/MSDK/MGui/Sources/X11/MX11Window.cpp
  211. ===================================================================
  212. --- trunk/dev/MSDK/MGui/Sources/X11/MX11Window.cpp (Revision 198)
  213. +++ trunk/dev/MSDK/MGui/Sources/X11/MX11Window.cpp (Arbeitskopie)
  214. @@ -7,6 +7,7 @@
  215. // Copyright (c) 2011 teZeriusz <lukasz.wychrystenko@gmail.com>
  216. // Copyright (c) 2011 Anael Seghezzi <www.maratis3d.com>
  217. // Copyright (c) some parts using GLFW X11 code : http://www.glfw.org/
  218. +// Copyright (c) 2013 Yannick Pflanzer <yp1995@live.de>
  219. //
  220. // This software is provided 'as-is', without any express or implied
  221. // warranty. In no event will the authors be held liable for any damages
  222. @@ -44,7 +45,14 @@
  223. #include <X11/extensions/xf86vmode.h>
  224. #endif
  225.  
  226. +#ifdef linux
  227. + #include "../../Includes/X11/MJoystickLinux.h"
  228.  
  229. + // Joystick class
  230. + MJoystickLinux joystick1(JOY1_DEV);
  231. + MJoystickLinux joystick2(JOY2_DEV);
  232. +#endif
  233. +
  234. // start time
  235. static struct timeval startTime;
  236.  
  237. @@ -203,6 +211,40 @@
  238. }
  239. }
  240.  
  241. +#ifdef linux
  242. +static void updateJoystick(MJoystick* joystick, MJoystickLinux* joystickLinux,
  243. + MWindow* window)
  244. +{
  245. + joystickLinux->updateData();
  246. +
  247. + for(int i=0; i<8; i++)
  248. + {
  249. + bool pressed = joystick->isKeyPressed(i);
  250. +
  251. + if(joystickLinux->getButton(i))
  252. + {
  253. + joystick->downKey(i);
  254. + }
  255. + else
  256. + {
  257. + joystick->upKey(i);
  258. + }
  259. + }
  260. +
  261. + joystick->setX(joystickLinux->getDeltaAxis(0));
  262. + joystick->setY(joystickLinux->getDeltaAxis(1));
  263. + joystick->setZ(joystickLinux->getDeltaAxis(2));
  264. + joystick->setR(joystickLinux->getDeltaAxis(3));
  265. + joystick->setU(joystickLinux->getDeltaAxis(4));
  266. + joystick->setV(joystickLinux->getDeltaAxis(5));
  267. +
  268. + MWinEvent events;
  269. + events.type = MWIN_EVENT_JOYSTICK1_UPDATE;
  270. + window->sendEvents(&events);
  271. +
  272. + joystick->flush();
  273. +}
  274. +#endif
  275.  
  276. MWindow::MWindow(void):
  277. m_focus(true),
  278. @@ -529,6 +571,16 @@
  279. }
  280. }
  281.  
  282. +#ifdef linux
  283. + MWindow* window = MWindow::getInstance();
  284. +
  285. + MJoystick* joystick = window->getJoystick1();
  286. + updateJoystick(joystick, &joystick1, window);
  287. +
  288. + joystick = window->getJoystick2();
  289. + updateJoystick(joystick, &joystick2, window);
  290. +#endif
  291. +
  292. return true;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement