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_GET_RAW, /* get but with an explicit req str */
35 IMSG_ERR,
36 IMSG_CHECK_CERT,
37 IMSG_CERT_STATUS,
38 IMSG_GOT_CODE,
39 IMSG_GOT_META,
40 IMSG_PROCEED,
41 IMSG_STOP,
42 IMSG_BUF,
43 IMSG_EOF,
44 IMSG_QUIT,
46 /* ui <-> fs */
47 IMSG_BOOKMARK_PAGE,
48 IMSG_BOOKMARK_OK,
49 IMSG_SAVE_CERT,
50 IMSG_SAVE_CERT_OK,
51 IMSG_UPDATE_CERT,
52 IMSG_UPDATE_CERT_OK,
54 IMSG_FILE_OPEN,
55 IMSG_FILE_OPENED,
57 IMSG_SESSION_START,
58 IMSG_SESSION_TAB,
59 IMSG_SESSION_END,
60 };
62 extern char *new_tab_url;
63 extern int fill_column;
64 extern int olivetti_mode;
65 extern int enable_colors;
67 struct lineprefix {
68 const char *prfx1;
69 const char *prfx2;
70 };
71 extern struct lineprefix line_prefixes[];
73 struct line_face {
74 int prfx_pair, pair, trail_pair;
75 int prfx_bg, bg, trail_bg;
76 int prfx_fg, fg, trail_fg;
77 int prfx_attr, attr, trail_attr;
79 int prefix, text, trail;
80 };
81 extern struct line_face line_faces[];
83 struct tab_face {
84 int bg_attr, bg_bg, bg_fg;
85 int t_attr, t_bg, t_fg;
86 int c_attr, c_bg, c_fg;
88 int background, tab, current;
89 };
90 extern struct tab_face tab_face;
92 struct body_face {
93 int lbg, lfg;
94 int bg, fg;
95 int rbg, rfg;
97 int left, body, right;
98 };
99 extern struct body_face body_face;
101 struct modeline_face {
102 int background;
103 };
104 extern struct modeline_face modeline_face;
106 struct minibuffer_face {
107 int background;
108 };
109 extern struct minibuffer_face minibuffer_face;
111 enum line_type {
112 LINE_TEXT,
113 LINE_LINK,
114 LINE_TITLE_1,
115 LINE_TITLE_2,
116 LINE_TITLE_3,
117 LINE_ITEM,
118 LINE_QUOTE,
119 LINE_PRE_START,
120 LINE_PRE_CONTENT,
121 LINE_PRE_END,
122 };
124 struct line {
125 enum line_type type;
126 char *line;
127 char *alt;
128 int flags;
129 TAILQ_ENTRY(line) lines;
130 };
132 struct vline {
133 const struct line *parent;
134 char *line;
135 int flags;
136 TAILQ_ENTRY(vline) vlines;
137 };
139 struct parser;
140 struct page;
142 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
143 typedef int (*parserfreefn)(struct parser*);
145 typedef void (imsg_handlerfn)(struct imsg*, size_t);
147 struct parser {
148 const char *name;
149 char title[32+1];
150 char *buf;
151 size_t len;
152 size_t cap;
153 int flags;
154 parsechunkfn parse;
155 parserfreefn free;
157 TAILQ_HEAD(, line) head;
158 };
160 /*
161 * differnt types of trust for a certificate. Following
162 * gemini://thfr.info/gemini/modified-trust-verify.gmi
163 */
164 enum trust_state {
165 TS_UNKNOWN,
166 TS_UNTRUSTED,
167 TS_TRUSTED,
168 TS_VERIFIED,
169 };
171 struct tofu_entry {
172 char domain[GEMINI_URL_LEN];
174 /*
175 * enough space for ``PROTO:HASH''. probably isn't a good
176 * idea tho.
177 */
178 char hash[128+1];
179 int verified;
180 };
182 struct histhead {
183 TAILQ_HEAD(mhisthead, hist) head;
184 size_t len;
185 };
186 struct hist {
187 char h[1025];
188 TAILQ_ENTRY(hist) entries;
189 };
191 struct buffer {
192 struct parser page;
194 size_t last_line_off;
195 int force_redraw;
197 int curs_x;
198 int curs_y;
199 size_t line_off;
200 size_t line_max;
201 struct vline *current_line;
202 size_t cpoff;
203 TAILQ_HEAD(vhead, vline) head;
204 };
206 #define TAB_CURRENT 0x1
207 #define TAB_URGENT 0x2
209 #define NEW_TAB_URL "about:new"
211 extern TAILQ_HEAD(tabshead, tab) tabshead;
212 struct tab {
213 TAILQ_ENTRY(tab) tabs;
214 uint32_t id;
215 uint32_t flags;
217 char *cert;
218 enum trust_state trust;
219 struct proxy *proxy;
220 struct phos_uri uri;
221 struct histhead hist;
222 struct hist *hist_cur;
223 size_t hist_off;
225 int code;
226 char meta[GEMINI_URL_LEN];
227 int redirect_count;
229 struct buffer buffer;
231 short loading_anim;
232 short loading_anim_step;
233 struct event loadingev;
235 int fd;
236 size_t bytes;
237 char *path;
238 };
240 struct proto {
241 const char *schema;
243 /*
244 * should load the given url in the tab. Optionally, it may
245 * consider the given url as relative to the one already
246 * present in tab. It must set tab->urlstr to a serialized
247 * human-friendly URL.
248 */
249 void (*loadfn)(struct tab*, const char*);
250 };
252 extern TAILQ_HEAD(proxylist, proxy) proxies;
253 struct proxy {
254 char *match_proto;
256 char *host;
257 char *port;
258 int proto;
260 TAILQ_ENTRY(proxy) proxies;
261 };
263 enum {
264 PROTO_GEMINI,
265 /* ... */
266 };
268 struct get_req {
269 int proto;
270 char host[254];
271 char port[16];
272 char req[1027];
273 };
275 struct kmap {
276 TAILQ_HEAD(map, keymap) m;
277 void (*unhandled_input)(void);
278 };
280 struct keymap {
281 int meta;
282 int key;
283 struct kmap map;
284 void (*fn)(struct buffer*);
286 TAILQ_ENTRY(keymap) keymaps;
287 };
289 /* defaults.c */
290 void config_init(void);
291 int config_setprfx(const char *, const char *, const char *);
292 int config_setvari(const char *, int);
293 int config_setvars(const char *, char *);
294 int config_setcolor(int, const char *, int, int, int);
295 int config_setattr(const char *, int, int, int);
296 void config_apply_style(void);
298 /* fs.c */
299 int fs_init(void);
300 int fs_main(struct imsgbuf*);
301 int load_certs(struct ohash*);
302 int load_last_session(void(*)(const char*));
304 /* gemini.c */
305 int client_main(struct imsgbuf*);
307 /* gemtext.c */
308 void gemtext_initparser(struct parser*);
310 /* hist.c */
311 void hist_clear_forward(struct histhead*, struct hist*);
312 void hist_push(struct histhead*, struct hist*);
314 /* keymap.c */
315 int kbd(const char*);
316 const char *unkbd(int);
317 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
319 /* mime.c */
320 int setup_parser_for(struct tab*);
322 /* pages.c */
323 extern const char *about_about;
324 extern const char *about_blank;
325 extern const char *about_help;
326 extern const char *about_new;
328 #define CANNOT_FETCH 0
329 #define TOO_MUCH_REDIRECTS 1
330 #define MALFORMED_RESPONSE 2
331 #define UNKNOWN_TYPE_OR_CSET 3
332 extern const char *err_pages[70];
334 /* parse.y */
335 void parseconfig(const char *, int);
337 /* parser.c */
338 int parser_append(struct parser*, const char*, size_t);
339 int parser_set_buf(struct parser*, const char*, size_t);
340 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
342 /* sandbox.c */
343 void sandbox_network_process(void);
344 void sandbox_ui_process(void);
345 void sandbox_fs_process(void);
347 /* telescope.c */
348 void load_about_url(struct tab*, const char*);
349 void load_gemini_url(struct tab*, const char*);
350 void load_via_proxy(struct tab *, const char *, struct proxy *);
351 void load_url(struct tab*, const char*);
352 int load_previous_page(struct tab*);
353 int load_next_page(struct tab*);
354 void stop_tab(struct tab*);
355 void add_to_bookmarks(const char*);
356 void save_session(void);
358 /* textplain.c */
359 void textplain_initparser(struct parser*);
361 /* tofu.c */
362 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
363 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
364 void tofu_add(struct ohash*, struct tofu_entry*);
365 void tofu_update(struct ohash*, struct tofu_entry*);
367 /* ui.c */
368 extern int body_lines;
369 extern int body_cols;
370 extern int in_minibuffer;
372 struct excursion {
373 int curs_x, curs_y;
374 size_t line_off;
375 struct vline *current_line;
376 size_t cpoff;
377 };
379 enum pairs {
380 PTL_BG = 1,
381 PTL_TAB,
382 PTL_CURR,
384 PBODY,
385 PBLEFT,
386 PBRIGHT,
388 PT,
389 PT_PRFX,
390 PT_TRAIL,
391 PL,
392 PL_PRFX,
393 PL_TRAIL,
394 PT1,
395 PT1_PRFX,
396 PT1_TRAIL,
397 PT2,
398 PT2_PRFX,
399 PT2_TRAIL,
400 PT3,
401 PT3_PRFX,
402 PT3_TRAIL,
403 PI,
404 PI_PRFX,
405 PI_TRAIL,
406 PQ,
407 PQ_PRFX,
408 PQ_TRAIL,
409 PPSTART,
410 PPSTART_PRFX,
411 PPSTART_TRAIL,
412 PP,
413 PP_PRFX,
414 PP_TRAIL,
415 PPEND,
416 PPEND_PRFX,
417 PPEND_TRAIL,
418 };
420 struct thiskey {
421 short meta;
422 int key;
423 uint32_t cp;
424 };
425 extern struct thiskey thiskey;
427 extern struct histhead eecmd_history,
428 ir_history,
429 lu_history,
430 read_history;
432 struct ministate {
433 char *curmesg;
435 char prompt[64];
436 void (*donefn)(void);
437 void (*abortfn)(void);
439 char buf[1025];
440 struct line line;
441 struct vline vline;
442 struct buffer buffer;
444 struct histhead *history;
445 struct hist *hist_cur;
446 size_t hist_off;
447 };
448 extern struct ministate ministate;
450 void save_excursion(struct excursion *, struct buffer *);
451 void restore_excursion(struct excursion *, struct buffer *);
452 void restore_cursor(struct buffer *);
453 void minibuffer_taint_hist(void);
454 void eecmd_self_insert(void);
455 void eecmd_select(void);
456 void ir_self_insert(void);
457 void ir_select(void);
458 void lu_self_insert(void);
459 void lu_select(void);
460 void bp_select(void);
461 void vmessage(const char*, va_list);
462 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
463 void start_loading_anim(struct tab *tab);
464 void load_url_in_tab(struct tab *, const char *);
465 void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead *);
466 void exit_minibuffer(void);
467 void switch_to_tab(struct tab *);
468 struct tab *current_tab(void);
469 struct tab *new_tab(const char *);
470 unsigned int tab_new_id(void);
471 int ui_init(int, char * const*);
472 void ui_on_tab_loaded(struct tab*);
473 void ui_on_tab_refresh(struct tab*);
474 const char *ui_keyname(int);
475 void ui_toggle_side_window(void);
476 void ui_schedule_redraw(void);
477 void ui_require_input(struct tab*, int);
478 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
479 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
480 void ui_end(void);
482 /* utf.8 */
483 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
484 size_t utf8_encode(uint32_t, char*);
485 char *utf8_nth(char*, size_t);
486 size_t utf8_cplen(char*);
487 size_t utf8_chwidth(uint32_t);
488 size_t utf8_snwidth(const char*, size_t);
489 size_t utf8_swidth(const char*);
490 size_t utf8_swidth_between(const char*, const char*);
491 char *utf8_next_cp(const char*);
492 char *utf8_prev_cp(const char*, const char*);
494 /* util.c */
495 int mark_nonblock(int);
496 int has_prefix(const char*, const char*);
497 int unicode_isspace(uint32_t);
498 int unicode_isgraph(uint32_t);
499 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
501 /* wrap.c */
502 void empty_linelist(struct buffer*);
503 void empty_vlist(struct buffer*);
504 int wrap_text(struct buffer*, const char*, struct line*, size_t);
505 int hardwrap_text(struct buffer*, struct line*, size_t);
507 #endif /* TELESCOPE_H */