Add UTF8 support
[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 void expire()
14 {
15 XEvent event;
16 event.type = ButtonPress;
17 XSendEvent(display, window, 0, 0, &event);
18 XFlush(display);
19 }
20
21 int main(int argc, char *argv[])
22 {
23 if (argc != 2) {
24 fprintf(stderr, "Usage: herbe message\n");
25 exit(EXIT_FAILURE);
26 }
27
28 signal(SIGALRM, expire);
29 alarm(duration);
30
31 display = XOpenDisplay(NULL);
32
33 if (display == NULL)
34 {
35 fprintf(stderr, "Cannot open display\n");
36 exit(EXIT_FAILURE);
37 }
38
39 int screen = DefaultScreen(display);
40
41 int window_width = DisplayWidth(display, screen);
42 int window_height = DisplayHeight(display, screen);
43
44 XftColor color;
45
46 Window root = RootWindow(display, screen);
47 XSetWindowAttributes attributes;
48 attributes.override_redirect = True;
49 XftColorAllocName(display, DefaultVisual(display, screen), DefaultColormap(display, screen), background_color, &color);
50 attributes.background_pixel = color.pixel;
51 XftColorAllocName(display, DefaultVisual(display, screen), DefaultColormap(display, screen), border_color, &color);
52 attributes.border_pixel = color.pixel;
53
54 XftFont *font = XftFontOpenName(display, screen, font_style);
55
56 unsigned short x = pos_x;
57 unsigned short y = pos_y;
58 int height = font->ascent - font->descent + text_padding * 2;
59 switch (corner)
60 {
61 case down_right:
62 y = window_height - height - border_size * 2 - pos_y;
63 case top_right:
64 x = window_width - width - border_size * 2 - pos_x;
65 break;
66 case down_left:
67 y = window_height - height - border_size * 2 - pos_y;
68 }
69
70 window = XCreateWindow(
71 display, root, x,
72 y, width, height, border_size,
73 DefaultDepth(display, screen), CopyFromParent,
74 DefaultVisual(display, screen),
75 CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
76
77 XftDraw *draw = XftDrawCreate(display, window, DefaultVisual(display, screen), DefaultColormap(display, screen));
78 XftColorAllocName(display, DefaultVisual(display, screen), DefaultColormap(display, screen), font_color, &color);
79
80 XSelectInput(display, window, ExposureMask | ButtonPress);
81
82 XMapWindow(display, window);
83
84 XEvent event;
85
86 while (1)
87 {
88 XNextEvent(display, &event);
89
90 if (event.type == Expose)
91 {
92 XClearWindow(display, window);
93 XftDrawStringUtf8(draw, &color, font, text_padding, height - text_padding, (XftChar8 *)argv[1], strlen(argv[1]));
94 }
95 if (event.type == ButtonPress)
96 break;
97 }
98
99 XftDrawDestroy(draw);
100 XftColorFree(display, DefaultVisual(display, screen), DefaultColormap(display, screen), &color);
101 XftFontClose(display, font);
102
103 XCloseDisplay(display);
104 return 0;
105 }