1
+ #include < flutter/flutter_engine.h>
1
2
#include < flutter/flutter_window_controller.h>
2
3
#include < linux/limits.h>
3
4
#include < unistd.h>
12
13
13
14
namespace {
14
15
15
- // Returns the path of the directory containing this executable, or an empty
16
- // string if the directory cannot be found.
17
- std::string GetExecutableDirectory () {
18
- char buffer[PATH_MAX + 1 ];
19
- ssize_t length = readlink (" /proc/self/exe" , buffer, sizeof (buffer));
20
- if (length > PATH_MAX) {
21
- std::cerr << " Couldn't locate executable" << std::endl;
22
- return " " ;
16
+ // Runs the application in headless mode, without a window.
17
+ void RunHeadless (const std::string& icu_data_path,
18
+ const std::string& assets_path,
19
+ const std::vector<std::string>& arguments) {
20
+ flutter::FlutterEngine engine;
21
+ engine.Start (icu_data_path, assets_path, arguments);
22
+ RegisterPlugins (&engine);
23
+ while (true ) {
24
+ engine.RunEventLoopWithTimeout ();
23
25
}
24
- std::string executable_path (buffer, length);
25
- size_t last_separator_position = executable_path.find_last_of (' /' );
26
- if (last_separator_position == std::string::npos) {
27
- std::cerr << " Unabled to find parent directory of " << executable_path
28
- << std::endl;
29
- return " " ;
30
- }
31
- return executable_path.substr (0 , last_separator_position);
32
26
}
33
27
34
28
} // namespace
35
29
36
30
int main (int argc, char **argv) {
37
- // Resources are located relative to the executable.
38
- std::string base_directory = GetExecutableDirectory ();
39
- if (base_directory.empty ()) {
40
- base_directory = " ." ;
41
- }
42
- std::string data_directory = base_directory + " /data" ;
31
+ std::string data_directory = " data" ;
43
32
std::string assets_path = data_directory + " /flutter_assets" ;
44
33
std::string icu_data_path = data_directory + " /icudtl.dat" ;
45
34
@@ -55,13 +44,17 @@ int main(int argc, char **argv) {
55
44
// Start the engine.
56
45
if (!flutter_controller.CreateWindow (window_properties, assets_path,
57
46
arguments)) {
47
+ if (getenv (" DISPLAY" ) == nullptr ) {
48
+ std::cout << " No DISPLAY; falling back to headless mode." << std::endl;
49
+ RunHeadless (icu_data_path, assets_path, arguments);
50
+ return EXIT_SUCCESS;
51
+ }
58
52
return EXIT_FAILURE;
59
53
}
60
54
RegisterPlugins (&flutter_controller);
61
55
62
56
// Run until the window is closed.
63
- while (flutter_controller.RunEventLoopWithTimeout (
64
- std::chrono::milliseconds::max ())) {
57
+ while (flutter_controller.RunEventLoopWithTimeout ()) {
65
58
}
66
59
return EXIT_SUCCESS;
67
60
}
0 commit comments