Add dynamic space allocation for notification body
authorSamuel Dudik <redacted>
Sat, 8 Aug 2020 15:57:24 +0000 (17:57 +0200)
committerSamuel Dudik <redacted>
Sat, 8 Aug 2020 15:57:24 +0000 (17:57 +0200)
config.h
herbe.c

index c140dd412c29e530acd84d5bb45a95568ec67088..6611bad833cad06a94fb471b3eeb67b5b9efe6c7 100644 (file)
--- a/config.h
+++ b/config.h
@@ -1,16 +1,16 @@
 static const char *background_color = "#3e3e3e";
 static const char *border_color = "#ececec";
 static const char *font_color = "#ececec";
-static const char *font_pattern = "Inconsolata:style=Medium:size=13";
+static const char *font_pattern = "Inconsolata:style=Medium:size=12";
 static const unsigned line_spacing = 5;
 static const unsigned int padding = 15;
 
-static const unsigned int width = 400;
+static const unsigned int width = 450;
 static const unsigned int border_size = 2;
 static const unsigned int pos_x = 30;
-static const unsigned int pos_y = 50;
+static const unsigned int pos_y = 60;
 
 enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
 enum corners corner = TOP_RIGHT;
 
-static const unsigned int duration = 60; /* in seconds */
+static const unsigned int duration = 5; /* in seconds */
\ No newline at end of file
diff --git a/herbe.c b/herbe.c
index e219301540223496e70a4d006887e322551bfdb2..503e6f26356af9986dcd697c47777534e99b71eb 100644 (file)
--- a/herbe.c
+++ b/herbe.c
@@ -5,12 +5,23 @@
 #include <signal.h>
 #include <unistd.h>
 #include <string.h>
+#include <stdarg.h>
 
 #include "config.h"
 
 Display *display;
 Window window;
 
+static void die(const char *format, ...)
+{
+       va_list ap;
+       va_start(ap, format);
+       vfprintf(stderr, format, ap);
+       fprintf(stderr, "\n");
+       va_end(ap);
+       exit(EXIT_FAILURE);
+}
+
 int get_max_len(char *body, XftFont *font, int max_text_width)
 {
        int body_len = strlen(body);
@@ -53,10 +64,7 @@ void expire()
 int main(int argc, char *argv[])
 {
        if (argc == 1)
-       {
-               fprintf(stderr, "Usage: herbe body\n");
-               exit(EXIT_FAILURE);
-       }
+               die("Usage: %s body", argv[0]);
 
        signal(SIGALRM, expire);
        alarm(duration);
@@ -64,10 +72,7 @@ int main(int argc, char *argv[])
        display = XOpenDisplay(0);
 
        if (display == 0)
-       {
-               fprintf(stderr, "Cannot open display\n");
-               exit(EXIT_FAILURE);
-       }
+               die("Cannot open display");
 
        int screen = DefaultScreen(display);
        Visual *visual = DefaultVisual(display, screen);
@@ -89,8 +94,10 @@ int main(int argc, char *argv[])
 
        int num_of_lines = 0;
        int max_text_width = width - 2 * padding;
-       // TODO replace hard-coded size 100
-       char words[100][max_text_width / font->max_advance_width + 2];
+       int words_size = 5;
+       char **words = malloc(words_size * sizeof(char *));
+       if (!words)
+               die("malloc failed");
 
        for (int i = 1; i < argc; i++)
        {
@@ -98,6 +105,15 @@ int main(int argc, char *argv[])
 
                for (unsigned int eol = get_max_len(body, font, max_text_width); eol <= strlen(body) && eol; body += eol, num_of_lines++, eol = get_max_len(body, font, max_text_width))
                {
+                       if (words_size <= num_of_lines)
+                       {
+                               words = realloc(words, (words_size += 5) * sizeof(char *));
+                               if (!words)
+                                       die("malloc failed");
+                       }
+                       words[num_of_lines] = malloc((eol + 1) * sizeof(char));
+                       if (!words[num_of_lines])
+                               die("malloc failed");
                        strncpy(words[num_of_lines], body, eol);
                        words[num_of_lines][eol] = '\0';
                }
@@ -139,10 +155,14 @@ int main(int argc, char *argv[])
                        break;
        }
 
+       for (int i = 0; i < num_of_lines; i++)
+               free(words[i]);
+
+       free(words);
        XftDrawDestroy(draw);
        XftColorFree(display, visual, colormap, &color);
        XftFontClose(display, font);
        XCloseDisplay(display);
 
-       return EXIT_SUCCESS;
+       exit(EXIT_SUCCESS);
 }
\ No newline at end of file