| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990,91,92 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.2 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoXtSpaceball |
| 47 |
| |
| 48 |
| Author(s): David Mott |
| 49 |
| |
| 50 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 |
_______________________________________________________________________ |
| 52 |
*/ |
| 53 |
|
| 54 |
#include <Inventor/SbTime.h> |
| 55 |
#include <Inventor/Xt/SoXt.h> |
| 56 |
#include <Inventor/Xt/devices/SoXtSpaceball.h> |
| 57 |
#include <Inventor/errors/SoDebugError.h> |
| 58 |
|
| 59 |
#include <X11/Xlib.h> |
| 60 |
#include <X11/extensions/XI.h> |
| 61 |
|
| 62 |
extern "C" { |
| 63 |
XDeviceInfo *XListInputDevices(Display *, int *); |
| 64 |
XDevice *XOpenDevice(Display *, XID); |
| 65 |
int XSelectExtensionEvent(Display *, Window, XEventClass *, int); |
| 66 |
} |
| 67 |
|
| 68 |
//////////////////////////////////////////////////////////////////////// |
| 69 |
// |
| 70 |
// Description: |
| 71 |
// Constructor which uses the primary display (specified at SoXt::init). |
| 72 |
// |
| 73 |
// public |
| 74 |
// |
| 75 |
SoXtSpaceball::SoXtSpaceball(SoXtSpaceball::Mask whichEvents) |
| 76 |
// |
| 77 |
//////////////////////////////////////////////////////////////////////// |
| 78 |
{ |
| 79 |
constructorCommon(SoXt::getDisplay(), whichEvents); |
| 80 |
} |
| 81 |
|
| 82 |
//////////////////////////////////////////////////////////////////////// |
| 83 |
// |
| 84 |
// Description: |
| 85 |
// Constructor which uses the passed display. |
| 86 |
// |
| 87 |
// public |
| 88 |
// |
| 89 |
SoXtSpaceball::SoXtSpaceball(Display *d, SoXtSpaceball::Mask whichEvents) |
| 90 |
// |
| 91 |
//////////////////////////////////////////////////////////////////////// |
| 92 |
{ |
| 93 |
constructorCommon(d, whichEvents); |
| 94 |
} |
| 95 |
|
| 96 |
//////////////////////////////////////////////////////////////////////// |
| 97 |
// |
| 98 |
// This is where the real constructor work gets done. |
| 99 |
// |
| 100 |
// private |
| 101 |
// |
| 102 |
void |
| 103 |
SoXtSpaceball::constructorCommon( |
| 104 |
Display *display, |
| 105 |
SoXtSpaceball::Mask whichEvents) |
| 106 |
// |
| 107 |
//////////////////////////////////////////////////////////////////////// |
| 108 |
{ |
| 109 |
if (display == NULL) { |
| 110 |
#ifdef DEBUG |
| 111 |
SoDebugError::post("SoXtSpaceball::SoXtSpaceball()", |
| 112 |
"display is NULL.\n"); |
| 113 |
#endif |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
eventMask = whichEvents; |
| 118 |
motionEvent = new SoMotion3Event; |
| 119 |
buttonEvent = new SoSpaceballButtonEvent; |
| 120 |
|
| 121 |
// these are empirically good default values |
| 122 |
rotScale = .006; |
| 123 |
transScale = .006; |
| 124 |
|
| 125 |
// get the list of input devices that are attached to the display now |
| 126 |
XDeviceInfoPtr list; |
| 127 |
int numDevices; |
| 128 |
|
| 129 |
list = (XDeviceInfoPtr) XListInputDevices(display, &numDevices); |
| 130 |
|
| 131 |
// now run through the list looking for the spaceball device |
| 132 |
device = NULL; |
| 133 |
for (int i = 0; i < numDevices; i++) { |
| 134 |
// Open the spaceball device - the device id is set at runtime. |
| 135 |
if (strcmp(list[i].name, "spaceball") == 0) { |
| 136 |
device = XOpenDevice(display, list[i].id); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// make sure we found the spaceball device |
| 141 |
if (device == NULL) { |
| 142 |
#ifdef DEBUG |
| 143 |
SoDebugError::post("SoXtSpaceball::SoXtSpaceball", |
| 144 |
"Sorry there is no Spaceball attached to this display"); |
| 145 |
#endif |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
// query the event types and classes |
| 150 |
uint32_t eventClass; |
| 151 |
numEventClasses = 0; |
| 152 |
|
| 153 |
if (eventMask & SoXtSpaceball::MOTION) { |
| 154 |
DeviceMotionNotify(device, motionEventType, eventClass); |
| 155 |
eventClasses[numEventClasses] = eventClass; |
| 156 |
eventTypes[numEventClasses] = motionEventType; |
| 157 |
numEventClasses++; |
| 158 |
} |
| 159 |
|
| 160 |
if (eventMask & SoXtSpaceball::PRESS) { |
| 161 |
DeviceButtonPress(device, buttonPressEventType, eventClass); |
| 162 |
eventClasses[numEventClasses] = eventClass; |
| 163 |
eventTypes[numEventClasses] = buttonPressEventType; |
| 164 |
numEventClasses++; |
| 165 |
} |
| 166 |
|
| 167 |
if (eventMask & SoXtSpaceball::RELEASE) { |
| 168 |
DeviceButtonRelease(device, buttonReleaseEventType, eventClass); |
| 169 |
eventClasses[numEventClasses] = eventClass; |
| 170 |
eventTypes[numEventClasses] = buttonReleaseEventType; |
| 171 |
numEventClasses++; |
| 172 |
} |
| 173 |
|
| 174 |
#ifdef DEBUG |
| 175 |
if (numEventClasses == 0) { |
| 176 |
SoDebugError::postWarning("SoXtSpaceball::SoXtSpaceball", |
| 177 |
"eventMask is NULL. No spaceball events will be received"); |
| 178 |
} |
| 179 |
#endif |
| 180 |
} |
| 181 |
|
| 182 |
//////////////////////////////////////////////////////////////////////// |
| 183 |
// |
| 184 |
// Description: |
| 185 |
// Constructor. |
| 186 |
// |
| 187 |
// public |
| 188 |
// |
| 189 |
SoXtSpaceball::~SoXtSpaceball() |
| 190 |
// |
| 191 |
//////////////////////////////////////////////////////////////////////// |
| 192 |
{ |
| 193 |
delete motionEvent; |
| 194 |
delete buttonEvent; |
| 195 |
} |
| 196 |
|
| 197 |
//////////////////////////////////////////////////////////////////////// |
| 198 |
// |
| 199 |
// Description: |
| 200 |
// returns whether the spaceball device exists for use or not. |
| 201 |
// |
| 202 |
// static, public |
| 203 |
// |
| 204 |
SbBool |
| 205 |
SoXtSpaceball::exists(Display *display) |
| 206 |
// |
| 207 |
//////////////////////////////////////////////////////////////////////// |
| 208 |
{ |
| 209 |
// get the list of input devices that are attached to the display now |
| 210 |
XDeviceInfoPtr list; |
| 211 |
int numDevices; |
| 212 |
|
| 213 |
if (display == NULL) { |
| 214 |
#ifdef DEBUG |
| 215 |
SoDebugError::post("SoXtSpaceball::exists()", |
| 216 |
"display is NULL.\n"); |
| 217 |
#endif |
| 218 |
return FALSE; |
| 219 |
} |
| 220 |
|
| 221 |
list = (XDeviceInfoPtr) XListInputDevices(display, &numDevices); |
| 222 |
|
| 223 |
// now run through the list looking for the spaceball device |
| 224 |
int i; |
| 225 |
for (i = 0; (i < numDevices) && |
| 226 |
(strcmp(list[i].name, "spaceball") != 0); i++) |
| 227 |
; // shut up and loop |
| 228 |
|
| 229 |
// if we broke out of the loop before i reached numDevices, |
| 230 |
// then the spaceball does in fact exist. |
| 231 |
return (i < numDevices); |
| 232 |
} |
| 233 |
|
| 234 |
//////////////////////////////////////////////////////////////////////// |
| 235 |
// |
| 236 |
// Description: |
| 237 |
// This selects input for spaceball device events which occur in w. |
| 238 |
// The callback routine is proc, and the callback data is clientData. |
| 239 |
// |
| 240 |
// virtual public |
| 241 |
// |
| 242 |
void |
| 243 |
SoXtSpaceball::enable( |
| 244 |
Widget w, |
| 245 |
XtEventHandler proc, |
| 246 |
XtPointer clientData, |
| 247 |
Window window) |
| 248 |
// |
| 249 |
//////////////////////////////////////////////////////////////////////// |
| 250 |
{ |
| 251 |
if (numEventClasses == 0) |
| 252 |
return; |
| 253 |
|
| 254 |
if (w == NULL) { |
| 255 |
#ifdef DEBUG |
| 256 |
SoDebugError::post("SoXtSpaceball::enable", |
| 257 |
"widget is NULL."); |
| 258 |
#endif |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
if (window == (Window) NULL) { |
| 263 |
#ifdef DEBUG |
| 264 |
SoDebugError::post("SoXtSpaceball::enable", |
| 265 |
"widget must be realized (Window is NULL)."); |
| 266 |
#endif |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
Display *display = XtDisplay(w); |
| 271 |
if (display == NULL) { |
| 272 |
#ifdef DEBUG |
| 273 |
SoDebugError::post("SoXtSpaceball::enable()", |
| 274 |
"display is NULL.\n"); |
| 275 |
#endif |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
// select extension events for the spaceball which the user wants |
| 280 |
XSelectExtensionEvent(display, window, eventClasses, numEventClasses); |
| 281 |
|
| 282 |
// tell Inventor about these extension events! |
| 283 |
for (int i = 0; i < numEventClasses; i++) |
| 284 |
SoXt::addExtensionEventHandler(w, eventTypes[i], proc, clientData); |
| 285 |
} |
| 286 |
|
| 287 |
//////////////////////////////////////////////////////////////////////// |
| 288 |
// |
| 289 |
// Description: |
| 290 |
// This unselects input for spaceball device events which occur in w, |
| 291 |
// i.e. spaceball events will no longer be recognized. |
| 292 |
// |
| 293 |
// virtual public |
| 294 |
// |
| 295 |
void |
| 296 |
SoXtSpaceball::disable( |
| 297 |
Widget w, |
| 298 |
XtEventHandler proc, |
| 299 |
XtPointer clientData) |
| 300 |
// |
| 301 |
//////////////////////////////////////////////////////////////////////// |
| 302 |
{ |
| 303 |
// tell Inventor to forget about these classes |
| 304 |
for (int i = 0; i < numEventClasses; i++) |
| 305 |
SoXt::removeExtensionEventHandler(w, eventTypes[i], proc, clientData); |
| 306 |
} |
| 307 |
|
| 308 |
//////////////////////////////////////////////////////////////////////// |
| 309 |
// |
| 310 |
// Description: |
| 311 |
// This returns an SoEvent for the passed X event, if the event |
| 312 |
// was generated by the mouse device. |
| 313 |
// |
| 314 |
// virtual public |
| 315 |
// |
| 316 |
const SoEvent * |
| 317 |
SoXtSpaceball::translateEvent(XAnyEvent *xevent) |
| 318 |
// |
| 319 |
//////////////////////////////////////////////////////////////////////// |
| 320 |
{ |
| 321 |
SoEvent *event = NULL; |
| 322 |
|
| 323 |
// see if this is a spaceball event |
| 324 |
if (xevent->type == motionEventType) { |
| 325 |
XDeviceMotionEvent *me = (XDeviceMotionEvent *) xevent; |
| 326 |
if (me->deviceid == device->device_id) |
| 327 |
event = translateMotionEvent(me); |
| 328 |
} |
| 329 |
else if (xevent->type == buttonPressEventType) { |
| 330 |
XDeviceButtonEvent *be = (XDeviceButtonEvent *) xevent; |
| 331 |
if (be->deviceid == device->device_id) |
| 332 |
event = translateButtonEvent(be, SoButtonEvent::DOWN); |
| 333 |
} |
| 334 |
else if (xevent->type == buttonReleaseEventType) { |
| 335 |
XDeviceButtonEvent *be = (XDeviceButtonEvent *) xevent; |
| 336 |
if (be->deviceid == device->device_id) |
| 337 |
event = translateButtonEvent(be, SoButtonEvent::UP); |
| 338 |
} |
| 339 |
|
| 340 |
return event; |
| 341 |
} |
| 342 |
|
| 343 |
//////////////////////////////////////////////////////////////////////// |
| 344 |
// |
| 345 |
// Description: |
| 346 |
// This returns an SoMotion3Event for the passed X event. |
| 347 |
// |
| 348 |
// private |
| 349 |
// |
| 350 |
SoMotion3Event * |
| 351 |
SoXtSpaceball::translateMotionEvent(XDeviceMotionEvent *me) |
| 352 |
// |
| 353 |
//////////////////////////////////////////////////////////////////////// |
| 354 |
{ |
| 355 |
// spaceball period event? - ignore it |
| 356 |
if (me->first_axis == 6) |
| 357 |
return NULL; |
| 358 |
|
| 359 |
// unknown data? - ignore it |
| 360 |
if ((me->first_axis != 0) || (me->axes_count != 6)) |
| 361 |
return NULL; |
| 362 |
|
| 363 |
// ok, set up a motion event |
| 364 |
setEventPosition(motionEvent, me->x, me->y); |
| 365 |
int32_t secs = me->time / 1000; |
| 366 |
motionEvent->setTime(SbTime(secs, 1000 * (me->time - 1000 * secs))); |
| 367 |
motionEvent->setShiftDown(me->state & ShiftMask); |
| 368 |
motionEvent->setCtrlDown(me->state & ControlMask); |
| 369 |
motionEvent->setAltDown(me->state & Mod1Mask); |
| 370 |
|
| 371 |
// get the translation data in a right handed system (flip z) |
| 372 |
int *sbdata = me->axis_data; |
| 373 |
SbVec3f trans(sbdata[0], sbdata[1], -sbdata[2]); |
| 374 |
trans *= transScale; |
| 375 |
motionEvent->setTranslation(trans); |
| 376 |
|
| 377 |
// get the rotation data in a right handed system (flip z) |
| 378 |
SbVec3f axis((float) sbdata[3], (float) sbdata[4], (float) -sbdata[5]); |
| 379 |
axis *= rotScale; |
| 380 |
float angle = axis.length(); |
| 381 |
axis.normalize(); |
| 382 |
motionEvent->setRotation(SbRotation(axis, angle)); |
| 383 |
|
| 384 |
return motionEvent; |
| 385 |
} |
| 386 |
|
| 387 |
//////////////////////////////////////////////////////////////////////// |
| 388 |
// |
| 389 |
// Description: |
| 390 |
// This returns an SoSpaceballButtonEvent for the passed X event. |
| 391 |
// |
| 392 |
// private |
| 393 |
// |
| 394 |
SoSpaceballButtonEvent * |
| 395 |
SoXtSpaceball::translateButtonEvent(XDeviceButtonEvent *be, |
| 396 |
SoButtonEvent::State whichState) |
| 397 |
// |
| 398 |
//////////////////////////////////////////////////////////////////////// |
| 399 |
{ |
| 400 |
setEventPosition(buttonEvent, be->x, be->y); |
| 401 |
int32_t secs = be->time / 1000; |
| 402 |
buttonEvent->setTime(SbTime(secs, 1000 * (be->time - 1000 * secs))); |
| 403 |
buttonEvent->setShiftDown(be->state & ShiftMask); |
| 404 |
buttonEvent->setCtrlDown(be->state & ControlMask); |
| 405 |
buttonEvent->setAltDown(be->state & Mod1Mask); |
| 406 |
|
| 407 |
// the value of be->button happens to match the SoSpaceballButton values |
| 408 |
buttonEvent->setButton((SoSpaceballButtonEvent::Button) be->button); |
| 409 |
buttonEvent->setState(whichState); |
| 410 |
|
| 411 |
return buttonEvent; |
| 412 |
} |
| 413 |
|