/* * SHOW BUTTONS */ #include #include #include #include #include #include #include #include "functions/ncursefuncs.h" /* ----------------------------------------------------------------- */ #define LOCAL_PORT "9002" #define NB_BUTTONS 24 int verbosity; typedef struct { int type; int value; char texte[20]; } Event; #define BUTTONS_EV 1 #define XY_EV 2 #define ID_EV 3 static int count; static int pipefd[2]; #define EVNT_RD (pipefd[0]) #define EVNT_WR (pipefd[1]) unsigned char buttons[NB_BUTTONS]; short xy_values[2]; /* ----------------------------------------------------------------- */ #define LINE0 8 /* vertical position of the top line */ int draw_buttons(unsigned char *states, int nbre, int unused) { int idx, pos_c, pos_l; char txt[20]; #if DEBUG_LEVEL fprintf(stderr, "--> %s ( %p %d )\n", __func__, states, nbre); #endif for (idx=0; idx 1 fprintf(stderr, "in %s : value=%6d, seuil=%d\n", __func__, value, seuil); #endif for (foo=0; foo 1 sprintf(buffer, "%s : %6d ", __func__, value); blast_error_message(buffer, 0, 0); #endif return 0; } /* ----------------------------------------------------------------- */ int display_values(short *vals, int nbre, int ligne) { int foo; char buffer[80]; for (foo=0; fooi; y = argv[1]->i; /* may be we can bound-check that pair or values ? */ xy_values[0] = x; xy_values[1] = y; evt.type = XY_EV; evt.value = 0x55; foo = write(EVNT_WR, &evt, sizeof(Event)); if (sizeof(Event) != foo) { fprintf(stderr, "%s : fifo error %d\n", __func__, errno); exit(1); } count++; return 0; } /* ----------------------------------------------------------------- */ int id_handler(const char *path, const char *types, lo_arg ** argv, int argc, void *data, void *user_data) { Event evt; int foo; evt.type = ID_EV; evt.value = 0x55; foo = write(EVNT_WR, &evt, sizeof(Event)); if (sizeof(Event) != foo) { fprintf(stderr, "%s : fifo error %d\n", __func__, errno); exit(1); } return 0; } /* ----------------------------------------------------------------- */ int button_handler(const char *path, const char *types, lo_arg ** argv, int argc, void *data, void *user_data) { // char ligne[80]; int button, state, foo; Event evt; #if DEBUG_LEVEL fprintf(stderr, "%s : %s %s %d %p\n", __func__, path, types, argc, user_data); #endif button = argv[0]->i; state = argv[1]->i; if (button<0 || button>=NB_BUTTONS) { return 1; } ((unsigned char *)user_data)[button] = state; evt.type = BUTTONS_EV; evt.value = 0x55; foo = write(EVNT_WR, &evt, sizeof(Event)); if (sizeof(Event) != foo) { fprintf(stderr, "%s : fifo error %d\n", __func__, errno); exit(1); } count++; return 0; } /* ----------------------------------------------------------------- */ static void help(int k) { puts("\t * showbuttons " __DATE__ " *"); puts("\t-a\tshow animation"); puts("\t-p\tlocal udp port ("LOCAL_PORT")"); puts("\t-v\tenhance my verbosity"); exit(0); } /* ----------------------------------------------------------------- */ int main(int argc, char *argv[]) { int foo; lo_server_thread st; char *local_port = LOCAL_PORT; int opt; char ligne[81]; long tdebut; Event event; /* parsing command line options */ while ((opt = getopt(argc, argv, "ahp:vE:C:")) != -1) { switch (opt) { case 'a': do_animation(200, 20); break; case 'h': help(0); break; case 'p': local_port = optarg; break; case 'v': verbosity++; break; default: exit(1); } } tdebut = time(NULL); count = 0; /* * setup the inter-thread event synthetizer */ foo = pipe(pipefd); if (foo) { perror("event synth"); exit(1); } /* set up the pretty screen user interface */ foo = initcurses(); sprintf(ligne, ":%s ", local_port); foo = draw_main_screen(ligne, 0); if (foo) { endwin(); fprintf(stderr, "main screen : err %d\n", foo); exit(1); } /* fireup the OSC liblo engine */ st = lo_server_thread_new(local_port, error); #if DEBUG_LEVEL fprintf(stderr, "Address of 'values' = %p\n", buttons); #endif lo_server_thread_add_method(st, "/joystick/b", "ii", button_handler, buttons); lo_server_thread_add_method(st, "/joystick/xy", "ii", xy_handler, NULL); lo_server_thread_start(st); sprintf(ligne, "process %d on board, captain", getpid()); blast_error_message(ligne, 0, 0); sleep(1); for (;;) { /* wait for un event from liblo threads */ foo = read(EVNT_RD, &event, sizeof(Event)); if (sizeof(Event) != foo) { /* FAILURE */ perror("event fifo read error"); exit(1); } #if DEBUG_LEVEL fprintf(stderr, "event %5d %8d %s\n", event.type, event.value, event.texte); #endif switch (event.type) { case BUTTONS_EV: draw_buttons(buttons, NB_BUTTONS, 0); break; case XY_EV: display_values(xy_values, 2, 4); break; case ID_EV: break; default: blast_error_message("bad event", 0, 0); break; } } if (verbosity) { fprintf(stderr, "elapsed %ld s.\n", time(NULL)-tdebut); } return 0; } /* ----------------------------------------------------------------- */