3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include <qsocketnotifier.h>
7 #include <qpopupmenu.h>
10 #include <kglobalsettings.h>
21 #include <unistd.h> /* open, close, etc. */
23 #ifdef HAVE_SYS_IOCTL_H
24 #include <sys/ioctl.h>
26 #ifdef HAVE_SYS_STAT_H
30 #include <pty.h> /* openpty on Linux */
33 #include <libutil.h> /* openpty on FreeBSD */
48 ::fcntl(m_masterfd
, F_SETFL
, O_NDELAY
);
49 m_outNotifier
= new QSocketNotifier(m_masterfd
, QSocketNotifier::Read
);
50 connect(m_outNotifier
, SIGNAL(activated(int)), SLOT(outReceived(int)));
52 m_slavetty
= QString();
70 #ifdef HAVE_FUNC_OPENPTY
71 /* use glibc2's openpty */
74 if (::openpty(&m_masterfd
, &m_slavefd
, 0, 0, 0) == 0) {
75 const char* tname
= ::ttyname(m_slavefd
);
81 m_masterfd
= m_slavefd
= -1;
87 // resort to BSD-style terminals
93 char ptynam
[] = "/dev/ptyxx";
94 char ttynam
[] = "/dev/ttyxx";
95 static const char ptyc3
[] = "pqrstuvwxyzabcde";
96 static const char ptyc4
[] = "0123456789abcdef";
98 // Find a master pty that we can open
99 for (s3
= ptyc3
; *s3
!= 0 && m_masterfd
< 0; s3
++)
101 for (s4
= ptyc4
; *s4
!= 0; s4
++)
103 ptynam
[8] = ttynam
[8] = *s3
;
104 ptynam
[9] = ttynam
[9] = *s4
;
105 if ((m_masterfd
= ::open(ptynam
,O_RDWR
)) >= 0)
107 if (::geteuid() == 0 || ::access(ttynam
,R_OK
|W_OK
) == 0)
119 return m_masterfd
>= 0;
122 void STTY::outReceived(int f
)
126 int n
= ::read(f
, buf
, sizeof(buf
));
128 if (errno
!= EAGAIN
) { /* this is not an error */
129 // ugh! error! somebody disconnect this signal please!
131 TRACE(QString().sprintf("error reading tty: %d", e
));
143 TTYWindow::TTYWindow(QWidget
* parent
, const char* name
) :
144 KTextView(parent
, name
),
148 setFont(kapp
->fixedFont
);
149 setFocusPolicy(StrongFocus
);
151 setFont(KGlobalSettings::fixedFont());
152 setFocusPolicy(WheelFocus
);
156 // create a context menu
157 m_popmenu
= new QPopupMenu
;
158 m_popmenu
->insertItem(i18n("&Clear"), this, SLOT(clear()));
161 TTYWindow::~TTYWindow()
168 QString
TTYWindow::activate()
170 // allocate a pseudo terminal
173 QString ttyName
= m_tty
->slaveTTY();
174 if (ttyName
.isEmpty()) {
175 // failed to allocate terminal
180 connect(m_tty
, SIGNAL(output(char*,int)), SLOT(slotAppend(char*,int)));
185 void TTYWindow::deactivate()
191 void TTYWindow::slotAppend(char* buffer
, int count
)
193 // is last line visible?
194 bool bottomVisible
= lastRowVisible() == m_texts
.size()-1;
197 char* start
= buffer
;
200 while (count
> 0 && start
[len
] != '\n') {
206 QString
str(start
, len
+1);
208 QString str
= QString::fromLatin1(start
, len
);
211 str
= m_texts
[m_texts
.size()-1] + str
;
212 replaceLine(m_texts
.size()-1, str
);
216 if (count
> 0 && *start
== '\n') {
217 insertLine(QString());
223 // if last row was visible, scroll down to make it visible again
225 setTopCell(m_texts
.size()-1);
228 void TTYWindow::clear()
231 m_width
= 300; m_height
= 14; /* Same as in KTextView::KTextView */
232 insertLine(QString());
235 void TTYWindow::mousePressEvent(QMouseEvent
* mouseEvent
)
237 // Check if right button was clicked.
238 if (mouseEvent
->button() == RightButton
)
240 if (m_popmenu
->isVisible()) {
243 m_popmenu
->popup(mapToGlobal(mouseEvent
->pos()));
246 QWidget::mousePressEvent(mouseEvent
);
250 #include "ttywnd.moc"