From 330056d95a2e223edc16e457781ff5e223abb516 Mon Sep 17 00:00:00 2001 From: Suleyman Farajli Date: Tue, 4 Nov 2025 18:31:15 +0400 Subject: feat: add notification to bar --- config.h | 3 ++- dwm.c | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/config.h b/config.h index e94c31c..73600e3 100644 --- a/config.h +++ b/config.h @@ -4,6 +4,7 @@ #define BROWSER "firefox" #define TERMINAL "st" +#define NOTIFICATION_FILE "/tmp/noti.txt" /* appearance */ static const unsigned int borderpx = 2; /* border pixel of windows */ @@ -170,5 +171,5 @@ static const Button buttons[] = { /* trigger signals using `xsetroot -name "fsignal:"` */ static Signal signals[] = { /* signum function argument */ - { 1, setlayout, {.v = 0} }, + { 1, updatenotification, {.v = 0} }, }; diff --git a/dwm.c b/dwm.c index 091650c..7a6b626 100644 --- a/dwm.c +++ b/dwm.c @@ -246,12 +246,13 @@ static int xerror(Display *dpy, XErrorEvent *ee); static int xerrordummy(Display *dpy, XErrorEvent *ee); static int xerrorstart(Display *dpy, XErrorEvent *ee); static void zoom(const Arg *arg); - +static void updatenotification(const Arg *arg); static void focusmaster(const Arg *arg); /* variables */ static const char broken[] = "broken"; static char stext[256]; +static char notification_text[100]; static int screen; static int sw, sh; /* X display screen geometry width, height */ static int bh; /* bar height */ @@ -766,10 +767,20 @@ drawbar(Monitor *m) } if ((w = m->ww - tw - x) > bh) { - drw_setscheme(drw, scheme[SchemeNorm]); - drw_rect(drw, x, 0, w, bh, 1, 1); - } + if (m == selmon) { /* Only draw text on the selected monitor */ + int mid = (m->ww - (int)TEXTW(notification_text)) / 2 - x; + mid = mid >= lrpad / 2 ? mid : lrpad / 2; + drw_setscheme(drw, scheme[SchemeNorm]); + drw_text(drw, x, 0, w, bh, mid, notification_text, 0); + } else { + drw_setscheme(drw, scheme[SchemeNorm]); + drw_rect(drw, x, 0, w, bh, 1, 1); + } + /* Keep floating indicator for the selected monitor */ + if (m == selmon && m->sel && m->sel->isfloating) + drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0); + } drw_map(drw, m->barwin, 0, 0, m->ww, bh); } @@ -782,6 +793,26 @@ drawbars(void) drawbar(m); } +void +updatenotification(const Arg *arg) +{ + ssize_t size = 100; + FILE *fp = fopen(NOTIFICATION_FILE, "r"); + if (fp == NULL) { + notification_text[0] = '\0'; + return; + } + + if (fgets(notification_text, size, fp) == NULL) { + notification_text[0] = '\0'; + return; + } else { + notification_text[strcspn(notification_text, "\n")] = '\0'; + } + fclose(fp); + drawbar(selmon); +} + void enternotify(XEvent *e) { -- cgit v1.2.3