mangohudctl and interface

gpu_metrics
FlightlessMango 2 years ago
parent ba583552b5
commit 97f6a00171

@ -0,0 +1,83 @@
#include <string.h>
#include "mangoapp.h"
#include <stdio.h>
void help_and_quit() {
// TODO! document attributes and values
fprintf(stderr, "Usage: mangohudctl [set|toggle] attribute [value]\n");
exit(1);
}
bool str_to_bool(const char *value)
{
if (strcasecmp(value, "true") == 0 || strcmp(value, "1") == 0)
return true;
else if (strcasecmp(value, "false") == 0 || strcmp(value, "0") == 0)
return false;
/* invalid boolean, display a nice error message saying that */
fprintf(stderr, "The value '%s' is not an accepted boolean. Use 0/1 or true/false\n", value);
exit(1);
}
bool set_attribute(struct mangoapp_ctrl_msgid1_v1 *ctrl_msg,
const char *attribute, const char* value)
{
if (strcmp(attribute, "no_display") == 0) {
ctrl_msg->no_display = str_to_bool(value) ? 1 : 2;
return true;
} else if (strcmp(attribute, "log_session") == 0) {
ctrl_msg->log_session = str_to_bool(value) ? 1 : 2;
return true;
}
return false;
}
bool toggle_attribute(struct mangoapp_ctrl_msgid1_v1 *ctrl_msg,
const char *attribute)
{
if (strcmp(attribute, "no_display") == 0) {
ctrl_msg->no_display = 3;
return true;
} else if (strcmp(attribute, "log_session") == 0) {
ctrl_msg->log_session = 3;
return true;
}
return false;
}
int main(int argc, char *argv[])
{
/* Set up message queue */
int key = ftok("mangoapp", 65);
int msgid = msgget(key, 0666 | IPC_CREAT);
/* Create the message that we will send to mangohud */
struct mangoapp_ctrl_msgid1_v1 ctrl_msg = {0};
ctrl_msg.hdr.msg_type = 2;
ctrl_msg.hdr.ctrl_msg_type = 1;
ctrl_msg.hdr.version = 1;
if (argc <= 2)
help_and_quit();
if (strcmp(argv[1], "set") == 0) {
if (argc != 4)
help_and_quit();
set_attribute(&ctrl_msg, argv[2], argv[3]);
} else if (strcmp(argv[1], "toggle") == 0) {
if (argc != 3)
help_and_quit();
toggle_attribute(&ctrl_msg, argv[2]);
} else
help_and_quit();
msgsnd(msgid, &ctrl_msg, sizeof(mangoapp_ctrl_msgid1_v1), IPC_NOWAIT);
return 0;
}

@ -34,6 +34,31 @@ std::mutex mangoapp_m;
std::condition_variable mangoapp_cv;
static uint8_t raw_msg[1024] = {0};
void ctrl_thread(){
while (1){
const struct mangoapp_ctrl_msgid1_v1 *mangoapp_ctrl_v1 = (const struct mangoapp_ctrl_msgid1_v1*) raw_msg;
size_t msg_size = msgrcv(msgid, (void *) raw_msg, sizeof(raw_msg), 2, 0);
switch (mangoapp_ctrl_v1->log_session) {
case 1:
logger->start_logging();
case 2:
logger->stop_logging();
case 3:
logger->is_active() ? logger->stop_logging() : logger->start_logging();
}
std::lock_guard<std::mutex> lk(mangoapp_m);
switch (mangoapp_ctrl_v1->no_display){
case 1:
params->no_display = 1;
case 2:
params->no_display = 0;
case 3:
params->no_display ? params->no_display = 0 : params->no_display = 1;
}
mangoapp_cv.notify_one();
}
}
void msg_read_thread(){
int key = ftok("mangoapp", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
@ -134,6 +159,7 @@ int main(int, char**)
init_system_info();
sw_stats.engine = EngineTypes::GAMESCOPE;
std::thread(msg_read_thread).detach();
std::thread(ctrl_thread).detach();
if(!logger) logger = std::make_unique<Logger>(HUDElements.params);
// Main loop
while (!glfwWindowShouldClose(window)){

@ -19,5 +19,22 @@ struct mangoapp_msg_v1 {
uint32_t pid;
uint64_t frametime_ns;
// WARNING: Always ADD fields, never remove or repurpose fields
} __attribute__((packed));
struct mangoapp_ctrl_header {
long msg_type; // Message queue ID, never change
uint32_t ctrl_msg_type; /* This is a way to share the same thread between multiple types of messages */
uint32_t version; /* version of the message type, for backwards incompatible changes */
} __attribute__((packed));
struct mangoapp_ctrl_msgid1_v1 {
struct mangoapp_ctrl_header hdr;
// When a field is set to 0, it should always mean "ignore" or "no changes"
uint8_t no_display; // 0x0 = ignore; 0x1 = disable; 0x2 = enable; 0x3 = toggle
uint8_t log_session; // 0x0 = ignore; 0x1 = start a session; 0x2 = stop the current session; 0x3 = toggle logging
char log_session_name[64]; // if byte 0 is NULL, ignore. Needs to be set when starting/toggling a session if we want to override the default name
// WARNING: Always ADD fields, never remove or repurpose fields
} __attribute__((packed));
Loading…
Cancel
Save