Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

feat(shim): Command-Q quits the game

raymondchins4deae7612688
diff --git a/Makefile b/Makefile
index ca13e36..3047cfb 100644
--- a/Makefile
+++ b/Makefile
@@ -443,7 +443,7 @@ OVERLAY_SHIM_TEST_FLAGS := -mmacosx-version-min=$(MIN_VER) -O2 -Wall -Wextra \
overlay-shim: $(OVERLAY_SHIM)
-$(OVERLAY_SHIM): $(OVERLAY_SHIM_SRC) overlay-shim/framegen.m
+$(OVERLAY_SHIM): $(OVERLAY_SHIM_SRC) overlay-shim/framegen.m overlay-shim/quitkey.m
@mkdir -p $(OUT_DIR)
$(CC) -arch arm64 $(OVERLAY_SHIM_FLAGS) -o $(OVERLAY_SHIM_ARM) $<
$(CC) -arch x86_64 $(OVERLAY_SHIM_FLAGS) -o $(OVERLAY_SHIM_X86) $<
diff --git a/overlay-shim/overlay_shim.m b/overlay-shim/overlay_shim.m
index 17821f9..907366d 100644
--- a/overlay-shim/overlay_shim.m
+++ b/overlay-shim/overlay_shim.m
@@ -283,6 +283,7 @@ static int stage_claimed(Class seen, Class *held, const char *stage)
// Frame generation (NOTPROTON_FRAMEGEN=1), one translation unit with the shim
#include "framegen.m"
+#include "quitkey.m"
static void encoder_mark(id encoder, NSUInteger format, id<MTLCommandBuffer> buffer)
{
@@ -1059,6 +1060,8 @@ __attribute__((visibility("default"))) void np_overlay_shim_install(void)
__attribute__((constructor)) static void overlay_shim_load(void)
{
+ quitkey_init();
+
Class layer = objc_getClass("CAMetalLayer");
if (layer == Nil) {
shim_log("overlay_shim_load: CAMetalLayer absent, nothing to correct\n");
diff --git a/overlay-shim/quitkey.m b/overlay-shim/quitkey.m
new file mode 100644
index 0000000..46729e9
--- /dev/null
+++ b/overlay-shim/quitkey.m
@@ -0,0 +1,71 @@
+// Command-Q quits the game.
+//
+// Wine maps Command-Q to its Quit menu item, which only asks the game's windows to close
+// (WM_CLOSE), and most games ignore that, so Command-Q did nothing. Here the game
+// process's own NSApplication sendEvent: (Wine's subclass handles keys there, before
+// AppKit's local monitors) catches Command-Q and ends the process. The run script sees
+// the game exit and tears the Wine prefix down as it does after any exit, so nothing is
+// left running. BRIDGEPLAY_COMMAND_Q=0 turns it off.
+//
+// The shim never links AppKit (loading it early breaks AppKit's CALayer categories, see
+// interpose()), so this goes through the runtime and starts only once the game has
+// brought up NSApplication itself. Included from overlay_shim.m.
+
+#import <dispatch/dispatch.h>
+#import <signal.h>
+
+#define quit_key_code 12 // kVK_ANSI_Q
+#define event_type_key_down 10 // NSEventTypeKeyDown
+#define modifier_command (1UL << 20) // NSEventModifierFlagCommand
+#define modifier_others ((1UL << 17) | (1UL << 18) | (1UL << 19)) // shift, control, option
+
+static int quit_tries;
+static void (*next_send_event)(id, SEL, id);
+
+static int quit_is_command_q(id event)
+{
+ if (((NSUInteger (*)(id, SEL))objc_msgSend)(event, sel_registerName("type")) != event_type_key_down)
+ return 0;
+ NSUInteger flags = ((NSUInteger (*)(id, SEL))objc_msgSend)(event, sel_registerName("modifierFlags"));
+ if (!(flags & modifier_command) || (flags & modifier_others))
+ return 0;
+ return ((unsigned short (*)(id, SEL))objc_msgSend)(event, sel_registerName("keyCode")) == quit_key_code;
+}
+
+static void hook_send_event(id app, SEL selector, id event)
+{
+ if (event != nil && quit_is_command_q(event)) {
+ shim_log("overlay_shim quitkey: Command-Q, quitting pid=%d\n", getpid());
+ // The run script waits on this process and kills the rest of the prefix once it ends.
+ kill(getpid(), SIGKILL);
+ return;
+ }
+ next_send_event(app, selector, event);
+}
+
+static void quit_install(void)
+{
+ id *app_slot = (id *)dlsym(RTLD_DEFAULT, "NSApp");
+ id app = app_slot != NULL ? *app_slot : nil;
+
+ if (objc_getClass("NSEvent") == Nil || app == nil) {
+ // The game brings AppKit up on its own schedule; look again shortly, for a minute.
+ if (++quit_tries < 120)
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC),
+ dispatch_get_main_queue(), ^{ quit_install(); });
+ return;
+ }
+
+ Class app_class = object_getClass(app);
+ int hooked = interpose(app_class, "sendEvent:", (IMP)hook_send_event, (IMP *)&next_send_event);
+ shim_log("overlay_shim quitkey: Command-Q quits %s on %s\n", hooked ? "armed" : "failed",
+ class_getName(app_class));
+}
+
+static void quitkey_init(void)
+{
+ const char *off = getenv("BRIDGEPLAY_COMMAND_Q");
+ if (off != NULL && strcmp(off, "0") == 0)
+ return;
+ dispatch_async(dispatch_get_main_queue(), ^{ quit_install(); });
+}