flutter_embedder_example.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <linux/limits.h>
  15. #include <unistd.h>
  16. #include <cstdlib>
  17. #include <iostream>
  18. #include <memory>
  19. #include <vector>
  20. #include <flutter/flutter_window_controller.h>
  21. namespace {
  22. // Returns the path of the directory containing this executable, or an empty
  23. // string if the directory cannot be found.
  24. std::string GetExecutableDirectory() {
  25. char buffer[PATH_MAX + 1];
  26. ssize_t length = readlink("/proc/self/exe", buffer, sizeof(buffer));
  27. if (length > PATH_MAX) {
  28. std::cerr << "Couldn't locate executable" << std::endl;
  29. return "";
  30. }
  31. std::string executable_path(buffer, length);
  32. size_t last_separator_position = executable_path.find_last_of('/');
  33. if (last_separator_position == std::string::npos) {
  34. std::cerr << "Unabled to find parent directory of " << executable_path
  35. << std::endl;
  36. return "";
  37. }
  38. return executable_path.substr(0, last_separator_position);
  39. }
  40. } // namespace
  41. int main(int argc, char **argv) {
  42. // Resources are located relative to the executable.
  43. std::string base_directory = GetExecutableDirectory();
  44. if (base_directory.empty()) {
  45. base_directory = ".";
  46. }
  47. std::string data_directory = base_directory + "/data";
  48. std::string assets_path = data_directory + "/flutter_assets";
  49. std::string icu_data_path = data_directory + "/icudtl.dat";
  50. // Arguments for the Flutter Engine.
  51. std::vector<std::string> arguments;
  52. #ifdef NDEBUG
  53. arguments.push_back("--disable-dart-asserts");
  54. #endif
  55. flutter::FlutterWindowController flutter_controller(icu_data_path);
  56. // Start the engine.
  57. if (!flutter_controller.CreateWindow(800, 600, "Flutter Desktop Example",
  58. assets_path, arguments)) {
  59. return EXIT_FAILURE;
  60. }
  61. // Run until the window is closed.
  62. flutter_controller.RunEventLoop();
  63. return EXIT_SUCCESS;
  64. }