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 bg_attr, bg_bg, bg_fg;
81 int t_attr, t_bg, t_fg;
82 int c_attr, c_bg, c_fg;
84 int background, tab, current;
85 };
86 extern struct tab_face tab_face;
88 struct body_face {
89 int lbg, lfg;
90 int bg, fg;
91 int rbg, rfg;
93 int left, body, right;
94 };
95 extern struct body_face body_face;
97 struct modeline_face {
98 int background;
99 };
100 extern struct modeline_face modeline_face;
102 struct minibuffer_face {
103 int background;
104 };
105 extern struct minibuffer_face minibuffer_face;
107 enum line_type {
108 LINE_TEXT,
109 LINE_LINK,
110 LINE_TITLE_1,
111 LINE_TITLE_2,
112 LINE_TITLE_3,
113 LINE_ITEM,
114 LINE_QUOTE,
115 LINE_PRE_START,
116 LINE_PRE_CONTENT,
117 LINE_PRE_END,
118 };
120 struct line {
121 enum line_type type;
122 char *line;
123 char *alt;
124 int flags;
125 TAILQ_ENTRY(line) lines;
126 };
128 struct vline {
129 const struct line *parent;
130 char *line;
131 int flags;
132 TAILQ_ENTRY(vline) vlines;
133 };
135 struct parser;
136 struct page;
138 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
139 typedef int (*parserfreefn)(struct parser*);
141 typedef void (imsg_handlerfn)(struct imsg*, size_t);
143 struct parser {
144 const char *name;
145 char title[32+1];
146 char *buf;
147 size_t len;
148 size_t cap;
149 int flags;
150 parsechunkfn parse;
151 parserfreefn free;
153 TAILQ_HEAD(, line) head;
154 };
156 /*
157 * differnt types of trust for a certificate. Following
158 * gemini://thfr.info/gemini/modified-trust-verify.gmi
159 */
160 enum trust_state {
161 TS_UNKNOWN,
162 TS_UNTRUSTED,
163 TS_TRUSTED,
164 TS_VERIFIED,
165 };
167 struct tofu_entry {
168 char domain[GEMINI_URL_LEN];
170 /*
171 * enough space for ``PROTO:HASH''. probably isn't a good
172 * idea tho.
173 */
174 char hash[128+1];
175 int verified;
176 };
178 struct histhead {
179 TAILQ_HEAD(mhisthead, hist) head;
180 size_t len;
181 };
182 struct hist {
183 char h[1025];
184 TAILQ_ENTRY(hist) entries;
185 };
187 struct buffer {
188 struct parser page;
190 size_t last_line_off;
191 int force_redraw;
193 int curs_x;
194 int curs_y;
195 size_t line_off;
196 size_t line_max;
197 struct vline *current_line;
198 size_t cpoff;
199 TAILQ_HEAD(vhead, vline) head;
200 };
202 #define TAB_CURRENT 0x1
203 #define TAB_URGENT 0x2
205 #define NEW_TAB_URL "about:new"
207 extern TAILQ_HEAD(tabshead, tab) tabshead;
208 struct tab {
209 TAILQ_ENTRY(tab) tabs;
210 uint32_t id;
211 uint32_t flags;
213 char *cert;
214 enum trust_state trust;
215 struct phos_uri uri;
216 struct histhead hist;
217 struct hist *hist_cur;
218 size_t hist_off;
220 int code;
221 char meta[GEMINI_URL_LEN];
222 int redirect_count;
224 struct buffer buffer;
226 short loading_anim;
227 short loading_anim_step;
228 struct event loadingev;
230 int fd;
231 size_t bytes;
232 char *path;
233 };
235 struct proto {
236 const char *schema;
238 /*
239 * should load the given url in the tab. Optionally, it may
240 * consider the given url as relative to the one already
241 * present in tab. It must set tab->urlstr to a serialized
242 * human-friendly URL.
243 */
244 void (*loadfn)(struct tab*, const char*);
245 };
247 struct kmap {
248 TAILQ_HEAD(map, keymap) m;
249 void (*unhandled_input)(void);
250 };
252 struct keymap {
253 int meta;
254 int key;
255 struct kmap map;
256 void (*fn)(struct buffer*);
258 TAILQ_ENTRY(keymap) keymaps;
259 };
261 /* defaults.c */
262 void config_init(void);
263 int config_setprfx(const char *, const char *, const char *);
264 int config_setvari(const char *, int);
265 int config_setvars(const char *, char *);
266 int config_setcolor(int, const char *, int, int, int);
267 int config_setattr(const char *, int, int, int);
268 void config_apply_colors(void);
270 /* fs.c */
271 int fs_init(void);
272 int fs_main(struct imsgbuf*);
273 int load_certs(struct ohash*);
274 int load_last_session(void(*)(const char*));
276 /* gemini.c */
277 int client_main(struct imsgbuf*);
279 /* gemtext.c */
280 void gemtext_initparser(struct parser*);
282 /* hist.c */
283 void hist_clear_forward(struct histhead*, struct hist*);
284 void hist_push(struct histhead*, struct hist*);
286 /* keymap.c */
287 int kbd(const char*);
288 const char *unkbd(int);
289 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
291 /* mime.c */
292 int setup_parser_for(struct tab*);
294 /* pages.c */
295 extern const char *about_about;
296 extern const char *about_blank;
297 extern const char *about_help;
298 extern const char *about_new;
300 #define CANNOT_FETCH 0
301 #define TOO_MUCH_REDIRECTS 1
302 #define MALFORMED_RESPONSE 2
303 #define UNKNOWN_TYPE_OR_CSET 3
304 extern const char *err_pages[70];
306 /* parse.y */
307 void parseconfig(const char *, int);
309 /* parser.c */
310 int parser_append(struct parser*, const char*, size_t);
311 int parser_set_buf(struct parser*, const char*, size_t);
312 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
314 /* sandbox.c */
315 void sandbox_network_process(void);
316 void sandbox_ui_process(void);
317 void sandbox_fs_process(void);
319 /* telescope.c */
320 void load_about_url(struct tab*, const char*);
321 void load_gemini_url(struct tab*, const char*);
322 void load_url(struct tab*, const char*);
323 int load_previous_page(struct tab*);
324 int load_next_page(struct tab*);
325 void stop_tab(struct tab*);
326 void add_to_bookmarks(const char*);
327 void save_session(void);
329 /* textplain.c */
330 void textplain_initparser(struct parser*);
332 /* tofu.c */
333 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
334 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
335 void tofu_add(struct ohash*, struct tofu_entry*);
336 void tofu_update(struct ohash*, struct tofu_entry*);
338 /* ui.c */
339 extern int body_lines;
340 extern int body_cols;
341 extern int in_minibuffer;
343 enum pairs {
344 PTL_BG = 1,
345 PTL_TAB,
346 PTL_CURR,
348 PBODY,
349 PBLEFT,
350 PBRIGHT,
352 PT,
353 PT_PRFX,
354 PT_TRAIL,
355 PL,
356 PL_PRFX,
357 PL_TRAIL,
358 PT1,
359 PT1_PRFX,
360 PT1_TRAIL,
361 PT2,
362 PT2_PRFX,
363 PT2_TRAIL,
364 PT3,
365 PT3_PRFX,
366 PT3_TRAIL,
367 PI,
368 PI_PRFX,
369 PI_TRAIL,
370 PQ,
371 PQ_PRFX,
372 PQ_TRAIL,
373 PPSTART,
374 PPSTART_PRFX,
375 PPSTART_TRAIL,
376 PP,
377 PP_PRFX,
378 PP_TRAIL,
379 PPEND,
380 PPEND_PRFX,
381 PPEND_TRAIL,
382 };
384 struct thiskey {
385 short meta;
386 int key;
387 uint32_t cp;
388 };
389 extern struct thiskey thiskey;
391 extern struct histhead eecmd_history,
392 ir_history,
393 lu_history,
394 read_history;
396 struct ministate {
397 char *curmesg;
399 char prompt[64];
400 void (*donefn)(void);
401 void (*abortfn)(void);
403 char buf[1025];
404 struct line line;
405 struct vline vline;
406 struct buffer buffer;
408 struct histhead *history;
409 struct hist *hist_cur;
410 size_t hist_off;
411 };
412 extern struct ministate ministate;
414 void restore_cursor(struct buffer *);
415 void minibuffer_taint_hist(void);
416 void eecmd_self_insert(void);
417 void eecmd_select(void);
418 void ir_self_insert(void);
419 void ir_select(void);
420 void lu_self_insert(void);
421 void lu_select(void);
422 void bp_select(void);
423 void vmessage(const char*, va_list);
424 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
425 void start_loading_anim(struct tab *tab);
426 void load_url_in_tab(struct tab *, const char *);
427 void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead *);
428 void exit_minibuffer(void);
429 void switch_to_tab(struct tab *);
430 struct tab *current_tab(void);
431 struct tab *new_tab(const char *);
432 unsigned int tab_new_id(void);
433 int ui_init(int, char * const*);
434 void ui_on_tab_loaded(struct tab*);
435 void ui_on_tab_refresh(struct tab*);
436 const char *ui_keyname(int);
437 void ui_toggle_side_window(void);
438 void ui_schedule_redraw(void);
439 void ui_require_input(struct tab*, int);
440 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
441 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
442 void ui_end(void);
444 /* utf.8 */
445 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
446 size_t utf8_encode(uint32_t, char*);
447 char *utf8_nth(char*, size_t);
448 size_t utf8_cplen(char*);
449 size_t utf8_chwidth(uint32_t);
450 size_t utf8_snwidth(const char*, size_t);
451 size_t utf8_swidth(const char*);
452 size_t utf8_swidth_between(const char*, const char*);
453 char *utf8_next_cp(const char*);
454 char *utf8_prev_cp(const char*, const char*);
456 /* util.c */
457 int mark_nonblock(int);
458 int has_prefix(const char*, const char*);
459 int unicode_isspace(uint32_t);
460 int unicode_isgraph(uint32_t);
461 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
463 /* wrap.c */
464 void empty_linelist(struct buffer*);
465 void empty_vlist(struct buffer*);
466 int wrap_text(struct buffer*, const char*, struct line*, size_t);
467 int hardwrap_text(struct buffer*, struct line*, size_t);
469 #endif /* TELESCOPE_H */