File indexing completed on 2024-05-12 16:34:30

0001 /* This file is part of the KDE project
0002  * Copyright (c) 2008 Hans Bakker <hansmbakker@gmail.com>
0003  * Copyright (c) 2008 Jan Hambrecht <jaham@gmx.net>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "SpaceNavigatorPollingThread.h"
0022 
0023 #include "SpaceNavigatorDebug.h"
0024 
0025 #include <KoInputDeviceHandlerEvent.h>
0026 
0027 #include <spnav.h>
0028 
0029 SpaceNavigatorPollingThread::SpaceNavigatorPollingThread( QObject * parent )
0030 : QThread( parent ), m_stopped( false )
0031 {
0032 }
0033 
0034 SpaceNavigatorPollingThread::~SpaceNavigatorPollingThread()
0035 {
0036 }
0037 
0038 void SpaceNavigatorPollingThread::run()
0039 {
0040     m_stopped = false;
0041     if(spnav_open() == -1)
0042         return;
0043 
0044     qreal posfactor = 0.1;
0045     int currX = 0, currY = 0, currZ = 0;
0046     int currRX = 0, currRY = 0, currRZ = 0;
0047     Qt::MouseButtons buttons = Qt::NoButton;
0048 
0049     debugSpaceNavigator << "started spacenavigator polling thread";
0050     while( ! m_stopped )
0051     {
0052         spnav_event event;
0053 
0054         if( spnav_poll_event( &event ) )
0055         {
0056             if( event.type == SPNAV_EVENT_MOTION )
0057             {
0058                 /*
0059                  * The coordinate system of the space navigator is like the following:
0060                  * x-axis : from left to right
0061                  * y-axis : from down to up
0062                  * z-axis : from front to back
0063                  * We probably want to make sure that the x- and y-axis match Qt widget
0064                  * coordinate system:
0065                  * x-axis : from left to right
0066                  * y-axis : from back to front
0067                  * The z-axis would then go from up to down in a right handed coordinate system.
0068                  * z-axis : from up to down
0069                  */
0070                 //debugSpaceNavigator << "got motion event: t("<< event.motion.x << event.motion.y << event.motion.z << ") r(" << event.motion.rx << event.motion.ry << event.motion.rz << ")";
0071                 currX = static_cast<int>( posfactor * event.motion.x );
0072                 currY = -static_cast<int>( posfactor * event.motion.z );
0073                 currZ = -static_cast<int>( posfactor * event.motion.y );
0074                 currRX = static_cast<int>( posfactor * event.motion.rx );
0075                 currRY = static_cast<int>( -posfactor * event.motion.rz );
0076                 currRZ = static_cast<int>( -posfactor * event.motion.ry );
0077                 emit moveEvent( currX, currY, currZ, currRX, currRY, currRZ, buttons );
0078             }
0079             else
0080             {
0081                 /* SPNAV_EVENT_BUTTON */
0082                 Qt::MouseButton button = event.button.bnum == 0 ? Qt::LeftButton : Qt::RightButton;
0083                 KoInputDeviceHandlerEvent::Type type;
0084                 if( event.button.press )
0085                 {
0086                     //debugSpaceNavigator << "got button press event b(" << event.button.bnum << ")";
0087                     buttons |= button;
0088                     type = KoInputDeviceHandlerEvent::ButtonPressed;
0089                 }
0090                 else
0091                 {
0092                     //debugSpaceNavigator << "got button release event b(" << event.button.bnum << ")";
0093                     buttons &= ~button;
0094                     type = KoInputDeviceHandlerEvent::ButtonReleased;
0095                 }
0096                 emit buttonEvent( currX, currY, currZ, currRX, currRY, currRZ, buttons, button, type );
0097             }
0098             spnav_remove_events( event.type );
0099         }
0100         msleep(10);
0101     }
0102 
0103     debugSpaceNavigator << "finished spacenavigator polling thread";
0104 }
0105 
0106 void SpaceNavigatorPollingThread::stop()
0107 {
0108     m_stopped = true;
0109 }