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