Mercurial > sdl-ios-xcode
comparison src/power/os2/SDL_syspower.c @ 3170:b7a48f533966
Initial work on power subsystem for SDL 1.3.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Sun, 07 Jun 2009 06:06:35 +0000 |
parents | |
children | 51750b7a966f |
comparison
equal
deleted
inserted
replaced
3169:f294338ca6eb | 3170:b7a48f533966 |
---|---|
1 /* | |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997-2009 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Lesser General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2.1 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Lesser General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Lesser General Public | |
16 License along with this library; if not, write to the Free Software | |
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 #include "SDL_config.h" | |
23 | |
24 /* !!! FIXME: | |
25 * Please note that this code has not been tested (or even compiled!). It | |
26 * should, in theory, run on any version of OS/2, and work with any system | |
27 * that has APM.SYS loaded. I don't know if ACPI.SYS works. | |
28 */ | |
29 | |
30 #ifndef SDL_POWER_DISABLED | |
31 #ifdef SDL_POWER_OS2 | |
32 | |
33 #define INCL_DOSFILEMGR | |
34 #define INCL_DOSDEVICES | |
35 #define INCL_DOSDEVIOCTL | |
36 #define INCL_DOSERRORS | |
37 #include <os2.h> | |
38 | |
39 #include "SDL_power.h" | |
40 | |
41 typedef struct { | |
42 USHORT len; | |
43 USHORT flags; | |
44 UCHAR ac_status; | |
45 UCHAR battery_status; | |
46 UCHAR battery_life; | |
47 UCHAR battery_time_form; | |
48 USHORT battery_time; | |
49 UCHAR battery_flags; | |
50 } PowerStatus; | |
51 extern int CompilerAssertPowerStatus[(sizeof (PowerStatus) == 10) ? 1 : -1]; | |
52 | |
53 | |
54 SDL_bool | |
55 SDL_GetPowerInfo_OS2(SDL_PowerState *state, int *seconds, int *percent) | |
56 { | |
57 PowerStatus status; | |
58 HFILE hfile = 0; | |
59 ULONG action = 0; | |
60 APIRET rc = 0; | |
61 | |
62 *state = SDL_POWERSTATE_UNKNOWN; | |
63 *percent = -1; | |
64 *seconds = -1; | |
65 | |
66 /* open the power management device */ | |
67 rc = DosOpen("APM$", &hfile, &action, 0, FILE_NORMAL, FILE_OPEN, | |
68 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0); | |
69 | |
70 if (rc == NO_ERROR) { | |
71 USHORT iorc = 0; | |
72 ULONG iorclen = sizeof (iorc); | |
73 ULONG statuslen = sizeof (status); | |
74 | |
75 SDL_memset(&status, '\0', sizeof (status)); | |
76 status.len = sizeof (status); | |
77 | |
78 rc = DosDevIOCtl(hfile, IOCTL_POWER, POWER_GETPOWERSTATUS, &status, | |
79 statuslen, &statuslen, &iorc, iorclen, &iorclen); | |
80 DosClose(hfile); | |
81 | |
82 /* (status.flags & 0x1) == power subsystem enabled. */ | |
83 if ((rc == NO_ERROR) && (status.flags & 0x1)) { | |
84 if (statuslen == 7) { /* older OS/2 APM driver? Less fields. */ | |
85 status.battery_time_form = 0xFF; | |
86 status.battery_time = 0; | |
87 if (status.battery_status == 0xFF) { | |
88 status.battery_flags = 0xFF; | |
89 } else { | |
90 status.battery_flags = (1 << status.battery_status); | |
91 } | |
92 } | |
93 | |
94 if (status.battery_flags == 0xFF) { /* unknown state */ | |
95 *state = SDL_POWERSTATE_UNKNOWN; | |
96 } else if (status.battery_flags & (1 << 7)) { /* no battery */ | |
97 *state = SDL_POWERSTATE_NO_BATTERY; | |
98 } else if (status.battery_flags & (1 << 3)) { /* charging */ | |
99 *state = SDL_POWERSTATE_CHARGING; | |
100 need_details = SDL_TRUE; | |
101 } else if (status.ac_status == 1) { | |
102 *state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */ | |
103 need_details = SDL_TRUE; | |
104 } else { | |
105 *state = SDL_POWERSTATE_ON_BATTERY; /* not on AC. */ | |
106 need_details = SDL_TRUE; | |
107 } | |
108 | |
109 if (need_details) { | |
110 const int pct = (int) status.battery_life; | |
111 const int secs = (int) status.battery_time; | |
112 | |
113 if (pct != 0xFF) { /* 255 == unknown */ | |
114 *percent = (pct > 100) ? 100 : pct; | |
115 } | |
116 | |
117 if (status.battery_time_form == 0xFF) { /* unknown */ | |
118 *seconds = -1; | |
119 } else if (status.battery_time_form == 1) { /* minutes */ | |
120 *seconds = secs * 60; | |
121 } else { | |
122 *seconds = secs; | |
123 } | |
124 } | |
125 } | |
126 } | |
127 | |
128 return SDL_TRUE; /* always the definitive answer on OS/2. */ | |
129 } | |
130 | |
131 #endif /* SDL_POWER_OS2 */ | |
132 #endif /* SDL_POWER_DISABLED */ | |
133 | |
134 /* vi: set ts=4 sw=4 expandtab: */ | |
135 |