Mostly working now - as long as I use hacked xmms2
[adorno.git] / inc / xmms2Player.php
blob6149e5f85dcdf68c9186f416ebe2f166700dbb3b
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;
38 $output = array();
39 $retval = 0;
40 exec( $command, &$output, &$retval );
42 return $output;
45 /**
46 * Query the xmms2 about it's current status
48 function UpdateStatus() {
49 $status = $this->Daemon( "info" );
50 foreach( $status AS $k => $v ) {
51 list( $key, $value ) = preg_split( '/ = /', $v, 2);
52 $this->status[$key] = $value;
54 $status = $this->Daemon( "state" );
55 $this->status['state'] = $status[0];
59 /**
60 * Play a track. Xmms2 is OK if we tell it to play when it is already.
62 function Play( $track ) {
63 global $c;
65 $this->UpdateStatus();
67 $this->Daemon("add $track");
68 if ( $this->status['state'] == 'Stopped' ) {
69 $this->Daemon("next");
70 $this->Daemon("play");
72 else
73 $this->Daemon("play");
77 /**
78 * Query the xmms2 about it's current playlist and position
80 function GetCurrent() {
81 global $c;
83 $this->UpdateStatus();
84 $songnow = (object) array();
85 $songnow->started = date( 'Y-m-d H:i:s', $this->status['[server] laststarted'] );
86 $songnow->track = str_replace( 'file://', '', $this->status['[server] url'] );
88 return $songnow;
91 /**
92 * Query the xmms2 about it's current playlist and position
94 function GetQueue() {
95 global $c;
97 $this->UpdateStatus();
98 $next = $this->status['song'] + 1;
99 $this->queue = array();
101 $filename = "";
102 $pos = -1;
104 $playlist = $this->Daemon( "list" );
105 foreach( $playlist AS $k => $v ) {
107 if ( $pos >= 0 ) {
108 $filename = preg_replace( '#^( |->)\[\d+/\d+\] #', '', $v );
109 if ( substr($filename,0,7) == 'file://' ) {
110 $filename = substr( $filename, 7 );
111 $filename = urldecode( $filename );
112 $this->queue[$pos++] = $filename;
115 else if ( $pos < 0 && substr( $v, 0, 2) == '->' ) {
116 $pos = 0;
120 return $this->queue;
125 $GLOBALS["xmms2"] = new xmms2Connection();
131 /******************************************************************
132 * The actual API the web interface calls is then very simple...
133 ******************************************************************/
136 * Queue a file for playing
138 function daemon_play_track( $path ) {
139 global $xmms2;
140 error_log("adorno: DBG: Trying to play '$path'");
141 $xmms2->Play( $path );
146 * Get a list of the files currently queued for the future
148 function daemon_get_queue() {
149 global $xmms2;
150 $q = $xmms2->GetQueue();
151 return $q;
156 * Get the currently playing track and it's starting time
158 function daemon_current_track() {
159 global $xmms2;
160 return $xmms2->GetCurrent();
165 * Get the currently playing track and it's starting time
167 function daemon_other_command( $action, $track ) {
168 global $xmms2;
169 switch( $action ) {
170 case 'resume':
171 $xmms2->Daemon('play');
172 break;
173 case 'pause':
174 $xmms2->Daemon($action);
175 break;
176 case 'next':
177 $xmms2->Daemon($action);
178 break;
179 default:
180 error_log("adorno: ERROR: Unsupported command '$action'" );
183 return true;