Logiciels divers autour du protocole « Open Sound Control » aka OSC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

osc-joy.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Pour compiler :
  3. * gcc -Wall osc-joy.c -llo -o osc-joy
  4. *
  5. * Options :
  6. * -j joydevice default /dev/input/js0
  7. * -h destination (or IP addr) default localhost
  8. * -p destport default is 9000
  9. * -o nnn offset for button number
  10. */
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <sys/ioctl.h>
  17. #include <linux/joystick.h>
  18. #include <lo/lo.h>
  19. #include "functions/senders.h"
  20. #include "functions/joyutils.h"
  21. /* default values, can be changed on command line */
  22. #define REMOTE_HOST "localhost" /* just loling */
  23. #define REMOTE_PORT "9000"
  24. #define JOY_DEVICE "/dev/input/js0"
  25. #define MY_TEXT_ID "suck my stick"
  26. /* ----------------------------------------------------------------- */
  27. int verbosity = 0;
  28. int button_offset = 0;
  29. char *my_id = MY_TEXT_ID;
  30. /* ----------------------------------------------------------------- */
  31. static void help(int k)
  32. {
  33. puts("\t * joystick -> osc "__DATE__" *");
  34. puts("\t-D\tdump joystick datas");
  35. puts("\t-r\tremote host ("REMOTE_HOST")");
  36. puts("\t-p\tremote UDP port ("REMOTE_PORT")");
  37. puts("\t-j\tjoystick device ("JOY_DEVICE")");
  38. puts("\t-v\tincrease verbosity");
  39. printf("\t-o\toffset added to button number (%d)\n", button_offset);
  40. puts("\t-I\tchange text id (\""MY_TEXT_ID"\")");
  41. exit(0);
  42. }
  43. /* ----------------------------------------------------------------- */
  44. int main(int argc, char *argv[])
  45. {
  46. int foo, joy_fd;
  47. struct js_event js;
  48. int x_pos, y_pos, flag;
  49. char joy_name[128];
  50. int opt;
  51. char *remote_host = REMOTE_HOST;
  52. char *remote_port = REMOTE_PORT;
  53. char *joy_device = JOY_DEVICE;
  54. int do_dump = 0;
  55. /* parsing command line options */
  56. while ((opt = getopt(argc, argv, "Dhp:r:vj:o:I:")) != -1) {
  57. switch (opt) {
  58. case 'D': do_dump = 1; break;
  59. case 'h': help(0); break;
  60. case 'r': remote_host = optarg; break;
  61. case 'p': remote_port = optarg; break;
  62. case 'j': joy_device = optarg; break;
  63. case 'v': verbosity++; break;
  64. case 'o': button_offset = atoi(optarg);
  65. break;
  66. case 'I': my_id = optarg; break;
  67. default: exit(1); break;
  68. }
  69. }
  70. if (do_dump) {
  71. fprintf(stderr, "dumping data from '%s'\n", joy_device);
  72. dump_my_joystick(joy_device);
  73. }
  74. if (verbosity) {
  75. fprintf(stderr, "%s is sending to %s:%s\n", argv[0],
  76. remote_host, remote_port);
  77. fprintf(stderr, " the stick '%s'is on %s\n", my_id, joy_device);
  78. }
  79. lo_address t = lo_address_new(remote_host, remote_port);
  80. if( ( joy_fd = open(joy_device , O_RDONLY)) == -1 ) {
  81. fprintf(stderr, "Couldn't open %s\n", JOY_DEVICE);
  82. exit(1);
  83. }
  84. if (verbosity) {
  85. if (ioctl(joy_fd, JSIOCGNAME(sizeof(joy_name)), joy_name) < 0)
  86. strncpy(joy_name, "Unknown", sizeof(joy_name));
  87. fprintf(stderr, "Name: %s\n", joy_name);
  88. }
  89. send_data_id(t, my_id);
  90. x_pos = y_pos = 0;
  91. for (;;) {
  92. foo = read(joy_fd, &js, sizeof(struct js_event));
  93. if (8 != foo) {
  94. fprintf(stderr, "err reading joy\n");
  95. exit(1);
  96. }
  97. /* calibration datas ignored */
  98. if (js.type > 128) continue;
  99. if (verbosity>1) {
  100. fprintf(stderr, "%10u %2d %2d %7d\n",
  101. js.time, js.type, js.number, js.value);
  102. }
  103. if (2==js.type) { /* oscillating stick */
  104. flag = 0;
  105. switch (js.number)
  106. {
  107. case 0:
  108. x_pos = js.value;
  109. flag = 1;
  110. break;
  111. case 1:
  112. y_pos = js.value;
  113. flag = 1;
  114. break;
  115. }
  116. if (flag) {
  117. foo = send_data_xy(t, x_pos, y_pos);
  118. flag = 0;
  119. }
  120. }
  121. if (1==js.type) { /* it's a button */
  122. foo = send_data_button(t, js.number+button_offset,
  123. js.value);
  124. }
  125. }
  126. return 0;
  127. }
  128. /* ----------------------------------------------------------------- */