testapp.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 16 Sep 2015 01:33:55 -0400
changeset 15 4018ba1b0e14
parent 11 41535db7e805
permissions -rw-r--r--
A couple README updates.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11
41535db7e805 Added a brief comment on the test app.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
     1
// This example assumes you own Postal 1 on Steam...
41535db7e805 Added a brief comment on the test app.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
     2
//
41535db7e805 Added a brief comment on the test app.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
     3
//     http://store.steampowered.com/app/232770
41535db7e805 Added a brief comment on the test app.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
     4
//
41535db7e805 Added a brief comment on the test app.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
     5
//  ...and it will RESET ALL YOUR ACHIEVEMENTS for that game, so BE CAREFUL
41535db7e805 Added a brief comment on the test app.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
     6
//  before running this!
41535db7e805 Added a brief comment on the test app.
Ryan C. Gordon <icculus@icculus.org>
parents: 9
diff changeset
     7
4
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
#include "steamshim_child.h"
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
#include <stdio.h>
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#include <stdlib.h>
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <time.h>
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include <unistd.h>
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
static void printEvent(const STEAMSHIM_Event *e)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
{
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
    if (!e) return;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
    printf("CHILD EVENT: ");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
    switch (e->type)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    {
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
        #define PRINTGOTEVENT(x) case SHIMEVENT_##x: printf("%s(", #x); break
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
        PRINTGOTEVENT(BYE);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
        PRINTGOTEVENT(STATSRECEIVED);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
        PRINTGOTEVENT(STATSSTORED);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
        PRINTGOTEVENT(SETACHIEVEMENT);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
        PRINTGOTEVENT(GETACHIEVEMENT);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
        PRINTGOTEVENT(RESETSTATS);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
        PRINTGOTEVENT(SETSTATI);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
        PRINTGOTEVENT(GETSTATI);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
        PRINTGOTEVENT(SETSTATF);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
        PRINTGOTEVENT(GETSTATF);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
        #undef PRINTGOTEVENT
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
        default: printf("UNKNOWN("); break;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
    } /* switch */
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    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
    37
            e->okay ? "" : "!", e->ivalue, e->fvalue, e->epochsecs, e->name);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
} /* printEvent */
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
int main(int argc, char **argv)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
{
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    const int retval = (int) time(NULL) % 127;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    int i;
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 argv (argc=%d):\n", argc);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    for (i = 0; i <= argc; i++)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
        printf("  - '%s'\n", argv[i]);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    printf("\n");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    if (!STEAMSHIM_init())
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
        printf("Child init failed, terminating.\n");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
        return 42;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    } /* if */
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    STEAMSHIM_requestStats();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    while (STEAMSHIM_alive())
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
        const STEAMSHIM_Event *e = STEAMSHIM_pump();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        printEvent(e);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        if (e && e->type == SHIMEVENT_STATSRECEIVED)
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
            break;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
        usleep(100 * 1000);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    } // while
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    STEAMSHIM_getStatI("BulletsFired");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    STEAMSHIM_getAchievement("KILL_FIRST_VICTIM");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    STEAMSHIM_resetStats(1);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    STEAMSHIM_storeStats();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    STEAMSHIM_setAchievement("KILL_FIRST_VICTIM", 1);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
    STEAMSHIM_getAchievement("KILL_FIRST_VICTIM");
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    STEAMSHIM_setStatI("BulletsFired", 22);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    STEAMSHIM_storeStats();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    {
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
        time_t x = time(NULL) + 5;
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
        while ( STEAMSHIM_alive() && (time(NULL) < x) )
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
        {
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
            const STEAMSHIM_Event *e = STEAMSHIM_pump();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
            printEvent(e);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
            usleep(100 * 1000);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
        } // while
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
    }
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    STEAMSHIM_deinit();
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
9
47ad19ad0536 Ten seconds was way too much to wait here.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    90
    sleep(3);
4
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    printf("Child returning %d\n", retval);
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    return retval;
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
} /* main */
c02dc1a56d12 Added a test app.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95