]> git.lizzy.rs Git - clockoverlay.git/blob - clockoverlay.c
Create .gitignore
[clockoverlay.git] / clockoverlay.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/select.h>
6 #include <time.h>
7 #include <unistd.h>
8 #include <X11/Xlib.h>
9 #include <X11/Xutil.h>
10
11 static inline unsigned long _RGB(int r, int g, int b)
12 {
13         return b + (g << 8) + (r << 16);
14 }
15
16 int main(void) {        
17         Display *display = XOpenDisplay(NULL);
18         assert(display);
19
20         int fd = ConnectionNumber(display);
21
22         XVisualInfo vinfo;
23         XMatchVisualInfo(display, DefaultScreen(display), 32, TrueColor, &vinfo);
24
25         XSetWindowAttributes attribs;
26         attribs.colormap = XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
27         attribs.override_redirect = 1;
28         attribs.border_pixel = 0;
29         attribs.background_pixel = 0;
30
31         Window window = XCreateWindow(display, DefaultRootWindow(display), 10, 10, 24 / 2 * 8, 24, 0,
32                 vinfo.depth, InputOutput, vinfo.visual,
33                 CWColormap | CWOverrideRedirect | CWBorderPixel | CWBackPixel, &attribs);
34         XMapWindow(display, window);
35
36         Font font = XLoadFont(display, "*x24");
37
38         GC ctx = XCreateGC(display, window, 0, NULL);
39         XSetForeground(display, ctx, _RGB(255, 255, 255));
40         XSetFont(display, ctx, font);
41
42         for (;;) {
43                 time_t t = time(NULL);
44                 struct tm *tm = localtime(&t);
45
46                 char buf[9];
47                 strftime(buf, 9, "%H:%M:%S", tm);
48                 XClearWindow(display, window);
49                 XDrawString(display, window, ctx, 0, 24, buf, 8);
50
51                 XEvent event;
52                 while (XPending(display))
53                         XNextEvent(display, &event);
54
55                 fd_set set;
56                 FD_ZERO(&set);
57                 FD_SET(fd, &set);
58                 select(fd + 1, &set, NULL, NULL, &(struct timeval) {1, 0});
59         }
60 }