Path: blob/main/test/browser/test_glfw_joystick.c
4150 views
/*1* Copyright 2017 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#include <stdio.h>8#include <assert.h>9#include <string.h>10#ifdef __EMSCRIPTEN__11#include <emscripten.h>12#endif13#define GLFW_INCLUDE_ES214#include <GLFW/glfw3.h>1516GLFWwindow* g_window;1718void error_callback(int error, const char* description);1920int joy_connected = -1;2122void joystick_callback(int joy, int event) {23if (event == GLFW_CONNECTED) {24printf("Joystick %d was connected: %s\n", joy, glfwGetJoystickName(joy));25joy_connected = joy; // use the most recently connected joystick26} else if (event == GLFW_DISCONNECTED) {27printf("Joystick %d was disconnected\n", joy);28if (joy == joy_connected) joy_connected = -1;29}30}3132void main_2(void *arg) {33printf("Testing adding new gamepads\n");34emscripten_run_script("window.addNewGamepad('Pad Thai', 4, 16)");35emscripten_run_script("window.addNewGamepad('Pad Kee Mao', 0, 4)");36// Check that the joysticks exist properly.37assert(glfwJoystickPresent(GLFW_JOYSTICK_1));38assert(glfwJoystickPresent(GLFW_JOYSTICK_2));3940assert(strcmp(glfwGetJoystickName(GLFW_JOYSTICK_1), "Pad Thai") == 0);41assert(strcmp(glfwGetJoystickName(GLFW_JOYSTICK_2), "Pad Kee Mao") == 0);4243int axes_count = 0;44int buttons_count = 0;4546glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);47glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);48assert(axes_count == 4);49assert(buttons_count == 16);5051glfwGetJoystickAxes(GLFW_JOYSTICK_2, &axes_count);52glfwGetJoystickButtons(GLFW_JOYSTICK_2, &buttons_count);53assert(axes_count == 0);54assert(buttons_count == 4);5556// Buttons57printf("Testing buttons\n");58const unsigned char *buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);59assert(buttons_count == 16);60assert(buttons[0] == 0);61emscripten_run_script("window.simulateGamepadButtonDown(0, 1)");62buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);63assert(buttons_count == 16);64assert(buttons[1] == 1);6566emscripten_run_script("window.simulateGamepadButtonUp(0, 1)");67buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);68assert(buttons_count == 16);69assert(buttons[1] == 0);707172emscripten_run_script("window.simulateGamepadButtonDown(1, 0)");73buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_2, &buttons_count);74assert(buttons_count == 4);75assert(buttons[0] == 1);7677emscripten_run_script("window.simulateGamepadButtonUp(1, 0)");78buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_2, &buttons_count);79assert(buttons_count == 4);80assert(buttons[1] == 0);8182// Joystick wiggling83printf("Testing joystick axes\n");84emscripten_run_script("window.simulateAxisMotion(0, 0, 1)");85const float *axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);86assert(axes_count == 4);87assert(axes[0] == 1);8889emscripten_run_script("window.simulateAxisMotion(0, 0, 0)");90axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);91assert(axes_count == 4);92assert(axes[0] == 0);9394emscripten_run_script("window.simulateAxisMotion(0, 1, -1)");95axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);96assert(axes_count == 4);97assert(axes[1] == -1);9899// End test.100printf("Test passed!\n");101emscripten_force_exit(0);102}103104int main() {105if (!glfwInit()) {106printf("Could not create window. Test failed.\n");107return 1;108}109glfwWindowHint(GLFW_RESIZABLE , 1);110g_window = glfwCreateWindow(600, 450, "GLFW joystick test", NULL, NULL);111if (!g_window) {112printf("Could not create window. Test failed.\n");113glfwTerminate();114return 1;115}116glfwMakeContextCurrent(g_window);117glfwSetJoystickCallback(joystick_callback);118119emscripten_async_call(main_2, NULL, 3000); // avoid startup delays and intermittent errors120121#ifndef __EMSCRIPTEN__122while (!glfwWindowShouldClose(window)) {123glfwPollEvents();124}125#endif126127glfwTerminate();128return 99;129}130131132