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

Konsole

  • sources
  • kde-4.12
  • 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  _imageSizeInitialized(false)
43 {
44  // create screens with a default size
45  _screen[0] = new Screen(40, 80);
46  _screen[1] = new Screen(40, 80);
47  _currentScreen = _screen[0];
48 
49  QObject::connect(&_bulkTimer1, SIGNAL(timeout()), this, SLOT(showBulk()));
50  QObject::connect(&_bulkTimer2, SIGNAL(timeout()), this, SLOT(showBulk()));
51 
52  // listen for mouse status changes
53  connect(this , SIGNAL(programUsesMouseChanged(bool)) ,
54  SLOT(usesMouseChanged(bool)));
55 }
56 
57 bool Emulation::programUsesMouse() const
58 {
59  return _usesMouse;
60 }
61 
62 void Emulation::usesMouseChanged(bool usesMouse)
63 {
64  _usesMouse = usesMouse;
65 }
66 
67 ScreenWindow* Emulation::createWindow()
68 {
69  ScreenWindow* window = new ScreenWindow();
70  window->setScreen(_currentScreen);
71  _windows << window;
72 
73  connect(window , SIGNAL(selectionChanged()),
74  this , SLOT(bufferedUpdate()));
75  connect(window, SIGNAL(selectionChanged()),
76  this, SLOT(checkSelectedText()));
77 
78  connect(this , SIGNAL(outputChanged()),
79  window , SLOT(notifyOutputChanged()));
80 
81  return window;
82 }
83 
84 void Emulation::checkScreenInUse()
85 {
86  emit primaryScreenInUse(_currentScreen == _screen[0]);
87 }
88 
89 void Emulation::checkSelectedText()
90 {
91  QString text = _currentScreen->selectedText(true);
92  emit selectionChanged(text);
93 }
94 
95 Emulation::~Emulation()
96 {
97  foreach(ScreenWindow* window, _windows) {
98  delete window;
99  }
100 
101  delete _screen[0];
102  delete _screen[1];
103  delete _decoder;
104 }
105 
106 void Emulation::setScreen(int index)
107 {
108  Screen* oldScreen = _currentScreen;
109  _currentScreen = _screen[index & 1];
110  if (_currentScreen != oldScreen) {
111  // tell all windows onto this emulation to switch to the newly active screen
112  foreach(ScreenWindow * window, _windows) {
113  window->setScreen(_currentScreen);
114  }
115 
116  checkScreenInUse();
117  checkSelectedText();
118  }
119 }
120 
121 void Emulation::clearHistory()
122 {
123  _screen[0]->setScroll(_screen[0]->getScroll() , false);
124 }
125 void Emulation::setHistory(const HistoryType& history)
126 {
127  _screen[0]->setScroll(history);
128 
129  showBulk();
130 }
131 
132 const HistoryType& Emulation::history() const
133 {
134  return _screen[0]->getScroll();
135 }
136 
137 void Emulation::setCodec(const QTextCodec * codec)
138 {
139  if (codec) {
140  _codec = codec;
141 
142  delete _decoder;
143  _decoder = _codec->makeDecoder();
144 
145  emit useUtf8Request(utf8());
146  } else {
147  setCodec(LocaleCodec);
148  }
149 }
150 
151 void Emulation::setCodec(EmulationCodec codec)
152 {
153  if (codec == Utf8Codec)
154  setCodec(QTextCodec::codecForName("utf8"));
155  else if (codec == LocaleCodec)
156  setCodec(QTextCodec::codecForLocale());
157 }
158 
159 void Emulation::setKeyBindings(const QString& name)
160 {
161  _keyTranslator = KeyboardTranslatorManager::instance()->findTranslator(name);
162  if (!_keyTranslator) {
163  _keyTranslator = KeyboardTranslatorManager::instance()->defaultTranslator();
164  }
165 }
166 
167 QString Emulation::keyBindings() const
168 {
169  return _keyTranslator->name();
170 }
171 
172 // process application unicode input to terminal
173 // this is a trivial scanner
174 void Emulation::receiveChar(int c)
175 {
176  c &= 0xff;
177  switch (c) {
178  case '\b' : _currentScreen->backspace(); break;
179  case '\t' : _currentScreen->tab(); break;
180  case '\n' : _currentScreen->newLine(); break;
181  case '\r' : _currentScreen->toStartOfLine(); break;
182  case 0x07 : emit stateSet(NOTIFYBELL); break;
183  default : _currentScreen->displayCharacter(c); break;
184  }
185 }
186 
187 void Emulation::sendKeyEvent(QKeyEvent* ev)
188 {
189  emit stateSet(NOTIFYNORMAL);
190 
191  if (!ev->text().isEmpty()) {
192  // A block of text
193  // Note that the text is proper unicode.
194  // We should do a conversion here
195  emit sendData(ev->text().toUtf8(), ev->text().length());
196  }
197 }
198 
199 void Emulation::sendString(const char*, int)
200 {
201  // default implementation does nothing
202 }
203 
204 void Emulation::sendMouseEvent(int /*buttons*/, int /*column*/, int /*row*/, int /*eventType*/)
205 {
206  // default implementation does nothing
207 }
208 
209 /*
210  We are doing code conversion from locale to unicode first.
211 */
212 
213 void Emulation::receiveData(const char* text, int length)
214 {
215  emit stateSet(NOTIFYACTIVITY);
216 
217  bufferedUpdate();
218 
219  QString unicodeText = _decoder->toUnicode(text, length);
220 
221  //send characters to terminal emulator
222  for (int i = 0; i < unicodeText.length(); i++)
223  receiveChar(unicodeText[i].unicode());
224 
225  //look for z-modem indicator
226  //-- someone who understands more about z-modems that I do may be able to move
227  //this check into the above for loop?
228  for (int i = 0; i < length; i++) {
229  if (text[i] == '\030') {
230  if ((length - i - 1 > 3) && (qstrncmp(text + i + 1, "B00", 3) == 0))
231  emit zmodemDetected();
232  }
233  }
234 }
235 
236 //OLDER VERSION
237 //This version of onRcvBlock was commented out because
238 // a) It decoded incoming characters one-by-one, which is slow in the current version of Qt (4.2 tech preview)
239 // b) It messed up decoding of non-ASCII characters, with the result that (for example) chinese characters
240 // were not printed properly.
241 //
242 //There is something about stopping the _decoder if "we get a control code halfway a multi-byte sequence" (see below)
243 //which hasn't been ported into the newer function (above). Hopefully someone who understands this better
244 //can find an alternative way of handling the check.
245 
246 /*void Emulation::onRcvBlock(const char *s, int len)
247 {
248  emit notifySessionState(NOTIFYACTIVITY);
249 
250  bufferedUpdate();
251  for (int i = 0; i < len; i++)
252  {
253 
254  QString result = _decoder->toUnicode(&s[i],1);
255  int reslen = result.length();
256 
257  // If we get a control code halfway a multi-byte sequence
258  // we flush the _decoder and continue with the control code.
259  if ((s[i] < 32) && (s[i] > 0))
260  {
261  // Flush _decoder
262  while(!result.length())
263  result = _decoder->toUnicode(&s[i],1);
264  reslen = 1;
265  result.resize(reslen);
266  result[0] = QChar(s[i]);
267  }
268 
269  for (int j = 0; j < reslen; j++)
270  {
271  if (result[j].characterategory() == QChar::Mark_NonSpacing)
272  _currentScreen->compose(result.mid(j,1));
273  else
274  onRcvChar(result[j].unicode());
275  }
276  if (s[i] == '\030')
277  {
278  if ((len-i-1 > 3) && (qstrncmp(s+i+1, "B00", 3) == 0))
279  emit zmodemDetected();
280  }
281  }
282 }*/
283 
284 void Emulation::writeToStream(TerminalCharacterDecoder* decoder ,
285  int startLine ,
286  int endLine)
287 {
288  _currentScreen->writeLinesToStream(decoder, startLine, endLine);
289 }
290 
291 int Emulation::lineCount() const
292 {
293  // sum number of lines currently on _screen plus number of lines in history
294  return _currentScreen->getLines() + _currentScreen->getHistLines();
295 }
296 
297 void Emulation::showBulk()
298 {
299  _bulkTimer1.stop();
300  _bulkTimer2.stop();
301 
302  emit outputChanged();
303 
304  _currentScreen->resetScrolledLines();
305  _currentScreen->resetDroppedLines();
306 }
307 
308 void Emulation::bufferedUpdate()
309 {
310  static const int BULK_TIMEOUT1 = 10;
311  static const int BULK_TIMEOUT2 = 40;
312 
313  _bulkTimer1.setSingleShot(true);
314  _bulkTimer1.start(BULK_TIMEOUT1);
315  if (!_bulkTimer2.isActive()) {
316  _bulkTimer2.setSingleShot(true);
317  _bulkTimer2.start(BULK_TIMEOUT2);
318  }
319 }
320 
321 char Emulation::eraseChar() const
322 {
323  return '\b';
324 }
325 
326 void Emulation::setImageSize(int lines, int columns)
327 {
328  if ((lines < 1) || (columns < 1))
329  return;
330 
331  QSize screenSize[2] = { QSize(_screen[0]->getColumns(),
332  _screen[0]->getLines()),
333  QSize(_screen[1]->getColumns(),
334  _screen[1]->getLines())
335  };
336  QSize newSize(columns, lines);
337 
338  if (newSize == screenSize[0] && newSize == screenSize[1]) {
339  // If this method is called for the first time, always emit
340  // SIGNAL(imageSizeChange()), even if the new size is the same as the
341  // current size. See #176902
342  if (!_imageSizeInitialized) {
343  emit imageSizeChanged(lines, columns);
344  }
345  } else {
346  _screen[0]->resizeImage(lines, columns);
347  _screen[1]->resizeImage(lines, columns);
348 
349  emit imageSizeChanged(lines, columns);
350 
351  bufferedUpdate();
352  }
353 
354  if (!_imageSizeInitialized) {
355  _imageSizeInitialized = true;
356 
357  // FIXME
358  // a hard-coded, small delay is introduced to guarantee Session::run()
359  // does not get triggered by SIGNAL(imageSizeInitialized()) before
360  // Pty::setWindowSize() is triggered by previously emitted
361  // SIGNAL(imageSizeChanged()); See #203185
362  QTimer::singleShot(200, this, SIGNAL(imageSizeInitialized()));
363  }
364 }
365 
366 QSize Emulation::imageSize() const
367 {
368  return QSize(_currentScreen->getColumns(), _currentScreen->getLines());
369 }
370 
371 #include "Emulation.moc"
372 
Konsole::Emulation::setHistory
void setHistory(const HistoryType &)
Sets the history store used by this emulation.
Definition: Emulation.cpp:125
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::setImageSize
virtual void setImageSize(int lines, int columns)
Change the size of the emulation's image.
Definition: Emulation.cpp:326
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:441
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:366
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:434
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:321
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:137
Konsole::Emulation::_keyTranslator
const KeyboardTranslator * _keyTranslator
Definition: Emulation.h:454
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:284
Konsole::Emulation::LocaleCodec
Definition: Emulation.h:433
Konsole::Emulation::bufferedUpdate
void bufferedUpdate()
Schedules an update of attached views.
Definition: Emulation.cpp:308
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:204
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:106
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:432
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:199
Konsole::Emulation::checkScreenInUse
void checkScreenInUse()
Definition: Emulation.cpp:84
Konsole::ScreenWindow
Provides a window onto a section of a terminal screen.
Definition: ScreenWindow.h:52
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:167
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
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:57
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:67
Konsole::Emulation::~Emulation
~Emulation()
Definition: Emulation.cpp:95
Konsole::Emulation::lineCount
int lineCount() const
Returns the total number of lines, including those stored in the history.
Definition: Emulation.cpp:291
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:452
Konsole::HistoryType
Definition: History.h:319
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:174
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.
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:121
Konsole::KeyboardTranslatorManager::instance
static KeyboardTranslatorManager * instance()
Returns the global KeyboardTranslatorManager instance.
Definition: KeyboardTranslatorManager.cpp:49
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:213
Konsole::Emulation::checkSelectedText
void checkSelectedText()
Definition: Emulation.cpp:89
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:159
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:444
Konsole::Emulation::_windows
QList< ScreenWindow * > _windows
Definition: Emulation.h:439
Konsole::Emulation::history
const HistoryType & history() const
Returns the history store used by this emulation.
Definition: Emulation.cpp:132
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:45
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:187
Konsole::Emulation::_decoder
QTextDecoder * _decoder
Definition: Emulation.h:453
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.
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:31:24 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
  • Applications
  •   Libraries
  •     libkonq
  • 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