| 1 | /* |
| 2 | * Copyright (C) 2001 The XFree86 Project, Inc. All Rights Reserved. |
| 3 | * |
| 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: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 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. |
| 22 | * |
| 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. |
| 27 | */ |
| 28 | |
| 29 | #ifdef HAVE_XORG_CONFIG_H |
| 30 | #include <xorg-config.h> |
| 31 | #endif |
| 32 | |
| 33 | #include <X11/X.h> |
| 34 | #include "os.h" |
| 35 | #include "xf86.h" |
| 36 | #include "xf86Priv.h" |
| 37 | #define XF86_OS_PRIVS |
| 38 | #include "xf86_OSproc.h" |
| 39 | #include "xf86_OSlib.h" |
| 40 | |
| 41 | #include <sys/event.h> |
| 42 | #include <machine/apmvar.h> |
| 43 | |
| 44 | #define _PATH_APM_SOCKET "/var/run/apmdev" |
| 45 | #define _PATH_APM_DEV "/dev/apm" |
| 46 | #define _PATH_APM_CTLDEV "/dev/apmctl" |
| 47 | |
| 48 | static pointer APMihPtr = NULL; |
| 49 | static int devFd = -1; |
| 50 | static int ctlFd = -1; |
| 51 | static void bsdCloseAPM(void); |
| 52 | |
| 53 | static struct { |
| 54 | u_int apmBsd; |
| 55 | pmEvent xf86; |
| 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}, |
| 70 | #endif |
| 71 | }; |
| 72 | |
| 73 | #define numApmEvents (sizeof(bsdToXF86Array) / sizeof(bsdToXF86Array[0])) |
| 74 | |
| 75 | static pmEvent |
| 76 | bsdToXF86(int type) |
| 77 | { |
| 78 | int i; |
| 79 | |
| 80 | for (i = 0; i < numApmEvents; i++) { |
| 81 | if (type == bsdToXF86Array[i].apmBsd) { |
| 82 | return bsdToXF86Array[i].xf86; |
| 83 | } |
| 84 | } |
| 85 | return XF86_APM_UNKNOWN; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * APM events can be requested direclty from /dev/apm |
| 90 | */ |
| 91 | static int |
| 92 | bsdPMGetEventFromOS(int kq, pmEvent * events, int num) |
| 93 | { |
| 94 | struct kevent ev; |
| 95 | int i, result; |
| 96 | struct timespec ts = { 0, 0 }; |
| 97 | |
| 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) { |
| 101 | /* no event */ |
| 102 | break; |
| 103 | } |
| 104 | else if (result < 0) { |
| 105 | xf86Msg(X_WARNING, "bsdPMGetEventFromOS: kevent returns" |
| 106 | " %s\n", strerror(errno)); |
| 107 | break; |
| 108 | } |
| 109 | events[i] = bsdToXF86(APM_EVENT_TYPE(ev.data)); |
| 110 | } |
| 111 | return i; |
| 112 | } |
| 113 | |
| 114 | /* |
| 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. |
| 119 | */ |
| 120 | static pmWait |
| 121 | bsdPMConfirmEventToOs(int dummyfd, pmEvent event) |
| 122 | { |
| 123 | if (ctlFd < 0) { |
| 124 | if ((ctlFd = open(_PATH_APM_CTLDEV, O_RDWR)) < 0) { |
| 125 | return PM_NONE; |
| 126 | } |
| 127 | } |
| 128 | /* apmctl open succeedeed */ |
| 129 | switch (event) { |
| 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? */ |
| 134 | else |
| 135 | return PM_NONE; |
| 136 | |
| 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) |
| 141 | return PM_WAIT; |
| 142 | else |
| 143 | return PM_NONE; |
| 144 | break; |
| 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: |
| 150 | return PM_CONTINUE; |
| 151 | break; |
| 152 | default: |
| 153 | return PM_NONE; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | PMClose |
| 158 | xf86OSPMOpen(void) |
| 159 | { |
| 160 | int kq; |
| 161 | struct kevent ev; |
| 162 | |
| 163 | if (APMihPtr || !xf86Info.pmFlag) { |
| 164 | return NULL; |
| 165 | } |
| 166 | if ((devFd = open(_PATH_APM_DEV, O_RDONLY)) == -1) { |
| 167 | return NULL; |
| 168 | } |
| 169 | if ((kq = kqueue()) <= 0) { |
| 170 | close(devFd); |
| 171 | return NULL; |
| 172 | } |
| 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) { |
| 175 | close(devFd); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | xf86PMGetEventFromOs = bsdPMGetEventFromOS; |
| 180 | xf86PMConfirmEventToOs = bsdPMConfirmEventToOs; |
| 181 | APMihPtr = xf86AddGeneralHandler(kq, xf86HandlePMEvents, NULL); |
| 182 | return bsdCloseAPM; |
| 183 | } |
| 184 | |
| 185 | static void |
| 186 | bsdCloseAPM(void) |
| 187 | { |
| 188 | int kq; |
| 189 | |
| 190 | if (APMihPtr) { |
| 191 | kq = xf86RemoveGeneralHandler(APMihPtr); |
| 192 | close(devFd); |
| 193 | devFd = -1; |
| 194 | close(kq); |
| 195 | if (ctlFd >= 0) { |
| 196 | close(ctlFd); |
| 197 | ctlFd = -1; |
| 198 | } |
| 199 | APMihPtr = NULL; |
| 200 | } |
| 201 | } |