Skip to content

Commit 421411c

Browse files
committed
Add usage docs and simple argument parsing.
1 parent 5e45a8e commit 421411c

File tree

6 files changed

+143
-38
lines changed

6 files changed

+143
-38
lines changed

src/flutter_application.cc

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,19 @@
66

77
#include <EGL/egl.h>
88
#include <sys/types.h>
9-
#include <unistd.h>
109

1110
#include <chrono>
1211
#include <sstream>
1312
#include <vector>
1413

14+
#include "utils.h"
15+
1516
namespace flutter {
1617

1718
static_assert(FLUTTER_ENGINE_VERSION == 1, "");
1819

1920
static const char* kICUDataFileName = "icudtl.dat";
2021

21-
static std::string GetExecutableDirectory() {
22-
char executable_path[1024] = {0};
23-
std::stringstream stream;
24-
stream << "/proc/" << getpid() << "/exe";
25-
auto path = stream.str();
26-
auto executable_path_size =
27-
::readlink(path.c_str(), executable_path, sizeof(executable_path));
28-
if (executable_path_size <= 0) {
29-
return "";
30-
}
31-
32-
auto path_string =
33-
std::string{executable_path, static_cast<size_t>(executable_path_size)};
34-
35-
auto found = path_string.find_last_of('/');
36-
37-
if (found == std::string::npos) {
38-
return "";
39-
}
40-
41-
return path_string.substr(0, found + 1);
42-
}
43-
4422
static std::string GetICUDataPath() {
4523
auto exe_dir = GetExecutableDirectory();
4624
if (exe_dir == "") {
@@ -51,7 +29,7 @@ static std::string GetICUDataPath() {
5129

5230
auto icu_path = stream.str();
5331

54-
if (::access(icu_path.c_str(), R_OK) != 0) {
32+
if (!FileExistsAtPath(icu_path.c_str())) {
5533
FLWAY_ERROR << "Could not find " << icu_path << std::endl;
5634
return "";
5735
}
@@ -60,9 +38,15 @@ static std::string GetICUDataPath() {
6038
}
6139

6240
FlutterApplication::FlutterApplication(
41+
std::string bundle_path,
6342
const std::vector<std::string>& command_line_args,
6443
RenderDelegate& render_delegate)
6544
: render_delegate_(render_delegate) {
45+
if (!FlutterAssetBundleIsValid(bundle_path)) {
46+
FLWAY_ERROR << "Flutter asset bundle was not valid." << std::endl;
47+
return;
48+
}
49+
6650
FlutterRendererConfig config = {};
6751
config.type = kOpenGL;
6852
config.open_gl.struct_size = sizeof(config.open_gl);
@@ -92,11 +76,6 @@ FlutterApplication::FlutterApplication(
9276
return nullptr;
9377
};
9478

95-
// TODO: Pipe this in through command line args.
96-
#define MY_PROJECT \
97-
"/usr/local/google/home/chinmaygarde/VersionControlled/flutter/examples/" \
98-
"flutter_gallery/build/flutter_assets"
99-
10079
auto icu_data_path = GetICUDataPath();
10180

10281
if (icu_data_path == "") {
@@ -114,7 +93,7 @@ FlutterApplication::FlutterApplication(
11493

11594
FlutterProjectArgs args = {
11695
.struct_size = sizeof(FlutterProjectArgs),
117-
.assets_path = MY_PROJECT,
96+
.assets_path = bundle_path.c_str(),
11897
.main_path = "",
11998
.packages_path = "",
12099
.icu_data_path = icu_data_path.c_str(),

src/flutter_application.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class FlutterApplication {
2626
virtual uint32_t OnApplicationGetOnscreenFBO() = 0;
2727
};
2828

29-
FlutterApplication(const std::vector<std::string>& args,
29+
FlutterApplication(std::string bundle_path,
30+
const std::vector<std::string>& args,
3031
RenderDelegate& render_delegate);
3132

3233
~FlutterApplication();

src/main.cc

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,68 @@
88
#include <vector>
99

1010
#include "flutter_application.h"
11+
#include "utils.h"
1112
#include "wayland_display.h"
1213

1314
namespace flutter {
1415

15-
bool Main(std::vector<std::string> args) {
16+
static void PrintUsage() {
17+
std::cerr << "Flutter Wayland Embedder" << std::endl << std::endl;
18+
std::cerr << "========================" << std::endl;
19+
std::cerr << "Usage: `" << GetExecutableName()
20+
<< " <asset_bundle_path> <flutter_flags>`" << std::endl
21+
<< std::endl;
22+
std::cerr << R"~(
23+
This utility runs an instance of a Flutter application and renders using
24+
Wayland core protocols.
25+
26+
The Flutter tools can be obtained at https://flutter.io/
27+
28+
asset_bundle_path: The Flutter application code needs to be snapshotted using
29+
the Flutter tools and the assets packaged in the appropriate
30+
location. This can be done for any Flutter application by
31+
running `flutter build bundle` while in the directory of a
32+
valid Flutter project. This should package all the code and
33+
assets in the "build/flutter_assets" directory. Specify this
34+
directory as the first argument to this utility.
35+
36+
flutter_flags: Typically empty. These extra flags are passed directly to the
37+
Flutter engine. To see all supported flags, run
38+
`flutter_tester --help` using the test binary included in the
39+
Flutter tools.
40+
)~" << std::endl;
41+
}
42+
43+
static bool Main(std::vector<std::string> args) {
44+
if (args.size() == 0) {
45+
std::cerr << " <Invalid Arguments> " << std::endl;
46+
PrintUsage();
47+
return false;
48+
}
49+
50+
const auto asset_bundle_path = args[0];
51+
52+
if (!FlutterAssetBundleIsValid(asset_bundle_path)) {
53+
std::cerr << " <Invalid Flutter Asset Bundle> " << std::endl;
54+
PrintUsage();
55+
return false;
56+
}
57+
1658
const size_t kWidth = 800;
1759
const size_t kHeight = 600;
1860

61+
for (const auto& arg : args) {
62+
FLWAY_ERROR << "Arg: " << arg << std::endl;
63+
}
64+
1965
WaylandDisplay display(kWidth, kHeight);
2066

2167
if (!display.IsValid()) {
2268
FLWAY_ERROR << "Wayland display was not valid." << std::endl;
2369
return false;
2470
}
2571

26-
FlutterApplication application(args, display);
72+
FlutterApplication application(asset_bundle_path, args, display);
2773
if (!application.IsValid()) {
2874
FLWAY_ERROR << "Flutter application was not valid." << std::endl;
2975
return false;
@@ -43,7 +89,7 @@ bool Main(std::vector<std::string> args) {
4389

4490
int main(int argc, char* argv[]) {
4591
std::vector<std::string> args;
46-
for (int i = 0; i < argc; ++i) {
92+
for (int i = 1; i < argc; ++i) {
4793
args.push_back(argv[i]);
4894
}
4995
return flutter::Main(std::move(args)) ? EXIT_SUCCESS : EXIT_FAILURE;

src/utils.cc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2018 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "utils.h"
6+
7+
#include <unistd.h>
8+
9+
#include <sstream>
10+
11+
namespace flutter {
12+
13+
static std::string GetExecutablePath() {
14+
char executable_path[1024] = {0};
15+
std::stringstream stream;
16+
stream << "/proc/" << getpid() << "/exe";
17+
auto path = stream.str();
18+
auto executable_path_size =
19+
::readlink(path.c_str(), executable_path, sizeof(executable_path));
20+
if (executable_path_size <= 0) {
21+
return "";
22+
}
23+
return std::string{executable_path,
24+
static_cast<size_t>(executable_path_size)};
25+
}
26+
27+
std::string GetExecutableName() {
28+
auto path_string = GetExecutablePath();
29+
auto found = path_string.find_last_of('/');
30+
if (found == std::string::npos) {
31+
return "";
32+
}
33+
return path_string.substr(found + 1);
34+
}
35+
36+
std::string GetExecutableDirectory() {
37+
auto path_string = GetExecutablePath();
38+
auto found = path_string.find_last_of('/');
39+
if (found == std::string::npos) {
40+
return "";
41+
}
42+
return path_string.substr(0, found + 1);
43+
}
44+
45+
bool FileExistsAtPath(const std::string& path) {
46+
return ::access(path.c_str(), R_OK) == 0;
47+
}
48+
49+
bool FlutterAssetBundleIsValid(const std::string& bundle_path) {
50+
if (!FileExistsAtPath(bundle_path)) {
51+
FLWAY_ERROR << "Bundle directory does not exist." << std::endl;
52+
return false;
53+
}
54+
55+
if (!FileExistsAtPath(bundle_path + std::string{"/kernel_blob.bin"})) {
56+
FLWAY_ERROR << "Kernel blob does not exist." << std::endl;
57+
return false;
58+
}
59+
60+
return true;
61+
}
62+
63+
} // namespace flutter

src/utils.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "macros.h"
8+
9+
namespace flutter {
10+
11+
std::string GetExecutableName();
12+
13+
std::string GetExecutableDirectory();
14+
15+
bool FileExistsAtPath(const std::string& path);
16+
17+
bool FlutterAssetBundleIsValid(const std::string& bundle_path);
18+
19+
} // namespace flutter

src/wayland_display.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ bool WaylandDisplay::Run() {
144144
}
145145

146146
while (valid_) {
147-
FLWAY_LOG << "Dispatching." << std::endl;
148147
wl_display_dispatch(display_);
149148
}
150149

@@ -330,8 +329,6 @@ void WaylandDisplay::AnnounceRegistryInterface(struct wl_registry* wl_registry,
330329
wl_registry_bind(wl_registry, name, &wl_shell_interface, 1));
331330
return;
332331
}
333-
334-
FLWAY_LOG << "Unhandled Announce of: " << interface_name << std::endl;
335332
}
336333

337334
void WaylandDisplay::UnannounceRegistryInterface(

0 commit comments

Comments
 (0)