diff options
Diffstat (limited to 'frontends/fltk/main.cpp')
-rw-r--r-- | frontends/fltk/main.cpp | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/frontends/fltk/main.cpp b/frontends/fltk/main.cpp new file mode 100644 index 000000000..ab0e1bca5 --- /dev/null +++ b/frontends/fltk/main.cpp @@ -0,0 +1,253 @@ +/* + * Copyright 2021 Vincent Sanders <vince@netsurf-browser.org> + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <assert.h> +#include <sys/stat.h> +#include <string.h> +#include <FL/Fl.H> + +extern "C" { + +#include "utils/utils.h" +#include "utils/log.h" +#include "utils/messages.h" +#include "utils/nsoption.h" +#include "utils/nsurl.h" + +#include "netsurf/netsurf.h" +#include "netsurf/content.h" +#include "netsurf/browser_window.h" + +} + +#include "fltk/misc.h" +#include "fltk/window.h" +#include "fltk/fetch.h" +#include "fltk/bitmap.h" +#include "fltk/layout.h" +#include "fltk/resources.h" + +bool nsfltk_done = false; + +/** + * Set option defaults for fltk frontend + * + * @param defaults The option table to update. + * @return error status. + */ +static nserror set_option_defaults(struct nsoption_s *defaults) +{ + return NSERROR_OK; +} + + +/** + * Ensures output logging stream is correctly configured + */ +static bool nslog_stream_configure(FILE *fptr) +{ + /* set log stream to be non-buffering */ + setbuf(fptr, NULL); + + return true; +} + + +/** + * fltk frontend specific initialisation + */ +static nserror nsfltk_init(int *pargc, char** argv) +{ + nserror res; + + /* Prep the resource search paths */ + res = nsfltk_init_resource_path("${HOME}/.netsurf/:${NETSURFRES}:" FLTK_RESPATH); + if (res != NSERROR_OK) { + fprintf(stderr, "Resources failed to initialise (%s)\n", + messages_get_errorcode(res)); + return res; + } + + /* Initialise logging. Not fatal if it fails but not much we + * can do about it either. + */ + nslog_init(nslog_stream_configure, pargc, argv); + + /* override loaded options with those from commandline */ + nsoption_commandline(pargc, argv, nsoptions); + + /* Initialise user options */ + res = nsoption_init(set_option_defaults, &nsoptions, &nsoptions_default); + if (res != NSERROR_OK) { + fprintf(stderr, "Options failed to initialise (%s)\n", + messages_get_errorcode(res)); + return res; + } + + return NSERROR_OK; +} + + +/** + * Start fltk browser. + * + * performs fltk specific startup including opening initial window if necessary + * + * \param argc The number of arguments on the command line + * \param argv A string vector of command line arguments. + */ +static nserror nsfltk_start(int argc, char** argv) +{ + char *addr = NULL; + nsurl *url; + nserror res; + + /* If there is a url specified on the command line use it */ + if (argc > 1) { + struct stat fs; + if (stat(argv[1], &fs) == 0) { + size_t addrlen; + char *rp = realpath(argv[1], NULL); + assert(rp != NULL); + + /* calculate file url length including terminator */ + addrlen = SLEN("file://") + strlen(rp) + 1; + addr = (char *)malloc(addrlen); + assert(addr != NULL); + snprintf(addr, addrlen, "file://%s", rp); + free(rp); + } else { + addr = strdup(argv[1]); + } + } + if (addr != NULL) { + /* managed to set up based on local launch */ + } else if (nsoption_charp(homepage_url) != NULL) { + addr = strdup(nsoption_charp(homepage_url)); + } else { + addr = strdup(NETSURF_HOMEPAGE); + } + + /* create an initial browser window */ + res = nsurl_create(addr, &url); + if (res == NSERROR_OK) { + res = browser_window_create(BW_CREATE_HISTORY, + url, + NULL, + NULL, + NULL); + nsurl_unref(url); + } + + free(addr); + + return res; +} + +/** + * Run the fltk event loop. + * + */ +static void nsfltk_run(void) +{ + int schedtm; + do { + /* run scheduled callbacks and get the next event delta in ms */ + schedtm = nsfltk_schedule_run(); + Fl::wait(((double)schedtm/1000)); + } while(nsfltk_done != true); +} + +static nserror nsfltk_finalise(void) +{ + /* common finalisation */ + netsurf_exit(); + + /* finalise options */ + nsoption_finalise(nsoptions, nsoptions_default); + + /* finalise logging */ + nslog_finalise(); + + return NSERROR_OK; +} + +/** + * Main entry point from OS. + */ +int main(int argc, char** argv) +{ + nserror res; + struct netsurf_table nsfltk_table = { + .misc = nsfltk_misc_table, + .window = nsfltk_window_table, + .download = NULL, /* no download functionality */ + .clipboard = NULL, /* no clipboard functionality */ + .fetch = nsfltk_fetch_table, + .file = NULL, /* use the posix default file operations */ + .utf8 = NULL, /* use default utf-8 processing */ + .search = NULL, /* use the default text search */ + .search_web = NULL, /* use default web search */ + .llcache = NULL, /* use default low level cache storage */ + .bitmap = nsfltk_bitmap_table, + .layout = nsfltk_layout_table, + }; + + /* register operation tables */ + res = netsurf_register(&nsfltk_table); + if (res != NSERROR_OK) { + fprintf(stderr, + "NetSurf operation table failed registration (%s)\n", + messages_get_errorcode(res)); + return 1; + } + + /* fltk specific initialisation */ + res = nsfltk_init(&argc, argv); + if (res != NSERROR_OK) { + fprintf(stderr, "NetSurf fltk initialisation failed (%s)\n", + messages_get_errorcode(res)); + return 2; + } + + /* netsurf initialisation */ + res = netsurf_init(NULL); + if (res != NSERROR_OK) { + fprintf(stderr, "NetSurf core failed to initialise (%s)\n", + messages_get_errorcode(res)); + return 3; + } + + /* fltk specific startup */ + res = nsfltk_start(argc, argv); + if (res != NSERROR_OK) { + fprintf(stderr, "NetSurf fltk startup failed (%s)\n", + messages_get_errorcode(res)); + nsfltk_finalise(); + return 4; + } + + /* startup suceeded so main run loop */ + nsfltk_run(); + + /* finalise everything */ + nsfltk_finalise(); + + return 0; + +} |