|
| 1 | + |
| 2 | +#include <assert.h> |
| 3 | +#include <embedder.h> |
| 4 | +#include <glfw3.h> |
| 5 | +#include <chrono> |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +// This value is calculated after the window is created. |
| 9 | +static double g_pixelRatio = 1.0; |
| 10 | +static const size_t kInitialWindowWidth = 800; |
| 11 | +static const size_t kInitialWindowHeight = 600; |
| 12 | + |
| 13 | +static_assert(FLUTTER_ENGINE_VERSION == 1, |
| 14 | + "This Flutter Embedder was authored against the stable Flutter " |
| 15 | + "API at version 1. There has been a serious breakage in the " |
| 16 | + "API. Please read the ChangeLog and take appropriate action " |
| 17 | + "before updating this assertion"); |
| 18 | + |
| 19 | +void GLFWcursorPositionCallbackAtPhase(GLFWwindow* window, |
| 20 | + FlutterPointerPhase phase, |
| 21 | + double x, |
| 22 | + double y) { |
| 23 | + FlutterPointerEvent event = {}; |
| 24 | + event.struct_size = sizeof(event); |
| 25 | + event.phase = phase; |
| 26 | + event.x = x * g_pixelRatio; |
| 27 | + event.y = y * g_pixelRatio; |
| 28 | + event.timestamp = |
| 29 | + std::chrono::duration_cast<std::chrono::microseconds>( |
| 30 | + std::chrono::high_resolution_clock::now().time_since_epoch()) |
| 31 | + .count(); |
| 32 | + FlutterEngineSendPointerEvent( |
| 33 | + reinterpret_cast<FlutterEngine>(glfwGetWindowUserPointer(window)), &event, |
| 34 | + 1); |
| 35 | +} |
| 36 | + |
| 37 | +void GLFWcursorPositionCallback(GLFWwindow* window, double x, double y) { |
| 38 | + GLFWcursorPositionCallbackAtPhase(window, FlutterPointerPhase::kMove, x, y); |
| 39 | +} |
| 40 | + |
| 41 | +void GLFWmouseButtonCallback(GLFWwindow* window, |
| 42 | + int key, |
| 43 | + int action, |
| 44 | + int mods) { |
| 45 | + if (key == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) { |
| 46 | + double x, y; |
| 47 | + glfwGetCursorPos(window, &x, &y); |
| 48 | + GLFWcursorPositionCallbackAtPhase(window, FlutterPointerPhase::kDown, x, y); |
| 49 | + glfwSetCursorPosCallback(window, GLFWcursorPositionCallback); |
| 50 | + } |
| 51 | + |
| 52 | + if (key == GLFW_MOUSE_BUTTON_1 && action == GLFW_RELEASE) { |
| 53 | + double x, y; |
| 54 | + glfwGetCursorPos(window, &x, &y); |
| 55 | + GLFWcursorPositionCallbackAtPhase(window, FlutterPointerPhase::kUp, x, y); |
| 56 | + glfwSetCursorPosCallback(window, nullptr); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +static void GLFWKeyCallback(GLFWwindow* window, |
| 61 | + int key, |
| 62 | + int scancode, |
| 63 | + int action, |
| 64 | + int mods) { |
| 65 | + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { |
| 66 | + glfwSetWindowShouldClose(window, GLFW_TRUE); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +void GLFWwindowSizeCallback(GLFWwindow* window, int width, int height) { |
| 71 | + FlutterWindowMetricsEvent event = {}; |
| 72 | + event.struct_size = sizeof(event); |
| 73 | + event.width = width * g_pixelRatio; |
| 74 | + event.height = height * g_pixelRatio; |
| 75 | + event.pixel_ratio = g_pixelRatio; |
| 76 | + FlutterEngineSendWindowMetricsEvent( |
| 77 | + reinterpret_cast<FlutterEngine>(glfwGetWindowUserPointer(window)), |
| 78 | + &event); |
| 79 | +} |
| 80 | + |
| 81 | +bool RunFlutter(GLFWwindow* window, |
| 82 | + const std::string& project_path, |
| 83 | + const std::string& icudtl_path) { |
| 84 | + FlutterRendererConfig config = {}; |
| 85 | + config.type = kOpenGL; |
| 86 | + config.open_gl.struct_size = sizeof(config.open_gl); |
| 87 | + config.open_gl.make_current = [](void* userdata) -> bool { |
| 88 | + glfwMakeContextCurrent((GLFWwindow*)userdata); |
| 89 | + return true; |
| 90 | + }; |
| 91 | + config.open_gl.clear_current = [](void*) -> bool { |
| 92 | + glfwMakeContextCurrent(nullptr); // is this even a thing? |
| 93 | + return true; |
| 94 | + }; |
| 95 | + config.open_gl.present = [](void* userdata) -> bool { |
| 96 | + glfwSwapBuffers((GLFWwindow*)userdata); |
| 97 | + return true; |
| 98 | + }; |
| 99 | + config.open_gl.fbo_callback = [](void*) -> uint32_t { |
| 100 | + return 0; // FBO0 |
| 101 | + }; |
| 102 | + |
| 103 | + // This directory is generated by `flutter build bundle`. |
| 104 | + std::string assets_path = project_path + "/build/flutter_assets"; |
| 105 | + FlutterProjectArgs args = { |
| 106 | + .struct_size = sizeof(FlutterProjectArgs), |
| 107 | + .assets_path = assets_path.c_str(), |
| 108 | + .icu_data_path = |
| 109 | + icudtl_path.c_str(), // Find this in your bin/cache directory. |
| 110 | + }; |
| 111 | + FlutterEngine engine = nullptr; |
| 112 | + FlutterEngineResult result = |
| 113 | + FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, // renderer |
| 114 | + &args, window, &engine); |
| 115 | + assert(result == kSuccess && engine != nullptr); |
| 116 | + |
| 117 | + glfwSetWindowUserPointer(window, engine); |
| 118 | + GLFWwindowSizeCallback(window, kInitialWindowWidth, kInitialWindowHeight); |
| 119 | + |
| 120 | + return true; |
| 121 | +} |
| 122 | + |
| 123 | +void printUsage() { |
| 124 | + std::cout << "usage: flutter_glfw <path to project> <path to icudtl.dat>" |
| 125 | + << std::endl; |
| 126 | +} |
| 127 | + |
| 128 | +int main(int argc, const char* argv[]) { |
| 129 | + if (argc != 3) { |
| 130 | + printUsage(); |
| 131 | + return 1; |
| 132 | + } |
| 133 | + |
| 134 | + std::string project_path = argv[1]; |
| 135 | + std::string icudtl_path = argv[2]; |
| 136 | + |
| 137 | + int result = glfwInit(); |
| 138 | + assert(result == GLFW_TRUE); |
| 139 | + |
| 140 | + GLFWwindow* window = glfwCreateWindow( |
| 141 | + kInitialWindowWidth, kInitialWindowHeight, "Flutter", NULL, NULL); |
| 142 | + assert(window != nullptr); |
| 143 | + |
| 144 | + int framebuffer_width, framebuffer_height; |
| 145 | + glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height); |
| 146 | + g_pixelRatio = framebuffer_width / kInitialWindowWidth; |
| 147 | + |
| 148 | + bool runResult = RunFlutter(window, project_path, icudtl_path); |
| 149 | + assert(runResult); |
| 150 | + |
| 151 | + glfwSetKeyCallback(window, GLFWKeyCallback); |
| 152 | + glfwSetWindowSizeCallback(window, GLFWwindowSizeCallback); |
| 153 | + glfwSetMouseButtonCallback(window, GLFWmouseButtonCallback); |
| 154 | + |
| 155 | + while (!glfwWindowShouldClose(window)) { |
| 156 | + std::cout << "Looping..." << std::endl; |
| 157 | + glfwWaitEvents(); |
| 158 | + } |
| 159 | + |
| 160 | + glfwDestroyWindow(window); |
| 161 | + glfwTerminate(); |
| 162 | + |
| 163 | + return EXIT_SUCCESS; |
| 164 | +} |
0 commit comments