1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-11 15:49:38 +00:00
CLK/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift

209 lines
6.1 KiB
Swift
Raw Normal View History

//
// MachineDocument.swift
// Clock Signal
//
// Created by Thomas Harte on 04/01/2016.
// Copyright © 2016 Thomas Harte. All rights reserved.
//
import Cocoa
import AudioToolbox
class MachineDocument:
NSDocument,
NSWindowDelegate,
CSMachineDelegate,
CSOpenGLViewDelegate,
CSOpenGLViewResponderDelegate,
CSBestEffortUpdaterDelegate,
CSAudioQueueDelegate
{
lazy var actionLock = NSLock()
lazy var drawLock = NSLock()
var machine: CSMachine!
var name: String! {
get {
return nil
}
}
func aspectRatio() -> NSSize {
return NSSize(width: 4.0, height: 3.0)
}
@IBOutlet weak var openGLView: CSOpenGLView!
@IBOutlet var optionsPanel: MachinePanel!
2016-09-16 02:12:12 +00:00
@IBAction func showOptions(_ sender: AnyObject!) {
2016-04-18 01:43:39 +00:00
optionsPanel?.setIsVisible(true)
}
2016-09-16 02:12:12 +00:00
fileprivate var audioQueue: CSAudioQueue! = nil
fileprivate var bestEffortUpdater: CSBestEffortUpdater?
override var windowNibName: NSNib.Name? {
return NSNib.Name(rawValue: "MachineDocument")
}
2016-09-16 02:12:12 +00:00
override func windowControllerDidLoadNib(_ aController: NSWindowController) {
super.windowControllerDidLoadNib(aController)
// establish the output aspect ratio and audio
let displayAspectRatio = self.aspectRatio()
aController.window?.contentAspectRatio = displayAspectRatio
2016-09-16 02:12:12 +00:00
openGLView.perform(glContext: {
self.machine.setView(self.openGLView, aspectRatio: Float(displayAspectRatio.width / displayAspectRatio.height))
})
self.machine.delegate = self
self.bestEffortUpdater = CSBestEffortUpdater()
// callbacks from the OpenGL may come on a different thread, immediately following the .delegate set;
// hence the full setup of the best-effort updater prior to setting self as a delegate
self.openGLView.delegate = self
self.openGLView.responderDelegate = self
setupClockRate()
self.optionsPanel?.establishStoredOptions()
// bring OpenGL view-holding window on top of the options panel
self.openGLView.window!.makeKeyAndOrderFront(self)
// start accepting best effort updates
self.bestEffortUpdater!.delegate = self
}
2016-09-16 02:12:12 +00:00
func machineDidChangeClockRate(_ machine: CSMachine!) {
setupClockRate()
}
2016-09-16 02:12:12 +00:00
func machineDidChangeClockIsUnlimited(_ machine: CSMachine!) {
self.bestEffortUpdater?.runAsUnlimited = machine.clockIsUnlimited
}
2016-09-16 02:12:12 +00:00
fileprivate func setupClockRate() {
// establish and provide the audio queue, taking advice as to an appropriate sampling rate
2016-06-15 12:07:25 +00:00
let maximumSamplingRate = CSAudioQueue.preferredSamplingRate()
2016-09-16 02:12:12 +00:00
let selectedSamplingRate = self.machine.idealSamplingRate(from: NSRange(location: 0, length: NSInteger(maximumSamplingRate)))
if selectedSamplingRate > 0 {
2016-06-15 12:07:25 +00:00
audioQueue = CSAudioQueue(samplingRate: Float64(selectedSamplingRate))
audioQueue.delegate = self
self.machine.audioQueue = self.audioQueue
2016-10-10 11:45:09 +00:00
self.machine.setAudioSamplingRate(selectedSamplingRate, bufferSize:audioQueue.preferredBufferSize)
}
self.bestEffortUpdater?.clockRate = self.machine.clockRate
}
override func close() {
optionsPanel?.setIsVisible(false)
optionsPanel = nil
openGLView.delegate = nil
bestEffortUpdater!.delegate = nil
bestEffortUpdater = nil
actionLock.lock()
drawLock.lock()
machine = nil
openGLView.invalidate()
actionLock.unlock()
drawLock.unlock()
super.close()
}
// MARK: configuring
2016-09-16 02:12:12 +00:00
func configureAs(_ analysis: CSStaticAnalyser) {
if let machine = analysis.newMachine() {
self.machine = machine
}
2016-09-16 02:12:12 +00:00
analysis.apply(to: self.machine)
if let optionsPanelNibName = analysis.optionsPanelNibName {
Bundle.main.loadNibNamed(NSNib.Name(rawValue: optionsPanelNibName), owner: self, topLevelObjects: nil)
self.optionsPanel.machine = self.machine
showOptions(self)
}
}
override func read(from url: URL, ofType typeName: String) throws {
if let analyser = CSStaticAnalyser(fileAt: url) {
self.displayName = analyser.displayName
self.configureAs(analyser)
} else {
throw NSError(domain: "MachineDocument", code: -1, userInfo: nil)
}
}
// MARK: the pasteboard
2016-09-16 02:12:12 +00:00
func paste(_ sender: AnyObject!) {
let pasteboard = NSPasteboard.general
if let string = pasteboard.string(forType: .string) {
self.machine.paste(string)
}
}
// MARK: CSBestEffortUpdaterDelegate
2016-09-16 02:12:12 +00:00
final func bestEffortUpdater(_ bestEffortUpdater: CSBestEffortUpdater!, runForCycles cycles: UInt, didSkipPreviousUpdate: Bool) {
runForNumberOfCycles(Int32(cycles))
}
2016-09-16 02:12:12 +00:00
func runForNumberOfCycles(_ numberOfCycles: Int32) {
if let bestEffortUpdater = bestEffortUpdater {
let cyclesToRunFor = min(numberOfCycles, Int32(bestEffortUpdater.clockRate / 10))
if actionLock.try() {
self.machine.runForNumber(ofCycles: cyclesToRunFor)
actionLock.unlock()
}
}
}
// MARK: CSAudioQueueDelegate
final func audioQueueIsRunningDry(_ audioQueue: CSAudioQueue) {
bestEffortUpdater?.update()
}
// MARK: CSOpenGLViewDelegate
2016-09-16 02:12:12 +00:00
final func openGLView(_ view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) {
if let bestEffortUpdater = bestEffortUpdater {
bestEffortUpdater.update()
if drawLock.try() {
self.machine.drawView(forPixelSize: view.backingSize, onlyIfDirty: onlyIfDirty)
drawLock.unlock()
}
}
}
final func openGLView(_ view: CSOpenGLView, didReceiveFileAt URL: URL) {
let mediaSet = CSMediaSet(fileAt: URL)
if let mediaSet = mediaSet {
mediaSet.apply(to: self.machine)
}
}
// MARK: NSDocument overrides
2016-09-16 02:12:12 +00:00
override func data(ofType typeName: String) throws -> Data {
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
// MARK: Input management
2016-09-16 02:12:12 +00:00
func windowDidResignKey(_ notification: Notification) {
self.machine.clearAllKeys()
}
2016-09-16 02:12:12 +00:00
func keyDown(_ event: NSEvent) {
self.machine.setKey(event.keyCode, isPressed: true)
}
2016-09-16 02:12:12 +00:00
func keyUp(_ event: NSEvent) {
self.machine.setKey(event.keyCode, isPressed: false)
}
2016-09-16 02:12:12 +00:00
func flagsChanged(_ newModifiers: NSEvent) {
self.machine.setKey(VK_Shift, isPressed: newModifiers.modifierFlags.contains(.shift))
self.machine.setKey(VK_Control, isPressed: newModifiers.modifierFlags.contains(.control))
self.machine.setKey(VK_Command, isPressed: newModifiers.modifierFlags.contains(.command))
self.machine.setKey(VK_Option, isPressed: newModifiers.modifierFlags.contains(.option))
}
}