herbe

[fork] notifications
Log | Files | Refs | README | LICENSE

commit 36c03fec6648ce7ed84e0b32c33880a71d180f72
parent 2c2741a9cc571b0555390991583075b0e392fcdc
Author: Samuel Dudik <samuel.dudik@gmail.com>
Date:   Sat,  8 Aug 2020 17:57:24 +0200

Add dynamic space allocation for notification body

Diffstat:
Mconfig.h | 9+++++----
Mherbe.c | 42+++++++++++++++++++++++++++++++-----------
2 files changed, 36 insertions(+), 15 deletions(-)

diff --git 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 @@ -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