emu_window.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <android/native_window_jni.h>
  2. #include "common/logging/log.h"
  3. #include "input_common/drivers/touch_screen.h"
  4. #include "input_common/drivers/virtual_gamepad.h"
  5. #include "input_common/main.h"
  6. #include "jni/emu_window/emu_window.h"
  7. void EmuWindow_Android::OnSurfaceChanged(ANativeWindow* surface) {
  8. render_window = surface;
  9. }
  10. void EmuWindow_Android::OnTouchPressed(int id, float x, float y) {
  11. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  12. input_subsystem->GetTouchScreen()->TouchPressed(touch_x, touch_y, id);
  13. }
  14. void EmuWindow_Android::OnTouchMoved(int id, float x, float y) {
  15. const auto [touch_x, touch_y] = MapToTouchScreen(x, y);
  16. input_subsystem->GetTouchScreen()->TouchMoved(touch_x, touch_y, id);
  17. }
  18. void EmuWindow_Android::OnTouchReleased(int id) {
  19. input_subsystem->GetTouchScreen()->TouchReleased(id);
  20. }
  21. void EmuWindow_Android::OnGamepadButtonEvent(int player_index, int button_id, bool pressed) {
  22. input_subsystem->GetVirtualGamepad()->SetButtonState(player_index, button_id, pressed);
  23. }
  24. void EmuWindow_Android::OnGamepadJoystickEvent(int player_index, int stick_id, float x, float y) {
  25. input_subsystem->GetVirtualGamepad()->SetStickPosition(player_index, stick_id, x, y);
  26. }
  27. void EmuWindow_Android::OnGamepadMotionEvent(int player_index, u64 delta_timestamp, float gyro_x,
  28. float gyro_y, float gyro_z, float accel_x,
  29. float accel_y, float accel_z) {
  30. input_subsystem->GetVirtualGamepad()->SetMotionState(player_index, delta_timestamp, gyro_x,
  31. gyro_y, gyro_z, accel_x, accel_y, accel_z);
  32. }
  33. EmuWindow_Android::EmuWindow_Android(InputCommon::InputSubsystem* input_subsystem_,
  34. ANativeWindow* surface_)
  35. : input_subsystem{input_subsystem_} {
  36. LOG_INFO(Frontend, "initializing");
  37. if (!surface_) {
  38. LOG_CRITICAL(Frontend, "surface is nullptr");
  39. return;
  40. }
  41. window_width = ANativeWindow_getWidth(surface_);
  42. window_height = ANativeWindow_getHeight(surface_);
  43. // Ensures that we emulate with the correct aspect ratio.
  44. UpdateCurrentFramebufferLayout(window_width, window_height);
  45. host_window = surface_;
  46. window_info.type = Core::Frontend::WindowSystemType::Android;
  47. window_info.render_surface = reinterpret_cast<void*>(host_window);
  48. input_subsystem->Initialize();
  49. }
  50. EmuWindow_Android::~EmuWindow_Android() {
  51. input_subsystem->Shutdown();
  52. }