diff --git a/draw.c b/draw.c index 159ac7b..663e88a 100644 --- a/draw.c +++ b/draw.c @@ -235,12 +235,12 @@ void initfont(DC * dc, const char *fontstr) return; } -void -setopacity(DC *dc, Window win, unsigned long opacity) +void setopacity(DC * dc, Window win, unsigned long opacity) { - Atom _NET_WM_WINDOW_OPACITY = XInternAtom(dc->dpy, "_NET_WM_WINDOW_OPACITY", false); - XChangeProperty(dc->dpy, win, _NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32, PropModeReplace, - (unsigned char *)&opacity, 1L); + Atom _NET_WM_WINDOW_OPACITY = + XInternAtom(dc->dpy, "_NET_WM_WINDOW_OPACITY", false); + XChangeProperty(dc->dpy, win, _NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32, + PropModeReplace, (unsigned char *)&opacity, 1L); } void mapdc(DC * dc, Window win, unsigned int w, unsigned int h) diff --git a/draw.h b/draw.h index 117986a..7a5c3be 100644 --- a/draw.h +++ b/draw.h @@ -70,7 +70,7 @@ unsigned long getcolor(DC * dc, const char *colstr); ColorSet *initcolor(DC * dc, const char *foreground, const char *background); DC *initdc(void); void initfont(DC * dc, const char *fontstr); -void setopacity(DC *dc, Window win, unsigned long opacity); +void setopacity(DC * dc, Window win, unsigned long opacity); void mapdc(DC * dc, Window win, unsigned int w, unsigned int h); void resizedc(DC * dc, unsigned int w, unsigned int h); int textnw(DC * dc, const char *text, size_t len); diff --git a/dunst.c b/dunst.c index b112a1e..4a904cd 100644 --- a/dunst.c +++ b/dunst.c @@ -45,7 +45,6 @@ #define INFO 2 #define DEBUG 3 - /* global variables */ #include "config.h" @@ -107,7 +106,7 @@ void warn(const char *text, int urg); void init_shortcut(keyboard_shortcut * shortcut); KeySym string_to_mask(char *str); -static void print_notification(notification *n) +static void print_notification(notification * n) { printf("{\n"); printf("\tappname: %s\n", n->appname); @@ -154,7 +153,7 @@ static int tear_down_error_handler(void) int grab_key(keyboard_shortcut * ks) { if (!ks->is_valid) - return 1; + return 1; Window root; root = RootWindow(dc->dpy, DefaultScreen(dc->dpy)); @@ -379,9 +378,9 @@ int do_word_wrap(char *source, int max_width) *eol = '\0'; return 1 + do_word_wrap(eol + 1, max_width); } - if (*eol == '\\' && *(eol+1) == 'n') { + if (*eol == '\\' && *(eol + 1) == 'n') { *eol = ' '; - *(eol+1) = '\0'; + *(eol + 1) = '\0'; return 1 + do_word_wrap(eol + 2, max_width); } @@ -404,7 +403,7 @@ int do_word_wrap(char *source, int max_width) return 1 + do_word_wrap(space + 1, max_width); } } - eol++; + eol++; } } @@ -412,7 +411,7 @@ void update_draw_txt_buf(notification * n, int max_width) { rstrip(n->msg); char *msg = n->msg; - while(isspace(*msg)) + while (isspace(*msg)) msg++; if (n->draw_txt_buf.txt) @@ -447,9 +446,10 @@ void update_draw_txt_buf(notification * n, int max_width) char *new_buf; if (hours > 0) { asprintf(&new_buf, "%s (%dh %dm %ds old)", buf, hours, - minutes, seconds); + minutes, seconds); } else if (minutes > 0) { - asprintf(&new_buf, "%s (%dm %ds old)", buf, minutes, seconds); + asprintf(&new_buf, "%s (%dm %ds old)", buf, minutes, + seconds); } else { asprintf(&new_buf, "%s (%ds old)", buf, seconds); } @@ -484,7 +484,8 @@ int calculate_x_offset(int line_width, int text_width) /* If the text is wider than the frame, bouncing is enabled and word_wrap disabled */ if (line_width < text_width && bounce_freq > 0.0001 && !word_wrap) { gettimeofday(&t, NULL); - pos = ((t.tv_sec % 100) * 1e6 + t.tv_usec) / (1e6 / bounce_freq); + pos = + ((t.tv_sec % 100) * 1e6 + t.tv_usec) / (1e6 / bounce_freq); return (1 + sinf(2 * 3.14159 * pos)) * leftover / 2; } switch (align) { @@ -579,8 +580,6 @@ void draw_win(void) line_cnt += n->draw_txt_buf.line_count; } - - /* calculate height */ if (geometry.h == 0) { height = line_cnt * line_height; @@ -590,7 +589,6 @@ void draw_win(void) height += (l_length(displayed_notifications) - 1) * separator_height; - /* add "(x more)" */ draw_txt x_more; x_more.txt = NULL; @@ -612,7 +610,8 @@ void draw_win(void) } else { /* append "(x more)" message to notification text */ notification *n = - (notification *) displayed_notifications->head->data; + (notification *) displayed_notifications->head-> + data; print_to = draw_txt_get_line(&n->draw_txt_buf, n->draw_txt_buf.line_count); @@ -628,7 +627,7 @@ void draw_win(void) notification *n = (notification *) iter->data; for (int i = 0; i < n->draw_txt_buf.line_count; i++) { char *line = - draw_txt_get_line(&n->draw_txt_buf, i+1); + draw_txt_get_line(&n->draw_txt_buf, i + 1); assert(line != NULL); width = MAX(width, textw(dc, line)); } @@ -668,14 +667,16 @@ void draw_win(void) /* draw separator */ if (separator_height > 0) { - dc -> x = 0; + dc->x = 0; double color; if (sep_color == AUTO) - color = calculate_foreground_color(n->colors->BG); + color = + calculate_foreground_color(n->colors->BG); else color = n->colors->FG; - drawrect(dc, 0, 0, width, separator_height, true, color); + drawrect(dc, 0, 0, width, separator_height, true, + color); dc->y += separator_height; } } @@ -703,8 +704,7 @@ void draw_win(void) /* move and map window */ if (x != window_dim.x || y != window_dim.y - || width != window_dim.w - || height != window_dim.h) { + || width != window_dim.w || height != window_dim.h) { XResizeWindow(dc->dpy, win, width, height); XMoveWindow(dc->dpy, win, x, y); @@ -914,7 +914,7 @@ int init_notification(notification * n, int id) n->msg = string_replace("%b", n->body, n->msg); if (n->progress) { char pg[10]; - sprintf(pg, "[%3d%%]", n->progress-1); + sprintf(pg, "[%3d%%]", n->progress - 1); n->msg = string_replace("%p", pg, n->msg); } else { n->msg = string_replace("%p", "", n->msg); @@ -922,14 +922,14 @@ int init_notification(notification * n, int id) n->msg = fix_markup(n->msg); - n->dup_count = 0; n->draw_txt_buf.txt = NULL; /* check if n is a duplicate */ for (l_node * iter = notification_queue->head; iter; iter = iter->next) { notification *orig = (notification *) iter->data; - if (strcmp(orig->appname, n->appname) == 0 && strcmp(orig->msg, n->msg) == 0) { + if (strcmp(orig->appname, n->appname) == 0 + && strcmp(orig->msg, n->msg) == 0) { orig->dup_count++; free_notification(n); return orig->id; @@ -939,7 +939,8 @@ int init_notification(notification * n, int id) for (l_node * iter = displayed_notifications->head; iter; iter = iter->next) { notification *orig = (notification *) iter->data; - if (strcmp(orig->appname, n->appname) == 0 && strcmp(orig->msg, n->msg) == 0) { + if (strcmp(orig->appname, n->appname) == 0 + && strcmp(orig->msg, n->msg) == 0) { orig->dup_count++; orig->start = now; free_notification(n); @@ -978,7 +979,7 @@ int init_notification(notification * n, int id) n->id = id; } - if(strlen(n->msg) == 0) { + if (strlen(n->msg) == 0) { close_notification(n, 2); printf("skipping notification: %s %s\n", n->body, n->summary); } else { @@ -988,7 +989,6 @@ int init_notification(notification * n, int id) if (print_notifications) print_notification(n); - return n->id; } @@ -1100,7 +1100,6 @@ void init_shortcut(keyboard_shortcut * ks) } } - if (ks->sym == NoSymbol || ks->code == NoSymbol) { fprintf(stderr, "Warning: Unknown keyboard shortcut: %s\n", ks->str); @@ -1305,7 +1304,6 @@ void setup(void) color_strings[ColBG][NORM] = normbgcolor; color_strings[ColBG][CRIT] = critbgcolor; - /* parse and set geometry and monitor position */ if (geom[0] == '-') { geometry.negative_width = true; @@ -1352,7 +1350,8 @@ void setup(void) DefaultScreen(dc->dpy)), CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); transparency = transparency > 100 ? 100 : transparency; - setopacity(dc, win, (unsigned long)((100 - transparency) * (0xffffffff/100))); + setopacity(dc, win, + (unsigned long)((100 - transparency) * (0xffffffff / 100))); grab_key(&history_ks); } @@ -1444,22 +1443,45 @@ void load_options(char *cmdline_config_path) load_ini_file(config_file); #endif - font = option_get_string("global", "font", "-fn", font, "The font dunst should use."); - format = option_get_string("global", "format", "-format", format, "The format template for the notifictions"); - sort = option_get_bool("global", "sort", "-sort", sort, "Sort notifications by urgency and date?"); - indicate_hidden = option_get_bool("global", "indicate_hidden", "-indicate_hidden", indicate_hidden, "Show how many notificaitons are hidden?"); - word_wrap = option_get_bool("global", "word_wrap", "-word_wrap", word_wrap, "Truncating long lines or do word wrap"); - idle_threshold = option_get_int("global", "idle_threshold", "-idle_threshold", idle_threshold, "Don't timeout notifications if user is longer idle than threshold"); - monitor = option_get_int("global", "monitor", "-mon", monitor, "On which monitor should the notifications be displayed"); + font = + option_get_string("global", "font", "-fn", font, + "The font dunst should use."); + format = + option_get_string("global", "format", "-format", format, + "The format template for the notifictions"); + sort = + option_get_bool("global", "sort", "-sort", sort, + "Sort notifications by urgency and date?"); + indicate_hidden = + option_get_bool("global", "indicate_hidden", "-indicate_hidden", + indicate_hidden, + "Show how many notificaitons are hidden?"); + word_wrap = + option_get_bool("global", "word_wrap", "-word_wrap", word_wrap, + "Truncating long lines or do word wrap"); + idle_threshold = + option_get_int("global", "idle_threshold", "-idle_threshold", + idle_threshold, + "Don't timeout notifications if user is longer idle than threshold"); + 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?"); + char *c = + option_get_string("global", "follow", "-follow", "", + "Follow mouse, keyboard or none?"); if (strlen(c) > 0) { parse_follow_mode(c); free(c); } } - geom = option_get_string("global", "geometry", "-geom/-geometry", geom, "Geometry for the window"); - line_height = option_get_int("global", "line_height", "-lh/-line_height", line_height, "Add additional padding above and beneath text"); + geom = + option_get_string("global", "geometry", "-geom/-geometry", geom, + "Geometry for the window"); + line_height = + option_get_int("global", "line_height", "-lh/-line_height", + line_height, + "Add additional padding above and beneath text"); { char *c = option_get_string("global", "modifier", NULL, "", ""); if (strlen(c) > 0) { @@ -1471,12 +1493,22 @@ void load_options(char *cmdline_config_path) free(c); } } - close_ks.str = option_get_string("global", "key", NULL, close_ks.str, ""); - close_all_ks.str = option_get_string("global", "all_key", NULL, close_all_ks.str, ""); - history_ks.str = option_get_string("global", "history_key", NULL, history_ks.str, ""); - bounce_freq = option_get_double("global", "bounce_freq", "-bounce_freq", bounce_freq, "Make long text bounce from side to side"); + close_ks.str = + option_get_string("global", "key", NULL, close_ks.str, ""); + close_all_ks.str = + option_get_string("global", "all_key", NULL, close_all_ks.str, ""); + history_ks.str = + option_get_string("global", "history_key", NULL, history_ks.str, + ""); + 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"); + char *c = + option_get_string("global", "alignment", + "-align/-alignment", "", + "Align notifications left/center/right"); if (strlen(c) > 0) { if (strcmp(c, "left") == 0) align = left; @@ -1485,42 +1517,91 @@ void load_options(char *cmdline_config_path) else if (strcmp(c, "right") == 0) align = right; else - fprintf(stderr, "Warning: unknown allignment\n"); + fprintf(stderr, + "Warning: unknown allignment\n"); free(c); } } - 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?"); - sticky_history = option_get_bool("global", "sticky_history", "-sticky_history", sticky_history, "Don't timeout notifications popped up from history"); - separator_height = option_get_int("global", "separator_height", "-sep_height/-separator_height", separator_height, "height of the separator line"); - transparency = option_get_int("global", "transparency", "-transparency", transparency, "Transparency. range 0-100"); + 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?"); + sticky_history = + option_get_bool("global", "sticky_history", "-sticky_history", + sticky_history, + "Don't timeout notifications popped up from history"); + separator_height = + option_get_int("global", "separator_height", + "-sep_height/-separator_height", separator_height, + "height of the separator line"); + 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')"); + 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) sep_color = AUTO; else if (strcmp(c, "foreground") == 0) sep_color = FOREGROUND; else - fprintf(stderr, "Warning: Unknown separator color\n"); + fprintf(stderr, + "Warning: Unknown separator color\n"); free(c); } } - lowbgcolor = option_get_string("urgency_low", "background", "-lb", lowbgcolor, "Background color for notifcations with low urgency"); - lowfgcolor = option_get_string("urgency_low", "foreground", "-lf", lowfgcolor, "Foreground color for notifications with low urgency"); - timeouts[LOW] = option_get_int("urgency_low", "timeout", "-lto", timeouts[LOW], "Timeout for notifications with low urgency"); - normbgcolor = option_get_string("urgency_normal", "background", "-nb", normbgcolor, "Background color for notifications with normal urgency"); - normfgcolor = option_get_string("urgency_normal", "foreground", "-nf", normfgcolor, "Foreground color for notifications with normal urgency"); - timeouts[NORM] = option_get_int("urgency_normal", "timeout", "-nto", timeouts[NORM], "Timeout for notifications with normal urgency"); - critbgcolor = option_get_string("urgency_critical", "background", "-cb", critbgcolor, "Background color for notifications with critical urgency"); - critfgcolor = option_get_string("urgency_critical", "foreground", "-cf", critfgcolor, "Foreground color for notifications with ciritical urgency"); - timeouts[CRIT] = option_get_int("urgency_critical", "timeout", "-cto", timeouts[CRIT], "Timeout for notifications with critical urgency"); + lowbgcolor = + option_get_string("urgency_low", "background", "-lb", lowbgcolor, + "Background color for notifcations with low urgency"); + lowfgcolor = + option_get_string("urgency_low", "foreground", "-lf", lowfgcolor, + "Foreground color for notifications with low urgency"); + timeouts[LOW] = + option_get_int("urgency_low", "timeout", "-lto", timeouts[LOW], + "Timeout for notifications with low urgency"); + normbgcolor = + option_get_string("urgency_normal", "background", "-nb", + normbgcolor, + "Background color for notifications with normal urgency"); + normfgcolor = + option_get_string("urgency_normal", "foreground", "-nf", + normfgcolor, + "Foreground color for notifications with normal urgency"); + timeouts[NORM] = + option_get_int("urgency_normal", "timeout", "-nto", timeouts[NORM], + "Timeout for notifications with normal urgency"); + critbgcolor = + option_get_string("urgency_critical", "background", "-cb", + critbgcolor, + "Background color for notifications with critical urgency"); + critfgcolor = + option_get_string("urgency_critical", "foreground", "-cf", + critfgcolor, + "Foreground color for notifications with ciritical urgency"); + timeouts[CRIT] = + option_get_int("urgency_critical", "timeout", "-cto", + timeouts[CRIT], + "Timeout for notifications with critical urgency"); - close_ks.str = option_get_string("shortcuts", "close", "-key", close_ks.str, "Shortcut for closing one notification"); - close_all_ks.str = option_get_string("shortcuts", "close_all", "-all_key", close_all_ks.str, "Shortcut for closing all notifications"); - history_ks.str = option_get_string("shortcuts", "history", "-history_key", history_ks.str, "Shortcut to pop the last notification from history"); + close_ks.str = + option_get_string("shortcuts", "close", "-key", close_ks.str, + "Shortcut for closing one notification"); + close_all_ks.str = + option_get_string("shortcuts", "close_all", "-all_key", + close_all_ks.str, + "Shortcut for closing all notifications"); + history_ks.str = + option_get_string("shortcuts", "history", "-history_key", + history_ks.str, + "Shortcut to pop the last notification from history"); - print_notifications = cmdline_get_bool("-print", false, "Print notifications to cmdline (DEBUG)"); + print_notifications = + cmdline_get_bool("-print", false, + "Print notifications to cmdline (DEBUG)"); char *cur_section = NULL; for (;;) { @@ -1528,23 +1609,25 @@ void load_options(char *cmdline_config_path) 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) + || strcmp(cur_section, "shortcuts") == 0 + || strcmp(cur_section, "urgency_low") == 0 + || strcmp(cur_section, "urgency_normal") == 0 + || strcmp(cur_section, "urgency_critical") == 0) continue; rule_t *current_rule = dunst_rules_find_or_create(cur_section); - current_rule->appname = ini_get_string( - cur_section, "appname", current_rule->appname); - current_rule->summary = ini_get_string( - cur_section, "summary", current_rule->summary); - current_rule->body = ini_get_string( - cur_section, "body", current_rule->body); - current_rule->icon = ini_get_string( - cur_section, "icon", current_rule->icon); - current_rule->timeout = ini_get_int( - cur_section, "timeout", current_rule->timeout); + current_rule->appname = + ini_get_string(cur_section, "appname", + current_rule->appname); + current_rule->summary = + ini_get_string(cur_section, "summary", + current_rule->summary); + current_rule->body = + ini_get_string(cur_section, "body", current_rule->body); + current_rule->icon = + ini_get_string(cur_section, "icon", current_rule->icon); + current_rule->timeout = + ini_get_int(cur_section, "timeout", current_rule->timeout); { char *urg = ini_get_string(cur_section, "urgency", ""); if (strlen(urg) > 0) { @@ -1556,16 +1639,17 @@ void load_options(char *cmdline_config_path) current_rule->urgency = CRIT; else fprintf(stderr, - "unknown urgency: %s, ignoring\n", urg); + "unknown urgency: %s, ignoring\n", + urg); free(urg); } } - current_rule->fg = ini_get_string( - cur_section, "foreground", current_rule->fg); - current_rule->bg = ini_get_string( - cur_section, "background", current_rule->bg); - current_rule->format = ini_get_string( - cur_section, "format", current_rule->format); + current_rule->fg = + ini_get_string(cur_section, "foreground", current_rule->fg); + current_rule->bg = + ini_get_string(cur_section, "background", current_rule->bg); + current_rule->format = + ini_get_string(cur_section, "format", current_rule->format); } #ifndef STATIC_CONFIG @@ -1575,7 +1659,6 @@ void load_options(char *cmdline_config_path) #endif } - int main(int argc, char *argv[]) { now = time(&now); @@ -1591,20 +1674,22 @@ int main(int argc, char *argv[]) cmdline_load(argc, argv); - - if (cmdline_get_bool("-v/-version", false, "Print version") || cmdline_get_bool("--version", false, "Print version")) { + if (cmdline_get_bool("-v/-version", false, "Print version") + || cmdline_get_bool("--version", false, "Print version")) { print_version(); } char *cmdline_config_path; - cmdline_config_path = cmdline_get_string("-conf/-config", NULL, "Path to configuration file"); + cmdline_config_path = + cmdline_get_string("-conf/-config", NULL, + "Path to configuration file"); load_options(cmdline_config_path); - if (cmdline_get_bool("-h/-help", false, "Print help") || cmdline_get_bool("--help", false, "Print help")) { + if (cmdline_get_bool("-h/-help", false, "Print help") + || cmdline_get_bool("--help", false, "Print help")) { usage(EXIT_SUCCESS); } - initdbus(); setup(); @@ -1628,7 +1713,8 @@ void usage(int exit_status) void print_version(void) { - printf("Dunst - a dmenu-ish notification-daemon, version: %s\n", VERSION); + printf("Dunst - a dmenu-ish notification-daemon, version: %s\n", + VERSION); exit(EXIT_SUCCESS); } diff --git a/dunst.h b/dunst.h index b390998..e56e8d4 100644 --- a/dunst.h +++ b/dunst.h @@ -14,7 +14,6 @@ #define ColFG 1 #define ColBG 0 - enum alignment { left, center, right }; enum separator_color { FOREGROUND, AUTO }; enum follow_mode { FOLLOW_NONE, FOLLOW_MOUSE, FOLLOW_KEYBOARD }; @@ -51,7 +50,7 @@ typedef struct _notification { time_t timestamp; int timeout; int urgency; - bool redisplayed; /* has been displayed before? */ + bool redisplayed; /* has been displayed before? */ int id; int dup_count; ColorSet *colors; diff --git a/dunst_dbus.c b/dunst_dbus.c index 8590af5..e7a40af 100644 --- a/dunst_dbus.c +++ b/dunst_dbus.c @@ -336,11 +336,17 @@ void notify(DBusMessage * dmsg) continue; } dbus_message_iter_get_basic(&hint, &hint_name); - _extract_hint(DBUS_TYPE_BYTE, "urgency", hint_name, &hint, &urgency); - _extract_hint(DBUS_TYPE_STRING, "fgcolor", hint_name, &hint, &fgcolor); - _extract_hint(DBUS_TYPE_STRING, "bgcolor", hint_name, &hint, &bgcolor); - _extract_hint(DBUS_TYPE_INT32, "value", hint_name, &hint, &progress); - if (!progress) _extract_hint(DBUS_TYPE_UINT32, "value", hint_name, &hint, &progress); + _extract_hint(DBUS_TYPE_BYTE, "urgency", hint_name, + &hint, &urgency); + _extract_hint(DBUS_TYPE_STRING, "fgcolor", hint_name, + &hint, &fgcolor); + _extract_hint(DBUS_TYPE_STRING, "bgcolor", hint_name, + &hint, &bgcolor); + _extract_hint(DBUS_TYPE_INT32, "value", hint_name, + &hint, &progress); + if (!progress) + _extract_hint(DBUS_TYPE_UINT32, "value", + hint_name, &hint, &progress); dbus_message_iter_next(&hint); } dbus_message_iter_next(&hints); @@ -358,7 +364,7 @@ void notify(DBusMessage * dmsg) n->body = body != NULL ? strdup(body) : ""; n->icon = icon != NULL ? strdup(icon) : ""; n->timeout = expires; - n->progress = (progress < 0 || progress > 100) ? 0 : progress+1; + n->progress = (progress < 0 || progress > 100) ? 0 : progress + 1; n->urgency = urgency; n->dbus_client = strdup(dbus_message_get_sender(dmsg)); for (i = 0; i < ColLast; i++) { diff --git a/options.c b/options.c index 225b47d..29c0e58 100644 --- a/options.c +++ b/options.c @@ -11,20 +11,16 @@ #include "utils.h" typedef struct _entry_t { - char *key; - char *value; + char *key; + char *value; } entry_t; typedef struct _section_t { - char *name; - int entry_count; - entry_t *entries; + char *name; + int entry_count; + entry_t *entries; } section_t; - - - - static int section_count = 0; static section_t *sections; @@ -45,16 +41,16 @@ static int cmdline_find_option(char *key); section_t *new_section(char *name) { section_count++; - sections = realloc(sections, sizeof(section_t) *section_count); + sections = realloc(sections, sizeof(section_t) * section_count); sections[section_count - 1].name = strdup(name); sections[section_count - 1].entries = NULL; sections[section_count - 1].entry_count = 0; - return §ions[section_count -1]; + return §ions[section_count - 1]; } void free_ini(void) { - for(int i = 0; i < section_count; i++) { + for (int i = 0; i < section_count; i++) { for (int j = 0; j < sections[i].entry_count; j++) { free(sections[i].entries[j].key); free(sections[i].entries[j].value); @@ -145,7 +141,7 @@ char *next_section(char *section) if (i + 1 >= section_count) return NULL; else - return sections[i+1].name; + return sections[i + 1].name; } } return NULL; @@ -192,7 +188,7 @@ char *clean_value(char *value) } -int load_ini_file(FILE *fp) +int load_ini_file(FILE * fp) { char line[BUFSIZ]; @@ -209,7 +205,9 @@ int load_ini_file(FILE *fp) if (*start == '[') { char *end = strstr(start + 1, "]"); if (!end) { - printf("Warning: invalid config file at line %d\n", line_num); + printf + ("Warning: invalid config file at line %d\n", + line_num); printf("Missing ']'\n"); continue; } @@ -218,27 +216,30 @@ int load_ini_file(FILE *fp) if (current_section) free(current_section); - current_section = (strdup(start+1)); + current_section = (strdup(start + 1)); new_section(current_section); continue; } char *equal = strstr(start + 1, "="); if (!equal) { - printf("Warning: invalid config file at line %d\n", line_num); + printf("Warning: invalid config file at line %d\n", + line_num); printf("Missing '='\n"); continue; } *equal = '\0'; char *key = rstrip(start); - char *value = lskip(equal+1); + char *value = lskip(equal + 1); char *quote = strstr(value, "\""); if (quote) { char *closing_quote = strstr(quote + 1, "\""); if (!closing_quote) { - printf("Warning: invalid config file at line %d\n", line_num); + printf + ("Warning: invalid config file at line %d\n", + line_num); printf("Missing '\"'\n"); continue; } @@ -254,7 +255,8 @@ int load_ini_file(FILE *fp) value = rstrip(value); if (!current_section) { - printf("Warning: invalid config file at line: %d\n", line_num); + printf("Warning: invalid config file at line: %d\n", + line_num); printf("Key value pair without a section\n"); continue; } @@ -314,12 +316,13 @@ static char *cmdline_get_value(char *key) return NULL; } - if (idx + 1 >= cmdline_argc || cmdline_argv[idx+1][0] == '-') { + if (idx + 1 >= cmdline_argc || cmdline_argv[idx + 1][0] == '-') { /* the argument is missing */ - fprintf(stderr, "Warning: %s, missing argument. Ignoring\n", key); + fprintf(stderr, "Warning: %s, missing argument. Ignoring\n", + key); return NULL; } - return cmdline_argv[idx+1]; + return cmdline_argv[idx + 1]; } char *cmdline_get_string(char *key, char *def, char *description) @@ -364,7 +367,8 @@ int cmdline_get_bool(char *key, int def, char *description) return def; } -char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, char *def, char *description) +char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, + char *def, char *description) { char *val = NULL; @@ -380,7 +384,8 @@ char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, cha } -int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, char *description) +int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, + char *description) { /* *str is only used to check wether the cmdline option is actually set. */ char *str = cmdline_get_value(cmdline_key); @@ -395,7 +400,8 @@ int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, return val; } -double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, double def, char *description) +double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, + double def, char *description) { char *str = cmdline_get_value(cmdline_key); double val = cmdline_get_double(cmdline_key, def, description); @@ -406,7 +412,8 @@ double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, do return val; } -int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, int def, char *description) +int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, + int def, char *description) { int val; @@ -449,4 +456,5 @@ char *cmdline_create_usage(void) { return strdup(usage_str); } + /* vim: set ts=8 sw=8 tw=0: */ diff --git a/options.h b/options.h index e752e9e..dd34a79 100644 --- a/options.h +++ b/options.h @@ -3,7 +3,6 @@ #include - int load_ini_file(FILE *); char *ini_get_string(char *section, char *key, const char *def); int ini_get_int(char *section, char *key, int def); @@ -19,10 +18,14 @@ double cmdline_get_double(char *key, double def, char *description); int cmdline_get_bool(char *key, int def, char *description); char *cmdline_create_usage(void); -char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, char *def, char *description); -int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, char *description); -double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, double def, char *description); -int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, int def, char *description); +char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, + char *def, char *description); +int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, + char *description); +double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, + double def, char *description); +int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, + int def, char *description); /* returns the next known section. * if section == NULL returns first section. diff --git a/utils.c b/utils.c index e587429..895af89 100644 --- a/utils.c +++ b/utils.c @@ -6,7 +6,6 @@ #include "utils.h" #include "dunst.h" - char *rstrip(char *str) { char *end; @@ -20,7 +19,7 @@ char *rstrip(char *str) char *lskip(char *s) { - for(; *s && isspace(*s); s++); + for (; *s && isspace(*s); s++) ; return s; } @@ -54,7 +53,7 @@ char *string_replace(const char *needle, const char *replacement, int digit_count(int i) { int len = 0; - if ( i == 0) { + if (i == 0) { return 1; } diff --git a/utils.h b/utils.h index 8995b01..3d3774f 100644 --- a/utils.h +++ b/utils.h @@ -9,7 +9,7 @@ char *string_replace(const char *needle, const char *replacement, char *haystack); /* exit with an error message */ -void die(char * msg, int exit_value); +void die(char *msg, int exit_value); int digit_count(int i);