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 IMSG_GET, /* data is URL, peerid the tab id */
33 IMSG_ERR,
34 IMSG_CHECK_CERT,
35 IMSG_CERT_STATUS,
36 IMSG_GOT_CODE,
37 IMSG_GOT_META,
38 IMSG_PROCEED,
39 IMSG_STOP,
40 IMSG_BUF,
41 IMSG_EOF,
42 IMSG_QUIT,
43 };
45 enum line_type {
46 LINE_TEXT,
47 LINE_LINK,
48 LINE_TITLE_1,
49 LINE_TITLE_2,
50 LINE_TITLE_3,
51 LINE_ITEM,
52 LINE_QUOTE,
53 LINE_PRE_START,
54 LINE_PRE_CONTENT,
55 LINE_PRE_END,
56 };
58 struct line {
59 enum line_type type;
60 char *line;
61 char *alt;
62 int flags;
63 TAILQ_ENTRY(line) lines;
64 };
66 struct vline {
67 const struct line *parent;
68 char *line;
69 int flags;
70 TAILQ_ENTRY(vline) vlines;
71 };
73 struct parser;
74 struct page;
76 /* typedef void (*initparserfn)(struct parser*); */
78 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
79 typedef int (*parserfreefn)(struct parser*);
81 typedef void (imsg_handlerfn)(struct imsg*, size_t);
83 struct parser {
84 const char *name;
85 char title[32+1];
86 char *buf;
87 size_t len;
88 size_t cap;
89 int flags;
90 parsechunkfn parse;
91 parserfreefn free;
93 TAILQ_HEAD(, line) head;
94 };
96 struct histhead {
97 TAILQ_HEAD(mhisthead, hist) head;
98 size_t len;
99 };
100 struct hist {
101 char h[1025];
102 TAILQ_ENTRY(hist) entries;
103 };
105 struct ui_state {
106 int curs_x;
107 int curs_y;
108 size_t line_off;
109 size_t line_max;
111 short loading_anim;
112 short loading_anim_step;
113 struct event loadingev;
115 TAILQ_HEAD(, vline) head;
116 };
118 extern TAILQ_HEAD(tabshead, tab) tabshead;
119 struct tab {
120 struct parser page;
121 TAILQ_ENTRY(tab) tabs;
122 uint32_t id;
123 uint32_t flags;
125 struct url url;
126 struct histhead hist;
127 struct hist *hist_cur;
128 size_t hist_off;
130 int code;
131 char meta[GEMINI_URL_LEN];
132 int redirect_count;
134 struct ui_state s;
135 };
137 struct proto {
138 const char *schema;
140 /* should load the given url in the tab. Optionally, it may
141 * consider the given url as relative to the one already
142 * present in tab. It must set tab->urlstr to a serialized
143 * human-friendly URL. */
144 void (*loadfn)(struct tab*, const char*);
145 };
147 struct kmap {
148 TAILQ_HEAD(map, keymap) m;
149 void (*unhandled_input)(void);
150 };
152 struct keymap {
153 int meta;
154 int key;
155 struct kmap map;
156 void (*fn)(struct tab*);
158 TAILQ_ENTRY(keymap) keymaps;
159 };
161 extern struct event imsgev;
163 /* gemini.c */
164 int client_main(struct imsgbuf *b);
166 /* gemtext.c */
167 void gemtext_initparser(struct parser*);
169 /* hist.c */
170 void hist_clear_forward(struct histhead*, struct hist*);
171 void hist_push(struct histhead*, struct hist*);
173 /* keymap.c */
174 int kbd(const char*);
175 const char *unkbd(int);
176 int kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
178 /* mime.c */
179 int setup_parser_for(struct tab*);
181 /* pages.c */
182 extern const char *about_new;
184 #define CANNOT_FETCH 0
185 #define TOO_MUCH_REDIRECTS 1
186 #define MALFORMED_RESPONSE 2
187 #define UNKNOWN_TYPE_OR_CSET 3
188 extern const char *err_pages[70];
190 /* parser.c */
191 int parser_append(struct parser*, const char*, size_t);
192 int parser_set_buf(struct parser*, const char*, size_t);
194 /* sandbox.c */
195 void sandbox_network_process(void);
196 void sandbox_ui_process(void);
198 /* telescope.c */
199 void load_about_url(struct tab*, const char*);
200 void load_gemini_url(struct tab*, const char*);
201 void load_url(struct tab*, const char*);
202 int load_previous_page(struct tab*);
203 int load_next_page(struct tab*);
204 void stop_tab(struct tab*);
206 /* textplain.c */
207 void textplain_initparser(struct parser*);
209 /* ui.c */
210 int ui_init(void);
211 void ui_on_tab_loaded(struct tab*);
212 void ui_on_tab_refresh(struct tab*);
213 void ui_require_input(struct tab*, int);
214 void ui_end(void);
216 /* util.c */
217 int mark_nonblock(int);
218 char *telescope_strnchr(char*, char, size_t);
219 int has_prefix(const char*, const char*);
221 /* wrap.c */
222 void wrap_text(struct tab*, const char*, struct line*, size_t);
223 int hardwrap_text(struct tab*, struct line*, size_t);
225 #endif /* TELESCOPE_H */