1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2014-2015 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
17
18 #define _ATL_FREE_THREADED
19 #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS
20 #include <AtlBase.h>
21 #include <AtlCom.h>
22 #include <AtlCtl.h>
23 #include <AtlWin.h>
24 #include <AtlStr.h>
25 #include <AtlFile.h>
26 #include <AtlTypes.h>
27 #include <exdisp.h>
28 #include <exdispid.h>
29 #include <urlmon.h>
30 #include <string>
31 #include "win_util.h"
32 #include "version.h"
33 #include "boinc_api.h"
34 #include "diagnostics.h"
35 #include "filesys.h"
36 #include "webserver.h"
37 #include "browser.h"
38 #include "browser_i.h"
39 #include "browser_i.c"
40 #include "browser_win.h"
41 #include "browserlog.h"
42 #include "browserctrl_win.h"
43 #include "browserwnd_win.h"
44 #include "browsermain_win.h"
45
46
47 #ifdef _MSC_VER
48 #define snprintf _snprintf
49 #endif
50
51
52 CBrowserModule _AtlModule;
53
54
CBrowserModule()55 CBrowserModule::CBrowserModule()
56 {
57 m_pWnd = NULL;
58 }
59
RegisterWebControlCompatiblity()60 int CBrowserModule::RegisterWebControlCompatiblity()
61 {
62 LONG lReturnValue;
63 HKEY hKey;
64 TCHAR szPath[MAX_PATH-1];
65 LPTSTR lpszFileName = NULL;
66 DWORD dwSize = 0;
67
68
69 // Isolate the executable name
70 GetModuleFileName(NULL, szPath, (sizeof(szPath)/sizeof(TCHAR)));
71 lpszFileName = _tcsrchr(szPath, '\\');
72 if (lpszFileName)
73 lpszFileName++;
74 else
75 return ERROR_FILE_NOT_FOUND;
76
77 lReturnValue = RegOpenKeyEx(
78 HKEY_CURRENT_USER,
79 _T("SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION"),
80 0,
81 KEY_WRITE,
82 &hKey
83 );
84 if (lReturnValue == ERROR_SUCCESS) {
85 DWORD dwIE11 = 11001;
86 RegSetValueEx(hKey, lpszFileName, 0, REG_DWORD, (const BYTE *)&dwIE11, sizeof(DWORD));
87 RegCloseKey(hKey);
88 }
89
90
91 return 0;
92 }
93
InitializeCom()94 HRESULT CBrowserModule::InitializeCom() throw()
95 {
96 return OleInitialize(NULL);
97 }
98
UninitializeCom()99 void CBrowserModule::UninitializeCom() throw()
100 {
101 OleUninitialize();
102 }
103
PreMessageLoop(int nShowCmd)104 HRESULT CBrowserModule::PreMessageLoop(int nShowCmd) throw()
105 {
106 HRESULT hr = 0;
107 RECT rc = {0, 0, 0, 0};
108 DWORD dwExStyle = 0;
109 DWORD dwStyle = 0;
110 APP_INIT_DATA aid;
111 char szWindowTitle[256];
112 char szWindowInfo[256];
113 char szDebuggingInfo[256];
114
115
116 hr = __super::PreMessageLoop(nShowCmd);
117 if (FAILED(hr)) {
118 return hr;
119 }
120
121 // Initialize ATL Window Classes
122 //
123 AtlAxWinInit();
124
125 // Prepare environment for web browser control
126 //
127 RegisterWebControlCompatiblity();
128
129 // Prepare environment for detecting system conditions
130 //
131 boinc_parse_init_data_file();
132 boinc_get_init_data(aid);
133
134 // Create Window Instance
135 //
136 m_pWnd = new CHTMLBrowserWnd();
137 if (m_pWnd == NULL)
138 {
139 __super::PostMessageLoop();
140 return E_OUTOFMEMORY;
141 }
142
143 // Construct the window caption
144 //
145 if (aid.app_version) {
146 snprintf(
147 szWindowInfo, sizeof(szWindowInfo),
148 "%s version %.2f [workunit: %s]",
149 aid.app_name, aid.app_version/100.0, aid.wu_name
150 );
151 } else {
152 snprintf(
153 szWindowInfo, sizeof(szWindowInfo),
154 "%s [workunit: %s]",
155 aid.app_name, aid.wu_name
156 );
157 }
158
159 if (is_htmlgfx_in_debug_mode()) {
160 snprintf(szDebuggingInfo, sizeof(szDebuggingInfo), "[Web Server Port: %d]", get_htmlgfx_webserver_port());
161 } else {
162 strcpy(szDebuggingInfo, "");
163 }
164
165 snprintf(szWindowTitle, sizeof(szWindowTitle)-1, "%s %s", szWindowInfo, szDebuggingInfo);
166
167 // Determine window size and placement
168 if (is_htmlgfx_in_fullscreen_mode()) {
169 HDC dc = GetDC(NULL);
170 rc.left = 0;
171 rc.top = 0;
172 rc.right = GetDeviceCaps(dc, HORZRES);
173 rc.bottom = GetDeviceCaps(dc, VERTRES);
174 ReleaseDC(NULL, dc);
175
176 dwStyle = WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_POPUP;
177 dwExStyle = WS_EX_APPWINDOW|WS_EX_TOPMOST;
178 while(ShowCursor(false) >= 0);
179 } else {
180 rc.left = 0;
181 rc.top = 0;
182 rc.right = 1024;
183 rc.bottom = 768;
184
185 dwStyle = WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW;
186 dwExStyle = WS_EX_APPWINDOW|WS_EX_WINDOWEDGE;
187 while(ShowCursor(true) < 0);
188 }
189
190 // Create Window
191 //
192 m_pWnd->Create(GetDesktopWindow(), rc, szWindowTitle, dwStyle, dwExStyle);
193 m_pWnd->ShowWindow(nShowCmd);
194
195 return S_OK;
196 }
197
PostMessageLoop()198 HRESULT CBrowserModule::PostMessageLoop() throw()
199 {
200 if (m_pWnd)
201 {
202 delete m_pWnd;
203 m_pWnd = NULL;
204 }
205
206 AtlAxWinTerm();
207
208 return __super::PostMessageLoop();
209 }
210
211
run(int argc,char ** argv)212 int run(int argc, char** argv) {
213 return _AtlModule.WinMain(SW_SHOWDEFAULT);
214 }
215
216
217 extern int main(int, char**);
WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR Args,int WinMode)218 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
219 LPSTR command_line;
220 char* argv[100];
221 int argc;
222 int retval = 0;
223
224 command_line = GetCommandLine();
225 argc = parse_command_line(command_line, argv);
226
227 WSADATA wsdata;
228 WSAStartup( MAKEWORD( 1, 1 ), &wsdata);
229
230 retval = main(argc, argv);
231
232 WSACleanup();
233
234 return retval;
235 }
236