From 478d081095f7cc21a737e6436dd21f3071122e4a Mon Sep 17 00:00:00 2001
From: Thomas Harte <thomas.harte@gmail.com>
Date: Tue, 23 Jun 2020 23:27:56 -0400
Subject: [PATCH] Ensures machines take user-friendly default settings.

---
 Machines/Utility/MachineForTarget.hpp         |  1 +
 .../Mac/Clock Signal/Machine/CSMachine.mm     |  1 -
 OSBindings/Qt/mainwindow.cpp                  | 35 ++++++++-----------
 3 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/Machines/Utility/MachineForTarget.hpp b/Machines/Utility/MachineForTarget.hpp
index 27ebe6581..e0b0dbc37 100644
--- a/Machines/Utility/MachineForTarget.hpp
+++ b/Machines/Utility/MachineForTarget.hpp
@@ -82,6 +82,7 @@ std::vector<std::string> AllMachines(Type type, bool long_names);
 
 /*!
 	Returns a map from long machine name to the list of options that machine exposes, for all machines.
+	In all cases, user-friendly selections will have been filled in by default.
 */
 std::map<std::string, std::unique_ptr<Reflection::Struct>> AllOptionsByMachineName();
 
diff --git a/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm b/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm
index 8d7d841f0..cd5d8ba25 100644
--- a/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm	
+++ b/OSBindings/Mac/Clock Signal/Machine/CSMachine.mm	
@@ -151,7 +151,6 @@ struct ActivityObserver: public Activity::Observer {
 	MachineTypes::JoystickMachine *_joystickMachine;
 
 	CSJoystickManager *_joystickManager;
-	std::bitset<65536> _depressedKeys;
 	NSMutableArray<NSString *> *_leds;
 
 	CSHighPrecisionTimer *_timer;
diff --git a/OSBindings/Qt/mainwindow.cpp b/OSBindings/Qt/mainwindow.cpp
index 298f7dc5c..a1102b858 100644
--- a/OSBindings/Qt/mainwindow.cpp
+++ b/OSBindings/Qt/mainwindow.cpp
@@ -282,6 +282,16 @@ void MainWindow::launchMachine() {
 						audioBuffer.setDepth(audioOutput->bufferSize());
 					});
 				}
+
+				// Set user-friendly default options.
+				const std::string longMachineName = Machine::LongNameForTargetMachine(targets[0]->machine);
+				const auto configurable = machine->configurable_device();
+				if(configurable) {
+					configurable->set_options(Machine::AllOptionsByMachineName()[longMachineName]);
+				}
+
+				// Update the window title. TODO: clearly I need a proper functional solution for this.
+				setWindowTitle(QString::fromStdString(longMachineName));
 			}
 
 			// If this is a timed machine, start up the timer.
@@ -459,6 +469,10 @@ bool MainWindow::processEvent(QKeyEvent *event) {
 	switch(event->key()) {
 		default: return true;
 
+		// TODO: Qt factors in modifiers when deciding which key to declare has been pressed.
+		// E.g. on my keyboard a shifted Key_Comma produces a Key_Less, not a Key_Comma and a shift.
+		// Find a way to disable that, or else work around it here.
+
 		BIND(Escape);
 		BIND(F1);	BIND(F2);	BIND(F3);	BIND(F4);	BIND(F5);	BIND(F6);
 		BIND(F7);	BIND(F8);	BIND(F9);	BIND(F10);	BIND(F11);	BIND(F12);
@@ -481,6 +495,7 @@ bool MainWindow::processEvent(QKeyEvent *event) {
 		BIND(CapsLock);	BIND(A);	BIND(S);	BIND(D);	BIND(F);	BIND(G);
 		BIND(H);		BIND(J);	BIND(K);	BIND(L);
 		BIND(Semicolon);
+		BIND2(Key_Apostrophe, Quote);
 		BIND2(Key_QuoteDbl, Quote);
 		// TODO: something to hash?
 		BIND2(Key_Return, Enter);
@@ -548,8 +563,6 @@ void MainWindow::startMachine() {
 }
 
 void MainWindow::start_appleII() {
-	setWindowTitle(tr("Apple II"));
-
 	using Target = Analyser::Static::AppleII::Target;
 	auto target = std::make_unique<Target>();
 
@@ -570,8 +583,6 @@ void MainWindow::start_appleII() {
 }
 
 void MainWindow::start_amstradCPC() {
-	setWindowTitle(tr("Amstrad CPC"));
-
 	using Target = Analyser::Static::AmstradCPC::Target;
 	auto target = std::make_unique<Target>();
 
@@ -585,8 +596,6 @@ void MainWindow::start_amstradCPC() {
 }
 
 void MainWindow::start_atariST() {
-	setWindowTitle(tr("Atari ST"));
-
 	using Target = Analyser::Static::AtariST::Target;
 	auto target = std::make_unique<Target>();
 
@@ -596,8 +605,6 @@ void MainWindow::start_atariST() {
 }
 
 void MainWindow::start_electron() {
-	setWindowTitle(tr("Acorn Electron"));
-
 	using Target = Analyser::Static::Acorn::Target;
 	auto target = std::make_unique<Target>();
 
@@ -608,8 +615,6 @@ void MainWindow::start_electron() {
 }
 
 void MainWindow::start_macintosh() {
-	setWindowTitle(tr("Macintosh"));
-
 	using Target = Analyser::Static::Macintosh::Target;
 	auto target = std::make_unique<Target>();
 
@@ -622,8 +627,6 @@ void MainWindow::start_macintosh() {
 }
 
 void MainWindow::start_msx() {
-	setWindowTitle(tr("MSX"));
-
 	using Target = Analyser::Static::MSX::Target;
 	auto target = std::make_unique<Target>();
 
@@ -639,8 +642,6 @@ void MainWindow::start_msx() {
 }
 
 void MainWindow::start_oric() {
-	setWindowTitle(tr("Oric"));
-
 	using Target = Analyser::Static::Oric::Target;
 	auto target = std::make_unique<Target>();
 
@@ -662,8 +663,6 @@ void MainWindow::start_oric() {
 }
 
 void MainWindow::start_vic20() {
-	setWindowTitle(tr("Vic-20"));
-
 	using Target = Analyser::Static::Commodore::Target;
 	auto target = std::make_unique<Target>();
 
@@ -689,8 +688,6 @@ void MainWindow::start_vic20() {
 }
 
 void MainWindow::start_zx80() {
-	setWindowTitle(tr("ZX80"));
-
 	using Target = Analyser::Static::ZX8081::Target;
 	auto target = std::make_unique<Target>();
 
@@ -706,8 +703,6 @@ void MainWindow::start_zx80() {
 }
 
 void MainWindow::start_zx81() {
-	setWindowTitle(tr("ZX81"));
-
 	using Target = Analyser::Static::ZX8081::Target;
 	auto target = std::make_unique<Target>();