Support XMMS2 for the backend player.
[adorno.git] / inc / xmms2Player.php
blob943aaf92813db13ace62c1ee04041e27a3766ad4
1 <?php
3 error_log( "Using XMMS backend: ". $c->daemon_type );
4 class xmms2Connection {
5 var $active; // Once it actually is...
6 var $xmms2;
7 var $status;
9 /**
10 * Create a minimally initialised connection object
12 function xmms2Connection() {
13 $this->active = false;
14 $this->xmms2 = false;
15 $this->status = array();
18 /**
19 * Connect to the remote xmms2
20 * - a complete fake.
22 function Connect() {
23 global $c;
25 $this->xmms2 = true;
26 $this->active = true;
29 /**
30 * Do a question/response pair with the daemon
32 function Daemon( $say ) {
33 if ( ! $this->active ) $this->Connect();
35 $command = 'xmms2 ' . $say;
36 $output = array();
37 $retval = 0;
38 exec( $command, &$output, &$retval );
40 return $output;
43 /**
44 * Query the xmms2 about it's current status
46 function UpdateStatus() {
47 $status = $this->Daemon( "info" );
48 foreach( $status AS $k => $v ) {
49 list( $key, $value ) = preg_split( '/ = /', $v, 2);
50 $this->status[$key] = $value;
55 /**
56 * Play a track. Xmms2 is OK if we tell it to play when it is already.
58 function Play( $track ) {
59 global $c;
61 $this->UpdateStatus();
63 $this->Daemon("add $track");
64 $this->Daemon("play");
68 /**
69 * Query the xmms2 about it's current playlist and position
71 function GetCurrent() {
72 global $c;
74 $this->UpdateStatus();
75 $songnow = (object) array();
76 $songnow->started = date( 'Y-m-d H:i:s', $this->status['[server] laststarted'] );
77 $songnow->track = str_replace( 'file://', '', $this->status['[server] url'] );
81 /**
82 * Query the xmms2 about it's current playlist and position
84 function GetQueue() {
85 global $c;
87 $this->UpdateStatus();
88 $next = $this->status['song'] + 1;
89 $this->queue = array();
91 $filename = "";
92 $pos = -1;
94 $playlist = $this->Daemon( "list" );
95 foreach( $playlist AS $k => $v ) {
96 if ( substr( $v, 0, 2) == '->' ) $pos = 0;
98 if ( $pos >= 0 ) {
99 $filename = preg_replace( '#^( |->)\[\d+/\d+\] #', '', $v );
100 $filename = preg_replace( '#^file://#', '', $filename );
101 $filename = urldecode( $filename );
102 $this->queue[$pos++] = $filename;
106 return $this->queue;
111 $GLOBALS["xmms2"] = new xmms2Connection();
117 /******************************************************************
118 * The actual API the web interface calls is then very simple...
119 ******************************************************************/
122 * Queue a file for playing
124 function daemon_play_track( $path ) {
125 global $xmms2;
126 error_log("adorno: DBG: Trying to play '$path'");
127 $xmms2->Play( $path );
132 * Get a list of the files currently queued for the future
134 function daemon_get_queue() {
135 global $xmms2;
136 $q = $xmms2->GetQueue();
137 return $q;
142 * Get the currently playing track and it's starting time
144 function daemon_current_track() {
145 global $xmms2;
146 return $xmms2->GetCurrent();
151 * Get the currently playing track and it's starting time
153 function daemon_other_command( $action, $track ) {
154 global $xmms2;
155 switch( $action ) {
156 case 'resume':
157 $xmms2->Daemon('play');
158 break;
159 case 'pause':
160 $xmms2->Daemon($action);
161 break;
162 case 'next':
163 $xmms2->Daemon($action);
164 break;
165 default:
166 error_log("adorno: ERROR: Unsupported command '$action'" );
169 return true;