-
Notifications
You must be signed in to change notification settings - Fork 935
Expand file tree
/
Copy pathHotkeySettings.cpp
More file actions
524 lines (469 loc) · 15.5 KB
/
Copy pathHotkeySettings.cpp
File metadata and controls
524 lines (469 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include "wxgui/input/HotkeySettings.h"
#include "Cafe/HW/Latte/Renderer/Renderer.h"
#include "interface/WindowSystem.h"
#include <config/ActiveSettings.h>
#include "input/InputManager.h"
#include "HotkeySettings.h"
#include "MainWindow.h"
#include <wx/app.h>
#include <wx/clipbrd.h>
#if BOOST_OS_WINDOWS
#include <ole2.h>
#endif
#if BOOST_OS_LINUX || BOOST_OS_MACOS || BOOST_OS_BSD
#include "resource/embedded/resources.h"
#endif
std::optional<fs::path> GenerateScreenshotFilename(bool isDRC)
{
fs::path screendir = ActiveSettings::GetUserDataPath("screenshots");
// build screenshot name with format Screenshot_YYYY-MM-DD_HH-MM-SS[_GamePad].png
// if the file already exists add a suffix counter (_2.png, _3.png etc)
std::time_t time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm* tm = std::localtime(&time_t);
std::string screenshotFileName = fmt::format("Screenshot_{:04}-{:02}-{:02}_{:02}-{:02}-{:02}", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
if (isDRC)
screenshotFileName.append("_GamePad");
fs::path screenshotPath;
for (sint32 i = 0; i < 999; i++)
{
screenshotPath = screendir;
if (i == 0)
screenshotPath.append(fmt::format("{}.png", screenshotFileName));
else
screenshotPath.append(fmt::format("{}_{}.png", screenshotFileName, i + 1));
std::error_code ec;
bool exists = fs::exists(screenshotPath, ec);
if (!ec && !exists)
return screenshotPath;
}
return std::nullopt;
}
bool SaveScreenshotToFile(const fs::path& imagePath, const wxImage& image)
{
std::error_code ec;
fs::create_directories(imagePath.parent_path(), ec);
if (ec)
return false;
// suspend wxWidgets logging for the lifetime this object, to prevent a message box if wxImage::SaveFile fails
wxLogNull _logNo;
return image.SaveFile(imagePath.wstring());
}
bool SaveScreenshotToClipboard(const wxImage& image)
{
static std::mutex s_clipboardMutex;
bool success = false;
s_clipboardMutex.lock();
if (wxTheClipboard->Open())
{
wxTheClipboard->SetData(new wxImageDataObject(image));
wxTheClipboard->Close();
success = true;
}
s_clipboardMutex.unlock();
return success;
}
std::optional<std::string> SaveScreenshot(std::vector<uint8> data, int width, int height, bool mainWindow)
{
#if BOOST_OS_WINDOWS
// on Windows wxWidgets uses OLE API for the clipboard
// to make this work we need to call OleInitialize() on the same thread
OleInitialize(nullptr);
#endif
bool save_screenshot = GetWxGUIConfig().save_screenshot;
wxImage image(width, height, data.data(), true);
if (mainWindow)
{
if (SaveScreenshotToClipboard(image))
{
if (!save_screenshot)
return _tr("Screenshot saved to clipboard");
}
else
{
return _tr("Failed to open clipboard");
}
}
if (save_screenshot)
{
// GenerateScreenshotFilename takes isDRC, which is the opposite of mainWindow. Passing
// mainWindow directly gave TV captures the _GamePad suffix and GamePad captures none
auto imagePath = GenerateScreenshotFilename(!mainWindow);
if (imagePath.has_value() && SaveScreenshotToFile(imagePath.value(), image))
{
if (mainWindow)
return _tr("Screenshot saved");
}
else
{
return _tr("Failed to save screenshot to file");
}
}
return std::nullopt;
}
extern WindowSystem::WindowInfo g_window_info;
std::unordered_map<sHotkeyCfg*, std::function<void(void)>> HotkeySettings::s_cfgHotkeyToFuncMap;
struct HotkeyEntry
{
std::unique_ptr<wxStaticText> name;
std::unique_ptr<wxButton> keyInput;
std::unique_ptr<wxButton> controllerInput;
sHotkeyCfg& hotkey;
HotkeyEntry(wxStaticText* name, wxButton* keyInput, wxButton* controllerInput, sHotkeyCfg& hotkey)
: name(name), keyInput(keyInput), controllerInput(controllerInput), hotkey(hotkey)
{
keyInput->SetClientData(&hotkey);
controllerInput->SetClientData(&hotkey);
}
};
HotkeySettings::HotkeySettings(wxWindow* parent)
: wxFrame(parent, wxID_ANY, _("Hotkey Settings"))
{
SetIcon(wxICON(X_HOTKEY_SETTINGS));
m_sizer = new wxFlexGridSizer(0, 3, 5, 5);
m_sizer->AddGrowableCol(1);
m_sizer->AddGrowableCol(2);
m_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_THEME);
m_panel->SetSizer(m_sizer);
Center();
SetActiveController();
CreateColumnHeaders();
/* global modifier */
CreateHotkeyRow(_tr("Hotkey modifier"), s_cfgHotkeys.modifiers);
m_hotkeys.at(0).keyInput->Hide();
/* hotkeys */
CreateHotkeyRow(_tr("Toggle fullscreen"), s_cfgHotkeys.toggleFullscreen);
CreateHotkeyRow(_tr("Take screenshot"), s_cfgHotkeys.takeScreenshot);
CreateHotkeyRow(_tr("Toggle fast-forward"), s_cfgHotkeys.toggleFastForward);
#ifdef CEMU_DEBUG_ASSERT
CreateHotkeyRow(_tr("End emulation"), s_cfgHotkeys.endEmulation);
#endif
CreateHotkeyRow(_tr("Exit application"), s_cfgHotkeys.exitApplication);
m_controllerTimer = new wxTimer(this);
Bind(wxEVT_TIMER, &HotkeySettings::OnControllerTimer, this);
m_sizer->SetSizeHints(this);
}
HotkeySettings::~HotkeySettings()
{
m_controllerTimer->Stop();
if (m_needToSave)
{
GetConfigHandle().Save();
}
}
void HotkeySettings::Init(MainWindow* mainWindowFrame)
{
s_cfgHotkeyToFuncMap.insert({
{&s_cfgHotkeys.toggleFullscreen, [](void) {
s_mainWindow->SetFullScreen(!s_mainWindow->IsFullScreen());
}},
{&s_cfgHotkeys.toggleFullscreenAlt, [](void) {
s_mainWindow->SetFullScreen(!s_mainWindow->IsFullScreen());
}},
{&s_cfgHotkeys.exitFullscreen, [](void) {
s_mainWindow->SetFullScreen(false);
}},
{&s_cfgHotkeys.takeScreenshot, [](void) {
if (g_renderer)
g_renderer->RequestScreenshot(SaveScreenshot);
}},
{&s_cfgHotkeys.toggleFastForward, [](void) {
ActiveSettings::SetTimerShiftFactor((ActiveSettings::GetTimerShiftFactor() < 3) ? 3 : 1);
}},
{&s_cfgHotkeys.exitApplication, [](void) {
auto closeEvent = new wxCloseEvent{wxEVT_CLOSE_WINDOW, s_mainWindow->GetId()};
closeEvent->SetCanVeto(false);
wxQueueEvent(s_mainWindow, closeEvent);
}},
#ifdef CEMU_DEBUG_ASSERT
{&s_cfgHotkeys.endEmulation, [](void) {
wxTheApp->CallAfter([]() {
s_mainWindow->EndEmulation();
});
}},
#endif
});
s_keyboardHotkeyToFuncMap.reserve(s_cfgHotkeyToFuncMap.size());
for (const auto& [cfgHotkey, func] : s_cfgHotkeyToFuncMap)
{
auto keyboardHotkey = cfgHotkey->keyboard.raw;
if (keyboardHotkey > sHotkeyCfg::keyboardNone)
{
s_keyboardHotkeyToFuncMap[keyboardHotkey] = func;
}
auto controllerHotkey = cfgHotkey->controller;
if (controllerHotkey > sHotkeyCfg::controllerNone)
{
s_controllerHotkeyToFuncMap[controllerHotkey] = func;
}
}
s_mainWindow = mainWindowFrame;
}
void HotkeySettings::CreateColumnHeaders(void)
{
auto* emptySpace = new wxStaticText(m_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER_HORIZONTAL);
auto* keyboard = new wxStaticText(m_panel, wxID_ANY, _tr("Keyboard"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER_HORIZONTAL);
auto* controller = new wxStaticText(m_panel, wxID_ANY, _tr("Controller"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER_HORIZONTAL);
keyboard->SetFont(keyboard->GetFont().Bold().Larger());
controller->SetFont(controller->GetFont().Bold().Larger());
keyboard->SetMinSize(m_minButtonSize);
controller->SetMinSize(m_minButtonSize);
auto flags = wxSizerFlags().Expand().Border(wxTOP, 10);
m_sizer->Add(emptySpace, flags);
m_sizer->Add(keyboard, flags);
m_sizer->Add(controller, flags);
}
void HotkeySettings::CreateHotkeyRow(const wxString& label, sHotkeyCfg& cfgHotkey)
{
auto* name = new wxStaticText(m_panel, wxID_ANY, label);
auto* keyInput = new wxButton(m_panel, wxID_ANY, To_wxString(cfgHotkey.keyboard), wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS | wxBU_EXACTFIT);
auto* controllerInput = new wxButton(m_panel, wxID_ANY, To_wxString(cfgHotkey.controller), wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS | wxBU_EXACTFIT);
/* for starting input */
keyInput->Bind(wxEVT_BUTTON, &HotkeySettings::OnKeyboardHotkeyInputLeftClick, this);
controllerInput->Bind(wxEVT_BUTTON, &HotkeySettings::OnControllerHotkeyInputLeftClick, this);
/* for cancelling and clearing input */
keyInput->Bind(wxEVT_RIGHT_UP, &HotkeySettings::OnKeyboardHotkeyInputRightClick, this);
controllerInput->Bind(wxEVT_RIGHT_UP, &HotkeySettings::OnControllerHotkeyInputRightClick, this);
keyInput->SetMinSize(m_minButtonSize);
controllerInput->SetMinSize(m_minButtonSize);
const wxColour inputButtonColor = GetBackgroundColour();
keyInput->SetBackgroundColour(inputButtonColor);
controllerInput->SetBackgroundColour(inputButtonColor);
auto flags = wxSizerFlags(1).Expand().Border(wxALL, 5).CenterVertical();
m_sizer->Add(name, flags);
m_sizer->Add(keyInput, flags);
m_sizer->Add(controllerInput, flags);
m_hotkeys.emplace_back(name, keyInput, controllerInput, cfgHotkey);
}
void HotkeySettings::OnControllerTimer(wxTimerEvent& event)
{
if (m_activeController.expired())
{
m_controllerTimer->Stop();
return;
}
auto& controller = *m_activeController.lock();
auto buttons = controller.update_state().buttons;
if (!buttons.IsIdle())
{
for (const auto& newHotkey : buttons.GetButtonList())
{
m_controllerTimer->Stop();
auto* inputButton = static_cast<wxButton*>(m_controllerTimer->GetClientData());
auto& cfgHotkey = *static_cast<sHotkeyCfg*>(inputButton->GetClientData());
const auto oldHotkey = cfgHotkey.controller;
const bool isModifier = (&cfgHotkey == &s_cfgHotkeys.modifiers);
/* ignore same hotkeys and block duplicate hotkeys */
if ((newHotkey != oldHotkey) && (isModifier || (newHotkey != s_cfgHotkeys.modifiers.controller)) &&
(s_controllerHotkeyToFuncMap.find(newHotkey) == s_controllerHotkeyToFuncMap.end()))
{
m_needToSave |= true;
cfgHotkey.controller = newHotkey;
/* don't bind modifier to map */
if (!isModifier)
{
s_controllerHotkeyToFuncMap.erase(oldHotkey);
s_controllerHotkeyToFuncMap[newHotkey] = s_cfgHotkeyToFuncMap.at(&cfgHotkey);
}
}
FinalizeInput<ControllerHotkey_t>(inputButton);
return;
}
}
}
void HotkeySettings::OnKeyboardHotkeyInputLeftClick(wxCommandEvent& event)
{
auto* inputButton = static_cast<wxButton*>(event.GetEventObject());
if (m_activeInputButton)
{
/* ignore multiple clicks of the same button */
if (inputButton == m_activeInputButton) return;
RestoreInputButton<uKeyboardHotkey>();
}
inputButton->Bind(wxEVT_KEY_UP, &HotkeySettings::OnKeyUp, this);
inputButton->SetLabelText(m_editModeHotkeyText);
m_activeInputButton = inputButton;
}
void HotkeySettings::OnControllerHotkeyInputLeftClick(wxCommandEvent& event)
{
auto* inputButton = static_cast<wxButton*>(event.GetEventObject());
if (m_activeInputButton)
{
/* ignore multiple clicks of the same button */
if (inputButton == m_activeInputButton) return;
RestoreInputButton<ControllerHotkey_t>();
}
m_controllerTimer->Stop();
if (!SetActiveController())
{
return;
}
inputButton->SetLabelText(m_editModeHotkeyText);
m_controllerTimer->SetClientData(inputButton);
m_controllerTimer->Start(25);
m_activeInputButton = inputButton;
}
void HotkeySettings::OnKeyboardHotkeyInputRightClick(wxMouseEvent& event)
{
if (m_activeInputButton)
{
RestoreInputButton<uKeyboardHotkey>();
return;
}
auto* inputButton = static_cast<wxButton*>(event.GetEventObject());
auto& cfgHotkey = *static_cast<sHotkeyCfg*>(inputButton->GetClientData());
uKeyboardHotkey newHotkey{sHotkeyCfg::keyboardNone};
if (cfgHotkey.keyboard.raw != newHotkey.raw)
{
m_needToSave |= true;
s_keyboardHotkeyToFuncMap.erase(cfgHotkey.keyboard.raw);
cfgHotkey.keyboard = newHotkey;
FinalizeInput<uKeyboardHotkey>(inputButton);
}
}
void HotkeySettings::OnControllerHotkeyInputRightClick(wxMouseEvent& event)
{
if (m_activeInputButton)
{
RestoreInputButton<ControllerHotkey_t>();
return;
}
auto* inputButton = static_cast<wxButton*>(event.GetEventObject());
auto& cfgHotkey = *static_cast<sHotkeyCfg*>(inputButton->GetClientData());
ControllerHotkey_t newHotkey{sHotkeyCfg::controllerNone};
if (cfgHotkey.controller != newHotkey)
{
m_needToSave |= true;
s_controllerHotkeyToFuncMap.erase(cfgHotkey.controller);
cfgHotkey.controller = newHotkey;
FinalizeInput<ControllerHotkey_t>(inputButton);
}
}
bool HotkeySettings::SetActiveController(void)
{
auto emulatedController = InputManager::instance().get_controller(0);
if (emulatedController.use_count() <= 1)
{
return false;
}
const auto& controllers = emulatedController->get_controllers();
if (controllers.empty())
{
return false;
}
m_activeController = controllers.at(0);
return true;
}
void HotkeySettings::OnKeyUp(wxKeyEvent& event)
{
auto* inputButton = static_cast<wxButton*>(event.GetEventObject());
auto& cfgHotkey = *static_cast<sHotkeyCfg*>(inputButton->GetClientData());
if (auto keycode = event.GetKeyCode(); IsValidKeycodeUp(keycode))
{
auto oldHotkey = cfgHotkey.keyboard;
uKeyboardHotkey newHotkey{};
newHotkey.key = keycode;
newHotkey.alt = event.AltDown();
newHotkey.ctrl = event.ControlDown();
newHotkey.shift = event.ShiftDown();
if ((newHotkey.raw != oldHotkey.raw) &&
(s_keyboardHotkeyToFuncMap.find(newHotkey.raw) == s_keyboardHotkeyToFuncMap.end()))
{
m_needToSave |= true;
cfgHotkey.keyboard = newHotkey;
s_keyboardHotkeyToFuncMap.erase(oldHotkey.raw);
s_keyboardHotkeyToFuncMap[newHotkey.raw] = s_cfgHotkeyToFuncMap.at(&cfgHotkey);
}
}
FinalizeInput<uKeyboardHotkey>(inputButton);
}
template<typename T>
void HotkeySettings::FinalizeInput(wxButton* inputButton)
{
auto& cfgHotkey = *static_cast<sHotkeyCfg*>(inputButton->GetClientData());
if constexpr (std::is_same_v<T, uKeyboardHotkey>)
{
inputButton->Unbind(wxEVT_KEY_UP, &HotkeySettings::OnKeyUp, this);
inputButton->SetLabelText(To_wxString(cfgHotkey.keyboard));
}
else if constexpr (std::is_same_v<T, ControllerHotkey_t>)
{
inputButton->SetLabelText(To_wxString(cfgHotkey.controller));
}
m_activeInputButton = nullptr;
}
template<typename T>
void HotkeySettings::RestoreInputButton(void)
{
FinalizeInput<T>(m_activeInputButton);
}
bool HotkeySettings::IsValidKeycodeUp(int keycode)
{
switch (keycode)
{
case WXK_NONE:
case WXK_ESCAPE:
case WXK_ALT:
case WXK_CONTROL:
case WXK_SHIFT:
return false;
default:
return true;
}
}
wxString HotkeySettings::To_wxString(uKeyboardHotkey hotkey)
{
if (hotkey.raw == sHotkeyCfg::keyboardNone) {
return m_disabledHotkeyText;
}
wxString ret{};
if (hotkey.alt)
{
ret.append(_("Alt + "));
}
if (hotkey.ctrl)
{
ret.append(_("Ctrl + "));
}
if (hotkey.shift)
{
ret.append(_("Shift + "));
}
ret.append(wxAcceleratorEntry(0, hotkey.key).ToString());
return ret;
}
wxString HotkeySettings::To_wxString(ControllerHotkey_t hotkey)
{
if ((hotkey == sHotkeyCfg::controllerNone) || m_activeController.expired()) {
return m_disabledHotkeyText;
}
return m_activeController.lock()->get_button_name(hotkey);
}
void HotkeySettings::CaptureInput(wxKeyEvent& event)
{
uKeyboardHotkey hotkey{};
hotkey.key = event.GetKeyCode();
hotkey.alt = event.AltDown();
hotkey.ctrl = event.ControlDown();
hotkey.shift = event.ShiftDown();
const auto it = s_keyboardHotkeyToFuncMap.find(hotkey.raw);
if (it != s_keyboardHotkeyToFuncMap.end())
it->second();
}
void HotkeySettings::CaptureInput(const ControllerState& currentState, const ControllerState& lastState)
{
const auto& modifier = s_cfgHotkeys.modifiers.controller;
if ((modifier >= 0) && currentState.buttons.GetButtonState(modifier))
{
for (const auto& buttonId : currentState.buttons.GetButtonList())
{
const auto it = s_controllerHotkeyToFuncMap.find(buttonId);
if (it == s_controllerHotkeyToFuncMap.end())
continue;
/* only capture clicks */
if (lastState.buttons.GetButtonState(buttonId))
break;
it->second();
break;
}
}
}