emu_window_glfw.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <string>
  7. // Let’s use our own GL header, instead of one from GLFW.
  8. #include "video_core/renderer_opengl/generated/gl_3_2_core.h"
  9. #define GLFW_INCLUDE_NONE
  10. #include <GLFW/glfw3.h>
  11. #include "common/assert.h"
  12. #include "common/key_map.h"
  13. #include "common/logging/log.h"
  14. #include "common/scm_rev.h"
  15. #include "common/string_util.h"
  16. #include "video_core/video_core.h"
  17. #include "core/settings.h"
  18. #include "core/hle/service/hid/hid.h"
  19. #include "citra/emu_window/emu_window_glfw.h"
  20. EmuWindow_GLFW* EmuWindow_GLFW::GetEmuWindow(GLFWwindow* win) {
  21. return static_cast<EmuWindow_GLFW*>(glfwGetWindowUserPointer(win));
  22. }
  23. void EmuWindow_GLFW::OnMouseButtonEvent(GLFWwindow* win, int button, int action, int mods) {
  24. if (button == GLFW_MOUSE_BUTTON_LEFT) {
  25. auto emu_window = GetEmuWindow(win);
  26. auto layout = emu_window->GetFramebufferLayout();
  27. double x, y;
  28. glfwGetCursorPos(win, &x, &y);
  29. if (action == GLFW_PRESS)
  30. emu_window->TouchPressed(static_cast<unsigned>(x), static_cast<unsigned>(y));
  31. else if (action == GLFW_RELEASE)
  32. emu_window->TouchReleased();
  33. }
  34. }
  35. void EmuWindow_GLFW::OnCursorPosEvent(GLFWwindow* win, double x, double y) {
  36. GetEmuWindow(win)->TouchMoved(static_cast<unsigned>(std::max(x, 0.0)), static_cast<unsigned>(std::max(y, 0.0)));
  37. }
  38. /// Called by GLFW when a key event occurs
  39. void EmuWindow_GLFW::OnKeyEvent(GLFWwindow* win, int key, int scancode, int action, int mods) {
  40. auto emu_window = GetEmuWindow(win);
  41. int keyboard_id = emu_window->keyboard_id;
  42. if (action == GLFW_PRESS) {
  43. emu_window->KeyPressed({key, keyboard_id});
  44. } else if (action == GLFW_RELEASE) {
  45. emu_window->KeyReleased({key, keyboard_id});
  46. }
  47. }
  48. /// Whether the window is still open, and a close request hasn't yet been sent
  49. const bool EmuWindow_GLFW::IsOpen() {
  50. return glfwWindowShouldClose(m_render_window) == 0;
  51. }
  52. void EmuWindow_GLFW::OnFramebufferResizeEvent(GLFWwindow* win, int width, int height) {
  53. GetEmuWindow(win)->NotifyFramebufferLayoutChanged(EmuWindow::FramebufferLayout::DefaultScreenLayout(width, height));
  54. }
  55. void EmuWindow_GLFW::OnClientAreaResizeEvent(GLFWwindow* win, int width, int height) {
  56. // NOTE: GLFW provides no proper way to set a minimal window size.
  57. // Hence, we just ignore the corresponding EmuWindow hint.
  58. OnFramebufferResizeEvent(win, width, height);
  59. }
  60. /// EmuWindow_GLFW constructor
  61. EmuWindow_GLFW::EmuWindow_GLFW() {
  62. keyboard_id = KeyMap::NewDeviceId();
  63. ReloadSetKeymaps();
  64. glfwSetErrorCallback([](int error, const char *desc){
  65. LOG_ERROR(Frontend, "GLFW 0x%08x: %s", error, desc);
  66. });
  67. // Initialize the window
  68. if(glfwInit() != GL_TRUE) {
  69. LOG_CRITICAL(Frontend, "Failed to initialize GLFW! Exiting...");
  70. exit(1);
  71. }
  72. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  73. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  74. // GLFW on OSX requires these window hints to be set to create a 3.2+ GL context.
  75. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  76. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  77. std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
  78. m_render_window = glfwCreateWindow(VideoCore::kScreenTopWidth,
  79. (VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight),
  80. window_title.c_str(), nullptr, nullptr);
  81. if (m_render_window == nullptr) {
  82. LOG_CRITICAL(Frontend, "Failed to create GLFW window! Exiting...");
  83. exit(1);
  84. }
  85. glfwSetWindowUserPointer(m_render_window, this);
  86. // Notify base interface about window state
  87. int width, height;
  88. glfwGetFramebufferSize(m_render_window, &width, &height);
  89. OnFramebufferResizeEvent(m_render_window, width, height);
  90. glfwGetWindowSize(m_render_window, &width, &height);
  91. OnClientAreaResizeEvent(m_render_window, width, height);
  92. // Setup callbacks
  93. glfwSetKeyCallback(m_render_window, OnKeyEvent);
  94. glfwSetMouseButtonCallback(m_render_window, OnMouseButtonEvent);
  95. glfwSetCursorPosCallback(m_render_window, OnCursorPosEvent);
  96. glfwSetFramebufferSizeCallback(m_render_window, OnFramebufferResizeEvent);
  97. glfwSetWindowSizeCallback(m_render_window, OnClientAreaResizeEvent);
  98. DoneCurrent();
  99. }
  100. /// EmuWindow_GLFW destructor
  101. EmuWindow_GLFW::~EmuWindow_GLFW() {
  102. glfwTerminate();
  103. }
  104. /// Swap buffers to display the next frame
  105. void EmuWindow_GLFW::SwapBuffers() {
  106. glfwSwapBuffers(m_render_window);
  107. }
  108. /// Polls window events
  109. void EmuWindow_GLFW::PollEvents() {
  110. glfwPollEvents();
  111. }
  112. /// Makes the GLFW OpenGL context current for the caller thread
  113. void EmuWindow_GLFW::MakeCurrent() {
  114. glfwMakeContextCurrent(m_render_window);
  115. }
  116. /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread
  117. void EmuWindow_GLFW::DoneCurrent() {
  118. glfwMakeContextCurrent(nullptr);
  119. }
  120. void EmuWindow_GLFW::ReloadSetKeymaps() {
  121. for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
  122. KeyMap::SetKeyMapping({Settings::values.input_mappings[Settings::NativeInput::All[i]], keyboard_id}, Service::HID::pad_mapping[i]);
  123. }
  124. }
  125. void EmuWindow_GLFW::OnMinimalClientAreaChangeRequest(const std::pair<unsigned,unsigned>& minimal_size) {
  126. std::pair<int,int> current_size;
  127. glfwGetWindowSize(m_render_window, &current_size.first, &current_size.second);
  128. DEBUG_ASSERT((int)minimal_size.first > 0 && (int)minimal_size.second > 0);
  129. int new_width = std::max(current_size.first, (int)minimal_size.first);
  130. int new_height = std::max(current_size.second, (int)minimal_size.second);
  131. if (current_size != std::make_pair(new_width, new_height))
  132. glfwSetWindowSize(m_render_window, new_width, new_height);
  133. }