-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglue.cpp
639 lines (550 loc) · 21.6 KB
/
glue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
SANE WebAssembly (sane-wasm)
Copyright (C) 2023 Gonçalo MB <me@goncalomb.com>
GNU GPLv2
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
GNU LGPLv2.1
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <emscripten/proxying.h>
#include <emscripten/eventloop.h>
#include <sane/sane.h>
#include <string.h>
#include <string>
#include <coroutine>
#include "build/version.h"
using namespace emscripten;
#define BUFFER_LEN 2*1024*1024
static ProxyingQueue queue;
static std::thread helper;
SANE_Int version_code = 0;
SANE_Handle handle = NULL;
SANE_Byte buffer[BUFFER_LEN];
void helper_thread_main() {
emscripten_runtime_keepalive_push();
}
template <typename Function>
auto run_on_helper_thread(Function&& fn) {
// https://en.cppreference.com/w/cpp/language/coroutines
struct awaiter : std::suspend_always
{
Function _fn;
std::optional<std::invoke_result_t<Function>> _re;
void await_suspend(std::coroutine_handle<> h)
{
queue.proxyCallback(
helper.native_handle(),
[this] { _re.emplace(_fn()); },
[h] { h.resume(); },
NULL
);
}
std::invoke_result_t<Function> await_resume()
{
return std::move(_re.value());
}
};
return awaiter{{}, fn};
}
val build_response(SANE_Status status, const char *key, const val &value = val::null()) {
val obj = val::object();
obj.set("status", (int) status);
obj.set(key, value);
return obj;
}
val build_response(SANE_Status status) {
val obj = val::object();
obj.set("status", (int) status);
return obj;
}
#define RETURN_IF_ERROR(status) if (status != SANE_STATUS_GOOD) return build_response(status);
#define RETURN_IF_ERROR_KEY(status, key) if (status != SANE_STATUS_GOOD) return build_response(status, key);
#define CORETURN_IF_ERROR(status) if (status != SANE_STATUS_GOOD) co_return build_response(status);
#define CORETURN_IF_ERROR_KEY(status, key) if (status != SANE_STATUS_GOOD) co_return build_response(status, key);
val bitmap_cap_to_val(SANE_Int cap) {
val obj = val::object();
obj.set("SOFT_SELECT", (bool) ((cap & SANE_CAP_SOFT_SELECT) != 0));
obj.set("HARD_SELECT", (bool) ((cap & SANE_CAP_HARD_SELECT) != 0));
obj.set("SOFT_DETECT", (bool) ((cap & SANE_CAP_SOFT_DETECT) != 0));
obj.set("EMULATED", (bool) ((cap & SANE_CAP_EMULATED) != 0));
obj.set("AUTOMATIC", (bool) ((cap & SANE_CAP_AUTOMATIC) != 0));
obj.set("INACTIVE", (bool) ((cap & SANE_CAP_INACTIVE) != 0));
obj.set("ADVANCED", (bool) ((cap & SANE_CAP_ADVANCED) != 0));
return obj;
}
val bitmap_info_to_val(SANE_Int info) {
val obj = val::object();
obj.set("INEXACT", (bool) ((info & SANE_INFO_INEXACT) != 0));
obj.set("RELOAD_OPTIONS", (bool) ((info & SANE_INFO_RELOAD_OPTIONS) != 0));
obj.set("RELOAD_PARAMS", (bool) ((info & SANE_INFO_RELOAD_PARAMS) != 0));
return obj;
}
// SANE API
namespace sane {
// SANE_Status
std::map<const char *, int> SANE_STATUS = {
{"GOOD", SANE_STATUS_GOOD},
{"UNSUPPORTED", SANE_STATUS_UNSUPPORTED},
{"CANCELLED", SANE_STATUS_CANCELLED},
{"DEVICE_BUSY", SANE_STATUS_DEVICE_BUSY},
{"INVAL", SANE_STATUS_INVAL},
{"EOF", SANE_STATUS_EOF},
{"JAMMED", SANE_STATUS_JAMMED},
{"NO_DOCS", SANE_STATUS_NO_DOCS},
{"COVER_OPEN", SANE_STATUS_COVER_OPEN},
{"IO_ERROR", SANE_STATUS_IO_ERROR},
{"NO_MEM", SANE_STATUS_NO_MEM},
{"ACCESS_DENIED", SANE_STATUS_ACCESS_DENIED},
};
// SANE_Value_Type
std::map<const char *, int> SANE_TYPE = {
{"BOOL", SANE_TYPE_BOOL},
{"INT", SANE_TYPE_INT},
{"FIXED", SANE_TYPE_FIXED},
{"STRING", SANE_TYPE_STRING},
{"BUTTON", SANE_TYPE_BUTTON},
{"GROUP", SANE_TYPE_GROUP},
};
// SANE_Unit
std::map<const char *, int> SANE_UNIT = {
{"NONE", SANE_UNIT_NONE},
{"PIXEL", SANE_UNIT_PIXEL},
{"BIT", SANE_UNIT_BIT},
{"MM", SANE_UNIT_MM},
{"DPI", SANE_UNIT_DPI},
{"PERCENT", SANE_UNIT_PERCENT},
{"MICROSECOND", SANE_UNIT_MICROSECOND},
};
// SANE_Constraint_Type
std::map<const char *, int> SANE_CONSTRAINT = {
{"NONE", SANE_CONSTRAINT_NONE},
{"RANGE", SANE_CONSTRAINT_RANGE},
{"WORD_LIST", SANE_CONSTRAINT_WORD_LIST},
{"STRING_LIST", SANE_CONSTRAINT_STRING_LIST},
};
// SANE_Action not used on the exposed api
// SANE_Frame
std::map<const char *, int> SANE_FRAME = {
{"GRAY", SANE_FRAME_GRAY},
{"RGB", SANE_FRAME_RGB},
{"RED", SANE_FRAME_RED},
{"GREEN", SANE_FRAME_GREEN},
{"BLUE", SANE_FRAME_BLUE},
};
val sane_get_state() {
val version = val::object();
version.set("major", SANE_VERSION_MAJOR(version_code));
version.set("minor", SANE_VERSION_MINOR(version_code));
version.set("build", SANE_VERSION_BUILD(version_code));
val state = val::object();
state.set("initialized", !!version_code);
state.set("version_code", version_code);
state.set("version", version);
state.set("open", !!handle);
return state;
}
val sane_init() {
if (version_code) {
return build_response(SANE_STATUS_INVAL, "version_code");
}
SANE_Status status = ::sane_init(&version_code, NULL);
RETURN_IF_ERROR_KEY(status, "version_code");
return build_response(status, "version_code", val(version_code));
}
val sane_exit() {
if (!version_code) {
return build_response(SANE_STATUS_INVAL);
}
::sane_exit();
version_code = 0;
handle = NULL;
return build_response(SANE_STATUS_GOOD);
}
val sane_get_devices() {
const SANE_Device **device_list;
SANE_Status status = ::sane_get_devices(&device_list, SANE_TRUE);
RETURN_IF_ERROR_KEY(status, "devices");
val devices = val::array();
for (int i = 0; device_list[i]; i++) {
val device = val::object();
device.set("name", device_list[i]->name);
device.set("vendor", device_list[i]->vendor);
device.set("model", device_list[i]->model);
device.set("type", device_list[i]->type);
devices.call<void>("push", device);
}
return build_response(status, "devices", devices);
}
val sane_open(std::string devicename) {
if (!version_code || handle) {
return build_response(SANE_STATUS_INVAL);
}
SANE_Status status = ::sane_open(devicename.c_str(), &handle);
return build_response(status);
}
val sane_close() {
if (!handle) {
return build_response(SANE_STATUS_INVAL);
}
::sane_close(handle);
handle = NULL;
return build_response(SANE_STATUS_GOOD);
}
val sane_get_option_descriptor(int option) {
if (!handle) {
return build_response(SANE_STATUS_INVAL, "option_descriptor");
}
const SANE_Option_Descriptor *desc = ::sane_get_option_descriptor(handle, option);
if (desc == NULL) {
return build_response(SANE_STATUS_GOOD, "option_descriptor");
}
// convert size, we don't need to expose size in byte units
// for the frontend is more useful in "number of words" (items)
int size = desc->size;
switch (desc->type) {
case SANE_TYPE_BOOL: size = size/sizeof(SANE_Bool); break;
case SANE_TYPE_INT: size = size/sizeof(SANE_Int); break;
case SANE_TYPE_FIXED: size = size/sizeof(SANE_Fixed); break;
case SANE_TYPE_STRING: size = size/sizeof(SANE_Char) - 1; break;
case SANE_TYPE_BUTTON: break;
case SANE_TYPE_GROUP: break;
}
// decode and convert constraint
val constraint = val::null();
if (desc->constraint_type == SANE_CONSTRAINT_RANGE) {
constraint = val::object();
if (desc->type == SANE_TYPE_FIXED) {
constraint.set("min", SANE_UNFIX(desc->constraint.range->min));
constraint.set("max", SANE_UNFIX(desc->constraint.range->max));
constraint.set("quant", SANE_UNFIX(desc->constraint.range->quant));
} else {
constraint.set("min", desc->constraint.range->min);
constraint.set("max", desc->constraint.range->max);
constraint.set("quant", desc->constraint.range->quant);
}
} else if (desc->constraint_type == SANE_CONSTRAINT_WORD_LIST) {
constraint = val::array();
if (desc->type == SANE_TYPE_FIXED) {
for (int i = 1; i <= desc->constraint.word_list[0]; i++) {
constraint.call<void>("push", SANE_UNFIX(desc->constraint.word_list[i]));
}
} else {
for (int i = 1; i <= desc->constraint.word_list[0]; i++) {
constraint.call<void>("push", desc->constraint.word_list[i]);
}
}
} else if (desc->constraint_type == SANE_CONSTRAINT_STRING_LIST) {
constraint = val::array();
for (const SANE_String_Const *str = desc->constraint.string_list; *str != NULL; str++) {
constraint.call<void>("push", val(*str));
}
}
// construct final object
val option_descriptor = val::object();
option_descriptor.set("name", desc->name);
option_descriptor.set("title", desc->title);
option_descriptor.set("desc", desc->desc);
option_descriptor.set("type", (int) desc->type);
option_descriptor.set("unit", (int) desc->unit);
option_descriptor.set("size", size);
option_descriptor.set("cap", bitmap_cap_to_val(desc->cap));
option_descriptor.set("constraint_type", (int) desc->constraint_type);
option_descriptor.set("constraint", constraint);
return build_response(SANE_STATUS_GOOD, "option_descriptor", option_descriptor);
}
val sane_control_option_get_value(int option) {
if (!handle) {
return build_response(SANE_STATUS_INVAL, "value");
}
const SANE_Option_Descriptor *desc = ::sane_get_option_descriptor(handle, option);
if (desc == NULL) {
return build_response(SANE_STATUS_INVAL, "value");
}
void *v = NULL;
if (desc->size > 0) {
v = malloc(desc->size);
if (!v) {
return build_response(SANE_STATUS_NO_MEM, "value");
}
}
SANE_Int info = 0; // discard
SANE_Status status = ::sane_control_option(handle, option, SANE_ACTION_GET_VALUE, v, &info);
// RETURN_IF_ERROR_KEY(status, "value");
if (status != SANE_STATUS_GOOD) {
if (v) {
free(v);
}
return build_response(status, "value");
}
val value = val::null();
int n;
switch (desc->type) {
case SANE_TYPE_BOOL:
value = val((bool) *((SANE_Bool *) v));
break;
case SANE_TYPE_INT:
n = desc->size/sizeof(SANE_Int);
if (n == 1) {
value = val(*((SANE_Int *) v));
} else if (n > 1) {
value = val::array();
for (int i = 0; i < n; i++) {
value.call<void>("push", ((SANE_Int *) v)[i]);
}
}
break;
case SANE_TYPE_FIXED:
n = desc->size/sizeof(SANE_Fixed);
if (n == 1) {
value = val(SANE_UNFIX(*((SANE_Fixed *) v)));
} else if (n > 1) {
value = val::array();
for (int i = 0; i < n; i++) {
value.call<void>("push", SANE_UNFIX(((SANE_Fixed *) v)[i]));
}
}
break;
case SANE_TYPE_STRING:
value = val((SANE_String_Const) v);
break;
case SANE_TYPE_BUTTON:
case SANE_TYPE_GROUP:
default:
break;
}
if (v) {
free(v);
}
return build_response(status, "value", value);
}
val sane_control_option_set_value(int option, val value) {
if (!handle) {
return build_response(SANE_STATUS_INVAL, "info");
}
const SANE_Option_Descriptor *desc = ::sane_get_option_descriptor(handle, option);
if (desc == NULL) {
return build_response(SANE_STATUS_INVAL, "info");
}
void *v = NULL;
if (desc->size > 0) {
v = malloc(desc->size);
if (!v) {
return build_response(SANE_STATUS_NO_MEM, "info");
}
}
int n;
bool inval = false;
switch (desc->type) {
case SANE_TYPE_BOOL:
if (value.isTrue()) {
*((SANE_Bool *) v) = SANE_TRUE;
} else if (value.isFalse()) {
*((SANE_Bool *) v) = SANE_FALSE;
} else {
inval = true;
}
break;
case SANE_TYPE_INT:
n = desc->size/sizeof(SANE_Int);
if (n == 1) {
if (value.isNumber()) {
*((SANE_Int *) v) = value.as<int>();
} else {
inval = true;
}
} else if (n > 1) {
if (value.isArray()) {
auto vec = vecFromJSArray<int>(value);
for (int i = 0; i < vec.size() && i < n; i++) {
((SANE_Int *) v)[i] = vec[i];
}
} else {
inval = true;
}
}
break;
case SANE_TYPE_FIXED:
n = desc->size/sizeof(SANE_Fixed);
if (n == 1) {
if (value.isNumber()) {
*((SANE_Fixed *) v) = SANE_FIX(value.as<double>());
} else {
inval = true;
}
} else if (n > 1) {
if (value.isArray()) {
auto vec = vecFromJSArray<double>(value);
for (int i = 0; i < vec.size() && i < n; i++) {
((SANE_Fixed *) v)[i] = SANE_FIX(vec[i]);
}
} else {
inval = true;
}
}
break;
case SANE_TYPE_STRING:
if (value.isString()) {
int n = desc->size/sizeof(SANE_Char) - 1;
std::string str = value.as<std::string>();
strncpy((char *) v, str.c_str(), n);
((char *) v)[n] = 0x00;
} else {
inval = true;
}
break;
case SANE_TYPE_BUTTON:
case SANE_TYPE_GROUP:
default:
break;
}
if (inval) {
if (v) {
free(v);
}
return build_response(SANE_STATUS_INVAL, "info");
}
SANE_Int info = 0;
SANE_Status status = ::sane_control_option(handle, option, SANE_ACTION_SET_VALUE, v, &info);
if (v) {
free(v);
}
RETURN_IF_ERROR_KEY(status, "info");
return build_response(status, "info", bitmap_info_to_val(info));
}
val sane_control_option_set_auto(int option) {
SANE_Int info = 0;
SANE_Status status = ::sane_control_option(handle, option, SANE_ACTION_SET_AUTO, NULL, &info);
RETURN_IF_ERROR_KEY(status, "info");
return build_response(status, "info", bitmap_info_to_val(info));
}
val sane_get_parameters() {
if (!handle) {
return build_response(SANE_STATUS_INVAL, "parameters");
}
SANE_Parameters params;
SANE_Status status = ::sane_get_parameters(handle, ¶ms);
RETURN_IF_ERROR_KEY(status, "parameters");
val parameters = val::object();
parameters.set("format", (int) params.format);
parameters.set("last_frame", (bool) params.last_frame);
parameters.set("bytes_per_line", params.bytes_per_line);
parameters.set("pixels_per_line", params.pixels_per_line);
parameters.set("lines", params.lines);
parameters.set("depth", params.depth);
return build_response(status, "parameters", parameters);
}
val sane_start() {
if (!handle) {
return build_response(SANE_STATUS_INVAL);
}
SANE_Status status = ::sane_start(handle);
return build_response(status);
}
val sane_read() {
if (!handle) {
co_return build_response(SANE_STATUS_INVAL, "data");
}
SANE_Int len = 0;
SANE_Status status = co_await run_on_helper_thread([&len] {
return ::sane_read(handle, buffer, BUFFER_LEN, &len);
});
CORETURN_IF_ERROR_KEY(status, "data");
val data = val(typed_memory_view(len, buffer));
co_return build_response(status, "data", data);
}
val sane_cancel() {
if (!handle) {
co_return build_response(SANE_STATUS_INVAL);
}
co_await run_on_helper_thread([&] {
::sane_cancel(handle);
return NULL;
});
co_return build_response(SANE_STATUS_GOOD);
}
val sane_strstatus(int status) {
return val(::sane_strstatus((SANE_Status) status));
}
}
/*
Weird way to export enums as simple JS objects...
I tried using constant bindings to expose the enums, but could not make it
work. I don't think it's possible to create the val::object during static
initialization (int binding error?).
Using main() to map the enums at runtime, kind of weird?
XXX: help me
val enum_SANE_STATUS_to_val() {
val obj = val::object();
obj.set("GOOD", (int) SANE_STATUS_GOOD);
// ...
return obj;
}
EMSCRIPTEN_BINDINGS(sane_bindings) {
constant("SANE_STATUS", enum_SANE_STATUS_to_val());
}
*/
EM_JS(void, module_set, (const char* k, EM_VAL v), {
Module[UTF8ToString(k)] = Emval.toValue(v);
});
val map_to_val_object(const std::map<const char *, int> &map) {
val obj = val::object();
for (const auto &kv : map) {
obj.set(kv.first, kv.second);
}
return obj;
}
int main() {
module_set("SANE_STATUS", map_to_val_object(sane::SANE_STATUS).as_handle());
module_set("SANE_TYPE", map_to_val_object(sane::SANE_TYPE).as_handle());
module_set("SANE_UNIT", map_to_val_object(sane::SANE_UNIT).as_handle());
module_set("SANE_CONSTRAINT", map_to_val_object(sane::SANE_CONSTRAINT).as_handle());
module_set("SANE_FRAME", map_to_val_object(sane::SANE_FRAME).as_handle());
helper = std::thread(helper_thread_main);
return 0;
}
EMSCRIPTEN_BINDINGS(sane_bindings) {
constant("SANE_WASM_COMMIT", val(SANE_WASM_COMMIT));
constant("SANE_WASM_VERSION", val(SANE_WASM_VERSION));
constant("SANE_WASM_BACKENDS", val(SANE_WASM_BACKENDS));
constant("SANE_CURRENT_MAJOR", SANE_CURRENT_MAJOR);
constant("SANE_CURRENT_MINOR", SANE_CURRENT_MINOR);
function("sane_get_state", &sane::sane_get_state);
function("sane_init", &sane::sane_init);
function("sane_exit", &sane::sane_exit);
function("sane_get_devices", &sane::sane_get_devices);
function("sane_open", &sane::sane_open);
function("sane_close", &sane::sane_close);
function("sane_get_option_descriptor", &sane::sane_get_option_descriptor);
function("sane_control_option_get_value", &sane::sane_control_option_get_value);
function("sane_control_option_set_value", &sane::sane_control_option_set_value);
function("sane_control_option_set_auto", &sane::sane_control_option_set_auto);
function("sane_get_parameters", &sane::sane_get_parameters);
function("sane_start", &sane::sane_start);
function("sane_read", &sane::sane_read);
function("sane_cancel", &sane::sane_cancel);
function("sane_strstatus", &sane::sane_strstatus);
}