Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
15 const char *(*func
)();
21 static volatile sig_atomic_t done
;
27 terminate(const int signo
)
34 difftimespec(struct timespec
*res
, struct timespec
*a
, struct timespec
*b
)
36 res
->tv_sec
= a
->tv_sec
- b
->tv_sec
- (a
->tv_nsec
< b
->tv_nsec
);
37 res
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
+
38 (a
->tv_nsec
< b
->tv_nsec
) * 1E9
;
44 die("usage: %s [-s] [-1]", argv0
);
48 main(int argc
, char *argv
[])
51 struct timespec start
, current
, diff
, intspec
, wait
;
73 memset(&act
, 0, sizeof(act
));
74 act
.sa_handler
= terminate
;
75 sigaction(SIGINT
, &act
, NULL
);
76 sigaction(SIGTERM
, &act
, NULL
);
77 act
.sa_flags
|= SA_RESTART
;
78 sigaction(SIGUSR1
, &act
, NULL
);
80 if (!sflag
&& !(dpy
= XOpenDisplay(NULL
))) {
81 die("XOpenDisplay: Failed to open display");
85 if (clock_gettime(CLOCK_MONOTONIC
, &start
) < 0) {
86 die("clock_gettime:");
90 for (i
= len
= 0; i
< LEN(args
); i
++) {
91 if (!(res
= args
[i
].func(args
[i
].args
))) {
94 if ((ret
= esnprintf(status
+ len
, sizeof(status
) - len
,
95 args
[i
].fmt
, res
)) < 0) {
107 if (XStoreName(dpy
, DefaultRootWindow(dpy
), status
)
109 die("XStoreName: Allocation failed");
115 if (clock_gettime(CLOCK_MONOTONIC
, ¤t
) < 0) {
116 die("clock_gettime:");
118 difftimespec(&diff
, ¤t
, &start
);
120 intspec
.tv_sec
= interval
/ 1000;
121 intspec
.tv_nsec
= (interval
% 1000) * 1E6
;
122 difftimespec(&wait
, &intspec
, &diff
);
124 if (wait
.tv_sec
>= 0) {
125 if (nanosleep(&wait
, NULL
) < 0 &&
134 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);
135 if (XCloseDisplay(dpy
) < 0) {
136 die("XCloseDisplay: Failed to close display");