Fix problem (with bitmap fonts) causing new lines to be printed
[herbe.git] / herbe.c
1 #include <X11/Xlib.h>
2 #include <X11/Xft/Xft.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <signal.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <stdarg.h>
9 #include <fcntl.h>
10 #include <semaphore.h>
11
12 #include "config.h"
13
14 Display *display;
15 Window window;
16
17 static void die(const char *format, ...)
18 {
19 va_list ap;
20 va_start(ap, format);
21 vfprintf(stderr, format, ap);
22 fprintf(stderr, "\n");
23 va_end(ap);
24 exit(EXIT_FAILURE);
25 }
26
27 int get_max_len(char *body, XftFont *font, int max_text_width)
28 {
29 int eol = strlen(body);
30 XGlyphInfo info;
31 XftTextExtentsUtf8(display, font, (FcChar8 *)body, eol, &info);
32
33 if (info.width > max_text_width)
34 {
35
36 eol = max_text_width / font->max_advance_width;
37 info.width = 0;
38
39 while (info.width < max_text_width)
40 {
41 eol++;
42 XftTextExtentsUtf8(display, font, (FcChar8 *)body, eol, &info);
43 }
44
45 eol--;
46 }
47
48 for (int i = 0; i < eol; i++)
49 if (body[i] == '\n') {
50 body[i] = ' ';
51 return ++i;
52 }
53
54 if (info.width < max_text_width)
55 return eol;
56
57 int temp = eol;
58
59 while (body[eol] != ' ' && eol)
60 --eol;
61
62 if (eol == 0)
63 return temp;
64 else
65 return ++eol;
66 }
67
68 void expire()
69 {
70 XEvent event;
71 event.type = ButtonPress;
72 XSendEvent(display, window, 0, 0, &event);
73 XFlush(display);
74 }
75
76 int main(int argc, char *argv[])
77 {
78 if (argc == 1)
79 die("Usage: %s body", argv[0]);
80
81 signal(SIGALRM, expire);
82 signal(SIGTERM, expire);
83 signal(SIGINT, expire);
84
85 display = XOpenDisplay(0);
86
87 if (display == 0)
88 die("Cannot open display");
89
90 int screen = DefaultScreen(display);
91 Visual *visual = DefaultVisual(display, screen);
92 Colormap colormap = DefaultColormap(display, screen);
93
94 int screen_width = DisplayWidth(display, screen);
95 int screen_height = DisplayHeight(display, screen);
96
97 XftColor color;
98
99 XSetWindowAttributes attributes;
100 attributes.override_redirect = True;
101 XftColorAllocName(display, visual, colormap, background_color, &color);
102 attributes.background_pixel = color.pixel;
103 XftColorAllocName(display, visual, colormap, border_color, &color);
104 attributes.border_pixel = color.pixel;
105
106 XftFont *font = XftFontOpenName(display, screen, font_pattern);
107
108 int num_of_lines = 0;
109 int max_text_width = width - 2 * padding;
110 int words_size = 5;
111 char **words = malloc(words_size * sizeof(char *));
112 if (!words)
113 die("malloc failed");
114
115 for (int i = 1; i < argc; i++)
116 {
117 char *body = argv[i];
118
119 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))
120 {
121 if (words_size <= num_of_lines)
122 {
123 words = realloc(words, (words_size += 5) * sizeof(char *));
124 if (!words)
125 die("malloc failed");
126 }
127 words[num_of_lines] = malloc((eol + 1) * sizeof(char));
128 if (!words[num_of_lines])
129 die("malloc failed");
130 strncpy(words[num_of_lines], body, eol);
131 words[num_of_lines][eol] = '\0';
132 }
133 }
134
135 unsigned int x = pos_x;
136 unsigned int y = pos_y;
137 unsigned int text_height = font->ascent - font->descent;
138 unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding;
139
140 if (corner == TOP_RIGHT || corner == BOTTOM_RIGHT)
141 x = screen_width - width - border_size * 2 - pos_x;
142
143 if (corner == BOTTOM_LEFT || corner == BOTTOM_RIGHT)
144 y = screen_height - height - border_size * 2 - pos_y;
145
146 window = XCreateWindow(display, RootWindow(display, screen), x, y, width, height, border_size, DefaultDepth(display, screen), CopyFromParent, visual,
147 CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
148
149 XftDraw *draw = XftDrawCreate(display, window, visual, colormap);
150 XftColorAllocName(display, visual, colormap, font_color, &color);
151
152 XSelectInput(display, window, ExposureMask | ButtonPress);
153
154 XMapWindow(display, window);
155
156 sem_t *mutex = sem_open("/herbe", O_CREAT, 0644, 1);
157 sem_wait(mutex);
158
159 if (duration != 0)
160 alarm(duration);
161
162 while (1)
163 {
164 XEvent event;
165 XNextEvent(display, &event);
166
167 if (event.type == Expose)
168 {
169 XClearWindow(display, window);
170 for (int i = 0; i < num_of_lines; i++)
171 XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding, (FcChar8 *)words[i], strlen(words[i]));
172 }
173 if (event.type == ButtonPress)
174 break;
175 }
176
177 sem_post(mutex);
178 sem_close(mutex);
179
180 for (int i = 0; i < num_of_lines; i++)
181 free(words[i]);
182
183 free(words);
184 XftDrawDestroy(draw);
185 XftColorFree(display, visual, colormap, &color);
186 XftFontClose(display, font);
187 XCloseDisplay(display);
188
189 exit(EXIT_SUCCESS);
190 }