add more spacing
[personal-kdebase.git] / apps / konsole / src / Pty.cpp
blob19e6fabd28c2935eaa5f1724932aa9522a778efd
1 /*
2 This file is part of Konsole, an X terminal.
3 Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
21 // Own
22 #include "Pty.h"
24 // System
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <termios.h>
30 #include <signal.h>
32 // Qt
33 #include <QtCore/QStringList>
35 // KDE
36 #include <KStandardDirs>
37 #include <KLocale>
38 #include <KDebug>
39 #include <KPty>
40 #include <KPtyDevice>
41 #include <kde_file.h>
43 using namespace Konsole;
45 void Pty::setWindowSize(int lines, int cols)
47 _windowColumns = cols;
48 _windowLines = lines;
50 if (pty()->masterFd() >= 0)
51 pty()->setWinSize(lines, cols);
53 QSize Pty::windowSize() const
55 return QSize(_windowColumns,_windowLines);
58 void Pty::setFlowControlEnabled(bool enable)
60 _xonXoff = enable;
62 if (pty()->masterFd() >= 0)
64 struct ::termios ttmode;
65 pty()->tcGetAttr(&ttmode);
66 if (!enable)
67 ttmode.c_iflag &= ~(IXOFF | IXON);
68 else
69 ttmode.c_iflag |= (IXOFF | IXON);
70 if (!pty()->tcSetAttr(&ttmode))
71 kWarning() << "Unable to set terminal attributes.";
74 bool Pty::flowControlEnabled() const
76 if (pty()->masterFd() >= 0)
78 struct ::termios ttmode;
79 pty()->tcGetAttr(&ttmode);
80 return ttmode.c_iflag & IXOFF &&
81 ttmode.c_iflag & IXON;
83 kWarning() << "Unable to get flow control status, terminal not connected.";
84 return false;
87 void Pty::setUtf8Mode(bool enable)
89 #ifdef IUTF8 // XXX not a reasonable place to check it.
90 _utf8 = enable;
92 if (pty()->masterFd() >= 0)
94 struct ::termios ttmode;
95 pty()->tcGetAttr(&ttmode);
96 if (!enable)
97 ttmode.c_iflag &= ~IUTF8;
98 else
99 ttmode.c_iflag |= IUTF8;
100 if (!pty()->tcSetAttr(&ttmode))
101 kWarning() << "Unable to set terminal attributes.";
103 #endif
106 void Pty::setErase(char erase)
108 _eraseChar = erase;
110 if (pty()->masterFd() >= 0)
112 struct ::termios ttmode;
113 pty()->tcGetAttr(&ttmode);
114 ttmode.c_cc[VERASE] = erase;
115 if (!pty()->tcSetAttr(&ttmode))
116 kWarning() << "Unable to set terminal attributes.";
120 char Pty::erase() const
122 if (pty()->masterFd() >= 0)
124 struct ::termios ttyAttributes;
125 pty()->tcGetAttr(&ttyAttributes);
126 return ttyAttributes.c_cc[VERASE];
129 return _eraseChar;
132 void Pty::addEnvironmentVariables(const QStringList& environment)
134 QListIterator<QString> iter(environment);
135 while (iter.hasNext())
137 QString pair = iter.next();
139 // split on the first '=' character
140 int pos = pair.indexOf('=');
142 if ( pos >= 0 )
144 QString variable = pair.left(pos);
145 QString value = pair.mid(pos+1);
147 setEnv(variable,value);
152 int Pty::start(const QString& program,
153 const QStringList& programArguments,
154 const QStringList& environment,
155 ulong winid,
156 bool addToUtmp,
157 const QString& dbusService,
158 const QString& dbusSession)
160 clearProgram();
162 // For historical reasons, the first argument in programArguments is the
163 // name of the program to execute, so create a list consisting of all
164 // but the first argument to pass to setProgram()
165 Q_ASSERT(programArguments.count() >= 1);
166 setProgram(program.toLatin1(),programArguments.mid(1));
168 addEnvironmentVariables(environment);
170 if ( !dbusService.isEmpty() )
171 setEnv("KONSOLE_DBUS_SERVICE",dbusService);
172 if ( !dbusSession.isEmpty() )
173 setEnv("KONSOLE_DBUS_SESSION", dbusSession);
175 setEnv("WINDOWID", QString::number(winid));
177 // unless the LANGUAGE environment variable has been set explicitly
178 // set it to a null string
179 // this fixes the problem where KCatalog sets the LANGUAGE environment
180 // variable during the application's startup to something which
181 // differs from LANG,LC_* etc. and causes programs run from
182 // the terminal to display messages in the wrong language
184 // this can happen if LANG contains a language which KDE
185 // does not have a translation for
187 // BR:149300
188 setEnv("LANGUAGE",QString(),false /* do not overwrite existing value if any */);
190 setUseUtmp(addToUtmp);
192 struct ::termios ttmode;
193 pty()->tcGetAttr(&ttmode);
194 if (!_xonXoff)
195 ttmode.c_iflag &= ~(IXOFF | IXON);
196 else
197 ttmode.c_iflag |= (IXOFF | IXON);
198 #ifdef IUTF8 // XXX not a reasonable place to check it.
199 if (!_utf8)
200 ttmode.c_iflag &= ~IUTF8;
201 else
202 ttmode.c_iflag |= IUTF8;
203 #endif
205 if (_eraseChar != 0)
206 ttmode.c_cc[VERASE] = _eraseChar;
208 if (!pty()->tcSetAttr(&ttmode))
209 kWarning() << "Unable to set terminal attributes.";
211 pty()->setWinSize(_windowLines, _windowColumns);
213 KProcess::start();
215 if (!waitForStarted())
216 return -1;
218 return 0;
221 void Pty::setWriteable(bool writeable)
223 KDE_struct_stat sbuf;
224 KDE_stat(pty()->ttyName(), &sbuf);
225 if (writeable)
226 chmod(pty()->ttyName(), sbuf.st_mode | S_IWGRP);
227 else
228 chmod(pty()->ttyName(), sbuf.st_mode & ~(S_IWGRP|S_IWOTH));
231 Pty::Pty(int masterFd, QObject* parent)
232 : KPtyProcess(masterFd,parent)
234 init();
236 Pty::Pty(QObject* parent)
237 : KPtyProcess(parent)
239 init();
241 void Pty::init()
243 _windowColumns = 0;
244 _windowLines = 0;
245 _eraseChar = 0;
246 _xonXoff = true;
247 _utf8 =true;
249 connect(pty(), SIGNAL(readyRead()) , this , SLOT(dataReceived()));
250 setPtyChannels(KPtyProcess::AllChannels);
253 Pty::~Pty()
257 void Pty::sendData(const char* data, int length)
259 if (!length)
260 return;
262 if (!pty()->write(data,length))
264 kWarning() << "Pty::doSendJobs - Could not send input data to terminal process.";
265 return;
269 void Pty::dataReceived()
271 QByteArray data = pty()->readAll();
272 emit receivedData(data.constData(),data.count());
275 void Pty::lockPty(bool lock)
277 #ifdef __GNUC__
278 #warning "TODO: Support for locking the Pty"
279 #endif
280 //if (lock)
281 //suspend();
282 //else
283 //resume();
286 int Pty::foregroundProcessGroup() const
288 int pid = tcgetpgrp(pty()->masterFd());
290 if ( pid != -1 )
292 return pid;
295 return 0;
298 void Pty::setupChildProcess()
300 KPtyProcess::setupChildProcess();
302 // reset all signal handlers
303 // this ensures that terminal applications respond to
304 // signals generated via key sequences such as Ctrl+C
305 // (which sends SIGINT)
306 struct sigaction action;
307 sigemptyset(&action.sa_mask);
308 action.sa_handler = SIG_DFL;
309 action.sa_flags = 0;
310 for (int signal=1;signal < NSIG; signal++)
311 sigaction(signal,&action,0L);
315 #include "Pty.moc"