HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
crt_utils_win32_impl.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
8
9#include "crt_utils_intf.hpp"
10#include "terminate.hpp"
11#include "../telemetry/telemetry.hpp"
12#include "../console/console.hpp"
13#include "../utility/utility.hpp"
14#include "../concurrency/concurrency.hpp"
15#include "../char_maps/module.hpp"
16#include "../time/module.hpp"
17#include "../macros.hpp"
18
19hi_export_module(hikogui.crt.crt_utils : impl);
20
21hi_warning_push();
22// C26400: Do not assign the result of an allocation or a function cal with an owner<T> return value to... (i11)
23// For compatibility reasons we work with raw pointers here.
24hi_warning_ignore_msvc(26400);
25
26namespace hi { inline namespace v1 {
27
31hi_export [[nodiscard]] inline char *make_cstr(char const *c_str, std::size_t size = -1) noexcept
32{
33 if (size == -1) {
34 size = std::strlen(c_str);
35 }
36
37 auto r = new char[size + 1];
38 std::memcpy(r, c_str, size + 1);
39 return r;
40}
41
45hi_export [[nodiscard]] inline char *make_cstr(std::string const& s) noexcept
46{
47 return make_cstr(s.c_str(), s.size());
48}
49
50hi_export inline void console_start() noexcept
51{
53
54 if (out_handle == NULL) {
55 // The stdout is not set, which means our parent process has
56 // not set it. This is the most likely case on Windows 10.
57
59 // Our parent process is a console, like cmd and powershell.
60 // After attaching to the console we need re-open stdin,
61 // stdout, stderr using the original device names.
62
63 // Since stdin, stdout, stderr are macro's make sure we get pointers
64 // which can be modified by freopen_s().
65 FILE *fpstdin = stdin;
66 FILE *fpstdout = stdout;
67 FILE *fpstderr = stderr;
68
69 freopen_s(&fpstdin, "CONIN$", "r", stdin);
70 freopen_s(&fpstdout, "CONOUT$", "w", stdout);
71 freopen_s(&fpstderr, "CONOUT$", "w", stderr);
72 }
73
74 } else {
75 // stdout is already working, this happens when a UNIX-like shell
76 // as setup stdin, stdout, stderr. For example when the application
77 // is started from git-bash.
78 // Since everything is already working, don't do anything.
79 ;
80 }
81}
82
83hi_export inline std::pair<int, char **> crt_start(int, char **, void *instance, int show_cmd)
84{
85 // Switch out the terminate handler with one that can print an error message.
87
88 // lpCmdLine does not handle UTF-8 command line properly.
89 // So use GetCommandLineW() to get wide string arguments.
90 // CommandLineToArgW properly unescapes the command line
91 // and splits in separate arguments.
92 int wargc = 0;
94 hi_assert_not_null(wargv);
95
96 // Convert the wchar arguments to UTF-8 and create nul terminated
97 // c-strings. main() compatibility requires writable strings, so
98 // we need to allocate old-style.
99 char **argv = new char *[wargc + 2];
100 hi_assert_not_null(argv);
101
102 int argc = 0;
103 for (; argc != wargc; ++argc) {
105 }
107
108 // Pass nShowCmd as a the second command line argument.
109 if (show_cmd == 3) {
110 argv[argc++] = make_cstr("--window-state=maximize");
111 } else if (show_cmd == 0 || show_cmd == 2 || show_cmd == 6 || show_cmd == 7 || show_cmd == 11) {
112 argv[argc++] = make_cstr("--window-state=minimize");
113 }
114
115 // Add a nullptr to the end of the argument list.
116 argv[argc] = nullptr;
117
118 // Make sure the console is in a valid state to write text to it.
120 hilet [tsc_frequency, aux_is_cpu_id] = hi::time_stamp_count::start_subsystem();
121
122 start_system();
123 if (aux_is_cpu_id) {
124 hi_log_info("The AUX value from the time-stamp-count is equal to the cpu-id.");
125 }
126 hi_log_info("The measured frequency of the TSC is {} Hz.", tsc_frequency);
127
129 return {argc, argv};
130}
131
132inline int crt_finish(int argc, char **argv, int exit_code)
133{
134 hi_assert_not_null(argv);
135
137
138 for (auto i = 0; i != argc; ++i) {
139 delete[] argv[i];
140 }
141 delete[] argv;
142 return exit_code;
143}
144
145}} // namespace hi::inline v1
146
147hi_warning_pop();
Rules for working with win32 headers.
Utilities for throwing exceptions and terminating the application.
void shutdown_system() noexcept
Shutdown the system.
Definition subsystem.hpp:230
void start_system() noexcept
Start the system.
Definition subsystem.hpp:217
DOXYGEN BUG.
Definition algorithm.hpp:16
hi_export void console_start() noexcept
Initialize the console.
int crt_finish(int argc, char **argv, int exit_code)
Finish the hikogui system.
os_handle crt_application_instance
The application instance identified by the operating system.
Definition crt_utils_intf.hpp:24
std::pair< int, char ** > crt_start(int argc, char **argv, void *instance, int show_cmd)
Start the hikogui system.
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
hi_export char * make_cstr(char const *c_str, std::size_t size=-1) noexcept
Copy a std::string to new memory.
Definition crt_utils_win32_impl.hpp:31
std::terminate_handler old_terminate_handler
The old terminate handler.
Definition terminate.hpp:28
void terminate_handler() noexcept
The HikoGUI terminate handler.
Definition terminate.hpp:36
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
T memcpy(T... args)
T set_terminate(T... args)
T strlen(T... args)
T to_string(T... args)