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 void *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 static pmEvent
sunToXF86(int type)117 sunToXF86(int type)
118 {
119 int i;
120
121 for (i = 0; i < ARRAY_SIZE(sunToXF86Array); i++) {
122 if (type == sunToXF86Array[i].apmBsd) {
123 return sunToXF86Array[i].xf86;
124 }
125 }
126 return XF86_APM_UNKNOWN;
127 }
128
129 /*
130 * APM events can be requested direclty from /dev/apm
131 */
132 static int
sunPMGetEventFromOS(int fd,pmEvent * events,int num)133 sunPMGetEventFromOS(int fd, pmEvent * events, int num)
134 {
135 struct apm_event_info sunEvent;
136 int i;
137
138 for (i = 0; i < num; i++) {
139
140 if (ioctl(fd, APM_IOC_NEXTEVENT, &sunEvent) < 0) {
141 if (errno != EAGAIN) {
142 xf86Msg(X_WARNING, "sunPMGetEventFromOS: APM_IOC_NEXTEVENT"
143 " %s\n", strerror(errno));
144 }
145 break;
146 }
147 events[i] = sunToXF86(sunEvent.type);
148 }
149 xf86Msg(X_WARNING, "Got some events\n");
150 return i;
151 }
152
153 static pmWait
sunPMConfirmEventToOs(int fd,pmEvent event)154 sunPMConfirmEventToOs(int fd, pmEvent event)
155 {
156 switch (event) {
157 /* XXX: NOT CURRENTLY RETURNED FROM OS */
158 case XF86_APM_SYS_STANDBY:
159 case XF86_APM_USER_STANDBY:
160 if (ioctl(fd, APM_IOC_STANDBY, NULL) == 0)
161 return PM_WAIT; /* should we stop the Xserver in standby, too? */
162 else
163 return PM_NONE;
164 case XF86_APM_SYS_SUSPEND:
165 case XF86_APM_CRITICAL_SUSPEND:
166 case XF86_APM_USER_SUSPEND:
167 xf86Msg(X_WARNING, "Got SUSPENDED\n");
168 if (ioctl(fd, APM_IOC_SUSPEND, NULL) == 0)
169 return PM_CONTINUE;
170 else {
171 xf86Msg(X_WARNING, "sunPMConfirmEventToOs: APM_IOC_SUSPEND"
172 " %s\n", strerror(errno));
173 return PM_FAILED;
174 }
175 case XF86_APM_STANDBY_RESUME:
176 case XF86_APM_NORMAL_RESUME:
177 case XF86_APM_CRITICAL_RESUME:
178 case XF86_APM_STANDBY_FAILED:
179 case XF86_APM_SUSPEND_FAILED:
180 xf86Msg(X_WARNING, "Got RESUME\n");
181 if (ioctl(fd, APM_IOC_RESUME, NULL) == 0)
182 return PM_CONTINUE;
183 else {
184 xf86Msg(X_WARNING, "sunPMConfirmEventToOs: APM_IOC_RESUME"
185 " %s\n", strerror(errno));
186 return PM_FAILED;
187 }
188 default:
189 return PM_NONE;
190 }
191 }
192
193 PMClose
xf86OSPMOpen(void)194 xf86OSPMOpen(void)
195 {
196 int fd;
197
198 if (APMihPtr || !xf86Info.pmFlag) {
199 return NULL;
200 }
201
202 if ((fd = open(APM_DEVICE, O_RDWR)) == -1) {
203 if ((fd = open(APM_DEVICE1, O_RDWR)) == -1) {
204 return NULL;
205 }
206 }
207 xf86PMGetEventFromOs = sunPMGetEventFromOS;
208 xf86PMConfirmEventToOs = sunPMConfirmEventToOs;
209 APMihPtr = xf86AddGeneralHandler(fd, xf86HandlePMEvents, NULL);
210 return sunCloseAPM;
211 }
212
213 static void
sunCloseAPM(void)214 sunCloseAPM(void)
215 {
216 int fd;
217
218 if (APMihPtr) {
219 fd = xf86RemoveGeneralHandler(APMihPtr);
220 close(fd);
221 APMihPtr = NULL;
222 }
223 }
224