2020-06-01 03:39:08 +00:00
|
|
|
#include <QtWidgets>
|
|
|
|
#include <QObject>
|
2020-06-02 03:14:57 +00:00
|
|
|
#include <QStandardPaths>
|
2020-06-01 03:39:08 +00:00
|
|
|
|
2020-05-30 04:37:06 +00:00
|
|
|
#include "mainwindow.h"
|
2020-06-01 03:39:08 +00:00
|
|
|
#include "timer.h"
|
|
|
|
|
2020-06-03 03:35:01 +00:00
|
|
|
#include "../../Numeric/CRC.hpp"
|
2020-06-02 03:14:57 +00:00
|
|
|
|
2020-06-02 02:08:21 +00:00
|
|
|
/*
|
|
|
|
General Qt implementation notes:
|
|
|
|
|
|
|
|
* it seems like Qt doesn't offer a way to constrain the aspect ratio of a view by constraining
|
|
|
|
the size of the window (i.e. you can use a custom layout to constrain a view, but that won't
|
2020-06-09 04:01:22 +00:00
|
|
|
affect the window, so isn't useful for this project). Therefore the emulation window resizes freely.
|
2020-06-02 02:08:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-30 04:37:06 +00:00
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
2020-06-01 03:39:08 +00:00
|
|
|
: QMainWindow(parent)
|
|
|
|
, ui(new Ui::MainWindow)
|
2020-05-30 04:37:06 +00:00
|
|
|
{
|
2020-06-01 03:39:08 +00:00
|
|
|
ui->setupUi(this);
|
|
|
|
createActions();
|
2020-06-06 02:11:17 +00:00
|
|
|
qApp->installEventFilter(this);
|
2020-06-01 03:39:08 +00:00
|
|
|
|
2020-06-07 04:31:46 +00:00
|
|
|
timer = std::make_unique<Timer>(this);
|
2020-06-01 03:58:19 +00:00
|
|
|
|
2020-06-19 00:05:46 +00:00
|
|
|
setVisibleWidgetSet(WidgetSet::MachinePicker);
|
2020-06-03 03:35:01 +00:00
|
|
|
romRequestBaseText = ui->missingROMsBox->toPlainText();
|
2020-05-30 04:37:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 03:39:08 +00:00
|
|
|
void MainWindow::createActions() {
|
|
|
|
// Create a file menu.
|
|
|
|
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
|
|
|
|
2020-06-17 03:15:47 +00:00
|
|
|
// Add file option: 'New...'
|
|
|
|
QAction *newAct = new QAction(tr("&New"), this);
|
|
|
|
newAct->setShortcuts(QKeySequence::New);
|
|
|
|
newAct->setStatusTip(tr("Create a new file"));
|
|
|
|
connect(newAct, &QAction::triggered, this, &MainWindow::newFile);
|
|
|
|
fileMenu->addAction(newAct);
|
|
|
|
|
|
|
|
// Add file option: 'Open...'
|
2020-06-01 03:39:08 +00:00
|
|
|
QAction *openAct = new QAction(tr("&Open..."), this);
|
|
|
|
openAct->setShortcuts(QKeySequence::Open);
|
|
|
|
openAct->setStatusTip(tr("Open an existing file"));
|
|
|
|
connect(openAct, &QAction::triggered, this, &MainWindow::open);
|
|
|
|
fileMenu->addAction(openAct);
|
2020-06-17 03:15:47 +00:00
|
|
|
|
|
|
|
// Add Help menu, with an 'About...' option.
|
|
|
|
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
|
|
|
|
QAction *aboutAct = helpMenu->addAction(tr("&About"), this, &MainWindow::about);
|
|
|
|
aboutAct->setStatusTip(tr("Show the application's About box"));
|
2020-06-01 03:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::open() {
|
|
|
|
QString fileName = QFileDialog::getOpenFileName(this);
|
2020-06-01 03:58:19 +00:00
|
|
|
if(!fileName.isEmpty()) {
|
2020-06-03 03:35:01 +00:00
|
|
|
targets = Analyser::Static::GetTargets(fileName.toStdString());
|
2020-06-17 03:15:47 +00:00
|
|
|
if(!targets.empty()) {
|
2020-06-03 03:35:01 +00:00
|
|
|
launchMachine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 03:15:47 +00:00
|
|
|
void MainWindow::newFile() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::about() {
|
|
|
|
QMessageBox::about(this, tr("About Clock Signal"),
|
|
|
|
tr( "<p>Clock Signal is an emulator of various platforms by "
|
|
|
|
"<a href=\"mailto:thomas.harte@gmail.com\">Thomas Harte</a>.</p>"
|
|
|
|
|
|
|
|
"<p>This emulator is offered under the MIT licence; its source code "
|
|
|
|
"is available from <a href=\"https://github.com/tomharte/CLK\">GitHub</a>.</p>"
|
|
|
|
|
|
|
|
"<p>This port is experimental, especially with regard to latency; "
|
|
|
|
"please don't hesitate to provide feedback.</p>"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-06-03 03:35:01 +00:00
|
|
|
MainWindow::~MainWindow() {
|
2020-06-15 03:22:00 +00:00
|
|
|
// Stop the audio output, and its thread.
|
|
|
|
if(audioOutput) {
|
2020-06-17 02:33:50 +00:00
|
|
|
audioThread.performAsync([this] {
|
|
|
|
audioOutput->stop();
|
|
|
|
});
|
2020-06-15 03:38:44 +00:00
|
|
|
audioThread.stop();
|
2020-06-15 03:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the timer.
|
2020-06-07 04:31:46 +00:00
|
|
|
timer.reset();
|
2020-06-03 03:35:01 +00:00
|
|
|
}
|
2020-06-02 03:14:57 +00:00
|
|
|
|
2020-06-03 03:35:01 +00:00
|
|
|
// MARK: Machine launch.
|
2020-06-02 03:14:57 +00:00
|
|
|
|
2020-06-03 03:35:01 +00:00
|
|
|
namespace {
|
2020-06-02 03:14:57 +00:00
|
|
|
|
2020-06-03 03:35:01 +00:00
|
|
|
std::unique_ptr<std::vector<uint8_t>> fileContentsAndClose(FILE *file) {
|
|
|
|
auto data = std::make_unique<std::vector<uint8_t>>();
|
2020-06-02 03:14:57 +00:00
|
|
|
|
2020-06-03 03:35:01 +00:00
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
data->resize(std::ftell(file));
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
size_t read = fread(data->data(), 1, data->size(), file);
|
|
|
|
fclose(file);
|
2020-06-02 03:14:57 +00:00
|
|
|
|
2020-06-03 03:35:01 +00:00
|
|
|
if(read == data->size()) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::launchMachine() {
|
|
|
|
const QStringList appDataLocations = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
|
|
|
|
missingRoms.clear();
|
|
|
|
|
|
|
|
ROMMachine::ROMFetcher rom_fetcher = [&appDataLocations, this]
|
|
|
|
(const std::vector<ROMMachine::ROM> &roms) -> std::vector<std::unique_ptr<std::vector<uint8_t>>> {
|
|
|
|
std::vector<std::unique_ptr<std::vector<uint8_t>>> results;
|
|
|
|
|
|
|
|
for(const auto &rom: roms) {
|
|
|
|
FILE *file = nullptr;
|
|
|
|
for(const auto &path: appDataLocations) {
|
|
|
|
const std::string source = path.toStdString() + "/ROMImages/" + rom.machine_name + "/" + rom.file_name;
|
|
|
|
const std::string nativeSource = QDir::toNativeSeparators(QString::fromStdString(source)).toStdString();
|
|
|
|
|
|
|
|
file = fopen(nativeSource.c_str(), "rb");
|
|
|
|
if(file) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(file) {
|
|
|
|
auto data = fileContentsAndClose(file);
|
|
|
|
if(data) {
|
|
|
|
results.push_back(std::move(data));
|
|
|
|
continue;
|
2020-06-02 03:14:57 +00:00
|
|
|
}
|
2020-06-03 03:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
results.push_back(nullptr);
|
|
|
|
missingRoms.push_back(rom);
|
2020-06-02 03:14:57 +00:00
|
|
|
}
|
2020-06-03 03:35:01 +00:00
|
|
|
return results;
|
|
|
|
};
|
|
|
|
Machine::Error error;
|
2020-06-04 03:39:16 +00:00
|
|
|
machine.reset(Machine::MachineForTargets(targets, rom_fetcher, error));
|
2020-06-03 03:35:01 +00:00
|
|
|
|
|
|
|
switch(error) {
|
2020-06-03 04:21:37 +00:00
|
|
|
default: {
|
2020-06-06 23:19:01 +00:00
|
|
|
// TODO: correct assumptions herein that this is the first machine to be
|
|
|
|
// assigned to this window.
|
2020-06-19 00:05:46 +00:00
|
|
|
setVisibleWidgetSet(WidgetSet::RunningMachine);
|
2020-06-03 03:35:01 +00:00
|
|
|
uiPhase = UIPhase::RunningMachine;
|
|
|
|
|
2020-06-04 03:39:16 +00:00
|
|
|
// Supply the scan target.
|
|
|
|
// TODO: in the future, hypothetically, deal with non-scan producers.
|
|
|
|
const auto scan_producer = machine->scan_producer();
|
|
|
|
if(scan_producer) {
|
|
|
|
scan_producer->set_scan_target(ui->openGLWidget->getScanTarget());
|
|
|
|
}
|
2020-06-03 04:21:37 +00:00
|
|
|
|
2020-06-06 23:19:01 +00:00
|
|
|
// Install audio output if required.
|
|
|
|
const auto audio_producer = machine->audio_producer();
|
|
|
|
if(audio_producer) {
|
2020-06-15 03:22:00 +00:00
|
|
|
static constexpr size_t samplesPerBuffer = 256; // TODO: select this dynamically.
|
2020-06-06 23:19:01 +00:00
|
|
|
const auto speaker = audio_producer->get_speaker();
|
|
|
|
if(speaker) {
|
|
|
|
const QAudioDeviceInfo &defaultDeviceInfo = QAudioDeviceInfo::defaultOutputDevice();
|
|
|
|
QAudioFormat idealFormat = defaultDeviceInfo.preferredFormat();
|
|
|
|
|
|
|
|
// Use the ideal format's sample rate, provide stereo as long as at least two channels
|
2020-06-07 03:47:57 +00:00
|
|
|
// are available, and — at least for now — assume a good buffer size.
|
2020-06-06 23:19:01 +00:00
|
|
|
audioIsStereo = (idealFormat.channelCount() > 1) && speaker->get_is_stereo();
|
|
|
|
audioIs8bit = idealFormat.sampleSize() < 16;
|
|
|
|
idealFormat.setChannelCount(1 + int(audioIsStereo));
|
|
|
|
idealFormat.setSampleSize(audioIs8bit ? 8 : 16);
|
2020-06-06 23:47:35 +00:00
|
|
|
|
2020-06-11 02:14:54 +00:00
|
|
|
speaker->set_output_rate(idealFormat.sampleRate(), samplesPerBuffer, audioIsStereo);
|
2020-06-06 23:19:01 +00:00
|
|
|
speaker->set_delegate(this);
|
2020-06-11 02:14:54 +00:00
|
|
|
|
2020-06-17 02:33:50 +00:00
|
|
|
audioThread.performAsync([this, idealFormat] {
|
2020-06-11 02:14:54 +00:00
|
|
|
// Create an audio output.
|
|
|
|
audioOutput = std::make_unique<QAudioOutput>(idealFormat);
|
|
|
|
|
|
|
|
// Start the output. The additional `audioBuffer` is meant to minimise latency,
|
|
|
|
// believe it or not, given Qt's semantics.
|
|
|
|
audioOutput->setBufferSize(samplesPerBuffer * sizeof(int16_t));
|
|
|
|
audioOutput->start(&audioBuffer);
|
|
|
|
audioBuffer.setDepth(audioOutput->bufferSize());
|
|
|
|
});
|
2020-06-06 23:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 04:21:37 +00:00
|
|
|
// If this is a timed machine, start up the timer.
|
|
|
|
const auto timedMachine = machine->timed_machine();
|
|
|
|
if(timedMachine) {
|
2020-06-15 03:22:00 +00:00
|
|
|
timer->startWithMachine(timedMachine, &machineMutex);
|
2020-06-03 04:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2020-06-03 03:35:01 +00:00
|
|
|
|
|
|
|
case Machine::Error::MissingROM: {
|
2020-06-19 00:05:46 +00:00
|
|
|
setVisibleWidgetSet(WidgetSet::ROMRequester);
|
2020-06-03 03:35:01 +00:00
|
|
|
uiPhase = UIPhase::RequestingROMs;
|
|
|
|
|
|
|
|
// Populate request text.
|
|
|
|
QString requestText = romRequestBaseText;
|
|
|
|
size_t index = 0;
|
|
|
|
for(const auto rom: missingRoms) {
|
|
|
|
requestText += "• ";
|
|
|
|
requestText += rom.descriptive_name.c_str();
|
|
|
|
|
|
|
|
++index;
|
|
|
|
if(index == missingRoms.size()) {
|
|
|
|
requestText += ".\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(index == missingRoms.size() - 1) {
|
|
|
|
requestText += "; and\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
requestText += ";\n";
|
|
|
|
}
|
|
|
|
ui->missingROMsBox->setPlainText(requestText);
|
|
|
|
} break;
|
2020-06-01 03:58:19 +00:00
|
|
|
}
|
2020-06-01 03:39:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 23:19:01 +00:00
|
|
|
void MainWindow::speaker_did_complete_samples(Outputs::Speaker::Speaker *, const std::vector<int16_t> &buffer) {
|
2020-06-09 04:01:22 +00:00
|
|
|
audioBuffer.write(buffer);
|
2020-06-06 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 03:35:01 +00:00
|
|
|
void MainWindow::dragEnterEvent(QDragEnterEvent* event) {
|
|
|
|
// Always accept dragged files.
|
|
|
|
if(event->mimeData()->hasUrls())
|
|
|
|
event->accept();
|
2020-05-30 04:37:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 03:35:01 +00:00
|
|
|
void MainWindow::dropEvent(QDropEvent* event) {
|
|
|
|
if(!event->mimeData()->hasUrls()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event->accept();
|
|
|
|
|
|
|
|
switch(uiPhase) {
|
|
|
|
case UIPhase::NoFileSelected:
|
|
|
|
// Treat exactly as a File -> Open... .
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UIPhase::RequestingROMs: {
|
|
|
|
// Attempt to match up the dragged files to the requested ROM list;
|
|
|
|
// if and when they match, copy to a writeable QStandardPaths::AppDataLocation
|
|
|
|
// and try launchMachine() again.
|
|
|
|
|
|
|
|
bool foundROM = false;
|
|
|
|
const auto appDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString();
|
|
|
|
|
|
|
|
for(const auto &url: event->mimeData()->urls()) {
|
|
|
|
const char *const name = url.toLocalFile().toUtf8();
|
|
|
|
FILE *const file = fopen(name, "rb");
|
|
|
|
const auto contents = fileContentsAndClose(file);
|
|
|
|
if(!contents) continue;
|
|
|
|
|
|
|
|
CRC::CRC32 generator;
|
|
|
|
const uint32_t crc = generator.compute_crc(*contents);
|
|
|
|
|
|
|
|
for(const auto &rom: missingRoms) {
|
|
|
|
if(std::find(rom.crc32s.begin(), rom.crc32s.end(), crc) != rom.crc32s.end()) {
|
|
|
|
foundROM = true;
|
|
|
|
|
|
|
|
// Ensure the destination folder exists.
|
|
|
|
const std::string path = appDataLocation + "/ROMImages/" + rom.machine_name;
|
|
|
|
QDir dir(QString::fromStdString(path));
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkpath(".");
|
|
|
|
|
|
|
|
// Write into place.
|
|
|
|
const std::string destination = QDir::toNativeSeparators(QString::fromStdString(path+ "/" + rom.file_name)).toStdString();
|
|
|
|
FILE *const target = fopen(destination.c_str(), "wb");
|
|
|
|
fwrite(contents->data(), 1, contents->size(), target);
|
|
|
|
fclose(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(foundROM) launchMachine();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case UIPhase::RunningMachine:
|
|
|
|
// Attempt to insert into the running machine.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 02:11:17 +00:00
|
|
|
|
|
|
|
// MARK: Input capture.
|
|
|
|
|
|
|
|
bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
|
|
|
|
switch(event->type()) {
|
|
|
|
case QEvent::KeyPress:
|
|
|
|
case QEvent::KeyRelease: {
|
|
|
|
const auto keyEvent = static_cast<QKeyEvent *>(event);
|
2020-06-06 03:06:28 +00:00
|
|
|
if(!processEvent(keyEvent)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-06 02:11:17 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QObject::eventFilter(obj, event);
|
|
|
|
}
|
|
|
|
|
2020-06-19 00:05:46 +00:00
|
|
|
void MainWindow::setVisibleWidgetSet(WidgetSet set) {
|
|
|
|
// The volume slider is never visible by default; a running machine
|
|
|
|
// will show and hide it dynamically.
|
|
|
|
ui->volumeSlider->setVisible(false);
|
|
|
|
|
|
|
|
// Show or hide the missing ROMs box.
|
|
|
|
ui->missingROMsBox->setVisible(set == WidgetSet::ROMRequester);
|
|
|
|
|
|
|
|
// Show or hide the various machine-picking chrome.
|
|
|
|
ui->machineSelectionTabs->setVisible(set == WidgetSet::MachinePicker);
|
|
|
|
ui->startMachineButton->setVisible(set == WidgetSet::MachinePicker);
|
|
|
|
ui->topTipLabel->setVisible(set == WidgetSet::MachinePicker);
|
|
|
|
}
|
|
|
|
|
2020-06-06 03:06:28 +00:00
|
|
|
bool MainWindow::processEvent(QKeyEvent *event) {
|
|
|
|
if(!machine) return true;
|
|
|
|
|
|
|
|
// First version: support keyboard input only.
|
|
|
|
const auto keyboardMachine = machine->keyboard_machine();
|
|
|
|
if(!keyboardMachine) return true;
|
|
|
|
|
|
|
|
#define BIND2(qtKey, clkKey) case Qt::qtKey: key = Inputs::Keyboard::Key::clkKey; break;
|
|
|
|
#define BIND(key) BIND2(Key_##key, key)
|
|
|
|
|
|
|
|
Inputs::Keyboard::Key key;
|
|
|
|
switch(event->key()) {
|
|
|
|
default: return true;
|
|
|
|
|
|
|
|
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);
|
|
|
|
BIND2(Key_Print, PrintScreen);
|
|
|
|
BIND(ScrollLock); BIND(Pause);
|
|
|
|
|
|
|
|
BIND2(Key_AsciiTilde, BackTick);
|
|
|
|
BIND2(Key_1, k1); BIND2(Key_2, k2); BIND2(Key_3, k3); BIND2(Key_4, k4); BIND2(Key_5, k5);
|
|
|
|
BIND2(Key_6, k6); BIND2(Key_7, k7); BIND2(Key_8, k8); BIND2(Key_9, k9); BIND2(Key_0, k0);
|
|
|
|
BIND2(Key_Minus, Hyphen);
|
|
|
|
BIND2(Key_Plus, Equals);
|
|
|
|
BIND(Backspace);
|
|
|
|
|
|
|
|
BIND(Tab); BIND(Q); BIND(W); BIND(E); BIND(R); BIND(T); BIND(Y);
|
|
|
|
BIND(U); BIND(I); BIND(O); BIND(P);
|
|
|
|
BIND2(Key_BraceLeft, OpenSquareBracket);
|
|
|
|
BIND2(Key_BraceRight, CloseSquareBracket);
|
|
|
|
BIND(Backslash);
|
|
|
|
|
|
|
|
BIND(CapsLock); BIND(A); BIND(S); BIND(D); BIND(F); BIND(G);
|
|
|
|
BIND(H); BIND(J); BIND(K); BIND(L);
|
|
|
|
BIND(Semicolon);
|
|
|
|
BIND2(Key_QuoteDbl, Quote);
|
|
|
|
// TODO: something to hash?
|
|
|
|
BIND2(Key_Return, Enter);
|
|
|
|
|
|
|
|
BIND2(Key_Shift, LeftShift);
|
|
|
|
BIND(Z); BIND(X); BIND(C); BIND(V);
|
|
|
|
BIND(B); BIND(N); BIND(M);
|
|
|
|
BIND(Comma);
|
|
|
|
BIND2(Key_Period, FullStop);
|
|
|
|
BIND2(Key_Slash, ForwardSlash);
|
|
|
|
// Omitted: right shift.
|
|
|
|
|
|
|
|
BIND2(Key_Control, LeftControl);
|
|
|
|
BIND2(Key_Alt, LeftOption);
|
|
|
|
BIND2(Key_Meta, LeftMeta);
|
|
|
|
BIND(Space);
|
|
|
|
BIND2(Key_AltGr, RightOption);
|
|
|
|
|
|
|
|
BIND(Left); BIND(Right); BIND(Up); BIND(Down);
|
|
|
|
|
|
|
|
BIND(Insert); BIND(Home); BIND(PageUp); BIND(Delete); BIND(End); BIND(PageDown);
|
|
|
|
|
|
|
|
BIND(NumLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_lock lock(machineMutex);
|
|
|
|
keyboardMachine->get_keyboard().set_key_pressed(key, event->text()[0].toLatin1(), event->type() == QEvent::KeyPress);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|