-
Notifications
You must be signed in to change notification settings - Fork 6
/
widget-alternatives.c
202 lines (173 loc) · 4.35 KB
/
widget-alternatives.c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "gui.h"
#include "widget-utils.h"
/**************************************************************************
Alternative widgets
**************************************************************************/
struct widget_alternative_def {
int user_has;
int theme_has;
int used;
const char *name;
};
#define WIDGET_ALTERNATIVE_DEF(name) \
{0, 0, 0, name}
#define WIDGET_ALTERNATIVE_DEF_END \
{0, 0, 0, 0}
/* for example, not used */
static struct widget_alternative_def desktops_alternatives[] = {
WIDGET_ALTERNATIVE_DEF("desktop_switcher"),
WIDGET_ALTERNATIVE_DEF("pager"),
WIDGET_ALTERNATIVE_DEF_END
};
static struct widget_alternative_def *widget_alternatives[] = {
desktops_alternatives,
0
};
/**************************************************************************
Alternative widgets functions
**************************************************************************/
static int group_has_user_preference(struct widget_alternative_def *group)
{
while (group->name) {
if (group->user_has)
return 1;
group++;
}
return 0;
}
static struct widget_alternative_def *
group_has_user_theme_match(struct widget_alternative_def *group)
{
while (group->name) {
if (group->user_has && group->theme_has)
return group;
group++;
}
return 0;
}
static struct widget_alternative_def *
find_used_widget(struct widget_alternative_def *group)
{
while (group->name) {
if (group->used)
return group;
group++;
}
return 0;
}
static struct widget_alternative_def *
find_widget_alternative_def(const char *name, struct widget_alternative_def **group_out)
{
struct widget_alternative_def **wa = widget_alternatives;
while (*wa) {
struct widget_alternative_def *group = (*wa);
struct widget_alternative_def *wad = group;
while (wad->name) {
if (strcmp(wad->name, name) == 0) {
if (group_out)
*group_out = group;
return wad;
}
wad++;
}
wa++;
}
return 0;
}
static void match_user_preference(const char *pref, void *not_used)
{
struct widget_alternative_def *group;
struct widget_alternative_def *wad;
wad = find_widget_alternative_def(pref, &group);
if (!wad)
return;
if (group_has_user_preference(group)) {
printf("warning: group already has user preference, ignoring '%s'\n", pref);
return;
}
wad->user_has = 1;
}
static void match_theme_has(const char *theme_name)
{
struct widget_alternative_def *wad;
wad = find_widget_alternative_def(theme_name, 0);
if (!wad)
return;
wad->theme_has = 1;
}
static void match_theme_has_widget_alternatives(struct config_format_tree *tree)
{
size_t i;
for (i = 0; i < tree->root.children_n; ++i) {
struct config_format_entry *e = &tree->root.children[i];
struct widget_interface *we = lookup_widget_interface(e->name);
if (!we)
continue;
match_theme_has(e->name);
}
}
void update_alternatives_preference(char *prefstr, struct config_format_tree *tree)
{
for_each_word(prefstr, match_user_preference, 0);
match_theme_has_widget_alternatives(tree);
}
int validate_widget_for_alternatives(const char *theme_name)
{
struct widget_alternative_def *group;
struct widget_alternative_def *wad;
struct widget_alternative_def *match;
wad = find_widget_alternative_def(theme_name, &group);
if (!wad) /* if we can't find widget/group with that theme_name, then it's ok */
return 1;
match = group_has_user_theme_match(group);
if (match) {
if (match == wad)
return 1;
else
return 0;
} else {
match = find_used_widget(group);
if (!match) {
wad->used = 1;
return 1;
}
}
return 0;
}
void reset_alternatives()
{
struct widget_alternative_def **wa = widget_alternatives;
while (*wa) {
struct widget_alternative_def *wad = (*wa);
while (wad->name) {
wad->user_has = wad->theme_has = wad->used = 0;
wad++;
}
wa++;
}
}
#if 0
static void print_check_widget(const char *wname)
{
int ok = check_widget(wname);
printf("!%s!: %d\n", wname, ok);
}
int main(int argc, char **argv)
{
print_alternatives();
char preferred_alts[] = "pager taskbar2";
match_theme_has("panel");
match_theme_has("desktop_switcher");
match_theme_has("pager");
match_theme_has("taskbar");
match_user_preferred_widget_alternatives(preferred_alts);
print_check_widget("panel");
print_check_widget("desktop_switcher");
print_check_widget("pager");
print_check_widget("taskbar");
print_check_widget("systray");
print_check_widget("clock");
print_alternatives();
return 0;
}
#endif