|
| 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 "flutter_application.h" |
| 6 | + |
| 7 | +#include <sys/types.h> |
| 8 | +#include <unistd.h> |
| 9 | + |
| 10 | +#include <chrono> |
| 11 | +#include <sstream> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +namespace flutter { |
| 15 | + |
| 16 | +static_assert(FLUTTER_ENGINE_VERSION == 1, ""); |
| 17 | + |
| 18 | +static const char* kICUDataFileName = "icudtl.dat"; |
| 19 | + |
| 20 | +static std::string GetExecutableDirectory() { |
| 21 | + char executable_path[1024] = {0}; |
| 22 | + std::stringstream stream; |
| 23 | + stream << "/proc/" << getpid() << "/exe"; |
| 24 | + auto path = stream.str(); |
| 25 | + auto executable_path_size = |
| 26 | + ::readlink(path.c_str(), executable_path, sizeof(executable_path)); |
| 27 | + if (executable_path_size <= 0) { |
| 28 | + return ""; |
| 29 | + } |
| 30 | + |
| 31 | + auto path_string = |
| 32 | + std::string{executable_path, static_cast<size_t>(executable_path_size)}; |
| 33 | + |
| 34 | + auto found = path_string.find_last_of('/'); |
| 35 | + |
| 36 | + if (found == std::string::npos) { |
| 37 | + return ""; |
| 38 | + } |
| 39 | + |
| 40 | + return path_string.substr(0, found + 1); |
| 41 | +} |
| 42 | + |
| 43 | +static std::string GetICUDataPath() { |
| 44 | + auto exe_dir = GetExecutableDirectory(); |
| 45 | + if (exe_dir == "") { |
| 46 | + return ""; |
| 47 | + } |
| 48 | + std::stringstream stream; |
| 49 | + stream << exe_dir << kICUDataFileName; |
| 50 | + |
| 51 | + auto icu_path = stream.str(); |
| 52 | + |
| 53 | + if (::access(icu_path.c_str(), R_OK) != 0) { |
| 54 | + FLWAY_ERROR << "Could not find " << icu_path << std::endl; |
| 55 | + return ""; |
| 56 | + } |
| 57 | + |
| 58 | + return icu_path; |
| 59 | +} |
| 60 | + |
| 61 | +FlutterApplication::FlutterApplication() { |
| 62 | + FlutterRendererConfig config = {}; |
| 63 | + config.type = kSoftware; |
| 64 | + config.software.struct_size = sizeof(FlutterSoftwareRendererConfig); |
| 65 | + config.software.surface_present_callback = |
| 66 | + &FlutterApplication::PresentSurface; |
| 67 | + |
| 68 | +// TODO: Pipe this in through command line args. |
| 69 | +#define MY_PROJECT \ |
| 70 | + "/home/buzzy/VersionControlled/flutter/examples/flutter_gallery" |
| 71 | + |
| 72 | + std::vector<const char*> engine_command_line_args = { |
| 73 | + "--disable-observatory", // |
| 74 | + "--dart-non-checked-mode", // |
| 75 | + }; |
| 76 | + |
| 77 | + auto icu_data_path = GetICUDataPath(); |
| 78 | + |
| 79 | + if (icu_data_path == "") { |
| 80 | + FLWAY_ERROR << "Could not find ICU data. It should be placed next to the " |
| 81 | + "executable but it wasn't there." |
| 82 | + << std::endl; |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + FlutterProjectArgs args = { |
| 87 | + .struct_size = sizeof(FlutterProjectArgs), |
| 88 | + .assets_path = MY_PROJECT "/build/flutter_assets", |
| 89 | + .main_path = "", |
| 90 | + .packages_path = "", |
| 91 | + .icu_data_path = icu_data_path.c_str(), |
| 92 | + .command_line_argc = static_cast<int>(engine_command_line_args.size()), |
| 93 | + .command_line_argv = engine_command_line_args.data(), |
| 94 | + }; |
| 95 | + |
| 96 | + FlutterEngine engine = nullptr; |
| 97 | + auto result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, // renderer |
| 98 | + &args, this, &engine_); |
| 99 | + |
| 100 | + if (result != kSuccess) { |
| 101 | + FLWAY_ERROR << "Could not run the Flutter engine" << std::endl; |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + valid_ = true; |
| 106 | +} |
| 107 | + |
| 108 | +FlutterApplication::~FlutterApplication() { |
| 109 | + if (engine_ == nullptr) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + auto result = FlutterEngineShutdown(engine_); |
| 114 | + |
| 115 | + if (result != kSuccess) { |
| 116 | + FLWAY_ERROR << "Could not shutdown the Flutter engine." << std::endl; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +bool FlutterApplication::IsValid() const { |
| 121 | + return valid_; |
| 122 | +} |
| 123 | + |
| 124 | +bool FlutterApplication::SetWindowSize(size_t width, size_t height) { |
| 125 | + FlutterWindowMetricsEvent event = {}; |
| 126 | + event.struct_size = sizeof(event); |
| 127 | + event.width = width; |
| 128 | + event.height = height; |
| 129 | + event.pixel_ratio = 1.0; |
| 130 | + return FlutterEngineSendWindowMetricsEvent(engine_, &event) == kSuccess; |
| 131 | +} |
| 132 | + |
| 133 | +void FlutterApplication::ProcessEvents() { |
| 134 | + __FlutterEngineFlushPendingTasksNow(); |
| 135 | +} |
| 136 | + |
| 137 | +bool FlutterApplication::PresentSurface(void* user_data, |
| 138 | + const void* allocation, |
| 139 | + size_t row_bytes, |
| 140 | + size_t height) { |
| 141 | + return reinterpret_cast<FlutterApplication*>(user_data)->PresentSurface( |
| 142 | + allocation, row_bytes, height); |
| 143 | +} |
| 144 | + |
| 145 | +void FlutterApplication::SetOnPresentCallback(PresentCallback callback) { |
| 146 | + std::lock_guard<std::mutex> lock(mutex_); |
| 147 | + present_callback_ = callback; |
| 148 | +} |
| 149 | + |
| 150 | +bool FlutterApplication::PresentSurface(const void* allocation, |
| 151 | + size_t row_bytes, |
| 152 | + size_t height) { |
| 153 | + std::lock_guard<std::mutex> lock(mutex_); |
| 154 | + if (!present_callback_) { |
| 155 | + FLWAY_ERROR << "Present callback was not set." << std::endl; |
| 156 | + return false; |
| 157 | + } |
| 158 | + present_callback_(allocation, row_bytes, height); |
| 159 | + return true; |
| 160 | +} |
| 161 | + |
| 162 | +bool FlutterApplication::SendPointerEvent(int button, int x, int y) { |
| 163 | + if (!valid_) { |
| 164 | + FLWAY_ERROR << "Pointer events on an invalid application." << std::endl; |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + // Simple hover event. Nothing to do. |
| 169 | + if (last_button_ == 0 && button == 0) { |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + FlutterPointerPhase phase = kCancel; |
| 174 | + |
| 175 | + if (last_button_ == 0 && button != 0) { |
| 176 | + phase = kDown; |
| 177 | + } else if (last_button_ == button) { |
| 178 | + phase = kMove; |
| 179 | + } else { |
| 180 | + phase = kUp; |
| 181 | + } |
| 182 | + |
| 183 | + last_button_ = button; |
| 184 | + return SendFlutterPointerEvent(phase, x, y); |
| 185 | +} |
| 186 | + |
| 187 | +bool FlutterApplication::SendFlutterPointerEvent(FlutterPointerPhase phase, |
| 188 | + double x, |
| 189 | + double y) { |
| 190 | + FlutterPointerEvent event = {}; |
| 191 | + event.struct_size = sizeof(event); |
| 192 | + event.phase = phase; |
| 193 | + event.x = x; |
| 194 | + event.y = y; |
| 195 | + event.timestamp = |
| 196 | + std::chrono::duration_cast<std::chrono::microseconds>( |
| 197 | + std::chrono::high_resolution_clock::now().time_since_epoch()) |
| 198 | + .count(); |
| 199 | + return FlutterEngineSendPointerEvent(engine_, &event, 1) == kSuccess; |
| 200 | +} |
| 201 | + |
| 202 | +} // namespace flutter |
0 commit comments