Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
QHyodaX11XtermLog.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7#include <unistd.h>
8#include <QWindow>
9#include <QGroupBox>
10#include <QHyodaX11XtermLog.h>
11#include <QProcess>
12
13// ****************************************************************************
14// * QHyodaX11XtermLog
15// ****************************************************************************
16QHyodaX11XtermLog::QHyodaX11XtermLog(QHyodaJob *j,
17 QWidget *parent,
18 QString tabName):
19 QWidget(parent),
20 job(j),
21 xterm(new QProcess()),
22 xwininfo(new QProcess()),
23 xdotool(new QProcess()),
24 xWidget(NULL),
25 xtermTitle(QString("xtermHyoda0x%1").arg(qHash(QUuid::createUuid()),0,16).toLocal8Bit().constData()),
26 WinId(0),
27 size(QSize())
28{
29 qDebug() << "\33[32m[QHyodaX11XtermLog::QHyodaX11XtermLog] NEW"<<"\33[m";
30 // Mise en place de l'UI
31 setupUI(parent);
32 // Connexion du signal indiquant la fin de l'xterm
33 connect(xterm, SIGNAL(finished(int,QProcess::ExitStatus)),
34 this,SLOT(xtermFinished(int,QProcess::ExitStatus)));
35 // On prépare l'xterm
36 QString executable("xterm");
37 QStringList arguments=
38 QStringList() << "-fg" << "green" << "-bg" << "black"
39 << "-maximized"
40 << "-title" << xtermTitle
41 << "-j" // indicates that xterm should do jump scrolling
42 << "-b" << "2" // size of the inner border, in pixels
43 << "-vb" // Visual bell is preferred over an audible one
44 << "-sb" << "-sl" << "8192"
45 << "+cm" // Enables recognition of ANSI color-change escape sequences
46 << "-bc" // Turn on text cursor blinking
47 << "+l" // Turn logging off
48 << "+lc" // Turn off support of automatic selection of locale encodings
49 //<< "-s" // xterm may scroll asynchronously
50 << "+aw" // auto-wraparound should not be allowed
51 << "-ut" // xterm should not write a record into the the system utmp log file
52 << "-into" << QString::number(xWidget->winId())
53 << "-e";
54 // On prépare le programme à exécuter dans cet xterm
55 QString xterm_execute;
56 for(int i=1;i<job->argc;i+=1)
57 xterm_execute.append(QString("%1%2").arg((i>1)?" ":"",job->argv[i]));
58 xterm_execute.append(";/bin/bash");
59 arguments << xterm_execute;
60 qDebug() << "\33[32m[QHyodaX11XtermLog] arguments: "<<arguments<<"\33[m";
61 // Lancement de l'xterm
62 xterm->start(executable,arguments);
63 if (!xterm->waitForStarted()) qFatal("QHyodaX11XtermLog::refresh NOT started!");
64 // Lancement du xwininfo
65 xwininfo->setProcessChannelMode(QProcess::MergedChannels);
66 connect(xwininfo,SIGNAL(readyReadStandardOutput()),this,SLOT(xwininfoOutput()));
67 xwininfoStart();
68}
69
70// ****************************************************************************
71// * setupUI
72// ****************************************************************************
73void QHyodaX11XtermLog::setupUI(QWidget *parent){
74 QGroupBox *groupBox = new QGroupBox(QString("Log"),parent);
75 QVBoxLayout *layout = new QVBoxLayout(this);
76 QVBoxLayout *vbox = new QVBoxLayout;
77 vbox->addWidget(xWidget=new QWidget(this));
78 groupBox->setLayout(vbox);
79 layout->addWidget(groupBox);
80 setLayout(layout);
81}
82
83// ****************************************************************************
84// * xwininfoStart
85// ****************************************************************************
86void QHyodaX11XtermLog::xwininfoStart(){
87 xwininfo->start(QString("xwininfo"), QStringList()<< "-name" << xtermTitle);
88 if (!xwininfo->waitForStarted()) qFatal("xlsclients NOT started!");
89}
90
91// ****************************************************************************
92// * setXtermSizeWithXdotool
93// ****************************************************************************
94void QHyodaX11XtermLog::setXtermSizeWithXdotool(const QSize size){
95 if (WinId==0) return;
96 if (xdotool->state()==QProcess::Running) return;
97 xdotool->start(QString("xdotool"),
98 QStringList()
99 << "windowsize"
100 << QString::number(WinId)
101 << QString::number(size.width())
102 << QString::number(size.height()));
103 if (!xdotool->waitForStarted()) qFatal("xlsclients NOT started!");
104}
105
106// ****************************************************************************
107// * resizeEvent
108// ****************************************************************************
109void QHyodaX11XtermLog::resizeEvent(QResizeEvent *e){
110 setXtermSizeWithXdotool(size=e->size());
111}
112
113// ****************************************************************************
114// * xtermFinished
115// ****************************************************************************
116void QHyodaX11XtermLog::xtermFinished(int exitCode, QProcess::ExitStatus exitStatus){
117 qDebug() << "\33[32m[QHyodaX11XtermLog] xterm has finished with exitCode"
118 <<exitCode<<", and exitStatus"<<exitStatus<<"\33[m";
119 xterm->closeWriteChannel();
120 if (xterm->waitForFinished()) qFatal("xterm NOT finished!");
121 if (exitStatus==QProcess::NormalExit) job->quit();
122}
123
124// ****************************************************************************
125// * xwininfoOutput
126// ****************************************************************************
127void QHyodaX11XtermLog::xwininfoOutput(){
128 // Si on a déjà récupéré l'id de la fenêtre, il n'y a plus rien à faire
129 if (WinId!=0) return;
130 const QStringList lines=QString(xwininfo->readAllStandardOutput()).split(QRegExp("\\s+"));
131 // On scrute le retour pour voir si l'on peut récupérer l'id
132 if (lines.size()>=4 && lines.at(4).startsWith("0x")){
133 bool ok;
134 WinId = lines.at(4).toLong(&ok, 16);
135 qDebug()<<"␛[1;32m[xwininfoOutput] WinId is"
136 <<QString("0x%1").arg(WinId,0,16).toLocal8Bit().constData()<<"␛[0m";
137 if (!ok) qFatal("Could not play with WinID");
138 setXtermSizeWithXdotool(size);
139 return;
140 }
141
142 xwininfo->close();
143 if (xwininfo->waitForFinished()) qFatal("xwininfo NOT closed!");
144 xwininfoStart();
145}
void quit()
Definition QHyodaJob.cc:258