Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Initial work on power subsystem for SDL 1.3.

  • Loading branch information
icculus committed Jun 7, 2009
1 parent 275d309 commit 7a45400695202fda9ad2731dcf682ed9980f7411
@@ -31,6 +31,7 @@ SRCS = \
src/events/SDL_quit.c \
src/events/SDL_resize.c \
src/file/SDL_rwops.c \
src/power/SDL_power.c \
src/joystick/dc/SDL_sysjoystick.c \
src/joystick/SDL_joystick.c \
src/loadso/dummy/SDL_sysloadso.c \
@@ -55,6 +55,8 @@ src/events/SDL_mouse.c \
src/events/SDL_quit.c \
src/events/SDL_windowevents.c \
src/file/SDL_rwops.c \
src/power/SDL_power.c \
src/power/nds/SDL_syspower.c \
src/haptic/SDL_haptic.c \
src/haptic/nds/SDL_syshaptic.c \
src/joystick/nds/SDL_sysjoystick.c \
@@ -42,7 +42,7 @@ SDLMAIN_OBJECTS = @SDLMAIN_OBJECTS@

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

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
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

LT_AGE = @LT_AGE@
LT_CURRENT = @LT_CURRENT@
@@ -20,6 +20,7 @@ SOURCES = \
src/thread/*.c \
src/timer/*.c \
src/video/*.c \
src/power/*.c \
src/audio/dummy/*.c \
src/video/dummy/*.c \
src/joystick/dummy/*.c \
@@ -14,7 +14,7 @@ TARGET = libSDL.a

SOURCES = ./src/*.c ./src/audio/*.c ./src/cdrom/*.c ./src/cpuinfo/*.c ./src/events/*.c \
./src/file/*.c ./src/stdlib/*.c ./src/thread/*.c ./src/timer/*.c ./src/video/*.c \
./src/joystick/*.c ./src/haptic/*.c ./src/video/dummy/*.c ./src/audio/disk/*.c \
./src/joystick/*.c ./src/haptic/*.c ./src/power/*.c ./src/video/dummy/*.c ./src/audio/disk/*.c \
./src/audio/dummy/*.c ./src/loadso/dlopen/*.c ./src/audio/dsp/*.c ./src/audio/dma/*.c \
./src/thread/pthread/SDL_systhread.c ./src/thread/pthread/SDL_syssem.c \
./src/thread/pthread/SDL_sysmutex.c ./src/thread/pthread/SDL_syscond.c \
@@ -250,6 +250,14 @@ if test x$enable_haptic != xyes; then
else
SOURCES="$SOURCES $srcdir/src/haptic/*.c"
fi
AC_ARG_ENABLE(power,
AC_HELP_STRING([--enable-power], [Enable the power subsystem [[default=yes]]]),
, enable_power=yes)
if test x$enable_power != xyes; then
AC_DEFINE(SDL_POWER_DISABLED)
else
SOURCES="$SOURCES $srcdir/src/power/*.c"
fi
AC_ARG_ENABLE(cdrom,
AC_HELP_STRING([--enable-cdrom], [Enable the cdrom subsystem [[default=yes]]]),
, enable_cdrom=yes)
@@ -2446,6 +2454,16 @@ case "$host" in
esac
fi
fi
# Set up files for the power library
if test x$enable_power = xyes; then
case $ARCH in
linux)
AC_DEFINE(SDL_POWER_LINUX)
SOURCES="$SOURCES $srcdir/src/power/linux/*.c"
have_power=yes
;;
esac
fi
# Set up files for the cdrom library
if test x$enable_cdrom = xyes; then
case $ARCH in
@@ -2620,6 +2638,12 @@ AC_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
SOURCES="$SOURCES $srcdir/src/loadso/win32/*.c"
have_loadso=yes
fi
# Set up files for the system power library
if test x$enable_power = xyes; then
AC_DEFINE(SDL_POWER_WINDOWS)
SOURCES="$SOURCES $srcdir/src/power/windows/*.c"
have_power=yes
fi
# Set up the system libraries we need
EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lcoredll -lcommctrl -lmmtimer"
# The Win32 platform requires special setup
@@ -2691,6 +2715,11 @@ AC_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
have_haptic=yes
fi
fi
if test x$enable_power = xyes; then
AC_DEFINE(SDL_POWER_WINDOWS)
SOURCES="$SOURCES $srcdir/src/power/windows/SDL_syspower.c"
have_power=yes
fi
# Set up files for the cdrom library
if test x$enable_cdrom = xyes; then
AC_DEFINE(SDL_CDROM_WIN32)
@@ -2842,6 +2871,12 @@ AC_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
have_haptic=yes
EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-framework,ForceFeedback"
fi
# Set up files for the power library
if test x$enable_power = xyes; then
AC_DEFINE(SDL_POWER_MACOSX)
SOURCES="$SOURCES $srcdir/src/power/macosx/*.c"
have_power=yes
fi
# Set up files for the cdrom library
if test x$enable_cdrom = xyes; then
AC_DEFINE(SDL_CDROM_MACOSX)
@@ -84,6 +84,7 @@ Enjoy!
#include "SDL_events.h"
#include "SDL_loadso.h"
#include "SDL_mutex.h"
#include "SDL_power.h"
#include "SDL_rwops.h"
#include "SDL_thread.h"
#include "SDL_timer.h"
@@ -166,6 +166,7 @@
#undef SDL_THREADS_DISABLED
#undef SDL_TIMERS_DISABLED
#undef SDL_VIDEO_DISABLED
#undef SDL_POWER_DISABLED

/* Enable various audio drivers */
#undef SDL_AUDIO_DRIVER_ALSA
@@ -324,6 +325,14 @@
#undef SDL_VIDEO_OPENGL_OSMESA
#undef SDL_VIDEO_OPENGL_OSMESA_DYNAMIC

/* Enable system power support */
#undef SDL_POWER_LINUX
#undef SDL_POWER_WINDOWS
#undef SDL_POWER_MACOSX
#undef SDL_POWER_OS2
#undef SDL_POWER_NINTENDODS
#undef SDL_POWER_HARDWIRED

/* Enable assembly routines */
#undef SDL_ASSEMBLY_ROUTINES
#undef SDL_ALTIVEC_BLITTERS
@@ -104,4 +104,6 @@ typedef unsigned long uintptr_t;
#define SDL_VIDEO_DRIVER_DC 1
#define SDL_VIDEO_DRIVER_DUMMY 1

#define SDL_POWER_HARDWIRED 1

#endif /* _SDL_config_dreamcast_h */
@@ -131,6 +131,9 @@ typedef unsigned long uintptr_t;
#define SDL_VIDEO_OPENGL_ES 1
#define SDL_VIDEO_RENDER_OGL_ES 1

/* Enable system power support */
#define SDL_POWER_MACOSX 1

/* enable iPhone keyboard support */
#define SDL_IPHONE_KEYBOARD 1

@@ -137,6 +137,9 @@
#define SDL_VIDEO_OPENGL_CGL 1
#define SDL_VIDEO_RENDER_OGL 1

/* Enable system power support */
#define SDL_POWER_MACOSX 1

/* Enable assembly routines */
#define SDL_ASSEMBLY_ROUTINES 1
#ifdef __ppc__
@@ -116,4 +116,7 @@ typedef unsigned __PTRDIFF_TYPE__ uintptr_t;
#define SDL_VIDEO_DRIVER_NDS 1
/*#define SDL_VIDEO_DRIVER_DUMMY 1 TODO: uncomment this later*/

/* Enable system power support */
#define SDL_POWER_NINTENDODS 1

#endif /* _SDL_config_nintendods_h */
@@ -136,6 +136,9 @@ typedef unsigned long long uint64_t;
/* Enable OpenGL support */
/* Nothing here yet for OS/2... :( */

/* Enable system power support */
#define SDL_POWER_OS2 1

/* Enable assembly routines where available */
#define SDL_ASSEMBLY_ROUTINES 1

@@ -185,6 +185,9 @@ typedef unsigned int uintptr_t;
#define SDL_VIDEO_RENDER_OGL 1
#endif

/* Enable system power support */
#define SDL_POWER_WINDOWS 1

/* Enable assembly routines (Win64 doesn't have inline asm) */
#ifndef _WIN64
#define SDL_ASSEMBLY_ROUTINES 1
@@ -0,0 +1,84 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2009 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/

#ifndef _SDL_power_h
#define _SDL_power_h

/**
* \file SDL_power.h
*
* Header for the SDL power management routines
*/

#include "SDL_stdinc.h"

#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C" {
/* *INDENT-ON* */
#endif

/**
* \enum SDL_PowerState
*
* \brief The basic state for the system's power supply.
*/
typedef enum
{
SDL_POWERSTATE_UNKNOWN, /**< cannot determine power status */
SDL_POWERSTATE_ON_BATTERY, /**< Not plugged in, running on the battery */
SDL_POWERSTATE_NO_BATTERY, /**< Plugged in, no battery available */
SDL_POWERSTATE_CHARGING, /**< Plugged in, charging battery */
SDL_POWERSTATE_CHARGED, /**< Plugged in, battery charged */
} SDL_PowerState;


/**
* \fn int SDL_GetPowerInfo(void)
*
* \brief Get the current power supply details.
*
* \param secs Seconds of battery life left. You can pass a NULL here if
* you don't care. Will return -1 if we can't determine a
* value, or we're not running on a battery.
*
* \param pct Percentage of battery life left, between 0 and 100. You can
* pass a NULL here if you don't care. Will return -1 if we
* can't determine a value, or we're not running on a battery.
*
* \return The state of the battery (if any).
*/
extern DECLSPEC SDL_PowerState SDLCALL SDL_GetPowerInfo(int *secs, int *pct);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif
#include "close_code.h"

#endif /* _SDL_power_h */

/* vi: set ts=4 sw=4 expandtab: */
@@ -0,0 +1,110 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2009 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_power.h"

/*
* Returns SDL_TRUE if we have a definitive answer.
* SDL_FALSE to try next implementation.
*/
typedef SDL_bool
(*SDL_GetPowerInfo_Impl)(SDL_PowerState *state, int *seconds, int *percent);

SDL_bool SDL_GetPowerInfo_Linux_sys_power(SDL_PowerState*, int*, int*);
SDL_bool SDL_GetPowerInfo_Linux_proc_apci(SDL_PowerState*, int*, int*);
SDL_bool SDL_GetPowerInfo_Linux_proc_apm(SDL_PowerState*, int*, int*);
SDL_bool SDL_GetPowerInfo_Windows(SDL_PowerState*, int*, int*);
SDL_bool SDL_GetPowerInfo_MacOSX(SDL_PowerState*, int*, int*);
SDL_bool SDL_GetPowerInfo_OS2(SDL_PowerState*, int*, int*);
SDL_bool SDL_GetPowerInfo_NintendoDS(SDL_PowerState*, int*, int*);

#ifndef SDL_POWER_DISABLED
#ifdef SDL_POWER_HARDWIRED
/* This is for things that _never_ have a battery, like the Dreamcast, etc. */
static SDL_bool
SDL_GetPowerInfo_Hardwired(SDL_PowerState *state, int *seconds, int *percent)
{
*seconds = -1;
*percent = -1;
*state = SDL_POWERSTATE_NO_BATTERY;
return SDL_TRUE;
}
#endif
#endif


static SDL_GetPowerInfo_Impl implementations[] = {
#ifndef SDL_POWER_DISABLED
#ifdef SDL_POWER_LINUX /* in order of preference. More than could work. */
SDL_GetPowerInfo_Linux_sys_power,
SDL_GetPowerInfo_Linux_proc_apci,
SDL_GetPowerInfo_Linux_proc_apm,
#endif
#ifdef SDL_POWER_WINDOWS /* handles Win32, Win64, PocketPC. */
SDL_GetPowerInfo_Windows,
#endif
#ifdef SDL_POWER_MACOSX /* handles Mac OS X, Darwin, iPhone. */
SDL_GetPowerInfo_MacOSX,
#endif
#ifdef SDL_POWER_OS2 /* handles OS/2, Warp, eComStation. */
SDL_GetPowerInfo_OS2,
#endif
#ifdef SDL_POWER_NINTENDODS /* handles Nintendo DS. */
SDL_GetPowerInfo_NintendoDS,
#endif
#ifdef SDL_POWER_HARDWIRED
SDL_GetPowerInfo_Hardwired,
#endif
#endif
};

SDL_PowerState
SDL_GetPowerInfo(int *seconds, int *percent)
{
const int total = sizeof (implementations) / sizeof (implementations[0]);
int _seconds, _percent;
SDL_PowerState retval;
int i;

/* Make these never NULL for platform-specific implementations. */
if (seconds == NULL) {
seconds = &_seconds;
}

if (percent == NULL) {
percent = &_percent;
}

for (i = 0; i < total; i++) {
if (implementations[i](&retval, seconds, percent)) {
return retval;
}
}

/* nothing was definitive. */
*seconds = -1;
*percent = -1;
return SDL_POWERSTATE_UNKNOWN;
}

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 7a45400

Please sign in to comment.