Unverified Commit 9d4b0cb8 authored by stuartmorgan's avatar stuartmorgan Committed by GitHub

Update Linux template for headless mode (#56618)

parent 06adde0b
...@@ -49,6 +49,7 @@ ALL_LIBS_OUT=$(FLUTTER_LIB_OUT) \ ...@@ -49,6 +49,7 @@ ALL_LIBS_OUT=$(FLUTTER_LIB_OUT) \
# issues if they start with one or more '../'s. # issues if they start with one or more '../'s.
WRAPPER_ROOT=$(abspath $(FLUTTER_EPHEMERAL_DIR)/cpp_client_wrapper_glfw) WRAPPER_ROOT=$(abspath $(FLUTTER_EPHEMERAL_DIR)/cpp_client_wrapper_glfw)
WRAPPER_SOURCES= \ WRAPPER_SOURCES= \
$(WRAPPER_ROOT)/flutter_engine.cc \
$(WRAPPER_ROOT)/flutter_window_controller.cc \ $(WRAPPER_ROOT)/flutter_window_controller.cc \
$(WRAPPER_ROOT)/plugin_registrar.cc \ $(WRAPPER_ROOT)/plugin_registrar.cc \
$(WRAPPER_ROOT)/engine_method_result.cc $(WRAPPER_ROOT)/engine_method_result.cc
......
#include <flutter/flutter_engine.h>
#include <flutter/flutter_window_controller.h> #include <flutter/flutter_window_controller.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <unistd.h> #include <unistd.h>
...@@ -12,34 +13,22 @@ ...@@ -12,34 +13,22 @@
namespace { namespace {
// Returns the path of the directory containing this executable, or an empty // Runs the application in headless mode, without a window.
// string if the directory cannot be found. void RunHeadless(const std::string& icu_data_path,
std::string GetExecutableDirectory() { const std::string& assets_path,
char buffer[PATH_MAX + 1]; const std::vector<std::string>& arguments) {
ssize_t length = readlink("/proc/self/exe", buffer, sizeof(buffer)); flutter::FlutterEngine engine;
if (length > PATH_MAX) { engine.Start(icu_data_path, assets_path, arguments);
std::cerr << "Couldn't locate executable" << std::endl; RegisterPlugins(&engine);
return ""; while (true) {
engine.RunEventLoopWithTimeout();
} }
std::string executable_path(buffer, length);
size_t last_separator_position = executable_path.find_last_of('/');
if (last_separator_position == std::string::npos) {
std::cerr << "Unabled to find parent directory of " << executable_path
<< std::endl;
return "";
}
return executable_path.substr(0, last_separator_position);
} }
} // namespace } // namespace
int main(int argc, char **argv) { int main(int argc, char **argv) {
// Resources are located relative to the executable. std::string data_directory = "data";
std::string base_directory = GetExecutableDirectory();
if (base_directory.empty()) {
base_directory = ".";
}
std::string data_directory = base_directory + "/data";
std::string assets_path = data_directory + "/flutter_assets"; std::string assets_path = data_directory + "/flutter_assets";
std::string icu_data_path = data_directory + "/icudtl.dat"; std::string icu_data_path = data_directory + "/icudtl.dat";
...@@ -55,13 +44,17 @@ int main(int argc, char **argv) { ...@@ -55,13 +44,17 @@ int main(int argc, char **argv) {
// Start the engine. // Start the engine.
if (!flutter_controller.CreateWindow(window_properties, assets_path, if (!flutter_controller.CreateWindow(window_properties, assets_path,
arguments)) { arguments)) {
if (getenv("DISPLAY") == nullptr) {
std::cout << "No DISPLAY; falling back to headless mode." << std::endl;
RunHeadless(icu_data_path, assets_path, arguments);
return EXIT_SUCCESS;
}
return EXIT_FAILURE; return EXIT_FAILURE;
} }
RegisterPlugins(&flutter_controller); RegisterPlugins(&flutter_controller);
// Run until the window is closed. // Run until the window is closed.
while (flutter_controller.RunEventLoopWithTimeout( while (flutter_controller.RunEventLoopWithTimeout()) {
std::chrono::milliseconds::max())) {
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment