Add new line formatting functionality
[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
8 #include "config.h"
9
10 Display *display;
11 Window window;
12
13 int get_max_len(char *body, XftFont *font, int max_text_width)
14 {
15 int body_len = strlen(body);
16 XGlyphInfo info;
17 XftTextExtentsUtf8(display, font, (FcChar8 *)body, body_len, &info);
18
19 if (info.width < max_text_width)
20 return body_len;
21
22 int eol = max_text_width / font->max_advance_width;
23 info.width = 0;
24
25 while (info.width < max_text_width)
26 {
27 eol++;
28 XftTextExtentsUtf8(display, font, (FcChar8 *)body, eol, &info);
29 }
30
31 eol--;
32
33 int temp = eol;
34
35 while (body[eol] != ' ' && eol)
36 --eol;
37
38 if (eol == 0)
39 return temp;
40 else
41 return ++eol;
42 }
43
44 void expire()
45 {
46 XEvent event;
47 event.type = ButtonPress;
48 XSendEvent(display, window, 0, 0, &event);
49 XFlush(display);
50 }
51
52 int main(int argc, char *argv[])
53 {
54 if (argc == 1)
55 {
56 fprintf(stderr, "Usage: herbe body\n");
57 exit(EXIT_FAILURE);
58 }
59
60 signal(SIGALRM, expire);
61 alarm(duration);
62
63 display = XOpenDisplay(NULL);
64
65 if (display == NULL)
66 {
67 fprintf(stderr, "Cannot open display\n");
68 exit(EXIT_FAILURE);
69 }
70
71 int screen = DefaultScreen(display);
72 Visual *visual = DefaultVisual(display, screen);
73 Colormap colormap = DefaultColormap(display, screen);
74
75 int screen_width = DisplayWidth(display, screen);
76 int screen_height = DisplayHeight(display, screen);
77
78 XftColor color;
79
80 XSetWindowAttributes attributes;
81 attributes.override_redirect = True;
82 XftColorAllocName(display, visual, colormap, background_color, &color);
83 attributes.background_pixel = color.pixel;
84 XftColorAllocName(display, visual, colormap, border_color, &color);
85 attributes.border_pixel = color.pixel;
86
87 XftFont *font = XftFontOpenName(display, screen, font_pattern);
88
89 int num_of_lines = 0;
90 int max_text_width = width - 2 * padding;
91 // TODO replace hard-coded size 100
92 char words[100][max_text_width / font->max_advance_width + 2];
93
94 for (int i = 1; i < argc; i++)
95 {
96 char *body = argv[i];
97
98 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))
99 {
100 strncpy(words[num_of_lines], body, eol);
101 words[num_of_lines][eol] = '\0';
102 }
103 }
104
105 unsigned int x = pos_x;
106 unsigned int y = pos_y;
107 unsigned int text_height = font->ascent - font->descent;
108 unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding;
109
110 if (corner == TOP_RIGHT || corner == BOTTOM_RIGHT)
111 x = screen_width - width - border_size * 2 - pos_x;
112
113 if (corner == BOTTOM_LEFT || corner == BOTTOM_RIGHT)
114 y = screen_height - height - border_size * 2 - pos_y;
115
116 window = XCreateWindow(display, RootWindow(display, screen), x, y, width, height, border_size, DefaultDepth(display, screen), CopyFromParent, visual,
117 CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
118
119 XftDraw *draw = XftDrawCreate(display, window, visual, colormap);
120 XftColorAllocName(display, visual, colormap, font_color, &color);
121
122 XSelectInput(display, window, ExposureMask | ButtonPress);
123
124 XMapWindow(display, window);
125
126 XEvent event;
127
128 while (1)
129 {
130 XNextEvent(display, &event);
131
132 if (event.type == Expose)
133 {
134 XClearWindow(display, window);
135 for (int i = 0; i < num_of_lines; i++)
136 XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding, (FcChar8 *)words[i], strlen(words[i]));
137 }
138 if (event.type == ButtonPress)
139 break;
140 }
141
142 XftDrawDestroy(draw);
143 XftColorFree(display, visual, colormap, &color);
144 XftFontClose(display, font);
145
146 XCloseDisplay(display);
147 return EXIT_SUCCESS;
148 }