• 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
History.h
Go to the documentation of this file.
1 /*
2  This file is part of Konsole, an X terminal.
3  Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
4 
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.
9 
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.
14 
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.
19 */
20 
21 #ifndef HISTORY_H
22 #define HISTORY_H
23 
24 // System
25 #include <sys/mman.h>
26 
27 // Qt
28 #include <QtCore/QList>
29 #include <QtCore/QVector>
30 #include <QtCore/QTemporaryFile>
31 
32 #include "konsole_export.h"
33 
34 // Konsole
35 #include "Character.h"
36 
37 namespace Konsole
38 {
39 /*
40  An extendable tmpfile(1) based buffer.
41 */
42 
43 class HistoryFile
44 {
45 public:
46  HistoryFile();
47  virtual ~HistoryFile();
48 
49  virtual void add(const unsigned char* bytes, int len);
50  virtual void get(unsigned char* bytes, int len, int loc);
51  virtual int len() const;
52 
53  //mmaps the file in read-only mode
54  void map();
55  //un-mmaps the file
56  void unmap();
57  //returns true if the file is mmap'ed
58  bool isMapped() const;
59 
60 
61 private:
62  int _fd;
63  int _length;
64  QTemporaryFile _tmpFile;
65 
66  //pointer to start of mmap'ed file data, or 0 if the file is not mmap'ed
67  char* _fileMap;
68 
69  //incremented whenever 'add' is called and decremented whenever
70  //'get' is called.
71  //this is used to detect when a large number of lines are being read and processed from the history
72  //and automatically mmap the file for better performance (saves the overhead of many lseek-read calls).
73  int _readWriteBalance;
74 
75  //when _readWriteBalance goes below this threshold, the file will be mmap'ed automatically
76  static const int MAP_THRESHOLD = -1000;
77 };
78 
80 
82 // Abstract base class for file and buffer versions
84 class HistoryType;
85 
86 class HistoryScroll
87 {
88 public:
89  explicit HistoryScroll(HistoryType*);
90  virtual ~HistoryScroll();
91 
92  virtual bool hasScroll();
93 
94  // access to history
95  virtual int getLines() = 0;
96  virtual int getLineLen(int lineno) = 0;
97  virtual void getCells(int lineno, int colno, int count, Character res[]) = 0;
98  virtual bool isWrappedLine(int lineno) = 0;
99 
100  // adding lines.
101  virtual void addCells(const Character a[], int count) = 0;
102  // convenience method - this is virtual so that subclasses can take advantage
103  // of QVector's implicit copying
104  virtual void addCellsVector(const QVector<Character>& cells) {
105  addCells(cells.data(), cells.size());
106  }
107 
108  virtual void addLine(bool previousWrapped = false) = 0;
109 
110  //
111  // FIXME: Passing around constant references to HistoryType instances
112  // is very unsafe, because those references will no longer
113  // be valid if the history scroll is deleted.
114  //
115  const HistoryType& getType() const {
116  return *_historyType;
117  }
118 
119 protected:
120  HistoryType* _historyType;
121 };
122 
124 // File-based history (e.g. file log, no limitation in length)
126 
127 class KONSOLEPRIVATE_EXPORT HistoryScrollFile : public HistoryScroll
128 {
129 public:
130  explicit HistoryScrollFile(const QString& logFileName);
131  virtual ~HistoryScrollFile();
132 
133  virtual int getLines();
134  virtual int getLineLen(int lineno);
135  virtual void getCells(int lineno, int colno, int count, Character res[]);
136  virtual bool isWrappedLine(int lineno);
137 
138  virtual void addCells(const Character a[], int count);
139  virtual void addLine(bool previousWrapped = false);
140 
141 private:
142  int startOfLine(int lineno);
143 
144  HistoryFile _index; // lines Row(int)
145  HistoryFile _cells; // text Row(Character)
146  HistoryFile _lineflags; // flags Row(unsigned char)
147 };
148 
150 // Nothing-based history (no history :-)
152 class KONSOLEPRIVATE_EXPORT HistoryScrollNone : public HistoryScroll
153 {
154 public:
155  HistoryScrollNone();
156  virtual ~HistoryScrollNone();
157 
158  virtual bool hasScroll();
159 
160  virtual int getLines();
161  virtual int getLineLen(int lineno);
162  virtual void getCells(int lineno, int colno, int count, Character res[]);
163  virtual bool isWrappedLine(int lineno);
164 
165  virtual void addCells(const Character a[], int count);
166  virtual void addLine(bool previousWrapped = false);
167 };
168 
170 // History using compact storage
171 // This implementation uses a list of fixed-sized blocks
172 // where history lines are allocated in (avoids heap fragmentation)
174 typedef QVector<Character> TextLine;
175 
176 class CharacterFormat
177 {
178 public:
179  bool equalsFormat(const CharacterFormat& other) const {
180  return (other.rendition & ~RE_EXTENDED_CHAR) == (rendition & ~RE_EXTENDED_CHAR) && other.fgColor == fgColor && other.bgColor == bgColor;
181  }
182 
183  bool equalsFormat(const Character& c) const {
184  return (c.rendition & ~RE_EXTENDED_CHAR) == (rendition & ~RE_EXTENDED_CHAR) && c.foregroundColor == fgColor && c.backgroundColor == bgColor;
185  }
186 
187  void setFormat(const Character& c) {
188  rendition = c.rendition;
189  fgColor = c.foregroundColor;
190  bgColor = c.backgroundColor;
191  isRealCharacter = c.isRealCharacter;
192  }
193 
194  CharacterColor fgColor, bgColor;
195  quint16 startPos;
196  quint8 rendition;
197  bool isRealCharacter;
198 };
199 
200 class CompactHistoryBlock
201 {
202 public:
203  CompactHistoryBlock() {
204  _blockLength = 4096 * 64; // 256kb
205  _head = (quint8*) mmap(0, _blockLength, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
206  //_head = (quint8*) malloc(_blockLength);
207  Q_ASSERT(_head != MAP_FAILED);
208  _tail = _blockStart = _head;
209  _allocCount = 0;
210  }
211 
212  virtual ~CompactHistoryBlock() {
213  //free(_blockStart);
214  munmap(_blockStart, _blockLength);
215  }
216 
217  virtual unsigned int remaining() {
218  return _blockStart + _blockLength - _tail;
219  }
220  virtual unsigned length() {
221  return _blockLength;
222  }
223  virtual void* allocate(size_t length);
224  virtual bool contains(void* addr) {
225  return addr >= _blockStart && addr < (_blockStart + _blockLength);
226  }
227  virtual void deallocate();
228  virtual bool isInUse() {
229  return _allocCount != 0;
230  };
231 
232 private:
233  size_t _blockLength;
234  quint8* _head;
235  quint8* _tail;
236  quint8* _blockStart;
237  int _allocCount;
238 };
239 
240 class CompactHistoryBlockList
241 {
242 public:
243  CompactHistoryBlockList() {}
244  ~CompactHistoryBlockList();
245 
246  void* allocate(size_t size);
247  void deallocate(void *);
248  int length() {
249  return list.size();
250  }
251 private:
252  QList<CompactHistoryBlock*> list;
253 };
254 
255 class CompactHistoryLine
256 {
257 public:
258  CompactHistoryLine(const TextLine&, CompactHistoryBlockList& blockList);
259  virtual ~CompactHistoryLine();
260 
261  // custom new operator to allocate memory from custom pool instead of heap
262  static void* operator new(size_t size, CompactHistoryBlockList& blockList);
263  static void operator delete(void *) {
264  /* do nothing, deallocation from pool is done in destructor*/
265  };
266 
267  virtual void getCharacters(Character* array, int length, int startColumn);
268  virtual void getCharacter(int index, Character& r);
269  virtual bool isWrapped() const {
270  return _wrapped;
271  };
272  virtual void setWrapped(bool value) {
273  _wrapped = value;
274  };
275  virtual unsigned int getLength() const {
276  return _length;
277  };
278 
279 protected:
280  CompactHistoryBlockList& _blockListRef;
281  CharacterFormat* _formatArray;
282  quint16 _length;
283  quint16* _text;
284  quint16 _formatLength;
285  bool _wrapped;
286 };
287 
288 class KONSOLEPRIVATE_EXPORT CompactHistoryScroll : public HistoryScroll
289 {
290  typedef QList<CompactHistoryLine*> HistoryArray;
291 
292 public:
293  explicit CompactHistoryScroll(unsigned int maxNbLines = 1000);
294  virtual ~CompactHistoryScroll();
295 
296  virtual int getLines();
297  virtual int getLineLen(int lineno);
298  virtual void getCells(int lineno, int colno, int count, Character res[]);
299  virtual bool isWrappedLine(int lineno);
300 
301  virtual void addCells(const Character a[], int count);
302  virtual void addCellsVector(const TextLine& cells);
303  virtual void addLine(bool previousWrapped = false);
304 
305  void setMaxNbLines(unsigned int nbLines);
306 
307 private:
308  bool hasDifferentColors(const TextLine& line) const;
309  HistoryArray _lines;
310  CompactHistoryBlockList _blockList;
311 
312  unsigned int _maxLineCount;
313 };
314 
316 // History type
318 
319 class KONSOLEPRIVATE_EXPORT HistoryType
320 {
321 public:
322  HistoryType();
323  virtual ~HistoryType();
324 
329  virtual bool isEnabled() const = 0;
334  virtual int maximumLineCount() const = 0;
339  virtual HistoryScroll* scroll(HistoryScroll *) const = 0;
343  bool isUnlimited() const {
344  return maximumLineCount() == -1;
345  }
346 };
347 
348 class KONSOLEPRIVATE_EXPORT HistoryTypeNone : public HistoryType
349 {
350 public:
351  HistoryTypeNone();
352 
353  virtual bool isEnabled() const;
354  virtual int maximumLineCount() const;
355 
356  virtual HistoryScroll* scroll(HistoryScroll *) const;
357 };
358 
359 class KONSOLEPRIVATE_EXPORT HistoryTypeFile : public HistoryType
360 {
361 public:
362  explicit HistoryTypeFile(const QString& fileName = QString());
363 
364  virtual bool isEnabled() const;
365  virtual int maximumLineCount() const;
366 
367  virtual HistoryScroll* scroll(HistoryScroll *) const;
368 
369 protected:
370  QString _fileName;
371 };
372 
373 class KONSOLEPRIVATE_EXPORT CompactHistoryType : public HistoryType
374 {
375 public:
376  explicit CompactHistoryType(unsigned int size);
377 
378  virtual bool isEnabled() const;
379  virtual int maximumLineCount() const;
380 
381  virtual HistoryScroll* scroll(HistoryScroll *) const;
382 
383 protected:
384  unsigned int _maxLines;
385 };
386 }
387 
388 #endif // HISTORY_H
Konsole::TextLine
QVector< Character > TextLine
Definition: History.h:174
Konsole::CompactHistoryBlockList::deallocate
void deallocate(void *)
Definition: History.cpp:341
Konsole::CompactHistoryBlockList
Definition: History.h:240
Konsole::CompactHistoryScroll
Definition: History.h:288
Konsole::CharacterFormat::setFormat
void setFormat(const Character &c)
Definition: History.h:187
Konsole::CompactHistoryLine::getLength
virtual unsigned int getLength() const
Definition: History.h:275
Konsole::HistoryScroll::getType
const HistoryType & getType() const
Definition: History.h:115
Konsole::CompactHistoryBlockList::allocate
void * allocate(size_t size)
Definition: History.cpp:327
Konsole::CompactHistoryBlock::allocate
virtual void * allocate(size_t length)
Definition: History.cpp:308
Konsole::HistoryTypeNone
Definition: History.h:348
Konsole::HistoryTypeFile::_fileName
QString _fileName
Definition: History.h:370
Konsole::CompactHistoryBlockList::CompactHistoryBlockList
CompactHistoryBlockList()
Definition: History.h:243
Konsole::CompactHistoryLine
Definition: History.h:255
Konsole::CompactHistoryBlock
Definition: History.h:200
Konsole::CharacterFormat::rendition
quint8 rendition
Definition: History.h:196
Konsole::CompactHistoryType
Definition: History.h:373
Konsole::HistoryFile::unmap
void unmap()
Definition: History.cpp:96
Konsole::HistoryScroll::getCells
virtual void getCells(int lineno, int colno, int count, Character res[])=0
konsole_export.h
Konsole::CompactHistoryBlock::isInUse
virtual bool isInUse()
Definition: History.h:228
Konsole::CompactHistoryLine::_formatArray
CharacterFormat * _formatArray
Definition: History.h:281
Konsole::CompactHistoryBlock::length
virtual unsigned length()
Definition: History.h:220
Konsole::HistoryScrollFile
Definition: History.h:127
Konsole::HistoryScroll::isWrappedLine
virtual bool isWrappedLine(int lineno)=0
Konsole::CompactHistoryBlock::CompactHistoryBlock
CompactHistoryBlock()
Definition: History.h:203
Konsole::Character
A single character in the terminal which consists of a unicode character value, foreground and backgr...
Definition: Character.h:77
QVector::data
T * data()
Konsole::Character::isRealCharacter
bool isRealCharacter
Indicate whether this character really exists, or exists simply as place holder.
Definition: Character.h:128
Konsole::HistoryScroll::addLine
virtual void addLine(bool previousWrapped=false)=0
Konsole::CompactHistoryBlock::contains
virtual bool contains(void *addr)
Definition: History.h:224
Konsole::CompactHistoryType::_maxLines
unsigned int _maxLines
Definition: History.h:384
Konsole::HistoryScroll::getLines
virtual int getLines()=0
Konsole::CompactHistoryLine::isWrapped
virtual bool isWrapped() const
Definition: History.h:269
Konsole::HistoryScroll::addCellsVector
virtual void addCellsVector(const QVector< Character > &cells)
Definition: History.h:104
Konsole::CompactHistoryLine::getCharacters
virtual void getCharacters(Character *array, int length, int startColumn)
Definition: History.cpp:456
Konsole::HistoryFile::HistoryFile
HistoryFile()
Definition: History.cpp:58
Konsole::CharacterFormat::fgColor
CharacterColor fgColor
Definition: History.h:194
Konsole::CompactHistoryLine::_length
quint16 _length
Definition: History.h:282
loc
#define loc(X, Y)
Definition: Screen.cpp:51
Konsole::HistoryFile::map
void map()
Definition: History.cpp:82
Konsole::CompactHistoryLine::_text
quint16 * _text
Definition: History.h:283
Konsole::CompactHistoryLine::_wrapped
bool _wrapped
Definition: History.h:285
Konsole::CompactHistoryBlock::deallocate
virtual void deallocate()
Definition: History.cpp:321
Konsole::Character::backgroundColor
CharacterColor backgroundColor
The color used to draw this character's background.
Definition: Character.h:117
QString
QList
Konsole::HistoryFile::isMapped
bool isMapped() const
Definition: History.cpp:105
Konsole::HistoryScroll::HistoryScroll
HistoryScroll(HistoryType *)
Definition: History.cpp:170
Character.h
Konsole::CharacterFormat::bgColor
CharacterColor bgColor
Definition: History.h:194
Konsole::HistoryScroll::getLineLen
virtual int getLineLen(int lineno)=0
Konsole::CharacterColor
Describes the color of a single character in the terminal.
Definition: CharacterColor.h:147
Konsole::HistoryFile::add
virtual void add(const unsigned char *bytes, int len)
Definition: History.cpp:110
Konsole::HistoryTypeFile
Definition: History.h:359
Konsole::CompactHistoryLine::getCharacter
virtual void getCharacter(int index, Character &r)
Definition: History.cpp:442
Konsole::HistoryType
Definition: History.h:319
Konsole::CharacterFormat::equalsFormat
bool equalsFormat(const Character &c) const
Definition: History.h:183
Konsole::HistoryScroll::~HistoryScroll
virtual ~HistoryScroll()
Definition: History.cpp:175
Konsole::CompactHistoryBlock::remaining
virtual unsigned int remaining()
Definition: History.h:217
Konsole::CharacterFormat
Definition: History.h:176
QVector
Konsole::HistoryScroll::_historyType
HistoryType * _historyType
Definition: History.h:120
Konsole::CharacterFormat::equalsFormat
bool equalsFormat(const CharacterFormat &other) const
Definition: History.h:179
Konsole::CompactHistoryBlockList::~CompactHistoryBlockList
~CompactHistoryBlockList()
Definition: History.cpp:363
KONSOLEPRIVATE_EXPORT
#define KONSOLEPRIVATE_EXPORT
Definition: konsole_export.h:33
Konsole::HistoryScroll
Definition: History.h:86
Konsole::CompactHistoryLine::~CompactHistoryLine
virtual ~CompactHistoryLine()
Definition: History.cpp:433
Konsole::HistoryType::isUnlimited
bool isUnlimited() const
Returns true if the history size is unlimited.
Definition: History.h:343
Konsole::CompactHistoryLine::CompactHistoryLine
CompactHistoryLine(const TextLine &, CompactHistoryBlockList &blockList)
Definition: History.cpp:374
Konsole::CharacterFormat::isRealCharacter
bool isRealCharacter
Definition: History.h:197
QTemporaryFile
Konsole::CompactHistoryLine::_blockListRef
CompactHistoryBlockList & _blockListRef
Definition: History.h:277
Konsole::HistoryScrollNone
Definition: History.h:152
Konsole::CompactHistoryLine::setWrapped
virtual void setWrapped(bool value)
Definition: History.h:272
QVector::size
int size() const
Konsole::HistoryFile
Definition: History.h:43
Konsole::HistoryScroll::addCells
virtual void addCells(const Character a[], int count)=0
Konsole::HistoryFile::~HistoryFile
virtual ~HistoryFile()
Definition: History.cpp:73
Konsole::CompactHistoryBlock::~CompactHistoryBlock
virtual ~CompactHistoryBlock()
Definition: History.h:212
Konsole::Character::rendition
quint8 rendition
A combination of RENDITION flags which specify options for drawing the character. ...
Definition: Character.h:111
Konsole::CompactHistoryLine::_formatLength
quint16 _formatLength
Definition: History.h:284
Konsole::CompactHistoryBlockList::length
int length()
Definition: History.h:248
Konsole::Character::foregroundColor
CharacterColor foregroundColor
The foreground color used to draw this character.
Definition: Character.h:114
Konsole::HistoryScroll::hasScroll
virtual bool hasScroll()
Definition: History.cpp:180
Konsole::CharacterFormat::startPos
quint16 startPos
Definition: History.h:195
Konsole::RE_EXTENDED_CHAR
const int RE_EXTENDED_CHAR
Definition: Character.h:46
Konsole::HistoryFile::len
virtual int len() const
Definition: History.cpp:163
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