diff options
author | Vincent Sanders <vince@kyllikki.org> | 2024-01-05 00:09:23 +0000 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2024-02-25 10:00:56 +0000 |
commit | bf50c7d7a1f48135641631a4f299dde08214ca07 (patch) | |
tree | 1595f9c16ef72b18feb2df563f7a88ba0374477a | |
parent | 4cec093d2e9306f0d036963af1f499c8f4014755 (diff) | |
download | netsurf-bf50c7d7a1f48135641631a4f299dde08214ca07.tar.gz netsurf-bf50c7d7a1f48135641631a4f299dde08214ca07.tar.bz2 |
move qt application code to a suitable class
-rw-r--r-- | frontends/qt/Makefile | 3 | ||||
-rw-r--r-- | frontends/qt/application.cls.h | 56 | ||||
-rw-r--r-- | frontends/qt/application.cpp | 218 | ||||
-rw-r--r-- | frontends/qt/main.cpp | 222 | ||||
-rw-r--r-- | frontends/qt/misc.cpp | 7 | ||||
-rw-r--r-- | frontends/qt/window.cls.h | 1 | ||||
-rw-r--r-- | frontends/qt/window.cpp | 5 |
7 files changed, 305 insertions, 207 deletions
diff --git a/frontends/qt/Makefile b/frontends/qt/Makefile index 286d34cab..756b7eda7 100644 --- a/frontends/qt/Makefile +++ b/frontends/qt/Makefile @@ -37,7 +37,7 @@ S_RESOURCE := # Meta Object Compiler (MOC) handling # ---------------------------------------------------------------------------- -MOC_CLASS := window widget urlbar scaffolding +MOC_CLASS := application window widget urlbar scaffolding # Generate cpp from h via Qt's Meta Object Compiler $(OBJROOT)/%.moc.cpp: $(FRONTEND_SOURCE_DIR)/%.cls.h @@ -52,6 +52,7 @@ S_MOC := $(foreach CLS,$(MOC_CLASS),$(OBJROOT)/$(CLS).moc.cpp) # S_FRONTEND are sources purely for the QT frontend S_FRONTEND := main.cpp \ + application.cpp \ misc.cpp \ scaffolding.cpp \ window.cpp \ diff --git a/frontends/qt/application.cls.h b/frontends/qt/application.cls.h new file mode 100644 index 000000000..38ecba219 --- /dev/null +++ b/frontends/qt/application.cls.h @@ -0,0 +1,56 @@ +/* + * Copyright 2024 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/>. + */ + +/** + * \file + * Application class for QT frontend. + */ + +#include <QApplication> +#include <QTimer> + +extern "C" { +#include "utils/errors.h" +} + +class NS_Exception { +public: + std::string m_str; + nserror m_err; + + NS_Exception(std::string str, nserror err):m_str(str),m_err(err) {} +}; + +/** + * class for netsurf application + */ +class NS_Application : public QApplication +{ + Q_OBJECT +public: + NS_Application(int &argc, char **argv); + ~NS_Application(); + nserror start(int argc, char** argv); + void next_schedule(int ms); +public slots: + void schedule_run(); +private: + QTimer *m_schedule_timer; + static bool nslog_stream_configure(FILE *fptr); + static nserror set_option_defaults(struct nsoption_s *defaults); +}; diff --git a/frontends/qt/application.cpp b/frontends/qt/application.cpp new file mode 100644 index 000000000..9bd76b5ec --- /dev/null +++ b/frontends/qt/application.cpp @@ -0,0 +1,218 @@ +/* + * Copyright 2024 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/>. + */ + +/** + * \file + * Implementation of netsurf application for qt frontend. + */ + +#include <sys/stat.h> + +extern "C" { +#include "utils/utils.h" +#include "utils/errors.h" +#include "utils/log.h" +#include "utils/nsoption.h" +#include "utils/nsurl.h" + +#include "netsurf/bitmap.h" +#include "netsurf/netsurf.h" +#include "netsurf/content.h" +#include "netsurf/browser_window.h" +} + +#include "qt/resources.h" +#include "qt/misc.h" + +#include "qt/application.cls.h" + +//QTimer +//QSocketNotifier + +/** + * Ensures output logging stream is correctly configured + */ +bool NS_Application::nslog_stream_configure(FILE *fptr) +{ + /* set log stream to be non-buffering */ + setbuf(fptr, NULL); + + return true; +} + +/** + * Set option defaults for qt frontend + * + * @param defaults The option table to update. + * @return error status. + */ +nserror NS_Application::set_option_defaults(struct nsoption_s *defaults) +{ + return NSERROR_OK; +} + +NS_Application::NS_Application(int &argc, char **argv) + :QApplication(argc, argv) +{ + nserror res; + + // set up scheduler timer + m_schedule_timer = new QTimer(this); + m_schedule_timer->setSingleShot(true); + connect(m_schedule_timer, &QTimer::timeout, this, &NS_Application::schedule_run); + + /* Prep the resource search paths */ + res = nsqt_init_resource_path("${HOME}/.netsurf/:${NETSURFRES}:" QT_RESPATH); + if (res != NSERROR_OK) { + throw NS_Exception("Resources failed to initialise",res); + } + + /* Initialise logging. Not fatal if it fails but not much we + * can do about it either. + */ + nslog_init(nslog_stream_configure, &argc, argv); + + /* override loaded options with those from commandline */ + nsoption_commandline(&argc, argv, nsoptions); + + /* Initialise user options */ + res = nsoption_init(set_option_defaults, &nsoptions, &nsoptions_default); + if (res != NSERROR_OK) { + throw NS_Exception("Options failed to initialise", res); + } + + + bitmap_fmt_t qtfmt = { + .layout = BITMAP_LAYOUT_ARGB8888, + .pma = false, + }; + + // set bitmap format + bitmap_set_format(&qtfmt ); + + +} + +NS_Application::~NS_Application() +{ + + /* common finalisation */ + netsurf_exit(); + + /* finalise options */ + nsoption_finalise(nsoptions, nsoptions_default); + + /* finalise logging */ + nslog_finalise(); + + delete m_schedule_timer; + +} + +/** + * scheduled timer slot + */ +void NS_Application::schedule_run() +{ + int ms; + ms = nsqt_schedule_run(); + if (ms >=0) { + m_schedule_timer->start(ms); + } +} + +/** + * + */ +void NS_Application::next_schedule(int ms) +{ + if ((m_schedule_timer->isActive()==false) || + (m_schedule_timer->remainingTime() > ms)) { + m_schedule_timer->start(ms); + } +} + +/** + * Initial start of qt browser. + * + * performs qt 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. + */ +nserror NS_Application::start(int argc, char** argv) +{ + char *addr = NULL; + nsurl *url; + nserror res; + + /* netsurf initialisation */ + res = netsurf_init(NULL); + if (res != NSERROR_OK) { + return 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); + } + + /* convert initial target to url*/ + res = nsurl_create(addr, &url); + free(addr); + if (res != NSERROR_OK) { + return res; + } + + /* create an initial browser window */ + res = browser_window_create(BW_CREATE_HISTORY, + url, + NULL, + NULL, + NULL); + nsurl_unref(url); + if (res != NSERROR_OK) { + return res; + } + + /* startup succeeded so run event loop */ + exec(); + + return res; +} diff --git a/frontends/qt/main.cpp b/frontends/qt/main.cpp index b012439e2..86e697050 100644 --- a/frontends/qt/main.cpp +++ b/frontends/qt/main.cpp @@ -16,26 +16,16 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <assert.h> -#include <sys/stat.h> -#include <string.h> -#include <QApplication> extern "C" { - -#include "utils/utils.h" #include "utils/log.h" #include "utils/messages.h" -#include "utils/nsoption.h" -#include "utils/nsurl.h" -#include "netsurf/bitmap.h" #include "netsurf/netsurf.h" -#include "netsurf/content.h" -#include "netsurf/browser_window.h" - } +#include "qt/application.cls.h" + #include "qt/misc.h" #include "qt/window.h" #include "qt/fetch.h" @@ -43,165 +33,8 @@ extern "C" { #include "qt/layout.h" #include "qt/resources.h" -bool nsqt_done = false; - -/** - * Set option defaults for qt 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; -} - -/** - * qt frontend specific initialisation - */ -static nserror nsqt_init(int *pargc, char** argv,QApplication **papp) -{ - nserror res; - QApplication *app; - - /* qt application */ - app = new QApplication(*pargc, argv); - if (app == NULL) { - return NSERROR_NOMEM; - } - *papp = app; - - /* Prep the resource search paths */ - res = nsqt_init_resource_path("${HOME}/.netsurf/:${NETSURFRES}:" QT_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 qt browser. - * - * performs qt 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 nsqt_start(int argc, char** argv) -{ - char *addr = NULL; - nsurl *url; - nserror res; - bitmap_fmt_t qtfmt = { - .layout = BITMAP_LAYOUT_ARGB8888, - .pma = false, - }; - - // set bitmap format - bitmap_set_format(&qtfmt ); - - /* 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 qt event loop. - * - */ -static void nsqt_run(QApplication *app) -{ - int schedtm; - do { - /* run scheduled callbacks and get the next event delta in ms */ - schedtm = nsqt_schedule_run(); - app->processEvents(QEventLoop::AllEvents, schedtm); - } while(nsqt_done != true); -} - -static nserror nsqt_finalise(void) -{ - /* common finalisation */ - netsurf_exit(); - - /* finalise options */ - nsoption_finalise(nsoptions, nsoptions_default); - - /* finalise logging */ - nslog_finalise(); - - return NSERROR_OK; -} +NS_Application *nsqtapp; /** * Main entry point from OS. @@ -209,7 +42,6 @@ static nserror nsqt_finalise(void) int main(int argc, char** argv) { nserror res; - QApplication *app; struct netsurf_table nsqt_table = { .misc = nsqt_misc_table, .window = nsqt_window_table, @@ -234,37 +66,27 @@ int main(int argc, char** argv) return 1; } - /* qt specific initialisation */ - res = nsqt_init(&argc, argv, &app); - if (res != NSERROR_OK) { - fprintf(stderr, "NetSurf qt 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; - } + /* qt application */ + try { + nsqtapp = new NS_Application(argc, argv); + + /* qt specific startup */ + res = nsqtapp->start(argc, argv); + if (res != NSERROR_OK) { + fprintf(stderr, "NetSurf qt startup failed (%s)\n", + messages_get_errorcode(res)); + delete nsqtapp; + return 4; + } - /* qt specific startup */ - res = nsqt_start(argc, argv); - if (res != NSERROR_OK) { - fprintf(stderr, "NetSurf qt startup failed (%s)\n", - messages_get_errorcode(res)); - nsqt_finalise(); - return 4; + delete nsqtapp; + } catch(NS_Exception &exp) { + fprintf(stderr, + "NetSurf qt application initialisation failed. %s (%s)\n", + exp.m_str.c_str(), + messages_get_errorcode(exp.m_err)); + return 2; } - /* startup suceeded so main run loop */ - nsqt_run(app); - - /* finalise everything */ - nsqt_finalise(); - return 0; - } diff --git a/frontends/qt/misc.cpp b/frontends/qt/misc.cpp index d11f99703..09a58df18 100644 --- a/frontends/qt/misc.cpp +++ b/frontends/qt/misc.cpp @@ -36,6 +36,10 @@ extern "C" { #include "qt/misc.h" +#include "qt/application.cls.h" + +extern NS_Application *nsqtapp; + /* linked list of scheduled callbacks */ static struct nscallback *schedule_list = NULL; @@ -222,6 +226,9 @@ static nserror nsqt_schedule(int tival, void (*callback)(void *p), void *p) nscb->next = schedule_list; schedule_list = nscb; + // ensure timer will run the scheduler at appropriate time + nsqtapp->next_schedule(tival); + return NSERROR_OK; } diff --git a/frontends/qt/window.cls.h b/frontends/qt/window.cls.h index e50c94375..90a7dcdc3 100644 --- a/frontends/qt/window.cls.h +++ b/frontends/qt/window.cls.h @@ -59,7 +59,6 @@ protected: public: NS_Window(QWidget* parent, struct browser_window *bw); - ~NS_Window(); void destroy(void); diff --git a/frontends/qt/window.cpp b/frontends/qt/window.cpp index 1b2aaaa92..19c2d8d61 100644 --- a/frontends/qt/window.cpp +++ b/frontends/qt/window.cpp @@ -46,8 +46,6 @@ extern "C" { #include "qt/window.h" -extern bool nsqt_done; - struct gui_window { class NS_Window *window; }; @@ -109,9 +107,6 @@ NS_Window::NS_Window(QWidget *parent, struct browser_window *bw) layout->addWidget(splitter, 2,0); } -NS_Window::~NS_Window() { - nsqt_done=true; -} void NS_Window::closeEvent(QCloseEvent *event) { |