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 "OSGLinux2AxisEventInterface.h"
48 #include "OSGThreadManager.h"
49 #include "OSGLinuxEventOptions.h"
54 #include <sys/ioctl.h>
55 #include <linux/input.h>
60 /***************************************************************************\
62 \***************************************************************************/
64 MPThreadType
Linux2AxisEventInterface::_type(
65 "Linux2AxisEventInterface",
67 static_cast<CreateThreadF
>(Linux2AxisEventInterface::create
),
70 /***************************************************************************\
72 \***************************************************************************/
74 /***************************************************************************\
76 \***************************************************************************/
78 /*-------------------------------------------------------------------------*\
80 \*-------------------------------------------------------------------------*/
82 Linux2AxisEventInterface
*Linux2AxisEventInterface::find(Char8
*szName
)
84 BaseThread
*pThread
= ThreadManager::the()->findThread(szName
);
86 return dynamic_cast<Linux2AxisEventInterface
*>(pThread
);
89 Linux2AxisEventInterface::ObjTransitPtr
90 Linux2AxisEventInterface::get(Char8
*szName
, bool bGlobal
)
92 BaseThreadTransitPtr pThread
= ThreadManager::the()->getThread(
95 "Linux2AxisEventInterface");
97 return dynamic_pointer_cast
<Linux2AxisEventInterface
>(pThread
);
101 BaseThread
*Linux2AxisEventInterface::create(const Char8
*szName
,
105 return new Linux2AxisEventInterface(szName
, uiId
, bGlobal
);
108 Linux2AxisEventInterface::Linux2AxisEventInterface(const Char8
*szName
,
124 Linux2AxisEventInterface::~Linux2AxisEventInterface(void)
128 bool Linux2AxisEventInterface::start(void)
130 bool returnValue
= Inherited::start();
134 if(_szPort
.empty() == true)
137 _iFileDesc
= open(_szPort
.c_str(), O_RDONLY
); // | O_NONBLOCK);
149 void Linux2AxisEventInterface::shutdown(void)
157 void Linux2AxisEventInterface::workProc(void)
159 while(_bRunning
== true)
164 osgSleep(_uiNapTime
);
168 #ifdef OSG_DEBUG_OLD_C_CASTS
170 // For my debugging, should not be active for any other case (GV)
173 # define __FDMASK(d) (__fd_mask(1) << ((d) % __NFDBITS))
177 # define __FD_MASK(d) (__fd_mask(1) << ((d) % __NFDBITS))
181 # define __NFDBITS (8 * int(sizeof (__fd_mask)))
184 #if defined(__FD_SET) && defined(__FD_ELT) && defined(__FD_MASK)
186 # define __FD_SET(d, set) \
187 (void ((__FDS_BITS (set)[__FD_ELT (d)] |= __FD_MASK (d))))
189 #if defined(__FD_CLR) && defined(__FD_ELT) && defined(__FD_MASK)
191 # define __FD_CLR(d, set) \
192 (void ((__FDS_BITS (set)[__FD_ELT (d)] &= ~__FD_MASK (d))))
197 void Linux2AxisEventInterface::getRawData(void)
199 Int32 iBytesRead
= 0;
208 FD_SET (_iFileDesc
, &_rFds
);
213 iSelRet
= select(_iFileDesc
+ 1, &_rFds
, NULL
, NULL
, &tv
);
215 if(iSelRet
> 0 && FD_ISSET(_iFileDesc
, &_rFds
) == true)
217 iBytesRead
= read(_iFileDesc
, ev
, sizeof(input_event
) * 64);
223 _bHasNewData
= false;
226 Int32 iButtonState
= -1;
231 bool bSetButton
= false;
233 bool bGotData
= false;
236 i
< UInt32(iBytesRead
/ sizeof(input_event
));
239 if(ev
[i
].type
== EV_KEY
)
244 iButton
= MouseData::LeftButton
;
247 iButton
= MouseData::RightButton
;
250 iButton
= MouseData::MiddleButton
;
256 iButtonState
= 1 - ev
[i
].value
;
260 else if(ev
[i
].type
== EV_REL
)
277 else if(ev
[i
].type
== EV_SYN
)
290 if(bSetButton
== true)
292 tmpElem
.setData(iButton
,
294 MouseData::NoModifier
,
298 MouseData::RelValues
);
302 tmpElem
.updateData(rX
,
305 MouseData::RelValues
);
308 _cbMouseData
.push_back(tmpElem
);
319 void Linux2AxisEventInterface::setOptions(InterfaceOptions
*pOptions
)
324 Inherited::setOptions(pOptions
);
326 LinuxEventOptions
*pOpts
=
327 dynamic_cast<LinuxEventOptions
*>(pOptions
);
331 _szPort
= pOpts
->getDevice();
333 _rTxRange
= pOpts
->getTRange()[0];
334 _rTyRange
= pOpts
->getTRange()[1];
336 _cbMouseData
.set_capacity(pOpts
->getBufferSize());