Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/Defaults.cpp @ 961:92e32e263ae9 toa2019082601
Removed log in IsContextLost (too many calls) + removed glGetError guards on calls (expensive) + added shared_ptr ref count dump in LoaderCache (still needs to figure that one out)
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 26 Aug 2019 11:01:53 +0200 |
parents | 8f7930f589ef |
children | 92a043b8e431 |
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" |
732
c35e98d22764
move Deprecated classes to a separate folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
726
diff
changeset
|
5 #include "../../Framework/Deprecated/Widgets/TestCairoWidget.h" |
c35e98d22764
move Deprecated classes to a separate folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
726
diff
changeset
|
6 #include <Framework/Deprecated/Viewport/WidgetViewport.h> |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
7 #include <Applications/Wasm/StartupParametersBuilder.h> |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
8 #include <Platforms/Wasm/WasmPlatformApplicationAdapter.h> |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
9 #include <Core/Logging.h> |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
10 |
228 | 11 #include <algorithm> |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
12 |
223 | 13 |
14 static unsigned int width_ = 0; | |
15 static unsigned int height_ = 0; | |
16 | |
17 /**********************************/ | |
18 | |
288 | 19 static std::unique_ptr<OrthancStone::IStoneApplication> application; |
307 | 20 static std::unique_ptr<OrthancStone::WasmPlatformApplicationAdapter> applicationWasmAdapter = NULL; |
288 | 21 static std::unique_ptr<OrthancStone::StoneApplicationContext> context; |
242 | 22 static OrthancStone::StartupParametersBuilder startupParametersBuilder; |
253 | 23 static OrthancStone::MessageBroker broker; |
242 | 24 |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
25 static OrthancStone::ViewportContentChangedObserver viewportContentChangedObserver_(broker); |
223 | 26 static OrthancStone::StatusBar statusBar_; |
27 | |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
28 static std::list<std::shared_ptr<Deprecated::WidgetViewport>> viewports_; |
227 | 29 |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
30 std::shared_ptr<Deprecated::WidgetViewport> FindViewportSharedPtr(ViewportHandle viewport) { |
228 | 31 for (const auto& v : viewports_) { |
32 if (v.get() == viewport) { | |
33 return v; | |
34 } | |
35 } | |
229 | 36 assert(false); |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
37 return std::shared_ptr<Deprecated::WidgetViewport>(); |
228 | 38 } |
39 | |
223 | 40 #ifdef __cplusplus |
41 extern "C" { | |
42 #endif | |
43 | |
877
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
44 #if 0 |
878 | 45 // rewrite malloc/free in order to monitor allocations. We actually only monitor large allocations (like images ...) |
46 | |
877
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
47 size_t bigChunksTotalSize = 0; |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
48 std::map<void*, size_t> allocatedBigChunks; |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
49 |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
50 extern void* emscripten_builtin_malloc(size_t bytes); |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
51 extern void emscripten_builtin_free(void* mem); |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
52 |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
53 void * __attribute__((noinline)) malloc(size_t size) |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
54 { |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
55 void *ptr = emscripten_builtin_malloc(size); |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
56 if (size > 100000) |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
57 { |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
58 bigChunksTotalSize += size; |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
59 printf("++ Allocated %zu bytes, got %p. (%zu MB consumed by big chunks)\n", size, ptr, bigChunksTotalSize/(1024*1024)); |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
60 allocatedBigChunks[ptr] = size; |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
61 } |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
62 return ptr; |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
63 } |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
64 |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
65 void __attribute__((noinline)) free(void *ptr) |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
66 { |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
67 emscripten_builtin_free(ptr); |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
68 |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
69 std::map<void*, size_t>::iterator it = allocatedBigChunks.find(ptr); |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
70 if (it != allocatedBigChunks.end()) |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
71 { |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
72 bigChunksTotalSize -= it->second; |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
73 printf("-- Freed %zu bytes at %p. (%zu MB consumed by big chunks)\n", it->second, ptr, bigChunksTotalSize/(1024*1024)); |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
74 allocatedBigChunks.erase(it); |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
75 } |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
76 } |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
77 #endif // 0 |
f7e5ee59ba17
debug code to monitor large malloc/free (currently disabled)
Alain Mazy <alain@mazy.be>
parents:
860
diff
changeset
|
78 |
223 | 79 using namespace OrthancStone; |
227 | 80 |
81 // when WASM needs a C++ viewport | |
82 ViewportHandle EMSCRIPTEN_KEEPALIVE CreateCppViewport() { | |
83 | |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
84 std::shared_ptr<Deprecated::WidgetViewport> viewport(new Deprecated::WidgetViewport(broker)); |
287 | 85 printf("viewport %x\n", (int)viewport.get()); |
227 | 86 |
87 viewports_.push_back(viewport); | |
88 | |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
89 printf("There are now %lu viewports in C++\n", viewports_.size()); |
227 | 90 |
91 viewport->SetStatusBar(statusBar_); | |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
92 |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
93 viewport->RegisterObserverCallback( |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
94 new Callable<ViewportContentChangedObserver, Deprecated::IViewport::ViewportChangedMessage> |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
327
diff
changeset
|
95 (viewportContentChangedObserver_, &ViewportContentChangedObserver::OnViewportChanged)); |
227 | 96 |
97 return viewport.get(); | |
98 } | |
99 | |
100 // when WASM does not need a viewport anymore, it should release it | |
101 void EMSCRIPTEN_KEEPALIVE ReleaseCppViewport(ViewportHandle viewport) { | |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
102 viewports_.remove_if([viewport](const std::shared_ptr<Deprecated::WidgetViewport>& v) { return v.get() == viewport;}); |
227 | 103 |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
104 printf("There are now %lu viewports in C++\n", viewports_.size()); |
227 | 105 } |
106 | |
107 void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport) { | |
223 | 108 |
109 printf("CreateWasmApplication\n"); | |
110 | |
253 | 111 application.reset(CreateUserApplication(broker)); |
307 | 112 applicationWasmAdapter.reset(CreateWasmApplicationAdapter(broker, application.get())); |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
113 Deprecated::WasmWebService::SetBroker(broker); |
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
114 Deprecated::WasmDelayedCallExecutor::SetBroker(broker); |
223 | 115 |
242 | 116 startupParametersBuilder.Clear(); |
223 | 117 } |
118 | |
119 void EMSCRIPTEN_KEEPALIVE SetStartupParameter(const char* keyc, | |
120 const char* value) { | |
242 | 121 startupParametersBuilder.SetStartupParameter(keyc, value); |
223 | 122 } |
123 | |
418 | 124 void EMSCRIPTEN_KEEPALIVE StartWasmApplication(const char* baseUri) { |
223 | 125 |
126 printf("StartWasmApplication\n"); | |
127 | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
128 Orthanc::Logging::SetErrorWarnInfoTraceLoggingFunctions( |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
129 stone_console_error, stone_console_warning, |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
130 stone_console_info, stone_console_trace); |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
131 |
223 | 132 // recreate a command line from uri arguments and parse it |
133 boost::program_options::variables_map parameters; | |
242 | 134 boost::program_options::options_description options; |
135 application->DeclareStartupOptions(options); | |
136 startupParametersBuilder.GetStartupParameters(parameters, options); | |
223 | 137 |
418 | 138 context.reset(new OrthancStone::StoneApplicationContext(broker)); |
139 context->SetOrthancBaseUrl(baseUri); | |
140 printf("Base URL to Orthanc API: [%s]\n", baseUri); | |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
141 context->SetWebService(Deprecated::WasmWebService::GetInstance()); |
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
142 context->SetDelayedCallExecutor(Deprecated::WasmDelayedCallExecutor::GetInstance()); |
242 | 143 application->Initialize(context.get(), statusBar_, parameters); |
144 application->InitializeWasm(); | |
223 | 145 |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
229
diff
changeset
|
146 // viewport->SetSize(width_, height_); |
223 | 147 printf("StartWasmApplication - completed\n"); |
148 } | |
149 | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
150 bool EMSCRIPTEN_KEEPALIVE WasmIsTraceLevelEnabled() |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
151 { |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
152 return Orthanc::Logging::IsTraceLevelEnabled(); |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
153 } |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
154 |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
155 bool EMSCRIPTEN_KEEPALIVE WasmIsInfoLevelEnabled() |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
156 { |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
157 return Orthanc::Logging::IsInfoLevelEnabled(); |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
158 } |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
535
diff
changeset
|
159 |
386
e33659decec5
renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
385
diff
changeset
|
160 void EMSCRIPTEN_KEEPALIVE WasmDoAnimation() |
223 | 161 { |
228 | 162 for (auto viewport : viewports_) { |
386
e33659decec5
renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
385
diff
changeset
|
163 // TODO Only launch the JavaScript timer if "HasAnimation()" |
e33659decec5
renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
385
diff
changeset
|
164 if (viewport->HasAnimation()) |
228 | 165 { |
386
e33659decec5
renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
385
diff
changeset
|
166 viewport->DoAnimation(); |
228 | 167 } |
168 | |
223 | 169 } |
170 | |
171 } | |
172 | |
173 | |
228 | 174 void EMSCRIPTEN_KEEPALIVE ViewportSetSize(ViewportHandle viewport, unsigned int width, unsigned int height) |
223 | 175 { |
176 width_ = width; | |
177 height_ = height; | |
178 | |
228 | 179 viewport->SetSize(width, height); |
223 | 180 } |
181 | |
228 | 182 int EMSCRIPTEN_KEEPALIVE ViewportRender(ViewportHandle viewport, |
227 | 183 unsigned int width, |
223 | 184 unsigned int height, |
185 uint8_t* data) | |
186 { | |
278 | 187 viewportContentChangedObserver_.Reset(); |
223 | 188 |
189 //printf("ViewportRender called %dx%d\n", width, height); | |
190 if (width == 0 || | |
191 height == 0) | |
192 { | |
193 return 1; | |
194 } | |
195 | |
196 Orthanc::ImageAccessor surface; | |
197 surface.AssignWritable(Orthanc::PixelFormat_BGRA32, width, height, 4 * width, data); | |
198 | |
227 | 199 viewport->Render(surface); |
223 | 200 |
201 // Convert from BGRA32 memory layout (only color mode supported by | |
202 // Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 (as | |
203 // expected by HTML5 canvas). This simply amounts to swapping the | |
204 // B and R channels. | |
205 uint8_t* p = data; | |
206 for (unsigned int y = 0; y < height; y++) { | |
207 for (unsigned int x = 0; x < width; x++) { | |
208 uint8_t tmp = p[0]; | |
209 p[0] = p[2]; | |
210 p[2] = tmp; | |
211 | |
212 p += 4; | |
213 } | |
214 } | |
215 | |
216 return 1; | |
217 } | |
218 | |
219 | |
228 | 220 void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(ViewportHandle viewport, |
221 unsigned int rawButton, | |
223 | 222 int x, |
223 int y, | |
224 unsigned int rawModifiers) | |
225 { | |
226 OrthancStone::MouseButton button; | |
227 switch (rawButton) | |
228 { | |
229 case 0: | |
230 button = OrthancStone::MouseButton_Left; | |
231 break; | |
232 | |
233 case 1: | |
234 button = OrthancStone::MouseButton_Middle; | |
235 break; | |
236 | |
237 case 2: | |
238 button = OrthancStone::MouseButton_Right; | |
239 break; | |
240 | |
241 default: | |
242 return; // Unknown button | |
243 } | |
244 | |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
245 viewport->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None, std::vector<Deprecated::Touch>()); |
223 | 246 } |
247 | |
248 | |
228 | 249 void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(ViewportHandle viewport, |
250 int deltaY, | |
223 | 251 int x, |
252 int y, | |
253 int isControl) | |
254 { | |
228 | 255 if (deltaY != 0) |
223 | 256 { |
257 OrthancStone::MouseWheelDirection direction = (deltaY < 0 ? | |
258 OrthancStone::MouseWheelDirection_Up : | |
259 OrthancStone::MouseWheelDirection_Down); | |
260 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; | |
261 | |
262 if (isControl != 0) | |
263 { | |
264 modifiers = OrthancStone::KeyboardModifiers_Control; | |
265 } | |
266 | |
228 | 267 viewport->MouseWheel(direction, x, y, modifiers); |
223 | 268 } |
269 } | |
270 | |
271 | |
228 | 272 void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(ViewportHandle viewport, |
273 int x, | |
223 | 274 int y) |
275 { | |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
276 viewport->MouseMove(x, y, std::vector<Deprecated::Touch>()); |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
277 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
278 |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
279 void GetTouchVector(std::vector<Deprecated::Touch>& output, |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
280 int touchCount, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
281 float x0, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
282 float y0, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
283 float x1, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
284 float y1, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
285 float x2, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
286 float y2) |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
287 { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
288 // TODO: it might be nice to try to pass all the x0,y0 coordinates as arrays but that's not so easy to pass array between JS and C++ |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
289 if (touchCount > 0) |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
290 { |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
291 output.push_back(Deprecated::Touch(x0, y0)); |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
292 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
293 if (touchCount > 1) |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
294 { |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
295 output.push_back(Deprecated::Touch(x1, y1)); |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
296 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
297 if (touchCount > 2) |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
298 { |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
299 output.push_back(Deprecated::Touch(x2, y2)); |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
300 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
301 |
223 | 302 } |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
303 |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
304 void EMSCRIPTEN_KEEPALIVE ViewportTouchStart(ViewportHandle viewport, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
305 int touchCount, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
306 float x0, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
307 float y0, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
308 float x1, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
309 float y1, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
310 float x2, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
311 float y2) |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
312 { |
838 | 313 // printf("touch start with %d touches\n", touchCount); |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
314 |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
315 std::vector<Deprecated::Touch> touches; |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
316 GetTouchVector(touches, touchCount, x0, y0, x1, y1, x2, y2); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
317 viewport->TouchStart(touches); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
318 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
319 |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
320 void EMSCRIPTEN_KEEPALIVE ViewportTouchMove(ViewportHandle viewport, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
321 int touchCount, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
322 float x0, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
323 float y0, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
324 float x1, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
325 float y1, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
326 float x2, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
327 float y2) |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
328 { |
838 | 329 // printf("touch move with %d touches\n", touchCount); |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
330 |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
331 std::vector<Deprecated::Touch> touches; |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
332 GetTouchVector(touches, touchCount, x0, y0, x1, y1, x2, y2); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
333 viewport->TouchMove(touches); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
334 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
335 |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
336 void EMSCRIPTEN_KEEPALIVE ViewportTouchEnd(ViewportHandle viewport, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
337 int touchCount, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
338 float x0, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
339 float y0, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
340 float x1, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
341 float y1, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
342 float x2, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
343 float y2) |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
344 { |
838 | 345 // printf("touch end with %d touches remaining\n", touchCount); |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
346 |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
603
diff
changeset
|
347 std::vector<Deprecated::Touch> touches; |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
348 GetTouchVector(touches, touchCount, x0, y0, x1, y1, x2, y2); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
349 viewport->TouchEnd(touches); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
350 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
431
diff
changeset
|
351 |
228 | 352 void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(ViewportHandle viewport, |
327 | 353 int key, |
354 const char* keyChar, | |
223 | 355 bool isShiftPressed, |
356 bool isControlPressed, | |
357 bool isAltPressed) | |
358 | |
359 { | |
228 | 360 OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; |
361 if (isShiftPressed) { | |
362 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift); | |
223 | 363 } |
228 | 364 if (isControlPressed) { |
365 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control); | |
366 } | |
367 if (isAltPressed) { | |
368 modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt); | |
369 } | |
327 | 370 |
371 char c = 0; | |
372 if (keyChar != NULL && key == OrthancStone::KeyboardKeys_Generic) { | |
373 c = keyChar[0]; | |
374 } | |
375 viewport->KeyPressed(static_cast<OrthancStone::KeyboardKeys>(key), c, modifiers); | |
223 | 376 } |
377 | |
378 | |
228 | 379 void EMSCRIPTEN_KEEPALIVE ViewportMouseUp(ViewportHandle viewport) |
223 | 380 { |
228 | 381 viewport->MouseUp(); |
223 | 382 } |
383 | |
384 | |
228 | 385 void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter(ViewportHandle viewport) |
223 | 386 { |
228 | 387 viewport->MouseEnter(); |
223 | 388 } |
389 | |
390 | |
228 | 391 void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave(ViewportHandle viewport) |
223 | 392 { |
228 | 393 viewport->MouseLeave(); |
223 | 394 } |
395 | |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
458
diff
changeset
|
396 const char* EMSCRIPTEN_KEEPALIVE SendSerializedMessageToStoneApplication(const char* message) |
287 | 397 { |
398 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) | |
399 | |
838 | 400 //printf("SendSerializedMessageToStoneApplication\n"); |
401 //printf("%s", message); | |
307 | 402 |
403 if (applicationWasmAdapter.get() != NULL) { | |
508
7105a0bad250
- Added HandleSerializedMessage to IStoneApplication (empty impl)
Benjamin Golinvaux <bgo@osimis.io>
parents:
458
diff
changeset
|
404 applicationWasmAdapter->HandleSerializedMessageFromWeb(output, std::string(message)); |
287 | 405 return output.c_str(); |
406 } | |
838 | 407 printf("This Stone application does not have a Web Adapter, unable to send messages"); |
307 | 408 return NULL; |
287 | 409 } |
410 | |
223 | 411 #ifdef __cplusplus |
412 } | |
413 #endif |