|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <signal.h> |
| 4 | +#include <X11/Xlib.h> |
| 5 | +#include <spnav.h> |
| 6 | + |
| 7 | +void sig(int s) |
| 8 | +{ |
| 9 | + spnav_close(); |
| 10 | + exit(0); |
| 11 | +} |
| 12 | + |
| 13 | +int main(void) |
| 14 | +{ |
| 15 | + Display *dpy; |
| 16 | + Window win; |
| 17 | + unsigned long bpix; |
| 18 | + spnav_event sev; |
| 19 | + |
| 20 | + signal(SIGINT, sig); |
| 21 | + |
| 22 | + if(!(dpy = XOpenDisplay(0))) { |
| 23 | + fprintf(stderr, "failed to connect to the X server\n"); |
| 24 | + return 1; |
| 25 | + } |
| 26 | + |
| 27 | + bpix = BlackPixel(dpy, DefaultScreen(dpy)); |
| 28 | + win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, bpix, bpix); |
| 29 | + |
| 30 | + /* This actually registers our window with the driver for receiving |
| 31 | + * motion/button events through the 3dxsrv-compatible X11 protocol. |
| 32 | + */ |
| 33 | + if(spnav_x11_open(dpy, win) == -1) { |
| 34 | + fprintf(stderr, "failed to connect to the space navigator daemon\n"); |
| 35 | + return 1; |
| 36 | + } |
| 37 | + |
| 38 | + /* spnav_wait_event() and spnav_poll_event(), will silently ignore any non-spnav X11 events. |
| 39 | + * |
| 40 | + * If you need to handle other X11 events you will have to use a regular XNextEvent() loop, |
| 41 | + * and pass any ClientMessage events to spnav_x11_event, which will return the event type or |
| 42 | + * zero if it's not an spnav event (see spnav.h). |
| 43 | + */ |
| 44 | + while(spnav_wait_event(&sev)) { |
| 45 | + if(sev.type == SPNAV_EVENT_MOTION) { |
| 46 | + printf("got motion event: t(%d, %d, %d) ", sev.motion.x, sev.motion.y, sev.motion.z); |
| 47 | + printf("r(%d, %d, %d)\n", sev.motion.rx, sev.motion.ry, sev.motion.rz); |
| 48 | + } else { /* SPNAV_EVENT_BUTTON */ |
| 49 | + printf("got button %s event b(%d)\n", sev.button.press ? "press" : "release", sev.button.bnum); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + spnav_close(); |
| 54 | + return 0; |
| 55 | +} |
0 commit comments