1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00

Move MPD connection to AppDelegate

This commit is contained in:
Daniel Barber 2019-02-01 19:53:34 -05:00
parent 607fa1770d
commit 5d1230f2cc
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
6 changed files with 88 additions and 23 deletions

View File

@ -17,6 +17,8 @@
E41B22C121FB6C3300D544F6 /* libmpdclient.2.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; E41B22C121FB6C3300D544F6 /* libmpdclient.2.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41B22C521FB932700D544F6 /* MPDClient.swift */; }; E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41B22C521FB932700D544F6 /* MPDClient.swift */; };
E465049A21E94DF500A70F4C /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E465049921E94DF500A70F4C /* WindowController.swift */; }; E465049A21E94DF500A70F4C /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E465049921E94DF500A70F4C /* WindowController.swift */; };
E4E8CC902204EC7F0024217A /* MPDClientDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */; };
E4E8CC922204F4B80024217A /* QueueController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8CC912204F4B80024217A /* QueueController.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -103,6 +105,8 @@
E41B22EA21FB966C00D544F6 /* queue.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = queue.h; sourceTree = "<group>"; }; E41B22EA21FB966C00D544F6 /* queue.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = queue.h; sourceTree = "<group>"; };
E41B22EB21FB966C00D544F6 /* playlist.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = playlist.h; sourceTree = "<group>"; }; E41B22EB21FB966C00D544F6 /* playlist.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = playlist.h; sourceTree = "<group>"; };
E465049921E94DF500A70F4C /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = "<group>"; }; E465049921E94DF500A70F4C /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = "<group>"; };
E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDClientDelegate.swift; sourceTree = "<group>"; };
E4E8CC912204F4B80024217A /* QueueController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -160,9 +164,11 @@
E407861D2110CE6E006887B1 /* ViewController.swift */, E407861D2110CE6E006887B1 /* ViewController.swift */,
E407861F2110CE70006887B1 /* Assets.xcassets */, E407861F2110CE70006887B1 /* Assets.xcassets */,
E40786212110CE70006887B1 /* Main.storyboard */, E40786212110CE70006887B1 /* Main.storyboard */,
E4E8CC912204F4B80024217A /* QueueController.swift */,
E465049921E94DF500A70F4C /* WindowController.swift */, E465049921E94DF500A70F4C /* WindowController.swift */,
E40786242110CE70006887B1 /* Info.plist */, E40786242110CE70006887B1 /* Info.plist */,
E40786252110CE70006887B1 /* Persephone.entitlements */, E40786252110CE70006887B1 /* Persephone.entitlements */,
E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */,
E41B22C521FB932700D544F6 /* MPDClient.swift */, E41B22C521FB932700D544F6 /* MPDClient.swift */,
); );
path = Persephone; path = Persephone;
@ -385,6 +391,8 @@
E465049A21E94DF500A70F4C /* WindowController.swift in Sources */, E465049A21E94DF500A70F4C /* WindowController.swift in Sources */,
E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */, E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */,
E407861C2110CE6E006887B1 /* AppDelegate.swift in Sources */, E407861C2110CE6E006887B1 /* AppDelegate.swift in Sources */,
E4E8CC902204EC7F0024217A /* MPDClientDelegate.swift in Sources */,
E4E8CC922204F4B80024217A /* QueueController.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -8,19 +8,17 @@
import Cocoa import Cocoa
extension MPDClient {
static let shared = MPDClient(notificationQueue: .main)
}
@NSApplicationMain @NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate { class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) { func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application MPDClient.shared.connect()
} }
func applicationWillTerminate(_ aNotification: Notification) { func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application MPDClient.shared.disconnect()
} }
} }

View File

@ -713,9 +713,25 @@
</segmentedControl> </segmentedControl>
</toolbarItem> </toolbarItem>
<toolbarItem implicitItemIdentifier="NSToolbarFlexibleSpaceItem" id="9ol-aR-mzv"/> <toolbarItem implicitItemIdentifier="NSToolbarFlexibleSpaceItem" id="9ol-aR-mzv"/>
<toolbarItem implicitItemIdentifier="6D294101-04FF-4E2A-98A7-77A48E2296CD" label="" paletteLabel="" sizingBehavior="auto" id="FoD-o2-4f2">
<nil key="toolTip"/>
<textField key="view" horizontalHuggingPriority="251" verticalHuggingPriority="750" id="KVt-e7-7I5">
<rect key="frame" x="0.0" y="14" width="37" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Label" id="bCv-8Z-qfx">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<action selector="stateLabel:" target="B8D-0N-5wS" id="YXt-Ni-yie"/>
</connections>
</textField>
</toolbarItem>
</allowedToolbarItems> </allowedToolbarItems>
<defaultToolbarItems> <defaultToolbarItems>
<toolbarItem reference="p3r-ty-Pxf"/> <toolbarItem reference="p3r-ty-Pxf"/>
<toolbarItem reference="FoD-o2-4f2"/>
</defaultToolbarItems> </defaultToolbarItems>
</toolbar> </toolbar>
<connections> <connections>
@ -723,6 +739,7 @@
</connections> </connections>
</window> </window>
<connections> <connections>
<outlet property="stateLabel" destination="KVt-e7-7I5" id="xMD-Iq-CSh"/>
<segue destination="fnD-7K-pHK" kind="relationship" relationship="window.shadowedContentViewController" id="fQQ-kB-KVc"/> <segue destination="fnD-7K-pHK" kind="relationship" relationship="window.shadowedContentViewController" id="fQQ-kB-KVc"/>
</connections> </connections>
</windowController> </windowController>
@ -755,10 +772,10 @@
</objects> </objects>
<point key="canvasLocation" x="74" y="677"/> <point key="canvasLocation" x="74" y="677"/>
</scene> </scene>
<!--View Controller--> <!--Queue Controller-->
<scene sceneID="QcX-dC-cTZ"> <scene sceneID="QcX-dC-cTZ">
<objects> <objects>
<viewController id="KIP-rq-4dM" sceneMemberID="viewController"> <viewController id="KIP-rq-4dM" customClass="QueueController" customModule="Persephone" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="2su-YT-hba"> <view key="view" id="2su-YT-hba">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/> <rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask"/> <autoresizingMask key="autoresizingMask"/>

View File

@ -10,8 +10,14 @@ import Foundation
import mpdclient import mpdclient
class MPDClient { class MPDClient {
static let stateChanged = Notification.Name("MPDClientStateChanged")
static let queueChanged = Notification.Name("MPDClientQueueChanged")
static let stateKey = "state"
let HOST = "localhost" let HOST = "localhost"
let PORT: UInt32 = 6600 let PORT: UInt32 = 6600
let notificationQueue: DispatchQueue
private var connection: OpaquePointer? private var connection: OpaquePointer?
private var status: OpaquePointer? private var status: OpaquePointer?
@ -30,6 +36,10 @@ class MPDClient {
case paused = 3 case paused = 3
} }
init(notificationQueue: DispatchQueue) {
self.notificationQueue = notificationQueue
}
func connect() { func connect() {
guard let connection = mpd_connection_new(HOST, PORT, 0) guard let connection = mpd_connection_new(HOST, PORT, 0)
else { return } else { return }
@ -99,8 +109,6 @@ class MPDClient {
guard let status = mpd_run_status(connection) else { break } guard let status = mpd_run_status(connection) else { break }
self.status = status self.status = status
} }
print(getLastErrorMessage()!)
} }
func sendNextTrack() { func sendNextTrack() {
@ -137,9 +145,15 @@ class MPDClient {
mpd_recv_idle(self.connection, true) mpd_recv_idle(self.connection, true)
if !self.commandQueued { if !self.commandQueued {
print("Fetching status")
self.fetchStatus() self.fetchStatus()
print(self.getState()) let state = self.getState()
self.notificationQueue.async {
NotificationCenter.default.post(
name: MPDClient.stateChanged,
object: self,
userInfo: [MPDClient.stateKey: state]
)
}
self.idle() self.idle()
} }
} }

View File

@ -0,0 +1,18 @@
//
// QueueController.swift
// Persephone
//
// Created by Daniel Barber on 2019/2/01.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import Cocoa
class QueueController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}

View File

@ -9,24 +9,32 @@
import Cocoa import Cocoa
class WindowController: NSWindowController { class WindowController: NSWindowController {
let mpdClient = MPDClient.shared
enum TransportAction: Int { enum TransportAction: Int {
case prevTrack = 0 case prevTrack = 0
case playPause = 1 case playPause = 1
case stop = 2 case stop = 2
case nextTrack = 3 case nextTrack = 3
} }
var mpdClient: MPDClient?
override func windowDidLoad() { override func windowDidLoad() {
super.windowDidLoad() super.windowDidLoad()
window?.titleVisibility = .hidden window?.titleVisibility = .hidden
mpdInit() NotificationCenter.default.addObserver(
self,
selector: #selector(stateChanged(_:)),
name: MPDClient.stateChanged,
object: mpdClient
)
} }
func mpdInit() { @objc func stateChanged(_ notification: Notification) {
mpdClient = MPDClient() guard let state = notification.userInfo?[MPDClient.stateKey] as? MPDClient.State
mpdClient?.connect() else { return }
stateLabel.stringValue = "\(state)".localizedCapitalized
} }
@IBAction func handleTransportControl(_ sender: NSSegmentedControl) { @IBAction func handleTransportControl(_ sender: NSSegmentedControl) {
@ -35,13 +43,15 @@ class WindowController: NSWindowController {
switch transportAction { switch transportAction {
case .prevTrack: case .prevTrack:
mpdClient?.prevTrack() mpdClient.prevTrack()
case .playPause: case .playPause:
mpdClient?.playPause() mpdClient.playPause()
case .stop: case .stop:
mpdClient?.stop() mpdClient.stop()
case .nextTrack: case .nextTrack:
mpdClient?.nextTrack() mpdClient.nextTrack()
} }
} }
@IBOutlet var stateLabel: NSTextField!
} }