Switched to two hooks, so the emulator doesn't get the input from the entire host OS.

This commit is contained in:
OBattler
2024-12-31 03:59:38 +01:00
parent a11712b651
commit 8b05c58fb9
2 changed files with 42 additions and 21 deletions

View File

@@ -144,16 +144,11 @@ keyboard_getkeymap()
}
static LRESULT CALLBACK
emu_LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
input_LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LPKBDLLHOOKSTRUCT lpKdhs = (LPKBDLLHOOKSTRUCT) lParam;
/* Checks if CTRL was pressed. */
BOOL bCtrlDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);
uint16_t scancode = lpKdhs->scanCode & 0x00ff;
if ((nCode < 0) || (nCode != HC_ACTION) || (!mouse_capture && !video_fullscreen))
return CallNextHookEx(NULL, nCode, wParam, lParam);
if (lpKdhs->flags & LLKHF_EXTENDED)
scancode |= 0x100;
@@ -184,7 +179,19 @@ emu_LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
main_window->checkFullscreenHotkey();
if ((lpKdhs->scanCode == 0x01) && (lpKdhs->flags & LLKHF_ALTDOWN) &&
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
static LRESULT CALLBACK
emu_LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LPKBDLLHOOKSTRUCT lpKdhs = (LPKBDLLHOOKSTRUCT) lParam;
/* Checks if CTRL was pressed. */
BOOL bCtrlDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1);
if ((nCode < 0) || (nCode != HC_ACTION) || (!mouse_capture && !video_fullscreen))
return CallNextHookEx(NULL, nCode, wParam, lParam);
else if ((lpKdhs->scanCode == 0x01) && (lpKdhs->flags & LLKHF_ALTDOWN) &&
!(lpKdhs->flags & (LLKHF_UP | LLKHF_EXTENDED)))
return TRUE;
else if ((lpKdhs->scanCode == 0x01) && bCtrlDown && !(lpKdhs->flags & (LLKHF_UP | LLKHF_EXTENDED)))
@@ -209,6 +216,11 @@ emu_LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
}
#endif
#ifdef Q_OS_WINDOWS
static HHOOK llhook = NULL;
static HHOOK llihook = NULL;
#endif
void
main_thread_fn()
{
@@ -286,10 +298,6 @@ main_thread_fn()
static std::thread *main_thread;
#ifdef Q_OS_WINDOWS
static HHOOK llhook = NULL;
#endif
int
main(int argc, char *argv[])
{
@@ -473,17 +481,11 @@ main(int argc, char *argv[])
});
}
#ifdef Q_OS_WINDOWS
if (!raw_input) {
llhook = SetWindowsHookEx(WH_KEYBOARD_LL, emu_LowLevelKeyboardProc, NULL, 0);
atexit([] () -> void { if (llhook) UnhookWindowsHookEx(llhook); });
}
#endif
/* Setup raw input */
auto rawInputFilter = WindowsRawInputFilter::Register(main_window);
if (rawInputFilter) {
app.installNativeEventFilter(rawInputFilter.get());
if (raw_input)
main_window->setSendKeyboardInput(false);
}
#endif
@@ -543,6 +545,19 @@ main(int argc, char *argv[])
plat_pause(0);
});
#ifdef Q_OS_WINDOWS
if (!raw_input) {
llhook = SetWindowsHookEx(WH_KEYBOARD_LL, emu_LowLevelKeyboardProc, NULL, 0);
llihook = SetWindowsHookEx(WH_KEYBOARD_LL, input_LowLevelKeyboardProc, NULL, GetCurrentThreadId());
atexit([] () -> void {
if (llihook)
UnhookWindowsHookEx(llihook);
if (llhook)
UnhookWindowsHookEx(llhook);
});
}
#endif
const auto ret = app.exec();
cpu_thread_run = 0;
main_thread->join();

View File

@@ -525,6 +525,7 @@ void
MediaMenu::cdromMount(int i, const QString &filename)
{
QByteArray fn = filename.toUtf8().data();
int was_empty = cdrom_is_empty(i);
cdrom_exit(i);
@@ -542,9 +543,14 @@ MediaMenu::cdromMount(int i, const QString &filename)
cdrom_image_open(&(cdrom[i]), fn.data());
/* Signal media change to the emulated machine. */
if (cdrom[i].insert)
if (cdrom[i].insert) {
cdrom[i].insert(cdrom[i].priv);
/* The drive was previously empty, transition directly to UNIT ATTENTION. */
if (was_empty)
cdrom[i].insert(cdrom[i].priv);
}
if (strlen(cdrom[i].image_path) > 0)
ui_sb_update_icon_state(SB_CDROM | i, 0);
else