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-10-02 20:57:57 +00:00
|
|
|
var machine: CSMachine!
|
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-10-02 20:31:50 +00:00
|
|
|
@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 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-10-02 20:57:57 +00:00
|
|
|
override var windowNibName: String? {
|
|
|
|
return "MachineDocument"
|
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
override func windowControllerDidLoadNib(_ aController: NSWindowController) {
|
2016-01-05 04:40:43 +00:00
|
|
|
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
|
2016-09-16 02:12:12 +00:00
|
|
|
openGLView.perform(glContext: {
|
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-10-03 02:04:47 +00:00
|
|
|
self.optionsPanel?.establishStoredOptions()
|
2016-10-25 02:08:24 +00:00
|
|
|
|
|
|
|
// bring OpenGL view-holding window on top of the options panel
|
|
|
|
self.openGLView.window!.makeKeyAndOrderFront(self)
|
2016-06-21 01:47:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
func machineDidChangeClockRate(_ machine: CSMachine!) {
|
2016-06-21 01:47:27 +00:00
|
|
|
setupClockRate()
|
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
func machineDidChangeClockIsUnlimited(_ machine: CSMachine!) {
|
2016-09-13 02:06:03 +00:00
|
|
|
self.bestEffortUpdater.runAsUnlimited = machine.clockIsUnlimited
|
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
fileprivate 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-09-16 02:12:12 +00:00
|
|
|
let selectedSamplingRate = self.machine.idealSamplingRate(from: 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
|
2016-10-10 11:45:09 +00:00
|
|
|
self.machine.setAudioSamplingRate(selectedSamplingRate, bufferSize:audioQueue.preferredBufferSize)
|
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() {
|
2016-10-17 02:14:47 +00:00
|
|
|
bestEffortUpdater.flush()
|
2016-06-01 02:32:38 +00:00
|
|
|
actionLock.lock()
|
|
|
|
drawLock.lock()
|
|
|
|
openGLView.invalidate()
|
|
|
|
openGLView.openGLContext!.makeCurrentContext()
|
|
|
|
actionLock.unlock()
|
|
|
|
drawLock.unlock()
|
|
|
|
|
|
|
|
super.close()
|
|
|
|
}
|
|
|
|
|
2016-09-01 02:03:42 +00:00
|
|
|
// MARK: configuring
|
2016-09-16 02:12:12 +00:00
|
|
|
func configureAs(_ analysis: CSStaticAnalyser) {
|
2016-10-02 20:57:57 +00:00
|
|
|
if let machine = analysis.newMachine() {
|
|
|
|
self.machine = machine
|
|
|
|
}
|
2016-09-16 02:12:12 +00:00
|
|
|
analysis.apply(to: self.machine)
|
2016-10-02 20:31:50 +00:00
|
|
|
|
|
|
|
if let optionsPanelNibName = analysis.optionsPanelNibName {
|
|
|
|
Bundle.main.loadNibNamed(optionsPanelNibName, owner: self, topLevelObjects: nil)
|
|
|
|
self.optionsPanel.machine = self.machine
|
|
|
|
showOptions(self)
|
|
|
|
}
|
2016-09-01 02:03:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 21:04:14 +00:00
|
|
|
override func read(from url: URL, ofType typeName: String) throws {
|
|
|
|
if let analyser = CSStaticAnalyser(fileAt: url) {
|
|
|
|
self.displayName = analyser.displayName
|
|
|
|
self.configureAs(analyser)
|
2016-10-12 01:03:01 +00:00
|
|
|
} else {
|
|
|
|
throw NSError(domain: "MachineDocument", code: -1, userInfo: nil)
|
2016-10-02 21:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 20:35:04 +00:00
|
|
|
// 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: 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-09-16 02:12:12 +00:00
|
|
|
final func bestEffortUpdater(_ bestEffortUpdater: CSBestEffortUpdater!, runForCycles cycles: UInt, didSkipPreviousUpdate: Bool) {
|
2016-06-16 22:19:23 +00:00
|
|
|
runForNumberOfCycles(Int32(cycles))
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
func runForNumberOfCycles(_ numberOfCycles: Int32) {
|
2016-06-17 01:35:22 +00:00
|
|
|
let cyclesToRunFor = min(numberOfCycles, Int32(bestEffortUpdater.clockRate / 10))
|
2016-09-16 02:12:12 +00:00
|
|
|
if actionLock.try() {
|
|
|
|
self.machine.runForNumber(ofCycles: cyclesToRunFor)
|
2016-06-17 00:51:35 +00:00
|
|
|
actionLock.unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: CSAudioQueueDelegate
|
2016-10-17 12:18:32 +00:00
|
|
|
final func audioQueueIsRunningDry(_ audioQueue: CSAudioQueue) {
|
2016-06-17 00:51:35 +00:00
|
|
|
bestEffortUpdater.update()
|
2016-06-01 02:32:38 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 00:51:35 +00:00
|
|
|
// MARK: CSOpenGLViewDelegate
|
2016-09-16 02:12:12 +00:00
|
|
|
final func openGLView(_ view: CSOpenGLView, drawViewOnlyIfDirty onlyIfDirty: Bool) {
|
2016-06-16 22:19:23 +00:00
|
|
|
bestEffortUpdater.update()
|
2016-09-16 02:12:12 +00:00
|
|
|
if drawLock.try() {
|
|
|
|
self.machine.drawView(forPixelSize: 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
|
2016-09-16 02:12:12 +00:00
|
|
|
override func data(ofType typeName: String) throws -> Data {
|
2016-06-05 01:43:50 +00:00
|
|
|
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
|
|
|
|
}
|
2016-06-05 12:53:05 +00:00
|
|
|
|
2016-10-03 12:01:04 +00:00
|
|
|
// MARK: Input management
|
2016-09-16 02:12:12 +00:00
|
|
|
fileprivate 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 12:01:04 +00:00
|
|
|
fileprivate func withJoystickMachine(_ action: (CSJoystickMachine) -> ()) {
|
|
|
|
if let joystickMachine = self.machine as? CSJoystickMachine {
|
|
|
|
action(joystickMachine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate func sendJoystickEvent(_ machine: CSJoystickMachine, keyCode: UInt16, isPressed: Bool) {
|
|
|
|
switch keyCode {
|
|
|
|
case 123: machine.setDirection(.left, onPad: 0, isPressed: isPressed)
|
|
|
|
case 126: machine.setDirection(.up, onPad: 0, isPressed: isPressed)
|
|
|
|
case 124: machine.setDirection(.right, onPad: 0, isPressed: isPressed)
|
|
|
|
case 125: machine.setDirection(.down, onPad: 0, isPressed: isPressed)
|
|
|
|
default: machine.setButtonAt(0, onPad: 0, isPressed: isPressed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
func windowDidResignKey(_ notification: Notification) {
|
2016-06-05 12:53:05 +00:00
|
|
|
self.withKeyboardMachine { $0.clearAllKeys() }
|
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
func keyDown(_ event: NSEvent) {
|
2016-06-05 12:53:05 +00:00
|
|
|
self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: true) }
|
2016-10-27 23:44:48 +00:00
|
|
|
self.withJoystickMachine { sendJoystickEvent($0, keyCode: event.keyCode, isPressed: true) }
|
2016-06-05 12:53:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
func keyUp(_ event: NSEvent) {
|
2016-06-05 12:53:05 +00:00
|
|
|
self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: false) }
|
2016-10-27 23:44:48 +00:00
|
|
|
self.withJoystickMachine { sendJoystickEvent($0, keyCode: event.keyCode, isPressed: false) }
|
2016-06-05 12:53:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
func flagsChanged(_ newModifiers: NSEvent) {
|
2016-06-05 12:53:05 +00:00
|
|
|
self.withKeyboardMachine {
|
2016-09-16 02:12:12 +00:00
|
|
|
$0.setKey(VK_Shift, isPressed: newModifiers.modifierFlags.contains(.shift))
|
|
|
|
$0.setKey(VK_Control, isPressed: newModifiers.modifierFlags.contains(.control))
|
|
|
|
$0.setKey(VK_Command, isPressed: newModifiers.modifierFlags.contains(.command))
|
|
|
|
$0.setKey(VK_Option, isPressed: newModifiers.modifierFlags.contains(.option))
|
2016-06-05 12:53:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|