commit f7667fd8111f8ab68c6aa20e1343eedb08885ea3
Author: uint23 <abhinav.prsai@gmail.com>
AuthorDate: Thu Apr 10 12:39:11 2025 +0100
Commit: uint23 <abhinav.prsai@gmail.com>
CommitDate: Thu Apr 10 12:39:11 2025 +0100
bar now works properly
proper bar padding, border etc logic
next: add proper emwh or wtv support so it works on
window managers
---
src/config.h | 19 +++++++--
src/sxbar.c | 128 +++++++++++++++++++----------------------------------------
2 files changed, 56 insertions(+), 91 deletions(-)
diff --git a/src/config.h b/src/config.h
index e5517df..a7795d8 100644
--- a/src/config.h
+++ b/src/config.h
@@ -1,9 +1,20 @@
+// MIT License
+// See LICENSE for more information
// Config file - see, it's not that difficult
-static unsigned int barw = 500; // Bar width
-static unsigned int barh = 10; // Bar height
-static unsigned int padh = 10; // Horizontal bar padding
-static unsigned int padv = 10; // Vertical bar padding
+#define MAXPADV 500 // The vertical padding of the bar,
+#define MAXPADH 500 // The horizonral padding of the bar,
+#define MAXBARH 40 // The max height of the bar, this shouldn't really be more than 40
+#define MAXBDRW 10 // The max border width of the bar
+
+// Keybindings
+#define TOGGLE { ModMask|ShiftMask, XK_b, {.v=toggle}}
+
+static unsigned int topbar = 0; // Bar is at top (1) or bottom (0)
+static unsigned int centre = 1; // If using padding, this will centre your bar
+static unsigned int barh = 20; // Bar height
+static unsigned int padh = 400; // Horizontal bar padding
+static unsigned int padv = 20; // Vertical bar padding
static unsigned int bdrw = 1; // Border width of the bar. Set to 0 for no border
static const char colfg[] = "#ABCDEF"; // Foreground colour
diff --git a/src/sxbar.c b/src/sxbar.c
index 1e498b5..8f964f1 100644
--- a/src/sxbar.c
+++ b/src/sxbar.c
@@ -15,7 +15,6 @@ View the 'LICENSE' file for more info"
#define AUTHOR "(C) Abhinav Prasai 2025"
// Structs
-
typedef union {
} Args;
@@ -29,70 +28,44 @@ typedef struct {
static void createwin(void);
static void cleanup(void);
static void run(void);
-static void sethint(void);
static void setup(void);
// Variables
-static unsigned int screenw;
-static unsigned int screenh;
-static unsigned int screen;
-static GC gc;
+static unsigned int scrw;
+static unsigned int scrh;
+static unsigned int scr;
+static unsigned int barx;
+static unsigned int bary;
+static unsigned int barw;
static Display *dpy;
-static Window barwin, root;
+static Window barwin;
+static Window root;
// Functions
-static void
-debug_config(void)
-{
- fprintf(stderr, "Debug Configuration:\n");
- fprintf(stderr, " screenw: %u\n", screenw);
- fprintf(stderr, " screenh: %u\n", screenh);
- fprintf(stderr, " barw: %u\n", barw);
- fprintf(stderr, " barh: %u\n", barh);
- fprintf(stderr, " padv: %d\n", padv);
- fprintf(stderr, " padh: %d\n", padh);
- fprintf(stderr, " bdrw: %u\n", bdrw);
-}
-
static void
createwin(void)
{
- unsigned int width = (barw > 0) ? barw : 100;
- unsigned int height = (barh > 0) ? barh : 20;
- int x_pos = (padh >= 0) ? padh : 0;
- int y_pos = (padv >= 0) ? padv : 0;
- unsigned int border_width = (bdrw > 0) ? bdrw : 1;
-
- // Debug
- fprintf(stderr, "Creating window with:\n");
- fprintf(stderr, " Position: (%d, %d)\n", x_pos, y_pos);
- fprintf(stderr, " Size: %u x %u\n", width, height);
- fprintf(stderr, " Border width: %u\n", border_width);
-
barwin = XCreateSimpleWindow(
dpy,
root,
- padv, padh,
+ barx, bary,
barw, barh,
bdrw,
- BlackPixel(dpy, screen),
- WhitePixel(dpy, screen)
+ BlackPixel(dpy, scr),
+ WhitePixel(dpy, scr)
);
+
XSelectInput(dpy, barwin,
ExposureMask | KeyPressMask
);
XMapWindow(dpy, barwin);
- sethint();
- gc = XCreateGC(dpy, barwin, 0, NULL);
- XSetForeground(dpy, gc, BlackPixel(dpy, screen));
XRaiseWindow(dpy, barwin);
}
static void
cleanup(void)
{
- XFreeGC(dpy, gc);
XDestroyWindow(dpy, barwin);
XCloseDisplay(dpy);
}
@@ -106,23 +79,6 @@ run(void)
for(;;) {
XNextEvent(dpy, &ev);
if (ev.type == Expose) {
- // Get current time
- time_t now = time(NULL);
- struct tm *tm_info = localtime(&now);
- char time_str[100];
- strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", tm_info);
-
- // Add username for demonstration
- char status_text[256];
- snprintf(status_text, sizeof(status_text), "User: uint23 | %s", time_str);
-
- // Clear the window (redraw the background)
- XClearWindow(dpy, barwin);
-
- // Draw text
- XDrawString(dpy, barwin, gc, 10, 15, status_text, strlen(status_text));
-
- // Flush changes to display
XFlush(dpy);
} else if (ev.type == KeyPress) {
return;
@@ -139,40 +95,38 @@ setup(void)
if (dpy == 0)
death("Failed to open display.");
- screen = DefaultScreen(dpy);
- screenw = DisplayWidth(dpy, screen);
- screenh = DisplayHeight(dpy, screen);
- root = RootWindow(dpy, screen);
-
- if (barw == 0)
- barw = screenw;
- if (barh > screenh)
- barh = screenh;
- if (barw > screenw)
- barw = screenw;
- if (2 * padh >= barw)
- padh = 0;
-
- barw -= padh * 2;
-
- if (bdrw > 10)
- bdrw = 1;
-
- debug_config();
-}
+ scr = DefaultScreen(dpy);
+ scrw = DisplayWidth(dpy, scr);
+ scrh = DisplayHeight(dpy, scr);
+ root = RootWindow(dpy, scr);
+
+ if (topbar) {
+ if (padv) bary = padv;
+ else bary = 0;
+ } else {
+ if (padv) bary = scrh - barh - padv;
+ else bary = scrh - barh;
+ }
-void
-sethint(void)
-{
- XClassHint *classhint = XAllocClassHint();
- if (classhint) {
- classhint->res_name = "sxbar";
- classhint->res_class = "sxbar";
- XSetClassHint(dpy, barwin, classhint);
- XFree(classhint);
+ if (padh) {
+ if (centre) {
+ barx = padh;
+ barw = scrw - (padh * 2);
+ } else {
+ barx = 0;
+ barw = scrw - padh;
+ }
} else {
- death("Unable to set classhint");
+ barx = 0;
+ barw = scrw;
}
+
+ if (barh > MAXBARH)
+ barh = MAXBARH;
+ if (padv > MAXPADV)
+ padv = MAXPADV;
+ if (padh > MAXPADH)
+ padh = MAXPADH;
}
int