Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/Defaults.cpp @ 473:628941d63b8c bgo-commands-codegen
Ongoing work. Parsing tests work
author | bgo-osimis |
---|---|
date | Wed, 13 Feb 2019 12:07:00 +0100 |
parents | 26b90b110719 |
children | 3b4df9925db6 |
rev | line source |
---|---|
222 | 1 #include "Defaults.h" |
2 | |
223 | 3 #include "WasmWebService.h" |
431
26b90b110719
added DelayedCallExecutor to avoid using sleep() in C++ that consumes 100% CPU once executed in WASM
am@osimis.io
parents:
418
diff
changeset
|
4 #include "WasmDelayedCallExecutor.h" |
223 | 5 #include <Framework/dev.h> |
6 #include "Framework/Widgets/TestCairoWidget.h" | |
7 #include <Framework/Viewport/WidgetViewport.h> | |
228 | 8 #include <algorithm> |
242 | 9 #include "Applications/Wasm/StartupParametersBuilder.h" |
307 | 10 #include "Platforms/Wasm/WasmPlatformApplicationAdapter.h" |
223 | 11 |
12 static unsigned int width_ = 0; | |
13 static unsigned int height_ = 0; | |
14 | |
15 /**********************************/ | |
16 | |
288 | 17 static std::unique_ptr<OrthancStone::IStoneApplication> application; |
307 | 18 static std::unique_ptr<OrthancStone::WasmPlatformApplicationAdapter> applicationWasmAdapter = NULL; |
288 | 19 static std::unique_ptr<OrthancStone::StoneApplicationContext> context; |
242 | 20 static OrthancStone::StartupParametersBuilder startupParametersBuilder; |
253 | 21 static OrthancStone::MessageBroker broker; |
242 | 22 |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
23 static OrthancStone::ViewportContentChangedObserver viewportContentChangedObserver_(broker); |
223 | 24 static OrthancStone::StatusBar statusBar_; |
25 | |
227 | 26 static std::list<std::shared_ptr<OrthancStone::WidgetViewport>> viewports_; |
27 | |
228 | 28 std::shared_ptr<OrthancStone::WidgetViewport> FindViewportSharedPtr(ViewportHandle viewport) { |
29 for (const auto& v : viewports_) { | |
30 if (v.get() == viewport) { | |
31 return v; | |
32 } | |
33 } | |
229 | 34 assert(false); |
35 return std::shared_ptr<OrthancStone::WidgetViewport>(); | |
228 | 36 } |
37 | |
223 | 38 #ifdef __cplusplus |
39 extern "C" { | |
40 #endif | |
41 | |
42 using namespace OrthancStone; | |
227 | 43 |
44 // when WASM needs a C++ viewport | |
45 ViewportHandle EMSCRIPTEN_KEEPALIVE CreateCppViewport() { | |
46 | |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
47 std::shared_ptr<OrthancStone::WidgetViewport> viewport(new OrthancStone::WidgetViewport(broker)); |
287 | 48 printf("viewport %x\n", (int)viewport.get()); |
227 | 49 |
50 viewports_.push_back(viewport); | |
51 | |
52 printf("There are now %d viewports in C++\n", viewports_.size()); | |
53 | |
54 viewport->SetStatusBar(statusBar_); | |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
55 |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
56 viewport->RegisterObserverCallback( |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
57 new Callable<ViewportContentChangedObserver, IViewport::ViewportChangedMessage> |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
58 (viewportContentChangedObserver_, &ViewportContentChangedObserver::OnViewportChanged)); |
227 | 59 |
60 return viewport.get(); | |
61 } | |
62 | |
63 // when WASM does not need a viewport anymore, it should release it | |
64 void EMSCRIPTEN_KEEPALIVE ReleaseCppViewport(ViewportHandle viewport) { | |
65 viewports_.remove_if([viewport](const std::shared_ptr<OrthancStone::WidgetViewport>& v) { return v.get() == viewport;}); | |
66 | |
67 printf("There are now %d viewports in C++\n", viewports_.size()); | |
68 } | |
69 | |
70 void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport) { | |
223 | 71 |
72 printf("CreateWasmApplication\n"); | |
73 | |
253 | 74 application.reset(CreateUserApplication(broker)); |
307 | 75 applicationWasmAdapter.reset(CreateWasmApplicationAdapter(broker, application.get())); |
255 | 76 WasmWebService::SetBroker(broker); |
431
26b90b110719
added DelayedCallExecutor to avoid using sleep() in C++ that consumes 100% CPU once executed in WASM
am@osimis.io
parents:
418
diff
changeset
|
77 WasmDelayedCallExecutor::SetBroker(broker); |
223 | 78 |
242 | 79 startupParametersBuilder.Clear(); |
223 | 80 } |
81 | |
82 void EMSCRIPTEN_KEEPALIVE SetStartupParameter(const char* keyc, | |
83 const char* value) { | |
242 | 84 startupParametersBuilder.SetStartupParameter(keyc, value); |
223 | 85 } |
86 | |
418 | 87 void EMSCRIPTEN_KEEPALIVE StartWasmApplication(const char* baseUri) { |
223 | 88 |
89 printf("StartWasmApplication\n"); | |
90 | |
91 // recreate a command line from uri arguments and parse it | |
92 boost::program_options::variables_map parameters; | |
242 | 93 boost::program_options::options_description options; |
94 application->DeclareStartupOptions(options); | |
95 startupParametersBuilder.GetStartupParameters(parameters, options); | |
223 | 96 |
418 | 97 context.reset(new OrthancStone::StoneApplicationContext(broker)); |
98 context->SetOrthancBaseUrl(baseUri); | |
99 printf("Base URL to Orthanc API: [%s]\n", baseUri); | |
100 context->SetWebService(OrthancStone::WasmWebService::GetInstance()); | |
431
26b90b110719
added DelayedCallExecutor to avoid using sleep() in C++ that consumes 100% CPU once executed in WASM
am@osimis.io
parents:
418
diff
changeset
|
101 context->SetDelayedCallExecutor(OrthancStone::WasmDelayedCallExecutor::GetInstance()); |
242 | 102 application->Initialize(context.get(), statusBar_, parameters); |
103 application->InitializeWasm(); | |
223 | 104 |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
229
diff
changeset
|
105 // viewport->SetSize(width_, height_); |
223 | 106 printf("StartWasmApplication - completed\n"); |
107 } | |
108 | |
386
e33659decec5
renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
385
diff
changeset
|
109 void EMSCRIPTEN_KEEPALIVE WasmDoAnimation() |
223 | 110 { |
228 | 111 for (auto viewport : viewports_) { |
386
e33659decec5
renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
385
diff
changeset
|
112 // TODO Only launch the JavaScript timer if "HasAnimation()" |
e33659decec5
renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
385
diff
changeset
|
113 if (viewport->HasAnimation()) |
228 | 114 { |
386
e33659decec5
renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
385
diff
changeset
|
115 viewport->DoAnimation(); |
228 | 116 } |
117 | |
223 | 118 } |
119 | |
120 } | |
121 | |
122 | |
228 | 123 void EMSCRIPTEN_KEEPALIVE ViewportSetSize(ViewportHandle viewport, unsigned int width, unsigned int height) |
223 | 124 { |
125 width_ = width; | |
126 height_ = height; | |
127 | |
228 | 128 viewport->SetSize(width, height); |
223 | 129 } |
130 | |
228 | 131 int EMSCRIPTEN_KEEPALIVE ViewportRender(ViewportHandle viewport, |
227 | 132 unsigned int width, |
223 | 133 unsigned int height, |
134 uint8_t* data) | |
135 { | |
278 | 136 viewportContentChangedObserver_.Reset(); |
223 | 137 |
138 //printf("ViewportRender called %dx%d\n", width, height); | |
139 if (width == 0 || | |
140 height == 0) | |
141 { | |
142 return 1; | |
143 } | |
144 | |
145 Orthanc::ImageAccessor surface; | |
146 surface.AssignWritable(Orthanc::PixelFormat_BGRA32, width, height, 4 * width, data); | |
147 | |
227 | 148 viewport->Render(surface); |
223 | 149 |
150 // Convert from BGRA32 memory layout (only color mode supported by | |
151 // Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 (as | |
152 // expected by HTML5 canvas). This simply amounts to swapping the | |
153 // B and R channels. | |
154 uint8_t* p = data; | |
155 for (unsigned int y = 0; y < height; y++) { | |
156 for (unsigned int x = 0; x < width; x++) { | |
157 uint8_t tmp = p[0]; | |
158 p[0] = p[2]; | |
159 p[2] = tmp; | |
160 | |
161 p += 4; | |
162 } | |
163 } | |
164 | |
165 return 1; | |
166 } | |
167 | |
168 | |
228 | 169 void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(ViewportHandle viewport, |
170 unsigned int rawButton, | |
223 | 171 int x, |
172 int y, | |
173 unsigned int rawModifiers) | |
174 { | |
175 OrthancStone::MouseButton button; | |
176 switch (rawButton) | |
177 { | |
178 case 0: | |
179 button = OrthancStone::MouseButton_Left; | |
180 break; | |
181 | |
182 case 1: | |
183 button = OrthancStone::MouseButton_Middle; | |
184 break; | |
185 | |
186 case 2: | |
187 button = OrthancStone::MouseButton_Right; | |
188 break; | |
189 | |
190 default: | |
191 return; // Unknown button | |
192 } | |
193 | |
228 | 194 viewport->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */); |
223 | 195 } |
196 | |
197 | |
228 | 198 void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(ViewportHandle viewport, |
199 int deltaY, | |
223 | 200 int x, |
201 int y, | |
202 int isControl) | |
203 { | |
228 | 204 if (deltaY != 0) |
223 | 205 { |
206 OrthancStone::MouseWheelDirection direction = (deltaY < 0 ? | |
207 OrthancStone::MouseWheelDirection_Up : | |
208 OrthancStone::MouseWheelDirection_Down); | |
209 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; | |
210 | |
211 if (isControl != 0) | |
212 { | |
213 modifiers = OrthancStone::KeyboardModifiers_Control; | |
214 } | |
215 | |
228 | 216 viewport->MouseWheel(direction, x, y, modifiers); |
223 | 217 } |
218 } | |
219 | |
220 | |
228 | 221 void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(ViewportHandle viewport, |
222 int x, | |
223 | 223 int y) |
224 { | |
228 | 225 viewport->MouseMove(x, y); |
223 | 226 } |
227 | |
228 | 228 void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(ViewportHandle viewport, |
327 | 229 int key, |
230 const char* keyChar, | |
223 | 231 bool isShiftPressed, |
232 bool isControlPressed, | |
233 bool isAltPressed) | |
234 | |
235 { | |
228 | 236 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; |
237 if (isShiftPressed) { | |
238 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift); | |
223 | 239 } |
228 | 240 if (isControlPressed) { |
241 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control); | |
242 } | |
243 if (isAltPressed) { | |
244 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt); | |
245 } | |
327 | 246 |
247 char c = 0; | |
248 if (keyChar != NULL && key == OrthancStone::KeyboardKeys_Generic) { | |
249 c = keyChar[0]; | |
250 } | |
251 viewport->KeyPressed(static_cast<OrthancStone::KeyboardKeys>(key), c, modifiers); | |
223 | 252 } |
253 | |
254 | |
228 | 255 void EMSCRIPTEN_KEEPALIVE ViewportMouseUp(ViewportHandle viewport) |
223 | 256 { |
228 | 257 viewport->MouseUp(); |
223 | 258 } |
259 | |
260 | |
228 | 261 void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter(ViewportHandle viewport) |
223 | 262 { |
228 | 263 viewport->MouseEnter(); |
223 | 264 } |
265 | |
266 | |
228 | 267 void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave(ViewportHandle viewport) |
223 | 268 { |
228 | 269 viewport->MouseLeave(); |
223 | 270 } |
271 | |
287 | 272 const char* EMSCRIPTEN_KEEPALIVE SendMessageToStoneApplication(const char* message) |
273 { | |
274 static std::string output; // we don't want the string to be deallocated when we return to JS code so we always use the same string (this is fine since JS is single-thread) | |
275 | |
307 | 276 printf("SendMessageToStoneApplication\n"); |
309 | 277 printf("%s", message); |
307 | 278 |
279 if (applicationWasmAdapter.get() != NULL) { | |
280 printf("sending message to C++\n"); | |
281 applicationWasmAdapter->HandleMessageFromWeb(output, std::string(message)); | |
287 | 282 return output.c_str(); |
283 } | |
307 | 284 printf("This stone application does not have a Web Adapter"); |
285 return NULL; | |
287 | 286 } |
287 | |
223 | 288 |
289 #ifdef __cplusplus | |
290 } | |
291 #endif |