2 Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "SDL_config.h"
23 #ifndef SDL_POWER_DISABLED
31 #include <drivers/Drivers.h>
33 /* These values are from apm.h ... */
34 #define APM_DEVICE_PATH "/dev/misc/apm"
35 #define APM_FUNC_OFFSET 0x5300
36 #define APM_FUNC_GET_POWER_STATUS 10
37 #define APM_DEVICE_ALL 1
38 #define APM_BIOS_CALL (B_DEVICE_OP_CODES_END + 3)
40 #include "SDL_power.h"
43 SDL_GetPowerInfo_BeOS(SDL_PowerState * state, int *seconds, int *percent)
45 const int fd = open("/dev/misc/apm", O_RDONLY);
46 SDL_bool need_details = SDL_FALSE;
56 return SDL_FALSE; /* maybe some other method will work? */
59 memset(regs, '\0', sizeof(regs));
60 regs[0] = APM_FUNC_OFFSET + APM_FUNC_GET_POWER_STATUS;
61 regs[1] = APM_DEVICE_ALL;
62 rc = ioctl(fd, APM_BIOS_CALL, regs);
69 ac_status = regs[1] >> 8;
70 battery_status = regs[1] & 0xFF;
71 battery_flags = regs[2] >> 8;
72 battery_life = regs[2] & 0xFF;
73 battery_time = (uint32) regs[3];
75 /* in theory, _something_ should be set in battery_flags, right? */
76 if (battery_flags == 0x00) { /* older APM BIOS? Less fields. */
77 battery_time = 0xFFFF;
78 if (battery_status == 0xFF) {
81 battery_flags = (1 << battery_status);
85 if ((battery_time != 0xFFFF) && (battery_time & (1 << 15))) {
86 /* time is in minutes, not seconds */
87 battery_time = (battery_time & 0x7FFF) * 60;
90 if (battery_flags == 0xFF) { /* unknown state */
91 *state = SDL_POWERSTATE_UNKNOWN;
92 } else if (battery_flags & (1 << 7)) { /* no battery */
93 *state = SDL_POWERSTATE_NO_BATTERY;
94 } else if (battery_flags & (1 << 3)) { /* charging */
95 *state = SDL_POWERSTATE_CHARGING;
96 need_details = SDL_TRUE;
97 } else if (ac_status == 1) {
98 *state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */
99 need_details = SDL_TRUE;
101 *state = SDL_POWERSTATE_ON_BATTERY; /* not on AC. */
102 need_details = SDL_TRUE;
108 const int pct = (int) battery_life;
109 const int secs = (int) battery_time;
111 if (pct != 255) { /* 255 == unknown */
112 *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
114 if (secs != 0xFFFF) { /* 0xFFFF == unknown */
119 return SDL_TRUE; /* the definitive answer if APM driver replied. */
122 #endif /* SDL_POWER_BEOS */
123 #endif /* SDL_POWER_DISABLED */
125 /* vi: set ts=4 sw=4 expandtab: */