GNU Octave  3.8.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-2013 Jacob Dawid <jacob.dawid@googlemail.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  setFocusProxy(m_terminalView);
63  setFocus(Qt::OtherFocusReason);
64 
65  m_kpty = new KPty();
66  m_kpty->open();
67 
70  m_terminalModel->setCodec(QTextCodec::codecForName("UTF-8"));
76  connectToPty();
77 }
78 
80 {
81  // Store the file descriptor associated with the STDERR stream onto
82  // another temporary file descriptor for reconnect in the destructor.
83  fdstderr = dup (STDERR_FILENO);
84 
85  int fds = m_kpty->slaveFd();
86 
87  dup2 (fds, STDIN_FILENO);
88  dup2 (fds, STDOUT_FILENO);
89  dup2 (fds, STDERR_FILENO);
90 
91  if(!isatty(STDIN_FILENO)) {
92  qDebug("Error: stdin is not a tty.");
93  }
94 
95  if(!isatty(STDOUT_FILENO)) {
96  qDebug("Error: stdout is not a tty.");
97  }
98 
99  if(!isatty(STDERR_FILENO)) {
100  qDebug("Error: stderr is not a tty.");
101  }
102 }
103 
105 {
106  // Restore stderr so that any errors at exit might appear somewhere.
107  dup2 (fdstderr, STDERR_FILENO);
108 
109  emit destroyed();
110 }
111 
112 void QUnixTerminalImpl::setTerminalFont(const QFont &font)
113 {
114  if(!m_terminalView)
115  return;
116  m_terminalView->setVTFont(font);
117 }
118 
119 void QUnixTerminalImpl::setSize(int h, int v)
120 {
121  if(!m_terminalView)
122  return;
123  m_terminalView->setSize(h, v);
124 }
125 
126 void QUnixTerminalImpl::sendText(const QString& text)
127 {
128  m_terminalModel->sendText(text);
129 }
130 
132 {
133  switch(type) {
137  }
139 }
140 
141 // FIXME -- not sure how to make these work properly given the way the
142 // Unix terminal handles colors.
143 void QUnixTerminalImpl::setBackgroundColor (const QColor& color)
144  {
145  ColorEntry cols[TABLE_COLORS];
146 
147  const ColorEntry * curr_cols = m_terminalView->colorTable();
148  for(int i=0;i<TABLE_COLORS;i++)
149  {
150  cols[i] = curr_cols[i];
151  }
152 
153  cols[DEFAULT_BACK_COLOR].color = color;
154 
156 
157  }
158 void QUnixTerminalImpl::setForegroundColor (const QColor& color)
159 {
160  ColorEntry cols[TABLE_COLORS];
161 
162  const ColorEntry * curr_cols = m_terminalView->colorTable();
163  for(int i=0;i<TABLE_COLORS;i++)
164  {
165  cols[i] = curr_cols[i];
166  }
167 
168  cols[DEFAULT_FORE_COLOR].color = color;
169 
171 
172 
173 }
174 void QUnixTerminalImpl::setSelectionColor (const QColor& color) { }
175 
176 void QUnixTerminalImpl::setCursorColor (bool useForegroundColor,
177  const QColor& color)
178 {
179  m_terminalView->setKeyboardCursorColor (useForegroundColor, color);
180 }
181 
183 {
185  m_terminalView->repaint();
186  m_terminalView->update();
187 }
188 
190 {
191  m_terminalView->resize(this->size());
193  m_terminalView->repaint();
194  m_terminalView->update();
195 }
196 
198 {
200 }
201 
203 {
205 }
206 
208 {
209  return m_terminalView->selectedText ();
210 }