tEverything on separate files - spoon - dwm status utility (2f30 fork)
 (HTM) git clone git://src.adamsgaard.dk/spoon
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit 57cba38800da81ab96354b669cd4ab8f6962fe5f
 (DIR) parent e12c3d3c4d8ba3ddd7734b1cfacecbd609b33b40
 (HTM) Author: lostd <lostd@2f30.org>
       Date:   Thu, 13 Oct 2016 00:42:22 +0100
       
       Everything on separate files
       
       Also make sure we only include what we use.
       
       Diffstat:
         M Makefile                            |       7 ++++---
         A date.c                              |      19 +++++++++++++++++++
         A mpd.c                               |      54 +++++++++++++++++++++++++++++++
         M spoon.c                             |     120 +------------------------------
         A xkblayout.c                         |      55 +++++++++++++++++++++++++++++++
       
       5 files changed, 134 insertions(+), 121 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       t@@ -1,9 +1,10 @@
        VERSION = 0.2
        PREFIX = /usr/local
       -DISTFILES = spoon.c batt.c wifi.c cpu.c temp.c mix.c\
       -            strlcpy.c strlcat.c util.h config.def.h\
       +DISTFILES = spoon.c batt.c wifi.c cpu.c temp.c mix.c date.c mpd.c\
       +            xkblayout.c strlcpy.c strlcat.c util.h config.def.h\
                    Makefile LICENSE configure
       -OBJ = spoon.o batt.o wifi.o cpu.o temp.o mix.o strlcpy.o strlcat.o
       +OBJ = spoon.o batt.o wifi.o cpu.o temp.o mix.o date.o mpd.o xkblayout.o\
       +      strlcpy.o strlcat.o
        BIN = spoon
        
        include config.mk
 (DIR) diff --git a/date.c b/date.c
       t@@ -0,0 +1,19 @@
       +#include <sys/types.h>
       +
       +#include <stddef.h>
       +#include <stdio.h>
       +#include <time.h>
       +
       +int
       +dateread(char *buf, size_t len)
       +{
       +        struct tm *now;
       +        time_t t;
       +
       +        time(&t);
       +        now = localtime(&t);
       +        if (now == NULL)
       +                return -1;
       +        strftime(buf, len, "%a %d %b %Y %H:%M %Z", now);
       +        return 0;
       +}
 (DIR) diff --git a/mpd.c b/mpd.c
       t@@ -0,0 +1,54 @@
       +#include <err.h>
       +#include <stddef.h>
       +#include <stdio.h>
       +
       +#include <mpd/client.h>
       +
       +#include "util.h"
       +
       +int
       +mpdread(char *buf, size_t len)
       +{
       +        static struct mpd_connection *conn;
       +        struct mpd_song *song;
       +        const char *artist, *title, *name;
       +
       +        if (conn == NULL) {
       +                conn = mpd_connection_new(NULL, 0, 0);
       +                if (conn == NULL)
       +                        return -1;
       +                if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
       +                        goto out;
       +        }
       +        mpd_send_current_song(conn);
       +        song = mpd_recv_song(conn);
       +        if (song == NULL) {
       +                if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
       +                        goto out;
       +                /* if no song is playing, reuse connection next time */
       +                return -1;
       +        }
       +        artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
       +        title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
       +        if (artist != NULL && title != NULL) {
       +                snprintf(buf, len, "%s - %s", artist, title);
       +        } else if (title != NULL) {
       +                strlcpy(buf, title, len);
       +        } else {
       +                name = mpd_song_get_uri(song);
       +                if (name == NULL) {
       +                        mpd_song_free(song);
       +                        goto out;
       +                }
       +                strlcpy(buf, name, len);
       +        }
       +        mpd_song_free(song);
       +        if (!mpd_response_finish(conn))
       +                goto out;
       +        return 0;
       +out:
       +        warnx("failed to talk to mpd");
       +        mpd_connection_free(conn);
       +        conn = NULL;
       +        return -1;
       +}
 (DIR) diff --git a/spoon.c b/spoon.c
       t@@ -1,19 +1,9 @@
        /* See LICENSE file for copyright and license details. */
       -#include <sys/types.h>
       -
        #include <err.h>
       +#include <stddef.h>
        #include <stdio.h>
       -#include <stdlib.h>
       -#include <string.h>
       -#include <time.h>
       -#include <unistd.h>
       -
       -#include <X11/XKBlib.h>
       -#include <X11/extensions/XKBrules.h>
        
       -#include <mpd/client.h>
       -
       -#include "util.h"
       +#include <X11/Xlib.h>
        
        #define LEN(x) (sizeof (x) / sizeof *(x))
        
       t@@ -41,112 +31,6 @@ dummyread(char *buf, size_t len)
                return 0;
        }
        
       -int
       -mpdread(char *buf, size_t len)
       -{
       -        static struct mpd_connection *conn;
       -        struct mpd_song *song;
       -        const char *artist, *title, *name;
       -
       -        if (conn == NULL) {
       -                conn = mpd_connection_new(NULL, 0, 0);
       -                if (conn == NULL)
       -                        return -1;
       -                if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
       -                        goto out;
       -        }
       -        mpd_send_current_song(conn);
       -        song = mpd_recv_song(conn);
       -        if (song == NULL) {
       -                if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
       -                        goto out;
       -                /* if no song is playing, reuse connection next time */
       -                return -1;
       -        }
       -        artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
       -        title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
       -        if (artist != NULL && title != NULL) {
       -                snprintf(buf, len, "%s - %s", artist, title);
       -        } else if (title != NULL) {
       -                strlcpy(buf, title, len);
       -        } else {
       -                name = mpd_song_get_uri(song);
       -                if (name == NULL) {
       -                        mpd_song_free(song);
       -                        goto out;
       -                }
       -                strlcpy(buf, name, len);
       -        }
       -        mpd_song_free(song);
       -        if (!mpd_response_finish(conn))
       -                goto out;
       -        return 0;
       -out:
       -        warnx("failed to talk to mpd");
       -        mpd_connection_free(conn);
       -        conn = NULL;
       -        return -1;
       -}
       -
       -int
       -dateread(char *buf, size_t len)
       -{
       -        struct tm *now;
       -        time_t t;
       -
       -        time(&t);
       -        now = localtime(&t);
       -        if (now == NULL)
       -                return -1;
       -        strftime(buf, len, "%a %d %b %Y %H:%M %Z", now);
       -        return 0;
       -}
       -
       -int
       -xkblayoutread(char *buf, size_t len)
       -{
       -        Display *dpy;
       -        XkbStateRec state;
       -        XkbRF_VarDefsRec vd;
       -        char *tmp = NULL, *str, *tok;
       -        int i, ret = 0;
       -
       -        dpy = XOpenDisplay(NULL);
       -        if (dpy == NULL) {
       -                warnx("cannot open display");
       -                return -1;
       -        }
       -        XkbGetState(dpy, XkbUseCoreKbd, &state);
       -        if (XkbRF_GetNamesProp(dpy, &tmp, &vd) == 0){
       -                warnx("cannot extract keyboard properties");
       -                ret = -1;
       -                goto out0;
       -        }
       -        str = strdup(vd.layout);
       -        if (str == NULL) {
       -                ret = -1;
       -                goto out1;
       -        }
       -        tok = strtok(str, ",");
       -        for (i = 0; i < state.group; i++) {
       -                tok = strtok(NULL, ",");
       -                if (tok == NULL) {
       -                        warnx("cannot extract layout");
       -                        ret = -1;
       -                        goto out2;
       -                }
       -        }
       -        strlcpy(buf, tok, len);
       -out2:
       -        free(str);
       -out1:
       -        free(tmp);
       -        XFree(vd.options);
       -out0:
       -        XCloseDisplay(dpy);
       -        return ret;
       -}
       -
        void
        entcat(char *line, size_t len)
        {
 (DIR) diff --git a/xkblayout.c b/xkblayout.c
       t@@ -0,0 +1,55 @@
       +#include <err.h>
       +#include <stdio.h>
       +#include <stdlib.h>
       +#include <string.h>
       +
       +#include <X11/Xlib.h>
       +#include <X11/XKBlib.h>
       +#include <X11/extensions/XKBrules.h>
       +
       +#include "util.h"
       +
       +int
       +xkblayoutread(char *buf, size_t len)
       +{
       +        Display *dpy;
       +        XkbStateRec state;
       +        XkbRF_VarDefsRec vd;
       +        char *tmp = NULL, *str, *tok;
       +        int i, ret = 0;
       +
       +        dpy = XOpenDisplay(NULL);
       +        if (dpy == NULL) {
       +                warnx("cannot open display");
       +                return -1;
       +        }
       +        XkbGetState(dpy, XkbUseCoreKbd, &state);
       +        if (XkbRF_GetNamesProp(dpy, &tmp, &vd) == 0){
       +                warnx("cannot extract keyboard properties");
       +                ret = -1;
       +                goto out0;
       +        }
       +        str = strdup(vd.layout);
       +        if (str == NULL) {
       +                ret = -1;
       +                goto out1;
       +        }
       +        tok = strtok(str, ",");
       +        for (i = 0; i < state.group; i++) {
       +                tok = strtok(NULL, ",");
       +                if (tok == NULL) {
       +                        warnx("cannot extract layout");
       +                        ret = -1;
       +                        goto out2;
       +                }
       +        }
       +        strlcpy(buf, tok, len);
       +out2:
       +        free(str);
       +out1:
       +        free(tmp);
       +        XFree(vd.options);
       +out0:
       +        XCloseDisplay(dpy);
       +        return ret;
       +}