Commit: 673ece171ea8a6c6c176dde75288bed34bb5e4f7
Parent: a996f90f3a25c4f52f65d32d914a4a210f99afc0
Author: Randy Palamar
Date: Fri, 19 Apr 2024 17:11:01 -0600
compile as a single translation unit
No more header nonsense or Makefile garbage. Look at the diffstat
on this one - even a tiny program like this had >100 lines of
useless boilerplate. I also have better ways of separating the
platform layers nowadays but we will see if I care enough to
change it.
The build.sh script is added for documentation. Its also not
really important.
Diffstat:
23 files changed, 85 insertions(+), 188 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,29 +0,0 @@
-# See LICENSE for license details.
-include config.mk
-
-SRC += status.c util.c
-OBJ = $(SRC:.c=.o)
-
-all: status
-
-config.h:
- cp config.def.h config.h
-
-.c.o:
- $(CC) $(CFLAGS) $(CPPFLAGS) $(INCS) -o $@ -c $<
-
-$(OBJ): config.h
-
-status: $(OBJ)
- $(CC) -o $@ $(OBJ) $(LIBS) $(LDFLAGS)
-
-clean:
- rm -f $(OBJ) status
-
-install: all
- mkdir -p $(DESTDIR)$(PREFIX)/bin
- cp -f status $(DESTDIR)$(PREFIX)/bin
- chmod 755 $(DESTDIR)$(PREFIX)/bin/status
-
-uninstall:
- rm -f $(DESTDIR)$(PREFIX)/bin/status
diff --git a/README.md b/README.md
@@ -10,19 +10,20 @@ Inspired by and drawing from
Installation
------------
-Modify `config.h` and `config.mk` to your liking and use the following
-to install (using root as needed):
+Copy `config.def.h` to `config.h` and modify to your liking then to build:
```
-make clean install
+./build.sh
```
+Then copy `status` to wherever you want.
+
Usage
-----
-Execute with `status` or use `status &` to run in the background. `status`
-also accepts the `-d` option to print the line to stdout instead of
-setting the x root window name.
+Execute with `status` or use `setsid -f status` to run in the
+background. `status` also accepts the `-d` option to print the
+line to `stdout` instead of setting the X root window name.
Gotchas
-------
@@ -30,7 +31,3 @@ Gotchas
* alsalib is horribly broken. Don't compile with optimizations more
exotic than `-O2` and expect alsa to work. If you aren't using alsa
then you can use whatever meme flags you want.
-
-* If you want a smaller binary remove blocks you aren't using from
- `config.mk`. You can also remove their linker options. Additionally,
- consider using a better compiler like [tcc](https://bellard.org/tcc/).
diff --git a/blocks/date.c b/blocks/date.c
@@ -1,12 +1,5 @@
/* See LICENSE for license details. */
-#include <stdio.h>
-#include <time.h>
-
-#include "../status.h"
-#include "../util.h"
-#include "date.h"
-
-size_t
+static size_t
date(struct Block *b)
{
time_t t = time(NULL);
diff --git a/blocks/date.h b/blocks/date.h
@@ -1 +0,0 @@
-size_t date(struct Block *b);
diff --git a/blocks/linux/battery.c b/blocks/linux/battery.c
@@ -1,16 +1,17 @@
/* See LICENSE for license details. */
#include <limits.h>
-#include <stdio.h>
-#include <string.h>
-#include "../../status.h"
-#include "../../util.h"
-#include "battery.h"
+struct bat_arg {
+ char *bat; /* BAT name (ex. BAT0) */
+ char *pre; /* prefix for percentages less than thres */
+ char *suf; /* suffix for percentages less than thres */
+ int thres; /* % threshold to consider low (-1 to disable) */
+};
-size_t
+static size_t
batinfo(struct Block *b)
{
- const struct bat_arg *ba = b->arg;
+ struct bat_arg *ba = b->arg;
char *pre = ba->pre ? ba->pre : "";
char *suf = ba->suf ? ba->suf : "";
char path[PATH_MAX], state[12];
diff --git a/blocks/linux/battery.h b/blocks/linux/battery.h
@@ -1,8 +0,0 @@
-struct bat_arg {
- char *bat; /* BAT name (ex. BAT0) */
- char *pre; /* prefix for percentages less than thres */
- char *suf; /* suffix for percentages less than thres */
- int thres; /* % threshold to consider low (-1 to disable) */
-};
-
-size_t batinfo(struct Block *b);
diff --git a/blocks/linux/blight.c b/blocks/linux/blight.c
@@ -1,13 +1,7 @@
/* See LICENSE for license details. */
#include <limits.h>
-#include <stdio.h>
-#include <string.h>
-#include "../../status.h"
-#include "../../util.h"
-#include "blight.h"
-
-size_t
+static size_t
blight(struct Block *b)
{
char path[PATH_MAX];
diff --git a/blocks/linux/blight.h b/blocks/linux/blight.h
@@ -1 +0,0 @@
-size_t blight(struct Block *);
diff --git a/blocks/linux/volume.c b/blocks/linux/volume.c
@@ -2,20 +2,19 @@
#include <alsa/asoundlib.h>
#include <alsa/mixer.h>
#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "../../status.h"
-#include "../../util.h"
-#include "volume.h"
+struct vol_arg {
+ const char *card;
+ const char *sink;
+};
-size_t
+static size_t
volume(struct Block *b)
{
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
snd_mixer_elem_t *elem;
- const struct vol_arg *va = b->arg;
+ struct vol_arg *va = b->arg;
const char *str = "muted";
int notmuted;
diff --git a/blocks/linux/volume.h b/blocks/linux/volume.h
@@ -1,6 +0,0 @@
-struct vol_arg {
- const char *card;
- const char *sink;
-};
-
-size_t volume(struct Block *b);
diff --git a/blocks/mpd.c b/blocks/mpd.c
@@ -1,10 +1,13 @@
/* See LICENSE for license details. */
#include <mpd/client.h>
-#include <stdio.h>
+#include <mpd/tag.h>
-#include "../status.h"
-#include "../util.h"
-#include "mpd.h"
+struct mpd_arg {
+ const char *host;
+ const char *sep;
+ const enum mpd_tag_type *tags;
+ size_t ntags;
+};
static struct mpd_connection *conn;
@@ -32,12 +35,12 @@ check_conn(const char *host)
return open_conn(host);
}
-size_t
+static size_t
mpd_tag(struct Block *b)
{
struct mpd_song *song = NULL;
struct mpd_status *status = NULL;
- const struct mpd_arg *ma = b->arg;
+ struct mpd_arg *ma = b->arg;
size_t i, len = 0;
if ((!conn && open_conn(ma->host) != 0) || check_conn(ma->host) != 0)
diff --git a/blocks/mpd.h b/blocks/mpd.h
@@ -1,11 +0,0 @@
-/* to have access to tag types */
-#include <mpd/tag.h>
-
-struct mpd_arg {
- const char *host;
- const char *sep;
- const enum mpd_tag_type *tags;
- size_t ntags;
-};
-
-size_t mpd_tag(struct Block *b);
diff --git a/blocks/openbsd/battery.c b/blocks/openbsd/battery.c
@@ -1,18 +1,19 @@
/* See LICENSE for license details. */
#include <fcntl.h>
#include <machine/apmvar.h>
-#include <stdio.h>
#include <sys/ioctl.h>
-#include <unistd.h>
-#include "../../status.h"
-#include "../../util.h"
-#include "battery.h"
+struct bat_arg {
+ char *bat; /* BAT name (ignored on OpenBSD) */
+ char *pre; /* prefix for percentages less than thres */
+ char *suf; /* suffix for percentages less than thres */
+ int thres; /* % threshold to consider low (-1 to disable) */
+};
-size_t
+static size_t
batinfo(struct Block *b)
{
- const struct bat_arg ba = b->arg;
+ struct bat_arg ba = b->arg;
char *pre = ba->pre ? ba->pre : "";
char *suf = ba->suf ? ba->suf : "";
struct apm_power_info pi;
diff --git a/blocks/openbsd/battery.h b/blocks/openbsd/battery.h
@@ -1,8 +0,0 @@
-struct bat_arg {
- char *bat; /* BAT name (ignored on OpenBSD) */
- char *pre; /* prefix for percentages less than thres */
- char *suf; /* suffix for percentages less than thres */
- int thres; /* % threshold to consider low (-1 to disable) */
-};
-
-size_t batinfo(struct Block *b);
diff --git a/blocks/script.c b/blocks/script.c
@@ -1,12 +1,5 @@
/* See LICENSE for license details. */
-#include <stdio.h>
-#include <string.h>
-
-#include "../status.h"
-#include "../util.h"
-#include "script.h"
-
-size_t
+static size_t
script(struct Block *b)
{
FILE *fp;
diff --git a/blocks/script.h b/blocks/script.h
@@ -1 +0,0 @@
-size_t script(struct Block *);
diff --git a/build.sh b/build.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+cflags="-O3 -std=c99 -Wall -pedantic"
+cflags="$cflags -D_POSIX_C_SOURCE=200808L"
+cflags="$cflags -I /usr/X11R6/include"
+
+ldflags="-lX11"
+#ldflags="$ldflags -lmpdclient" # needed for blocks/mpd.c
+#ldflags="$ldflags -lasound" # needed for blocks/linux/volume.c
+
+echo "cflags: $cflags"
+echo "ldflags: $ldflags"
+
+cc $cflags status.c $ldflags -o status
diff --git a/config.def.h b/config.def.h
@@ -1,9 +1,9 @@
-#include "blocks/date.h"
-#include "blocks/linux/battery.h"
-#include "blocks/linux/blight.h"
-#include "blocks/linux/volume.h"
-#include "blocks/mpd.h"
-#include "blocks/script.h"
+#include "blocks/date.c"
+#include "blocks/linux/battery.c"
+//#include "blocks/linux/blight.c"
+//#include "blocks/linux/volume.c"
+//#include "blocks/mpd.c"
+//#include "blocks/script.c"
/* update intervals: SEC+NANO gives sleep interval */
/* SEC must be >= 0 and 0 <= NANO <= 999999999 */
@@ -11,15 +11,15 @@
#define INTERVAL_NANO 0
/* mpd_arg.host can be NULL to use the MPD_HOST env variable */
-const enum mpd_tag_type tags[] = { MPD_TAG_TITLE, MPD_TAG_ARTIST };
-const struct mpd_arg ma = { "localhost", "|", tags, 2 };
+// static enum mpd_tag_type tags[] = { MPD_TAG_TITLE, MPD_TAG_ARTIST };
+// static struct mpd_arg ma = { "localhost", "|", tags, 2 };
/* alsa card and sink */
/* card is found with 'aplay -L', default is probably correct */
-const struct vol_arg va = { "default", "Speaker" };
+// static struct vol_arg va = { "default", "Speaker" };
/* check battery.h for info */
-const struct bat_arg ba = { "BAT0", NULL, NULL, -1 };
+static struct bat_arg ba = { "BAT0", NULL, NULL, -1 };
/* status block definitions
*
diff --git a/config.mk b/config.mk
@@ -1,21 +0,0 @@
-SRC =\
- blocks/date.c\
- blocks/linux/battery.c\
- blocks/linux/blight.c\
- blocks/script.c
-# blocks/linux/volume.c: needs: -lasound
-# blocks/linux/volume.c
-# blocks/mpd.c: needs: -lmpdclient
-# blocks/mpd.c
-
-PREFIX = /
-
-X11INC = /usr/X11R6/include
-X11LIB = /usr/X11R6/lib
-
-INCS = -I$(X11INC) -I/usr/local/include
-LIBS = -L$(X11LIB) -L/usr/local/lib -lX11
-
-CPPFLAGS = -D_POSIX_C_SOURCE=200809L
-CFLAGS = -O2 -std=c99 -Wall -pedantic
-LDFLAGS = -s
diff --git a/status.c b/status.c
@@ -7,19 +7,31 @@
#include <unistd.h>
#include <X11/Xlib.h>
-#include "status.h"
-#include "util.h"
-#include "config.h"
-
+#define LEN(a) (sizeof(a) / sizeof(*a))
+#define BLOCKLEN 128
+#define BLOCKPAD 4
#define STATUSLEN ((LEN(blks) - 1) * BLOCKLEN + 1)
-char buf[BLOCKLEN - BLOCKPAD];
-
+struct Block {
+ size_t (*const fn)(struct Block *b);
+ const char *fmt;
+ const int interval;
+ const int signal;
+ const void *arg;
+ char curstr[BLOCKLEN];
+ char prevstr[BLOCKLEN];
+ size_t len;
+};
+
+static char buf[BLOCKLEN - BLOCKPAD];
static Display *dpy;
static int dflag = 0;
static sigset_t blocksigmask;
static struct Block *dirty;
+#include "util.c"
+#include "config.h"
+
static void
terminate(int signo)
{
diff --git a/status.h b/status.h
@@ -1,15 +0,0 @@
-#define BLOCKLEN 128
-#define BLOCKPAD 4
-
-struct Block {
- size_t (*const fn)(struct Block *b);
- const char *fmt;
- const int interval;
- const int signal;
- const void *arg;
- char curstr[BLOCKLEN];
- char prevstr[BLOCKLEN];
- size_t len;
-};
-
-extern char buf[BLOCKLEN - BLOCKPAD];
diff --git a/util.c b/util.c
@@ -1,12 +1,7 @@
/* See LICENSE for license details. */
#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "status.h"
-#include "util.h"
-
-void
+static void
die(const char *errstr, ...)
{
va_list ap;
@@ -17,7 +12,7 @@ die(const char *errstr, ...)
exit(1);
}
-int
+static int
pscanf(const char *path, const char *fmt, ...)
{
FILE *fp;
diff --git a/util.h b/util.h
@@ -1,4 +0,0 @@
-#define LEN(a) (sizeof(a) / sizeof(*a))
-
-void die(const char *errstr, ...);
-int pscanf(const char *path, const char *fmt, ...);