This commit is contained in:
2026-03-02 23:10:29 +01:00
parent 07004097e9
commit 25ca1ec4a4
24 changed files with 1054 additions and 31 deletions

1
rktwebview/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.user

View File

@@ -4,10 +4,88 @@ project(rktwebview LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(GIT_EXECUTABLE git)
set(CMAKE_VERBOSE_MAKEFILE ON)
include_directories(../webview/core/include)
include_directories(../webview/build/Release/_deps/microsoft_web_webview2-src/build/native/include)
link_directories(../webview/build/Release/core)
set(WEBVIEW_BUILD_STATIC_LIBRARY ON)
set(WEBVIEW_WEBKITGTK_API "6.0")
#set(WEBVIEW_WEBKITGTK_PREFERRED_API_LIST webkitgtk-6.0)
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Release")
endif()
if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(LIBKIND "d")
else()
set(LIBKIND "r")
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(LIBEXT lib)
set(LIBPRE )
else()
set(LIBEXT a)
set(LIBPRE lib)
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# using Clang
add_compile_options(-fPIC)
set(CFLAGS_ENV "CFLAGS=-fPIC")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# using GCC
add_compile_options(-fPIC)
set(CFLAGS_ENV "CFLAGS=-fPIC")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
# using Intel C++
set(CFLAGS_ENV "")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# using Visual Studio C++
set(CFLAGS_ENV "")
endif()
if(NOT(EXISTS ./deps))
file(MAKE_DIRECTORY ./deps)
endif()
function(git_dep dir dep)
if(EXISTS ${rktwebview_SOURCE_DIR}/deps/${dir})
execute_process(
COMMAND ${GIT_EXECUTABLE} pull
COMMAND_ECHO STDOUT
WORKING_DIRECTORY ${rktwebview_SOURCE_DIR}/deps/${dir}
)
else()
message("Executing git clone...")
execute_process(
COMMAND ${GIT_EXECUTABLE} clone ${dep} ${rktwebview_SOURCE_DIR}/deps/${dir}
COMMAND_ECHO STDOUT
)
endif()
endfunction()
git_dep(webview https://github.com/webview/webview.git)
add_subdirectory(deps/webview)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED gtk+-3.0)
include_directories(${GTK_INCLUDE_DIRS})
message("${GTK_INCLUDE_DIRS}")
link_directories(${GTK_LIBRARY_DIRS})
pkg_check_modules(GTKWEBVIEW REQUIRED webkit2gtk-4.1)
include_directories(${GTKWEBVIEW_INCLUDE_DIRS})
message("${GTKWEBVIEW_INCLUDE_DIRS}")
link_directories(${GTKWEBVIEW_LIBRARY_DIRS})
endif()
include_directories(deps/webview/core/include)
include_directories(deps/webview/build/Release/_deps/microsoft_web_webview2-src/build/native/include)
link_directories(build/Release/deps/webview/core)
link_directories(build/Release)
add_library(rktwebview SHARED
@@ -15,18 +93,31 @@ add_library(rktwebview SHARED
rktwebview.cpp
rktwebview.h
json.h
json.cpp
main.cpp
)
add_executable(rktwebview_test
main.cpp
rktwebview_global.h
rktwebview.cpp
rktwebview.h
json.h
json.cpp
)
#add_dependencies(rktwebview webview)
target_link_libraries(rktwebview webview_static)
target_link_libraries(rktwebview_test rktwebview)
target_link_libraries(rktwebview webview_core_static)
#target_link_libraries(rktwebview_test rktwebview)
target_link_libraries(rktwebview_test webview_core_static)
target_compile_definitions(rktwebview PRIVATE RKTWEBVIEW_LIBRARY)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(rktwebview ${GTK_LIBRARIES} ${GTKWEBVIEW_LIBRARIES})
target_link_libraries(rktwebview_test ${GTK_LIBRARIES} ${GTKWEBVIEW_LIBRARIES})
endif()

View File

@@ -15,6 +15,10 @@
std::string json_escape( const std::string &str );
#ifdef Bool
#undef Bool
#endif
class JSON
{
union BackingData {

View File

@@ -5,12 +5,19 @@ int main()
{
rkt_webview_t *wv = rkt_create_webview();
result_t r = rkt_webview_navigate(wv, "https://wikipedia.org");
rkt_webview_t *wv1 = nullptr;
//result_t r = rkt_set_html(wv, "<html><head><title>He daar!</title></head><body><h1>He daar!</h1></body>");
printf("Navigate: result = %d\n", r);
int i = 0;
while(rkt_webview_valid(wv)) {
printf("Waiting...\n");
printf("Waiting...%d\n", i);
#ifdef USE_WIN_THREADS
Sleep(1000);
#endif
#ifdef USE_PTHREADS
usleep(1000 * 1000);
#endif
i += 1;
if (i > 5) {
item_t item = rkt_webview_call_js(wv, "{ window.location = 'https://dijkewijk.nl'; return 42; }");
@@ -20,6 +27,11 @@ int main()
printf("%d, %s\n", item1.context, item1.data);
rkt_webview_destroy_item(item1);
}
if (i == 16) {
wv1 = rkt_create_webview();
result_t r = rkt_webview_navigate(wv, "https://www.cs.cmu.edu/afs/cs/academic/class/15492-f07/www/pthreads.html");
}
}
return 0;
}

View File

@@ -24,7 +24,7 @@ static result_t do_dispatch(rkt_webview_t *, item_t item);
static DWORD webviewThread(LPVOID args)
#endif
#ifdef USE_PTHREADS
static int webviewThread(void *args)
static void *webviewThread(void *args)
#endif
{
rkt_webview_t *wv = (rkt_webview_t *) args;
@@ -41,14 +41,19 @@ static int webviewThread(void *args)
wv->webview_handle = NULL;
wv->handle_destroyed = true;
mutex_unlock(wv);
#ifdef USE_WIN_THREADS
return 0;
#endif
#ifdef USE_PTHREADS
return NULL;
#endif
}
#ifdef USE_WIN_THREADS
static DWORD queueGuardThread(LPVOID args)
#endif
#ifdef USE_PTHREADS
static int queueGuardThread(void *args)
static void *queueGuardThread(void *args)
#endif
{
rkt_webview_t *wv = (rkt_webview_t *) args;
@@ -73,7 +78,12 @@ static DWORD queueGuardThread(LPVOID args)
thread_sleep_ms(wv, 10);
}
#ifdef USE_WIN_THREADS
return 0;
#endif
#ifdef USE_PTHREADS
return NULL;
#endif
}
@@ -82,9 +92,12 @@ RKTWEBVIEW_EXPORT rkt_webview_t *rkt_create_webview()
rkt_webview_t *wv = (rkt_webview_t *) malloc(sizeof(rkt_webview_t));
if (wv == NULL) { return NULL; }
#ifdef _WIN32
#ifdef USE_WIN_THREADS
wv->mutex = CreateMutex(NULL, FALSE, NULL);
#endif
#ifdef USE_PTHREADS
wv->mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
wv->handle_set = false;
wv->handle_destroyed = false;
queue_init(&wv->to_webview);
@@ -112,7 +125,9 @@ RKTWEBVIEW_EXPORT rkt_webview_t *rkt_create_webview()
&wv->queue_guard_thread_id
);
#endif
#ifdef USE_PTHREAD
#ifdef USE_PTHREADS
wv->webview_thread_id = pthread_create(&wv->webview_thread, NULL, webviewThread, wv);
wv->queue_guard_thread_id = pthread_create(&wv->queue_guard_thread, NULL, queueGuardThread, wv);
#endif
bool go_on = true;
while(go_on) {
@@ -123,6 +138,7 @@ RKTWEBVIEW_EXPORT rkt_webview_t *rkt_create_webview()
thread_sleep_ms(wv, 10);
}
}
printf("handle_set = %d\n", wv->handle_set);
return wv;
}
@@ -152,6 +168,7 @@ RKTWEBVIEW_EXPORT item_t rkt_webview_call_js(rkt_webview_t *wv, const char *js)
mutex_unlock(wv);
char buf[30];
sprintf(buf, "%d", call_nr);
std::string _js = std::string("{ let f = function() { ") + js + " };" +
" let call_nr = " + buf + ";" +
" try { let r = { result: f() };" +
@@ -159,7 +176,7 @@ RKTWEBVIEW_EXPORT item_t rkt_webview_call_js(rkt_webview_t *wv, const char *js)
" } catch(e) {" +
" rkt_webview_call_js_result(call_nr, false, e.message); " +
" }" +
"}"; "";
"}";
item_t item = { CONTEXT_CALL_JS, strdup(_js.c_str()) };
@@ -232,7 +249,9 @@ RKTWEBVIEW_EXPORT result_t rkt_destroy_webview(rkt_webview_t *wv)
CloseHandle(wv->queue_guard_thread);
CloseHandle(wv->mutex);
#endif
#ifdef USE_PTHREAD
#ifdef USE_PTHREADS
pthread_join(wv->webview_thread, NULL);
pthread_join(wv->queue_guard_thread, NULL);
#endif
delete wv->js_evaluated;
free(wv);
@@ -405,11 +424,24 @@ result_t do_dispatch(rkt_webview_t *wv, item_t item)
}
return r;
} else {
lr = reason_set_html_failed;
switch(e) {
case WEBVIEW_ERROR_MISSING_DEPENDENCY: lr = reason_webview_missing_dependency;
break;
case WEBVIEW_ERROR_CANCELED: lr = reason_webview_canceled;
break;
case WEBVIEW_ERROR_INVALID_STATE: lr = reason_webview_invalid_state;
break;
case WEBVIEW_ERROR_INVALID_ARGUMENT: lr = reason_webview_invalid_argument;
break;
case WEBVIEW_ERROR_UNSPECIFIED: lr = reason_webview_unspecified;
break;
default:
lr = reason_webview_dispatch_failed;
}
r = error;
}
printf("error = %d, reason = %d", r, lr);
printf("error = %d, reason = %d\n", r, lr);
return r;
}
@@ -490,7 +522,8 @@ void mutex_lock(rkt_webview_t *wv)
#ifdef USE_WIN_THREADS
WaitForSingleObject(wv->mutex, INFINITE);
#endif
#ifdef USE_PTHREAD
#ifdef USE_PTHREADS
pthread_mutex_lock(&wv->mutex);
#endif
}
@@ -500,7 +533,8 @@ void mutex_unlock(rkt_webview_t *wv)
#ifdef USE_WIN_THREADS
ReleaseMutex(wv->mutex);
#endif
#ifdef USE_PTHREAD
#ifdef USE_PTHREADS
pthread_mutex_unlock(&wv->mutex);
#endif
}
@@ -509,6 +543,7 @@ static void thread_sleep_ms(rkt_webview_t *wv, int ms)
#ifdef USE_WIN_THREADS
Sleep(ms);
#endif
#ifdef USE_PTHREAD
#ifdef USE_PTHREADS
usleep(ms * 1000);
#endif
}

View File

@@ -13,9 +13,13 @@ typedef DWORD thread_id_t;
#define USE_WIN_THREADS
#endif
#ifdef _linux
#define USE_PTHREADS
#ifdef __linux
#include <pthread.h>
#include <unistd.h>
typedef pthread_t thread_t;
typedef pthread_mutex_t mutex_t;
typedef int thread_id_t;
#define USE_PTHREADS
#endif
#define CONTEXT_INVALID 0
@@ -38,11 +42,17 @@ typedef enum {
typedef enum {
reason_no_result_yet = -1,
reason_oke = 0,
reason_set_html_failed,
reason_set_navigate_failed,
reason_eval_js_failed,
reason_no_devtools_on_platform,
reason_no_delegate_for_context
reason_set_html_failed = 1,
reason_set_navigate_failed = 2,
reason_eval_js_failed = 3,
reason_no_devtools_on_platform = 4,
reason_no_delegate_for_context = 5,
reason_webview_missing_dependency = 6,
reason_webview_canceled = 7,
reason_webview_invalid_state = 8,
reason_webview_invalid_argument = 9,
reason_webview_unspecified = 10,
reason_webview_dispatch_failed = 11
} reason_t;
typedef struct {