/
index
/
herbe.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
inline
| side by side (parent:
2c2741a
)
Add dynamic space allocation for notification body
author
Samuel Dudik
<redacted>
Sat, 8 Aug 2020 15:57:24 +0000
(17:57 +0200)
committer
Samuel Dudik
<redacted>
Sat, 8 Aug 2020 15:57:24 +0000
(17:57 +0200)
config.h
patch
|
blob
|
history
herbe.c
patch
|
blob
|
history
diff --git
a/config.h
b/config.h
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 *background_color = "#3e3e3e";
static const char *border_color = "#ececec";
static const char *font_color = "#ececec";
-static const char *font_pattern = "Inconsolata:style=Medium:size=1
3
";
+static const char *font_pattern = "Inconsolata:style=Medium:size=1
2
";
static const unsigned line_spacing = 5;
static const unsigned int padding = 15;
static const unsigned line_spacing = 5;
static const unsigned int padding = 15;
-static const unsigned int width = 4
0
0;
+static const unsigned int width = 4
5
0;
static const unsigned int border_size = 2;
static const unsigned int pos_x = 30;
static const unsigned int border_size = 2;
static const unsigned int pos_x = 30;
-static const unsigned int pos_y =
5
0;
+static const unsigned int pos_y =
6
0;
enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
enum corners corner = TOP_RIGHT;
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 <signal.h>
#include <unistd.h>
#include <string.h>
+#include <stdarg.h>
#include "config.h"
Display *display;
Window window;
#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 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)
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);
signal(SIGALRM, expire);
alarm(duration);
@@
-64,10
+72,7
@@
int main(int argc, char *argv[])
display = XOpenDisplay(0);
if (display == 0)
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 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;
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 (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);
words[num_of_lines][eol] = '\0';
}
strncpy(words[num_of_lines], body, eol);
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);
XftColorFree(display, visual, colormap, &color);
XftFontClose(display, font);
XCloseDisplay(display);
XftDrawDestroy(draw);
XftColorFree(display, visual, colormap, &color);
XftFontClose(display, font);
XCloseDisplay(display);
-
return EXIT_SUCCESS
;
+
exit(EXIT_SUCCESS)
;
}
\ No newline at end of file
}
\ No newline at end of file