Simpler approach.
[adorno.git] / inc / xmms2Player.php
blob28e45703795e10f0f487a67050305f8c98a227fe
1 <?php
3 putenv('COLUMNS=500');
5 error_log( "Using XMMS backend: ". $c->daemon_type );
6 class xmms2Connection {
7 var $active; // Once it actually is...
8 var $xmms2;
9 var $status;
11 /**
12 * Create a minimally initialised connection object
14 function xmms2Connection() {
15 $this->active = false;
16 $this->xmms2 = false;
17 $this->status = array();
20 /**
21 * Connect to the remote xmms2
22 * - a complete fake.
24 function Connect() {
25 global $c;
27 $this->xmms2 = true;
28 $this->active = true;
31 /**
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';
38 $output = array();
39 $retval = 0;
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]);
48 $output = array();
49 $retval = 0;
50 exec( $command, &$output, &$retval );
52 return $output;
55 /**
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;
69 /**
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;
76 return $encoded;
80 /**
81 * Play a track. Xmms2 is OK if we tell it to play when it is already.
83 function Play( $track ) {
84 global $c;
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");
94 else
95 $this->Daemon("play");
99 /**
100 * Query the xmms2 about it's current playlist and position
102 function GetCurrent() {
103 global $c;
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'] );
110 return $songnow;
114 * Query the xmms2 about it's current playlist and position
116 function GetQueue() {
117 global $c;
119 $this->UpdateStatus();
120 $next = $this->status['song'] + 1;
121 $this->queue = array();
123 $filename = "";
124 $pos = -1;
126 $playlist = $this->Daemon( "list" );
127 foreach( $playlist AS $k => $v ) {
129 if ( $pos >= 0 ) {
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) == '->' ) {
138 $pos = 0;
142 return $this->queue;
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 ) {
161 global $xmms2;
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() {
171 global $xmms2;
172 $q = $xmms2->GetQueue();
173 return $q;
178 * Get the currently playing track and it's starting time
180 function daemon_current_track() {
181 global $xmms2;
182 return $xmms2->GetCurrent();
187 * Get the currently playing track and it's starting time
189 function daemon_other_command( $action, $track ) {
190 global $xmms2;
191 switch( $action ) {
192 case 'resume':
193 $xmms2->Daemon('play');
194 break;
195 case 'pause':
196 $xmms2->Daemon($action);
197 break;
198 case 'next':
199 $xmms2->Daemon($action);
200 break;
201 default:
202 error_log("adorno: ERROR: Unsupported command '$action'" );
205 return true;