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 void config_apply_colors(void);
265 /* fs.c */
266 int fs_init(void);
267 int fs_main(struct imsgbuf*);
268 int load_certs(struct ohash*);
269 int load_last_session(void(*)(const char*));
271 /* gemini.c */
272 int client_main(struct imsgbuf*);
274 /* gemtext.c */
275 void gemtext_initparser(struct parser*);
277 /* hist.c */
278 void hist_clear_forward(struct histhead*, struct hist*);
279 void hist_push(struct histhead*, struct hist*);
281 /* keymap.c */
282 int kbd(const char*);
283 const char *unkbd(int);
284 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
286 /* mime.c */
287 int setup_parser_for(struct tab*);
289 /* pages.c */
290 extern const char *about_about;
291 extern const char *about_blank;
292 extern const char *about_help;
293 extern const char *about_new;
295 #define CANNOT_FETCH 0
296 #define TOO_MUCH_REDIRECTS 1
297 #define MALFORMED_RESPONSE 2
298 #define UNKNOWN_TYPE_OR_CSET 3
299 extern const char *err_pages[70];
301 /* parse.y */
302 void parseconfig(const char *, int);
304 /* parser.c */
305 int parser_append(struct parser*, const char*, size_t);
306 int parser_set_buf(struct parser*, const char*, size_t);
307 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
309 /* sandbox.c */
310 void sandbox_network_process(void);
311 void sandbox_ui_process(void);
312 void sandbox_fs_process(void);
314 /* telescope.c */
315 void load_about_url(struct tab*, const char*);
316 void load_gemini_url(struct tab*, const char*);
317 void load_url(struct tab*, const char*);
318 int load_previous_page(struct tab*);
319 int load_next_page(struct tab*);
320 void stop_tab(struct tab*);
321 void add_to_bookmarks(const char*);
322 void save_session(void);
324 /* textplain.c */
325 void textplain_initparser(struct parser*);
327 /* tofu.c */
328 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
329 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
330 void tofu_add(struct ohash*, struct tofu_entry*);
331 void tofu_update(struct ohash*, struct tofu_entry*);
333 /* ui.c */
334 extern int body_lines;
335 extern int body_cols;
336 extern int in_minibuffer;
338 enum pairs {
339 PBODY = 1,
340 PBLEFT,
341 PBRIGHT,
343 PT,
344 PT_PRFX,
345 PT_TRAIL,
346 PL,
347 PL_PRFX,
348 PL_TRAIL,
349 PT1,
350 PT1_PRFX,
351 PT1_TRAIL,
352 PT2,
353 PT2_PRFX,
354 PT2_TRAIL,
355 PT3,
356 PT3_PRFX,
357 PT3_TRAIL,
358 PI,
359 PI_PRFX,
360 PI_TRAIL,
361 PQ,
362 PQ_PRFX,
363 PQ_TRAIL,
364 PPSTART,
365 PPSTART_PRFX,
366 PPSTART_TRAIL,
367 PP,
368 PP_PRFX,
369 PP_TRAIL,
370 PPEND,
371 PPEND_PRFX,
372 PPEND_TRAIL,
373 };
375 struct thiskey {
376 short meta;
377 int key;
378 uint32_t cp;
379 };
380 extern struct thiskey thiskey;
382 extern struct histhead eecmd_history,
383 ir_history,
384 lu_history,
385 read_history;
387 struct ministate {
388 char *curmesg;
390 char prompt[64];
391 void (*donefn)(void);
392 void (*abortfn)(void);
394 char buf[1025];
395 struct line line;
396 struct vline vline;
397 struct buffer buffer;
399 struct histhead *history;
400 struct hist *hist_cur;
401 size_t hist_off;
402 };
403 extern struct ministate ministate;
405 void restore_cursor(struct buffer *);
406 void minibuffer_taint_hist(void);
407 void eecmd_self_insert(void);
408 void eecmd_select(void);
409 void ir_self_insert(void);
410 void ir_select(void);
411 void lu_self_insert(void);
412 void lu_select(void);
413 void bp_select(void);
414 void vmessage(const char*, va_list);
415 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
416 void start_loading_anim(struct tab *tab);
417 void load_url_in_tab(struct tab *, const char *);
418 void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead *);
419 void exit_minibuffer(void);
420 void switch_to_tab(struct tab *);
421 struct tab *current_tab(void);
422 struct tab *new_tab(const char *);
423 unsigned int tab_new_id(void);
424 int ui_init(int, char * const*);
425 void ui_on_tab_loaded(struct tab*);
426 void ui_on_tab_refresh(struct tab*);
427 const char *ui_keyname(int);
428 void ui_toggle_side_window(void);
429 void ui_schedule_redraw(void);
430 void ui_require_input(struct tab*, int);
431 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
432 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
433 void ui_end(void);
435 /* utf.8 */
436 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
437 size_t utf8_encode(uint32_t, char*);
438 char *utf8_nth(char*, size_t);
439 size_t utf8_cplen(char*);
440 size_t utf8_chwidth(uint32_t);
441 size_t utf8_snwidth(const char*, size_t);
442 size_t utf8_swidth(const char*);
443 size_t utf8_swidth_between(const char*, const char*);
444 char *utf8_next_cp(const char*);
445 char *utf8_prev_cp(const char*, const char*);
447 /* util.c */
448 int mark_nonblock(int);
449 int has_prefix(const char*, const char*);
450 int unicode_isspace(uint32_t);
451 int unicode_isgraph(uint32_t);
452 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
454 /* wrap.c */
455 void empty_linelist(struct buffer*);
456 void empty_vlist(struct buffer*);
457 int wrap_text(struct buffer*, const char*, struct line*, size_t);
458 int hardwrap_text(struct buffer*, struct line*, size_t);
460 #endif /* TELESCOPE_H */