-
Notifications
You must be signed in to change notification settings - Fork 6
/
bmpanel.c
313 lines (264 loc) · 6.89 KB
/
bmpanel.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <dirent.h>
#include <locale.h>
#include "gui.h"
#include "config-parser.h"
#include "xdg.h"
#include "settings.h"
#include "widget-utils.h"
#include "builtin-widgets.h"
#include "args.h"
/**************************************************************************
Listing themes
**************************************************************************/
static void list_theme(const char *themefile, const char *shortname)
{
if (!is_file_exists(themefile))
return;
struct config_format_tree tree;
if (0 != load_config_format_tree(&tree, themefile))
return;
const char *longname = 0;
const char *author = 0;
struct config_format_entry *e = find_config_format_entry(&tree.root,
"theme");
if (e) {
longname = find_config_format_entry_value(e, "name");
author = find_config_format_entry_value(e, "author");
}
printf(" * %s", shortname);
if (longname || author) {
printf(" (");
if (longname) {
printf("name: %s", longname);
if (author)
printf(", ");
}
if (author)
printf("author: %s", author);
printf(")");
}
printf("\n");
free_config_format_tree(&tree);
}
static void list_themes_in_dir(DIR *d, const char *dirpath)
{
char buf[4096];
struct dirent *de;
int len;
while ((de = readdir(d)) != 0) {
len = strlen(de->d_name);
switch (len) {
/* skip current dir and parent dir */
case 1:
if (de->d_name[0] == '.')
continue;
case 2:
if (de->d_name[0] == '.' && de->d_name[1] == '.')
continue;
default:
break;
}
snprintf(buf, sizeof(buf), "%s/%s/theme", dirpath, de->d_name);
list_theme(buf, de->d_name);
}
}
static void list_themes()
{
char buf[4096];
size_t data_dirs_len;
char **data_dirs = get_XDG_DATA_DIRS(&data_dirs_len);
DIR *d;
size_t i;
for (i = 0; i < data_dirs_len; ++i) {
snprintf(buf, sizeof(buf), "%s/bmpanel2/themes", data_dirs[i]);
buf[sizeof(buf)-1] = '\0';
printf("listing themes in \"%s\":\n", buf);
d = opendir(buf);
if (d) {
list_themes_in_dir(d, buf);
closedir(d);
} else {
printf(" - none\n");
}
}
}
/**************************************************************************
Theme loading
**************************************************************************/
static int try_load_theme(struct config_format_tree *tree, const char *name)
{
char buf[4096];
size_t data_dirs_len;
char **data_dirs;
int found = 0;
/* try to load it in-place */
snprintf(buf, sizeof(buf), "%s/theme", name);
if (is_file_exists(buf) && 0 == load_config_format_tree(tree, buf))
return 0;
/* scan XDG dirs */
data_dirs = get_XDG_DATA_DIRS(&data_dirs_len);
size_t i;
for (i = 0; i < data_dirs_len; ++i) {
snprintf(buf, sizeof(buf), "%s/bmpanel2/themes/%s/theme",
data_dirs[i], name);
buf[sizeof(buf)-1] = '\0';
if (is_file_exists(buf)) {
found = 1;
break;
}
}
free_XDG(data_dirs);
if (!found)
return -1;
if (0 != load_config_format_tree(tree, buf))
return -1;
return 0;
}
static int load_theme(struct config_format_tree *theme, const char *theme_override)
{
int theme_load_status = -1;
const char *theme_name;
if (theme_override)
theme_name = theme_override;
else
theme_name = find_config_format_entry_value(&g_settings.root,
"theme");
if (theme_name)
theme_load_status = try_load_theme(theme, theme_name);
if (theme_load_status < 0) {
if (theme_name)
XWARNING("Failed to load theme: \"%s\", "
"trying default \"native\"", theme_name);
else
XWARNING("Missing theme parameter, trying default \"native\"");
theme_load_status = try_load_theme(theme, "native");
}
return theme_load_status;
}
/*************************************************************************/
static struct config_format_tree theme;
static struct panel p;
/* options */
static int show_usage;
static int show_version;
static int show_list;
static const char *theme_override;
static const char *config_override;
#define BMPANEL2_VERSION_STR "bmpanel2 version 2.1\n"
#define BMPANEL2_USAGE \
"usage: bmpanel2 [-h | --help] [--version] [--usage] [--list] [--theme=<theme>]\n" \
" [--config=<config>]\n"
static const char *bmpanel2_version_str = BMPANEL2_VERSION_STR BMPANEL2_USAGE;
static int get_monitor()
{
return parse_int("monitor", &g_settings.root, 0);
}
static void reload_config_and_theme()
{
/* TODO: optimize here, when changing monitor,
* it's not necessary to reload theme
*/
struct widget_stash ws;
free_settings();
load_settings(config_override);
/* free theme */
free_config_format_tree(&theme);
reconfigure_free_panel(&p, &ws);
/* reload */
if (load_theme(&theme, theme_override) < 0)
XDIE("Failed to load theme");
reconfigure_panel(&p, &theme, &ws, get_monitor());
clean_image_cache(0);
}
static void reload_config()
{
free_settings();
load_settings(config_override);
reconfigure_panel_config(&p);
reconfigure_widgets(&p);
}
static void sigint_handler(int xxx)
{
XWARNING("sigint signal received, stopping main loop...");
g_main_loop_quit(p.loop);
}
static void sigterm_handler(int xxx)
{
XWARNING("sigterm signal received, stopping main loop...");
g_main_loop_quit(p.loop);
}
static gboolean reload_config_event(gpointer data)
{
if ((int)data)
reload_config_and_theme();
else
reload_config();
return 0;
}
static void sigusr1_handler(int xxx)
{
g_idle_add(reload_config_event, 0);
}
static void sigusr2_handler(int xxx)
{
g_idle_add(reload_config_event, (gpointer)1);
}
static void mysignal(int sig, void (*handler)(int))
{
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigaction(sig, &sa, 0);
}
static void parse_bmpanel2_args(int argc, char **argv)
{
struct argument args[] = {
ARG_BOOLEAN("usage", &show_usage, "show usage reminder", 0),
ARG_BOOLEAN("version", &show_version, "print bmpanel2 version", 0),
ARG_BOOLEAN("list", &show_list, "list available themes", 0),
ARG_STRING("config", &config_override, "use custom configuration file", 0),
ARG_STRING("theme", &theme_override, "override config theme parameter", 0),
ARG_END
};
parse_args(args, argc, argv, bmpanel2_version_str);
if (show_usage) {
printf(BMPANEL2_USAGE);
exit(0);
}
if (show_version) {
printf(BMPANEL2_VERSION_STR);
exit(0);
}
if (show_list) {
list_themes();
exit(0);
}
}
int main(int argc, char **argv)
{
setlocale(LC_TIME, "");
g_thread_init(0);
if (!g_thread_supported())
XDIE("bmpanel2 requires glib with thread support enabled");
parse_bmpanel2_args(argc, argv);
load_settings(config_override);
if (load_theme(&theme, theme_override) < 0)
XDIE("Failed to load theme");
clean_image_cache(0);
init_panel(&p, &theme, get_monitor());
mysignal(SIGINT, sigint_handler);
mysignal(SIGTERM, sigterm_handler);
mysignal(SIGUSR1, sigusr1_handler);
mysignal(SIGUSR2, sigusr2_handler);
panel_main_loop(&p);
free_panel(&p);
free_config_format_tree(&theme);
clean_static_buf();
clean_image_cache(1);
free_settings();
xmemstat(0, 0, 1);
return EXIT_SUCCESS;
}