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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /* default values, can be changed on command line */
  21. #define REMOTE_HOST "localhost" /* just loling */
  22. #define REMOTE_PORT "9000"
  23. #define JOY_DEVICE "/dev/input/js0"
  24. #define MY_TEXT_ID "suck my stick"
  25. /* ----------------------------------------------------------------- */
  26. int verbosity = 0;
  27. int button_offset = 0;
  28. char *my_id = MY_TEXT_ID;
  29. /* ----------------------------------------------------------------- */
  30. static void help(int k)
  31. {
  32. puts("\t * joystick -> osc "__DATE__" *");
  33. puts("\t-r\tremote host ("REMOTE_HOST")");
  34. puts("\t-p\tremote UDP port ("REMOTE_PORT")");
  35. puts("\t-j\tjoystick device ("JOY_DEVICE")");
  36. puts("\t-v\tincrease verbosity");
  37. puts("\t-o\toffset added to button number");
  38. puts("\t-I\tchange text id (\""MY_TEXT_ID"\")");
  39. exit(0);
  40. }
  41. /* ----------------------------------------------------------------- */
  42. int main(int argc, char *argv[])
  43. {
  44. int foo, joy_fd;
  45. struct js_event js;
  46. int x_pos, y_pos, flag;
  47. char joy_name[128];
  48. int opt;
  49. char *remote_host = REMOTE_HOST;
  50. char *remote_port = REMOTE_PORT;
  51. char *joy_device = JOY_DEVICE;
  52. /* parsing command line options */
  53. while ((opt = getopt(argc, argv, "hp:r:vj:o:I:")) != -1) {
  54. switch (opt) {
  55. case 'h': help(0); break;
  56. case 'r': remote_host = optarg; break;
  57. case 'p': remote_port = optarg; break;
  58. case 'j': joy_device = optarg; break;
  59. case 'v': verbosity++; break;
  60. case 'o': button_offset = atoi(optarg);
  61. break;
  62. case 'I': my_id = optarg; break;
  63. default: exit(1); break;
  64. }
  65. }
  66. if (verbosity) {
  67. fprintf(stderr, "%s is sending to %s:%s\n", argv[0],
  68. remote_host, remote_port);
  69. fprintf(stderr, " the stick '%s'is on %s\n", my_id, joy_device);
  70. }
  71. lo_address t = lo_address_new(remote_host, remote_port);
  72. if( ( joy_fd = open(joy_device , O_RDONLY)) == -1 ) {
  73. fprintf(stderr, "Couldn't open %s\n", JOY_DEVICE);
  74. exit(1);
  75. }
  76. if (verbosity) {
  77. if (ioctl(joy_fd, JSIOCGNAME(sizeof(joy_name)), joy_name) < 0)
  78. strncpy(joy_name, "Unknown", sizeof(joy_name));
  79. fprintf(stderr, "Name: %s\n", joy_name);
  80. }
  81. send_data_id(t, my_id);
  82. x_pos = y_pos = 0;
  83. for (;;) {
  84. foo = read(joy_fd, &js, sizeof(struct js_event));
  85. if (8 != foo) {
  86. fprintf(stderr, "err reading joy\n");
  87. exit(1);
  88. }
  89. /* calibration datas ignored */
  90. if (js.type > 128) continue;
  91. if (verbosity>1) {
  92. fprintf(stderr, "%10u %2d %2d %7d\n",
  93. js.time, js.type, js.number, js.value);
  94. }
  95. if (2==js.type) { /* oscillating stick */
  96. flag = 0;
  97. switch (js.number)
  98. {
  99. case 0:
  100. x_pos = js.value;
  101. flag = 1;
  102. break;
  103. case 1:
  104. y_pos = js.value;
  105. flag = 1;
  106. break;
  107. }
  108. if (flag) {
  109. foo = send_data_xy(t, x_pos, y_pos);
  110. flag = 0;
  111. }
  112. }
  113. if (1==js.type) { /* it's a button */
  114. foo = send_data_button(t, js.number+button_offset,
  115. js.value);
  116. }
  117. }
  118. return 0;
  119. }
  120. /* ----------------------------------------------------------------- */