diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index d5c0e2b9e..f340bb52b 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -37,7 +37,6 @@ 4B4DC8211D2C2425003C5BF8 /* Vic20.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B4DC81F1D2C2425003C5BF8 /* Vic20.cpp */; }; 4B4DC8281D2C2470003C5BF8 /* C1540.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B4DC8261D2C2470003C5BF8 /* C1540.cpp */; }; 4B4DC82B1D2C27A4003C5BF8 /* SerialBus.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B4DC8291D2C27A4003C5BF8 /* SerialBus.cpp */; }; - 4B55CE581C3B7D360093A61B /* Atari2600Document.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B55CE561C3B7D360093A61B /* Atari2600Document.swift */; }; 4B55CE5D1C3B7D6F0093A61B /* CSOpenGLView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B55CE5C1C3B7D6F0093A61B /* CSOpenGLView.m */; }; 4B55CE5F1C3B7D960093A61B /* MachineDocument.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B55CE5E1C3B7D960093A61B /* MachineDocument.swift */; }; 4B643F3A1D77AD1900D431D6 /* CSStaticAnalyser.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B643F391D77AD1900D431D6 /* CSStaticAnalyser.mm */; }; @@ -453,7 +452,6 @@ 4B4DC8271D2C2470003C5BF8 /* C1540.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = C1540.hpp; sourceTree = ""; }; 4B4DC8291D2C27A4003C5BF8 /* SerialBus.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SerialBus.cpp; sourceTree = ""; }; 4B4DC82A1D2C27A4003C5BF8 /* SerialBus.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SerialBus.hpp; sourceTree = ""; }; - 4B55CE561C3B7D360093A61B /* Atari2600Document.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Atari2600Document.swift; sourceTree = ""; }; 4B55CE5B1C3B7D6F0093A61B /* CSOpenGLView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSOpenGLView.h; sourceTree = ""; }; 4B55CE5C1C3B7D6F0093A61B /* CSOpenGLView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CSOpenGLView.m; sourceTree = ""; }; 4B55CE5E1C3B7D960093A61B /* MachineDocument.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MachineDocument.swift; sourceTree = ""; }; @@ -481,6 +479,7 @@ 4B96F7201D75119A0058BB2D /* Tape.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Tape.cpp; path = ../../StaticAnalyser/Acorn/Tape.cpp; sourceTree = ""; }; 4B96F7211D75119A0058BB2D /* Tape.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Tape.hpp; path = ../../StaticAnalyser/Acorn/Tape.hpp; sourceTree = ""; }; 4B9CCDA01DA279CA0098B625 /* Vic20OptionsPanel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Vic20OptionsPanel.swift; sourceTree = ""; }; + 4B9CCDA21DA27C3F0098B625 /* CSJoystickMachine.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CSJoystickMachine.h; sourceTree = ""; }; 4BA22B051D8817CE0008C640 /* Disk.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Disk.cpp; path = ../../StaticAnalyser/Commodore/Disk.cpp; sourceTree = ""; }; 4BA22B061D8817CE0008C640 /* Disk.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Disk.hpp; path = ../../StaticAnalyser/Commodore/Disk.hpp; sourceTree = ""; }; 4BA61EAE1D91515900B3C876 /* NSData+StdVector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+StdVector.h"; sourceTree = ""; }; @@ -938,18 +937,19 @@ isa = PBXGroup; children = ( 4BBC34241D2208B100FFC9DF /* CSFastLoading.h */, + 4B9CCDA21DA27C3F0098B625 /* CSJoystickMachine.h */, 4B2A53931D117D36003C6002 /* CSKeyboardMachine.h */, 4B2A53951D117D36003C6002 /* CSMachine.h */, 4B2A53941D117D36003C6002 /* CSMachine+Subclassing.h */, 4B643F3C1D77AE5C00D431D6 /* CSMachine+Target.h */, 4B2A53971D117D36003C6002 /* KeyCodes.h */, + 4B8FE2251DA1DE2D0090D3CE /* NSBundle+DataResource.h */, + 4BA61EAE1D91515900B3C876 /* NSData+StdVector.h */, + 4B8FE2261DA1DE2D0090D3CE /* NSBundle+DataResource.m */, 4B2A53961D117D36003C6002 /* CSMachine.mm */, + 4BA61EAF1D91515900B3C876 /* NSData+StdVector.mm */, 4B643F3B1D77AD6D00D431D6 /* StaticAnalyser */, 4B2A53981D117D36003C6002 /* Wrappers */, - 4BA61EAE1D91515900B3C876 /* NSData+StdVector.h */, - 4BA61EAF1D91515900B3C876 /* NSData+StdVector.mm */, - 4B8FE2251DA1DE2D0090D3CE /* NSBundle+DataResource.h */, - 4B8FE2261DA1DE2D0090D3CE /* NSBundle+DataResource.m */, ); path = Machine; sourceTree = ""; @@ -1048,7 +1048,6 @@ 4B55CE551C3B7D360093A61B /* Documents */ = { isa = PBXGroup; children = ( - 4B55CE561C3B7D360093A61B /* Atari2600Document.swift */, 4B8FE21F1DA19D7C0090D3CE /* Atari2600OptionsPanel.swift */, 4B8FE2281DA1EDDF0090D3CE /* ElectronOptionsPanel.swift */, 4B55CE5E1C3B7D960093A61B /* MachineDocument.swift */, @@ -2151,7 +2150,6 @@ 4B4DC82B1D2C27A4003C5BF8 /* SerialBus.cpp in Sources */, 4BC3B74F1CD194CC00F86E85 /* Shader.cpp in Sources */, 4B8FE2221DA19FB20090D3CE /* MachinePanel.swift in Sources */, - 4B55CE581C3B7D360093A61B /* Atari2600Document.swift in Sources */, 4BBB14311CD2CECE00BDB55C /* IntermediateShader.cpp in Sources */, 4BD5F1951D13528900631CD1 /* CSBestEffortUpdater.m in Sources */, 4B96F7221D75119A0058BB2D /* Tape.cpp in Sources */, diff --git a/OSBindings/Mac/Clock Signal/Document Controller/DocumentController.swift b/OSBindings/Mac/Clock Signal/Document Controller/DocumentController.swift index 7171ce0d1..01ec875bb 100644 --- a/OSBindings/Mac/Clock Signal/Document Controller/DocumentController.swift +++ b/OSBindings/Mac/Clock Signal/Document Controller/DocumentController.swift @@ -9,18 +9,4 @@ import Cocoa class DocumentController: NSDocumentController { -/* override func makeDocument(withContentsOf url: URL, ofType typeName: String) throws -> NSDocument { - if let analyser = CSStaticAnalyser(fileAt: url) { - if let documentClass = analyser.documentClass as? NSDocument.Type { - let document = documentClass.init() - if let machineDocument = document as? MachineDocument { - machineDocument.displayName = analyser.displayName - machineDocument.configureAs(analyser) - return machineDocument - } - } - } - - return try super.makeDocument(withContentsOf: url, ofType: typeName) - }*/ } diff --git a/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift b/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift deleted file mode 100644 index e23fff014..000000000 --- a/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift +++ /dev/null @@ -1,50 +0,0 @@ -// -// Atari2600Document.swift -// Clock Signal -// -// Created by Thomas Harte on 16/07/2015. -// Copyright © 2015 Thomas Harte. All rights reserved. -// - -import Cocoa - -class Atari2600Document: MachineDocument { - - // MARK: CSOpenGLViewResponderDelegate -/* fileprivate func inputForKey(_ event: NSEvent) -> Atari2600DigitalInput? { - switch event.keyCode { - case 123: return Atari2600DigitalInputJoy1Left - case 126: return Atari2600DigitalInputJoy1Up - case 124: return Atari2600DigitalInputJoy1Right - case 125: return Atari2600DigitalInputJoy1Down - case 0: return Atari2600DigitalInputJoy1Fire - default: - Swift.print("\(event.keyCode)") - return nil - } - } - - override func keyDown(_ event: NSEvent) { - super.keyDown(event) - - if let input = inputForKey(event) { - atari2600.setState(true, for: input) - } - - if event.keyCode == 36 { - atari2600.setResetLineEnabled(true) - } - } - - override func keyUp(_ event: NSEvent) { - super.keyUp(event) - - if let input = inputForKey(event) { - atari2600.setState(false, for: input) - } - - if event.keyCode == 36 { - atari2600.setResetLineEnabled(false) - } - }*/ -} diff --git a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift index 7ff3c7ae4..5ef302a53 100644 --- a/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift +++ b/OSBindings/Mac/Clock Signal/Documents/MachineDocument.swift @@ -163,23 +163,41 @@ class MachineDocument: throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil) } - // MARK: Key forwarding + // MARK: Input management fileprivate func withKeyboardMachine(_ action: (CSKeyboardMachine) -> ()) { if let keyboardMachine = self.machine as? CSKeyboardMachine { action(keyboardMachine) } } + 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) + } + } + func windowDidResignKey(_ notification: Notification) { self.withKeyboardMachine { $0.clearAllKeys() } } func keyDown(_ event: NSEvent) { self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: true) } + self.withJoystickMachine { sendJoystickEvent($0, keyCode: event.keyCode, isPressed: false) } } func keyUp(_ event: NSEvent) { self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: false) } + self.withJoystickMachine { sendJoystickEvent($0, keyCode: event.keyCode, isPressed: true) } } func flagsChanged(_ newModifiers: NSEvent) { diff --git a/OSBindings/Mac/Clock Signal/Machine/CSJoystickMachine.h b/OSBindings/Mac/Clock Signal/Machine/CSJoystickMachine.h new file mode 100644 index 000000000..ed1cc5e67 --- /dev/null +++ b/OSBindings/Mac/Clock Signal/Machine/CSJoystickMachine.h @@ -0,0 +1,22 @@ +// +// CSJoystickMachine.h +// Clock Signal +// +// Created by Thomas Harte on 03/10/2016. +// Copyright © 2016 Thomas Harte. All rights reserved. +// + +typedef NS_ENUM(NSInteger, CSJoystickDirection) +{ + CSJoystickDirectionUp, + CSJoystickDirectionDown, + CSJoystickDirectionLeft, + CSJoystickDirectionRight +}; + +@protocol CSJoystickMachine + +- (void)setButtonAtIndex:(NSUInteger)button onPad:(NSUInteger)pad isPressed:(BOOL)isPressed; +- (void)setDirection:(CSJoystickDirection)direction onPad:(NSUInteger)pad isPressed:(BOOL)isPressed; + +@end diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h index 3fa47b124..7026f02d7 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.h @@ -8,10 +8,10 @@ #include "CSMachine.h" #include "Atari2600Inputs.h" +#import "CSJoystickMachine.h" -@interface CSAtari2600 : CSMachine +@interface CSAtari2600 : CSMachine -- (void)setState:(BOOL)state forDigitalInput:(Atari2600DigitalInput)digitalInput; - (void)setResetLineEnabled:(BOOL)enabled; @property (nonatomic, assign) BOOL colourButton; diff --git a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm index 13865638a..bf268669b 100644 --- a/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm +++ b/OSBindings/Mac/Clock Signal/Machine/Wrappers/CSAtari2600.mm @@ -49,9 +49,23 @@ struct CRTDelegate: public Outputs::CRT::Delegate { } } -- (void)setState:(BOOL)state forDigitalInput:(Atari2600DigitalInput)digitalInput { +- (void)setDirection:(CSJoystickDirection)direction onPad:(NSUInteger)pad isPressed:(BOOL)isPressed { + Atari2600DigitalInput input; + switch(direction) + { + case CSJoystickDirectionUp: input = pad ? Atari2600DigitalInputJoy2Up : Atari2600DigitalInputJoy1Up; break; + case CSJoystickDirectionDown: input = pad ? Atari2600DigitalInputJoy2Down : Atari2600DigitalInputJoy1Down; break; + case CSJoystickDirectionLeft: input = pad ? Atari2600DigitalInputJoy2Left : Atari2600DigitalInputJoy1Left; break; + case CSJoystickDirectionRight: input = pad ? Atari2600DigitalInputJoy2Right : Atari2600DigitalInputJoy1Right; break; + } @synchronized(self) { - _atari2600.set_digital_input(digitalInput, state ? true : false); + _atari2600.set_digital_input(input, isPressed ? true : false); + } +} + +- (void)setButtonAtIndex:(NSUInteger)button onPad:(NSUInteger)pad isPressed:(BOOL)isPressed { + @synchronized(self) { + _atari2600.set_digital_input(pad ? Atari2600DigitalInputJoy2Fire : Atari2600DigitalInputJoy1Fire, isPressed ? true : false); } }