Skip to content

Commit

Permalink
mangoapp: Select unique SysV message queue
Browse files Browse the repository at this point in the history
This allows multiple mangoapp instances to run on the same system.
  • Loading branch information
ColinKinloch committed Dec 16, 2024
1 parent 1c9495c commit e9a20e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/mangoapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions src/steamcompmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#endif
#include <sys/socket.h>
#include <sys/resource.h>
#include <sys/msg.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
Expand Down Expand Up @@ -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<steamcompmgr_win_t*>& vecPossibleFocusWindows, bool globalFocus, const std::vector<uint32_t>& ctxFocusControlAppIDs);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
Expand Down

0 comments on commit e9a20e7

Please sign in to comment.