Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
17 const char *(*func
)();
24 static unsigned short int done
;
30 terminate(const int signo
)
38 difftimespec(struct timespec
*res
, struct timespec
*a
, struct timespec
*b
)
40 res
->tv_sec
= a
->tv_sec
- b
->tv_sec
- (a
->tv_nsec
< b
->tv_nsec
);
41 res
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
+
42 (a
->tv_nsec
< b
->tv_nsec
) * 1000000000;
48 fprintf(stderr
, "usage: %s [-s]\n", argv0
);
53 main(int argc
, char *argv
[])
56 struct timespec start
, current
, diff
, intspec
, wait
;
73 setlocale(LC_ALL
, "");
75 memset(&act
, 0, sizeof(act
));
76 act
.sa_handler
= terminate
;
77 sigaction(SIGINT
, &act
, NULL
);
78 sigaction(SIGTERM
, &act
, NULL
);
80 if (!sflag
&& !(dpy
= XOpenDisplay(NULL
))) {
81 fprintf(stderr
, "slstatus: cannot open display");
86 clock_gettime(CLOCK_MONOTONIC
, &start
);
89 for (i
= len
= 0; i
< LEN(args
); i
++) {
90 len
+= snprintf(status
+ len
, sizeof(status
) - len
,
91 args
[i
].fmt
, args
[i
].func(args
[i
].args
));
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
);