1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/gatemaindefs.h"
#include "gate/platforms.h"
/* generic implementation: */
void gate_main_init()
{
gate_platform_init();
}
void gate_main_uninit()
{
}
#if defined(GATE_MAIN_WINAPI_IMPL)
#include "gate/strings.h"
GATE_CORE_API int GATE_CALL gate_main_winapi_entry(gate_main_func main_func, void* hInstance, void* hPrevInstance, void* lpCmdLine, int nShowCmd);
#if defined(GATE_SYS_WIN16)
# define GATE_MAX_APP_PATH_LEN 260
# define GATE_MAX_APP_ARGS_LEN 260
#else
# define GATE_MAX_APP_PATH_LEN 2048
# define GATE_MAX_APP_ARGS_LEN 8192
GATE_CORE_API int GATE_CALL gate_main_wchar_entry(gate_main_func main_func, int argc, wchar_t* argv[], wchar_t* envp[]);
#endif
static int gate_main_winapi_entry_impl(gate_main_func main_func, HINSTANCE hInstance, LPCWSTR cmdline)
{
static TCHAR nativeAppPath[GATE_MAX_APP_PATH_LEN];
static DWORD nativeAppPathUsed;
static char appPath[GATE_MAX_APP_PATH_LEN];
static char cmdLineBuffer[GATE_MAX_APP_ARGS_LEN];
static gate_size_t cmdLineBufferLen = sizeof(cmdLineBuffer);
gate_uintptr_t apphandle = (gate_uintptr_t)hInstance;
char const* cmdArgs[192];
gate_size_t cmdArgCount = sizeof(cmdArgs) / sizeof(cmdArgs[0]);
gate_size_t cmdArgsUsed = 0;
int ret;
gate_win32_set_hinst(hInstance);
gate_main_init();
if (cmdline != NULL)
{
cmdArgsUsed = gate_win32_parse_command_line(cmdline, &cmdLineBuffer[0], cmdLineBufferLen, cmdArgs, cmdArgCount);
}
else
{
cmdArgsUsed = 0;
}
if (cmdArgsUsed == 0)
{
cmdArgs[1] = 0;
cmdArgsUsed = 1;
}
nativeAppPathUsed = GetModuleFileName((HMODULE)hInstance, nativeAppPath, sizeof(nativeAppPath) / sizeof(nativeAppPath[0]) - 1);
if (nativeAppPathUsed == 0)
{
gate_str_print_text(appPath, sizeof(appPath), "winapp", 6);
}
else
{
gate_win32_winstr_2_utf8(nativeAppPath, nativeAppPathUsed, appPath, sizeof(appPath));
}
ret = main_func(appPath, (cmdArgsUsed <= 1) ? NULL : &cmdArgs[1], (cmdArgsUsed <= 1) ? 0 : cmdArgsUsed - 1, apphandle);
gate_main_uninit();
return ret;
}
int gate_main_winapi_entry(gate_main_func main_func, void* hInstance, void* hPrevInstance, void* lpCmdLine, int nShowCmd)
{
int ret;
#if !defined(GATE_SYS_WIN16)
LPTSTR tcmd_line = GetCommandLine();
#endif
#if defined(GATE_WIN32_UNICODE)
LPWSTR wcmd_line = tcmd_line;
#else
#if defined(GATE_SYS_WIN16)
LPWSTR wcmd_line = NULL;
#else
static WCHAR wcmd_line[8192];
gate_size_t tcmd_line_length = gate_str_length(tcmd_line);
gate_win32_ansi_2_utf16(tcmd_line, tcmd_line_length, (wchar_t*)&wcmd_line[0], sizeof(wcmd_line) / sizeof(wcmd_line[0]));
#endif
#endif
GATE_UNUSED_ARG(hPrevInstance);
GATE_UNUSED_ARG(lpCmdLine);
GATE_UNUSED_ARG(nShowCmd);
ret = gate_main_winapi_entry_impl(main_func, (HINSTANCE)hInstance, wcmd_line);
return ret;
}
#if !defined(GATE_COMPILER_WATCOM)
int gate_main_wchar_entry(gate_main_func main_func, int argc, wchar_t* argv[], wchar_t* envp[])
{
int ret = 0;
gate_uintptr_t apphandle = 0;
char* c_args[1024] = GATE_INIT_EMPTY;
gate_size_t ndx;
gate_size_t len;
GATE_UNUSED_ARG(envp);
#if !defined(GATE_SYS_WINSTORE)
apphandle = (gate_uintptr_t)gate_win32_get_hinst();
#endif
gate_main_init();
if (argc >= sizeof(c_args) / sizeof(c_args[0]))
{
argc = sizeof(c_args) / sizeof(c_args[0]) - 1;
}
for (ndx = 0; ndx != argc; ++ndx)
{
len = gate_str16_length(argv[ndx]);
c_args[ndx] = (char*)gate_mem_alloc(len * 2);
gate_str_utf16_2_utf8(argv[ndx], len, c_args[ndx], len * 2);
}
ret = main_func(c_args[0], (argc <= 1) ? NULL : (char const* const*)&c_args[1], (argc < 1) ? 0 : (argc - 1), apphandle);
for (ndx = 0; ndx != argc; ++ndx)
{
if (c_args[ndx] != NULL)
{
gate_mem_dealloc(c_args[ndx]);
c_args[ndx] = NULL;
}
}
gate_main_uninit();
return ret;
}
#endif /* !GATE_COMPILER_WATCOM */
#endif /* GATE_MAIN_WINAPI_IMPL */
#if defined(GATE_MAIN_WASM_WEB_IMPL)
#define GATE_MAIN_NO_STDC_IMPL 1
#include "gate/results.h"
#include "gate/platform/wasm/wasm_gate.h"
#include <stdio.h>
int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[])
{
(void)argc;
(void)argv;
gate_wasm_register_gate_main(main_func);
gate_wasm_register_gate_main_program_name(argv[0]);
gate_main_init();
return gate_wasm_start_main();
}
#endif /* GATE_MAIN_WASM_WEB_IMPL */
#if defined(GATE_MAIN_DOS_IMPL)
#include "gate/strings.h"
#define GATE_MAIN_NO_STDC_IMPL 1
#include "process.h"
static char near main_command_path[256];
static char near main_command_argline[256];
static char const* main_command_args[32];
int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[])
{
int ret_code;
char* start;
char* ptr;
gate_size_t cmdline_len;
gate_size_t args_count = 0;
gate_uintptr_t apphandle = 0;
GATE_UNUSED_ARG(argc);
GATE_UNUSED_ARG(argv);
_cmdname(main_command_path);
getcmd(main_command_argline);
cmdline_len = gate_str_length(main_command_argline);
ptr = start = main_command_argline;
while (cmdline_len-- > 0)
{
if (*ptr == ' ')
{
*ptr = '\0';
if (ptr != start)
{
main_command_args[args_count++] = start;
}
start = ptr + 1;
}
++ptr;
}
if (ptr != start)
{
main_command_args[args_count++] = start;
}
gate_dos_register_app_path(main_command_path);
gate_main_init();
ret_code = main_func(main_command_path, &main_command_args[0], args_count, apphandle);
gate_main_uninit();
return ret_code;
}
#endif /* GATE_MAIN_DOS_IMPL */
#if defined(GATE_MAIN_EFI_IMPL)
# define GATE_MAIN_NO_STDC_IMPL 1
# include <lib.h>
# include "gate/platform/efi/efi_gate.h"
int GATE_CORE_API gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[])
{
int ret_code;
gate_uintptr_t apphandle = 0;
gate_main_init();
gate_platform_efi_init(LibImageHandle, ST);
ret_code = gate_platform_efi_run_main(main_func);
gate_main_uninit();
return ret_code;
}
#endif /* GATE_MAIN_EFI_IMPL */
#if defined(GATE_MAIN_ANDROID_IMPL)
# define GATE_MAIN_NO_STDC_IMPL 1
# include <android/native_activity.h>
# include "gate/platform/android/gate_android_app.h"
void GATE_CORE_API gate_main_android_entry(gate_main_func main_func, void* activity, void* savedState, size_t savedStateSize)
{
gate_result_t result = gate_platform_init();
gate_android_app_create((ANativeActivity*)activity, savedState, savedStateSize, main_func);
}
#endif /* GATE_MAIN_ANDROID_IMPL */
#if defined(GATE_MAIN_ARDUINO_IMPL)
#include <Arduino.h>
void gate_main_arduino_setup(void)
{
gate_main_init();
}
void gate_main_arduino_loop(void)
{
static char const progname[] PROGMEM = "arduino";
gate_main(progname, NULL, 0, 0);
}
#endif
#if defined(GATE_MAIN_WASI_IMPL)
#include <stdio.h>
#include <unistd.h>
int gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[])
{
int ret_code;
gate_uintptr_t apphandle = 0;
gate_main_init();
ret_code = main_func(argv[0], (argc <= 1) ? NULL : (char const* const*)&argv[1], (argc <= 1) ? 0 : (argc - 1), apphandle);
gate_main_uninit();
return ret_code;
}
#endif /* GATE_MAIN_WASI_IMPL */
#if !defined(GATE_MAIN_NO_STDC_IMPL)
GATE_CORE_API int GATE_CALL gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[]);
int gate_main_stdc_entry(gate_main_func main_func, int argc, char* argv[])
{
int ret_code;
gate_uintptr_t apphandle = 0;
#if defined(GATE_SYS_WIN)
apphandle = (gate_uintptr_t)gate_win32_get_hinst();
gate_win32_set_hinst((HINSTANCE)apphandle);
#endif
gate_main_init();
ret_code = main_func(argv[0], (argc <= 1) ? NULL : (char const* const*)&argv[1], (argc <= 1) ? 0 : (argc - 1), apphandle);
gate_main_uninit();
return ret_code;
}
#endif
|