1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
47 #include "OSGLinux3AxisEventInterface.h"
48 #include "OSGThreadManager.h"
49 #include "OSGLinuxEventOptions.h"
50 #include "OSGConceptPropertyChecks.h"
55 #include <sys/ioctl.h>
56 #include <linux/input.h>
62 /***************************************************************************\
64 \***************************************************************************/
66 MPThreadType
Linux3AxisEventInterface::_type(
67 "Linux3AxisEventInterface",
69 static_cast<CreateThreadF
>(Linux3AxisEventInterface::create
),
72 /***************************************************************************\
74 \***************************************************************************/
76 /***************************************************************************\
78 \***************************************************************************/
80 /*-------------------------------------------------------------------------*\
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(
97 "Linux3AxisEventInterface");
99 return dynamic_pointer_cast
<Linux3AxisEventInterface
>(pThread
);
103 BaseThread
*Linux3AxisEventInterface::create(const Char8
*szName
,
107 return new Linux3AxisEventInterface(szName
, uiId
, bGlobal
);
110 Linux3AxisEventInterface::Linux3AxisEventInterface(const Char8
*szName
,
130 Linux3AxisEventInterface::~Linux3AxisEventInterface(void)
134 bool Linux3AxisEventInterface::start(void)
136 bool returnValue
= Inherited::start();
140 if(_szPort
.empty() == true)
143 _iFileDesc
= open(_szPort
.c_str(), O_RDONLY
); // | O_NONBLOCK);
147 FWARNING(("Could not open device at %s\n", _szPort
.c_str()));
155 ioctl(_iFileDesc
, EVIOCGRAB
, &iGrab
);
157 osgSinkUnusedWarning(iGrab
);
165 void Linux3AxisEventInterface::shutdown(void)
173 void Linux3AxisEventInterface::workProc(void)
175 while(_bRunning
== true)
180 osgSleep(_uiNapTime
);
184 #ifdef OSG_DEBUG_OLD_C_CASTS
186 // For my debugging, should not be active for any other case (GV)
189 # define __FDMASK(d) (__fd_mask(1) << ((d) % __NFDBITS))
193 # define __FD_MASK(d) (__fd_mask(1) << ((d) % __NFDBITS))
197 # define __NFDBITS (8 * int(sizeof (__fd_mask)))
199 #if defined(__FD_SET) && defined(__FD_ELT) && defined(__FD_MASK)
201 # define __FD_SET(d, set) \
202 (void ((__FDS_BITS (set)[__FD_ELT (d)] |= __FD_MASK (d))))
204 #if defined(__FD_CLR) && defined(__FD_ELT) && defined(__FD_MASK)
206 # define __FD_CLR(d, set) \
207 (void ((__FDS_BITS (set)[__FD_ELT (d)] &= ~__FD_MASK (d))))
212 void Linux3AxisEventInterface::getRawData(void)
214 Int32 iBytesRead
= 0;
224 FD_SET (_iFileDesc
, &_rFds
);
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);
239 _bHasNewData
= false;
242 i
< UInt32(iBytesRead
/ sizeof(input_event
));
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
);
283 void Linux3AxisEventInterface::setOptions(InterfaceOptions
*pOptions
)
288 Inherited::setOptions(pOptions
);
290 LinuxEventOptions
*pOpts
=
291 dynamic_cast<LinuxEventOptions
*>(pOptions
);
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];