1/* 2 * Copyright (c) 2000-2002 by The XFree86 Project, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Except as contained in this notice, the name of the copyright holder(s) 23 * and author(s) shall not be used in advertising or otherwise to promote 24 * the sale, use or other dealings in this Software without prior written 25 * authorization from the copyright holder(s) and author(s). 26 */ 27 28#ifdef HAVE_XORG_CONFIG_H 29#include <xorg-config.h> 30#endif 31 32#include <X11/X.h> 33#include "xf86.h" 34#include "xf86Priv.h" 35#include "xf86Xinput.h" 36#include "xf86_OSproc.h" 37 38int (*xf86PMGetEventFromOs)(int fd,pmEvent *events,int num) = NULL; 39pmWait (*xf86PMConfirmEventToOs)(int fd,pmEvent event) = NULL; 40 41static Bool suspended = FALSE; 42 43static int 44eventName(pmEvent event, char **str) 45{ 46 switch(event) { 47 case XF86_APM_SYS_STANDBY: *str="System Standby Request"; return 0; 48 case XF86_APM_SYS_SUSPEND: *str="System Suspend Request"; return 0; 49 case XF86_APM_CRITICAL_SUSPEND: *str="Critical Suspend"; return 0; 50 case XF86_APM_USER_STANDBY: *str="User System Standby Request"; return 0; 51 case XF86_APM_USER_SUSPEND: *str="User System Suspend Request"; return 0; 52 case XF86_APM_STANDBY_RESUME: *str="System Standby Resume"; return 0; 53 case XF86_APM_NORMAL_RESUME: *str="Normal Resume System"; return 0; 54 case XF86_APM_CRITICAL_RESUME: *str="Critical Resume System"; return 0; 55 case XF86_APM_LOW_BATTERY: *str="Battery Low"; return 3; 56 case XF86_APM_POWER_STATUS_CHANGE: *str="Power Status Change";return 3; 57 case XF86_APM_UPDATE_TIME: *str="Update Time";return 3; 58 case XF86_APM_CAPABILITY_CHANGED: *str="Capability Changed"; return 3; 59 case XF86_APM_STANDBY_FAILED: *str="Standby Request Failed"; return 0; 60 case XF86_APM_SUSPEND_FAILED: *str="Suspend Request Failed"; return 0; 61 default: *str="Unknown Event"; return 0; 62 } 63} 64 65static int sigio_blocked_for_suspend; 66 67static void 68suspend (pmEvent event, Bool undo) 69{ 70 int i; 71 InputInfoPtr pInfo; 72 73 for (i = 0; i < xf86NumScreens; i++) { 74 if (xf86Screens[i]->EnableDisableFBAccess) 75 (*xf86Screens[i]->EnableDisableFBAccess) (i, FALSE); 76 } 77 pInfo = xf86InputDevs; 78 while (pInfo) { 79 DisableDevice(pInfo->dev, TRUE); 80 pInfo = pInfo->next; 81 } 82 sigio_blocked_for_suspend = xf86BlockSIGIO(); 83 for (i = 0; i < xf86NumScreens; i++) { 84 if (xf86Screens[i]->PMEvent) 85 xf86Screens[i]->PMEvent(i,event,undo); 86 else { 87 xf86Screens[i]->LeaveVT(i, 0); 88 xf86Screens[i]->vtSema = FALSE; 89 } 90 } 91 xf86AccessLeave(); 92 93} 94 95static void 96resume(pmEvent event, Bool undo) 97{ 98 int i; 99 InputInfoPtr pInfo; 100 101 xf86AccessEnter(); 102 for (i = 0; i < xf86NumScreens; i++) { 103 if (xf86Screens[i]->PMEvent) 104 xf86Screens[i]->PMEvent(i,event,undo); 105 else { 106 xf86Screens[i]->vtSema = TRUE; 107 xf86Screens[i]->EnterVT(i, 0); 108 } 109 } 110 xf86UnblockSIGIO(sigio_blocked_for_suspend); 111 for (i = 0; i < xf86NumScreens; i++) { 112 if (xf86Screens[i]->EnableDisableFBAccess) 113 (*xf86Screens[i]->EnableDisableFBAccess) (i, TRUE); 114 } 115 dixSaveScreens(serverClient, SCREEN_SAVER_FORCER, ScreenSaverReset); 116 pInfo = xf86InputDevs; 117 while (pInfo) { 118 EnableDevice(pInfo->dev, TRUE); 119 pInfo = pInfo->next; 120 } 121} 122 123static void 124DoApmEvent(pmEvent event, Bool undo) 125{ 126 int i, was_blocked; 127 128 switch(event) { 129#if 0 130 case XF86_APM_SYS_STANDBY: 131 case XF86_APM_USER_STANDBY: 132#endif 133 case XF86_APM_SYS_SUSPEND: 134 case XF86_APM_CRITICAL_SUSPEND: /*do we want to delay a critical suspend?*/ 135 case XF86_APM_USER_SUSPEND: 136 /* should we do this ? */ 137 if (!undo && !suspended) { 138 suspend(event,undo); 139 suspended = TRUE; 140 } else if (undo && suspended) { 141 resume(event,undo); 142 suspended = FALSE; 143 } 144 break; 145#if 0 146 case XF86_APM_STANDBY_RESUME: 147#endif 148 case XF86_APM_NORMAL_RESUME: 149 case XF86_APM_CRITICAL_RESUME: 150 if (suspended) { 151 resume(event,undo); 152 suspended = FALSE; 153 } 154 break; 155 default: 156 was_blocked = xf86BlockSIGIO(); 157 for (i = 0; i < xf86NumScreens; i++) { 158 if (xf86Screens[i]->PMEvent) { 159 xf86Screens[i]->PMEvent(i,event,undo); 160 } 161 } 162 xf86UnblockSIGIO(was_blocked); 163 break; 164 } 165} 166 167#define MAX_NO_EVENTS 8 168 169void 170xf86HandlePMEvents(int fd, pointer data) 171{ 172 pmEvent events[MAX_NO_EVENTS]; 173 int i,n; 174 Bool wait = FALSE; 175 176 if (!xf86PMGetEventFromOs) 177 return; 178 179 if ((n = xf86PMGetEventFromOs(fd,events,MAX_NO_EVENTS))) { 180 do { 181 for (i = 0; i < n; i++) { 182 char *str = NULL; 183 int verb = eventName(events[i],&str); 184 185 xf86MsgVerb(X_INFO,verb,"PM Event received: %s\n",str); 186 DoApmEvent(events[i],FALSE); 187 switch (xf86PMConfirmEventToOs(fd,events[i])) { 188 case PM_WAIT: 189 wait = TRUE; 190 break; 191 case PM_CONTINUE: 192 wait = FALSE; 193 break; 194 case PM_FAILED: 195 DoApmEvent(events[i],TRUE); 196 wait = FALSE; 197 break; 198 default: 199 break; 200 } 201 } 202 if (wait) 203 n = xf86PMGetEventFromOs(fd,events,MAX_NO_EVENTS); 204 else 205 break; 206 } while (1); 207 } 208} 209