aboutsummaryrefslogtreecommitdiff
path: root/src/tsar.c
blob: 869d73c0d20a8e4896d8628e16b923e5cee2e017 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <X11/Xft/Xft.h>
#include <X11/Xlib.h>

#include "pipe.h"

Display* display;
Window win;
GC gc;

XftFont* font;
XftColor fg_color;

static int height = 17;
static int width = 1000;
static float width_ratio = 1;

static int err_code = 1;

void die(const char* fmt, ...) {
  va_list args;
  va_start(args, fmt);
  vfprintf(stderr, fmt, args);
  va_end(args);

  putc('\n', stderr);

  exit(err_code);
}

void draw_bar(void) {
}

void init_x(void) {
  display = XOpenDisplay(NULL);
  if (display == NULL) {
    die("could not open X display");
  }

  XSetWindowAttributes win_attr = {
    .override_redirect = 1,
    .background_pixel = 0x202020,
    .event_mask = ExposureMask | PropertyChangeMask,
  };

  Screen* screen = DefaultScreenOfDisplay(display);
  width = WidthOfScreen(screen);

  win = XCreateWindow(
    display,
    DefaultRootWindow(display),
    0, 0,
    width, height,
    0,
    CopyFromParent,
    InputOutput,
    CopyFromParent,
    CWOverrideRedirect | CWBackPixel | CWEventMask,
    &win_attr
  );

  XMapRaised(display, win);

  gc = XCreateGC(display, win, 0, NULL);
  XSetForeground(display, gc, 0xFFFFFF);

  const char font_name[] = "CommitMono:style=Regular:antialias=true";
  font = XftFontOpenName(display, DefaultScreen(display), font_name);

  XftColorAllocName(
    display,
    DefaultVisual(display, DefaultScreen(display)),
    DefaultColormap(display, DefaultScreen(display)),
    "#FFFFFF",
    &fg_color
  );

  XFlush(display);
}

void deinit_x(void) {
  XftFontClose(display, font);
  XUnmapWindow(display, win);
  XDestroyWindow(display, win);
}

void draw_text(const char* text, int x, int y) {
  XftDraw* xft_draw = XftDrawCreate(
    display,
    win,
    DefaultVisual(display, DefaultScreen(display)),
    DefaultColormap(display, DefaultScreen(display))
  );

  int dy = y + (height - font->height) / 2 + font->ascent;
  XftDrawString8(xft_draw, &fg_color, font, x, dy, (const FcChar8*)text, strlen(text));

  XftDrawDestroy(xft_draw);
}

int main(void) {
  init_pipe();
  init_x();

  while (true) {
    XEvent ev;
    XNextEvent(display, &ev);

    XClearWindow(display, win);

    draw_text("Hello, world!", 0, 0);

    XFlush(display);
    await_change();
  }

  deinit_x();
  return 0;
}