source: trunk/src/helpers/apmh.c@ 108

Last change on this file since 108 was 105, checked in by umoeller, 24 years ago

Misc changes.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
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 M”ller.
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
65APIRET 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
95APIRET apmhOpen(PAPM *ppApm)
96{
97 // open APM.SYS
98 APIRET arc;
99 HFILE hfAPMSys = NULLHANDLE;
100 ULONG ulAction;
101
102 if (!(arc = DosOpen("\\DEV\\APM$",
103 &hfAPMSys,
104 &ulAction,
105 0,
106 FILE_NORMAL,
107 OPEN_ACTION_OPEN_IF_EXISTS,
108 OPEN_FLAGS_FAIL_ON_ERROR
109 | OPEN_SHARE_DENYNONE
110 | OPEN_ACCESS_READWRITE,
111 NULL)))
112 {
113 // query version of APM-BIOS and APM driver
114 GETPOWERINFO getpowerinfo;
115 memset(&getpowerinfo, 0, sizeof(getpowerinfo));
116 getpowerinfo.usParmLength = sizeof(getpowerinfo);
117
118 if (!(arc = apmhIOCtl(hfAPMSys,
119 POWER_GETPOWERINFO,
120 &getpowerinfo,
121 getpowerinfo.usParmLength)))
122 {
123 PAPM papm;
124 if (!(papm = NEW(APM)))
125 arc = ERROR_NOT_ENOUGH_MEMORY;
126 else
127 {
128 ZERO(papm);
129
130 papm->hfAPMSys = hfAPMSys;
131
132 // swap lower-byte(major vers.) to higher-byte(minor vers.)
133 papm->usBIOSVersion = (getpowerinfo.usBIOSVersion & 0xff) << 8
134 | (getpowerinfo.usBIOSVersion >> 8);
135 papm->usDriverVersion = (getpowerinfo.usDriverVersion & 0xff) << 8
136 | (getpowerinfo.usDriverVersion >> 8);
137
138 // set general APM version to the lower of the two
139 papm->usLowestAPMVersion = (papm->usBIOSVersion < papm->usDriverVersion)
140 ? papm->usBIOSVersion
141 : papm->usDriverVersion;
142
143 *ppApm = papm;
144 }
145 }
146 }
147
148 if ((arc) && (hfAPMSys))
149 DosClose(hfAPMSys);
150
151 return (arc);
152}
153
154/*
155 *@@ apmhReadStatus:
156 * reads in the current battery status.
157 *
158 * After this, the status fields in APM
159 * are valid (if NO_ERROR is returned).
160 * If the values changed since the previous
161 * call, *pfChanged is set to TRUE; FALSE
162 * otherwise.
163 */
164
165APIRET apmhReadStatus(PAPM pApm, // in: APM structure created by apmhOpen
166 PBOOL pfChanged) // out: values changed (ptr can be NULL)
167{
168 APIRET arc = NO_ERROR;
169 BOOL fChanged = FALSE;
170
171 if ((pApm) && (pApm->hfAPMSys))
172 {
173 APMGIO_QSTATUS_PPKT PowerStatus;
174 PowerStatus.ParmLength = sizeof(PowerStatus);
175
176 if (!(arc = apmhIOCtl(pApm->hfAPMSys,
177 APMGIO_QueryStatus,
178 &PowerStatus,
179 PowerStatus.ParmLength)))
180 {
181 if ( (pApm->fAlreadyRead)
182 || (pApm->ulBatteryStatus != PowerStatus.BatteryStatus)
183 || (pApm->ulBatteryLife != PowerStatus.BatteryLife)
184 )
185 {
186 pApm->ulBatteryStatus = PowerStatus.BatteryStatus;
187 pApm->ulBatteryLife = PowerStatus.BatteryLife;
188
189 pApm->fAlreadyRead = FALSE;
190 fChanged = TRUE;
191 }
192 }
193
194 if (pfChanged)
195 *pfChanged = fChanged;
196 }
197 else
198 arc = ERROR_INVALID_PARAMETER;
199
200 return (arc);
201}
202
203/*
204 *@@ apmhClose:
205 * closes the APM device driver and frees
206 * the APM structure. *ppApm is set to NULL.
207 */
208
209VOID apmhClose(PAPM *ppApm)
210{
211 if (ppApm && *ppApm)
212 {
213 PAPM pApm = *ppApm;
214 if (pApm->hfAPMSys)
215 DosClose(pApm->hfAPMSys);
216 free(pApm);
217 *ppApm = NULL;
218 }
219}
220
221
Note: See TracBrowser for help on using the repository browser.