This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_syspower.c
135 lines (110 loc) · 4.35 KB
/
SDL_syspower.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
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"
/* !!! FIXME:
* Please note that this code has not been tested (or even compiled!). It
* should, in theory, run on any version of OS/2, and work with any system
* that has APM.SYS loaded. I don't know if ACPI.SYS works.
*/
#ifndef SDL_POWER_DISABLED
#ifdef SDL_POWER_OS2
#define INCL_DOSFILEMGR
#define INCL_DOSDEVICES
#define INCL_DOSDEVIOCTL
#define INCL_DOSERRORS
#include <os2.h>
#include "SDL_power.h"
typedef struct {
USHORT len;
USHORT flags;
UCHAR ac_status;
UCHAR battery_status;
UCHAR battery_life;
UCHAR battery_time_form;
USHORT battery_time;
UCHAR battery_flags;
} PowerStatus;
extern int CompilerAssertPowerStatus[(sizeof (PowerStatus) == 10) ? 1 : -1];
SDL_bool
SDL_GetPowerInfo_OS2(SDL_PowerState *state, int *seconds, int *percent)
{
PowerStatus status;
HFILE hfile = 0;
ULONG action = 0;
APIRET rc = 0;
*state = SDL_POWERSTATE_UNKNOWN;
*percent = -1;
*seconds = -1;
/* open the power management device */
rc = DosOpen("APM$", &hfile, &action, 0, FILE_NORMAL, FILE_OPEN,
OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0);
if (rc == NO_ERROR) {
USHORT iorc = 0;
ULONG iorclen = sizeof (iorc);
ULONG statuslen = sizeof (status);
SDL_memset(&status, '\0', sizeof (status));
status.len = sizeof (status);
rc = DosDevIOCtl(hfile, IOCTL_POWER, POWER_GETPOWERSTATUS, &status,
statuslen, &statuslen, &iorc, iorclen, &iorclen);
DosClose(hfile);
/* (status.flags & 0x1) == power subsystem enabled. */
if ((rc == NO_ERROR) && (status.flags & 0x1)) {
if (statuslen == 7) { /* older OS/2 APM driver? Less fields. */
status.battery_time_form = 0xFF;
status.battery_time = 0;
if (status.battery_status == 0xFF) {
status.battery_flags = 0xFF;
} else {
status.battery_flags = (1 << status.battery_status);
}
}
if (status.battery_flags == 0xFF) { /* unknown state */
*state = SDL_POWERSTATE_UNKNOWN;
} else if (status.battery_flags & (1 << 7)) { /* no battery */
*state = SDL_POWERSTATE_NO_BATTERY;
} else if (status.battery_flags & (1 << 3)) { /* charging */
*state = SDL_POWERSTATE_CHARGING;
need_details = SDL_TRUE;
} else if (status.ac_status == 1) {
*state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */
need_details = SDL_TRUE;
} else {
*state = SDL_POWERSTATE_ON_BATTERY; /* not on AC. */
need_details = SDL_TRUE;
}
if (need_details) {
const int pct = (int) status.battery_life;
const int secs = (int) status.battery_time;
if (pct != 0xFF) { /* 255 == unknown */
*percent = (pct > 100) ? 100 : pct;
}
if (status.battery_time_form == 0xFF) { /* unknown */
*seconds = -1;
} else if (status.battery_time_form == 1) { /* minutes */
*seconds = secs * 60;
} else {
*seconds = secs;
}
}
}
}
return SDL_TRUE; /* always the definitive answer on OS/2. */
}
#endif /* SDL_POWER_OS2 */
#endif /* SDL_POWER_DISABLED */
/* vi: set ts=4 sw=4 expandtab: */