From caf68ab21e373594866ef2b26736d3ae02ac6659 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Tue, 13 Dec 2011 11:29:11 +0100 Subject: Add a list_foreach makro for GList Saves around 1-2 lines per foreach, and should be more error prone. --- src/command.c | 18 ++++++------------ src/interface.c | 27 +++++++++++---------------- src/pa-sink-ctl.c | 18 ++++++------------ src/pa-sink-ctl.h | 3 +++ 4 files changed, 26 insertions(+), 40 deletions(-) diff --git a/src/command.c b/src/command.c index db131e4..3abe744 100644 --- a/src/command.c +++ b/src/command.c @@ -27,15 +27,12 @@ static int sink_input_len(struct context *ctx, struct sink_info *sink) { + struct sink_input_info *input; int len = 0; - GList *l; - - for (l = ctx->input_list; l; l = l->next) { - struct sink_input_info *input = l->data; + list_foreach(ctx->input_list, input) if (input->sink == sink->index) len++; - } return len; } @@ -43,11 +40,10 @@ sink_input_len(struct context *ctx, struct sink_info *sink) static struct sink_input_info * sink_get_nth_input(struct context *ctx, struct sink_info *sink, int n) { - GList *l; + struct sink_input_info *input; int i = 0; - for (l = ctx->input_list; l; l = l->next) { - struct sink_input_info *input = l->data; + list_foreach(ctx->input_list, input) { if (input->sink != sink->index) continue; if (i++ == n) @@ -182,7 +178,7 @@ static void switch_sink(struct context *ctx, int key) { struct sink_info *sink = NULL; - struct sink_input_info *input; + struct sink_input_info *input, *t; pa_operation *o; gint i; @@ -207,9 +203,7 @@ switch_sink(struct context *ctx, int key) /* get new chooser_input, if non, select sink as fallback */ ctx->chooser_input = SELECTED_SINK; i = -1; - for (GList *l = ctx->input_list; l; l = l->next) { - struct sink_input_info *t = l->data; - + list_foreach(ctx->input_list, t) { if (t->index == input->index) { ctx->chooser_input = ++i; break; diff --git a/src/interface.c b/src/interface.c index 8d9819a..54859f0 100644 --- a/src/interface.c +++ b/src/interface.c @@ -89,12 +89,11 @@ static void print_input_list(struct context *ctx, struct sink_info *sink, gint sink_num, gint *poffset) { - GList *l; + struct sink_input_info *input; gint offset = *poffset; - gint i; + gint i = -1; - for (l = ctx->input_list, i = -1; l; l = l->next) { - struct sink_input_info *input = l->data; + list_foreach(ctx->input_list, input) { if (input->sink != sink->index) continue; gboolean selected = (ctx->chooser_sink == sink_num && @@ -120,22 +119,19 @@ print_input_list(struct context *ctx, struct sink_info *sink, static void set_max_name_len(struct context *ctx) { - GList *l; + struct sink_info *sink; + struct sink_input_info *input; guint len = 0; ctx->max_name_len = len; - for (l = ctx->sink_list; l; l = l->next) { - struct sink_info *sink = l->data; - + list_foreach(ctx->sink_list, sink) { len = strlen(sink->name); if (len > ctx->max_name_len) ctx->max_name_len = len; } - for (l = ctx->input_list; l; l = l->next) { - struct sink_input_info *input = l->data; - + list_foreach(ctx->input_list, input) { len = strlen(input->name) + 1 /* indentation */; if (len > ctx->max_name_len) @@ -146,10 +142,10 @@ set_max_name_len(struct context *ctx) void print_sink_list(struct context *ctx) { - gint i = 0; + struct sink_info *sink; + gint i = -1; gint x = 2; gint offset = 2; /* top border + 1 empty line */ - GList *l; /* looking for the longest name for right indentation */ set_max_name_len(ctx); @@ -157,9 +153,8 @@ print_sink_list(struct context *ctx) werase(ctx->menu_win); box(ctx->menu_win, 0, 0); - for (l = ctx->sink_list, i = 0; l; l = l->next,++i) { - struct sink_info *sink = l->data; - gboolean selected = (i == ctx->chooser_sink && + list_foreach(ctx->sink_list, sink) { + gboolean selected = (++i == ctx->chooser_sink && ctx->chooser_input == SELECTED_SINK); if (selected) diff --git a/src/pa-sink-ctl.c b/src/pa-sink-ctl.c index 24c9d6c..3b0a827 100644 --- a/src/pa-sink-ctl.c +++ b/src/pa-sink-ctl.c @@ -29,13 +29,11 @@ static struct sink_input_info * find_sink_input_by_idx(struct context *ctx, gint idx) { - GList *l; + struct sink_input_info *input; - for (l = ctx->input_list; l; l = l->next) { - struct sink_input_info *input = l->data; + list_foreach(ctx->input_list, input) if (input->index == idx) return input; - } return NULL; } @@ -43,13 +41,11 @@ find_sink_input_by_idx(struct context *ctx, gint idx) static struct sink_info * find_sink_by_idx(struct context *ctx, gint idx) { - GList *l; + struct sink_info *sink; - for (l = ctx->sink_list; l; l = l->next) { - struct sink_info *sink = l->data; + list_foreach(ctx->sink_list, sink) if (sink->index == idx) return sink; - } return NULL; } @@ -99,12 +95,10 @@ sink_input_info_cb(pa_context *c, const pa_sink_input_info *i, static int get_sink_priority(struct context *ctx, const pa_sink_info *sink_info) { - GList *l; + struct priority *p; const char *value; - for (l = ctx->config.priorities; l; l = l->next) { - struct priority *p = l->data; - + list_foreach(ctx->config.priorities, p) { value = pa_proplist_gets(sink_info->proplist, p->match); if (g_strcmp0(value, p->value) == 0) diff --git a/src/pa-sink-ctl.h b/src/pa-sink-ctl.h index d8cd5bb..f56b17c 100644 --- a/src/pa-sink-ctl.h +++ b/src/pa-sink-ctl.h @@ -67,4 +67,7 @@ change_callback(pa_context* c, gint success, gpointer); g_memdup(&(data), sizeof(data))); \ } while (0) +#define list_foreach(list, el) \ + for (GList *__l = (list); __l && ((el) = __l->data); __l = __l->next) + #endif -- cgit