Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mangoapp: Select unique SysV message queue #1502

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ static ImVec2 window_size;
static uint32_t vendorID;
static std::string deviceName;
static notify_thread notifier;
static int msgid;
static int msgid = 0;
static int unique_msgid = 0;
static bool mangoapp_paused = false;
std::mutex mangoapp_m;
std::condition_variable mangoapp_cv;
static uint8_t raw_msg[1024] = {0};
static uint8_t raw_ctrl_msg[1024] = {0};
static uint32_t screenWidth, screenHeight;

static unsigned int get_prop(const char* propName){
Expand Down Expand Up @@ -78,9 +80,9 @@ static unsigned int get_prop(const char* propName){

static void ctrl_thread(){
while (1){
const struct mangoapp_ctrl_msgid1_v1 *mangoapp_ctrl_v1 = (const struct mangoapp_ctrl_msgid1_v1*) raw_msg;
memset(raw_msg, 0, sizeof(raw_msg));
msgrcv(msgid, (void *) raw_msg, sizeof(raw_msg), 2, 0);
const struct mangoapp_ctrl_msgid1_v1 *mangoapp_ctrl_v1 = (const struct mangoapp_ctrl_msgid1_v1*) raw_ctrl_msg;
memset(raw_ctrl_msg, 0, sizeof(raw_ctrl_msg));
msgrcv(msgid, (void *) raw_ctrl_msg, sizeof(raw_ctrl_msg), 2, 0);
switch (mangoapp_ctrl_v1->log_session) {
case 0:
// Keep as-is
Expand Down Expand Up @@ -154,15 +156,28 @@ static void msg_read_thread(){
HUDElements.gamescope_debug_app.push_back(0);
HUDElements.gamescope_debug_latency.push_back(0);
}
int key = ftok("mangoapp", 65);
// WARNING: Unless a file named "mangoapp" is in the current directory key will be -1 (0xffffffff) and errno will be set
// 0xffffffff is a valid key, but is likely to collide
// TODO: Deprecate SysV message queues
key_t key = ftok("mangoapp", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
// uint32_t previous_pid = 0;
const char *mangoapp_mq_key_string = getenv("MANGOAPP_MQ_KEY");
if (mangoapp_mq_key_string) {
key = atoi(mangoapp_mq_key_string);
if (key > 0) {
unique_msgid = msgget(key, 0);
printf("mangoapp: Opened message queue with key %d\n", key);
}
}

const struct mangoapp_msg_header *hdr = (const struct mangoapp_msg_header*) raw_msg;
const struct mangoapp_msg_v1 *mangoapp_v1 = (const struct mangoapp_msg_v1*) raw_msg;
while (1){
// make sure that the message recieved is compatible
// and that we're not trying to use variables that don't exist (yet)
size_t msg_size = msgrcv(msgid, (void *) raw_msg, sizeof(raw_msg), 1, 0);
int mq_id = unique_msgid == 0 ? msgid : unique_msgid;
size_t msg_size = msgrcv(mq_id, (void *) raw_msg, sizeof(raw_msg), 1, 0);
if (msg_size != size_t(-1))
{
if (hdr->version == 1){
Expand Down Expand Up @@ -218,6 +233,10 @@ static void msg_read_thread(){
}
else
{
if (errno == EIDRM) {
printf("mangoapp: message queue removed\n");
exit(1);
}
printf("mangoapp: msgrcv returned -1 with error %d - %s\n", errno, strerror(errno));
}
}
Expand Down