spacenavOSC.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #ifndef WIN32
  4. #include <unistd.h>
  5. #endif
  6. #include "lo/lo.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <signal.h>
  10. // #include <X11/Xlib.h>
  11. #include <spnav.h>
  12. void sig(int s)
  13. {
  14. spnav_close();
  15. exit(0);
  16. }
  17. int main(void)
  18. {
  19. spnav_event sev;
  20. signal(SIGINT, sig);
  21. if(spnav_open()==-1) {
  22. fprintf(stderr, "failed to connect to the space navigator daemon\n");
  23. return 1;
  24. }
  25. lo_address t = lo_address_new(NULL, "7770");
  26. /* spnav_wait_event() and spnav_poll_event(), will silently ignore any non-spnav X11 events.
  27. *
  28. * If you need to handle other X11 events you will have to use a regular XNextEvent() loop,
  29. * and pass any ClientMessage events to spnav_x11_event, which will return the event type or
  30. * zero if it's not an spnav event (see spnav.h).
  31. */
  32. while(spnav_wait_event(&sev)) {
  33. if(sev.type == SPNAV_EVENT_MOTION) {
  34. printf("got motion event: t(%d, %d, %d) ", sev.motion.x, sev.motion.y, sev.motion.z);
  35. printf("r(%d, %d, %d)\n", sev.motion.rx, sev.motion.ry, sev.motion.rz);
  36. lo_send(t, "/motion", "iiiiii",sev.motion.x, sev.motion.y, sev.motion.z, sev.motion.rx, sev.motion.ry, sev.motion.rz);
  37. } else { /* SPNAV_EVENT_BUTTON */
  38. printf("got button %s event b(%d)\n", sev.button.press ? "press" : "release", sev.button.bnum);
  39. lo_send(t, "/button", "ii", sev.button.press ? 1 : 0, sev.button.bnum);
  40. }
  41. }
  42. spnav_close();
  43. return 0;
  44. }