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
|
#ifndef COMET_BLOCK_H
#define COMET_BLOCK_H
#include <stddef.h>
#include <stdbool.h>
#include <time.h>
#include "event.h"
#include "util.h"
#include "layout.h"
#include "config.h"
// Element or text alignment
//
typedef enum {
ALIGN_LEFT,
ALIGN_CENTER,
ALIGN_RIGHT,
} align_t;
// Block type identifier
//
typedef enum {
BLOCK_TEXT,
BLOCK_GROUP,
BLOCK_SPEC,
} block_type_t;
typedef struct block block_t;
typedef struct block_scheme block_scheme_t;
// Regularly called depending on the inverval passed from the last update
//
typedef void (*block_update_t)(block_t *block);
// Called in layout_init
//
typedef void (*block_layout_t)(block_t *block, layout_t *layout, layout_info_t info);
// Called in layout_render
//
typedef void (*block_render_t)(layout_t *layout, cairo_t *cr);
// Triggered when an event is directed towards the block
//
typedef void (*block_event_t)(layout_t *layout, config_t *config, event_t event);
// The block struct
//
struct block {
block_type_t type;
char *label;
const block_scheme_t *scheme;
bool validated;
bool resolved;
bool grouped;
bool hidden;
struct timespec update_interval;
struct timespec update_last;
block_update_t update_fn;
gradient_t bg_color;
gradient_t line_color;
unsigned int line_width;
unsigned int x_padding, y_padding;
unsigned int min_width, max_width;
action_t *actions[EVENT_MAX];
};
typedef struct {
block_t block;
char *text;
color_t text_color;
align_t text_align;
unsigned int text_size;
} block_text_t;
typedef struct {
block_t block;
unsigned int spacing;
size_t n_children;
block_t **children;
bool collapse;
} block_group_t;
// NOTE: I am not sure this is required anymore since we can just put
// special function in the scheme
typedef struct {
block_t block;
block_layout_t layout_fn;
block_render_t render_fn;
block_event_t event_fn;
} block_spec_t;
// Called to allocate a block with its default state
//
typedef void (*block_init_t)(block_t *block);
// Called to cleanup a block and free its memory
// NOTE: This should NOT free any other block!
//
typedef void (*block_clean_t)(block_t *block);
// Called to validate the block after parsing the config
//
typedef int (*block_validate_t)(block_t *block, config_t *config);
// Called to resolve references in the block
//
typedef bool (*block_resolve_t)(block_t *block, config_t *config);
// Called to validate changes to a block variable
//
typedef config_status_t (*block_change_t)(block_t *block, config_t *config, const char *key, const char *value);
struct block_scheme {
const char *name;
const config_entry_t *entries;
size_t size;
bool validate_change;
block_init_t init_fn;
block_clean_t clean_fn;
block_validate_t validate_fn;
block_resolve_t resolve_fn;
block_change_t change_fn;
};
extern const block_scheme_t *block_schemes[];
void block_update(block_t *block);
int block_validate_entry(block_t *block, const config_entry_t *entry);
int block_validate(block_t *block, config_t *config);
bool block_resolve_action(block_t *block, config_t *config, action_t **action);
bool block_resolve(block_t *block, config_t *config);
bool block_change(block_t *block, config_t *config, const char *key, const char *value);
void block_free(block_t *block);
#endif
|