utils.{c,h}

This commit is contained in:
Sascha Kruse 2012-07-25 08:46:36 +02:00
parent a369f5d344
commit d4f38c0533
5 changed files with 102 additions and 69 deletions

View File

@ -3,7 +3,7 @@
include config.mk 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} OBJ = ${SRC:.c=.o}
all: doc options dunst all: doc options dunst
@ -20,9 +20,9 @@ options:
${OBJ}: config.mk ${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 $@ @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: clean:
@echo cleaning @echo cleaning

69
dunst.c
View File

@ -24,6 +24,7 @@
#include "dunst_dbus.h" #include "dunst_dbus.h"
#include "list.h" #include "list.h"
#include "ini.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 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]) #define LENGTH(X) (sizeof X / sizeof X[0])
@ -53,6 +54,9 @@ typedef struct _notification_buffer {
} notification_buffer; } notification_buffer;
/* global variables */ /* global variables */
/* extern */
int verbosity = 0;
char *font = "-*-terminus-medium-r-*-*-16-*-*-*-*-*-*-*"; char *font = "-*-terminus-medium-r-*-*-16-*-*-*-*-*-*-*";
char *normbgcolor = "#1793D1"; char *normbgcolor = "#1793D1";
char *normfgcolor = "#DDDDDD"; char *normfgcolor = "#DDDDDD";
@ -72,7 +76,6 @@ int show_age_threshold = -1;
enum alignment align = left; enum alignment align = left;
int sticky_history = True; int sticky_history = True;
int verbosity = 0;
list *rules = NULL; list *rules = NULL;
/* index of colors fit to urgency level */ /* 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 apply_rules(notification * n);
void check_timeouts(void); void check_timeouts(void);
void draw_notifications(void); void draw_notifications(void);
void dunst_printf(int level, const char *fmt, ...);
char *fix_markup(char *str); char *fix_markup(char *str);
void handle_mouse_click(XEvent ev); void handle_mouse_click(XEvent ev);
void handleXEvents(void); void handleXEvents(void);
void history_pop(void); void history_pop(void);
rule_t *initrule(void); rule_t *initrule(void);
int is_idle(void); int is_idle(void);
char *string_replace(const char *needle, const char *replacement,
char *haystack);
char *strtrim(char *str);
void run(void); void run(void);
void setup(void); void setup(void);
void update_screen_info(); void update_screen_info();
@ -136,16 +135,13 @@ void draw_win(void);
void hide_win(void); void hide_win(void);
void move_all_to_history(void); void move_all_to_history(void);
void print_version(void); void print_version(void);
/* show warning notification */
void warn(const char * text, int urg); void warn(const char * text, int urg);
void init_shortcut(keyboard_shortcut * shortcut); void init_shortcut(keyboard_shortcut * shortcut);
KeySym string_to_mask(char *str); 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) void warn(const char *text, int urg)
{ {
@ -531,17 +527,6 @@ void draw_win(void)
free(n_buf); 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 char
*fix_markup(char *str) *fix_markup(char *str)
@ -929,48 +914,6 @@ int is_idle(void)
return screensaver_info->idle / 1000 > idle_threshold; 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) void run(void)
{ {

View File

@ -1,7 +1,6 @@
/* copyright 2012 Sascha Kruse and contributors (see LICENSE for licensing information) */ /* copyright 2012 Sascha Kruse and contributors (see LICENSE for licensing information) */
#ifndef DUNST_H #pragma once
#define DUNST_H
#include "draw.h" #include "draw.h"
@ -13,6 +12,7 @@
#define ColFG 1 #define ColFG 1
#define ColBG 0 #define ColBG 0
enum alignment { left, center, right }; enum alignment { left, center, right };
typedef struct _rule_t { typedef struct _rule_t {
@ -67,11 +67,12 @@ typedef struct _keyboard_shortcut {
int is_valid; int is_valid;
} keyboard_shortcut; } keyboard_shortcut;
extern int verbosity;
/* return id of notification */ /* return id of notification */
int init_notification(notification * n, int id); int init_notification(notification * n, int id);
int close_notification(notification * n, int reason); int close_notification(notification * n, int reason);
int close_notification_by_id(int id, int reason); int close_notification_by_id(int id, int reason);
void map_win(void); void map_win(void);
#endif
/* vim: set ts=8 sw=8 tw=0: */ /* vim: set ts=8 sw=8 tw=0: */

71
utils.c Normal file
View File

@ -0,0 +1,71 @@
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#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: */

18
utils.h Normal file
View File

@ -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: */