test/testpower.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 17 Aug 2016 21:04:50 +0200
changeset 10275 ba7dc3ebb81e
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
Emscripten: Fixed opening previously closed joystick.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 3186
diff changeset
    11
*/
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
/* Simple test of power subsystem. */
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#include <stdio.h>
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include "SDL.h"
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    17
static void
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    18
report_power(void)
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
{
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
    int seconds, percent;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
    const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
    char *statestr = NULL;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    24
    SDL_Log("SDL-reported power info...\n");
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    25
    switch (state) {
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    26
    case SDL_POWERSTATE_UNKNOWN:
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    27
        statestr = "Unknown";
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    28
        break;
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    29
    case SDL_POWERSTATE_ON_BATTERY:
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    30
        statestr = "On battery";
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    31
        break;
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    32
    case SDL_POWERSTATE_NO_BATTERY:
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    33
        statestr = "No battery";
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    34
        break;
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    35
    case SDL_POWERSTATE_CHARGING:
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    36
        statestr = "Charging";
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    37
        break;
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    38
    case SDL_POWERSTATE_CHARGED:
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    39
        statestr = "Charged";
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    40
        break;
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    41
    default:
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    42
        statestr = "!!API ERROR!!";
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    43
        break;
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    46
    SDL_Log("State: %s\n", statestr);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    if (percent == -1) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    49
        SDL_Log("Percent left: unknown\n");
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    } else {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    51
        SDL_Log("Percent left: %d%%\n", percent);
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    if (seconds == -1) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    55
        SDL_Log("Time left: unknown\n");
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    } else {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    57
        SDL_Log("Time left: %d minutes, %d seconds\n", (int) (seconds / 60),
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    58
               (int) (seconds % 60));
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
}
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    63
int
Sam Lantinga <slouken@libsdl.org>
parents: 3170
diff changeset
    64
main(int argc, char *argv[])
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
{
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    66
    /* Enable standard application logging */
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    67
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    68
7307
5d6b7b9432d3 Fixed SDL building with the minimal configuration
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    69
    if (SDL_Init(0) == -1) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    70
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
3170
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        return 1;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    }
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    report_power();
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    SDL_Quit();
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    return 0;
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
}
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
b7a48f533966 Initial work on power subsystem for SDL 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
/* end of testpower.c ... */