2014-04-09 00:25:53 +00:00
|
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 05:38:14 +00:00
|
|
|
|
// Licensed under GPLv2 or any later version
|
2014-04-09 00:25:53 +00:00
|
|
|
|
// Refer to the license.txt file included.
|
2013-09-05 02:10:47 +00:00
|
|
|
|
|
2015-06-20 19:34:41 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
// Let’s use our own GL header, instead of one from GLFW.
|
2015-08-30 06:37:42 +00:00
|
|
|
|
#include <glad/glad.h>
|
2015-06-20 19:34:41 +00:00
|
|
|
|
#define GLFW_INCLUDE_NONE
|
2014-10-12 16:14:57 +00:00
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
2015-06-20 19:34:41 +00:00
|
|
|
|
#include "common/assert.h"
|
|
|
|
|
#include "common/key_map.h"
|
2015-05-06 07:06:12 +00:00
|
|
|
|
#include "common/logging/log.h"
|
2015-06-20 19:34:41 +00:00
|
|
|
|
#include "common/scm_rev.h"
|
|
|
|
|
#include "common/string_util.h"
|
2014-04-09 03:18:23 +00:00
|
|
|
|
|
2014-04-09 00:15:08 +00:00
|
|
|
|
#include "video_core/video_core.h"
|
2014-04-09 03:18:23 +00:00
|
|
|
|
|
2014-09-13 00:06:13 +00:00
|
|
|
|
#include "core/settings.h"
|
2015-06-20 19:34:41 +00:00
|
|
|
|
#include "core/hle/service/hid/hid.h"
|
2013-09-05 02:10:47 +00:00
|
|
|
|
|
2014-09-13 00:06:13 +00:00
|
|
|
|
#include "citra/emu_window/emu_window_glfw.h"
|
2014-09-04 01:12:58 +00:00
|
|
|
|
|
2014-10-12 16:14:57 +00:00
|
|
|
|
EmuWindow_GLFW* EmuWindow_GLFW::GetEmuWindow(GLFWwindow* win) {
|
|
|
|
|
return static_cast<EmuWindow_GLFW*>(glfwGetWindowUserPointer(win));
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 01:45:45 +00:00
|
|
|
|
void EmuWindow_GLFW::OnMouseButtonEvent(GLFWwindow* win, int button, int action, int mods) {
|
2015-03-08 07:19:33 +00:00
|
|
|
|
if (button == GLFW_MOUSE_BUTTON_LEFT) {
|
2015-03-09 01:45:45 +00:00
|
|
|
|
auto emu_window = GetEmuWindow(win);
|
|
|
|
|
auto layout = emu_window->GetFramebufferLayout();
|
2015-03-08 07:19:33 +00:00
|
|
|
|
double x, y;
|
2015-03-09 01:45:45 +00:00
|
|
|
|
glfwGetCursorPos(win, &x, &y);
|
2015-03-08 07:19:33 +00:00
|
|
|
|
|
2015-03-09 04:14:59 +00:00
|
|
|
|
if (action == GLFW_PRESS)
|
|
|
|
|
emu_window->TouchPressed(static_cast<unsigned>(x), static_cast<unsigned>(y));
|
|
|
|
|
else if (action == GLFW_RELEASE)
|
|
|
|
|
emu_window->TouchReleased();
|
2015-03-08 07:19:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 01:45:45 +00:00
|
|
|
|
void EmuWindow_GLFW::OnCursorPosEvent(GLFWwindow* win, double x, double y) {
|
2015-04-14 04:06:44 +00:00
|
|
|
|
GetEmuWindow(win)->TouchMoved(static_cast<unsigned>(std::max(x, 0.0)), static_cast<unsigned>(std::max(y, 0.0)));
|
2015-03-08 07:19:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-09 04:46:02 +00:00
|
|
|
|
/// Called by GLFW when a key event occurs
|
|
|
|
|
void EmuWindow_GLFW::OnKeyEvent(GLFWwindow* win, int key, int scancode, int action, int mods) {
|
2015-03-09 01:45:45 +00:00
|
|
|
|
auto emu_window = GetEmuWindow(win);
|
|
|
|
|
int keyboard_id = emu_window->keyboard_id;
|
2014-09-04 01:12:58 +00:00
|
|
|
|
|
|
|
|
|
if (action == GLFW_PRESS) {
|
2015-03-09 01:45:45 +00:00
|
|
|
|
emu_window->KeyPressed({key, keyboard_id});
|
2014-10-12 16:14:57 +00:00
|
|
|
|
} else if (action == GLFW_RELEASE) {
|
2015-03-09 04:14:59 +00:00
|
|
|
|
emu_window->KeyReleased({key, keyboard_id});
|
2014-09-04 01:12:58 +00:00
|
|
|
|
}
|
2013-09-05 02:10:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-16 01:48:02 +00:00
|
|
|
|
/// Whether the window is still open, and a close request hasn't yet been sent
|
|
|
|
|
const bool EmuWindow_GLFW::IsOpen() {
|
2014-10-19 07:53:49 +00:00
|
|
|
|
return glfwWindowShouldClose(m_render_window) == 0;
|
2014-10-16 01:48:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-12 16:14:57 +00:00
|
|
|
|
void EmuWindow_GLFW::OnFramebufferResizeEvent(GLFWwindow* win, int width, int height) {
|
2015-03-07 22:21:19 +00:00
|
|
|
|
GetEmuWindow(win)->NotifyFramebufferLayoutChanged(EmuWindow::FramebufferLayout::DefaultScreenLayout(width, height));
|
2014-10-12 16:14:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EmuWindow_GLFW::OnClientAreaResizeEvent(GLFWwindow* win, int width, int height) {
|
2014-11-13 19:47:46 +00:00
|
|
|
|
// NOTE: GLFW provides no proper way to set a minimal window size.
|
|
|
|
|
// Hence, we just ignore the corresponding EmuWindow hint.
|
2015-03-07 22:21:19 +00:00
|
|
|
|
OnFramebufferResizeEvent(win, width, height);
|
2014-08-30 05:23:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-05 02:10:47 +00:00
|
|
|
|
/// EmuWindow_GLFW constructor
|
|
|
|
|
EmuWindow_GLFW::EmuWindow_GLFW() {
|
2014-09-09 04:46:02 +00:00
|
|
|
|
keyboard_id = KeyMap::NewDeviceId();
|
|
|
|
|
|
2014-09-13 00:06:13 +00:00
|
|
|
|
ReloadSetKeymaps();
|
2014-09-04 01:12:58 +00:00
|
|
|
|
|
2014-11-28 23:35:57 +00:00
|
|
|
|
glfwSetErrorCallback([](int error, const char *desc){
|
2014-12-06 01:53:49 +00:00
|
|
|
|
LOG_ERROR(Frontend, "GLFW 0x%08x: %s", error, desc);
|
2014-11-28 23:35:57 +00:00
|
|
|
|
});
|
|
|
|
|
|
2013-09-05 02:10:47 +00:00
|
|
|
|
// Initialize the window
|
|
|
|
|
if(glfwInit() != GL_TRUE) {
|
2014-12-06 01:53:49 +00:00
|
|
|
|
LOG_CRITICAL(Frontend, "Failed to initialize GLFW! Exiting...");
|
2013-09-05 02:10:47 +00:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
2015-08-30 07:29:03 +00:00
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
2014-06-09 23:51:09 +00:00
|
|
|
|
// GLFW on OSX requires these window hints to be set to create a 3.2+ GL context.
|
2014-05-01 03:12:01 +00:00
|
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
2014-10-12 16:14:57 +00:00
|
|
|
|
|
2014-11-13 17:17:39 +00:00
|
|
|
|
std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
|
|
|
|
|
m_render_window = glfwCreateWindow(VideoCore::kScreenTopWidth,
|
|
|
|
|
(VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight),
|
2014-12-03 18:57:57 +00:00
|
|
|
|
window_title.c_str(), nullptr, nullptr);
|
2013-09-05 02:10:47 +00:00
|
|
|
|
|
2014-12-03 18:57:57 +00:00
|
|
|
|
if (m_render_window == nullptr) {
|
2014-12-06 01:53:49 +00:00
|
|
|
|
LOG_CRITICAL(Frontend, "Failed to create GLFW window! Exiting...");
|
2014-05-01 03:12:01 +00:00
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2014-10-12 16:14:57 +00:00
|
|
|
|
|
2014-04-09 03:18:23 +00:00
|
|
|
|
glfwSetWindowUserPointer(m_render_window, this);
|
2013-09-05 02:10:47 +00:00
|
|
|
|
|
2014-10-12 16:14:57 +00:00
|
|
|
|
// Notify base interface about window state
|
|
|
|
|
int width, height;
|
|
|
|
|
glfwGetFramebufferSize(m_render_window, &width, &height);
|
|
|
|
|
OnFramebufferResizeEvent(m_render_window, width, height);
|
|
|
|
|
|
|
|
|
|
glfwGetWindowSize(m_render_window, &width, &height);
|
|
|
|
|
OnClientAreaResizeEvent(m_render_window, width, height);
|
|
|
|
|
|
|
|
|
|
// Setup callbacks
|
|
|
|
|
glfwSetKeyCallback(m_render_window, OnKeyEvent);
|
2015-03-08 07:19:33 +00:00
|
|
|
|
glfwSetMouseButtonCallback(m_render_window, OnMouseButtonEvent);
|
|
|
|
|
glfwSetCursorPosCallback(m_render_window, OnCursorPosEvent);
|
2014-10-12 16:14:57 +00:00
|
|
|
|
glfwSetFramebufferSizeCallback(m_render_window, OnFramebufferResizeEvent);
|
|
|
|
|
glfwSetWindowSizeCallback(m_render_window, OnClientAreaResizeEvent);
|
2014-08-30 05:23:12 +00:00
|
|
|
|
|
2013-09-05 02:10:47 +00:00
|
|
|
|
DoneCurrent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// EmuWindow_GLFW destructor
|
|
|
|
|
EmuWindow_GLFW::~EmuWindow_GLFW() {
|
|
|
|
|
glfwTerminate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Swap buffers to display the next frame
|
|
|
|
|
void EmuWindow_GLFW::SwapBuffers() {
|
2014-04-09 03:18:23 +00:00
|
|
|
|
glfwSwapBuffers(m_render_window);
|
2013-09-05 02:10:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Polls window events
|
|
|
|
|
void EmuWindow_GLFW::PollEvents() {
|
|
|
|
|
glfwPollEvents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Makes the GLFW OpenGL context current for the caller thread
|
|
|
|
|
void EmuWindow_GLFW::MakeCurrent() {
|
2014-04-09 03:18:23 +00:00
|
|
|
|
glfwMakeContextCurrent(m_render_window);
|
2013-09-05 02:10:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Releases (dunno if this is the "right" word) the GLFW context from the caller thread
|
|
|
|
|
void EmuWindow_GLFW::DoneCurrent() {
|
2014-12-03 18:57:57 +00:00
|
|
|
|
glfwMakeContextCurrent(nullptr);
|
2013-09-05 02:10:47 +00:00
|
|
|
|
}
|
2014-09-13 00:06:13 +00:00
|
|
|
|
|
|
|
|
|
void EmuWindow_GLFW::ReloadSetKeymaps() {
|
2015-06-20 03:34:45 +00:00
|
|
|
|
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
|
|
|
|
|
KeyMap::SetKeyMapping({Settings::values.input_mappings[Settings::NativeInput::All[i]], keyboard_id}, Service::HID::pad_mapping[i]);
|
|
|
|
|
}
|
2014-09-13 00:06:13 +00:00
|
|
|
|
}
|
2014-10-12 20:46:33 +00:00
|
|
|
|
|
|
|
|
|
void EmuWindow_GLFW::OnMinimalClientAreaChangeRequest(const std::pair<unsigned,unsigned>& minimal_size) {
|
|
|
|
|
std::pair<int,int> current_size;
|
|
|
|
|
glfwGetWindowSize(m_render_window, ¤t_size.first, ¤t_size.second);
|
|
|
|
|
|
2015-01-21 01:16:47 +00:00
|
|
|
|
DEBUG_ASSERT((int)minimal_size.first > 0 && (int)minimal_size.second > 0);
|
2014-10-12 20:46:33 +00:00
|
|
|
|
int new_width = std::max(current_size.first, (int)minimal_size.first);
|
|
|
|
|
int new_height = std::max(current_size.second, (int)minimal_size.second);
|
|
|
|
|
|
|
|
|
|
if (current_size != std::make_pair(new_width, new_height))
|
|
|
|
|
glfwSetWindowSize(m_render_window, new_width, new_height);
|
|
|
|
|
}
|