Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #ifndef TELESCOPE_H
18 #define TELESCOPE_H
20 #include "cmd.h"
21 #include "compat.h"
22 #include "phos/phos.h"
24 #include <event.h>
26 #define MIN(a, b) ((a) < (b) ? (a) : (b))
27 #define MAX(a, b) ((a) > (b) ? (a) : (b))
29 #define GEMINI_URL_LEN 1024
31 enum imsg_type {
32 /* ui <-> client/fs */
33 IMSG_GET, /* data is URL, peerid the tab id */
34 IMSG_ERR,
35 IMSG_CHECK_CERT,
36 IMSG_CERT_STATUS,
37 IMSG_GOT_CODE,
38 IMSG_GOT_META,
39 IMSG_PROCEED,
40 IMSG_STOP,
41 IMSG_BUF,
42 IMSG_EOF,
43 IMSG_QUIT,
45 /* ui <-> fs */
46 IMSG_BOOKMARK_PAGE,
47 IMSG_BOOKMARK_OK,
48 IMSG_SAVE_CERT,
49 IMSG_SAVE_CERT_OK,
50 IMSG_UPDATE_CERT,
51 IMSG_UPDATE_CERT_OK,
53 IMSG_FILE_OPEN,
54 IMSG_FILE_OPENED,
56 IMSG_SESSION_START,
57 IMSG_SESSION_TAB,
58 IMSG_SESSION_END,
59 };
61 extern char *new_tab_url;
62 extern int fill_column;
63 extern int olivetti_mode;
64 extern int enable_colors;
66 struct lineprefix {
67 const char *prfx1;
68 const char *prfx2;
69 };
70 extern struct lineprefix line_prefixes[];
72 struct line_face {
73 int prefix_prop;
74 int text_prop;
75 int trail_prop;
76 };
77 extern struct line_face line_faces[];
79 struct tab_face {
80 int background, tab, current_tab;
81 };
82 extern struct tab_face tab_face;
84 struct body_face {
85 int bg, fg;
86 int body;
87 };
88 extern struct body_face body_face;
90 struct modeline_face {
91 int background;
92 };
93 extern struct modeline_face modeline_face;
95 struct minibuffer_face {
96 int background;
97 };
98 extern struct minibuffer_face minibuffer_face;
100 enum line_type {
101 LINE_TEXT,
102 LINE_LINK,
103 LINE_TITLE_1,
104 LINE_TITLE_2,
105 LINE_TITLE_3,
106 LINE_ITEM,
107 LINE_QUOTE,
108 LINE_PRE_START,
109 LINE_PRE_CONTENT,
110 LINE_PRE_END,
111 };
113 struct line {
114 enum line_type type;
115 char *line;
116 char *alt;
117 int flags;
118 TAILQ_ENTRY(line) lines;
119 };
121 struct vline {
122 const struct line *parent;
123 char *line;
124 int flags;
125 TAILQ_ENTRY(vline) vlines;
126 };
128 struct parser;
129 struct page;
131 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
132 typedef int (*parserfreefn)(struct parser*);
134 typedef void (imsg_handlerfn)(struct imsg*, size_t);
136 struct parser {
137 const char *name;
138 char title[32+1];
139 char *buf;
140 size_t len;
141 size_t cap;
142 int flags;
143 parsechunkfn parse;
144 parserfreefn free;
146 TAILQ_HEAD(, line) head;
147 };
149 /*
150 * differnt types of trust for a certificate. Following
151 * gemini://thfr.info/gemini/modified-trust-verify.gmi
152 */
153 enum trust_state {
154 TS_UNKNOWN,
155 TS_UNTRUSTED,
156 TS_TRUSTED,
157 TS_VERIFIED,
158 };
160 struct tofu_entry {
161 char domain[GEMINI_URL_LEN];
163 /*
164 * enough space for ``PROTO:HASH''. probably isn't a good
165 * idea tho.
166 */
167 char hash[128+1];
168 int verified;
169 };
171 struct histhead {
172 TAILQ_HEAD(mhisthead, hist) head;
173 size_t len;
174 };
175 struct hist {
176 char h[1025];
177 TAILQ_ENTRY(hist) entries;
178 };
180 struct buffer {
181 struct parser page;
183 size_t last_line_off;
184 int force_redraw;
186 int curs_x;
187 int curs_y;
188 size_t line_off;
189 size_t line_max;
190 struct vline *current_line;
191 size_t cpoff;
192 TAILQ_HEAD(vhead, vline) head;
193 };
195 #define TAB_CURRENT 0x1
196 #define TAB_URGENT 0x2
198 #define NEW_TAB_URL "about:new"
200 extern TAILQ_HEAD(tabshead, tab) tabshead;
201 struct tab {
202 TAILQ_ENTRY(tab) tabs;
203 uint32_t id;
204 uint32_t flags;
206 char *cert;
207 enum trust_state trust;
208 struct phos_uri uri;
209 struct histhead hist;
210 struct hist *hist_cur;
211 size_t hist_off;
213 int code;
214 char meta[GEMINI_URL_LEN];
215 int redirect_count;
217 struct buffer buffer;
219 short loading_anim;
220 short loading_anim_step;
221 struct event loadingev;
223 int fd;
224 size_t bytes;
225 char *path;
226 };
228 struct proto {
229 const char *schema;
231 /*
232 * should load the given url in the tab. Optionally, it may
233 * consider the given url as relative to the one already
234 * present in tab. It must set tab->urlstr to a serialized
235 * human-friendly URL.
236 */
237 void (*loadfn)(struct tab*, const char*);
238 };
240 struct kmap {
241 TAILQ_HEAD(map, keymap) m;
242 void (*unhandled_input)(void);
243 };
245 struct keymap {
246 int meta;
247 int key;
248 struct kmap map;
249 void (*fn)(struct buffer*);
251 TAILQ_ENTRY(keymap) keymaps;
252 };
254 /* defaults.c */
255 int config_setprfx(const char *, const char *, const char *);
256 int config_setvari(const char *, int);
257 int config_setvars(const char *, char *);
258 int config_setcolor(int, const char *, int, int, int);
259 void config_apply_colors(void);
261 /* fs.c */
262 int fs_init(void);
263 int fs_main(struct imsgbuf*);
264 int load_certs(struct ohash*);
265 int load_last_session(void(*)(const char*));
267 /* gemini.c */
268 int client_main(struct imsgbuf*);
270 /* gemtext.c */
271 void gemtext_initparser(struct parser*);
273 /* hist.c */
274 void hist_clear_forward(struct histhead*, struct hist*);
275 void hist_push(struct histhead*, struct hist*);
277 /* keymap.c */
278 int kbd(const char*);
279 const char *unkbd(int);
280 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
282 /* mime.c */
283 int setup_parser_for(struct tab*);
285 /* pages.c */
286 extern const char *about_about;
287 extern const char *about_blank;
288 extern const char *about_help;
289 extern const char *about_new;
291 #define CANNOT_FETCH 0
292 #define TOO_MUCH_REDIRECTS 1
293 #define MALFORMED_RESPONSE 2
294 #define UNKNOWN_TYPE_OR_CSET 3
295 extern const char *err_pages[70];
297 /* parse.y */
298 void parseconfig(const char *, int);
300 /* parser.c */
301 int parser_append(struct parser*, const char*, size_t);
302 int parser_set_buf(struct parser*, const char*, size_t);
303 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
305 /* sandbox.c */
306 void sandbox_network_process(void);
307 void sandbox_ui_process(void);
308 void sandbox_fs_process(void);
310 /* telescope.c */
311 void load_about_url(struct tab*, const char*);
312 void load_gemini_url(struct tab*, const char*);
313 void load_url(struct tab*, const char*);
314 int load_previous_page(struct tab*);
315 int load_next_page(struct tab*);
316 void stop_tab(struct tab*);
317 void add_to_bookmarks(const char*);
318 void save_session(void);
320 /* textplain.c */
321 void textplain_initparser(struct parser*);
323 /* tofu.c */
324 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
325 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
326 void tofu_add(struct ohash*, struct tofu_entry*);
327 void tofu_update(struct ohash*, struct tofu_entry*);
329 /* ui.c */
330 extern int body_lines;
331 extern int body_cols;
332 extern int in_minibuffer;
334 enum pairs {
335 PBODY = 1,
336 PT,
337 PT_PRFX,
338 PT_TRAIL,
339 PL,
340 PL_PRFX,
341 PL_TRAIL,
342 PT1,
343 PT1_PRFX,
344 PT1_TRAIL,
345 PT2,
346 PT2_PRFX,
347 PT2_TRAIL,
348 PT3,
349 PT3_PRFX,
350 PT3_TRAIL,
351 PI,
352 PI_PRFX,
353 PI_TRAIL,
354 PQ,
355 PQ_PRFX,
356 PQ_TRAIL,
357 PPSTART,
358 PPSTART_PRFX,
359 PPSTART_TRAIL,
360 PP,
361 PP_PRFX,
362 PP_TRAIL,
363 PPEND,
364 PPEND_PRFX,
365 PPEND_TRAIL,
366 };
368 struct thiskey {
369 short meta;
370 int key;
371 uint32_t cp;
372 };
373 extern struct thiskey thiskey;
375 extern struct histhead eecmd_history,
376 ir_history,
377 lu_history,
378 read_history;
380 struct ministate {
381 char *curmesg;
383 char prompt[64];
384 void (*donefn)(void);
385 void (*abortfn)(void);
387 char buf[1025];
388 struct line line;
389 struct vline vline;
390 struct buffer buffer;
392 struct histhead *history;
393 struct hist *hist_cur;
394 size_t hist_off;
395 };
396 extern struct ministate ministate;
398 void restore_cursor(struct buffer *);
399 void minibuffer_taint_hist(void);
400 void eecmd_self_insert(void);
401 void eecmd_select(void);
402 void ir_self_insert(void);
403 void ir_select(void);
404 void lu_self_insert(void);
405 void lu_select(void);
406 void bp_select(void);
407 void vmessage(const char*, va_list);
408 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
409 void start_loading_anim(struct tab *tab);
410 void load_url_in_tab(struct tab *, const char *);
411 void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead *);
412 void exit_minibuffer(void);
413 void switch_to_tab(struct tab *);
414 struct tab *current_tab(void);
415 struct tab *new_tab(const char *);
416 unsigned int tab_new_id(void);
417 int ui_init(int, char * const*);
418 void ui_on_tab_loaded(struct tab*);
419 void ui_on_tab_refresh(struct tab*);
420 const char *ui_keyname(int);
421 void ui_toggle_side_window(void);
422 void ui_schedule_redraw(void);
423 void ui_require_input(struct tab*, int);
424 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
425 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
426 void ui_end(void);
428 /* utf.8 */
429 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
430 size_t utf8_encode(uint32_t, char*);
431 char *utf8_nth(char*, size_t);
432 size_t utf8_cplen(char*);
433 size_t utf8_chwidth(uint32_t);
434 size_t utf8_snwidth(const char*, size_t);
435 size_t utf8_swidth(const char*);
436 size_t utf8_swidth_between(const char*, const char*);
437 char *utf8_next_cp(const char*);
438 char *utf8_prev_cp(const char*, const char*);
440 /* util.c */
441 int mark_nonblock(int);
442 int has_prefix(const char*, const char*);
443 int unicode_isspace(uint32_t);
444 int unicode_isgraph(uint32_t);
445 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
447 /* wrap.c */
448 void empty_linelist(struct buffer*);
449 void empty_vlist(struct buffer*);
450 int wrap_text(struct buffer*, const char*, struct line*, size_t);
451 int hardwrap_text(struct buffer*, struct line*, size_t);
453 #endif /* TELESCOPE_H */