From e9a20e764f5f5e144673392eaf72e0631e36014a Mon Sep 17 00:00:00 2001 From: Colin Kinloch Date: Mon, 16 Dec 2024 11:43:26 +0000 Subject: [PATCH] mangoapp: Select unique SysV message queue This allows multiple mangoapp instances to run on the same system. --- src/mangoapp.cpp | 9 ++++++++- src/steamcompmgr.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/mangoapp.cpp b/src/mangoapp.cpp index 91e01bc275..7ff94b895b 100644 --- a/src/mangoapp.cpp +++ b/src/mangoapp.cpp @@ -11,6 +11,7 @@ static bool inited = false; static int msgid = 0; extern bool g_bAppWantsHDRCached; extern uint32_t g_focusedBaseAppId; +extern int g_sMangoappMqId; struct mangoapp_msg_header { long msg_type; // Message queue ID, never change @@ -36,8 +37,11 @@ struct mangoapp_msg_v1 { } __attribute__((packed)) mangoapp_msg_v1; void init_mangoapp(){ - int key = ftok("mangoapp", 65); + // WARNING: "mangoapp" most likely isn't in the working directory so key will be -1 (0xffffffff) + // TODO: Stop using message queues + key_t key = ftok("mangoapp", 65); msgid = msgget(key, 0666 | IPC_CREAT); + mangoapp_msg_v1.hdr.msg_type = 1; mangoapp_msg_v1.hdr.version = 1; mangoapp_msg_v1.fsrUpscale = 0; @@ -60,7 +64,10 @@ void mangoapp_update( uint64_t visible_frametime, uint64_t app_frametime_ns, uin mangoapp_msg_v1.displayRefresh = (uint16_t) gamescope::ConvertmHzToHz( g_nOutputRefresh ); mangoapp_msg_v1.bAppWantsHDR = g_bAppWantsHDRCached; mangoapp_msg_v1.bSteamFocused = g_focusedBaseAppId == 769; + msgsnd(msgid, &mangoapp_msg_v1, sizeof(mangoapp_msg_v1) - sizeof(mangoapp_msg_v1.hdr.msg_type), IPC_NOWAIT); + if (g_sMangoappMqId > 0) + msgsnd(g_sMangoappMqId, &mangoapp_msg_v1, sizeof(mangoapp_msg_v1) - sizeof(mangoapp_msg_v1.hdr.msg_type), IPC_NOWAIT); } extern uint64_t g_uCurrentBasePlaneCommitID; diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp index 18be94d19c..7aadbf02cf 100644 --- a/src/steamcompmgr.cpp +++ b/src/steamcompmgr.cpp @@ -69,6 +69,7 @@ #endif #include #include +#include #include #include #include @@ -164,6 +165,8 @@ uint32_t g_reshade_technique_idx = 0; bool g_bSteamIsActiveWindow = false; bool g_bForceInternal = false; +int g_sMangoappMqId = -1; + static std::vector< steamcompmgr_win_t* > GetGlobalPossibleFocusWindows(); static bool pick_primary_focus_and_override(focus_t *out, Window focusControlWindow, const std::vector& vecPossibleFocusWindows, bool globalFocus, const std::vector& ctxFocusControlAppIDs); @@ -5900,6 +5903,9 @@ steamcompmgr_exit(void) { g_ImageWaiter.Shutdown(); + if ( g_sMangoappMqId > 0 ) + msgctl( g_sMangoappMqId, IPC_RMID, NULL); + // Clean up any commits. { gamescope_xwayland_server_t *server = NULL; @@ -7390,6 +7396,27 @@ void LaunchNestedChildren( char **ppPrimaryChildArgv ) if ( g_bLaunchMangoapp ) { + char szMangoappMqKey[ NAME_MAX ]; + // Attempt to find a fresh message queue id + for ( int i = 0; i < 255; i++ ) { + key_t key = ftok( ".", i ); + int res = msgget( key, 0666 | IPC_CREAT | IPC_EXCL ); + if ( res == -1 ) { + switch ( errno ) { + case EEXIST: + continue; + default: + break; + } + } + + // Success + g_sMangoappMqId = res; + snprintf( szMangoappMqKey, sizeof( szMangoappMqKey ), "%d", key ); + setenv( "MANGOAPP_MQ_KEY", szMangoappMqKey, 0 ); + break; + } + char *ppMangoappArgv[] = { (char *)"mangoapp", NULL }; gamescope::Process::SpawnProcessInWatchdog( ppMangoappArgv, true ); }