fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / ComplexSceneManager / Sensor / InterfaceSensors / Devices / OSGLinux2AxisEventInterface.cpp
blobbcee4e1c5e8b9c64586ffb370d031a050e99ac1f
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 "OSGLinux2AxisEventInterface.h"
48 #include "OSGThreadManager.h"
49 #include "OSGLinuxEventOptions.h"
51 #if defined(__linux)
52 #include <fcntl.h>
53 #include <termios.h>
54 #include <sys/ioctl.h>
55 #include <linux/input.h>
56 #endif
58 OSG_BEGIN_NAMESPACE
60 /***************************************************************************\
61 * Class variables *
62 \***************************************************************************/
64 MPThreadType Linux2AxisEventInterface::_type(
65 "Linux2AxisEventInterface",
66 "OSGThread",
67 static_cast<CreateThreadF>(Linux2AxisEventInterface::create),
68 NULL);
70 /***************************************************************************\
71 * Class methods *
72 \***************************************************************************/
74 /***************************************************************************\
75 * Instance methods *
76 \***************************************************************************/
78 /*-------------------------------------------------------------------------*\
79 - private -
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(
93 szName,
94 bGlobal,
95 "Linux2AxisEventInterface");
97 return dynamic_pointer_cast<Linux2AxisEventInterface>(pThread);
101 BaseThread *Linux2AxisEventInterface::create(const Char8 *szName,
102 UInt32 uiId,
103 bool bGlobal)
105 return new Linux2AxisEventInterface(szName, uiId, bGlobal);
108 Linux2AxisEventInterface::Linux2AxisEventInterface(const Char8 *szName,
109 UInt32 uiId,
110 bool bGlobal) :
111 Inherited (szName,
112 uiId,
113 bGlobal),
114 _rTxRange (1.f ),
115 _rTyRange (1.f ),
116 _iFileDesc (-1 ),
117 #ifdef __linux
118 _rFds ( ),
119 #endif
120 _szPort ( )
124 Linux2AxisEventInterface::~Linux2AxisEventInterface(void)
128 bool Linux2AxisEventInterface::start(void)
130 bool returnValue = Inherited::start();
132 #if defined(__linux)
134 if(_szPort.empty() == true)
135 return false;
137 _iFileDesc = open(_szPort.c_str(), O_RDONLY); // | O_NONBLOCK);
139 if(_iFileDesc == -1)
141 returnValue = false;
144 #endif
146 return returnValue;
149 void Linux2AxisEventInterface::shutdown(void)
151 #if defined(__linux)
152 if(_iFileDesc != -1)
153 ::close(_iFileDesc);
154 #endif
157 void Linux2AxisEventInterface::workProc(void)
159 while(_bRunning == true)
161 getRawData();
163 if(_uiNapTime > 0)
164 osgSleep(_uiNapTime);
168 #ifdef OSG_DEBUG_OLD_C_CASTS
170 // For my debugging, should not be active for any other case (GV)
171 #ifdef __FDMASK
172 # undef __FDMASK
173 # define __FDMASK(d) (__fd_mask(1) << ((d) % __NFDBITS))
174 #endif
175 #ifdef __FD_MASK
176 # undef __FD_MASK
177 # define __FD_MASK(d) (__fd_mask(1) << ((d) % __NFDBITS))
178 #endif
179 #ifdef __NFDBITS
180 # undef __NFDBITS
181 # define __NFDBITS (8 * int(sizeof (__fd_mask)))
182 #endif
184 #if defined(__FD_SET) && defined(__FD_ELT) && defined(__FD_MASK)
185 # undef __FD_SET
186 # define __FD_SET(d, set) \
187 (void ((__FDS_BITS (set)[__FD_ELT (d)] |= __FD_MASK (d))))
188 #endif
189 #if defined(__FD_CLR) && defined(__FD_ELT) && defined(__FD_MASK)
190 # undef __FD_CLR
191 # define __FD_CLR(d, set) \
192 (void ((__FDS_BITS (set)[__FD_ELT (d)] &= ~__FD_MASK (d))))
193 #endif
195 #endif
197 void Linux2AxisEventInterface::getRawData(void)
199 Int32 iBytesRead = 0;
201 #if defined(__linux)
202 input_event ev[64];
204 struct timeval tv;
205 Int32 iSelRet = 0;
207 FD_ZERO(&_rFds);
208 FD_SET (_iFileDesc, &_rFds);
210 tv.tv_sec = 0;
211 tv.tv_usec = 500000;
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);
219 if(iBytesRead > 0)
221 Inherited::lock();
223 _bHasNewData = false;
225 Int32 iButton = -1;
226 Int32 iButtonState = -1;
228 Real32 rX = 0.0f;
229 Real32 rY = 0.0f;
231 bool bSetButton = false;
233 bool bGotData = false;
235 for (UInt32 i = 0;
236 i < UInt32(iBytesRead / sizeof(input_event));
237 ++i)
239 if(ev[i].type == EV_KEY)
241 switch(ev[i].code)
243 case BTN_LEFT:
244 iButton = MouseData::LeftButton;
245 break;
246 case BTN_RIGHT:
247 iButton = MouseData::RightButton;
248 break;
249 case BTN_MIDDLE:
250 iButton = MouseData::MiddleButton;
251 break;
252 default:
253 break;
256 iButtonState = 1 - ev[i].value;
257 bSetButton = true;
258 bGotData = true;
260 else if(ev[i].type == EV_REL)
262 switch(ev[i].code)
264 case REL_X:
265 rX = ev[i].value;
266 bGotData = true;
267 break;
268 case REL_Y:
269 rY = ev[i].value;
270 bGotData = true;
271 break;
272 default:
273 break;
277 else if(ev[i].type == EV_SYN)
279 break;
283 if(bGotData == true)
285 MouseData tmpElem;
287 rX /= _rTxRange;
288 rY /= _rTyRange;
290 if(bSetButton == true)
292 tmpElem.setData(iButton,
293 iButtonState,
294 MouseData::NoModifier,
295 rX,
297 NULL,
298 MouseData::RelValues);
300 else
302 tmpElem.updateData(rX,
304 NULL,
305 MouseData::RelValues);
308 _cbMouseData.push_back(tmpElem);
310 _bHasNewData = true;
313 Inherited::unlock();
316 #endif
319 void Linux2AxisEventInterface::setOptions(InterfaceOptions *pOptions)
321 if(pOptions == NULL)
322 return;
324 Inherited::setOptions(pOptions);
326 LinuxEventOptions *pOpts =
327 dynamic_cast<LinuxEventOptions *>(pOptions);
329 if(pOpts != NULL)
331 _szPort = pOpts->getDevice();
333 _rTxRange = pOpts->getTRange()[0];
334 _rTyRange = pOpts->getTRange()[1];
336 _cbMouseData.set_capacity(pOpts->getBufferSize());
340 OSG_END_NAMESPACE