Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: trunk/dev/MSDK/MGui/Includes/X11/MJoystickLinux.h
- ===================================================================
- --- trunk/dev/MSDK/MGui/Includes/X11/MJoystickLinux.h (Revision 0)
- +++ trunk/dev/MSDK/MGui/Includes/X11/MJoystickLinux.h (Arbeitskopie)
- @@ -0,0 +1,62 @@
- +/////////////////////////////////////////////////////////////////////////////////////////////////////////
- +// MGui
- +// MJoystickLinux.h
- +/////////////////////////////////////////////////////////////////////////////////////////////////////////
- +
- +//========================================================================
- +// Copyright (c) 2013 Yannick Pflanzer <yp1995@live.de>
- +//
- +// This software is provided 'as-is', without any express or implied
- +// warranty. In no event will the authors be held liable for any damages
- +// arising from the use of this software.
- +//
- +// Permission is granted to anyone to use this software for any purpose,
- +// including commercial applications, and to alter it and redistribute it
- +// freely, subject to the following restrictions:
- +//
- +// 1. The origin of this software must not be misrepresented; you must not
- +// claim that you wrote the original software. If you use this software
- +// in a product, an acknowledgment in the product documentation would
- +// be appreciated but is not required.
- +//
- +// 2. Altered source versions must be plainly marked as such, and must not
- +// be misrepresented as being the original software.
- +//
- +// 3. This notice may not be removed or altered from any source
- +// distribution.
- +//
- +//========================================================================
- +
- +#ifndef MJOYSTICK_LINUX_H
- +#define MJOYSTICK_LINUX_H
- +
- +#include <linux/joystick.h>
- +
- +#define JOY1_DEV "/dev/input/js0"
- +#define JOY2_DEV "/dev/input/js1"
- +
- +#define JOY_AXIS_MIN -32767
- +#define JOY_AXIS_MAX 32767
- +
- +class MJoystickLinux
- +{
- +public:
- + MJoystickLinux(const char* joydev);
- + ~MJoystickLinux();
- + void updateData();
- +
- + int getAxis(int idx);
- + float getDeltaAxis(int idx);
- + bool getButton(int idx);
- +
- +private:
- + int m_file, m_numAxes, m_numButtons;
- + int* m_axis;
- + float* m_dAxis;
- +
- + char* m_button, m_joystickName[80];
- +
- + struct js_event m_js;
- +};
- +
- +#endif
- Index: trunk/dev/MSDK/MGui/Sources/X11/MJoystickLinux.cpp
- ===================================================================
- --- trunk/dev/MSDK/MGui/Sources/X11/MJoystickLinux.cpp (Revision 0)
- +++ trunk/dev/MSDK/MGui/Sources/X11/MJoystickLinux.cpp (Arbeitskopie)
- @@ -0,0 +1,136 @@
- +/////////////////////////////////////////////////////////////////////////////////////////////////////////
- +// MGui
- +// MJoystickLinux.cpp
- +/////////////////////////////////////////////////////////////////////////////////////////////////////////
- +
- +//========================================================================
- +// Copyright (c) 2013 Yannick Pflanzer <yp1995@live.de>
- +//
- +// This software is provided 'as-is', without any express or implied
- +// warranty. In no event will the authors be held liable for any damages
- +// arising from the use of this software.
- +//
- +// Permission is granted to anyone to use this software for any purpose,
- +// including commercial applications, and to alter it and redistribute it
- +// freely, subject to the following restrictions:
- +//
- +// 1. The origin of this software must not be misrepresented; you must not
- +// claim that you wrote the original software. If you use this software
- +// in a product, an acknowledgment in the product documentation would
- +// be appreciated but is not required.
- +//
- +// 2. Altered source versions must be plainly marked as such, and must not
- +// be misrepresented as being the original software.
- +//
- +// 3. This notice may not be removed or altered from any source
- +// distribution.
- +//
- +//========================================================================
- +
- +#include "../../Includes/X11/MJoystickLinux.h"
- +
- +#include <cstdio>
- +#include <cstdlib>
- +#include <fcntl.h>
- +#include <unistd.h>
- +#include <sys/ioctl.h>
- +
- +MJoystickLinux::MJoystickLinux(const char* joydev)
- +{
- + if(joydev == NULL)
- + {
- + return;
- + }
- +
- + m_numAxes = 0;
- + m_numButtons = 0;
- + m_axis = NULL;
- + m_button = NULL;
- + m_dAxis = NULL;
- +
- + if((m_file = open(joydev, O_RDONLY)) == -1)
- + {
- + return;
- + }
- +
- + ioctl(m_file, JSIOCGAXES, &m_numAxes);
- + ioctl(m_file, JSIOCGBUTTONS, &m_numButtons);
- + ioctl(m_file, JSIOCGNAME(80), &m_joystickName);
- +
- + m_axis = (int*) calloc(m_numAxes, sizeof(int));
- + m_dAxis = (float*) calloc(m_numAxes, sizeof(float));
- + m_button = (char*) calloc(m_numButtons, sizeof(char));
- +
- + fcntl(m_file, F_SETFL, O_NONBLOCK);
- +}
- +
- +MJoystickLinux::~MJoystickLinux()
- +{
- +
- +}
- +
- +int MJoystickLinux::getAxis(int idx)
- +{
- + if(idx < m_numAxes)
- + {
- + return m_axis[idx];
- + }
- +
- + return 0;
- +}
- +
- +float MJoystickLinux::getDeltaAxis(int idx)
- +{
- + if(idx < m_numAxes)
- + {
- + return m_dAxis[idx];
- + }
- +
- + return 0;
- +}
- +
- +bool MJoystickLinux::getButton(int idx)
- +{
- + if(idx < m_numButtons)
- + {
- + return (bool) m_button[idx];
- + }
- +
- + return false;
- +}
- +
- +void MJoystickLinux::updateData()
- +{
- + if(m_file == -1)
- + return;
- +
- + read(m_file, &m_js, sizeof(struct js_event));
- +
- + switch(m_js.type & ~JS_EVENT_INIT)
- + {
- + case JS_EVENT_AXIS:
- + //m_dAxis[m_js.number] = m_axis[m_js.number];
- + m_axis[m_js.number] = m_js.value;
- + m_dAxis[m_js.number] = m_js.value;
- + m_dAxis[m_js.number] /= (float) JOY_AXIS_MAX;
- + break;
- + case JS_EVENT_BUTTON:
- + m_button[m_js.number] = m_js.value;
- + break;
- + }
- +
- +
- +}
- +
- +int main(int argc, char* argv[])
- +{
- + MJoystickLinux joystick(JOY1_DEV);
- +
- + while(1)
- + {
- + joystick.updateData();
- +
- + printf("Axis x = %f\n", joystick.getDeltaAxis(0));
- + fflush(stdout);
- + }
- +}
- \ Kein Zeilenumbruch am Ende der Datei
- Index: trunk/dev/MSDK/MGui/Sources/X11/MX11Window.cpp
- ===================================================================
- --- trunk/dev/MSDK/MGui/Sources/X11/MX11Window.cpp (Revision 198)
- +++ trunk/dev/MSDK/MGui/Sources/X11/MX11Window.cpp (Arbeitskopie)
- @@ -7,6 +7,7 @@
- // Copyright (c) 2011 teZeriusz <lukasz.wychrystenko@gmail.com>
- // Copyright (c) 2011 Anael Seghezzi <www.maratis3d.com>
- // Copyright (c) some parts using GLFW X11 code : http://www.glfw.org/
- +// Copyright (c) 2013 Yannick Pflanzer <yp1995@live.de>
- //
- // This software is provided 'as-is', without any express or implied
- // warranty. In no event will the authors be held liable for any damages
- @@ -44,7 +45,14 @@
- #include <X11/extensions/xf86vmode.h>
- #endif
- +#ifdef linux
- + #include "../../Includes/X11/MJoystickLinux.h"
- + // Joystick class
- + MJoystickLinux joystick1(JOY1_DEV);
- + MJoystickLinux joystick2(JOY2_DEV);
- +#endif
- +
- // start time
- static struct timeval startTime;
- @@ -203,6 +211,40 @@
- }
- }
- +#ifdef linux
- +static void updateJoystick(MJoystick* joystick, MJoystickLinux* joystickLinux,
- + MWindow* window)
- +{
- + joystickLinux->updateData();
- +
- + for(int i=0; i<8; i++)
- + {
- + bool pressed = joystick->isKeyPressed(i);
- +
- + if(joystickLinux->getButton(i))
- + {
- + joystick->downKey(i);
- + }
- + else
- + {
- + joystick->upKey(i);
- + }
- + }
- +
- + joystick->setX(joystickLinux->getDeltaAxis(0));
- + joystick->setY(joystickLinux->getDeltaAxis(1));
- + joystick->setZ(joystickLinux->getDeltaAxis(2));
- + joystick->setR(joystickLinux->getDeltaAxis(3));
- + joystick->setU(joystickLinux->getDeltaAxis(4));
- + joystick->setV(joystickLinux->getDeltaAxis(5));
- +
- + MWinEvent events;
- + events.type = MWIN_EVENT_JOYSTICK1_UPDATE;
- + window->sendEvents(&events);
- +
- + joystick->flush();
- +}
- +#endif
- MWindow::MWindow(void):
- m_focus(true),
- @@ -529,6 +571,16 @@
- }
- }
- +#ifdef linux
- + MWindow* window = MWindow::getInstance();
- +
- + MJoystick* joystick = window->getJoystick1();
- + updateJoystick(joystick, &joystick1, window);
- +
- + joystick = window->getJoystick2();
- + updateJoystick(joystick, &joystick2, window);
- +#endif
- +
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement