Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
15 const char *(*func
)();
27 terminate(const int signo
)
35 difftimespec(struct timespec
*res
, struct timespec
*a
, struct timespec
*b
)
37 res
->tv_sec
= a
->tv_sec
- b
->tv_sec
- (a
->tv_nsec
< b
->tv_nsec
);
38 res
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
+
39 (a
->tv_nsec
< b
->tv_nsec
) * 1000000000;
45 die("usage: %s [-s]", argv0
);
49 main(int argc
, char *argv
[])
52 struct timespec start
, current
, diff
, intspec
, wait
;
71 memset(&act
, 0, sizeof(act
));
72 act
.sa_handler
= terminate
;
73 sigaction(SIGINT
, &act
, NULL
);
74 sigaction(SIGTERM
, &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) {
102 printf("%s\n", status
);
104 if (XStoreName(dpy
, DefaultRootWindow(dpy
), status
) < 0) {
105 die("XStoreName: Allocation failed");
111 if (clock_gettime(CLOCK_MONOTONIC
, ¤t
) < 0) {
112 die("clock_gettime:");
114 difftimespec(&diff
, ¤t
, &start
);
116 intspec
.tv_sec
= interval
/ 1000;
117 intspec
.tv_nsec
= (interval
% 1000) * 1000000;
118 difftimespec(&wait
, &intspec
, &diff
);
120 if (wait
.tv_sec
>= 0) {
121 if (nanosleep(&wait
, NULL
) < 0 &&
130 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);
131 if (XCloseDisplay(dpy
) < 0) {
132 die("XCloseDisplay: Failed to close display");