Files

197 lines
6.0 KiB
C

/*
* energy.c -- dtsystemsettings: the Energy Saver (t0120)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The reference's rows: "Computer sleep:", "Display sleep:", "Put the hard
* disks to sleep when possible:" and a schedule. Two of them are the
* desktop's own:
*
* Display sleep is dtsession's saver time (SmStateInfo.smSaverTimeout),
* the same setting the Desktop & Screen Saver page shows --
* on this desktop the saver blanks the display; the DPMS
* timeouts are a NetBSD/VM follow-up (Xvfb has no DPMS
* extension, measured).
* Computer sleep and the disks need the operating system (root): the
* values are kept in ~/.dt/energy.conf for the part that
* puts them into effect (NetBSD: sysctl powerd/atactl, the
* follow-up task).
* Schedule left out with its reason: a timed wake or sleep needs the
* same root part.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Intrinsic.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.h>
#include <Dt/McdePrefs.h>
#include <Dt/SessionM.h>
#include "ui.h"
static const char *const minutes[] = { "Never", "1 minute", "5 minutes",
"10 minutes", "15 minutes", "30 minutes", "1 hour" };
static const int values[] = { 0, 1, 5, 10, 15, 30, 60 };
#define NMIN ((int) (sizeof values / sizeof values[0]))
static Widget displayNote;
static int displayChoice = -1;
/* the value of KEY as an index into VALUES (DEF when the file has none) */
static int
Index(const char *key, int def)
{
char v[32];
int i;
if (!McdePrefsConfGet("energy", key, v, sizeof v) || v[0] == '\0')
return def;
for (i = 0; i < NMIN; i++)
if (atoi(v) == values[i])
return i;
return def;
}
/* dtsession's saver time into the display menu */
static int
SessionSaver(Widget w)
{
Display *dpy = XtDisplayOfObject(w);
Window sm = None;
SmStateInfo st;
int i;
if (_DtGetSmWindow(dpy, RootWindow(dpy, 0), &sm) != Success)
return -1;
if (_DtGetSmState(dpy, sm, &st) != Success)
return -1;
for (i = 0; i < NMIN; i++)
if (values[i] * 60 == st.smSaverTimeout)
return i;
return st.smSaverTimeout > 0 ? 2 : 0;
}
static void
SetSaver(Widget w, int mins)
{
/* W is the menu's item, a gadget: XtDisplayOfObject knows gadgets */
Display *dpy = XtDisplayOfObject(w);
Window sm = None;
SmStateInfo st;
if (_DtGetSmWindow(dpy, RootWindow(dpy, 0), &sm) != Success) {
SetText(displayNote, "No session manager runs: the display "
"time is kept, but not in effect.");
return;
}
memset(&st, 0, sizeof st);
st.flags = SM_STATE_SAVERTIMEOUT;
st.smSaverTimeout = mins * 60;
if (_DtSetSmState(dpy, sm, &st) != Success)
SetText(displayNote, "The session manager did not take it.");
else if (mins == 0)
SetText(displayNote, "The display stays on.");
else
SetText(displayNote, "The display blanks with the screen "
"saver after that time.");
XFlush(dpy);
}
static void
Chosen(Widget w, XtPointer data, XtPointer call)
{
const char *key = (const char *) data; /* "computer", "display", … */
char *text = NULL;
int i, at = -1;
(void) call;
XtVaGetValues(w, XmNuserData, &text, NULL);
for (i = 0; i < NMIN; i++)
if (text != NULL && strcmp(text, minutes[i]) == 0)
at = i;
if (at < 0)
return;
{
char v[16];
(void)snprintf(v, sizeof v, "%d", values[at]);
(void)McdePrefsConfSet("energy", key, v);
}
if (strcmp(key, "display") == 0) {
displayChoice = at;
SetSaver(w, values[at]);
}
}
static Widget
Row(Widget parent, const char *key, const char *label, int cur)
{
Widget row = XtVaCreateManagedWidget("energyRow",
xmRowColumnWidgetClass, parent, XmNorientation, XmHORIZONTAL,
XmNspacing, 10, XmNmarginWidth, 0, NULL);
XmString s = XmStringCreateLocalized((char *) label);
Widget l = XtVaCreateManagedWidget("energyLabel",
xmLabelWidgetClass, row, XmNlabelString, s, XmNwidth, 280,
XmNrecomputeSize, False, XmNalignment, XmALIGNMENT_END, NULL);
Widget opt, pd, item[8];
int i;
XmStringFree(s);
(void) l;
opt = XmCreateOptionMenu(row, (char *) key, NULL, 0);
pd = XmCreatePulldownMenu(opt, "energyMenu", NULL, 0);
for (i = 0; i < NMIN; i++) {
s = XmStringCreateLocalized((char *) minutes[i]);
item[i] = XtVaCreateManagedWidget("item",
xmPushButtonGadgetClass, pd, XmNlabelString, s,
XmNuserData, minutes[i], NULL);
XmStringFree(s);
XtAddCallback(item[i], XmNactivateCallback, Chosen,
(XtPointer) key);
}
XtVaSetValues(opt, XmNsubMenuId, pd, XmNmenuHistory,
item[cur >= 0 && cur < NMIN ? cur : 0], NULL);
XtManageChild(opt);
return row;
}
Widget
MakeEnergy(Widget parent)
{
Widget box = XtVaCreateWidget("energy", xmRowColumnWidgetClass,
parent, XmNorientation, XmVERTICAL, XmNspacing, 8, NULL);
int saver;
(void) Text(box, "energyLabel", "Put the computer to sleep when it "
"is not used:");
displayNote = Text(box, "displayNote", "");
(void) Row(box, "computer", "Computer sleep:",
Index("computer", 3));
saver = SessionSaver(box);
(void) Row(box, "display", "Display sleep:",
saver >= 0 ? saver : Index("display", 2));
SetText(displayNote, saver >= 0 ? "The session's screen saver time, "
"the same setting as on the Desktop & Screen Saver page." :
"No session manager runs: the time is kept, but not in effect.");
(void) Row(box, "disks", "Put the hard disks to sleep:",
Index("disks", 0));
(void) Text(box, "scheduleNote", "A schedule (sleep or wake at a "
"time) needs the part of the operating system that sleeps and "
"wakes the machine; it is a follow-up with the NetBSD/DPMS "
"work.");
(void) Text(box, "energyFoot", "Computer and disk sleep are kept in "
"~/.dt/energy.conf; putting them into effect needs the "
"administrator and the operating system.");
return box;
}