mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
Refactor mpdclient to fetch status
This commit is contained in:
parent
c4cfd57d3d
commit
637711edc1
@ -13,40 +13,47 @@ class MPDClient {
|
|||||||
let HOST = "localhost"
|
let HOST = "localhost"
|
||||||
let PORT: UInt32 = 6600
|
let PORT: UInt32 = 6600
|
||||||
|
|
||||||
private let connection: OpaquePointer
|
private var connection: OpaquePointer?
|
||||||
private var status: OpaquePointer?
|
private var status: OpaquePointer?
|
||||||
|
|
||||||
private let commandQueue = DispatchQueue(label: "commandQueue")
|
private let commandQueue = DispatchQueue(label: "commandQueue")
|
||||||
private var commandQueued = false
|
private var commandQueued = false
|
||||||
|
|
||||||
enum TransportCommand {
|
enum Command {
|
||||||
case prevTrack, nextTrack, playPause, stop
|
case prevTrack, nextTrack, playPause, stop, fetchStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
init?() {
|
enum State: UInt32 {
|
||||||
|
case unknown = 0
|
||||||
|
case stopped = 1
|
||||||
|
case playing = 2
|
||||||
|
case paused = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
func connect() {
|
||||||
guard let connection = mpd_connection_new(HOST, PORT, 0)
|
guard let connection = mpd_connection_new(HOST, PORT, 0)
|
||||||
else { return nil }
|
else { return }
|
||||||
|
|
||||||
guard let status = mpd_run_status(connection)
|
guard let status = mpd_run_status(connection)
|
||||||
else { return nil }
|
else { return }
|
||||||
|
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
self.status = status
|
self.status = status
|
||||||
|
idle()
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
func disconnect() {
|
||||||
mpd_status_free(status)
|
mpd_status_free(status)
|
||||||
mpd_connection_free(connection)
|
mpd_connection_free(connection)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchStatus() {
|
func fetchStatus() {
|
||||||
status = mpd_run_status(connection)
|
sendCommand(command: .fetchStatus)
|
||||||
idle()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getState() {
|
func getState() -> State {
|
||||||
print(mpd_status_get_state(status))
|
let state = mpd_status_get_state(status)
|
||||||
idle()
|
return State(rawValue: state.rawValue)!
|
||||||
}
|
}
|
||||||
|
|
||||||
func playPause() {
|
func playPause() {
|
||||||
@ -65,7 +72,7 @@ class MPDClient {
|
|||||||
queueCommand(command: .nextTrack)
|
queueCommand(command: .nextTrack)
|
||||||
}
|
}
|
||||||
|
|
||||||
func queueCommand(command: TransportCommand) {
|
func queueCommand(command: Command) {
|
||||||
commandQueued = true
|
commandQueued = true
|
||||||
noIdle()
|
noIdle()
|
||||||
commandQueue.async { [unowned self] in
|
commandQueue.async { [unowned self] in
|
||||||
@ -75,8 +82,10 @@ class MPDClient {
|
|||||||
idle()
|
idle()
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendCommand(command: TransportCommand) {
|
func sendCommand(command: Command) {
|
||||||
switch command {
|
switch command {
|
||||||
|
|
||||||
|
// Transport commands
|
||||||
case .prevTrack:
|
case .prevTrack:
|
||||||
mpd_run_previous(connection)
|
mpd_run_previous(connection)
|
||||||
case .nextTrack:
|
case .nextTrack:
|
||||||
@ -84,6 +93,20 @@ class MPDClient {
|
|||||||
case .stop:
|
case .stop:
|
||||||
mpd_run_stop(connection)
|
mpd_run_stop(connection)
|
||||||
case .playPause:
|
case .playPause:
|
||||||
|
sendPlay()
|
||||||
|
|
||||||
|
case .fetchStatus:
|
||||||
|
guard let status = mpd_run_status(connection) else { break }
|
||||||
|
self.status = status
|
||||||
|
}
|
||||||
|
|
||||||
|
print(getLastErrorMessage()!)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendPlay() {
|
||||||
|
if getState() == .stopped {
|
||||||
|
mpd_run_play(connection)
|
||||||
|
} else {
|
||||||
mpd_run_toggle_pause(connection)
|
mpd_run_toggle_pause(connection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,7 +120,12 @@ class MPDClient {
|
|||||||
mpd_send_idle(self.connection)
|
mpd_send_idle(self.connection)
|
||||||
mpd_recv_idle(self.connection, true)
|
mpd_recv_idle(self.connection, true)
|
||||||
|
|
||||||
if !self.commandQueued { self.idle() }
|
if !self.commandQueued {
|
||||||
|
print("Fetching status")
|
||||||
|
self.fetchStatus()
|
||||||
|
print(self.getState())
|
||||||
|
self.idle()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,6 +138,6 @@ class MPDClient {
|
|||||||
return String(cString: errorMessage)
|
return String(cString: errorMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
return "no error message"
|
return "no error"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,8 +26,7 @@ class WindowController: NSWindowController {
|
|||||||
|
|
||||||
func mpdInit() {
|
func mpdInit() {
|
||||||
mpdClient = MPDClient()
|
mpdClient = MPDClient()
|
||||||
mpdClient?.idle()
|
mpdClient?.connect()
|
||||||
// let state = mpdClient?.getState()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func handleTransportControl(_ sender: NSSegmentedControl) {
|
@IBAction func handleTransportControl(_ sender: NSSegmentedControl) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user