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 QString last = self->m_lines.takeFirst();
84 if (last.isNull()) {
85 return nullptr;
86 }
87 self->m_last = last.toUtf8();
88 *len = self->m_last.size();
89 return self->m_last.constData();
90
91}
92
93void DebuggerConsoleController::lineAppend(struct CLIDebuggerBackend* be, const char* line) {
94 Backend* consoleBe = reinterpret_cast<Backend*>(be);
95 DebuggerConsoleController* self = consoleBe->self;
96 self->lineAppend(QString::fromUtf8(line));
97}
98
99const char* DebuggerConsoleController::historyLast(struct CLIDebuggerBackend* be, size_t* len) {
100 Backend* consoleBe = reinterpret_cast<Backend*>(be);
101 DebuggerConsoleController* self = consoleBe->self;
102 GameController::Interrupter interrupter(self->m_gameController, true);
103 QMutexLocker lock(&self->m_mutex);
104 if (self->m_history.isEmpty()) {
105 return "i";
106 }
107 self->m_last = self->m_history.last().toUtf8();
108 return self->m_last.constData();
109}
110
111void DebuggerConsoleController::historyAppend(struct CLIDebuggerBackend* be, const char* line) {
112 Backend* consoleBe = reinterpret_cast<Backend*>(be);
113 DebuggerConsoleController* self = consoleBe->self;
114 GameController::Interrupter interrupter(self->m_gameController, true);
115 QMutexLocker lock(&self->m_mutex);
116 self->m_history.append(QString::fromUtf8(line));
117}