From cd691db02e28fd09861baf55c3fe4bc8804827ff Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Sat, 29 May 2010 10:44:16 +1200 Subject: [PATCH] Now working with nycli --- inc/xmms2Player.php | 66 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/inc/xmms2Player.php b/inc/xmms2Player.php index f79aa2a..1def58f 100644 --- a/inc/xmms2Player.php +++ b/inc/xmms2Player.php @@ -1,6 +1,7 @@ daemon_type ); class xmms2Connection { @@ -14,7 +15,7 @@ class xmms2Connection { function xmms2Connection() { $this->active = false; $this->xmms2 = false; - $this->status = array(); + $this->status = false; } /** @@ -31,10 +32,10 @@ class xmms2Connection { /** * Do a question/response pair with the daemon */ - function Daemon( $say ) { + function OldDaemon( $say ) { if ( ! $this->active ) $this->Connect(); - $command = 'xmms2 ' . $say . ' 2>&1'; + $command = 'LC_ALL=en_NZ.UTF8 xmms2 ' . $say . ' 2>&1'; $output = array(); $retval = 0; error_log("adorno: DBG: XMMS2CMD: >>>$command<<<"); @@ -53,14 +54,47 @@ class xmms2Connection { } /** + * Do a question/response pair with the daemon via nyxmms2 + */ + function Daemon( $say ) { + if ( ! $this->active ) $this->Connect(); + + $command = 'LC_ALL=en_NZ.UTF8 nyxmms2 ' . $say . ' 2>&1'; + $output = array(); + $retval = 0; + error_log("adorno: DBG: NYCLICMD: >>>$command<<<"); + exec( $command, &$output, &$retval ); + + if ( preg_match( '#Could not connect to #i', $output[0] ) ) { + exec( 'xmms2-launcher', &$output, &$retval ); + error_log("adorno: DBG: Launching XMMS2 daemon: ($retval) - " . $output[0]); + exec( 'xmms2 playlist type Default queue', &$output, &$retval ); + error_log("adorno: DBG: Launching XMMS2 daemon: ($retval) - " . $output[0]); + $output = array(); + $retval = 0; + exec( $command, &$output, &$retval ); + } + return $output; + } + + /** * Query the xmms2 about it's current status */ - function UpdateStatus() { - $status = $this->Daemon( "state" ); - $this->status['state'] = $status[0]; + function UpdateStatus( $force = false ) { + if ( ! $force && is_array($this->status) ) return; + $this->status = array(); + $status = $this->Daemon( "status" ); + $this->status['state'] = preg_replace( '/:.*$/', '', $status[0]); $status = $this->Daemon( "info" ); foreach( $status AS $k => $v ) { list( $key, $value ) = preg_split( '/ = /', $v, 2); + if ( preg_match( '/\b(url|laststarted|duration|mime|album|artist|title)\s*$/', $key, $matches ) ) { + $key = $matches[1]; + } + if ( preg_match( '/\%[0-9a-f][0-9a-f]/i', $value ) ) { + $value = rawurldecode($value); + } + // printf( "

status[%s] = [[%s]]

\n", $key, $value ); $this->status[$key] = $value; } } @@ -90,7 +124,7 @@ class xmms2Connection { function Play( $track ) { global $c; - $this->UpdateStatus(); + $this->UpdateStatus( ($this->status['state'] == 'Stopped') ); // Replace ' with '' so escapeshellcmd reliably ignores it, then with \' afterwards $this->Daemon("add ". $this->EncodeFileName($track)); @@ -121,8 +155,12 @@ class xmms2Connection { $this->UpdateStatus(); $songnow = (object) array(); - $songnow->started = date( 'Y-m-d H:i:s', $this->status['[server] laststarted'] ); - $songnow->track = str_replace( 'file://', '', $this->status['[server] url'] ); + $songnow->started = date( 'Y-m-d H:i:s', $this->status['laststarted'] ); + $songnow->track = str_replace( 'file://', '', $this->status['url'] ); + $songnow->duration = intval($this->status['duration']) / 1000; + $songnow->finishing = date( 'Y-m-d H:i:s', $this->status['laststarted'] + $songnow->duration); + + // printf( "

Song now is: %s started %s, finishes %s

\n", $songnow->track, $songnow->started, $songnow->finishing ); return $songnow; } @@ -134,7 +172,7 @@ class xmms2Connection { global $c; $this->UpdateStatus(); - $next = $this->status['song'] + 1; + $this->queue = array(); $filename = ""; @@ -143,8 +181,10 @@ class xmms2Connection { $playlist = $this->Daemon( "list" ); foreach( $playlist AS $k => $v ) { + $filename = $v; + if ( preg_match( '{@@(.+)@@}', $v, $matches ) ) $filename = $matches[1]; + // echo "

$filename

\n"; if ( $pos >= 0 ) { - $filename = preg_replace( '#^( |->)\[\d+/\d+\] #', '', $v ); if ( substr($filename,0,7) == 'file://' ) { $filename = substr( $filename, 7 ); $filename = urldecode( $filename ); @@ -153,6 +193,10 @@ class xmms2Connection { } else if ( $pos < 0 && substr( $v, 0, 2) == '->' ) { $pos = 0; + // echo "

Pos($pos) $k == $v

\n"; + } + else { + // echo "

Pos($pos) $k -> $v

\n"; } } -- 2.11.4.GIT