nandi/jolt-nativepublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

ffi.clj · 144 lines · 7.0 KBClojure Blame HistoryRaw
Bring vidya in cfd3e36 nandi 19d ago1(ns glimmer-vidya.ffi
2 "Raw bindings for Vidya's retained-tree ABI (`ffi/include/vidya_tree.h`).
3
4 Nothing here interprets a prop or an event; that is
5 `glimmer-vidya.core`'s job. This namespace exists so the boundary is one
6 readable list of symbols, and so the marshalling rules that come with it —
7 which pointers die when, which calls must stay on the window thread — are
8 stated once.
9
10 **Strings returned by this library are borrowed.** `node-get-str`,
11 `event-name` and `event-text` all answer the same scratch buffer, which the
12 next such call overwrites. Jolt copies a `:string` return into a Scheme
13 string as it crosses, so holding the value is safe; holding the *pointer* is
14 not, and there is no way to do that from here anyway.
15
16 **Every call belongs to the thread that opened the window.** The library
17 keeps its state in thread-local storage, so a call from anywhere else is
18 inert rather than unsound — but it is still a bug. `glimmer-vidya.core`
19 routes off-thread work through glimmer's `schedule`."
20 (:require [jolt.ffi :as ffi]))
21
22;; --- window and frame (shared with the push/pop ABI) -------------------------
23(ffi/defcfn raw-open "vidya_open" [:int :int :string] :int)
24(ffi/defcfn close! "vidya_close" [] :void)
25(ffi/defcfn raw-should-close "vidya_should_close" [] :int)
Let the window be renamed after it opens 5adddc7 nandi 19d ago26(ffi/defcfn set-title! "vidya_set_title" [:string] :void)
Bring vidya in cfd3e36 nandi 19d ago27(ffi/defcfn set-target-fps! "vidya_set_target_fps" [:int] :void)
28(ffi/defcfn set-mode! "vidya_set_mode" [:int] :void)
29(ffi/defcfn raw-load-font "vidya_load_font" [:string :int] :int)
30
31;; --- the tree ----------------------------------------------------------------
32(ffi/defcfn tree-root "vidya_tree_root" [] :int)
33(ffi/defcfn node-new "vidya_node_new" [:string] :int)
34(ffi/defcfn node-free! "vidya_node_free" [:int] :void)
35(ffi/defcfn raw-node-exists "vidya_node_exists" [:int] :int)
36
37(ffi/defcfn node-set-str! "vidya_node_set_str" [:int :string :string] :void)
38(ffi/defcfn node-set-num! "vidya_node_set_num" [:int :string :double] :void)
39(ffi/defcfn raw-node-set-bool "vidya_node_set_bool" [:int :string :int] :void)
40(ffi/defcfn node-clear-props! "vidya_node_clear_props" [:int] :void)
41
42(ffi/defcfn node-get-str "vidya_node_get_str" [:int :string] :string)
43(ffi/defcfn node-get-num "vidya_node_get_num" [:int :string] :double)
44(ffi/defcfn raw-node-get-bool "vidya_node_get_bool" [:int :string] :int)
45
46(ffi/defcfn node-tag "vidya_node_tag" [:int] :string)
47(ffi/defcfn node-child-count "vidya_node_child_count" [:int] :int)
48(ffi/defcfn node-child-at "vidya_node_child_at" [:int :int] :int)
49(ffi/defcfn tree-dump "vidya_tree_dump" [:int] :string)
50
51(ffi/defcfn raw-node-append "vidya_node_append" [:int :int] :int)
52(ffi/defcfn node-remove! "vidya_node_remove" [:int :int] :void)
53(ffi/defcfn raw-node-insert-after "vidya_node_insert_after" [:int :int :int] :int)
54(ffi/defcfn raw-node-replace "vidya_node_replace" [:int :int :int] :int)
55
56;; --- frame and events --------------------------------------------------------
57(ffi/defcfn tree-frame! "vidya_tree_frame" [] :void)
58(ffi/defcfn raw-poll-event "vidya_tree_poll_event" [] :int)
59(ffi/defcfn event-node "vidya_tree_event_node" [] :int)
60(ffi/defcfn event-name "vidya_tree_event_name" [] :string)
61(ffi/defcfn event-text "vidya_tree_event_text" [] :string)
62(ffi/defcfn event-num "vidya_tree_event_num" [] :double)
63
Tell a caller how big the window is 42dabb0 nandi 19d ago64
65;; --- the window --------------------------------------------------------------
66;; Its size in points, following the window as it is dragged. Points rather
67;; than pixels because a caller asking is about to lay something out.
68(ffi/defcfn screen-width "vidya_screen_width" [] :float)
69(ffi/defcfn screen-height "vidya_screen_height" [] :float)
70
Bring vidya in cfd3e36 nandi 19d ago71;; --- live frames -------------------------------------------------------------
72;; Pixels pushed in under a name, painted by an `:image` whose `:feed` names it.
73;; `rgba` is a foreign pointer to width*height*4 bytes, copied before the call
74;; returns — the backend keeps nothing, so the caller may reuse its buffer. The
75;; pointer form is the point: a video frame moved through jolt as a byte array
76;; would be copied twice a frame for nothing.
77(ffi/defcfn raw-frame-rgba "vidya_frame_rgba" [:string :int :int :pointer] :int)
78(ffi/defcfn raw-frame-drop "vidya_frame_drop" [:string] :int)
79
80;; --- the clipboard -----------------------------------------------------------
81;; Not an event: egui carries clipboard text into a frame and nothing else, so a
82;; pasted picture is asked for rather than waited for.
83(ffi/defcfn raw-clipboard-image-png "vidya_clipboard_image_png" [:string] :int)
84
85;; --- the browser -------------------------------------------------------------
86;; Leaving for a page and coming back is the platform's business: xdg-open on a
87;; desktop, an ACTION_VIEW intent on Android.
88(ffi/defcfn raw-open-url "vidya_open_url" [:string] :int)
89
Catch up with vidya c90f8af nandi 19d ago90;; --- the platform's picture chooser ------------------------------------------
91;; Opened here, answered later: the reader leaves for a screen of the system's
92;; own, so the pick is polled for rather than returned.
93(ffi/defcfn raw-pick-image "vidya_pick_image" [] :int)
94(ffi/defcfn raw-picked-image "vidya_picked_image" [:string] :int)
95
Bring vidya in cfd3e36 nandi 19d ago96;; --- the int/bool seam -------------------------------------------------------
97;; C has no booleans; every predicate here crosses as 0 or 1. Converting at the
98;; binding rather than at each call site keeps the rest of the backend written
99;; in jolt's own truthiness.
100
101(def dark-mode 0)
102(def light-mode 1)
103
104(defn open!
105 [width height title]
106 (not (zero? (raw-open width height title))))
107
108(defn should-close? [] (not (zero? (raw-should-close))))
109(defn load-font! [path] (not (zero? (raw-load-font path 32))))
110(defn node-exists? [node] (not (zero? (raw-node-exists node))))
111(defn node-set-bool! [node key value] (raw-node-set-bool node key (if value 1 0)))
112(defn node-get-bool [node key] (not (zero? (raw-node-get-bool node key))))
113(defn node-append! [parent child] (not (zero? (raw-node-append parent child))))
114(defn clipboard-image-png!
115 "Write the clipboard's picture to `path` as a PNG; true when there was one."
116 [path]
117 (not (zero? (raw-clipboard-image-png path))))
118(defn open-url!
119 "Hand `url` to the platform's browser; true when something took it."
120 [url]
121 (not (zero? (raw-open-url url))))
Catch up with vidya c90f8af nandi 19d ago122(defn pick-image!
123 "Open the platform's picture chooser; true when one opened."
124 []
125 (not (zero? (raw-pick-image))))
126(defn picked-image!
127 "Move the picture chosen since the last call to `path`; true when there was
128 one."
129 [path]
130 (not (zero? (raw-picked-image path))))
Bring vidya in cfd3e36 nandi 19d ago131(defn frame-rgba!
132 "Paint `rgba` — a pointer to width*height*4 un-premultiplied bytes — under
133 `key`. True when the frame was accepted; false for dimensions the length does
134 not match."
135 [key width height rgba]
136 (not (zero? (raw-frame-rgba key width height rgba))))
137(defn frame-drop!
138 "Forget the feed named `key`; true when there was one."
139 [key]
140 (not (zero? (raw-frame-drop key))))
141(defn node-insert-after! [parent child sibling]
142 (not (zero? (raw-node-insert-after parent child sibling))))
143(defn node-replace! [parent old new] (not (zero? (raw-node-replace parent old new))))
144(defn poll-event! [] (not (zero? (raw-poll-event))))