mangohudctl: rework argv handling, always send valid data

Currently, if the user sets attribute we send dummy data to mangohud.
Avoid that by pulling the attrib handling into main and avoiding the
duplicate attribute name checking.

Kill off the no-longer relevant TODO while we're here.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
pull/707/head
Emil Velikov 2 years ago committed by jackun
parent f4ea65429e
commit 5e53342f6c
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -3,7 +3,6 @@
#include "mangoapp.h"
void help_and_quit() {
// TODO! document attributes and values
fprintf(stderr, "Usage: mangohudctl [set|toggle] attribute [value]\n");
fprintf(stderr, "Attributes:\n");
fprintf(stderr, " no_display hides or shows hud\n");
@ -22,40 +21,12 @@ bool str_to_bool(const char *value)
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 */
@ -66,24 +37,33 @@ int main(int argc, char *argv[])
ctrl_msg.hdr.msg_type = 2;
ctrl_msg.hdr.ctrl_msg_type = 1;
ctrl_msg.hdr.version = 1;
uint8_t value;
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]);
value = str_to_bool(argv[3]) ? 1 : 2;
} else if (strcmp(argv[1], "toggle") == 0) {
if (argc != 3)
help_and_quit();
toggle_attribute(&ctrl_msg, argv[2]);
} else
value = 3;
} else {
help_and_quit();
}
if (strcmp(argv[2], "no_display") == 0)
ctrl_msg.no_display = value;
else if (strcmp(argv[2], "log_session") == 0)
ctrl_msg.log_session = value;
else
help_and_quit();
msgsnd(msgid, &ctrl_msg, sizeof(mangoapp_ctrl_msgid1_v1), IPC_NOWAIT);
return 0;
}
}

Loading…
Cancel
Save