testapp.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 16 Sep 2015 00:55:01 -0400
changeset 9 47ad19ad0536
parent 7 591f5e984cc0
child 11 41535db7e805
permissions -rw-r--r--
Ten seconds was way too much to wait here.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
#include "steamshim_child.h"
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#include <stdio.h>
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#include <stdlib.h>
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include <time.h>
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
#include <unistd.h>
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
static void printEvent(const STEAMSHIM_Event *e)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
{
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
    if (!e) return;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
    printf("CHILD EVENT: ");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
    switch (e->type)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
    {
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
        #define PRINTGOTEVENT(x) case SHIMEVENT_##x: printf("%s(", #x); break
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
        PRINTGOTEVENT(BYE);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
        PRINTGOTEVENT(STATSRECEIVED);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
        PRINTGOTEVENT(STATSSTORED);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
        PRINTGOTEVENT(SETACHIEVEMENT);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
        PRINTGOTEVENT(GETACHIEVEMENT);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
        PRINTGOTEVENT(RESETSTATS);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
        PRINTGOTEVENT(SETSTATI);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
        PRINTGOTEVENT(GETSTATI);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
        PRINTGOTEVENT(SETSTATF);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
        PRINTGOTEVENT(GETSTATF);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
        #undef PRINTGOTEVENT
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
        default: printf("UNKNOWN("); break;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
    } /* switch */
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
    printf("%sokay, ival=%d, fval=%f, time=%llu, name='%s').\n",
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
            e->okay ? "" : "!", e->ivalue, e->fvalue, e->epochsecs, e->name);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
} /* printEvent */
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
int main(int argc, char **argv)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
{
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
    const int retval = (int) time(NULL) % 127;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    int i;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    printf("Child argv (argc=%d):\n", argc);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    for (i = 0; i <= argc; i++)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
        printf("  - '%s'\n", argv[i]);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    printf("\n");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    if (!STEAMSHIM_init())
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    {
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
        printf("Child init failed, terminating.\n");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
        return 42;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    } /* if */
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    STEAMSHIM_requestStats();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    while (STEAMSHIM_alive())
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    {
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        const STEAMSHIM_Event *e = STEAMSHIM_pump();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
        printEvent(e);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
        if (e && e->type == SHIMEVENT_STATSRECEIVED)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
            break;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
        usleep(100 * 1000);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    } // while
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    STEAMSHIM_getStatI("BulletsFired");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    STEAMSHIM_getAchievement("KILL_FIRST_VICTIM");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    STEAMSHIM_resetStats(1);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    STEAMSHIM_storeStats();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    STEAMSHIM_setAchievement("KILL_FIRST_VICTIM", 1);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    STEAMSHIM_getAchievement("KILL_FIRST_VICTIM");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    STEAMSHIM_setStatI("BulletsFired", 22);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    STEAMSHIM_storeStats();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    {
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        time_t x = time(NULL) + 5;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
        while ( STEAMSHIM_alive() && (time(NULL) < x) )
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
        {
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
            const STEAMSHIM_Event *e = STEAMSHIM_pump();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
            printEvent(e);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
            usleep(100 * 1000);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
        } // while
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    }
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    STEAMSHIM_deinit();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
9
47ad19ad0536 Ten seconds was way too much to wait here.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    83
    sleep(3);
4
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    printf("Child returning %d\n", retval);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    return retval;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
} /* main */
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88