Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
15 const char *(*func
)();
22 static unsigned short int done
;
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
;
71 setlocale(LC_ALL
, "");
73 memset(&act
, 0, sizeof(act
));
74 act
.sa_handler
= terminate
;
75 sigaction(SIGINT
, &act
, NULL
);
76 sigaction(SIGTERM
, &act
, NULL
);
78 if (!sflag
&& !(dpy
= XOpenDisplay(NULL
))) {
79 fprintf(stderr
, "Cannot open display");
84 clock_gettime(CLOCK_MONOTONIC
, &start
);
87 for (i
= len
= 0; i
< LEN(args
); i
++) {
88 const char * res
= args
[i
].func(args
[i
].args
);
89 res
= (res
== NULL
) ? unknown_str
: res
;
90 len
+= snprintf(status
+ len
, sizeof(status
) - len
,
93 if (len
>= sizeof(status
)) {
94 status
[sizeof(status
) - 1] = '\0';
99 printf("%s\n", status
);
101 XStoreName(dpy
, DefaultRootWindow(dpy
), status
);
106 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
107 difftimespec(&diff
, ¤t
, &start
);
109 intspec
.tv_sec
= interval
/ 1000;
110 intspec
.tv_nsec
= (interval
% 1000) * 1000000;
111 difftimespec(&wait
, &intspec
, &diff
);
113 if (wait
.tv_sec
>= 0) {
114 nanosleep(&wait
, NULL
);
120 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);