BeOS support for power subsystem.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 07 Jun 2009 22:44:59 +0000
changeset 3173510e3f36c04a
parent 3172 f0191bd9f99c
child 3174 c8b9c6d27476
BeOS support for power subsystem.
configure.in
include/SDL_config.h.in
src/power/SDL_power.c
src/power/beos/SDL_syspower.c
     1.1 --- a/configure.in	Sun Jun 07 06:49:01 2009 +0000
     1.2 +++ b/configure.in	Sun Jun 07 22:44:59 2009 +0000
     1.3 @@ -2804,6 +2804,12 @@
     1.4              SOURCES="$SOURCES $srcdir/src/loadso/beos/*.c"
     1.5              have_loadso=yes
     1.6          fi
     1.7 +        # Set up files for the system power library
     1.8 +        if test x$enable_power = xyes; then
     1.9 +            AC_DEFINE(SDL_POWER_BEOS)
    1.10 +            SOURCES="$SOURCES $srcdir/src/power/beos/*.c"
    1.11 +            have_power=yes
    1.12 +        fi
    1.13          # The BeOS platform requires special setup.
    1.14          SOURCES="$srcdir/src/main/beos/*.cc $SOURCES"
    1.15          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lroot -lbe -lmedia -lgame -ldevice -ltextencoding"
     2.1 --- a/include/SDL_config.h.in	Sun Jun 07 06:49:01 2009 +0000
     2.2 +++ b/include/SDL_config.h.in	Sun Jun 07 22:44:59 2009 +0000
     2.3 @@ -330,6 +330,7 @@
     2.4  #undef SDL_POWER_WINDOWS
     2.5  #undef SDL_POWER_MACOSX
     2.6  #undef SDL_POWER_OS2
     2.7 +#undef SDL_POWER_BEOS
     2.8  #undef SDL_POWER_NINTENDODS
     2.9  #undef SDL_POWER_HARDWIRED
    2.10  
     3.1 --- a/src/power/SDL_power.c	Sun Jun 07 06:49:01 2009 +0000
     3.2 +++ b/src/power/SDL_power.c	Sun Jun 07 22:44:59 2009 +0000
     3.3 @@ -35,6 +35,7 @@
     3.4  SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState*, int*, int*);
     3.5  SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState*, int*, int*);
     3.6  SDL_bool SDL_GetPowerInfo_OS2(SDL_PowerState*, int*, int*);
     3.7 +SDL_bool SDL_GetPowerInfo_BeOS(SDL_PowerState*, int*, int*);
     3.8  SDL_bool SDL_GetPowerInfo_NintendoDS(SDL_PowerState*, int*, int*);
     3.9  
    3.10  #ifndef SDL_POWER_DISABLED
    3.11 @@ -71,6 +72,9 @@
    3.12  #ifdef SDL_POWER_NINTENDODS  /* handles Nintendo DS. */
    3.13      SDL_GetPowerInfo_NintendoDS,
    3.14  #endif
    3.15 +#ifdef SDL_POWER_BEOS  /* handles BeOS, Zeta, with euc.jp apm driver. */
    3.16 +    SDL_GetPowerInfo_BeOS,
    3.17 +#endif
    3.18  #ifdef SDL_POWER_HARDWIRED
    3.19      SDL_GetPowerInfo_Hardwired,
    3.20  #endif
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/power/beos/SDL_syspower.c	Sun Jun 07 22:44:59 2009 +0000
     4.3 @@ -0,0 +1,126 @@
     4.4 +/*
     4.5 +    SDL - Simple DirectMedia Layer
     4.6 +    Copyright (C) 1997-2009 Sam Lantinga
     4.7 +
     4.8 +    This library is free software; you can redistribute it and/or
     4.9 +    modify it under the terms of the GNU Lesser General Public
    4.10 +    License as published by the Free Software Foundation; either
    4.11 +    version 2.1 of the License, or (at your option) any later version.
    4.12 +
    4.13 +    This library is distributed in the hope that it will be useful,
    4.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.16 +    Lesser General Public License for more details.
    4.17 +
    4.18 +    You should have received a copy of the GNU Lesser General Public
    4.19 +    License along with this library; if not, write to the Free Software
    4.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    4.21 +
    4.22 +    Sam Lantinga
    4.23 +    slouken@libsdl.org
    4.24 +*/
    4.25 +#include "SDL_config.h"
    4.26 +
    4.27 +#ifndef SDL_POWER_DISABLED
    4.28 +#ifdef SDL_POWER_BEOS
    4.29 +
    4.30 +#include <stdio.h>
    4.31 +#include <stdlib.h>
    4.32 +#include <unistd.h>
    4.33 +#include <fcntl.h>
    4.34 +#include <ctype.h>
    4.35 +#include <drivers/Drivers.h>
    4.36 +
    4.37 +/* These values are from apm.h ... */
    4.38 +#define APM_DEVICE_PATH "/dev/misc/apm"
    4.39 +#define APM_FUNC_OFFSET 0x5300
    4.40 +#define APM_FUNC_GET_POWER_STATUS 10
    4.41 +#define APM_DEVICE_ALL 1
    4.42 +#define APM_BIOS_CALL (B_DEVICE_OP_CODES_END + 3)
    4.43 +
    4.44 +#include "SDL_power.h"
    4.45 +
    4.46 +SDL_bool
    4.47 +SDL_GetPowerInfo_BeOS(SDL_PowerState *state, int *seconds, int *percent)
    4.48 +{
    4.49 +    const int fd = open("/dev/misc/apm", O_RDONLY);
    4.50 +    SDL_bool need_details = SDL_FALSE;
    4.51 +    uint16 regs[6];
    4.52 +    uint8 ac_status;
    4.53 +    uint8 battery_status;
    4.54 +    uint8 battery_flags;
    4.55 +    uint8 battery_life;
    4.56 +    uint32 battery_time;
    4.57 +
    4.58 +    if (fd == -1) {
    4.59 +        return SDL_FALSE;  /* maybe some other method will work? */
    4.60 +    }
    4.61 +
    4.62 +    memset(regs, '\0', sizeof (regs));
    4.63 +    regs[0] = APM_FUNC_OFFSET + APM_FUNC_GET_POWER_STATUS;
    4.64 +    regs[1] = APM_DEVICE_ALL;
    4.65 +    rc = ioctl(fd, APM_BIOS_CALL, regs);
    4.66 +    close(fd);
    4.67 +
    4.68 +    if (rc < 0) {
    4.69 +        return SDL_FALSE;
    4.70 +    }
    4.71 +
    4.72 +    ac_status = regs[1] >> 8;
    4.73 +    battery_status = regs[1] & 0xFF;
    4.74 +    battery_flags = regs[2] >> 8;
    4.75 +    battery_life = regs[2] & 0xFF;
    4.76 +    battery_time = (uint32) regs[3];
    4.77 +
    4.78 +    /* in theory, _something_ should be set in battery_flags, right? */
    4.79 +    if (battery_flags == 0x00) {  /* older APM BIOS? Less fields. */
    4.80 +        battery_time = 0xFFFF;
    4.81 +        if (battery_status == 0xFF) {
    4.82 +            battery_flags = 0xFF;
    4.83 +        } else {
    4.84 +            battery_flags = (1 << status.battery_status);
    4.85 +        }
    4.86 +    }
    4.87 +
    4.88 +    if ( (battery_time != 0xFFFF) && (battery_time & (1 << 15)) ) {
    4.89 +        /* time is in minutes, not seconds */
    4.90 +        battery_time = (battery_time & 0x7FFF) * 60;
    4.91 +    }
    4.92 +
    4.93 +    if (battery_flags == 0xFF) {  /* unknown state */
    4.94 +        *state = SDL_POWERSTATE_UNKNOWN;
    4.95 +    } else if (battery_flags & (1 << 7)) {  /* no battery */
    4.96 +        *state = SDL_POWERSTATE_NO_BATTERY;
    4.97 +    } else if (battery_flags & (1 << 3)) {  /* charging */
    4.98 +        *state = SDL_POWERSTATE_CHARGING;
    4.99 +        need_details = SDL_TRUE;
   4.100 +    } else if (ac_status == 1) {
   4.101 +        *state = SDL_POWERSTATE_CHARGED;  /* on AC, not charging. */
   4.102 +        need_details = SDL_TRUE;
   4.103 +    } else {
   4.104 +        *state = SDL_POWERSTATE_ON_BATTERY;  /* not on AC. */
   4.105 +        need_details = SDL_TRUE;
   4.106 +    }
   4.107 +
   4.108 +    *percent = -1;
   4.109 +    *seconds = -1;
   4.110 +    if (need_details) {
   4.111 +        const int pct = (int) battery_life;
   4.112 +        const int secs = (int) battery_time;
   4.113 +
   4.114 +        if (pct != 255) {  /* 255 == unknown */
   4.115 +            *percent = (pct > 100) ? 100 : pct;  /* clamp between 0%, 100% */
   4.116 +        }
   4.117 +        if (secs != 0xFFFF) {  /* 0xFFFF == unknown */
   4.118 +            *seconds = secs;
   4.119 +        }
   4.120 +    }
   4.121 +
   4.122 +    return SDL_TRUE;  /* the definitive answer if APM driver replied. */
   4.123 +}
   4.124 +
   4.125 +#endif /* SDL_POWER_BEOS */
   4.126 +#endif /* SDL_POWER_DISABLED */
   4.127 +
   4.128 +/* vi: set ts=4 sw=4 expandtab: */
   4.129 +