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