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 "compat.h"
22 #include <event.h>
24 #include "url.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 };
52 enum line_type {
53 LINE_TEXT,
54 LINE_LINK,
55 LINE_TITLE_1,
56 LINE_TITLE_2,
57 LINE_TITLE_3,
58 LINE_ITEM,
59 LINE_QUOTE,
60 LINE_PRE_START,
61 LINE_PRE_CONTENT,
62 LINE_PRE_END,
63 };
65 struct line {
66 enum line_type type;
67 char *line;
68 char *alt;
69 int flags;
70 TAILQ_ENTRY(line) lines;
71 };
73 struct vline {
74 const struct line *parent;
75 char *line;
76 int flags;
77 TAILQ_ENTRY(vline) vlines;
78 };
80 struct parser;
81 struct page;
83 /* typedef void (*initparserfn)(struct parser*); */
85 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
86 typedef int (*parserfreefn)(struct parser*);
88 typedef void (imsg_handlerfn)(struct imsg*, size_t);
90 struct parser {
91 const char *name;
92 char title[32+1];
93 char *buf;
94 size_t len;
95 size_t cap;
96 int flags;
97 parsechunkfn parse;
98 parserfreefn free;
100 TAILQ_HEAD(, line) head;
101 };
103 /*
104 * differnt types of trust for a certificate. Following
105 * gemini://thfr.info/gemini/modified-trust-verify.gmi
106 */
107 enum trust_state {
108 TS_UNKNOWN,
109 TS_UNTRUSTED,
110 TS_TRUSTED,
111 TS_VERIFIED,
112 };
114 struct tofu_entry {
115 char domain[GEMINI_URL_LEN];
116 /* enough space for ``PROTO:HASH''. probably isn't a good
117 * idea thou. */
118 char hash[128+1];
119 int verified;
120 };
122 struct histhead {
123 TAILQ_HEAD(mhisthead, hist) head;
124 size_t len;
125 };
126 struct hist {
127 char h[1025];
128 TAILQ_ENTRY(hist) entries;
129 };
131 struct window {
132 struct parser page;
133 int curs_x;
134 int curs_y;
135 size_t line_off;
136 size_t line_max;
137 struct vline *current_line;
138 size_t cpoff;
139 TAILQ_HEAD(vhead, vline) head;
140 };
142 extern TAILQ_HEAD(tabshead, tab) tabshead;
143 struct tab {
144 TAILQ_ENTRY(tab) tabs;
145 uint32_t id;
146 uint32_t flags;
148 enum trust_state trust;
149 struct url url;
150 struct histhead hist;
151 struct hist *hist_cur;
152 size_t hist_off;
154 int code;
155 char meta[GEMINI_URL_LEN];
156 int redirect_count;
158 struct window window;
160 short loading_anim;
161 short loading_anim_step;
162 struct event loadingev;
163 };
165 struct proto {
166 const char *schema;
168 /* should load the given url in the tab. Optionally, it may
169 * consider the given url as relative to the one already
170 * present in tab. It must set tab->urlstr to a serialized
171 * human-friendly URL. */
172 void (*loadfn)(struct tab*, const char*);
173 };
175 struct kmap {
176 TAILQ_HEAD(map, keymap) m;
177 void (*unhandled_input)(void);
178 };
180 struct keymap {
181 int meta;
182 int key;
183 struct kmap map;
184 void (*fn)(struct window*);
186 TAILQ_ENTRY(keymap) keymaps;
187 };
189 /* fs.c */
190 int fs_init(void);
191 int fs_main(struct imsgbuf*);
192 int load_certs(struct ohash*);
194 /* gemini.c */
195 int client_main(struct imsgbuf*);
197 /* gemtext.c */
198 void gemtext_initparser(struct parser*);
200 /* hash.c */
201 void telescope_ohash_init(struct ohash*, unsigned int, ptrdiff_t);
202 struct tofu_entry *telescope_lookup_tofu(struct ohash*, const char*);
203 void telescope_ohash_insert(struct ohash*, struct tofu_entry*);
205 /* hist.c */
206 void hist_clear_forward(struct histhead*, struct hist*);
207 void hist_push(struct histhead*, struct hist*);
209 /* keymap.c */
210 int kbd(const char*);
211 const char *unkbd(int);
212 int kmap_define_key(struct kmap*, const char*, void(*)(struct window*));
214 /* mime.c */
215 int setup_parser_for(struct tab*);
217 /* pages.c */
218 extern const char *about_new;
220 #define CANNOT_FETCH 0
221 #define TOO_MUCH_REDIRECTS 1
222 #define MALFORMED_RESPONSE 2
223 #define UNKNOWN_TYPE_OR_CSET 3
224 extern const char *err_pages[70];
226 /* parser.c */
227 int parser_append(struct parser*, const char*, size_t);
228 int parser_set_buf(struct parser*, const char*, size_t);
229 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
231 /* sandbox.c */
232 void sandbox_network_process(void);
233 void sandbox_ui_process(void);
234 void sandbox_fs_process(void);
236 /* telescope.c */
237 void load_about_url(struct tab*, const char*);
238 void load_gemini_url(struct tab*, const char*);
239 void load_url(struct tab*, const char*);
240 int load_previous_page(struct tab*);
241 int load_next_page(struct tab*);
242 void stop_tab(struct tab*);
243 void add_to_bookmarks(const char*);
245 /* textplain.c */
246 void textplain_initparser(struct parser*);
248 /* ui.c */
249 int ui_init(int, char * const*);
250 void ui_on_tab_loaded(struct tab*);
251 void ui_on_tab_refresh(struct tab*);
252 void ui_require_input(struct tab*, int);
253 void ui_yornp(const char*, void (*)(int, unsigned int));
254 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
255 void ui_end(void);
257 /* utf.8 */
258 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
259 size_t utf8_encode(uint32_t, char*);
260 char *utf8_nth(char*, size_t);
261 size_t utf8_cplen(char*);
262 size_t utf8_chwidth(uint32_t);
263 size_t utf8_snwidth(const char*, size_t);
264 size_t utf8_swidth(const char*);
265 size_t utf8_swidth_between(const char*, const char*);
266 char *utf8_next_cp(const char*);
267 char *utf8_prev_cp(const char*, const char*);
269 /* util.c */
270 int mark_nonblock(int);
271 int has_prefix(const char*, const char*);
272 int unicode_isspace(uint32_t);
273 int unicode_isgraph(uint32_t);
274 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
276 /* wrap.c */
277 int wrap_text(struct window*, const char*, struct line*, size_t);
278 int hardwrap_text(struct window*, struct line*, size_t);
280 #endif /* TELESCOPE_H */