src/power/linux/SDL_syspower.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 30 Jun 2009 05:27:11 +0000
changeset 3204 f77f50add24f
parent 3203 790cbbda6429
child 3205 f9a4aa1e1b34
permissions -rw-r--r--
Removed stub for Linux /sys/power processing.

Apparently /sys/power doesn't expose battery information at this time.
icculus@3170
     1
/*
icculus@3170
     2
    SDL - Simple DirectMedia Layer
icculus@3170
     3
    Copyright (C) 1997-2009 Sam Lantinga
icculus@3170
     4
icculus@3170
     5
    This library is free software; you can redistribute it and/or
icculus@3170
     6
    modify it under the terms of the GNU Lesser General Public
icculus@3170
     7
    License as published by the Free Software Foundation; either
icculus@3170
     8
    version 2.1 of the License, or (at your option) any later version.
icculus@3170
     9
icculus@3170
    10
    This library is distributed in the hope that it will be useful,
icculus@3170
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@3170
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@3170
    13
    Lesser General Public License for more details.
icculus@3170
    14
icculus@3170
    15
    You should have received a copy of the GNU Lesser General Public
icculus@3170
    16
    License along with this library; if not, write to the Free Software
icculus@3170
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@3170
    18
icculus@3170
    19
    Sam Lantinga
icculus@3170
    20
    slouken@libsdl.org
icculus@3170
    21
*/
icculus@3170
    22
#include "SDL_config.h"
icculus@3170
    23
icculus@3170
    24
#ifndef SDL_POWER_DISABLED
icculus@3170
    25
#ifdef SDL_POWER_LINUX
icculus@3170
    26
icculus@3170
    27
#include <stdio.h>
icculus@3170
    28
#include <unistd.h>
icculus@3170
    29
bob@3174
    30
#include <sys/types.h>
bob@3174
    31
#include <sys/stat.h>
icculus@3203
    32
#include <dirent.h>
bob@3174
    33
#include <fcntl.h>
bob@3174
    34
icculus@3170
    35
#include "SDL_power.h"
icculus@3170
    36
icculus@3203
    37
static const char *proc_acpi_path = "/proc/acpi/battery";
icculus@3203
    38
icculus@3203
    39
static int open_acpi_file(const char *node, const char *key)
icculus@3203
    40
{
icculus@3203
    41
    const size_t pathlen = strlen(proc_acpi_path)+strlen(node)+strlen(key)+3;
icculus@3203
    42
    char *path = (char *) alloca(pathlen);
icculus@3203
    43
    if (path == NULL) {
icculus@3203
    44
        return -1;  /* oh well. */
icculus@3203
    45
    }
icculus@3203
    46
icculus@3203
    47
    snprintf(path, pathlen, "%s/%s/%s", proc_acpi_path, node, key);
icculus@3203
    48
    return open(path, O_RDONLY);
icculus@3203
    49
}
icculus@3203
    50
icculus@3203
    51
icculus@3203
    52
static SDL_bool
icculus@3203
    53
load_acpi_file(const char *node, const char *key, char *buf, size_t buflen)
icculus@3203
    54
{
icculus@3203
    55
    ssize_t br = 0;
icculus@3203
    56
    const int fd = open_acpi_file(node, key);
icculus@3170
    57
    if (fd == -1) {
icculus@3203
    58
        return SDL_FALSE;
icculus@3170
    59
    }
icculus@3203
    60
    br = read(fd, buf, buflen-1);
icculus@3203
    61
    close(fd);
icculus@3203
    62
    if (br < 0) {
icculus@3203
    63
        return SDL_FALSE;
icculus@3203
    64
    }
icculus@3203
    65
    buf[br] = '\0';             // null-terminate the string.
icculus@3170
    66
    return SDL_TRUE;
icculus@3203
    67
}
icculus@3203
    68
icculus@3203
    69
static SDL_bool
icculus@3203
    70
make_acpi_key_val(char **_ptr, char **_key, char **_val)
icculus@3203
    71
{
icculus@3203
    72
    char *ptr = *_ptr;
icculus@3203
    73
icculus@3203
    74
    while (*ptr == ' ') {
icculus@3203
    75
        ptr++;  /* skip whitespace. */
icculus@3203
    76
    }
icculus@3203
    77
icculus@3203
    78
    if (*ptr == '\0') {
icculus@3203
    79
        return SDL_FALSE;  /* EOF. */
icculus@3203
    80
    }
icculus@3203
    81
icculus@3203
    82
    *_key = ptr;
icculus@3203
    83
icculus@3203
    84
    while ((*ptr != ':') && (*ptr != '\0')) {
icculus@3203
    85
        ptr++;
icculus@3203
    86
    }
icculus@3203
    87
icculus@3203
    88
    if (*ptr == '\0') {
icculus@3203
    89
        return SDL_FALSE;  /* (unexpected) EOF. */
icculus@3203
    90
    }
icculus@3203
    91
icculus@3203
    92
    *(ptr++) = '\0';  /* terminate the key. */
icculus@3203
    93
icculus@3203
    94
    while ((*ptr == ' ') && (*ptr != '\0')) {
icculus@3203
    95
        ptr++;  /* skip whitespace. */
icculus@3203
    96
    }
icculus@3203
    97
icculus@3203
    98
    if (*ptr == '\0') {
icculus@3203
    99
        return SDL_FALSE;  /* (unexpected) EOF. */
icculus@3203
   100
    }
icculus@3203
   101
icculus@3203
   102
    *_val = ptr;
icculus@3203
   103
icculus@3203
   104
    while ((*ptr != '\n') && (*ptr != '\0')) {
icculus@3203
   105
        ptr++;
icculus@3203
   106
    }
icculus@3203
   107
icculus@3203
   108
    if (*ptr != '\0') {
icculus@3203
   109
        *(ptr++) = '\0';  /* terminate the value. */
icculus@3203
   110
    }
icculus@3203
   111
icculus@3203
   112
    *_ptr = ptr;  /* store for next time. */
icculus@3203
   113
    return SDL_TRUE;
icculus@3203
   114
}
icculus@3203
   115
icculus@3203
   116
static void
icculus@3203
   117
check_acpi(const char * fname, SDL_bool * have_ac, SDL_bool * have_battery,
icculus@3203
   118
           SDL_bool * charging, int *seconds, int *percent)
icculus@3203
   119
{
icculus@3203
   120
    int fd = -1;
icculus@3203
   121
    char info[1024];
icculus@3203
   122
    char state[1024];
icculus@3203
   123
    ssize_t br = 0;
icculus@3203
   124
    char *ptr = NULL;
icculus@3203
   125
    char *key = NULL;
icculus@3203
   126
    char *val = NULL;
icculus@3203
   127
    SDL_bool charge = SDL_FALSE;
icculus@3203
   128
    SDL_bool choose = SDL_FALSE;
icculus@3203
   129
    SDL_bool is_ac = SDL_FALSE;
icculus@3203
   130
    int maximum = -1;
icculus@3203
   131
    int remaining = -1;
icculus@3203
   132
    int secs = -1;
icculus@3203
   133
    int pct = -1;
icculus@3203
   134
icculus@3203
   135
    if (!load_acpi_file(fname, "state", state, sizeof (state))) {
icculus@3203
   136
        return;
icculus@3203
   137
    } else if (!load_acpi_file(fname, "info", info, sizeof (info))) {
icculus@3203
   138
        return;
icculus@3203
   139
    }
icculus@3203
   140
icculus@3203
   141
    ptr = &state[0];
icculus@3203
   142
    while (make_acpi_key_val(&ptr, &key, &val)) {
icculus@3203
   143
        if (strcmp(key, "present") == 0) {
icculus@3203
   144
            if (strcmp(val, "yes") == 0) {
icculus@3203
   145
                *have_battery = SDL_TRUE;
icculus@3203
   146
            }
icculus@3203
   147
        } else if (strcmp(key, "charging state") == 0) {
icculus@3203
   148
            /* !!! FIXME: what exactly _does_ charging/discharging mean? */
icculus@3203
   149
            if (strcmp(val, "charging/discharging") == 0) {
icculus@3203
   150
                *have_ac = is_ac = SDL_TRUE;
icculus@3203
   151
                charge = SDL_TRUE;
icculus@3203
   152
            } else if (strcmp(val, "charging") == 0) {
icculus@3203
   153
                *have_ac = is_ac = SDL_TRUE;
icculus@3203
   154
                charge = SDL_TRUE;
icculus@3203
   155
            } else if (strcmp(val, "charged") == 0) {
icculus@3203
   156
                /* !!! FIXME: maybe another battery is discharging,
icculus@3203
   157
                   !!! FIXME:   instead of AC connection. */
icculus@3203
   158
                *have_ac = is_ac = SDL_TRUE;
icculus@3203
   159
                charge = SDL_TRUE;
icculus@3203
   160
            }
icculus@3203
   161
        } else if (strcmp(key, "remaining capacity") == 0) {
icculus@3203
   162
            char *endptr = NULL;
icculus@3203
   163
            const int cvt = (int) strtol(val, &endptr, 10);
icculus@3203
   164
            if (*endptr == ' ') {
icculus@3203
   165
                remaining = cvt;
icculus@3203
   166
            }
icculus@3203
   167
        }
icculus@3203
   168
    }
icculus@3203
   169
    
icculus@3203
   170
    ptr = &info[0];
icculus@3203
   171
    while (make_acpi_key_val(&ptr, &key, &val)) {
icculus@3203
   172
        if (strcmp(key, "design capacity") == 0) {
icculus@3203
   173
            char *endptr = NULL;
icculus@3203
   174
            const int cvt = (int) strtol(val, &endptr, 10);
icculus@3203
   175
            if (*endptr == ' ') {
icculus@3203
   176
                maximum = cvt;
icculus@3203
   177
            }
icculus@3203
   178
        }
icculus@3203
   179
    }
icculus@3203
   180
icculus@3203
   181
    if ((maximum >= 0) && (remaining >= 0)) {
icculus@3203
   182
        pct = (int) ((((float) remaining) / ((float) maximum)) * 100.0f);
icculus@3203
   183
        if (pct < 0) {
icculus@3203
   184
            pct = 0;
icculus@3203
   185
        } else if (pct > 100) {
icculus@3203
   186
            pct = 100;
icculus@3203
   187
        }
icculus@3203
   188
    }
icculus@3203
   189
icculus@3203
   190
    /* !!! FIXME: calculate (secs). */
icculus@3203
   191
icculus@3203
   192
    /*
icculus@3203
   193
     * We pick the battery that claims to have the most minutes left.
icculus@3203
   194
     *  (failing a report of minutes, we'll take the highest percent.)
icculus@3203
   195
     */
icculus@3203
   196
    if ((secs < 0) && (*seconds < 0)) {
icculus@3203
   197
        if ((pct < 0) && (*percent < 0)) {
icculus@3203
   198
            choose = SDL_TRUE;  /* at least we know there's a battery. */
icculus@3203
   199
        }
icculus@3203
   200
        if (pct > *percent) {
icculus@3203
   201
            choose = SDL_TRUE;
icculus@3203
   202
        }
icculus@3203
   203
    } else if (secs > *seconds) {
icculus@3203
   204
        choose = SDL_TRUE;
icculus@3203
   205
    }
icculus@3203
   206
icculus@3203
   207
    if (choose) {
icculus@3203
   208
        *seconds = secs;
icculus@3203
   209
        *percent = pct;
icculus@3203
   210
        *charging = charge;
icculus@3203
   211
    }
icculus@3203
   212
icculus@3170
   213
}
icculus@3170
   214
icculus@3170
   215
SDL_bool
slouken@3186
   216
SDL_GetPowerInfo_Linux_proc_acpi(SDL_PowerState * state,
slouken@3186
   217
                                 int *seconds, int *percent)
icculus@3170
   218
{
icculus@3203
   219
    struct dirent *dent = NULL;
icculus@3203
   220
    DIR *dirp = NULL;
icculus@3203
   221
    SDL_bool have_ac = SDL_FALSE;
icculus@3203
   222
    SDL_bool have_battery = SDL_FALSE;
icculus@3203
   223
    SDL_bool charging = SDL_FALSE;
icculus@3203
   224
icculus@3203
   225
    *seconds = -1;
icculus@3203
   226
    *percent = -1;
icculus@3203
   227
    *state = SDL_POWERSTATE_UNKNOWN;
icculus@3203
   228
icculus@3203
   229
    dirp = opendir(proc_acpi_path);
icculus@3203
   230
    if (dirp == NULL) {
icculus@3203
   231
        return SDL_FALSE;  /* can't use this interface. */
icculus@3170
   232
    }
icculus@3203
   233
icculus@3203
   234
    while ((dent = readdir(dirp)) != NULL) {
icculus@3203
   235
        const char *name = dent->d_name;
icculus@3203
   236
        check_acpi(name, &have_ac, &have_battery, &charging, seconds, percent);
icculus@3203
   237
    }
icculus@3203
   238
icculus@3203
   239
    if (!have_battery) {
icculus@3203
   240
        *state = SDL_POWERSTATE_NO_BATTERY;
icculus@3203
   241
    } else if (charging) {
icculus@3203
   242
        *state = SDL_POWERSTATE_CHARGING;
icculus@3203
   243
    } else if (have_ac) {
icculus@3203
   244
        *state = SDL_POWERSTATE_CHARGED;
icculus@3203
   245
    } else {
icculus@3203
   246
        *state = SDL_POWERSTATE_ON_BATTERY;
icculus@3203
   247
    }
icculus@3203
   248
icculus@3203
   249
    return SDL_TRUE;   /* definitive answer. */
icculus@3170
   250
}
icculus@3170
   251
icculus@3203
   252
icculus@3170
   253
static SDL_bool
icculus@3170
   254
next_string(char **_ptr, char **_str)
icculus@3170
   255
{
icculus@3170
   256
    char *ptr = *_ptr;
icculus@3170
   257
    char *str = *_str;
icculus@3170
   258
slouken@3186
   259
    while (*ptr == ' ') {       /* skip any spaces... */
icculus@3170
   260
        ptr++;
icculus@3170
   261
    }
icculus@3170
   262
icculus@3170
   263
    if (*ptr == '\0') {
icculus@3170
   264
        return SDL_FALSE;
icculus@3170
   265
    }
icculus@3170
   266
icculus@3170
   267
    str = ptr;
icculus@3203
   268
    while ((*ptr != ' ') && (*ptr != '\n') && (*ptr != '\0'))
icculus@3170
   269
        ptr++;
icculus@3170
   270
icculus@3170
   271
    if (*ptr != '\0')
icculus@3170
   272
        *(ptr++) = '\0';
icculus@3170
   273
icculus@3170
   274
    *_str = str;
icculus@3170
   275
    *_ptr = ptr;
icculus@3170
   276
    return SDL_TRUE;
icculus@3170
   277
}
icculus@3170
   278
icculus@3170
   279
static SDL_bool
icculus@3170
   280
int_string(char *str, int *val)
icculus@3170
   281
{
icculus@3170
   282
    char *endptr = NULL;
slouken@3186
   283
    *val = (int) strtol(str + 2, &endptr, 16);
icculus@3170
   284
    return ((*str != '\0') && (*endptr == '\0'));
icculus@3170
   285
}
icculus@3170
   286
icculus@3170
   287
/* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */
icculus@3170
   288
SDL_bool
slouken@3186
   289
SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState * state,
slouken@3186
   290
                                int *seconds, int *percent)
icculus@3170
   291
{
icculus@3170
   292
    SDL_bool need_details = SDL_FALSE;
icculus@3170
   293
    int ac_status = 0;
icculus@3170
   294
    int battery_status = 0;
icculus@3170
   295
    int battery_flag = 0;
icculus@3170
   296
    int battery_percent = 0;
icculus@3170
   297
    int battery_time = 0;
icculus@3170
   298
    const int fd = open("/proc/apm", O_RDONLY);
icculus@3170
   299
    char buf[128];
icculus@3170
   300
    char *ptr = &buf[0];
icculus@3170
   301
    char *str = NULL;
icculus@3170
   302
    ssize_t br;
icculus@3170
   303
icculus@3170
   304
    if (fd == -1) {
slouken@3186
   305
        return SDL_FALSE;       /* can't use this interface. */
icculus@3170
   306
    }
icculus@3170
   307
icculus@3203
   308
    br = read(fd, buf, sizeof (buf) - 1);
icculus@3170
   309
    close(fd);
icculus@3170
   310
icculus@3170
   311
    if (br < 0) {
icculus@3170
   312
        return SDL_FALSE;
icculus@3170
   313
    }
icculus@3170
   314
slouken@3186
   315
    buf[br] = '\0';             // null-terminate the string.
slouken@3186
   316
    if (!next_string(&ptr, &str)) {     /* driver version */
icculus@3170
   317
        return SDL_FALSE;
icculus@3170
   318
    }
slouken@3186
   319
    if (!next_string(&ptr, &str)) {     /* BIOS version */
icculus@3170
   320
        return SDL_FALSE;
icculus@3170
   321
    }
slouken@3186
   322
    if (!next_string(&ptr, &str)) {     /* APM flags */
icculus@3170
   323
        return SDL_FALSE;
icculus@3170
   324
    }
icculus@3170
   325
slouken@3186
   326
    if (!next_string(&ptr, &str)) {     /* AC line status */
icculus@3170
   327
        return SDL_FALSE;
icculus@3170
   328
    } else if (!int_string(str, &ac_status)) {
icculus@3170
   329
        return SDL_FALSE;
icculus@3170
   330
    }
icculus@3170
   331
slouken@3186
   332
    if (!next_string(&ptr, &str)) {     /* battery status */
icculus@3170
   333
        return SDL_FALSE;
icculus@3170
   334
    } else if (!int_string(str, &battery_status)) {
icculus@3170
   335
        return SDL_FALSE;
icculus@3170
   336
    }
slouken@3186
   337
    if (!next_string(&ptr, &str)) {     /* battery flag */
icculus@3170
   338
        return SDL_FALSE;
icculus@3170
   339
    } else if (!int_string(str, &battery_flag)) {
icculus@3170
   340
        return SDL_FALSE;
icculus@3170
   341
    }
slouken@3186
   342
    if (!next_string(&ptr, &str)) {     /* remaining battery life percent */
icculus@3170
   343
        return SDL_FALSE;
icculus@3170
   344
    }
icculus@3170
   345
    if (str[strlen(str) - 1] == '%') {
icculus@3170
   346
        str[strlen(str) - 1] = '\0';
icculus@3170
   347
    }
icculus@3170
   348
    if (!int_string(str, &battery_percent)) {
icculus@3170
   349
        return SDL_FALSE;
icculus@3170
   350
    }
icculus@3170
   351
slouken@3186
   352
    if (!next_string(&ptr, &str)) {     /* remaining battery life time */
icculus@3170
   353
        return SDL_FALSE;
icculus@3170
   354
    } else if (!int_string(str, &battery_time)) {
icculus@3170
   355
        return SDL_FALSE;
icculus@3170
   356
    }
icculus@3170
   357
slouken@3186
   358
    if (!next_string(&ptr, &str)) {     /* remaining battery life time units */
icculus@3170
   359
        return SDL_FALSE;
icculus@3170
   360
    } else if (strcmp(str, "min") == 0) {
icculus@3170
   361
        battery_time *= 60;
icculus@3170
   362
    }
icculus@3170
   363
slouken@3186
   364
    if (battery_flag == 0xFF) { /* unknown state */
icculus@3170
   365
        *state = SDL_POWERSTATE_UNKNOWN;
slouken@3186
   366
    } else if (battery_flag & (1 << 7)) {       /* no battery */
icculus@3170
   367
        *state = SDL_POWERSTATE_NO_BATTERY;
slouken@3186
   368
    } else if (battery_flag & (1 << 3)) {       /* charging */
icculus@3170
   369
        *state = SDL_POWERSTATE_CHARGING;
icculus@3170
   370
        need_details = SDL_TRUE;
icculus@3170
   371
    } else if (ac_status == 1) {
slouken@3186
   372
        *state = SDL_POWERSTATE_CHARGED;        /* on AC, not charging. */
icculus@3170
   373
        need_details = SDL_TRUE;
icculus@3170
   374
    } else {
icculus@3170
   375
        *state = SDL_POWERSTATE_ON_BATTERY;
icculus@3170
   376
        need_details = SDL_TRUE;
icculus@3170
   377
    }
icculus@3170
   378
icculus@3170
   379
    *percent = -1;
icculus@3170
   380
    *seconds = -1;
icculus@3170
   381
    if (need_details) {
icculus@3170
   382
        const int pct = battery_percent;
icculus@3170
   383
        const int secs = battery_time;
icculus@3170
   384
slouken@3186
   385
        if (pct >= 0) {         /* -1 == unknown */
slouken@3186
   386
            *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
icculus@3170
   387
        }
slouken@3186
   388
        if (secs >= 0) {        /* -1 == unknown */
icculus@3170
   389
            *seconds = secs;
icculus@3170
   390
        }
icculus@3170
   391
    }
icculus@3170
   392
icculus@3170
   393
    return SDL_TRUE;
icculus@3170
   394
}
icculus@3170
   395
icculus@3170
   396
#endif /* SDL_POWER_LINUX */
icculus@3170
   397
#endif /* SDL_POWER_DISABLED */
icculus@3170
   398
icculus@3170
   399
/* vi: set ts=4 sw=4 expandtab: */