4 putenv('XMMS_PATH=tcp://127.0.0.1:9667/');
6 error_log( "Using XMMS backend: ". $c->daemon_type
);
7 class xmms2Connection
{
8 var $active; // Once it actually is...
13 * Create a minimally initialised connection object
15 function xmms2Connection() {
16 $this->active
= false;
18 $this->status
= false;
22 * Connect to the remote xmms2
33 * Do a question/response pair with the daemon
35 function OldDaemon( $say ) {
36 if ( ! $this->active
) $this->Connect();
38 $command = 'LC_ALL=en_NZ.UTF8 xmms2 ' . $say . ' 2>&1';
41 error_log("adorno: DBG: XMMS2CMD: >>>$command<<<");
42 exec( $command, &$output, &$retval );
44 if ( preg_match( '#Could not connect to xmms2d#i', $output[0] ) ) {
45 exec( 'xmms2-launcher', &$output, &$retval );
46 error_log("adorno: DBG: Launching XMMS2 daemon: ($retval) - " . $output[0]);
47 exec( 'xmms2 playlist type Default queue', &$output, &$retval );
48 error_log("adorno: DBG: Launching XMMS2 daemon: ($retval) - " . $output[0]);
51 exec( $command, &$output, &$retval );
57 * Do a question/response pair with the daemon via nyxmms2
59 function Daemon( $say ) {
60 if ( ! $this->active
) $this->Connect();
62 $command = 'LC_ALL=en_NZ.UTF8 nyxmms2 ' . $say . ' 2>&1';
65 error_log("adorno: DBG: NYCLICMD: >>>$command<<<");
66 exec( $command, &$output, &$retval );
68 if ( preg_match( '#Could not connect to #i', $output[0] ) ) {
69 exec( 'xmms2-launcher', &$output, &$retval );
70 error_log("adorno: DBG: Launching XMMS2 daemon: ($retval) - " . $output[0]);
71 exec( 'xmms2 playlist type Default queue', &$output, &$retval );
72 error_log("adorno: DBG: Launching XMMS2 daemon: ($retval) - " . $output[0]);
75 exec( $command, &$output, &$retval );
81 * Query the xmms2 about it's current status
83 function UpdateStatus( $force = false ) {
84 if ( ! $force && is_array($this->status
) ) return;
85 $this->status
= array();
86 $status = $this->Daemon( "status" );
87 $this->status
['state'] = preg_replace( '/:.*$/', '', $status[0]);
88 $status = $this->Daemon( "info" );
89 foreach( $status AS $k => $v ) {
90 list( $key, $value ) = preg_split( '/ = /', $v, 2);
91 if ( preg_match( '/\b(url|laststarted|duration|mime|album|artist|title)\s*$/', $key, $matches ) ) {
94 if ( preg_match( '/\%[0-9a-f][0-9a-f]/i', $value ) ) {
95 $value = rawurldecode($value);
97 // printf( "<p>status[%s] = [[%s]]</p>\n", $key, $value );
98 $this->status
[$key] = $value;
104 * Encode a filename to escape spaces & stuff.
106 function EncodeFileName( $track ) {
107 if ( preg_match( '#[a-z]{3,7}://#', $track ) ) {
110 $encoded = str_replace( chr(92), '\\\\', $track);
111 $encoded = str_replace( '"', '\\"', $track);
112 // $encoded = str_replace( "'", "\\'", $track);
113 // $encoded = preg_replace( '/([\\\'"#&;`|*?~<>^\(\)[]{}$ ])/', '\\\\\1', $encoded);
114 # $encoded = escapeshellarg($track);
115 $encoded = '"file://' . $encoded . '"';
116 error_log( "Trying to play: $encoded" );
122 * Play a track. Xmms2 is OK if we tell it to play when it is already.
124 function Play( $track ) {
127 $this->UpdateStatus( ($this->status
['state'] == 'Stopped') );
129 // Replace ' with '' so escapeshellcmd reliably ignores it, then with \' afterwards
130 $this->Daemon("add ". $this->EncodeFileName($track));
131 if ( $this->status
['state'] == 'Stopped' ) {
132 $this->Daemon("next");
133 $this->Daemon("play");
136 $this->Daemon("play");
141 * Move a track in the playlist
143 function MoveTrack( $index, $offset ) {
144 if ( $index < 1 ||
($index +
$offset) < 1 ) return; // Can't move playing track
146 $this->Daemon( sprintf("move %d %d", $index, $index +
$offset ) );
151 * Query the xmms2 about it's current playlist and position
153 function GetCurrent() {
156 $this->UpdateStatus();
157 $songnow = (object) array();
158 $songnow->started
= date( 'Y-m-d H:i:s', $this->status
['laststarted'] );
159 $songnow->track
= str_replace( 'file://', '', $this->status
['url'] );
160 $songnow->duration
= intval($this->status
['duration']) / 1000;
161 $songnow->finishing
= date( 'Y-m-d H:i:s', $this->status
['laststarted'] +
$songnow->duration
);
163 // printf( "<p>Song now is: %s started %s, finishes %s</p>\n", $songnow->track, $songnow->started, $songnow->finishing );
169 * Query the xmms2 about it's current playlist and position
171 function GetQueue() {
174 $this->UpdateStatus();
176 $this->queue
= array();
181 $playlist = $this->Daemon( "list" );
182 foreach( $playlist AS $k => $v ) {
185 if ( preg_match( '{@@(.+)@@}', $v, $matches ) ) $filename = $matches[1];
186 // echo "<p>$filename</p>\n";
188 if ( substr($filename,0,7) == 'file://' ) {
189 $filename = substr( $filename, 7 );
190 $filename = urldecode( $filename );
191 $this->queue
[$pos++
] = $filename;
194 else if ( $pos < 0 && substr( $v, 0, 2) == '->' ) {
196 // echo "<p>Pos($pos) $k == $v</p>\n";
199 // echo "<p>Pos($pos) $k -> $v</p>\n";
208 $GLOBALS["xmms2"] = new xmms2Connection();
214 /******************************************************************
215 * The actual API the web interface calls is then very simple...
216 ******************************************************************/
219 * Queue a file for playing
221 function daemon_play_track( $path ) {
223 error_log("adorno: DBG: Trying to play '$path'");
224 $xmms2->Play( $path );
229 * Get a list of the files currently queued for the future
231 function daemon_get_queue() {
233 $q = $xmms2->GetQueue();
239 * Get the currently playing track and it's starting time
241 function daemon_current_track() {
243 return $xmms2->GetCurrent();
248 * Get the currently playing track and it's starting time
250 function daemon_other_command( $action, $track ) {
254 $xmms2->Daemon('play');
257 $xmms2->Daemon($action);
260 $xmms2->Daemon($action);
263 error_log("adorno: ERROR: Unsupported command '$action'" );
270 * Get the currently playing track and it's starting time
272 function daemon_move( $index, $offset ) {
274 return $xmms2->MoveTrack($index,$offset);