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
|
#include <stdlib.h>
#include <locale.h>
#include <signal.h>
#include <math.h>
#include "window.h"
#include "layout.h"
#include "util.h"
#include "event.h"
#include "config.h"
#include "block.h"
#include "lua/api.h"
#ifdef RELEASE
#define ANY_LOG_VALUE_BEFORE(level, module, func, message) \
"[%s%s%s] %s%s%s: %s [", any_log_colors[ANY_LOG_ALL + 2], func, any_log_colors[ANY_LOG_ALL], \
any_log_colors[level], any_log_level_strings[level], any_log_colors[ANY_LOG_ALL], message
#define ANY_LOG_FORMAT_BEFORE(level, module, func) \
"[%s%s%s] %s%s%s: ", any_log_colors[ANY_LOG_ALL + 2], func, any_log_colors[ANY_LOG_ALL], \
any_log_colors[level], any_log_level_strings[level], any_log_colors[ANY_LOG_ALL]
#define ANY_LOG_FUNC_COLOR ""
#define ANY_LOG_NO_TRACE
#endif
#define ANY_LOG_VALUE_STRING(key, value) "%s=\"%s\"", key, value ? value : "(null)"
#define ANY_LOG_IMPLEMENT
#include "any_log.h"
static sig_atomic_t running = true;
static void signal_quit(int status)
{
running = false;
}
static cairo_surface_t *render_all(layout_t *layout, config_t *config, layout_info_t info)
{
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
config->width,
config->height);
log_value_trace("Created cairo surface",
"i:width", config->width,
"i:height", config->height);
cairo_t *cr = cairo_create(surface);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_matrix_t matrix;
cairo_matrix_init_scale(&matrix, 1.0 / config->width, 1.0);
cairo_pattern_set_matrix(config->background.pattern, &matrix);
cairo_set_source(cr, config->background.pattern);
cairo_paint(cr);
layout_render(layout, cr);
log_trace("Rendered layouts");
cairo_destroy(cr);
return surface;
}
int main(int argc, char **argv)
{
setlocale(LC_CTYPE, "");
any_log_level_t log_level = ANY_LOG_INFO;
if (argc != 1 && !strcmp(argv[1], "--trace"))
log_level = ANY_LOG_TRACE;
any_log_init(stdout, log_level);
config_t config;
config_init(&config);
log_debug("Copied default config");
lua_api_t lua;
lua_api_init(&lua);
if (luaL_dofile(lua.state, "example.lua") != LUA_OK) {
const char *error_message = lua_tostring(lua.state, -1);
log_error("Lua script failed: %s", error_message);
}
const char *config_path = "comet.conf";
FILE *config_file = fopen(config_path, "rb");
if (config_file != NULL) {
log_info("Reading config '%s'", config_path);
int errors = config_read(&config, config_file);
if (errors > 0) {
log_error("Config file contained %d syntax errors", errors);
return EXIT_FAILURE;
}
}
int errors = config_validate(&config);
if (errors > 0) {
log_error("Config file contained %d errors", errors);
return EXIT_FAILURE;
}
signal(SIGINT, signal_quit);
signal(SIGTERM, signal_quit);
display_t display;
display_init(&display);
window_t window;
window_init(&window, &display, &config);
log_debug("Searching font '%s'", config.font);
PangoFontDescription *fontdesc = pango_font_description_from_string(config.font);
log_debug("Loaded font family '%s'", pango_font_description_get_family(fontdesc));
layout_info_t info = {
.fontdesc = fontdesc,
.context = pango_cairo_create_context(window.cr),
.x_offset = 0,
.height = config.height,
};
block_t *block = NULL;
if (!config_resolve(&config, &block)) {
log_error("Config could not be resolved");
return EXIT_FAILURE;
}
log_debug("Starting bar");
window_resize(&window, config.width, config.height);
window_move(&window, config.x_offset, config.y_offset);
const double fps = 60.0;
const double freq = 1.0 / fps + 0.5e-9;
// TODO: Allow ondemand/lazy rendering
struct timespec rate, start, end, diff;
rate.tv_sec = (long)freq;
rate.tv_nsec = (freq - rate.tv_sec) * 1000000000ul;
event_state_t state = { 0 };
state.config = &config;
while (running) {
timespec_get(&start, TIME_UTC);
block_update(block);
layout_t layout;
layout_init(&layout, block, info);
state.layout = &layout;
event_dispatch(&state, &window);
cairo_surface_t *surface = render_all(&layout, &config, info);
window_present(&window, surface);
cairo_surface_destroy(surface);
layout_free(&layout);
timespec_get(&end, TIME_UTC);
diff = timespec_diff(rate, timespec_diff(end, start));
nanosleep(&diff, NULL);
}
log_debug("Quitting bar");
g_object_unref(info.context);
pango_font_description_free(fontdesc);
window_close(&window);
display_close(&display);
config_free(&config);
lua_api_close(&lua);
return 0;
}
|