Skip to content

Latest commit

 

History

History
444 lines (375 loc) · 11.6 KB

steamshim_child.c

File metadata and controls

444 lines (375 loc) · 11.6 KB
 
Jul 25, 2013
Jul 25, 2013
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
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
typedef HANDLE PipeType;
#define NULLPIPE NULL
typedef unsigned __int8 uint8;
typedef __int32 int32;
typedef unsigned __int64 uint64;
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include <signal.h>
typedef uint8_t uint8;
typedef int32_t int32;
typedef uint64_t uint64;
typedef int PipeType;
#define NULLPIPE -1
#endif
#include "steamshim_child.h"
Jul 25, 2013
Jul 25, 2013
28
29
30
31
32
33
34
#define DEBUGPIPE 1
#if DEBUGPIPE
#define dbgpipe printf
#else
static inline void dbgpipe(const char *fmt, ...) {}
#endif
Jul 25, 2013
Jul 25, 2013
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
static int writePipe(PipeType fd, const void *buf, const unsigned int _len);
static int readPipe(PipeType fd, void *buf, const unsigned int _len);
static void closePipe(PipeType fd);
static char *getEnvVar(const char *key, char *buf, const size_t buflen);
static int pipeReady(PipeType fd);
#ifdef _WIN32
static int pipeReady(PipeType fd)
{
DWORD avail = 0;
return (PeekNamedPipe(fd, NULL, 0, NULL, &avail, NULL) && (avail > 0));
} /* pipeReady */
static int writePipe(PipeType fd, const void *buf, const unsigned int _len)
{
const DWORD len = (DWORD) _len;
DWORD bw = 0;
return ((WriteFile(fd, buf, len, &bw, NULL) != 0) && (bw == len));
} /* writePipe */
static int readPipe(PipeType fd, void *buf, const unsigned int _len)
{
const DWORD len = (DWORD) _len;
DWORD br = 0;
return ReadFile(fd, buf, len, &br, NULL) ? (int) br : -1;
} /* readPipe */
static void closePipe(PipeType fd)
{
CloseHandle(fd);
} /* closePipe */
static char *getEnvVar(const char *key, char *buf, const size_t _buflen)
{
const DWORD buflen = (DWORD) _buflen;
const DWORD rc = GetEnvironmentVariableA(key, val, buflen);
/* rc doesn't count null char, hence "<". */
return ((rc > 0) && (rc < buflen)) ? NULL : buf;
} /* getEnvVar */
#else
static int pipeReady(PipeType fd)
{
int rc;
struct pollfd pfd = { fd, POLLIN | POLLERR | POLLHUP, 0 };
while (((rc = poll(&pfd, 1, 0)) == -1) && (errno == EINTR)) { /*spin*/ }
return (rc == 1);
} /* pipeReady */
static int writePipe(PipeType fd, const void *buf, const unsigned int _len)
{
const ssize_t len = (ssize_t) _len;
ssize_t bw;
while (((bw = write(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ }
return (bw == len);
} /* writePipe */
static int readPipe(PipeType fd, void *buf, const unsigned int _len)
{
const ssize_t len = (ssize_t) _len;
ssize_t br;
while (((br = read(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ }
return (int) br;
} /* readPipe */
static void closePipe(PipeType fd)
{
close(fd);
} /* closePipe */
static char *getEnvVar(const char *key, char *buf, const size_t buflen)
{
const char *envr = getenv(key);
Jul 25, 2013
Jul 25, 2013
111
if (!envr || (strlen(envr) >= buflen))
Jul 25, 2013
Jul 25, 2013
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
return NULL;
strcpy(buf, envr);
return buf;
} /* getEnvVar */
#endif
static PipeType GPipeRead = NULLPIPE;
static PipeType GPipeWrite = NULLPIPE;
typedef enum ShimCmd
{
SHIMCMD_BYE,
SHIMCMD_PUMP,
SHIMCMD_REQUESTSTATS,
SHIMCMD_STORESTATS,
SHIMCMD_SETACHIEVEMENT,
SHIMCMD_GETACHIEVEMENT,
SHIMCMD_RESETSTATS,
SHIMCMD_SETSTATI,
SHIMCMD_GETSTATI,
SHIMCMD_SETSTATF,
SHIMCMD_GETSTATF,
} ShimCmd;
static int write1ByteCmd(const uint8 b1)
{
const uint8 buf[] = { 1, b1 };
return writePipe(GPipeWrite, buf, sizeof (buf));
} /* write1ByteCmd */
static int write2ByteCmd(const uint8 b1, const uint8 b2)
{
const uint8 buf[] = { 2, b1, b2 };
return writePipe(GPipeWrite, buf, sizeof (buf));
} /* write2ByteCmd */
static inline int writeBye(void)
{
Jul 25, 2013
Jul 25, 2013
152
dbgpipe("Child sending SHIMCMD_BYE().\n");
Jul 25, 2013
Jul 25, 2013
153
154
155
156
157
158
return write1ByteCmd(SHIMCMD_BYE);
} // writeBye
static int initPipes(void)
{
char buf[64];
Jul 25, 2013
Jul 25, 2013
159
unsigned long long val;
Jul 25, 2013
Jul 25, 2013
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
if (!getEnvVar("STEAMSHIM_READHANDLE", buf, sizeof (buf)))
return 0;
else if (sscanf(buf, "%llu", &val) != 1)
return 0;
else
GPipeRead = (PipeType) val;
if (!getEnvVar("STEAMSHIM_WRITEHANDLE", buf, sizeof (buf)))
return 0;
else if (sscanf(buf, "%llu", &val) != 1)
return 0;
else
GPipeWrite = (PipeType) val;
return ((GPipeRead != NULLPIPE) && (GPipeWrite != NULLPIPE));
} /* initPipes */
int STEAMSHIM_init(void)
{
Jul 25, 2013
Jul 25, 2013
181
dbgpipe("Child init start.\n");
Jul 25, 2013
Jul 25, 2013
182
if (!initPipes())
Jul 25, 2013
Jul 25, 2013
183
184
{
dbgpipe("Child init failed.\n");
Jul 25, 2013
Jul 25, 2013
185
return 0;
Jul 25, 2013
Jul 25, 2013
186
} /* if */
Jul 25, 2013
Jul 25, 2013
187
188
189
signal(SIGPIPE, SIG_IGN);
Jul 25, 2013
Jul 25, 2013
190
dbgpipe("Child init success!\n");
Jul 25, 2013
Jul 25, 2013
191
192
193
194
195
return 1;
} /* STEAMSHIM_init */
void STEAMSHIM_deinit(void)
{
Jul 25, 2013
Jul 25, 2013
196
dbgpipe("Child deinit.\n");
Jul 25, 2013
Jul 25, 2013
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
226
227
228
229
230
231
232
233
234
235
if (GPipeWrite != NULLPIPE)
{
writeBye();
closePipe(GPipeWrite);
} /* if */
if (GPipeRead != NULLPIPE)
closePipe(GPipeRead);
GPipeRead = GPipeWrite = NULLPIPE;
signal(SIGPIPE, SIG_DFL);
} /* STEAMSHIM_deinit */
static inline int isAlive(void)
{
return ((GPipeRead != NULLPIPE) && (GPipeWrite != NULLPIPE));
} /* isAlive */
static inline int isDead(void)
{
return !isAlive();
} /* isDead */
int STEAMSHIM_alive(void)
{
return isAlive();
} /* STEAMSHIM_alive */
static const STEAMSHIM_Event *processEvent(const uint8 *buf, size_t buflen)
{
static STEAMSHIM_Event event;
const STEAMSHIM_EventType type = (STEAMSHIM_EventType) *(buf++);
buflen--;
memset(&event, '\0', sizeof (event));
event.type = type;
event.okay = 1;
Jul 25, 2013
Jul 25, 2013
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#if DEBUGPIPE
if (0) {}
#define PRINTGOTEVENT(x) else if (type == x) printf("Child got " #x ".\n")
PRINTGOTEVENT(SHIMEVENT_BYE);
PRINTGOTEVENT(SHIMEVENT_STATSRECEIVED);
PRINTGOTEVENT(SHIMEVENT_STATSSTORED);
PRINTGOTEVENT(SHIMEVENT_SETACHIEVEMENT);
PRINTGOTEVENT(SHIMEVENT_GETACHIEVEMENT);
PRINTGOTEVENT(SHIMEVENT_RESETSTATS);
PRINTGOTEVENT(SHIMEVENT_SETSTATI);
PRINTGOTEVENT(SHIMEVENT_GETSTATI);
PRINTGOTEVENT(SHIMEVENT_SETSTATF);
PRINTGOTEVENT(SHIMEVENT_GETSTATF);
#undef PRINTGOTEVENT
else printf("Child got unknown shimevent %d.\n", (int) type);
#endif
Jul 25, 2013
Jul 25, 2013
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
switch (type)
{
case SHIMEVENT_BYE:
break;
case SHIMEVENT_STATSRECEIVED:
case SHIMEVENT_STATSSTORED:
if (!buflen) return NULL;
event.okay = *(buf++) ? 1 : 0;
break;
case SHIMEVENT_SETACHIEVEMENT:
if (buflen < 3) return NULL;
event.ivalue = *(buf++) ? 1 : 0;
event.okay = *(buf++) ? 1 : 0;
strcpy(event.name, (const char *) buf);
break;
case SHIMEVENT_GETACHIEVEMENT:
if (buflen < 10) return NULL;
event.ivalue = (int) *(buf++);
if (event.ivalue == 2)
event.ivalue = event.okay = 0;
event.epochsecs = (long long unsigned) *((uint64 *) buf);
buf += sizeof (uint64);
strcpy(event.name, (const char *) buf);
break;
case SHIMEVENT_RESETSTATS:
if (buflen != 2) return NULL;
event.ivalue = *(buf++) ? 1 : 0;
event.okay = *(buf++) ? 1 : 0;
break;
case SHIMEVENT_SETSTATI:
case SHIMEVENT_GETSTATI:
event.okay = *(buf++) ? 1 : 0;
event.ivalue = (int) *((int32 *) buf);
buf += sizeof (int32);
strcpy(event.name, (const char *) buf);
break;
case SHIMEVENT_SETSTATF:
case SHIMEVENT_GETSTATF:
event.okay = *(buf++) ? 1 : 0;
event.fvalue = (int) *((float *) buf);
buf += sizeof (float);
strcpy(event.name, (const char *) buf);
break;
default: /* uh oh */
return NULL;
} /* switch */
return &event;
} /* processEvent */
const STEAMSHIM_Event *STEAMSHIM_pump(void)
{
static uint8 buf[256];
static int br = 0;
int evlen = (br > 0) ? ((int) buf[0]) : 0;
if (isDead())
return NULL;
if (br <= evlen) /* we have an incomplete commmand. Try to read more. */
{
if (pipeReady(GPipeRead))
{
const int morebr = readPipe(GPipeRead, buf + br, sizeof (buf) - br);
Jul 25, 2013
Jul 25, 2013
324
if (morebr > 0)
Jul 25, 2013
Jul 25, 2013
325
br += morebr;
Jul 25, 2013
Jul 25, 2013
326
327
328
329
330
else /* uh oh */
{
dbgpipe("Child readPipe failed! Shutting down.\n");
STEAMSHIM_deinit(); /* kill it all. */
} /* else */
Jul 25, 2013
Jul 25, 2013
331
332
333
334
335
336
337
338
339
340
341
342
343
344
} /* if */
} /* if */
if (evlen && (br > evlen))
{
const STEAMSHIM_Event *retval = processEvent(buf+1, evlen);
br -= evlen + 1;
if (br > 0)
memmove(buf, buf+evlen+1, br);
return retval;
} /* if */
/* Run Steam event loop. */
if (br == 0)
Jul 25, 2013
Jul 25, 2013
345
346
{
dbgpipe("Child sending SHIMCMD_PUMP().\n");
Jul 25, 2013
Jul 25, 2013
347
write1ByteCmd(SHIMCMD_PUMP);
Jul 25, 2013
Jul 25, 2013
348
} /* if */
Jul 25, 2013
Jul 25, 2013
349
350
351
352
353
354
355
return NULL;
} /* STEAMSHIM_pump */
void STEAMSHIM_requestStats(void)
{
if (isDead()) return;
Jul 25, 2013
Jul 25, 2013
356
dbgpipe("Child sending SHIMCMD_REQUESTSTATS().\n");
Jul 25, 2013
Jul 25, 2013
357
358
359
360
361
362
write1ByteCmd(SHIMCMD_REQUESTSTATS);
} /* STEAMSHIM_requestStats */
void STEAMSHIM_storeStats(void)
{
if (isDead()) return;
Jul 25, 2013
Jul 25, 2013
363
dbgpipe("Child sending SHIMCMD_STORESTATS().\n");
Jul 25, 2013
Jul 25, 2013
364
365
366
367
368
369
370
371
write1ByteCmd(SHIMCMD_STORESTATS);
} /* STEAMSHIM_storeStats */
void STEAMSHIM_setAchievement(const char *name, const int enable)
{
uint8 buf[256];
uint8 *ptr = buf+1;
if (isDead()) return;
Jul 25, 2013
Jul 25, 2013
372
dbgpipe("Child sending SHIMCMD_SETACHIEVEMENT('%s', %senable).\n", name, enable ? "" : "!");
Jul 25, 2013
Jul 25, 2013
373
374
375
376
377
378
379
380
381
382
383
384
385
*(ptr++) = (uint8) SHIMCMD_SETACHIEVEMENT;
*(ptr++) = enable ? 1 : 0;
strcpy((char *) ptr, name);
ptr += strlen(name) + 1;
buf[0] = (uint8) ((ptr-1) - buf);
writePipe(GPipeWrite, buf, buf[0] + 1);
} /* STEAMSHIM_setAchievement */
void STEAMSHIM_getAchievement(const char *name)
{
uint8 buf[256];
uint8 *ptr = buf+1;
if (isDead()) return;
Jul 25, 2013
Jul 25, 2013
386
dbgpipe("Child sending SHIMCMD_GETACHIEVEMENT('%s').\n", name);
Jul 25, 2013
Jul 25, 2013
387
388
389
390
391
392
393
394
395
396
*(ptr++) = (uint8) SHIMCMD_GETACHIEVEMENT;
strcpy((char *) ptr, name);
ptr += strlen(name) + 1;
buf[0] = (uint8) ((ptr-1) - buf);
writePipe(GPipeWrite, buf, buf[0] + 1);
} /* STEAMSHIM_getAchievement */
void STEAMSHIM_resetStats(const int bAlsoAchievements)
{
if (isDead()) return;
Jul 25, 2013
Jul 25, 2013
397
dbgpipe("Child sending SHIMCMD_RESETSTATS(%salsoAchievements).\n", bAlsoAchievements ? "" : "!");
Jul 25, 2013
Jul 25, 2013
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
write2ByteCmd(SHIMCMD_RESETSTATS, bAlsoAchievements ? 1 : 0);
} /* STEAMSHIM_resetStats */
static void writeStatThing(const ShimCmd cmd, const char *name, const void *val, const size_t vallen)
{
uint8 buf[256];
uint8 *ptr = buf+1;
if (isDead()) return;
*(ptr++) = (uint8) cmd;
if (vallen)
{
memcpy(ptr, val, vallen);
ptr += vallen;
} /* if */
strcpy((char *) ptr, name);
ptr += strlen(name) + 1;
buf[0] = (uint8) ((ptr-1) - buf);
writePipe(GPipeWrite, buf, buf[0] + 1);
} /* writeStatThing */
void STEAMSHIM_setStatI(const char *name, const int _val)
{
const int32 val = (int32) _val;
Jul 25, 2013
Jul 25, 2013
421
dbgpipe("Child sending SHIMCMD_SETSTATI('%s', val %d).\n", name, val);
Jul 25, 2013
Jul 25, 2013
422
423
424
425
426
writeStatThing(SHIMCMD_SETSTATI, name, &val, sizeof (val));
} /* STEAMSHIM_setStatI */
void STEAMSHIM_getStatI(const char *name)
{
Jul 25, 2013
Jul 25, 2013
427
dbgpipe("Child sending SHIMCMD_GETSTATI('%s').\n", name);
Jul 25, 2013
Jul 25, 2013
428
429
430
431
432
writeStatThing(SHIMCMD_GETSTATI, name, NULL, 0);
} /* STEAMSHIM_getStatI */
void STEAMSHIM_setStatF(const char *name, const float val)
{
Jul 25, 2013
Jul 25, 2013
433
dbgpipe("Child sending SHIMCMD_SETSTATF('%s', val %f).\n", name, val);
Jul 25, 2013
Jul 25, 2013
434
435
436
437
438
writeStatThing(SHIMCMD_SETSTATF, name, &val, sizeof (val));
} /* STEAMSHIM_setStatF */
void STEAMSHIM_getStatF(const char *name)
{
Jul 25, 2013
Jul 25, 2013
439
dbgpipe("Child sending SHIMCMD_GETSTATF('%s').\n", name);
Jul 25, 2013
Jul 25, 2013
440
441
442
443
writeStatThing(SHIMCMD_GETSTATF, name, NULL, 0);
} /* STEAMSHIM_getStatF */
/* end of steamshim_child.c ... */