Racket FFI Interface for rktwebview_  qt
1 Overview
2 Requirements
3 Backend Availability
4 Module Initialization
5 Data Model
5.1 The rkt-wv Structure
rkt-wv
6 HTTP(S) Contexts
rkt-webview-new-context
7 Creating Webviews
rkt-webview-create
8 Window Lifecycle
rkt-webview-close
rkt-webview-valid?
rkt-webview-exit
9 Window Configuration
rkt-webview-set-title!
rkt-webview-set-ou-token
10 Navigation
rkt-webview-set-url!
rkt-webview-set-html!
11 Java  Script Execution
rkt-webview-run-js
rkt-webview-call-js
12 Window Geometry
rkt-webview-move
rkt-webview-resize
rkt-webview-show
rkt-webview-hide
rkt-webview-show-normal
rkt-webview-maximize
rkt-webview-minimize
rkt-webview-present
rkt-webview-window-state
13 Developer Tools
rkt-webview-open-devtools
14 Native Dialogs
rkt-webview-choose-dir
rkt-webview-file-open
rkt-webview-file-save
rkt-webview-messagebox
15 Event Delivery
16 Version Information
rkt-webview-version
17 Example
18 Summary
9.1.900

Racket FFI Interface for rktwebview_qt🔗ℹ

Hans Dijkema <hans@dijkewijk.nl>

 (require "racket-webview/racket-webview-qt")
  package: racket-webview

1 Overview🔗ℹ

The module racket-webview-qt.rkt provides a Racket FFI wrapper around the native rktwebview_qt library. It loads the shared library, initializes the native runtime, and exposes Racket functions for creating and controlling webview windows.

If the Qt backend is available locally, it is loaded directly. Otherwise the module attempts to resolve and download the backend. If that is not possible, the module continues in a degraded mode in which a limited subset of the FFI entry points will only display a warning and perform a no-op. All others will fail.

The wrapper translates the low-level C interface into a Racket-oriented API based on structures, callbacks, and ordinary Racket values.

The module provides:

  • creation of HTTP(S) contexts

  • creation and management of webview windows

  • navigation and HTML loading

  • JavaScript execution

  • window geometry and visibility control

  • native dialogs

  • asynchronous event delivery

  • version and cleanup utilities

2 Requirements🔗ℹ

The native backend requires Qt version 6.10.2 or newer.

The shared library rktwebview_qt must therefore be built against Qt 6.10.2 or a compatible later release.

Earlier Qt versions are not supported.

3 Backend Availability🔗ℹ

The module first checks whether the expected racket-webview-qt backend is already installed.

If it is not installed, the module attempts to resolve the configured download site. If the site can be resolved and the configured archive is downloadable, the backend is downloaded automatically.

If the download site cannot be resolved, if no archive is available for the current operating system and machine architecture, or if the download fails, the module does not immediately abort module loading. Instead it switches to a degraded mode in which native FFI loading is disabled.

In that degraded mode, a textual reason is stored internally and selected FFI entry points are replaced by fallback implementations.

When the backend cannot be loaded, the module defines fallback implementations for missing FFI entry points through define-ffi-definer and #:default-make-fail.

These fallbacks behave in two different ways.

For a small set of initialization and shutdown functions, a non-failing fallback is installed:

  • rkt_webview_env returns #t

  • rkt_webview_events_waiting returns 0

  • rkt_webview_init returns #t

  • rkt_webview_cleanup returns #t

All other missing FFI functions raise an exception when called.

Fallback warnings are emitted at most once per function. If native loading was disabled because the backend was unavailable, the warning message includes the recorded reason. If native loading was enabled but a specific symbol could not be loaded from the library, the error names the library file.

4 Module Initialization🔗ℹ

Loading the module performs several initialization steps automatically.

  • determines the operating system and architecture

  • sets Qt runtime environment variables

  • loads the rktwebview_qt shared library

  • initializes the native runtime

  • starts a background thread that processes native events

Currently the wrapper supports the following platforms:

  • linux

  • windows

If the current system is unsupported, loading the module raises an error.

5 Data Model🔗ℹ

5.1 The rkt-wv Structure🔗ℹ

Each webview window is represented by a transparent Racket structure.

struct

(struct rkt-wv (win evt-queue callback valid close-callback))

  win : exact-integer?
  evt-queue : any/c
  callback : procedure?
  valid : boolean?
  close-callback : procedure?
Represents a webview instance managed by the Racket wrapper.

Fields:

  • win: the native integer window handle

  • evt-queue: internal queue of pending event strings

  • callback: user event callback

  • valid: mutable flag indicating whether the wrapper considers the window active

  • close-callback: procedure invoked when the window is closed

Although the structure is transparent, user code should normally treat it as an opaque handle.

6 HTTP(S) Contexts🔗ℹ

A context represents the shared HTTP(S) environment used by webviews.

Contexts correspond to native WebEngine profiles and determine properties such as:

  • cookies and cache

  • injected JavaScript

  • trusted certificates

procedure

(rkt-webview-new-context boilerplate-js    
  server-cert)  exact-integer?
  boilerplate-js : string?
  server-cert : bytes?
Creates a new HTTP(S) context and returns its native identifier.

Arguments:

  • boilerplate-js: JavaScript source injected into pages

  • server-cert: optional certificate data

The returned context identifier can be passed to rkt-webview-create to create webviews within that context.

7 Creating Webviews🔗ℹ

procedure

(rkt-webview-create context    
  parent    
  evt-callback    
  close-callback)  rkt-wv?
  context : exact-integer?
  parent : (or/c #f rkt-wv?)
  evt-callback : (-> rkt-wv? string? any)
  close-callback : (-> any)
Creates a new webview window in the given HTTP(S) context.

Arguments:

  • context: context identifier returned by rkt-webview-new-context

  • parent: optional parent webview

  • evt-callback: procedure invoked for each event

  • close-callback: procedure invoked when the window is closed

The result is a new rkt-wv structure.

Events generated by the native layer are delivered asynchronously through evt-callback.

8 Window Lifecycle🔗ℹ

procedure

(rkt-webview-close wv)  boolean?

  wv : rkt-wv?
Requests that the webview window be closed.

The wrapper forwards the request to the native backend and schedules cleanup of the event-processing loop.

Returns #t.

procedure

(rkt-webview-valid? wv)  boolean?

  wv : rkt-wv?
Returns whether the webview still exists.

The wrapper first checks its internal validity flag and then queries the native runtime.

procedure

(rkt-webview-exit)  void?

Closes all webviews and stops the background event-processing thread.

This function is also registered with the Racket plumber so that cleanup occurs automatically when the process exits.

9 Window Configuration🔗ℹ

procedure

(rkt-webview-set-title! wv title)  symbol?

  wv : rkt-wv?
  title : string?
Sets the native window title.

Returns a result symbol such as:

  • 'oke

  • 'failed

procedure

(rkt-webview-set-ou-token wv token)  boolean?

  wv : rkt-wv?
  token : string?
Associates an Organizational Unit token with the window.

This token may be used by the native layer when accepting certain self-signed certificates.

10 Navigation🔗ℹ

procedure

(rkt-webview-set-url! wv url)  symbol?

  wv : rkt-wv?
  url : string?
Navigates the webview to the given URL.

Returns a result symbol such as:

  • 'oke

  • 'set_navigate_failed

procedure

(rkt-webview-set-html! wv html)  symbol?

  wv : rkt-wv?
  html : string?
Loads HTML directly into the webview.

Returns a result symbol such as:

  • 'oke

  • 'set_html_failed

11 JavaScript Execution🔗ℹ

procedure

(rkt-webview-run-js wv js)  symbol?

  wv : rkt-wv?
  js : string?
Executes JavaScript without returning a value.

Returns a result symbol such as:

  • 'oke

  • 'eval_js_failed

procedure

(rkt-webview-call-js wv js)  (list/c symbol? string?)

  wv : rkt-wv?
  js : string?
Executes JavaScript and returns:

(list result value)

where:

  • result is the native result code

  • value is a JSON string containing the returned data

The JSON structure is generated by the native backend.

12 Window Geometry🔗ℹ

procedure

(rkt-webview-move wv x y)  symbol?

  wv : rkt-wv?
  x : exact-integer?
  y : exact-integer?
Moves the webview window to the given screen coordinates.

procedure

(rkt-webview-resize wv width height)  symbol?

  wv : rkt-wv?
  width : exact-integer?
  height : exact-integer?
Resizes the window.

procedure

(rkt-webview-show wv)  symbol?

  wv : rkt-wv?
Shows the window.

procedure

(rkt-webview-hide wv)  symbol?

  wv : rkt-wv?
Hides the window.

procedure

(rkt-webview-show-normal wv)  symbol?

  wv : rkt-wv?
Restores the window to its normal state.

procedure

(rkt-webview-maximize wv)  symbol?

  wv : rkt-wv?
Maximizes the window.

procedure

(rkt-webview-minimize wv)  symbol?

  wv : rkt-wv?
Minimizes the window.

procedure

(rkt-webview-present wv)  symbol?

  wv : rkt-wv?
Requests that the window be presented to the user.

procedure

(rkt-webview-window-state wv)  symbol?

  wv : rkt-wv?
Returns the current window state.

Possible results:

  • 'normal

  • 'minimized

  • 'maximized

  • 'hidden

  • 'normal_active

  • 'maximized_active

13 Developer Tools🔗ℹ

procedure

(rkt-webview-open-devtools wv)  symbol?

  wv : rkt-wv?
Opens the browser developer tools window.

14 Native Dialogs🔗ℹ

Dialog functions return immediately with a status code. The user’s choice is delivered asynchronously through the event callback.

procedure

(rkt-webview-choose-dir wv title base-dir)  symbol?

  wv : rkt-wv?
  title : string?
  base-dir : string?
Requests a directory-selection dialog.

procedure

(rkt-webview-file-open wv    
  title    
  base-dir    
  extensions)  symbol?
  wv : rkt-wv?
  title : string?
  base-dir : string?
  extensions : string?
Requests a file-open dialog.

procedure

(rkt-webview-file-save wv    
  title    
  base-dir    
  extensions)  symbol?
  wv : rkt-wv?
  title : string?
  base-dir : string?
  extensions : string?
Requests a file-save dialog.

procedure

(rkt-webview-messagebox wv    
  title    
  message    
  submessage    
  type)  symbol?
  wv : rkt-wv?
  title : string?
  message : string?
  submessage : string?
  type : symbol?
Shows a native message box.

15 Event Delivery🔗ℹ

Each webview has an associated event callback.

The callback has the form:

(λ (wv event-json) ...)

Arguments:

  • wv: the webview handle

  • event-json: JSON event string from the native backend

Typical event types include:

  • "show"

  • "hide"

  • "move"

  • "resize"

  • "closed"

  • "page-loaded"

  • "navigation-request"

  • "js-evt"

The wrapper does not parse the JSON payload.

16 Version Information🔗ℹ

procedure

(rkt-webview-version)  (list/c list? list?)

Returns version information for the native backend.

Example result:

(list
 (list 'webview-c-api 1 0 0)
 (list 'qt 6 10 2))

17 Example🔗ℹ

(define ctx
  (rkt-webview-new-context "" #""))
 
(define wv
  (rkt-webview-create
   ctx
   #f
   (λ (wv evt)
     (displayln evt))
   (λ ()
     (displayln "closed"))))
 
(rkt-webview-set-title! wv "Example")
(rkt-webview-set-url! wv "https://example.org")

18 Summary🔗ℹ

The FFI module provides a thin Racket interface to the native rktwebview_qt backend.

Key characteristics:

  • thin wrapper around the native C API

  • asynchronous event delivery

  • JSON-based event payloads

  • simple Racket structures for webviews