222
|
1 #include "Defaults.h"
|
|
2
|
223
|
3 #include "WasmWebService.h"
|
|
4 #include <Framework/dev.h>
|
|
5 #include "Framework/Widgets/TestCairoWidget.h"
|
|
6 #include <Framework/Viewport/WidgetViewport.h>
|
|
7 #include <Framework/Widgets/LayerWidget.h>
|
228
|
8 #include <algorithm>
|
223
|
9
|
|
10 static unsigned int width_ = 0;
|
|
11 static unsigned int height_ = 0;
|
|
12
|
|
13 /**********************************/
|
|
14
|
|
15 static std::auto_ptr<OrthancStone::BasicWasmApplication> application;
|
|
16 static OrthancStone::ChangeObserver changeObserver_;
|
|
17 static OrthancStone::StatusBar statusBar_;
|
|
18
|
|
19
|
227
|
20 static std::list<std::shared_ptr<OrthancStone::WidgetViewport>> viewports_;
|
|
21
|
228
|
22 std::shared_ptr<OrthancStone::WidgetViewport> FindViewportSharedPtr(ViewportHandle viewport) {
|
|
23 for (const auto& v : viewports_) {
|
|
24 if (v.get() == viewport) {
|
|
25 return v;
|
|
26 }
|
|
27 assert(false);
|
|
28 }
|
|
29 }
|
|
30
|
223
|
31 #ifdef __cplusplus
|
|
32 extern "C" {
|
|
33 #endif
|
|
34
|
|
35 using namespace OrthancStone;
|
227
|
36
|
|
37 // when WASM needs a C++ viewport
|
|
38 ViewportHandle EMSCRIPTEN_KEEPALIVE CreateCppViewport() {
|
|
39
|
|
40 std::shared_ptr<OrthancStone::WidgetViewport> viewport(new OrthancStone::WidgetViewport);
|
|
41 printf("viewport %x\n", viewport.get());
|
|
42
|
|
43 viewports_.push_back(viewport);
|
|
44
|
|
45 printf("There are now %d viewports in C++\n", viewports_.size());
|
|
46
|
|
47 viewport->SetStatusBar(statusBar_);
|
|
48 viewport->Register(changeObserver_);
|
|
49
|
|
50 return viewport.get();
|
|
51 }
|
|
52
|
|
53 // when WASM does not need a viewport anymore, it should release it
|
|
54 void EMSCRIPTEN_KEEPALIVE ReleaseCppViewport(ViewportHandle viewport) {
|
|
55 viewports_.remove_if([viewport](const std::shared_ptr<OrthancStone::WidgetViewport>& v) { return v.get() == viewport;});
|
|
56
|
|
57 printf("There are now %d viewports in C++\n", viewports_.size());
|
|
58 }
|
|
59
|
|
60 void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport) {
|
223
|
61
|
|
62 printf("CreateWasmApplication\n");
|
|
63
|
|
64 application.reset(CreateUserApplication());
|
|
65
|
|
66 boost::program_options::options_description options;
|
|
67 application->DeclareStartupOptions(options);
|
|
68 }
|
|
69
|
|
70 void EMSCRIPTEN_KEEPALIVE SetStartupParameter(const char* keyc,
|
|
71 const char* value) {
|
|
72 application->SetStartupParameter(keyc, value);
|
|
73 }
|
|
74
|
228
|
75 void EMSCRIPTEN_KEEPALIVE StartWasmApplication(ViewportHandle viewport) {
|
223
|
76
|
|
77 printf("StartWasmApplication\n");
|
|
78
|
|
79 // recreate a command line from uri arguments and parse it
|
|
80 boost::program_options::variables_map parameters;
|
|
81 application->GetStartupParameters(parameters);
|
|
82
|
228
|
83 BasicWasmApplicationContext& context = dynamic_cast<BasicWasmApplicationContext&>(application->CreateApplicationContext(OrthancStone::WasmWebService::GetInstance(), FindViewportSharedPtr(viewport)));
|
223
|
84 application->Initialize(statusBar_, parameters);
|
|
85
|
228
|
86 viewport->SetSize(width_, height_);
|
223
|
87 printf("StartWasmApplication - completed\n");
|
|
88 }
|
|
89
|
|
90 void EMSCRIPTEN_KEEPALIVE NotifyUpdateContent()
|
|
91 {
|
228
|
92 for (auto viewport : viewports_) {
|
|
93 // TODO Only launch the JavaScript timer if "HasUpdateContent()"
|
|
94 if (viewport->HasUpdateContent())
|
|
95 {
|
|
96 viewport->UpdateContent();
|
|
97 }
|
|
98
|
223
|
99 }
|
|
100
|
|
101 }
|
|
102
|
|
103
|
228
|
104 void EMSCRIPTEN_KEEPALIVE ViewportSetSize(ViewportHandle viewport, unsigned int width, unsigned int height)
|
223
|
105 {
|
|
106 width_ = width;
|
|
107 height_ = height;
|
|
108
|
228
|
109 viewport->SetSize(width, height);
|
223
|
110 }
|
|
111
|
228
|
112 int EMSCRIPTEN_KEEPALIVE ViewportRender(ViewportHandle viewport,
|
227
|
113 unsigned int width,
|
223
|
114 unsigned int height,
|
|
115 uint8_t* data)
|
|
116 {
|
|
117 changeObserver_.Reset();
|
|
118
|
|
119 //printf("ViewportRender called %dx%d\n", width, height);
|
|
120 if (width == 0 ||
|
|
121 height == 0)
|
|
122 {
|
|
123 return 1;
|
|
124 }
|
|
125
|
|
126 Orthanc::ImageAccessor surface;
|
|
127 surface.AssignWritable(Orthanc::PixelFormat_BGRA32, width, height, 4 * width, data);
|
|
128
|
227
|
129 viewport->Render(surface);
|
223
|
130
|
|
131 // Convert from BGRA32 memory layout (only color mode supported by
|
|
132 // Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 (as
|
|
133 // expected by HTML5 canvas). This simply amounts to swapping the
|
|
134 // B and R channels.
|
|
135 uint8_t* p = data;
|
|
136 for (unsigned int y = 0; y < height; y++) {
|
|
137 for (unsigned int x = 0; x < width; x++) {
|
|
138 uint8_t tmp = p[0];
|
|
139 p[0] = p[2];
|
|
140 p[2] = tmp;
|
|
141
|
|
142 p += 4;
|
|
143 }
|
|
144 }
|
|
145
|
|
146 return 1;
|
|
147 }
|
|
148
|
|
149
|
228
|
150 void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(ViewportHandle viewport,
|
|
151 unsigned int rawButton,
|
223
|
152 int x,
|
|
153 int y,
|
|
154 unsigned int rawModifiers)
|
|
155 {
|
|
156 OrthancStone::MouseButton button;
|
|
157 switch (rawButton)
|
|
158 {
|
|
159 case 0:
|
|
160 button = OrthancStone::MouseButton_Left;
|
|
161 break;
|
|
162
|
|
163 case 1:
|
|
164 button = OrthancStone::MouseButton_Middle;
|
|
165 break;
|
|
166
|
|
167 case 2:
|
|
168 button = OrthancStone::MouseButton_Right;
|
|
169 break;
|
|
170
|
|
171 default:
|
|
172 return; // Unknown button
|
|
173 }
|
|
174
|
228
|
175 viewport->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */);
|
223
|
176 }
|
|
177
|
|
178
|
228
|
179 void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(ViewportHandle viewport,
|
|
180 int deltaY,
|
223
|
181 int x,
|
|
182 int y,
|
|
183 int isControl)
|
|
184 {
|
228
|
185 if (deltaY != 0)
|
223
|
186 {
|
|
187 OrthancStone::MouseWheelDirection direction = (deltaY < 0 ?
|
|
188 OrthancStone::MouseWheelDirection_Up :
|
|
189 OrthancStone::MouseWheelDirection_Down);
|
|
190 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None;
|
|
191
|
|
192 if (isControl != 0)
|
|
193 {
|
|
194 modifiers = OrthancStone::KeyboardModifiers_Control;
|
|
195 }
|
|
196
|
228
|
197 viewport->MouseWheel(direction, x, y, modifiers);
|
223
|
198 }
|
|
199 }
|
|
200
|
|
201
|
228
|
202 void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(ViewportHandle viewport,
|
|
203 int x,
|
223
|
204 int y)
|
|
205 {
|
228
|
206 viewport->MouseMove(x, y);
|
223
|
207 }
|
|
208
|
228
|
209 void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(ViewportHandle viewport,
|
|
210 const char* key,
|
223
|
211 bool isShiftPressed,
|
|
212 bool isControlPressed,
|
|
213 bool isAltPressed)
|
|
214
|
|
215 {
|
228
|
216 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None;
|
|
217 if (isShiftPressed) {
|
|
218 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift);
|
223
|
219 }
|
228
|
220 if (isControlPressed) {
|
|
221 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control);
|
|
222 }
|
|
223 if (isAltPressed) {
|
|
224 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt);
|
|
225 }
|
|
226 printf("key pressed : %c\n", key[0]);
|
|
227 viewport->KeyPressed(key[0], modifiers);
|
223
|
228 }
|
|
229
|
|
230
|
228
|
231 void EMSCRIPTEN_KEEPALIVE ViewportMouseUp(ViewportHandle viewport)
|
223
|
232 {
|
228
|
233 viewport->MouseUp();
|
223
|
234 }
|
|
235
|
|
236
|
228
|
237 void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter(ViewportHandle viewport)
|
223
|
238 {
|
228
|
239 viewport->MouseEnter();
|
223
|
240 }
|
|
241
|
|
242
|
228
|
243 void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave(ViewportHandle viewport)
|
223
|
244 {
|
228
|
245 viewport->MouseLeave();
|
223
|
246 }
|
|
247
|
|
248
|
|
249 #ifdef __cplusplus
|
|
250 }
|
|
251 #endif
|