commit b54509d46107eb7b64f4913fc378934034c4735d
parent bf81f5736fc3c8a7c3c22fc70b0ccfa147130454
Author: Michael Constantine Dimopoulos <mk@mcdim.xyz>
Date: Sun, 5 Nov 2023 03:37:49 +0000
Cleaned up code; dynamic width bug
Diffstat:
M | sxalert.c | | | 113 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
1 file changed, 58 insertions(+), 55 deletions(-)
diff --git a/sxalert.c b/sxalert.c
@@ -36,7 +36,7 @@ usage(const char *bin)
}
static void
-help(void) /* TODO: update */
+help(void)
{
printf("sXalert %s\n", VERSION);
printf("Alert utility for X\n\n");
@@ -79,6 +79,61 @@ get_max(int arr[], int len)
return max;
}
+static int
+get_width(Display *dpy, XftFont *font, char **lines, int length)
+{
+ XGlyphInfo extents;
+ int width = 0;
+ if (length > 0) {
+ int width_lines[length];
+ int j = 0;
+ for (int i = 0; i < length; i++) {
+ XftTextExtentsUtf8(dpy, font, (XftChar8*)lines[i], strlen(lines[i]), &extents);
+ width_lines[i] = extents.xOff;
+ j++;
+ }
+
+ int text_width = get_max(width_lines, length);
+ width = (text_x_padding * 2) + text_width;
+ }
+
+ if (width < min_width)
+ return min_width;
+ if (width > max_width)
+ return max_width;
+}
+
+
+static void
+write_text(Display *dpy, XftDraw *draw, XftColor color, XftFont *font, int text_height, char **lines, int length)
+{
+ while (1) {
+ struct pollfd pfd = {
+ .fd = ConnectionNumber(dpy),
+ .events = POLLIN,
+ };
+ Bool pending = XPending(dpy) > 0 || poll(&pfd, 1, duration) > 0;
+
+ if (!pending)
+ break;
+
+ XEvent ev;
+
+ XNextEvent(dpy, &ev);
+
+ if (ev.type == Expose) {
+ int spacing = text_height * 2;
+ for (int i = 0; i < length; i++) {
+ if (length != 0)
+ XftDrawStringUtf8(draw, &color, font, text_x_padding, spacing, (XftChar8 *)lines[i], strlen(lines[i]));
+ spacing += text_height * 2;
+ }
+ } else if (ev.type == ButtonPress) { /*&& ev.xbutton.button == 1) { */
+ break;
+ }
+ }
+}
+
static void
draw(int border, int duration, char **lines, int length)
{
@@ -100,35 +155,11 @@ draw(int border, int duration, char **lines, int length)
if (!XftColorAllocName(dpy, visual, cmap, text_color_pnd, &color))
die("Cannot allocate Xft color\n", EXIT_FAILURE);
- printf("ok\n");
- int width;
- if (length > 0) {
- int width_lines[length];
- int j = 0;
- for (int i = 0; i < length; i++) {
- XftTextExtentsUtf8(dpy, font, (XftChar8*)lines[i], strlen(lines[i]), &extents);
- width_lines[i] = extents.xOff;
- printf("text_width[%d]=%d\n", j, width_lines[j]);
- j++;
- }
+ int width = get_width(dpy, font, lines, length);
- int text_width = get_max(width_lines, length);
- printf("text_width=%d\n", text_width);
- width = (text_x_padding * 2) + text_width;
- } else {
- width = 0;
- }
- printf("ok\n");
- if (width < min_width)
- width = min_width;
- if (width > max_width)
- width = max_width;
-
- printf("susok\n");
int text_height, height;
if (length > 0) {
XftTextExtentsUtf8(dpy, font, (XftChar8*)lines[0], strlen(lines[0]), &extents);
- printf("susok2\n");
text_height = extents.height;
height = length * (text_height * 2) + text_height;
} else {
@@ -138,42 +169,15 @@ draw(int border, int duration, char **lines, int length)
/* TODO: calculate position dynamically */
Window win = XCreateSimpleWindow(dpy, RootWindow(dpy, scr), 1500, 50, width, height, border, hex2int(border_color), hex2int(bg_color));
- /* make window fixed */
XSetWindowAttributes attributes;
attributes.override_redirect = True;
XChangeWindowAttributes(dpy, win, CWOverrideRedirect, &attributes);
-
XSelectInput(dpy, win, ExposureMask | KeyPressMask);
XMapWindow(dpy, win);
XftDraw *draw = XftDrawCreate(dpy, win, visual, cmap);
- while (1) {
- struct pollfd pfd = {
- .fd = ConnectionNumber(dpy),
- .events = POLLIN,
- };
- Bool pending = XPending(dpy) > 0 || poll(&pfd, 1, duration) > 0;
-
- if (!pending)
- break;
-
- XEvent ev;
-
- XNextEvent(dpy, &ev);
-
- if (ev.type == Expose) {
- int spacing = text_height * 2;
- for (int i = 0; i < length; i++) {
- if (length != 0)
- XftDrawStringUtf8(draw, &color, font, text_x_padding, spacing, (XftChar8 *)lines[i], strlen(lines[i]));
- spacing += text_height * 2;
- }
- } else if (ev.type == ButtonPress) { //&& ev.xbutton.button == 1) {
- printf("Event: mouse xbutton.butotn = 1\n LEFT CLICK\n");
- break; // Exit the loop if a key is pressed
- }
- }
+ write_text(dpy, draw, color, font, text_height, lines, length);
/* cleanup */
XftColorFree(dpy, visual, cmap, &color);
@@ -217,7 +221,6 @@ main(int argc, char **argv)
int lines_len=argc-optind;
char** lines = argv + optind; /* get lines to print */
- printf("ok\n");
draw(border_width, duration, lines, lines_len);