diff options
Diffstat (limited to 'src/citra_qt')
-rw-r--r-- | src/citra_qt/debugger/graphics_cmdlists.cpp | 44 | ||||
-rw-r--r-- | src/citra_qt/debugger/graphics_vertex_shader.cpp | 256 | ||||
-rw-r--r-- | src/citra_qt/debugger/graphics_vertex_shader.h | 51 | ||||
-rw-r--r-- | src/citra_qt/main.cpp | 97 | ||||
-rw-r--r-- | src/citra_qt/main.h | 25 | ||||
-rw-r--r-- | src/citra_qt/main.ui | 7 |
6 files changed, 408 insertions, 72 deletions
diff --git a/src/citra_qt/debugger/graphics_cmdlists.cpp b/src/citra_qt/debugger/graphics_cmdlists.cpp index 7ac3ea542..35a3140b2 100644 --- a/src/citra_qt/debugger/graphics_cmdlists.cpp +++ b/src/citra_qt/debugger/graphics_cmdlists.cpp @@ -73,7 +73,7 @@ TextureInfoDockWidget::TextureInfoDockWidget(const Pica::DebugUtils::TextureInfo format_choice->addItem(tr("RGB565")); format_choice->addItem(tr("RGBA4")); format_choice->addItem(tr("IA8")); - format_choice->addItem(tr("UNK6")); + format_choice->addItem(tr("RG8")); format_choice->addItem(tr("I8")); format_choice->addItem(tr("A8")); format_choice->addItem(tr("IA4")); @@ -175,29 +175,29 @@ int GPUCommandListModel::rowCount(const QModelIndex& parent) const { } int GPUCommandListModel::columnCount(const QModelIndex& parent) const { - return 3; + return 4; } QVariant GPUCommandListModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) return QVariant(); - const auto& writes = pica_trace.writes; - const Pica::CommandProcessor::CommandHeader cmd{writes[index.row()].Id()}; - const u32 val{writes[index.row()].Value()}; + const auto& write = pica_trace.writes[index.row()]; if (role == Qt::DisplayRole) { QString content; switch ( index.column() ) { case 0: - return QString::fromLatin1(Pica::Regs::GetCommandName(cmd.cmd_id).c_str()); + return QString::fromLatin1(Pica::Regs::GetCommandName(write.cmd_id).c_str()); case 1: - return QString("%1").arg(cmd.cmd_id, 3, 16, QLatin1Char('0')); + return QString("%1").arg(write.cmd_id, 3, 16, QLatin1Char('0')); case 2: - return QString("%1").arg(val, 8, 16, QLatin1Char('0')); + return QString("%1").arg(write.mask, 4, 2, QLatin1Char('0')); + case 3: + return QString("%1").arg(write.value, 8, 16, QLatin1Char('0')); } } else if (role == CommandIdRole) { - return QVariant::fromValue<int>(cmd.cmd_id.Value()); + return QVariant::fromValue<int>(write.cmd_id); } return QVariant(); @@ -213,6 +213,8 @@ QVariant GPUCommandListModel::headerData(int section, Qt::Orientation orientatio case 1: return tr("Register"); case 2: + return tr("Mask"); + case 3: return tr("New Value"); } @@ -260,7 +262,7 @@ void GPUCommandListWidget::OnCommandDoubleClicked(const QModelIndex& index) { } void GPUCommandListWidget::SetCommandInfo(const QModelIndex& index) { - QWidget* new_info_widget; + QWidget* new_info_widget = nullptr; const unsigned int command_id = list_widget->model()->data(index, GPUCommandListModel::CommandIdRole).toUInt(); if (COMMAND_IN_RANGE(command_id, texture0) || @@ -281,14 +283,15 @@ void GPUCommandListWidget::SetCommandInfo(const QModelIndex& index) { auto info = Pica::DebugUtils::TextureInfo::FromPicaRegister(config, format); u8* src = Memory::GetPhysicalPointer(config.GetPhysicalAddress()); new_info_widget = new TextureInfoWidget(src, info); - } else { - new_info_widget = new QWidget; } - - widget()->layout()->removeWidget(command_info_widget); - delete command_info_widget; - widget()->layout()->addWidget(new_info_widget); - command_info_widget = new_info_widget; + if (command_info_widget) { + delete command_info_widget; + command_info_widget = nullptr; + } + if (new_info_widget) { + widget()->layout()->addWidget(new_info_widget); + command_info_widget = new_info_widget; + } } #undef COMMAND_IN_RANGE @@ -300,7 +303,9 @@ GPUCommandListWidget::GPUCommandListWidget(QWidget* parent) : QDockWidget(tr("Pi list_widget = new QTreeView; list_widget->setModel(model); - list_widget->setFont(QFont("monospace")); + QFont font("monospace"); + font.setStyleHint(QFont::Monospace); // Automatic fallback to a monospace font on on platforms without a font called "monospace" + list_widget->setFont(font); list_widget->setRootIsDecorated(false); list_widget->setUniformRowHeights(true); @@ -324,7 +329,7 @@ GPUCommandListWidget::GPUCommandListWidget(QWidget* parent) : QDockWidget(tr("Pi connect(copy_all, SIGNAL(clicked()), this, SLOT(CopyAllToClipboard())); - command_info_widget = new QWidget; + command_info_widget = nullptr; QVBoxLayout* main_layout = new QVBoxLayout; main_layout->addWidget(list_widget); @@ -334,7 +339,6 @@ GPUCommandListWidget::GPUCommandListWidget(QWidget* parent) : QDockWidget(tr("Pi sub_layout->addWidget(copy_all); main_layout->addLayout(sub_layout); } - main_layout->addWidget(command_info_widget); main_widget->setLayout(main_layout); setWidget(main_widget); diff --git a/src/citra_qt/debugger/graphics_vertex_shader.cpp b/src/citra_qt/debugger/graphics_vertex_shader.cpp index 302e22d7a..0c17edee0 100644 --- a/src/citra_qt/debugger/graphics_vertex_shader.cpp +++ b/src/citra_qt/debugger/graphics_vertex_shader.cpp @@ -6,9 +6,16 @@ #include <sstream> #include <QBoxLayout> +#include <QFileDialog> +#include <QGroupBox> +#include <QLabel> +#include <QLineEdit> +#include <QPushButton> +#include <QSignalMapper> +#include <QSpinBox> #include <QTreeView> -#include "video_core/shader/shader_interpreter.h" +#include "video_core/shader/shader.h" #include "graphics_vertex_shader.h" @@ -17,7 +24,7 @@ using nihstro::Instruction; using nihstro::SourceRegister; using nihstro::SwizzlePattern; -GraphicsVertexShaderModel::GraphicsVertexShaderModel(QObject* parent): QAbstractItemModel(parent) { +GraphicsVertexShaderModel::GraphicsVertexShaderModel(GraphicsVertexShaderWidget* parent): QAbstractItemModel(parent), par(parent) { } @@ -34,7 +41,7 @@ int GraphicsVertexShaderModel::columnCount(const QModelIndex& parent) const { } int GraphicsVertexShaderModel::rowCount(const QModelIndex& parent) const { - return static_cast<int>(info.code.size()); + return static_cast<int>(par->info.code.size()); } QVariant GraphicsVertexShaderModel::headerData(int section, Qt::Orientation orientation, int role) const { @@ -62,21 +69,21 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con { switch (index.column()) { case 0: - if (info.HasLabel(index.row())) - return QString::fromStdString(info.GetLabel(index.row())); + if (par->info.HasLabel(index.row())) + return QString::fromStdString(par->info.GetLabel(index.row())); return QString("%1").arg(4*index.row(), 4, 16, QLatin1Char('0')); case 1: - return QString("%1").arg(info.code[index.row()].hex, 8, 16, QLatin1Char('0')); + return QString("%1").arg(par->info.code[index.row()].hex, 8, 16, QLatin1Char('0')); case 2: { std::stringstream output; output.flags(std::ios::hex); - Instruction instr = info.code[index.row()]; - const SwizzlePattern& swizzle = info.swizzle_info[instr.common.operand_desc_id].pattern; + Instruction instr = par->info.code[index.row()]; + const SwizzlePattern& swizzle = par->info.swizzle_info[instr.common.operand_desc_id].pattern; // longest known instruction name: "setemit " output << std::setw(8) << std::left << instr.opcode.Value().GetInfo().name; @@ -130,13 +137,13 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con print_input_indexed_compact(output, src1, swizzle.negate_src1, swizzle.SelectorToString(false).substr(0,1), instr.common.AddressRegisterName()); output << " " << instr.common.compare_op.ToString(instr.common.compare_op.x) << " "; - print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(false).substr(0,1)); + print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(true).substr(0,1)); output << ", "; print_input_indexed_compact(output, src1, swizzle.negate_src1, swizzle.SelectorToString(false).substr(1,1), instr.common.AddressRegisterName()); output << " " << instr.common.compare_op.ToString(instr.common.compare_op.y) << " "; - print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(false).substr(1,1)); + print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(true).substr(1,1)); break; } @@ -167,7 +174,7 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con // TODO: In some cases, the Address Register is used as an index for SRC2 instead of SRC1 if (instr.opcode.Value().GetInfo().subtype & OpCode::Info::Src2) { SourceRegister src2 = instr.common.GetSrc2(src_is_inverted); - print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(false)); + print_input(output, src2, swizzle.negate_src2, swizzle.SelectorToString(true)); } break; } @@ -240,6 +247,18 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con case Qt::FontRole: return QFont("monospace"); + case Qt::BackgroundRole: + // Highlight instructions which have no debug data associated to them + for (const auto& record : par->debug_data.records) + if (index.row() == record.instruction_offset) + return QVariant(); + + return QBrush(QColor(255, 255, 127)); + + + // TODO: Draw arrows for each "reachable" instruction to visualize control flow + + default: break; } @@ -247,53 +266,232 @@ QVariant GraphicsVertexShaderModel::data(const QModelIndex& index, int role) con return QVariant(); } -void GraphicsVertexShaderModel::OnUpdate() -{ - beginResetModel(); - - info.Clear(); - - for (auto instr : Pica::g_state.vs.program_code) - info.code.push_back({instr}); +void GraphicsVertexShaderWidget::DumpShader() { + QString filename = QFileDialog::getSaveFileName(this, tr("Save Shader Dump"), "shader_dump.shbin", + tr("Shader Binary (*.shbin)")); - for (auto pattern : Pica::g_state.vs.swizzle_data) - info.swizzle_info.push_back({pattern}); + if (filename.isEmpty()) { + // If the user canceled the dialog, don't dump anything. + return; + } - info.labels.insert({ Pica::g_state.regs.vs.main_offset, "main" }); + auto& setup = Pica::g_state.vs; + auto& config = Pica::g_state.regs.vs; - endResetModel(); + Pica::DebugUtils::DumpShader(filename.toStdString(), config, setup, Pica::g_state.regs.vs_output_attributes); } - GraphicsVertexShaderWidget::GraphicsVertexShaderWidget(std::shared_ptr< Pica::DebugContext > debug_context, QWidget* parent) : BreakPointObserverDock(debug_context, "Pica Vertex Shader", parent) { setObjectName("PicaVertexShader"); - auto binary_model = new GraphicsVertexShaderModel(this); - auto binary_list = new QTreeView; - binary_list->setModel(binary_model); + auto input_data_mapper = new QSignalMapper(this); + + // TODO: Support inputting data in hexadecimal raw format + for (unsigned i = 0; i < ARRAY_SIZE(input_data); ++i) { + input_data[i] = new QLineEdit; + input_data[i]->setValidator(new QDoubleValidator(input_data[i])); + } + + breakpoint_warning = new QLabel(tr("(data only available at VertexLoaded breakpoints)")); + + // TODO: Add some button for jumping to the shader entry point + + model = new GraphicsVertexShaderModel(this); + binary_list = new QTreeView; + binary_list->setModel(model); binary_list->setRootIsDecorated(false); binary_list->setAlternatingRowColors(true); - connect(this, SIGNAL(Update()), binary_model, SLOT(OnUpdate())); + auto dump_shader = new QPushButton(QIcon::fromTheme("document-save"), tr("Dump")); + + instruction_description = new QLabel; + + cycle_index = new QSpinBox; + + connect(this, SIGNAL(SelectCommand(const QModelIndex&, QItemSelectionModel::SelectionFlags)), + binary_list->selectionModel(), SLOT(select(const QModelIndex&, QItemSelectionModel::SelectionFlags))); + + connect(dump_shader, SIGNAL(clicked()), this, SLOT(DumpShader())); + + connect(cycle_index, SIGNAL(valueChanged(int)), this, SLOT(OnCycleIndexChanged(int))); + + for (unsigned i = 0; i < ARRAY_SIZE(input_data); ++i) { + connect(input_data[i], SIGNAL(textEdited(const QString&)), input_data_mapper, SLOT(map())); + input_data_mapper->setMapping(input_data[i], i); + } + connect(input_data_mapper, SIGNAL(mapped(int)), this, SLOT(OnInputAttributeChanged(int))); auto main_widget = new QWidget; auto main_layout = new QVBoxLayout; { + auto input_data_group = new QGroupBox(tr("Input Data")); + + // For each vertex attribute, add a QHBoxLayout consisting of: + // - A QLabel denoting the source attribute index + // - Four QLineEdits for showing and manipulating attribute data + // - A QLabel denoting the shader input attribute index + auto sub_layout = new QVBoxLayout; + for (unsigned i = 0; i < 16; ++i) { + // Create an HBoxLayout to store the widgets used to specify a particular attribute + // and store it in a QWidget to allow for easy hiding and unhiding. + auto row_layout = new QHBoxLayout; + row_layout->addWidget(new QLabel(tr("Attribute %1").arg(i, 2))); + for (unsigned comp = 0; comp < 4; ++comp) + row_layout->addWidget(input_data[4 * i + comp]); + + row_layout->addWidget(input_data_mapping[i] = new QLabel); + + input_data_container[i] = new QWidget; + input_data_container[i]->setLayout(row_layout); + input_data_container[i]->hide(); + + sub_layout->addWidget(input_data_container[i]); + } + + sub_layout->addWidget(breakpoint_warning); + breakpoint_warning->hide(); + + input_data_group->setLayout(sub_layout); + main_layout->addWidget(input_data_group); + } + { auto sub_layout = new QHBoxLayout; sub_layout->addWidget(binary_list); main_layout->addLayout(sub_layout); } + main_layout->addWidget(dump_shader); + { + auto sub_layout = new QHBoxLayout; + sub_layout->addWidget(new QLabel(tr("Cycle Index:"))); + sub_layout->addWidget(cycle_index); + main_layout->addLayout(sub_layout); + } + main_layout->addWidget(instruction_description); + main_layout->addStretch(); main_widget->setLayout(main_layout); setWidget(main_widget); + + widget()->setEnabled(false); } void GraphicsVertexShaderWidget::OnBreakPointHit(Pica::DebugContext::Event event, void* data) { - emit Update(); + auto input = static_cast<Pica::Shader::InputVertex*>(data); + if (event == Pica::DebugContext::Event::VertexLoaded) { + Reload(true, data); + } else { + // No vertex data is retrievable => invalidate currently stored vertex data + Reload(true, nullptr); + } widget()->setEnabled(true); } +void GraphicsVertexShaderWidget::Reload(bool replace_vertex_data, void* vertex_data) { + model->beginResetModel(); + + if (replace_vertex_data) { + if (vertex_data) { + memcpy(&input_vertex, vertex_data, sizeof(input_vertex)); + for (unsigned attr = 0; attr < 16; ++attr) { + for (unsigned comp = 0; comp < 4; ++comp) { + input_data[4 * attr + comp]->setText(QString("%1").arg(input_vertex.attr[attr][comp].ToFloat32())); + } + } + breakpoint_warning->hide(); + } else { + for (unsigned attr = 0; attr < 16; ++attr) { + for (unsigned comp = 0; comp < 4; ++comp) { + input_data[4 * attr + comp]->setText(QString("???")); + } + } + breakpoint_warning->show(); + } + } + + // Reload shader code + info.Clear(); + + auto& shader_setup = Pica::g_state.vs; + auto& shader_config = Pica::g_state.regs.vs; + for (auto instr : shader_setup.program_code) + info.code.push_back({instr}); + + for (auto pattern : shader_setup.swizzle_data) + info.swizzle_info.push_back({pattern}); + + u32 entry_point = Pica::g_state.regs.vs.main_offset; + info.labels.insert({ entry_point, "main" }); + + // Generate debug information + debug_data = Pica::Shader::ProduceDebugInfo(input_vertex, 1, shader_config, shader_setup); + + // Reload widget state + + // Only show input attributes which are used as input to the shader + for (unsigned int attr = 0; attr < 16; ++attr) { + input_data_container[attr]->setVisible(false); + } + for (unsigned int attr = 0; attr < Pica::g_state.regs.vertex_attributes.GetNumTotalAttributes(); ++attr) { + unsigned source_attr = shader_config.input_register_map.GetRegisterForAttribute(attr); + input_data_mapping[source_attr]->setText(QString("-> v%1").arg(attr)); + input_data_container[source_attr]->setVisible(true); + } + + // Initialize debug info text for current cycle count + cycle_index->setMaximum(debug_data.records.size() - 1); + OnCycleIndexChanged(cycle_index->value()); + + model->endResetModel(); +} + void GraphicsVertexShaderWidget::OnResumed() { widget()->setEnabled(false); } + +void GraphicsVertexShaderWidget::OnInputAttributeChanged(int index) { + float value = input_data[index]->text().toFloat(); + Reload(); +} + +void GraphicsVertexShaderWidget::OnCycleIndexChanged(int index) { + QString text; + + auto& record = debug_data.records[index]; + if (record.mask & Pica::Shader::DebugDataRecord::SRC1) + text += tr("SRC1: %1, %2, %3, %4\n").arg(record.src1.x.ToFloat32()).arg(record.src1.y.ToFloat32()).arg(record.src1.z.ToFloat32()).arg(record.src1.w.ToFloat32()); + if (record.mask & Pica::Shader::DebugDataRecord::SRC2) + text += tr("SRC2: %1, %2, %3, %4\n").arg(record.src2.x.ToFloat32()).arg(record.src2.y.ToFloat32()).arg(record.src2.z.ToFloat32()).arg(record.src2.w.ToFloat32()); + if (record.mask & Pica::Shader::DebugDataRecord::SRC3) + text += tr("SRC3: %1, %2, %3, %4\n").arg(record.src3.x.ToFloat32()).arg(record.src3.y.ToFloat32()).arg(record.src3.z.ToFloat32()).arg(record.src3.w.ToFloat32()); + if (record.mask & Pica::Shader::DebugDataRecord::DEST_IN) + text += tr("DEST_IN: %1, %2, %3, %4\n").arg(record.dest_in.x.ToFloat32()).arg(record.dest_in.y.ToFloat32()).arg(record.dest_in.z.ToFloat32()).arg(record.dest_in.w.ToFloat32()); + if (record.mask & Pica::Shader::DebugDataRecord::DEST_OUT) + text += tr("DEST_OUT: %1, %2, %3, %4\n").arg(record.dest_out.x.ToFloat32()).arg(record.dest_out.y.ToFloat32()).arg(record.dest_out.z.ToFloat32()).arg(record.dest_out.w.ToFloat32()); + + if (record.mask & Pica::Shader::DebugDataRecord::ADDR_REG_OUT) + text += tr("Addres Registers: %1, %2\n").arg(record.address_registers[0]).arg(record.address_registers[1]); + if (record.mask & Pica::Shader::DebugDataRecord::CMP_RESULT) + text += tr("Compare Result: %1, %2\n").arg(record.conditional_code[0] ? "true" : "false").arg(record.conditional_code[1] ? "true" : "false"); + + if (record.mask & Pica::Shader::DebugDataRecord::COND_BOOL_IN) + text += tr("Static Condition: %1\n").arg(record.cond_bool ? "true" : "false"); + if (record.mask & Pica::Shader::DebugDataRecord::COND_CMP_IN) + text += tr("Dynamic Conditions: %1, %2\n").arg(record.cond_cmp[0] ? "true" : "false").arg(record.cond_cmp[1] ? "true" : "false"); + if (record.mask & Pica::Shader::DebugDataRecord::LOOP_INT_IN) + text += tr("Loop Parameters: %1 (repeats), %2 (initializer), %3 (increment), %4\n").arg(record.loop_int.x).arg(record.loop_int.y).arg(record.loop_int.z).arg(record.loop_int.w); + + text += tr("Instruction offset: 0x%1").arg(4 * record.instruction_offset, 4, 16, QLatin1Char('0')); + if (record.mask & Pica::Shader::DebugDataRecord::NEXT_INSTR) { + text += tr(" -> 0x%2").arg(4 * record.next_instruction, 4, 16, QLatin1Char('0')); + } else { + text += tr(" (last instruction)"); + } + + instruction_description->setText(text); + + // Scroll to current instruction + const QModelIndex& instr_index = model->index(record.instruction_offset, 0); + emit SelectCommand(instr_index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + binary_list->scrollTo(instr_index, QAbstractItemView::EnsureVisible); +} diff --git a/src/citra_qt/debugger/graphics_vertex_shader.h b/src/citra_qt/debugger/graphics_vertex_shader.h index 38339dc05..1b3f1f7ec 100644 --- a/src/citra_qt/debugger/graphics_vertex_shader.h +++ b/src/citra_qt/debugger/graphics_vertex_shader.h @@ -10,11 +10,18 @@ #include "nihstro/parser_shbin.h" +#include "video_core/shader/shader.h" + +class QLabel; +class QSpinBox; + +class GraphicsVertexShaderWidget; + class GraphicsVertexShaderModel : public QAbstractItemModel { Q_OBJECT public: - GraphicsVertexShaderModel(QObject* parent); + GraphicsVertexShaderModel(GraphicsVertexShaderWidget* parent); QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex& child) const override; @@ -23,11 +30,10 @@ public: QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; -public slots: - void OnUpdate(); - private: - nihstro::ShaderInfo info; + GraphicsVertexShaderWidget* par; + + friend class GraphicsVertexShaderWidget; }; class GraphicsVertexShaderWidget : public BreakPointObserverDock { @@ -43,9 +49,42 @@ private slots: void OnBreakPointHit(Pica::DebugContext::Event event, void* data) override; void OnResumed() override; + void OnInputAttributeChanged(int index); + + void OnCycleIndexChanged(int index); + + void DumpShader(); + + /** + * Reload widget based on the current PICA200 state + * @param replace_vertex_data If true, invalidate all current vertex data + * @param vertex_data New vertex data to use, as passed to OnBreakPointHit. May be nullptr to specify that no valid vertex data can be retrieved currently. Only used if replace_vertex_data is true. + */ + void Reload(bool replace_vertex_data = false, void* vertex_data = nullptr); + + signals: - void Update(); + // Call this to change the current command selection in the disassembly view + void SelectCommand(const QModelIndex&, QItemSelectionModel::SelectionFlags); private: + QLabel* instruction_description; + QTreeView* binary_list; + GraphicsVertexShaderModel* model; + + /// TODO: Move these into a single struct + std::array<QLineEdit*, 4*16> input_data; // A text box for each of the 4 components of up to 16 vertex attributes + std::array<QWidget*, 16> input_data_container; // QWidget containing the QLayout containing each vertex attribute + std::array<QLabel*, 16> input_data_mapping; // A QLabel denoting the shader input attribute which the vertex attribute maps to + + // Text to be shown when input vertex data is not retrievable + QLabel* breakpoint_warning; + + QSpinBox* cycle_index; + + nihstro::ShaderInfo info; + Pica::Shader::DebugData<true> debug_data; + Pica::Shader::InputVertex input_vertex; + friend class GraphicsVertexShaderModel; }; diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp index 4c3edf87a..8bf2a3e13 100644 --- a/src/citra_qt/main.cpp +++ b/src/citra_qt/main.cpp @@ -7,6 +7,7 @@ #include <QtGui> #include <QDesktopWidget> #include <QFileDialog> +#include <QMessageBox> #include "qhexedit.h" #include "main.h" @@ -140,6 +141,16 @@ GMainWindow::GMainWindow() : emu_thread(nullptr) ui.actionDisplay_widget_title_bars->setChecked(settings.value("displayTitleBars", true).toBool()); OnDisplayTitleBars(ui.actionDisplay_widget_title_bars->isChecked()); + // Prepare actions for recent files + for (int i = 0; i < max_recent_files_item; ++i) { + actions_recent_files[i] = new QAction(this); + actions_recent_files[i]->setVisible(false); + connect(actions_recent_files[i], SIGNAL(triggered()), this, SLOT(OnMenuRecentFile())); + + ui.menu_recent_files->addAction(actions_recent_files[i]); + } + UpdateRecentFiles(); + // Setup connections connect(ui.action_Load_File, SIGNAL(triggered()), this, SLOT(OnMenuLoadFile())); connect(ui.action_Load_Symbol_Map, SIGNAL(triggered()), this, SLOT(OnMenuLoadSymbolMap())); @@ -213,6 +224,10 @@ void GMainWindow::OnDisplayTitleBars(bool show) void GMainWindow::BootGame(const std::string& filename) { LOG_INFO(Frontend, "Citra starting...\n"); + // Shutdown previous session if the emu thread is still active... + if (emu_thread != nullptr) + ShutdownGame(); + // Initialize the core emulation System::Init(render_window); @@ -272,18 +287,51 @@ void GMainWindow::ShutdownGame() { render_window->hide(); } -void GMainWindow::OnMenuLoadFile() +void GMainWindow::StoreRecentFile(const QString& filename) { QSettings settings; + QStringList recent_files = settings.value("recentFiles").toStringList(); + recent_files.prepend(filename); + recent_files.removeDuplicates(); + settings.setValue("recentFiles", recent_files); + + UpdateRecentFiles(); +} + +void GMainWindow::UpdateRecentFiles() { + QSettings settings; + QStringList recent_files = settings.value("recentFiles").toStringList(); + + unsigned int num_recent_files = std::min(recent_files.size(), static_cast<int>(max_recent_files_item)); + + for (unsigned int i = 0; i < num_recent_files; i++) { + QString text = QString("&%1. %2").arg(i + 1).arg(QFileInfo(recent_files[i]).fileName()); + actions_recent_files[i]->setText(text); + actions_recent_files[i]->setData(recent_files[i]); + actions_recent_files[i]->setToolTip(recent_files[i]); + actions_recent_files[i]->setVisible(true); + } + + for (int j = num_recent_files; j < max_recent_files_item; ++j) { + actions_recent_files[j]->setVisible(false); + } + + // Grey out the recent files menu if the list is empty + if (num_recent_files == 0) { + ui.menu_recent_files->setEnabled(false); + } else { + ui.menu_recent_files->setEnabled(true); + } +} + +void GMainWindow::OnMenuLoadFile() { + QSettings settings; QString rom_path = settings.value("romsPath", QString()).toString(); QString filename = QFileDialog::getOpenFileName(this, tr("Load File"), rom_path, tr("3DS executable (*.3ds *.3dsx *.elf *.axf *.cci *.cxi)")); if (filename.size()) { settings.setValue("romsPath", QFileInfo(filename).path()); - - // Shutdown previous session if the emu thread is still active... - if (emu_thread != nullptr) - ShutdownGame(); + StoreRecentFile(filename); BootGame(filename.toLatin1().data()); } @@ -301,8 +349,28 @@ void GMainWindow::OnMenuLoadSymbolMap() { } } -void GMainWindow::OnStartGame() -{ +void GMainWindow::OnMenuRecentFile() { + QAction* action = qobject_cast<QAction*>(sender()); + assert(action); + + QString filename = action->data().toString(); + QFileInfo file_info(filename); + if (file_info.exists()) { + BootGame(filename.toLatin1().data()); + StoreRecentFile(filename); // Put the filename on top of the list + } else { + // Display an error message and remove the file from the list. + QMessageBox::information(this, tr("File not found"), tr("File \"%1\" not found").arg(filename)); + + QSettings settings; + QStringList recent_files = settings.value("recentFiles").toStringList(); + recent_files.removeOne(filename); + settings.setValue("recentFiles", recent_files); + UpdateRecentFiles(); + } +} + +void GMainWindow::OnStartGame() { emu_thread->SetRunning(true); ui.action_Start->setEnabled(false); @@ -312,8 +380,7 @@ void GMainWindow::OnStartGame() ui.action_Stop->setEnabled(true); } -void GMainWindow::OnPauseGame() -{ +void GMainWindow::OnPauseGame() { emu_thread->SetRunning(false); ui.action_Start->setEnabled(true); @@ -325,8 +392,7 @@ void GMainWindow::OnStopGame() { ShutdownGame(); } -void GMainWindow::OnOpenHotkeysDialog() -{ +void GMainWindow::OnOpenHotkeysDialog() { GHotkeysDialog dialog(this); dialog.exec(); } @@ -358,13 +424,11 @@ void GMainWindow::ToggleWindowMode() { } } -void GMainWindow::OnConfigure() -{ +void GMainWindow::OnConfigure() { //GControllerConfigDialog* dialog = new GControllerConfigDialog(controller_ports, this); } -void GMainWindow::closeEvent(QCloseEvent* event) -{ +void GMainWindow::closeEvent(QCloseEvent* event) { // Save window layout QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Citra team", "Citra"); settings.setValue("geometry", saveGeometry()); @@ -388,8 +452,7 @@ void GMainWindow::closeEvent(QCloseEvent* event) #undef main #endif -int main(int argc, char* argv[]) -{ +int main(int argc, char* argv[]) { Log::Filter log_filter(Log::Level::Info); Log::SetFilter(&log_filter); diff --git a/src/citra_qt/main.h b/src/citra_qt/main.h index 61114a04d..6f1292295 100644 --- a/src/citra_qt/main.h +++ b/src/citra_qt/main.h @@ -24,6 +24,8 @@ class GMainWindow : public QMainWindow { Q_OBJECT + static const int max_recent_files_item = 10; ///< Max number of recently loaded items to keep track + // TODO: Make use of this! enum { UI_IDLE, @@ -58,6 +60,26 @@ private: void BootGame(const std::string& filename); void ShutdownGame(); + /** + * Stores the filename in the recently loaded files list. + * The new filename is stored at the beginning of the recently loaded files list. + * After inserting the new entry, duplicates are removed meaning that if + * this was inserted from \a OnMenuRecentFile(), the entry will be put on top + * and remove from its previous position. + * + * Finally, this function calls \a UpdateRecentFiles() to update the UI. + * + * @param filename the filename to store + */ + void StoreRecentFile(const QString& filename); + + /** + * Updates the recent files menu. + * Menu entries are rebuilt from the configuration file. + * If there is no entry in the menu, the menu is greyed out. + */ + void UpdateRecentFiles(); + void closeEvent(QCloseEvent* event) override; private slots: @@ -66,6 +88,7 @@ private slots: void OnStopGame(); void OnMenuLoadFile(); void OnMenuLoadSymbolMap(); + void OnMenuRecentFile(); void OnOpenHotkeysDialog(); void OnConfigure(); void OnDisplayTitleBars(bool); @@ -86,6 +109,8 @@ private: CallstackWidget* callstackWidget; GPUCommandStreamWidget* graphicsWidget; GPUCommandListWidget* graphicsCommandsWidget; + + QAction* actions_recent_files[max_recent_files_item]; }; #endif // _CITRA_QT_MAIN_HXX_ diff --git a/src/citra_qt/main.ui b/src/citra_qt/main.ui index b2ce8167d..1ba700a3a 100644 --- a/src/citra_qt/main.ui +++ b/src/citra_qt/main.ui @@ -52,9 +52,16 @@ <property name="title"> <string>&File</string> </property> + <widget class="QMenu" name="menu_recent_files"> + <property name="title"> + <string>Recent Files</string> + </property> + </widget> <addaction name="action_Load_File"/> <addaction name="action_Load_Symbol_Map"/> <addaction name="separator"/> + <addaction name="menu_recent_files"/> + <addaction name="separator"/> <addaction name="action_Exit"/> </widget> <widget class="QMenu" name="menu_Emulation"> |