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