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
|
#include "bardata.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pipe.h"
#include "tsar.h"
int fontsize = 16;
color_t default_fg, default_bg;
int bar_height = 20;
// Global state, oh naur D:
comp_t comps[32];
size_t comps_size = 0;
comp_t *comps_left[16], *comps_right[16], *comps_center[16];
size_t left_size = 0, right_size = 0, center_size = 0;
comp_t *find_comp(char *name) {
for (size_t i = 0; i < comps_size; i++) {
if (strcmp(comps[i].name, name) == 0) {
return &comps[i];
}
}
return NULL;
}
void new_component(char *name) {
comp_t* existing = find_comp(name);
if (existing)
return;
strcpy(comps[comps_size].name, name);
comps_size++;
}
void add_component(comp_side_t side, char *name) {
comp_t *comp = find_comp(name);
if (side == SIDE_LEFT) {
comps_left[left_size] = comp;
left_size++;
return;
}
if (side == SIDE_RIGHT) {
comps_right[right_size] = comp;
right_size++;
return;
}
if (side == SIDE_CENTER) {
comps_center[center_size] = comp;
center_size++;
return;
}
}
void set_component(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) {
comp_t* comp = find_comp(argv[1]);
for (int i = 2; i < argc; i++) {
if (strcmp(argv[i], "-text") == 0) {
strcpy(comp->data, argv[i + 1]);
i++;
continue;
}
}
}
void set_layout(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) {
comp_side_t side = SIDE_LEFT;
left_size = 0;
center_size = 0;
right_size = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-left") == 0) {
side = SIDE_LEFT;
continue;
}
if (strcmp(argv[i], "-center") == 0) {
side = SIDE_CENTER;
continue;
}
if (strcmp(argv[i], "-right") == 0) {
side = SIDE_RIGHT;
continue;
}
add_component(side, argv[i]);
}
}
void set_config(char argv[MAX_ARGS][MAX_ARG_LEN], int argc) {
char *var = argv[1];
if (strcmp(var, "height") == 0) {
int new_bar_height = atoi(argv[2]);
if (new_bar_height <= 0) {
fprintf(stderr, "invalid bar height %d\n", new_bar_height);
return;
}
bar_height = new_bar_height;
}
if (strcmp(var, "font") == 0) {
// fontsize = atoi(argv[2]);
load_font(argv[2]);
}
}
|