File: settings.c

package info (click to toggle)
dunst 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 484 kB
  • ctags: 519
  • sloc: ansic: 3,699; sh: 170; makefile: 76
file content (372 lines) | stat: -rw-r--r-- 16,959 bytes parent folder | download | duplicates (2)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */

#include <glib.h>

#ifndef STATIC_CONFIG
#include <basedir.h>
#include <basedir_fs.h>
#endif

#include "dunst.h"
#include "rules.h"
#include "option_parser.h"
#include "settings.h"
#include "config.h"
#include "utils.h"

settings_t settings;

static void parse_follow_mode(const char *mode)
{
        if (strcmp(mode, "mouse") == 0)
                settings.f_mode = FOLLOW_MOUSE;
        else if (strcmp(mode, "keyboard") == 0)
                settings.f_mode = FOLLOW_KEYBOARD;
        else if (strcmp(mode, "none") == 0)
                settings.f_mode = FOLLOW_NONE;
        else {
                fprintf(stderr, "Warning: unknown follow mode: \"%s\"\n", mode);
                settings.f_mode = FOLLOW_NONE;
        }

}

static int ini_get_urgency(char *section, char *key, int def)
{
        int ret = def;
        char *urg = ini_get_string(section, key, "");

        if (strlen(urg) > 0) {
                if (strcmp(urg, "low") == 0)
                        ret = LOW;
                else if (strcmp(urg, "normal") == 0)
                        ret = NORM;
                else if (strcmp(urg, "critical") == 0)
                        ret = CRIT;
                else
                        fprintf(stderr,
                                "unknown urgency: %s, ignoring\n",
                                urg);
                free(urg);
        }
        return ret;
}

void load_settings(char *cmdline_config_path)
{

#ifndef STATIC_CONFIG
        xdgHandle xdg;
        FILE *config_file = NULL;

        xdgInitHandle(&xdg);

        if (cmdline_config_path != NULL) {
                config_file = fopen(cmdline_config_path, "r");
        }
        if (config_file == NULL) {
                config_file = xdgConfigOpen("dunst/dunstrc", "r", &xdg);
        }
        if (config_file == NULL) {
                /* Fall back to just "dunstrc", which was used before 2013-06-23
                 * (before v0.2). */
                config_file = xdgConfigOpen("dunstrc", "r", &xdg);
                if (config_file == NULL) {
                        puts("no dunstrc found -> skipping\n");
                        xdgWipeHandle(&xdg);
                }
        }

        load_ini_file(config_file);
#endif

        settings.font =
            option_get_string("global", "font", "-fn", font,
                              "The font dunst should use.");
        settings.allow_markup =
                option_get_bool("global", "allow_markup", "-markup", allow_markup,
                                "Allow markups.");
        settings.format =
            option_get_string("global", "format", "-format", format,
                              "The format template for the notifictions");
        settings.sort =
            option_get_bool("global", "sort", "-sort", sort,
                            "Sort notifications by urgency and date?");
        settings.indicate_hidden =
            option_get_bool("global", "indicate_hidden", "-indicate_hidden",
                            indicate_hidden,
                            "Show how many notificaitons are hidden?");
        settings.word_wrap =
            option_get_bool("global", "word_wrap", "-word_wrap", word_wrap,
                            "Truncating long lines or do word wrap");

        settings.ignore_newline =
            option_get_bool("global", "ignore_newline", "-ignore_newline",
                            ignore_newline,
                            "Ignore newline characters in notifications");
        settings.idle_threshold =
            option_get_int("global", "idle_threshold", "-idle_threshold",
                           idle_threshold,
                           "Don't timeout notifications if user is longer idle than threshold");
        settings.monitor =
            option_get_int("global", "monitor", "-mon", monitor,
                           "On which monitor should the notifications be displayed");
        {
                char *c = option_get_string("global", "follow", "-follow", "",
                                            "Follow mouse, keyboard or none?");
                if (strlen(c) > 0) {
                        parse_follow_mode(c);
                        free(c);
                }
        }
        settings.title =
            option_get_string("global", "title", "-t/-title", title,
                              "Define the title of windows spawned by dunst.");
        settings.class =
            option_get_string("global", "class", "-c/-class", class,
                              "Define the class of windows spawned by dunst.");
        settings.geom =
            option_get_string("global", "geometry", "-geom/-geometry", geom,
                              "Geometry for the window");
        settings.shrink =
            option_get_bool("global", "shrink", "-shrink",
                            shrink,
                            "Shrink window if it's smaller than the width");
        settings.line_height =
            option_get_int("global", "line_height", "-lh/-line_height",
                           line_height,
                           "Add additional padding above and beneath text");
        settings.bounce_freq =
            option_get_double("global", "bounce_freq", "-bounce_freq",
                              bounce_freq,
                              "Make long text bounce from side to side");
        {
                char *c = option_get_string("global", "alignment",
                                            "-align/-alignment", "",
                                            "Align notifications left/center/right");
                if (strlen(c) > 0) {
                        if (strcmp(c, "left") == 0)
                                settings.align = left;
                        else if (strcmp(c, "center") == 0)
                                settings.align = center;
                        else if (strcmp(c, "right") == 0)
                                settings.align = right;
                        else
                                fprintf(stderr,
                                        "Warning: unknown allignment\n");
                        free(c);
                }
        }
        settings.show_age_threshold =
            option_get_int("global", "show_age_threshold",
                           "-show_age_threshold", show_age_threshold,
                           "When should the age of the notification be displayed?");
        settings.sticky_history =
            option_get_bool("global", "sticky_history", "-sticky_history",
                            sticky_history,
                            "Don't timeout notifications popped up from history");
        settings.history_length = 
                option_get_int("global", "history_length", "-history_length",
                                history_length,
                                "Max amount of notifications kept in history");
        settings.show_indicators =
            option_get_bool("global", "show_indicators", "-show_indicators",
			    show_indicators,
			    "Show indicators for actions \"(A)\" and URLs \"(U)\"");
        settings.separator_height =
            option_get_int("global", "separator_height",
                           "-sep_height/-separator_height", separator_height,
                           "height of the separator line");
        settings.padding =
            option_get_int("global", "padding", "-padding", padding,
                           "Padding between text and separator");
        settings.h_padding =
            option_get_int("global", "horizontal_padding",
                           "-horizontal_padding", h_padding,
                           "horizontal padding");
        settings.transparency =
            option_get_int("global", "transparency", "-transparency",
                           transparency, "Transparency. range 0-100");
        {
                char *c = option_get_string("global", "separator_color",
                                            "-sep_color/-separator_color", "",
                                            "Color of the separator line (or 'auto')");
                if (strlen(c) > 0) {
                        if (strcmp(c, "auto") == 0)
                                settings.sep_color = AUTO;
                        else if (strcmp(c, "foreground") == 0)
                                settings.sep_color = FOREGROUND;
                        else if (strcmp(c, "frame") == 0)
                                settings.sep_color = FRAME;
                        else {
                                settings.sep_color = CUSTOM;
                                settings.sep_custom_color_str = g_strdup(c);
                        }
                        free(c);
                }
        }

        settings.stack_duplicates = option_get_bool("global", "stack_duplicates",
                        "-stack_duplicates", true, "Merge multiple notifications with the same content");

        settings.startup_notification =
            option_get_bool("global", "startup_notification",
                            "-startup_notification", false,
                            "print notification on startup");

        settings.dmenu =
            option_get_string("global", "dmenu", "-dmenu", dmenu,
                              "path to dmenu");
        settings.dmenu_cmd = string_to_argv(settings.dmenu);

        settings.browser =
            option_get_string("global", "browser", "-browser", browser,
                              "path to browser");

        {
                char *c = option_get_string("global", "icon_position",
                                            "-icon_position", "off",
                                            "Align icons left/right/off");
                if (strlen(c) > 0) {
                        if (strcmp(c, "left") == 0)
                                settings.icon_position = icons_left;
                        else if (strcmp(c, "right") == 0)
                                settings.icon_position = icons_right;
                        else if (strcmp(c, "off") == 0)
                                settings.icon_position = icons_off;
                        else
                                fprintf(stderr,
                                        "Warning: unknown icon position: %s\n", c);
                        free(c);
                }
        }

        settings.icon_folders =
            option_get_string("global", "icon_folders", "-icon_folders", icon_folders,
                              "paths to default icons");

        settings.frame_width =
            option_get_int("frame", "width", "-frame_width", frame_width,
                           "Width of frame around window");

        settings.frame_color =
            option_get_string("frame", "color", "-frame_color", frame_color,
                              "Color of the frame around window");

        settings.lowbgcolor =
            option_get_string("urgency_low", "background", "-lb", lowbgcolor,
                              "Background color for notifcations with low urgency");
        settings.lowfgcolor =
            option_get_string("urgency_low", "foreground", "-lf", lowfgcolor,
                              "Foreground color for notifications with low urgency");
        settings.timeouts[LOW] =
            option_get_int("urgency_low", "timeout", "-lto", timeouts[LOW],
                           "Timeout for notifications with low urgency");
        settings.icons[LOW] =
            option_get_string("urgency_low", "icon", "-li", icons[LOW],
                              "Icon for notifications with low urgency");
        settings.normbgcolor =
            option_get_string("urgency_normal", "background", "-nb",
                              normbgcolor,
                              "Background color for notifications with normal urgency");
        settings.normfgcolor =
            option_get_string("urgency_normal", "foreground", "-nf",
                              normfgcolor,
                              "Foreground color for notifications with normal urgency");
        settings.timeouts[NORM] =
            option_get_int("urgency_normal", "timeout", "-nto", timeouts[NORM],
                           "Timeout for notifications with normal urgency");
        settings.icons[NORM] =
            option_get_string("urgency_normal", "icon", "-ni", icons[NORM],
                              "Icon for notifications with normal urgency");
        settings.critbgcolor =
            option_get_string("urgency_critical", "background", "-cb",
                              critbgcolor,
                              "Background color for notifications with critical urgency");
        settings.critfgcolor =
            option_get_string("urgency_critical", "foreground", "-cf",
                              critfgcolor,
                              "Foreground color for notifications with ciritical urgency");
        settings.timeouts[CRIT] =
            option_get_int("urgency_critical", "timeout", "-cto", timeouts[CRIT],
                           "Timeout for notifications with critical urgency");
        settings.icons[CRIT] =
            option_get_string("urgency_critical", "icon", "-ci", icons[CRIT],
                              "Icon for notifications with critical urgency");

        settings.close_ks.str =
            option_get_string("shortcuts", "close", "-key", close_ks.str,
                              "Shortcut for closing one notification");
        settings.close_all_ks.str =
            option_get_string("shortcuts", "close_all", "-all_key",
                              close_all_ks.str,
                              "Shortcut for closing all notifications");
        settings.history_ks.str =
            option_get_string("shortcuts", "history", "-history_key",
                              history_ks.str,
                              "Shortcut to pop the last notification from history");

        settings.context_ks.str =
            option_get_string("shortcuts", "context", "-context_key",
                              context_ks.str, "Shortcut for context menu");

        settings.print_notifications =
            cmdline_get_bool("-print", false,
                             "Print notifications to cmdline (DEBUG)");

        /* push hardcoded default rules into rules list */
        for (int i = 0; i < LENGTH(default_rules); i++) {
                rules = g_slist_insert(rules, &(default_rules[i]), -1);
        }

        char *cur_section = NULL;
        for (;;) {
                cur_section = next_section(cur_section);
                if (!cur_section)
                        break;
                if (strcmp(cur_section, "global") == 0
                    || strcmp(cur_section, "shortcuts") == 0
                    || strcmp(cur_section, "urgency_low") == 0
                    || strcmp(cur_section, "urgency_normal") == 0
                    || strcmp(cur_section, "urgency_critical") == 0)
                        continue;

                /* check for existing rule with same name */
                rule_t *r = NULL;
                for (GSList * iter = rules; iter; iter = iter->next) {
                        rule_t *match = iter->data;
                        if (match->name &&
                            strcmp(match->name, cur_section) == 0)
                                r = match;
                }

                if (r == NULL) {
                        r = g_malloc(sizeof(rule_t));
                        rule_init(r);
                        rules = g_slist_insert(rules, r, -1);
                }

                r->name = g_strdup(cur_section);
                r->appname = ini_get_string(cur_section, "appname", r->appname);
                r->summary = ini_get_string(cur_section, "summary", r->summary);
                r->body = ini_get_string(cur_section, "body", r->body);
                r->icon = ini_get_string(cur_section, "icon", r->icon);
                r->timeout = ini_get_int(cur_section, "timeout", r->timeout);
                r->urgency = ini_get_urgency(cur_section, "urgency", r->urgency);
                r->msg_urgency = ini_get_urgency(cur_section, "msg_urgency", r->msg_urgency);
                r->fg = ini_get_string(cur_section, "foreground", r->fg);
                r->bg = ini_get_string(cur_section, "background", r->bg);
                r->format = ini_get_string(cur_section, "format", r->format);
                r->new_icon = ini_get_string(cur_section, "new_icon", r->new_icon);
				r->script = ini_get_string(cur_section, "script", NULL);
        }

#ifndef STATIC_CONFIG
        if (config_file) {
                fclose(config_file);
                free_ini();
                xdgWipeHandle(&xdg);
        }
#endif
}
/* vim: set ts=8 sw=8 tw=0: */