2 * \file server_monitor.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Enrico Forestieri
8 * Full author contact details are available in file CREDITS.
10 * This program sends commands to a running instance of LyX and
11 * receives information back from LyX.
14 * 1) Run moc or moc-qt5 on server_monitor.h to produce moc_server_monitor.cpp:
15 * moc server_monitor.h -o moc_server_monitor.cpp
16 * 2) If the QtGui.pc file is not in the pkg-config search path, find the
17 * directory where it is located (e.g., use the command `locate QtGui.pc')
18 * and set the environment variable PKG_CONFIG_PATH to this directory.
20 * export PKG_CONFIG_PATH=/path/to/directory (if using bash)
21 * setenv PKG_CONFIG_PATH /path/to/directory (if using tcsh)
22 * If the command `pkg-config --modversion QtGui' does not complain and
23 * prints the Qt version, you don't need to set PKG_CONFIG_PATH.
24 * 3) Compile using the following command:
25 * g++ -fPIC server_monitor.cpp -o monitor -I. `pkg-config --cflags --libs Qt5Gui Qt5Widgets`
27 * Alternatively, you can create a Makefile with qmake and then build
28 * the executable by running make (or nmake, if you use msvc):
33 * 1) Set the LyXserver pipe path in the LyX preferences (on *nix you can use
34 * any path, for example ~/.lyx/lyxpipe, whereas on Windows the path has
35 * to start with `\\.\pipe\', for example you can use \\.\pipe\lyxpipe).
36 * 2) Quit and restart LyX.
37 * 3) Launch this program, adjust the pipe name to match that one used in LyX,
38 * push the button labeled "Open pipes" and then try issuing some commands.
41 #include <QApplication>
46 #include "server_monitor.h"
48 LyXServerMonitor::LyXServerMonitor()
49 : pipein(-1), pipeout(-1), thread_exit(false), lyx_listen(false)
54 char const * const home = getenv("HOME");
55 QString const pipeName = (home && home[0]) ?
56 QString::fromUtf8(home) + "/.lyx/lyxpipe" : "\\\\.\\pipe\\lyxpipe";
58 pipeNameLE->setText(pipeName);
59 clientNameLE->setText("monitor");
60 submitCommandPB->setDisabled(true);
61 closePipesPB->setDisabled(true);
63 connect(openPipesPB, SIGNAL(clicked()), this, SLOT(openPipes()));
64 connect(closePipesPB, SIGNAL(clicked()), this, SLOT(closePipes()));
65 connect(submitCommandPB, SIGNAL(clicked()), this, SLOT(submitCommand()));
66 connect(donePB, SIGNAL(clicked()), this, SLOT(reject()));
68 QVBoxLayout * mainLayout = new QVBoxLayout;
69 mainLayout->addWidget(gridGB);
70 mainLayout->addWidget(horizontalGB);
71 setLayout(mainLayout);
73 setWindowTitle("LyX Server Monitor");
77 LyXServerMonitor::~LyXServerMonitor()
84 void LyXServerMonitor::createGridGroupBox()
86 gridGB = new QGroupBox;
87 QGridLayout * layout = new QGridLayout;
89 labels[0] = new QLabel("Pipe name");
90 pipeNameLE = new QLineEdit;
91 layout->addWidget(labels[0], 0, 0, Qt::AlignRight);
92 layout->addWidget(pipeNameLE, 0, 1);
94 labels[1] = new QLabel("Command");
95 commandLE = new QLineEdit;
96 layout->addWidget(labels[1], 1, 0, Qt::AlignRight);
97 layout->addWidget(commandLE, 1, 1);
99 labels[2] = new QLabel("Client name");
100 clientNameLE = new QLineEdit;
101 layout->addWidget(labels[2], 0, 2, Qt::AlignRight);
102 layout->addWidget(clientNameLE, 0, 3);
104 labels[3] = new QLabel("Argument");
105 argumentLE = new QLineEdit;
106 layout->addWidget(labels[3], 1, 2, Qt::AlignRight);
107 layout->addWidget(argumentLE, 1, 3);
109 labels[4] = new QLabel("Info");
111 infoLB->setFrameStyle(QFrame::Panel | QFrame::Sunken);
112 infoLB->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
113 layout->addWidget(labels[4], 2, 0, Qt::AlignRight);
114 layout->addWidget(infoLB, 2, 1, 1, 3);
116 labels[5] = new QLabel("Notify");
117 notifyLB = new QLabel;
118 notifyLB->setFrameStyle(QFrame::Panel | QFrame::Sunken);
119 notifyLB->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
120 layout->addWidget(labels[5], 3, 0, Qt::AlignRight);
121 layout->addWidget(notifyLB, 3, 1, 1, 3);
123 layout->setColumnMinimumWidth(1, 200);
124 layout->setColumnMinimumWidth(3, 200);
125 gridGB->setLayout(layout);
129 void LyXServerMonitor::createCmdsGroupBox()
131 horizontalGB = new QGroupBox;
132 QHBoxLayout * layout = new QHBoxLayout;
134 openPipesPB = new QPushButton("&Open pipes");
135 layout->addWidget(openPipesPB);
137 closePipesPB = new QPushButton("C&lose pipes");
138 layout->addWidget(closePipesPB);
140 submitCommandPB = new QPushButton("&Submit Command");
141 layout->addWidget(submitCommandPB);
143 donePB = new QPushButton("&Done");
144 layout->addWidget(donePB);
146 horizontalGB->setLayout(layout);
150 void LyXServerMonitor::readPipe()
154 bool notified = false;
156 while ((n = ::read(pipeout, pipedata, BUFSIZE - 1)) && !thread_exit) {
159 QString const fromLyX =
160 QString::fromUtf8(pipedata).trimmed();
161 qWarning() << "monitor: Coming: " << fromLyX;
162 if (fromLyX.startsWith("LYXSRV:")) {
163 if (fromLyX.contains("bye")) {
164 qWarning() << "monitor: LyX has closed "
166 pipethread->emitNotice(fromLyX);
170 if (fromLyX.contains("hello")) {
172 qWarning() << "monitor: "
174 submitCommandPB->setDisabled(false);
177 if (fromLyX[0] == QLatin1Char('I'))
178 pipethread->emitInfo(fromLyX);
180 pipethread->emitNotice(fromLyX);
182 // On Windows, we have to close and reopen
183 // the pipe after each use.
186 outPipeName().toLocal8Bit().constData(),
190 pipethread->emitNotice("An error occurred, "
198 if (errno == ECOMM) {
199 // When talking to a native Windows version of
200 // LyX, the second time we try to use the pipe,
201 // read() fails with ECOMM. In this case, we
202 // have to simply close and reopen it.
205 outPipeName().toLocal8Bit().constData(),
212 pipethread->emitNotice("An error occurred, closing pipes");
221 qWarning() << "monitor: Closing pipes";
222 pipethread->emitNotice("Closing pipes");
224 qWarning() << "monitor: LyX has closed connection!";
225 pipethread->emitNotice("LyX has closed connection!");
228 QEvent * event = new QEvent(QEvent::User);
229 QCoreApplication::postEvent(this, event);
232 pipethread->emitClosing();
236 bool LyXServerMonitor::event(QEvent * e)
238 if (e->type() == QEvent::User) {
244 return QDialog::event(e);
248 void LyXServerMonitor::showInfo(QString const & msg)
250 infoLB->setText(msg);
255 void LyXServerMonitor::showNotice(QString const & msg)
258 notifyLB->setText(msg);
262 void LyXServerMonitor::openPipes()
265 qWarning() << "monitor: Opening pipes " << inPipeName()
266 << " and " << outPipeName();
267 pipein = ::open(inPipeName().toLocal8Bit().constData(),
269 pipeout = ::open(outPipeName().toLocal8Bit().constData(),
271 if (pipein < 0 || pipeout < 0) {
272 qWarning() << "monitor: Could not open the pipes";
274 notifyLB->setText("Could not open the pipes");
275 if (pipein >= 0 || pipeout >= 0)
279 pipethread = new ReadPipe(this);
281 if (!pipethread->isRunning()) {
282 qWarning() << "monitor: Could not create pipe thread";
284 notifyLB->setText("Could not create pipe thread");
288 connect(pipethread, SIGNAL(info(QString const &)),
289 this, SLOT(showInfo(QString const &)));
290 connect(pipethread, SIGNAL(notice(QString const &)),
291 this, SLOT(showNotice(QString const &)));
292 connect(pipethread, SIGNAL(closing()),
293 this, SLOT(closePipes()));
294 openPipesPB->setDisabled(true);
295 closePipesPB->setDisabled(false);
297 QString const clientname = clientNameLE->text();
298 snprintf(buffer, BUFSIZE - 1,
299 "LYXSRV:%s:hello\n", clientname.toUtf8().constData());
300 buffer[BUFSIZE - 1] = '\0';
301 ::write(pipein, buffer, strlen(buffer));
303 qWarning() << "monitor: Pipes already opened, close them first\n";
307 void LyXServerMonitor::closePipes()
309 if (pipein == -1 && pipeout == -1) {
310 qWarning() << "monitor: Pipes are not opened";
317 QString const clientname = clientNameLE->text();
318 if (pipethread->isRunning()) {
320 // The thread, currently blocked on the read()
321 // call, will be waked up by the reply from
322 // LyX and will exit.
323 snprintf(buffer, BUFSIZE - 1,
324 "LYXCMD:%s:message:Client '%s' is leaving\n",
325 clientname.toUtf8().constData(),
326 clientname.toUtf8().constData());
327 buffer[BUFSIZE - 1] = '\0';
328 ::write(pipein, buffer, strlen(buffer));
331 snprintf(buffer, BUFSIZE - 1, "LYXSRV:%s:bye\n",
332 clientname.toUtf8().constData());
333 buffer[BUFSIZE - 1] = '\0';
334 ::write(pipein, buffer, strlen(buffer));
343 pipein = pipeout = -1;
344 submitCommandPB->setDisabled(true);
345 openPipesPB->setDisabled(false);
346 closePipesPB->setDisabled(true);
350 void LyXServerMonitor::submitCommand()
353 QString const command = commandLE->text();
354 QString const argument = argumentLE->text();
355 QString const clientname = clientNameLE->text();
356 snprintf(buffer, BUFSIZE - 2, "LYXCMD:%s:%s:%s",
357 clientname.toUtf8().constData(),
358 command.toUtf8().constData(),
359 argument.toUtf8().constData());
360 buffer[BUFSIZE - 1] = '\0';
361 qWarning() << "monitor: Sending: " << buffer;
362 strcat(buffer, "\n");
363 ::write(pipein, buffer, strlen(buffer));
365 qWarning() << "monitor: Pipe is not opened";
369 int main(int argc, char * argv[])
371 QApplication app(argc, argv);
372 LyXServerMonitor dialog;
373 return dialog.exec();
376 #include "moc_server_monitor.cpp"