This commit is contained in:
2026-03-26 14:35:10 +01:00
parent b35a982c6f
commit cf28fba3f5
14 changed files with 314 additions and 149 deletions

View File

@@ -2,6 +2,7 @@
#include <chrono>
#include <stdint.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <filesystem>
@@ -9,6 +10,7 @@
#include <unistd.h>
#include <spawn.h>
#endif
#include <fcntl.h>
#include "rktwebview.h"
#include "rkt_protocol.h"
@@ -65,11 +67,11 @@ bool runRktWebview(Handle_t *handler)
{
char *rkt_webview_prg_path = getenv("RKT_WEBVIEW_PRG");
if (rkt_webview_prg_path == nullptr) {
fprintf(stderr, "RKT_WEBVIEW_PRG environment variable not set, cannot start webview program\n");fflush(stderr);
ERROR0("RKT_WEBVIEW_PRG environment variable not set, cannot start webview program\n");
return false;
}
if (!fileExists(rkt_webview_prg_path)) {
fprintf(stderr, "%s does not exist or is not executable\n", rkt_webview_prg_path);fflush(stderr);
ERROR1("%s does not exist or is not executable\n", rkt_webview_prg_path);
return false;
}
@@ -88,36 +90,50 @@ bool runRktWebview(Handle_t *handler)
// run rktwebview_prg using the environment variable RKT_WEBVIEW_PRG
#ifdef _WIN32
//STARTUPINFO si;
//PROCESS_INFORMATION pi;
//ZeroMemory( &si, sizeof(si) );
//si.cb = sizeof(si);
//ZeroMemory( &pi, sizeof(pi) );
std::string cmdargs = std::string("") + handler->name + " " + shm_size_str + " " + command_slot + " " + command_result_slot + " " + event_slot;
std::string exe = std::string(rkt_webview_prg_path);
const char *td = getenv("TEMP");
if (td == nullptr) { td = "C:\\"; }
std::string tmpdir = td;
std::string logfile = tmpdir + "\\" + "rktwebview.log"; //handler->name + ".log";
std::string errfile = tmpdir + "\\" + "rktwebview.err";
std::string redirect = " ^>" + logfile + " 2^>" + errfile;
std::string cmdline = "start /b \"\" cmd /c \"" + exe + " " + cmdargs + " " + /*redirect*/ + "\"" + redirect;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE h = CreateFile(logfile.c_str(),
FILE_APPEND_DATA,
FILE_SHARE_WRITE | FILE_SHARE_READ,
&sa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
ZeroMemory( &pi, sizeof(pi) );
si.cb = sizeof(si);
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdInput= NULL;
si.hStdOutput = h;
si.hStdError = h;
DWORD flags = CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS;
std::string cmdargs = std::string("") + handler->name + " " + shm_size_str + " " + command_slot + " " + command_result_slot + " " + event_slot;
std::string exe = std::string(rkt_webview_prg_path);
std::string dir = basedir(exe);
std::string cmdline = exe + " " + cmdargs;
char *cmdline_str = const_cast<char *>(cmdline.c_str());
auto cwd = std::filesystem::current_path();
std::filesystem::current_path(path);
int r = system(cmdline_str);
std::filesystem::current_path(cwd);
//CreateProcessA(NULL, cmdline_str, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, path.c_str(), &si, &pi);
handler->rkt_webview_prg_pid = 0;
if (r != 0) {
fprintf(stderr, "Cannot create process '%s' (error = %ld)\n", cmdline_str, GetLastError());fflush(stderr);
bool r = CreateProcessA(NULL, cmdline_str, NULL, NULL, TRUE, flags, NULL, path.c_str(), &si, &pi);
if (!r) {
ERROR2("Cannot create process '%s' (error = %ld)\n", cmdline_str, GetLastError());
}
return (r == 0);
return r;
#else
char *argv[] = { rkt_webview_prg_path, const_cast<char *>(handler->name.c_str()), shm_size_str, command_slot, command_result_slot, event_slot, nullptr };
@@ -161,16 +177,16 @@ void rkt_webview_cleanup()
*/
if (handler->valid) {
fprintf(stderr, "Sending quit message\n");fflush(stderr);
INFO0("Sending quit message\n");
handler->command_queue->enqueue(CMD_QUIT);
fprintf(stderr, "Message sent\n");fflush(stderr);
INFO0("Message sent\n");
bool stopped = false;
while(!stopped) {
int cmd;
std::string s;
fprintf(stderr, "Getting result of quit message\n");fflush(stderr);
INFO0("Getting result of quit message\n");
handler->command_result_queue->dequeue(cmd, s, true);
fprintf(stderr, "got %d\n", cmd);fflush(stderr);
INFO1("got %d\n", cmd);
if (cmd == RESULT_QUIT) {
stopped = true;
}
@@ -481,7 +497,7 @@ void rkt_webview_free_data(rkt_data_t *d)
free(d->data.js_result.value);
free(d);
} else {
fprintf(stderr, "UNEXPECTED: data kind %d cannot be freed\n", d->kind);fflush(stderr);
ERROR1("UNEXPECTED: data kind %d cannot be freed\n", d->kind);
}
}