| 1 | package SDL2::power 0.01 { | ||
| 2 | use SDL2::Utils; | ||
| 3 | # | ||
| 4 | use SDL2::stdinc; | ||
| 5 | # | ||
| 6 | enum SDL_PowerState => [ | ||
| 7 | qw[SDL_POWERSTATE_UNKNOWN | ||
| 8 | SDL_POWERSTATE_ON_BATTERY | ||
| 9 | SDL_POWERSTATE_NO_BATTERY | ||
| 10 | SDL_POWERSTATE_CHARGING | ||
| 11 | SDL_POWERSTATE_CHARGED | ||
| 12 | ] | ||
| 13 | ]; | ||
| 14 | attach power => { SDL_GetPowerInfo => [ [ 'int*', 'int*' ], 'SDL_PowerState' ] }; | ||
| 15 | |||
| 16 | =encoding utf-8 | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | SDL2::power - SDL Power Management Routines | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | use SDL2 qw[:power :powerState]; | ||
| 25 | if ( SDL_GetPowerInfo( \my $secs, \my $pct ) == SDL_POWERSTATE_ON_BATTERY ) { | ||
| 26 | printf("Battery is draining: "); | ||
| 27 | if ( $secs == -1 ) { | ||
| 28 | printf("(unknown time left)\n"); | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | printf( "(%d seconds left)\n", $secs ); | ||
| 32 | } | ||
| 33 | if ( $pct == -1 ) { | ||
| 34 | printf("(unknown percentage left)\n"); | ||
| 35 | } | ||
| 36 | else { | ||
| 37 | printf( "(%d percent left)\n", $pct ); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | SDL2::power exposes a supported system's power state. | ||
| 44 | |||
| 45 | =head1 Functions | ||
| 46 | |||
| 47 | These may be imported by name or with the C<:power> tag. | ||
| 48 | |||
| 49 | =head2 C<SDL_GetPowerInfo( ... )> | ||
| 50 | |||
| 51 | Get the current power supply details. | ||
| 52 | |||
| 53 | my $state = SDL_GetPowerInfo( \my $secs, \my $pct ); | ||
| 54 | |||
| 55 | You should never take a battery status as absolute truth. Batteries (especially | ||
| 56 | failing batteries) are delicate hardware, and the values reported here are best | ||
| 57 | estimates based on what that hardware reports. It's not uncommon for older | ||
| 58 | batteries to lose stored power much faster than it reports, or completely drain | ||
| 59 | when reporting it has 20 percent left, etc. | ||
| 60 | |||
| 61 | Battery status can change at any time; if you are concerned with power state, | ||
| 62 | you should call this function frequently, and perhaps ignore changes until they | ||
| 63 | seem to be stable for a few seconds. | ||
| 64 | |||
| 65 | It's possible a platform can only report battery percentage or time left but | ||
| 66 | not both. | ||
| 67 | |||
| 68 | Expected parameters include: | ||
| 69 | |||
| 70 | =over | ||
| 71 | |||
| 72 | =item C<secs> - seconds of battery life left, you can pass a C<undef> here if you don't care, will return C<-1> if we can't determine a value, or we're not running on a battery | ||
| 73 | |||
| 74 | =item C<pct> - percentage of battery life left, between C<0> and C<100>, you can pass a C<undef> here if you don't care, will return C<-1> if we can't determine a value, or we're not running on a battery | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | Returns an C<SDL_PowerState> enum representing the current battery state. | ||
| 79 | |||
| 80 | =head1 Enumerations | ||
| 81 | |||
| 82 | These may be imported with the given tag. | ||
| 83 | |||
| 84 | =head2 C<SDL_PowerState> | ||
| 85 | |||
| 86 | The basic state for the system's power supply. These may be imported with the | ||
| 87 | C<:powerState> tag. | ||
| 88 | |||
| 89 | =over | ||
| 90 | |||
| 91 | =item C<SDL_POWERSTATE_UNKNOWN> - cannot determine power status | ||
| 92 | |||
| 93 | =item C<SDL_POWERSTATE_ON_BATTERY> - Not plugged in, running on the battery | ||
| 94 | |||
| 95 | =item C<SDL_POWERSTATE_NO_BATTERY> - Plugged in, no battery available | ||
| 96 | |||
| 97 | =item C<SDL_POWERSTATE_CHARGING> - Plugged in, charging battery | ||
| 98 | |||
| 99 | =item C<SDL_POWERSTATE_CHARGED> - Plugged in, battery charged | ||
| 100 | |||
| 101 | =back | ||
| 102 | |||
| 103 | =head1 LICENSE | ||
| 104 | |||
| 105 | Copyright (C) Sanko Robinson. | ||
| 106 | |||
| 107 | This library is free software; you can redistribute it and/or modify it under | ||
| 108 | the terms found in the Artistic License 2. Other copyrights, terms, and | ||
| 109 | conditions may apply to data transmitted through this module. | ||
| 110 | |||
| 111 | =head1 AUTHOR | ||
| 112 | |||
| 113 | Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||
| 114 | |||
| 115 | =begin stopwords | ||
| 116 | |||
| 117 | =end stopwords | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | }; | ||
| 122 | 1; |