The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

SDL2::power - SDL Power Management Routines

SYNOPSIS

    use SDL2 qw[:power :powerState];
        if ( SDL_GetPowerInfo( \my $secs, \my $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 );
        }
    }

DESCRIPTION

SDL2::power exposes a supported system's power state.

Functions

These may be imported by name or with the :power tag.

SDL_GetPowerInfo( ... )

Get the current power supply details.

    my $state = SDL_GetPowerInfo( \my $secs, \my $pct );

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.

It's possible a platform can only report battery percentage or time left but not both.

Expected parameters include:

secs - seconds of battery life left, you can pass a undef 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 undef here if you don't care, will return -1 if we can't determine a value, or we're not running on a battery

Returns an SDL_PowerState enum representing the current battery state.

Enumerations

These may be imported with the given tag.

SDL_PowerState

The basic state for the system's power supply. These may be imported with the :powerState tag.

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

LICENSE

Copyright (C) Sanko Robinson.

This library is free software; you can redistribute it and/or modify it under the terms found in the Artistic License 2. Other copyrights, terms, and conditions may apply to data transmitted through this module.

AUTHOR

Sanko Robinson <sanko@cpan.org>