2 * Copyright (C) 2001 The XFree86 Project, Inc. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES
19 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Except as contained in this notice, the name of the XFree86 Project
24 * shall not be used in advertising or otherwise to promote the sale, use
25 * or other dealings in this Software without prior written authorization
26 * from the XFree86 Project.
29 #ifdef HAVE_XORG_CONFIG_H
30 #include <xorg-config.h>
38 #include "xf86_OSproc.h"
39 #include "xf86_OSlib.h"
41 #include <sys/event.h>
42 #include <machine/apmvar.h>
44 #define _PATH_APM_SOCKET "/var/run/apmdev"
45 #define _PATH_APM_DEV "/dev/apm"
46 #define _PATH_APM_CTLDEV "/dev/apmctl"
48 static pointer APMihPtr
= NULL
;
49 static int devFd
= -1;
50 static int ctlFd
= -1;
51 static void bsdCloseAPM(void);
56 } bsdToXF86Array
[] = {
57 {APM_STANDBY_REQ
, XF86_APM_SYS_STANDBY
},
58 {APM_SUSPEND_REQ
, XF86_APM_SYS_SUSPEND
},
59 {APM_NORMAL_RESUME
, XF86_APM_NORMAL_RESUME
},
60 {APM_CRIT_RESUME
, XF86_APM_CRITICAL_RESUME
},
61 {APM_BATTERY_LOW
, XF86_APM_LOW_BATTERY
},
62 {APM_POWER_CHANGE
, XF86_APM_POWER_STATUS_CHANGE
},
63 {APM_UPDATE_TIME
, XF86_APM_UPDATE_TIME
},
64 {APM_CRIT_SUSPEND_REQ
, XF86_APM_CRITICAL_SUSPEND
},
65 {APM_USER_STANDBY_REQ
, XF86_APM_USER_STANDBY
},
66 {APM_USER_SUSPEND_REQ
, XF86_APM_USER_SUSPEND
},
67 {APM_SYS_STANDBY_RESUME
, XF86_APM_STANDBY_RESUME
},
68 #ifdef APM_CAPABILITY_CHANGE
69 {APM_CAPABILITY_CHANGE
, XF86_APM_CAPABILITY_CHANGED
},
73 #define numApmEvents (sizeof(bsdToXF86Array) / sizeof(bsdToXF86Array[0]))
80 for (i
= 0; i
< numApmEvents
; i
++) {
81 if (type
== bsdToXF86Array
[i
].apmBsd
) {
82 return bsdToXF86Array
[i
].xf86
;
85 return XF86_APM_UNKNOWN
;
89 * APM events can be requested direclty from /dev/apm
92 bsdPMGetEventFromOS(int kq
, pmEvent
* events
, int num
)
96 struct timespec ts
= { 0, 0 };
98 for (i
= 0; i
< num
; i
++) {
99 result
= kevent(kq
, NULL
, 0, &ev
, 1, &ts
);
100 if (result
== 0 || APM_EVENT_TYPE(ev
.data
) == APM_NOEVENT
) {
104 else if (result
< 0) {
105 xf86Msg(X_WARNING
, "bsdPMGetEventFromOS: kevent returns"
106 " %s\n", strerror(errno
));
109 events
[i
] = bsdToXF86(APM_EVENT_TYPE(ev
.data
));
115 * If apmd(8) is running, he will get the events and handle them,
116 * so, we've nothing to do here.
117 * Otherwise, opening /dev/apmctl will succeed and we have to send the
118 * confirmations to /dev/apmctl.
121 bsdPMConfirmEventToOs(int dummyfd
, pmEvent event
)
124 if ((ctlFd
= open(_PATH_APM_CTLDEV
, O_RDWR
)) < 0) {
128 /* apmctl open succeedeed */
130 case XF86_APM_SYS_STANDBY
:
131 case XF86_APM_USER_STANDBY
:
132 if (ioctl(ctlFd
, APM_IOC_STANDBY
, NULL
) == 0)
133 return PM_WAIT
; /* should we stop the Xserver in standby, too? */
137 case XF86_APM_SYS_SUSPEND
:
138 case XF86_APM_CRITICAL_SUSPEND
:
139 case XF86_APM_USER_SUSPEND
:
140 if (ioctl(ctlFd
, APM_IOC_SUSPEND
, NULL
) == 0)
145 case XF86_APM_STANDBY_RESUME
:
146 case XF86_APM_NORMAL_RESUME
:
147 case XF86_APM_CRITICAL_RESUME
:
148 case XF86_APM_STANDBY_FAILED
:
149 case XF86_APM_SUSPEND_FAILED
:
163 if (APMihPtr
|| !xf86Info
.pmFlag
) {
166 if ((devFd
= open(_PATH_APM_DEV
, O_RDONLY
)) == -1) {
169 if ((kq
= kqueue()) <= 0) {
173 EV_SET(&ev
, devFd
, EVFILT_READ
, EV_ADD
| EV_ENABLE
| EV_CLEAR
, 0, 0, NULL
);
174 if (kevent(kq
, &ev
, 1, NULL
, 0, NULL
) < 0) {
179 xf86PMGetEventFromOs
= bsdPMGetEventFromOS
;
180 xf86PMConfirmEventToOs
= bsdPMConfirmEventToOs
;
181 APMihPtr
= xf86AddGeneralHandler(kq
, xf86HandlePMEvents
, NULL
);
191 kq
= xf86RemoveGeneralHandler(APMihPtr
);