Initial work on power subsystem for SDL 1.3.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 07 Jun 2009 06:06:35 +0000
changeset 3170b7a48f533966
parent 3169 f294338ca6eb
child 3171 5ecd475f38e7
Initial work on power subsystem for SDL 1.3.
Makefile.dc
Makefile.ds
Makefile.in
Makefile.minimal
Makefile.pandora
configure.in
include/SDL.h
include/SDL_config.h.in
include/SDL_config_dreamcast.h
include/SDL_config_iphoneos.h
include/SDL_config_macosx.h
include/SDL_config_nintendods.h
include/SDL_config_os2.h
include/SDL_config_win32.h
include/SDL_power.h
src/power/SDL_power.c
src/power/linux/SDL_syspower.c
src/power/macosx/SDL_syspower.c
src/power/nds/SDL_syspower.c
src/power/os2/SDL_syspower.c
src/power/windows/SDL_syspower.c
test/Makefile.in
test/testpower.c
     1.1 --- a/Makefile.dc	Sun Jun 07 02:45:55 2009 +0000
     1.2 +++ b/Makefile.dc	Sun Jun 07 06:06:35 2009 +0000
     1.3 @@ -31,6 +31,7 @@
     1.4  	src/events/SDL_quit.c \
     1.5  	src/events/SDL_resize.c \
     1.6  	src/file/SDL_rwops.c \
     1.7 +	src/power/SDL_power.c \
     1.8  	src/joystick/dc/SDL_sysjoystick.c \
     1.9  	src/joystick/SDL_joystick.c \
    1.10  	src/loadso/dummy/SDL_sysloadso.c \
     2.1 --- a/Makefile.ds	Sun Jun 07 02:45:55 2009 +0000
     2.2 +++ b/Makefile.ds	Sun Jun 07 06:06:35 2009 +0000
     2.3 @@ -55,6 +55,8 @@
     2.4  src/events/SDL_quit.c \
     2.5  src/events/SDL_windowevents.c \
     2.6  src/file/SDL_rwops.c \
     2.7 +src/power/SDL_power.c \
     2.8 +src/power/nds/SDL_syspower.c \
     2.9  src/haptic/SDL_haptic.c \
    2.10  src/haptic/nds/SDL_syshaptic.c \
    2.11  src/joystick/nds/SDL_sysjoystick.c \
     3.1 --- a/Makefile.in	Sun Jun 07 02:45:55 2009 +0000
     3.2 +++ b/Makefile.in	Sun Jun 07 06:06:35 2009 +0000
     3.3 @@ -42,7 +42,7 @@
     3.4  
     3.5  DIST = acinclude.m4 autogen.sh Borland.html Borland.zip BUGS build-scripts configure configure.in COPYING CREDITS docs docs.html include INSTALL Makefile.dc Makefile.minimal Makefile.in README* sdl-config.in sdl.m4 sdl.pc.in SDL.qpg.in SDL.spec SDL.spec.in src test TODO VisualC.html VisualC VisualCE Watcom-OS2.zip Watcom-Win32.zip WhatsNew Xcode
     3.6  
     3.7 -HDRS = SDL.h SDL_audio.h SDL_cdrom.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h
     3.8 +HDRS = SDL.h SDL_audio.h SDL_cdrom.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_power.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h
     3.9  
    3.10  LT_AGE      = @LT_AGE@
    3.11  LT_CURRENT  = @LT_CURRENT@
     4.1 --- a/Makefile.minimal	Sun Jun 07 02:45:55 2009 +0000
     4.2 +++ b/Makefile.minimal	Sun Jun 07 06:06:35 2009 +0000
     4.3 @@ -20,6 +20,7 @@
     4.4  	src/thread/*.c \
     4.5  	src/timer/*.c \
     4.6  	src/video/*.c \
     4.7 +	src/power/*.c \
     4.8  	src/audio/dummy/*.c \
     4.9  	src/video/dummy/*.c \
    4.10  	src/joystick/dummy/*.c \
     5.1 --- a/Makefile.pandora	Sun Jun 07 02:45:55 2009 +0000
     5.2 +++ b/Makefile.pandora	Sun Jun 07 06:06:35 2009 +0000
     5.3 @@ -14,7 +14,7 @@
     5.4  
     5.5  SOURCES = ./src/*.c ./src/audio/*.c ./src/cdrom/*.c ./src/cpuinfo/*.c ./src/events/*.c \
     5.6  	./src/file/*.c ./src/stdlib/*.c ./src/thread/*.c ./src/timer/*.c ./src/video/*.c \
     5.7 -	./src/joystick/*.c ./src/haptic/*.c ./src/video/dummy/*.c ./src/audio/disk/*.c \
     5.8 +	./src/joystick/*.c ./src/haptic/*.c ./src/power/*.c ./src/video/dummy/*.c ./src/audio/disk/*.c \
     5.9  	./src/audio/dummy/*.c ./src/loadso/dlopen/*.c ./src/audio/dsp/*.c ./src/audio/dma/*.c \
    5.10  	./src/thread/pthread/SDL_systhread.c ./src/thread/pthread/SDL_syssem.c \
    5.11  	./src/thread/pthread/SDL_sysmutex.c ./src/thread/pthread/SDL_syscond.c \
     6.1 --- a/configure.in	Sun Jun 07 02:45:55 2009 +0000
     6.2 +++ b/configure.in	Sun Jun 07 06:06:35 2009 +0000
     6.3 @@ -250,6 +250,14 @@
     6.4  else
     6.5      SOURCES="$SOURCES $srcdir/src/haptic/*.c"
     6.6  fi
     6.7 +AC_ARG_ENABLE(power,
     6.8 +AC_HELP_STRING([--enable-power], [Enable the power subsystem [[default=yes]]]),
     6.9 +              , enable_power=yes)
    6.10 +if test x$enable_power != xyes; then
    6.11 +    AC_DEFINE(SDL_POWER_DISABLED)
    6.12 +else
    6.13 +    SOURCES="$SOURCES $srcdir/src/power/*.c"
    6.14 +fi
    6.15  AC_ARG_ENABLE(cdrom,
    6.16  AC_HELP_STRING([--enable-cdrom], [Enable the cdrom subsystem [[default=yes]]]),
    6.17                , enable_cdrom=yes)
    6.18 @@ -2446,6 +2454,16 @@
    6.19               esac
    6.20             fi
    6.21          fi
    6.22 +        # Set up files for the power library
    6.23 +        if test x$enable_power = xyes; then
    6.24 +             case $ARCH in
    6.25 +               linux)
    6.26 +                   AC_DEFINE(SDL_POWER_LINUX)
    6.27 +                   SOURCES="$SOURCES $srcdir/src/power/linux/*.c"
    6.28 +                   have_power=yes
    6.29 +               ;;
    6.30 +             esac
    6.31 +        fi
    6.32          # Set up files for the cdrom library
    6.33          if test x$enable_cdrom = xyes; then
    6.34            case $ARCH in
    6.35 @@ -2620,6 +2638,12 @@
    6.36              SOURCES="$SOURCES $srcdir/src/loadso/win32/*.c"
    6.37              have_loadso=yes
    6.38          fi
    6.39 +        # Set up files for the system power library
    6.40 +        if test x$enable_power = xyes; then
    6.41 +            AC_DEFINE(SDL_POWER_WINDOWS)
    6.42 +            SOURCES="$SOURCES $srcdir/src/power/windows/*.c"
    6.43 +            have_power=yes
    6.44 +        fi
    6.45          # Set up the system libraries we need
    6.46          EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lcoredll -lcommctrl -lmmtimer"
    6.47          # The Win32 platform requires special setup
    6.48 @@ -2691,6 +2715,11 @@
    6.49                  have_haptic=yes
    6.50              fi
    6.51          fi
    6.52 +        if test x$enable_power = xyes; then
    6.53 +            AC_DEFINE(SDL_POWER_WINDOWS)
    6.54 +            SOURCES="$SOURCES $srcdir/src/power/windows/SDL_syspower.c"
    6.55 +            have_power=yes
    6.56 +        fi
    6.57          # Set up files for the cdrom library
    6.58          if test x$enable_cdrom = xyes; then
    6.59              AC_DEFINE(SDL_CDROM_WIN32)
    6.60 @@ -2842,6 +2871,12 @@
    6.61              have_haptic=yes
    6.62              EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,ForceFeedback"
    6.63          fi
    6.64 +        # Set up files for the power library
    6.65 +        if test x$enable_power = xyes; then
    6.66 +            AC_DEFINE(SDL_POWER_MACOSX)
    6.67 +            SOURCES="$SOURCES $srcdir/src/power/macosx/*.c"
    6.68 +            have_power=yes
    6.69 +        fi
    6.70          # Set up files for the cdrom library
    6.71          if test x$enable_cdrom = xyes; then
    6.72              AC_DEFINE(SDL_CDROM_MACOSX)
     7.1 --- a/include/SDL.h	Sun Jun 07 02:45:55 2009 +0000
     7.2 +++ b/include/SDL.h	Sun Jun 07 06:06:35 2009 +0000
     7.3 @@ -84,6 +84,7 @@
     7.4  #include "SDL_events.h"
     7.5  #include "SDL_loadso.h"
     7.6  #include "SDL_mutex.h"
     7.7 +#include "SDL_power.h"
     7.8  #include "SDL_rwops.h"
     7.9  #include "SDL_thread.h"
    7.10  #include "SDL_timer.h"
     8.1 --- a/include/SDL_config.h.in	Sun Jun 07 02:45:55 2009 +0000
     8.2 +++ b/include/SDL_config.h.in	Sun Jun 07 06:06:35 2009 +0000
     8.3 @@ -166,6 +166,7 @@
     8.4  #undef SDL_THREADS_DISABLED
     8.5  #undef SDL_TIMERS_DISABLED
     8.6  #undef SDL_VIDEO_DISABLED
     8.7 +#undef SDL_POWER_DISABLED
     8.8  
     8.9  /* Enable various audio drivers */
    8.10  #undef SDL_AUDIO_DRIVER_ALSA
    8.11 @@ -324,6 +325,14 @@
    8.12  #undef SDL_VIDEO_OPENGL_OSMESA
    8.13  #undef SDL_VIDEO_OPENGL_OSMESA_DYNAMIC
    8.14  
    8.15 +/* Enable system power support */
    8.16 +#undef SDL_POWER_LINUX
    8.17 +#undef SDL_POWER_WINDOWS
    8.18 +#undef SDL_POWER_MACOSX
    8.19 +#undef SDL_POWER_OS2
    8.20 +#undef SDL_POWER_NINTENDODS
    8.21 +#undef SDL_POWER_HARDWIRED
    8.22 +
    8.23  /* Enable assembly routines */
    8.24  #undef SDL_ASSEMBLY_ROUTINES
    8.25  #undef SDL_ALTIVEC_BLITTERS
     9.1 --- a/include/SDL_config_dreamcast.h	Sun Jun 07 02:45:55 2009 +0000
     9.2 +++ b/include/SDL_config_dreamcast.h	Sun Jun 07 06:06:35 2009 +0000
     9.3 @@ -104,4 +104,6 @@
     9.4  #define SDL_VIDEO_DRIVER_DC	1
     9.5  #define SDL_VIDEO_DRIVER_DUMMY	1
     9.6  
     9.7 +#define SDL_POWER_HARDWIRED 1
     9.8 +
     9.9  #endif /* _SDL_config_dreamcast_h */
    10.1 --- a/include/SDL_config_iphoneos.h	Sun Jun 07 02:45:55 2009 +0000
    10.2 +++ b/include/SDL_config_iphoneos.h	Sun Jun 07 06:06:35 2009 +0000
    10.3 @@ -131,6 +131,9 @@
    10.4  #define SDL_VIDEO_OPENGL_ES	1
    10.5  #define SDL_VIDEO_RENDER_OGL_ES	1
    10.6  
    10.7 +/* Enable system power support */
    10.8 +#define SDL_POWER_MACOSX 1
    10.9 +
   10.10  /* enable iPhone keyboard support */
   10.11  #define SDL_IPHONE_KEYBOARD 1
   10.12  
    11.1 --- a/include/SDL_config_macosx.h	Sun Jun 07 02:45:55 2009 +0000
    11.2 +++ b/include/SDL_config_macosx.h	Sun Jun 07 06:06:35 2009 +0000
    11.3 @@ -137,6 +137,9 @@
    11.4  #define SDL_VIDEO_OPENGL_CGL	1
    11.5  #define SDL_VIDEO_RENDER_OGL	1
    11.6  
    11.7 +/* Enable system power support */
    11.8 +#define SDL_POWER_MACOSX 1
    11.9 +
   11.10  /* Enable assembly routines */
   11.11  #define SDL_ASSEMBLY_ROUTINES	1
   11.12  #ifdef __ppc__
    12.1 --- a/include/SDL_config_nintendods.h	Sun Jun 07 02:45:55 2009 +0000
    12.2 +++ b/include/SDL_config_nintendods.h	Sun Jun 07 06:06:35 2009 +0000
    12.3 @@ -116,4 +116,7 @@
    12.4  #define SDL_VIDEO_DRIVER_NDS	1
    12.5  /*#define SDL_VIDEO_DRIVER_DUMMY	1 TODO: uncomment this later*/
    12.6  
    12.7 +/* Enable system power support */
    12.8 +#define SDL_POWER_NINTENDODS 1
    12.9 +
   12.10  #endif /* _SDL_config_nintendods_h */
    13.1 --- a/include/SDL_config_os2.h	Sun Jun 07 02:45:55 2009 +0000
    13.2 +++ b/include/SDL_config_os2.h	Sun Jun 07 06:06:35 2009 +0000
    13.3 @@ -136,6 +136,9 @@
    13.4  /* Enable OpenGL support */
    13.5  /* Nothing here yet for OS/2... :( */
    13.6  
    13.7 +/* Enable system power support */
    13.8 +#define SDL_POWER_OS2 1
    13.9 +
   13.10  /* Enable assembly routines where available */
   13.11  #define SDL_ASSEMBLY_ROUTINES	1
   13.12  
    14.1 --- a/include/SDL_config_win32.h	Sun Jun 07 02:45:55 2009 +0000
    14.2 +++ b/include/SDL_config_win32.h	Sun Jun 07 06:06:35 2009 +0000
    14.3 @@ -185,6 +185,9 @@
    14.4  #define SDL_VIDEO_RENDER_OGL	1
    14.5  #endif
    14.6  
    14.7 +/* Enable system power support */
    14.8 +#define SDL_POWER_WINDOWS 1
    14.9 +
   14.10  /* Enable assembly routines (Win64 doesn't have inline asm) */
   14.11  #ifndef _WIN64
   14.12  #define SDL_ASSEMBLY_ROUTINES	1
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/include/SDL_power.h	Sun Jun 07 06:06:35 2009 +0000
    15.3 @@ -0,0 +1,84 @@
    15.4 +/*
    15.5 +    SDL - Simple DirectMedia Layer
    15.6 +    Copyright (C) 1997-2009 Sam Lantinga
    15.7 +
    15.8 +    This library is free software; you can redistribute it and/or
    15.9 +    modify it under the terms of the GNU Lesser General Public
   15.10 +    License as published by the Free Software Foundation; either
   15.11 +    version 2.1 of the License, or (at your option) any later version.
   15.12 +
   15.13 +    This library is distributed in the hope that it will be useful,
   15.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15.16 +    Lesser General Public License for more details.
   15.17 +
   15.18 +    You should have received a copy of the GNU Lesser General Public
   15.19 +    License along with this library; if not, write to the Free Software
   15.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   15.21 +
   15.22 +    Sam Lantinga
   15.23 +    slouken@libsdl.org
   15.24 +*/
   15.25 +
   15.26 +#ifndef _SDL_power_h
   15.27 +#define _SDL_power_h
   15.28 +
   15.29 +/**
   15.30 + * \file SDL_power.h
   15.31 + *
   15.32 + * Header for the SDL power management routines
   15.33 + */
   15.34 +
   15.35 +#include "SDL_stdinc.h"
   15.36 +
   15.37 +#include "begin_code.h"
   15.38 +/* Set up for C function definitions, even when using C++ */
   15.39 +#ifdef __cplusplus
   15.40 +/* *INDENT-OFF* */
   15.41 +extern "C" {
   15.42 +/* *INDENT-ON* */
   15.43 +#endif
   15.44 +
   15.45 +/**
   15.46 + * \enum SDL_PowerState
   15.47 + *
   15.48 + * \brief The basic state for the system's power supply.
   15.49 + */
   15.50 +typedef enum
   15.51 +{
   15.52 +    SDL_POWERSTATE_UNKNOWN,      /**< cannot determine power status */
   15.53 +    SDL_POWERSTATE_ON_BATTERY,   /**< Not plugged in, running on the battery */
   15.54 +    SDL_POWERSTATE_NO_BATTERY,   /**< Plugged in, no battery available */
   15.55 +    SDL_POWERSTATE_CHARGING,     /**< Plugged in, charging battery */
   15.56 +    SDL_POWERSTATE_CHARGED,      /**< Plugged in, battery charged */
   15.57 +} SDL_PowerState;
   15.58 +
   15.59 +
   15.60 +/**
   15.61 + * \fn int SDL_GetPowerInfo(void)
   15.62 + *
   15.63 + * \brief Get the current power supply details.
   15.64 + *
   15.65 + * \param secs Seconds of battery life left. You can pass a NULL here if
   15.66 + *                you don't care. Will return -1 if we can't determine a
   15.67 + *                value, or we're not running on a battery.
   15.68 + *
   15.69 + * \param pct Percentage of battery life left, between 0 and 100. You can
   15.70 + *                pass a NULL here if you don't care. Will return -1 if we
   15.71 + *                can't determine a value, or we're not running on a battery.
   15.72 + *
   15.73 + * \return The state of the battery (if any).
   15.74 + */
   15.75 +extern DECLSPEC SDL_PowerState SDLCALL SDL_GetPowerInfo(int *secs, int *pct);
   15.76 +
   15.77 +/* Ends C function definitions when using C++ */
   15.78 +#ifdef __cplusplus
   15.79 +/* *INDENT-OFF* */
   15.80 +}
   15.81 +/* *INDENT-ON* */
   15.82 +#endif
   15.83 +#include "close_code.h"
   15.84 +
   15.85 +#endif /* _SDL_power_h */
   15.86 +
   15.87 +/* vi: set ts=4 sw=4 expandtab: */
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/src/power/SDL_power.c	Sun Jun 07 06:06:35 2009 +0000
    16.3 @@ -0,0 +1,110 @@
    16.4 +/*
    16.5 +    SDL - Simple DirectMedia Layer
    16.6 +    Copyright (C) 1997-2009 Sam Lantinga
    16.7 +
    16.8 +    This library is free software; you can redistribute it and/or
    16.9 +    modify it under the terms of the GNU Lesser General Public
   16.10 +    License as published by the Free Software Foundation; either
   16.11 +    version 2.1 of the License, or (at your option) any later version.
   16.12 +
   16.13 +    This library is distributed in the hope that it will be useful,
   16.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   16.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   16.16 +    Lesser General Public License for more details.
   16.17 +
   16.18 +    You should have received a copy of the GNU Lesser General Public
   16.19 +    License along with this library; if not, write to the Free Software
   16.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   16.21 +
   16.22 +    Sam Lantinga
   16.23 +    slouken@libsdl.org
   16.24 +*/
   16.25 +#include "SDL_config.h"
   16.26 +#include "SDL_power.h"
   16.27 +
   16.28 +/*
   16.29 + * Returns SDL_TRUE if we have a definitive answer.
   16.30 + * SDL_FALSE to try next implementation.
   16.31 + */
   16.32 +typedef SDL_bool
   16.33 +(*SDL_GetPowerInfo_Impl)(SDL_PowerState *state, int *seconds, int *percent);
   16.34 +
   16.35 +SDL_bool SDL_GetPowerInfo_Linux_sys_power(SDL_PowerState*, int*, int*);
   16.36 +SDL_bool SDL_GetPowerInfo_Linux_proc_apci(SDL_PowerState*, int*, int*);
   16.37 +SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState*, int*, int*);
   16.38 +SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState*, int*, int*);
   16.39 +SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState*, int*, int*);
   16.40 +SDL_bool SDL_GetPowerInfo_OS2(SDL_PowerState*, int*, int*);
   16.41 +SDL_bool SDL_GetPowerInfo_NintendoDS(SDL_PowerState*, int*, int*);
   16.42 +
   16.43 +#ifndef SDL_POWER_DISABLED
   16.44 +#ifdef SDL_POWER_HARDWIRED
   16.45 +/* This is for things that _never_ have a battery, like the Dreamcast, etc. */
   16.46 +static SDL_bool
   16.47 +SDL_GetPowerInfo_Hardwired(SDL_PowerState *state, int *seconds, int *percent)
   16.48 +{
   16.49 +    *seconds = -1;
   16.50 +    *percent = -1;
   16.51 +    *state = SDL_POWERSTATE_NO_BATTERY;
   16.52 +    return SDL_TRUE;
   16.53 +}
   16.54 +#endif
   16.55 +#endif
   16.56 +
   16.57 +
   16.58 +static SDL_GetPowerInfo_Impl implementations[] = {
   16.59 +#ifndef SDL_POWER_DISABLED
   16.60 +#ifdef SDL_POWER_LINUX /* in order of preference. More than could work. */
   16.61 +    SDL_GetPowerInfo_Linux_sys_power,
   16.62 +    SDL_GetPowerInfo_Linux_proc_apci,
   16.63 +    SDL_GetPowerInfo_Linux_proc_apm,
   16.64 +#endif
   16.65 +#ifdef SDL_POWER_WINDOWS /* handles Win32, Win64, PocketPC. */
   16.66 +    SDL_GetPowerInfo_Windows,
   16.67 +#endif
   16.68 +#ifdef SDL_POWER_MACOSX  /* handles Mac OS X, Darwin, iPhone. */
   16.69 +    SDL_GetPowerInfo_MacOSX,
   16.70 +#endif
   16.71 +#ifdef SDL_POWER_OS2  /* handles OS/2, Warp, eComStation. */
   16.72 +    SDL_GetPowerInfo_OS2,
   16.73 +#endif
   16.74 +#ifdef SDL_POWER_NINTENDODS  /* handles Nintendo DS. */
   16.75 +    SDL_GetPowerInfo_NintendoDS,
   16.76 +#endif
   16.77 +#ifdef SDL_POWER_HARDWIRED
   16.78 +    SDL_GetPowerInfo_Hardwired,
   16.79 +#endif
   16.80 +#endif
   16.81 +};
   16.82 +
   16.83 +SDL_PowerState
   16.84 +SDL_GetPowerInfo(int *seconds, int *percent)
   16.85 +{
   16.86 +    const int total = sizeof (implementations) / sizeof (implementations[0]);
   16.87 +    int _seconds, _percent;
   16.88 +    SDL_PowerState retval;
   16.89 +    int i;
   16.90 +
   16.91 +    /* Make these never NULL for platform-specific implementations. */
   16.92 +    if (seconds == NULL) {
   16.93 +        seconds = &_seconds;
   16.94 +    }
   16.95 +
   16.96 +    if (percent == NULL) {
   16.97 +        percent = &_percent;
   16.98 +    }
   16.99 +
  16.100 +    for (i = 0; i < total; i++) {
  16.101 +        if (implementations[i](&retval, seconds, percent)) {
  16.102 +            return retval;
  16.103 +        }
  16.104 +    }
  16.105 +
  16.106 +    /* nothing was definitive. */
  16.107 +    *seconds = -1;
  16.108 +    *percent = -1;
  16.109 +    return SDL_POWERSTATE_UNKNOWN;
  16.110 +}
  16.111 +
  16.112 +/* vi: set ts=4 sw=4 expandtab: */
  16.113 +
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/src/power/linux/SDL_syspower.c	Sun Jun 07 06:06:35 2009 +0000
    17.3 @@ -0,0 +1,207 @@
    17.4 +/*
    17.5 +    SDL - Simple DirectMedia Layer
    17.6 +    Copyright (C) 1997-2009 Sam Lantinga
    17.7 +
    17.8 +    This library is free software; you can redistribute it and/or
    17.9 +    modify it under the terms of the GNU Lesser General Public
   17.10 +    License as published by the Free Software Foundation; either
   17.11 +    version 2.1 of the License, or (at your option) any later version.
   17.12 +
   17.13 +    This library is distributed in the hope that it will be useful,
   17.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   17.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   17.16 +    Lesser General Public License for more details.
   17.17 +
   17.18 +    You should have received a copy of the GNU Lesser General Public
   17.19 +    License along with this library; if not, write to the Free Software
   17.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   17.21 +
   17.22 +    Sam Lantinga
   17.23 +    slouken@libsdl.org
   17.24 +*/
   17.25 +#include "SDL_config.h"
   17.26 +
   17.27 +#ifndef SDL_POWER_DISABLED
   17.28 +#ifdef SDL_POWER_LINUX
   17.29 +
   17.30 +#include <stdio.h>
   17.31 +#include <unistd.h>
   17.32 +
   17.33 +#include "SDL_power.h"
   17.34 +
   17.35 +SDL_bool
   17.36 +SDL_GetPowerInfo_Linux_sys_power(SDL_PowerState *state,
   17.37 +                                 int *seconds, int *percent)
   17.38 +{
   17.39 +    return SDL_FALSE;  /* !!! FIXME: write me. */
   17.40 +#if 0
   17.41 +    const int fd = open("/sys/power", O_RDONLY);
   17.42 +    if (fd == -1) {
   17.43 +        return SDL_FALSE;  /* can't use this interface. */
   17.44 +    }
   17.45 +    return SDL_TRUE;
   17.46 +#endif
   17.47 +}
   17.48 +
   17.49 +SDL_bool
   17.50 +SDL_GetPowerInfo_Linux_sys_proc_acpi(SDL_PowerState *state,
   17.51 +                                     int *seconds, int *percent)
   17.52 +{
   17.53 +    return SDL_FALSE;  /* !!! FIXME: write me. */
   17.54 +#if 0
   17.55 +    const int fd = open("/proc/acpi", O_RDONLY);
   17.56 +    if (fd == -1) {
   17.57 +        return SDL_FALSE;  /* can't use this interface. */
   17.58 +    }
   17.59 +    return SDL_TRUE;
   17.60 +#endif
   17.61 +}
   17.62 +
   17.63 +static SDL_bool
   17.64 +next_string(char **_ptr, char **_str)
   17.65 +{
   17.66 +    char *ptr = *_ptr;
   17.67 +    char *str = *_str;
   17.68 +
   17.69 +    while (*ptr == ' ') {  /* skip any spaces... */
   17.70 +        ptr++;
   17.71 +    }
   17.72 +
   17.73 +    if (*ptr == '\0') {
   17.74 +        return SDL_FALSE;
   17.75 +    }
   17.76 +
   17.77 +    str = ptr;
   17.78 +    while ((*ptr != ' ') && (*ptr != '\0'))
   17.79 +        ptr++;
   17.80 +
   17.81 +    if (*ptr != '\0')
   17.82 +        *(ptr++) = '\0';
   17.83 +
   17.84 +    *_str = str;
   17.85 +    *_ptr = ptr;
   17.86 +    return SDL_TRUE;
   17.87 +}
   17.88 +
   17.89 +static SDL_bool
   17.90 +int_string(char *str, int *val)
   17.91 +{
   17.92 +    char *endptr = NULL;
   17.93 +    *val = (int) strtol(str+2, &endptr, 16);
   17.94 +    return ((*str != '\0') && (*endptr == '\0'));
   17.95 +}
   17.96 +
   17.97 +/* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */
   17.98 +SDL_bool
   17.99 +SDL_GetPowerInfo_Linux_sys_proc_apm(SDL_PowerState *state,
  17.100 +                                    int *seconds, int *percent)
  17.101 +{
  17.102 +    SDL_bool need_details = SDL_FALSE;
  17.103 +    int ac_status = 0;
  17.104 +    int battery_status = 0;
  17.105 +    int battery_flag = 0;
  17.106 +    int battery_percent = 0;
  17.107 +    int battery_time = 0;
  17.108 +    const int fd = open("/proc/apm", O_RDONLY);
  17.109 +    char buf[128];
  17.110 +    char *ptr = &buf[0];
  17.111 +    char *str = NULL;
  17.112 +    ssize_t br;
  17.113 +
  17.114 +    if (fd == -1) {
  17.115 +        return SDL_FALSE;  /* can't use this interface. */
  17.116 +    }
  17.117 +
  17.118 +    br = read(fd, buf, sizeof (buf) - 1);
  17.119 +    close(fd);
  17.120 +
  17.121 +    if (br < 0) {
  17.122 +        return SDL_FALSE;
  17.123 +    }
  17.124 +
  17.125 +    buf[br] = '\0';  // null-terminate the string.
  17.126 +    if (!next_string(&ptr, &str)) {  /* driver version */
  17.127 +        return SDL_FALSE;
  17.128 +    }
  17.129 +    if (!next_string(&ptr, &str)) {  /* BIOS version */
  17.130 +        return SDL_FALSE;
  17.131 +    }
  17.132 +    if (!next_string(&ptr, &str)) {  /* APM flags */
  17.133 +        return SDL_FALSE;
  17.134 +    }
  17.135 +
  17.136 +    if (!next_string(&ptr, &str)) {  /* AC line status */
  17.137 +        return SDL_FALSE;
  17.138 +    } else if (!int_string(str, &ac_status)) {
  17.139 +        return SDL_FALSE;
  17.140 +    }
  17.141 +
  17.142 +    if (!next_string(&ptr, &str)) {  /* battery status */
  17.143 +        return SDL_FALSE;
  17.144 +    } else if (!int_string(str, &battery_status)) {
  17.145 +        return SDL_FALSE;
  17.146 +    }
  17.147 +    if (!next_string(&ptr, &str)) {  /* battery flag */
  17.148 +        return SDL_FALSE;
  17.149 +    } else if (!int_string(str, &battery_flag)) {
  17.150 +        return SDL_FALSE;
  17.151 +    }
  17.152 +    if (!next_string(&ptr, &str)) {  /* remaining battery life percent */
  17.153 +        return SDL_FALSE;
  17.154 +    }
  17.155 +    if (str[strlen(str) - 1] == '%') {
  17.156 +        str[strlen(str) - 1] = '\0';
  17.157 +    }
  17.158 +    if (!int_string(str, &battery_percent)) {
  17.159 +        return SDL_FALSE;
  17.160 +    }
  17.161 +
  17.162 +    if (!next_string(&ptr, &str)) {  /* remaining battery life time */
  17.163 +        return SDL_FALSE;
  17.164 +    } else if (!int_string(str, &battery_time)) {
  17.165 +        return SDL_FALSE;
  17.166 +    }
  17.167 +
  17.168 +    if (!next_string(&ptr, &str)) {  /* remaining battery life time units */
  17.169 +        return SDL_FALSE;
  17.170 +    } else if (strcmp(str, "min") == 0) {
  17.171 +        battery_time *= 60;
  17.172 +    }
  17.173 +
  17.174 +    if (battery_flag == 0xFF) {  /* unknown state */
  17.175 +        *state = SDL_POWERSTATE_UNKNOWN;
  17.176 +    } else if (battery_flag & (1 << 7)) {  /* no battery */
  17.177 +        *state = SDL_POWERSTATE_NO_BATTERY;
  17.178 +    } else if (battery_flag & (1 << 3)) {  /* charging */
  17.179 +        *state = SDL_POWERSTATE_CHARGING;
  17.180 +        need_details = SDL_TRUE;
  17.181 +    } else if (ac_status == 1) {
  17.182 +        *state = SDL_POWERSTATE_CHARGED;  /* on AC, not charging. */
  17.183 +        need_details = SDL_TRUE;
  17.184 +    } else {
  17.185 +        *state = SDL_POWERSTATE_ON_BATTERY;
  17.186 +        need_details = SDL_TRUE;
  17.187 +    }
  17.188 +
  17.189 +    *percent = -1;
  17.190 +    *seconds = -1;
  17.191 +    if (need_details) {
  17.192 +        const int pct = battery_percent;
  17.193 +        const int secs = battery_time;
  17.194 +
  17.195 +        if (pct >= 0) {  /* -1 == unknown */
  17.196 +            *percent = (pct > 100) ? 100 : pct;  /* clamp between 0%, 100% */
  17.197 +        }
  17.198 +        if (secs >= 0) {  /* -1 == unknown */
  17.199 +            *seconds = secs;
  17.200 +        }
  17.201 +    }
  17.202 +
  17.203 +    return SDL_TRUE;
  17.204 +}
  17.205 +
  17.206 +#endif /* SDL_POWER_LINUX */
  17.207 +#endif /* SDL_POWER_DISABLED */
  17.208 +
  17.209 +/* vi: set ts=4 sw=4 expandtab: */
  17.210 +
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/src/power/macosx/SDL_syspower.c	Sun Jun 07 06:06:35 2009 +0000
    18.3 @@ -0,0 +1,193 @@
    18.4 +/*
    18.5 +    SDL - Simple DirectMedia Layer
    18.6 +    Copyright (C) 1997-2009 Sam Lantinga
    18.7 +
    18.8 +    This library is free software; you can redistribute it and/or
    18.9 +    modify it under the terms of the GNU Lesser General Public
   18.10 +    License as published by the Free Software Foundation; either
   18.11 +    version 2.1 of the License, or (at your option) any later version.
   18.12 +
   18.13 +    This library is distributed in the hope that it will be useful,
   18.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   18.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   18.16 +    Lesser General Public License for more details.
   18.17 +
   18.18 +    You should have received a copy of the GNU Lesser General Public
   18.19 +    License along with this library; if not, write to the Free Software
   18.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   18.21 +
   18.22 +    Sam Lantinga
   18.23 +    slouken@libsdl.org
   18.24 +*/
   18.25 +#include "SDL_config.h"
   18.26 +
   18.27 +#ifndef SDL_POWER_DISABLED
   18.28 +#ifdef SDL_POWER_MACOSX
   18.29 +
   18.30 +#include <Carbon/Carbon.h>
   18.31 +#include <IOKit/ps/IOPowerSources.h>
   18.32 +#include <IOKit/ps/IOPSKeys.h>
   18.33 +
   18.34 +#include "SDL_power.h"
   18.35 +
   18.36 +/* Carbon is so verbose... */
   18.37 +#define STRMATCH(a,b) (CFStringCompare(a, b, 0) == kCFCompareEqualTo)
   18.38 +#define GETVAL(k,v) \
   18.39 +    CFDictionaryGetValueIfPresent(dict, CFSTR(k), (const void **) v)
   18.40 +
   18.41 +/* Note that AC power sources also include a laptop battery it is charging. */
   18.42 +static void
   18.43 +checkps(CFDictionaryRef dict, SDL_bool *have_ac, SDL_bool *have_battery,
   18.44 +        SDL_bool *charging, int *seconds, int *percent)
   18.45 +{
   18.46 +    CFStringRef strval;  /* don't CFRelease() this. */
   18.47 +    CFBooleanRef bval;
   18.48 +    CFNumberRef numval;
   18.49 +    SDL_bool charge = SDL_FALSE;
   18.50 +    SDL_bool choose = SDL_FALSE;
   18.51 +    SDL_bool is_ac = SDL_FALSE;
   18.52 +    int secs = -1;
   18.53 +    int maxpct = -1;
   18.54 +    int pct = -1;
   18.55 +
   18.56 +    if ((GETVAL(kIOPSIsPresentKey, &bval)) && (bval == kCFBooleanFalse)) {
   18.57 +        return;  /* nothing to see here. */
   18.58 +    }
   18.59 +
   18.60 +    if (!GETVAL(kIOPSPowerSourceStateKey, &strval)) {
   18.61 +        return;
   18.62 +    }
   18.63 +
   18.64 +    if (STRMATCH(strval, CFSTR(kIOPSACPowerValue))) {
   18.65 +        is_ac = *have_ac = SDL_TRUE;
   18.66 +    } else if (!STRMATCH(strval, CFSTR(kIOPSBatteryPowerValue))) {
   18.67 +        return;  /* not a battery? */
   18.68 +    }
   18.69 +
   18.70 +    if ((GETVAL(kIOPSIsChargingKey, &bval)) && (bval == kCFBooleanTrue)) {
   18.71 +        charge = SDL_TRUE;
   18.72 +    }
   18.73 +
   18.74 +    if (GETVAL(kIOPSMaxCapacityKey, &numval)) {
   18.75 +        SInt32 val = -1;
   18.76 +        CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
   18.77 +        if (val > 0) {
   18.78 +            *have_battery = SDL_TRUE;
   18.79 +            maxpct = (int) val;
   18.80 +        }
   18.81 +    }
   18.82 +
   18.83 +    if (GETVAL(kIOPSMaxCapacityKey, &numval)) {
   18.84 +        SInt32 val = -1;
   18.85 +        CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
   18.86 +        if (val > 0) {
   18.87 +            *have_battery = SDL_TRUE;
   18.88 +            maxpct = (int) val;
   18.89 +        }
   18.90 +    }
   18.91 +
   18.92 +    if (GETVAL(kIOPSTimeToEmptyKey, &numval)) {
   18.93 +        SInt32 val = -1;
   18.94 +        CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
   18.95 +
   18.96 +        /* Mac OS X reports 0 minutes until empty if you're plugged in. :( */
   18.97 +        if ((val == 0) && (is_ac)) {
   18.98 +            val = -1;  /* !!! FIXME: calc from timeToFull and capacity? */
   18.99 +        }
  18.100 +
  18.101 +        secs = (int) val;
  18.102 +        if (secs > 0) {
  18.103 +            secs *= 60;  /* value is in minutes, so convert to seconds. */
  18.104 +        }
  18.105 +    }
  18.106 +
  18.107 +    if (GETVAL(kIOPSCurrentCapacityKey, &numval)) {
  18.108 +        SInt32 val = -1;
  18.109 +        CFNumberGetValue(numval, kCFNumberSInt32Type, &val);
  18.110 +        pct = (int) val;
  18.111 +    }
  18.112 +
  18.113 +    if ((pct > 0) && (maxpct > 0)) {
  18.114 +    	pct = (int) ((((double)pct)/((double)maxpct)) * 100.0);
  18.115 +    }
  18.116 +
  18.117 +    if (pct > 100) {
  18.118 +        pct = 100;
  18.119 +    }
  18.120 +
  18.121 +    /*
  18.122 +     * We pick the battery that claims to have the most minutes left.
  18.123 +     *  (failing a report of minutes, we'll take the highest percent.)
  18.124 +     */
  18.125 +    if ((secs < 0) && (*seconds < 0)) {
  18.126 +        if ((pct < 0) && (*percent < 0)) {
  18.127 +            choose = SDL_TRUE; /* at least we know there's a battery. */
  18.128 +        }
  18.129 +        if (pct > *percent) {
  18.130 +            choose = SDL_TRUE;
  18.131 +        }
  18.132 +    } else if (secs > *seconds) {
  18.133 +        choose = SDL_TRUE;
  18.134 +    }
  18.135 +
  18.136 +    if (choose) {
  18.137 +        *seconds = secs;
  18.138 +        *percent = pct;
  18.139 +        *charging = charge;
  18.140 +    }
  18.141 +}
  18.142 +
  18.143 +#undef GETVAL
  18.144 +#undef STRMATCH
  18.145 +
  18.146 +
  18.147 +SDL_bool
  18.148 +SDL_GetPowerInfo_MacOSX(SDL_PowerState *state, int *seconds, int *percent)
  18.149 +{
  18.150 +    CFTypeRef blob = IOPSCopyPowerSourcesInfo();
  18.151 +
  18.152 +    *seconds = -1;
  18.153 +    *percent = -1;
  18.154 +    *state = SDL_POWERSTATE_UNKNOWN;
  18.155 +
  18.156 +    if (blob != NULL) {
  18.157 +        CFArrayRef list = IOPSCopyPowerSourcesList(blob);
  18.158 +        if (list != NULL) {
  18.159 +            /* don't CFRelease() the list items, or dictionaries! */
  18.160 +            SDL_bool have_ac = SDL_FALSE;
  18.161 +            SDL_bool have_battery = SDL_FALSE;
  18.162 +            SDL_bool charging = SDL_FALSE;
  18.163 +            const CFIndex total = CFArrayGetCount(list);
  18.164 +            CFIndex i;
  18.165 +            for (i = 0; i < total; i++) {
  18.166 +                CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i);
  18.167 +                CFDictionaryRef dict = IOPSGetPowerSourceDescription(blob, ps);
  18.168 +                if (dict != NULL) {
  18.169 +                    checkps(dict, &have_ac, &have_battery, &charging,
  18.170 +                            seconds, percent);
  18.171 +                }
  18.172 +            }
  18.173 +
  18.174 +            if (!have_battery) {
  18.175 +                *state = SDL_POWERSTATE_NO_BATTERY;
  18.176 +            } else if (charging) {
  18.177 +                *state = SDL_POWERSTATE_CHARGING;
  18.178 +            } else if (have_ac) {
  18.179 +                *state = SDL_POWERSTATE_CHARGED;
  18.180 +            } else {
  18.181 +                *state = SDL_POWERSTATE_ON_BATTERY;
  18.182 +            }
  18.183 +
  18.184 +            CFRelease(list);
  18.185 +        }
  18.186 +        CFRelease(blob);
  18.187 +    }
  18.188 +
  18.189 +    return SDL_TRUE;  /* always the definitive answer on Mac OS X. */
  18.190 +}
  18.191 +
  18.192 +#endif /* SDL_POWER_MACOSX */
  18.193 +#endif /* SDL_POWER_DISABLED */
  18.194 +
  18.195 +/* vi: set ts=4 sw=4 expandtab: */
  18.196 +
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/src/power/nds/SDL_syspower.c	Sun Jun 07 06:06:35 2009 +0000
    19.3 @@ -0,0 +1,45 @@
    19.4 +/*
    19.5 +    SDL - Simple DirectMedia Layer
    19.6 +    Copyright (C) 1997-2009 Sam Lantinga
    19.7 +
    19.8 +    This library is free software; you can redistribute it and/or
    19.9 +    modify it under the terms of the GNU Lesser General Public
   19.10 +    License as published by the Free Software Foundation; either
   19.11 +    version 2.1 of the License, or (at your option) any later version.
   19.12 +
   19.13 +    This library is distributed in the hope that it will be useful,
   19.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   19.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   19.16 +    Lesser General Public License for more details.
   19.17 +
   19.18 +    You should have received a copy of the GNU Lesser General Public
   19.19 +    License along with this library; if not, write to the Free Software
   19.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   19.21 +
   19.22 +    Sam Lantinga
   19.23 +    slouken@libsdl.org
   19.24 +*/
   19.25 +#include "SDL_config.h"
   19.26 +
   19.27 +#ifndef SDL_POWER_DISABLED
   19.28 +#ifdef SDL_POWER_NINTENDODS
   19.29 +
   19.30 +#include "SDL_power.h"
   19.31 +
   19.32 +SDL_bool
   19.33 +SDL_GetPowerInfo_NintendoDS(SDL_PowerState *state, int *seconds, int *percent)
   19.34 +{
   19.35 +    /* !!! FIXME: write me. */
   19.36 +
   19.37 +    *state = SDL_POWERSTATE_UNKNOWN;
   19.38 +    *percent = -1;
   19.39 +    *seconds = -1;
   19.40 +
   19.41 +    return SDL_TRUE;  /* always the definitive answer on Nintendo DS. */
   19.42 +}
   19.43 +
   19.44 +#endif /* SDL_POWER_NINTENDODS */
   19.45 +#endif /* SDL_POWER_DISABLED */
   19.46 +
   19.47 +/* vi: set ts=4 sw=4 expandtab: */
   19.48 +
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/src/power/os2/SDL_syspower.c	Sun Jun 07 06:06:35 2009 +0000
    20.3 @@ -0,0 +1,135 @@
    20.4 +/*
    20.5 +    SDL - Simple DirectMedia Layer
    20.6 +    Copyright (C) 1997-2009 Sam Lantinga
    20.7 +
    20.8 +    This library is free software; you can redistribute it and/or
    20.9 +    modify it under the terms of the GNU Lesser General Public
   20.10 +    License as published by the Free Software Foundation; either
   20.11 +    version 2.1 of the License, or (at your option) any later version.
   20.12 +
   20.13 +    This library is distributed in the hope that it will be useful,
   20.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   20.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   20.16 +    Lesser General Public License for more details.
   20.17 +
   20.18 +    You should have received a copy of the GNU Lesser General Public
   20.19 +    License along with this library; if not, write to the Free Software
   20.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   20.21 +
   20.22 +    Sam Lantinga
   20.23 +    slouken@libsdl.org
   20.24 +*/
   20.25 +#include "SDL_config.h"
   20.26 +
   20.27 +/* !!! FIXME:
   20.28 + * Please note that this code has not been tested (or even compiled!). It
   20.29 + *  should, in theory, run on any version of OS/2, and work with any system
   20.30 + *  that has APM.SYS loaded. I don't know if ACPI.SYS works.
   20.31 + */
   20.32 +
   20.33 +#ifndef SDL_POWER_DISABLED
   20.34 +#ifdef SDL_POWER_OS2
   20.35 +
   20.36 +#define INCL_DOSFILEMGR
   20.37 +#define INCL_DOSDEVICES
   20.38 +#define INCL_DOSDEVIOCTL
   20.39 +#define INCL_DOSERRORS
   20.40 +#include <os2.h>
   20.41 +
   20.42 +#include "SDL_power.h"
   20.43 +
   20.44 +typedef struct {
   20.45 +    USHORT len;
   20.46 +    USHORT flags;
   20.47 +    UCHAR ac_status;
   20.48 +    UCHAR battery_status;
   20.49 +    UCHAR battery_life;
   20.50 +    UCHAR battery_time_form;
   20.51 +    USHORT battery_time;
   20.52 +    UCHAR battery_flags;
   20.53 +} PowerStatus;
   20.54 +extern int CompilerAssertPowerStatus[(sizeof (PowerStatus) == 10) ? 1 : -1];
   20.55 +
   20.56 +
   20.57 +SDL_bool
   20.58 +SDL_GetPowerInfo_OS2(SDL_PowerState *state, int *seconds, int *percent)
   20.59 +{
   20.60 +    PowerStatus status;
   20.61 +    HFILE hfile = 0;
   20.62 +    ULONG action = 0;
   20.63 +    APIRET rc = 0;
   20.64 +
   20.65 +    *state = SDL_POWERSTATE_UNKNOWN;
   20.66 +    *percent = -1;
   20.67 +    *seconds = -1;
   20.68 +
   20.69 +    /* open the power management device */
   20.70 +    rc = DosOpen("APM$", &hfile, &action, 0, FILE_NORMAL, FILE_OPEN,
   20.71 +                 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0);
   20.72 +
   20.73 +    if (rc == NO_ERROR) {
   20.74 +        USHORT iorc = 0;
   20.75 +        ULONG iorclen = sizeof (iorc);
   20.76 +        ULONG statuslen = sizeof (status);
   20.77 +
   20.78 +        SDL_memset(&status, '\0', sizeof (status));
   20.79 +        status.len = sizeof (status);
   20.80 +
   20.81 +        rc = DosDevIOCtl(hfile, IOCTL_POWER, POWER_GETPOWERSTATUS, &status,
   20.82 +                         statuslen, &statuslen, &iorc, iorclen, &iorclen);
   20.83 +        DosClose(hfile);
   20.84 +
   20.85 +        /* (status.flags & 0x1) == power subsystem enabled. */
   20.86 +        if ((rc == NO_ERROR) && (status.flags & 0x1)) {
   20.87 +            if (statuslen == 7) {  /* older OS/2 APM driver? Less fields. */
   20.88 +                status.battery_time_form = 0xFF;
   20.89 +                status.battery_time = 0;
   20.90 +                if (status.battery_status == 0xFF) {
   20.91 +                    status.battery_flags = 0xFF;
   20.92 +                } else {
   20.93 +                    status.battery_flags = (1 << status.battery_status);
   20.94 +                }
   20.95 +            }
   20.96 +
   20.97 +            if (status.battery_flags == 0xFF) {  /* unknown state */
   20.98 +                *state = SDL_POWERSTATE_UNKNOWN;
   20.99 +            } else if (status.battery_flags & (1 << 7)) {  /* no battery */
  20.100 +                *state = SDL_POWERSTATE_NO_BATTERY;
  20.101 +            } else if (status.battery_flags & (1 << 3)) {  /* charging */
  20.102 +                *state = SDL_POWERSTATE_CHARGING;
  20.103 +                need_details = SDL_TRUE;
  20.104 +            } else if (status.ac_status == 1) {
  20.105 +                *state = SDL_POWERSTATE_CHARGED;  /* on AC, not charging. */
  20.106 +                need_details = SDL_TRUE;
  20.107 +            } else {
  20.108 +                *state = SDL_POWERSTATE_ON_BATTERY;  /* not on AC. */
  20.109 +                need_details = SDL_TRUE;
  20.110 +            }
  20.111 +
  20.112 +            if (need_details) {
  20.113 +                const int pct = (int) status.battery_life;
  20.114 +                const int secs = (int) status.battery_time;
  20.115 +
  20.116 +                if (pct != 0xFF) {  /* 255 == unknown */
  20.117 +                    *percent = (pct > 100) ? 100 : pct;
  20.118 +                }
  20.119 +
  20.120 +                if (status.battery_time_form == 0xFF) {  /* unknown */
  20.121 +                    *seconds = -1;
  20.122 +                } else if (status.battery_time_form == 1) {  /* minutes */
  20.123 +                    *seconds = secs * 60;
  20.124 +                } else {
  20.125 +                    *seconds = secs;
  20.126 +                }
  20.127 +            }
  20.128 +        }
  20.129 +    }
  20.130 +
  20.131 +    return SDL_TRUE;  /* always the definitive answer on OS/2. */
  20.132 +}
  20.133 +
  20.134 +#endif /* SDL_POWER_OS2 */
  20.135 +#endif /* SDL_POWER_DISABLED */
  20.136 +
  20.137 +/* vi: set ts=4 sw=4 expandtab: */
  20.138 +
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/src/power/windows/SDL_syspower.c	Sun Jun 07 06:06:35 2009 +0000
    21.3 @@ -0,0 +1,78 @@
    21.4 +/*
    21.5 +    SDL - Simple DirectMedia Layer
    21.6 +    Copyright (C) 1997-2009 Sam Lantinga
    21.7 +
    21.8 +    This library is free software; you can redistribute it and/or
    21.9 +    modify it under the terms of the GNU Lesser General Public
   21.10 +    License as published by the Free Software Foundation; either
   21.11 +    version 2.1 of the License, or (at your option) any later version.
   21.12 +
   21.13 +    This library is distributed in the hope that it will be useful,
   21.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
   21.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   21.16 +    Lesser General Public License for more details.
   21.17 +
   21.18 +    You should have received a copy of the GNU Lesser General Public
   21.19 +    License along with this library; if not, write to the Free Software
   21.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   21.21 +
   21.22 +    Sam Lantinga
   21.23 +    slouken@libsdl.org
   21.24 +*/
   21.25 +#include "SDL_config.h"
   21.26 +
   21.27 +#ifndef SDL_POWER_DISABLED
   21.28 +#ifdef SDL_POWER_WINDOWS
   21.29 +
   21.30 +#define WIN32_LEAN_AND_MEAN
   21.31 +#include <windows.h>
   21.32 +
   21.33 +#include "SDL_power.h"
   21.34 +
   21.35 +SDL_bool
   21.36 +SDL_GetPowerInfo_Windows(SDL_PowerState *state, int *seconds, int *percent)
   21.37 +{
   21.38 +    SYSTEM_POWER_STATUS status;
   21.39 +    SDL_bool need_details = SDL_FALSE;
   21.40 +
   21.41 +    /* This API should exist back to Win95 and Windows CE. */
   21.42 +    if (!GetSystemPowerStatus(&status)) {
   21.43 +        /* !!! FIXME: push GetLastError() into SDL_GetError() */
   21.44 +        *state = SDL_POWERSTATE_UNKNOWN;
   21.45 +    } else if (status.BatteryFlag == 0xFF) {  /* unknown state */
   21.46 +        *state = SDL_POWERSTATE_UNKNOWN;
   21.47 +    } else if (status.BatteryFlag & (1 << 7)) {  /* no battery */
   21.48 +        *state = SDL_POWERSTATE_NO_BATTERY;
   21.49 +    } else if (status.BatteryFlag & (1 << 3)) {  /* charging */
   21.50 +        *state = SDL_POWERSTATE_CHARGING;
   21.51 +        need_details = SDL_TRUE;
   21.52 +    } else if (status.ACLineStatus == 1) {
   21.53 +        *state = SDL_POWERSTATE_CHARGED;  /* on AC, not charging. */
   21.54 +        need_details = SDL_TRUE;
   21.55 +    } else {
   21.56 +        *state = SDL_POWERSTATE_ON_BATTERY;  /* not on AC. */
   21.57 +        need_details = SDL_TRUE;
   21.58 +    }
   21.59 +
   21.60 +    *percent = -1;
   21.61 +    *seconds = -1;
   21.62 +    if (need_details) {
   21.63 +        const int pct = (int) status.BatteryLifePercent;
   21.64 +        const int secs = (int) status.BatteryLifeTime;
   21.65 +
   21.66 +        if (pct != 255) {  /* 255 == unknown */
   21.67 +            *percent = (pct > 100) ? 100 : pct;  /* clamp between 0%, 100% */
   21.68 +        }
   21.69 +        if (secs != 0xFFFFFFFF) {  /* ((DWORD)-1) == unknown */
   21.70 +            *seconds = secs;
   21.71 +        }
   21.72 +    }
   21.73 +
   21.74 +    return SDL_TRUE;  /* always the definitive answer on Windows. */
   21.75 +}
   21.76 +
   21.77 +#endif /* SDL_POWER_WINDOWS */
   21.78 +#endif /* SDL_POWER_DISABLED */
   21.79 +
   21.80 +/* vi: set ts=4 sw=4 expandtab: */
   21.81 +
    22.1 --- a/test/Makefile.in	Sun Jun 07 02:45:55 2009 +0000
    22.2 +++ b/test/Makefile.in	Sun Jun 07 06:06:35 2009 +0000
    22.3 @@ -7,7 +7,7 @@
    22.4  CFLAGS  = @CFLAGS@
    22.5  LIBS	= @LIBS@
    22.6  
    22.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testresample$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testintersections$(EXE) testdraw2$(EXE) testdyngl$(EXE) testdyngles$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testgles$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE)
    22.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testresample$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testpower$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testintersections$(EXE) testdraw2$(EXE) testdyngl$(EXE) testdyngles$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testgles$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE)
    22.9  
   22.10  all: Makefile $(TARGETS)
   22.11  
   22.12 @@ -32,6 +32,9 @@
   22.13  testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c
   22.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
   22.15  
   22.16 +testpower$(EXE): $(srcdir)/testpower.c
   22.17 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
   22.18 +
   22.19  testalpha$(EXE): $(srcdir)/testalpha.c
   22.20  	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
   22.21  
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/test/testpower.c	Sun Jun 07 06:06:35 2009 +0000
    23.3 @@ -0,0 +1,65 @@
    23.4 +/* Simple test of power subsystem. */
    23.5 +
    23.6 +#include <stdio.h>
    23.7 +#include "SDL.h"
    23.8 +
    23.9 +static void report_power(void)
   23.10 +{
   23.11 +    int seconds, percent;
   23.12 +    const SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
   23.13 +    char *statestr = NULL;
   23.14 +
   23.15 +    printf("SDL-reported power info...\n");
   23.16 +    switch(state)
   23.17 +    {
   23.18 +        case SDL_POWERSTATE_UNKNOWN:
   23.19 +            statestr = "Unknown";
   23.20 +            break;
   23.21 +        case SDL_POWERSTATE_ON_BATTERY:
   23.22 +            statestr = "On battery";
   23.23 +            break;
   23.24 +        case SDL_POWERSTATE_NO_BATTERY:
   23.25 +            statestr = "No battery";
   23.26 +            break;
   23.27 +        case SDL_POWERSTATE_CHARGING:
   23.28 +            statestr = "Charging";
   23.29 +            break;
   23.30 +        case SDL_POWERSTATE_CHARGED:
   23.31 +            statestr = "Charged";
   23.32 +            break;
   23.33 +        default:
   23.34 +            statestr = "!!API ERROR!!";
   23.35 +            break;
   23.36 +    }
   23.37 +
   23.38 +    printf("State: %s\n", statestr);
   23.39 +
   23.40 +    if (percent == -1) {
   23.41 +        printf("Percent left: unknown\n");
   23.42 +    } else {
   23.43 +        printf("Percent left: %d%%\n", percent);
   23.44 +    }
   23.45 +
   23.46 +    if (seconds == -1) {
   23.47 +        printf("Time left: unknown\n");
   23.48 +    } else {
   23.49 +        printf("Time left: %d minutes, %d seconds\n", (int) (seconds / 60), (int) (seconds % 60));
   23.50 +    }
   23.51 +}
   23.52 +
   23.53 +
   23.54 +int main(int argc, char *argv[])
   23.55 +{
   23.56 +    if (SDL_Init(SDL_INIT_VIDEO) == -1) {
   23.57 +        fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
   23.58 +        return 1;
   23.59 +    }
   23.60 +
   23.61 +    report_power();
   23.62 +
   23.63 +    SDL_Quit();
   23.64 +    return 0;
   23.65 +}
   23.66 +
   23.67 +/* end of testpower.c ... */
   23.68 +