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.

osc2cursor.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * dessiner avec OSC et la manette de jeu
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <limits.h> /* for SHRT_MIN */
  8. #include <time.h>
  9. #include <curses.h>
  10. #include <signal.h>
  11. #include <locale.h>
  12. #include <getopt.h>
  13. #include <lo/lo.h>
  14. #include "functions/ncursefuncs.h"
  15. /* ----------------------------------------------------------------- */
  16. #define LOCAL_PORT "9000"
  17. int verbosity = 0;
  18. unsigned long hits = 0L;
  19. int erase_button = -1;
  20. int current_char = '%';
  21. volatile int must_erase = 0;
  22. /* ----------------------------------------------------------------- */
  23. void error(int num, const char *msg, const char *path)
  24. {
  25. fprintf(stderr, "liblo server error %d in path %s : %s\n", num, path, msg);
  26. exit(1);
  27. }
  28. /* ----------------------------------------------------------------- */
  29. int button_handler(const char *path, const char *types, lo_arg ** argv,
  30. int argc, void *data, void *user_data)
  31. {
  32. char ligne[80];
  33. #if DEBUG_LEVEL > 1
  34. printf(stderr, "%s : %s %s %d\n", __func__, path, types, argc);
  35. #endif
  36. if (verbosity) {
  37. sprintf(ligne, "%s %s %s %d\n", __func__, path, types, argc);
  38. mvaddstr(2, 2, ligne);
  39. refresh();
  40. }
  41. if (-1 == erase_button) return 0;
  42. if ( (argv[0]->i == erase_button) &&
  43. (argv[1]->i == 0) ) {
  44. must_erase = 1;
  45. }
  46. return 0;
  47. }
  48. /* ----------------------------------------------------------------- */
  49. static int old_c = 0, old_l = 0;
  50. int xy_handler(const char *path, const char *types, lo_arg ** argv,
  51. int argc, void *data, void *user_data)
  52. {
  53. char ligne[80];
  54. int val_x, val_y;
  55. int cur_l, cur_c;
  56. float kx, ky;
  57. if (verbosity>1) fprintf(stderr, "%s : %s %s %d\n", __func__,
  58. path, types, argc);
  59. if (must_erase) {
  60. move(1, 0);
  61. clrtobot();
  62. must_erase = 0;
  63. }
  64. standout();
  65. val_x = argv[0]->i; val_y = argv[1]->i;
  66. if (verbosity) {
  67. sprintf(ligne, " xy %6d %6d ", val_x, val_y);
  68. mvaddstr(0, 30, ligne);
  69. hits++;
  70. sprintf(ligne, " %7lu ", hits);
  71. mvaddstr(0, COLS-12, ligne);
  72. }
  73. standend();
  74. /* boundary checking */
  75. if (val_x < SHRT_MIN) val_x = SHRT_MIN;
  76. if (val_y < SHRT_MIN) val_y = SHRT_MIN;
  77. if (val_x > SHRT_MAX) val_x = SHRT_MAX;
  78. if (val_y > SHRT_MAX) val_y = SHRT_MAX;
  79. /* make values positives */
  80. val_x += -SHRT_MIN;
  81. val_y += -SHRT_MIN;
  82. /* compute position of the new spot */
  83. kx = (float)COLS / 65535.0;
  84. ky = (float)(LINES-1) / 65535.0;
  85. cur_c = (int)((float)val_x * kx);
  86. cur_l = 1 + (int)((float)val_y * ky);
  87. if (verbosity) {
  88. fprintf(stderr, "%7d %7d -> %7d %7d\n",
  89. val_x, val_y, cur_l, cur_c);
  90. }
  91. /* erase the old spot */
  92. if (old_l) mvaddch(old_l, old_c, current_char);
  93. standout();
  94. mvaddch(cur_l, cur_c, '#');
  95. standend();
  96. old_l = cur_l, old_c = cur_c;
  97. refresh();
  98. return 0;
  99. }
  100. /* ----------------------------------------------------------------- */
  101. /* ----------------------------------------------------------------- */
  102. static int help(int notused)
  103. {
  104. fprintf(stderr, "\t * osc2cursor %s\n", __DATE__" *");
  105. puts("\t-p\tlistening UDP port ("LOCAL_PORT")");
  106. puts("\t-v\tincrease verbosity");
  107. puts("\t-E\terasing button number (0)");
  108. puts("\t-C\tdrawing character (NA)");
  109. return -1;
  110. }
  111. /* ----------------------------------------------------------------- */
  112. int main(int argc, char *argv[])
  113. {
  114. int foo;
  115. lo_server_thread st;
  116. char *local_port = LOCAL_PORT;
  117. int opt;
  118. char ligne[81];
  119. /* parsing command line options */
  120. while ((opt = getopt(argc, argv, "hp:vE:C:")) != -1) {
  121. switch (opt) {
  122. case 'h': if (help(0)) exit(1); break;
  123. case 'p': local_port = optarg; break;
  124. case 'v': verbosity++; break;
  125. case 'E': erase_button = atoi(optarg);
  126. break;
  127. case 'C': current_char = *optarg; break;
  128. default: exit(1);
  129. }
  130. }
  131. fprintf(stderr, "erase %d\n", erase_button);
  132. st = lo_server_thread_new(local_port, error);
  133. foo = initcurses();
  134. sprintf(ligne, ":%s ", local_port);
  135. foo = draw_main_screen(ligne, 0);
  136. if (verbosity) fprintf(stderr, "dms %d\n", foo);
  137. lo_server_thread_add_method(st, "/joystick/xy", "ii", xy_handler, NULL);
  138. lo_server_thread_add_method(st, "/joystick/b", "ii", button_handler, NULL);
  139. lo_server_thread_start(st);
  140. for (;;) {
  141. if (verbosity)
  142. fprintf(stderr, "t = %ld\n", time(NULL));
  143. sleep(100);
  144. }
  145. return 0;
  146. }
  147. /* ----------------------------------------------------------------- */