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
#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);
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);
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);
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++)
{
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';
}
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