summaryrefslogtreecommitdiffstats
path: root/src/plugin.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugin.c')
-rw-r--r--src/plugin.c156
1 files changed, 97 insertions, 59 deletions
diff --git a/src/plugin.c b/src/plugin.c
index 6d39c13..39420e4 100644
--- a/src/plugin.c
+++ b/src/plugin.c
@@ -1,13 +1,36 @@
/* $Id$ */
+/***
+ This file is part of xmms-polyp.
+
+ xmms-polyp is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ xmms-polyp is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with xmms-polyp; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <limits.h>
+#include <gtk/gtk.h>
#include <plugin.h>
+#include <xmmsctrl.h>
+#include <util.h>
#include <polyp/mainloop.h>
#include <polyp/polyplib.h>
@@ -53,12 +76,41 @@ static int do_trigger = 0, triggered = 0;
static struct pa_sample_spec sample_spec;
static int locked_by_thread = 0;
+/* This function is from xmms' core */
+gint ctrlsocket_get_session_id(void);
+
static void *memdup(void *p, size_t l) {
void *r = malloc(l);
memcpy(r, p, l);
return r;
}
+#ifndef HOST_NAME_MAX
+#define HOST_NAME_MAX 255
+#endif
+
+static const char* get_song_name(void) {
+ static char t[256];
+ gint session, pos;
+ char *str;
+
+ session = ctrlsocket_get_session_id();
+ pos = xmms_remote_get_playlist_pos(session);
+ if (!(str = xmms_remote_get_playlist_title(session, pos)))
+ return "xmms";
+
+ snprintf(t, sizeof(t), "XMMS [%s]", str);
+
+ return t;
+}
+
+static const char* get_host(void) {
+ static char t[HOST_NAME_MAX+1];
+ gethostname(t, sizeof(t));
+ t[HOST_NAME_MAX] = 0;
+ return t;
+}
+
static void lock_request(void) {
assert(thread_running && pthread_equal(pthread_self(), thread_id));
@@ -80,8 +132,6 @@ static void finish_request(int success) {
lock_request();
if (current_request) {
- if (!success)
- fprintf(stderr, "failure: %i\n", success);
current_request->done = 1;
current_request->success = success;
pthread_cond_broadcast(&request_cond);
@@ -127,8 +177,6 @@ static void stream_state_callback(struct pa_stream *s, void *userdata) {
static void context_state_callback(struct pa_context *c, void *userdata) {
assert(c && c == context);
- fprintf(stderr, "state\n");
-
switch (pa_context_get_state(c)) {
case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_AUTHORIZING:
@@ -137,16 +185,17 @@ static void context_state_callback(struct pa_context *c, void *userdata) {
case PA_CONTEXT_READY :
assert(!stream && current_request);
+
pa_context_set_subscribe_callback(context, subscribe_callback, NULL);
pa_operation_unref(pa_context_subscribe(context, PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL));
-
- stream = pa_stream_new(c, "xmms", &current_request->ss);
+
+ stream = pa_stream_new(c, get_song_name(), &current_request->ss);
assert(stream);
pa_stream_set_state_callback(stream, stream_state_callback, NULL);
pa_stream_connect_playback(stream, NULL, NULL);
break;
-
+
default:
finish_request(0);
}
@@ -197,21 +246,21 @@ static void request_func(struct pa_mainloop_api*api, struct pa_io_event *io, int
/*fprintf(stderr, "req: %i\n", current_request->message);*/
switch (current_request->message) {
- case MESSAGE_OPEN:
+ case MESSAGE_OPEN: {
+ char t[64];
assert(!context && !stream);
- fprintf(stderr, "opening\n");
- context = pa_context_new(api, "xmms");
+ snprintf(t, sizeof(t), "XMMS (PID %lu on %s)", (unsigned long) getpid(), get_host());
+ context = pa_context_new(api, t);
assert(context);
pa_context_set_state_callback(context, context_state_callback, NULL);
pa_context_connect(context, NULL);
break;
+ }
case MESSAGE_CLOSE: {
struct pa_operation *o;
- fprintf(stderr, "closing\n");
-
assert(context);
if ((o = pa_context_drain(context, context_drain_callback, NULL)))
pa_operation_unref(o);
@@ -237,7 +286,7 @@ static void request_func(struct pa_mainloop_api*api, struct pa_io_event *io, int
case MESSAGE_PAUSE:
case MESSAGE_UNPAUSE:
assert(context && stream);
- pa_operation_unref(pa_stream_cork(stream, current_request->message == MESSAGE_UNPAUSE, stream_success_callback, NULL));
+ pa_operation_unref(pa_stream_cork(stream, current_request->message == MESSAGE_PAUSE, stream_success_callback, NULL));
break;
case MESSAGE_LATENCY:
@@ -288,8 +337,6 @@ static void* thread_func(void *t) {
struct pa_io_event *io;
assert(pipe_fds[0] >= 0 && !mainloop_api);
-
- fprintf(stderr, "THREAD START\n");
failed = locked_by_thread = 0;
thread_running = 1;
@@ -321,8 +368,6 @@ static void* thread_func(void *t) {
thread_running = 0;
pthread_cond_broadcast(&request_cond);
-
- fprintf(stderr, "THREAD STOP\n");
return NULL;
}
@@ -343,15 +388,11 @@ static void start_thread(void) {
}
static void stop_thread(void) {
- fprintf(stderr, "joining\n");
pthread_join(thread_id, NULL);
-
thread_running = 0;
-
close(pipe_fds[0]);
close(pipe_fds[1]);
pipe_fds[0] = pipe_fds[1] = -1;
- fprintf(stderr, "join done\n");
}
static void execute_request(struct request *r) {
@@ -374,10 +415,8 @@ static void execute_request(struct request *r) {
assert(pipe_fds[1] >= 0);
write(pipe_fds[1], &x, sizeof(x));
- while (!r->done && thread_running) {
+ while (!r->done && thread_running)
pthread_cond_wait(&request_cond, &request_mutex);
- /*fprintf(stderr, "COND done\n"); */
- }
if (!thread_running) {
r->success = 0;
@@ -386,7 +425,7 @@ static void execute_request(struct request *r) {
current_request = NULL;
- /* Notify other waiting threads, that the request was completed */
+ /* Notify other waiting threads that the request was completed */
pthread_cond_broadcast(&request_cond);
}
@@ -397,8 +436,6 @@ static void polyp_get_volume(int *l, int *r) {
struct request req;
int v;
- /*fprintf(stderr, "get_volume\n");*/
-
req.message = MESSAGE_GETVOLUME;
execute_request(&req);
@@ -408,27 +445,19 @@ static void polyp_get_volume(int *l, int *r) {
}
v = (req.volume*100)/PA_VOLUME_NORM;
-
*r = *l = v > 100 ? 100 : v;
}
void polyp_set_volume(int l, int r) {
struct request req;
- /*fprintf(stderr, "set_volume\n");*/
-
req.message = MESSAGE_SETVOLUME;
- req.volume = ((l+r)*PA_VOLUME_NORM)/200;
+ req.volume = ((l>r?l:r)*PA_VOLUME_NORM)/100;
execute_request(&req);
}
static void polyp_pause(short b) {
struct request r;
-
- fprintf(stderr, "*** PAUSE ***\n");
-
- fprintf(stderr, "pause: %s\n", b ? "yes" : "no");
-
r.message = b ? MESSAGE_PAUSE : MESSAGE_UNPAUSE;
execute_request(&r);
}
@@ -444,10 +473,8 @@ static int polyp_free(void) {
return 0;
ret = r.value;
- fprintf(stderr, "free: %u", ret);
if (do_trigger && !triggered) {
- fprintf(stderr, "trigger\n");
r.message = MESSAGE_TRIGGER;
execute_request(&r);
triggered = 1;
@@ -466,8 +493,6 @@ static int polyp_playing(void) {
if (!r.success)
return 0;
- fprintf(stderr, "playing : %s\n", r.value ? "yes" : "no");
-
return r.value != 0;
}
@@ -494,15 +519,10 @@ static int polyp_get_output_time(void) {
static void polyp_flush(int time) {
struct request r;
- fprintf(stderr, "*** FLUSH ***\n");
-
- /*fprintf(stderr, "flush: %i\n", time);*/
-
r.message = MESSAGE_FLUSH;
execute_request(&r);
written = (size_t) (((double)time*sample_spec.rate/1000)*pa_frame_size(&sample_spec));
- fprintf(stderr, "flush: %u -- %u\n", written, time);
}
static void polyp_write(void* ptr, int length) {
@@ -520,21 +540,16 @@ static void polyp_write(void* ptr, int length) {
static void polyp_close(void) {
struct request r;
- fprintf(stderr, "close\n");
-
assert(thread_running);
- fprintf(stderr, "msg close\n");
r.message = MESSAGE_CLOSE;
execute_request(&r);
- fprintf(stderr, "msg done\n");
stop_thread();
}
static int polyp_open(AFormat fmt, int rate, int nch) {
struct request r;
- fprintf(stderr, "open\n");
if (fmt == FMT_U8)
r.ss.format = PA_SAMPLE_U8;
@@ -557,35 +572,58 @@ static int polyp_open(AFormat fmt, int rate, int nch) {
start_thread();
- fprintf(stderr, "go for req\n");
r.message = MESSAGE_OPEN;
execute_request(&r);
- fprintf(stderr, "done req\n");
if (!r.success) {
polyp_close();
- fprintf(stderr, "open() failed.\n");
return 0;
}
written = do_trigger = triggered = failed = 0;
- fprintf(stderr, "READY\n");
-
return 1;
}
static void polyp_init(void) {
- fprintf(stderr, "init\n");
}
+static void polyp_about(void) {
+ static GtkWidget *dialog;
+
+ if (dialog != NULL)
+ return;
+
+ dialog = xmms_show_message(
+ "About XMMS Polypaudio Output Plugin",
+ "XMMS Polypaudio Output Plugin\n\n "
+ "This program is free software; you can redistribute it and/or modify\n"
+ "it under the terms of the GNU General Public License as published by\n"
+ "the Free Software Foundation; either version 2 of the License, or\n"
+ "(at your option) any later version.\n"
+ "\n"
+ "This program is distributed in the hope that it will be useful,\n"
+ "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
+ "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
+ "GNU General Public License for more details.\n"
+ "\n"
+ "You should have received a copy of the GNU General Public License\n"
+ "along with this program; if not, write to the Free Software\n"
+ "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,\n"
+ "USA.", "OK", FALSE, NULL, NULL);
+ gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
+ GTK_SIGNAL_FUNC(gtk_widget_destroyed),
+ &dialog);
+}
+
+
static OutputPlugin polyp_plugin = {
NULL,
NULL,
- "Polypaudio Output Plugin", /* Description */
+ "Polypaudio Output Plugin",
polyp_init,
- NULL, /* polyp_about, */
- NULL, /* polyp_configure, */
+ polyp_about,
+ NULL, /* polyp_configure, */
polyp_get_volume,
polyp_set_volume,
polyp_open,