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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#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 <X11/Xatom.h>
#include "pipe.h"
#include "bardata.h"
Display* display;
Window win;
GC gc;
XftFont* font = NULL;
static int bar_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);
}
size_t utf8_strlen(const char *s) {
size_t len = 0;
while (*s) {
if ((*s & 0xC0) != 0x80) len++;
s++;
}
return len;
}
void load_font(const char* font_name) {
XftFont* new_font = XftFontOpenName(display, DefaultScreen(display), font_name);
if (new_font == NULL) {
fprintf(stderr, "cannot open font %s\n", font_name);
return;
}
if (font != NULL) {
XftFontClose(display, font);
}
font = new_font;
}
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);
bar_width = WidthOfScreen(screen);
win = XCreateWindow(
display,
DefaultRootWindow(display),
0, 0,
bar_width, bar_height,
0,
CopyFromParent,
InputOutput,
CopyFromParent,
CWOverrideRedirect | CWBackPixel | CWEventMask,
&win_attr
);
XClassHint class_hint;
class_hint.res_name = "tsarbar";
class_hint.res_class = "Bar";
XSetClassHint(display, win, &class_hint);
XMapWindow(display, win);
load_font("monospace:size=10");
gc = XCreateGC(display, win, 0, NULL);
XSetForeground(display, gc, 0xFFFFFF);
XLowerWindow(display, win);
XFlush(display);
}
void deinit_x(void) {
XftFontClose(display, font);
XUnmapWindow(display, win);
XDestroyWindow(display, win);
}
void draw_text(
comp_t *comp,
int x,
int y
) {
XftDraw* xft_draw = XftDrawCreate(
display,
win,
DefaultVisual(display, DefaultScreen(display)),
DefaultColormap(display, DefaultScreen(display))
);
size_t width = font->max_advance_width * utf8_strlen(comp->data);
width += comp->margin_left + comp->margin_right;
XftDrawRect(xft_draw, comp->bg, x, y, width, bar_height);
x += comp->margin_left;
int dy = y + (bar_height - font->height) / 2 + font->ascent;
XftDrawStringUtf8(
xft_draw,
comp->fg,
font,
x,
dy,
(const FcChar8*)comp->data,
strlen(comp->data)
);
XftDrawDestroy(xft_draw);
}
void draw_comp_set(float pos_ratio, comp_t **set, int size) {
int width = (size - 1) * comp_gap;
for (int i = 0; i < size; i++) {
comp_t *comp = set[i];
int margin = comp->margin_left + comp->margin_right;
width += font->max_advance_width * utf8_strlen(comp->data) + margin;
}
int x = (bar_width - width) * pos_ratio;
x += padding * 2*(0.5 - pos_ratio); // Magic formula lol
int y = 0;//(bar_height - font->height) / 2;
for (int i = 0; i < size; i++) {
comp_t *comp = set[i];
draw_text(comp, x, y);
int margin = comp->margin_left + comp->margin_right;
x += font->max_advance_width * utf8_strlen(comp->data) + margin;
x += comp_gap;
}
}
int main(void) {
init_pipe();
init_x();
init_bardata();
while (true) {
XEvent ev;
while (XPending(display))
XNextEvent(display, &ev);
XClearWindow(display, win);
XResizeWindow(display, win, bar_width, bar_height);
draw_comp_set(0, comps_left, left_size);
draw_comp_set(0.5, comps_center, center_size);
draw_comp_set(1, comps_right, right_size);
XFlush(display);
await_change();
}
deinit_x();
return 0;
}
|