mirror of
https://github.com/NohamR/Reclass.git
synced 2026-05-10 19:59:21 +00:00
Add MCP bridge for external tool integration
Embedded JSON-RPC server over named pipes (rcx-mcp) enabling external tools like Claude Code to inspect and manipulate the node tree, read/write hex data, switch sources, and trigger UI actions. Includes stdio adapter (rcx-mcp-stdio) for stdin/stdout transport. Server is stopped by default; user starts via File > Start MCP Server. Also extracts MainWindow class declaration to mainwindow.h and improves type selector popup Esc button styling.
This commit is contained in:
@@ -7,7 +7,7 @@ set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Widgets PrintSupport Svg Concurrent)
|
||||
find_package(Qt6 REQUIRED COMPONENTS Widgets PrintSupport Svg Concurrent Network)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
|
||||
find_package(QScintilla REQUIRED)
|
||||
@@ -41,6 +41,9 @@ add_executable(ReclassX
|
||||
src/themes/thememanager.cpp
|
||||
src/themes/themeeditor.h
|
||||
src/themes/themeeditor.cpp
|
||||
src/mainwindow.h
|
||||
src/mcp/mcp_bridge.h
|
||||
src/mcp/mcp_bridge.cpp
|
||||
)
|
||||
|
||||
target_include_directories(ReclassX PRIVATE src)
|
||||
@@ -50,12 +53,16 @@ target_link_libraries(ReclassX PRIVATE
|
||||
Qt6::PrintSupport
|
||||
Qt6::Svg
|
||||
Qt6::Concurrent
|
||||
Qt6::Network
|
||||
QScintilla::QScintilla
|
||||
)
|
||||
if(WIN32)
|
||||
target_link_libraries(ReclassX PRIVATE dbghelp psapi)
|
||||
endif()
|
||||
|
||||
add_executable(rcx-mcp-stdio tools/rcx-mcp-stdio.cpp)
|
||||
target_link_libraries(rcx-mcp-stdio PRIVATE Qt6::Core Qt6::Network)
|
||||
|
||||
add_custom_target(screenshot ALL
|
||||
COMMAND ReclassX --screenshot ${CMAKE_BINARY_DIR}/screenshot.png
|
||||
DEPENDS ReclassX
|
||||
|
||||
@@ -110,6 +110,12 @@ public:
|
||||
RcxDocument* document() const { return m_doc; }
|
||||
void setEditorFont(const QString& fontName);
|
||||
|
||||
// MCP bridge accessors
|
||||
void setSuppressRefresh(bool v) { m_suppressRefresh = v; }
|
||||
const QVector<SavedSourceEntry>& savedSources() const { return m_savedSources; }
|
||||
int activeSourceIndex() const { return m_activeSourceIdx; }
|
||||
void switchSource(int idx) { switchToSavedSource(idx); }
|
||||
|
||||
signals:
|
||||
void nodeSelected(int nodeIdx);
|
||||
void selectionChanged(int count);
|
||||
|
||||
125
src/main.cpp
125
src/main.cpp
@@ -1,6 +1,6 @@
|
||||
#include "controller.h"
|
||||
#include "mainwindow.h"
|
||||
#include "generator.h"
|
||||
#include "pluginmanager.h"
|
||||
#include "mcp/mcp_bridge.h"
|
||||
#include <QApplication>
|
||||
#include <QMainWindow>
|
||||
#include <QMdiArea>
|
||||
@@ -170,98 +170,7 @@ static void applyGlobalTheme(const rcx::Theme& theme) {
|
||||
|
||||
namespace rcx {
|
||||
|
||||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MainWindow(QWidget* parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void newFile();
|
||||
void newDocument();
|
||||
void selfTest();
|
||||
void openFile();
|
||||
void saveFile();
|
||||
void saveFileAs();
|
||||
|
||||
|
||||
void addNode();
|
||||
void removeNode();
|
||||
void changeNodeType();
|
||||
void renameNodeAction();
|
||||
void duplicateNodeAction();
|
||||
void splitView();
|
||||
void unsplitView();
|
||||
|
||||
void undo();
|
||||
void redo();
|
||||
void about();
|
||||
void setEditorFont(const QString& fontName);
|
||||
void exportCpp();
|
||||
void showTypeAliasesDialog();
|
||||
void editTheme();
|
||||
|
||||
public:
|
||||
// Project Lifecycle API
|
||||
QMdiSubWindow* project_new();
|
||||
QMdiSubWindow* project_open(const QString& path = {});
|
||||
bool project_save(QMdiSubWindow* sub = nullptr, bool saveAs = false);
|
||||
void project_close(QMdiSubWindow* sub = nullptr);
|
||||
|
||||
private:
|
||||
enum ViewMode { VM_Reclass, VM_Rendered };
|
||||
|
||||
QMdiArea* m_mdiArea;
|
||||
QLabel* m_statusLabel;
|
||||
PluginManager m_pluginManager;
|
||||
|
||||
struct SplitPane {
|
||||
QTabWidget* tabWidget = nullptr;
|
||||
RcxEditor* editor = nullptr;
|
||||
QsciScintilla* rendered = nullptr;
|
||||
ViewMode viewMode = VM_Reclass;
|
||||
uint64_t lastRenderedRootId = 0;
|
||||
};
|
||||
|
||||
struct TabState {
|
||||
RcxDocument* doc;
|
||||
RcxController* ctrl;
|
||||
QSplitter* splitter;
|
||||
QVector<SplitPane> panes;
|
||||
int activePaneIdx = 0;
|
||||
};
|
||||
QMap<QMdiSubWindow*, TabState> m_tabs;
|
||||
|
||||
|
||||
void createMenus();
|
||||
void createStatusBar();
|
||||
void showPluginsDialog();
|
||||
QIcon makeIcon(const QString& svgPath);
|
||||
|
||||
RcxController* activeController() const;
|
||||
TabState* activeTab();
|
||||
QMdiSubWindow* createTab(RcxDocument* doc);
|
||||
void updateWindowTitle();
|
||||
|
||||
void setViewMode(ViewMode mode);
|
||||
void updateRenderedView(TabState& tab, SplitPane& pane);
|
||||
void updateAllRenderedPanes(TabState& tab);
|
||||
uint64_t findRootStructForNode(const NodeTree& tree, uint64_t nodeId) const;
|
||||
void setupRenderedSci(QsciScintilla* sci);
|
||||
|
||||
SplitPane createSplitPane(TabState& tab);
|
||||
void applyTheme(const Theme& theme);
|
||||
void applyTabWidgetStyle(QTabWidget* tw);
|
||||
SplitPane* findPaneByTabWidget(QTabWidget* tw);
|
||||
SplitPane* findActiveSplitPane();
|
||||
RcxEditor* activePaneEditor();
|
||||
|
||||
// Workspace dock
|
||||
QDockWidget* m_workspaceDock = nullptr;
|
||||
QTreeView* m_workspaceTree = nullptr;
|
||||
QStandardItemModel* m_workspaceModel = nullptr;
|
||||
void createWorkspaceDock();
|
||||
void rebuildWorkspaceModel();
|
||||
};
|
||||
// MainWindow class declaration is in mainwindow.h
|
||||
|
||||
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
|
||||
setWindowTitle("Reclass");
|
||||
@@ -300,6 +209,9 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
|
||||
// Load plugins
|
||||
m_pluginManager.LoadPlugins();
|
||||
|
||||
// MCP bridge (stopped by default — user starts via File → Start MCP)
|
||||
m_mcp = new McpBridge(this, this);
|
||||
|
||||
connect(m_mdiArea, &QMdiArea::subWindowActivated,
|
||||
this, [this](QMdiSubWindow*) {
|
||||
updateWindowTitle();
|
||||
@@ -336,6 +248,8 @@ void MainWindow::createMenus() {
|
||||
file->addSeparator();
|
||||
file->addAction(makeIcon(":/vsicons/export.svg"), "Export &C++ Header...", this, &MainWindow::exportCpp);
|
||||
file->addSeparator();
|
||||
m_mcpAction = file->addAction("Start &MCP Server", this, &MainWindow::toggleMcp);
|
||||
file->addSeparator();
|
||||
file->addAction(makeIcon(":/vsicons/close.svg"), "E&xit", QKeySequence(Qt::Key_Close), this, &QMainWindow::close);
|
||||
|
||||
// Edit
|
||||
@@ -839,6 +753,18 @@ void MainWindow::about() {
|
||||
dlg.exec();
|
||||
}
|
||||
|
||||
void MainWindow::toggleMcp() {
|
||||
if (m_mcp->isRunning()) {
|
||||
m_mcp->stop();
|
||||
m_mcpAction->setText("Start &MCP Server");
|
||||
m_statusLabel->setText("MCP server stopped");
|
||||
} else {
|
||||
m_mcp->start();
|
||||
m_mcpAction->setText("Stop &MCP Server");
|
||||
m_statusLabel->setText("MCP server listening on pipe: rcx-mcp");
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::applyTheme(const Theme& theme) {
|
||||
applyGlobalTheme(theme);
|
||||
|
||||
@@ -922,6 +848,15 @@ MainWindow::TabState* MainWindow::activeTab() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
MainWindow::TabState* MainWindow::tabByIndex(int index) {
|
||||
auto subs = m_mdiArea->subWindowList();
|
||||
if (index < 0 || index >= subs.size()) return nullptr;
|
||||
auto* sub = subs[index];
|
||||
if (m_tabs.contains(sub))
|
||||
return &m_tabs[sub];
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void MainWindow::updateWindowTitle() {
|
||||
auto* sub = m_mdiArea->activeSubWindow();
|
||||
if (sub && m_tabs.contains(sub)) {
|
||||
@@ -1465,4 +1400,4 @@ int main(int argc, char* argv[]) {
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#include "main.moc"
|
||||
// MainWindow Q_OBJECT is now in mainwindow.h; AUTOMOC handles moc generation.
|
||||
|
||||
119
src/mainwindow.h
Normal file
119
src/mainwindow.h
Normal file
@@ -0,0 +1,119 @@
|
||||
#pragma once
|
||||
#include "controller.h"
|
||||
#include "pluginmanager.h"
|
||||
#include <QMainWindow>
|
||||
#include <QMdiArea>
|
||||
#include <QMdiSubWindow>
|
||||
#include <QLabel>
|
||||
#include <QSplitter>
|
||||
#include <QTabWidget>
|
||||
#include <QDockWidget>
|
||||
#include <QTreeView>
|
||||
#include <QStandardItemModel>
|
||||
#include <QMap>
|
||||
#include <Qsci/qsciscintilla.h>
|
||||
|
||||
namespace rcx {
|
||||
|
||||
class McpBridge;
|
||||
|
||||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
friend class McpBridge;
|
||||
public:
|
||||
explicit MainWindow(QWidget* parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void newFile();
|
||||
void newDocument();
|
||||
void selfTest();
|
||||
void openFile();
|
||||
void saveFile();
|
||||
void saveFileAs();
|
||||
|
||||
|
||||
void addNode();
|
||||
void removeNode();
|
||||
void changeNodeType();
|
||||
void renameNodeAction();
|
||||
void duplicateNodeAction();
|
||||
void splitView();
|
||||
void unsplitView();
|
||||
|
||||
void undo();
|
||||
void redo();
|
||||
void about();
|
||||
void toggleMcp();
|
||||
void setEditorFont(const QString& fontName);
|
||||
void exportCpp();
|
||||
void showTypeAliasesDialog();
|
||||
void editTheme();
|
||||
|
||||
public:
|
||||
// Project Lifecycle API
|
||||
QMdiSubWindow* project_new();
|
||||
QMdiSubWindow* project_open(const QString& path = {});
|
||||
bool project_save(QMdiSubWindow* sub = nullptr, bool saveAs = false);
|
||||
void project_close(QMdiSubWindow* sub = nullptr);
|
||||
|
||||
private:
|
||||
enum ViewMode { VM_Reclass, VM_Rendered };
|
||||
|
||||
QMdiArea* m_mdiArea;
|
||||
QLabel* m_statusLabel;
|
||||
PluginManager m_pluginManager;
|
||||
McpBridge* m_mcp = nullptr;
|
||||
QAction* m_mcpAction = nullptr;
|
||||
|
||||
struct SplitPane {
|
||||
QTabWidget* tabWidget = nullptr;
|
||||
RcxEditor* editor = nullptr;
|
||||
QsciScintilla* rendered = nullptr;
|
||||
ViewMode viewMode = VM_Reclass;
|
||||
uint64_t lastRenderedRootId = 0;
|
||||
};
|
||||
|
||||
struct TabState {
|
||||
RcxDocument* doc;
|
||||
RcxController* ctrl;
|
||||
QSplitter* splitter;
|
||||
QVector<SplitPane> panes;
|
||||
int activePaneIdx = 0;
|
||||
};
|
||||
QMap<QMdiSubWindow*, TabState> m_tabs;
|
||||
|
||||
|
||||
void createMenus();
|
||||
void createStatusBar();
|
||||
void showPluginsDialog();
|
||||
QIcon makeIcon(const QString& svgPath);
|
||||
|
||||
RcxController* activeController() const;
|
||||
TabState* activeTab();
|
||||
TabState* tabByIndex(int index);
|
||||
int tabCount() const { return m_tabs.size(); }
|
||||
QMdiSubWindow* createTab(RcxDocument* doc);
|
||||
void updateWindowTitle();
|
||||
|
||||
void setViewMode(ViewMode mode);
|
||||
void updateRenderedView(TabState& tab, SplitPane& pane);
|
||||
void updateAllRenderedPanes(TabState& tab);
|
||||
uint64_t findRootStructForNode(const NodeTree& tree, uint64_t nodeId) const;
|
||||
void setupRenderedSci(QsciScintilla* sci);
|
||||
|
||||
SplitPane createSplitPane(TabState& tab);
|
||||
void applyTheme(const Theme& theme);
|
||||
void applyTabWidgetStyle(QTabWidget* tw);
|
||||
SplitPane* findPaneByTabWidget(QTabWidget* tw);
|
||||
SplitPane* findActiveSplitPane();
|
||||
RcxEditor* activePaneEditor();
|
||||
|
||||
// Workspace dock
|
||||
QDockWidget* m_workspaceDock = nullptr;
|
||||
QTreeView* m_workspaceTree = nullptr;
|
||||
QStandardItemModel* m_workspaceModel = nullptr;
|
||||
void createWorkspaceDock();
|
||||
void rebuildWorkspaceModel();
|
||||
};
|
||||
|
||||
} // namespace rcx
|
||||
1040
src/mcp/mcp_bridge.cpp
Normal file
1040
src/mcp/mcp_bridge.cpp
Normal file
File diff suppressed because it is too large
Load Diff
67
src/mcp/mcp_bridge.h
Normal file
67
src/mcp/mcp_bridge.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
#include "mainwindow.h"
|
||||
#include <QObject>
|
||||
#include <QLocalServer>
|
||||
#include <QLocalSocket>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QByteArray>
|
||||
|
||||
namespace rcx {
|
||||
|
||||
class McpBridge : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit McpBridge(MainWindow* mainWindow, QObject* parent = nullptr);
|
||||
~McpBridge() override;
|
||||
|
||||
void start();
|
||||
void stop();
|
||||
bool isRunning() const { return m_server != nullptr; }
|
||||
|
||||
// Call from controller refresh / data change to notify MCP clients
|
||||
void notifyTreeChanged();
|
||||
void notifyDataChanged();
|
||||
|
||||
private:
|
||||
MainWindow* m_mainWindow;
|
||||
QLocalServer* m_server = nullptr;
|
||||
QLocalSocket* m_client = nullptr; // single client for v1
|
||||
QByteArray m_readBuffer;
|
||||
bool m_initialized = false;
|
||||
|
||||
// JSON-RPC plumbing
|
||||
void onNewConnection();
|
||||
void onReadyRead();
|
||||
void onDisconnected();
|
||||
void processLine(const QByteArray& line);
|
||||
void sendJson(const QJsonObject& obj);
|
||||
QJsonObject okReply(const QJsonValue& id, const QJsonObject& result);
|
||||
QJsonObject errReply(const QJsonValue& id, int code, const QString& msg);
|
||||
void sendNotification(const QString& method, const QJsonObject& params = {});
|
||||
|
||||
// MCP method handlers
|
||||
QJsonObject handleInitialize(const QJsonValue& id, const QJsonObject& params);
|
||||
QJsonObject handleToolsList(const QJsonValue& id);
|
||||
QJsonObject handleToolsCall(const QJsonValue& id, const QJsonObject& params);
|
||||
|
||||
// Tool implementations
|
||||
QJsonObject toolProjectState(const QJsonObject& args);
|
||||
QJsonObject toolTreeApply(const QJsonObject& args);
|
||||
QJsonObject toolSourceSwitch(const QJsonObject& args);
|
||||
QJsonObject toolHexRead(const QJsonObject& args);
|
||||
QJsonObject toolHexWrite(const QJsonObject& args);
|
||||
QJsonObject toolStatusSet(const QJsonObject& args);
|
||||
QJsonObject toolUiAction(const QJsonObject& args);
|
||||
|
||||
// Helpers
|
||||
QJsonObject makeTextResult(const QString& text, bool isError = false);
|
||||
QString resolvePlaceholder(const QString& ref,
|
||||
const QHash<QString, uint64_t>& placeholderMap);
|
||||
|
||||
// Smart tab resolution: tabIndex arg → activeTab → first tab → auto-create
|
||||
MainWindow::TabState* resolveTab(const QJsonObject& args);
|
||||
};
|
||||
|
||||
} // namespace rcx
|
||||
@@ -131,10 +131,17 @@ TypeSelectorPopup::TypeSelectorPopup(QWidget* parent)
|
||||
|
||||
row->addStretch();
|
||||
|
||||
m_escLabel = new QLabel(QStringLiteral("Esc"));
|
||||
QPalette dimPal = pal;
|
||||
dimPal.setColor(QPalette::WindowText, theme.textDim);
|
||||
m_escLabel->setPalette(dimPal);
|
||||
m_escLabel = new QToolButton;
|
||||
m_escLabel->setText(QStringLiteral("\u2715 Esc"));
|
||||
m_escLabel->setAutoRaise(true);
|
||||
m_escLabel->setCursor(Qt::PointingHandCursor);
|
||||
m_escLabel->setStyleSheet(QStringLiteral(
|
||||
"QToolButton { color: %1; border: none; padding: 2px 6px; }"
|
||||
"QToolButton:hover { color: %2; }")
|
||||
.arg(theme.textDim.name(), theme.indHoverSpan.name()));
|
||||
connect(m_escLabel, &QToolButton::clicked, this, [this]() {
|
||||
hide();
|
||||
});
|
||||
row->addWidget(m_escLabel);
|
||||
|
||||
layout->addLayout(row);
|
||||
@@ -144,8 +151,7 @@ TypeSelectorPopup::TypeSelectorPopup(QWidget* parent)
|
||||
{
|
||||
m_createBtn = new QToolButton;
|
||||
m_createBtn->setText(QStringLiteral("+ Create new type\u2026"));
|
||||
m_createBtn->setIcon(QIcon(QStringLiteral(":/vsicons/add.svg")));
|
||||
m_createBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
||||
m_createBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
|
||||
m_createBtn->setAutoRaise(true);
|
||||
m_createBtn->setCursor(Qt::PointingHandCursor);
|
||||
m_createBtn->setPalette(pal);
|
||||
|
||||
@@ -39,7 +39,7 @@ protected:
|
||||
|
||||
private:
|
||||
QLabel* m_titleLabel = nullptr;
|
||||
QLabel* m_escLabel = nullptr;
|
||||
QToolButton* m_escLabel = nullptr;
|
||||
QToolButton* m_createBtn = nullptr;
|
||||
QLineEdit* m_filterEdit = nullptr;
|
||||
QListView* m_listView = nullptr;
|
||||
|
||||
117
tools/rcx-mcp-stdio.cpp
Normal file
117
tools/rcx-mcp-stdio.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
// rcx-mcp-stdio: Bridges stdin/stdout to QLocalSocket for MCP transport.
|
||||
// Claude Desktop spawns this process; it connects to the rcx-mcp named pipe
|
||||
// inside the running ReclassX application.
|
||||
//
|
||||
// stdin (from Claude) → QLocalSocket → McpBridge (in ReclassX)
|
||||
// stdout (to Claude) ← QLocalSocket ← McpBridge (in ReclassX)
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QLocalSocket>
|
||||
#include <QTimer>
|
||||
#include <QTextStream>
|
||||
#include <cstdio>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
#ifdef _WIN32
|
||||
// Ensure stdin/stdout are in binary mode on Windows
|
||||
_setmode(_fileno(stdin), _O_BINARY);
|
||||
_setmode(_fileno(stdout), _O_BINARY);
|
||||
#endif
|
||||
|
||||
auto* socket = new QLocalSocket(&app);
|
||||
QByteArray readBuf;
|
||||
|
||||
// Socket → stdout: forward lines from ReclassX to Claude Desktop
|
||||
QObject::connect(socket, &QLocalSocket::readyRead, [&]() {
|
||||
readBuf.append(socket->readAll());
|
||||
while (true) {
|
||||
int idx = readBuf.indexOf('\n');
|
||||
if (idx < 0) break;
|
||||
QByteArray line = readBuf.left(idx + 1); // include newline
|
||||
readBuf.remove(0, idx + 1);
|
||||
fwrite(line.constData(), 1, line.size(), stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
});
|
||||
|
||||
QObject::connect(socket, &QLocalSocket::disconnected, [&]() {
|
||||
fprintf(stderr, "[rcx-mcp-stdio] Disconnected from server\n");
|
||||
app.quit();
|
||||
});
|
||||
|
||||
QObject::connect(socket, &QLocalSocket::errorOccurred, [&](QLocalSocket::LocalSocketError err) {
|
||||
fprintf(stderr, "[rcx-mcp-stdio] Socket error %d: %s\n",
|
||||
(int)err, socket->errorString().toUtf8().constData());
|
||||
app.quit();
|
||||
});
|
||||
|
||||
// Connect to the named pipe
|
||||
socket->connectToServer("rcx-mcp");
|
||||
if (!socket->waitForConnected(5000)) {
|
||||
fprintf(stderr, "[rcx-mcp-stdio] Failed to connect to rcx-mcp pipe: %s\n",
|
||||
socket->errorString().toUtf8().constData());
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "[rcx-mcp-stdio] Connected to rcx-mcp\n");
|
||||
|
||||
// Stdin → socket: poll stdin with a timer (stdin isn't a socket on Windows)
|
||||
QByteArray stdinBuf;
|
||||
auto* stdinTimer = new QTimer(&app);
|
||||
stdinTimer->setInterval(10);
|
||||
|
||||
QObject::connect(stdinTimer, &QTimer::timeout, [&]() {
|
||||
#ifdef _WIN32
|
||||
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
||||
DWORD avail = 0;
|
||||
if (!PeekNamedPipe(hStdin, nullptr, 0, nullptr, &avail, nullptr)) {
|
||||
// stdin closed (pipe broken)
|
||||
app.quit();
|
||||
return;
|
||||
}
|
||||
if (avail == 0) return;
|
||||
|
||||
char buf[4096];
|
||||
DWORD bytesRead = 0;
|
||||
DWORD toRead = qMin(avail, (DWORD)sizeof(buf));
|
||||
if (!ReadFile(hStdin, buf, toRead, &bytesRead, nullptr) || bytesRead == 0) {
|
||||
app.quit();
|
||||
return;
|
||||
}
|
||||
stdinBuf.append(buf, (int)bytesRead);
|
||||
#else
|
||||
// On Unix, we could use QSocketNotifier, but timer works fine too
|
||||
char buf[4096];
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(STDIN_FILENO, &fds);
|
||||
struct timeval tv = {0, 0};
|
||||
if (select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv) <= 0) return;
|
||||
ssize_t n = ::read(STDIN_FILENO, buf, sizeof(buf));
|
||||
if (n <= 0) {
|
||||
app.quit();
|
||||
return;
|
||||
}
|
||||
stdinBuf.append(buf, (int)n);
|
||||
#endif
|
||||
// Forward complete lines to socket
|
||||
while (true) {
|
||||
int idx = stdinBuf.indexOf('\n');
|
||||
if (idx < 0) break;
|
||||
QByteArray line = stdinBuf.left(idx + 1);
|
||||
stdinBuf.remove(0, idx + 1);
|
||||
socket->write(line);
|
||||
socket->flush();
|
||||
}
|
||||
});
|
||||
|
||||
stdinTimer->start();
|
||||
return app.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user