Skip to content

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

Stops Steam from stealing focus

NotProtonNot3bd0bf4ef256
diff --git a/.github/workflows/app.yml b/.github/workflows/app.yml
index fa60bdb..0332cd0 100644
--- a/.github/workflows/app.yml
+++ b/.github/workflows/app.yml
@@ -116,6 +116,7 @@ jobs:
make -s peicon-fixtures
make -s panel-behavior
make -s spawn-env
+ make -s activatecheck
make -s compatcheck
make -s compatsvc-check
make -s shortcutcheck
diff --git a/Makefile b/Makefile
index b922c87..47f5d37 100644
--- a/Makefile
+++ b/Makefile
@@ -40,6 +40,7 @@ SRCS := \
dylib/hooks/hook_webpatch.c \
dylib/hooks/hook_spawn.c \
dylib/hooks/hook_launch.c \
+ dylib/hooks/hook_activate.c \
dylib/feats/compat.c \
dylib/feats/webui.c \
dylib/feats/compatsvc.c \
@@ -67,7 +68,7 @@ DEPS := $(OBJS:.o=.d)
tests-list overlay-shim overlay-shim-install overlay-shim-tests \
overlay-shim-bench iconmaker icon \
appinfo helpers-install ntdll-resolve bridge runcheck compatcheck \
- compatsvc-check shortcutcheck scriptcheck envcheck buildcheck settingscheck routecheck bridgecheck gamedrivecheck launchercheck launch-shell FORCE
+ compatsvc-check shortcutcheck scriptcheck envcheck buildcheck settingscheck routecheck bridgecheck gamedrivecheck launchercheck launch-shell activatecheck FORCE
APP_PAYLOAD := app/Sources/NotProtonApp/Resources/payload
@@ -292,6 +293,14 @@ launch-shell:
$(TEST_CC) -O1 -o $(LAUNCH_SHELL) dylib/tests/launch-shell.c && \
$(LAUNCH_SHELL)
+ACTIVATECHECK := $(OUT_DIR)/activatecheck
+
+activatecheck:
+ @if [ ! -f dylib/tests/activatecheck.c ]; then $(call SKIP,activatecheck,dylib/tests/activatecheck.c); exit 0; fi; \
+ mkdir -p $(OUT_DIR) && \
+ $(TEST_CC) -O1 -o $(ACTIVATECHECK) dylib/tests/activatecheck.c -framework AppKit -lobjc && \
+ $(ACTIVATECHECK)
+
SPAWN_ENV := $(OUT_DIR)/spawn-env
spawn-env:
diff --git a/dylib/core/loader.c b/dylib/core/loader.c
index 7939b01..503ed9a 100644
--- a/dylib/core/loader.c
+++ b/dylib/core/loader.c
@@ -283,6 +283,7 @@ static void np_init(void) {
// before that occurs.
np_compat_ensure_tool_manifest();
np_hooks_spawn_install();
+ np_hooks_activate_install();
if (pthread_create(&g_install, NULL, install_thread, NULL) == 0)
g_install_started = 1;
diff --git a/dylib/hooks/hook_activate.c b/dylib/hooks/hook_activate.c
new file mode 100644
index 0000000..ea1e035
--- /dev/null
+++ b/dylib/hooks/hook_activate.c
@@ -0,0 +1,116 @@
+// Stops Steam from rudely stealing focus
+#include "hooks.h"
+#include "../util/log.h"
+
+#include <CoreFoundation/CoreFoundation.h>
+#include <dispatch/dispatch.h>
+#include <dlfcn.h>
+#include <mach-o/dyld.h>
+#include <string.h>
+#include <unistd.h>
+
+typedef void (*fn_did_become_active)(void *self, void *cmd, void *note);
+
+static void *(*np_get_class)(const char *);
+static void *(*np_sel)(const char *);
+static void *np_msg_send;
+static fn_did_become_active orig_did_become_active;
+static void *np_watcher;
+static int np_menu_bar_lost;
+static int np_vgui_seen;
+
+static void *np_msg(void *obj, const char *sel) {
+ return ((void *(*)(void *, void *))np_msg_send)(obj, np_sel(sel));
+}
+
+static int np_owns_menu_bar(void *workspace) {
+ void *owner = np_msg(workspace, "menuBarOwningApplication");
+ return owner && ((int (*)(void *, void *))np_msg_send)(owner, np_sel("processIdentifier")) == getpid();
+}
+
+static void np_menu_bar_sync(void *workspace) {
+ __atomic_store_n(&np_menu_bar_lost, !np_owns_menu_bar(workspace), __ATOMIC_RELEASE);
+}
+
+static void np_menu_bar_owner_changed(void *self, void *cmd, void *path, void *workspace,
+ void *change, void *context) {
+ (void)self; (void)cmd; (void)path; (void)change; (void)context;
+ if (!np_owns_menu_bar(workspace))
+ __atomic_store_n(&np_menu_bar_lost, 1, __ATOMIC_RELEASE);
+}
+
+static void np_hook_did_become_active(void *self, void *cmd, void *note) {
+ if (!__atomic_exchange_n(&np_menu_bar_lost, 0, __ATOMIC_ACQ_REL)) {
+ NP_LOG("[activate] skipped, Steam kept the menu bar so this was a menu bar click");
+ return;
+ }
+ orig_did_become_active(self, cmd, note);
+}
+
+static void np_activate_install_main(void *unused) {
+ (void)unused;
+ void *(*alloc_pair)(void *, const char *, size_t) = dlsym(RTLD_DEFAULT, "objc_allocateClassPair");
+ void (*register_pair)(void *) = dlsym(RTLD_DEFAULT, "objc_registerClassPair");
+ int (*add_method)(void *, void *, void *, const char *) = dlsym(RTLD_DEFAULT, "class_addMethod");
+ void *(*get_method)(void *, void *) = dlsym(RTLD_DEFAULT, "class_getInstanceMethod");
+ void *(*set_imp)(void *, void *) = dlsym(RTLD_DEFAULT, "method_setImplementation");
+ if (!alloc_pair || !register_pair || !add_method || !get_method || !set_imp) {
+ NP_WARN("[activate] objc class functions unavailable, not hooked");
+ return;
+ }
+
+ void *delegate = np_get_class("VGUIApplicationDelegate");
+ void *method = delegate ? get_method(delegate, np_sel("applicationDidBecomeActive:")) : NULL;
+ void *ws_cls = np_get_class("NSWorkspace");
+ void *workspace = ws_cls ? np_msg(ws_cls, "sharedWorkspace") : NULL;
+ if (!method || !workspace) {
+ NP_WARN("[activate] VGUIApplicationDelegate or NSWorkspace not found, not hooked");
+ return;
+ }
+
+ void *watcher_cls = alloc_pair(np_get_class("NSObject"), "NPActivateWatcher", 0);
+ if (!watcher_cls) {
+ NP_WARN("[activate] cannot create the menu bar watcher, not hooked");
+ return;
+ }
+ add_method(watcher_cls, np_sel("observeValueForKeyPath:ofObject:change:context:"),
+ (void *)np_menu_bar_owner_changed, "v@:@@@^v");
+ register_pair(watcher_cls);
+ np_watcher = np_msg(np_msg(watcher_cls, "alloc"), "init");
+ ((void (*)(void *, void *, void *, CFStringRef, unsigned long, void *))np_msg_send)(
+ workspace, np_sel("addObserver:forKeyPath:options:context:"), np_watcher,
+ CFSTR("menuBarOwningApplication"), 0, NULL);
+ np_menu_bar_sync(workspace);
+
+ orig_did_become_active = (fn_did_become_active)set_imp(method, (void *)np_hook_did_become_active);
+ NP_LOG("[activate] hooked");
+}
+
+static void np_activate_image_added(const struct mach_header *mh, intptr_t slide) {
+ (void)slide;
+ Dl_info di;
+ if (!dladdr(mh, &di) || !di.dli_fname)
+ return;
+ const char *base = strrchr(di.dli_fname, '/');
+ if (strcmp(base ? base + 1 : di.dli_fname, "vgui2_s.dylib") != 0)
+ return;
+ if (__atomic_exchange_n(&np_vgui_seen, 1, __ATOMIC_ACQ_REL))
+ return;
+ dispatch_async_f(dispatch_get_main_queue(), NULL, np_activate_install_main);
+}
+
+void np_hooks_activate_install(void) {
+ if (np_hooks_env_lists_label("NOTPROTON_DISABLE", "activate")) {
+ NP_WARN("[activate] DISABLED via NOTPROTON_DISABLE");
+ return;
+ }
+
+ np_get_class = (void *(*)(const char *))dlsym(RTLD_DEFAULT, "objc_getClass");
+ np_sel = (void *(*)(const char *))dlsym(RTLD_DEFAULT, "sel_registerName");
+ np_msg_send = dlsym(RTLD_DEFAULT, "objc_msgSend");
+ if (!np_get_class || !np_sel || !np_msg_send) {
+ NP_WARN("[activate] objc runtime unavailable, not hooked");
+ return;
+ }
+ _dyld_register_func_for_add_image(np_activate_image_added);
+}
diff --git a/dylib/hooks/hooks.h b/dylib/hooks/hooks.h
index ba08132..9d951a6 100644
--- a/dylib/hooks/hooks.h
+++ b/dylib/hooks/hooks.h
@@ -73,4 +73,6 @@ void np_hooks_launch_install(const struct mach_header_64 *mh, intptr_t slide);
// can spawn anything, so this one does not wait for steamclient the way the rest do.
void np_hooks_spawn_install(void);
+void np_hooks_activate_install(void);
+
#endif // NOTPROTON_HOOKS_HOOKS_H
diff --git a/dylib/tests/activatecheck.c b/dylib/tests/activatecheck.c
new file mode 100644
index 0000000..4be5f10
--- /dev/null
+++ b/dylib/tests/activatecheck.c
@@ -0,0 +1,110 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <objc/runtime.h>
+#include <objc/message.h>
+
+int np_log_level = -1;
+FILE *np_log_file = NULL;
+
+int np_hooks_env_lists_label(const char *var, const char *label) {
+ (void)var; (void)label;
+ return 0;
+}
+
+#include "../hooks/hook_activate.c"
+
+static int failures;
+static int handoffs;
+static id fake_owner;
+static int fake_pid;
+
+static void check(int ok, const char *what) {
+ if (!ok) {
+ printf("FAIL %s\n", what);
+ failures++;
+ }
+}
+
+static void fake_did_become_active(id self, SEL cmd, id note) {
+ handoffs++;
+}
+
+static id fake_menu_bar_owner(id self, SEL cmd) { return fake_owner; }
+static int fake_process_identifier(id self, SEL cmd) { return fake_pid; }
+
+static id msg(id obj, const char *sel) {
+ return ((id (*)(id, SEL))objc_msgSend)(obj, sel_registerName(sel));
+}
+
+static Class make_class(const char *name, const char *sel, IMP imp, const char *types) {
+ Class cls = objc_allocateClassPair(objc_getClass("NSObject"), name, 0);
+ class_addMethod(cls, sel_registerName(sel), imp, types);
+ objc_registerClassPair(cls);
+ return cls;
+}
+
+static void become_active(id delegate) {
+ ((void (*)(id, SEL, id))objc_msgSend)(delegate, sel_registerName("applicationDidBecomeActive:"), nil);
+}
+
+static void menu_bar_owner(id workspace, id owner, int pid) {
+ fake_owner = owner;
+ fake_pid = pid;
+ ((void (*)(id, SEL, id, id, id, void *))objc_msgSend)(
+ (id)np_watcher, sel_registerName("observeValueForKeyPath:ofObject:change:context:"),
+ (id)CFSTR("menuBarOwningApplication"), workspace, nil, NULL);
+}
+
+int main(void) {
+ Class delegate_cls = make_class("VGUIApplicationDelegate", "applicationDidBecomeActive:",
+ (IMP)fake_did_become_active, "v@:@");
+ Class app_cls = make_class("FakeRunningApplication", "processIdentifier",
+ (IMP)fake_process_identifier, "i@:");
+ Class ws_cls = make_class("FakeWorkspace", "menuBarOwningApplication",
+ (IMP)fake_menu_bar_owner, "@@:");
+
+ id delegate = msg(msg((id)delegate_cls, "alloc"), "init");
+ id app = msg(msg((id)app_cls, "alloc"), "init");
+ id workspace = msg(msg((id)ws_cls, "alloc"), "init");
+
+ np_hooks_activate_install();
+ np_activate_install_main(NULL);
+ check(orig_did_become_active != NULL && np_watcher != NULL,
+ "the hook installs on VGUIApplicationDelegate");
+
+ fake_owner = app;
+ fake_pid = getpid();
+ np_menu_bar_sync(workspace);
+ become_active(delegate);
+ check(handoffs == 0, "a menu bar click is skipped when Steam already owned the menu bar at install");
+
+ fake_pid = getpid() + 1;
+ np_menu_bar_sync(workspace);
+ become_active(delegate);
+ check(handoffs == 1, "the first activation hands off when another app owned the menu bar at install");
+
+ become_active(delegate);
+ check(handoffs == 1, "an activation while Steam kept the menu bar is skipped");
+
+ menu_bar_owner(workspace, app, getpid());
+ become_active(delegate);
+ check(handoffs == 1, "Steam owning the menu bar again does not count");
+
+ menu_bar_owner(workspace, app, getpid() + 1);
+ become_active(delegate);
+ check(handoffs == 2, "an activation after another app took the menu bar hands off");
+
+ become_active(delegate);
+ check(handoffs == 2, "the next menu bar click is skipped again");
+
+ menu_bar_owner(workspace, nil, 0);
+ become_active(delegate);
+ check(handoffs == 3, "an activation after nobody owned the menu bar hands off");
+
+ if (failures) {
+ printf("==> activatecheck: %d check(s) failed\n", failures);
+ return 1;
+ }
+ printf("==> activatecheck: Steam hands off after losing the menu bar and skips menu bar clicks\n");
+ return 0;
+}