1 /*
2
3 Copyright (C) 2011-2019 Michael Goffioul
4
5 This file is part of QConsole.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not,
19 see <https://www.gnu.org/licenses/>.
20
21 */
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QColor>
26 #include <QFont>
27 #include <QGridLayout>
28 #include <QPaintEvent>
29 #include <QPainter>
30 #include <QResizeEvent>
31 #include <QScrollBar>
32 #include <QtDebug>
33 #include <QThread>
34 #include <QTimer>
35 #include <QToolTip>
36 #include <QCursor>
37 #include <QMessageBox>
38 #include <QDragEnterEvent>
39 #include <QDropEvent>
40 #include <QUrl>
41 #include <QMimeData>
42
43 #include <fcntl.h>
44 #include <io.h>
45 #include <stdio.h>
46 #include <stdarg.h>
47 #define WIN32_LEAN_AND_MEAN
48 #if ! defined (_WIN32_WINNT) && ! defined (NTDDI_VERSION)
49 #define _WIN32_WINNT 0x0500
50 #endif
51 #include <windows.h>
52 #include <versionhelpers.h>
53 #include <cstring>
54 #include <csignal>
55
56 #include "QWinTerminalImpl.h"
57 #include "QTerminalColors.h"
58
59 // Uncomment to log activity to LOGFILENAME
60 // #define DEBUG_QCONSOLE
61 #define LOGFILENAME "QConsole.log"
62 // Uncomment to create hidden console window
63 #define HIDDEN_CONSOLE
64
65 #ifdef _MSC_VER
66 # pragma warning(disable : 4996)
67 #endif
68
69 //////////////////////////////////////////////////////////////////////////////
70
71 class QConsoleView : public QWidget
72 {
73 public:
QConsoleView(QWinTerminalImpl * parent=0)74 QConsoleView (QWinTerminalImpl* parent = 0) : QWidget (parent), q (parent) { }
~QConsoleView(void)75 ~QConsoleView (void) { }
76
77 protected:
paintEvent(QPaintEvent * event)78 void paintEvent (QPaintEvent* event) { q->viewPaintEvent (this, event); }
resizeEvent(QResizeEvent * event)79 void resizeEvent (QResizeEvent* event) { q->viewResizeEvent (this, event); }
80
81 private:
82 QWinTerminalImpl* q;
83 };
84
85 //////////////////////////////////////////////////////////////////////////////
86
87 class QConsoleThread : public QThread
88 {
89 public:
QConsoleThread(QWinTerminalImpl * console)90 QConsoleThread (QWinTerminalImpl* console) : QThread (console), q (console) { }
91
92 protected:
run(void)93 void run (void)
94 { q->start (); }
95
96 private:
97 QWinTerminalImpl* q;
98 };
99
100 //////////////////////////////////////////////////////////////////////////////
101
translateKey(QKeyEvent * ev)102 static QString translateKey (QKeyEvent *ev)
103 {
104 QString esc = "\x1b";
105 QString s;
106
107 if (ev->key () == Qt::Key_Delete)
108 s = esc + "[C\b";
109 else if (!ev->text ().isEmpty ())
110 s = ev->text ();
111 else
112 {
113
114 switch (ev->key ())
115 {
116 case Qt::Key_Up:
117 s = esc + "[A";
118 break;
119
120 case Qt::Key_Down:
121 s = esc + "[B";
122 break;
123
124 case Qt::Key_Right:
125 s = esc + "[C";
126 break;
127
128 case Qt::Key_Left:
129 s = esc + "[D";
130 break;
131
132 case Qt::Key_Home:
133 s = esc + "[H";
134 break;
135
136 case Qt::Key_End:
137 s = esc + "[F";
138 break;
139
140 case Qt::Key_Insert:
141 s = esc + "[2~";
142 break;
143
144 case Qt::Key_PageUp:
145 s = esc + "[5~";
146 break;
147
148 case Qt::Key_PageDown:
149 s = esc + "[6~";
150 break;
151
152 case Qt::Key_Escape:
153 s = esc;
154 break;
155
156 default:
157 break;
158 }
159 }
160
161 return s;
162 }
163
164 class QConsolePrivate
165 {
166 friend class QWinTerminalImpl;
167
168 public:
169
170 enum KeyboardCursorType
171 {
172 BlockCursor,
173 UnderlineCursor,
174 IBeamCursor
175 };
176
177 QConsolePrivate (QWinTerminalImpl* parent, const QString& cmd = QString ());
178 ~QConsolePrivate (void);
179
180 void updateConsoleSize (bool sync = false, bool allow_smaller_width = false);
181 void syncConsoleParameters (void);
182 void grabConsoleBuffer (CHAR_INFO* buf = 0);
183 void updateHorizontalScrollBar (void);
184 void updateVerticalScrollBar (void);
185 void setHorizontalScrollValue (int value);
186 void setVerticalScrollValue (int value);
187 void updateConsoleView (bool grab = true);
188 void monitorConsole (void);
189 void startCommand (void);
190 void sendConsoleText (const QString& s);
191 QRect cursorRect (void);
192 void selectAll();
193 void selectWord(const QPoint& cellPos);
194 void selectLine(const QPoint& cellPos);
195
196 void log (const char* fmt, ...);
197
198 void closeStandardIO (int fd, DWORD stdHandleId, const char* name);
199 void setupStandardIO (DWORD stdHandleId, int fd, const char* name,
200 const char* devName);
201
202 QPoint posToCell (const QPoint& pt);
203 QString getSelection (void);
204 void updateSelection (void);
205 void clearSelection (void);
206
207 QColor backgroundColor (void) const;
208 QColor foregroundColor (void) const;
209 QColor selectionColor (void) const;
210 QColor cursorColor (void) const;
211
212 void setBackgroundColor (const QColor& color);
213 void setForegroundColor (const QColor& color);
214 void setSelectionColor (const QColor& color);
215 void setCursorColor (bool useForegroundColor, const QColor& color);
216 void setScrollBufferSize (int value);
217
218 void drawTextBackground (QPainter& p, int cx1, int cy1, int cx2, int cy2,
219 int cw, int ch);
220
221 void drawSelection (QPainter& p, int cx1, int cy1, int cx2, int cy2,
222 int cw, int ch);
223
224 void drawCursor (QPainter& p);
225
226 void drawText (QPainter& p, int cx1, int cy1, int cx2, int cy2,
227 int cw, int ch);
228
229 private:
230 QWinTerminalImpl* q;
231
232 private:
233 QFont m_font;
234 QString m_command;
235 QConsoleColors m_colors;
236 bool m_inWheelEvent;
237 QString m_title;
238
239 QSize m_charSize;
240 QSize m_bufferSize;
241 QRect m_consoleRect;
242 bool m_auto_scroll;
243 QPoint m_cursorPos;
244 bool m_cursorBlinking;
245 bool m_hasBlinkingCursor;
246 QTimer *m_blinkCursorTimer;
247 KeyboardCursorType m_cursorType;
248
249 QPoint m_beginSelection;
250 QPoint m_endSelection;
251 bool m_settingSelection;
252
253 QColor m_selectionColor;
254 QColor m_cursorColor;
255
256 HANDLE m_stdOut;
257 HWND m_consoleWindow;
258 CHAR_INFO* m_buffer;
259 CHAR_INFO* m_tmpBuffer;
260 HANDLE m_process;
261
262 QConsoleView* m_consoleView;
263 QScrollBar* m_horizontalScrollBar;
264 QScrollBar* m_verticalScrollBar;
265 QTimer* m_consoleWatcher;
266 QConsoleThread *m_consoleThread;
267
268 // The delay in milliseconds between redrawing blinking text.
269 static const int BLINK_DELAY = 500;
270 };
271
maybeSwapPoints(QPoint & begin,QPoint & end)272 static void maybeSwapPoints (QPoint& begin, QPoint& end)
273 {
274 if (end.y () < begin.y ()
275 || (end.y () == begin.y () && end.x () < begin.x ()))
276 qSwap (begin, end);
277 }
278
279 //////////////////////////////////////////////////////////////////////////////
280
QConsolePrivate(QWinTerminalImpl * parent,const QString & cmd)281 QConsolePrivate::QConsolePrivate (QWinTerminalImpl* parent, const QString& cmd)
282 : q (parent), m_command (cmd), m_auto_scroll (true), m_cursorBlinking (false),
283 m_hasBlinkingCursor (true), m_cursorType (BlockCursor),
284 m_beginSelection (0, 0), m_endSelection (0, 0), m_settingSelection (false),
285 m_process (nullptr), m_inWheelEvent (false)
286 {
287 log (nullptr);
288
289 // Possibly detach from any existing console
290 log ("Detaching from existing console (if any)...\n");
291 FreeConsole ();
292 log ("Closing standard IO...\n");
293 closeStandardIO (0, STD_INPUT_HANDLE, "STDIN");
294 closeStandardIO (1, STD_OUTPUT_HANDLE, "STDOUT");
295 closeStandardIO (2, STD_ERROR_HANDLE, "STDERR");
296
297 #ifdef HIDDEN_CONSOLE
298 HWINSTA hOrigSta, hNewSta;
299
300 // Create new (hidden) console
301 hOrigSta = GetProcessWindowStation ();
302 hNewSta = CreateWindowStation (nullptr, 0, GENERIC_ALL, nullptr);
303 log ("Current Windows station: %p.\nNew Windows station: %p.\n", hOrigSta,
304 hNewSta);
305 if (! SetProcessWindowStation (hNewSta))
306 log ("Failed to switch to new Windows station.\n");
307 #endif
308 if (! AllocConsole ())
309 log ("Failed to create new console.\n");
310 #ifdef HIDDEN_CONSOLE
311 if (! SetProcessWindowStation (hOrigSta))
312 log ("Failed to restore original Windows station.\n");
313 if (! CloseWindowStation (hNewSta))
314 log ("Failed to close new Windows station.\n");
315 #endif
316
317 log ("New (hidden) console created.\n");
318
319 setupStandardIO (STD_INPUT_HANDLE, 0, "STDIN", "CONIN$");
320 setupStandardIO (STD_OUTPUT_HANDLE, 1, "STDOUT", "CONOUT$");
321 setupStandardIO (STD_ERROR_HANDLE, 2, "STDERR", "CONOUT$");
322
323 log ("Standard input/output/error set up.\n");
324
325 *stdin = *(fdopen (0, "rb"));
326 *stdout = *(fdopen (1, "wb"));
327 *stderr = *(fdopen (2, "wb"));
328
329 log ("POSIX standard streams created.\n");
330
331 setvbuf (stdin, nullptr, _IONBF, 0);
332 setvbuf (stdout, nullptr, _IONBF, 0);
333 setvbuf (stderr, nullptr, _IONBF, 0);
334
335 log ("POSIX standard stream buffers adjusted.\n");
336
337 HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE);
338
339 log ("Console allocated: hStdOut: %p\n", hStdOut);
340
341 m_stdOut = hStdOut;
342 m_consoleWindow = GetConsoleWindow ();
343
344 // In case the console window hasn't been created hidden...
345 #ifdef HIDDEN_CONSOLE
346 ShowWindow (m_consoleWindow, SW_HIDE);
347 #endif
348
349 CONSOLE_SCREEN_BUFFER_INFO sbi;
350
351 GetConsoleScreenBufferInfo (hStdOut, &sbi);
352 m_bufferSize = QSize (sbi.dwSize.X, qMax (sbi.dwSize.Y, (SHORT)500));
353 m_consoleRect = QRect (sbi.srWindow.Left, sbi.srWindow.Top,
354 sbi.srWindow.Right - sbi.srWindow.Left + 1,
355 sbi.srWindow.Bottom - sbi.srWindow.Top + 1);
356 m_cursorPos = QPoint (sbi.dwCursorPosition.X, sbi.dwCursorPosition.Y);
357
358 log ("Initial console parameters:\n");
359 log (" buffer size: %d x %d\n", m_bufferSize.width (),
360 m_bufferSize.height ());
361 log (" window: (%d, %d) -> (%d, %d) [%d x %d]\n",
362 m_consoleRect.left (), m_consoleRect.top (),
363 m_consoleRect.right (), m_consoleRect.bottom (),
364 m_consoleRect.width (), m_consoleRect.height ());
365
366 wchar_t titleBuf[260];
367 GetConsoleTitleW (titleBuf, sizeof (titleBuf));
368 q->setWindowTitle (QString::fromWCharArray (titleBuf));
369
370 m_font.setFamily ("Lucida Console");
371 m_font.setPointSize (9);
372 m_font.setStyleHint (QFont::TypeWriter);
373
374 m_buffer = m_tmpBuffer = 0;
375
376 m_consoleView = new QConsoleView (parent);
377 m_horizontalScrollBar = new QScrollBar (Qt::Horizontal, parent);
378 m_verticalScrollBar = new QScrollBar (Qt::Vertical, parent);
379
380 QGridLayout* l = new QGridLayout (parent);
381 l->setContentsMargins (0, 0, 0, 0);
382 l->setSpacing (0);
383 l->addWidget (m_consoleView, 0, 0);
384 l->addWidget (m_horizontalScrollBar, 1, 0);
385 l->addWidget (m_verticalScrollBar, 0, 1);
386
387 if (IsWindows7OrGreater ())
388 {
389 SetConsoleCP (65001);
390 SetConsoleOutputCP (65001);
391 }
392
393 // Choose 0 (0x0) as index into the Windows console color map for the
394 // background and 7 (0x7) as the index for the foreground. This
395 // selection corresponds to the indices used in the foregroundColor,
396 // setForegroundColor, backgroundColor, and SetBackgroundColor
397 // functions.
398
399 SetConsoleTextAttribute (m_stdOut, 0x07);
400
401 // Defaults.
402 setBackgroundColor (Qt::white);
403 setForegroundColor (Qt::black);
404 setSelectionColor (Qt::lightGray);
405 setCursorColor (false, Qt::darkGray);
406
407 // FIXME -- should we set the palette?
408 QPalette palette (backgroundColor ());
409 m_consoleView->setPalette (palette);
410
411 m_consoleView->setFont (m_font);
412 parent->setFocusPolicy (Qt::StrongFocus);
413 parent->winId ();
414
415 updateHorizontalScrollBar ();
416 updateVerticalScrollBar ();
417
418 m_consoleWatcher = new QTimer (parent);
419 m_consoleWatcher->setInterval (10);
420 m_consoleWatcher->setSingleShot (false);
421
422 m_blinkCursorTimer = new QTimer (parent);
423 QObject::connect (m_blinkCursorTimer, SIGNAL (timeout()),
424 q, SLOT (blinkCursorEvent ()));
425
426 QObject::connect (m_horizontalScrollBar, SIGNAL (valueChanged (int)),
427 q, SLOT (horizontalScrollValueChanged (int)));
428
429 QObject::connect (m_verticalScrollBar, SIGNAL (valueChanged (int)),
430 q, SLOT (verticalScrollValueChanged (int)));
431
432 QObject::connect (m_consoleWatcher, SIGNAL (timeout (void)),
433 q, SLOT (monitorConsole (void)));
434
435 m_consoleWatcher->start ();
436
437 if (m_command.isEmpty ())
438 m_consoleThread = 0;
439 else
440 {
441 m_consoleThread = new QConsoleThread (q);
442 QObject::connect (m_consoleThread, SIGNAL (finished (void)),
443 q, SIGNAL (terminated (void)));
444 m_consoleThread->start ();
445 }
446 }
447
448 //////////////////////////////////////////////////////////////////////////////
449
~QConsolePrivate(void)450 QConsolePrivate::~QConsolePrivate (void)
451 {
452 if (m_consoleThread && m_consoleThread->isRunning () && m_process)
453 {
454 TerminateProcess (m_process, (UINT)-1);
455 m_consoleThread->wait ();
456 }
457 if (m_buffer)
458 delete [] m_buffer;
459 if (m_tmpBuffer)
460 delete [] m_tmpBuffer;
461 }
462
463 //////////////////////////////////////////////////////////////////////////////
464
setupStandardIO(DWORD stdHandleId,int targetFd,const char * name,const char * devName)465 void QConsolePrivate::setupStandardIO (DWORD stdHandleId, int targetFd,
466 const char* name, const char* devName)
467 {
468 log ("Opening %s...\n", devName);
469
470 int fd = open (devName, _O_RDWR | _O_BINARY);
471
472 if (fd != -1)
473 {
474 if (fd != targetFd)
475 {
476 log ("Opened %s is not at target file descriptor %d, "
477 "duplicating...\n", name, targetFd);
478 if (dup2 (fd, targetFd) == -1)
479 log ("Failed to duplicate file descriptor: errno=%d.\n", errno);
480 if (close (fd) == -1)
481 log ("Failed to close original file descriptor: errno=%d.\n",
482 errno);
483 }
484 else
485 log ("%s opened and assigned to file descriptor %d.\n", devName, fd);
486 if (! SetStdHandle (stdHandleId, (HANDLE) _get_osfhandle (targetFd)))
487 log ("Failed to re-assign %s: error=%08x.\n", name, GetLastError ());
488 }
489 else
490 log ("Failed to open %s: errno=%d.\n", devName, errno);
491 }
492
posToCell(const QPoint & p)493 QPoint QConsolePrivate::posToCell (const QPoint& p)
494 {
495 return QPoint (m_consoleRect.left () + p.x () / m_charSize.width (),
496 m_consoleRect.top () + p.y () / m_charSize.height ());
497 }
498
getSelection(void)499 QString QConsolePrivate::getSelection (void)
500 {
501 QString selection;
502
503 QPoint begin = m_beginSelection;
504 QPoint end = m_endSelection;
505
506 maybeSwapPoints (begin, end);
507
508 if (begin != end)
509 {
510 CHAR_INFO* buf;
511 COORD bufSize, bufCoord;
512 SMALL_RECT bufRect;
513 int nr;
514
515 nr = end.y () - begin.y () + 1;
516 buf = new CHAR_INFO[m_bufferSize.width () * nr];
517 bufSize.X = m_bufferSize.width ();
518 bufSize.Y = nr;
519 bufCoord.X = 0;
520 bufCoord.Y = 0;
521
522 bufRect.Left = 0;
523 bufRect.Right = m_bufferSize.width ();
524 bufRect.Top = begin.y ();
525 bufRect.Bottom = end.y ();
526
527 if (ReadConsoleOutput (m_stdOut, buf, bufSize, bufCoord, &bufRect))
528 {
529 int start_pos = begin.x ();
530 int end_pos = (nr - 1) * m_bufferSize.width () + end.x ();
531 int lastNonSpace = -1;
532
533 for (int i = start_pos; i <= end_pos; i++)
534 {
535 if (i && (i % m_bufferSize.width ()) == 0)
536 {
537 if (lastNonSpace >= 0)
538 selection.truncate (lastNonSpace);
539 selection.append ('\n');
540 lastNonSpace = selection.length ();
541 }
542
543 QChar c (buf[i].Char.UnicodeChar);
544 if (c.isNull ())
545 c = QChar (' ');
546
547 selection.append (c);
548 if (! c.isSpace ())
549 lastNonSpace = selection.length ();
550 }
551
552 if (lastNonSpace >= 0)
553 selection.truncate (lastNonSpace);
554 }
555 }
556
557 return selection;
558 }
559
updateSelection(void)560 void QConsolePrivate::updateSelection (void)
561 {
562 QPoint begin = m_beginSelection;
563 QPoint end = m_endSelection;
564
565 maybeSwapPoints (begin, end);
566
567 begin.rx () = 0;
568 end.rx () = m_consoleRect.width ();
569
570 m_consoleView->update ();
571 }
572
clearSelection(void)573 void QConsolePrivate::clearSelection (void)
574 {
575 m_beginSelection = m_endSelection = QPoint ();
576
577 m_consoleView->update ();
578 }
579
backgroundColor(void) const580 QColor QConsolePrivate::backgroundColor (void) const
581 {
582 return m_colors[0];
583 }
584
foregroundColor(void) const585 QColor QConsolePrivate::foregroundColor (void) const
586 {
587 return m_colors[7];
588 }
589
selectionColor(void) const590 QColor QConsolePrivate::selectionColor (void) const
591 {
592 return m_selectionColor;
593 }
594
cursorColor(void) const595 QColor QConsolePrivate::cursorColor (void) const
596 {
597 return m_cursorColor.isValid () ? m_cursorColor : foregroundColor ();
598 }
599
setBackgroundColor(const QColor & color)600 void QConsolePrivate::setBackgroundColor (const QColor& color)
601 {
602 m_colors[0] = color;
603
604 QPalette palette (color);
605 palette.setColor(QPalette::Base, color);
606 m_consoleView->setPalette (palette);
607 }
608
setForegroundColor(const QColor & color)609 void QConsolePrivate::setForegroundColor (const QColor& color)
610 {
611 m_colors[7] = color;
612 }
613
setSelectionColor(const QColor & color)614 void QConsolePrivate::setSelectionColor (const QColor& color)
615 {
616 m_selectionColor = color;
617 }
618
setCursorColor(bool useForegroundColor,const QColor & color)619 void QConsolePrivate::setCursorColor (bool useForegroundColor,
620 const QColor& color)
621 {
622 m_cursorColor = useForegroundColor ? QColor () : color;
623 }
624
setScrollBufferSize(int value)625 void QConsolePrivate::setScrollBufferSize (int value)
626 {
627 CONSOLE_SCREEN_BUFFER_INFO sbi;
628 GetConsoleScreenBufferInfo (m_stdOut, &sbi);
629
630 m_bufferSize = QSize (sbi.dwSize.X, (SHORT)value);
631
632 updateConsoleSize (true);
633 }
634
drawTextBackground(QPainter & p,int cx1,int cy1,int cx2,int cy2,int cw,int ch)635 void QConsolePrivate::drawTextBackground (QPainter& p, int cx1, int cy1,
636 int cx2, int cy2, int cw, int ch)
637 {
638 p.save ();
639
640 int ascent = p.fontMetrics ().ascent ();
641 int stride = m_consoleRect.width ();
642 int y = ascent + cy1 * ch;;
643
644 for (int j = cy1; j <= cy2; j++, y += ch)
645 {
646 int len = 0;
647 bool hasChar = false;
648 int x = cx1 * cw;
649 WORD attr = 0;
650
651 for (int i = cx1; i <= cx2; i++)
652 {
653 CHAR_INFO* ci = &(m_buffer[stride*j+i]);
654
655 if ((ci->Attributes & 0x00ff) != attr)
656 {
657 // Character attributes changed
658 if (len != 0)
659 {
660 // String buffer not empty -> draw it
661 if (hasChar || (attr & 0x00f0))
662 {
663 if (attr & 0x00f0)
664 p.fillRect (x, y-ascent, len * cw, ch, p.brush ());
665 }
666
667 x += (len * cw);
668 len = 0;
669 hasChar = false;
670 }
671 // Update current brush and store current attributes
672 attr = (ci->Attributes & 0x00ff);
673 p.setBrush (m_colors[(attr >> 4) & 0x000f]);
674 }
675
676 // Append current character to the string buffer
677 len++;
678 if (ci->Char.UnicodeChar != L' ')
679 hasChar = true;
680 }
681
682 if (len != 0 && (hasChar || (attr & 0x00f0)))
683 {
684 // Line end reached, but string buffer not empty -> draw it
685 // No need to update s or x, they will be reset on the next
686 // for-loop iteration
687
688 if (attr & 0x00f0)
689 p.fillRect (x, y-ascent, len * cw, ch, p.brush ());
690 }
691 }
692
693 p.restore ();
694 }
695
selectAll()696 void QConsolePrivate::selectAll()
697 {
698 m_beginSelection = QPoint (0,0);
699 m_endSelection = QPoint(m_bufferSize.width (),
700 m_cursorPos.y());
701 updateSelection();
702 }
703
selectWord(const QPoint & cellpos)704 void QConsolePrivate::selectWord (const QPoint & cellpos)
705 {
706 QPoint begin = cellpos;
707 QPoint end = cellpos;
708
709 int stride = m_consoleRect.width ();
710
711 int verticalScrollOffset = m_consoleRect.top ();
712 int horizontalScrollOffset = m_consoleRect.left ();
713
714 // get begin, end in buffer offsets
715 begin.ry () -= verticalScrollOffset;
716 end.ry () -= verticalScrollOffset;
717
718 begin.rx () -= horizontalScrollOffset;
719 end.rx () -= horizontalScrollOffset;
720
721 // loog at current clicked on char to determinate ig getting space chunk or nonspace chunk
722 if (QChar(m_buffer[begin.y ()*stride + begin.x ()].Char.UnicodeChar).isSpace () == false)
723 {
724 // from current char, go back and fwd to find start and end of block
725 while(begin.x () > 0 &&
726 QChar(m_buffer[begin.y ()*stride + begin.x () -1].Char.UnicodeChar).isSpace() == false)
727 {
728 begin.rx () --;
729 }
730
731 while(end.x () < m_consoleRect.width () &&
732 QChar(m_buffer[end.y ()*stride + end.x () +1].Char.UnicodeChar).isSpace() == false)
733 {
734 end.rx () ++;
735 }
736 }
737 else
738 {
739 while(begin.x () > 0 &&
740 QChar(m_buffer[begin.y ()*stride + begin.x () -1].Char.UnicodeChar).isSpace())
741 {
742 begin.rx () --;
743 }
744
745 while(end.x () < m_consoleRect.width () &&
746 QChar(m_buffer[end.y ()*stride + end.x () +1].Char.UnicodeChar).isSpace ())
747 {
748 end.rx () ++;
749 }
750 }
751
752 // convert console offsets to absolute cell positions
753 begin.ry () += verticalScrollOffset;
754 end.ry () += verticalScrollOffset;
755
756 begin.rx () += horizontalScrollOffset;
757 end.rx () += horizontalScrollOffset;
758
759 m_beginSelection = begin;
760 m_endSelection = end;
761
762 updateSelection ();
763 }
764
selectLine(const QPoint & cellpos)765 void QConsolePrivate::selectLine (const QPoint & cellpos)
766 {
767 m_beginSelection = QPoint (0, cellpos.y ());
768 m_endSelection = QPoint (m_bufferSize.width ()-1, cellpos.y ());
769 updateSelection ();
770 }
771
772
drawSelection(QPainter & p,int cx1,int cy1,int cx2,int cy2,int cw,int ch)773 void QConsolePrivate::drawSelection (QPainter& p, int cx1, int cy1,
774 int cx2, int cy2, int cw, int ch)
775 {
776 p.save ();
777
778 QPoint begin = m_beginSelection;
779 QPoint end = m_endSelection;
780
781 bool haveSelection = (begin != end);
782
783 if (haveSelection)
784 maybeSwapPoints (begin, end);
785
786 int verticalScrollOffset = m_consoleRect.top ();
787 int horizontalScrollOffset = m_consoleRect.left ();
788
789 begin.ry () -= verticalScrollOffset;
790 end.ry () -= verticalScrollOffset;
791
792 begin.rx () -= horizontalScrollOffset;
793 end.rx () -= horizontalScrollOffset;
794
795 int ascent = p.fontMetrics ().ascent ();
796 int stride = m_consoleRect.width ();
797
798 int y = ascent + cy1 * ch;;
799 for (int j = cy1; j <= cy2; j++, y += ch)
800 {
801 int charsThisLine = 0;
802 int len = 0;
803 bool hasChar = false;
804 WORD attr = 0;
805
806 for (int i = cx1; i <= cx2; i++)
807 {
808 CHAR_INFO* ci = &(m_buffer[stride*j+i]);
809
810 if ((ci->Attributes & 0x00ff) != attr)
811 {
812 // Character attributes changed
813 if (len != 0)
814 {
815 charsThisLine += len;
816 len = 0;
817 hasChar = false;
818 }
819
820 // Store current attributes
821 attr = (ci->Attributes & 0x00ff);
822 }
823
824 // Append current character to the string buffer
825 len++;
826 if (ci->Char.UnicodeChar != L' ')
827 hasChar = true;
828 }
829
830 if (len != 0 && (hasChar || (attr & 0x00f0)))
831 charsThisLine += len;
832
833 if (haveSelection && j >= begin.y () && j <= end.y ())
834 {
835 int selectionBegin = j == begin.y () ? begin.x (): 0;
836
837 int len = ((j == end.y () && end.x () < charsThisLine)
838 ? end.x () - selectionBegin + 1
839 : stride - selectionBegin);
840
841 p.fillRect (selectionBegin * cw, y-ascent, len * cw, ch,
842 selectionColor ());
843 }
844 }
845
846 p.restore ();
847 }
848
drawCursor(QPainter & p)849 void QConsolePrivate::drawCursor (QPainter& p)
850 {
851 if (! m_cursorBlinking)
852 {
853 p.save ();
854
855 QRect rect = cursorRect ();
856 QColor color = cursorColor ();
857
858 p.setPen (color);
859
860 if (m_cursorType == QConsolePrivate::BlockCursor)
861 {
862 if (q->hasFocus ())
863 p.fillRect (rect, color);
864 else
865 {
866 // draw the cursor outline, adjusting the area so that
867 // it is draw entirely inside 'rect'
868
869 int penWidth = qMax (1, p.pen().width());
870
871 p.drawRect (rect.adjusted (penWidth/2, penWidth/2,
872 - penWidth/2 - penWidth%2,
873 - penWidth/2 - penWidth%2));
874 }
875 }
876 else if (m_cursorType == QConsolePrivate::UnderlineCursor)
877 {
878 p.drawLine (rect.left (), rect.bottom (),
879 rect.right (), rect.bottom ());
880 }
881 else if (m_cursorType == QConsolePrivate::IBeamCursor)
882 {
883 p.drawLine (rect.left (), rect.top (),
884 rect.left (), rect.bottom ());
885 }
886
887 p.restore ();
888 }
889 }
890
drawText(QPainter & p,int cx1,int cy1,int cx2,int cy2,int cw,int ch)891 void QConsolePrivate::drawText (QPainter& p, int cx1, int cy1,
892 int cx2, int cy2, int cw, int ch)
893 {
894 p.save ();
895
896 p.setFont (m_font);
897 p.setPen (foregroundColor ());
898
899 QString s;
900 s.reserve (cx2 - cx1 + 1);
901
902 int ascent = p.fontMetrics ().ascent ();
903 int stride = m_consoleRect.width ();
904
905 int y = ascent + cy1 * ch;;
906 for (int j = cy1; j <= cy2; j++, y += ch)
907 {
908 // Reset string buffer and starting X coordinate
909 s.clear ();
910 bool hasChar = false;
911 int x = cx1 * cw;
912 WORD attr = 0;
913
914 for (int i = cx1; i <= cx2; i++)
915 {
916 CHAR_INFO* ci = &(m_buffer[stride*j+i]);
917
918 if ((ci->Attributes & 0x00ff) != attr)
919 {
920 // Character attributes changed
921 if (! s.isEmpty ())
922 {
923 // String buffer not empty -> draw it
924 if (hasChar || (attr & 0x00f0))
925 p.drawText (x, y, s);
926
927 x += (s.length () * cw);
928 s.clear ();
929 hasChar = false;
930 }
931 // Update current pen and store current attributes
932 attr = (ci->Attributes & 0x00ff);
933 p.setPen (m_colors[attr & 0x000f]);
934 }
935
936 // Append current character to the string buffer
937 s.append (ci->Char.UnicodeChar);
938 if (ci->Char.UnicodeChar != L' ')
939 hasChar = true;
940 }
941
942 if (! s.isEmpty () && (hasChar || (attr & 0x00f0)))
943 {
944 // Line end reached, but string buffer not empty -> draw it
945 // No need to update s or x, they will be reset on the next
946 // for-loop iteration
947
948 p.drawText (x, y, s);
949 }
950 }
951
952 p.restore ();
953 }
954
955 /////////////////////////////////////////////////////////////////////////////
956
closeStandardIO(int fd,DWORD stdHandleId,const char * name)957 void QConsolePrivate::closeStandardIO (int fd, DWORD stdHandleId,
958 const char* name)
959 {
960 if (close (fd) == -1)
961 log ("Failed to close file descriptor %d: errno=%d.\n", fd, errno);
962 if (! CloseHandle (GetStdHandle (stdHandleId)))
963 log ("Failed to close Win32 %s: error=%08x.\n", name, GetLastError ());
964 }
965
966 //////////////////////////////////////////////////////////////////////////////
967
log(const char * fmt,...)968 void QConsolePrivate::log (const char* fmt, ...)
969 {
970 #ifdef DEBUG_QCONSOLE
971 if (fmt)
972 {
973 va_list l;
974 FILE* flog = fopen (LOGFILENAME, "ab");
975
976 va_start (l, fmt);
977 vfprintf (flog, fmt, l);
978 va_end (l);
979 fclose (flog);
980 }
981 else
982 {
983 // Special case to re-initialize the log file
984 FILE* flog = fopen (LOGFILENAME, "w");
985 fclose (flog);
986 }
987 #else
988 Q_UNUSED (fmt);
989 #endif
990 }
991
992 //////////////////////////////////////////////////////////////////////////////
993
updateConsoleSize(bool sync,bool allow_smaller_width)994 void QConsolePrivate::updateConsoleSize (bool sync, bool allow_smaller_width)
995 {
996 QFontMetrics fm = m_consoleView->fontMetrics ();
997 QSize winSize = m_consoleView->size ();
998
999 m_charSize.rwidth () = fm.averageCharWidth ();
1000 m_charSize.rheight () = fm.lineSpacing ();
1001
1002 m_consoleRect.setWidth (winSize.width () / fm.averageCharWidth ());
1003 m_consoleRect.setHeight (winSize.height () / fm.lineSpacing ());
1004
1005 // Don't shrink the size of the buffer. That way wide lines won't be
1006 // truncated and will reappear if the window is enlarged again later.
1007
1008 if (allow_smaller_width || m_consoleRect.width () > m_bufferSize.width ())
1009 m_bufferSize.rwidth () = m_consoleRect.width ();
1010
1011 if (qMax (m_bufferSize.height (), m_consoleRect.height ())
1012 > m_bufferSize.height ())
1013 m_bufferSize.rheight () = qMax (m_bufferSize.height (),
1014 m_consoleRect.height ());
1015
1016 // Store the terminal size in the environment. When Octave is
1017 // initialized, we ask the command editor (usually readline) to prefer
1018 // using these values rather than querying the terminal so that the
1019 // buffer size can be larger than the size of the window that the
1020 // command editor will actually use.
1021
1022 qputenv ("LINES", QByteArray::number (m_consoleRect.height ()));
1023 qputenv ("COLUMNS", QByteArray::number (m_consoleRect.width ()));
1024
1025 // Force the command line editor (usually readline) to notice the
1026 // change in screen size as soon as possible.
1027
1028 q->setSize (m_consoleRect.height (), m_consoleRect.width ());
1029
1030 m_consoleRect.moveLeft (0);
1031 if (m_consoleRect.bottom () >= m_bufferSize.height ())
1032 m_consoleRect.moveTop (m_bufferSize.height () - m_consoleRect.height ());
1033
1034 log ("Console resized:\n");
1035 log (" widget size: %d x %d\n", winSize.width (), winSize.height ());
1036 log (" buffer size: %d x %d\n", m_bufferSize.width (),
1037 m_bufferSize.height ());
1038 log (" window: (%d, %d) -> (%d, %d) [%d x %d]\n",
1039 m_consoleRect.left (), m_consoleRect.top (),
1040 m_consoleRect.right (), m_consoleRect.bottom (),
1041 m_consoleRect.width (), m_consoleRect.height ());
1042
1043 if (sync)
1044 syncConsoleParameters ();
1045
1046 updateHorizontalScrollBar ();
1047 updateVerticalScrollBar ();
1048 }
1049
1050 //////////////////////////////////////////////////////////////////////////////
1051
syncConsoleParameters(void)1052 void QConsolePrivate::syncConsoleParameters (void)
1053 {
1054 CONSOLE_SCREEN_BUFFER_INFO sbi;
1055 HANDLE hStdOut = m_stdOut;
1056
1057 GetConsoleScreenBufferInfo (hStdOut, &sbi);
1058
1059 COORD bs;
1060 SMALL_RECT sr;
1061
1062 bs.X = sbi.dwSize.X;
1063 bs.Y = m_bufferSize.height ();
1064 sr.Left = sbi.srWindow.Left;
1065 sr.Right = sbi.srWindow.Right;
1066 sr.Top = m_consoleRect.top ();
1067 sr.Bottom = m_consoleRect.bottom ();
1068
1069 if (bs.Y > sbi.dwSize.Y)
1070 {
1071 SetConsoleScreenBufferSize (hStdOut, bs);
1072 SetConsoleWindowInfo (hStdOut, TRUE, &sr);
1073 }
1074 else
1075 {
1076 SetConsoleWindowInfo (hStdOut, TRUE, &sr);
1077 SetConsoleScreenBufferSize (hStdOut, bs);
1078 }
1079
1080 bs.X = m_bufferSize.width ();
1081 sr.Left = m_consoleRect.left ();
1082 sr.Right = m_consoleRect.right ();
1083
1084 if (bs.X > sbi.dwSize.X)
1085 {
1086 SetConsoleScreenBufferSize (hStdOut, bs);
1087 SetConsoleWindowInfo (hStdOut, TRUE, &sr);
1088 }
1089 else
1090 {
1091 SetConsoleWindowInfo (hStdOut, TRUE, &sr);
1092 SetConsoleScreenBufferSize (hStdOut, bs);
1093 }
1094
1095 log ("Sync'ing console parameters:\n");
1096 log (" buffer size: %d x %d\n", bs.X, bs.Y);
1097 log (" window: (%d, %d) -> (%d, %d)\n",
1098 sr.Left, sr.Top, sr.Right, sr.Bottom);
1099
1100 if (m_buffer)
1101 delete [] m_buffer;
1102 if (m_tmpBuffer)
1103 delete [] m_tmpBuffer;
1104
1105 int bufSize = m_consoleRect.width () * m_consoleRect.height ();
1106
1107 m_buffer = new CHAR_INFO[bufSize];
1108 m_tmpBuffer = new CHAR_INFO[bufSize];
1109 }
1110
1111 //////////////////////////////////////////////////////////////////////////////
1112
grabConsoleBuffer(CHAR_INFO * buf)1113 void QConsolePrivate::grabConsoleBuffer (CHAR_INFO* buf)
1114 {
1115 COORD bs, bc;
1116 SMALL_RECT r;
1117
1118 bs.X = m_consoleRect.width ();
1119 bs.Y = m_consoleRect.height ();
1120 bc.X = 0;
1121 bc.Y = 0;
1122
1123 r.Left = m_consoleRect.left ();
1124 r.Top = m_consoleRect.top ();
1125 r.Right = m_consoleRect.right ();
1126 r.Bottom = m_consoleRect.bottom ();
1127
1128 log ("ReadConsoleOutput (%d,%d) -> (%d,%d)\n", r.Left, r.Top, r.Right, r.Bottom);
1129 if (! ReadConsoleOutput (m_stdOut, (buf ? buf : m_buffer), bs, bc, &r))
1130 qCritical ("cannot read console output");
1131 }
1132
1133 //////////////////////////////////////////////////////////////////////////////
1134
updateHorizontalScrollBar(void)1135 void QConsolePrivate::updateHorizontalScrollBar (void)
1136 {
1137 m_horizontalScrollBar->setMinimum (0);
1138 if (m_bufferSize.width () > m_consoleRect.width ())
1139 m_horizontalScrollBar->setMaximum (m_bufferSize.width () - m_consoleRect.width ());
1140 else
1141 m_horizontalScrollBar->setMaximum (0);
1142 m_horizontalScrollBar->setSingleStep (1);
1143 m_horizontalScrollBar->setPageStep (m_consoleRect.width ());
1144 m_horizontalScrollBar->setValue (m_consoleRect.left ());
1145
1146 log ("Horizontal scrollbar parameters updated: %d/%d/%d/%d\n",
1147 m_horizontalScrollBar->minimum (),
1148 m_horizontalScrollBar->maximum (),
1149 m_horizontalScrollBar->singleStep (),
1150 m_horizontalScrollBar->pageStep ());
1151 }
1152
updateVerticalScrollBar(void)1153 void QConsolePrivate::updateVerticalScrollBar (void)
1154 {
1155 m_verticalScrollBar->setMinimum (0);
1156 if (m_bufferSize.height () > m_consoleRect.height ())
1157 m_verticalScrollBar->setMaximum (m_bufferSize.height () - m_consoleRect.height ());
1158 else
1159 m_verticalScrollBar->setMaximum (0);
1160 m_verticalScrollBar->setSingleStep (1);
1161 m_verticalScrollBar->setPageStep (m_consoleRect.height ());
1162 m_verticalScrollBar->setValue (m_consoleRect.top ());
1163
1164 log ("Vertical scrollbar parameters updated: %d/%d/%d/%d\n",
1165 m_verticalScrollBar->minimum (), m_verticalScrollBar->maximum (),
1166 m_verticalScrollBar->singleStep (), m_verticalScrollBar->pageStep ());
1167 }
1168
1169 //////////////////////////////////////////////////////////////////////////////
1170
setHorizontalScrollValue(int value)1171 void QConsolePrivate::setHorizontalScrollValue (int value)
1172 {
1173 if (value == m_consoleRect.left ())
1174 return;
1175
1176 SMALL_RECT r;
1177 HANDLE hStdOut = m_stdOut;
1178
1179 if (value + m_consoleRect.width () > m_bufferSize.width ())
1180 value = m_bufferSize.width () - m_consoleRect.width ();
1181
1182 r.Left = value;
1183 r.Top = m_consoleRect.top ();
1184 r.Right = value + m_consoleRect.width () - 1;
1185 r.Bottom = m_consoleRect.bottom ();
1186
1187 log ("Scrolling window horizontally: (%d, %d) -> (%d, %d) [%d x %d]\n",
1188 r.Left, r.Top, r.Right, r.Bottom,
1189 r.Right - r.Left + 1, r.Bottom - r.Top + 1);
1190
1191 if (SetConsoleWindowInfo (hStdOut, TRUE, &r))
1192 {
1193 m_consoleRect.moveLeft (value);
1194 updateConsoleView ();
1195 }
1196 }
1197
setVerticalScrollValue(int value)1198 void QConsolePrivate::setVerticalScrollValue (int value)
1199 {
1200 if (value == m_consoleRect.top ())
1201 return;
1202
1203 SMALL_RECT r;
1204 HANDLE hStdOut = m_stdOut;
1205
1206 if (value + m_consoleRect.height () > m_bufferSize.height ())
1207 value = m_bufferSize.height () - m_consoleRect.height ();
1208
1209 r.Left = m_consoleRect.left ();
1210 r.Top = value;
1211 r.Right = m_consoleRect.right ();
1212 r.Bottom = value + m_consoleRect.height () - 1;
1213
1214 log ("Scrolling window vertically: (%d, %d) -> (%d, %d) [%d x %d]\n",
1215 r.Left, r.Top, r.Right, r.Bottom,
1216 r.Right - r.Left + 1, r.Bottom - r.Top + 1);
1217
1218 if (SetConsoleWindowInfo (hStdOut, TRUE, &r))
1219 {
1220 m_consoleRect.moveTop (value);
1221
1222 CONSOLE_SCREEN_BUFFER_INFO sbi;
1223 if (GetConsoleScreenBufferInfo (hStdOut, &sbi))
1224 {
1225 if (sbi.dwCursorPosition.Y > m_consoleRect.bottom ())
1226 m_auto_scroll = false;
1227 else
1228 m_auto_scroll = true;
1229 }
1230
1231 updateConsoleView ();
1232 }
1233 }
1234
1235 //////////////////////////////////////////////////////////////////////////////
1236
updateConsoleView(bool grab)1237 void QConsolePrivate::updateConsoleView (bool grab)
1238 {
1239 if (grab)
1240 grabConsoleBuffer ();
1241 m_consoleView->update ();
1242 m_consoleWatcher->start ();
1243 }
1244
1245 //////////////////////////////////////////////////////////////////////////////
1246
monitorConsole(void)1247 void QConsolePrivate::monitorConsole (void)
1248 {
1249 CONSOLE_SCREEN_BUFFER_INFO sbi;
1250 HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE);
1251
1252 static wchar_t titleBuf[260];
1253
1254 GetConsoleTitleW (titleBuf, sizeof (titleBuf));
1255 QString title = QString::fromWCharArray (titleBuf);
1256
1257 if (title != m_title)
1258 {
1259 q->setWindowTitle (title);
1260 emit q->titleChanged (title);
1261 }
1262
1263 if (GetConsoleScreenBufferInfo (hStdOut, &sbi))
1264 {
1265 if (m_bufferSize.width () != sbi.dwSize.X
1266 || m_bufferSize.height () != sbi.dwSize.Y)
1267 {
1268 // Buffer size changed
1269 m_bufferSize.rwidth () = sbi.dwSize.X;
1270 m_bufferSize.rheight () = sbi.dwSize.Y;
1271 updateHorizontalScrollBar ();
1272 updateVerticalScrollBar ();
1273 }
1274
1275 if (m_cursorPos.x () != sbi.dwCursorPosition.X
1276 || m_cursorPos.y () != sbi.dwCursorPosition.Y)
1277 {
1278 // Cursor position changed
1279 m_consoleView->update
1280 ((m_cursorPos.x () - sbi.srWindow.Left) * m_charSize.width (),
1281 (m_cursorPos.y () - sbi.srWindow.Top) * m_charSize.height (),
1282 m_charSize.width (), m_charSize.height ());
1283 m_cursorPos.rx () = sbi.dwCursorPosition.X;
1284 m_cursorPos.ry () = sbi.dwCursorPosition.Y;
1285 m_consoleView->update
1286 ((m_cursorPos.x () - sbi.srWindow.Left) * m_charSize.width (),
1287 (m_cursorPos.y () - sbi.srWindow.Top) * m_charSize.height (),
1288 m_charSize.width (), m_charSize.height ());
1289 }
1290
1291 if (m_consoleRect.left () != sbi.srWindow.Left
1292 || m_consoleRect.right () != sbi.srWindow.Right
1293 || (m_auto_scroll &&
1294 (m_consoleRect.top () != sbi.srWindow.Top
1295 || m_consoleRect.bottom () != sbi.srWindow.Bottom)))
1296 {
1297 // Console window changed
1298 log ("--> Console window changed\n");
1299 m_consoleRect = QRect (sbi.srWindow.Left, sbi.srWindow.Top,
1300 sbi.srWindow.Right - sbi.srWindow.Left + 1,
1301 sbi.srWindow.Bottom - sbi.srWindow.Top + 1);
1302 updateHorizontalScrollBar ();
1303 updateVerticalScrollBar ();
1304 updateConsoleView ();
1305 return;
1306 }
1307
1308 if (m_tmpBuffer && m_buffer)
1309 {
1310 grabConsoleBuffer (m_tmpBuffer);
1311 if (memcmp (m_tmpBuffer, m_buffer,
1312 sizeof (CHAR_INFO) * m_consoleRect.width () *
1313 m_consoleRect.height ()))
1314 {
1315 // FIXME: compute the area to update based on the
1316 // difference between the 2 buffers.
1317 qSwap (m_buffer, m_tmpBuffer);
1318 updateConsoleView (false);
1319 }
1320 }
1321 }
1322 }
1323
1324 //////////////////////////////////////////////////////////////////////////////
1325
startCommand(void)1326 void QConsolePrivate::startCommand (void)
1327 {
1328 QString cmd = m_command;
1329
1330 if (cmd.isEmpty ())
1331 cmd = qgetenv ("COMSPEC").constData ();
1332
1333 if (! cmd.isEmpty ())
1334 {
1335 STARTUPINFO si;
1336 PROCESS_INFORMATION pi;
1337
1338 ZeroMemory (&si, sizeof (si));
1339 si.cb = sizeof (si);
1340 ZeroMemory (&pi, sizeof (pi));
1341
1342 if (CreateProcessW (nullptr,
1343 (LPWSTR)cmd.unicode (),
1344 nullptr,
1345 nullptr,
1346 TRUE,
1347 0,
1348 nullptr,
1349 nullptr,
1350 &si,
1351 &pi))
1352 {
1353 CloseHandle (pi.hThread);
1354 m_process = pi.hProcess;
1355 WaitForSingleObject (m_process, INFINITE);
1356 CloseHandle (m_process);
1357 m_process = nullptr;
1358 }
1359 }
1360 }
1361
1362 //////////////////////////////////////////////////////////////////////////////
1363
sendConsoleText(const QString & s)1364 void QConsolePrivate::sendConsoleText (const QString& s)
1365 {
1366 // Send the string in chunks of 512 characters. Each character is
1367 // translated into an equivalent keypress event.
1368
1369 #define TEXT_CHUNK_SIZE 512
1370
1371 // clear any selection on inserting text
1372 clearSelection();
1373 // enable auto-scrolling
1374 m_auto_scroll = true;
1375
1376 int len = s.length ();
1377 INPUT_RECORD events[TEXT_CHUNK_SIZE];
1378 DWORD nEvents = 0, written;
1379 HANDLE hStdIn = GetStdHandle (STD_INPUT_HANDLE);
1380
1381 ZeroMemory (events, sizeof (events));
1382
1383 for (int i = 0; i < len; i++)
1384 {
1385 QChar c = s.at (i);
1386
1387 if (c == L'\r' || c == L'\n')
1388 {
1389 if (c == L'\r' && i < (len - 1) && s.at (i+1) == L'\n')
1390 i++;
1391
1392 // add new line
1393 events[nEvents].EventType = KEY_EVENT;
1394 events[nEvents].Event.KeyEvent.bKeyDown = TRUE;
1395 events[nEvents].Event.KeyEvent.wRepeatCount = 1;
1396 events[nEvents].Event.KeyEvent.wVirtualKeyCode =
1397 VK_RETURN;
1398 events[nEvents].Event.KeyEvent.wVirtualScanCode = 0;
1399 events[nEvents].Event.KeyEvent.uChar.UnicodeChar = c.unicode ();
1400 events[nEvents].Event.KeyEvent.dwControlKeyState = 0;
1401 nEvents++;
1402
1403 WriteConsoleInput (hStdIn, events, nEvents, &written);
1404 nEvents = 0;
1405 ZeroMemory (events, sizeof (events));
1406
1407 }
1408 else
1409 {
1410 events[nEvents].EventType = KEY_EVENT;
1411 events[nEvents].Event.KeyEvent.bKeyDown = TRUE;
1412 events[nEvents].Event.KeyEvent.wRepeatCount = 1;
1413 events[nEvents].Event.KeyEvent.wVirtualKeyCode =
1414 LOBYTE (VkKeyScan (c.unicode ()));
1415 events[nEvents].Event.KeyEvent.wVirtualScanCode = 0;
1416 events[nEvents].Event.KeyEvent.uChar.UnicodeChar = c.unicode ();
1417 events[nEvents].Event.KeyEvent.dwControlKeyState = 0;
1418 nEvents++;
1419 }
1420
1421 if (nEvents == TEXT_CHUNK_SIZE
1422 || (nEvents > 0 && i == (len - 1)))
1423 {
1424 WriteConsoleInput (hStdIn, events, nEvents, &written);
1425 nEvents = 0;
1426 ZeroMemory (events, sizeof (events));
1427 }
1428 }
1429 }
1430
1431 QRect
cursorRect(void)1432 QConsolePrivate::cursorRect (void)
1433 {
1434 int cw = m_charSize.width ();
1435 int ch = m_charSize.height ();
1436
1437 return QRect ((m_cursorPos.x () - m_consoleRect.x ()) * cw,
1438 (m_cursorPos.y () - m_consoleRect.y ()) * ch,
1439 cw, ch);
1440 }
1441
1442 //////////////////////////////////////////////////////////////////////////////
1443
QWinTerminalImpl(QWidget * parent)1444 QWinTerminalImpl::QWinTerminalImpl (QWidget* parent)
1445 : QTerminal (parent), d (new QConsolePrivate (this)),
1446 allowTripleClick (false)
1447 {
1448 installEventFilter (this);
1449
1450 connect (this, SIGNAL (set_global_shortcuts_signal (bool)),
1451 parent, SLOT (set_global_shortcuts (bool)));
1452 connect (this, SIGNAL (set_global_shortcuts_signal (bool)),
1453 this, SLOT (set_global_shortcuts (bool)));
1454
1455 connect (this, SIGNAL (set_screen_size_signal (int, int)),
1456 parent, SLOT (set_screen_size (int, int)));
1457
1458 setAcceptDrops (true);
1459 }
1460
1461 //////////////////////////////////////////////////////////////////////////////
1462
QWinTerminalImpl(const QString & cmd,QWidget * parent)1463 QWinTerminalImpl::QWinTerminalImpl (const QString& cmd, QWidget* parent)
1464 : QTerminal (parent), d (new QConsolePrivate (this, cmd))
1465 {
1466 }
1467
1468 //////////////////////////////////////////////////////////////////////////////
1469
~QWinTerminalImpl(void)1470 QWinTerminalImpl::~QWinTerminalImpl (void)
1471 {
1472 delete d;
1473 }
1474
mouseMoveEvent(QMouseEvent * event)1475 void QWinTerminalImpl::mouseMoveEvent (QMouseEvent *event)
1476 {
1477 if (d->m_settingSelection)
1478 {
1479 d->m_endSelection = d->posToCell (event->pos ());
1480
1481 updateSelection ();
1482 }
1483 }
1484
mousePressEvent(QMouseEvent * event)1485 void QWinTerminalImpl::mousePressEvent (QMouseEvent *event)
1486 {
1487 if (allowTripleClick)
1488 {
1489 mouseTripleClickEvent (event);
1490 }
1491 else if (event->button () == Qt::LeftButton)
1492 {
1493 d->m_settingSelection = true;
1494
1495 d->m_beginSelection = d->posToCell (event->pos ());
1496 }
1497 }
1498
mouseReleaseEvent(QMouseEvent * event)1499 void QWinTerminalImpl::mouseReleaseEvent (QMouseEvent *event)
1500 {
1501 if (event->button () == Qt::LeftButton && d->m_settingSelection)
1502 {
1503 d->m_endSelection = d->posToCell (event->pos ());
1504
1505 updateSelection ();
1506
1507 d->m_settingSelection = false;
1508 }
1509 }
1510
mouseDoubleClickEvent(QMouseEvent * event)1511 void QWinTerminalImpl::mouseDoubleClickEvent (QMouseEvent *event)
1512 {
1513 if (event->button () == Qt::LeftButton)
1514 {
1515 // doubleclick - select word
1516 d->m_settingSelection = false;
1517
1518 d->selectWord (d->posToCell (event->pos ()));
1519
1520 allowTripleClick = true;
1521
1522 QTimer::singleShot (QApplication::doubleClickInterval (),this,
1523 SLOT (tripleClickTimeout ()));
1524
1525 }
1526 }
1527
mouseTripleClickEvent(QMouseEvent * event)1528 void QWinTerminalImpl::mouseTripleClickEvent (QMouseEvent *event)
1529 {
1530 if (event->button () == Qt::LeftButton)
1531 {
1532 d->selectLine (d->posToCell (event->pos ()));
1533 }
1534 }
1535
tripleClickTimeout()1536 void QWinTerminalImpl::tripleClickTimeout ()
1537 {
1538 allowTripleClick = false;
1539 }
1540
1541 //////////////////////////////////////////////////////////////////////////////
1542
viewResizeEvent(QConsoleView *,QResizeEvent *)1543 void QWinTerminalImpl::viewResizeEvent (QConsoleView*, QResizeEvent*)
1544 {
1545 d->updateConsoleSize (true);
1546 d->grabConsoleBuffer ();
1547 }
1548
1549 //////////////////////////////////////////////////////////////////////////////
1550
viewPaintEvent(QConsoleView * w,QPaintEvent * event)1551 void QWinTerminalImpl::viewPaintEvent (QConsoleView* w, QPaintEvent* event)
1552 {
1553 QPainter p (w);
1554
1555 int cw = d->m_charSize.width ();
1556 int ch = d->m_charSize.height ();
1557
1558 QRect updateRect = event->rect ();
1559 p.fillRect(updateRect, QBrush(d->backgroundColor()));
1560
1561 int cx1 = updateRect.left () / cw;
1562 int cy1 = updateRect.top () / ch;
1563 int cx2 = qMin (d->m_consoleRect.width () - 1, updateRect.right () / cw);
1564 int cy2 = qMin (d->m_consoleRect.height () - 1, updateRect.bottom () / ch);
1565
1566 if (cx1 > d->m_consoleRect.width () - 1
1567 || cy1 > d->m_consoleRect.height () - 1)
1568 return;
1569
1570 d->drawTextBackground (p, cx1, cy1, cx2, cy2, cw, ch);
1571 d->drawSelection (p, cx1, cy1, cx2, cy2, cw, ch);
1572 d->drawCursor (p);
1573 d->drawText (p, cx1, cy1, cx2, cy2, cw, ch);
1574 }
1575
blinkCursorEvent(void)1576 void QWinTerminalImpl::blinkCursorEvent (void)
1577 {
1578 if (d->m_hasBlinkingCursor)
1579 d->m_cursorBlinking = ! d->m_cursorBlinking;
1580 else
1581 d->m_cursorBlinking = false;
1582
1583 d->m_consoleView->update (d->cursorRect ());
1584 }
1585
setBlinkingCursor(bool blink)1586 void QWinTerminalImpl::setBlinkingCursor (bool blink)
1587 {
1588 d->m_hasBlinkingCursor = blink;
1589
1590 setBlinkingCursorState (blink);
1591 }
1592
setBlinkingCursorState(bool blink)1593 void QWinTerminalImpl::setBlinkingCursorState (bool blink)
1594 {
1595 if (blink && ! d->m_blinkCursorTimer->isActive ())
1596 d->m_blinkCursorTimer->start (d->BLINK_DELAY);
1597
1598 if (! blink && d->m_blinkCursorTimer->isActive ())
1599 {
1600 d->m_blinkCursorTimer->stop ();
1601
1602 if (d->m_cursorBlinking)
1603 blinkCursorEvent ();
1604 }
1605 }
1606
1607 // Reset width of console buffer and terminal window to be the same.
1608
init_terminal_size(void)1609 void QWinTerminalImpl::init_terminal_size (void)
1610 {
1611 d->updateConsoleSize (true, true);
1612 }
1613
1614 //////////////////////////////////////////////////////////////////////////////
1615
wheelEvent(QWheelEvent * event)1616 void QWinTerminalImpl::wheelEvent (QWheelEvent* event)
1617 {
1618 if (! d->m_inWheelEvent)
1619 {
1620 // Forward to the scrollbar (avoid recursion)
1621 d->m_inWheelEvent = true;
1622 QApplication::sendEvent (d->m_verticalScrollBar, event);
1623 d->m_inWheelEvent = false;
1624 }
1625 }
1626
1627 //////////////////////////////////////////////////////////////////////////////
1628
horizontalScrollValueChanged(int value)1629 void QWinTerminalImpl::horizontalScrollValueChanged (int value)
1630 {
1631 d->setHorizontalScrollValue (value);
1632 }
1633
verticalScrollValueChanged(int value)1634 void QWinTerminalImpl::verticalScrollValueChanged (int value)
1635 {
1636 d->setVerticalScrollValue (value);
1637 }
1638
1639 //////////////////////////////////////////////////////////////////////////////
1640
monitorConsole(void)1641 void QWinTerminalImpl::monitorConsole (void)
1642 {
1643 d->monitorConsole ();
1644 }
1645
updateSelection(void)1646 void QWinTerminalImpl::updateSelection (void)
1647 {
1648 d->updateSelection ();
1649 }
1650
1651 //////////////////////////////////////////////////////////////////////////////
1652
focusInEvent(QFocusEvent * event)1653 void QWinTerminalImpl::focusInEvent (QFocusEvent* event)
1654 {
1655 emit set_global_shortcuts_signal (false); // disable some shortcuts
1656
1657 setBlinkingCursorState (true);
1658
1659 QWidget::focusInEvent (event);
1660 }
1661
focusOutEvent(QFocusEvent * event)1662 void QWinTerminalImpl::focusOutEvent (QFocusEvent* event)
1663 {
1664 emit set_global_shortcuts_signal (true); // re-enable shortcuts
1665
1666 // Force the cursor to be redrawn.
1667 d->m_cursorBlinking = true;
1668
1669 setBlinkingCursorState (false);
1670
1671 QWidget::focusOutEvent (event);
1672 }
1673
eventFilter(QObject * obj,QEvent * event)1674 bool QWinTerminalImpl::eventFilter (QObject *obj, QEvent * event)
1675 {
1676 // if a keypress, filter out tab keys so that the next/prev tabbing is
1677 // disabled - but we still need to pass along to the console .
1678 if (event->type () == QEvent::KeyPress)
1679 {
1680 QKeyEvent* k = static_cast<QKeyEvent*>(event);
1681 if (k->key () == Qt::Key_Tab)
1682 {
1683 sendText ("\t");
1684 return true;
1685 }
1686 }
1687 return false;
1688 }
1689
keyPressEvent(QKeyEvent * event)1690 void QWinTerminalImpl::keyPressEvent (QKeyEvent* event)
1691 {
1692 QString s = translateKey (event);
1693 if (!s.isEmpty ())
1694 sendText (s);
1695
1696 if (d->m_hasBlinkingCursor)
1697 {
1698 d->m_blinkCursorTimer->start (d->BLINK_DELAY);
1699
1700 if (d->m_cursorBlinking)
1701 blinkCursorEvent ();
1702 }
1703
1704 QWidget::keyPressEvent (event);
1705 }
1706
1707 //////////////////////////////////////////////////////////////////////////////
1708
start(void)1709 void QWinTerminalImpl::start (void)
1710 {
1711 d->startCommand ();
1712 }
1713
1714 //////////////////////////////////////////////////////////////////////////////
1715
sendText(const QString & s)1716 void QWinTerminalImpl::sendText (const QString& s)
1717 {
1718 d->sendConsoleText (s);
1719 }
1720
setCursorType(CursorType type,bool blinking)1721 void QWinTerminalImpl::setCursorType (CursorType type, bool blinking)
1722 {
1723 switch (type)
1724 {
1725 case UnderlineCursor:
1726 d->m_cursorType = QConsolePrivate::UnderlineCursor;
1727 break;
1728
1729 case BlockCursor:
1730 d->m_cursorType = QConsolePrivate::BlockCursor;
1731 break;
1732
1733 case IBeamCursor:
1734 d->m_cursorType = QConsolePrivate::IBeamCursor;
1735 break;
1736 }
1737
1738 setBlinkingCursor (blinking);
1739 }
1740
setBackgroundColor(const QColor & color)1741 void QWinTerminalImpl::setBackgroundColor (const QColor& color)
1742 {
1743 d->setBackgroundColor (color);
1744 }
1745
setForegroundColor(const QColor & color)1746 void QWinTerminalImpl::setForegroundColor (const QColor& color)
1747 {
1748 d->setForegroundColor (color);
1749 }
1750
setSelectionColor(const QColor & color)1751 void QWinTerminalImpl::setSelectionColor (const QColor& color)
1752 {
1753 d->setSelectionColor (color);
1754 }
1755
setCursorColor(bool useForegroundColor,const QColor & color)1756 void QWinTerminalImpl::setCursorColor (bool useForegroundColor,
1757 const QColor& color)
1758 {
1759 d->setCursorColor (useForegroundColor, color);
1760 }
1761
setScrollBufferSize(int value)1762 void QWinTerminalImpl::setScrollBufferSize(int value)
1763 {
1764 d->setScrollBufferSize (value);
1765 }
1766
1767
1768 //////////////////////////////////////////////////////////////////////////////
1769
setTerminalFont(const QFont & f)1770 void QWinTerminalImpl::setTerminalFont (const QFont& f)
1771 {
1772 d->m_font = f;
1773 d->m_consoleView->setFont (f);
1774 d->updateConsoleSize (true);
1775 }
1776
1777 //////////////////////////////////////////////////////////////////////////////
1778
setSize(int columns,int lines)1779 void QWinTerminalImpl::setSize (int columns, int lines)
1780 {
1781 d->log ("emit set_screen_size_signal (%d, %d)\n", columns, lines);
1782
1783 emit set_screen_size_signal (columns, lines);
1784 }
1785
1786 //////////////////////////////////////////////////////////////////////////////
1787
copyClipboard()1788 void QWinTerminalImpl::copyClipboard ()
1789 {
1790 if(!hasFocus()) return;
1791
1792 QClipboard *clipboard = QApplication::clipboard ();
1793
1794 QString selection = d->getSelection ();
1795
1796 if (selection.isEmpty ())
1797 {
1798 if (! _extra_interrupt)
1799 terminal_interrupt ();
1800 }
1801 else
1802 {
1803 clipboard->setText (selection);
1804 emit report_status_message (tr ("copied selection to clipboard"));
1805 }
1806 }
1807
1808 //////////////////////////////////////////////////////////////////////////////
1809
pasteClipboard(void)1810 void QWinTerminalImpl::pasteClipboard (void)
1811 {
1812 if(!hasFocus()) return;
1813
1814 QString text = QApplication::clipboard()->text (QClipboard::Clipboard);
1815
1816 if (! text.isEmpty ())
1817 sendText (text);
1818 }
1819
1820 //////////////////////////////////////////////////////////////////////////////
1821
selectAll(void)1822 void QWinTerminalImpl::selectAll (void)
1823 {
1824 if(!hasFocus()) return;
1825
1826 d->selectAll();
1827 }
1828
1829
1830
1831 //////////////////////////////////////////////////////////////////////////////
1832
selectedText()1833 QString QWinTerminalImpl::selectedText ()
1834 {
1835 QString selection = d->getSelection ();
1836 return selection;
1837 }
1838
1839 //////////////////////////////////////////////////////////////////////////////
1840
dragEnterEvent(QDragEnterEvent * event)1841 void QWinTerminalImpl::dragEnterEvent (QDragEnterEvent *event)
1842 {
1843 if (event->mimeData ()->hasUrls ())
1844 {
1845 event->acceptProposedAction();
1846 }
1847 }
1848
1849 //////////////////////////////////////////////////////////////////////////////
1850
dropEvent(QDropEvent * event)1851 void QWinTerminalImpl::dropEvent (QDropEvent *event)
1852 {
1853 QString dropText;
1854
1855 if (event->mimeData ()->hasUrls ())
1856 {
1857 foreach (QUrl url, event->mimeData ()->urls ())
1858 {
1859 if(dropText.length () > 0)
1860 dropText += '\n';
1861 dropText += url.toLocalFile ();
1862 }
1863 sendText (dropText);
1864 }
1865 }
1866
1867 //////////////////////////////////////////////////////////////////////////////
1868
has_extra_interrupt(bool extra)1869 void QWinTerminalImpl::has_extra_interrupt (bool extra)
1870 {
1871 _extra_interrupt = extra;
1872 }
1873