Standardize battery.c
[dwm-status.git] / dwm-status.c
blob153dfebb73190e7a96ce346caabcf0c70e1d59da
1 /*-
2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
6 * Tobias Rehbein
7 */
9 #define _POSIX_C_SOURCE 199506
11 #include <err.h>
12 #include <locale.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sysexits.h>
16 #include <unistd.h>
18 #include <X11/Xlib.h>
20 #include "battery.h"
21 #include "buffers.h"
22 #include "clock.h"
23 #include "load.h"
24 #include "mpd.h"
25 #include "tools.h"
27 enum {
28 SLEEP = 1,
31 int
32 main(void)
34 Display *dpy;
35 Window root;
36 struct clock_context *clock_ctx;
37 struct battery_context *battery_ctx;
38 struct load_context *load_ctx;
39 struct mpd_context *mpd_ctx;
40 int screen;
42 if (setlocale(LC_ALL, "") == NULL)
43 errx(EX_SOFTWARE, "setlocale()");
45 if ((dpy = XOpenDisplay(NULL)) == NULL)
46 errx(EX_SOFTWARE, "unable to open display '%s'", XDisplayName(NULL));
47 screen = DefaultScreen(dpy);
48 root = RootWindow(dpy, screen);
51 if ((clock_ctx = clock_context_open()) == NULL)
52 errx(EX_SOFTWARE, "clock_context_open()");
53 if ((battery_ctx = battery_context_open()) == NULL)
54 errx(EX_SOFTWARE, "battery_context_open()");
55 if ((load_ctx = load_context_open()) == NULL)
56 errx(EX_SOFTWARE, "load_context_open()");
57 if ((mpd_ctx = mpd_context_open()) == NULL)
58 errx(EX_SOFTWARE, "mpd_context_open()");
60 for (;;) {
61 char status[STATUS_BUFFLEN];
62 char *clock;
63 char *battery;
64 char *load;
65 char *mpd;
67 if ((clock = clock_str(clock_ctx)) == NULL)
68 err(EX_SOFTWARE, "clock_str");
69 if ((battery = battery_str(battery_ctx)) == NULL)
70 err(EX_SOFTWARE, "clock_str");
71 if ((load = load_str(load_ctx)) == NULL)
72 err(EX_SOFTWARE, "load_str");
73 if ((mpd = mpd_str(mpd_ctx)) == NULL)
74 err(EX_SOFTWARE, "mpd_str");
76 tools_catitems(status, sizeof(status), mpd, " | ", load, " | ", battery, " | ", clock, NULL);
77 XStoreName(dpy, root, status);
78 XFlush(dpy);
80 sleep(SLEEP);
84 * This code will never be reached (at least at the moment).
85 * Nonetheless I regard it good style to implement cleanup code.
87 mpd_context_close(mpd_ctx);
88 load_context_close(load_ctx);
89 battery_context_close(battery_ctx);
90 clock_context_close(clock_ctx);
91 XCloseDisplay(dpy);
93 return (0);