diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | lib/util.c | 46 | ||||
-rw-r--r-- | lib/util.h | 3 | ||||
-rw-r--r-- | src/stclock.c | 39 |
4 files changed, 92 insertions, 1 deletions
@@ -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) @@ -2,6 +2,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <libnotify/notify.h> #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]); + } +} @@ -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 <https://www.gnu.org/licenses/>. */ +#include <stdlib.h> #include <time.h> #include <stdio.h> #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"); |