Debian Codegraph @

test/testpower.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11 */
12 /* Simple test of power subsystem. */
13
14 #include <stdio.h>
15 #include "SDL.h"
16 #include "SDL_test.h"
17
18 static void
19 report_power(void)
20 {
21 int seconds, percent;
22 const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
23 const char *statestr = NULL;
24
25 SDL_Log("SDL-reported power info...\n");
26 switch (state) {
27 case SDL_POWERSTATE_UNKNOWN:
28 statestr = "Unknown";
29 break;
30 case SDL_POWERSTATE_ON_BATTERY:
31 statestr = "On battery";
32 break;
33 case SDL_POWERSTATE_NO_BATTERY:
34 statestr = "No battery";
35 break;
36 case SDL_POWERSTATE_CHARGING:
37 statestr = "Charging";
38 break;
39 case SDL_POWERSTATE_CHARGED:
40 statestr = "Charged";
41 break;
42 default:
43 statestr = "!!API ERROR!!";
44 break;
45 }
46
47 SDL_Log("State: %s\n", statestr);
48
49 if (percent == -1) {
50 SDL_Log("Percent left: unknown\n");
51 } else {
52 SDL_Log("Percent left: %d%%\n", percent);
53 }
54
55 if (seconds == -1) {
56 SDL_Log("Time left: unknown\n");
57 } else {
58 SDL_Log("Time left: %d minutes, %d seconds\n", seconds / 60, seconds % 60);
59 }
60 }
61
62 int main(int argc, char *argv[])
63 {
64 SDLTest_CommonState *state;
65
66 state = SDLTest_CommonCreateState(argv, 0);
67 if (!state) {
68 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
69 return 1;
70 }
71
72 /* Enable standard application logging */
73 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
74
75 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
76 return 1;
77 }
78
79 if (!SDLTest_CommonInit(state)) {
80 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
81 return 1;
82 }
83
84 report_power();
85
86 SDLTest_CommonQuit(state);
87 return 0;
88 }
89
90 /* end of testpower.c ... */