changed: gcc8 base update
[opensg.git] / Source / Contrib / ComplexSceneManager / Sensor / InterfaceSensors / Devices / OSGLinux3AxisEventInterface.cpp
blobc5dac9b59bd7e3a8036ec66fd82568351bec1ccf
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
47 #include "OSGLinux3AxisEventInterface.h"
48 #include "OSGThreadManager.h"
49 #include "OSGLinuxEventOptions.h"
50 #include "OSGConceptPropertyChecks.h"
52 #if defined(__linux)
53 #include <fcntl.h>
54 #include <termios.h>
55 #include <sys/ioctl.h>
56 #include <linux/input.h>
57 #include <sys/time.h>
58 #endif
60 OSG_BEGIN_NAMESPACE
62 /***************************************************************************\
63 * Class variables *
64 \***************************************************************************/
66 MPThreadType Linux3AxisEventInterface::_type(
67 "Linux3AxisEventInterface",
68 "OSGThread",
69 static_cast<CreateThreadF>(Linux3AxisEventInterface::create),
70 NULL);
72 /***************************************************************************\
73 * Class methods *
74 \***************************************************************************/
76 /***************************************************************************\
77 * Instance methods *
78 \***************************************************************************/
80 /*-------------------------------------------------------------------------*\
81 - private -
82 \*-------------------------------------------------------------------------*/
84 Linux3AxisEventInterface *Linux3AxisEventInterface::find(Char8 *szName)
86 BaseThread *pThread = ThreadManager::the()->findThread(szName);
88 return dynamic_cast<Linux3AxisEventInterface *>(pThread);
91 Linux3AxisEventInterface::ObjTransitPtr
92 Linux3AxisEventInterface::get(Char8 *szName, bool bGlobal)
94 BaseThreadTransitPtr pThread = ThreadManager::the()->getThread(
95 szName,
96 bGlobal,
97 "Linux3AxisEventInterface");
99 return dynamic_pointer_cast<Linux3AxisEventInterface>(pThread);
103 BaseThread *Linux3AxisEventInterface::create(const Char8 *szName,
104 UInt32 uiId,
105 bool bGlobal)
107 return new Linux3AxisEventInterface(szName, uiId, bGlobal);
110 Linux3AxisEventInterface::Linux3AxisEventInterface(const Char8 *szName,
111 UInt32 uiId,
112 bool bGlobal) :
113 Inherited(szName,
114 uiId,
115 bGlobal),
116 _rTxRange (450.f ),
117 _rTyRange (450.f ),
118 _rTzRange (450.f ),
119 _rRxRange (450.f ),
120 _rRyRange (450.f ),
121 _rRzRange (450.f ),
122 _iFileDesc(-1 ),
123 #ifdef __linux
124 _rFds ( ),
125 #endif
126 _szPort ( )
130 Linux3AxisEventInterface::~Linux3AxisEventInterface(void)
134 bool Linux3AxisEventInterface::start(void)
136 bool returnValue = Inherited::start();
138 #if defined(__linux)
140 if(_szPort.empty() == true)
141 return false;
143 _iFileDesc = open(_szPort.c_str(), O_RDONLY); // | O_NONBLOCK);
145 if(_iFileDesc == -1)
147 FWARNING(("Could not open device at %s\n", _szPort.c_str()));
149 returnValue = false;
151 else
153 int iGrab = 1;
155 ioctl(_iFileDesc, EVIOCGRAB, &iGrab);
157 osgSinkUnusedWarning(iGrab);
160 #endif
162 return returnValue;
165 void Linux3AxisEventInterface::shutdown(void)
167 #if defined(__linux)
168 if(_iFileDesc != -1)
169 ::close(_iFileDesc);
170 #endif
173 void Linux3AxisEventInterface::workProc(void)
175 while(_bRunning == true)
177 getRawData();
179 if(_uiNapTime > 0)
180 osgSleep(_uiNapTime);
184 #ifdef OSG_DEBUG_OLD_C_CASTS
186 // For my debugging, should not be active for any other case (GV)
187 #ifdef __FDMASK
188 # undef __FDMASK
189 # define __FDMASK(d) (__fd_mask(1) << ((d) % __NFDBITS))
190 #endif
191 #ifdef __FD_MASK
192 # undef __FD_MASK
193 # define __FD_MASK(d) (__fd_mask(1) << ((d) % __NFDBITS))
194 #endif
195 #ifdef __NFDBITS
196 # undef __NFDBITS
197 # define __NFDBITS (8 * int(sizeof (__fd_mask)))
198 #endif
199 #if defined(__FD_SET) && defined(__FD_ELT) && defined(__FD_MASK)
200 # undef __FD_SET
201 # define __FD_SET(d, set) \
202 (void ((__FDS_BITS (set)[__FD_ELT (d)] |= __FD_MASK (d))))
203 #endif
204 #if defined(__FD_CLR) && defined(__FD_ELT) && defined(__FD_MASK)
205 # undef __FD_CLR
206 # define __FD_CLR(d, set) \
207 (void ((__FDS_BITS (set)[__FD_ELT (d)] &= ~__FD_MASK (d))))
208 #endif
210 #endif
212 void Linux3AxisEventInterface::getRawData(void)
214 Int32 iBytesRead = 0;
216 #if defined(__linux)
218 input_event ev[64];
220 struct timeval tv;
221 Int32 iSelRet = 0;
223 FD_ZERO(&_rFds);
224 FD_SET (_iFileDesc, &_rFds);
226 tv.tv_sec = 0;
227 tv.tv_usec = 500000;
229 iSelRet = select(_iFileDesc + 1, &_rFds, NULL, NULL, &tv);
231 if(iSelRet > 0 && FD_ISSET(_iFileDesc, &_rFds) == true)
233 iBytesRead = read(_iFileDesc, ev, sizeof(input_event) * 64);
235 if(iBytesRead > 0)
237 Inherited::lock();
239 _bHasNewData = false;
241 for (UInt32 i = 0;
242 i < UInt32(iBytesRead / sizeof(input_event));
243 ++i)
245 if(ev[i].type == EV_REL)
247 if(ev[i].code == REL_X)
249 _vTranslate[0] = Real32(ev[i].value) / _rTxRange;
251 else if(ev[i].code == REL_Y)
253 _vTranslate[2] = Real32(ev[i].value) / _rTyRange;
255 else if(ev[i].code == REL_Z)
257 _vTranslate[1] = -(Real32(ev[i].value) / _rTzRange);
259 else if(ev[i].code == REL_RX)
261 _qRotate[0] = Real32(ev[i].value) / _rRxRange;
263 else if(ev[i].code == REL_RY)
265 _qRotate[2] = Real32(ev[i].value) / _rRzRange;
267 else if(ev[i].code == REL_RZ)
269 _qRotate[1] = -(Real32(ev[i].value) / _rRyRange);
275 _bHasNewData = true;
277 Inherited::unlock();
280 #endif
283 void Linux3AxisEventInterface::setOptions(InterfaceOptions *pOptions)
285 if(pOptions == NULL)
286 return;
288 Inherited::setOptions(pOptions);
290 LinuxEventOptions *pOpts =
291 dynamic_cast<LinuxEventOptions *>(pOptions);
293 if(pOpts != NULL)
295 _szPort = pOpts->getDevice();
297 _rTxRange = pOpts->getTRange()[0];
298 _rTyRange = pOpts->getTRange()[1];
299 _rTzRange = pOpts->getTRange()[2];
300 _rRxRange = pOpts->getRRange()[0];
301 _rRyRange = pOpts->getRRange()[1];
302 _rRzRange = pOpts->getRRange()[2];
306 OSG_END_NAMESPACE