Show a program's ZX window in the browser through the canvas player
A program entry (one that declares main) was refused in any profile but codegen c99, so window.zi's ZX could not run in a page. A codegen = "zx" profile now bundles the program's own main with portable IR, which kryon/app gives the zx_window window; no generated program host is made. In the page the ZX canvas player runs such a bundle through window_page_player.zi: it binds zx_window's five capabilities to the page's canvas with page_core, as page_window does for a C program, lowers the bundle to Wasm in the page and runs main until the page closes. Each wait for an event sleeps, which yields to the page, so the program's own loop never blocks it. zx_window_bundle.zi holds the capability check both players use. The editor's Wasm platform opens window.zi this way, and its browser test checks that the page's canvas is cleared to the frame colour.
This commit is contained in:
1 parent
f10e551f6c
commit
0c6f138e31
8 files changed
+177
-33
No files matched your search
@@ -2,6 +2,13 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
- A program entry builds in a profile with `codegen = "zx"`: its bundle is
|
||||
the program's own `main`, with portable IR, so `kryon/app` gives it the ZX
|
||||
window. In a canvas profile the ZX canvas player binds `zx_window` to the
|
||||
page's canvas (`window_page_player.zi`), lowers the bundle to Wasm in the
|
||||
page and runs `main` there, each wait for an event yielding to the page, so
|
||||
`examples/window.zi`'s ZX shows its window in the browser; the editor's
|
||||
Wasm platform opens it there. Both players share `zx_window_bundle.zi`.
|
||||
- `zx_window_host.zi` is the desktop's zx_window capabilities as a host
|
||||
library: `make zx-window-host` builds `build/lib/libkryon-desktop.so` and
|
||||
`make install-user` installs it and registers it as
|
||||
|
||||
+6
-1
@@ -1043,7 +1043,12 @@ window.zx` opens the window. The same capabilities are a host library,
|
||||
`make install-user` registers as `kryon-desktop.library`, so `zx2native
|
||||
window.zx` writes a Linux executable that links them and opens the window
|
||||
itself. `make zx-window-test` checks both on a private Xvfb, and that with no
|
||||
library registered `zx2native` stops at the gap naming `ZxWindowOpenHost`. `App.Run(window, Draw)` brings the frame loop, the widget tree's frame
|
||||
library registered `zx2native` stops at the gap naming `ZxWindowOpenHost`. In
|
||||
the browser, a project profile with `backend = "canvas"` and `codegen = "zx"`
|
||||
takes a program entry too: its bundle is the program's own `main`, and the ZX
|
||||
canvas player binds the window to the page's canvas (`window_page_player`),
|
||||
lowers the bundle to Wasm in the page and runs it, each wait for an event
|
||||
yielding to the page. `App.Run(window, Draw)` brings the frame loop, the widget tree's frame
|
||||
driver (`TreeFrameUse`) and, on Linux, the accessibility tree
|
||||
(`AccessibilityTreeUse`). The desktop raster loads pictures, smooth copies and
|
||||
fonts when a frame first draws them, video when it opens, and the test driver
|
||||
|
||||
+14
-7
@@ -128,9 +128,15 @@ endif
|
||||
# the profile's host owns main.
|
||||
PROJECT_PROGRAM := $(shell grep -Eqs '^main[[:space:]]*::' '$(PROJECT_ENTRY)' && echo program)
|
||||
ifneq ($(PROJECT_PROGRAM),)
|
||||
ifneq ($(PROJECT_CODEGEN),c99)
|
||||
$(error a program entry ($(PROJECT_ENTRY) declares main) needs codegen c99; a portable profile needs a Frame entry)
|
||||
endif
|
||||
ifeq ($(PROJECT_CODEGEN),zx)
|
||||
# A portable program's bundle is its own main, which kryon/app gives the
|
||||
# window ZX bundles have; the profile's player runs it.
|
||||
HOST_ID := $(PROJECT_ENTRY_MODULE)
|
||||
PROJECT_FRAME_BIND :=
|
||||
HOST_ROOT_SOURCES := $(PROJECT_ENTRY)
|
||||
else ifneq ($(PROJECT_CODEGEN),c99)
|
||||
$(error a program entry ($(PROJECT_ENTRY) declares main) needs codegen c99 or zx)
|
||||
else
|
||||
# The generated host is the process entry; it only calls the program's main.
|
||||
PROGRAM_HOST := $(GEN_DIR)/program-host/kryon_program_host.zi
|
||||
HOST := $(PROGRAM_HOST)
|
||||
@@ -138,6 +144,7 @@ HOST_ID := kryon_program_host
|
||||
# Its folder is a module path, so the host is saved under its own name.
|
||||
PROJECT_FRAME_BIND := --module-path $(dir $(PROGRAM_HOST))
|
||||
HOST_ROOT_SOURCES := $(PROGRAM_HOST) $(PROJECT_ENTRY)
|
||||
endif
|
||||
else
|
||||
PROJECT_FRAME_BIND := --bind $(ZIRAN_PACKAGE_ID)_frame_entry:ProjectFrame=$(PROJECT_ENTRY_MODULE):Frame
|
||||
HOST_ROOT_SOURCES += $(PROJECT_ENTRY) $(KRYON_DIR)/src/backend/frame_entry.zi
|
||||
@@ -172,9 +179,9 @@ IR_COMMAND := $(ZIRAN) ir $(ZIRAN_MODULE_ARGS) $(PROJECT_FRAME_BIND) $(WEB_TARGE
|
||||
C_COMMAND := $(ZIRAN_DIR)/build/bin/zi2c --define PLATFORM_WEB --entry $(HOST_ID):main \
|
||||
--root $(IR_DIR) -o $(C_DIR) $(IR_DIR)/*.zir
|
||||
else
|
||||
# A program selects its kryon/app host by compiled target, so its IR names
|
||||
# the native C target; frame IR stays portable.
|
||||
IR_COMMAND := $(ZIRAN) ir $(ZIRAN_MODULE_ARGS) $(PROJECT_FRAME_BIND) $(if $(PROJECT_PROGRAM),--target=c) $(if $(filter c99,$(PROJECT_CODEGEN)),--define POSIX_THREADS) --entry $(HOST_ID):main -o $(IR_DIR) $(HOST_ROOT_SOURCES)
|
||||
# A program selects its kryon/app host by compiled target, so a C program's
|
||||
# IR names the native C target; a ZX program's and frame IR stay portable.
|
||||
IR_COMMAND := $(ZIRAN) ir $(ZIRAN_MODULE_ARGS) $(PROJECT_FRAME_BIND) $(if $(PROJECT_PROGRAM),$(if $(filter c99,$(PROJECT_CODEGEN)),--target=c)) $(if $(filter c99,$(PROJECT_CODEGEN)),--define POSIX_THREADS) --entry $(HOST_ID):main -o $(IR_DIR) $(HOST_ROOT_SOURCES)
|
||||
C_COMMAND := $(ZIRAN) build --target=c --entry $(HOST_ID):main \
|
||||
--root $(IR_DIR) -o $(C_DIR) $(IR_DIR)/*.zir
|
||||
endif
|
||||
@@ -374,7 +381,7 @@ $(PROGRAM): $(IR_STAMP) $(ASSET_DEPENDENCY) $(ZIRAN_DIR)/build/bin/zi2zx
|
||||
-o $@ $(IR_DIR)/*.zir
|
||||
endif
|
||||
|
||||
ifneq ($(PROJECT_PROGRAM),)
|
||||
ifneq ($(PROGRAM_HOST),)
|
||||
# main takes nothing or (argc, argv) and returns nothing or its exit status.
|
||||
$(PROGRAM_HOST): $(PROJECT_ENTRY) $(KRYON_DIR)/mk/ziran-project.mk
|
||||
@mkdir -p $(dir $@)
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
// The page's side of a bundle that only shows a window, as page_window shows
|
||||
// a C program's: Kryon's ZX canvas player binds the bundle's zx_window
|
||||
// capabilities to the page's canvas and runs the bundle's entry until the
|
||||
// page closes. Waiting for an event sleeps, which yields to the page, so the
|
||||
// entry's own loop never blocks it. No frame loop, raster, fonts or input
|
||||
// routing run for it.
|
||||
#import "std/bundle_host"
|
||||
#import "std/byte_text_linux"
|
||||
#import "std/c_string"
|
||||
#import "page_core"
|
||||
#import "web"
|
||||
using Window :: #import "zx_window_bundle";
|
||||
|
||||
using HostValueKind;
|
||||
|
||||
// zx_window's events.
|
||||
WINDOW_NONE :: 0
|
||||
WINDOW_CLOSE :: 1
|
||||
WINDOW_DRAW :: 2
|
||||
|
||||
// How long a wait sleeps before it looks at the page again.
|
||||
WINDOW_WAIT_MS :: 50
|
||||
|
||||
PageWindowCall :: (context: *void, module_name: *u8, function_name: *u8,
|
||||
args: *HostValue, count: s32, result: *HostValue) -> s32 {
|
||||
function := TextFromCString(function_name)
|
||||
result.kind = HostVoid
|
||||
if function == "ZxWindowOpenHost" && count == 3 && args[0].kind == HostString &&
|
||||
args[1].kind == HostInteger && args[2].kind == HostInteger {
|
||||
width := args[1].integer
|
||||
height := args[2].integer
|
||||
title: string
|
||||
if args[0].length > 0 { title = TextFromBytes(BytesFromPointer(args[0].data, cast(u64)args[0].length)) }
|
||||
open := false
|
||||
if width > 0 && width <= 16384 && height > 0 && height <= 16384 {
|
||||
PageBoot(cast(s32)width, cast(s32)height, title)
|
||||
open = PageIsOpen() && PageCanvas() != 0
|
||||
if open { PageBindResize() }
|
||||
}
|
||||
result.kind = HostInteger
|
||||
result.integer = cast(s64)open
|
||||
return 1
|
||||
}
|
||||
if function == "ZxWindowClearHost" && count == 1 && args[0].kind == HostUnsigned {
|
||||
ctx := PageContext()
|
||||
color := args[0].bits
|
||||
if ctx != 0 {
|
||||
scope := WebScope()
|
||||
PageBackground(ctx, 0.0, 0.0, cast(float64)PageDimension(0), cast(float64)PageDimension(1),
|
||||
cast(s32)((color >> 16) & 255), cast(s32)((color >> 8) & 255), cast(s32)(color & 255), 255)
|
||||
WebEnd(scope)
|
||||
}
|
||||
result.kind = HostInteger
|
||||
result.integer = cast(s64)(ctx != 0)
|
||||
return 1
|
||||
}
|
||||
if count != 0 { return 0 }
|
||||
if function == "ZxWindowEventHost" {
|
||||
event := WINDOW_NONE
|
||||
if PageDimension(6) != 0 { event = WINDOW_CLOSE }
|
||||
else {
|
||||
PageSleep(WINDOW_WAIT_MS)
|
||||
// A resized canvas starts empty.
|
||||
if PageDimension(6) != 0 { event = WINDOW_CLOSE }
|
||||
else if PageDimension(4) != 0 { event = WINDOW_DRAW }
|
||||
}
|
||||
result.kind = HostInteger
|
||||
result.integer = cast(s64)event
|
||||
return 1
|
||||
}
|
||||
if function == "ZxWindowPresentHost" { PageFrame(); return 1 }
|
||||
if function == "ZxWindowCloseHost" { PageClose(); return 1 }
|
||||
return 0
|
||||
}
|
||||
|
||||
// Binds the bundle's window capabilities to the page and runs its entry
|
||||
// until it returns, with the entry's result as the status. A capability
|
||||
// other than zx_window's stops it before anything runs.
|
||||
Run :: (bundle: *void) -> s32 {
|
||||
bindings: [WINDOW_CAPABILITIES]HostBinding
|
||||
count: u64 = 0
|
||||
index: u64 = 0
|
||||
while index < BundleCapabilities(bundle) {
|
||||
module := BundleCapabilityModuleName(bundle, index)
|
||||
function := BundleCapabilityFunctionName(bundle, index)
|
||||
if !Capability(module, function) || count >= cast(u64)WINDOW_CAPABILITIES { return 1 }
|
||||
bindings[count] = HostBinding.{.module = module, .function = function, .call = PageWindowCall,
|
||||
.context = null}
|
||||
count += 1
|
||||
index += 1
|
||||
}
|
||||
instance := InstantiateBundle(bundle, *bindings[0], count)
|
||||
if instance == null { return 1 }
|
||||
result: s64
|
||||
has_result: s32
|
||||
ran := RunBundleInstance(instance, *result, *has_result) != 0
|
||||
CloseBundleInstance(instance)
|
||||
// An entry that stopped without closing its window leaves the page open
|
||||
// as it is.
|
||||
if !ran { return 1 }
|
||||
return ifx has_result != 0 then cast(s32)result else 0
|
||||
}
|
||||
@@ -6,34 +6,11 @@
|
||||
#import "std/bundle_host"
|
||||
#import "std/byte_text_linux"
|
||||
#import "std/c_string"
|
||||
Names :: #import "bundle_names";
|
||||
using Window :: #import "zx_window_bundle";
|
||||
Desktop :: #import "desktop_window";
|
||||
|
||||
using HostValueKind;
|
||||
|
||||
// zx_window's five capabilities.
|
||||
WINDOW_CAPABILITIES :: 5
|
||||
|
||||
Capability :: (module: *u8, function: *u8) -> bool {
|
||||
if !Names.ModuleName(TextFromCString(module), "zx_window") { return false }
|
||||
name := TextFromCString(function)
|
||||
return name == "ZxWindowOpenHost" || name == "ZxWindowEventHost" ||
|
||||
name == "ZxWindowClearHost" || name == "ZxWindowPresentHost" ||
|
||||
name == "ZxWindowCloseHost"
|
||||
}
|
||||
|
||||
// The bundle shows its window through zx_window.
|
||||
WindowBundle :: (bundle: *void) -> bool {
|
||||
index: u64 = 0
|
||||
while index < BundleCapabilities(bundle) {
|
||||
if Names.ModuleName(TextFromCString(BundleCapabilityModuleName(bundle, index)), "zx_window") {
|
||||
return true
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
WindowCall :: (context: *void, module_name: *u8, function_name: *u8,
|
||||
args: *HostValue, count: s32, result: *HostValue) -> s32 {
|
||||
opened := cast(*Desktop.DesktopWindow)context
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// A bundle that shows its window through zx_window, as App.Show's portable
|
||||
// window does, and its five capabilities: what a player binds for it, the
|
||||
// desktop's in window_player and the page's in window_page_player.
|
||||
#import "std/bundle_host"
|
||||
#import "std/byte_text_linux"
|
||||
#import "std/c_string"
|
||||
Names :: #import "bundle_names";
|
||||
|
||||
// zx_window's five capabilities.
|
||||
WINDOW_CAPABILITIES :: 5
|
||||
|
||||
Capability :: (module: *u8, function: *u8) -> bool {
|
||||
if !Names.ModuleName(TextFromCString(module), "zx_window") { return false }
|
||||
name := TextFromCString(function)
|
||||
return name == "ZxWindowOpenHost" || name == "ZxWindowEventHost" ||
|
||||
name == "ZxWindowClearHost" || name == "ZxWindowPresentHost" ||
|
||||
name == "ZxWindowCloseHost"
|
||||
}
|
||||
|
||||
// The bundle shows its window through zx_window.
|
||||
WindowBundle :: (bundle: *void) -> bool {
|
||||
index: u64 = 0
|
||||
while index < BundleCapabilities(bundle) {
|
||||
if Names.ModuleName(TextFromCString(BundleCapabilityModuleName(bundle, index)), "zx_window") {
|
||||
return true
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
return false
|
||||
}
|
||||
+2
-1
@@ -22,6 +22,7 @@ Background :: #import "background_window_linux";
|
||||
Watch :: #import "bundle_watch";
|
||||
AppPlayer :: #import "app_player";
|
||||
WindowPlayer :: #import "window_player";
|
||||
WindowBundles :: #import "zx_window_bundle";
|
||||
#import "timer_linux"
|
||||
|
||||
using HostValueKind;
|
||||
@@ -580,7 +581,7 @@ main :: (argc: s32, argv: **u8) -> s32 {
|
||||
return status
|
||||
}
|
||||
// A bundle that only shows a window runs its own entry in it.
|
||||
if WindowPlayer.WindowBundle(bundle) {
|
||||
if WindowBundles.WindowBundle(bundle) {
|
||||
if watch {
|
||||
CloseBundle(bundle)
|
||||
print("zx: window release watching is not available yet\n")
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
using Host :: #import "Canvas";
|
||||
using Render :: #import "canvas_raster";
|
||||
Player :: #import "bundle_player";
|
||||
WindowBundles :: #import "zx_window_bundle";
|
||||
WindowPage :: #import "window_page_player";
|
||||
#import "bundle_media"
|
||||
#import "page_core"
|
||||
#import "bundle_app"
|
||||
@@ -162,6 +164,19 @@ LoadSelected :: (player: *Player.Player, assets: *AppFiles, raster: *RasterHost)
|
||||
data := BytesFromPointer(descriptor.data, cast(u64)bytes.count)
|
||||
checked := OpenBundleBytes(descriptor.data, cast(u64)bytes.count)
|
||||
if checked == null { ShowStatus("This file is not a valid ZX."); return false }
|
||||
// A bundle that only shows a window runs its own entry on the page until
|
||||
// the page closes, with the page's canvas as its window.
|
||||
if WindowBundles.WindowBundle(checked) {
|
||||
Player.PlayerClose(player)
|
||||
BundleMediaClose(*media)
|
||||
BundleAppClose(*apps[active_app])
|
||||
hosted = false
|
||||
ShowStatus("")
|
||||
status := WindowPage.Run(checked)
|
||||
CloseBundle(checked)
|
||||
if status != 0 { ShowStatus("The ZX window could not open.") }
|
||||
return status == 0
|
||||
}
|
||||
is_app := BundleIsApp(checked)
|
||||
accepted := !is_app || BundleAppCheck(checked)
|
||||
CloseBundle(checked)
|
||||
|
||||
Reference in new issue
Block a user