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