src/power/linux/SDL_syspower.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 30 Jun 2009 03:50:30 +0000
changeset 3203 790cbbda6429
parent 3186 51750b7a966f
child 3204 f77f50add24f
permissions -rw-r--r--
Power: First shot at Linux /proc/acpi/battery support.

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