Add dynamic space allocation for notification body
This commit is contained in:
parent
2c2741a9cc
commit
36c03fec66
2 changed files with 35 additions and 15 deletions
8
config.h
8
config.h
|
@ -1,16 +1,16 @@
|
||||||
static const char *background_color = "#3e3e3e";
|
static const char *background_color = "#3e3e3e";
|
||||||
static const char *border_color = "#ececec";
|
static const char *border_color = "#ececec";
|
||||||
static const char *font_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 line_spacing = 5;
|
||||||
static const unsigned int padding = 15;
|
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 border_size = 2;
|
||||||
static const unsigned int pos_x = 30;
|
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 { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
|
||||||
enum corners corner = TOP_RIGHT;
|
enum corners corner = TOP_RIGHT;
|
||||||
|
|
||||||
static const unsigned int duration = 60; /* in seconds */
|
static const unsigned int duration = 5; /* in seconds */
|
42
herbe.c
42
herbe.c
|
@ -5,12 +5,23 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
Display *display;
|
Display *display;
|
||||||
Window window;
|
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 get_max_len(char *body, XftFont *font, int max_text_width)
|
||||||
{
|
{
|
||||||
int body_len = strlen(body);
|
int body_len = strlen(body);
|
||||||
|
@ -53,10 +64,7 @@ void expire()
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
{
|
die("Usage: %s body", argv[0]);
|
||||||
fprintf(stderr, "Usage: herbe body\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
signal(SIGALRM, expire);
|
signal(SIGALRM, expire);
|
||||||
alarm(duration);
|
alarm(duration);
|
||||||
|
@ -64,10 +72,7 @@ int main(int argc, char *argv[])
|
||||||
display = XOpenDisplay(0);
|
display = XOpenDisplay(0);
|
||||||
|
|
||||||
if (display == 0)
|
if (display == 0)
|
||||||
{
|
die("Cannot open display");
|
||||||
fprintf(stderr, "Cannot open display\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
int screen = DefaultScreen(display);
|
int screen = DefaultScreen(display);
|
||||||
Visual *visual = DefaultVisual(display, screen);
|
Visual *visual = DefaultVisual(display, screen);
|
||||||
|
@ -89,8 +94,10 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
int num_of_lines = 0;
|
int num_of_lines = 0;
|
||||||
int max_text_width = width - 2 * padding;
|
int max_text_width = width - 2 * padding;
|
||||||
// TODO replace hard-coded size 100
|
int words_size = 5;
|
||||||
char words[100][max_text_width / font->max_advance_width + 2];
|
char **words = malloc(words_size * sizeof(char *));
|
||||||
|
if (!words)
|
||||||
|
die("malloc failed");
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++)
|
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))
|
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);
|
strncpy(words[num_of_lines], body, eol);
|
||||||
words[num_of_lines][eol] = '\0';
|
words[num_of_lines][eol] = '\0';
|
||||||
}
|
}
|
||||||
|
@ -139,10 +155,14 @@ int main(int argc, char *argv[])
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < num_of_lines; i++)
|
||||||
|
free(words[i]);
|
||||||
|
|
||||||
|
free(words);
|
||||||
XftDrawDestroy(draw);
|
XftDrawDestroy(draw);
|
||||||
XftColorFree(display, visual, colormap, &color);
|
XftColorFree(display, visual, colormap, &color);
|
||||||
XftFontClose(display, font);
|
XftFontClose(display, font);
|
||||||
XCloseDisplay(display);
|
XCloseDisplay(display);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue