nandi/jolt-nativepublic Fork 0
fc16d8e4f1cf0b5245d833bf4521e99d73b72df4
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.jolt · 121 lines · 6.0 KBGDScript3 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)
26(ffi/defcfn set-target-fps! "vidya_set_target_fps" [:int] :void)
27(ffi/defcfn set-mode! "vidya_set_mode" [:int] :void)
28(ffi/defcfn raw-load-font "vidya_load_font" [:string :int] :int)
29
30;; --- the tree ----------------------------------------------------------------
31(ffi/defcfn tree-root "vidya_tree_root" [] :int)
32(ffi/defcfn node-new "vidya_node_new" [:string] :int)
33(ffi/defcfn node-free! "vidya_node_free" [:int] :void)
34(ffi/defcfn raw-node-exists "vidya_node_exists" [:int] :int)
35
36(ffi/defcfn node-set-str! "vidya_node_set_str" [:int :string :string] :void)
37(ffi/defcfn node-set-num! "vidya_node_set_num" [:int :string :double] :void)
38(ffi/defcfn raw-node-set-bool "vidya_node_set_bool" [:int :string :int] :void)
39(ffi/defcfn node-clear-props! "vidya_node_clear_props" [:int] :void)
40
41(ffi/defcfn node-get-str "vidya_node_get_str" [:int :string] :string)
42(ffi/defcfn node-get-num "vidya_node_get_num" [:int :string] :double)
43(ffi/defcfn raw-node-get-bool "vidya_node_get_bool" [:int :string] :int)
44
45(ffi/defcfn node-tag "vidya_node_tag" [:int] :string)
46(ffi/defcfn node-child-count "vidya_node_child_count" [:int] :int)
47(ffi/defcfn node-child-at "vidya_node_child_at" [:int :int] :int)
48(ffi/defcfn tree-dump "vidya_tree_dump" [:int] :string)
49
50(ffi/defcfn raw-node-append "vidya_node_append" [:int :int] :int)
51(ffi/defcfn node-remove! "vidya_node_remove" [:int :int] :void)
52(ffi/defcfn raw-node-insert-after "vidya_node_insert_after" [:int :int :int] :int)
53(ffi/defcfn raw-node-replace "vidya_node_replace" [:int :int :int] :int)
54
55;; --- frame and events --------------------------------------------------------
56(ffi/defcfn tree-frame! "vidya_tree_frame" [] :void)
57(ffi/defcfn raw-poll-event "vidya_tree_poll_event" [] :int)
58(ffi/defcfn event-node "vidya_tree_event_node" [] :int)
59(ffi/defcfn event-name "vidya_tree_event_name" [] :string)
60(ffi/defcfn event-text "vidya_tree_event_text" [] :string)
61(ffi/defcfn event-num "vidya_tree_event_num" [] :double)
62
63;; --- live frames -------------------------------------------------------------
64;; Pixels pushed in under a name, painted by an `:image` whose `:feed` names it.
65;; `rgba` is a foreign pointer to width*height*4 bytes, copied before the call
66;; returns the backend keeps nothing, so the caller may reuse its buffer. The
67;; pointer form is the point: a video frame moved through jolt as a byte array
68;; would be copied twice a frame for nothing.
69(ffi/defcfn raw-frame-rgba "vidya_frame_rgba" [:string :int :int :pointer] :int)
70(ffi/defcfn raw-frame-drop "vidya_frame_drop" [:string] :int)
71
72;; --- the clipboard -----------------------------------------------------------
73;; Not an event: egui carries clipboard text into a frame and nothing else, so a
74;; pasted picture is asked for rather than waited for.
75(ffi/defcfn raw-clipboard-image-png "vidya_clipboard_image_png" [:string] :int)
76
77;; --- the browser -------------------------------------------------------------
78;; Leaving for a page and coming back is the platform's business: xdg-open on a
79;; desktop, an ACTION_VIEW intent on Android.
80(ffi/defcfn raw-open-url "vidya_open_url" [:string] :int)
81
82;; --- the int/bool seam -------------------------------------------------------
83;; C has no booleans; every predicate here crosses as 0 or 1. Converting at the
84;; binding rather than at each call site keeps the rest of the backend written
85;; in jolt's own truthiness.
86
87(def dark-mode 0)
88(def light-mode 1)
89
90(defn open!
91 [width height title]
92 (not (zero? (raw-open width height title))))
93
94(defn should-close? [] (not (zero? (raw-should-close))))
95(defn load-font! [path] (not (zero? (raw-load-font path 32))))
96(defn node-exists? [node] (not (zero? (raw-node-exists node))))
97(defn node-set-bool! [node key value] (raw-node-set-bool node key (if value 1 0)))
98(defn node-get-bool [node key] (not (zero? (raw-node-get-bool node key))))
99(defn node-append! [parent child] (not (zero? (raw-node-append parent child))))
100(defn clipboard-image-png!
101 "Write the clipboard's picture to `path` as a PNG; true when there was one."
102 [path]
103 (not (zero? (raw-clipboard-image-png path))))
104(defn open-url!
105 "Hand `url` to the platform's browser; true when something took it."
106 [url]
107 (not (zero? (raw-open-url url))))
108(defn frame-rgba!
109 "Paint `rgba` — a pointer to width*height*4 un-premultiplied bytes — under
110 `key`. True when the frame was accepted; false for dimensions the length does
111 not match."
112 [key width height rgba]
113 (not (zero? (raw-frame-rgba key width height rgba))))
114(defn frame-drop!
115 "Forget the feed named `key`; true when there was one."
116 [key]
117 (not (zero? (raw-frame-drop key))))
118(defn node-insert-after! [parent child sibling]
119 (not (zero? (raw-node-insert-after parent child sibling))))
120(defn node-replace! [parent old new] (not (zero? (raw-node-replace parent old new))))
121(defn poll-event! [] (not (zero? (raw-poll-event))))