-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathxsssuspend.c
140 lines (124 loc) · 3.14 KB
/
xsssuspend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2022, 2024 Vadim Vygonets <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/scrnsaver.h>
#ifndef __dead
#ifdef __GNUC__
#define __dead __attribute__((noreturn))
#else
#define __dead
#endif
#endif
#define EXIT_RUNTIME 125
#define EXIT_EXEC 126
#define EXIT_ERR 127
#define EXIT_ENOENT EXIT_ERR
#define EXIT_SIG 128
#define DELAY 3600
static int xerr = EXIT_ERR;
static int suspended;
static Display *dpy;
static void
closedisplay(void)
{
if (dpy != NULL) {
if (suspended)
XScreenSaverSuspend(dpy, False);
XCloseDisplay(dpy);
}
}
static int __dead
eh(Display *dpy, XErrorEvent *e)
{
char buf[BUFSIZ];
XGetErrorText(dpy, e->error_code, buf, sizeof(buf));
errx(xerr, "X error: %s", buf);
/* NOTREACHED */
}
int
main(int argc, char *argv[])
{
struct timespec ts = {
.tv_sec = DELAY,
.tv_nsec = 0,
};
sigset_t set;
pid_t child = 0;
int evbase, errbase, status;
if ((dpy = XOpenDisplay(NULL)) == NULL)
errx(EXIT_ERR, "no display");
atexit(closedisplay);
XSetErrorHandler(eh);
if (!XScreenSaverQueryExtension(dpy, &evbase, &errbase))
errx(EXIT_ERR, "X11 extension MIT-SCREEN-SAVER not supported");
XScreenSaverSuspend(dpy, True);
XFlush(dpy);
suspended = 1;
sigemptyset(&set);
sigaddset(&set, SIGTERM);
if (argc > 1)
sigaddset(&set, SIGCHLD);
else
sigaddset(&set, SIGINT);
if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
err(EXIT_ERR, "sigprocmask");
if (argc > 1) {
struct sigaction sa = {
.sa_handler = SIG_IGN,
};
if (sigaction(SIGINT, &sa, NULL) == -1)
err(EXIT_ERR, "sigaction");
switch ((child = fork())) {
case -1:
err(EXIT_ERR, "fork");
case 0:
sa.sa_handler = SIG_DFL;
if (sigaction(SIGINT, &sa, NULL) == -1)
err(EXIT_ERR, "sigaction");
if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1)
err(EXIT_ERR, "sigprocmask");
execvp(argv[1], argv + 1);
dpy = NULL;
err(errno == ENOENT ? EXIT_ENOENT : EXIT_EXEC, "exec");
}
}
xerr = EXIT_RUNTIME;
for (;;) {
switch (sigtimedwait(&set, NULL, &ts)) {
case -1:
break;
case SIGCHLD:
if (wait4(child, &status, WNOHANG, NULL) == child) {
if (WIFEXITED(status))
return WEXITSTATUS(status);
if (WIFSIGNALED(status))
return EXIT_SIG + WTERMSIG(status);
}
break;
default:
return 0;
}
XFlush(dpy);
}
/* NOTREACHED */
}