HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
crt.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021.
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
21#pragma once
22
23#include "architecture.hpp"
24#include "subsystem.hpp"
25#include "URL.hpp"
26#include "strings.hpp"
27#include "cast.hpp"
28#include "console.hpp"
29#include "time_stamp_count.hpp"
30#include "GUI/gui_system.hpp"
31#include <chrono>
32
33namespace tt {
34
42void crt_configure_process() noexcept;
43
44}
45
52int tt_main(int argc, char *argv[]);
53
54#if not defined(TT_CRT_NO_MAIN)
55#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
56
67int WINAPI WinMain(
68 _In_ HINSTANCE hInstance,
69 [[maybe_unused]] _In_opt_ HINSTANCE hPrevInstance,
70 [[maybe_unused]] _In_ LPSTR lpCmdLine,
71 _In_ int nShowCmd)
72{
73 tt::crt_configure_process();
74
75 // lpCmdLine does not handle UTF-8 command line properly.
76 // So use GetCommandLineW() to get wide string arguments.
77 // CommandLineToArgW properly unescapes the command line
78 // and splits in separate arguments.
79 int argc;
80 auto argv = CommandLineToArgvW(GetCommandLineW(), &argc);
81
82 // Convert the wchar arguments to UTF-8 and create nul terminated
83 // c-strings. main() compatibility requires writable strings, so
84 // we need to allocate old-style.
85 auto arguments = std::vector<char *>{};
86 arguments.reserve(argc + 2);
87 for (auto i = 0; i != argc; ++i) {
88 arguments.push_back(tt::make_cstr(tt::to_string(std::wstring(argv[i]))));
89 }
90 LocalFree(argv);
91
92 // Pass nShowCmd as a the second command line argument.
93 if (nShowCmd == 3) {
94 arguments.insert(std::next(std::begin(arguments)), tt::make_cstr("--window-state=maximize"));
95 } else if (nShowCmd == 0 || nShowCmd == 2 || nShowCmd == 6 || nShowCmd == 7 || nShowCmd == 11) {
96 arguments.insert(std::next(std::begin(arguments)), tt::make_cstr("--window-state=minimize"));
97 }
98
99 // Add a nullptr to the end of the argument list.
100 arguments.push_back(nullptr);
101
102 // Initialize tzdata base.
103 try {
104 [[maybe_unused]] ttlet time_zone = std::chrono::current_zone();
105 } catch (std::runtime_error const &e) {
106 tt_log_error("Could not get current time zone: \"{}\"", e.what());
107 }
108
109 // Make sure the console is in a valid state to write text to it.
110 tt::console_start();
112 tt::start_system();
113
114 tt::gui_system::instance = hInstance;
115 ttlet r = tt_main(tt::narrow_cast<int>(arguments.size() - 1), arguments.data());
116
117 tt::shutdown_system();
118
119 for (auto argument: arguments) {
120 delete [] argument;
121 }
122 return r;
123}
124
125#else
126
127int main(int argc, char *argv[])
128{
130
131 // XXX - The URL system needs to know about the location of the executable.
132#if USE_OS_TZDB == 0
133 ttlet tzdata_location = tt::URL::urlFromResourceDirectory() / "tzdata";
134 std::set_install(tzdata_location.nativePath());
135 try {
136 [[maybe_unused]] ttlet time_zone = std::chrono::current_zone();
137 } catch (std::runtime_error const &e) {
138 tt_log_error("Could not get current time zone: \"{}\"", e.what());
139 }
140#endif
141
142 // Make sure the console is in a valid state to write text to it.
143 tt::console_start();
144 tt::time_stamp_count::start();
145 tt::start_system();
146
147 ttlet r = tt_main(argc, argv);
148 tt::shutdown_system();
149 return r;
150}
151
152#endif
153#endif
int tt_main(int argc, char *argv[])
Main entry-point.
void crt_configure_process() noexcept
Configure the process for use with ttauri library.
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
Windows entry-point.
Definition crt.hpp:67
static void start_subsystem() noexcept
Start the time_stamp_count subsystem.
T begin(T... args)
T next(T... args)
T reserve(T... args)
T what(T... args)