main.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <flutter/flutter_window_controller.h>
  2. #include <linux/limits.h>
  3. #include <unistd.h>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <memory>
  7. #include <vector>
  8. #include "flutter/generated_plugin_registrant.h"
  9. #include "window_configuration.h"
  10. namespace {
  11. // Returns the path of the directory containing this executable, or an empty
  12. // string if the directory cannot be found.
  13. std::string GetExecutableDirectory() {
  14. char buffer[PATH_MAX + 1];
  15. ssize_t length = readlink("/proc/self/exe", buffer, sizeof(buffer));
  16. if (length > PATH_MAX) {
  17. std::cerr << "Couldn't locate executable" << std::endl;
  18. return "";
  19. }
  20. std::string executable_path(buffer, length);
  21. size_t last_separator_position = executable_path.find_last_of('/');
  22. if (last_separator_position == std::string::npos) {
  23. std::cerr << "Unabled to find parent directory of " << executable_path
  24. << std::endl;
  25. return "";
  26. }
  27. return executable_path.substr(0, last_separator_position);
  28. }
  29. } // namespace
  30. int main(int argc, char **argv) {
  31. // Resources are located relative to the executable.
  32. std::string base_directory = GetExecutableDirectory();
  33. if (base_directory.empty()) {
  34. base_directory = ".";
  35. }
  36. std::string data_directory = base_directory + "/data";
  37. std::string assets_path = data_directory + "/flutter_assets";
  38. std::string icu_data_path = data_directory + "/icudtl.dat";
  39. // Arguments for the Flutter Engine.
  40. std::vector<std::string> arguments;
  41. flutter::FlutterWindowController flutter_controller(icu_data_path);
  42. flutter::WindowProperties window_properties = {};
  43. window_properties.title = kFlutterWindowTitle;
  44. window_properties.width = kFlutterWindowWidth;
  45. window_properties.height = kFlutterWindowHeight;
  46. // Start the engine.
  47. if (!flutter_controller.CreateWindow(window_properties, assets_path,
  48. arguments)) {
  49. return EXIT_FAILURE;
  50. }
  51. RegisterPlugins(&flutter_controller);
  52. // Run until the window is closed.
  53. while (flutter_controller.RunEventLoopWithTimeout(
  54. std::chrono::milliseconds::max())) {
  55. }
  56. return EXIT_SUCCESS;
  57. }