spacenavOSC.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(int argc, char *argv[])
  18. {
  19. const char * ip = NULL;
  20. const char * port = "8000" ;
  21. // handle cli arguments
  22. int opt;
  23. while((opt = getopt(argc, argv, "hi:p:")) != -1)
  24. {
  25. switch(opt)
  26. {
  27. case 'i':
  28. printf("ip: %s\n", optarg);
  29. ip = optarg ;
  30. break;
  31. case 'p':
  32. printf("port: %s\n", optarg);
  33. port = optarg ;
  34. break;
  35. case 'h':
  36. fprintf(stderr, "Usage: spacenavOSC -i 192.168.0.1 -p 8000\n"
  37. "Options:\n"
  38. " -i target IP, default to localhost\n"
  39. " -p target port, default 8000\n"
  40. " -h print this message.\n"
  41. );
  42. return 0;
  43. case ':':
  44. printf("option needs a value\n");
  45. return 0;
  46. break;
  47. case '?':
  48. printf("unknown option: %c\n", optopt);
  49. return 0;
  50. break;
  51. }
  52. }
  53. // optind is for the extra arguments
  54. // which are not parsed
  55. for(; optind < argc; optind++){
  56. printf("extra arguments: %s\n", argv[optind]);
  57. }
  58. spnav_event sev;
  59. signal(SIGINT, sig);
  60. if(spnav_open()==-1) {
  61. fprintf(stderr, "failed to connect to the space navigator daemon\n");
  62. return 1;
  63. }
  64. lo_address t = lo_address_new(ip, port);
  65. lo_send(t, "/spnav", "s","spnavOSC started");
  66. /* spnav_wait_event() and spnav_poll_event(), will silently ignore any non-spnav X11 events.
  67. *
  68. * If you need to handle other X11 events you will have to use a regular XNextEvent() loop,
  69. * and pass any ClientMessage events to spnav_x11_event, which will return the event type or
  70. * zero if it's not an spnav event (see spnav.h).
  71. */
  72. while(spnav_wait_event(&sev)) {
  73. if(sev.type == SPNAV_EVENT_MOTION) {
  74. printf("got motion event: t(%d, %d, %d) ", sev.motion.x, sev.motion.y, sev.motion.z);
  75. printf("r(%d, %d, %d)\n", sev.motion.rx, sev.motion.ry, sev.motion.rz);
  76. lo_send(t, "/spnav/motion", "iiiiii",sev.motion.x, sev.motion.y, sev.motion.z, sev.motion.rx, sev.motion.ry, sev.motion.rz);
  77. } else { /* SPNAV_EVENT_BUTTON */
  78. printf("got button %s event b(%d)\n", sev.button.press ? "press" : "release", sev.button.bnum);
  79. lo_send(t, "/spnav/button", "ii", sev.button.press ? 1 : 0, sev.button.bnum);
  80. }
  81. }
  82. spnav_close();
  83. return 0;
  84. }