xserver/Xext/dpms/dpms.c
Enrico Weigelt, metux IT consult 4f8895a34a
Some checks failed
Build X servers / ubuntu-fetch-pkg (push) Has been cancelled
Build X servers / xserver-build-ubuntu-no-gbm (push) Has been cancelled
Build X servers / xserver-build-ubuntu (push) Has been cancelled
Build X servers / abi-changes-check (push) Has been cancelled
Build X servers / xserver-build-ubuntu-debug (push) Has been cancelled
Build X servers / xserver-build-ubuntu-valgrind (push) Has been cancelled
Build X servers / ubuntu-deps-image (push) Has been cancelled
Build X servers / build-sdk-image (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-elographics:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-evdev:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-joystick:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-keyboard:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-libinput:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-mouse:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-synaptics:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-vmmouse:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-void:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (input-wacom:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-amdgpu:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-apm:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-ark:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-ast:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-ati:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-chips:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-cirrus:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-dummy:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-fbdev:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-freedreno:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-geode:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-i128:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-i740:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-intel:25.0.1) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-mach64:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-mga:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-neomagic:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-nested:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-nouveau:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-nv:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-omap:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-qxl:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-r128:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-rendition:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-s3virge:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-savage:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-siliconmotion:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-sis:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-sisusb:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-suncg14:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-suncg3:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-suncg6:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-sunffb:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-sunleo:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-suntcx:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-tdfx:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-trident:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-v4l:25.0.1) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-vbox:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-vesa:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-vmware:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-voodoo:25.0.0) (push) Has been cancelled
Build X servers / drivers-build-ubuntu (video-xgi:25.0.0) (push) Has been cancelled
Build X servers / xserver-build-mingw32-ubuntu (push) Has been cancelled
Build X servers / xserver-build-macos (push) Has been cancelled
Build X servers / xserver-build-freebsd (push) Has been cancelled
Build X servers / xserver-build-dragonflybsd (push) Has been cancelled
Build X servers / xserver-build-netbsd (push) Has been cancelled
Build X servers / xserver-build-solaris (push) Has been cancelled
Build X servers / xserver-build-openbsd (push) Has been cancelled
Build X servers / xserver-build-hurd (push) Has been cancelled
Build X servers / xserver-build-alpine (push) Has been cancelled
Build X servers / gentoo-deps-image (push) Has been cancelled
Build X servers / xserver-build-gentoo (push) Has been cancelled
Build X servers / xserver-build-rhel (10) (push) Has been cancelled
Build X servers / xserver-build-rhel (9) (push) Has been cancelled
Build X servers / xserver-build-cygwin (push) Has been cancelled
Build X servers / xserver-build-arch (push) Has been cancelled
Build X servers / Check Signed-Off-By (push) Has been cancelled
Build X servers / Release pushed tag (push) Has been cancelled
check PRs for merge conflicts / notify (push) Has been cancelled
xext: dpms: make DPMSSupported() static and let it return bool
Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
2026-07-24 11:58:10 +02:00

452 lines
12 KiB
C

/*****************************************************************
Copyright (c) 1996 Digital Equipment Corporation, Maynard, Massachusetts.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Digital Equipment Corporation
shall not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from Digital
Equipment Corporation.
******************************************************************/
#include <dix-config.h>
#include <stdbool.h>
#include <X11/X.h>
#include <X11/Xproto.h>
#include <X11/extensions/dpmsproto.h>
#include "dix/client_priv.h"
#include "dix/dix_priv.h"
#include "dix/request_priv.h"
#include "dix/screenint_priv.h"
#include "dix/screensaver_priv.h"
#include "include/misc.h"
#include "miext/extinit_priv.h"
#include "os/screensaver.h"
#include "os/osdep.h"
#include "Xext/dpms/dpms_priv.h"
#include "Xext/geext/geext_priv.h"
#include "os.h"
#include "dixstruct.h"
#include "extnsionst.h"
#include "opaque.h"
#include "scrnintstr.h"
#include "windowstr.h"
#include "protocol-versions.h"
Bool noDPMSExtension = FALSE;
CARD16 DPMSPowerLevel = 0;
bool DPMSDisabledSwitch = FALSE;
CARD32 DPMSStandbyTime = -1;
CARD32 DPMSSuspendTime = -1;
CARD32 DPMSOffTime = -1;
bool DPMSEnabled;
static int DPMSReqCode = 0;
static struct xorg_list dpms_listeners = { 0 };
typedef struct _DPMSEvent *DPMSEventPtr;
typedef struct _DPMSEvent {
struct xorg_list entry;
ClientPtr client;
unsigned int mask;
} DPMSEventRec;
static void DPMSDeleteClient(ClientPtr pClient)
{
DPMSEventRec *walk, *tmp;
xorg_list_for_each_entry_safe(walk, tmp, &dpms_listeners, entry) {
if (walk->client == pClient) {
xorg_list_del(&walk->entry);
free(walk);
}
}
}
static void DPMSClientDestroyCallback(CallbackListPtr *pcbl, void *unused,
void *calldata) {
DPMSDeleteClient((ClientPtr)calldata);
}
static void
SDPMSInfoNotifyEvent(xGenericEvent * from,
xGenericEvent * to)
{
*to = *from;
swaps(&to->sequenceNumber);
swapl(&to->length);
swaps(&to->evtype);
if (from->evtype == DPMSInfoNotify) {
xDPMSInfoNotifyEvent *c = (xDPMSInfoNotifyEvent *) to;
swapl(&c->timestamp);
swaps(&c->power_level);
}
}
static int
ProcDPMSSelectInput(register ClientPtr client)
{
X_REQUEST_HEAD_STRUCT(xDPMSSelectInputReq);
X_REQUEST_FIELD_CARD32(eventMask);
__xorg_list_autoinit(&dpms_listeners);
if (stuff->eventMask == DPMSInfoNotifyMask) {
DPMSEventPtr walk = NULL;
/* do we already have an entry for this client ? */
xorg_list_for_each_entry(walk, &dpms_listeners, entry) {
if (walk->client == client) {
walk->mask = stuff->eventMask;
return Success;
}
}
/* build the entry */
DPMSEventPtr pNewEvent = calloc(1, sizeof(DPMSEventRec));
if (!pNewEvent)
return BadAlloc;
pNewEvent->client = client;
pNewEvent->mask = stuff->eventMask;
xorg_list_append(&pNewEvent->entry, &dpms_listeners);
}
else if (stuff->eventMask == 0) {
/* delete the interest */
DPMSDeleteClient(client);
}
else {
client->errorValue = stuff->eventMask;
return BadValue;
}
return Success;
}
static void
SendDPMSInfoNotify(void)
{
xDPMSInfoNotifyEvent se;
DPMSEventRec *pEvent;
xorg_list_for_each_entry(pEvent, &dpms_listeners, entry) {
if ((pEvent->mask & DPMSInfoNotifyMask) == 0) {
continue;
}
se.type = GenericEvent;
se.extension = DPMSReqCode;
se.length = (sizeof(xDPMSInfoNotifyEvent) - 32) >> 2;
se.evtype = DPMSInfoNotify;
se.timestamp = currentTime.milliseconds;
se.power_level = DPMSPowerLevel;
se.state = DPMSEnabled;
WriteEventsToClient(pEvent->client, 1, (xEvent *)&se);
}
}
/**
* Query whether DPMS is supported by any screen.
*
* Returns TRUE if at least one screen (or GPU screen) has a non-NULL
* DPMS function pointer. Called during DPMSExtensionInit to decide
* whether to register the extension at all, and by the Info request
* to report the DPMS state.
*/
static bool DPMSSupported(void)
{
/* For each screen, check if DPMS is supported */
DIX_FOR_EACH_SCREEN({
if (walkScreen->DPMS != NULL) {
return TRUE;
}
});
DIX_FOR_EACH_GPU_SCREEN({
if (walkScreen->DPMS != NULL) {
return TRUE;
}
});
return FALSE;
}
static bool isUnblank(int mode)
{
switch (mode) {
case SCREEN_SAVER_OFF:
case SCREEN_SAVER_FORCER:
return TRUE;
case SCREEN_SAVER_ON:
case SCREEN_SAVER_CYCLE:
return FALSE;
default:
return TRUE;
}
}
int
DPMSSet(ClientPtr client, int level)
{
int old_level = DPMSPowerLevel;
DPMSPowerLevel = level;
if (level != DPMSModeOn) {
if (isUnblank(screenIsSaved)) {
X_CALL_CHECK_ERR(dixSaveScreens(client, SCREEN_SAVER_FORCER, ScreenSaverActive));
}
} else if (!isUnblank(screenIsSaved)) {
X_CALL_CHECK_ERR(dixSaveScreens(client, SCREEN_SAVER_OFF, ScreenSaverReset));
}
DIX_FOR_EACH_SCREEN({
if (walkScreen->DPMS != NULL) {
walkScreen->DPMS(walkScreen, level);
}
});
DIX_FOR_EACH_GPU_SCREEN({
if (walkScreen->DPMS != NULL) {
walkScreen->DPMS(walkScreen, level);
}
});
if (DPMSPowerLevel != old_level) {
SendDPMSInfoNotify();
}
return Success;
}
static int
ProcDPMSGetVersion(ClientPtr client)
{
X_REQUEST_HEAD_STRUCT(xDPMSGetVersionReq);
X_REQUEST_FIELD_CARD16(majorVersion);
X_REQUEST_FIELD_CARD16(minorVersion);
xDPMSGetVersionReply reply = {
.majorVersion = SERVER_DPMS_MAJOR_VERSION,
.minorVersion = SERVER_DPMS_MINOR_VERSION
};
X_REPLY_FIELD_CARD16(majorVersion);
X_REPLY_FIELD_CARD16(minorVersion);
return X_SEND_REPLY_SIMPLE(client, reply);
}
static int
ProcDPMSCapable(ClientPtr client)
{
X_REQUEST_HEAD_STRUCT(xDPMSCapableReq);
xDPMSCapableReply reply = {
.capable = TRUE
};
return X_SEND_REPLY_SIMPLE(client, reply);
}
static int
ProcDPMSGetTimeouts(ClientPtr client)
{
X_REQUEST_HEAD_STRUCT(xDPMSGetTimeoutsReq);
xDPMSGetTimeoutsReply reply = {
.standby = DPMSStandbyTime / MILLI_PER_SECOND,
.suspend = DPMSSuspendTime / MILLI_PER_SECOND,
.off = DPMSOffTime / MILLI_PER_SECOND
};
X_REPLY_FIELD_CARD16(standby);
X_REPLY_FIELD_CARD16(suspend);
X_REPLY_FIELD_CARD16(off);
return X_SEND_REPLY_SIMPLE(client, reply);
}
static int
ProcDPMSSetTimeouts(ClientPtr client)
{
X_REQUEST_HEAD_STRUCT(xDPMSSetTimeoutsReq);
X_REQUEST_FIELD_CARD16(standby);
X_REQUEST_FIELD_CARD16(suspend);
X_REQUEST_FIELD_CARD16(off);
if ((stuff->off != 0) && (stuff->off < stuff->suspend)) {
client->errorValue = stuff->off;
return BadValue;
}
if ((stuff->suspend != 0) && (stuff->suspend < stuff->standby)) {
client->errorValue = stuff->suspend;
return BadValue;
}
DPMSStandbyTime = stuff->standby * MILLI_PER_SECOND;
DPMSSuspendTime = stuff->suspend * MILLI_PER_SECOND;
DPMSOffTime = stuff->off * MILLI_PER_SECOND;
SetScreenSaverTimer();
return Success;
}
static int
ProcDPMSEnable(ClientPtr client)
{
X_REQUEST_HEAD_STRUCT(xDPMSEnableReq);
bool was_enabled = DPMSEnabled;
DPMSEnabled = TRUE;
if (!was_enabled) {
SetScreenSaverTimer();
SendDPMSInfoNotify();
}
return Success;
}
static int
ProcDPMSDisable(ClientPtr client)
{
X_REQUEST_HEAD_STRUCT(xDPMSDisableReq);
bool was_enabled = DPMSEnabled;
DPMSSet(client, DPMSModeOn);
DPMSEnabled = FALSE;
if (was_enabled) {
SendDPMSInfoNotify();
}
return Success;
}
static int
ProcDPMSForceLevel(ClientPtr client)
{
X_REQUEST_HEAD_STRUCT(xDPMSForceLevelReq);
X_REQUEST_FIELD_CARD16(level);
if (!DPMSEnabled) {
return BadMatch;
}
if (stuff->level != DPMSModeOn &&
stuff->level != DPMSModeStandby &&
stuff->level != DPMSModeSuspend && stuff->level != DPMSModeOff) {
client->errorValue = stuff->level;
return BadValue;
}
DPMSSet(client, stuff->level);
return Success;
}
static int
ProcDPMSInfo(ClientPtr client)
{
X_REQUEST_HEAD_STRUCT(xDPMSInfoReq);
xDPMSInfoReply reply = {
.power_level = DPMSPowerLevel,
.state = DPMSEnabled
};
X_REPLY_FIELD_CARD16(power_level);
return X_SEND_REPLY_SIMPLE(client, reply);
}
static int
ProcDPMSDispatch(ClientPtr client)
{
REQUEST(xReq);
switch (stuff->data) {
case X_DPMSGetVersion:
return ProcDPMSGetVersion(client);
case X_DPMSCapable:
return ProcDPMSCapable(client);
case X_DPMSGetTimeouts:
return ProcDPMSGetTimeouts(client);
case X_DPMSSetTimeouts:
return ProcDPMSSetTimeouts(client);
case X_DPMSEnable:
return ProcDPMSEnable(client);
case X_DPMSDisable:
return ProcDPMSDisable(client);
case X_DPMSForceLevel:
return ProcDPMSForceLevel(client);
case X_DPMSInfo:
return ProcDPMSInfo(client);
case X_DPMSSelectInput:
return ProcDPMSSelectInput(client);
default:
return BadRequest;
}
}
static void
DPMSCloseDownExtension(ExtensionEntry *e)
{
DPMSSet(serverClient, DPMSModeOn);
DeleteCallback(&ClientDestroyCallback, DPMSClientDestroyCallback, NULL);
DPMSEventRec *walk, *tmp;
xorg_list_for_each_entry_safe(walk, tmp, &dpms_listeners, entry) {
xorg_list_del(&walk->entry);
free(walk);
}
}
void
DPMSExtensionInit(void)
{
ExtensionEntry *extEntry;
#define CONDITIONALLY_SET_DPMS_TIMEOUT(_timeout_value_) \
if ((_timeout_value_) == -1) { /* not yet set from config */ \
(_timeout_value_) = ScreenSaverTime; \
}
xorg_list_init(&dpms_listeners);
CONDITIONALLY_SET_DPMS_TIMEOUT(DPMSStandbyTime)
CONDITIONALLY_SET_DPMS_TIMEOUT(DPMSSuspendTime)
CONDITIONALLY_SET_DPMS_TIMEOUT(DPMSOffTime)
DPMSPowerLevel = DPMSModeOn;
DPMSEnabled = DPMSSupported();
if (DPMSEnabled &&
(extEntry = AddExtension(DPMSExtensionName, 0, 0,
ProcDPMSDispatch, ProcDPMSDispatch,
DPMSCloseDownExtension, StandardMinorOpcode))) {
DPMSReqCode = extEntry->base;
GERegisterExtension(DPMSReqCode, SDPMSInfoNotifyEvent);
AddCallback(&ClientDestroyCallback, DPMSClientDestroyCallback, NULL);
}
}