Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
15 const char *(*func
)();
28 terminate(const int signo
)
36 difftimespec(struct timespec
*res
, struct timespec
*a
, struct timespec
*b
)
38 res
->tv_sec
= a
->tv_sec
- b
->tv_sec
- (a
->tv_nsec
< b
->tv_nsec
);
39 res
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
+
40 (a
->tv_nsec
< b
->tv_nsec
) * 1000000000;
46 fprintf(stderr
, "usage: %s [-s]\n", argv0
);
51 main(int argc
, char *argv
[])
54 struct timespec start
, current
, diff
, intspec
, wait
;
72 setlocale(LC_ALL
, "");
74 memset(&act
, 0, sizeof(act
));
75 act
.sa_handler
= terminate
;
76 sigaction(SIGINT
, &act
, NULL
);
77 sigaction(SIGTERM
, &act
, NULL
);
79 if (!sflag
&& !(dpy
= XOpenDisplay(NULL
))) {
80 fprintf(stderr
, "Cannot open display");
85 clock_gettime(CLOCK_MONOTONIC
, &start
);
88 for (i
= len
= 0; i
< LEN(args
); i
++) {
89 const char * res
= args
[i
].func(args
[i
].args
);
90 res
= (res
== NULL
) ? unknown_str
: res
;
91 len
+= snprintf(status
+ len
, sizeof(status
) - len
,
94 if (len
>= sizeof(status
)) {
95 status
[sizeof(status
) - 1] = '\0';
100 printf("%s\n", status
);
103 XStoreName(dpy
, DefaultRootWindow(dpy
), status
);
108 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
109 difftimespec(&diff
, ¤t
, &start
);
111 intspec
.tv_sec
= interval
/ 1000;
112 intspec
.tv_nsec
= (interval
% 1000) * 1000000;
113 difftimespec(&wait
, &intspec
, &diff
);
115 if (wait
.tv_sec
>= 0) {
116 nanosleep(&wait
, NULL
);
122 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);