summaryrefslogtreecommitdiffstats
path: root/osdep/terminal-win.c
diff options
context:
space:
mode:
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>2015-04-13 19:55:48 +0900
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>2015-04-13 19:55:48 +0900
commit3287825bd2ee762a918961383604697358765397 (patch)
treed3513ec0b91a5c3832221c5b74225deb85fb2163 /osdep/terminal-win.c
parent9e65b5db0bf79b31c7816d1fdba77ea18fe82690 (diff)
parentf43017bfe97c07aa73206d8d1388294cdbee628f (diff)
downloadmpv-3287825bd2ee762a918961383604697358765397.tar.bz2
mpv-3287825bd2ee762a918961383604697358765397.tar.xz
Merge branch 'master' into release/0.9
* master: (39 commits) Update license headers af_lavrresample: minor simplification af_lavrresample: allow resetting output sample format audio/filter: fully renegotiate audio formats on every reconfig DOCS/waf: Edit --enable-feature sentences vo_opengl: slightly simplify check_gl_features() vo_opengl: unify blend-subtitles-res and blend-subtitles vo_opengl: fix blend-subtitles-res=video & anamorphic video manpage: mention how pseudo-gui mode is enabled on win32 main-fn-win: fix handle validity check for XP win32: use pseudo-gui profile when started without stdio win32: only attach to the console from mpv.com win32: use a platform-specific unicode entry-point vo_opengl: add blend-subtitles-res manpage: document pseudo-gui stuff mp_image: remove redundant flags field mp_image: remove redundant chroma_x/y_shift fields mp_image: remove redundant plane_w/h fields player: use config parser for setting up pseudo-gui profile player: change pseudo-gui settings ...
Diffstat (limited to 'osdep/terminal-win.c')
-rw-r--r--osdep/terminal-win.c59
1 files changed, 41 insertions, 18 deletions
diff --git a/osdep/terminal-win.c b/osdep/terminal-win.c
index 678cedb775..e6b17fd595 100644
--- a/osdep/terminal-win.c
+++ b/osdep/terminal-win.c
@@ -2,21 +2,20 @@
*
* copyright (C) 2003 Sascha Sommer
*
- * This file is part of MPlayer.
+ * This file is part of mpv.
*
- * MPlayer is free software; you can redistribute it and/or modify
+ * mpv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
- * MPlayer is distributed in the hope that it will be useful,
+ * mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
// See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
@@ -250,24 +249,48 @@ void mp_write_console_ansi(HANDLE wstream, char *buf)
}
}
-int terminal_init(void)
+static bool is_a_console(HANDLE h)
+{
+ return GetConsoleMode(h, &(DWORD){0});
+}
+
+static void reopen_console_handle(DWORD std, FILE *stream)
+{
+ HANDLE wstream = GetStdHandle(std);
+ if (is_a_console(wstream)) {
+ int fd = _open_osfhandle((intptr_t)wstream, _O_TEXT);
+ dup2(fd, fileno(stream));
+ setvbuf(stream, NULL, _IONBF, 0);
+ }
+}
+
+bool terminal_try_attach(void)
{
- if (AttachConsole(ATTACH_PARENT_PROCESS)) {
- // We have been started by something with a console window.
- // Redirect output streams to that console's low-level handles,
- // so we can actually use WriteConsole later on.
+ // mpv.exe is a flagged as a GUI application, but it acts as a console
+ // application when started from the console wrapper (see
+ // osdep/win32-console-wrapper.c). The console wrapper sets
+ // _started_from_console=yes, so check that variable before trying to
+ // attach to the console.
+ wchar_t console_env[4] = { 0 };
+ if (!GetEnvironmentVariableW(L"_started_from_console", console_env, 4))
+ return false;
+ if (wcsncmp(console_env, L"yes", 4))
+ return false;
+ SetEnvironmentVariableW(L"_started_from_console", NULL);
- int hConHandle;
+ if (!AttachConsole(ATTACH_PARENT_PROCESS))
+ return false;
- hConHandle = _open_osfhandle((intptr_t)hSTDOUT, _O_TEXT);
- *stdout = *_fdopen(hConHandle, "w");
- setvbuf(stdout, NULL, _IONBF, 0);
+ // We have a console window. Redirect output streams to that console's
+ // low-level handles, so we can actually use WriteConsole later on.
+ reopen_console_handle(STD_OUTPUT_HANDLE, stdout);
+ reopen_console_handle(STD_ERROR_HANDLE, stderr);
- hConHandle = _open_osfhandle((intptr_t)hSTDERR, _O_TEXT);
- *stderr = *_fdopen(hConHandle, "w");
- setvbuf(stderr, NULL, _IONBF, 0);
- }
+ return true;
+}
+int terminal_init(void)
+{
CONSOLE_SCREEN_BUFFER_INFO cinfo;
DWORD cmode = 0;
GetConsoleMode(hSTDOUT, &cmode);