Merge pull request #418 from bebehei/microopt

Microopt
This commit is contained in:
Nikos Tsipinakis 2017-10-24 06:03:58 +03:00 committed by GitHub
commit a2575566f5
8 changed files with 76 additions and 90 deletions

View File

@ -376,36 +376,36 @@ static void on_name_lost(GDBusConnection *connection,
static RawImage *get_raw_image_from_data_hint(GVariant *icon_data) static RawImage *get_raw_image_from_data_hint(GVariant *icon_data)
{ {
RawImage *image = g_malloc(sizeof(RawImage)); RawImage *image = g_malloc(sizeof(RawImage));
GVariant *data_variant; GVariant *data_variant;
gsize expected_len; gsize expected_len;
g_variant_get (icon_data, g_variant_get (icon_data,
"(iiibii@ay)", "(iiibii@ay)",
&image->width, &image->width,
&image->height, &image->height,
&image->rowstride, &image->rowstride,
&image->has_alpha, &image->has_alpha,
&image->bits_per_sample, &image->bits_per_sample,
&image->n_channels, &image->n_channels,
&data_variant); &data_variant);
expected_len = (image->height - 1) * image->rowstride + image->width expected_len = (image->height - 1) * image->rowstride + image->width
* ((image->n_channels * image->bits_per_sample + 7) / 8); * ((image->n_channels * image->bits_per_sample + 7) / 8);
if (expected_len != g_variant_get_size (data_variant)) { if (expected_len != g_variant_get_size (data_variant)) {
fprintf(stderr, "Expected image data to be of length %" G_GSIZE_FORMAT fprintf(stderr, "Expected image data to be of length %" G_GSIZE_FORMAT
" but got a " "length of %" G_GSIZE_FORMAT, " but got a " "length of %" G_GSIZE_FORMAT,
expected_len, expected_len,
g_variant_get_size (data_variant)); g_variant_get_size (data_variant));
g_free(image); g_free(image);
return NULL; return NULL;
} }
image->data = (guchar *) g_memdup (g_variant_get_data (data_variant), image->data = (guchar *) g_memdup (g_variant_get_data (data_variant),
g_variant_get_size (data_variant)); g_variant_get_size (data_variant));
return image; return image;
} }
int initdbus(void) int initdbus(void)

View File

@ -20,8 +20,6 @@
#include "x11/x.h" #include "x11/x.h"
#include "x11/screen.h" #include "x11/screen.h"
#define LENGTH(X) (sizeof X / sizeof X[0])
#ifndef VERSION #ifndef VERSION
#define VERSION "version info needed" #define VERSION "version info needed"
#endif #endif
@ -318,7 +316,7 @@ int dunst_main(int argc, char *argv[])
n->summary = g_strdup("startup"); n->summary = g_strdup("startup");
n->body = g_strdup("dunst is up and running"); n->body = g_strdup("dunst is up and running");
n->progress = 0; n->progress = 0;
n->timeout = 10; n->timeout = 10 * G_USEC_PER_SEC;
n->markup = MARKUP_NO; n->markup = MARKUP_NO;
n->urgency = LOW; n->urgency = LOW;
notification_init(n, 0); notification_init(n, 0);

View File

@ -9,9 +9,7 @@
#include "notification.h" #include "notification.h"
#define ERR(msg) printf("%s : %d\n", (msg), __LINE__)
#define PERR(msg, errnum) printf("(%d) %s : %s\n", __LINE__, (msg), (strerror(errnum))) #define PERR(msg, errnum) printf("(%d) %s : %s\n", __LINE__, (msg), (strerror(errnum)))
#define LENGTH(X) (sizeof X / sizeof X[0])
#define ColLast 3 #define ColLast 3
#define ColFrame 2 #define ColFrame 2

View File

@ -176,12 +176,8 @@ void notification_free(notification *n)
g_free(n->msg); g_free(n->msg);
g_free(n->dbus_client); g_free(n->dbus_client);
g_free(n->category); g_free(n->category);
g_free(n->text_to_render);
if (n->text_to_render) g_free(n->urls);
g_free(n->text_to_render);
if (n->urls)
g_free(n->urls);
if (n->actions) { if (n->actions) {
g_strfreev(n->actions->actions); g_strfreev(n->actions->actions);
@ -189,9 +185,9 @@ void notification_free(notification *n)
} }
if (n->raw_icon) { if (n->raw_icon) {
if (n->raw_icon->data) if (n->raw_icon->data)
g_free(n->raw_icon->data); g_free(n->raw_icon->data);
g_free(n->raw_icon); g_free(n->raw_icon);
} }
g_free(n); g_free(n);
@ -226,42 +222,42 @@ void notification_replace_single_field(char **haystack, char **needle,
} }
char *notification_extract_markup_urls(char **str_ptr) { char *notification_extract_markup_urls(char **str_ptr) {
char *start, *end, *replace_buf, *str, *urls = NULL, *url, *index_buf; char *start, *end, *replace_buf, *str, *urls = NULL, *url, *index_buf;
int linkno = 1; int linkno = 1;
str = *str_ptr; str = *str_ptr;
while ((start = strstr(str, "<a href")) != NULL) { while ((start = strstr(str, "<a href")) != NULL) {
end = strstr(start, ">"); end = strstr(start, ">");
if (end != NULL) { if (end != NULL) {
replace_buf = g_strndup(start, end - start + 1); replace_buf = g_strndup(start, end - start + 1);
url = extract_urls(replace_buf); url = extract_urls(replace_buf);
if (url != NULL) { if (url != NULL) {
str = string_replace(replace_buf, "[", str); str = string_replace(replace_buf, "[", str);
index_buf = g_strdup_printf("[#%d]", linkno++); index_buf = g_strdup_printf("[#%d]", linkno++);
if (urls == NULL) { if (urls == NULL) {
urls = g_strconcat(index_buf, " ", url, NULL); urls = g_strconcat(index_buf, " ", url, NULL);
} else { } else {
char *tmp = urls; char *tmp = urls;
urls = g_strconcat(tmp, "\n", index_buf, " ", url, NULL); urls = g_strconcat(tmp, "\n", index_buf, " ", url, NULL);
g_free(tmp); g_free(tmp);
} }
index_buf[0] = ' '; index_buf[0] = ' ';
str = string_replace("</a>", index_buf, str); str = string_replace("</a>", index_buf, str);
g_free(index_buf); g_free(index_buf);
g_free(url); g_free(url);
} else {
str = string_replace(replace_buf, "", str);
str = string_replace("</a>", "", str);
}
g_free(replace_buf);
} else { } else {
str = string_replace(replace_buf, "", str); break;
str = string_replace("</a>", "", str);
} }
g_free(replace_buf);
} else {
break;
} }
} *str_ptr = str;
*str_ptr = str; return urls;
return urls;
} }
/* /*
@ -642,10 +638,8 @@ bool notification_replace_by_id(notification *new)
void notification_update_text_to_render(notification *n) void notification_update_text_to_render(notification *n)
{ {
if (n->text_to_render) { g_free(n->text_to_render);
g_free(n->text_to_render); n->text_to_render = NULL;
n->text_to_render = NULL;
}
char *buf = NULL; char *buf = NULL;

View File

@ -248,8 +248,7 @@ int load_ini_file(FILE *fp)
*end = '\0'; *end = '\0';
if (current_section) g_free(current_section);
g_free(current_section);
current_section = (g_strdup(start + 1)); current_section = (g_strdup(start + 1));
new_section(current_section); new_section(current_section);
continue; continue;
@ -296,8 +295,7 @@ int load_ini_file(FILE *fp)
add_entry(current_section, key, value); add_entry(current_section, key, value);
} }
free(line); free(line);
if (current_section) g_free(current_section);
g_free(current_section);
return 0; return 0;
} }

View File

@ -23,8 +23,7 @@ void rule_apply(rule_t *r, notification *n)
if (r->markup != MARKUP_NULL) if (r->markup != MARKUP_NULL)
n->markup = r->markup; n->markup = r->markup;
if (r->new_icon) { if (r->new_icon) {
if(n->icon) g_free(n->icon);
g_free(n->icon);
n->icon = g_strdup(r->new_icon); n->icon = g_strdup(r->new_icon);
n->icon_overridden = true; n->icon_overridden = true;
} }

View File

@ -64,8 +64,7 @@ static int ini_get_urgency(const char *section, const char *key, const int def)
"unknown urgency: %s, ignoring\n", "unknown urgency: %s, ignoring\n",
urg); urg);
} }
if (urg) g_free(urg);
g_free(urg);
return ret; return ret;
} }
@ -601,7 +600,7 @@ void load_settings(char *cmdline_config_path)
); );
/* push hardcoded default rules into rules list */ /* push hardcoded default rules into rules list */
for (int i = 0; i < LENGTH(default_rules); i++) { for (int i = 0; i < G_N_ELEMENTS(default_rules); i++) {
rules = g_slist_insert(rules, &(default_rules[i]), -1); rules = g_slist_insert(rules, &(default_rules[i]), -1);
} }

View File

@ -488,11 +488,11 @@ static colored_layout *r_init_shared(cairo_t *c, notification *n)
static colored_layout *r_create_layout_for_xmore(cairo_t *c, notification *n, int qlen) static colored_layout *r_create_layout_for_xmore(cairo_t *c, notification *n, int qlen)
{ {
colored_layout *cl = r_init_shared(c, n); colored_layout *cl = r_init_shared(c, n);
cl->text = g_strdup_printf("(%d more)", qlen); cl->text = g_strdup_printf("(%d more)", qlen);
cl->attr = NULL; cl->attr = NULL;
pango_layout_set_text(cl->l, cl->text, -1); pango_layout_set_text(cl->l, cl->text, -1);
return cl; return cl;
} }
static colored_layout *r_create_layout_from_notification(cairo_t *c, notification *n) static colored_layout *r_create_layout_from_notification(cairo_t *c, notification *n)
@ -610,9 +610,9 @@ static dimension_t x_render_layout(cairo_t *c, colored_layout *cl, colored_layou
bool use_padding = settings.notification_height <= (2 * settings.padding) + h; bool use_padding = settings.notification_height <= (2 * settings.padding) + h;
if (use_padding) if (use_padding)
dim.y += settings.padding; dim.y += settings.padding;
else else
dim.y += (int) (ceil(bg_half_height) - pango_offset); dim.y += (int) (ceil(bg_half_height) - pango_offset);
if (cl->icon && settings.icon_position == icons_left) { if (cl->icon && settings.icon_position == icons_left) {
cairo_move_to(c, settings.frame_width + cairo_image_surface_get_width(cl->icon) + 2 * settings.h_padding, bg_y + settings.padding + h/2 - h_text/2); cairo_move_to(c, settings.frame_width + cairo_image_surface_get_width(cl->icon) + 2 * settings.h_padding, bg_y + settings.padding + h/2 - h_text/2);
@ -626,9 +626,9 @@ static dimension_t x_render_layout(cairo_t *c, colored_layout *cl, colored_layou
pango_cairo_update_layout(c, cl->l); pango_cairo_update_layout(c, cl->l);
pango_cairo_show_layout(c, cl->l); pango_cairo_show_layout(c, cl->l);
if (use_padding) if (use_padding)
dim.y += h + settings.padding; dim.y += h + settings.padding;
else else
dim.y += (int) (floor(bg_half_height) + pango_offset); dim.y += (int) (floor(bg_half_height) + pango_offset);
if (settings.separator_height > 0 && !last) { if (settings.separator_height > 0 && !last) {
color_t sep_color = x_get_separator_color(cl, cl_next); color_t sep_color = x_get_separator_color(cl, cl_next);