Engine/lib/sdl/src/power/linux/SDL_syspower.c
/* !!! FIXME: implement d-bus queries to org.freedesktop.UPower. */
implement d-bus queries to org.freedesktop.UPower.
implement d - bus queries to org . freedesktop . UPower .
SDL_bool
SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *seconds, int *percent)
{
    const char *base = sys_class_power_supply_path;
    struct dirent *dent;
    DIR *dirp;
    dirp = opendir(base);
    if (!dirp) {
        return SDL_FALSE;
    }
    *state = SDL_POWERSTATE_NO_BATTERY;  
    *seconds = -1;
    *percent = -1;
    while ((dent = readdir(dirp)) != NULL) {
        const char *name = dent->d_name;
        SDL_bool choose = SDL_FALSE;
        char str[64];
        SDL_PowerState st;
        int secs;
        int pct;
        if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) {
            continue;  
        } else if (!read_power_file(base, name, "type", str, sizeof (str))) {
            continue;  
        } else if (SDL_strcmp(str, "Battery\n") != 0) {
            continue;  
        }
        if (read_power_file(base, name, "present", str, sizeof (str)) && (SDL_strcmp(str, "0\n") == 0)) {
            st = SDL_POWERSTATE_NO_BATTERY;
        } else if (!read_power_file(base, name, "status", str, sizeof (str))) {
            st = SDL_POWERSTATE_UNKNOWN;  
        } else if (SDL_strcmp(str, "Charging\n") == 0) {
            st = SDL_POWERSTATE_CHARGING;
        } else if (SDL_strcmp(str, "Discharging\n") == 0) {
            st = SDL_POWERSTATE_ON_BATTERY;
        } else if ((SDL_strcmp(str, "Full\n") == 0) || (SDL_strcmp(str, "Not charging\n") == 0)) {
            st = SDL_POWERSTATE_CHARGED;
        } else {
            st = SDL_POWERSTATE_UNKNOWN;  
        }
        if (!read_power_file(base, name, "capacity", str, sizeof (str))) {
            pct = -1;
        } else {
            pct = SDL_atoi(str);
            pct = (pct > 100) ? 100 : pct; 
        }
        if (!read_power_file(base, name, "time_to_empty_now", str, sizeof (str))) {
            secs = -1;
        } else {
            secs = SDL_atoi(str);
            secs = (secs <= 0) ? -1 : secs;  
        }
        if ((secs < 0) && (*seconds < 0)) {
            if ((pct < 0) && (*percent < 0)) {
                choose = SDL_TRUE;  
            } else if (pct > *percent) {
                choose = SDL_TRUE;
            }
        } else if (secs > *seconds) {
            choose = SDL_TRUE;
        }
        if (choose) {
            *seconds = secs;
            *percent = pct;
            *state = st;
        }
    }
    closedir(dirp);
    return SDL_TRUE;  
}
SDL_bool SDL_GetPowerInfo_Linux_sys_class_power_supply ( SDL_PowerState * state , int * seconds , int * percent ) { const char * base = sys_class_power_supply_path ; struct dirent * dent ; DIR * dirp ; dirp = opendir ( base ) ; if ( ! dirp ) { return SDL_FALSE ; } * state = SDL_POWERSTATE_NO_BATTERY ; * seconds = -1 ; * percent = -1 ; while ( ( dent = readdir ( dirp ) ) != NULL ) { const char * name = dent -> d_name ; SDL_bool choose = SDL_FALSE ; char str [ 64 ] ; SDL_PowerState st ; int secs ; int pct ; if ( ( SDL_strcmp ( name , " " ) == 0 ) || ( SDL_strcmp ( name , " " ) == 0 ) ) { continue ; } else if ( ! read_power_file ( base , name , " " , str , sizeof ( str ) ) ) { continue ; } else if ( SDL_strcmp ( str , " \n " ) != 0 ) { continue ; } if ( read_power_file ( base , name , " " , str , sizeof ( str ) ) && ( SDL_strcmp ( str , " \n " ) == 0 ) ) { st = SDL_POWERSTATE_NO_BATTERY ; } else if ( ! read_power_file ( base , name , " " , str , sizeof ( str ) ) ) { st = SDL_POWERSTATE_UNKNOWN ; } else if ( SDL_strcmp ( str , " \n " ) == 0 ) { st = SDL_POWERSTATE_CHARGING ; } else if ( SDL_strcmp ( str , " \n " ) == 0 ) { st = SDL_POWERSTATE_ON_BATTERY ; } else if ( ( SDL_strcmp ( str , " \n " ) == 0 ) || ( SDL_strcmp ( str , " \n " ) == 0 ) ) { st = SDL_POWERSTATE_CHARGED ; } else { st = SDL_POWERSTATE_UNKNOWN ; } if ( ! read_power_file ( base , name , " " , str , sizeof ( str ) ) ) { pct = -1 ; } else { pct = SDL_atoi ( str ) ; pct = ( pct > 100 ) ? 100 : pct ; } if ( ! read_power_file ( base , name , " " , str , sizeof ( str ) ) ) { secs = -1 ; } else { secs = SDL_atoi ( str ) ; secs = ( secs <= 0 ) ? -1 : secs ; } if ( ( secs < 0 ) && ( * seconds < 0 ) ) { if ( ( pct < 0 ) && ( * percent < 0 ) ) { choose = SDL_TRUE ; } else if ( pct > * percent ) { choose = SDL_TRUE ; } } else if ( secs > * seconds ) { choose = SDL_TRUE ; } if ( choose ) { * seconds = secs ; * percent = pct ; * state = st ; } } closedir ( dirp ) ; return SDL_TRUE ; }
FIXME: implement d-bus queries to org.freedesktop.UPower.
FIXME : implement d - bus queries to org . freedesktop . UPower .
/* assume we're just plugged in. */ /* skip these, of course. */ /* Don't know _what_ we're looking at. Give up on it. */ /* we don't care about UPS and such. */ /* some drivers don't offer this, so if it's not explicitly reported assume it's present. */ /* uh oh */ /* uh oh */ /* clamp between 0%, 100% */ /* 0 == unknown */ /*
         * We pick the battery that claims to have the most minutes left.
         *  (failing a report of minutes, we'll take the highest percent.)
         */ /* at least we know there's a battery. */ /* don't look any further. */