Move type inference from parse time to display time.
[kdbg.git] / kdbg / ttywnd.cpp
blob195fa832a0233b34a067e46c9b8575535a36e6f3
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include <qsocketnotifier.h>
7 #include <qpopupmenu.h>
8 #include "ttywnd.h"
9 #include <kglobalsettings.h>
10 #include <klocale.h>
12 #include "config.h"
13 #ifdef HAVE_FCNTL_H
14 #include <fcntl.h>
15 #endif
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h> /* open, close, etc. */
18 #endif
19 #ifdef HAVE_SYS_IOCTL_H
20 #include <sys/ioctl.h>
21 #endif
22 #ifdef HAVE_SYS_STAT_H
23 #include <sys/stat.h>
24 #endif
25 #ifdef HAVE_PTY_H
26 #include <pty.h> /* openpty on Linux */
27 #endif
28 #ifdef HAVE_LIBUTIL_H
29 #include <libutil.h> /* openpty on FreeBSD */
30 #endif
31 #ifdef HAVE_UTIL_H /* openpty on NetBSD, OpenBSD */
32 #include <util.h>
33 #endif
34 #include <errno.h>
36 #include "mydebug.h"
39 STTY::STTY() :
40 QObject(),
41 m_masterfd(-1),
42 m_slavefd(-1),
43 m_outNotifier(0)
45 if (findTTY())
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)));
50 } else {
51 m_slavetty = QString();
55 STTY::~STTY()
57 if (m_outNotifier) {
58 ::close(m_masterfd);
59 if (m_slavefd >= 0)
60 ::close(m_slavefd);
61 delete m_outNotifier;
65 bool STTY::findTTY()
67 m_masterfd = -1;
69 #ifdef HAVE_FUNC_OPENPTY
70 /* use glibc2's openpty */
71 if (m_masterfd < 0)
73 if (::openpty(&m_masterfd, &m_slavefd, 0, 0, 0) == 0) {
74 const char* tname = ::ttyname(m_slavefd);
75 if (tname != 0) {
76 m_slavetty = tname;
77 } else {
78 ::close(m_slavefd);
79 ::close(m_masterfd);
80 m_masterfd = m_slavefd = -1;
84 #endif
86 // resort to BSD-style terminals
87 if (m_masterfd < 0)
89 const char* s3;
90 const char* s4;
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)
108 m_slavetty = ttynam;
109 break;
111 ::close(m_masterfd);
112 m_masterfd = -1;
118 return m_masterfd >= 0;
121 void STTY::outReceived(int f)
123 for (;;) {
124 char buf[1024];
125 int n = ::read(f, buf, sizeof(buf));
126 if (n < 0) {
127 if (errno != EAGAIN) { /* this is not an error */
128 // ugh! error! somebody disconnect this signal please!
129 int e = errno;
130 TRACE(QString().sprintf("error reading tty: %d", e));
132 break;
134 emit output(buf, n);
135 if (n == 0)
136 break;
142 TTYWindow::TTYWindow(QWidget* parent, const char* name) :
143 KTextView(parent, name),
144 m_tty(0)
146 setFont(KGlobalSettings::fixedFont());
147 setFocusPolicy(WheelFocus);
148 clear();
150 // create a context menu
151 m_popmenu = new QPopupMenu;
152 m_popmenu->insertItem(i18n("&Clear"), this, SLOT(clear()));
155 TTYWindow::~TTYWindow()
157 if (m_tty)
158 deactivate();
162 QString TTYWindow::activate()
164 // allocate a pseudo terminal
165 m_tty = new STTY;
167 QString ttyName = m_tty->slaveTTY();
168 if (ttyName.isEmpty()) {
169 // failed to allocate terminal
170 delete m_tty;
171 m_tty = 0;
172 return QString();
173 } else {
174 connect(m_tty, SIGNAL(output(char*,int)), SLOT(slotAppend(char*,int)));
175 return ttyName;
179 void TTYWindow::deactivate()
181 delete m_tty;
182 m_tty = 0;
185 void TTYWindow::slotAppend(char* buffer, int count)
187 // is last line visible?
188 bool bottomVisible = lastRowVisible() == m_texts.size()-1;
190 // parse off lines
191 char* start = buffer;
192 while (count > 0) {
193 int len = 0;
194 while (count > 0 && start[len] != '\n') {
195 --count;
196 ++len;
198 if (len > 0) {
199 QString str = QString::fromLatin1(start, len);
200 // update last line
201 str = m_texts[m_texts.size()-1] + str;
202 replaceLine(m_texts.size()-1, str);
203 start += len;
204 len = 0;
206 if (count > 0 && *start == '\n') {
207 insertLine(QString());
208 ++start;
209 --count;
213 // if last row was visible, scroll down to make it visible again
214 if (bottomVisible)
215 setTopCell(m_texts.size()-1);
218 void TTYWindow::clear()
220 m_texts.setSize(0);
221 m_width = 300; m_height = 14; /* Same as in KTextView::KTextView */
222 insertLine(QString());
225 void TTYWindow::mousePressEvent(QMouseEvent* mouseEvent)
227 // Check if right button was clicked.
228 if (mouseEvent->button() == RightButton)
230 if (m_popmenu->isVisible()) {
231 m_popmenu->hide();
232 } else {
233 m_popmenu->popup(mapToGlobal(mouseEvent->pos()));
235 } else {
236 QWidget::mousePressEvent(mouseEvent);
240 #include "ttywnd.moc"