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 struct histhead {
104 TAILQ_HEAD(mhisthead, hist) head;
105 size_t len;
106 };
107 struct hist {
108 char h[1025];
109 TAILQ_ENTRY(hist) entries;
110 };
112 struct ui_state {
113 int curs_x;
114 int curs_y;
115 size_t line_off;
116 size_t line_max;
117 struct vline *current_line;
118 size_t line_x;
120 short loading_anim;
121 short loading_anim_step;
122 struct event loadingev;
124 TAILQ_HEAD(vhead, vline) head;
125 };
127 /*
128 * differnt types of trust for a certificate. Following
129 * gemini://thfr.info/gemini/modified-trust-verify.gmi
130 */
131 enum trust_state {
132 TS_UNKNOWN,
133 TS_UNTRUSTED,
134 TS_TRUSTED,
135 TS_VERIFIED,
136 };
138 struct tofu_entry {
139 char domain[GEMINI_URL_LEN];
140 /* enough space for ``PROTO:HASH''. probably isn't a good
141 * idea thou. */
142 char hash[128+1];
143 int verified;
144 };
146 extern TAILQ_HEAD(tabshead, tab) tabshead;
147 struct tab {
148 struct parser page;
149 TAILQ_ENTRY(tab) tabs;
150 uint32_t id;
151 uint32_t flags;
153 enum trust_state trust;
154 struct url url;
155 struct histhead hist;
156 struct hist *hist_cur;
157 size_t hist_off;
159 int code;
160 char meta[GEMINI_URL_LEN];
161 int redirect_count;
163 struct ui_state s;
164 };
166 struct proto {
167 const char *schema;
169 /* should load the given url in the tab. Optionally, it may
170 * consider the given url as relative to the one already
171 * present in tab. It must set tab->urlstr to a serialized
172 * human-friendly URL. */
173 void (*loadfn)(struct tab*, const char*);
174 };
176 struct kmap {
177 TAILQ_HEAD(map, keymap) m;
178 void (*unhandled_input)(void);
179 };
181 struct keymap {
182 int meta;
183 int key;
184 struct kmap map;
185 void (*fn)(struct tab*);
187 TAILQ_ENTRY(keymap) keymaps;
188 };
190 /* fs.c */
191 int fs_init(void);
192 int fs_main(struct imsgbuf*);
193 int load_certs(struct ohash*);
195 /* gemini.c */
196 int client_main(struct imsgbuf*);
198 /* gemtext.c */
199 void gemtext_initparser(struct parser*);
201 /* hash.c */
202 void telescope_ohash_init(struct ohash*, unsigned int, ptrdiff_t);
203 struct tofu_entry *telescope_lookup_tofu(struct ohash*, const char*);
204 void telescope_ohash_insert(struct ohash*, struct tofu_entry*);
206 /* hist.c */
207 void hist_clear_forward(struct histhead*, struct hist*);
208 void hist_push(struct histhead*, struct hist*);
210 /* keymap.c */
211 int kbd(const char*);
212 const char *unkbd(int);
213 int kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
215 /* mime.c */
216 int setup_parser_for(struct tab*);
218 /* pages.c */
219 extern const char *about_new;
221 #define CANNOT_FETCH 0
222 #define TOO_MUCH_REDIRECTS 1
223 #define MALFORMED_RESPONSE 2
224 #define UNKNOWN_TYPE_OR_CSET 3
225 extern const char *err_pages[70];
227 /* parser.c */
228 int parser_append(struct parser*, const char*, size_t);
229 int parser_set_buf(struct parser*, const char*, size_t);
230 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
232 /* sandbox.c */
233 void sandbox_network_process(void);
234 void sandbox_ui_process(void);
235 void sandbox_fs_process(void);
237 /* telescope.c */
238 void load_about_url(struct tab*, const char*);
239 void load_gemini_url(struct tab*, const char*);
240 void load_url(struct tab*, const char*);
241 int load_previous_page(struct tab*);
242 int load_next_page(struct tab*);
243 void stop_tab(struct tab*);
244 void add_to_bookmarks(const char*);
246 /* textplain.c */
247 void textplain_initparser(struct parser*);
249 /* ui.c */
250 int ui_init(int, char * const*);
251 void ui_on_tab_loaded(struct tab*);
252 void ui_on_tab_refresh(struct tab*);
253 void ui_require_input(struct tab*, int);
254 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
255 void ui_end(void);
257 /* utf.8 */
258 char *utf8_nth(char*, size_t);
259 size_t utf8_cplen(char*);
260 size_t utf8_chwidth(uint32_t);
261 size_t utf8_snwidth(const char*, size_t);
262 size_t utf8_swidth(const char*);
264 /* util.c */
265 int mark_nonblock(int);
266 int has_prefix(const char*, const char*);
267 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
269 /* wrap.c */
270 void wrap_text(struct tab*, const char*, struct line*, size_t);
271 int hardwrap_text(struct tab*, struct line*, size_t);
273 #endif /* TELESCOPE_H */