Skip to content

Commit

Permalink
mangoapp: Use POSIX message queues
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 8, 2024
1 parent d317492 commit 64391b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/mangoapp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sys/ipc.h>
#include <unistd.h>
#include <sys/msg.h>
#include <mqueue.h>
#include <cstring>

#include "steamcompmgr.hpp"
Expand All @@ -9,8 +10,10 @@

static bool inited = false;
static int msgid = 0;
static mqd_t mangoapp_mq = 0;
extern bool g_bAppWantsHDRCached;
extern uint32_t g_focusedBaseAppId;
extern const char *g_sMangoappMqName;

struct mangoapp_msg_header {
long msg_type; // Message queue ID, never change
Expand All @@ -36,8 +39,22 @@ 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: Deprecate SysV message queues
key_t key = ftok("mangoapp", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
struct mq_attr attrs = {
.mq_flags = O_NONBLOCK,
.mq_maxmsg = 4,
.mq_msgsize = sizeof( mangoapp_msg_v1 ),
};
if ( g_sMangoappMqName != nullptr ) {
mq_unlink( g_sMangoappMqName );
mangoapp_mq = mq_open( g_sMangoappMqName, O_CREAT | O_EXCL | O_WRONLY | O_NONBLOCK, S_IRUSR | S_IWUSR, &attrs );
if (mangoapp_mq == -1)
console_log.errorf("Failed to create mangoapp message queue at %s %d (%s)", g_sMangoappMqName, errno, strerror(errno));
}

mangoapp_msg_v1.hdr.msg_type = 1;
mangoapp_msg_v1.hdr.version = 1;
mangoapp_msg_v1.fsrUpscale = 0;
Expand All @@ -60,6 +77,9 @@ 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;
if (mangoapp_mq > 0 && mq_send( mangoapp_mq, (const char *)&mangoapp_msg_v1, sizeof( struct mangoapp_msg_v1 ), 0 ) == -1 && errno != EAGAIN) {
console_log.errorf( "Failed to send message to mangoapp %d: %s", errno, strerror( errno ) );
}
msgsnd(msgid, &mangoapp_msg_v1, sizeof(mangoapp_msg_v1) - sizeof(mangoapp_msg_v1.hdr.msg_type), IPC_NOWAIT);
}

Expand Down
11 changes: 11 additions & 0 deletions src/steamcompmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include <getopt.h>
#include <spawn.h>
#include <signal.h>
#include <mqueue.h>
#include <linux/input-event-codes.h>
#include <X11/Xmu/CurUtil.h>
#include "waitable.h"
Expand Down Expand Up @@ -164,6 +165,8 @@ uint32_t g_reshade_technique_idx = 0;
bool g_bSteamIsActiveWindow = false;
bool g_bForceInternal = false;

const char *g_sMangoappMqName = nullptr;

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_sMangoappMqName != nullptr )
mq_unlink( g_sMangoappMqName );

// Clean up any commits.
{
gamescope_xwayland_server_t *server = NULL;
Expand Down Expand Up @@ -7384,6 +7390,11 @@ void LaunchNestedChildren( char **ppPrimaryChildArgv )

if ( g_bLaunchMangoapp )
{
char szMangoappMqName[ NAME_MAX ];
snprintf( szMangoappMqName, sizeof( szMangoappMqName ), "/gamescope-mangoapp-%d", getpid() );
setenv( "MANGOAPP_MQ_NAME", szMangoappMqName, 0 );
g_sMangoappMqName = getenv( "MANGOAPP_MQ_NAME" );

char *ppMangoappArgv[] = { (char *)"mangoapp", NULL };
gamescope::Process::SpawnProcessInWatchdog( ppMangoappArgv, true );
}
Expand Down

0 comments on commit 64391b8

Please sign in to comment.