From 75a1408e9d4e54af9695bc4eca650b35c22bcff2 Mon Sep 17 00:00:00 2001 From: awy Date: Thu, 11 Sep 2025 17:35:32 +0300 Subject: BIG HAPPENING: notifications and spawn functionality --- Makefile | 5 ++++- lib/util.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ lib/util.h | 3 +++ src/stclock.c | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d8239c0..824b81d 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ BINDIR := $(HOME)/.local/bin/statusbar SRCS := $(wildcard $(SRCDIR)/*.c) PROGS := $(patsubst $(SRCDIR)/%.c,$(BINDIR)/%,$(SRCS)) +NOTIFY_CFLAGS := $(shell pkg-config --cflags libnotify) +NOTIFY_LIBS := $(shell pkg-config --libs libnotify) + .PHONY: all clean all: $(BINDIR) $(PROGS) @@ -27,7 +30,7 @@ $(BINDIR)/stweath: $(SRCDIR)/stweath.c | $(BINDIR) $(CC) $(CFLAGS) -o $@ lib/util.c lib/cjson/cJSON.c $< -lcurl $(BINDIR)/%: $(SRCDIR)/%.c | $(BINDIR) - $(CC) $(CFLAGS) -o $@ lib/util.c $< + $(CC) $(CFLAGS) $(NOTIFY_CFLAGS) -o $@ lib/util.c $< $(NOTIFY_LIBS) clean: rm -f $(PROGS) diff --git a/lib/util.c b/lib/util.c index 1fe30da..0c22adf 100644 --- a/lib/util.c +++ b/lib/util.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "util.h" @@ -23,3 +24,48 @@ die(const char *fmt, ...) exit(1); } + +void +sendnotif(const char *appname, const char *title, const char *body) +{ + notify_init(appname); + + NotifyNotification *n; + n = notify_notification_new(title, body, NULL); + + notify_notification_show(n, NULL); + + g_object_unref(G_OBJECT(n)); + notify_uninit(); +} + +int +getbtnint(const char *blkbtn) +{ + char *endptr; + int errno; + long val; + + errno = 0; + + if (blkbtn == NULL) + return 0; + + val = strtol(blkbtn, &endptr, 10); + + if (errno == 0 && *endptr == '\0' && val >= 1 && val <= 8) + return (int)val; + + return 0; +} + +void +spawn(const char *const argv[]) +{ + if (fork() == 0) { + dup2(STDERR_FILENO, STDOUT_FILENO); + setsid(); + execvp(argv[0], (char * const *)argv); + die("spawn %s failed:", (argv)[0]); + } +} diff --git a/lib/util.h b/lib/util.h index 9ecc499..84c5d36 100644 --- a/lib/util.h +++ b/lib/util.h @@ -1 +1,4 @@ void die(const char *, ...); +void sendnotif(const char *, const char *, const char *); +int getbtnint(const char *blkbtn); +void spawn(const char *const argv[]); diff --git a/src/stclock.c b/src/stclock.c index 22dae6a..ef63fe6 100644 --- a/src/stclock.c +++ b/src/stclock.c @@ -14,17 +14,56 @@ License along with stclock. If not, see . */ +#include #include #include #include "../lib/util.h" +void +buttonhandler() +{ + char *term; + char *env; + int button; + + button = 0; + if ((env = getenv("BLOCK_BUTTON"))) + button = getbtnint(env); + + + if (!(term = getenv("TERMINAL"))) + term = "footclient"; + + const char *termcmd[] = { term, "-e", "calcurse", NULL }; + + switch (button) { + case 1: + sendnotif("stclock", "This Month", + "there should be calendar but you know how we doing"); + break; + case 2: + spawn(termcmd); + break; + case 3: + sendnotif("stclock", " Time/date module", + "- Left click to show upcoming \ +appointments for the next three days\ +via `calcurse -d3` and show the month via `cal`\n\ +- Middle click opens calcurse if installed"); + break; + default: break; + } +} + int main(void) { time_t t; char buf[64]; + buttonhandler(); + t = time(NULL); if (!strftime(buf, sizeof(buf), "%F %T", localtime(&t))) die("strftime: Result string exceeds buffer size"); -- cgit v1.2.3