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 "os_detect.hpp"
24#include "system_status.hpp"
25#include "URL.hpp"
26#include "strings.hpp"
27#include "cast.hpp"
28#include "console.hpp"
29
30#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
31#include "application_win32.hpp"
32#define tt_application(...) tt::application_win32(__VA_ARGS__)
33
34#include <Windows.h>
35#elif TT_OPERATING_SYSTEM == TT_OS_MACOS
36#include "application_macos.hpp"
37#define tt_application(...) tt::application_macos(__VA_ARGS__)
38
39#endif
40
41#include <date/tz.h>
42
51int tt_main(int argc, char *argv[], tt::os_handle instance);
52
53#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
54
65int WINAPI WinMain(
66 _In_ HINSTANCE hInstance,
67 [[maybe_unused]] _In_opt_ HINSTANCE hPrevInstance,
68 [[maybe_unused]] _In_ LPSTR lpCmdLine,
69 _In_ int nShowCmd)
70{
71 // lpCmdLine does not handle UTF-8 command line properly.
72 // So use GetCommandLineW() to get wide string arguments.
73 // CommandLineToArgW properly unescapes the command line
74 // and splits in separate arguments.
75 int argc;
76 auto argv = CommandLineToArgvW(GetCommandLineW(), &argc);
77
78 // Convert the wchar arguments to UTF-8 and create nul terminated
79 // c-strings. main() compatibility requires writable strings, so
80 // we need to allocate old-style.
81 auto arguments = std::vector<char *>{};
82 arguments.reserve(argc + 2);
83 for (auto i = 0; i != argc; ++i) {
84 arguments.push_back(tt::make_cstr(tt::to_string(std::wstring(argv[i]))));
85 }
86 LocalFree(argv);
87
88 // Pass nShowCmd as a the second command line argument.
89 if (nShowCmd == 3) {
90 arguments.insert(std::next(std::begin(arguments)), tt::make_cstr("--window-state=maximize"));
91 } else if (nShowCmd == 0 || nShowCmd == 2 || nShowCmd == 6 || nShowCmd == 7 || nShowCmd == 11) {
92 arguments.insert(std::next(std::begin(arguments)), tt::make_cstr("--window-state=minimize"));
93 }
94
95 // Add a nullptr to the end of the argument list.
96 arguments.push_back(nullptr);
97
98 // Initialize tzdata base.
99#if USE_OS_TZDB == 0
100 ttlet tzdata_location = tt::URL::urlFromResourceDirectory() / "tzdata";
101 date::set_install(tzdata_location.nativePath());
102 try {
103 [[maybe_unused]] ttlet time_zone = date::current_zone();
104 } catch (std::runtime_error const &e) {
105 tt_log_error("Could not get current time zone: \"{}\"", e.what());
106 }
107#endif
108
109 // Make sure the console is in a valid state to write text to it.
110 tt::console_init();
111
112 ttlet r = tt_main(tt::narrow_cast<int>(arguments.size() - 1), arguments.data(), hInstance);
113
114 tt::system_status_shutdown();
115
116 for (auto argument: arguments) {
117 delete [] argument;
118 }
119 return r;
120}
121
122#else
123
124int main(int argc, char *argv[])
125{
126 // XXX - The URL system needs to know about the location of the executable.
127#if USE_OS_TZDB == 0
128 ttlet tzdata_location = tt::URL::urlFromResourceDirectory() / "tzdata";
129 date::set_install(tzdata_location.nativePath());
130 try {
131 [[maybe_unused]] ttlet time_zone = date::current_zone();
132 } catch (std::runtime_error const &e) {
133 tt_log_error("Could not get current time zone: \"{}\"", e.what());
134 }
135#endif
136
137 // Make sure the console is in a valid state to write text to it.
138 tt::console_init();
139
140 ttlet r = tt_main(argc, argv, {});
141 tt::system_status_shutdown();
142 return r;
143}
144
145#endif
146
int tt_main(int argc, char *argv[], tt::os_handle instance)
Main entry-point.
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
Windows entry-point.
Definition crt.hpp:65
T begin(T... args)
T next(T... args)
T reserve(T... args)
T what(T... args)