-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathcontrol.cpp
More file actions
225 lines (192 loc) · 6.1 KB
/
Copy pathcontrol.cpp
File metadata and controls
225 lines (192 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <cstdint>
#include <assert.h>
#include <cerrno>
#include <cstring>
#include <sys/socket.h>
#include "mesa/util/os_socket.h"
#include "overlay.h"
#include "version.h"
#include "app/mangoapp.h"
#include "fps_limiter.h"
int global_control_client;
using namespace std;
static void parse_command(overlay_params ¶ms,
const char *cmd, unsigned cmdlen,
const char *param, unsigned paramlen)
{
if (!strncmp(cmd, "hud", cmdlen)) {
get_params()->no_display = !get_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();
}
else
{
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];
} else if (!strncmp(cmd, "set_fps_limit", cmdlen)) {
if (param && param[0] && fps_limiter) {
char *endptr;
float fps = strtof(param, &endptr);
if (endptr != param && fps >= 0)
fps_limiter->set_target(fps);
}
}
}
#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;
*/
static void process_char(const int control_client, overlay_params ¶ms, char c)
{
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';
parse_command(params, cmd, cmdpos, param, parampos);
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;
}
}
}
void control_send(int control_client,
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++] = ';';
}
os_socket_send(control_client, buffer, msglen, MSG_NOSIGNAL);
}
static void control_send_connection_string(int control_client, const std::string& deviceName)
{
const char *controlVersionCmd = "MangoHudControlVersion";
const char *controlVersionString = "1";
control_send(control_client, controlVersionCmd, strlen(controlVersionCmd),
controlVersionString, strlen(controlVersionString));
const char *deviceCmd = "DeviceName";
control_send(control_client, deviceCmd, strlen(deviceCmd),
deviceName.c_str(), deviceName.size());
const char *versionCmd = "MangoHudVersion";
const char *versionString = "MangoHud " MANGOHUD_VERSION;
control_send(control_client, versionCmd, strlen(versionCmd),
versionString, strlen(versionString));
}
void control_client_check(int control, int& control_client, const std::string& deviceName)
{
/* Already connected, just return. */
if (control_client >= 0){
global_control_client = control_client;
return;
}
int socket = os_socket_accept(control);
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);
control_client = socket;
control_send_connection_string(control_client, deviceName);
}
}
static void control_client_disconnected(int& control_client)
{
os_socket_close(control_client);
control_client = -1;
}
void process_control_socket(int& control_client, overlay_params ¶ms)
{
if (control_client >= 0) {
char buf[BUFSIZE];
while (true) {
ssize_t n = os_socket_recv(control_client, buf, BUFSIZE, 0);
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));
control_client_disconnected(control_client);
} else if (n == 0) {
/* recv() returns 0 when the client disconnects */
control_client_disconnected(control_client);
}
for (ssize_t i = 0; i < n; i++) {
process_char(control_client, params, buf[i]);
}
/* 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;
}
}
}