void list_joysticks() { int num_joysticks = SDL_NumJoysticks(); if (num_joysticks == 0) { printf("No joysticks were found\n"); } else { printf("Found %d joystick(s)\n\n", num_joysticks); for(int joy_idx = 0; joy_idx < num_joysticks; ++joy_idx) { SDL_Joystick* joy = SDL_JoystickOpen(joy_idx); if (!joy) { fprintf(stderr, "Unable to open joystick %d\n", joy_idx); } else { SDL_GameController* gamepad = SDL_GameControllerOpen(joy_idx); print_joystick_info(joy_idx, joy, gamepad); if (gamepad) { SDL_GameControllerClose(gamepad); } SDL_JoystickClose(joy); } } } }
void event_joystick(int joy_idx) { SDL_Joystick* joy = SDL_JoystickOpen(joy_idx); if (!joy) { fprintf(stderr, "Unable to open joystick %d\n", joy_idx); } else { print_joystick_info(joy_idx, joy, NULL); printf("Entering joystick test loop, press Ctrl-c to exit\n"); int quit = 0; SDL_Event event; while(!quit && SDL_WaitEvent(&event)) { switch(event.type) { case SDL_JOYAXISMOTION: printf("SDL_JOYAXISMOTION: joystick: %d axis: %d value: %d\n", event.jaxis.which, event.jaxis.axis, event.jaxis.value); break; case SDL_JOYBUTTONDOWN: printf("SDL_JOYBUTTONDOWN: joystick: %d button: %d state: %d\n", event.jbutton.which, event.jbutton.button, event.jbutton.state); break; case SDL_JOYBUTTONUP: printf("SDL_JOYBUTTONUP: joystick: %d button: %d state: %d\n", event.jbutton.which, event.jbutton.button, event.jbutton.state); break; case SDL_JOYHATMOTION: printf("SDL_JOYHATMOTION: joystick: %d hat: %d value: %d\n", event.jhat.which, event.jhat.hat, event.jhat.value); break; case SDL_JOYBALLMOTION: printf("SDL_JOYBALLMOTION: joystick: %d ball: %d x: %d y: %d\n", event.jball.which, event.jball.ball, event.jball.xrel, event.jball.yrel); break; case SDL_JOYDEVICEADDED: printf("SDL_JOYDEVICEADDED which:%d\n", event.jdevice.which); break; case SDL_JOYDEVICEREMOVED: printf("SDL_JOYDEVICEREMOVED which:%d\n", event.jdevice.which); break; case SDL_CONTROLLERBUTTONDOWN: printf("SDL_CONTROLLERBUTTONDOWN\n"); break; case SDL_CONTROLLERBUTTONUP: printf("SDL_CONTROLLERBUTTONUP\n"); break; case SDL_CONTROLLERDEVICEADDED: printf("SDL_CONTROLLERDEVICEADDED which:%d\n", event.cdevice.which); break; case SDL_CONTROLLERDEVICEREMOVED: printf("SDL_CONTROLLERDEVICEREMOVED which:%d\n", event.cdevice.which); break; case SDL_CONTROLLERDEVICEREMAPPED: printf("SDL_CONTROLLERDEVICEREMAPPED which:%d\n", event.cdevice.which); break; case SDL_QUIT: quit = 1; printf("Recieved interrupt, exiting\n"); break; default: fprintf(stderr, "Error: Unhandled event type: %d\n", event.type); break; } } SDL_JoystickClose(joy); } }
int main(int argc, char **argv) { int num_joysticks; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_JOYSTICK *curr_joy; ALLEGRO_DISPLAY *display; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } if (!al_install_joystick()) { abort_example("Could not init joysticks.\n"); } al_install_keyboard(); al_init_primitives_addon(); open_log(); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display.\n"); } queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_joystick_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); num_joysticks = al_get_num_joysticks(); log_printf("Num joysticks: %d\n", num_joysticks); if (num_joysticks > 0) { curr_joy = al_get_joystick(0); print_joystick_info(curr_joy); } else { curr_joy = NULL; } draw(curr_joy); while (1) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_KEY_CHAR) { int n = event.keyboard.unichar - '0'; if (n >= 0 && n < num_joysticks) { curr_joy = al_get_joystick(n); log_printf("switching to joystick %d\n", n); print_joystick_info(curr_joy); } } else if (event.type == ALLEGRO_EVENT_JOYSTICK_CONFIGURATION) { al_reconfigure_joysticks(); num_joysticks = al_get_num_joysticks(); log_printf("after reconfiguration num joysticks = %d\n", num_joysticks); if (curr_joy) { log_printf("current joystick is: %s\n", al_get_joystick_active(curr_joy) ? "active" : "inactive"); } curr_joy = al_get_joystick(0); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_AXIS) { log_printf("axis event from %p, stick %d, axis %d\n", event.joystick.id, event.joystick.stick, event.joystick.axis); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) { log_printf("button down event %d from %p\n", event.joystick.button, event.joystick.id); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_UP) { log_printf("button up event %d from %p\n", event.joystick.button, event.joystick.id); } draw(curr_joy); } close_log(false); return 0; }
int main(int argc, char** argv) { if (argc == 1) { print_help(argv[0]); exit(1); } // printf("argv: %s %s %s %s %s %i\n", argv[0], argv[1], argv[2], argv[3], argv[4], argc ); // FIXME: We don't need video, but without it SDL will fail to work in SDL_WaitEvent() if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } else { atexit(SDL_Quit); if (argc == 2 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) { print_help(argv[0]); } if (argc == 2 && (strcmp(argv[1], "--version") == 0)) { printf("sdl2osc 0.1.0\n"); exit(EXIT_SUCCESS); } else if (argc == 2 && (strcmp(argv[1], "--list") == 0 || (strcmp(argv[1], "-l") == 0))) { int num_joysticks = SDL_NumJoysticks(); if (num_joysticks == 0) { printf("No joysticks were found\n"); } else { int joy_idx; printf("Found %d joystick(s)\n\n", num_joysticks); for(joy_idx = 0; joy_idx < num_joysticks; ++joy_idx) { SDL_Joystick* joy = SDL_JoystickOpen(joy_idx); if (!joy) { fprintf(stderr, "Unable to open joystick %d\n", joy_idx); } else { print_joystick_info(joy_idx, joy); SDL_JoystickClose(joy); } } } } else if (argc == 3 && (strcmp(argv[1], "--event") == 0 || strcmp(argv[1], "-e") == 0)) { int joy_idx; if (!str2int(argv[2], &joy_idx)) { fprintf(stderr, "Error: JOYSTICKNUM argument must be a number, but was '%s'\n", argv[2]); exit(1); } SDL_Joystick* joy = SDL_JoystickOpen(joy_idx); if (!joy) { fprintf(stderr, "Unable to open joystick %d\n", joy_idx); } else { print_joystick_info(joy_idx, joy); printf("Entering joystick test loop, press Ctrl-c to exit\n"); int quit = 0; SDL_Event event; while(!quit && SDL_WaitEvent(&event)) { switch(event.type) { case SDL_JOYAXISMOTION: printf("SDL_JOYAXISMOTION: joystick: %d axis: %d value: %d\n", event.jaxis.which, event.jaxis.axis, event.jaxis.value); break; case SDL_JOYBUTTONDOWN: printf("SDL_JOYBUTTONUP: joystick: %d button: %d state: %d\n", event.jbutton.which, event.jbutton.button, event.jbutton.state); break; case SDL_JOYBUTTONUP: printf("SDL_JOYBUTTONDOWN: joystick: %d button: %d state: %d\n", event.jbutton.which, event.jbutton.button, event.jbutton.state); break; case SDL_JOYHATMOTION: printf("SDL_JOYHATMOTION: joystick: %d hat: %d value: %d\n", event.jhat.which, event.jhat.hat, event.jhat.value); break; case SDL_JOYBALLMOTION: printf("SDL_JOYBALLMOTION: joystick: %d ball: %d x: %d y: %d\n", event.jball.which, event.jball.ball, event.jball.xrel, event.jball.yrel); break; case SDL_QUIT: quit = 1; printf("Recieved interrupt, exiting\n"); break; default: fprintf(stderr, "Error: Unhandled event type: %d\n", event.type); } } SDL_JoystickClose(joy); } fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); } else if (argc == 2 && (strcmp(argv[1], "--osc") == 0 || strcmp(argv[1], "-o") == 0)) { int joy_idx; // if (!str2int(argv[2], &joy_idx)) // { // fprintf(stderr, "Error: JOYSTICKNUM argument must be a number, but was '%s'\n", argv[2]); // exit(1); // } char *port = "57151"; char *outport = "57120"; char *ip = "127.0.0.1"; if ( argc == 5 ) { ip = argv[4]; port = argv[3]; outport = argv[2]; } else if ( argc == 4 ) { port = argv[3]; outport = argv[2]; } else if ( argc == 3 ) { outport = argv[2]; } init_osc( ip, outport, port ); // SDL_Joystick* joy = SDL_JoystickOpen(joy_idx); // if (!joy) // { // fprintf(stderr, "Unable to open joystick %d\n", joy_idx); // } // else // { // print_joystick_info(joy_idx, joy); printf("Entering joystick test loop, press Ctrl-c to exit\n"); // int quit = 0; SDL_Event event; lo_timetag lo_now = LO_TT_IMMEDIATE; while(!done && SDL_WaitEvent(&event)) { switch(event.type) { case SDL_JOYAXISMOTION: lo_send_from( t, s, lo_now, "/joystick/axis", "iii", event.jaxis.which, event.jaxis.axis, event.jaxis.value ); // printf("SDL_JOYAXISMOTION: joystick: %d axis: %d value: %d\n", // event.jaxis.which, event.jaxis.axis, event.jaxis.value); break; case SDL_JOYBUTTONDOWN: lo_send_from( t, s, lo_now, "/joystick/button", "iii", event.jbutton.which, event.jbutton.button, event.jbutton.state ); // printf("SDL_JOYBUTTONUP: joystick: %d button: %d state: %d\n", // event.jbutton.which, event.jbutton.button, event.jbutton.state); break; case SDL_JOYBUTTONUP: lo_send_from( t, s, lo_now, "/joystick/button", "iii", event.jbutton.which, event.jbutton.button, event.jbutton.state ); // printf("SDL_JOYBUTTONDOWN: joystick: %d button: %d state: %d\n", // event.jbutton.which, event.jbutton.button, event.jbutton.state); break; case SDL_JOYHATMOTION: lo_send_from( t, s, lo_now, "/joystick/hat", "iii", event.jhat.which, event.jhat.hat, event.jhat.value ); // printf("SDL_JOYHATMOTION: joystick: %d hat: %d value: %d\n", // event.jhat.which, event.jhat.hat, event.jhat.value); break; case SDL_JOYBALLMOTION: lo_send_from( t, s, lo_now, "/joystick/ball", "iii", event.jball.which, event.jball.ball, event.jball.xrel, event.jball.yrel ); // printf("SDL_JOYBALLMOTION: joystick: %d ball: %d x: %d y: %d\n", // event.jball.which, event.jball.ball, event.jball.xrel, event.jball.yrel); break; case SDL_QUIT: done = 1; printf("Received interrupt, exiting\n"); break; default: fprintf(stderr, "Error: Unhandled event type: %d\n", event.type); } } // SDL_JoystickClose(joy); close_all_joysticks(); lo_send_from( t, s, lo_now, "/sdl2osc/quit", "s", "nothing more to do, quitting" ); lo_server_thread_free( st ); lo_address_free( t ); // } // fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); } else { fprintf(stderr, "%s: unknown arguments\n", argv[0]); fprintf(stderr, "Try '%s --help' for more informations\n", argv[0]); } } }