3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include <qsocketnotifier.h>
7 #include <qpopupmenu.h>
9 #include <kglobalsettings.h>
17 #include <unistd.h> /* open, close, etc. */
19 #ifdef HAVE_SYS_IOCTL_H
20 #include <sys/ioctl.h>
22 #ifdef HAVE_SYS_STAT_H
26 #include <pty.h> /* openpty on Linux */
29 #include <libutil.h> /* openpty on FreeBSD */
31 #ifdef HAVE_UTIL_H /* openpty on NetBSD, OpenBSD */
47 ::fcntl(m_masterfd
, F_SETFL
, O_NDELAY
);
48 m_outNotifier
= new QSocketNotifier(m_masterfd
, QSocketNotifier::Read
);
49 connect(m_outNotifier
, SIGNAL(activated(int)), SLOT(outReceived(int)));
51 m_slavetty
= QString();
69 #ifdef HAVE_FUNC_OPENPTY
70 /* use glibc2's openpty */
73 if (::openpty(&m_masterfd
, &m_slavefd
, 0, 0, 0) == 0) {
74 const char* tname
= ::ttyname(m_slavefd
);
80 m_masterfd
= m_slavefd
= -1;
86 // resort to BSD-style terminals
92 char ptynam
[] = "/dev/ptyxx";
93 char ttynam
[] = "/dev/ttyxx";
94 static const char ptyc3
[] = "pqrstuvwxyzabcde";
95 static const char ptyc4
[] = "0123456789abcdef";
97 // Find a master pty that we can open
98 for (s3
= ptyc3
; *s3
!= 0 && m_masterfd
< 0; s3
++)
100 for (s4
= ptyc4
; *s4
!= 0; s4
++)
102 ptynam
[8] = ttynam
[8] = *s3
;
103 ptynam
[9] = ttynam
[9] = *s4
;
104 if ((m_masterfd
= ::open(ptynam
,O_RDWR
)) >= 0)
106 if (::geteuid() == 0 || ::access(ttynam
,R_OK
|W_OK
) == 0)
118 return m_masterfd
>= 0;
121 void STTY::outReceived(int f
)
125 int n
= ::read(f
, buf
, sizeof(buf
));
127 if (errno
!= EAGAIN
) { /* this is not an error */
128 // ugh! error! somebody disconnect this signal please!
140 TTYWindow::TTYWindow(QWidget
* parent
, const char* name
) :
141 QTextEdit(parent
, name
),
145 setFont(KGlobalSettings::fixedFont());
147 setAutoFormatting(AutoNone
);
148 setTextFormat(PlainText
);
152 TTYWindow::~TTYWindow()
159 QString
TTYWindow::activate()
161 // allocate a pseudo terminal
164 QString ttyName
= m_tty
->slaveTTY();
165 if (ttyName
.isEmpty()) {
166 // failed to allocate terminal
171 connect(m_tty
, SIGNAL(output(char*,int)), SLOT(slotAppend(char*,int)));
176 void TTYWindow::deactivate()
183 * Note that it is necessary to track the horizontal position explicitly
184 * since if the user modifies the selected text in the window, the cursor
185 * position changes, too.
187 void TTYWindow::slotAppend(char* buffer
, int count
)
190 char* start
= buffer
;
193 while (count
> 0 && start
[len
] != '\n' && start
[len
] != '\r') {
198 QString str
= QString::fromLatin1(start
, len
);
199 // replace text in the last line
200 int para
= paragraphs()-1;
201 // this selection is non-empty only after a '\r' that was not
202 // followed by a '\n'
203 setSelection(para
, m_hPos
, para
, m_hPos
+len
, 1);
204 removeSelectedText(1);
205 insertAt(str
, para
, m_hPos
);
210 if (count
> 0 && *start
== '\r') {
215 if (count
> 0 && *start
== '\n') {
224 QPopupMenu
* TTYWindow::createPopupMenu(const QPoint
& pos
)
226 QPopupMenu
* menu
= QTextEdit::createPopupMenu(pos
);
227 menu
->insertSeparator();
228 menu
->insertItem(i18n("&Clear"), this, SLOT(slotClear()));
232 void TTYWindow::slotClear()
238 #include "ttywnd.moc"