GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
QUnixTerminalImpl.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 e_k (e_k@users.sourceforge.net)
2  Copyright (C) 2012-2015 Jacob Dawid <jacob.dawid@cybercatalyst.com>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include <QDebug>
21 
22 #include "unix/QUnixTerminalImpl.h"
23 #include "unix/kpty.h"
24 
25 #include <termios.h>
26 
28  : QTerminal(parent) {
29  setMinimumSize(300, 200);
30  initialize();
31 }
32 
34 {
35  m_terminalView = new TerminalView(this);
40  m_terminalView->setContextMenuPolicy(Qt::CustomContextMenu);
43  m_terminalView->setSize(80, 40);
45 
46  connect(m_terminalView, SIGNAL(customContextMenuRequested(QPoint)),
47  this, SLOT(handleCustomContextMenuRequested(QPoint)));
48 
49  connect (m_terminalView, SIGNAL (interrupt_signal (void)),
50  this, SLOT (terminal_interrupt ()));
51 
52 #ifdef Q_OS_MAC
53  QFont font = QFont("Monaco");
54  font.setStyleHint(QFont::TypeWriter);
55  font.setPointSize(11);
56 #else
57  QFont font = QFont("Monospace");
58  font.setStyleHint(QFont::TypeWriter);
59  font.setPointSize(10);
60 #endif
61  setTerminalFont(font);
62  setFocusPolicy (Qt::StrongFocus);
63  setFocusProxy(m_terminalView);
64  setFocus(Qt::OtherFocusReason);
65 
66  m_kpty = new KPty();
67  m_kpty->open();
68 
71  m_terminalModel->setCodec(QTextCodec::codecForName("UTF-8"));
77  connectToPty();
78 }
80 {
81  if (value > 0)
82  {
85  }
86  else
88 }
89 
91 {
92  // Store the file descriptor associated with the STDERR stream onto
93  // another temporary file descriptor for reconnect in the destructor.
94  fdstderr = dup (STDERR_FILENO);
95 
96  int fds = m_kpty->slaveFd();
97 
98  dup2 (fds, STDIN_FILENO);
99  dup2 (fds, STDOUT_FILENO);
100  dup2 (fds, STDERR_FILENO);
101 
102  if(!isatty(STDIN_FILENO)) {
103  qDebug("Error: stdin is not a tty.");
104  }
105 
106  if(!isatty(STDOUT_FILENO)) {
107  qDebug("Error: stdout is not a tty.");
108  }
109 
110  if(!isatty(STDERR_FILENO)) {
111  qDebug("Error: stderr is not a tty.");
112  }
113 }
114 
116 {
117  // Restore stderr so that any errors at exit might appear somewhere.
118  dup2 (fdstderr, STDERR_FILENO);
119 
120  emit destroyed();
121 }
122 
123 void QUnixTerminalImpl::setTerminalFont(const QFont &font)
124 {
125  if(!m_terminalView)
126  return;
127  m_terminalView->setVTFont(font);
128 }
129 
130 void QUnixTerminalImpl::setSize(int h, int v)
131 {
132  if(!m_terminalView)
133  return;
134  m_terminalView->setSize(h, v);
135 }
136 
137 void QUnixTerminalImpl::sendText(const QString& text)
138 {
139  m_terminalModel->sendText(text);
140 }
141 
143 {
144  switch(type) {
148  }
150 }
151 
152 // FIXME -- not sure how to make these work properly given the way the
153 // Unix terminal handles colors.
154 void QUnixTerminalImpl::setBackgroundColor (const QColor& color)
155  {
156  ColorEntry cols[TABLE_COLORS];
157 
158  const ColorEntry * curr_cols = m_terminalView->colorTable();
159  for(int i=0;i<TABLE_COLORS;i++)
160  {
161  cols[i] = curr_cols[i];
162  }
163 
164  cols[DEFAULT_BACK_COLOR].color = color;
165 
167 
168  }
169 void QUnixTerminalImpl::setForegroundColor (const QColor& color)
170 {
171  ColorEntry cols[TABLE_COLORS];
172 
173  const ColorEntry * curr_cols = m_terminalView->colorTable();
174  for(int i=0;i<TABLE_COLORS;i++)
175  {
176  cols[i] = curr_cols[i];
177  }
178 
179  cols[DEFAULT_FORE_COLOR].color = color;
180 
182 
183 
184 }
185 void QUnixTerminalImpl::setSelectionColor (const QColor& color) { }
186 
187 void QUnixTerminalImpl::setCursorColor (bool useForegroundColor,
188  const QColor& color)
189 {
190  m_terminalView->setKeyboardCursorColor (useForegroundColor, color);
191 }
192 
194 {
196  m_terminalView->repaint();
197  m_terminalView->update();
198 }
199 
201 {
202  m_terminalView->resize(this->size());
204  m_terminalView->repaint();
205  m_terminalView->update();
206 }
207 
209 {
211 }
212 
214 {
216 }
217 
219 {
221 }
222 
223 
225 {
226  return m_terminalView->selectedText ();
227 }
228 
229 void
231 {
232  _extra_interrupt = extra;
233 }
An entry in a terminal display's color palette.
void addView(TerminalView *widget)
Adds a new view for this session.
void setTerminalSizeStartup(bool on)
Sets whether the terminal size display is shown briefly after the widget is first shown...
Definition: TerminalView.h:379
#define TABLE_COLORS
void setScrollBarPosition(ScrollBarPosition position)
Specifies whether the terminal display has a vertical scroll bar, and if so whether it is shown on th...
void setSize(int h, int v)
Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
Definition: TerminalModel.h:57
void setTerminalSizeHint(bool on)
Sets whether or not the current height and width of the terminal in lines and columns is displayed wh...
Definition: TerminalView.h:366
void setKeyboardCursorShape(KeyboardCursorShape shape)
Sets the shape of the keyboard cursor.
void sendText(const QString &text) const
Sends text to the current foreground terminal program.
void run()
Starts the terminal session.
void clearHistory()
Clears the history store used by this session.
#define DEFAULT_FORE_COLOR
void interrupt_signal(void)
void setColorTable(const ColorEntry table[])
Sets the terminal color palette used by the display.
void setCursorType(CursorType type, bool blinking)
void setKeyBindings(const QString &id)
Sets the key bindings used by this session.
Select the whole line underneath the cursor.
Definition: TerminalView.h:165
void terminal_interrupt(void)
Definition: QTerminal.h:120
TerminalView * m_terminalView
void setSize(int cols, int lins)
bool open()
Create a pty master/slave pair.
Definition: kpty.cpp:214
QColor color
The color value of this entry for display.
void setHistoryType(const HistoryType &type)
Sets the type of history store used by this session.
QString selectedText()
int slaveFd() const
Definition: kpty.cpp:488
Show the scroll bar on the right side of the display.
Definition: TerminalView.h:98
An cursor shaped like the capital letter 'I', similar to the IBeam cursor used in Qt/KDE text editors...
Definition: TerminalView.h:196
A rectangular block which covers the entire area of the cursor character.
Definition: TerminalView.h:186
void selectAll()
selects all content
TerminalModel * m_terminalModel
void has_extra_interrupt(bool extra_interrupt)
void setBellMode(int mode)
Sets the type of effect used to alert the user when a 'bell' occurs in the terminal session...
void setCursorColor(bool useForegroundColor, const QColor &color)
size_t size(T const (&)[z])
Definition: help.cc:103
void pasteClipboard()
Pastes the content of the clipboard into the display.
void setDarkBackground(bool darkBackground)
Sets whether the session has a dark background or not.
void setBlinkingCursor(bool blink)
Specifies whether or not the cursor blinks.
void setForegroundColor(const QColor &color)
void setTerminalFont(const QFont &font)
virtual void resizeEvent(QResizeEvent *)
void setKeyboardCursorColor(bool useForegroundColor, const QColor &color)
Sets the color used to draw the keyboard cursor.
void showEvent(QShowEvent *)
void setCodec(QTextCodec *codec)
Sets the text codec used by this session's terminal emulation.
void sendText(const QString &text)
QUnixTerminalImpl(QWidget *parent=0)
void copyClipboard(bool extra_interrupt)
Copies the selected text to the clipboard.
void setAutoClose(bool b)
Specifies whether to close the session automatically when the terminal process terminates.
void setScrollBufferSize(int value)
void setBackgroundColor(const QColor &color)
KDE notification.
Definition: TerminalView.h:317
A single flat line which occupies the space at the bottom of the cursor character's area...
Definition: TerminalView.h:191
A widget which displays output from a terminal emulation and sends input keypresses and mouse activit...
Definition: TerminalView.h:63
Provides primitives for opening & closing a pseudo TTY pair, assigning the controlling TTY...
Definition: kpty.h:35
const ColorEntry * colorTable() const
Returns the terminal color palette used by the display.
void setVTFont(const QFont &font)
Sets the font used to draw the display.
void setSelectionColor(const QColor &color)
void updateImage()
Causes the terminal display to fetch the latest character image from the associated terminal screen (...
virtual void handleCustomContextMenuRequested(const QPoint &at)
Definition: QTerminal.h:105
void setTripleClickMode(TripleClickMode mode)
Sets how the text is selected when the user triple clicks within the display.
Definition: TerminalView.h:170
#define DEFAULT_BACK_COLOR