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 "../telemetry/telemetry.hpp"
11#include "../utility/utility.hpp"
12#include "../concurrency/concurrency.hpp"
13#include "../char_maps/char_maps.hpp"
14#include "../time/time.hpp"
15#include "../macros.hpp"
16#include <cstddef>
17#include <memory>
18#include <cstring>
19#include <string>
20#include <cstdio>
21#include <exception>
22#include <compare>
23#include <string_view>
24#include <format>
25#include <type_traits>
26#include <chrono>
27
28hi_export_module(hikogui.crt.crt_utils : impl);
29
30hi_warning_push();
31// C26400: Do not assign the result of an allocation or a function cal with an owner<T> return value to... (i11)
32// For compatibility reasons we work with raw pointers here.
33hi_warning_ignore_msvc(26400);
34
35hi_export namespace hi { inline namespace v1 {
36
40hi_export [[nodiscard]] inline char *make_cstr(char const *c_str, std::size_t size = -1) noexcept
41{
42 if (size == -1) {
43 size = std::strlen(c_str);
44 }
45
46 auto r = new char[size + 1];
47 std::memcpy(r, c_str, size + 1);
48 return r;
49}
50
54hi_export [[nodiscard]] inline char *make_cstr(std::string const& s) noexcept
55{
56 return make_cstr(s.c_str(), s.size());
57}
58
59hi_export inline std::pair<int, char **> crt_start(int, char **, void *instance, int show_cmd)
60{
61 // lpCmdLine does not handle UTF-8 command line properly.
62 // So use GetCommandLineW() to get wide string arguments.
63 // CommandLineToArgW properly unescapes the command line
64 // and splits in separate arguments.
65 int wargc = 0;
66 auto wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
67 hi_assert_not_null(wargv);
68
69 // Convert the wchar arguments to UTF-8 and create nul terminated
70 // c-strings. main() compatibility requires writable strings, so
71 // we need to allocate old-style.
72 char **argv = new char *[wargc + 2];
73 hi_assert_not_null(argv);
74
75 int argc = 0;
76 for (; argc != wargc; ++argc) {
77 argv[argc] = make_cstr(to_string(std::wstring(wargv[argc])));
78 }
79 LocalFree(wargv);
80
81 // Pass nShowCmd as a the second command line argument.
82 if (show_cmd == 3) {
83 argv[argc++] = make_cstr("--window-state=maximize");
84 } else if (show_cmd == 0 || show_cmd == 2 || show_cmd == 6 || show_cmd == 7 || show_cmd == 11) {
85 argv[argc++] = make_cstr("--window-state=minimize");
86 }
87
88 // Add a nullptr to the end of the argument list.
89 argv[argc] = nullptr;
90
91 // Make sure the console is in a valid state to write text to it.
92 auto const [tsc_frequency, aux_is_cpu_id] = hi::time_stamp_count::start_subsystem();
93
94 initialize();
96 if (aux_is_cpu_id) {
97 hi_log_info("The AUX value from the time-stamp-count is equal to the cpu-id.");
98 }
99 hi_log_info("The measured frequency of the TSC is {} Hz.", tsc_frequency);
100
101 crt_application_instance = instance;
102 return {argc, argv};
103}
104
105inline int crt_finish(int argc, char **argv, int exit_code)
106{
107 hi_assert_not_null(argv);
108
110
111 for (auto i = 0; i != argc; ++i) {
112 delete[] argv[i];
113 }
114 delete[] argv;
115 return exit_code;
116}
117
118}} // namespace hi::inline v1
119
120hi_warning_pop();
Rules for working with win32 headers.
void shutdown_system() noexcept
Shutdown the system.
Definition subsystem.hpp:231
void start_system() noexcept
Start the system.
Definition subsystem.hpp:218
The HikoGUI namespace.
Definition array_generic.hpp:20
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:40
void initialize() noexcept
Initialize base functionality of HikoGUI.
Definition initialize.hpp:56
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
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
T memcpy(T... args)
T strlen(T... args)