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 */
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,
44 };
46 enum line_type {
47 LINE_TEXT,
48 LINE_LINK,
49 LINE_TITLE_1,
50 LINE_TITLE_2,
51 LINE_TITLE_3,
52 LINE_ITEM,
53 LINE_QUOTE,
54 LINE_PRE_START,
55 LINE_PRE_CONTENT,
56 LINE_PRE_END,
57 };
59 struct line {
60 enum line_type type;
61 char *line;
62 char *alt;
63 int flags;
64 TAILQ_ENTRY(line) lines;
65 };
67 struct vline {
68 const struct line *parent;
69 char *line;
70 int flags;
71 TAILQ_ENTRY(vline) vlines;
72 };
74 struct parser;
75 struct page;
77 /* typedef void (*initparserfn)(struct parser*); */
79 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
80 typedef int (*parserfreefn)(struct parser*);
82 typedef void (imsg_handlerfn)(struct imsg*, size_t);
84 struct parser {
85 const char *name;
86 char title[32+1];
87 char *buf;
88 size_t len;
89 size_t cap;
90 int flags;
91 parsechunkfn parse;
92 parserfreefn free;
94 TAILQ_HEAD(, line) head;
95 };
97 struct histhead {
98 TAILQ_HEAD(mhisthead, hist) head;
99 size_t len;
100 };
101 struct hist {
102 char h[1025];
103 TAILQ_ENTRY(hist) entries;
104 };
106 struct ui_state {
107 int curs_x;
108 int curs_y;
109 size_t line_off;
110 size_t line_max;
112 short loading_anim;
113 short loading_anim_step;
114 struct event loadingev;
116 TAILQ_HEAD(, vline) head;
117 };
119 extern TAILQ_HEAD(tabshead, tab) tabshead;
120 struct tab {
121 struct parser page;
122 TAILQ_ENTRY(tab) tabs;
123 uint32_t id;
124 uint32_t flags;
126 struct url url;
127 struct histhead hist;
128 struct hist *hist_cur;
129 size_t hist_off;
131 int code;
132 char meta[GEMINI_URL_LEN];
133 int redirect_count;
135 struct ui_state s;
136 };
138 struct proto {
139 const char *schema;
141 /* should load the given url in the tab. Optionally, it may
142 * consider the given url as relative to the one already
143 * present in tab. It must set tab->urlstr to a serialized
144 * human-friendly URL. */
145 void (*loadfn)(struct tab*, const char*);
146 };
148 struct kmap {
149 TAILQ_HEAD(map, keymap) m;
150 void (*unhandled_input)(void);
151 };
153 struct keymap {
154 int meta;
155 int key;
156 struct kmap map;
157 void (*fn)(struct tab*);
159 TAILQ_ENTRY(keymap) keymaps;
160 };
162 /* fs.c */
163 int fs_main(struct imsgbuf*);
165 /* gemini.c */
166 int client_main(struct imsgbuf*);
168 /* gemtext.c */
169 void gemtext_initparser(struct parser*);
171 /* hist.c */
172 void hist_clear_forward(struct histhead*, struct hist*);
173 void hist_push(struct histhead*, struct hist*);
175 /* keymap.c */
176 int kbd(const char*);
177 const char *unkbd(int);
178 int kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
180 /* mime.c */
181 int setup_parser_for(struct tab*);
183 /* pages.c */
184 extern const char *about_new;
186 #define CANNOT_FETCH 0
187 #define TOO_MUCH_REDIRECTS 1
188 #define MALFORMED_RESPONSE 2
189 #define UNKNOWN_TYPE_OR_CSET 3
190 extern const char *err_pages[70];
192 /* parser.c */
193 int parser_append(struct parser*, const char*, size_t);
194 int parser_set_buf(struct parser*, const char*, size_t);
196 /* sandbox.c */
197 void sandbox_network_process(void);
198 void sandbox_ui_process(void);
199 void sandbox_fs_process(void);
201 /* telescope.c */
202 void load_about_url(struct tab*, const char*);
203 void load_gemini_url(struct tab*, const char*);
204 void load_url(struct tab*, const char*);
205 int load_previous_page(struct tab*);
206 int load_next_page(struct tab*);
207 void stop_tab(struct tab*);
209 /* textplain.c */
210 void textplain_initparser(struct parser*);
212 /* ui.c */
213 int ui_init(void);
214 void ui_on_tab_loaded(struct tab*);
215 void ui_on_tab_refresh(struct tab*);
216 void ui_require_input(struct tab*, int);
217 void ui_end(void);
219 /* util.c */
220 int mark_nonblock(int);
221 char *telescope_strnchr(char*, char, size_t);
222 int has_prefix(const char*, const char*);
224 /* wrap.c */
225 void wrap_text(struct tab*, const char*, struct line*, size_t);
226 int hardwrap_text(struct tab*, struct line*, size_t);
228 #endif /* TELESCOPE_H */