GNU Octave  6.2.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 
28  : QTerminal(p),
29  _parent (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  _parent, SLOT (edit_mfile (const QString&, int)));
55  connect (file_filter, SIGNAL (request_open_file_signal (const QString&, int)),
56  _parent, SLOT (open_file (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 STDERR stream onto
111  // another temporary file descriptor for reconnect in the destructor.
112  fdstderr = dup (STDERR_FILENO);
113 
114  int fds = m_kpty->slaveFd();
115 
116  dup2 (fds, STDIN_FILENO);
117  dup2 (fds, STDOUT_FILENO);
118  dup2 (fds, STDERR_FILENO);
119 
120  if(!isatty(STDIN_FILENO)) {
121  qDebug("Error: stdin is not a tty.");
122  }
123 
124  if(!isatty(STDOUT_FILENO)) {
125  qDebug("Error: stdout is not a tty.");
126  }
127 
128  if(!isatty(STDERR_FILENO)) {
129  qDebug("Error: stderr is not a tty.");
130  }
131 }
132 
134 {
135  delete m_terminalModel;
136  delete m_kpty;
137  delete m_terminalView;
138 
139  // Restore stderr so that any errors at exit might appear somewhere.
140  dup2 (fdstderr, STDERR_FILENO);
141 
142  emit destroyed();
143 }
144 
145 void QUnixTerminalImpl::setTerminalFont(const QFont &font)
146 {
147  if(!m_terminalView)
148  return;
149  m_terminalView->setVTFont(font);
150 }
151 
152 void QUnixTerminalImpl::setSize(int h, int v)
153 {
154  if(!m_terminalView)
155  return;
156  m_terminalView->setSize(h, v);
157 }
158 
159 void QUnixTerminalImpl::sendText(const QString& text)
160 {
162 }
163 
165 {
166  switch(type) {
170  }
172 }
173 
174 // FIXME -- not sure how to make these work properly given the way the
175 // Unix terminal handles colors.
176 void QUnixTerminalImpl::setBackgroundColor (const QColor& color)
177  {
178  ColorEntry cols[TABLE_COLORS];
179 
180  const ColorEntry * curr_cols = m_terminalView->colorTable();
181  for(int i=0;i<TABLE_COLORS;i++)
182  {
183  cols[i] = curr_cols[i];
184  }
185 
186  cols[DEFAULT_BACK_COLOR].color = color;
187 
189 
190  QString css = QString ("TerminalView {\n"
191  " background: %1;\n"
192  "}\n").arg (color.name ());
193  setStyleSheet (css);
194 
195  }
196 void QUnixTerminalImpl::setForegroundColor (const QColor& color)
197 {
198  ColorEntry cols[TABLE_COLORS];
199 
200  const ColorEntry * curr_cols = m_terminalView->colorTable();
201  for(int i=0;i<TABLE_COLORS;i++)
202  {
203  cols[i] = curr_cols[i];
204  }
205 
206  cols[DEFAULT_FORE_COLOR].color = color;
207 
209 
210 
211 }
212 void QUnixTerminalImpl::setSelectionColor (const QColor& color) { }
213 
214 void QUnixTerminalImpl::setCursorColor (bool useForegroundColor,
215  const QColor& color)
216 {
217  m_terminalView->setKeyboardCursorColor (useForegroundColor, color);
218 }
219 
221 {
223  m_terminalView->repaint();
224  m_terminalView->update();
225 }
226 
228 {
229  m_terminalView->resize(this->size());
231  m_terminalView->repaint();
232  m_terminalView->update();
233 }
234 
236 {
238 }
239 
241 {
243 }
244 
246 {
248 }
249 
250 
252 {
253  return m_terminalView->selectedText ();
254 }
255 
256 void
258 {
259  _extra_interrupt = extra;
260 }
261 
262 void
264 {
266 };
#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:212
int slaveFd() const
Definition: kpty.cpp:486
void interrupt_signal(void)
@ IBeamCursor
Definition: QTerminal.h:73
@ UnderlineCursor
Definition: QTerminal.h:75
@ BlockCursor
Definition: QTerminal.h:74
virtual void handleCustomContextMenuRequested(const QPoint &at)
Definition: QTerminal.cc:92
void terminal_interrupt(void)
Definition: QTerminal.h:124
virtual void handle_visibility_changed(bool visible)
QUnixTerminalImpl(QWidget *parent=nullptr)
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 *)
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:53
#define isatty
#define STDIN_FILENO
Definition: sysdep.cc:92