1 /* Spaceball support for Linux.
\r
2 * Written by John Tsiombikas <nuclear@member.fsf.org>
\r
4 * This code supports 3Dconnexion's 6-dof space-whatever devices.
\r
5 * It can communicate with either the proprietary 3Dconnexion daemon (3dxsrv)
\r
6 * free spacenavd (http://spacenav.sourceforge.net), through the "standard"
\r
7 * magellan X-based protocol.
\r
10 #include <GL/freeglut.h>
\r
11 #include "freeglut_internal.h"
\r
13 /* -- PRIVATE FUNCTIONS --------------------------------------------------- */
\r
15 #if TARGET_HOST_POSIX_X11
\r
16 #include <X11/Xlib.h>
\r
19 SPNAV_EVENT_ANY, /* used by spnav_remove_events() */
\r
21 SPNAV_EVENT_BUTTON /* includes both press and release */
\r
24 struct spnav_event_motion {
\r
28 unsigned int period;
\r
32 struct spnav_event_button {
\r
38 typedef union spnav_event {
\r
40 struct spnav_event_motion motion;
\r
41 struct spnav_event_button button;
\r
45 static int spnav_x11_open(Display *dpy, Window win);
\r
46 static int spnav_x11_window(Window win);
\r
47 static int spnav_x11_event(const XEvent *xev, spnav_event *event);
\r
48 static int spnav_close(void);
\r
49 static int spnav_fd(void);
\r
50 static int spnav_remove_events(int type);
\r
52 static SFG_Window *spnav_win;
\r
55 /* Flag telling whether we have a spaceball:
\r
56 * 0 - haven't tried initializing
\r
57 * 1 - have successfully initialized
\r
58 * -1 - have tried to initialize but not succeeded
\r
60 static int sball_initialized = 0;
\r
63 void fgInitialiseSpaceball(void)
\r
65 if(sball_initialized != 0) {
\r
69 #if TARGET_HOST_POSIX_X11
\r
73 if(!fgStructure.CurrentWindow)
\r
75 sball_initialized = -1;
\r
79 w = fgStructure.CurrentWindow->Window.Handle;
\r
80 if(spnav_x11_open(fgDisplay.Display, w) == -1)
\r
82 sball_initialized = -1;
\r
88 sball_initialized = 1;
\r
91 void fgSpaceballClose(void)
\r
93 #if TARGET_HOST_POSIX_X11
\r
98 int fgHasSpaceball(void)
\r
100 if(sball_initialized == 0) {
\r
101 fgInitialiseSpaceball();
\r
102 if(sball_initialized != 1) {
\r
103 fgWarning("fgInitialiseSpaceball failed\n");
\r
108 #if TARGET_HOST_POSIX_X11
\r
109 /* XXX this function should somehow query the driver if there's a device
\r
110 * plugged in, as opposed to just checking if there's a driver to talk to.
\r
112 return spnav_fd() == -1 ? 0 : 1;
\r
118 int fgSpaceballNumButtons(void)
\r
120 if(sball_initialized == 0) {
\r
121 fgInitialiseSpaceball();
\r
122 if(sball_initialized != 1) {
\r
123 fgWarning("fgInitialiseSpaceball failed\n");
\r
128 #if TARGET_HOST_POSIX_X11
\r
129 return 2; /* TODO implement this properly */
\r
135 void fgSpaceballSetWindow(SFG_Window *window)
\r
137 if(sball_initialized == 0) {
\r
138 fgInitialiseSpaceball();
\r
139 if(sball_initialized != 1) {
\r
144 #if TARGET_HOST_POSIX_X11
\r
145 if(spnav_win != window) {
\r
146 spnav_x11_window(window->Window.Handle);
\r
147 spnav_win = window;
\r
153 #if TARGET_HOST_POSIX_X11
\r
154 int fgIsSpaceballXEvent(const XEvent *xev)
\r
158 if(spnav_win != fgStructure.CurrentWindow) {
\r
159 /* this will also initialize spaceball if needed (first call) */
\r
160 fgSpaceballSetWindow(fgStructure.CurrentWindow);
\r
163 if(sball_initialized != 1) {
\r
167 return spnav_x11_event(xev, &sev);
\r
170 void fgSpaceballHandleXEvent(const XEvent *xev)
\r
174 if(sball_initialized == 0) {
\r
175 fgInitialiseSpaceball();
\r
176 if(sball_initialized != 1) {
\r
181 if(spnav_x11_event(xev, &sev)) {
\r
183 case SPNAV_EVENT_MOTION:
\r
184 if(sev.motion.x | sev.motion.y | sev.motion.z) {
\r
185 INVOKE_WCB(*spnav_win, SpaceMotion, (sev.motion.x, sev.motion.y, sev.motion.z));
\r
187 if(sev.motion.rx | sev.motion.ry | sev.motion.rz) {
\r
188 INVOKE_WCB(*spnav_win, SpaceRotation, (sev.motion.rx, sev.motion.ry, sev.motion.rz));
\r
190 spnav_remove_events(SPNAV_EVENT_MOTION);
\r
193 case SPNAV_EVENT_BUTTON:
\r
194 INVOKE_WCB(*spnav_win, SpaceButton, (sev.button.bnum, sev.button.press ? GLUT_DOWN : GLUT_UP));
\r
204 The following code is part of libspnav, part of the spacenav project (spacenav.sf.net)
\r
205 Copyright (C) 2007-2009 John Tsiombikas <nuclear@member.fsf.org>
\r
207 Redistribution and use in source and binary forms, with or without
\r
208 modification, are permitted provided that the following conditions are met:
\r
210 1. Redistributions of source code must retain the above copyright notice, this
\r
211 list of conditions and the following disclaimer.
\r
212 2. Redistributions in binary form must reproduce the above copyright notice,
\r
213 this list of conditions and the following disclaimer in the documentation
\r
214 and/or other materials provided with the distribution.
\r
215 3. The name of the author may not be used to endorse or promote products
\r
216 derived from this software without specific prior written permission.
\r
218 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
219 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
220 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
\r
221 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
222 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
\r
223 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
224 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
225 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
\r
226 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
\r
230 #include <stdlib.h>
\r
231 #include <string.h>
\r
233 #ifdef HAVE_ERRNO_H
\r
237 #include <X11/Xlib.h>
\r
238 #include <X11/Xutil.h>
\r
240 static Window get_daemon_window(Display *dpy);
\r
241 static int catch_badwin(Display *dpy, XErrorEvent *err);
\r
243 static Display *dpy;
\r
244 static Window app_win;
\r
245 static Atom motion_event, button_press_event, button_release_event, command_event;
\r
248 CMD_APP_WINDOW = 27695,
\r
252 #define IS_OPEN dpy
\r
254 struct event_node {
\r
256 struct event_node *next;
\r
259 static int spnav_x11_open(Display *display, Window win)
\r
267 motion_event = XInternAtom(dpy, "MotionEvent", True);
\r
268 button_press_event = XInternAtom(dpy, "ButtonPressEvent", True);
\r
269 button_release_event = XInternAtom(dpy, "ButtonReleaseEvent", True);
\r
270 command_event = XInternAtom(dpy, "CommandEvent", True);
\r
272 if(!motion_event || !button_press_event || !button_release_event || !command_event) {
\r
274 return -1; /* daemon not started */
\r
277 if(spnav_x11_window(win) == -1) {
\r
279 return -1; /* daemon not started */
\r
286 static int spnav_close(void)
\r
289 spnav_x11_window(DefaultRootWindow(dpy));
\r
297 static int spnav_x11_window(Window win)
\r
299 int (*prev_xerr_handler)(Display*, XErrorEvent*);
\r
307 if(!(daemon_win = get_daemon_window(dpy))) {
\r
311 prev_xerr_handler = XSetErrorHandler(catch_badwin);
\r
313 xev.type = ClientMessage;
\r
314 xev.xclient.send_event = False;
\r
315 xev.xclient.display = dpy;
\r
316 xev.xclient.window = win;
\r
317 xev.xclient.message_type = command_event;
\r
318 xev.xclient.format = 16;
\r
319 xev.xclient.data.s[0] = ((unsigned int)win & 0xffff0000) >> 16;
\r
320 xev.xclient.data.s[1] = (unsigned int)win & 0xffff;
\r
321 xev.xclient.data.s[2] = CMD_APP_WINDOW;
\r
323 XSendEvent(dpy, daemon_win, False, 0, &xev);
\r
326 XSetErrorHandler(prev_xerr_handler);
\r
330 static int spnav_fd(void)
\r
333 return ConnectionNumber(dpy);
\r
338 /*static int spnav_wait_event(spnav_event *event)
\r
343 XNextEvent(dpy, &xev);
\r
345 if(spnav_x11_event(&xev, event) > 0) {
\r
346 return event->type;
\r
353 static int spnav_poll_event(spnav_event *event)
\r
356 if(XPending(dpy)) {
\r
358 XNextEvent(dpy, &xev);
\r
360 return spnav_x11_event(&xev, event);
\r
366 static Bool match_events(Display *dpy, XEvent *xev, char *arg)
\r
368 int evtype = *(int*)arg;
\r
370 if(xev->type != ClientMessage) {
\r
374 if(xev->xclient.message_type == motion_event) {
\r
375 return !evtype || evtype == SPNAV_EVENT_MOTION ? True : False;
\r
377 if(xev->xclient.message_type == button_press_event ||
\r
378 xev->xclient.message_type == button_release_event) {
\r
379 return !evtype || evtype == SPNAV_EVENT_BUTTON ? True : False;
\r
384 static int spnav_remove_events(int type)
\r
391 while(XCheckIfEvent(dpy, &xev, match_events, (char*)&type)) {
\r
399 static int spnav_x11_event(const XEvent *xev, spnav_event *event)
\r
404 if(xev->type != ClientMessage) {
\r
408 xmsg_type = xev->xclient.message_type;
\r
410 if(xmsg_type != motion_event && xmsg_type != button_press_event &&
\r
411 xmsg_type != button_release_event) {
\r
415 if(xmsg_type == motion_event) {
\r
416 event->type = SPNAV_EVENT_MOTION;
\r
417 event->motion.data = &event->motion.x;
\r
419 for(i=0; i<6; i++) {
\r
420 event->motion.data[i] = xev->xclient.data.s[i + 2];
\r
422 event->motion.period = xev->xclient.data.s[8];
\r
424 event->type = SPNAV_EVENT_BUTTON;
\r
425 event->button.press = xmsg_type == button_press_event ? 1 : 0;
\r
426 event->button.bnum = xev->xclient.data.s[2];
\r
428 return event->type;
\r
432 static Window get_daemon_window(Display *dpy)
\r
434 Window win, root_win;
\r
435 XTextProperty wname;
\r
438 unsigned long nitems, bytes_after;
\r
439 unsigned char *prop;
\r
441 root_win = DefaultRootWindow(dpy);
\r
443 XGetWindowProperty(dpy, root_win, command_event, 0, 1, False, AnyPropertyType, &type, &fmt, &nitems, &bytes_after, &prop);
\r
448 win = *(Window*)prop;
\r
451 if(!XGetWMName(dpy, win, &wname) || strcmp("Magellan Window", (char*)wname.value) != 0) {
\r
458 static int catch_badwin(Display *dpy, XErrorEvent *err)
\r
462 if(err->error_code == BadWindow) {
\r
465 XGetErrorText(dpy, err->error_code, buf, sizeof buf);
\r
466 fprintf(stderr, "Caught unexpected X error: %s\n", buf);
\r
471 #endif /* TARGET_HOST_POSIX_X11 */
\r