From d0e9c6e2f513b1bacff72180716f9c38cb9487c2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 27 Aug 2004 16:02:50 +0000 Subject: make it work git-svn-id: file:///home/lennart/svn/public/xmms-pulse/trunk@9 ef929aba-56e2-0310-84e0-b7573d389508 --- acinclude.m4 | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/plugin.c | 156 ++++++++++++++++++++++++++++------------------ 2 files changed, 296 insertions(+), 59 deletions(-) create mode 100644 acinclude.m4 diff --git a/acinclude.m4 b/acinclude.m4 new file mode 100644 index 0000000..bedf51c --- /dev/null +++ b/acinclude.m4 @@ -0,0 +1,199 @@ +dnl Available from the GNU Autoconf Macro Archive at: +dnl http://www.gnu.org/software/ac-archive/htmldoc/acx_pthread.html +dnl +AC_DEFUN([ACX_PTHREAD], [ +AC_REQUIRE([AC_CANONICAL_HOST]) +AC_LANG_SAVE +AC_LANG_C +acx_pthread_ok=no + +# We used to check for pthread.h first, but this fails if pthread.h +# requires special compiler flags (e.g. on True64 or Sequent). +# It gets checked for in the link test anyway. + +# First of all, check if the user has set any of the PTHREAD_LIBS, +# etcetera environment variables, and if threads linking works using +# them: +if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then + save_CFLAGS="$CFLAGS" + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + save_LIBS="$LIBS" + LIBS="$PTHREAD_LIBS $LIBS" + AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS]) + AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes) + AC_MSG_RESULT($acx_pthread_ok) + if test x"$acx_pthread_ok" = xno; then + PTHREAD_LIBS="" + PTHREAD_CFLAGS="" + fi + LIBS="$save_LIBS" + CFLAGS="$save_CFLAGS" +fi + +# We must check for the threads library under a number of different +# names; the ordering is very important because some systems +# (e.g. DEC) have both -lpthread and -lpthreads, where one of the +# libraries is broken (non-POSIX). + +# Create a list of thread flags to try. Items starting with a "-" are +# C compiler flags, and other items are library names, except for "none" +# which indicates that we try without any flags at all, and "pthread-config" +# which is a program returning the flags for the Pth emulation library. + +acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" + +# The ordering *is* (sometimes) important. Some notes on the +# individual items follow: + +# pthreads: AIX (must check this before -lpthread) +# none: in case threads are in libc; should be tried before -Kthread and +# other compiler flags to prevent continual compiler warnings +# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) +# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) +# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) +# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads) +# -pthreads: Solaris/gcc +# -mthreads: Mingw32/gcc, Lynx/gcc +# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it +# doesn't hurt to check since this sometimes defines pthreads too; +# also defines -D_REENTRANT) +# pthread: Linux, etcetera +# --thread-safe: KAI C++ +# pthread-config: use pthread-config program (for GNU Pth library) + +case "${host_cpu}-${host_os}" in + *solaris*) + + # On Solaris (at least, for some versions), libc contains stubbed + # (non-functional) versions of the pthreads routines, so link-based + # tests will erroneously succeed. (We need to link with -pthread or + # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather + # a function called by this macro, so we could check for that, but + # who knows whether they'll stub that too in a future libc.) So, + # we'll just look for -pthreads and -lpthread first: + + acx_pthread_flags="-pthread -pthreads pthread -mt $acx_pthread_flags" + ;; +esac + +if test x"$acx_pthread_ok" = xno; then +for flag in $acx_pthread_flags; do + + case $flag in + none) + AC_MSG_CHECKING([whether pthreads work without any flags]) + ;; + + -*) + AC_MSG_CHECKING([whether pthreads work with $flag]) + PTHREAD_CFLAGS="$flag" + ;; + + pthread-config) + AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no) + if test x"$acx_pthread_config" = xno; then continue; fi + PTHREAD_CFLAGS="`pthread-config --cflags`" + PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" + ;; + + *) + AC_MSG_CHECKING([for the pthreads library -l$flag]) + PTHREAD_LIBS="-l$flag" + ;; + esac + + save_LIBS="$LIBS" + save_CFLAGS="$CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + + # Check for various functions. We must include pthread.h, + # since some functions may be macros. (On the Sequent, we + # need a special flag -Kthread to make this header compile.) + # We check for pthread_join because it is in -lpthread on IRIX + # while pthread_create is in libc. We check for pthread_attr_init + # due to DEC craziness with -lpthreads. We check for + # pthread_cleanup_push because it is one of the few pthread + # functions on Solaris that doesn't have a non-functional libc stub. + # We try pthread_create on general principles. + AC_TRY_LINK([#include ], + [pthread_t th; pthread_join(th, 0); + pthread_attr_init(0); pthread_cleanup_push(0, 0); + pthread_create(0,0,0,0); pthread_cleanup_pop(0); ], + [acx_pthread_ok=yes]) + + LIBS="$save_LIBS" + CFLAGS="$save_CFLAGS" + + AC_MSG_RESULT($acx_pthread_ok) + if test "x$acx_pthread_ok" = xyes; then + break; + fi + + PTHREAD_LIBS="" + PTHREAD_CFLAGS="" +done +fi + +# Various other checks: +if test "x$acx_pthread_ok" = xyes; then + save_LIBS="$LIBS" + LIBS="$PTHREAD_LIBS $LIBS" + save_CFLAGS="$CFLAGS" + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + + # Detect AIX lossage: threads are created detached by default + # and the JOINABLE attribute has a nonstandard name (UNDETACHED). + AC_MSG_CHECKING([for joinable pthread attribute]) + AC_TRY_LINK([#include ], + [int attr=PTHREAD_CREATE_JOINABLE;], + ok=PTHREAD_CREATE_JOINABLE, ok=unknown) + if test x"$ok" = xunknown; then + AC_TRY_LINK([#include ], + [int attr=PTHREAD_CREATE_UNDETACHED;], + ok=PTHREAD_CREATE_UNDETACHED, ok=unknown) + fi + if test x"$ok" != xPTHREAD_CREATE_JOINABLE; then + AC_DEFINE(PTHREAD_CREATE_JOINABLE, $ok, + [Define to the necessary symbol if this constant + uses a non-standard name on your system.]) + fi + AC_MSG_RESULT(${ok}) + if test x"$ok" = xunknown; then + AC_MSG_WARN([we do not know how to create joinable pthreads]) + fi + + AC_MSG_CHECKING([if more special flags are required for pthreads]) + flag=no + case "${host_cpu}-${host_os}" in + *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";; + *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";; + esac + AC_MSG_RESULT(${flag}) + if test "x$flag" != xno; then + PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS" + fi + + LIBS="$save_LIBS" + CFLAGS="$save_CFLAGS" + + # More AIX lossage: must compile with cc_r + AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC}) +else + PTHREAD_CC="$CC" +fi + +AC_SUBST(PTHREAD_LIBS) +AC_SUBST(PTHREAD_CFLAGS) +AC_SUBST(PTHREAD_CC) + +# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: +if test x"$acx_pthread_ok" = xyes; then + ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1]) + : +else + acx_pthread_ok=no + $2 +fi +AC_LANG_RESTORE +])dnl ACX_PTHREAD 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 #include #include #include #include #include +#include +#include #include +#include +#include #include #include @@ -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", ¤t_request->ss); + + stream = pa_stream_new(c, get_song_name(), ¤t_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, -- cgit