| 1 | // LAF Base Library |
| 2 | // Copyright (c) 2001-2016 David Capello |
| 3 | // |
| 4 | // This file is released under the terms of the MIT license. |
| 5 | // Read LICENSE.txt for more information. |
| 6 | |
| 7 | #ifndef BASE_SYSTEM_CONSOLE_H_INCLUDED |
| 8 | #define BASE_SYSTEM_CONSOLE_H_INCLUDED |
| 9 | #pragma once |
| 10 | |
| 11 | namespace base { |
| 12 | |
| 13 | // This class is needed only for Windows platform. |
| 14 | // |
| 15 | // Some background: This app is linked with /subsystem:windows flag, |
| 16 | // which is the only way to avoid a console when the program is |
| 17 | // double-clicked from Windows Explorer. The problem with this is if |
| 18 | // the user starts the program from cmd.exe, the output is not shown |
| 19 | // anywhere. Generally there is one simple solution for console only |
| 20 | // apps: The /subsystem:console flag, but it shows a console when the |
| 21 | // program is started from Windows Explorer (anyway we could call |
| 22 | // FreeConsole(), but the console is visible some milliseconds which |
| 23 | // is not an expected behavior by normal Windows user). |
| 24 | // |
| 25 | // So this class tries to make some adjustments for apps linked with |
| 26 | // /subsystem:windows but that want to display some text in the |
| 27 | // console in certain cases (e.g. when --help flag is specified). |
| 28 | // |
| 29 | // Basically it tries to redirect stdin/stdout handles so the end-user |
| 30 | // can have the best of both worlds on Windows: |
| 31 | // 1) If the app is executed with double-click, a console isn't shown. |
| 32 | // (Because the process was/should be linked with /subsystem:windows flag.) |
| 33 | // 2) If the app is executed from a process like cmd.exe, the output is |
| 34 | // redirected to the cmd.exe console. |
| 35 | // |
| 36 | // In the best case, the application should work as a Unix-like |
| 37 | // program, blocking the cmd.exe in case 2, but it cannot be |
| 38 | // possible. The output/input is deattached as soon as cmd.exe knows |
| 39 | // that the program was linked with /subsystem:windows. |
| 40 | // |
| 41 | class SystemConsole |
| 42 | { |
| 43 | public: |
| 44 | SystemConsole(); |
| 45 | ~SystemConsole(); |
| 46 | |
| 47 | // On Windows it creates a console so the user can start typing |
| 48 | // commands on it. It's necessary because we link the program with |
| 49 | // /subsystem:windows flag (not /subsystem:console), so the |
| 50 | // process's stdin starts deattached from the parent process's |
| 51 | // console. (On Unix-like systems it does nothing.) |
| 52 | void prepareShell(); |
| 53 | }; |
| 54 | |
| 55 | } // namespace base |
| 56 | |
| 57 | #endif |
| 58 | |