/* * Copyright (c) 2021-2024 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef __BROWSER_H__ #define __BROWSER_H__ #include #include "detritus.h" #include "textview.h" #include "util.h" //#define BROWSER_DEBUGF(x) browser_statusf #define BROWSER_DEBUGF(x) #define STYLE_NONE 0 #define STYLE_BOLD (1UL << 0) #define STYLE_ITALIC (1UL << 1) #define STYLE_H1 (1UL << 2) #define STYLE_H2 (1UL << 3) #define STYLE_H3 (1UL << 4) #define STYLE_H4 (1UL << 5) #define STYLE_H5 (1UL << 6) #define STYLE_H6 (1UL << 7) #define STYLE_LINK (1UL << 8) #define STYLE_PRE (1UL << 9) #define STYLE_LIST (1UL << 10) #define STYLE_QUOTE (1UL << 11) #define STYLE_UNDERLINE (1UL << 12) struct browser_link { char *uri; char *title; unsigned short pos; unsigned short len; BigRect rect; struct browser_link *next_link; }; typedef struct page **page_handle; struct page { struct URI *uri; struct browser *browser; page_handle back_page; page_handle fwd_page; struct page_handler *handler; void *handler_cookie; struct request *request; void *request_cookie; size_t content_size; #define PAGE_CONTENT_CHUNK_SIZE 1024 size_t content_len; size_t content_pos; size_t header_len; short parse_state; size_t server_content_len; short server_status; char content_type[32]; struct URI *redir_to; short download_frefnum; size_t download_len; struct TVStyle cur_style; char content[]; }; #define PAGE_CAN_READ_MORE(page) ((page)->request && \ !(page)->request->tcp_done_reading) struct browser { WindowPtr win; RgnHandle header; short shadow_refcnt; TEHandle uri_te; ControlHandle back; ControlHandle fwd; ControlHandle go_stop; TVHandle output_tv; ControlHandle output_tv_scroller; Rect status_rect; unsigned short status_length; short redirs; char status_text[256]; page_handle current_page; page_handle loading_page; unsigned long style; struct browser_link *first_link; struct browser_link *last_link; struct browser_link *hover_link; }; struct page_handler { /* can this handler process this URI? */ bool (*accept)(struct URI *uri); /* build a request to acquire content */ bool (*request_init)(page_handle pageh); /* queue outbound content, return false when done */ bool (*request_data_queuer)(struct request *request, void *cookie, char **buf, size_t *len, bool did_write); /* consume inbound content, return false when done */ bool (*request_data_consumer)(struct request *request, void *cookie, char **buf, size_t *len, bool did_read); /* free request, maybe change content before passing to parser */ void (*request_cleanup)(page_handle pageh); /* parse existing content, print to browser, return false when done */ bool (*process)(page_handle pageh); /* reset an existing page to be parsed again with no request */ void (*reset)(page_handle pageh); /* free anything else the page allocated before we xfree page obj */ void (*free)(page_handle pageh); }; struct browser * browser_init(void); size_t browser_statusf(struct browser *browser, const char *format, ...); size_t browser_print_link(struct browser *browser, const char *url, size_t url_len, const char *title, size_t title_len, bool newline); size_t browser_print(struct browser *browser, const char *str, size_t len, bool newline); size_t browser_print_bitmap(struct browser *browser, BitMap *icon, FontInfo *sizing); void browser_supress_updates(struct browser *browser, bool supress); void browser_recalc_scrollbar(struct browser *browser); struct page * browser_grow_page_content(struct page *page, size_t len); void browser_go_uri_str(struct browser *browser, char *str); void browser_commit_to_loading_page(struct browser *browser); bool browser_start_download(struct browser *browser, char *filename, char *dump_buf, size_t dump_len); /* generic versions */ bool page_print_plaintext(page_handle pageh); bool page_queue_output(struct request *request, void *cookie, char **buf, size_t *len, bool did_write); bool page_consume_data(struct request *request, void *cookie, char **buf, size_t *len, bool did_read); void page_request_cleanup(page_handle pageh); #endif