src/platform/qt/DebuggerConsoleController.cpp (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "DebuggerConsoleController.h"
7
8#include "GameController.h"
9
10#include <QMutexLocker>
11
12#include <mgba/internal/debugger/cli-debugger.h>
13
14using namespace QGBA;
15
16DebuggerConsoleController::DebuggerConsoleController(GameController* controller, QObject* parent)
17 : DebuggerController(controller, &m_cliDebugger.d, parent)
18{
19 m_backend.d.printf = printf;
20 m_backend.d.init = init;
21 m_backend.d.deinit = deinit;
22 m_backend.d.readline = readLine;
23 m_backend.d.lineAppend = lineAppend;
24 m_backend.d.historyLast = historyLast;
25 m_backend.d.historyAppend = historyAppend;
26 m_backend.self = this;
27
28 CLIDebuggerCreate(&m_cliDebugger);
29 CLIDebuggerAttachBackend(&m_cliDebugger, &m_backend.d);
30}
31
32void DebuggerConsoleController::enterLine(const QString& line) {
33 QMutexLocker lock(&m_mutex);
34 m_lines.append(line);
35 if (m_cliDebugger.d.state == DEBUGGER_RUNNING) {
36 mDebuggerEnter(&m_cliDebugger.d, DEBUGGER_ENTER_MANUAL, nullptr);
37 }
38 m_cond.wakeOne();
39}
40
41void DebuggerConsoleController::attachInternal() {
42 mCore* core = m_gameController->thread()->core;
43 CLIDebuggerAttachBackend(&m_cliDebugger, &m_backend.d);
44 CLIDebuggerAttachSystem(&m_cliDebugger, core->cliDebuggerSystem(core));
45}
46
47void DebuggerConsoleController::printf(struct CLIDebuggerBackend* be, const char* fmt, ...) {
48 Backend* consoleBe = reinterpret_cast<Backend*>(be);
49 DebuggerConsoleController* self = consoleBe->self;
50 va_list args;
51 va_start(args, fmt);
52 self->log(QString().vsprintf(fmt, args));
53 va_end(args);
54}
55
56void DebuggerConsoleController::init(struct CLIDebuggerBackend* be) {
57 Backend* consoleBe = reinterpret_cast<Backend*>(be);
58 DebuggerConsoleController* self = consoleBe->self;
59}
60
61void DebuggerConsoleController::deinit(struct CLIDebuggerBackend* be) {
62 Backend* consoleBe = reinterpret_cast<Backend*>(be);
63 DebuggerConsoleController* self = consoleBe->self;
64}
65
66const char* DebuggerConsoleController::readLine(struct CLIDebuggerBackend* be, size_t* len) {
67 Backend* consoleBe = reinterpret_cast<Backend*>(be);
68 DebuggerConsoleController* self = consoleBe->self;
69 GameController::Interrupter interrupter(self->m_gameController, true);
70 QMutexLocker lock(&self->m_mutex);
71 while (self->m_lines.isEmpty()) {
72 self->m_cond.wait(&self->m_mutex);
73 }
74 self->m_last = self->m_lines.takeFirst().toUtf8();
75 *len = self->m_last.size();
76 return self->m_last.constData();
77
78}
79
80void DebuggerConsoleController::lineAppend(struct CLIDebuggerBackend* be, const char* line) {
81 Backend* consoleBe = reinterpret_cast<Backend*>(be);
82 DebuggerConsoleController* self = consoleBe->self;
83 self->lineAppend(QString::fromUtf8(line));
84}
85
86const char* DebuggerConsoleController::historyLast(struct CLIDebuggerBackend* be, size_t* len) {
87 Backend* consoleBe = reinterpret_cast<Backend*>(be);
88 DebuggerConsoleController* self = consoleBe->self;
89 GameController::Interrupter interrupter(self->m_gameController, true);
90 QMutexLocker lock(&self->m_mutex);
91 if (self->m_history.isEmpty()) {
92 return nullptr;
93 }
94 self->m_last = self->m_history.last().toUtf8();
95 return self->m_last.constData();
96}
97
98void DebuggerConsoleController::historyAppend(struct CLIDebuggerBackend* be, const char* line) {
99 Backend* consoleBe = reinterpret_cast<Backend*>(be);
100 DebuggerConsoleController* self = consoleBe->self;
101 GameController::Interrupter interrupter(self->m_gameController, true);
102 QMutexLocker lock(&self->m_mutex);
103 self->m_history.append(QString::fromUtf8(line));
104}