Commit | Line | Data |
---|---|---|
a09e091a JB |
1 | /* Based on hw/xfree86/os-support/bsd/bsd_apm.c which bore no explicit |
2 | * copyright notice, so is covered by the following notice: | |
3 | * | |
4 | * Copyright (C) 1994-2003 The XFree86 Project, Inc. All Rights Reserved. | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a | |
7 | * copy of this software and associated documentation files (the "Software"), | |
8 | * to deal in the Software without restriction, including without limitation | |
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
10 | * and/or sell copies of the Software, and to permit persons to whom the | |
11 | * Software is furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
20 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF | |
21 | * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | * | |
24 | * Except as contained in this notice, the name of the XFree86 Project shall | |
25 | * not be used in advertising or otherwise to promote the sale, use or other | |
26 | * dealings in this Software without prior written authorization from the | |
27 | * XFree86 Project. | |
28 | */ | |
29 | ||
30 | /* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. | |
31 | * | |
32 | * Permission is hereby granted, free of charge, to any person obtaining a | |
33 | * copy of this software and associated documentation files (the "Software"), | |
34 | * to deal in the Software without restriction, including without limitation | |
35 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
36 | * and/or sell copies of the Software, and to permit persons to whom the | |
37 | * Software is furnished to do so, subject to the following conditions: | |
38 | * | |
39 | * The above copyright notice and this permission notice (including the next | |
40 | * paragraph) shall be included in all copies or substantial portions of the | |
41 | * Software. | |
42 | * | |
43 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
44 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
45 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
46 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
47 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
48 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
49 | * DEALINGS IN THE SOFTWARE. | |
50 | */ | |
51 | ||
52 | #ifdef HAVE_XORG_CONFIG_H | |
53 | #include <xorg-config.h> | |
54 | #endif | |
55 | ||
56 | #include <X11/X.h> | |
57 | #include "os.h" | |
58 | #include "xf86.h" | |
59 | #include "xf86Priv.h" | |
60 | #define XF86_OS_PRIVS | |
61 | #include "xf86_OSproc.h" | |
62 | #include "xf86_OSlib.h" | |
63 | ||
64 | #ifndef PLEASE_FIX_THIS | |
65 | #define APM_STANDBY_REQ 0xa01 | |
66 | #define APM_SUSPEND_REQ 0xa02 | |
67 | #define APM_NORMAL_RESUME 0xa03 | |
68 | #define APM_CRIT_RESUME 0xa04 | |
69 | #define APM_BATTERY_LOW 0xa05 | |
70 | #define APM_POWER_CHANGE 0xa06 | |
71 | #define APM_UPDATE_TIME 0xa07 | |
72 | #define APM_CRIT_SUSPEND_REQ 0xa08 | |
73 | #define APM_USER_STANDBY_REQ 0xa09 | |
74 | #define APM_USER_SUSPEND_REQ 0xa0a | |
75 | #define APM_SYS_STANDBY_RESUME 0xa0b | |
76 | #define APM_IOC_NEXTEVENT 0xa0c | |
77 | #define APM_IOC_RESUME 0xa0d | |
78 | #define APM_IOC_SUSPEND 0xa0e | |
79 | #define APM_IOC_STANDBY 0xa0f | |
80 | #endif | |
81 | ||
82 | typedef struct apm_event_info { | |
83 | int type; | |
84 | } apm_event_info; | |
85 | ||
86 | /* | |
87 | This may be replaced with a better device name | |
88 | very soon... | |
89 | */ | |
90 | #define APM_DEVICE "/dev/srn" | |
91 | #define APM_DEVICE1 "/dev/apm" | |
92 | ||
93 | static pointer APMihPtr = NULL; | |
94 | static void sunCloseAPM(void); | |
95 | ||
96 | static struct { | |
97 | u_int apmBsd; | |
98 | pmEvent xf86; | |
99 | } sunToXF86Array[] = { | |
100 | {APM_STANDBY_REQ, XF86_APM_SYS_STANDBY}, | |
101 | {APM_SUSPEND_REQ, XF86_APM_SYS_SUSPEND}, | |
102 | {APM_NORMAL_RESUME, XF86_APM_NORMAL_RESUME}, | |
103 | {APM_CRIT_RESUME, XF86_APM_CRITICAL_RESUME}, | |
104 | {APM_BATTERY_LOW, XF86_APM_LOW_BATTERY}, | |
105 | {APM_POWER_CHANGE, XF86_APM_POWER_STATUS_CHANGE}, | |
106 | {APM_UPDATE_TIME, XF86_APM_UPDATE_TIME}, | |
107 | {APM_CRIT_SUSPEND_REQ, XF86_APM_CRITICAL_SUSPEND}, | |
108 | {APM_USER_STANDBY_REQ, XF86_APM_USER_STANDBY}, | |
109 | {APM_USER_SUSPEND_REQ, XF86_APM_USER_SUSPEND}, | |
110 | {APM_SYS_STANDBY_RESUME, XF86_APM_STANDBY_RESUME}, | |
111 | #ifdef APM_CAPABILITY_CHANGE | |
112 | {APM_CAPABILITY_CHANGE, XF86_APM_CAPABILITY_CHANGED}, | |
113 | #endif | |
114 | }; | |
115 | ||
116 | #define numApmEvents (sizeof(sunToXF86Array) / sizeof(sunToXF86Array[0])) | |
117 | ||
118 | static pmEvent | |
119 | sunToXF86(int type) | |
120 | { | |
121 | int i; | |
122 | ||
123 | for (i = 0; i < numApmEvents; i++) { | |
124 | if (type == sunToXF86Array[i].apmBsd) { | |
125 | return sunToXF86Array[i].xf86; | |
126 | } | |
127 | } | |
128 | return XF86_APM_UNKNOWN; | |
129 | } | |
130 | ||
131 | /* | |
132 | * APM events can be requested direclty from /dev/apm | |
133 | */ | |
134 | static int | |
135 | sunPMGetEventFromOS(int fd, pmEvent * events, int num) | |
136 | { | |
137 | struct apm_event_info sunEvent; | |
138 | int i; | |
139 | ||
140 | for (i = 0; i < num; i++) { | |
141 | ||
142 | if (ioctl(fd, APM_IOC_NEXTEVENT, &sunEvent) < 0) { | |
143 | if (errno != EAGAIN) { | |
144 | xf86Msg(X_WARNING, "sunPMGetEventFromOS: APM_IOC_NEXTEVENT" | |
145 | " %s\n", strerror(errno)); | |
146 | } | |
147 | break; | |
148 | } | |
149 | events[i] = sunToXF86(sunEvent.type); | |
150 | } | |
151 | xf86Msg(X_WARNING, "Got some events\n"); | |
152 | return i; | |
153 | } | |
154 | ||
155 | static pmWait | |
156 | sunPMConfirmEventToOs(int fd, pmEvent event) | |
157 | { | |
158 | switch (event) { | |
159 | /* XXX: NOT CURRENTLY RETURNED FROM OS */ | |
160 | case XF86_APM_SYS_STANDBY: | |
161 | case XF86_APM_USER_STANDBY: | |
162 | if (ioctl(fd, APM_IOC_STANDBY, NULL) == 0) | |
163 | return PM_WAIT; /* should we stop the Xserver in standby, too? */ | |
164 | else | |
165 | return PM_NONE; | |
166 | case XF86_APM_SYS_SUSPEND: | |
167 | case XF86_APM_CRITICAL_SUSPEND: | |
168 | case XF86_APM_USER_SUSPEND: | |
169 | xf86Msg(X_WARNING, "Got SUSPENDED\n"); | |
170 | if (ioctl(fd, APM_IOC_SUSPEND, NULL) == 0) | |
171 | return PM_CONTINUE; | |
172 | else { | |
173 | xf86Msg(X_WARNING, "sunPMConfirmEventToOs: APM_IOC_SUSPEND" | |
174 | " %s\n", strerror(errno)); | |
175 | return PM_FAILED; | |
176 | } | |
177 | case XF86_APM_STANDBY_RESUME: | |
178 | case XF86_APM_NORMAL_RESUME: | |
179 | case XF86_APM_CRITICAL_RESUME: | |
180 | case XF86_APM_STANDBY_FAILED: | |
181 | case XF86_APM_SUSPEND_FAILED: | |
182 | xf86Msg(X_WARNING, "Got RESUME\n"); | |
183 | if (ioctl(fd, APM_IOC_RESUME, NULL) == 0) | |
184 | return PM_CONTINUE; | |
185 | else { | |
186 | xf86Msg(X_WARNING, "sunPMConfirmEventToOs: APM_IOC_RESUME" | |
187 | " %s\n", strerror(errno)); | |
188 | return PM_FAILED; | |
189 | } | |
190 | default: | |
191 | return PM_NONE; | |
192 | } | |
193 | } | |
194 | ||
195 | PMClose | |
196 | xf86OSPMOpen(void) | |
197 | { | |
198 | int fd; | |
199 | ||
200 | if (APMihPtr || !xf86Info.pmFlag) { | |
201 | return NULL; | |
202 | } | |
203 | ||
204 | if ((fd = open(APM_DEVICE, O_RDWR)) == -1) { | |
205 | if ((fd = open(APM_DEVICE1, O_RDWR)) == -1) { | |
206 | return NULL; | |
207 | } | |
208 | } | |
209 | xf86PMGetEventFromOs = sunPMGetEventFromOS; | |
210 | xf86PMConfirmEventToOs = sunPMConfirmEventToOs; | |
211 | APMihPtr = xf86AddGeneralHandler(fd, xf86HandlePMEvents, NULL); | |
212 | return sunCloseAPM; | |
213 | } | |
214 | ||
215 | static void | |
216 | sunCloseAPM(void) | |
217 | { | |
218 | int fd; | |
219 | ||
220 | if (APMihPtr) { | |
221 | fd = xf86RemoveGeneralHandler(APMihPtr); | |
222 | close(fd); | |
223 | APMihPtr = NULL; | |
224 | } | |
225 | } |