foxygit / sxbar Log in
commit a79e1336372cf17eb2a87cf51632253c799399a6
Author:     uint23 <abhinav.prsai@gmail.com>
AuthorDate: Thu Apr 10 00:55:47 2025 +0100
Commit:     uint23 <abhinav.prsai@gmail.com>
CommitDate: Thu Apr 10 00:55:47 2025 +0100

    Make an x window

    fix dimentions,
    add atom support,
    add proper stats
    remove dbg tmrw
---
 Makefile     |  27 +++++++++
 src/config.h |  13 ++++
 src/draw.c   |   0
 src/draw.h   |   6 ++
 src/sxbar.c  | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/utils.c  |  16 +++++
 src/utils.h  |   8 +++
 7 files changed, 264 insertions(+)

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..15e6fec
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,27 @@
+# Flags
+CC = gcc
+CFLAGS = -Wall -O2 -g `pkg-config --cflags x11 xft`
+LDFLAGS = `pkg-config --libs x11 xft`
+
+# Dirs
+SRC_DIR = src
+OBJ_DIR = obj
+BIN = sxbar
+
+# Source and object files
+SRC = $(wildcard $(SRC_DIR)/*.c)
+OBJ = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC))
+
+.PHONY: all clean
+
+all: $(BIN)
+
+$(BIN): $(OBJ)
+	$(CC) -o $@ $^ $(LDFLAGS)
+
+$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
+	@mkdir -p $(OBJ_DIR)
+	$(CC) $(CFLAGS) -c $< -o $@
+
+clean:
+	rm -rf $(OBJ_DIR) $(BIN)
diff --git a/src/config.h b/src/config.h
new file mode 100644
index 0000000..e5517df
--- /dev/null
+++ b/src/config.h
@@ -0,0 +1,13 @@
+// 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
+static unsigned int bdrw	= 1;	// Border width of the bar. Set to 0 for no border
+
+static const char colfg[]	= "#ABCDEF";	// Foreground colour
+static const char colbg[]	= "#090425";	// Background colour
+static const char colbdr[]	= "#090425";	// Border colour
+// static const char *font		= "terminus";
+// static const char *tags[]	= { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
diff --git a/src/draw.c b/src/draw.c
new file mode 100644
index 0000000..e69de29
diff --git a/src/draw.h b/src/draw.h
new file mode 100644
index 0000000..5ef1f51
--- /dev/null
+++ b/src/draw.h
@@ -0,0 +1,6 @@
+#ifndef DRAW_H
+#define DRAW_H
+
+static void drawbar();
+
+#endif
diff --git a/src/sxbar.c b/src/sxbar.c
new file mode 100644
index 0000000..1e498b5
--- /dev/null
+++ b/src/sxbar.c
@@ -0,0 +1,194 @@
+#include <X11/X.h>
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+#include "config.h"
+#include "utils.h"
+
+// Defs
+#define VERSION "0.1.0"
+#define LICENSE	"This software is under the MIT License.\n\
+View the 'LICENSE' file for more info"
+#define AUTHOR	"(C) Abhinav Prasai 2025"
+
+// Structs
+
+typedef union {
+
+} Args;
+
+typedef struct {
+	Window w;
+	int tag;
+} Client;
+
+// Function declerations
+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 Display 		*dpy;
+static Window 		barwin, 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,
+		barw, barh,
+		bdrw,
+		BlackPixel(dpy, screen),
+		WhitePixel(dpy, screen)
+	);
+	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);
+}
+
+static void
+run(void)
+{
+	XEvent ev;
+	createwin();
+
+	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;
+		}
+
+		usleep(SECOND(1));
+	}
+}
+
+void
+setup(void)
+{
+	dpy = XOpenDisplay(NULL);
+	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();
+}
+
+void
+sethint(void)
+{
+	XClassHint *classhint = XAllocClassHint();
+	if (classhint) {
+		classhint->res_name  = "sxbar";
+		classhint->res_class = "sxbar";
+		XSetClassHint(dpy, barwin, classhint);
+		XFree(classhint);
+	} else {
+		death("Unable to set classhint");
+	}
+}
+
+int
+main(int ac, char **av)
+{
+	if (ac > 1) {
+		if (strcmp(av[1], "-v") == 0 || strcmp(av[1], "--version") == 0)
+			death("sxbar ver. %s\n%s\n%s", VERSION, LICENSE, AUTHOR);
+		if (strcmp(av[1], "-h") == 0 || strcmp(av[1], "--help") == 0)
+			death("USEAGE:\n\t[-v || --version] is the only command..");
+		else
+			death("USEAGE:\n\t[-v || --version] is the only command..");
+	} else {
+		setup();
+		run();
+		cleanup();
+	}
+	return 0;
+}
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..798782f
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+void
+death(const char *fmt, ...)
+{
+	va_list args;
+	va_start(args, fmt);
+
+	vfprintf(stderr, fmt, args);
+	va_end(args);
+
+	fprintf(stderr, "\n");
+	exit(EXIT_FAILURE);
+}
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..185e07f
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,8 @@
+#ifndef UTILS_H
+#define UTILS_H
+#define SECOND(T)	(T * 1000000)
+#define MSECOND(T)	(T * 1000)
+
+void death(char* message, ...);
+
+#endif