src/power/beos/SDL_syspower.c
author Sam Lantinga
Sat, 19 Sep 2009 13:29:40 +0000
changeset 3280 00cace2d9080
parent 3186 51750b7a966f
child 3697 f7b03b6838cb
permissions -rw-r--r--
Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
icculus@3173
     1
/*
icculus@3173
     2
    SDL - Simple DirectMedia Layer
icculus@3173
     3
    Copyright (C) 1997-2009 Sam Lantinga
icculus@3173
     4
icculus@3173
     5
    This library is free software; you can redistribute it and/or
icculus@3173
     6
    modify it under the terms of the GNU Lesser General Public
icculus@3173
     7
    License as published by the Free Software Foundation; either
icculus@3173
     8
    version 2.1 of the License, or (at your option) any later version.
icculus@3173
     9
icculus@3173
    10
    This library is distributed in the hope that it will be useful,
icculus@3173
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@3173
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@3173
    13
    Lesser General Public License for more details.
icculus@3173
    14
icculus@3173
    15
    You should have received a copy of the GNU Lesser General Public
icculus@3173
    16
    License along with this library; if not, write to the Free Software
icculus@3173
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@3173
    18
icculus@3173
    19
    Sam Lantinga
icculus@3173
    20
    slouken@libsdl.org
icculus@3173
    21
*/
icculus@3173
    22
#include "SDL_config.h"
icculus@3173
    23
icculus@3173
    24
#ifndef SDL_POWER_DISABLED
icculus@3173
    25
#ifdef SDL_POWER_BEOS
icculus@3173
    26
icculus@3173
    27
#include <stdio.h>
icculus@3173
    28
#include <stdlib.h>
icculus@3173
    29
#include <unistd.h>
icculus@3173
    30
#include <fcntl.h>
icculus@3173
    31
#include <ctype.h>
icculus@3173
    32
#include <drivers/Drivers.h>
icculus@3173
    33
icculus@3173
    34
/* These values are from apm.h ... */
icculus@3173
    35
#define APM_DEVICE_PATH "/dev/misc/apm"
icculus@3173
    36
#define APM_FUNC_OFFSET 0x5300
icculus@3173
    37
#define APM_FUNC_GET_POWER_STATUS 10
icculus@3173
    38
#define APM_DEVICE_ALL 1
icculus@3173
    39
#define APM_BIOS_CALL (B_DEVICE_OP_CODES_END + 3)
icculus@3173
    40
icculus@3173
    41
#include "SDL_power.h"
icculus@3173
    42
icculus@3173
    43
SDL_bool
slouken@3186
    44
SDL_GetPowerInfo_BeOS(SDL_PowerState * state, int *seconds, int *percent)
icculus@3173
    45
{
icculus@3173
    46
    const int fd = open("/dev/misc/apm", O_RDONLY);
icculus@3173
    47
    SDL_bool need_details = SDL_FALSE;
icculus@3173
    48
    uint16 regs[6];
icculus@3173
    49
    uint8 ac_status;
icculus@3173
    50
    uint8 battery_status;
icculus@3173
    51
    uint8 battery_flags;
icculus@3173
    52
    uint8 battery_life;
icculus@3173
    53
    uint32 battery_time;
icculus@3173
    54
icculus@3173
    55
    if (fd == -1) {
slouken@3186
    56
        return SDL_FALSE;       /* maybe some other method will work? */
icculus@3173
    57
    }
icculus@3173
    58
slouken@3186
    59
    memset(regs, '\0', sizeof(regs));
icculus@3173
    60
    regs[0] = APM_FUNC_OFFSET + APM_FUNC_GET_POWER_STATUS;
icculus@3173
    61
    regs[1] = APM_DEVICE_ALL;
icculus@3173
    62
    rc = ioctl(fd, APM_BIOS_CALL, regs);
icculus@3173
    63
    close(fd);
icculus@3173
    64
icculus@3173
    65
    if (rc < 0) {
icculus@3173
    66
        return SDL_FALSE;
icculus@3173
    67
    }
icculus@3173
    68
icculus@3173
    69
    ac_status = regs[1] >> 8;
icculus@3173
    70
    battery_status = regs[1] & 0xFF;
icculus@3173
    71
    battery_flags = regs[2] >> 8;
icculus@3173
    72
    battery_life = regs[2] & 0xFF;
icculus@3173
    73
    battery_time = (uint32) regs[3];
icculus@3173
    74
icculus@3173
    75
    /* in theory, _something_ should be set in battery_flags, right? */
slouken@3186
    76
    if (battery_flags == 0x00) {        /* older APM BIOS? Less fields. */
icculus@3173
    77
        battery_time = 0xFFFF;
icculus@3173
    78
        if (battery_status == 0xFF) {
icculus@3173
    79
            battery_flags = 0xFF;
icculus@3173
    80
        } else {
icculus@3173
    81
            battery_flags = (1 << status.battery_status);
icculus@3173
    82
        }
icculus@3173
    83
    }
icculus@3173
    84
slouken@3186
    85
    if ((battery_time != 0xFFFF) && (battery_time & (1 << 15))) {
icculus@3173
    86
        /* time is in minutes, not seconds */
icculus@3173
    87
        battery_time = (battery_time & 0x7FFF) * 60;
icculus@3173
    88
    }
icculus@3173
    89
slouken@3186
    90
    if (battery_flags == 0xFF) {        /* unknown state */
icculus@3173
    91
        *state = SDL_POWERSTATE_UNKNOWN;
slouken@3186
    92
    } else if (battery_flags & (1 << 7)) {      /* no battery */
icculus@3173
    93
        *state = SDL_POWERSTATE_NO_BATTERY;
slouken@3186
    94
    } else if (battery_flags & (1 << 3)) {      /* charging */
icculus@3173
    95
        *state = SDL_POWERSTATE_CHARGING;
icculus@3173
    96
        need_details = SDL_TRUE;
icculus@3173
    97
    } else if (ac_status == 1) {
slouken@3186
    98
        *state = SDL_POWERSTATE_CHARGED;        /* on AC, not charging. */
icculus@3173
    99
        need_details = SDL_TRUE;
icculus@3173
   100
    } else {
slouken@3186
   101
        *state = SDL_POWERSTATE_ON_BATTERY;     /* not on AC. */
icculus@3173
   102
        need_details = SDL_TRUE;
icculus@3173
   103
    }
icculus@3173
   104
icculus@3173
   105
    *percent = -1;
icculus@3173
   106
    *seconds = -1;
icculus@3173
   107
    if (need_details) {
icculus@3173
   108
        const int pct = (int) battery_life;
icculus@3173
   109
        const int secs = (int) battery_time;
icculus@3173
   110
slouken@3186
   111
        if (pct != 255) {       /* 255 == unknown */
slouken@3186
   112
            *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
icculus@3173
   113
        }
slouken@3186
   114
        if (secs != 0xFFFF) {   /* 0xFFFF == unknown */
icculus@3173
   115
            *seconds = secs;
icculus@3173
   116
        }
icculus@3173
   117
    }
icculus@3173
   118
slouken@3186
   119
    return SDL_TRUE;            /* the definitive answer if APM driver replied. */
icculus@3173
   120
}
icculus@3173
   121
icculus@3173
   122
#endif /* SDL_POWER_BEOS */
icculus@3173
   123
#endif /* SDL_POWER_DISABLED */
icculus@3173
   124
icculus@3173
   125
/* vi: set ts=4 sw=4 expandtab: */