GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
QUnixTerminalImpl.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 e_k (e_k@users.sourceforge.net)
2  Copyright (C) 2012-2019 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 
27 QUnixTerminalImpl::QUnixTerminalImpl(octave::base_qobject& oct_qobj,
28  QWidget *p)
29  : QTerminal(oct_qobj, p)
30 {
31  initialize();
32 }
33 
35 {
36  m_terminalView = new TerminalView(this);
41  m_terminalView->setContextMenuPolicy(Qt::CustomContextMenu);
44  m_terminalView->setSize(80, 40);
46 
47  UrlFilter *url_filter = new UrlFilter();
48  m_terminalView->filterChain ()->addFilter (url_filter);
49 
50  UrlFilter *file_filter = new UrlFilter (Filter::Type::ErrorLink);
51  m_terminalView->filterChain ()->addFilter (file_filter);
52 
53  connect (file_filter, SIGNAL (request_edit_mfile_signal (const QString&, int)),
54  this, SIGNAL (request_edit_mfile_signal (const QString&, int)));
55  connect (file_filter, SIGNAL (request_open_file_signal (const QString&, const QString&,int)),
56  this, SIGNAL (request_open_file_signal (const QString&, const QString&,int)));
57 
58  connect(m_terminalView, SIGNAL(customContextMenuRequested(QPoint)),
59  this, SLOT(handleCustomContextMenuRequested(QPoint)));
60 
61  connect (m_terminalView, SIGNAL (interrupt_signal (void)),
62  this, SLOT (terminal_interrupt ()));
63 
64 #ifdef Q_OS_MAC
65  QFont font = QFont("Monaco");
66  font.setStyleHint(QFont::TypeWriter);
67  font.setPointSize(11);
68 #else
69  QFont font = QFont("Monospace");
70  font.setStyleHint(QFont::TypeWriter);
71  font.setPointSize(10);
72 #endif
73  setTerminalFont(font);
74  setFocusPolicy (Qt::StrongFocus);
75  setFocusProxy(m_terminalView);
76  setFocus(Qt::OtherFocusReason);
77 
78  m_kpty = new KPty();
79  m_kpty->open();
80 
83  m_terminalModel->setCodec(QTextCodec::codecForName("UTF-8"));
89  connectToPty();
90 }
92 {
93  if (value > 0)
94  {
97  }
98  else
100 }
101 
104 {
105  return m_terminalView->filterActions (at);
106 }
107 
109 {
110  // Store the file descriptor associated with the STDIN, STDOUT, and
111  // STDERR streams onto another temporary file descriptor for
112  // reconnect in the destructor.
113 
114  fdstdin = dup (STDIN_FILENO);
115  fdstdout = dup (STDOUT_FILENO);
116  fdstderr = dup (STDERR_FILENO);
117 
118  int fds = m_kpty->slaveFd();
119 
120  dup2 (fds, STDIN_FILENO);
121  dup2 (fds, STDOUT_FILENO);
122  dup2 (fds, STDERR_FILENO);
123 
124  if(!isatty(STDIN_FILENO)) {
125  qDebug("Error: stdin is not a tty.");
126  }
127 
128  if(!isatty(STDOUT_FILENO)) {
129  qDebug("Error: stdout is not a tty.");
130  }
131 
132  if(!isatty(STDERR_FILENO)) {
133  qDebug("Error: stderr is not a tty.");
134  }
135 }
136 
138 {
139  delete m_terminalModel;
140  delete m_kpty;
141  delete m_terminalView;
142 
143  // Restore STDIN, STDOUT, and STDERR so that I/O at exit will work
144  // as expected.
145 
147  dup2 (fdstdout, STDOUT_FILENO);
148  dup2 (fdstderr, STDERR_FILENO);
149 
150  emit destroyed();
151 }
152 
153 void QUnixTerminalImpl::setTerminalFont(const QFont &font)
154 {
155  if(!m_terminalView)
156  return;
157  m_terminalView->setVTFont(font);
158 }
159 
160 void QUnixTerminalImpl::setSize(int h, int v)
161 {
162  if(!m_terminalView)
163  return;
164  m_terminalView->setSize(h, v);
165 }
166 
167 void QUnixTerminalImpl::sendText(const QString& text)
168 {
169  m_terminalModel->sendText(text);
170 }
171 
173 {
174  switch(type) {
178  }
180 }
181 
182 // FIXME -- not sure how to make these work properly given the way the
183 // Unix terminal handles colors.
184 void QUnixTerminalImpl::setBackgroundColor (const QColor& color)
185  {
186  ColorEntry cols[TABLE_COLORS];
187 
188  const ColorEntry * curr_cols = m_terminalView->colorTable();
189  for(int i=0;i<TABLE_COLORS;i++)
190  {
191  cols[i] = curr_cols[i];
192  }
193 
194  cols[DEFAULT_BACK_COLOR].color = color;
195 
197 
198  QString css = QString ("TerminalView {\n"
199  " background: %1;\n"
200  "}\n").arg (color.name ());
201  setStyleSheet (css);
202 
203  }
204 void QUnixTerminalImpl::setForegroundColor (const QColor& color)
205 {
206  ColorEntry cols[TABLE_COLORS];
207 
208  const ColorEntry * curr_cols = m_terminalView->colorTable();
209  for(int i=0;i<TABLE_COLORS;i++)
210  {
211  cols[i] = curr_cols[i];
212  }
213 
214  cols[DEFAULT_FORE_COLOR].color = color;
215 
217 
218 
219 }
220 void QUnixTerminalImpl::setSelectionColor (const QColor& color) { }
221 
222 void QUnixTerminalImpl::setCursorColor (bool useForegroundColor,
223  const QColor& color)
224 {
225  m_terminalView->setKeyboardCursorColor (useForegroundColor, color);
226 }
227 
229 {
231  m_terminalView->repaint();
232  m_terminalView->update();
233 }
234 
236 {
237  m_terminalView->resize(this->size());
239  m_terminalView->repaint();
240  m_terminalView->update();
241 }
242 
244 {
246 }
247 
249 {
251 }
252 
254 {
256 }
257 
258 
260 {
261  return m_terminalView->selectedText ();
262 }
263 
264 void
266 {
267  _extra_interrupt = extra;
268 }
269 
270 void
272 {
274 };
#define TABLE_COLORS
#define DEFAULT_FORE_COLOR
#define DEFAULT_BACK_COLOR
An entry in a terminal display's color palette.
QColor color
The color value of this entry for display.
void addFilter(Filter *filter)
Adds a new filter to the chain.
Definition: Filter.cpp:51
Provides primitives for opening & closing a pseudo TTY pair, assigning the controlling TTY,...
Definition: kpty.h:35
bool open()
Create a pty master/slave pair.
Definition: kpty.cpp:215
int slaveFd() const
Definition: kpty.cpp:489
void interrupt_signal(void)
@ IBeamCursor
Definition: QTerminal.h:74
@ UnderlineCursor
Definition: QTerminal.h:76
@ BlockCursor
Definition: QTerminal.h:75
void request_edit_mfile_signal(const QString &, int)
virtual void handleCustomContextMenuRequested(const QPoint &at)
Definition: QTerminal.cc:73
void request_open_file_signal(const QString &, const QString &, int)
void terminal_interrupt(void)
Definition: QTerminal.h:133
virtual void handle_visibility_changed(bool visible)
void sendText(const QString &text)
void setSelectionColor(const QColor &color)
void setTerminalFont(const QFont &font)
void setSize(int h, int v)
void setCursorType(CursorType type, bool blinking)
void setBackgroundColor(const QColor &color)
void has_extra_interrupt(bool extra_interrupt)
QList< QAction * > get_hotspot_actions(const QPoint &at)
void setCursorColor(bool useForegroundColor, const QColor &color)
TerminalModel * m_terminalModel
void setScrollBufferSize(int value)
void showEvent(QShowEvent *)
QUnixTerminalImpl(octave::base_qobject &, QWidget *parent)
void setForegroundColor(const QColor &color)
TerminalView * m_terminalView
virtual void resizeEvent(QResizeEvent *)
Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
Definition: TerminalModel.h:57
void setCodec(QTextCodec *codec)
Sets the text codec used by this session's terminal emulation.
void setAutoClose(bool b)
Specifies whether to close the session automatically when the terminal process terminates.
void sendText(const QString &text) const
Sends text to the current foreground terminal program.
void run()
Starts the terminal session.
void addView(TerminalView *widget)
Adds a new view for this session.
void setKeyBindings(const QString &id)
Sets the key bindings used by this session.
void clearHistory()
Clears the history store used by this session.
void setDarkBackground(bool darkBackground)
Sets whether the session has a dark background or not.
void setHistoryType(const HistoryType &type)
Sets the type of history store used by this session.
A widget which displays output from a terminal emulation and sends input keypresses and mouse activit...
Definition: TerminalView.h:64
void selectAll()
selects all content
FilterChain * filterChain() const
Returns the display's filter chain.
void pasteClipboard()
Pastes the content of the clipboard into the display.
void setTerminalSizeStartup(bool on)
Sets whether the terminal size display is shown briefly after the widget is first shown.
Definition: TerminalView.h:366
void setBellMode(int mode)
Sets the type of effect used to alert the user when a 'bell' occurs in the terminal session.
void copyClipboard(bool extra_interrupt)
Copies the selected text to the clipboard.
@ SelectWholeLine
Select the whole line underneath the cursor.
Definition: TerminalView.h:149
void setColorTable(const ColorEntry table[])
Sets the terminal color palette used by the display.
void setScrollBarPosition(ScrollBarPosition position)
Specifies whether the terminal display has a vertical scroll bar, and if so whether it is shown on th...
void setKeyboardCursorShape(KeyboardCursorShape shape)
Sets the shape of the keyboard cursor.
void setKeyboardCursorColor(bool useForegroundColor, const QColor &color)
Sets the color used to draw the keyboard cursor.
void setVTFont(const QFont &font)
Sets the font used to draw the display.
QString selectedText()
void updateImage()
Causes the terminal display to fetch the latest character image from the associated terminal screen (...
@ NotifyBell
KDE notification.
Definition: TerminalView.h:304
void setBlinkingCursor(bool blink)
Specifies whether or not the cursor blinks.
QList< QAction * > filterActions(const QPoint &position)
Returns a list of menu actions created by the filters for the content at the given position.
void setTripleClickMode(TripleClickMode mode)
Sets how the text is selected when the user triple clicks within the display.
Definition: TerminalView.h:154
@ ScrollBarRight
Show the scroll bar on the right side of the display.
Definition: TerminalView.h:98
@ UnderlineCursor
A single flat line which occupies the space at the bottom of the cursor character's area.
Definition: TerminalView.h:178
@ IBeamCursor
An cursor shaped like the capital letter 'I', similar to the IBeam cursor used in Qt/KDE text editors...
Definition: TerminalView.h:183
@ BlockCursor
A rectangular block which covers the entire area of the cursor character.
Definition: TerminalView.h:173
const ColorEntry * colorTable() const
Returns the terminal color palette used by the display.
void visibility_changed(bool visible)
Is called, when the terminal's visibility has changed in order to stop orstart timers etc.
void setSize(int cols, int lins)
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:353
A filter which matches URLs in blocks of text.
Definition: Filter.h:266
int dup2(int old_fd, int new_fd)
Definition: oct-syscalls.cc:52
#define isatty
#define STDIN_FILENO
Definition: sysdep.cc:92