1 |
|
---|
2 | /*
|
---|
3 | *@@sourcefile apmh.c:
|
---|
4 | * contains helpers for accessing APM.SYS (Advanced Power Management).
|
---|
5 | *
|
---|
6 | * These functions have been moved to this file with V0.9.14
|
---|
7 | * and were previously used in XWorkplace code.
|
---|
8 | *
|
---|
9 | * Usage: All OS/2 programs.
|
---|
10 | *
|
---|
11 | * Function prefixes:
|
---|
12 | * -- apmh* APM helper functions
|
---|
13 | *
|
---|
14 | * Note: Version numbering in this file relates to XWorkplace version
|
---|
15 | * numbering.
|
---|
16 | *
|
---|
17 | *@@header "helpers\apmh.h"
|
---|
18 | *@@added V0.9.14 (2001-08-01) [umoeller]
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Copyright (C) 1998-2001 Ulrich Mller.
|
---|
23 | * This file is part of the "XWorkplace helpers" source package.
|
---|
24 | * This is free software; you can redistribute it and/or modify
|
---|
25 | * it under the terms of the GNU General Public License as published
|
---|
26 | * by the Free Software Foundation, in version 2 as it comes in the
|
---|
27 | * "COPYING" file of the XWorkplace main distribution.
|
---|
28 | * This program is distributed in the hope that it will be useful,
|
---|
29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
31 | * GNU General Public License for more details.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #define OS2EMX_PLAIN_CHAR
|
---|
35 | // this is needed for "os2emx.h"; if this is defined,
|
---|
36 | // emx will define PSZ as _signed_ char, otherwise
|
---|
37 | // as unsigned char
|
---|
38 |
|
---|
39 | #define INCL_DOSDEVICES
|
---|
40 | #define INCL_DOSDEVIOCTL
|
---|
41 | #define INCL_DOSERRORS
|
---|
42 | #include <os2.h>
|
---|
43 |
|
---|
44 | #include "setup.h" // code generation and debugging options
|
---|
45 |
|
---|
46 | #include "helpers\apmh.h"
|
---|
47 | #include "helpers\standards.h"
|
---|
48 |
|
---|
49 | /*
|
---|
50 | *@@category: Helpers\Control program helpers\Advanced Power Management
|
---|
51 | * See apmh.c.
|
---|
52 | */
|
---|
53 |
|
---|
54 | /* ******************************************************************
|
---|
55 | *
|
---|
56 | * APM monitor
|
---|
57 | *
|
---|
58 | ********************************************************************/
|
---|
59 |
|
---|
60 | /*
|
---|
61 | *@@ apmhIOCtl:
|
---|
62 | * shortcut for DosDevIOCtl on the APM.SYS driver.
|
---|
63 | */
|
---|
64 |
|
---|
65 | APIRET apmhIOCtl(HFILE hfAPMSys,
|
---|
66 | ULONG ulFunction,
|
---|
67 | PVOID pvParamPck,
|
---|
68 | ULONG cbParamPck)
|
---|
69 | {
|
---|
70 | APIRET arc;
|
---|
71 | APMGIO_DPKT DataPacket;
|
---|
72 | ULONG ulRetSize = sizeof(DataPacket);
|
---|
73 | DataPacket.ReturnCode = GIOERR_PowerNoError;
|
---|
74 | if (!(arc = DosDevIOCtl(hfAPMSys,
|
---|
75 | APMGIO_Category,
|
---|
76 | ulFunction,
|
---|
77 | pvParamPck, cbParamPck, &cbParamPck,
|
---|
78 | &DataPacket, sizeof(DataPacket), &ulRetSize)))
|
---|
79 | if (DataPacket.ReturnCode)
|
---|
80 | arc = DataPacket.ReturnCode + 10000;
|
---|
81 |
|
---|
82 | return arc;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | *@@ apmhOpen:
|
---|
87 | * opens the APM.SYS driver and creates
|
---|
88 | * an APM structure in *ppApm.
|
---|
89 | *
|
---|
90 | * In the APM structure, the version fields are
|
---|
91 | * set to the version flags returned from the
|
---|
92 | * BIOS and APM.SYS itself.
|
---|
93 | */
|
---|
94 |
|
---|
95 | APIRET apmhOpen(PAPM *ppApm)
|
---|
96 | {
|
---|
97 | // open APM.SYS
|
---|
98 | APIRET arc;
|
---|
99 | HFILE hfAPMSys = NULLHANDLE;
|
---|
100 | ULONG ulAction;
|
---|
101 | PAPM papm;
|
---|
102 |
|
---|
103 | if (!(arc = DosOpen("\\DEV\\APM$",
|
---|
104 | &hfAPMSys,
|
---|
105 | &ulAction,
|
---|
106 | 0,
|
---|
107 | FILE_NORMAL,
|
---|
108 | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
109 | OPEN_FLAGS_FAIL_ON_ERROR
|
---|
110 | | OPEN_SHARE_DENYNONE
|
---|
111 | | OPEN_ACCESS_READWRITE,
|
---|
112 | NULL)))
|
---|
113 | {
|
---|
114 | // query version of APM-BIOS and APM driver
|
---|
115 | GETPOWERINFO getpowerinfo;
|
---|
116 | memset(&getpowerinfo, 0, sizeof(getpowerinfo));
|
---|
117 | getpowerinfo.usParmLength = sizeof(getpowerinfo);
|
---|
118 |
|
---|
119 | if (!(arc = apmhIOCtl(hfAPMSys,
|
---|
120 | POWER_GETPOWERINFO,
|
---|
121 | &getpowerinfo,
|
---|
122 | getpowerinfo.usParmLength)))
|
---|
123 | {
|
---|
124 | // @@changed V1.0.9 (2012-02-20) [slevine]: reworked to use caller's buffer if passed
|
---|
125 | // Code from David Azarewicz
|
---|
126 | if (*ppApm==NULL)
|
---|
127 | {
|
---|
128 | if (!(papm = NEW(APM))) arc = ERROR_NOT_ENOUGH_MEMORY;
|
---|
129 | *ppApm = papm;
|
---|
130 | }
|
---|
131 | if (*ppApm)
|
---|
132 | {
|
---|
133 | ZERO(papm);
|
---|
134 |
|
---|
135 | papm->hfAPMSys = hfAPMSys;
|
---|
136 |
|
---|
137 | // swap lower-byte(major vers.) to higher-byte(minor vers.)
|
---|
138 | papm->usBIOSVersion = (getpowerinfo.usBIOSVersion & 0xff) << 8
|
---|
139 | | (getpowerinfo.usBIOSVersion >> 8);
|
---|
140 | papm->usDriverVersion = (getpowerinfo.usDriverVersion & 0xff) << 8
|
---|
141 | | (getpowerinfo.usDriverVersion >> 8);
|
---|
142 |
|
---|
143 | // set general APM version to the lower of the two
|
---|
144 | papm->usLowestAPMVersion = (papm->usBIOSVersion < papm->usDriverVersion)
|
---|
145 | ? papm->usBIOSVersion
|
---|
146 | : papm->usDriverVersion;
|
---|
147 |
|
---|
148 | *ppApm = papm;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | if ((arc) && (hfAPMSys))
|
---|
154 | DosClose(hfAPMSys);
|
---|
155 |
|
---|
156 | return arc;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*
|
---|
160 | *@@ apmhReadStatus:
|
---|
161 | * reads in the current battery status.
|
---|
162 | *
|
---|
163 | * After this, the status fields in APM
|
---|
164 | * are valid (if NO_ERROR is returned).
|
---|
165 | * If the values changed since the previous
|
---|
166 | * call, *pfChanged is set to TRUE; FALSE
|
---|
167 | * otherwise.
|
---|
168 | *
|
---|
169 | *@@changed V0.9.19 (2002-05-28) [umoeller]: added fUsingAC
|
---|
170 | */
|
---|
171 |
|
---|
172 | APIRET apmhReadStatus(PAPM pApm, // in: APM structure created by apmhOpen
|
---|
173 | PBOOL pfChanged) // out: values changed (ptr can be NULL)
|
---|
174 | {
|
---|
175 | APIRET arc = NO_ERROR;
|
---|
176 | BOOL fChanged = FALSE;
|
---|
177 |
|
---|
178 | if ((pApm) && (pApm->hfAPMSys))
|
---|
179 | {
|
---|
180 | APMGIO_QSTATUS_PPKT PowerStatus;
|
---|
181 | PowerStatus.ParmLength = sizeof(PowerStatus);
|
---|
182 |
|
---|
183 | if (!(arc = apmhIOCtl(pApm->hfAPMSys,
|
---|
184 | APMGIO_QueryStatus,
|
---|
185 | &PowerStatus,
|
---|
186 | PowerStatus.ParmLength)))
|
---|
187 | {
|
---|
188 | if ( (pApm->fAlreadyRead)
|
---|
189 | || (pApm->bBatteryStatus != PowerStatus.BatteryStatus)
|
---|
190 | || (pApm->bBatteryLife != PowerStatus.BatteryLife)
|
---|
191 | || (pApm->fUsingAC != PowerStatus.ACStatus)
|
---|
192 | )
|
---|
193 | {
|
---|
194 | pApm->bBatteryStatus = PowerStatus.BatteryStatus;
|
---|
195 | pApm->bBatteryLife = PowerStatus.BatteryLife;
|
---|
196 | pApm->fUsingAC = PowerStatus.ACStatus;
|
---|
197 |
|
---|
198 | pApm->fAlreadyRead = FALSE;
|
---|
199 | fChanged = TRUE;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (pfChanged)
|
---|
204 | *pfChanged = fChanged;
|
---|
205 | }
|
---|
206 | else
|
---|
207 | arc = ERROR_INVALID_PARAMETER;
|
---|
208 |
|
---|
209 | return arc;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*
|
---|
213 | *@@ apmhClose:
|
---|
214 | * closes the APM device driver and frees
|
---|
215 | * the APM structure. *ppApm is set to NULL.
|
---|
216 | */
|
---|
217 |
|
---|
218 | VOID apmhClose(PAPM *ppApm)
|
---|
219 | {
|
---|
220 | if (ppApm && *ppApm)
|
---|
221 | {
|
---|
222 | PAPM pApm = *ppApm;
|
---|
223 | if (pApm->hfAPMSys)
|
---|
224 | DosClose(pApm->hfAPMSys);
|
---|
225 | free(pApm);
|
---|
226 | *ppApm = NULL;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | /*
|
---|
231 | *@@ apmhHasBattery:
|
---|
232 | * quick'n'dirty helper which returns TRUE only
|
---|
233 | * if APM is supported on the system and the
|
---|
234 | * system actually has a battery (i.e. is a laptop).
|
---|
235 | *
|
---|
236 | *@@added V0.9.16 (2001-10-15) [umoeller]
|
---|
237 | */
|
---|
238 |
|
---|
239 | BOOL apmhHasBattery(VOID)
|
---|
240 | {
|
---|
241 | BOOL brc = FALSE;
|
---|
242 |
|
---|
243 | PAPM p = NULL;
|
---|
244 | if (!apmhOpen(&p))
|
---|
245 | {
|
---|
246 | if (!apmhReadStatus(p, NULL))
|
---|
247 | brc = (p->bBatteryStatus != 0xFF);
|
---|
248 |
|
---|
249 | apmhClose(&p);
|
---|
250 | }
|
---|
251 |
|
---|
252 | return brc;
|
---|
253 | }
|
---|