Mercurial > octave
comparison libqterminal/win32/QWinTerminalImpl.cpp @ 15651:845cebf281aa
Added files of QConsole.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Mon, 30 Jan 2012 11:23:13 +0100 |
parents | |
children | 2d6724358c12 |
comparison
equal
deleted
inserted
replaced
15650:ba360324035e | 15651:845cebf281aa |
---|---|
1 /* | |
2 | |
3 Copyright (C) 2011 Michael Goffioul. | |
4 | |
5 This file is part of QConsole. | |
6 | |
7 Foobar 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 QConsole 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 Foobar. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 */ | |
21 | |
22 #include <QApplication> | |
23 #include <QColor> | |
24 #include <QFont> | |
25 #include <QHBoxLayout> | |
26 #include <QPaintEvent> | |
27 #include <QPainter> | |
28 #include <QResizeEvent> | |
29 #include <QScrollBar> | |
30 #include <QtDebug> | |
31 #include <QThread> | |
32 #include <QTimer> | |
33 | |
34 #include <fcntl.h> | |
35 #include <io.h> | |
36 #include <stdio.h> | |
37 #include <stdarg.h> | |
38 #define WIN32_LEAN_AND_MEAN | |
39 #include <windows.h> | |
40 #include <cstring> | |
41 | |
42 #include "QConsole.h" | |
43 #include "QConsoleColors.h" | |
44 | |
45 // Uncomment to log activity to LOGFILENAME | |
46 // #define DEBUG_QCONSOLE | |
47 #define LOGFILENAME "QConsole.log" | |
48 // Uncomment to create hidden console window | |
49 #define HIDDEN_CONSOLE | |
50 | |
51 ////////////////////////////////////////////////////////////////////////////// | |
52 | |
53 class QConsoleView : public QWidget | |
54 { | |
55 public: | |
56 QConsoleView (QConsole* parent = 0) : QWidget (parent), q (parent) { } | |
57 ~QConsoleView (void) { } | |
58 | |
59 protected: | |
60 void paintEvent (QPaintEvent* event) { q->viewPaintEvent (this, event); } | |
61 void resizeEvent (QResizeEvent* event) { q->viewResizeEvent (this, event); } | |
62 | |
63 private: | |
64 QConsole* q; | |
65 }; | |
66 | |
67 ////////////////////////////////////////////////////////////////////////////// | |
68 | |
69 class QConsoleThread : public QThread | |
70 { | |
71 public: | |
72 QConsoleThread (QConsole* console) : QThread (console), q (console) { } | |
73 | |
74 protected: | |
75 void run (void) | |
76 { q->start (); } | |
77 | |
78 private: | |
79 QConsole* q; | |
80 }; | |
81 | |
82 ////////////////////////////////////////////////////////////////////////////// | |
83 | |
84 class QConsolePrivate | |
85 { | |
86 friend class QConsole; | |
87 | |
88 public: | |
89 QConsolePrivate (QConsole* parent, const QString& cmd = QString ()); | |
90 ~QConsolePrivate (void); | |
91 | |
92 void updateConsoleSize (bool sync = false); | |
93 void syncConsoleParameters (void); | |
94 void grabConsoleBuffer (CHAR_INFO* buf = 0); | |
95 void updateScrollBar (void); | |
96 void setScrollValue (int value); | |
97 void updateConsoleView (bool grab = true); | |
98 void monitorConsole (void); | |
99 void startCommand (void); | |
100 void sendConsoleText (const QString& s); | |
101 | |
102 void log (const char* fmt, ...); | |
103 | |
104 void closeStandardIO (int fd, DWORD stdHandleId, const char* name); | |
105 void setupStandardIO (DWORD stdHandleId, int fd, const char* name, | |
106 const char* devName); | |
107 | |
108 private: | |
109 QConsole* q; | |
110 | |
111 private: | |
112 QFont m_font; | |
113 QColor m_backgroundColor; | |
114 QString m_command; | |
115 QConsoleColors m_colors; | |
116 bool m_inWheelEvent; | |
117 QString m_title; | |
118 | |
119 QSize m_charSize; | |
120 QSize m_bufferSize; | |
121 QRect m_consoleRect; | |
122 QPoint m_cursorPos; | |
123 | |
124 HANDLE m_stdOut; | |
125 HWND m_consoleWindow; | |
126 CHAR_INFO* m_buffer; | |
127 CHAR_INFO* m_tmpBuffer; | |
128 HANDLE m_process; | |
129 | |
130 QConsoleView* m_consoleView; | |
131 QScrollBar* m_scrollBar; | |
132 QTimer* m_consoleWatcher; | |
133 QConsoleThread *m_consoleThread; | |
134 }; | |
135 | |
136 ////////////////////////////////////////////////////////////////////////////// | |
137 | |
138 QConsolePrivate::QConsolePrivate (QConsole* parent, const QString& cmd) | |
139 : q (parent), m_command (cmd), m_process (NULL), m_inWheelEvent (false) | |
140 { | |
141 log (NULL); | |
142 | |
143 // Possibly detach from any existing console | |
144 log ("Detaching from existing console (if any)...\n"); | |
145 FreeConsole (); | |
146 log ("Closing standard IO...\n"); | |
147 closeStandardIO (0, STD_INPUT_HANDLE, "STDIN"); | |
148 closeStandardIO (1, STD_OUTPUT_HANDLE, "STDOUT"); | |
149 closeStandardIO (2, STD_ERROR_HANDLE, "STDERR"); | |
150 | |
151 #ifdef HIDDEN_CONSOLE | |
152 HWINSTA hOrigSta, hNewSta; | |
153 | |
154 // Create new (hidden) console | |
155 hOrigSta = GetProcessWindowStation (); | |
156 hNewSta = CreateWindowStation (NULL, 0, GENERIC_ALL, NULL); | |
157 log ("Current Windows station: %p.\nNew Windows station: %p.\n", hOrigSta, | |
158 hNewSta); | |
159 if (! SetProcessWindowStation (hNewSta)) | |
160 log ("Failed to switch to new Windows station.\n"); | |
161 #endif | |
162 if (! AllocConsole ()) | |
163 log ("Failed to create new console.\n"); | |
164 #ifdef HIDDEN_CONSOLE | |
165 if (! SetProcessWindowStation (hOrigSta)) | |
166 log ("Failed to restore original Windows station.\n"); | |
167 if (! CloseWindowStation (hNewSta)) | |
168 log ("Failed to close new Windows station.\n"); | |
169 #endif | |
170 | |
171 log ("New (hidden) console created.\n"); | |
172 | |
173 setupStandardIO (STD_INPUT_HANDLE, 0, "STDIN", "CONIN$"); | |
174 setupStandardIO (STD_OUTPUT_HANDLE, 1, "STDOUT", "CONOUT$"); | |
175 setupStandardIO (STD_ERROR_HANDLE, 2, "STDERR", "CONOUT$"); | |
176 | |
177 log ("Standard input/output/error set up.\n"); | |
178 | |
179 *stdin = *(fdopen (0, "rb")); | |
180 *stdout = *(fdopen (1, "wb")); | |
181 *stderr = *(fdopen (2, "wb")); | |
182 | |
183 log ("POSIX standard streams created.\n"); | |
184 | |
185 setvbuf (stdin, NULL, _IONBF, 0); | |
186 setvbuf (stdout, NULL, _IONBF, 0); | |
187 setvbuf (stderr, NULL, _IONBF, 0); | |
188 | |
189 log ("POSIX standard stream buffers adjusted.\n"); | |
190 | |
191 HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE); | |
192 | |
193 log ("Console allocated: hStdOut: %p\n", hStdOut); | |
194 | |
195 m_stdOut = hStdOut; | |
196 m_consoleWindow = GetConsoleWindow (); | |
197 | |
198 // In case the console window hasn't been created hidden... | |
199 ShowWindow (m_consoleWindow, SW_HIDE); | |
200 | |
201 CONSOLE_SCREEN_BUFFER_INFO sbi; | |
202 | |
203 GetConsoleScreenBufferInfo (hStdOut, &sbi); | |
204 m_bufferSize = QSize (sbi.dwSize.X, qMax (sbi.dwSize.Y, (SHORT)500)); | |
205 m_consoleRect = QRect (sbi.srWindow.Left, sbi.srWindow.Top, | |
206 sbi.srWindow.Right - sbi.srWindow.Left + 1, | |
207 sbi.srWindow.Bottom - sbi.srWindow.Top + 1); | |
208 m_cursorPos = QPoint (sbi.dwCursorPosition.X, sbi.dwCursorPosition.Y); | |
209 | |
210 log ("Initial console parameters:\n"); | |
211 log (" buffer size: %d x %d\n", m_bufferSize.width (), | |
212 m_bufferSize.height ()); | |
213 log (" window: (%d, %d) -> (%d, %d) [%d x %d]\n", | |
214 m_consoleRect.left (), m_consoleRect.top (), | |
215 m_consoleRect.right (), m_consoleRect.bottom (), | |
216 m_consoleRect.width (), m_consoleRect.height ()); | |
217 | |
218 wchar_t titleBuf[260]; | |
219 GetConsoleTitleW (titleBuf, sizeof (titleBuf)); | |
220 q->setWindowTitle (QString::fromWCharArray (titleBuf)); | |
221 | |
222 m_font.setFamily ("Lucida Console"); | |
223 m_font.setPointSize (9); | |
224 m_font.setStyleHint (QFont::TypeWriter); | |
225 m_backgroundColor = Qt::black; | |
226 | |
227 m_buffer = m_tmpBuffer = 0; | |
228 | |
229 m_consoleView = new QConsoleView (parent); | |
230 m_scrollBar = new QScrollBar (Qt::Vertical, parent); | |
231 | |
232 QHBoxLayout* l = new QHBoxLayout (parent); | |
233 l->setContentsMargins (0, 0, 0, 0); | |
234 l->setSpacing (0); | |
235 l->addWidget (m_consoleView, 1); | |
236 l->addWidget (m_scrollBar, 0); | |
237 | |
238 m_consoleView->setPalette (QPalette (m_backgroundColor)); | |
239 m_consoleView->setAutoFillBackground (true); | |
240 m_consoleView->setFont (m_font); | |
241 parent->setFocusPolicy (Qt::StrongFocus); | |
242 parent->winId (); | |
243 | |
244 updateScrollBar (); | |
245 | |
246 m_consoleWatcher = new QTimer (parent); | |
247 m_consoleWatcher->setInterval (10); | |
248 m_consoleWatcher->setSingleShot (false); | |
249 | |
250 QObject::connect (m_scrollBar, SIGNAL (valueChanged (int)), | |
251 q, SLOT (scrollValueChanged (int))); | |
252 QObject::connect (m_consoleWatcher, SIGNAL (timeout (void)), | |
253 q, SLOT (monitorConsole (void))); | |
254 | |
255 m_consoleWatcher->start (); | |
256 | |
257 if (m_command.isEmpty ()) | |
258 m_consoleThread = 0; | |
259 else | |
260 { | |
261 m_consoleThread = new QConsoleThread (q); | |
262 QObject::connect (m_consoleThread, SIGNAL (finished (void)), | |
263 q, SIGNAL (terminated (void))); | |
264 m_consoleThread->start (); | |
265 } | |
266 } | |
267 | |
268 ////////////////////////////////////////////////////////////////////////////// | |
269 | |
270 QConsolePrivate::~QConsolePrivate (void) | |
271 { | |
272 if (m_consoleThread && m_consoleThread->isRunning () && m_process) | |
273 { | |
274 TerminateProcess (m_process, (UINT)-1); | |
275 m_consoleThread->wait (); | |
276 } | |
277 if (m_buffer) | |
278 delete [] m_buffer; | |
279 if (m_tmpBuffer) | |
280 delete [] m_tmpBuffer; | |
281 } | |
282 | |
283 ////////////////////////////////////////////////////////////////////////////// | |
284 | |
285 void QConsolePrivate::setupStandardIO (DWORD stdHandleId, int targetFd, | |
286 const char* name, const char* devName) | |
287 { | |
288 log ("Opening %s...\n", devName); | |
289 | |
290 int fd = open (devName, _O_RDWR | _O_BINARY); | |
291 | |
292 if (fd != -1) | |
293 { | |
294 if (fd != targetFd) | |
295 { | |
296 log ("Opened %s is not at target file descriptor %d, " | |
297 "duplicating...\n", name, targetFd); | |
298 if (dup2 (fd, targetFd) == -1) | |
299 log ("Failed to duplicate file descriptor: errno=%d.\n", errno); | |
300 if (close (fd) == -1) | |
301 log ("Failed to close original file descriptor: errno=%d.\n", | |
302 errno); | |
303 } | |
304 else | |
305 log ("%s opened and assigned to file descriptor %d.\n", devName, fd); | |
306 if (! SetStdHandle (stdHandleId, (HANDLE) _get_osfhandle (targetFd))) | |
307 log ("Failed to re-assign %s: error=%08x.\n", name, GetLastError ()); | |
308 } | |
309 else | |
310 log ("Failed to open %s: errno=%d.\n", devName, errno); | |
311 } | |
312 | |
313 ////////////////////////////////////////////////////////////////////////////// | |
314 | |
315 void QConsolePrivate::closeStandardIO (int fd, DWORD stdHandleId, | |
316 const char* name) | |
317 { | |
318 if (close (fd) == -1) | |
319 log ("Failed to close file descriptor %d: errno=%d.\n", fd, errno); | |
320 if (! CloseHandle (GetStdHandle (stdHandleId))) | |
321 log ("Failed to close Win32 %s: error=%08x.\n", name, GetLastError ()); | |
322 } | |
323 | |
324 ////////////////////////////////////////////////////////////////////////////// | |
325 | |
326 void QConsolePrivate::log (const char* fmt, ...) | |
327 { | |
328 #ifdef DEBUG_QCONSOLE | |
329 if (fmt) | |
330 { | |
331 va_list l; | |
332 FILE* flog = fopen (LOGFILENAME, "ab"); | |
333 | |
334 va_start (l, fmt); | |
335 vfprintf (flog, fmt, l); | |
336 va_end (l); | |
337 fclose (flog); | |
338 } | |
339 else | |
340 { | |
341 // Special case to re-initialize the log file | |
342 FILE* flog = fopen (LOGFILENAME, "w"); | |
343 fclose (flog); | |
344 } | |
345 #endif | |
346 } | |
347 | |
348 ////////////////////////////////////////////////////////////////////////////// | |
349 | |
350 void QConsolePrivate::updateConsoleSize (bool sync) | |
351 { | |
352 QFontMetrics fm (m_font); | |
353 QSize winSize = m_consoleView->size (); | |
354 | |
355 m_charSize.rwidth () = fm.maxWidth (); | |
356 m_charSize.rheight () = fm.lineSpacing (); | |
357 | |
358 m_consoleRect.setWidth (winSize.width () / fm.maxWidth ()); | |
359 m_consoleRect.setHeight (winSize.height () / fm.lineSpacing ()); | |
360 | |
361 m_bufferSize.rwidth () = m_consoleRect.width (); | |
362 m_bufferSize.rheight () = qMax (m_bufferSize.height (), | |
363 m_consoleRect.height ()); | |
364 | |
365 m_consoleRect.moveLeft (0); | |
366 if (m_consoleRect.bottom () >= m_bufferSize.height ()) | |
367 m_consoleRect.moveTop (m_bufferSize.height () - m_consoleRect.height ()); | |
368 | |
369 log ("Console resized:\n"); | |
370 log (" widget size: %d x %d\n", winSize.width (), winSize.height ()); | |
371 log (" buffer size: %d x %d\n", m_bufferSize.width (), | |
372 m_bufferSize.height ()); | |
373 log (" window: (%d, %d) -> (%d, %d) [%d x %d]\n", | |
374 m_consoleRect.left (), m_consoleRect.top (), | |
375 m_consoleRect.right (), m_consoleRect.bottom (), | |
376 m_consoleRect.width (), m_consoleRect.height ()); | |
377 | |
378 if (sync) | |
379 syncConsoleParameters (); | |
380 | |
381 updateScrollBar (); | |
382 } | |
383 | |
384 ////////////////////////////////////////////////////////////////////////////// | |
385 | |
386 void QConsolePrivate::syncConsoleParameters (void) | |
387 { | |
388 CONSOLE_SCREEN_BUFFER_INFO sbi; | |
389 HANDLE hStdOut = m_stdOut; | |
390 | |
391 GetConsoleScreenBufferInfo (hStdOut, &sbi); | |
392 | |
393 COORD bs; | |
394 SMALL_RECT sr; | |
395 | |
396 bs.X = sbi.dwSize.X; | |
397 bs.Y = m_bufferSize.height (); | |
398 sr.Left = sbi.srWindow.Left; | |
399 sr.Right = sbi.srWindow.Right; | |
400 sr.Top = m_consoleRect.top (); | |
401 sr.Bottom = m_consoleRect.bottom (); | |
402 | |
403 if (bs.Y > sbi.dwSize.Y) | |
404 { | |
405 SetConsoleScreenBufferSize (hStdOut, bs); | |
406 SetConsoleWindowInfo (hStdOut, TRUE, &sr); | |
407 } | |
408 else | |
409 { | |
410 SetConsoleWindowInfo (hStdOut, TRUE, &sr); | |
411 SetConsoleScreenBufferSize (hStdOut, bs); | |
412 } | |
413 | |
414 bs.X = m_bufferSize.width (); | |
415 sr.Left = m_consoleRect.left (); | |
416 sr.Right = m_consoleRect.right (); | |
417 | |
418 if (bs.X > sbi.dwSize.X) | |
419 { | |
420 SetConsoleScreenBufferSize (hStdOut, bs); | |
421 SetConsoleWindowInfo (hStdOut, TRUE, &sr); | |
422 } | |
423 else | |
424 { | |
425 SetConsoleWindowInfo (hStdOut, TRUE, &sr); | |
426 SetConsoleScreenBufferSize (hStdOut, bs); | |
427 } | |
428 | |
429 log ("Sync'ing console parameters:\n"); | |
430 log (" buffer size: %d x %d\n", bs.X, bs.Y); | |
431 log (" window: (%d, %d) -> (%d, %d)\n", | |
432 sr.Left, sr.Top, sr.Right, sr.Bottom); | |
433 | |
434 if (m_buffer) | |
435 delete [] m_buffer; | |
436 if (m_tmpBuffer) | |
437 delete [] m_tmpBuffer; | |
438 | |
439 int bufSize = m_consoleRect.width () * m_consoleRect.height (); | |
440 | |
441 m_buffer = new CHAR_INFO[bufSize]; | |
442 m_tmpBuffer = new CHAR_INFO[bufSize]; | |
443 } | |
444 | |
445 ////////////////////////////////////////////////////////////////////////////// | |
446 | |
447 void QConsolePrivate::grabConsoleBuffer (CHAR_INFO* buf) | |
448 { | |
449 COORD bs, bc; | |
450 SMALL_RECT r; | |
451 | |
452 bs.X = m_consoleRect.width (); | |
453 bs.Y = m_consoleRect.height (); | |
454 bc.X = 0; | |
455 bc.Y = 0; | |
456 | |
457 r.Left = m_consoleRect.left (); | |
458 r.Top = m_consoleRect.top (); | |
459 r.Right = m_consoleRect.right (); | |
460 r.Bottom = m_consoleRect.bottom (); | |
461 | |
462 if (! ReadConsoleOutput (m_stdOut, (buf ? buf : m_buffer), bs, bc, &r)) | |
463 qCritical ("cannot read console output"); | |
464 } | |
465 | |
466 ////////////////////////////////////////////////////////////////////////////// | |
467 | |
468 void QConsolePrivate::updateScrollBar (void) | |
469 { | |
470 m_scrollBar->setMinimum (0); | |
471 if (m_bufferSize.height () > m_consoleRect.height ()) | |
472 m_scrollBar->setMaximum (m_bufferSize.height () - m_consoleRect.height ()); | |
473 else | |
474 m_scrollBar->setMaximum (0); | |
475 m_scrollBar->setSingleStep (1); | |
476 m_scrollBar->setPageStep (m_consoleRect.height ()); | |
477 m_scrollBar->setValue (m_consoleRect.top ()); | |
478 | |
479 log ("Scrollbar parameters updated: %d/%d/%d/%d\n", | |
480 m_scrollBar->minimum (), m_scrollBar->maximum (), | |
481 m_scrollBar->singleStep (), m_scrollBar->pageStep ()); | |
482 } | |
483 | |
484 ////////////////////////////////////////////////////////////////////////////// | |
485 | |
486 void QConsolePrivate::setScrollValue (int value) | |
487 { | |
488 if (value == m_consoleRect.top ()) | |
489 return; | |
490 | |
491 SMALL_RECT r; | |
492 HANDLE hStdOut = m_stdOut; | |
493 | |
494 if (value + m_consoleRect.height () > m_bufferSize.height ()) | |
495 value = m_bufferSize.height () - m_consoleRect.height (); | |
496 | |
497 r.Left = m_consoleRect.left (); | |
498 r.Top = value; | |
499 r.Right = m_consoleRect.right (); | |
500 r.Bottom = value + m_consoleRect.height () - 1; | |
501 | |
502 log ("Scrolling window: (%d, %d) -> (%d, %d) [%d x %d]\n", | |
503 r.Left, r.Top, r.Right, r.Bottom, | |
504 r.Right - r.Left + 1, r.Bottom - r.Top + 1); | |
505 | |
506 if (SetConsoleWindowInfo (hStdOut, TRUE, &r)) | |
507 { | |
508 m_consoleRect.moveTop (value); | |
509 updateConsoleView (); | |
510 } | |
511 } | |
512 | |
513 ////////////////////////////////////////////////////////////////////////////// | |
514 | |
515 void QConsolePrivate::updateConsoleView (bool grab) | |
516 { | |
517 if (grab) | |
518 grabConsoleBuffer (); | |
519 m_consoleView->update (); | |
520 m_consoleWatcher->start (); | |
521 } | |
522 | |
523 ////////////////////////////////////////////////////////////////////////////// | |
524 | |
525 void QConsolePrivate::monitorConsole (void) | |
526 { | |
527 CONSOLE_SCREEN_BUFFER_INFO sbi; | |
528 HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE); | |
529 | |
530 static wchar_t titleBuf[260]; | |
531 | |
532 GetConsoleTitleW (titleBuf, sizeof (titleBuf)); | |
533 QString title = QString::fromWCharArray (titleBuf); | |
534 | |
535 if (title != m_title) | |
536 { | |
537 q->setWindowTitle (title); | |
538 emit q->titleChanged (title); | |
539 } | |
540 | |
541 if (GetConsoleScreenBufferInfo (hStdOut, &sbi)) | |
542 { | |
543 if (m_bufferSize.width () != sbi.dwSize.X | |
544 || m_bufferSize.height () != sbi.dwSize.Y) | |
545 { | |
546 // Buffer size changed | |
547 m_bufferSize.rwidth () = sbi.dwSize.X; | |
548 m_bufferSize.rheight () = sbi.dwSize.Y; | |
549 updateScrollBar (); | |
550 } | |
551 | |
552 if (m_cursorPos.x () != sbi.dwCursorPosition.X | |
553 || m_cursorPos.y () != sbi.dwCursorPosition.Y) | |
554 { | |
555 // Cursor position changed | |
556 m_consoleView->update | |
557 ((m_cursorPos.x () - sbi.srWindow.Left) * m_charSize.width (), | |
558 (m_cursorPos.y () - sbi.srWindow.Top) * m_charSize.height (), | |
559 m_charSize.width (), m_charSize.height ()); | |
560 m_cursorPos.rx () = sbi.dwCursorPosition.X; | |
561 m_cursorPos.ry () = sbi.dwCursorPosition.Y; | |
562 m_consoleView->update | |
563 ((m_cursorPos.x () - sbi.srWindow.Left) * m_charSize.width (), | |
564 (m_cursorPos.y () - sbi.srWindow.Top) * m_charSize.height (), | |
565 m_charSize.width (), m_charSize.height ()); | |
566 } | |
567 | |
568 if (m_consoleRect.left () != sbi.srWindow.Left | |
569 || m_consoleRect.right () != sbi.srWindow.Right | |
570 || m_consoleRect.top () != sbi.srWindow.Top | |
571 || m_consoleRect.bottom () != sbi.srWindow.Bottom) | |
572 { | |
573 // Console window changed | |
574 m_consoleRect = QRect (sbi.srWindow.Left, sbi.srWindow.Top, | |
575 sbi.srWindow.Right - sbi.srWindow.Left + 1, | |
576 sbi.srWindow.Bottom - sbi.srWindow.Top + 1); | |
577 updateScrollBar (); | |
578 updateConsoleView (); | |
579 return; | |
580 } | |
581 | |
582 if (m_tmpBuffer && m_buffer) | |
583 { | |
584 grabConsoleBuffer (m_tmpBuffer); | |
585 if (memcmp (m_tmpBuffer, m_buffer, | |
586 sizeof (CHAR_INFO) * m_consoleRect.width () * | |
587 m_consoleRect.height ())) | |
588 { | |
589 // FIXME: compute the area to update based on the | |
590 // difference between the 2 buffers. | |
591 qSwap (m_buffer, m_tmpBuffer); | |
592 updateConsoleView (false); | |
593 } | |
594 } | |
595 } | |
596 } | |
597 | |
598 ////////////////////////////////////////////////////////////////////////////// | |
599 | |
600 void QConsolePrivate::startCommand (void) | |
601 { | |
602 if (! m_command.isEmpty ()) | |
603 { | |
604 STARTUPINFO si; | |
605 PROCESS_INFORMATION pi; | |
606 | |
607 ZeroMemory (&si, sizeof (si)); | |
608 si.cb = sizeof (si); | |
609 ZeroMemory (&pi, sizeof (pi)); | |
610 | |
611 if (CreateProcessW (NULL, | |
612 (LPWSTR)m_command.unicode (), | |
613 NULL, | |
614 NULL, | |
615 TRUE, | |
616 0, | |
617 NULL, | |
618 NULL, | |
619 &si, | |
620 &pi)) | |
621 { | |
622 CloseHandle (pi.hThread); | |
623 m_process = pi.hProcess; | |
624 WaitForSingleObject (m_process, INFINITE); | |
625 CloseHandle (m_process); | |
626 m_process = NULL; | |
627 } | |
628 } | |
629 } | |
630 | |
631 ////////////////////////////////////////////////////////////////////////////// | |
632 | |
633 void QConsolePrivate::sendConsoleText (const QString& s) | |
634 { | |
635 // Send the string in chunks of 512 characters. Each character is | |
636 // translated into an equivalent keypress event. | |
637 | |
638 #define TEXT_CHUNK_SIZE 512 | |
639 | |
640 int len = s.length (); | |
641 INPUT_RECORD events[TEXT_CHUNK_SIZE]; | |
642 DWORD nEvents = 0, written; | |
643 HANDLE hStdIn = GetStdHandle (STD_INPUT_HANDLE); | |
644 | |
645 ZeroMemory (events, sizeof (events)); | |
646 | |
647 for (int i = 0; i < len; i++) | |
648 { | |
649 QChar c = s.at (i); | |
650 | |
651 if (c == L'\r' || c == L'\n') | |
652 { | |
653 if (c == L'\r' && i < (len - 1) && s.at (i+1) == L'\n') | |
654 i++; | |
655 if (nEvents) | |
656 { | |
657 WriteConsoleInput (hStdIn, events, nEvents, &written); | |
658 nEvents = 0; | |
659 ZeroMemory (events, sizeof (events)); | |
660 } | |
661 PostMessage (m_consoleWindow, WM_KEYDOWN, VK_RETURN, 0x001C0001); | |
662 PostMessage (m_consoleWindow, WM_KEYDOWN, VK_RETURN, 0xC01C0001); | |
663 } | |
664 else | |
665 { | |
666 events[nEvents].EventType = KEY_EVENT; | |
667 events[nEvents].Event.KeyEvent.bKeyDown = TRUE; | |
668 events[nEvents].Event.KeyEvent.wRepeatCount = 1; | |
669 events[nEvents].Event.KeyEvent.wVirtualKeyCode = | |
670 LOBYTE (VkKeyScan (c.unicode ())); | |
671 events[nEvents].Event.KeyEvent.wVirtualScanCode = 0; | |
672 events[nEvents].Event.KeyEvent.uChar.UnicodeChar = c.unicode (); | |
673 events[nEvents].Event.KeyEvent.dwControlKeyState = 0; | |
674 nEvents++; | |
675 } | |
676 | |
677 if (nEvents == TEXT_CHUNK_SIZE | |
678 || (nEvents > 0 && i == (len - 1))) | |
679 { | |
680 WriteConsoleInput (hStdIn, events, nEvents, &written); | |
681 nEvents = 0; | |
682 ZeroMemory (events, sizeof (events)); | |
683 } | |
684 } | |
685 } | |
686 | |
687 ////////////////////////////////////////////////////////////////////////////// | |
688 | |
689 QConsole::QConsole (QWidget* parent) | |
690 : d (new QConsolePrivate (this)) | |
691 { | |
692 } | |
693 | |
694 ////////////////////////////////////////////////////////////////////////////// | |
695 | |
696 QConsole::QConsole (const QString& cmd, QWidget* parent) | |
697 : d (new QConsolePrivate (this, cmd)) | |
698 { | |
699 } | |
700 | |
701 ////////////////////////////////////////////////////////////////////////////// | |
702 | |
703 QConsole::~QConsole (void) | |
704 { | |
705 delete d; | |
706 } | |
707 | |
708 ////////////////////////////////////////////////////////////////////////////// | |
709 | |
710 void QConsole::viewResizeEvent (QConsoleView*, QResizeEvent*) | |
711 { | |
712 d->updateConsoleSize (true); | |
713 d->grabConsoleBuffer (); | |
714 } | |
715 | |
716 ////////////////////////////////////////////////////////////////////////////// | |
717 | |
718 void QConsole::viewPaintEvent (QConsoleView* w, QPaintEvent* event) | |
719 { | |
720 QPainter p (w); | |
721 int cw = d->m_charSize.width (), ch = d->m_charSize.height (); | |
722 int ascent, stride, cx1, cy1, cx2, cy2, x, y; | |
723 WORD attr = 0; | |
724 QString s; | |
725 bool hasChar = false; | |
726 | |
727 QRect updateRect = event->rect (); | |
728 | |
729 cx1 = updateRect.left () / cw; | |
730 cy1 = updateRect.top () / ch; | |
731 cx2 = qMin (d->m_consoleRect.width () - 1, updateRect.right () / cw); | |
732 cy2 = qMin (d->m_consoleRect.height () - 1, updateRect.bottom () / ch); | |
733 | |
734 if (cx1 > d->m_consoleRect.width () - 1 | |
735 || cy1 > d->m_consoleRect.height () - 1) | |
736 return; | |
737 | |
738 p.setFont (d->m_font); | |
739 p.setPen (Qt::black); | |
740 | |
741 ascent = p.fontMetrics ().ascent (); | |
742 stride = d->m_consoleRect.width (); | |
743 | |
744 s.reserve (cx2 - cx1 + 1); | |
745 y = ascent + cy1 * ch;; | |
746 | |
747 for (int j = cy1; j <= cy2; j++, y += ch) | |
748 { | |
749 // Reset string buffer and starting X coordinate | |
750 s.clear (); | |
751 hasChar = false; | |
752 x = cx1 * cw; | |
753 | |
754 for (int i = cx1; i <= cx2; i++) | |
755 { | |
756 CHAR_INFO* ci = &(d->m_buffer[stride*j+i]); | |
757 | |
758 if ((ci->Attributes & 0x00ff) != attr) | |
759 { | |
760 // Character attributes changed | |
761 if (! s.isEmpty ()) | |
762 { | |
763 // String buffer not empty -> draw it | |
764 if (hasChar || (attr & 0x00f0)) | |
765 { | |
766 if (attr & 0x00f0) | |
767 p.fillRect (x, y-ascent, s.length () * cw, ch, | |
768 p.brush ()); | |
769 p.drawText (x, y, s); | |
770 } | |
771 x += (s.length () * cw); | |
772 s.clear (); | |
773 hasChar = false; | |
774 } | |
775 // Update current pen and store current attributes | |
776 // FIXME: what about background? | |
777 attr = (ci->Attributes & 0x00ff); | |
778 p.setPen (d->m_colors[attr & 0x000f]); | |
779 p.setBrush (d->m_colors[(attr >> 4) & 0x000f]); | |
780 } | |
781 | |
782 // Append current character to the string buffer | |
783 s.append (ci->Char.UnicodeChar); | |
784 if (ci->Char.UnicodeChar != L' ') | |
785 hasChar = true; | |
786 } | |
787 | |
788 if (! s.isEmpty () && (hasChar || (attr & 0x00f0))) | |
789 { | |
790 // Line end reached, but string buffer not empty -> draw it | |
791 // No need to update s or x, they will be reset on the next | |
792 // for-loop iteration | |
793 if (attr & 0x00f0) | |
794 p.fillRect (x, y-ascent, s.length () * cw, ch, p.brush ()); | |
795 p.drawText (x, y, s); | |
796 } | |
797 } | |
798 | |
799 // Draw cursor | |
800 p.setCompositionMode (QPainter::RasterOp_SourceXorDestination); | |
801 p.fillRect ((d->m_cursorPos.x () - d->m_consoleRect.x ()) * cw, | |
802 (d->m_cursorPos.y () - d->m_consoleRect.y ()) * ch, | |
803 cw, ch, d->m_colors[7]); | |
804 } | |
805 | |
806 ////////////////////////////////////////////////////////////////////////////// | |
807 | |
808 void QConsole::wheelEvent (QWheelEvent* event) | |
809 { | |
810 if (! d->m_inWheelEvent) | |
811 { | |
812 // Forward to the scrollbar (avoid recursion) | |
813 d->m_inWheelEvent = true; | |
814 QApplication::sendEvent (d->m_scrollBar, event); | |
815 d->m_inWheelEvent = false; | |
816 } | |
817 } | |
818 | |
819 ////////////////////////////////////////////////////////////////////////////// | |
820 | |
821 bool QConsole::winEvent (MSG* msg, long* result) | |
822 { | |
823 switch (msg->message) | |
824 { | |
825 case WM_KEYDOWN: | |
826 case WM_KEYUP: | |
827 //case WM_CHAR: | |
828 // Forward Win32 message to the console window | |
829 PostMessage (d->m_consoleWindow, | |
830 msg->message, | |
831 msg->wParam, | |
832 msg->lParam); | |
833 result = 0; | |
834 return true; | |
835 default: | |
836 return false; | |
837 } | |
838 } | |
839 | |
840 ////////////////////////////////////////////////////////////////////////////// | |
841 | |
842 void QConsole::scrollValueChanged (int value) | |
843 { | |
844 d->setScrollValue (value); | |
845 } | |
846 | |
847 ////////////////////////////////////////////////////////////////////////////// | |
848 | |
849 void QConsole::monitorConsole (void) | |
850 { | |
851 d->monitorConsole (); | |
852 } | |
853 | |
854 ////////////////////////////////////////////////////////////////////////////// | |
855 | |
856 void QConsole::focusInEvent (QFocusEvent* event) | |
857 { | |
858 QWidget::focusInEvent (event); | |
859 } | |
860 | |
861 ////////////////////////////////////////////////////////////////////////////// | |
862 | |
863 void QConsole::start (void) | |
864 { | |
865 d->startCommand (); | |
866 } | |
867 | |
868 ////////////////////////////////////////////////////////////////////////////// | |
869 | |
870 void QConsole::sendText (const QString& s) | |
871 { | |
872 d->sendConsoleText (s); | |
873 } | |
874 |