2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
8 #include <QSocketNotifier>
10 #include <kglobalsettings.h>
15 #include <unistd.h> /* open, close, etc. */
16 #include <sys/ioctl.h>
19 #include <pty.h> /* openpty on Linux */
22 #include <libutil.h> /* openpty on FreeBSD */
24 #ifdef HAVE_UTIL_H /* openpty on NetBSD, OpenBSD */
40 ::fcntl(m_masterfd
, F_SETFL
, O_NDELAY
);
41 m_outNotifier
= new QSocketNotifier(m_masterfd
, QSocketNotifier::Read
);
42 connect(m_outNotifier
, SIGNAL(activated(int)), SLOT(outReceived(int)));
44 m_slavetty
= QString();
62 #ifdef HAVE_FUNC_OPENPTY
63 /* use glibc2's openpty */
66 if (::openpty(&m_masterfd
, &m_slavefd
, 0, 0, 0) == 0) {
67 const char* tname
= ::ttyname(m_slavefd
);
73 m_masterfd
= m_slavefd
= -1;
79 // resort to BSD-style terminals
85 char ptynam
[] = "/dev/ptyxx";
86 char ttynam
[] = "/dev/ttyxx";
87 static const char ptyc3
[] = "pqrstuvwxyzabcde";
88 static const char ptyc4
[] = "0123456789abcdef";
90 // Find a master pty that we can open
91 for (s3
= ptyc3
; *s3
!= 0 && m_masterfd
< 0; s3
++)
93 for (s4
= ptyc4
; *s4
!= 0; s4
++)
95 ptynam
[8] = ttynam
[8] = *s3
;
96 ptynam
[9] = ttynam
[9] = *s4
;
97 if ((m_masterfd
= ::open(ptynam
,O_RDWR
)) >= 0)
99 if (::geteuid() == 0 || ::access(ttynam
,R_OK
|W_OK
) == 0)
111 return m_masterfd
>= 0;
114 void STTY::outReceived(int f
)
118 int n
= ::read(f
, buf
, sizeof(buf
));
120 if (errno
!= EAGAIN
) { /* this is not an error */
121 // ugh! error! somebody disconnect this signal please!
133 TTYWindow::TTYWindow(QWidget
* parent
) :
138 setFont(KGlobalSettings::fixedFont());
140 setAutoFormatting(AutoNone
);
141 setTextFormat(Qt::PlainText
);
145 TTYWindow::~TTYWindow()
152 QString
TTYWindow::activate()
154 // allocate a pseudo terminal
157 QString ttyName
= m_tty
->slaveTTY();
158 if (ttyName
.isEmpty()) {
159 // failed to allocate terminal
164 connect(m_tty
, SIGNAL(output(char*,int)), SLOT(slotAppend(char*,int)));
169 void TTYWindow::deactivate()
176 * Note that it is necessary to track the horizontal position explicitly
177 * since if the user modifies the selected text in the window, the cursor
178 * position changes, too.
180 void TTYWindow::slotAppend(char* buffer
, int count
)
183 char* start
= buffer
;
186 while (count
> 0 && start
[len
] != '\n' && start
[len
] != '\r') {
191 QString str
= QString::fromLatin1(start
, len
);
192 // replace text in the last line
193 int para
= paragraphs()-1;
194 // this selection is non-empty only after a '\r' that was not
195 // followed by a '\n'
196 setSelection(para
, m_hPos
, para
, m_hPos
+len
, 1);
197 removeSelectedText(1);
198 insertAt(str
, para
, m_hPos
);
203 if (count
> 0 && *start
== '\r') {
208 if (count
> 0 && *start
== '\n') {
217 Q3PopupMenu
* TTYWindow::createPopupMenu(const QPoint
& pos
)
219 Q3PopupMenu
* menu
= Q3TextEdit::createPopupMenu(pos
);
220 menu
->insertSeparator();
221 menu
->insertItem(i18n("&Clear"), this, SLOT(slotClear()));
225 void TTYWindow::slotClear()
231 #include "ttywnd.moc"