From e66372efda10692ea44fecf9cf3e8b2e664dcbb0 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Fri, 2 Aug 2024 00:55:31 +0200 Subject: [PATCH] Only pass mouse/keyboard to dethrace when the current window is DethRace This is useful when opening a 2nd SDL window to avoid events of the other window leaking into carmageddon (and the reverse). --- CMakeLists.txt | 6 +++++- src/harness/CMakeLists.txt | 4 +--- src/harness/platforms/sdl2.c | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4327fd02..762e138f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,11 +39,15 @@ option(DETHRACE_WERROR "Treat warnings as errors") option(DETHRACE_FIX_BUGS "Fix Dethrace bugs" ON) function(add_compile_flag_if_supported TARGET FLAG) + cmake_parse_arguments(ARGS "" "" "LANGUAGES" ${ARGN}) + if(NOT ARGS_LANGUAGES) + set(ARGS_LANGUAGES C CXX) + endif() string(MAKE_C_IDENTIFIER "${FLAG}" FLAG_TO_IDENTIFIER) set(HAVE_FLAG_VARIABLE_NAME "HAVE_${FLAG_TO_IDENTIFIER}") check_c_compiler_flag("${FLAG}" "${HAVE_FLAG_VARIABLE_NAME}") if(${HAVE_FLAG_VARIABLE_NAME}) - target_compile_options("${TARGET}" PRIVATE "${FLAG}") + target_compile_options("${TARGET}" PRIVATE "$<$:${FLAG}>") endif() endfunction() diff --git a/src/harness/CMakeLists.txt b/src/harness/CMakeLists.txt index dc3ec120..9349d4b7 100644 --- a/src/harness/CMakeLists.txt +++ b/src/harness/CMakeLists.txt @@ -27,9 +27,7 @@ if(NOT MSVC) -Wextra -Wno-unused-parameter ) - add_compile_flags_if_supported(harness - -Wstrict-prototypes - ) + add_compile_flag_if_supported(harness -Wstrict-prototypes LANGUAGES C) else() target_compile_definitions(harness PRIVATE -D_CRT_SECURE_NO_WARNINGS) endif() diff --git a/src/harness/platforms/sdl2.c b/src/harness/platforms/sdl2.c index 6ba8ad8c..1a9417d5 100644 --- a/src/harness/platforms/sdl2.c +++ b/src/harness/platforms/sdl2.c @@ -90,6 +90,9 @@ static int get_and_handle_message(MSG_* msg) { switch (event.type) { case SDL_KEYDOWN: case SDL_KEYUP: + if (event.key.windowID != SDL_GetWindowID(window)) { + continue; + } if (event.key.keysym.sym == SDLK_RETURN) { if (event.key.type == SDL_KEYDOWN) { if ((event.key.keysym.mod & (KMOD_CTRL | KMOD_SHIFT | KMOD_ALT | KMOD_GUI))) { @@ -115,6 +118,15 @@ static int get_and_handle_message(MSG_* msg) { directinput_key_state[dinput_key] = (event.type == SDL_KEYDOWN ? 0x80 : 0); break; + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_CLOSE) { + if (SDL_GetWindowID(window) == event.window.windowID) { + msg->message = WM_QUIT; + return 1; + } + } + break; + case SDL_QUIT: msg->message = WM_QUIT; return 1; @@ -128,6 +140,11 @@ static void get_keyboard_state(unsigned int count, uint8_t* buffer) { } static int get_mouse_buttons(int* pButton1, int* pButton2) { + if (SDL_GetMouseFocus() != window) { + *pButton1 = 0; + *pButton2 = 0; + return 0; + } int state = SDL_GetMouseState(NULL, NULL); *pButton1 = state & SDL_BUTTON_LMASK; *pButton2 = state & SDL_BUTTON_RMASK; @@ -136,6 +153,9 @@ static int get_mouse_buttons(int* pButton1, int* pButton2) { static int get_mouse_position(int* pX, int* pY) { float lX, lY; + if (SDL_GetMouseFocus() != window) { + return 0; + } SDL_GetMouseState(pX, pY); SDL_RenderWindowToLogical(renderer, *pX, *pY, &lX, &lY);