"Fossies" - the Fresh Open Source Software Archive

Member "xorg-server-1.20.5/hw/xfree86/os-support/bsd/bsd_kqueue_apm.c" (30 May 2019, 5662 Bytes) of package /linux/misc/xorg-server-1.20.5.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "bsd_kqueue_apm.c" see the Fossies "Dox" file reference documentation.

    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 void *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 static pmEvent
   74 bsdToXF86(int type)
   75 {
   76     int i;
   77 
   78     for (i = 0; i < ARRAY_SIZE(bsdToXF86Array); i++) {
   79         if (type == bsdToXF86Array[i].apmBsd) {
   80             return bsdToXF86Array[i].xf86;
   81         }
   82     }
   83     return XF86_APM_UNKNOWN;
   84 }
   85 
   86 /*
   87  * APM events can be requested direclty from /dev/apm
   88  */
   89 static int
   90 bsdPMGetEventFromOS(int kq, pmEvent * events, int num)
   91 {
   92     struct kevent ev;
   93     int i, result;
   94     struct timespec ts = { 0, 0 };
   95 
   96     for (i = 0; i < num; i++) {
   97         result = kevent(kq, NULL, 0, &ev, 1, &ts);
   98         if (result == 0 || APM_EVENT_TYPE(ev.data) == APM_NOEVENT) {
   99             /* no event */
  100             break;
  101         }
  102         else if (result < 0) {
  103             xf86Msg(X_WARNING, "bsdPMGetEventFromOS: kevent returns"
  104                     " %s\n", strerror(errno));
  105             break;
  106         }
  107         events[i] = bsdToXF86(APM_EVENT_TYPE(ev.data));
  108     }
  109     return i;
  110 }
  111 
  112 /*
  113  * If apmd(8) is running, he will get the events and handle them,
  114  * so, we've nothing to do here.
  115  * Otherwise, opening /dev/apmctl will succeed and we have to send the
  116  * confirmations to /dev/apmctl.
  117  */
  118 static pmWait
  119 bsdPMConfirmEventToOs(int dummyfd, pmEvent event)
  120 {
  121     if (ctlFd < 0) {
  122         if ((ctlFd = open(_PATH_APM_CTLDEV, O_RDWR)) < 0) {
  123             return PM_NONE;
  124         }
  125     }
  126     /* apmctl open succeedeed */
  127     switch (event) {
  128     case XF86_APM_SYS_STANDBY:
  129     case XF86_APM_USER_STANDBY:
  130         if (ioctl(ctlFd, APM_IOC_STANDBY, NULL) == 0)
  131             return PM_WAIT;     /* should we stop the Xserver in standby, too? */
  132         else
  133             return PM_NONE;
  134 
  135     case XF86_APM_SYS_SUSPEND:
  136     case XF86_APM_CRITICAL_SUSPEND:
  137     case XF86_APM_USER_SUSPEND:
  138         if (ioctl(ctlFd, APM_IOC_SUSPEND, NULL) == 0)
  139             return PM_WAIT;
  140         else
  141             return PM_NONE;
  142         break;
  143     case XF86_APM_STANDBY_RESUME:
  144     case XF86_APM_NORMAL_RESUME:
  145     case XF86_APM_CRITICAL_RESUME:
  146     case XF86_APM_STANDBY_FAILED:
  147     case XF86_APM_SUSPEND_FAILED:
  148         return PM_CONTINUE;
  149         break;
  150     default:
  151         return PM_NONE;
  152     }
  153 }
  154 
  155 PMClose
  156 xf86OSPMOpen(void)
  157 {
  158     int kq;
  159     struct kevent ev;
  160 
  161     if (APMihPtr || !xf86Info.pmFlag) {
  162         return NULL;
  163     }
  164     if ((devFd = open(_PATH_APM_DEV, O_RDONLY)) == -1) {
  165         return NULL;
  166     }
  167     if ((kq = kqueue()) <= 0) {
  168         close(devFd);
  169         return NULL;
  170     }
  171     EV_SET(&ev, devFd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, NULL);
  172     if (kevent(kq, &ev, 1, NULL, 0, NULL) < 0) {
  173         close(devFd);
  174         return NULL;
  175     }
  176 
  177     xf86PMGetEventFromOs = bsdPMGetEventFromOS;
  178     xf86PMConfirmEventToOs = bsdPMConfirmEventToOs;
  179     APMihPtr = xf86AddGeneralHandler(kq, xf86HandlePMEvents, NULL);
  180     return bsdCloseAPM;
  181 }
  182 
  183 static void
  184 bsdCloseAPM(void)
  185 {
  186     int kq;
  187 
  188     if (APMihPtr) {
  189         kq = xf86RemoveGeneralHandler(APMihPtr);
  190         close(devFd);
  191         devFd = -1;
  192         close(kq);
  193         if (ctlFd >= 0) {
  194             close(ctlFd);
  195             ctlFd = -1;
  196         }
  197         APMihPtr = NULL;
  198     }
  199 }