Clear window before redrawing
[herbe.git] / main.c
1 #include <X11/Xlib.h>
2 #include <X11/Xft/Xft.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include "config.h"
7
8 int main(int argc, char *argv[])
9 {
10 Display *display = XOpenDisplay(NULL);
11 XEvent event;
12
13 if (display == NULL)
14 {
15 fprintf(stderr, "Cannot open display\n");
16 exit(EXIT_FAILURE);
17 }
18
19 int screen = DefaultScreen(display);
20
21 int window_width = DisplayWidth(display, screen);
22 int window_height = DisplayHeight(display, screen);
23
24 XftColor color;
25
26 Window root = RootWindow(display, screen);
27 XSetWindowAttributes attributes;
28 attributes.override_redirect = True;
29 XftColorAllocName(display, DefaultVisual(display, screen), DefaultColormap(display, screen), background_color, &color);
30 attributes.background_pixel = color.pixel;
31 XftColorAllocName(display, DefaultVisual(display, screen), DefaultColormap(display, screen), border_color, &color);
32 attributes.border_pixel = color.pixel;
33
34 XftFont *font = XftFontOpenName(display, screen, font_style);
35
36 unsigned short x = pos_x;
37 unsigned short y = pos_y;
38 int height = font->ascent - font->descent + text_padding * 2;
39 switch (corner) {
40 case down_right:
41 y = window_height - height - border_size * 2 - pos_y;
42 case top_right:
43 x = window_width - width - border_size * 2 - pos_x;
44 break;
45 case down_left:
46 y = window_height - height - border_size * 2 - pos_y;
47 }
48
49 Window window = XCreateWindow(
50 display, root, x,
51 y, width, height, border_size,
52 DefaultDepth(display, screen), CopyFromParent,
53 DefaultVisual(display, screen),
54 CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
55
56 XftDraw *draw = XftDrawCreate(display, window, DefaultVisual(display, screen), DefaultColormap(display, screen));
57 XftColorAllocName(display, DefaultVisual(display, screen), DefaultColormap(display, screen), font_color, &color);
58
59 XSelectInput(display, window, ExposureMask | ButtonPress);
60
61 XMapWindow(display, window);
62
63 // TODO free xftcolor
64
65 while (1)
66 {
67 XNextEvent(display, &event);
68
69 if (event.type == Expose)
70 {
71 XClearWindow(display, window);
72 XftDrawString8(draw, &color, font, text_padding, height - text_padding, (XftChar8 *)argv[1], strlen(argv[1]));
73 }
74 if (event.type == ButtonPress)
75 break;
76 }
77
78 XCloseDisplay(display);
79 return 0;
80 }