summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Franzke <benjaminfranzke@googlemail.com>2011-09-24 12:54:31 +0200
committerBenjamin Franzke <benjaminfranzke@googlemail.com>2011-09-24 12:59:42 +0200
commitc820c3d4830b2c5db1c026ee332f2d773c366247 (patch)
tree993f5622d79236cdec2b86aa05f115ea2875c057
parent99961ccd710e5300de5fb749aa10619632ac96e9 (diff)
downloadcmumble-c820c3d4830b2c5db1c026ee332f2d773c366247.tar.gz
cmumble-c820c3d4830b2c5db1c026ee332f2d773c366247.tar.bz2
cmumble-c820c3d4830b2c5db1c026ee332f2d773c366247.zip
Add a general element finding routine to util.c
And use this to implement find_user and find_channel as inline functions.
-rw-r--r--src/Makefile.am4
-rw-r--r--src/cmumble.c33
-rw-r--r--src/cmumble.h2
-rw-r--r--src/commands.c2
-rw-r--r--src/util.c18
-rw-r--r--src/util.h26
6 files changed, 50 insertions, 35 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 40d19e8..567b3f9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,11 +1,11 @@
bin_PROGRAMS = cmumble
noinst_HEADERS = cmumble.h message.h varint.h io.h \
- connection.h audio.h commands.h
+ connection.h audio.h commands.h util.h
nodist_noinst_HEADERS = mumble.pb-c.h message_list.h
cmumble_SOURCES = cmumble.c message.c varint.c io.c \
- connection.c audio.c commands.c
+ connection.c audio.c commands.c util.c
nodist_cmumble_SOURCES = mumble.pb-c.c
cmumble_LDADD = $(PROTOBUF_LIBS) $(GLIB_LIBS) $(GIO_LIBS) \
diff --git a/src/cmumble.c b/src/cmumble.c
index cc2d22d..3599f91 100644
--- a/src/cmumble.c
+++ b/src/cmumble.c
@@ -6,36 +6,7 @@
#include "cmumble.h"
#include "io.h"
#include "connection.h"
-
-static struct cmumble_user *
-find_user(struct cmumble_context *ctx, uint32_t session)
-{
- struct cmumble_user *user = NULL;
- GList *l;
-
- for (l = ctx->users; l; l = l->next)
- if (((struct cmumble_user *) l->data)->session == session) {
- user = l->data;
- break;
- }
-
- return user;
-}
-
-static struct cmumble_channel *
-find_channel(struct cmumble_context *ctx, uint32_t id)
-{
- struct cmumble_channel *channel = NULL;
- GList *l;
-
- for (l = ctx->channels; l; l = l->next)
- if (((struct cmumble_channel *) l->data)->id == id) {
- channel = l->data;
- break;
- }
-
- return channel;
-}
+#include "util.h"
static void
recv_udp_tunnel(MumbleProto__UDPTunnel *tunnel, struct cmumble_context *ctx)
@@ -185,7 +156,7 @@ recv_user_state(MumbleProto__UserState *state, struct cmumble_context *ctx)
user->session = state->session;
user->name = g_strdup(state->name);
- user->user_id = state->user_id;
+ user->id = state->user_id;
cmumble_audio_create_playback_pipeline(ctx, user);
diff --git a/src/cmumble.h b/src/cmumble.h
index 9078bb8..a1950b8 100644
--- a/src/cmumble.h
+++ b/src/cmumble.h
@@ -37,7 +37,7 @@ struct cmumble_context {
struct cmumble_user {
uint32_t session;
char *name;
- uint32_t user_id;
+ uint32_t id;
GstElement *pipeline;
GstAppSrc *src;
diff --git a/src/commands.c b/src/commands.c
index 0e24f70..32746f6 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -16,7 +16,7 @@ list_users(struct cmumble_context *ctx)
for (l = ctx->users; l; l = l->next) {
user = l->data;
- g_print("%4d: %s\n", user->user_id, user->name);
+ g_print("%4d: %s\n", user->id, user->name);
}
}
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..a98731b
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,18 @@
+#include "util.h"
+#include <glib.h>
+
+gpointer
+cmumble_find_by_id(GList *list, gsize member_offset, guint id)
+{
+ gpointer el = NULL;
+ GList *l;
+
+ for (l = list; l; l = l->next) {
+ if (G_STRUCT_MEMBER(uint32_t, l, member_offset) == id) {
+ el = l->data;
+ break;
+ }
+ }
+
+ return el;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..dab7b54
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,26 @@
+#ifndef _UTIL_H_
+#define _UTIL_H_
+
+#include <glib.h>
+#include "cmumble.h"
+
+gpointer
+cmumble_find_by_id(GList *list, gsize member_offset, guint id);
+
+static inline struct cmumble_user *
+find_user(struct cmumble_context *ctx, uint32_t session)
+{
+ return cmumble_find_by_id(ctx->users,
+ G_STRUCT_OFFSET(struct cmumble_user, id),
+ session);
+}
+
+static inline struct cmumble_channel *
+find_channel(struct cmumble_context *ctx, guint channel_id)
+{
+ return cmumble_find_by_id(ctx->channels,
+ G_STRUCT_OFFSET(struct cmumble_channel, id),
+ channel_id);
+}
+
+#endif /* _UTIL_H_ */