Skip to content

Commit 89d5470

Browse files
committed
WIP: WebHID support through Emscripten
Unfinished.
1 parent 8c5f411 commit 89d5470

6 files changed

Lines changed: 399 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@ cmake_minimum_required(VERSION 3.1...3.19)
2020
project(
2121
libspnavdev
2222
VERSION 0.1
23-
LANGUAGES C)
23+
)
2424

2525
if(WIN32)
2626
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
2727
endif()
2828

29+
if(EMSCRIPTEN)
30+
set (CMAKE_CXX_STANDARD 11)
31+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
32+
SET_SOURCE_FILES_PROPERTIES(
33+
src/serdev.c
34+
src/spnavdev.c
35+
src/usbdev.c
36+
PROPERTIES LANGUAGE CXX )
37+
endif()
38+
2939
add_library(spnavdev
3040
src/serdev.c
3141
src/spnavdev.c
@@ -42,12 +52,15 @@ else()
4252
)
4353
endif()
4454

45-
if(WIN32 OR APPLE OR EMSCRIPTEN)
55+
if(WIN32 OR APPLE)
4656
set(HAVE_HIDAPI TRUE)
4757
set(BUILD_SHARED_LIBS FALSE)
4858
add_subdirectory(extlib/hidapi)
4959
target_link_libraries(spnavdev PRIVATE hidapi::hidapi)
5060
target_include_directories(spnavdev PRIVATE ${CMAKE_BINARY_DIR}/extlib/hidapi/hidapi)
61+
elseif(EMSCRIPTEN)
62+
set(HAVE_WEBHIDAPI TRUE)
63+
set(BUILD_SHARED_LIBS FALSE)
5164
else()
5265
find_package(hidapi)
5366
if(hidapi_FOUND)
@@ -66,6 +79,8 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
6679
# We are our own project, not a subproject
6780
if(WIN32)
6881
add_subdirectory(examples/win32)
82+
elseif(EMSCRIPTEN)
83+
add_subdirectory(examples/web)
6984
else()
7085
add_subdirectory(examples/unix)
7186
endif()

config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
#ifndef SPNAVDEV_CONFIG_H_
1919
#define SPNAVDEV_CONFIG_H_
2020
#cmakedefine HAVE_HIDAPI
21+
#cmakedefine HAVE_WEBHIDAPI
2122
#endif

examples/web/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# libspnavdev - direct 6dof device handling library
2+
# Copyright (C) 2021 Collabora, Ltd.
3+
#
4+
# SPDX-License-Identifier: GPL-3-or-later
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+
add_executable(test test.c)
20+
target_link_libraries(test PRIVATE spnavdev)
21+
22+
if(EMSCRIPTEN)
23+
set(M_LIBRARY "" CACHE STRING "libm (not necessary)" FORCE)
24+
25+
# set(CMAKE_EXECUTABLE_SUFFIX ".html")
26+
27+
set(SHELL ${CMAKE_CURRENT_SOURCE_DIR}/test.html)
28+
29+
set_target_properties(test PROPERTIES LINK_FLAGS
30+
"--bind
31+
--no-heap-copy -s ALLOW_MEMORY_GROWTH=1 -s WASM=1 -s ASYNCIFY=1
32+
-s DYNCALLS=1 -s ASSERTIONS=1
33+
-s EXPORTED_RUNTIME_METHODS=ccall,cwrap
34+
-s EXPORTED_FUNCTIONS=_test,_main
35+
-s TOTAL_STACK=33554432 -s TOTAL_MEMORY=134217728")
36+
endif()

examples/web/test.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <signal.h>
5+
#include <errno.h>
6+
#include "spnavdev.h"
7+
8+
static struct spndev *dev;
9+
static int quit = 0;
10+
11+
extern int test()
12+
{
13+
int fd;
14+
union spndev_event ev;
15+
const char *s;
16+
int led=0;
17+
18+
if(!(dev = spndev_open(0))) {
19+
fprintf(stderr, "Failed to open 6dof device\n");
20+
return 1;
21+
}
22+
fd = spndev_fd(dev);
23+
24+
printf("Monitoring device, ctrl-c to quit\n");
25+
26+
return 0;
27+
while(!quit) {
28+
29+
if (spndev_process(dev, &ev)) {
30+
switch (ev.type) {
31+
case SPNDEV_MOTION:
32+
printf("motion: T[%+6d %+6d %+6d] R[%+6d %+6d %+6d]\n",
33+
ev.mot.v[0], ev.mot.v[1], ev.mot.v[2], ev.mot.v[3],
34+
ev.mot.v[4], ev.mot.v[5]);
35+
break;
36+
37+
case SPNDEV_BUTTON:
38+
if ((s = spndev_button_name(dev, ev.bn.num))) {
39+
printf("button %d (\"%s\") ", ev.bn.num, s);
40+
} else {
41+
printf("button %d ", ev.bn.num);
42+
}
43+
puts(ev.bn.press ? "pressed" : "released");
44+
45+
if (ev.bn.press) {
46+
spndev_set_led(dev, led);
47+
spndev_write_lcd(dev, 0xAA);
48+
spndev_set_lcd_bl(dev, led);
49+
led = !led;
50+
}
51+
52+
break;
53+
54+
default:
55+
break;
56+
}
57+
}
58+
59+
}
60+
61+
spndev_close(dev);
62+
return 0;
63+
}
64+
65+
int main(int argc, char** argv) {
66+
printf("Press the button to test\n");
67+
return 0;
68+
}

0 commit comments

Comments
 (0)