Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
16 const char *(*func
)();
22 static unsigned short int done
;
29 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 fprintf(stderr
, "usage: %s [-s]\n", argv0
);
50 main(int argc
, char *argv
[])
53 struct timespec start
, current
, diff
, intspec
, wait
;
70 setlocale(LC_ALL
, "");
72 memset(&act
, 0, sizeof(act
));
73 act
.sa_handler
= terminate
;
74 sigaction(SIGINT
, &act
, NULL
);
75 sigaction(SIGTERM
, &act
, NULL
);
77 if (!sflag
&& !(dpy
= XOpenDisplay(NULL
))) {
78 fprintf(stderr
, "slstatus: cannot open display");
83 clock_gettime(CLOCK_MONOTONIC
, &start
);
86 for (i
= len
= 0; i
< LEN(args
); i
++) {
87 len
+= snprintf(status
+ len
, sizeof(status
) - len
,
88 args
[i
].fmt
, args
[i
].func(args
[i
].args
));
90 if (len
>= sizeof(status
)) {
91 status
[sizeof(status
) - 1] = '\0';
96 printf("%s\n", status
);
98 XStoreName(dpy
, DefaultRootWindow(dpy
), status
);
103 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
104 difftimespec(&diff
, ¤t
, &start
);
106 intspec
.tv_sec
= interval
/ 1000;
107 intspec
.tv_nsec
= (interval
% 1000) * 1000000;
108 difftimespec(&wait
, &intspec
, &diff
);
110 if (wait
.tv_sec
>= 0) {
111 nanosleep(&wait
, NULL
);
117 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);