You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

163 lines
6.2 KiB

#include "CommandLineParams.h"
#include "graphics/opengl/Window.h"
#include "environment/Environment.h"
#include "html/HTMLParser.h"
#include "Log.h"
#include "URL.h"
#include "WebResource.h"
#include "scheduler.h"
#include "CFGFileParser.h"
#include <ctime>
#include <iostream>
#include <sys/stat.h>
#include <string.h>
#ifdef _WIN32
extern "C"{
void init_heap();
}
#endif
#if defined(_WIN32) && !defined(_WIN64)
#define PLATFORM "i686-pc-winnt"
#endif
#ifdef _WIN64
#define PLATFORM "amd64-pc-winnt"
#endif
const std::unique_ptr<Window> window = std::make_unique<Window>();
// why can't I const this?
std::unique_ptr<Scheduler> scheduler = std::make_unique<Scheduler>();
//URL currentURL;
bool setWindowContent(URL const& url) {
logDebug() << "main::setWindowContent - " << url << std::endl;
// download URL
WebResource res = getWebResource(url);
if (res.resourceType == ResourceType::INVALID) {
logError() << "Invalid resource type: " << res.raw << std::endl;
return false;
}
// parse HTML
if (res.resourceType == ResourceType::HTML) {
HTMLParser parser;
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser.parse(res.raw);
const std::clock_t end = clock();
logDebug() << "main::setWindowContent - Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
// send NodeTree to window
window->setDOM(rootNode);
} else if (res.resourceType == ResourceType::TXT) {
std::cout << "Rendering text document" << std::endl;
std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT);
std::shared_ptr<TagNode> tagNode = std::make_shared<TagNode>();
tagNode->tag="p";
// bind tag to root
tagNode->parent = rootNode;
rootNode->children.push_back(tagNode);
std::shared_ptr<TextNode> textNode = std::make_shared<TextNode>();
textNode->text = res.raw;
// bind text to tag
textNode->parent = tagNode;
tagNode->children.push_back(textNode);
// send NodeTree to window
window->setDOM(rootNode);
} else {
std::cout << "setWindowContent() - I don't know how to render non-html files" << std::endl;
}
return true;
}
bool isAbsolutePath(const std::string s);
bool isAbsolutePath(const std::string s) {
return (s.length() > 0 && s[0] == '/');
}
bool fileExists(const std::string s);
bool fileExists(const std::string s) {
struct stat buf;
return stat(s.c_str(), &buf) != -1;
}
int main(int argc, char *argv[]) {
// show help msg when "--help" appears
if (argv[1] && (strcmp(argv[1], "--help")==0)) {
std::cout << "./netrunner [http://host.tld/|/path/to/file.html] [-log <error|warning|notice|info|debug>]" << std::endl;
return 1;
}
#ifdef _WIN32
init_heap(); // the NT port requires it. We do it at startup now, to allow 2LSF to run at any time
#endif
#if defined(VERSION) && defined(PLATFORM)
std::cout << "# # ######" << std::endl << "## # ###### ##### # # # # # # # # ###### #####" << std::endl << "# # # # # # # # # ## # ## # # # #" << std::endl << "# # # ##### # ###### # # # # # # # # ##### # #" << std::endl << "# # # # # # # # # # # # # # # # #####" << std::endl << "# ## # # # # # # # ## # ## # # #" << std::endl << "# # ###### # # # #### # # # # ###### # #" << std::endl;
std::cout << "/g/ntr - NetRunner build " << __DATE__ << ": rev-" << VERSION << " for " << PLATFORM << std::endl;
#else
std::cout << "/g/ntr - NetRunner build " << __DATE__ << std::endl;
#endif
Environment::init();
CFGFileParser *parser = new CFGFileParser("lynx.cfg");
parser->ParseText();
// we need to set up OGL before we can setDOM (because component can't be constructed (currently) without OGL)
// but should be after CommandLineParams incase we need to change some type of window config
window->windowWidth = 1024;
window->windowHeight = 640;
window->init();
if (!window->window) {
return 1;
}
//std::cout << "argc " << argc << std::endl;
if (argc > 1) {
initCLParams(argc, argv);
// this isn't going to work
std::string rawUrl = getCLParamByIndex(1);
// if we do this here, shouldn't we do this in parseUri too?
if (rawUrl.find("://") == rawUrl.npos) {
// Path should always be absolute for file://
if (isAbsolutePath(rawUrl)) {
rawUrl = "file://" + rawUrl;
} else {
auto absolutePath = std::string(getenv("PWD")) + '/' + rawUrl;
if (fileExists(absolutePath)) {
rawUrl = "file://" + absolutePath;
} else {
// Default to http if the file wasn't found
rawUrl = "http://" + rawUrl;
}
}
}
//logDebug() << "pre URL parse [" << url << "]" << std::endl;
window->currentURL = URL(rawUrl);
logDebug() << "loading [" << window->currentURL << "]" << std::endl;
if (!setWindowContent(window->currentURL)) {
return 1;
}
}
while (!glfwWindowShouldClose(window->window)) {
//const std::clock_t begin = clock();
window->render();
scheduler->fireTimers(); // render may have taken some time
double next = scheduler->getNext();
//std::cout << "next timer at " << next << std::endl;
if (next == LONG_MAX) {
glfwWaitEvents(); // block until something changes
} else {
glfwWaitEventsTimeout(next / 1000);
}
scheduler->fireTimers(); // check before we go into render again
//glfwWaitEventsTimeout(1.0 / 60.0); // increase the cpu from 0% to 2% on idle
//const std::clock_t end = clock();
//std::cout << '\r' << std::fixed << (((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) * 1000) << std::scientific << " ms/f " << std::flush;
}
return 0;
}