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.

main.cpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include <ctime>
  2. #include <iostream>
  3. #include <sys/stat.h>
  4. #include <string.h>
  5. #include "app/browser/browser.h"
  6. #include "environment/Environment.h"
  7. #include "interfaces/components/TabbedComponent.h"
  8. #include "interfaces/components/InputComponent.h"
  9. #include "interfaces/graphical/renderers/glfw/opengl.h"
  10. #include "parsers/images/netpbm/pnm.h"
  11. #include "platform/tlsf.h"
  12. #include "tools/CFGFileParser.h"
  13. #include "tools/CommandLineParams.h"
  14. #include "tools/Log.h"
  15. #include "tools/URL.h"
  16. #include "tools/WebResource.h"
  17. #if defined(_WIN32) && !defined(_WIN64)
  18. #define PLATFORM "i686-pc-winnt"
  19. #endif
  20. #ifdef _WIN64
  21. #define PLATFORM "amd64-pc-winnt"
  22. #endif
  23. // global
  24. // to remove this
  25. // window uses this to make windows and get window count (can be just be passed in when creating a window)
  26. // AElement uses it to getActiveDocumentComponent and navTo on that
  27. //
  28. std::unique_ptr<Browser> browser = nullptr;
  29. std::unique_ptr<BaseRenderer> renderer = nullptr;
  30. // forward declaration
  31. bool isAbsolutePath(const std::string s);
  32. bool fileExists(const std::string s);
  33. bool isAbsolutePath(const std::string s) {
  34. return (s.length() > 0 && s[0] == '/');
  35. }
  36. bool fileExists(const std::string s) {
  37. struct stat buf;
  38. return stat(s.c_str(), &buf) != -1;
  39. }
  40. extern TextRasterizerCache *rasterizerCache;
  41. #include <iostream>
  42. #include <fstream>
  43. std::ofstream assignfile;
  44. std::ofstream execfile;
  45. int main(int argc, char *argv[]) {
  46. // show help msg when "--help" appears
  47. if (argc > 1) {
  48. // has to be set up for logging before first logging call
  49. initCLParams(argc, argv);
  50. if (strcmp(argv[1], "--help")==0) {
  51. std::cout << "./netrunner [http://host.tld/|/path/to/file.html] [-log <error|warning|notice|info|debug>]" << std::endl;
  52. return 1;
  53. }
  54. }
  55. std::ofstream myfile;
  56. assignfile.open ("js_assignments.txt");
  57. execfile.open ("js_execution.txt");
  58. std::cout \
  59. << "# # ######" << std::endl \
  60. << "## # ###### ##### # # # # # # # # ###### #####" << std::endl \
  61. << "# # # # # # # # # ## # ## # # # #" << std::endl \
  62. << "# # # ##### # ###### # # # # # # # # ##### # #" << std::endl \
  63. << "# # # # # # # # # # # # # # # # #####" << std::endl \
  64. << "# ## # # # # # # # ## # ## # # #" << std::endl \
  65. << "# # ###### # # # #### # # # # ###### # #" << std::endl;
  66. #if defined(VERSION) && defined(PLATFORM)
  67. std::cout << "/g/ntr - NetRunner build " << __DATE__ << ": rev-" << VERSION << " for " << PLATFORM << std::endl;
  68. #else
  69. std::cout << "/g/ntr - NetRunner build " << __DATE__ << ": internal-dev non-Makefile build" << std::endl;
  70. #endif
  71. Environment::init();
  72. CFGFileParser *parser = new CFGFileParser("res/netrunner.cfg");
  73. BrowserConfiguration *config = new BrowserConfiguration();
  74. if (!parser) {
  75. std::cout << "no parser!" << std::endl;
  76. }
  77. if (parser->ParseText()){
  78. parser->WriteConfig(*config);
  79. }
  80. delete parser;
  81. std::cout << "Global Settings contains, in no particular order:\n";
  82. for ( auto it = config->Settings.begin(); it != config->Settings.end(); ++it ){
  83. std::cout << it->first << ":" << it->second << std::endl;
  84. }
  85. std::cout << std::endl;
  86. renderer = std::make_unique<OpenGL>();
  87. //Environment::init();
  88. renderer->initialize();
  89. /*
  90. Rect pos;
  91. pos.x=0;
  92. pos.y=0;
  93. pos.w=1024;
  94. pos.h=640;
  95. OpenGLWindowHandle *win = renderer.createWindow("NTR", &pos, 0);
  96. if (!win) {
  97. std::cout << "Couldn't create window" << std::endl;
  98. return 0;
  99. }
  100. OpenGLTexture *blue = win->createSpriteFromColor(0x0000FFFF);
  101. if (!blue) {
  102. std::cout << "Couldn't create blue" << std::endl;
  103. return 0;
  104. }
  105. Rect dPos = { 0, 0, 512, 320 };
  106. */
  107. /*
  108. const std::shared_ptr<TextRasterizer> textRasterizer = rasterizerCache->loadFont(12, false);
  109. rasterizationRequest request;
  110. request.text = "Hello World";
  111. request.startX = 512;
  112. request.availableWidth = 1024;
  113. request.maxTextureSize = 16384;
  114. request.noWrap = true;
  115. //std::cout << "rasterizing [" << text << "] @" << rawX << " availableWidth: " << availableWidth << " sourceStartX: " << rasterStartX << " noWrap: " << noWrap << std::endl;
  116. std::shared_ptr<rasterizationResponse> response = textRasterizer->rasterize(request);
  117. textureMap textureMap;
  118. textureMap.map[0] = response->s0;
  119. textureMap.map[1] = response->t0;
  120. textureMap.map[2] = response->s1;
  121. textureMap.map[3] = response->t1;
  122. OpenGLTexture *text = win->createTextSprite(response->textureData.get(), response->textureWidth, response->textureHeight, textureMap);
  123. Rect dPos3 = { 512, 320, 512, 320};
  124. */
  125. /*
  126. RGBAPNMObject *anime = readPPM("kon32_661.pam");
  127. if (!anime) {
  128. std::cout << "Couldn't load image" << std::endl;
  129. return 0;
  130. }
  131. opengl_texture_handle *red = win->createSprite(anime->m_Ptr, anime->width, anime->height);
  132. Rect dPos2 = { 512, 320, 512, 320};
  133. */
  134. /*
  135. win->clear();
  136. win->drawSpriteBox(blue, &dPos);
  137. win->drawSpriteText(text, 0x000000FF, &dPos3);
  138. //win->drawSpriteBox(red, &dPos2);
  139. win->swapBuffers();
  140. while(1) {
  141. glfwWaitEvents(); // block until something changes
  142. }
  143. return 0;
  144. */
  145. // we need to set up OGL before we can setDOM (because component can't be constructed (currently) without OGL)
  146. // but should be after CommandLineParams incase we need to change some type of window config
  147. browser = std::make_unique<Browser>();
  148. for ( auto it = config->Settings.begin(); it != config->Settings.end(); ++it ){
  149. if (it->first == "CONSOLE" && it->second == "TRUE") {
  150. std::cout << "turning on JS console" << std::endl;
  151. browser->addJSDebuggerWindow();
  152. }
  153. if (it->first == "JAVASCRIPT" && it->second == "TRUE") {
  154. std::cout << "turning on JS Javascript" << std::endl;
  155. browser->jsEnable = true;
  156. }
  157. }
  158. browser->addWindow();
  159. if (!browser->windows.front().get()->window) {
  160. std::cout << "main() - unable to add a window" << std::endl;
  161. return 1;
  162. }
  163. //std::cout << "argc " << argc << std::endl;
  164. if (argc > 1) {
  165. // this isn't going to work
  166. std::string rawUrl = getCLParamByIndex(1);
  167. if (rawUrl != "-log") {
  168. // if we do this here, shouldn't we do this in parseUri too?
  169. if (rawUrl.find("://") == rawUrl.npos) {
  170. // Path should always be absolute for file://
  171. if (isAbsolutePath(rawUrl)) {
  172. rawUrl = "file://" + rawUrl;
  173. } else {
  174. auto absolutePath = std::string(getenv("PWD")) + '/' + rawUrl;
  175. if (fileExists(absolutePath)) {
  176. rawUrl = "file://" + absolutePath;
  177. } else {
  178. // Default to http if the file wasn't found
  179. rawUrl = "http://" + rawUrl;
  180. }
  181. }
  182. }
  183. //logDebug() << "pre URL parse [" << url << "]" << std::endl;
  184. //window->currentURL = URL(rawUrl);
  185. logDebug() << "loading [" << rawUrl << "]" << std::endl;
  186. TabbedComponent *tabComponent = dynamic_cast<TabbedComponent*>(browser->windows.front().get()->tabComponent.get());
  187. if (tabComponent) {
  188. tabComponent->addTab("Start Tab, Loading...");
  189. tabComponent->selectTab(tabComponent->tabs.front());
  190. // render the loading label, before we block for loading
  191. browser->render();
  192. }
  193. /*
  194. if (!setWindowContent(window->currentURL)) {
  195. return 1;
  196. }
  197. */
  198. std::shared_ptr<DocumentComponent> docComponent = browser->getActiveDocumentComponent();
  199. if (docComponent) {
  200. // now tell it to navigate somewhere
  201. docComponent->navTo(rawUrl);
  202. }
  203. } else {
  204. // be nice if we could put http:// in the address bar
  205. }
  206. }
  207. browser->loop();
  208. return 0;
  209. }