Skip to content

Commit

Permalink
Added ability to work for other emulators and unnecessary optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
IamSanjid committed Nov 14, 2022
1 parent 2c9259e commit 338e744
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 135 deletions.
24 changes: 13 additions & 11 deletions Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,30 @@ void Application::Reconfig(Config* new_conf)
else
Config::Current(Config::Default());

Config::Current()->TOGGLE_KEY = glfwGetKeyScancode(Config::Current()->TOGGLE_KEY);
Config::Current()->TOGGLE_MODIFIER = glfwGetKeyScancode(Config::Current()->TOGGLE_MODIFIER);
Native::GetInstance()->RegisterHotKey(Config::Current()->TOGGLE_KEY, Config::Current()->TOGGLE_MODIFIER);
auto current_config = Config::Current();

current_config->TOGGLE_KEY = glfwGetKeyScancode(current_config->TOGGLE_KEY);
current_config->TOGGLE_MODIFIER = glfwGetKeyScancode(current_config->TOGGLE_MODIFIER);
Native::GetInstance()->RegisterHotKey(current_config->TOGGLE_KEY, current_config->TOGGLE_MODIFIER);

for (auto i = 0; i < 4; i++)
{
Config::Current()->RIGHT_STICK_KEYS[i] = glfwGetKeyScancode(Config::Current()->RIGHT_STICK_KEYS[i]);
current_config->RIGHT_STICK_KEYS[i] = glfwGetKeyScancode(current_config->RIGHT_STICK_KEYS[i]);
}

if (Config::Current()->LEFT_MOUSE_KEY)
Config::Current()->LEFT_MOUSE_KEY = glfwGetKeyScancode(Config::Current()->LEFT_MOUSE_KEY);
if (Config::Current()->RIGHT_MOUSE_KEY)
Config::Current()->RIGHT_MOUSE_KEY = glfwGetKeyScancode(Config::Current()->RIGHT_MOUSE_KEY);
if (Config::Current()->MIDDLE_MOUSE_KEY)
Config::Current()->MIDDLE_MOUSE_KEY = glfwGetKeyScancode(Config::Current()->MIDDLE_MOUSE_KEY);
if (current_config->LEFT_MOUSE_KEY)
current_config->LEFT_MOUSE_KEY = glfwGetKeyScancode(current_config->LEFT_MOUSE_KEY);
if (current_config->RIGHT_MOUSE_KEY)
current_config->RIGHT_MOUSE_KEY = glfwGetKeyScancode(current_config->RIGHT_MOUSE_KEY);
if (current_config->MIDDLE_MOUSE_KEY)
current_config->MIDDLE_MOUSE_KEY = glfwGetKeyScancode(current_config->MIDDLE_MOUSE_KEY);
}

void Application::TogglePanning()
{
if (!panning_started_)
{
if (Config::Current()->AUTO_FOCUS_RYU && !Native::GetInstance()->SetFocusOnWindow(Config::Current()->TARGET_NAME))
if (Config::Current()->AUTO_FOCUS_EMU_WINDOW && !Native::GetInstance()->SetFocusOnWindow(Config::Current()->TARGET_NAME))
return;
panning_started_ = true;
glfwIconifyWindow(main_window_);
Expand Down
66 changes: 65 additions & 1 deletion Config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Config.h"
#include <GLFW/glfw3.h>
#include <iniparser.hpp>

Config* Config::default_instance_ = nullptr;

Expand All @@ -23,7 +24,7 @@ Config::Config()
SENSITIVITY = 10.0f;

HIDE_MOUSE = true;
AUTO_FOCUS_RYU = true;
AUTO_FOCUS_EMU_WINDOW = true;
BIND_MOUSE_BUTTON = true;
}

Expand Down Expand Up @@ -56,3 +57,66 @@ Config* Config::Current(Config* change)
}
return current_instance_;
}

Config* Config::LoadNew(const std::string& file)
{
INI::File ft;
if (!ft.Load(file))
return nullptr;

auto new_conf = Config::New();

new_conf->TARGET_NAME = ft.GetValue("TargetEmulatorWindow", Config::Current()->TARGET_NAME).AsString();

new_conf->SENSITIVITY = static_cast<float>(ft.GetValue("Sensitivity", Config::Current()->SENSITIVITY).AsDouble());
new_conf->CAMERA_UPDATE_TIME = static_cast<float>(ft.GetValue("CameraUpdateTime", Config::Current()->CAMERA_UPDATE_TIME).AsDouble());

new_conf->HIDE_MOUSE = ft.GetValue("HideMouse", Config::Current()->HIDE_MOUSE).AsBool();
new_conf->AUTO_FOCUS_EMU_WINDOW = ft.GetValue("AutoFocusEmuWindow", Config::Current()->AUTO_FOCUS_EMU_WINDOW).AsBool();
new_conf->BIND_MOUSE_BUTTON = ft.GetValue("BindMouseButton", Config::Current()->BIND_MOUSE_BUTTON).AsBool();

for (int i = 0; i < 4; i++)
{
std::string key = "RightStick:" + std::to_string(i);
new_conf->RIGHT_STICK_KEYS[i] = ft.GetValue(key, Config::Current()->RIGHT_STICK_KEYS[i]).AsInt();
}

new_conf->LEFT_MOUSE_KEY = ft.GetValue("Mouse:LeftButton", Config::Current()->LEFT_MOUSE_KEY).AsInt();
new_conf->RIGHT_MOUSE_KEY = ft.GetValue("Mouse:RightButton", Config::Current()->RIGHT_MOUSE_KEY).AsInt();
new_conf->MIDDLE_MOUSE_KEY = ft.GetValue("Mouse:MiddleButton", Config::Current()->MIDDLE_MOUSE_KEY).AsInt();

new_conf->TOGGLE_MODIFIER = ft.GetValue("PanningToggle:Modifier", Config::Current()->TOGGLE_MODIFIER).AsInt();
new_conf->TOGGLE_KEY = ft.GetValue("PanningToggle:Key", Config::Current()->TOGGLE_KEY).AsInt();

return new_conf;
}

void Config::Save(const std::string& file)
{
INI::File ft;
ft.Load(file);

ft.SetValue("TargetEmulatorWindow", this->TARGET_NAME);

ft.SetValue("Sensitivity", this->SENSITIVITY);
ft.SetValue("CameraUpdateTime", this->CAMERA_UPDATE_TIME);

ft.SetValue("HideMouse", this->HIDE_MOUSE);
ft.SetValue("AutoFocusEmuWindow", this->AUTO_FOCUS_EMU_WINDOW);
ft.SetValue("BindMouseButton", this->BIND_MOUSE_BUTTON);

for (int i = 0; i < 4; i++)
{
std::string key = "RightStick:" + std::to_string(i);
ft.SetValue(key, this->RIGHT_STICK_KEYS[i]);
}

ft.SetValue("Mouse:LeftButton", this->LEFT_MOUSE_KEY);
ft.SetValue("Mouse:RightButton", this->RIGHT_MOUSE_KEY);
ft.SetValue("Mouse:MiddleButton", this->MIDDLE_MOUSE_KEY);

ft.SetValue("PanningToggle:Modifier", this->TOGGLE_MODIFIER);
ft.SetValue("PanningToggle:Key", this->TOGGLE_KEY);

ft.Save(file);
}
11 changes: 8 additions & 3 deletions Config.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#pragma once
#include <cstdint>
#include <string>

class Config
{
public:
static Config* New();
static Config* Default();
static Config* Current(Config* change = nullptr);
static Config* LoadNew(const std::string& file);

void Save(const std::string& file);

const char* NAME = "RMB";
const char* TARGET_NAME = "Ryujinx";
const uint32_t WIDTH = 420;
const uint32_t HEIGHT = 500;
const uint32_t HEIGHT = 540;

std::string TARGET_NAME = "Ryujinx";

uint32_t TOGGLE_MODIFIER;
uint32_t TOGGLE_KEY;
Expand All @@ -25,7 +30,7 @@ class Config
float SENSITIVITY;

bool HIDE_MOUSE;
bool AUTO_FOCUS_RYU;
bool AUTO_FOCUS_EMU_WINDOW;
bool BIND_MOUSE_BUTTON;

private:
Expand Down
59 changes: 27 additions & 32 deletions npad_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class StickInputHandler final : public InputHandler

timeout_queue_.Push({ button, time });
timeout_queue_.Push({ opposite_button, 0.f });

}

void OnStop() override
Expand Down Expand Up @@ -140,22 +139,21 @@ class ButtonInputHandler final : public InputHandler
{
if (stopped_)
{
std::lock_guard<std::mutex> lock{ mutex };
for (auto& it : pressed_buttons_)
{
if (it.second)
{
Native::GetInstance()->SendKeysUp((uint32_t*)&it.first, 1);
}
}
std::lock_guard<std::mutex> lock{ mutex };
pressed_buttons_.clear();
stopped_ = false;
}
}

void OnChange(int index, const InputStatus& status) override
{
std::lock_guard<std::mutex> lock{ mutex };
if (stopped_)
return;

Expand All @@ -167,6 +165,7 @@ class ButtonInputHandler final : public InputHandler
{
Native::GetInstance()->SendKeysDown((uint32_t*)&index, 1);
}
std::lock_guard<std::mutex> lock{ mutex };
pressed_buttons_[index] = status.reset;
}

Expand Down Expand Up @@ -214,43 +213,39 @@ void NpadController::SetStick(float raw_x, float raw_y)
#endif
last_raw_x_ = raw_x;
last_raw_y_ = raw_y;
OnChange();
}
}

void NpadController::SetButton(int button, int value)
{
input_handlers_[ButtonInputHandlerIndex]->OnChange(button, { value == 0, 0 });
}

void NpadController::OnChange()
{
const auto camera_update = Config::Current()->CAMERA_UPDATE_TIME;
SanatizeAxes(last_raw_x_, last_raw_y_, true);

const auto camera_update = Config::Current()->CAMERA_UPDATE_TIME;
SanatizeAxes(last_raw_x_, last_raw_y_, true);

auto last_x = axes_.x, last_y = axes_.y;
auto last_x = axes_.x, last_y = axes_.y;

axes_.x = static_cast<int>(last_x_ * camera_update);
axes_.y = static_cast<int>(last_y_ * camera_update);
axes_.x = static_cast<int>(last_x_ * camera_update);
axes_.y = static_cast<int>(last_y_ * camera_update);

InputStatus x_status{ axes_.x == 0, (axes_.x == 0 ? last_x : axes_.x) };
InputStatus y_status{ axes_.y == 0, (axes_.y == 0 ? last_y : axes_.y) };
InputStatus x_status{ axes_.x == 0, (axes_.x == 0 ? last_x : axes_.x) };
InputStatus y_status{ axes_.y == 0, (axes_.y == 0 ? last_y : axes_.y) };

input_handlers_[StickInputHandlerIndex]->OnChange(x_axis, x_status);
input_handlers_[StickInputHandlerIndex]->OnChange(y_axis, y_status);
input_handlers_[StickInputHandlerIndex]->OnChange(x_axis, x_status);
input_handlers_[StickInputHandlerIndex]->OnChange(y_axis, y_status);

axes_.right = last_x_ > threshold;
axes_.left = last_x_ < -threshold;
axes_.up = last_y_ > threshold;
axes_.down = last_y_ < -threshold;
axes_.right = last_x_ > threshold;
axes_.left = last_x_ < -threshold;
axes_.up = last_y_ > threshold;
axes_.down = last_y_ < -threshold;

#if _DEBUG
if (axes_.x != 0 || axes_.y != 0)
{
fprintf(stdout, "axes_change: %d, %d - actual_value: %f, %f\n", axes_.x, axes_.y, last_x_, last_y_);
fprintf(stdout, "right: %d left: %d up: %d down: %d\n", axes_.right, axes_.left, axes_.up, axes_.down);
}
if (axes_.x != 0 || axes_.y != 0)
{
fprintf(stdout, "axes_change: %d, %d - actual_value: %f, %f\n", axes_.x, axes_.y, last_x_, last_y_);
fprintf(stdout, "right: %d left: %d up: %d down: %d\n", axes_.right, axes_.left, axes_.up, axes_.down);
}
#endif
}
}

void NpadController::SetButton(int button, int value)
{
input_handlers_[ButtonInputHandlerIndex]->OnChange(button, { value == 0, 0 });
}

void NpadController::ClearState()
Expand Down
2 changes: 0 additions & 2 deletions npad_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ class NpadController
void ClearState();

private:

void UpdateThread(std::stop_token stop_token);
void OnChange();
void SanatizeAxes(float raw_x, float raw_y, bool clamp_value);

std::vector<InputHandler*> input_handlers_;
Expand Down
Loading

0 comments on commit 338e744

Please sign in to comment.