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::detach() {
42 m_lines.append(QString());
43 m_cond.wakeOne();
44 DebuggerController::detach();
45}
46
47void DebuggerConsoleController::attachInternal() {
48 m_history.clear();
49 mCore* core = m_gameController->thread()->core;
50 CLIDebuggerAttachBackend(&m_cliDebugger, &m_backend.d);
51 CLIDebuggerAttachSystem(&m_cliDebugger, core->cliDebuggerSystem(core));
52}
53
54void DebuggerConsoleController::printf(struct CLIDebuggerBackend* be, const char* fmt, ...) {
55 Backend* consoleBe = reinterpret_cast<Backend*>(be);
56 DebuggerConsoleController* self = consoleBe->self;
57 va_list args;
58 va_start(args, fmt);
59 self->log(QString().vsprintf(fmt, args));
60 va_end(args);
61}
62
63void DebuggerConsoleController::init(struct CLIDebuggerBackend* be) {
64 Backend* consoleBe = reinterpret_cast<Backend*>(be);
65 DebuggerConsoleController* self = consoleBe->self;
66}
67
68void DebuggerConsoleController::deinit(struct CLIDebuggerBackend* be) {
69 Backend* consoleBe = reinterpret_cast<Backend*>(be);
70 DebuggerConsoleController* self = consoleBe->self;
71 self->m_lines.append(QString());
72 self->m_cond.wakeOne();
73}
74
75const char* DebuggerConsoleController::readLine(struct CLIDebuggerBackend* be, size_t* len) {
76 Backend* consoleBe = reinterpret_cast<Backend*>(be);
77 DebuggerConsoleController* self = consoleBe->self;
78 GameController::Interrupter interrupter(self->m_gameController, true);
79 QMutexLocker lock(&self->m_mutex);
80 while (self->m_lines.isEmpty()) {
81 self->m_cond.wait(&self->m_mutex);
82 }
83 self->m_last = self->m_lines.takeFirst().toUtf8();
84 if (self->m_last.isEmpty()) {
85 self->m_last = "\n";
86 }
87 *len = self->m_last.size();
88 return self->m_last.constData();
89
90}
91
92void DebuggerConsoleController::lineAppend(struct CLIDebuggerBackend* be, const char* line) {
93 Backend* consoleBe = reinterpret_cast<Backend*>(be);
94 DebuggerConsoleController* self = consoleBe->self;
95 self->lineAppend(QString::fromUtf8(line));
96}
97
98const char* DebuggerConsoleController::historyLast(struct CLIDebuggerBackend* be, size_t* len) {
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 if (self->m_history.isEmpty()) {
104 return "\n";
105 }
106 self->m_last = self->m_history.last().toUtf8();
107 return self->m_last.constData();
108}
109
110void DebuggerConsoleController::historyAppend(struct CLIDebuggerBackend* be, const char* line) {
111 Backend* consoleBe = reinterpret_cast<Backend*>(be);
112 DebuggerConsoleController* self = consoleBe->self;
113 GameController::Interrupter interrupter(self->m_gameController, true);
114 QMutexLocker lock(&self->m_mutex);
115 self->m_history.append(QString::fromUtf8(line));
116}