emu_window_glfw.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <GLFW/glfw3.h>
  5. #include "common/common.h"
  6. #include "video_core/video_core.h"
  7. #include "core/settings.h"
  8. #include "citra/emu_window/emu_window_glfw.h"
  9. EmuWindow_GLFW* EmuWindow_GLFW::GetEmuWindow(GLFWwindow* win) {
  10. return static_cast<EmuWindow_GLFW*>(glfwGetWindowUserPointer(win));
  11. }
  12. /// Called by GLFW when a key event occurs
  13. void EmuWindow_GLFW::OnKeyEvent(GLFWwindow* win, int key, int scancode, int action, int mods) {
  14. int keyboard_id = GetEmuWindow(win)->keyboard_id;
  15. if (action == GLFW_PRESS) {
  16. EmuWindow::KeyPressed({key, keyboard_id});
  17. } else if (action == GLFW_RELEASE) {
  18. EmuWindow::KeyReleased({key, keyboard_id});
  19. }
  20. HID_User::PadUpdateComplete();
  21. }
  22. /// Whether the window is still open, and a close request hasn't yet been sent
  23. const bool EmuWindow_GLFW::IsOpen() {
  24. return glfwWindowShouldClose(m_render_window) == 0;
  25. }
  26. void EmuWindow_GLFW::OnFramebufferResizeEvent(GLFWwindow* win, int width, int height) {
  27. _dbg_assert_(Frontend, width > 0);
  28. _dbg_assert_(Frontend, height > 0);
  29. GetEmuWindow(win)->NotifyFramebufferSizeChanged(std::pair<unsigned,unsigned>(width, height));
  30. }
  31. void EmuWindow_GLFW::OnClientAreaResizeEvent(GLFWwindow* win, int width, int height) {
  32. _dbg_assert_(Frontend, width > 0);
  33. _dbg_assert_(Frontend, height > 0);
  34. // NOTE: GLFW provides no proper way to set a minimal window size.
  35. // Hence, we just ignore the corresponding EmuWindow hint.
  36. GetEmuWindow(win)->NotifyClientAreaSizeChanged(std::pair<unsigned,unsigned>(width, height));
  37. }
  38. /// EmuWindow_GLFW constructor
  39. EmuWindow_GLFW::EmuWindow_GLFW() {
  40. keyboard_id = KeyMap::NewDeviceId();
  41. ReloadSetKeymaps();
  42. glfwSetErrorCallback([](int error, const char *desc){
  43. LOG_ERROR(Frontend, "GLFW 0x%08x: %s", error, desc);
  44. });
  45. // Initialize the window
  46. if(glfwInit() != GL_TRUE) {
  47. LOG_CRITICAL(Frontend, "Failed to initialize GLFW! Exiting...");
  48. exit(1);
  49. }
  50. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  51. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  52. // GLFW on OSX requires these window hints to be set to create a 3.2+ GL context.
  53. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  54. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  55. std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
  56. m_render_window = glfwCreateWindow(VideoCore::kScreenTopWidth,
  57. (VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight),
  58. window_title.c_str(), nullptr, nullptr);
  59. if (m_render_window == nullptr) {
  60. LOG_CRITICAL(Frontend, "Failed to create GLFW window! Exiting...");
  61. exit(1);
  62. }
  63. glfwSetWindowUserPointer(m_render_window, this);
  64. // Notify base interface about window state
  65. int width, height;
  66. glfwGetFramebufferSize(m_render_window, &width, &height);
  67. OnFramebufferResizeEvent(m_render_window, width, height);
  68. glfwGetWindowSize(m_render_window, &width, &height);
  69. OnClientAreaResizeEvent(m_render_window, width, height);
  70. // Setup callbacks
  71. glfwSetKeyCallback(m_render_window, OnKeyEvent);
  72. glfwSetFramebufferSizeCallback(m_render_window, OnFramebufferResizeEvent);
  73. glfwSetWindowSizeCallback(m_render_window, OnClientAreaResizeEvent);
  74. DoneCurrent();
  75. }
  76. /// EmuWindow_GLFW destructor
  77. EmuWindow_GLFW::~EmuWindow_GLFW() {
  78. glfwTerminate();
  79. }
  80. /// Swap buffers to display the next frame
  81. void EmuWindow_GLFW::SwapBuffers() {
  82. glfwSwapBuffers(m_render_window);
  83. }
  84. /// Polls window events
  85. void EmuWindow_GLFW::PollEvents() {
  86. glfwPollEvents();
  87. }
  88. /// Makes the GLFW OpenGL context current for the caller thread
  89. void EmuWindow_GLFW::MakeCurrent() {
  90. glfwMakeContextCurrent(m_render_window);
  91. }
  92. /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread
  93. void EmuWindow_GLFW::DoneCurrent() {
  94. glfwMakeContextCurrent(nullptr);
  95. }
  96. void EmuWindow_GLFW::ReloadSetKeymaps() {
  97. KeyMap::SetKeyMapping({Settings::values.pad_a_key, keyboard_id}, HID_User::PAD_A);
  98. KeyMap::SetKeyMapping({Settings::values.pad_b_key, keyboard_id}, HID_User::PAD_B);
  99. KeyMap::SetKeyMapping({Settings::values.pad_select_key, keyboard_id}, HID_User::PAD_SELECT);
  100. KeyMap::SetKeyMapping({Settings::values.pad_start_key, keyboard_id}, HID_User::PAD_START);
  101. KeyMap::SetKeyMapping({Settings::values.pad_dright_key, keyboard_id}, HID_User::PAD_RIGHT);
  102. KeyMap::SetKeyMapping({Settings::values.pad_dleft_key, keyboard_id}, HID_User::PAD_LEFT);
  103. KeyMap::SetKeyMapping({Settings::values.pad_dup_key, keyboard_id}, HID_User::PAD_UP);
  104. KeyMap::SetKeyMapping({Settings::values.pad_ddown_key, keyboard_id}, HID_User::PAD_DOWN);
  105. KeyMap::SetKeyMapping({Settings::values.pad_r_key, keyboard_id}, HID_User::PAD_R);
  106. KeyMap::SetKeyMapping({Settings::values.pad_l_key, keyboard_id}, HID_User::PAD_L);
  107. KeyMap::SetKeyMapping({Settings::values.pad_x_key, keyboard_id}, HID_User::PAD_X);
  108. KeyMap::SetKeyMapping({Settings::values.pad_y_key, keyboard_id}, HID_User::PAD_Y);
  109. KeyMap::SetKeyMapping({Settings::values.pad_sright_key, keyboard_id}, HID_User::PAD_CIRCLE_RIGHT);
  110. KeyMap::SetKeyMapping({Settings::values.pad_sleft_key, keyboard_id}, HID_User::PAD_CIRCLE_LEFT);
  111. KeyMap::SetKeyMapping({Settings::values.pad_sup_key, keyboard_id}, HID_User::PAD_CIRCLE_UP);
  112. KeyMap::SetKeyMapping({Settings::values.pad_sdown_key, keyboard_id}, HID_User::PAD_CIRCLE_DOWN);
  113. }
  114. void EmuWindow_GLFW::OnMinimalClientAreaChangeRequest(const std::pair<unsigned,unsigned>& minimal_size) {
  115. std::pair<int,int> current_size;
  116. glfwGetWindowSize(m_render_window, &current_size.first, &current_size.second);
  117. _dbg_assert_(Frontend, (int)minimal_size.first > 0 && (int)minimal_size.second > 0);
  118. int new_width = std::max(current_size.first, (int)minimal_size.first);
  119. int new_height = std::max(current_size.second, (int)minimal_size.second);
  120. if (current_size != std::make_pair(new_width, new_height))
  121. glfwSetWindowSize(m_render_window, new_width, new_height);
  122. }