// Copyright 2021, Mateo de Mayo. // SPDX-License-Identifier: BSL-1.0 /*! * @file * @brief Connection between user-generated SDL events and qwerty devices. * @author Mateo de Mayo * @ingroup drv_qwerty */ // @note: This file will process more than one device, that's // why some comments reference multiple devices #include "qwerty_device.h" #include "xrt/xrt_device.h" #include static void find_qwerty_devices(struct xrt_device **xdevs, size_t num_xdevs, struct xrt_device **xd_hmd) { for (size_t i = 0; i < num_xdevs; i++) { if (xdevs[i] == NULL) { continue; } if (strcmp(xdevs[i]->str, QWERTY_HMD_STR) == 0) { *xd_hmd = xdevs[i]; } } } void qwerty_process_event(struct xrt_device **xdevs, size_t num_xdevs, SDL_Event event) { static struct xrt_device *xd_hmd = NULL; // We can cache the devices as they don't get destroyed during runtime static bool cached = false; if (!cached) { find_qwerty_devices(xdevs, num_xdevs, &xd_hmd); cached = true; } bool using_qhmd = xd_hmd != NULL; struct qwerty_device *qd_hmd = using_qhmd ? qwerty_device(xd_hmd) : NULL; if (!qd_hmd) { return; } // clang-format off // Determine focused device struct qwerty_device *qdev = qd_hmd; // WASDQE Movement if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_a) qwerty_press_left(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_a) qwerty_release_left(qdev); if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_d) qwerty_press_right(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_d) qwerty_release_right(qdev); if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_w) qwerty_press_forward(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_w) qwerty_release_forward(qdev); if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_s) qwerty_press_backward(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_s) qwerty_release_backward(qdev); if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_e) qwerty_press_up(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_e) qwerty_release_up(qdev); if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_q) qwerty_press_down(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_q) qwerty_release_down(qdev); // Arrow keys rotation if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_LEFT) qwerty_press_look_left(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_LEFT) qwerty_release_look_left(qdev); if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RIGHT) qwerty_press_look_right(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_RIGHT) qwerty_release_look_right(qdev); if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_UP) qwerty_press_look_up(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_UP) qwerty_release_look_up(qdev); if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_DOWN) qwerty_press_look_down(qdev); if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_DOWN) qwerty_release_look_down(qdev); // clang-format on }