2 #include <X11/Xft/Xft.h>
14 int get_eol(char *body
, XftFont
*font
)
16 int body_len
= strlen(body
);
18 XftTextExtentsUtf8(display
, font
, body
, body_len
, &info
);
20 int max_text_width
= width
- 2 * padding
;
22 if (info
.width
< max_text_width
)
25 int eol
= max_text_width
/ font
->max_advance_width
;
28 while (info
.width
< max_text_width
)
31 XftTextExtentsUtf8(display
, font
, body
, eol
, &info
);
36 // if (body[eol] == ' ')
39 // while (body[eol] != ' ')
50 event
.type
= ButtonPress
;
51 XSendEvent(display
, window
, 0, 0, &event
);
55 int main(int argc
, char *argv
[])
59 fprintf(stderr
, "Usage: herbe message\n");
65 signal(SIGALRM
, expire
);
68 display
= XOpenDisplay(NULL
);
72 fprintf(stderr
, "Cannot open display\n");
76 int screen
= DefaultScreen(display
);
77 Visual
*visual
= DefaultVisual(display
, screen
);
78 Colormap colormap
= DefaultColormap(display
, screen
);
80 int window_width
= DisplayWidth(display
, screen
);
81 int window_height
= DisplayHeight(display
, screen
);
85 XSetWindowAttributes attributes
;
86 attributes
.override_redirect
= True
;
87 XftColorAllocName(display
, visual
, colormap
, background_color
, &color
);
88 attributes
.background_pixel
= color
.pixel
;
89 XftColorAllocName(display
, visual
, colormap
, border_color
, &color
);
90 attributes
.border_pixel
= color
.pixel
;
92 XftFont
*font
= XftFontOpenName(display
, screen
, font_pattern
);
94 unsigned int x
= pos_x
;
95 unsigned int y
= pos_y
;
96 unsigned int height
= font
->ascent
- font
->descent
+ padding
* 2;
101 y
= window_height
- height
- border_size
* 2 - pos_y
;
103 x
= window_width
- width
- border_size
* 2 - pos_x
;
106 y
= window_height
- height
- border_size
* 2 - pos_y
;
110 XftTextExtentsUtf8(display
, font
, body
, strlen(body
), &info
);
111 int num_of_lines
= ceil((float)info
.width
/ (width
- 2 * padding
));
113 window
= XCreateWindow(
114 display
, RootWindow(display
, screen
), x
,
115 y
, width
, (num_of_lines
- 1) * 5 + num_of_lines
* (font
->ascent
- font
->descent
) + 2 * padding
, border_size
,
116 DefaultDepth(display
, screen
), CopyFromParent
,
118 CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
, &attributes
);
120 XftDraw
*draw
= XftDrawCreate(display
, window
, visual
, colormap
);
121 XftColorAllocName(display
, visual
, colormap
, font_color
, &color
);
123 XSelectInput(display
, window
, ExposureMask
| ButtonPress
);
125 XMapWindow(display
, window
);
127 int eols
[num_of_lines
+ 1];
130 for (int i
= 1; i
< num_of_lines
+ 1; i
++)
132 eols
[i
] = eols
[i
- 1] + get_eol(body
+ eols
[i
- 1], font
);
139 XNextEvent(display
, &event
);
141 if (event
.type
== Expose
)
143 XClearWindow(display
, window
);
145 for (int i
= 1; i
< num_of_lines
+ 1; i
++)
147 XftDrawStringUtf8(draw
, &color
, font
, padding
, 5 * (i
- 1) + (font
->ascent
- font
->descent
) * i
+ padding
, body
+ eols
[i
- 1], eols
[i
] - eols
[i
- 1]);
150 if (event
.type
== ButtonPress
)
154 XftDrawDestroy(draw
);
155 XftColorFree(display
, visual
, colormap
, &color
);
156 XftFontClose(display
, font
);
158 XCloseDisplay(display
);