Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: runtime/bin/platform_win.cc

Issue 2734323002: [dart:io][windows] Set Console VT modes (Closed)
Patch Set: Add a comment with a doc link Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_WINDOWS) 6 #if defined(TARGET_OS_WINDOWS)
7 7
8 #include "bin/platform.h" 8 #include "bin/platform.h"
9 9
10 #include <crtdbg.h> 10 #include <crtdbg.h>
11 11
12 #include "bin/file.h" 12 #include "bin/file.h"
13 #include "bin/lockers.h" 13 #include "bin/lockers.h"
14 #include "bin/log.h" 14 #include "bin/log.h"
15 #if !defined(DART_IO_DISABLED) && !defined(PLATFORM_DISABLE_SOCKET) 15 #if !defined(DART_IO_DISABLED) && !defined(PLATFORM_DISABLE_SOCKET)
16 #include "bin/socket.h" 16 #include "bin/socket.h"
17 #endif 17 #endif
18 #include "bin/thread.h" 18 #include "bin/thread.h"
19 #include "bin/utils.h" 19 #include "bin/utils.h"
20 #include "bin/utils_win.h" 20 #include "bin/utils_win.h"
21 21
22 // This is not always defined in the header files. See:
23 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).as px
24 #ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
25 #define ENABLE_VIRTUAL_TERMINAL_INPUT 0x200
26 #endif
27
22 namespace dart { 28 namespace dart {
23 29
24 // Defined in vm/os_thread_win.cc 30 // Defined in vm/os_thread_win.cc
25 extern bool private_flag_windows_run_tls_destructors; 31 extern bool private_flag_windows_run_tls_destructors;
26 32
27 namespace bin { 33 namespace bin {
28 34
29 const char* Platform::executable_name_ = NULL; 35 const char* Platform::executable_name_ = NULL;
30 char* Platform::resolved_executable_name_ = NULL; 36 char* Platform::resolved_executable_name_ = NULL;
31 int Platform::script_index_ = 1; 37 int Platform::script_index_ = 1;
(...skipping 10 matching lines...) Expand all
42 _set_invalid_parameter_handler(InvalidParameterHandler); 48 _set_invalid_parameter_handler(InvalidParameterHandler);
43 // Disable the message box for assertions in the CRT in Debug builds. 49 // Disable the message box for assertions in the CRT in Debug builds.
44 // See: https://msdn.microsoft.com/en-us/library/1y71x448.aspx 50 // See: https://msdn.microsoft.com/en-us/library/1y71x448.aspx
45 _CrtSetReportMode(_CRT_ASSERT, 0); 51 _CrtSetReportMode(_CRT_ASSERT, 0);
46 // Disable dialog boxes for "critical" errors or when OpenFile cannot find 52 // Disable dialog boxes for "critical" errors or when OpenFile cannot find
47 // the requested file. See: 53 // the requested file. See:
48 // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v= vs.85).aspx 54 // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v= vs.85).aspx
49 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); 55 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
50 } 56 }
51 57
52 static void SaveAndSetOutputCP() { 58 static void SaveAndConfigureConsole() {
53 MutexLocker ml(platform_win_mutex_); 59 MutexLocker ml(platform_win_mutex_);
54 ASSERT(saved_output_cp_ == -1); 60 ASSERT(saved_output_cp_ == -1);
55 saved_output_cp_ = GetConsoleOutputCP(); 61 saved_output_cp_ = GetConsoleOutputCP();
56 SetConsoleOutputCP(CP_UTF8); 62 SetConsoleOutputCP(CP_UTF8);
63
64 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
65 if ((out != INVALID_HANDLE_VALUE) &&
66 GetConsoleMode(out, &saved_console_out_mode_)) {
67 const DWORD request =
68 saved_console_out_mode_ | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
69 SetConsoleMode(out, request);
70 }
71
72 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
73 if ((in != INVALID_HANDLE_VALUE) &&
74 GetConsoleMode(in, &saved_console_in_mode_)) {
75 const DWORD request =
76 saved_console_in_mode_ | ENABLE_VIRTUAL_TERMINAL_INPUT;
77 SetConsoleMode(in, request);
78 }
57 } 79 }
58 80
59 static void RestoreOutputCP() { 81 static void RestoreConsole() {
60 MutexLocker ml(platform_win_mutex_); 82 MutexLocker ml(platform_win_mutex_);
61 if (saved_output_cp_ != -1) { 83 if (saved_output_cp_ != -1) {
62 SetConsoleOutputCP(saved_output_cp_); 84 SetConsoleOutputCP(saved_output_cp_);
63 saved_output_cp_ = -1; 85 saved_output_cp_ = -1;
64 } 86 }
87 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
88 if (out != INVALID_HANDLE_VALUE) {
89 SetConsoleMode(out, saved_console_out_mode_);
90 saved_console_out_mode_ = 0;
91 }
92 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
93 if (in != INVALID_HANDLE_VALUE) {
94 SetConsoleMode(in, saved_console_in_mode_);
95 saved_console_in_mode_ = 0;
96 }
65 } 97 }
66 98
67 private: 99 private:
68 static Mutex* platform_win_mutex_; 100 static Mutex* platform_win_mutex_;
69 static int saved_output_cp_; 101 static int saved_output_cp_;
102 static DWORD saved_console_out_mode_;
103 static DWORD saved_console_in_mode_;
70 104
71 static void InvalidParameterHandler(const wchar_t* expression, 105 static void InvalidParameterHandler(const wchar_t* expression,
72 const wchar_t* function, 106 const wchar_t* function,
73 const wchar_t* file, 107 const wchar_t* file,
74 unsigned int line, 108 unsigned int line,
75 uintptr_t reserved) { 109 uintptr_t reserved) {
76 // Doing nothing here means that the CRT call that invoked it will 110 // Doing nothing here means that the CRT call that invoked it will
77 // return an error code and/or set errno. 111 // return an error code and/or set errno.
78 } 112 }
79 113
80 DISALLOW_ALLOCATION(); 114 DISALLOW_ALLOCATION();
81 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformWin); 115 DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformWin);
82 }; 116 };
83 117
84 int PlatformWin::saved_output_cp_ = -1; 118 int PlatformWin::saved_output_cp_ = -1;
119 DWORD PlatformWin::saved_console_out_mode_ = 0;
120 DWORD PlatformWin::saved_console_in_mode_ = 0;
85 Mutex* PlatformWin::platform_win_mutex_ = NULL; 121 Mutex* PlatformWin::platform_win_mutex_ = NULL;
86 122
87 bool Platform::Initialize() { 123 bool Platform::Initialize() {
88 PlatformWin::InitOnce(); 124 PlatformWin::InitOnce();
89 PlatformWin::SaveAndSetOutputCP(); 125 PlatformWin::SaveAndConfigureConsole();
90 return true; 126 return true;
91 } 127 }
92 128
93 129
94 int Platform::NumberOfProcessors() { 130 int Platform::NumberOfProcessors() {
95 SYSTEM_INFO info; 131 SYSTEM_INFO info;
96 GetSystemInfo(&info); 132 GetSystemInfo(&info);
97 return info.dwNumberOfProcessors; 133 return info.dwNumberOfProcessors;
98 } 134 }
99 135
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 // Return the canonical path as the returned path might contain symlinks. 212 // Return the canonical path as the returned path might contain symlinks.
177 const char* canon_path = File::GetCanonicalPath(path); 213 const char* canon_path = File::GetCanonicalPath(path);
178 return canon_path; 214 return canon_path;
179 } 215 }
180 216
181 217
182 void Platform::Exit(int exit_code) { 218 void Platform::Exit(int exit_code) {
183 // TODO(zra): Remove once VM shuts down cleanly. 219 // TODO(zra): Remove once VM shuts down cleanly.
184 ::dart::private_flag_windows_run_tls_destructors = false; 220 ::dart::private_flag_windows_run_tls_destructors = false;
185 // Restore the console's output code page 221 // Restore the console's output code page
186 PlatformWin::RestoreOutputCP(); 222 PlatformWin::RestoreConsole();
187 // On Windows we use ExitProcess so that threads can't clobber the exit_code. 223 // On Windows we use ExitProcess so that threads can't clobber the exit_code.
188 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870 224 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870
189 ::ExitProcess(exit_code); 225 ::ExitProcess(exit_code);
190 } 226 }
191 227
192 } // namespace bin 228 } // namespace bin
193 } // namespace dart 229 } // namespace dart
194 230
195 #endif // defined(TARGET_OS_WINDOWS) 231 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698