Wiki Page Content

SDL_GetPowerInfo

Use this function to get the current power supply details.

Syntax

SDL_PowerState SDL_GetPowerInfo(int* secs,
                                int* pct)

Function Parameters

secs

seconds of battery life left, you can pass a NULL here if you don't care, will return -1 if we can't determine a value, or we're not running on a battery

pct

percentage of battery life left, between 0 and 100, you can pass a NULL here if you don't care, will return -1 if we can't determine a value, or we're not running on a battery

Return Value

Returns the state of the battery, if any. Return values may be any of the following:

SDL_POWERSTATE_UNKNOWN

cannot determine power status

SDL_POWERSTATE_ON_BATTERY

not plugged in, running on the battery

SDL_POWERSTATE_NO_BATTERY

plugged in, no battery available

SDL_POWERSTATE_CHARGING

plugged in, charging battery

SDL_POWERSTATE_CHARGED

plugged in, battery charged

See SDL_PowerState for more info.

Both parameters will return -1 if a value can't be determined, or if not running on a battery.

Code Examples

int secs, pct;
if (SDL_GetPowerInfo(&secs, &pct) == SDL_POWERSTATE_ON_BATTERY) {
    printf("Battery is draining: ");
    if (secs == -1) {
        printf("(unknown time left)\n");
    } else {
        printf("(%d seconds left)\n", secs);
    }

    if (pct == -1) {
        printf("(unknown percentage left)\n");
    } else {
        printf("(%d percent left)\n", pct);
    }
}

Remarks

You should never take a battery status as absolute truth. Batteries (especially failing batteries) are delicate hardware, and the values reported here are best estimates based on what that hardware reports. It's not uncommon for older batteries to lose stored power much faster than it reports, or completely drain when reporting it has 20 percent left, etc.

Battery status can change at any time; if you are concerned with power state, you should call this function frequently, and perhaps ignore changes until they seem to be stable for a few seconds.


CategoryAPI, CategoryPower

None: SDL_GetPowerInfo (last edited 2015-12-19 22:23:49 by PhilippWiesemann)

Feedback
Please include your contact information if you'd like to receive a reply.
Submit