5 error_log( "Using XMMS backend: ". $c->daemon_type
);
6 class xmms2Connection
{
7 var $active; // Once it actually is...
12 * Create a minimally initialised connection object
14 function xmms2Connection() {
15 $this->active
= false;
17 $this->status
= array();
21 * Connect to the remote xmms2
32 * Do a question/response pair with the daemon
34 function Daemon( $say ) {
35 if ( ! $this->active
) $this->Connect();
37 $command = 'xmms2 ' . $say . ' 2>&1';
40 error_log("adorno: DBG: XMMS2CMD: >>>$command<<<");
41 exec( $command, &$output, &$retval );
43 if ( preg_match( '#Could not connect to xmms2d#i', $output[0] ) ) {
44 exec( 'xmms2-launcher', &$output, &$retval );
45 error_log("adorno: DBG: Launching XMMS2 daemon: ($retval) - " . $output[0]);
46 exec( 'xmms2 playlist type Default queue', &$output, &$retval );
47 error_log("adorno: DBG: Launching XMMS2 daemon: ($retval) - " . $output[0]);
50 exec( $command, &$output, &$retval );
56 * Query the xmms2 about it's current status
58 function UpdateStatus() {
59 $status = $this->Daemon( "state" );
60 $this->status
['state'] = $status[0];
61 $status = $this->Daemon( "info" );
62 foreach( $status AS $k => $v ) {
63 list( $key, $value ) = preg_split( '/ = /', $v, 2);
64 $this->status
[$key] = $value;
70 * Encode a filename to escape spaces & stuff.
72 function EncodeFileName( $track ) {
73 // $encoded = preg_replace( '/([\\\'"#&;`|*?~<>^()[]{}$ ])/', '\\\\\1', $track);
74 $encoded = escapeshellarg($track);
75 # $encoded = "file://" . $encoded;
81 * Play a track. Xmms2 is OK if we tell it to play when it is already.
83 function Play( $track ) {
86 $this->UpdateStatus();
88 // Replace ' with '' so escapeshellcmd reliably ignores it, then with \' afterwards
89 $this->Daemon("add ". $this->EncodeFileName($track));
90 if ( $this->status
['state'] == 'Stopped' ) {
91 $this->Daemon("next");
92 $this->Daemon("play");
95 $this->Daemon("play");
100 * Query the xmms2 about it's current playlist and position
102 function GetCurrent() {
105 $this->UpdateStatus();
106 $songnow = (object) array();
107 $songnow->started
= date( 'Y-m-d H:i:s', $this->status
['[server] laststarted'] );
108 $songnow->track
= str_replace( 'file://', '', $this->status
['[server] url'] );
114 * Query the xmms2 about it's current playlist and position
116 function GetQueue() {
119 $this->UpdateStatus();
120 $next = $this->status
['song'] +
1;
121 $this->queue
= array();
126 $playlist = $this->Daemon( "list" );
127 foreach( $playlist AS $k => $v ) {
130 $filename = preg_replace( '#^( |->)\[\d+/\d+\] #', '', $v );
131 if ( substr($filename,0,7) == 'file://' ) {
132 $filename = substr( $filename, 7 );
133 $filename = urldecode( $filename );
134 $this->queue
[$pos++
] = $filename;
137 else if ( $pos < 0 && substr( $v, 0, 2) == '->' ) {
147 $GLOBALS["xmms2"] = new xmms2Connection();
153 /******************************************************************
154 * The actual API the web interface calls is then very simple...
155 ******************************************************************/
158 * Queue a file for playing
160 function daemon_play_track( $path ) {
162 error_log("adorno: DBG: Trying to play '$path'");
163 $xmms2->Play( $path );
168 * Get a list of the files currently queued for the future
170 function daemon_get_queue() {
172 $q = $xmms2->GetQueue();
178 * Get the currently playing track and it's starting time
180 function daemon_current_track() {
182 return $xmms2->GetCurrent();
187 * Get the currently playing track and it's starting time
189 function daemon_other_command( $action, $track ) {
193 $xmms2->Daemon('play');
196 $xmms2->Daemon($action);
199 $xmms2->Daemon($action);
202 error_log("adorno: ERROR: Unsupported command '$action'" );