From 41deab98a5c0ac7162cd2fe21a786e022cc53670 Mon Sep 17 00:00:00 2001 From: clsr Date: Fri, 2 Jul 2021 03:48:06 +0000 Subject: Add dwmclock-print --- Makefile | 19 ++++++++++++++----- README.md | 8 ++++++++ dwmclock.c | 15 ++++++++++++++- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 5133980..518f30c 100644 --- a/Makefile +++ b/Makefile @@ -20,17 +20,26 @@ all: $(SRC) $(MAIN) $(MAIN): $(OBJ) $(CC) $(LDFLAGS) -o $@ $(OBJ) +$(MAIN)-print: CFLAGS+=-DPRINT +$(MAIN)-print: LDFLAGS= +$(MAIN)-print: $(OBJ:.o=-print.o) + $(CC) $(LDFLAGS) -o $@ $(OBJ:.o=-print.o) + +%-print.o: + $(CC) -c $(CFLAGS) $(@:-print.o=.c) -o $@ + .c.o: - $(CC) -c $(CFLAGS) $< -o $@ -O2 + $(CC) -c $(CFLAGS) $< -o $@ clean: - -rm -f $(MAIN) $(OBJ) + -rm -f $(MAIN) $(OBJ) $(MAIN)-print $(OBJ:.o=-print.o) install: $(MAIN) - mkdir -p $(DESTDIR)$(PREFIX)/bin - cp $(MAIN) $(DESTDIR)$(PREFIX)/bin/$(MAIN) + install -d $(DESTDIR)$(PREFIX)/bin/ + install -m 755 $(MAIN) $(DESTDIR)$(PREFIX)/bin/ + [ -f $(MAIN)-print ] && install -m 755 $(MAIN)-print $(DESTDIR)$(PREFIX)/bin/ uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/$(MAIN) + rm -f $(DESTDIR)$(PREFIX)/bin/$(MAIN) $(DESTDIR)$(PREFIX)/bin/$(MAIN)-print .PHONY: all clean install uninstall diff --git a/README.md b/README.md index bf50b36..b522dd5 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ Compile it: make +Optionally also compile a version that prints time to stdout instead of updating the X root window name: + + make dwmclock-print + Install dwmclock (needs to be run as root if installing to `/usr` or `/usr/local`): make install @@ -36,3 +40,7 @@ Usage Amp it off in your `~/.xinitrc`: dwmclock & + +Use it in Sway bar config: + + status_command dwmclock-print diff --git a/dwmclock.c b/dwmclock.c index 95a0db0..4bc043c 100644 --- a/dwmclock.c +++ b/dwmclock.c @@ -7,7 +7,9 @@ #include #include #include +#ifndef PRINT #include +#endif /* PRINT */ int stop = 0; @@ -78,10 +80,12 @@ int main(void) char *clock = name; #endif /* BATTERY */ struct sigaction act; +#ifndef PRINT Display *disp; int screen; Window root; char *origname = NULL; +#endif /* !PRINT */ act.sa_handler = handler; act.sa_flags = 0; @@ -91,6 +95,7 @@ int main(void) sigaction(SIGQUIT, &act, NULL); sigaction(SIGTERM, &act, NULL); +#ifndef PRINT if (!(disp = XOpenDisplay(NULL))) { return EXIT_FAILURE; } @@ -100,9 +105,10 @@ int main(void) } XFetchName(disp, root, &origname); - fclose(stdin); fclose(stdout); fclose(stderr); +#endif /* !PRINT */ + fclose(stdin); #ifdef BATTERY for (i=0; !stop; ++i) { @@ -146,8 +152,13 @@ int main(void) tm = localtime(&now); strftime(clock, 9, "%H:%M:%S", tm); +#ifdef PRINT + printf("%s\n", name); + fflush(stdout); +#else XStoreName(disp, root, name); XFlush(disp); +#endif /* PRINT */ #ifdef EXACT_SLEEP ts.tv_sec = 0; @@ -158,10 +169,12 @@ int main(void) #endif /* EXACT_SLEEP */ } +#ifndef PRINT if (origname) { XStoreName(disp, root, origname); } XCloseDisplay(disp); +#endif /* !PRINT */ return EXIT_SUCCESS; } -- cgit