1// The C surface of crates/mobile; see its lib.rs and library.rs for each call's contract.
2#pragma once
3
4#include <stdbool.h>
5#include <stddef.h>
6#include <stdint.h>
7
8typedef struct Library Library;
9typedef struct Share Share;
10typedef struct Section Section;
11typedef struct View View;
12
13void sb_string_free(char *text);
14
15bool sb_crash_start(const char *path, const char *build, const char *system);
16const char *sb_crash_address(void);
17char *sb_crash_report(void);
18void sb_crash_forget(void);
19
20typedef void (*sb_coordinator)(const char *path, bool write, void (*body)(void *), void *context);
21void sb_set_coordinator(sb_coordinator coordinator);
22void sb_set_sync_wake(void (*wake)(void));
23typedef void (*sb_versions)(const char *path, void (*found)(void *, const char *id, const char *device), void *context);
24typedef bool (*sb_retire)(const char *path, const char *id, bool keep);
25void sb_set_versions(sb_versions versions, sb_retire retire, const char *device);
26char *sb_tags(void);
27char *sb_pens(uint32_t section);
28bool sb_tag_icon(uint16_t shape, bool checked, uint32_t pixels, uint8_t *rgba);
29
30// A document provider, as Android's ContentResolver reaches picked documents by URI. Each call
31// returns 0, or 1 while the provider can't be reached, 2 once the grant is revoked, 3 for a
32// document gone, anything else for another failure; size and modified (ms since 1970) are -1
33// where unknown. Every field is required. Calls come concurrently from the caller's thread, the
34// background sync thread and each section's worker, and the host attaches those threads to its
35// runtime (the JVM) itself; callbacks run only within the call that takes them. Ids and names
36// passed either way are non-null.
37typedef void (*sb_document_found)(void *context, const char *id, const char *name, bool directory, int64_t size,
38 int64_t modified, bool writable);
39typedef struct sb_provider {
40 int32_t (*document)(const char *id, sb_document_found found, void *context);
41 int32_t (*children)(const char *id, sb_document_found found, void *context);
42 // mode is "r", "rw", "wt" or "w"; fd is a duplicate Snowbound owns and closes, handle the descriptor
43 // the host keeps open: detachFd would end the channel checkError reads. A nonzero status
44 // sets neither; a negative fd with status 0 has its handle closed.
45 int32_t (*open)(const char *id, const char *mode, int32_t *fd, uint64_t *handle);
46 // Once fd is closed: the provider's error (checkError), then the host's close.
47 int32_t (*close)(uint64_t handle);
48 int32_t (*rename)(const char *id, const char *name, void (*renamed)(void *context, const char *id), void *context);
49} sb_provider;
50void sb_set_provider(const sb_provider *provider);
51
52const Library *sb_library_open(const char *path, const char *cache, bool local, char **error);
53const Library *sb_library_provider(const char *root, bool folder, bool local, const char *device, const char *cache,
54 char **error);
55const Library *sb_library_server(const char *address, const char *share, const char *user, const char *password,
56 const char *domain, const char *root, const char *cache, char **error);
57// Callback/token live until the blocking join returns; false cancels, approving reports host approval.
58typedef bool (*sb_join_wait)(uintptr_t token, bool approving);
59// Private credential/refusal JSON; persist credential before hosted open, then free with sb_string_free.
60char *sb_live_join(const char *author, const char *device, const char *code, const char *password,
61 const char *relay, bool local_network, sb_join_wait waiting, uintptr_t token);
62// Null relay disables relay, false local_network disables LAN; offline guards cached edits before first watch.
63const Library *sb_library_hosted(const char *credential, const char *author, const char *device,
64 const char *relay, bool local_network, bool offline, const char *cache, char **error);
65char *sb_library_live_status(const Library *library);
66bool sb_notebook_create(const char *path, const char *cache, const char *author, const char *date, const char *time,
67 char **error);
68bool sb_notebook_moved(const char *cache, const char *from, const char *to, char **error);
69bool sb_server_moved(const char *cache, const char *from_address, const char *from_share, const char *from_root,
70 const char *to_address, const char *to_share, const char *to_root, char **error);
71char *sb_library_sections(const Library *library);
72int32_t sb_library_color(const Library *library);
73char *sb_library_new_section(const Library *library, const char *folder, const char *name, const char *author,
74 const char *date, const char *time);
75char *sb_library_move_page(const Library *library, const char *from, const char *id, const char *to, const char *before,
76 uint32_t level, const char *discard, const char *author, const char *date, const char *time);
77char *sb_library_place(const Library *library, const char *path, const char *folder, const char *paths);
78char *sb_library_recycle_bin(const Library *library);
79void sb_library_purge_recycle_bin(const Library *library);
80bool sb_library_delete(const Library *library, const char *path);
81char *sb_search(const Library *const *libraries, size_t count, const char *section, const char *query);
82char *sb_library_tagged(const Library *library);
83bool sb_library_unlock(const Library *library, const char *path, const char *password);
84void sb_library_lock_all(const Library *library);
85char *sb_library_sync_status(const Library *library);
86void sb_library_set_offline(const Library *library, bool offline);
87void sb_library_touched(const Library *library, const char *path);
88void sb_library_sync_now(const Library *library);
89// Publishes, keeping offline copies; sb_library_close is Close Notebook and deletes them.
90bool sb_library_flush(const Library *library, double seconds, char **error);
91bool sb_library_stop(const Library *library, char **error);
92bool sb_library_close(const Library *library, double seconds, char **error);
93void sb_library_free(const Library *library);
94
95Share *sb_share_connect(const char *address, const char *share, const char *user, const char *password,
96 const char *domain, char **error);
97char *sb_share_list(const Share *share, const char *path);
98void sb_share_free(Share *share);
99
100typedef void (*sb_wake)(uintptr_t token);
101Section *sb_section_open(const Library *library, const char *path, const char *author, sb_wake wake,
102 uintptr_t token, char **error);
103char *sb_section_pages(const Section *section);
104uint32_t sb_section_poll(const Section *section, uint8_t *status);
105char *sb_section_new_page(const Section *section, const char *parent, const char *date, const char *time);
106bool sb_section_delete_page(const Section *section, const char *id, const char *date, const char *time);
107bool sb_section_arrange(const Section *section, const char *order, const char *moved);
108char *sb_section_themes(const Section *section, const char *id);
109bool sb_section_set_theme(const Section *section, const char *id, uint8_t scope, const char *theme);
110bool sb_section_flush(const Section *section, double seconds);
111void sb_section_wake(const Section *section);
112void sb_section_set_author(const Section *section, const char *author);
113void sb_section_free(Section *section);
114
115#if defined(__ANDROID__)
116struct ANativeWindow;
117// Acquires its own window reference; caller retains ownership of its reference.
118View *sb_view_new_android(struct ANativeWindow *window, const Section *section, const char *id, float width, float height, float scale);
119// Detach before SurfaceDestroyed returns; editor, selection and Undo remain alive.
120void sb_view_detach_android(View *view);
121// Attach at the last size/scale, then resize on SurfaceChanged; detach an attached view first.
122bool sb_view_attach_android(View *view, struct ANativeWindow *window);
123#else
124View *sb_view_new(void *layer, const Section *section, const char *id, float width, float height, float scale);
125#endif
126// Serialize all view calls; free only when the editor is no longer needed.
127void sb_view_free(View *view);
128// Dimensions are host points; scale is pixels per point. Invalid sizes leave the view unchanged.
129void sb_view_resize(View *view, float width, float height, float scale);
130bool sb_view_render(View *view);
131bool sb_view_frame_pending(const View *view);
132void sb_view_set_dark(View *view, bool dark);
133bool sb_view_read_only(const View *view);
134char *sb_view_reading(const View *view, float width);
135// Preserves the nearest source caret's host offset from the page-point anchor at scale 1.
136bool sb_view_set_reading(View *view, float width, float anchor[2]);
137bool sb_view_reload_ready(const View *view);
138// A non-null page-point anchor follows its source caret across the refresh.
139bool sb_view_reload(View *view, bool discard, float anchor[2]);
140bool sb_view_keep(const View *view);
141void sb_view_focus(View *view, bool focused);
142void sb_view_content(View *view, float bounds[4]);
143bool sb_view_block(const View *view, float x, float y, float rect[4]);
144void sb_view_set_transform(View *view, float zoom, float x, float y);
145uint8_t sb_view_target(const View *view, float x, float y);
146void sb_view_pressure(View *view, float pressure);
147void sb_set_snap_to_grid(bool on);
148void sb_set_default_font(const char *face, float size);
149void sb_set_markdown_shortcuts(bool on);
150bool sb_view_press(View *view, float x, float y);
151bool sb_view_drag(View *view, float x, float y);
152bool sb_view_release(View *view);
153bool sb_view_undo(View *view, bool redo);
154bool sb_view_can_undo(const View *view, bool redo);
155uint64_t sb_view_format(const View *view);
156bool sb_view_apply(View *view, uint8_t command);
157bool sb_view_highlight(View *view, int64_t color);
158bool sb_view_style(View *view, uint8_t place);
159int32_t sb_view_style_place(const View *view);
160char *sb_view_correction(const View *view);
161bool sb_view_correct(View *view, uint8_t command, const char *text);
162char *sb_view_title(const View *view);
163bool sb_view_focus_title(View *view);
164bool sb_view_find(View *view, const char *query);
165bool sb_view_select_paragraph(View *view, const char *id);
166char *sb_view_copy(View *view, bool cut);
167void sb_view_insert_space(View *view);
168void sb_view_set_tool(View *view, uint8_t tool, uint8_t detail, uint32_t section);
169bool sb_view_cancel(View *view);
170bool sb_view_ink_selection(const View *view, float rect[4]);
171char *sb_view_paper(const View *view);
172bool sb_view_set_paper(View *view, int16_t red, uint8_t green, uint8_t blue, int8_t ruled);
173bool sb_view_set_art(View *view, const char *name);
174uint8_t *sb_view_pdf(const View *view, float width, float height, const char *section, size_t *length);
175void sb_bytes_free(uint8_t *bytes, size_t length);
176int8_t sb_view_date_request(View *view, int64_t *seconds);
177bool sb_view_change_date(View *view, int64_t seconds, const char *date, const char *time);
178bool sb_view_insert_picture(View *view, const uint8_t *bytes, size_t length, float width, float height);
179
180typedef struct Movie Movie;
181uint32_t sb_recording_rate(void);
182bool sb_view_start_recording(View *view, bool video, const char *date, const char *time);
183int64_t sb_view_recording(const View *view, bool *paused);
184void sb_view_pause_recording(View *view, bool paused);
185bool sb_view_finish_recording(View *view, const uint8_t *bytes, size_t length, bool video);
186char *sb_view_play_request(View *view, const char *folder);
187bool sb_view_played(View *view, int64_t at_ms);
188Movie *sb_movie_new(void);
189void sb_movie_picture(Movie *movie, uint64_t at_us, const uint8_t *bgra, uint32_t width, uint32_t height,
190 size_t stride);
191void sb_movie_sound(Movie *movie, const uint8_t *pcm, size_t length);
192uint8_t *sb_movie_finish(Movie *movie, uint64_t duration_us, size_t *length);
193
194uint32_t sb_text_length(const View *view);
195char *sb_text(const View *view, uint32_t start, uint32_t end);
196void sb_selection(const View *view, uint32_t range[2]);
197bool sb_select(View *view, uint32_t start, uint32_t end);
198bool sb_select_more(View *view);
199bool sb_marked(const View *view, uint32_t range[2]);
200bool sb_set_marked(View *view, const char *text, uint32_t selected_start, uint32_t selected_end);
201void sb_unmark(View *view);
202bool sb_insert(View *view, const char *text);
203bool sb_replace(View *view, uint32_t start, uint32_t end, const char *text);
204bool sb_paste(View *view, const char *text, const char *language);
205bool sb_delete_backward(View *view);
206bool sb_caret_rect(const View *view, uint32_t offset, float rect[4]);
207size_t sb_range_rects(const View *view, uint32_t start, uint32_t end, float (*rects)[4], size_t capacity);
208uint32_t sb_closest(const View *view, float x, float y);