2016-01-05 04:40:43 +00:00
|
|
|
//
|
|
|
|
// MachineDocument.swift
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 04/01/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
2016-01-15 01:33:22 +00:00
|
|
|
import AudioToolbox
|
2016-01-05 04:40:43 +00:00
|
|
|
|
2016-06-17 00:51:35 +00:00
|
|
|
class MachineDocument:
|
|
|
|
NSDocument,
|
|
|
|
NSWindowDelegate,
|
2016-06-21 01:47:27 +00:00
|
|
|
CSMachineDelegate,
|
2016-06-17 00:51:35 +00:00
|
|
|
CSOpenGLViewDelegate,
|
|
|
|
CSOpenGLViewResponderDelegate,
|
|
|
|
CSBestEffortUpdaterDelegate,
|
|
|
|
CSAudioQueueDelegate
|
|
|
|
{
|
2016-06-01 02:32:38 +00:00
|
|
|
lazy var actionLock = NSLock()
|
|
|
|
lazy var drawLock = NSLock()
|
2016-06-24 01:09:34 +00:00
|
|
|
var machine: CSMachine! {
|
|
|
|
get {
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-01 02:32:38 +00:00
|
|
|
}
|
2016-06-28 01:38:14 +00:00
|
|
|
var name: String! {
|
|
|
|
get {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-06-01 02:32:38 +00:00
|
|
|
|
2016-06-01 23:04:07 +00:00
|
|
|
func aspectRatio() -> NSSize {
|
|
|
|
return NSSize(width: 4.0, height: 3.0)
|
|
|
|
}
|
|
|
|
|
2016-03-05 19:45:09 +00:00
|
|
|
@IBOutlet weak var openGLView: CSOpenGLView! {
|
2016-01-05 04:40:43 +00:00
|
|
|
didSet {
|
|
|
|
openGLView.delegate = self
|
|
|
|
openGLView.responderDelegate = self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 01:43:39 +00:00
|
|
|
@IBOutlet weak var optionsPanel: NSPanel!
|
2016-04-18 12:21:00 +00:00
|
|
|
@IBAction func showOptions(sender: AnyObject!) {
|
2016-04-18 01:43:39 +00:00
|
|
|
optionsPanel?.setIsVisible(true)
|
|
|
|
}
|
|
|
|
|
2016-06-17 00:39:46 +00:00
|
|
|
private var audioQueue: CSAudioQueue! = nil
|
|
|
|
private lazy var bestEffortUpdater: CSBestEffortUpdater = {
|
2016-06-16 22:19:23 +00:00
|
|
|
let updater = CSBestEffortUpdater()
|
|
|
|
updater.delegate = self
|
|
|
|
return updater
|
|
|
|
}()
|
2016-01-15 01:33:22 +00:00
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
override func windowControllerDidLoadNib(aController: NSWindowController) {
|
|
|
|
super.windowControllerDidLoadNib(aController)
|
|
|
|
|
2016-06-01 23:04:07 +00:00
|
|
|
// establish the output aspect ratio and audio
|
|
|
|
let displayAspectRatio = self.aspectRatio()
|
|
|
|
aController.window?.contentAspectRatio = displayAspectRatio
|
|
|
|
openGLView.performWithGLContext({
|
2016-06-24 01:09:34 +00:00
|
|
|
self.machine.setView(self.openGLView, aspectRatio: Float(displayAspectRatio.width / displayAspectRatio.height))
|
2016-06-01 23:04:07 +00:00
|
|
|
})
|
2016-06-01 02:36:53 +00:00
|
|
|
|
2016-06-21 01:47:27 +00:00
|
|
|
setupClockRate()
|
2016-06-24 01:09:34 +00:00
|
|
|
self.machine.delegate = self
|
2016-06-28 01:38:14 +00:00
|
|
|
establishStoredOptions()
|
2016-06-21 01:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func machineDidChangeClockRate(machine: CSMachine!) {
|
|
|
|
setupClockRate()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func setupClockRate() {
|
2016-06-01 23:04:07 +00:00
|
|
|
// 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-06-24 01:09:34 +00:00
|
|
|
let selectedSamplingRate = self.machine.idealSamplingRateFromRange(NSRange(location: 0, length: NSInteger(maximumSamplingRate)))
|
2016-06-05 15:20:05 +00:00
|
|
|
if selectedSamplingRate > 0 {
|
2016-06-15 12:07:25 +00:00
|
|
|
audioQueue = CSAudioQueue(samplingRate: Float64(selectedSamplingRate))
|
2016-06-17 00:51:35 +00:00
|
|
|
audioQueue.delegate = self
|
2016-06-24 01:09:34 +00:00
|
|
|
self.machine.audioQueue = self.audioQueue
|
|
|
|
self.machine.setAudioSamplingRate(selectedSamplingRate, bufferSize:audioQueue.bufferSize / 2)
|
2016-06-05 15:20:05 +00:00
|
|
|
}
|
2016-06-17 00:39:46 +00:00
|
|
|
|
2016-06-24 01:09:34 +00:00
|
|
|
self.bestEffortUpdater.clockRate = self.machine.clockRate
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 02:32:38 +00:00
|
|
|
override func close() {
|
|
|
|
actionLock.lock()
|
|
|
|
drawLock.lock()
|
|
|
|
openGLView.invalidate()
|
|
|
|
openGLView.openGLContext!.makeCurrentContext()
|
|
|
|
actionLock.unlock()
|
|
|
|
drawLock.unlock()
|
|
|
|
|
|
|
|
super.close()
|
|
|
|
}
|
|
|
|
|
2016-06-19 20:35:04 +00:00
|
|
|
// MARK: the pasteboard
|
|
|
|
func paste(sender: AnyObject!) {
|
|
|
|
let pasteboard = NSPasteboard.generalPasteboard()
|
|
|
|
if let string = pasteboard.stringForType(NSPasteboardTypeString) {
|
2016-06-24 01:09:34 +00:00
|
|
|
self.machine.paste(string)
|
2016-06-19 20:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:35 +00:00
|
|
|
// MARK: CSBestEffortUpdaterDelegate
|
2016-06-16 22:19:23 +00:00
|
|
|
final func bestEffortUpdater(bestEffortUpdater: CSBestEffortUpdater!, runForCycles cycles: UInt, didSkipPreviousUpdate: Bool) {
|
|
|
|
runForNumberOfCycles(Int32(cycles))
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:35 +00:00
|
|
|
func runForNumberOfCycles(numberOfCycles: Int32) {
|
2016-06-17 01:35:22 +00:00
|
|
|
let cyclesToRunFor = min(numberOfCycles, Int32(bestEffortUpdater.clockRate / 10))
|
2016-06-17 00:51:35 +00:00
|
|
|
if actionLock.tryLock() {
|
2016-06-24 01:09:34 +00:00
|
|
|
self.machine.runForNumberOfCycles(cyclesToRunFor)
|
2016-06-17 00:51:35 +00:00
|
|
|
actionLock.unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 12:33:01 +00:00
|
|
|
// MARK: Utilities for children
|
|
|
|
func dataForResource(name : String, ofType type: String, inDirectory directory: String) -> NSData? {
|
|
|
|
if let path = NSBundle.mainBundle().pathForResource(name, ofType: type, inDirectory: directory) {
|
|
|
|
return NSData(contentsOfFile: path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:35 +00:00
|
|
|
// MARK: CSAudioQueueDelegate
|
|
|
|
final func audioQueueDidCompleteBuffer(audioQueue: CSAudioQueue) {
|
|
|
|
bestEffortUpdater.update()
|
2016-06-01 02:32:38 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:35 +00:00
|
|
|
// MARK: CSOpenGLViewDelegate
|
|
|
|
final func openGLView(view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) {
|
2016-06-16 22:19:23 +00:00
|
|
|
bestEffortUpdater.update()
|
2016-06-01 02:32:38 +00:00
|
|
|
if drawLock.tryLock() {
|
2016-06-24 01:09:34 +00:00
|
|
|
self.machine.drawViewForPixelSize(view.backingSize, onlyIfDirty: onlyIfDirty)
|
2016-06-01 02:32:38 +00:00
|
|
|
drawLock.unlock()
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 04:40:43 +00:00
|
|
|
|
2016-06-05 01:43:50 +00:00
|
|
|
// MARK: NSDocument overrides
|
|
|
|
override func dataOfType(typeName: String) throws -> NSData {
|
|
|
|
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
|
|
|
|
}
|
2016-06-05 12:53:05 +00:00
|
|
|
|
|
|
|
// MARK: Key forwarding
|
|
|
|
private func withKeyboardMachine(action: (CSKeyboardMachine) -> ()) {
|
2016-06-24 01:09:34 +00:00
|
|
|
if let keyboardMachine = self.machine as? CSKeyboardMachine {
|
2016-06-05 12:53:05 +00:00
|
|
|
action(keyboardMachine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func windowDidResignKey(notification: NSNotification) {
|
|
|
|
self.withKeyboardMachine { $0.clearAllKeys() }
|
|
|
|
}
|
|
|
|
|
|
|
|
func keyDown(event: NSEvent) {
|
|
|
|
self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: true) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func keyUp(event: NSEvent) {
|
|
|
|
self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: false) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func flagsChanged(newModifiers: NSEvent) {
|
|
|
|
self.withKeyboardMachine {
|
|
|
|
$0.setKey(VK_Shift, isPressed: newModifiers.modifierFlags.contains(.ShiftKeyMask))
|
|
|
|
$0.setKey(VK_Control, isPressed: newModifiers.modifierFlags.contains(.ControlKeyMask))
|
|
|
|
$0.setKey(VK_Command, isPressed: newModifiers.modifierFlags.contains(.CommandKeyMask))
|
|
|
|
$0.setKey(VK_Option, isPressed: newModifiers.modifierFlags.contains(.AlternateKeyMask))
|
|
|
|
}
|
|
|
|
}
|
2016-06-28 01:38:14 +00:00
|
|
|
|
|
|
|
// MARK: IBActions
|
2016-08-06 21:39:27 +00:00
|
|
|
final func prefixedUserDefaultsKey(key: String) -> String {
|
|
|
|
return "\(self.name).\(key)"
|
|
|
|
}
|
2016-06-28 01:38:14 +00:00
|
|
|
var fastLoadingUserDefaultsKey: String {
|
|
|
|
get {
|
2016-08-06 21:39:27 +00:00
|
|
|
return prefixedUserDefaultsKey("fastLoading")
|
2016-06-28 01:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 01:48:09 +00:00
|
|
|
@IBOutlet var fastLoadingButton: NSButton?
|
2016-06-28 01:38:14 +00:00
|
|
|
@IBAction func setFastLoading(sender: NSButton!) {
|
2016-07-04 23:19:07 +00:00
|
|
|
if let fastLoadingMachine = machine as? CSFastLoading {
|
2016-06-28 01:38:14 +00:00
|
|
|
let useFastLoadingHack = sender.state == NSOnState
|
2016-07-04 23:19:07 +00:00
|
|
|
fastLoadingMachine.useFastLoadingHack = useFastLoadingHack
|
2016-06-28 01:38:14 +00:00
|
|
|
NSUserDefaults.standardUserDefaults().setBool(useFastLoadingHack, forKey: fastLoadingUserDefaultsKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func establishStoredOptions() {
|
|
|
|
let standardUserDefaults = NSUserDefaults.standardUserDefaults()
|
|
|
|
standardUserDefaults.registerDefaults([
|
|
|
|
fastLoadingUserDefaultsKey: true
|
|
|
|
])
|
|
|
|
|
2016-07-04 23:19:07 +00:00
|
|
|
if let fastLoadingMachine = machine as? CSFastLoading {
|
2016-06-28 01:38:14 +00:00
|
|
|
let useFastLoadingHack = standardUserDefaults.boolForKey(self.fastLoadingUserDefaultsKey)
|
2016-07-04 23:19:07 +00:00
|
|
|
fastLoadingMachine.useFastLoadingHack = useFastLoadingHack
|
2016-08-08 01:48:09 +00:00
|
|
|
self.fastLoadingButton?.state = useFastLoadingHack ? NSOnState : NSOffState
|
2016-06-28 01:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|