| 1 |
/* |
| 2 |
* Copyright (c) 2021-2024 joshua stein <jcs@jcs.org> |
| 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 |
*/ |
| 16 |
|
| 17 |
#ifndef __BROWSER_H__ |
| 18 |
#define __BROWSER_H__ |
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
|
| 22 |
#include "detritus.h" |
| 23 |
#include "textview.h" |
| 24 |
#include "util.h" |
| 25 |
|
| 26 |
//#define BROWSER_DEBUGF(x) browser_statusf |
| 27 |
#define BROWSER_DEBUGF(x) |
| 28 |
|
| 29 |
#define STYLE_NONE 0 |
| 30 |
#define STYLE_BOLD (1UL << 0) |
| 31 |
#define STYLE_ITALIC (1UL << 1) |
| 32 |
#define STYLE_H1 (1UL << 2) |
| 33 |
#define STYLE_H2 (1UL << 3) |
| 34 |
#define STYLE_H3 (1UL << 4) |
| 35 |
#define STYLE_H4 (1UL << 5) |
| 36 |
#define STYLE_H5 (1UL << 6) |
| 37 |
#define STYLE_H6 (1UL << 7) |
| 38 |
#define STYLE_LINK (1UL << 8) |
| 39 |
#define STYLE_PRE (1UL << 9) |
| 40 |
#define STYLE_LIST (1UL << 10) |
| 41 |
#define STYLE_QUOTE (1UL << 11) |
| 42 |
#define STYLE_UNDERLINE (1UL << 12) |
| 43 |
|
| 44 |
struct browser_link { |
| 45 |
char *uri; |
| 46 |
char *title; |
| 47 |
unsigned short pos; |
| 48 |
unsigned short len; |
| 49 |
BigRect rect; |
| 50 |
struct browser_link *next_link; |
| 51 |
}; |
| 52 |
|
| 53 |
typedef struct page **page_handle; |
| 54 |
|
| 55 |
struct page { |
| 56 |
struct URI *uri; |
| 57 |
struct browser *browser; |
| 58 |
page_handle back_page; |
| 59 |
page_handle fwd_page; |
| 60 |
|
| 61 |
struct page_handler *handler; |
| 62 |
void *handler_cookie; |
| 63 |
|
| 64 |
struct request *request; |
| 65 |
void *request_cookie; |
| 66 |
|
| 67 |
size_t content_size; |
| 68 |
#define PAGE_CONTENT_CHUNK_SIZE 1024 |
| 69 |
size_t content_len; |
| 70 |
size_t content_pos; |
| 71 |
size_t header_len; |
| 72 |
|
| 73 |
short parse_state; |
| 74 |
size_t server_content_len; |
| 75 |
short server_status; |
| 76 |
char content_type[32]; |
| 77 |
|
| 78 |
struct URI *redir_to; |
| 79 |
short download_frefnum; |
| 80 |
size_t download_len; |
| 81 |
|
| 82 |
struct TVStyle cur_style; |
| 83 |
|
| 84 |
char content[]; |
| 85 |
}; |
| 86 |
|
| 87 |
#define PAGE_CAN_READ_MORE(page) ((page)->request && \ |
| 88 |
!(page)->request->tcp_done_reading) |
| 89 |
|
| 90 |
struct browser { |
| 91 |
WindowPtr win; |
| 92 |
RgnHandle header; |
| 93 |
short shadow_refcnt; |
| 94 |
TEHandle uri_te; |
| 95 |
ControlHandle back; |
| 96 |
ControlHandle fwd; |
| 97 |
ControlHandle go_stop; |
| 98 |
TVHandle output_tv; |
| 99 |
ControlHandle output_tv_scroller; |
| 100 |
Rect status_rect; |
| 101 |
unsigned short status_length; |
| 102 |
short redirs; |
| 103 |
char status_text[256]; |
| 104 |
|
| 105 |
page_handle current_page; |
| 106 |
page_handle loading_page; |
| 107 |
|
| 108 |
unsigned long style; |
| 109 |
struct browser_link *first_link; |
| 110 |
struct browser_link *last_link; |
| 111 |
struct browser_link *hover_link; |
| 112 |
}; |
| 113 |
|
| 114 |
struct page_handler { |
| 115 |
/* can this handler process this URI? */ |
| 116 |
bool (*accept)(struct URI *uri); |
| 117 |
|
| 118 |
/* build a request to acquire content */ |
| 119 |
bool (*request_init)(page_handle pageh); |
| 120 |
|
| 121 |
/* queue outbound content, return false when done */ |
| 122 |
bool (*request_data_queuer)(struct request *request, void *cookie, |
| 123 |
char **buf, size_t *len, bool did_write); |
| 124 |
|
| 125 |
/* consume inbound content, return false when done */ |
| 126 |
bool (*request_data_consumer)(struct request *request, void *cookie, |
| 127 |
char **buf, size_t *len, bool did_read); |
| 128 |
|
| 129 |
/* free request, maybe change content before passing to parser */ |
| 130 |
void (*request_cleanup)(page_handle pageh); |
| 131 |
|
| 132 |
/* parse existing content, print to browser, return false when done */ |
| 133 |
bool (*process)(page_handle pageh); |
| 134 |
|
| 135 |
/* reset an existing page to be parsed again with no request */ |
| 136 |
void (*reset)(page_handle pageh); |
| 137 |
|
| 138 |
/* free anything else the page allocated before we xfree page obj */ |
| 139 |
void (*free)(page_handle pageh); |
| 140 |
}; |
| 141 |
|
| 142 |
struct browser * browser_init(void); |
| 143 |
size_t browser_statusf(struct browser *browser, const char *format, ...); |
| 144 |
size_t browser_print_link(struct browser *browser, const char *url, |
| 145 |
size_t url_len, const char *title, size_t title_len, bool newline); |
| 146 |
size_t browser_print(struct browser *browser, const char *str, size_t len, |
| 147 |
bool newline); |
| 148 |
size_t browser_print_bitmap(struct browser *browser, BitMap *icon, |
| 149 |
FontInfo *sizing); |
| 150 |
void browser_supress_updates(struct browser *browser, bool supress); |
| 151 |
void browser_recalc_scrollbar(struct browser *browser); |
| 152 |
struct page * browser_grow_page_content(struct page *page, size_t len); |
| 153 |
void browser_go_uri_str(struct browser *browser, char *str); |
| 154 |
void browser_commit_to_loading_page(struct browser *browser); |
| 155 |
bool browser_start_download(struct browser *browser, char *filename, |
| 156 |
char *dump_buf, size_t dump_len); |
| 157 |
|
| 158 |
/* generic versions */ |
| 159 |
|
| 160 |
bool page_print_plaintext(page_handle pageh); |
| 161 |
bool page_queue_output(struct request *request, void *cookie, char **buf, |
| 162 |
size_t *len, bool did_write); |
| 163 |
bool page_consume_data(struct request *request, void *cookie, char **buf, |
| 164 |
size_t *len, bool did_read); |
| 165 |
void page_request_cleanup(page_handle pageh); |
| 166 |
|
| 167 |
#endif |