Native desktop graphics backend for the Cosmos Space API, implemented with
SWI-Prolog's XPCE toolkit (library(pce) + library(pce_main)).
| File | Role |
|---|---|
libs/space.co |
Public entry point, and the whole Cosmos side of the library: one rel per method name (table literals cannot overload), each calling a pl::space_xpce_* predicate, re-exported as a nested table so consumers are type/arity checked. See XPCE_SIMPLE_LIBRARY.md. |
libs/space_xpce_prolog.pl |
Raw Prolog implementation (window, retained-object drawing, timer loop). Loaded at runtime from an absolute path recorded at build time (see below). Never embedded via qsave: a saved image containing library(pce) does not start. |
libs/space.pl / .cif |
Compiled form of space.co (checked in, like other libs). |
misc/simple_xpce.co |
Minimal app: one rect, circle, ellipse, line, text. |
misc/moving_rect.co |
Animated app: the Cosmos equivalent of ui2.pl's moving rectangle. |
test4.co |
Header-only app (--xpce): arrow-key-controlled rectangle, driven by the event queue. |
misc/demo_space_xpce.co |
Full demo: rect/circle/ellipse/arc/polygon/line, transforms, fonts, scissor record. |
misc/*.exe + misc/*.dll |
Built apps plus staged XPCE native libraries. |
sp.init(w,h), sp.open(), sp.start(ms,callbacks,state),
sp.startLoop(seconds,width,height,callbacks,state), sp.startCanvas(fps,callbacks,state),
sp.quit/dispose/stop/loadImage, sp.beginFrame/sp.endFrame,
graphics.clear/setColor/setBackgroundColor/getColor/getBackgroundColor/rectangle/line/point/points/circle/ellipse/arc/polygon/print/printf/push/pop/origin/translate/rotate/scale/shear/setLineWidth/getLineWidth/setLineStyle/getLineStyle/setScissor/clearScissor/getScissor/draw/refresh,
plus newFont/setFont/getFont, getDimensions/getWidth/getHeight, timer (getTime/getDelta/getFPS),
input (isDown/isMouseDown/getX/getY/getPosition/buttonDown) and the event queue
(event.get/getType/peek, plus the on callback table passed to sp.start).
Event types delivered: mousemotion, mousedown, mouseup, mousewheel
(with a real wheel amount), keydown, keyup, textinput, focus, blur
and resize.
XPCE 10.0.0 has two ways to see the keyboard, and the backend uses both.
Polling. The keyboard INPUT DEVICE class is reachable as
@keyboard_device and answers whether a key is held down right now:
get(@keyboard_device, key_down, Key, @on) % held
get(@keyboard_device, key_down, Key, @off) % not heldKey is a name XPCE understands: one of its own event names (RET, ESC,
TAB, BS, DEL, space, insert, pause, cursor_home, end,
page_up, page_down, cursor_left, cursor_right, cursor_up,
cursor_down), f1..f24, a single character, or the name SDL uses for
the key ("Left Shift"). A name XPCE does not know makes the get fail with
Unknown key, so the name is checked against the list XPCE accepts before
it is polled. This is the same call XPCE's own games use — see
prolog/demo/asteroids.pl in the XPCE package.
There is still no push_event/4, push_global_event/4, get_event/3 or
key_name/2; those do not exist in this build. But the keyboard device
covers the one thing they would have been used for.
Events. The hook is the ->event method of class graphical, redefined
on a subclass of picture:
:- op(1200, xfx, :->).
:- pce_begin_class(space_picture, picture, "...").
event(G, Ev:event) :->
( space_xpce_capture(G, Ev)
; true
),
( send_super(G, event, Ev)
; true
).
:- pce_end_class.Two constraints shape the whole implementation, and both are expensive to rediscover:
- An XPCE error raised while the loop is running opens a MODAL dialog
that blocks dispatch for good.
catch/3does not help: the error is raised in C++ and aborts the goal rather than becoming a Prolog exception. One bad call freezes the app with no way back, so every call inspace_xpce_read/4is verified against this build. ?/2does not exist here. It raisesexistence_error(procedure, (?)/2)even afteruse_module(library(yall)). Useget/3andsend/2,3only.
Verified accessors: get(Ev, id, _), get(Ev, position, _) followed by
get(Pt, x, _), get(Pt, y, _), get(Ev, button, _),
get(Ev, multiclick, _), get(Ev, key, _),
get(Ev, rotation, _), send(Ev, is_a, mouse),
send(Ev, is_a, key_up), send(Ev, is_down|is_up|is_drag)
(zero-argument behaviours, so send/2), and
send(Ev, has_modifier, s|c|m) — bare atoms; e is not accepted. Absent:
event <-modifiers, event <-click_time, event <-dy.
Event ids come from @event_tree: mouse press/release/drag as
ms_left_down and friends, loc_move, loc_still, wheel, area_enter;
keys as the ASCII code (an integer) or a named atom (f1,
cursor_left, 'ESC'). Shift is already folded into the code, so shift+A
arrives as 65 with modifier s.
Key releases exist. The release event is key_up, and it sits under
any in @event_tree rather than under keyboard/ — which is why looking
only under keyboard/ makes it look like the build has no releases at all.
Its id is the atom key_up; the key it releases comes from event <-key,
which answers the Emacs-style name of the matching key-down ("a", "A",
"RET", "<f1>"). The backend turns that into the same Love2D name a key
press reports, so a press and its release agree.
The wheel rotation amount is read from event <-rotation, which is an
ordinary get method: it answers the rotation in fifteenths of a notch and a
non-number for every event that is not a wheel.
The event queue is copied, not linked. The queue is a Cosmos table per
event (a Prolog assoc), and an event table is built inside XPCE's event
dispatch. The cells of that dispatch do not survive it, so a queue that only
refers to those tables comes back as half-built trees: assocs whose keys and
values have turned into unbound variables. A half-built table is not an assoc
any more, so get_assoc/3 fails on it, the type filter drops the event, and
the program never sees it. The queue is therefore stored with nb_setval/2
(which copies into the protected area of the global variable space) and read
back through a copy_term/2, so no caller ever holds cells that the next
event can free.
The symptom was confusing, because it only hit the newest event: the tables
that had been in the queue for a few frames were still intact, and a program
that reads the whole queue still saw most of it. A program that asks for one
kind of event (sp.event.getType('keydown', Keys)) always asks for the
newest one, so it always got nothing — which is why the keyboard looked dead
while the mouse still worked. test/space_xpce_events.pl reproduces the whole
path (inject an event with frame->sdl_test_event, read it back on the next
frame) and fails without the copy.
Synthetic events. graphical->generate_event/1 posts an event with the
given id to a graphical, which is the same path a real key press takes:
send(Window, generate_event, 119) % the key 'w'XPCE dispatches it, the ->event method captures it, and it lands in the
Space queue like any other event. test/space_xpce_events.pl uses it to
check the keyboard path without a keyboard.
Deliberate limits:
setScissoronly records the rect; clipping is not enforced.setLineStyleonly remembers the style: XPCE has no rough/smooth switch of its own, so the style is answered back unchanged.- Rotation of ellipses only moves the centre (shape stays axis-aligned).
getFontreports the font that was set, but the family and style are XPCE's own (sans,serif,mono;normal,bold,italic).
require('space', sp) is the supported entry point and is checked: arity and
member existence are both enforced. That works because libs/space.co is one
rel per method behind a nested export table that mirrors the shape of the
raw XPCE table.
misc/ has no space.cif, so the apps there are still compiled unchecked;
copy libs/space.cif beside a program to check it.
Adding a method means adding it to the export({...}) table at the bottom of
space.co as well, or it stays invisible to anyone requiring 'space'.
The user-facing spec is XPCE_SIMPLE_LIBRARY.md.
- No overloading in table literals: two
relwith the same name collapse (last wins). Every method name exists exactly once per table. - Value-position calls append the result:
g.newFont(24,'sans','normal')nested insidesetFont(...)only works because the rel is declarednewFont(size, family, style, font)with the out-param last. cut, not!; no(cond -> a ; b)goal syntax.- Single-quoted literals are strings, not atoms. Prolog predicates that
need atoms (e.g.
current_prolog_flag) must convert viaatom_string. - A top-level
require('space', sp)capturesspinto every rel, turningmain/1intomain/2and breaking the--main/--exeargv adapter. Keeprequireinside rel bodies for programs built with--main, or build with--xpceinstead and keeprequireat the top of the file (see Build integration). The captured callbacks are fine either way:call_cl/2appends a closure's captures as the final argument, soupdateanddrawstill land on the right predicates.
picturehasdisplay/clear/flush/background/foreground/pen(int)and counts as atoplevelframe forpce_loop.- Use retained objects (
box/circle/ellipse/arc/line/text/path) withfill_pattern/colour/pen/font+display. Verified safe. - Never call device
draw_*,clip,graph_device,push_state,pointer, ordestroyon an opened window from outside the event loop: some crash the process instead of failing. circle(R)/ellipse(W,H)take bounding-box sizes: pass diameter (2*radius) and display at(x-r, y-r)(verified:circle(30)reads back radius 15;ellipse(160,80)reads back 160x80).- A
box/2with nofill_patterndraws nothing but a default-pen outline, so it disappears against a dark background. This is how a misrouted'fill'mode presents as "nothing is drawn" rather than as an error. - Colours:
new(C, colour(@default, R, G, B)). - Loop entry must be
pce_main_loop(BootGoal)(fromlibrary(pce_main)) where the boot goal creates the window:pce_looponly dispatches frames created by that goal, and it callscall(Goal, Argv)(so the goal needs exactly one extra argument). - Closing the window halts via
done_message→message(@prolog, halt).space_xpce_quit/1halts for the same reason: it used to only retract the window fact, which leftpce_main_loopdispatching against a missing window — a frozen orphan window and a process that had to be killed by hand.
-
--main,--xpceand--exeall share one entry template, and each records the library/source templates at build time (exe-dir-relative../libsbreaks when the exe is not in a subdirectory) and runsabolish_all_tablesfirst (the tabled module loader otherwise poisons standalone runs). -
That same template appends
ensure_loaded(.../libs/space_xpce_prolog.pl), but only when the compiled output containscosmos_require("space— the compiler detects a Space program by that string. Consequence: a Space program compiled with no entry flag has no entry point at all, so it cannot load the backend and dies withUnknown procedure: space_xpce_…. -
Three entry kinds, chosen by flag (
compiler_add_entry/3):main— callModule:main/1with the argv list. Implied by--exe.xpce— call the module goal itself, i.e. run the file's top-level goals.none— bare compile, no entry point, not runnable.
xpceexists so a program can be written header-only: a top-levelrequire('space', sp)capturesspinto every relation, which would turnmain/1intomain/2and leave the--mainargv adapter calling it with the wrong arity. Running the top-level goals avoids namingmainat all, sorequire,sp.initandsp.startcan sit at the top of the file. It is not a different renderer — same window, same loop. -
load_anyuses the non-tabledcloadfor the same reason. -
compiler_stage_xpce_dlls/2copies the probedpl2xpceclosure (SDL3/cairo/pango/glib/fontconfig/freetype stack +libswipl.dll, resolved from the running SWI installation viape_imports.py-verified import tables) next to Space exes. Plain console exes are unaffected. The exe dir is derived from the.ploutput path: SWI path predicates only treat/as a separator, so backslash spellings are absolutized first (a barefile_directory_nameonD:\...\x.exereturns.). -
The backend
.plis loaded at runtime, never embedded (see above).
swipl -q -s compiler/platform/cli.pl -- misc/simple_xpce.co misc/simple_xpce.pl --main --exe
misc/simple_xpce.exe # window "Cosmos Space", draws, loop runs
Same for misc/demo_space_xpce.co (60 fps animated demo, no stderr output).
node --test tests/compiler/compiler.test.mjs: 26 pass / 1 fail (the fail
is the pre-existing v2 contract case, reproduced on the pristine tree).
The two JS-canvas/WASM tests live in their own bundle,
node --test tests/canvas/canvas.test.mjs (make test-canvas), which
skips cleanly when canvas/prolog-wasm/swipl-bundle.js is absent — so
make test no longer depends on the browser side. The build audit fixed
4 of the original 7 failures (--exe DLL staging; trace, compile_query
and -l -q exit codes via the repl halt-ball fix).