Xinqi Bao's Git

Fix typo in temperature
[slstatus.git] / slstatus.c
1 /* See LICENSE file for copyright and license details. */
2 #include <locale.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #include <X11/Xlib.h>
9
10 #include "arg.h"
11 #include "slstatus.h"
12 #include "util.h"
13
14 struct arg {
15 const char *(*func)();
16 const char *fmt;
17 const char *args;
18 };
19
20 char *argv0;
21 char buf[1024];
22 static unsigned short int done;
23 static Display *dpy;
24
25 #include "config.h"
26
27 static void
28 terminate(const int signo)
29 {
30 (void)signo;
31
32 done = 1;
33 }
34
35 static void
36 difftimespec(struct timespec *res, struct timespec *a, struct timespec *b)
37 {
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;
41 }
42
43 static void
44 usage(void)
45 {
46 fprintf(stderr, "usage: %s [-s]\n", argv0);
47 exit(1);
48 }
49
50 int
51 main(int argc, char *argv[])
52 {
53 struct sigaction act;
54 struct timespec start, current, diff, intspec, wait;
55 size_t i, len;
56 int sflag = 0;
57 char status[MAXLEN];
58
59 ARGBEGIN {
60 case 's':
61 sflag = 1;
62 break;
63 default:
64 usage();
65 } ARGEND
66
67 if (argc) {
68 usage();
69 }
70
71 setlocale(LC_ALL, "");
72
73 memset(&act, 0, sizeof(act));
74 act.sa_handler = terminate;
75 sigaction(SIGINT, &act, NULL);
76 sigaction(SIGTERM, &act, NULL);
77
78 if (!sflag && !(dpy = XOpenDisplay(NULL))) {
79 fprintf(stderr, "Cannot open display");
80 return 1;
81 }
82
83 while (!done) {
84 clock_gettime(CLOCK_MONOTONIC, &start);
85
86 status[0] = '\0';
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,
91 args[i].fmt, res);
92
93 if (len >= sizeof(status)) {
94 status[sizeof(status) - 1] = '\0';
95 }
96 }
97
98 if (sflag) {
99 printf("%s\n", status);
100 } else {
101 XStoreName(dpy, DefaultRootWindow(dpy), status);
102 XSync(dpy, False);
103 }
104
105 if (!done) {
106 clock_gettime(CLOCK_MONOTONIC, &current);
107 difftimespec(&diff, &current, &start);
108
109 intspec.tv_sec = interval / 1000;
110 intspec.tv_nsec = (interval % 1000) * 1000000;
111 difftimespec(&wait, &intspec, &diff);
112
113 if (wait.tv_sec >= 0) {
114 nanosleep(&wait, NULL);
115 }
116 }
117 }
118
119 if (!sflag) {
120 XStoreName(dpy, DefaultRootWindow(dpy), NULL);
121 XCloseDisplay(dpy);
122 }
123
124 return 0;
125 }