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-qt4 on server_monitor.h to produce moc_server_monitor.cpp:
15 * moc-qt4 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++ server_monitor.cpp -o monitor -I. `pkg-config --cflags --libs QtGui`
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>
45 #include "server_monitor.h"
47 LyXServerMonitor::LyXServerMonitor()
48 : pipein(-1), pipeout(-1), thread_exit(false), lyx_listen(false)
53 char const * const home = getenv("HOME");
54 QString const pipeName = (home && home[0]) ?
55 QString::fromUtf8(home) + "/.lyx/lyxpipe" : "\\\\.\\pipe\\lyxpipe";
57 pipeNameLE->setText(pipeName);
58 clientNameLE->setText("monitor");
59 submitCommandPB->setDisabled(true);
60 closePipesPB->setDisabled(true);
62 connect(openPipesPB, SIGNAL(clicked()), this, SLOT(openPipes()));
63 connect(closePipesPB, SIGNAL(clicked()), this, SLOT(closePipes()));
64 connect(submitCommandPB, SIGNAL(clicked()), this, SLOT(submitCommand()));
65 connect(donePB, SIGNAL(clicked()), this, SLOT(reject()));
67 QVBoxLayout * mainLayout = new QVBoxLayout;
68 mainLayout->addWidget(gridGB);
69 mainLayout->addWidget(horizontalGB);
70 setLayout(mainLayout);
72 setWindowTitle("LyX Server Monitor");
76 LyXServerMonitor::~LyXServerMonitor()
83 void LyXServerMonitor::createGridGroupBox()
85 gridGB = new QGroupBox;
86 QGridLayout * layout = new QGridLayout;
88 labels[0] = new QLabel("Pipe name");
89 pipeNameLE = new QLineEdit;
90 layout->addWidget(labels[0], 0, 0, Qt::AlignRight);
91 layout->addWidget(pipeNameLE, 0, 1);
93 labels[1] = new QLabel("Command");
94 commandLE = new QLineEdit;
95 layout->addWidget(labels[1], 1, 0, Qt::AlignRight);
96 layout->addWidget(commandLE, 1, 1);
98 labels[2] = new QLabel("Client name");
99 clientNameLE = new QLineEdit;
100 layout->addWidget(labels[2], 0, 2, Qt::AlignRight);
101 layout->addWidget(clientNameLE, 0, 3);
103 labels[3] = new QLabel("Argument");
104 argumentLE = new QLineEdit;
105 layout->addWidget(labels[3], 1, 2, Qt::AlignRight);
106 layout->addWidget(argumentLE, 1, 3);
108 labels[4] = new QLabel("Info");
110 infoLB->setFrameStyle(QFrame::Panel | QFrame::Sunken);
111 infoLB->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
112 layout->addWidget(labels[4], 2, 0, Qt::AlignRight);
113 layout->addWidget(infoLB, 2, 1, 1, 3);
115 labels[5] = new QLabel("Notify");
116 notifyLB = new QLabel;
117 notifyLB->setFrameStyle(QFrame::Panel | QFrame::Sunken);
118 notifyLB->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
119 layout->addWidget(labels[5], 3, 0, Qt::AlignRight);
120 layout->addWidget(notifyLB, 3, 1, 1, 3);
122 layout->setColumnMinimumWidth(1, 200);
123 layout->setColumnMinimumWidth(3, 200);
124 gridGB->setLayout(layout);
128 void LyXServerMonitor::createCmdsGroupBox()
130 horizontalGB = new QGroupBox;
131 QHBoxLayout * layout = new QHBoxLayout;
133 openPipesPB = new QPushButton("&Open pipes");
134 layout->addWidget(openPipesPB);
136 closePipesPB = new QPushButton("C&lose pipes");
137 layout->addWidget(closePipesPB);
139 submitCommandPB = new QPushButton("&Submit Command");
140 layout->addWidget(submitCommandPB);
142 donePB = new QPushButton("&Done");
143 layout->addWidget(donePB);
145 horizontalGB->setLayout(layout);
149 void LyXServerMonitor::readPipe()
153 bool notified = false;
155 while ((n = ::read(pipeout, pipedata, BUFSIZE - 1)) && !thread_exit) {
158 QString const fromLyX =
159 QString::fromUtf8(pipedata).trimmed();
160 qWarning() << "monitor: Coming: " << fromLyX;
161 if (fromLyX.startsWith("LYXSRV:")) {
162 if (fromLyX.contains("bye")) {
163 qWarning() << "monitor: LyX has closed "
165 pipethread->emitNotice(fromLyX);
169 if (fromLyX.contains("hello")) {
171 qWarning() << "monitor: "
173 submitCommandPB->setDisabled(false);
176 if (fromLyX[0] == QLatin1Char('I'))
177 pipethread->emitInfo(fromLyX);
179 pipethread->emitNotice(fromLyX);
181 // On Windows, we have to close and reopen
182 // the pipe after each use.
185 outPipeName().toLocal8Bit().constData(),
189 pipethread->emitNotice("An error occurred, "
197 if (errno == ECOMM) {
198 // When talking to a native Windows version of
199 // LyX, the second time we try to use the pipe,
200 // read() fails with ECOMM. In this case, we
201 // have to simply close and reopen it.
204 outPipeName().toLocal8Bit().constData(),
211 pipethread->emitNotice("An error occurred, closing pipes");
220 qWarning() << "monitor: Closing pipes";
221 pipethread->emitNotice("Closing pipes");
223 qWarning() << "monitor: LyX has closed connection!";
224 pipethread->emitNotice("LyX has closed connection!");
227 QEvent * event = new QEvent(QEvent::User);
228 QCoreApplication::postEvent(this, event);
231 pipethread->emitClosing();
235 bool LyXServerMonitor::event(QEvent * e)
237 if (e->type() == QEvent::User) {
243 return QDialog::event(e);
247 void LyXServerMonitor::showInfo(QString const & msg)
249 infoLB->setText(msg);
254 void LyXServerMonitor::showNotice(QString const & msg)
257 notifyLB->setText(msg);
261 void LyXServerMonitor::openPipes()
264 qWarning() << "monitor: Opening pipes " << inPipeName()
265 << " and " << outPipeName();
266 pipein = ::open(inPipeName().toLocal8Bit().constData(),
268 pipeout = ::open(outPipeName().toLocal8Bit().constData(),
270 if (pipein < 0 || pipeout < 0) {
271 qWarning() << "monitor: Could not open the pipes";
273 notifyLB->setText("Could not open the pipes");
274 if (pipein >= 0 || pipeout >= 0)
278 pipethread = new ReadPipe(this);
280 if (!pipethread->isRunning()) {
281 qWarning() << "monitor: Could not create pipe thread";
283 notifyLB->setText("Could not create pipe thread");
287 connect(pipethread, SIGNAL(info(QString const &)),
288 this, SLOT(showInfo(QString const &)));
289 connect(pipethread, SIGNAL(notice(QString const &)),
290 this, SLOT(showNotice(QString const &)));
291 connect(pipethread, SIGNAL(closing()),
292 this, SLOT(closePipes()));
293 openPipesPB->setDisabled(true);
294 closePipesPB->setDisabled(false);
296 QString const clientname = clientNameLE->text();
297 snprintf(buffer, BUFSIZE - 1,
298 "LYXSRV:%s:hello\n", clientname.toUtf8().constData());
299 buffer[BUFSIZE - 1] = '\0';
300 ::write(pipein, buffer, strlen(buffer));
302 qWarning() << "monitor: Pipes already opened, close them first\n";
306 void LyXServerMonitor::closePipes()
308 if (pipein == -1 && pipeout == -1) {
309 qWarning() << "monitor: Pipes are not opened";
316 QString const clientname = clientNameLE->text();
317 if (pipethread->isRunning()) {
319 // The thread, currently blocked on the read()
320 // call, will be waked up by the reply from
321 // LyX and will exit.
322 snprintf(buffer, BUFSIZE - 1,
323 "LYXCMD:%s:message:Client '%s' is leaving\n",
324 clientname.toUtf8().constData(),
325 clientname.toUtf8().constData());
326 buffer[BUFSIZE - 1] = '\0';
327 ::write(pipein, buffer, strlen(buffer));
330 snprintf(buffer, BUFSIZE - 1, "LYXSRV:%s:bye\n",
331 clientname.toUtf8().constData());
332 buffer[BUFSIZE - 1] = '\0';
333 ::write(pipein, buffer, strlen(buffer));
342 pipein = pipeout = -1;
343 submitCommandPB->setDisabled(true);
344 openPipesPB->setDisabled(false);
345 closePipesPB->setDisabled(true);
349 void LyXServerMonitor::submitCommand()
352 QString const command = commandLE->text();
353 QString const argument = argumentLE->text();
354 QString const clientname = clientNameLE->text();
355 snprintf(buffer, BUFSIZE - 2, "LYXCMD:%s:%s:%s",
356 clientname.toUtf8().constData(),
357 command.toUtf8().constData(),
358 argument.toUtf8().constData());
359 buffer[BUFSIZE - 1] = '\0';
360 qWarning() << "monitor: Sending: " << buffer;
361 strcat(buffer, "\n");
362 ::write(pipein, buffer, strlen(buffer));
364 qWarning() << "monitor: Pipe is not opened";
368 int main(int argc, char * argv[])
370 QApplication app(argc, argv);
371 LyXServerMonitor dialog;
372 return dialog.exec();
375 #include "moc_server_monitor.cpp"