Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: visual_studio/NativeClientVSAddIn/InstallerResources/examples/hello_nacl_cpp/hello_nacl_cpp/hello_nacl_cpp.cc

Issue 11150028: Add new VS AddIn examples (Closed) Base URL: http://nativeclient-sdk.googlecode.com/svn/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 // This project demonstrates how to migrate a Windows desktop app to Native
7 // Client, running first as a Win32 application (define STEP1), then as a PPAPI
8 // plugin (define STEP2 through STEP6), and finally as a Native Client module.
9
10 // Start with STEP1 defined and the defines for STEP2 through STEP6 commented
11 // out. For each step in the process, un-comment the next #define, leaving the
12 // previous ones on. Ready, set, port!
13
14 // *** SELECT THE WIN32 PLATFORM AND RUN WITH #define STEP1 ONLY ***
15
16 #define STEP1
17 // Launches the original Windows desktop application, Hello World, which runs
18 // as WinMain. STEP1 encloses Windows-specific functions that are used with
19 // the WIN32 and PPAPI platforms. These will be removed when the full PPAPI
20 // port is finished (STEP6)
21
22 // *** SELECT THE PPAPI PLATFORM ***
23
24 //#define STEP2
25
26 // Client Module. STEP2 encloses the Native Client module APIs needed to link
27 // any app to the browser. The module does nothing except report
28 // starting/ending the function Instance_DidCreate. The Windows app does not
29 // run because it is not being called.
30
31 //#define STEP3
32 // What changed: Replace WinMain with WndProc, and call it from
33 // Instance_DidCreate, launching hello_nacl_plus in its own window. Since
34 // WndProc spins in its message loop, the call to Instance_DidCreate never
35 // returns. Close the hello_nacl_plus window and the module initialization will
36 // finish.
37
38 //#define STEP4
39 // What changed: In WndProc replace the message loop with a callback function.
40 // Now the app window and the Native Client module are running concurrently.
41
42 //#define STEP5
43 // What changed: Instance_DidCreate calls InitInstanceInBrowserWindow rather
44 // than InitInstanceInPCWindow. The InitInstanceInBrowserWindow uses
45 // postMessage to place text (now "Hello, Native Client") in the web page
46 // instead of opening and writing to a window.
47
48 //#define STEP6
49 // What changed: All the Windows code is def'd out, to prove we are
50 // PPAPI-compliant. The functional code that is running is the same as STEP5.
51
52 // *** SELECT THE NACL64 PLATFORM AND RUN ***
53
54 // What changed: The code is the same as STEP6, but you are using the SDK
55 // toolchain to compile it into a nexe. The module is now running as a real
56 // Native Client executable in a NaCl sandbox, with nacl-gdb attached.
57
58 // *** RUN YOUR MODULE IN THE WILD ***
59 // You can run your nexe outside of Visual Studio, directly from Chrome by
60 // following these steps:
61 // - Build STEP6 and verify the file
62 // <project directory>/newlib/hello_nacl_plus/hello_nacl_plus.nexe exists
63 // - Copy the folder <project directory>/hello_nacl_plus into your NaCl SDK's
64 // example directory.
65 // - Go to the NaCl SDK directory and launch the httpd.py server.
66 // - Launch Chrome, go to about:flags and enable the Native Client flag and
67 // relaunch Chrome
68 // - Point Chrome at localhost:5103/hello_nacl_plus
69
70 #ifdef STEP6
71 // remove Windows-dependent code.
72 #undef STEP1
73 #undef STEP3
74 #undef STEP4
75 #define NULL 0
76 #else
77 // includes for Windows APIs.
78 #include <windows.h>
79 #include <stdlib.h>
80 #include <tchar.h>
81 #endif
82
83 #ifdef STEP2
84 // includes for PPAPI C++
85 #include "ppapi/cpp/instance.h"
86 #include "ppapi/cpp/module.h"
87 #include "ppapi/cpp/var.h"
88 #include "ppapi/cpp/core.h"
89 #include "ppapi/cpp/completion_callback.h"
90 #include "ppapi/utility/completion_callback_factory.h"
91
92 // This is a known PPAPI platform problem (Issue 81375).
93 // When WinUser.h is included it defines PostMessage, so we undef it here.
94 // There is a work-around:
95 #ifdef PostMessage
96 #undef PostMessage
97 #endif
98
99 class NaClProjectInstance *myInstance;
100 int InitInstanceInPCWindow();
101 void InitInstanceInBrowserWindow();
102
103 #endif
104
105 #ifdef STEP2
106
107 // **** Native Client Framework ****
108
109 // The Instance class.
110 class NaClProjectInstance : public pp::Instance {
111 public:
112 explicit NaClProjectInstance(PP_Instance instance)
113 : pp::Instance(instance),
114 // Use this macro to eliminate compiler warning.
115 PP_ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {
116 myInstance = this;
117 }
118
119 virtual ~NaClProjectInstance() {
120 }
121
122 virtual bool Init(uint32_t argc, const char* argn[],
123 const char* argv[]) {
124 PostMessage(pp::Var("Creating Instance Start"));
125 #ifdef STEP5
126 // Will be included in STEP5 and STEP6
127 // Uses messaging to relay text to the module's view on the web page
128 InitInstanceInBrowserWindow();
129 #else
130 #ifdef STEP3
131 // Will be included in STEP3 and STEP4 only
132 // Uses WndProc to place text in a window separate from the browser.
133 InitInstanceInPCWindow();
134 #endif
135 #endif
136 PostMessage(pp::Var("Creating Instance End"));
137 return true;
138 }
139
140 #ifdef STEP4
141 // Implements Windows window message loop with a callback function.
142 void NaClProjectInstance::SendCallback(int result) {
143 pp::Core* core = pp::Module::Get()->core();
144 core->CallOnMainThread(
145 100,
146 factory_.NewCallback(&NaClProjectInstance::HandleWindowMsg),
147 result);
148 }
149
150 void HandleWindowMsg(int32_t result) {
151 MSG uMsg;
152 if (PeekMessage(&uMsg, NULL, 0, 0, PM_REMOVE)) {
153 TranslateMessage(&uMsg);
154 DispatchMessage(&uMsg);
155 }
156 SendCallback(0);
157 }
158
159 #endif
160 private:
161 virtual void HandleMessage(const pp::Var& var_message) {
162 }
163
164 pp::CompletionCallbackFactory<NaClProjectInstance> factory_;
165 };
166
167 // The Module class.
168 class NaClProjectModule : public pp::Module {
169 public:
170 NaClProjectModule() : pp::Module() {}
171 virtual ~NaClProjectModule() {}
172
173 virtual pp::Instance* CreateInstance(PP_Instance instance) {
174 return new NaClProjectInstance(instance);
175 }
176 };
177
178 namespace pp {
179
180 Module* CreateModule() {
181 return new NaClProjectModule();
182 }
183
184 }
185
186 #endif
187
188 // **** Application Code ****
189
190 #ifdef STEP1
191 // Desktop Windows Hello World app. Native Client agnostic.
192
193 static TCHAR szWindowClass[] = _T("win32app");
194 static TCHAR szTitle[] = _T("hello_nacl_plus");
195 HINSTANCE hInst;
196 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
197
198 // WinMain
199 int WINAPI WinMain(HINSTANCE hInstance,
200 HINSTANCE hPrevInstance,
201 LPSTR lpCmdLine,
202 int nCmdShow)
binji 2012/10/15 23:03:07 brace this line
203 {
204 WNDCLASSEX wcex;
205
206 wcex.cbSize = sizeof(WNDCLASSEX);
207 wcex.style = CS_HREDRAW | CS_VREDRAW;
208 wcex.lpfnWndProc = WndProc;
209 wcex.cbClsExtra = 0;
210 wcex.cbWndExtra = 0;
211 wcex.hInstance = hInstance;
212 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
213 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
214 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
215 wcex.lpszMenuName = NULL;
216 wcex.lpszClassName = szWindowClass;
217 wcex.hIconSm = LoadIcon(wcex.hInstance,
218 MAKEINTRESOURCE(IDI_APPLICATION));
219
220 if (!RegisterClassEx(&wcex)) {
221 MessageBox(NULL,
222 _T("Call to RegisterClassEx failed!"),
223 _T("hello_nacl_plus"),
224 NULL);
225
226 return 1;
227 }
228
229 hInst = hInstance;
230
231 HWND hWnd = CreateWindow(
232 szWindowClass, szTitle,
233 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
234 500, 100, NULL, NULL, hInstance, NULL);
235
236 if (!hWnd) {
237 MessageBox(NULL,
238 _T("Call to CreateWindow failed!"),
239 _T("hello_nacl_plus"),
240 NULL);
241
242 return 1;
243 }
244
245 ShowWindow(hWnd, nCmdShow);
246 UpdateWindow(hWnd);
247
248 // Main message loop:
249 MSG msg;
250 while (GetMessage(&msg, NULL, 0, 0)) {
251 TranslateMessage(&msg);
252 DispatchMessage(&msg);
253 }
254
255 return (int) msg.wParam;
256 }
257
258 // WndProc
259 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
binji 2012/10/15 23:03:07 brace this line
260 {
261 PAINTSTRUCT ps;
262 HDC hdc;
263 TCHAR greeting[] = _T("Hello, World!");
264
265 switch (message)
266 {
267 case WM_PAINT:
268 hdc = BeginPaint(hWnd, &ps);
269
270 // Here your application is laid out.
271 // For this introduction, we just print out "Hello, World!"
272 // in the top left corner.
273 TextOut(hdc,
274 5, 5,
275 greeting, _tcslen(greeting));
276 // End application-specific layout section.
277
278 EndPaint(hWnd, &ps);
279 break;
280 case WM_DESTROY:
281 PostQuitMessage(0);
282 break;
283 default:
284 return DefWindowProc(hWnd, message, wParam, lParam);
285 break;
286 }
287
288 return 0;
289 }
290 #endif
291
292 #ifdef STEP3
293 // Replace WinMain with InitInstanceInPCWindow so the NativeClient Module can
294 // launch the original application.
295 // Note the inclusion of a message-handling loop. STEP4 will replace the loop
296 // with a callback.
297
298 void shutDown(void);
299 HINSTANCE g_hInstance = NULL;
300 HWND g_hWnd = NULL;
301
302 int InitInstanceInPCWindow()
303 {
304 WNDCLASSEX winClass;
305 MSG uMsg;
306
307 memset(&uMsg,0,sizeof(uMsg));
308
309 winClass.lpszClassName = _T("MY_WINDOWS_CLASS");
310 winClass.cbSize = sizeof(WNDCLASSEX);
311 winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
312 winClass.lpfnWndProc = WndProc;
313 winClass.hInstance = g_hInstance;
314 winClass.hIcon = NULL;
315 winClass.hIconSm = NULL;
316 winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
317 winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
318 winClass.lpszMenuName = NULL;
319 winClass.cbClsExtra = 0;
320 winClass.cbWndExtra = 0;
321
322 if (!RegisterClassEx(&winClass))
323 return E_FAIL;
324
325 g_hWnd = CreateWindowEx(
326 NULL,_T("MY_WINDOWS_CLASS"),
327 _T("hello_nacl_plus"),
328 WS_OVERLAPPEDWINDOW,
329 0, 0, 640, 480, NULL, NULL, g_hInstance, NULL);
330
331 if (g_hWnd == NULL)
332 return E_FAIL;
333
334 ShowWindow(g_hWnd, 1);
335
336 UpdateWindow(g_hWnd);
337
338 #ifdef STEP4
339 // Skip the message loop, schedule a callback instead to periodically check
340 // for messages. Here we schedule at 100ms intervals.
341 myInstance->SendCallback(0);
342 return 0;
343 #else
344 // Main message loop, Windows style.
345 while(uMsg.message != WM_QUIT) {
346 if (PeekMessage(&uMsg, NULL, 0, 0, PM_REMOVE)) {
347 TranslateMessage(&uMsg);
348 DispatchMessage(&uMsg);
349 }
350 }
351 return uMsg.wParam;
352 #endif
353
354 }
355 #endif
356
357 #ifdef STEP5
358 // Pass the text to the browser page, there is no separate app window anymore.
359 // The text is added as a new element to the page, it does not appear in the
360 // module's embed view.
361 void InitInstanceInBrowserWindow() {
362 myInstance->PostMessage(pp::Var("Hello, Native Client!"));
363 }
364 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698