diff --git a/Makefile b/Makefile index 3d62693..ed17495 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include config.mk -SRC = draw.c dunst.c list.c dunst_dbus.c ini.c +SRC = draw.c dunst.c list.c dunst_dbus.c ini.c utils.c OBJ = ${SRC:.c=.o} all: doc options dunst @@ -20,9 +20,9 @@ options: ${OBJ}: config.mk -dunst: draw.o dunst.o list.o dunst_dbus.o ini.o +dunst: draw.o dunst.o list.o dunst_dbus.o ini.o utils.o @echo CC -o $@ - @${CC} ${CFLAGS} -o $@ dunst.o draw.o list.o dunst_dbus.o ini.o ${LDFLAGS} + @${CC} ${CFLAGS} -o $@ dunst.o draw.o list.o dunst_dbus.o ini.o utils.o ${LDFLAGS} clean: @echo cleaning diff --git a/dunst.c b/dunst.c index 9cbf7d4..3714e1b 100644 --- a/dunst.c +++ b/dunst.c @@ -24,6 +24,7 @@ #include "dunst_dbus.h" #include "list.h" #include "ini.h" +#include "utils.h" #define INRECT(x,y,rx,ry,rw,rh) ((x) >= (rx) && (x) < (rx)+(rw) && (y) >= (ry) && (y) < (ry)+(rh)) #define LENGTH(X) (sizeof X / sizeof X[0]) @@ -53,6 +54,9 @@ typedef struct _notification_buffer { } notification_buffer; /* global variables */ + /* extern */ +int verbosity = 0; + char *font = "-*-terminus-medium-r-*-*-16-*-*-*-*-*-*-*"; char *normbgcolor = "#1793D1"; char *normfgcolor = "#DDDDDD"; @@ -72,7 +76,6 @@ int show_age_threshold = -1; enum alignment align = left; int sticky_history = True; -int verbosity = 0; list *rules = NULL; /* index of colors fit to urgency level */ @@ -116,16 +119,12 @@ list *notification_history = NULL; /* history of displayed notifications */ void apply_rules(notification * n); void check_timeouts(void); void draw_notifications(void); -void dunst_printf(int level, const char *fmt, ...); char *fix_markup(char *str); void handle_mouse_click(XEvent ev); void handleXEvents(void); void history_pop(void); rule_t *initrule(void); int is_idle(void); -char *string_replace(const char *needle, const char *replacement, - char *haystack); -char *strtrim(char *str); void run(void); void setup(void); void update_screen_info(); @@ -136,16 +135,13 @@ void draw_win(void); void hide_win(void); void move_all_to_history(void); void print_version(void); + +/* show warning notification */ void warn(const char * text, int urg); void init_shortcut(keyboard_shortcut * shortcut); KeySym string_to_mask(char *str); -void die(char *text, int exit_value) -{ - fputs(text, stderr); - exit(exit_value); -} void warn(const char *text, int urg) { @@ -531,17 +527,6 @@ void draw_win(void) free(n_buf); } -void dunst_printf(int level, const char *fmt, ...) -{ - va_list ap; - - if (level > verbosity) { - return; - } - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); -} char *fix_markup(char *str) @@ -929,48 +914,6 @@ int is_idle(void) return screensaver_info->idle / 1000 > idle_threshold; } -char *string_replace(const char *needle, const char *replacement, - char *haystack) -{ - char *tmp, *start; - int size; - start = strstr(haystack, needle); - if (start == NULL) { - return haystack; - } - - size = (strlen(haystack) - strlen(needle)) + strlen(replacement) + 1; - tmp = calloc(sizeof(char), size); - memset(tmp, '\0', size); - - strncpy(tmp, haystack, start - haystack); - tmp[start - haystack] = '\0'; - - sprintf(tmp + strlen(tmp), "%s%s", replacement, start + strlen(needle)); - free(haystack); - - if (strstr(tmp, needle)) { - return string_replace(needle, replacement, tmp); - } else { - return tmp; - } -} - -char *strtrim(char *str) -{ - char *end; - while (isspace(*str)) - str++; - - end = str + strlen(str) - 1; - while (isspace(*end)) { - *end = '\0'; - end--; - } - - return str; - -} void run(void) { diff --git a/dunst.h b/dunst.h index 86bf751..44a55c8 100644 --- a/dunst.h +++ b/dunst.h @@ -1,7 +1,6 @@ /* copyright 2012 Sascha Kruse and contributors (see LICENSE for licensing information) */ -#ifndef DUNST_H -#define DUNST_H +#pragma once #include "draw.h" @@ -13,6 +12,7 @@ #define ColFG 1 #define ColBG 0 + enum alignment { left, center, right }; typedef struct _rule_t { @@ -67,11 +67,12 @@ typedef struct _keyboard_shortcut { int is_valid; } keyboard_shortcut; +extern int verbosity; + /* return id of notification */ int init_notification(notification * n, int id); int close_notification(notification * n, int reason); int close_notification_by_id(int id, int reason); void map_win(void); -#endif /* vim: set ts=8 sw=8 tw=0: */ diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..7505a6c --- /dev/null +++ b/utils.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +#include "utils.h" +#include "dunst.h" + + +char *strtrim(char *str) +{ + char *end; + while (isspace(*str)) + str++; + + end = str + strlen(str) - 1; + while (isspace(*end)) { + *end = '\0'; + end--; + } + + return str; + +} + +char *string_replace(const char *needle, const char *replacement, + char *haystack) +{ + char *tmp, *start; + int size; + start = strstr(haystack, needle); + if (start == NULL) { + return haystack; + } + + size = (strlen(haystack) - strlen(needle)) + strlen(replacement) + 1; + tmp = calloc(sizeof(char), size); + memset(tmp, '\0', size); + + strncpy(tmp, haystack, start - haystack); + tmp[start - haystack] = '\0'; + + sprintf(tmp + strlen(tmp), "%s%s", replacement, start + strlen(needle)); + free(haystack); + + if (strstr(tmp, needle)) { + return string_replace(needle, replacement, tmp); + } else { + return tmp; + } +} + +void dunst_printf(int level, const char *fmt, ...) +{ + va_list ap; + + if (level > verbosity) { + return; + } + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + +void die(char *text, int exit_value) +{ + fputs(text, stderr); + exit(exit_value); +} + +/* vim: set ts=8 sw=8 tw=0: */ diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..fb4b36f --- /dev/null +++ b/utils.h @@ -0,0 +1,18 @@ +#ifndef UTIL_H +#define UTIL_H +/* remove spaces before and after str */ +char *strtrim(char *str); + +/* replace needle with replacement in haystack */ +char *string_replace(const char *needle, const char *replacement, + char *haystack); + +/* exit with an error message */ +void die(char * msg, int exit_value); + +/* print depending on verbosity */ +void dunst_printf(int level, const char *fmt, ...); + +#endif + +/* vim: set ts=8 sw=8 tw=0: */