• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
Emulation.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2007-2008 Robert Knight <robertknight@gmail.com>
3  Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
4  Copyright 1996 by Matthias Ettrich <ettrich@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20 */
21 
22 // Own
23 #include "Emulation.h"
24 
25 // Qt
26 #include <QtGui/QKeyEvent>
27 
28 // Konsole
29 #include "KeyboardTranslator.h"
30 #include "KeyboardTranslatorManager.h"
31 #include "Screen.h"
32 #include "ScreenWindow.h"
33 
34 using namespace Konsole;
35 
36 Emulation::Emulation() :
37  _currentScreen(0),
38  _codec(0),
39  _decoder(0),
40  _keyTranslator(0),
41  _usesMouse(false),
42  _bracketedPasteMode(false),
43  _imageSizeInitialized(false)
44 {
45  // create screens with a default size
46  _screen[0] = new Screen(40, 80);
47  _screen[1] = new Screen(40, 80);
48  _currentScreen = _screen[0];
49 
50  QObject::connect(&_bulkTimer1, SIGNAL(timeout()), this, SLOT(showBulk()));
51  QObject::connect(&_bulkTimer2, SIGNAL(timeout()), this, SLOT(showBulk()));
52 
53  // listen for mouse status changes
54  connect(this , SIGNAL(programUsesMouseChanged(bool)) ,
55  SLOT(usesMouseChanged(bool)));
56  connect(this , SIGNAL(programBracketedPasteModeChanged(bool)) ,
57  SLOT(bracketedPasteModeChanged(bool)));
58 }
59 
60 bool Emulation::programUsesMouse() const
61 {
62  return _usesMouse;
63 }
64 
65 void Emulation::usesMouseChanged(bool usesMouse)
66 {
67  _usesMouse = usesMouse;
68 }
69 
70 bool Emulation::programBracketedPasteMode() const
71 {
72  return _bracketedPasteMode;
73 }
74 
75 void Emulation::bracketedPasteModeChanged(bool bracketedPasteMode)
76 {
77  _bracketedPasteMode = bracketedPasteMode;
78 }
79 
80 ScreenWindow* Emulation::createWindow()
81 {
82  ScreenWindow* window = new ScreenWindow(_currentScreen);
83  _windows << window;
84 
85  connect(window , SIGNAL(selectionChanged()),
86  this , SLOT(bufferedUpdate()));
87  connect(window, SIGNAL(selectionChanged()),
88  this, SLOT(checkSelectedText()));
89 
90  connect(this , SIGNAL(outputChanged()),
91  window , SLOT(notifyOutputChanged()));
92 
93  return window;
94 }
95 
96 void Emulation::checkScreenInUse()
97 {
98  emit primaryScreenInUse(_currentScreen == _screen[0]);
99 }
100 
101 void Emulation::checkSelectedText()
102 {
103  QString text = _currentScreen->selectedText(true);
104  emit selectionChanged(text);
105 }
106 
107 Emulation::~Emulation()
108 {
109  foreach(ScreenWindow* window, _windows) {
110  delete window;
111  }
112 
113  delete _screen[0];
114  delete _screen[1];
115  delete _decoder;
116 }
117 
118 void Emulation::setScreen(int index)
119 {
120  Screen* oldScreen = _currentScreen;
121  _currentScreen = _screen[index & 1];
122  if (_currentScreen != oldScreen) {
123  // tell all windows onto this emulation to switch to the newly active screen
124  foreach(ScreenWindow * window, _windows) {
125  window->setScreen(_currentScreen);
126  }
127 
128  checkScreenInUse();
129  checkSelectedText();
130  }
131 }
132 
133 void Emulation::clearHistory()
134 {
135  _screen[0]->setScroll(_screen[0]->getScroll() , false);
136 }
137 void Emulation::setHistory(const HistoryType& history)
138 {
139  _screen[0]->setScroll(history);
140 
141  showBulk();
142 }
143 
144 const HistoryType& Emulation::history() const
145 {
146  return _screen[0]->getScroll();
147 }
148 
149 void Emulation::setCodec(const QTextCodec * codec)
150 {
151  if (codec) {
152  _codec = codec;
153 
154  delete _decoder;
155  _decoder = _codec->makeDecoder();
156 
157  emit useUtf8Request(utf8());
158  } else {
159  setCodec(LocaleCodec);
160  }
161 }
162 
163 void Emulation::setCodec(EmulationCodec codec)
164 {
165  if (codec == Utf8Codec)
166  setCodec(QTextCodec::codecForName("utf8"));
167  else if (codec == LocaleCodec)
168  setCodec(QTextCodec::codecForLocale());
169 }
170 
171 void Emulation::setKeyBindings(const QString& name)
172 {
173  _keyTranslator = KeyboardTranslatorManager::instance()->findTranslator(name);
174  if (!_keyTranslator) {
175  _keyTranslator = KeyboardTranslatorManager::instance()->defaultTranslator();
176  }
177 }
178 
179 QString Emulation::keyBindings() const
180 {
181  return _keyTranslator->name();
182 }
183 
184 // process application unicode input to terminal
185 // this is a trivial scanner
186 void Emulation::receiveChar(int c)
187 {
188  c &= 0xff;
189  switch (c) {
190  case '\b' : _currentScreen->backspace(); break;
191  case '\t' : _currentScreen->tab(); break;
192  case '\n' : _currentScreen->newLine(); break;
193  case '\r' : _currentScreen->toStartOfLine(); break;
194  case 0x07 : emit stateSet(NOTIFYBELL); break;
195  default : _currentScreen->displayCharacter(c); break;
196  }
197 }
198 
199 void Emulation::sendKeyEvent(QKeyEvent* ev)
200 {
201  emit stateSet(NOTIFYNORMAL);
202 
203  if (!ev->text().isEmpty()) {
204  // A block of text
205  // Note that the text is proper unicode.
206  // We should do a conversion here
207  emit sendData(ev->text().toUtf8(), ev->text().length());
208  }
209 }
210 
211 void Emulation::sendString(const char*, int)
212 {
213  // default implementation does nothing
214 }
215 
216 void Emulation::sendMouseEvent(int /*buttons*/, int /*column*/, int /*row*/, int /*eventType*/)
217 {
218  // default implementation does nothing
219 }
220 
221 /*
222  We are doing code conversion from locale to unicode first.
223 */
224 
225 void Emulation::receiveData(const char* text, int length)
226 {
227  emit stateSet(NOTIFYACTIVITY);
228 
229  bufferedUpdate();
230 
231  QString unicodeText = _decoder->toUnicode(text, length);
232 
233  //send characters to terminal emulator
234  for (int i = 0; i < unicodeText.length(); i++)
235  receiveChar(unicodeText[i].unicode());
236 
237  //look for z-modem indicator
238  //-- someone who understands more about z-modems that I do may be able to move
239  //this check into the above for loop?
240  for (int i = 0; i < length; i++) {
241  if (text[i] == '\030') {
242  if ((length - i - 1 > 3) && (qstrncmp(text + i + 1, "B00", 3) == 0))
243  emit zmodemDetected();
244  }
245  }
246 }
247 
248 //OLDER VERSION
249 //This version of onRcvBlock was commented out because
250 // a) It decoded incoming characters one-by-one, which is slow in the current version of Qt (4.2 tech preview)
251 // b) It messed up decoding of non-ASCII characters, with the result that (for example) chinese characters
252 // were not printed properly.
253 //
254 //There is something about stopping the _decoder if "we get a control code halfway a multi-byte sequence" (see below)
255 //which hasn't been ported into the newer function (above). Hopefully someone who understands this better
256 //can find an alternative way of handling the check.
257 
258 /*void Emulation::onRcvBlock(const char *s, int len)
259 {
260  emit notifySessionState(NOTIFYACTIVITY);
261 
262  bufferedUpdate();
263  for (int i = 0; i < len; i++)
264  {
265 
266  QString result = _decoder->toUnicode(&s[i],1);
267  int reslen = result.length();
268 
269  // If we get a control code halfway a multi-byte sequence
270  // we flush the _decoder and continue with the control code.
271  if ((s[i] < 32) && (s[i] > 0))
272  {
273  // Flush _decoder
274  while(!result.length())
275  result = _decoder->toUnicode(&s[i],1);
276  reslen = 1;
277  result.resize(reslen);
278  result[0] = QChar(s[i]);
279  }
280 
281  for (int j = 0; j < reslen; j++)
282  {
283  if (result[j].characterategory() == QChar::Mark_NonSpacing)
284  _currentScreen->compose(result.mid(j,1));
285  else
286  onRcvChar(result[j].unicode());
287  }
288  if (s[i] == '\030')
289  {
290  if ((len-i-1 > 3) && (qstrncmp(s+i+1, "B00", 3) == 0))
291  emit zmodemDetected();
292  }
293  }
294 }*/
295 
296 void Emulation::writeToStream(TerminalCharacterDecoder* decoder ,
297  int startLine ,
298  int endLine)
299 {
300  _currentScreen->writeLinesToStream(decoder, startLine, endLine);
301 }
302 
303 int Emulation::lineCount() const
304 {
305  // sum number of lines currently on _screen plus number of lines in history
306  return _currentScreen->getLines() + _currentScreen->getHistLines();
307 }
308 
309 void Emulation::showBulk()
310 {
311  _bulkTimer1.stop();
312  _bulkTimer2.stop();
313 
314  emit outputChanged();
315 
316  _currentScreen->resetScrolledLines();
317  _currentScreen->resetDroppedLines();
318 }
319 
320 void Emulation::bufferedUpdate()
321 {
322  static const int BULK_TIMEOUT1 = 10;
323  static const int BULK_TIMEOUT2 = 40;
324 
325  _bulkTimer1.setSingleShot(true);
326  _bulkTimer1.start(BULK_TIMEOUT1);
327  if (!_bulkTimer2.isActive()) {
328  _bulkTimer2.setSingleShot(true);
329  _bulkTimer2.start(BULK_TIMEOUT2);
330  }
331 }
332 
333 char Emulation::eraseChar() const
334 {
335  return '\b';
336 }
337 
338 void Emulation::setImageSize(int lines, int columns)
339 {
340  if ((lines < 1) || (columns < 1))
341  return;
342 
343  QSize screenSize[2] = { QSize(_screen[0]->getColumns(),
344  _screen[0]->getLines()),
345  QSize(_screen[1]->getColumns(),
346  _screen[1]->getLines())
347  };
348  QSize newSize(columns, lines);
349 
350  if (newSize == screenSize[0] && newSize == screenSize[1]) {
351  // If this method is called for the first time, always emit
352  // SIGNAL(imageSizeChange()), even if the new size is the same as the
353  // current size. See #176902
354  if (!_imageSizeInitialized) {
355  emit imageSizeChanged(lines, columns);
356  }
357  } else {
358  _screen[0]->resizeImage(lines, columns);
359  _screen[1]->resizeImage(lines, columns);
360 
361  emit imageSizeChanged(lines, columns);
362 
363  bufferedUpdate();
364  }
365 
366  if (!_imageSizeInitialized) {
367  _imageSizeInitialized = true;
368 
369  // FIXME
370  // a hard-coded, small delay is introduced to guarantee Session::run()
371  // does not get triggered by SIGNAL(imageSizeInitialized()) before
372  // Pty::setWindowSize() is triggered by previously emitted
373  // SIGNAL(imageSizeChanged()); See #203185
374  QTimer::singleShot(200, this, SIGNAL(imageSizeInitialized()));
375  }
376 }
377 
378 QSize Emulation::imageSize() const
379 {
380  return QSize(_currentScreen->getColumns(), _currentScreen->getLines());
381 }
382 
383 #include "Emulation.moc"
384 
Konsole::Emulation::setHistory
void setHistory(const HistoryType &)
Sets the history store used by this emulation.
Definition: Emulation.cpp:137
Konsole::Screen::writeLinesToStream
void writeLinesToStream(TerminalCharacterDecoder *decoder, int fromLine, int toLine) const
Copies part of the output to a stream.
Definition: Screen.cpp:1290
Konsole::Emulation::imageSizeInitialized
void imageSizeInitialized()
Emitted when the setImageSize() is called on this emulation for the first time.
Konsole::Emulation::programBracketedPasteModeChanged
void programBracketedPasteModeChanged(bool bracketedPasteMode)
Konsole::Emulation::setImageSize
virtual void setImageSize(int lines, int columns)
Change the size of the emulation's image.
Definition: Emulation.cpp:338
Konsole::Screen::selectedText
QString selectedText(bool preserveLineBreaks, bool trimTrailingSpaces=false) const
Convenience method.
Definition: Screen.cpp:1110
Konsole::Emulation::zmodemDetected
void zmodemDetected()
Emitted when the special sequence indicating the request for data transmission through ZModem protoco...
Konsole::Screen::getLines
int getLines() const
Return the number of lines.
Definition: Screen.h:382
Konsole::Emulation::_currentScreen
Screen * _currentScreen
Definition: Emulation.h:445
Konsole::Emulation::sendData
void sendData(const char *data, int len)
Emitted when a buffer of data is ready to send to the standard input of the terminal.
KeyboardTranslatorManager.h
Emulation.h
Konsole::Emulation::imageSize
QSize imageSize() const
Returns the size of the screen image which the emulation produces.
Definition: Emulation.cpp:378
Screen.h
Konsole::Screen::getScroll
const HistoryType & getScroll() const
Returns the type of storage used to keep lines in the history.
Definition: Screen.cpp:1371
Konsole::Emulation::Utf8Codec
Definition: Emulation.h:438
Konsole::Screen::getColumns
int getColumns() const
Return the number of columns.
Definition: Screen.h:386
Konsole::Emulation::eraseChar
virtual char eraseChar() const
Returns the special character used for erasing character.
Definition: Emulation.cpp:333
Konsole::Emulation::primaryScreenInUse
void primaryScreenInUse(bool use)
Emitted when the active screen is switched, to indicate whether the primary screen is in use...
Konsole::Emulation::setCodec
void setCodec(const QTextCodec *)
Sets the codec used to decode incoming characters.
Definition: Emulation.cpp:149
QTextCodec::codecForLocale
QTextCodec * codecForLocale()
Konsole::Emulation::_keyTranslator
const KeyboardTranslator * _keyTranslator
Definition: Emulation.h:458
Konsole::Emulation::writeToStream
virtual void writeToStream(TerminalCharacterDecoder *decoder, int startLine, int endLine)
Copies the output history from startLine to endLine into stream, using decoder to convert the termina...
Definition: Emulation.cpp:296
Konsole::Emulation::LocaleCodec
Definition: Emulation.h:437
Konsole::Emulation::programBracketedPasteMode
bool programBracketedPasteMode() const
Definition: Emulation.cpp:70
Konsole::Emulation::bufferedUpdate
void bufferedUpdate()
Schedules an update of attached views.
Definition: Emulation.cpp:320
Konsole::Emulation::Emulation
Emulation()
Constructs a new terminal emulation.
Definition: Emulation.cpp:36
Konsole::NOTIFYACTIVITY
The emulation is currently receiving data from its terminal input.
Definition: Emulation.h:62
Konsole::Emulation::sendMouseEvent
virtual void sendMouseEvent(int buttons, int column, int line, int eventType)
Converts information about a mouse event into an xterm-compatible escape sequence and emits the chara...
Definition: Emulation.cpp:216
Konsole::Emulation::programUsesMouseChanged
void programUsesMouseChanged(bool usesMouse)
This is emitted when the program running in the shell indicates whether or not it is interested in mo...
Konsole::Screen::resetScrolledLines
void resetScrolledLines()
Resets the count of the number of lines that the image has been scrolled up or down by...
Definition: Screen.cpp:749
Konsole::TerminalCharacterDecoder
Base class for terminal character decoders.
Definition: TerminalCharacterDecoder.h:45
Konsole::Emulation::setScreen
void setScreen(int index)
Sets the active screen.
Definition: Emulation.cpp:118
KeyboardTranslator.h
Konsole::Emulation::outputChanged
void outputChanged()
Emitted when the contents of the screen image change.
Konsole::Screen::displayCharacter
void displayCharacter(unsigned short c)
Displays a new character at the current cursor position.
Definition: Screen.cpp:629
Konsole::Screen::setScroll
void setScroll(const HistoryType &, bool copyPreviousScroll=true)
Sets the type of storage used to keep lines in the history.
Definition: Screen.cpp:1353
Konsole::Emulation::EmulationCodec
EmulationCodec
Definition: Emulation.h:436
Konsole::Emulation::sendString
virtual void sendString(const char *string, int length=-1)=0
Sends a string of characters to the foreground terminal process.
Definition: Emulation.cpp:211
Konsole::Emulation::checkScreenInUse
void checkScreenInUse()
Definition: Emulation.cpp:96
Konsole::ScreenWindow
Provides a window onto a section of a terminal screen.
Definition: ScreenWindow.h:52
QString::isEmpty
bool isEmpty() const
Konsole::Screen
An image of characters with associated attributes.
Definition: Screen.h:74
Konsole::Emulation::keyBindings
QString keyBindings() const
Returns the name of the emulation's current key bindings.
Definition: Emulation.cpp:179
Konsole::Screen::newLine
void newLine()
Moves the cursor down one line, if the MODE_NewLine mode flag is enabled then the cursor is returned ...
Definition: Screen.cpp:611
QKeyEvent::text
QString text() const
Konsole::Screen::toStartOfLine
void toStartOfLine()
Moves the cursor to the beginning of the current line.
Definition: Screen.cpp:825
Konsole::NOTIFYBELL
The terminal program has triggered a bell event to get the user's attention.
Definition: Emulation.h:57
Konsole::Emulation::programUsesMouse
bool programUsesMouse() const
Returns true if the active terminal program wants mouse input events.
Definition: Emulation.cpp:60
Konsole::NOTIFYNORMAL
The emulation is currently receiving user input.
Definition: Emulation.h:52
Konsole::Emulation::createWindow
ScreenWindow * createWindow()
Creates a new window onto the output from this emulation.
Definition: Emulation.cpp:80
QTextDecoder::toUnicode
QString toUnicode(const char *chars, int len)
Konsole::Emulation::~Emulation
~Emulation()
Definition: Emulation.cpp:107
QString
QTextCodec
Konsole::Emulation::lineCount
int lineCount() const
Returns the total number of lines, including those stored in the history.
Definition: Emulation.cpp:303
QSize
QTimer::stop
void stop()
Konsole::KeyboardTranslatorManager::defaultTranslator
const KeyboardTranslator * defaultTranslator()
Returns the default translator for Konsole.
Definition: KeyboardTranslatorManager.cpp:177
Konsole::Emulation::_codec
const QTextCodec * _codec
Definition: Emulation.h:456
Konsole::HistoryType
Definition: History.h:319
QKeyEvent
Konsole::KeyboardTranslator::name
QString name() const
Returns the name of this keyboard translator.
Definition: KeyboardTranslator.cpp:651
Konsole::Emulation::receiveChar
virtual void receiveChar(int ch)
Processes an incoming character.
Definition: Emulation.cpp:186
Konsole::Screen::backspace
void backspace()
Moves the cursor one column to the left and erases the character at the new cursor position...
Definition: Screen.cpp:547
Konsole::Emulation::selectionChanged
void selectionChanged(const QString &text)
Emitted when the text selection is changed.
QTextCodec::makeDecoder
QTextDecoder * makeDecoder() const
Konsole::Screen::resizeImage
void resizeImage(int new_lines, int new_columns)
Resizes the image to a new fixed size of new_lines by new_columns.
Definition: Screen.cpp:298
Konsole::Emulation::clearHistory
void clearHistory()
Clears the history scroll.
Definition: Emulation.cpp:133
QTextCodec::codecForName
QTextCodec * codecForName(const QByteArray &name)
Konsole::KeyboardTranslatorManager::instance
static KeyboardTranslatorManager * instance()
Returns the global KeyboardTranslatorManager instance.
Definition: KeyboardTranslatorManager.cpp:49
QString::length
int length() const
Konsole::Screen::resetDroppedLines
void resetDroppedLines()
Resets the count of the number of lines dropped from the history.
Definition: Screen.cpp:745
Konsole::Emulation::receiveData
void receiveData(const char *buffer, int len)
Processes an incoming stream of characters.
Definition: Emulation.cpp:225
Konsole::Emulation::checkSelectedText
void checkSelectedText()
Definition: Emulation.cpp:101
Konsole::Emulation::setKeyBindings
void setKeyBindings(const QString &name)
Sets the key bindings used to key events ( received through sendKeyEvent() ) into character streams t...
Definition: Emulation.cpp:171
QTimer::start
void start(int msec)
Konsole::Emulation::stateSet
void stateSet(int state)
Emitted when the activity state of the emulation is set.
Konsole::Emulation::codec
const QTextCodec * codec() const
Returns the codec used to decode incoming characters.
Definition: Emulation.h:169
Konsole::Emulation::_screen
Screen * _screen[2]
Definition: Emulation.h:448
Konsole::Emulation::_windows
QList< ScreenWindow * > _windows
Definition: Emulation.h:443
Konsole::Emulation::history
const HistoryType & history() const
Returns the history store used by this emulation.
Definition: Emulation.cpp:144
Konsole::Screen::getHistLines
int getHistLines() const
Return the number of lines in the history buffer.
Definition: Screen.cpp:1348
Konsole::ScreenWindow::setScreen
void setScreen(Screen *screen)
Sets the screen which this window looks onto.
Definition: ScreenWindow.cpp:46
QTimer::isActive
bool isActive() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
ScreenWindow.h
Konsole::Emulation::utf8
bool utf8() const
Convenience method.
Definition: Emulation.h:180
Konsole::Emulation::sendKeyEvent
virtual void sendKeyEvent(QKeyEvent *)
Interprets a key press event and emits the sendData() signal with the resulting character stream...
Definition: Emulation.cpp:199
Konsole::Emulation::_decoder
QTextDecoder * _decoder
Definition: Emulation.h:457
Konsole::Screen::tab
void tab(int n=1)
Moves the cursor n tab-stops to the right.
Definition: Screen.cpp:561
Konsole::Emulation::imageSizeChanged
void imageSizeChanged(int lineCount, int columnCount)
Emitted when the terminal emulator's size has changed.
Konsole::Emulation::useUtf8Request
void useUtf8Request(bool)
Requests that the pty used by the terminal process be set to UTF 8 mode.
QTimer::setSingleShot
void setSingleShot(bool singleShot)
Konsole::KeyboardTranslatorManager::findTranslator
const KeyboardTranslator * findTranslator(const QString &name)
Returns the keyboard translator with the given name or 0 if no translator with that name exists...
Definition: KeyboardTranslatorManager.cpp:102
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal