-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpdEventListener.class.php
190 lines (169 loc) · 5.06 KB
/
mpdEventListener.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
require_once 'mpd.class.php';
define ('MPDEVENTLISTENER_ONSTOP', 'onStop');
define ('MPDEVENTLISTENER_ONPAUSE', 'onPause');
define ('MPDEVENTLISTENER_ONSONGCHANGE', 'onSongChange');
define ('MPDEVENTLISTENER_ONPLAY', 'onPlay');
define ('MPDEVENTLISTENER_ONREPEATCHANGE', 'onRepeatChange');
define ('MPDEVENTLISTENER_ONSHUFFLECHANGE', 'onShuffleChange');
define ('MPDEVENTLISTENER_ONPLAYLISTCHANGE', 'onPlaylistChange');
define ('MPDEVENTLISTENER_ONCROSSFADECHANGE', 'onCrossfadeChange');
define ('MPDEVENTLISTENER_ONVOLUMECHANGE', 'onVolumeChange');
define ('MPDEVENTLISTENER_ONCONSUMECHANGE', 'onConsumeChange');
define ('MPDEVENTLISTENER_ONSINGLECHANGE', 'onSingleChange');
define ('MPDEVENTLISTENER_ONTIMECHANGE', 'onTimeChange');
define ('MPDEVENTLISTENER_ONOUTPUTCHANGE', 'onOutputChange');
class mpdEventListener {
private $mpd; // MPD connection
private $checkTime;
private $bindings = array();
private $status = array();
private $playlist = array();
/**
* Constructor
*
* @param mpd $mpdConnection
* @param int $checkTime in seconds
*/
public function mpdEventListener($mpdConnection, $checkTime = 1) {
if ($mpdConnection->connected) {
$this->mpd =& $mpdConnection;
$this->checkTime = $checkTime;
} else
return false;
}
/**
* Para comenzar a escuchar cambios
*/
public function startListening() {
set_time_limit(0);
do {
$this->status = $this->mpd->GetStatus();
$this->playlist = $this->mpd->GetPlaylist();
sleep($this->checkTime);
$this->mpd->RefreshInfo();
$this->_checkStatus($this->mpd->GetStatus(), $this->mpd->GetPlaylist());
} while (true);
}
/**
* Devuelve los segundos en que se escucharán los cambios
*
* @return int
*/
public function getCheckTime() {
return $this->$checkTime;
}
/**
* Establece los segundos en que se escucharán los cambios
*
* @param int $checkTime
*/
public function setCheckTime(string $checkTime) {
$this->checkTime = $checkTime;
}
/**
* Devuelve los bindings
*
* @return array
*/
public function getBindings() {
return $this->bindings;
}
/**
* Establece los bindings
*
* @param array $bindings
*/
public function setBindings(string $bindings) {
$this->bindings = $bindings;
}
/**
* Bind a function to an event
*
* @param string $event
* @param string $function
*/
public function bind($event, $function) {
$this->bindings[$event][] = $function;
return true;
}
/**
* Unbind a function from an event. Return if it existed and done.
*
* @param string $event
* @param string $function
*/
public function unbind($event, $function) {
if ($this->bindings[$event]) {
$func_key = array_search($function,$this->bindings[$event]);
if ($func_key) {
unset($this->bindings[$event][$func_key]);
return true;
} else {
return false;
}
} else
return false;
}
private function _checkStatus($newStatus, $newPlaylist) {
$change_status = array_diff_assoc($newStatus,$this->status);
foreach ($change_status as $attr => $value) {
switch ($attr) {
case 'volume':
$this->_triggerEvent(MPDEVENTLISTENER_ONVOLUMECHANGE,$this->status['volume'],$value);
break;
case 'repeat':
$this->_triggerEvent(MPDEVENTLISTENER_ONREPEATCHANGE,$this->status['repeat'],$value);
break;
case 'random':
$this->_triggerEvent(MPDEVENTLISTENER_ONSHUFFLECHANGE,$this->status['random'],$value);
break;
case 'single':
$this->_triggerEvent(MPDEVENTLISTENER_ONSINGLECHANGE,$this->status['single'],$value);
break;
case 'consume':
$this->_triggerEvent(MPDEVENTLISTENER_ONCONSUMECHANGE,$this->status['consume'],$value);
break;
case 'playlist':
$this->_triggerEvent(MPDEVENTLISTENER_ONPLAYLISTCHANGE,$this->playlist,$newPlaylist);
break;
case 'xfade':
$this->_triggerEvent(MPDEVENTLISTENER_ONCROSSFADECHANGE,$this->status['xfade'],$value);
break;
case 'state':
switch ($value) {
case MPD_STATE_PAUSED:
$this->_triggerEvent(MPDEVENTLISTENER_ONPAUSE,$this->status['state']);
break;
case MPD_STATE_PLAYING:
$this->_triggerEvent(MPDEVENTLISTENER_ONPLAY,$this->status['state']);
break;
case MPD_STATE_STOPPED:
$this->_triggerEvent(MPDEVENTLISTENER_ONSTOP,$this->status['state']);
break;
}
break;
case 'songid':
$songFile = $newPlaylist['files'][$newStatus['song']]['file'];
$this->_triggerEvent(MPDEVENTLISTENER_ONSONGCHANGE,$songFile);
break;
case 'time':
$this->_triggerEvent(MPDEVENTLISTENER_ONTIMECHANGE,$this->status['time'],$value);
break;
}
}
}
private function _triggerEvent($event) {
if (isset($this->bindings[$event])) {
$args = func_get_args();
array_shift($args);
foreach ($this->bindings[$event] as $func) {
//echo "calling " . $func;
if (function_exists($func)) {
call_user_func_array($func,$args);
}
}
}
}
}
?>