steamshim_parent.cpp
author Ryan C. Gordon <icculus@icculus.org>
Wed, 16 Sep 2015 01:33:55 -0400
changeset 15 4018ba1b0e14
parent 8 08d5ec77d929
permissions -rw-r--r--
A couple README updates.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
     1
#define GAME_LAUNCH_NAME "testapp"
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#ifndef GAME_LAUNCH_NAME
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#error Please define your game exe name.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#endif
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
#ifdef _WIN32
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
#define WIN32_LEAN_AND_MEAN 1
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
#include <windows.h>
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
typedef PROCESS_INFORMATION ProcessType;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
typedef HANDLE PipeType;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#define NULLPIPE NULL
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#else
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include <stdio.h>
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#include <stdlib.h>
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include <string.h>
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include <unistd.h>
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#include <errno.h>
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#include <sys/wait.h>
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#include <signal.h>
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
typedef pid_t ProcessType;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
typedef int PipeType;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
#define NULLPIPE -1
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#endif
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
#include "steam/steam_api.h"
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
    27
#define DEBUGPIPE 1
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
    28
#if DEBUGPIPE
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
    29
#define dbgpipe printf
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
    30
#else
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
    31
static inline void dbgpipe(const char *fmt, ...) {}
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
    32
#endif
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
    33
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
/* platform-specific mainline calls this. */
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
static int mainline(void);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
/* Windows and Unix implementations of this stuff below. */
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
static void fail(const char *err);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
static bool writePipe(PipeType fd, const void *buf, const unsigned int _len);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
static int readPipe(PipeType fd, void *buf, const unsigned int _len);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
static bool createPipes(PipeType *pPipeParentRead, PipeType *pPipeParentWrite,
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
                        PipeType *pPipeChildRead, PipeType *pPipeChildWrite);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
static void closePipe(PipeType fd);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
static bool setEnvVar(const char *key, const char *val);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
static bool launchChild(ProcessType *pid);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
static int closeProcess(ProcessType *pid);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
#ifdef _WIN32
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
static void fail(const char *err)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    MessageBoxA(NULL, err, "ERROR", MB_ICONERROR | MB_OK);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    ExitProcess(1);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
} // fail
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
static bool writePipe(PipeType fd, const void *buf, const unsigned int _len)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    const DWORD len = (DWORD) _len;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    DWORD bw = 0;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    return ((WriteFile(fd, buf, len, &bw, NULL) != 0) && (bw == len));
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
} // writePipe
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
static int readPipe(PipeType fd, void *buf, const unsigned int _len)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    const DWORD len = (DWORD) _len;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    DWORD br = 0;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    return ReadFile(fd, buf, len, &br, NULL) ? (int) br : -1;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
} // readPipe
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
static bool createPipes(PipeType *pPipeParentRead, PipeType *pPipeParentWrite,
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
                        PipeType *pPipeChildRead, PipeType *pPipeChildWrite)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    SECURITY_ATTRIBUTES pipeAttr;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    pipeAttr.nLength = sizeof (pipeAttr);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    pipeAttr.lpSecurityDescriptor = NULL;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    pipeAttr.bInheritHandle = TRUE;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    if (!CreatePipe(pPipeParentRead, pPipeChildWrite, &pipeAttr, 0))
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
        return 0;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    pipeAttr.nLength = sizeof (pipeAttr);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    pipeAttr.lpSecurityDescriptor = NULL;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    pipeAttr.bInheritHandle = TRUE;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    if (!CreatePipe(pPipeChildRead, pPipeParentWrite, &pipeAttr, 0))
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    {
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
        CloseHandle(*pPipeParentRead);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
        CloseHandle(*pPipeChildWrite);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
        return 0;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    } // if
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    return 1;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
} // createPipes
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
static void closePipe(PipeType fd)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    CloseHandle(fd);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
} // closePipe
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
static bool setEnvVar(const char *key, const char *val)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    return (SetEnvironmentVariableA(key, val) != 0);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
} // setEnvVar
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
static bool launchChild(ProcessType *pid);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    return (CreateProcessW(TEXT(".\\") TEXT(GAME_LAUNCH_NAME) TEXT(".exe"),
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
                           GetCommandLineW(), NULL, NULL, TRUE, 0, NULL,
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
                           NULL, NULL, pid) != 0);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
} // launchChild
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
static int closeProcess(ProcessType *pid)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    CloseHandle(pid->hProcess);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
    CloseHandle(pid->hThread);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    return 0;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
} // closeProcess
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
                     LPSTR lpCmdLine, int nCmdShow)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
    mainline();
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    ExitProcess(0);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    return 0;  // just in case.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
} // WinMain
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
#else  // everyone else that isn't Windows.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
static void fail(const char *err)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    // !!! FIXME: zenity or something.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    fprintf(stderr, "%s\n", err);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    _exit(1);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
} // fail
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
static bool writePipe(PipeType fd, const void *buf, const unsigned int _len)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    const ssize_t len = (ssize_t) _len;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    ssize_t bw;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    while (((bw = write(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ }
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    return (bw == len);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
} // writePipe
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
static int readPipe(PipeType fd, void *buf, const unsigned int _len)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    const ssize_t len = (ssize_t) _len;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    ssize_t br;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    while (((br = read(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ }
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    return (int) br;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
} // readPipe
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
static bool createPipes(PipeType *pPipeParentRead, PipeType *pPipeParentWrite,
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
                        PipeType *pPipeChildRead, PipeType *pPipeChildWrite)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    int fds[2];
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    if (pipe(fds) == -1)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
        return 0;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    *pPipeParentRead = fds[0];
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    *pPipeChildWrite = fds[1];
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    if (pipe(fds) == -1)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    {
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
        close(*pPipeParentRead);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
        close(*pPipeChildWrite);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
        return 0;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    } // if
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    *pPipeChildRead = fds[0];
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    *pPipeParentWrite = fds[1];
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    return 1;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
} // createPipes
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
static void closePipe(PipeType fd)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    close(fd);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
} // closePipe
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
static bool setEnvVar(const char *key, const char *val)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    return (setenv(key, val, 1) != -1);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
} // setEnvVar
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
static int GArgc = 0;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
static char **GArgv = NULL;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
static bool launchChild(ProcessType *pid)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    *pid = fork();
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    if (*pid == -1)   // failed
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
        return false;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    else if (*pid != 0)  // we're the parent
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
        return true;  // we'll let the pipe fail if this didn't work.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    // we're the child.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    GArgv[0] = strdup("./" GAME_LAUNCH_NAME);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    execvp(GArgv[0], GArgv);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
    // still here? It failed! Terminate, closing child's ends of the pipes.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    _exit(1);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
} // launchChild
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
static int closeProcess(ProcessType *pid)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
{
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   203
    int rc = 0;
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    while ((waitpid(*pid, &rc, 0) == -1) && (errno == EINTR)) { /*spin*/ }
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    if (!WIFEXITED(rc))
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
        return 1;  // oh well.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    return WEXITSTATUS(rc);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
} // closeProcess
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
int main(int argc, char **argv)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    signal(SIGPIPE, SIG_IGN);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
    GArgc = argc;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    GArgv = argv;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    return mainline();
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
} // main
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
#endif
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
// THE ACTUAL PROGRAM.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   223
class SteamBridge;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   224
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   225
static ISteamUserStats *GSteamStats = NULL;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   226
static ISteamUtils *GSteamUtils = NULL;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   227
static ISteamUser *GSteamUser = NULL;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   228
static AppId_t GAppID = 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   229
static uint64 GUserID = 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   230
static SteamBridge *GSteamBridge = NULL;
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   232
class SteamBridge
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
{
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   234
public:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   235
    SteamBridge(PipeType _fd);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   236
	STEAM_CALLBACK(SteamBridge, OnUserStatsReceived, UserStatsReceived_t, m_CallbackUserStatsReceived);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   237
	STEAM_CALLBACK(SteamBridge, OnUserStatsStored, UserStatsStored_t, m_CallbackUserStatsStored);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   238
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   239
private:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   240
    PipeType fd;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   241
};
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   242
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   243
typedef enum ShimCmd
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   244
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   245
    SHIMCMD_BYE,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   246
    SHIMCMD_PUMP,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   247
    SHIMCMD_REQUESTSTATS,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   248
    SHIMCMD_STORESTATS,
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    SHIMCMD_SETACHIEVEMENT,
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    SHIMCMD_GETACHIEVEMENT,
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   251
    SHIMCMD_RESETSTATS,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   252
    SHIMCMD_SETSTATI,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   253
    SHIMCMD_GETSTATI,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   254
    SHIMCMD_SETSTATF,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   255
    SHIMCMD_GETSTATF,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   256
} ShimCmd;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   257
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   258
typedef enum ShimEvent
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   259
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   260
    SHIMEVENT_BYE,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   261
    SHIMEVENT_STATSRECEIVED,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   262
    SHIMEVENT_STATSSTORED,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   263
    SHIMEVENT_SETACHIEVEMENT,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   264
    SHIMEVENT_GETACHIEVEMENT,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   265
    SHIMEVENT_RESETSTATS,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   266
    SHIMEVENT_SETSTATI,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   267
    SHIMEVENT_GETSTATI,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   268
    SHIMEVENT_SETSTATF,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   269
    SHIMEVENT_GETSTATF,
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   270
} ShimEvent;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   271
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   272
static bool write1ByteCmd(PipeType fd, const uint8 b1)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   273
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   274
    const uint8 buf[] = { 1, b1 };
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   275
    return writePipe(fd, buf, sizeof (buf));
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   276
} // write1ByteCmd
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   277
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   278
static bool write2ByteCmd(PipeType fd, const uint8 b1, const uint8 b2)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   279
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   280
    const uint8 buf[] = { 2, b1, b2 };
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   281
    return writePipe(fd, buf, sizeof (buf));
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   282
} // write2ByteCmd
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   284
static bool write3ByteCmd(PipeType fd, const uint8 b1, const uint8 b2, const uint8 b3)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   285
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   286
    const uint8 buf[] = { 3, b1, b2, b3 };
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   287
    return writePipe(fd, buf, sizeof (buf));
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   288
} // write3ByteCmd
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   289
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   290
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   291
static inline bool writeBye(PipeType fd)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   292
{
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   293
    dbgpipe("Parent sending SHIMEVENT_BYE().\n");
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   294
    return write1ByteCmd(fd, SHIMEVENT_BYE);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   295
} // writeBye
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   296
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   297
static inline bool writeStatsReceived(PipeType fd, const bool okay)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   298
{
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   299
    dbgpipe("Parent sending SHIMEVENT_STATSRECEIVED(%sokay).\n", okay ? "" : "!");
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   300
    return write2ByteCmd(fd, SHIMEVENT_STATSRECEIVED, okay ? 1 : 0);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   301
} // writeStatsReceived
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   302
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   303
static inline bool writeStatsStored(PipeType fd, const bool okay)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   304
{
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   305
    dbgpipe("Parent sending SHIMEVENT_STATSSTORED(%sokay).\n", okay ? "" : "!");
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   306
    return write2ByteCmd(fd, SHIMEVENT_STATSSTORED, okay ? 1 : 0);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   307
} // writeStatsStored
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   308
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   309
static bool writeAchievementSet(PipeType fd, const char *name, const bool enable, const bool okay)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   310
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   311
    uint8 buf[256];
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   312
    uint8 *ptr = buf+1;
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   313
    dbgpipe("Parent sending SHIMEVENT_SETACHIEVEMENT('%s', %senable, %sokay).\n", name, enable ? "" : "!", okay ? "" : "!");
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   314
    *(ptr++) = (uint8) SHIMEVENT_SETACHIEVEMENT;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   315
    *(ptr++) = enable ? 1 : 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   316
    *(ptr++) = okay ? 1 : 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   317
    strcpy((char *) ptr, name);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   318
    ptr += strlen(name) + 1;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   319
    buf[0] = (uint8) ((ptr-1) - buf);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   320
    return writePipe(fd, buf, buf[0] + 1);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   321
} // writeAchievementSet
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   322
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   323
static bool writeAchievementGet(PipeType fd, const char *name, const int status, const uint64 time)
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
{
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   325
    uint8 buf[256];
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   326
    uint8 *ptr = buf+1;
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   327
    dbgpipe("Parent sending SHIMEVENT_GETACHIEVEMENT('%s', status %d, time %llu).\n", name, status, (unsigned long long) time);
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   328
    *(ptr++) = (uint8) SHIMEVENT_GETACHIEVEMENT;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   329
    *(ptr++) = (uint8) status;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   330
    memcpy(ptr, &time, sizeof (time));
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   331
    ptr += sizeof (time);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   332
    strcpy((char *) ptr, name);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   333
    ptr += strlen(name) + 1;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   334
    buf[0] = (uint8) ((ptr-1) - buf);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   335
    return writePipe(fd, buf, buf[0] + 1);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   336
} // writeAchievementGet
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   337
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   338
static inline bool writeResetStats(PipeType fd, const bool alsoAch, const bool okay)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   339
{
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   340
    dbgpipe("Parent sending SHIMEVENT_RESETSTATS(%salsoAchievements, %sokay).\n", alsoAch ? "" : "!", okay ? "" : "!");
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   341
    return write3ByteCmd(fd, SHIMEVENT_RESETSTATS, alsoAch ? 1 : 0, okay ? 1 : 0);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   342
} // writeResetStats
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   343
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   344
static bool writeStatThing(PipeType fd, const ShimEvent ev, const char *name, const void *val, const size_t vallen, const bool okay)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   345
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   346
    uint8 buf[256];
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   347
    uint8 *ptr = buf+1;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   348
    *(ptr++) = (uint8) ev;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   349
    *(ptr++) = okay ? 1 : 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   350
    memcpy(ptr, val, vallen);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   351
    ptr += vallen;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   352
    strcpy((char *) ptr, name);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   353
    ptr += strlen(name) + 1;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   354
    buf[0] = (uint8) ((ptr-1) - buf);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   355
    return writePipe(fd, buf, buf[0] + 1);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   356
} // writeStatThing
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   357
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   358
static inline bool writeSetStatI(PipeType fd, const char *name, const int32 val, const bool okay)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   359
{
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   360
    dbgpipe("Parent sending SHIMEVENT_SETSTATI('%s', val %d, %sokay).\n", name, (int) val, okay ? "" : "!");
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   361
    return writeStatThing(fd, SHIMEVENT_SETSTATI, name, &val, sizeof (val), okay);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   362
} // writeSetStatI
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   364
static inline bool writeSetStatF(PipeType fd, const char *name, const float val, const bool okay)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   365
{
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   366
    dbgpipe("Parent sending SHIMEVENT_SETSTATF('%s', val %f, %sokay).\n", name, val, okay ? "" : "!");
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   367
    return writeStatThing(fd, SHIMEVENT_SETSTATF, name, &val, sizeof (val), okay);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   368
} // writeSetStatF
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   369
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   370
static inline bool writeGetStatI(PipeType fd, const char *name, const int32 val, const bool okay)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   371
{
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   372
    dbgpipe("Parent sending SHIMEVENT_GETSTATI('%s', val %d, %sokay).\n", name, (int) val, okay ? "" : "!");
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   373
    return writeStatThing(fd, SHIMEVENT_GETSTATI, name, &val, sizeof (val), okay);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   374
} // writeGetStatI
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   375
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   376
static inline bool writeGetStatF(PipeType fd, const char *name, const float val, const bool okay)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   377
{
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   378
    dbgpipe("Parent sending SHIMEVENT_GETSTATF('%s', val %f, %sokay).\n", name, val, okay ? "" : "!");
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   379
    return writeStatThing(fd, SHIMEVENT_GETSTATF, name, &val, sizeof (val), okay);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   380
} // writeGetStatF
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   381
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   382
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   383
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   384
SteamBridge::SteamBridge(PipeType _fd)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   385
    : m_CallbackUserStatsReceived( this, &SteamBridge::OnUserStatsReceived )
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   386
	, m_CallbackUserStatsStored( this, &SteamBridge::OnUserStatsStored )
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   387
	, fd(_fd)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   388
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   389
} // SteamBridge::SteamBridge
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   390
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   391
void SteamBridge::OnUserStatsReceived(UserStatsReceived_t *pCallback)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   392
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   393
	if (GAppID != pCallback->m_nGameID) return;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   394
	if (GUserID != pCallback->m_steamIDUser.ConvertToUint64()) return;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   395
    writeStatsReceived(fd, pCallback->m_eResult == k_EResultOK);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   396
} // SteamBridge::OnUserStatsReceived
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   397
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   398
void SteamBridge::OnUserStatsStored(UserStatsStored_t *pCallback)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   399
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   400
	if (GAppID != pCallback->m_nGameID) return;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   401
    writeStatsStored(fd, pCallback->m_eResult == k_EResultOK);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   402
} // SteamBridge::OnUserStatsStored
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   403
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   404
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   405
static bool processCommand(const uint8 *buf, unsigned int buflen, PipeType fd)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   406
{
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    if (buflen == 0)
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   408
        return true;
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   410
    const ShimCmd cmd = (ShimCmd) *(buf++);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   411
    buflen--;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   412
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   413
    #if DEBUGPIPE
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   414
    if (false) {}
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   415
    #define PRINTGOTCMD(x) else if (cmd == x) printf("Parent got " #x ".\n")
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   416
    PRINTGOTCMD(SHIMCMD_BYE);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   417
    PRINTGOTCMD(SHIMCMD_PUMP);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   418
    PRINTGOTCMD(SHIMCMD_REQUESTSTATS);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   419
    PRINTGOTCMD(SHIMCMD_STORESTATS);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   420
    PRINTGOTCMD(SHIMCMD_SETACHIEVEMENT);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   421
    PRINTGOTCMD(SHIMCMD_GETACHIEVEMENT);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   422
    PRINTGOTCMD(SHIMCMD_RESETSTATS);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   423
    PRINTGOTCMD(SHIMCMD_SETSTATI);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   424
    PRINTGOTCMD(SHIMCMD_GETSTATI);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   425
    PRINTGOTCMD(SHIMCMD_SETSTATF);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   426
    PRINTGOTCMD(SHIMCMD_GETSTATF);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   427
    #undef PRINTGOTCMD
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   428
    else printf("Parent got unknown shimcmd %d.\n", (int) cmd);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   429
    #endif
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   430
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
    switch (cmd)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
    {
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   433
        case SHIMCMD_PUMP:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   434
            SteamAPI_RunCallbacks();
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   435
            break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   436
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   437
        case SHIMCMD_BYE:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   438
            writeBye(fd);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   439
            return false;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   440
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   441
        case SHIMCMD_REQUESTSTATS:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   442
            if ((!GSteamStats) || (!GSteamStats->RequestCurrentStats()))
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   443
                writeStatsReceived(fd, false);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   444
            // callback later.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   445
            break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   446
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   447
        case SHIMCMD_STORESTATS:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   448
            if ((!GSteamStats) || (!GSteamStats->StoreStats()))
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   449
                writeStatsStored(fd, false);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   450
            // callback later.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   451
            break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   452
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   453
        case SHIMCMD_SETACHIEVEMENT:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   454
            if (buflen >= 2)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   455
            {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   456
                const bool enable = (*(buf++) != 0);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   457
                const char *name = (const char *) buf;   // !!! FIXME: buffer overflow possible.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   458
                if (!GSteamStats)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   459
                    writeAchievementSet(fd, name, enable, false);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   460
                else if (enable && !GSteamStats->SetAchievement(name))
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   461
                    writeAchievementSet(fd, name, enable, false);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   462
                else if (!enable && !GSteamStats->ClearAchievement(name))
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   463
                    writeAchievementSet(fd, name, enable, false);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   464
                else
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   465
                    writeAchievementSet(fd, name, enable, true);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   466
            } // if
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   467
            break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   468
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   469
        case SHIMCMD_GETACHIEVEMENT:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   470
            if (buflen)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   471
            {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   472
                const char *name = (const char *) buf;   // !!! FIXME: buffer overflow possible.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   473
                bool ach = false;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   474
	            uint32 t = 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   475
                if ((GSteamStats) && (GSteamStats->GetAchievementAndUnlockTime(name, &ach, &t)))
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   476
                    writeAchievementGet(fd, name, ach ? 1 : 0, t);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   477
	            else
3
6945d8e9267d Added child code.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   478
                    writeAchievementGet(fd, name, 2, 0);
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   479
            } // if
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   480
            break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   481
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   482
        case SHIMCMD_RESETSTATS:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   483
            if (buflen)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   484
            {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   485
                const bool alsoAch = (*(buf++) != 0);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   486
                writeResetStats(fd, alsoAch, (GSteamStats) && (GSteamStats->ResetAllStats(alsoAch)));
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   487
            } // if
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   488
            break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   489
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   490
        case SHIMCMD_SETSTATI:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   491
            if (buflen >= 5)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   492
            {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   493
                const int32 val = *((int32 *) buf);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   494
                buf += sizeof (int32);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   495
                const char *name = (const char *) buf;   // !!! FIXME: buffer overflow possible.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   496
                writeSetStatI(fd, name, val, (GSteamStats) && (GSteamStats->SetStat(name, val)));
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   497
            } // if
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   498
            break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   499
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   500
        case SHIMCMD_GETSTATI:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   501
            if (buflen)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   502
            {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   503
                const char *name = (const char *) buf;   // !!! FIXME: buffer overflow possible.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   504
                int32 val = 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   505
                if ((GSteamStats) && (GSteamStats->GetStat(name, &val)))
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   506
                    writeGetStatI(fd, name, val, true);
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   507
                else
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   508
                    writeGetStatI(fd, name, 0, false);
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   509
            } // if
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   510
            break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   511
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   512
        case SHIMCMD_SETSTATF:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   513
            if (buflen >= 5)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   514
            {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   515
                const float val = *((float *) buf);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   516
                buf += sizeof (float);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   517
                const char *name = (const char *) buf;   // !!! FIXME: buffer overflow possible.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   518
                writeSetStatF(fd, name, val, (GSteamStats) && (GSteamStats->SetStat(name, val)));
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   519
            } // if
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   520
            break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   521
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   522
        case SHIMCMD_GETSTATF:
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   523
            if (buflen)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   524
            {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   525
                const char *name = (const char *) buf;   // !!! FIXME: buffer overflow possible.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   526
                float val = 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   527
                if ((GSteamStats) && (GSteamStats->GetStat(name, &val)))
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   528
                    writeGetStatF(fd, name, val, true);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   529
                else
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   530
                    writeGetStatF(fd, name, 0.0f, false);
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   531
            } // if
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   532
            break;
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
    } // switch
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   534
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   535
    return true;  // keep going.
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
} // processCommand
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
static void processCommands(PipeType pipeParentRead, PipeType pipeParentWrite)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
{
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   540
    bool quit = false;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   541
    uint8 buf[256];
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   542
    int br;
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   544
    // this read blocks.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   545
    while (!quit && ((br = readPipe(pipeParentRead, buf, sizeof (buf))) > 0))
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   546
    {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   547
        while (br > 0)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   548
        {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   549
            const int cmdlen = (int) buf[0];
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   550
            if ((br-1) >= cmdlen)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   551
            {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   552
                if (!processCommand(buf+1, cmdlen, pipeParentWrite))
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   553
                {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   554
                    quit = true;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   555
                    break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   556
                } // if
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   557
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   558
                br -= cmdlen + 1;
3
6945d8e9267d Added child code.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   559
                if (br > 0)
6945d8e9267d Added child code.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   560
                    memmove(buf, buf+cmdlen+1, br);
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   561
            } // if
3
6945d8e9267d Added child code.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   562
            else  // get more data.
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   563
            {
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   564
                const int morebr = readPipe(pipeParentRead, buf+br, sizeof (buf) - br);
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   565
                if (morebr <= 0)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   566
                {
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   567
                    quit = true;  // uhoh.
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   568
                    break;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   569
                } // if
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   570
                br += morebr;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   571
            } // else
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   572
        } // while
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   573
    } // while
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
} // processCommands
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
static bool setEnvironmentVars(PipeType pipeChildRead, PipeType pipeChildWrite)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
    char buf[64];
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   579
    snprintf(buf, sizeof (buf), "%llu", (unsigned long long) pipeChildRead);
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
    if (!setEnvVar("STEAMSHIM_READHANDLE", buf))
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
        return false;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   583
    snprintf(buf, sizeof (buf), "%llu", (unsigned long long) pipeChildWrite);
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
    if (!setEnvVar("STEAMSHIM_WRITEHANDLE", buf))
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
        return false;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
    return true;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
} // setEnvironmentVars
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   590
static bool initSteamworks(PipeType fd)
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
{
8
08d5ec77d929 Added an explanation.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   592
    // this can fail for many reasons:
08d5ec77d929 Added an explanation.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   593
    //  - you forgot a steam_appid.txt in the current working directory.
08d5ec77d929 Added an explanation.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   594
    //  - you don't have Steam running
08d5ec77d929 Added an explanation.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   595
    //  - you don't own the game listed in steam_appid.txt
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
    if (!SteamAPI_Init())
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
        return 0;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   599
    GSteamStats = SteamUserStats();
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   600
    GSteamUtils = SteamUtils();
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   601
    GSteamUser = SteamUser();
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   602
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   603
    GAppID = GSteamUtils ? GSteamUtils->GetAppID() : 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   604
	GUserID = GSteamUser ? GSteamUser->GetSteamID().ConvertToUint64() : 0;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   605
    GSteamBridge = new SteamBridge(fd);
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
    return 1;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
} // initSteamworks
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   610
static void deinitSteamworks(void)
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   611
{
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   612
    SteamAPI_Shutdown();
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   613
    delete GSteamBridge;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   614
    GSteamBridge = NULL;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   615
    GSteamStats = NULL;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   616
    GSteamUtils= NULL;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   617
    GSteamUser = NULL;
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   618
} // deinitSteamworks
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   619
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
static int mainline(void)
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
{
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
    PipeType pipeParentRead = NULLPIPE;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
    PipeType pipeParentWrite = NULLPIPE;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
    PipeType pipeChildRead = NULLPIPE;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
    PipeType pipeChildWrite = NULLPIPE;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
    ProcessType childPid;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   628
    dbgpipe("Parent starting mainline.\n");
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   629
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   630
    if (!createPipes(&pipeParentRead, &pipeParentWrite, &pipeChildRead, &pipeChildWrite))
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   631
        fail("Failed to create application pipes");
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   632
    else if (!initSteamworks(pipeParentWrite))
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
        fail("Failed to initialize Steamworks");
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
    else if (!setEnvironmentVars(pipeChildRead, pipeChildWrite))
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
        fail("Failed to set environment variables");
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
    else if (!launchChild(&childPid))
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
        fail("Failed to launch application");
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
    // Close the ends of the pipes that the child will use; we don't need them.
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
    closePipe(pipeChildRead);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
    closePipe(pipeChildWrite);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
    pipeChildRead = pipeChildWrite = NULLPIPE;
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   644
    dbgpipe("Parent in command processing loop.\n");
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   645
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
    // Now, we block for instructions until the pipe fails (child closed it or
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
    //  terminated/crashed).
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
    processCommands(pipeParentRead, pipeParentWrite);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   650
    dbgpipe("Parent shutting down.\n");
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   651
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
    // Close our ends of the pipes.
3
6945d8e9267d Added child code.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   653
    writeBye(pipeParentWrite);
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
    closePipe(pipeParentRead);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
    closePipe(pipeParentWrite);
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
1
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   657
    deinitSteamworks();
1b29e8b80cf3 Bunch more work.
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
   658
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   659
    dbgpipe("Parent waiting on child process.\n");
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   660
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
    // Wait for the child to terminate, close the child process handles.
5
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   662
    const int retval = closeProcess(&childPid);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   663
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   664
    dbgpipe("Parent exiting mainline (child exit code %d).\n", retval);
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   665
edd9f2f020eb A bunch of work. Seems...complete and largely debugged, now?
Ryan C. Gordon <icculus@icculus.org>
parents: 3
diff changeset
   666
    return retval;
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
} // mainline
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
3
6945d8e9267d Added child code.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   669
// end of steamshim_parent.cpp ...
0
c5d6c455c76c First pile of work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670