• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 
8 #include <windows.h>
9 #include <tchar.h>
10 
11 #include "SkTypes.h"
12 #include "Timer.h"
13 #include "Window_win.h"
14 #include "../Application.h"
15 
16 using sk_app::Application;
17 
tchar_to_utf8(const TCHAR * str)18 static char* tchar_to_utf8(const TCHAR* str) {
19 #ifdef _UNICODE
20     int size = WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), NULL, 0, NULL, NULL);
21     char* str8 = (char*)sk_malloc_throw(size + 1);
22     WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), str8, size, NULL, NULL);
23     str8[size] = '\0';
24     return str8;
25 #else
26     return _strdup(str);
27 #endif
28 }
29 
30 // This file can work with GUI or CONSOLE subsystem types since we define _tWinMain and main().
31 
32 static int main_common(HINSTANCE hInstance, int show, int argc, char**argv);
33 
_tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)34 int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine,
35                        int nCmdShow) {
36 
37     // convert from lpCmdLine to argc, argv.
38     char* argv[4096];
39     int argc = 0;
40     TCHAR exename[1024], *next;
41     int exenameLen = GetModuleFileName(NULL, exename, SK_ARRAY_COUNT(exename));
42     // we're ignoring the possibility that the exe name exceeds the exename buffer
43     (void)exenameLen;
44     argv[argc++] = tchar_to_utf8(exename);
45     TCHAR* arg = _tcstok_s(lpCmdLine, _T(" "), &next);
46     while (arg != NULL) {
47         argv[argc++] = tchar_to_utf8(arg);
48         arg = _tcstok_s(NULL, _T(" "), &next);
49     }
50     int result = main_common(hInstance, nCmdShow, argc, argv);
51     for (int i = 0; i < argc; ++i) {
52         sk_free(argv[i]);
53     }
54     return result;
55 }
56 
main(int argc,char ** argv)57 int main(int argc, char**argv) {
58     return main_common(GetModuleHandle(NULL), SW_SHOW, argc, argv);
59 }
60 
main_common(HINSTANCE hInstance,int show,int argc,char ** argv)61 static int main_common(HINSTANCE hInstance, int show, int argc, char**argv) {
62 
63     Application* app = Application::Create(argc, argv, (void*)hInstance);
64 
65     MSG msg = { 0 };
66 
67     // Main message loop
68     while (WM_QUIT != msg.message) {
69         if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
70             TranslateMessage(&msg);
71             DispatchMessage(&msg);
72         } else {
73             app->onIdle();
74         }
75     }
76 
77     delete app;
78 
79     return (int)msg.wParam;
80 }
81