2023-03-14 20:18:33 +00:00
|
|
|
#include <cstdint>
|
2021-11-04 01:08:26 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
2022-04-07 08:28:48 +00:00
|
|
|
#include <sys/socket.h>
|
2021-11-04 01:08:26 +00:00
|
|
|
#include "mesa/util/os_socket.h"
|
|
|
|
#include "overlay.h"
|
2022-04-07 08:28:48 +00:00
|
|
|
#include "version.h"
|
2022-01-20 00:24:01 +00:00
|
|
|
#include "app/mangoapp.h"
|
2021-11-04 01:08:26 +00:00
|
|
|
|
2022-07-28 00:48:35 +00:00
|
|
|
int global_control_client;
|
|
|
|
|
2021-11-04 01:08:26 +00:00
|
|
|
using namespace std;
|
2022-04-07 08:28:48 +00:00
|
|
|
static void parse_command(overlay_params ¶ms,
|
2021-11-04 01:08:26 +00:00
|
|
|
const char *cmd, unsigned cmdlen,
|
|
|
|
const char *param, unsigned paramlen)
|
|
|
|
{
|
2022-04-07 08:28:48 +00:00
|
|
|
if (!strncmp(cmd, "hud", cmdlen)) {
|
|
|
|
params.no_display = !params.no_display;
|
|
|
|
} else if (!strncmp(cmd, "logging", cmdlen)) {
|
|
|
|
if (param && param[0])
|
|
|
|
{
|
|
|
|
int value = atoi(param);
|
|
|
|
if (!value && logger->is_active())
|
|
|
|
logger->stop_logging();
|
|
|
|
else if (value > 0 && !logger->is_active())
|
|
|
|
logger->start_logging();
|
|
|
|
}
|
2022-01-21 12:33:39 +00:00
|
|
|
else
|
2022-04-07 08:28:48 +00:00
|
|
|
{
|
|
|
|
if (logger->is_active())
|
|
|
|
logger->stop_logging();
|
|
|
|
else
|
|
|
|
logger->start_logging();
|
|
|
|
}
|
|
|
|
} else if (!strncmp(cmd, "fcat", cmdlen)) {
|
|
|
|
params.enabled[OVERLAY_PARAM_ENABLED_fcat] = !params.enabled[OVERLAY_PARAM_ENABLED_fcat];
|
|
|
|
}
|
2021-11-04 01:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define BUFSIZE 4096
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will process commands through the control file.
|
|
|
|
*
|
|
|
|
* A command starts with a colon, followed by the command, and followed by an
|
|
|
|
* option '=' and a parameter. It has to end with a semi-colon. A full command
|
|
|
|
* + parameter looks like:
|
|
|
|
*
|
|
|
|
* :cmd=param;
|
|
|
|
*/
|
2022-04-07 08:28:48 +00:00
|
|
|
static void process_char(const int control_client, overlay_params ¶ms, char c)
|
2021-11-04 01:08:26 +00:00
|
|
|
{
|
|
|
|
static char cmd[BUFSIZE];
|
|
|
|
static char param[BUFSIZE];
|
|
|
|
|
|
|
|
static unsigned cmdpos = 0;
|
|
|
|
static unsigned parampos = 0;
|
|
|
|
static bool reading_cmd = false;
|
|
|
|
static bool reading_param = false;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case ':':
|
|
|
|
cmdpos = 0;
|
|
|
|
parampos = 0;
|
|
|
|
reading_cmd = true;
|
|
|
|
reading_param = false;
|
|
|
|
break;
|
|
|
|
case ';':
|
|
|
|
if (!reading_cmd)
|
|
|
|
break;
|
|
|
|
cmd[cmdpos++] = '\0';
|
|
|
|
param[parampos++] = '\0';
|
2022-04-07 08:28:48 +00:00
|
|
|
parse_command(params, cmd, cmdpos, param, parampos);
|
2021-11-04 01:08:26 +00:00
|
|
|
reading_cmd = false;
|
|
|
|
reading_param = false;
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
if (!reading_cmd)
|
|
|
|
break;
|
|
|
|
reading_param = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!reading_cmd)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (reading_param) {
|
|
|
|
/* overflow means an invalid parameter */
|
|
|
|
if (parampos >= BUFSIZE - 1) {
|
|
|
|
reading_cmd = false;
|
|
|
|
reading_param = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
param[parampos++] = c;
|
|
|
|
} else {
|
|
|
|
/* overflow means an invalid command */
|
|
|
|
if (cmdpos >= BUFSIZE - 1) {
|
|
|
|
reading_cmd = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd[cmdpos++] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 00:48:35 +00:00
|
|
|
void control_send(int control_client,
|
2021-11-04 01:08:26 +00:00
|
|
|
const char *cmd, unsigned cmdlen,
|
|
|
|
const char *param, unsigned paramlen)
|
|
|
|
{
|
|
|
|
unsigned msglen = 0;
|
|
|
|
char buffer[BUFSIZE];
|
|
|
|
|
|
|
|
assert(cmdlen + paramlen + 3 < BUFSIZE);
|
|
|
|
|
|
|
|
buffer[msglen++] = ':';
|
|
|
|
|
|
|
|
memcpy(&buffer[msglen], cmd, cmdlen);
|
|
|
|
msglen += cmdlen;
|
|
|
|
|
|
|
|
if (paramlen > 0) {
|
|
|
|
buffer[msglen++] = '=';
|
|
|
|
memcpy(&buffer[msglen], param, paramlen);
|
|
|
|
msglen += paramlen;
|
|
|
|
buffer[msglen++] = ';';
|
|
|
|
}
|
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
os_socket_send(control_client, buffer, msglen, MSG_NOSIGNAL);
|
2021-11-04 01:08:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
static void control_send_connection_string(int control_client, const std::string& deviceName)
|
2021-11-04 01:08:26 +00:00
|
|
|
{
|
2022-04-07 08:28:48 +00:00
|
|
|
const char *controlVersionCmd = "MangoHudControlVersion";
|
2021-11-04 01:08:26 +00:00
|
|
|
const char *controlVersionString = "1";
|
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
control_send(control_client, controlVersionCmd, strlen(controlVersionCmd),
|
2021-11-04 01:08:26 +00:00
|
|
|
controlVersionString, strlen(controlVersionString));
|
|
|
|
|
|
|
|
const char *deviceCmd = "DeviceName";
|
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
control_send(control_client, deviceCmd, strlen(deviceCmd),
|
|
|
|
deviceName.c_str(), deviceName.size());
|
2021-11-04 01:08:26 +00:00
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
const char *versionCmd = "MangoHudVersion";
|
|
|
|
const char *versionString = "MangoHud " MANGOHUD_VERSION;
|
2021-11-04 01:08:26 +00:00
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
control_send(control_client, versionCmd, strlen(versionCmd),
|
|
|
|
versionString, strlen(versionString));
|
2021-11-04 01:08:26 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
void control_client_check(int control, int& control_client, const std::string& deviceName)
|
2021-11-04 01:08:26 +00:00
|
|
|
{
|
|
|
|
/* Already connected, just return. */
|
2022-07-28 00:48:35 +00:00
|
|
|
if (control_client >= 0){
|
|
|
|
global_control_client = control_client;
|
2021-11-04 01:08:26 +00:00
|
|
|
return;
|
2022-07-28 00:48:35 +00:00
|
|
|
}
|
2021-11-04 01:08:26 +00:00
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
int socket = os_socket_accept(control);
|
2021-11-04 01:08:26 +00:00
|
|
|
if (socket == -1) {
|
|
|
|
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != ECONNABORTED)
|
|
|
|
fprintf(stderr, "ERROR on socket: %s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socket >= 0) {
|
|
|
|
os_socket_block(socket, false);
|
2022-04-07 08:28:48 +00:00
|
|
|
control_client = socket;
|
|
|
|
control_send_connection_string(control_client, deviceName);
|
2021-11-04 01:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
static void control_client_disconnected(int& control_client)
|
2021-11-04 01:08:26 +00:00
|
|
|
{
|
2022-04-07 08:28:48 +00:00
|
|
|
os_socket_close(control_client);
|
|
|
|
control_client = -1;
|
2021-11-04 01:08:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
void process_control_socket(int& control_client, overlay_params ¶ms)
|
2021-11-04 01:08:26 +00:00
|
|
|
{
|
2022-04-07 08:28:48 +00:00
|
|
|
if (control_client >= 0) {
|
2021-11-04 01:08:26 +00:00
|
|
|
char buf[BUFSIZE];
|
|
|
|
|
|
|
|
while (true) {
|
2022-04-07 08:28:48 +00:00
|
|
|
ssize_t n = os_socket_recv(control_client, buf, BUFSIZE, 0);
|
2021-11-04 01:08:26 +00:00
|
|
|
|
|
|
|
if (n == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
/* nothing to read, try again later */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno != ECONNRESET)
|
|
|
|
fprintf(stderr, "ERROR on connection: %s\n", strerror(errno));
|
|
|
|
|
2022-04-07 08:28:48 +00:00
|
|
|
control_client_disconnected(control_client);
|
2021-11-04 01:08:26 +00:00
|
|
|
} else if (n == 0) {
|
|
|
|
/* recv() returns 0 when the client disconnects */
|
2022-04-07 08:28:48 +00:00
|
|
|
control_client_disconnected(control_client);
|
2021-11-04 01:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (ssize_t i = 0; i < n; i++) {
|
2022-04-07 08:28:48 +00:00
|
|
|
process_char(control_client, params, buf[i]);
|
2021-11-04 01:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we try to read BUFSIZE and receive BUFSIZE bytes from the
|
|
|
|
* socket, there's a good chance that there's still more data to be
|
|
|
|
* read, so we will try again. Otherwise, simply be done for this
|
|
|
|
* iteration and try again on the next frame.
|
|
|
|
*/
|
|
|
|
if (n < BUFSIZE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 13:32:35 +00:00
|
|
|
}
|