Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7dd25d7

Browse files
committed
Add screen access for GLFW, Linux
1 parent f485b3a commit 7dd25d7

File tree

10 files changed

+201
-18
lines changed

10 files changed

+201
-18
lines changed

examples/glfw/FlutterEmbedderGLFW.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,31 @@ static void GLFWKeyCallback(GLFWwindow* window,
6868
}
6969

7070
void GLFWwindowSizeCallback(GLFWwindow* window, int width, int height) {
71+
int left, top;
72+
glfwGetWindowPos(window, &left, &top);
7173
FlutterWindowMetricsEvent event = {};
7274
event.struct_size = sizeof(event);
75+
event.window_id = 0;
76+
event.screen_id = 0;
77+
event.left = left * g_pixelRatio;
78+
event.top = top * g_pixelRatio;
79+
event.width = width * g_pixelRatio;
80+
event.height = height * g_pixelRatio;
81+
event.pixel_ratio = g_pixelRatio;
82+
FlutterEngineSendWindowMetricsEvent(
83+
reinterpret_cast<FlutterEngine>(glfwGetWindowUserPointer(window)),
84+
&event);
85+
}
86+
87+
void GLFWwindowPosCallback(GLFWwindow* window, int left, int top) {
88+
int width, height;
89+
glfwGetWindowSize(window, &width, &height);
90+
FlutterWindowMetricsEvent event = {};
91+
event.struct_size = sizeof(event);
92+
event.window_id = 0;
93+
event.screen_id = 0;
94+
event.left = left * g_pixelRatio;
95+
event.top = top * g_pixelRatio;
7396
event.width = width * g_pixelRatio;
7497
event.height = height * g_pixelRatio;
7598
event.pixel_ratio = g_pixelRatio;
@@ -150,6 +173,7 @@ int main(int argc, const char* argv[]) {
150173

151174
glfwSetKeyCallback(window, GLFWKeyCallback);
152175
glfwSetWindowSizeCallback(window, GLFWwindowSizeCallback);
176+
glfwSetWindowPosCallback(window, GLFWwindowPosCallback);
153177
glfwSetMouseButtonCallback(window, GLFWmouseButtonCallback);
154178

155179
while (!glfwWindowShouldClose(window)) {

shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,13 @@ - (void)updateWindowMetrics {
315315
CGRect screenBounds = [view.screen convertRectToBacking:view.screen.bounds];
316316
const FlutterWindowMetricsEvent windowMetricsEvent = {
317317
.struct_size = sizeof(windowMetricsEvent),
318+
.window_id = 0,
319+
.screen_id = 0,
318320
.left = static_cast<size_t>(screenBounds.origin.x),
319321
.top = static_cast<size_t>(screenBounds.origin.y),
320322
.width = static_cast<size_t>(screenBounds.size.width),
321323
.height = static_cast<size_t>(screenBounds.size.height),
322324
.pixel_ratio = pixelRatio,
323-
.window_id = 0,
324-
.screen_id = 0,
325325
};
326326
FlutterEngineSendWindowMetricsEvent(_engine, &windowMetricsEvent);
327327
}

shell/platform/embedder/embedder.h

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ typedef bool (*TextureFrameCallback)(void* /* user data */,
273273
FlutterOpenGLTexture* /* texture out */);
274274
typedef void (*VsyncCallback)(void* /* user data */, intptr_t /* baton */);
275275

276+
/// The order, type, and size of these struct members must remain the same, and
277+
/// members should not be removed. This is to allow for forward and backward
278+
/// compatibility between the engine and the embedder.
276279
typedef struct {
277280
/// The size of this struct. Must be sizeof(FlutterOpenGLRendererConfig).
278281
size_t struct_size;
@@ -311,6 +314,9 @@ typedef struct {
311314
TextureFrameCallback gl_external_texture_frame_callback;
312315
} FlutterOpenGLRendererConfig;
313316

317+
/// The order, type, and size of these struct members must remain the same, and
318+
/// members should not be removed. This is to allow for forward and backward
319+
/// compatibility between the engine and the embedder.
314320
typedef struct {
315321
/// The size of this struct. Must be sizeof(FlutterSoftwareRendererConfig).
316322
size_t struct_size;
@@ -329,6 +335,9 @@ typedef struct {
329335
};
330336
} FlutterRendererConfig;
331337

338+
/// The order, type, and size of these struct members must remain the same, and
339+
/// members should not be removed. This is to allow for forward and backward
340+
/// compatibility between the engine and the embedder.
332341
typedef struct {
333342
/// The size of this struct. Must be sizeof(FlutterWindowMetricsEvent).
334343
size_t struct_size;
@@ -339,25 +348,28 @@ typedef struct {
339348
/// Pixel density scale factor for the window. This will be identical to the
340349
/// value of the pixel ratio for the screen that this window appears on.
341350
double pixel_ratio;
342-
/// Physical vertical location of the top of the window.
343-
size_t top;
344351
/// Physical horizontal location of the left side of the window.
345352
size_t left;
353+
/// Physical vertical location of the top of the window.
354+
size_t top;
346355
/// The id for the window that corresponds to these metrics.
347356
size_t window_id;
348357
/// The id of the screen that this window appears on.
349358
size_t screen_id;
350359
} FlutterWindowMetricsEvent;
351360

361+
/// The order, type, and size of these struct members must remain the same, and
362+
/// members should not be removed. This is to allow for forward and backward
363+
/// compatibility between the engine and the embedder.
352364
typedef struct {
353365
/// The size of this struct. Must be sizeof(FlutterScreenMetricsEvent).
354366
size_t struct_size;
355367
/// The id for the screen that corresponds to these metrics.
356368
size_t screen_id;
357-
/// Physical vertical location of the top of the window.
358-
size_t top;
359369
/// Physical horizontal location of the left side of the window.
360370
size_t left;
371+
/// Physical vertical location of the top of the window.
372+
size_t top;
361373
/// Physical width of the screen.
362374
size_t width;
363375
/// Physical height of the screen.
@@ -426,6 +438,9 @@ typedef enum {
426438
kFlutterPointerSignalKindScroll,
427439
} FlutterPointerSignalKind;
428440

441+
/// The order, type, and size of these struct members must remain the same, and
442+
/// members should not be removed. This is to allow for forward and backward
443+
/// compatibility between the engine and the embedder.
429444
typedef struct {
430445
/// The size of this struct. Must be sizeof(FlutterPointerEvent).
431446
size_t struct_size;
@@ -460,6 +475,9 @@ struct _FlutterPlatformMessageResponseHandle;
460475
typedef struct _FlutterPlatformMessageResponseHandle
461476
FlutterPlatformMessageResponseHandle;
462477

478+
/// The order, type, and size of these struct members must remain the same, and
479+
/// members should not be removed. This is to allow for forward and backward
480+
/// compatibility between the engine and the embedder.
463481
typedef struct {
464482
/// The size of this struct. Must be sizeof(FlutterPlatformMessage).
465483
size_t struct_size;
@@ -524,6 +542,10 @@ extern const int32_t kFlutterSemanticsNodeIdBatchEnd;
524542
/// (i.e., during PipelineOwner.flushSemantics), which happens after
525543
/// compositing. Updates are then pushed to embedders via the registered
526544
/// `FlutterUpdateSemanticsNodeCallback`.
545+
///
546+
/// The order, type, and size of these struct members must remain the same, and
547+
/// members should not be removed. This is to allow for forward and backward
548+
/// compatibility between the engine and the embedder.
527549
typedef struct {
528550
/// The size of this struct. Must be sizeof(FlutterSemanticsNode).
529551
size_t struct_size;
@@ -603,6 +625,10 @@ extern const int32_t kFlutterSemanticsCustomActionIdBatchEnd;
603625
/// Action overrides are custom actions that the application developer requests
604626
/// to be used in place of the standard actions in the `FlutterSemanticsAction`
605627
/// enum.
628+
///
629+
/// The order, type, and size of these struct members must remain the same, and
630+
/// members should not be removed. This is to allow for forward and backward
631+
/// compatibility between the engine and the embedder.
606632
typedef struct {
607633
/// The size of the struct. Must be sizeof(FlutterSemanticsCustomAction).
608634
size_t struct_size;
@@ -641,6 +667,10 @@ typedef void (*FlutterTaskRunnerPostTaskCallback)(
641667
/// on a specified thread. There should be a 1-1 relationship between a thread
642668
/// and a task runner. It is undefined behavior to run a task on a thread that
643669
/// is not associated with its task runner.
670+
///
671+
/// The order, type, and size of these struct members must remain the same, and
672+
/// members should not be removed. This is to allow for forward and backward
673+
/// compatibility between the engine and the embedder.
644674
typedef struct {
645675
/// The size of this struct. Must be sizeof(FlutterTaskRunnerDescription).
646676
size_t struct_size;
@@ -666,6 +696,9 @@ typedef struct {
666696
size_t identifier;
667697
} FlutterTaskRunnerDescription;
668698

699+
/// The order, type, and size of these struct members must remain the same, and
700+
/// members should not be removed. This is to allow for forward and backward
701+
/// compatibility between the engine and the embedder.
669702
typedef struct {
670703
/// The size of this struct. Must be sizeof(FlutterCustomTaskRunners).
671704
size_t struct_size;
@@ -737,6 +770,9 @@ typedef struct {
737770
};
738771
} FlutterPlatformViewMutation;
739772

773+
/// The order, type, and size of these struct members must remain the same, and
774+
/// members should not be removed. This is to allow for forward and backward
775+
/// compatibility between the engine and the embedder.
740776
typedef struct {
741777
/// The size of this struct. Must be sizeof(FlutterPlatformView).
742778
size_t struct_size;
@@ -770,6 +806,9 @@ typedef enum {
770806
kFlutterBackingStoreTypeSoftware,
771807
} FlutterBackingStoreType;
772808

809+
/// The order, type, and size of these struct members must remain the same, and
810+
/// members should not be removed. This is to allow for forward and backward
811+
/// compatibility between the engine and the embedder.
773812
typedef struct {
774813
/// The size of this struct. Must be sizeof(FlutterBackingStore).
775814
size_t struct_size;
@@ -790,6 +829,9 @@ typedef struct {
790829
};
791830
} FlutterBackingStore;
792831

832+
/// The order, type, and size of these struct members must remain the same, and
833+
/// members should not be removed. This is to allow for forward and backward
834+
/// compatibility between the engine and the embedder.
793835
typedef struct {
794836
/// The size of this struct. Must be sizeof(FlutterBackingStoreConfig).
795837
size_t struct_size;
@@ -805,6 +847,9 @@ typedef enum {
805847
kFlutterLayerContentTypePlatformView,
806848
} FlutterLayerContentType;
807849

850+
/// The order, type, and size of these struct members must remain the same, and
851+
/// members should not be removed. This is to allow for forward and backward
852+
/// compatibility between the engine and the embedder.
808853
typedef struct {
809854
/// This size of this struct. Must be sizeof(FlutterLayer).
810855
size_t struct_size;
@@ -839,6 +884,9 @@ typedef bool (*FlutterLayersPresentCallback)(const FlutterLayer** layers,
839884
size_t layers_count,
840885
void* user_data);
841886

887+
/// The order, type, and size of these struct members must remain the same, and
888+
/// members should not be removed. This is to allow for forward and backward
889+
/// compatibility between the engine and the embedder.
842890
typedef struct {
843891
/// This size of this struct. Must be sizeof(FlutterCompositor).
844892
size_t struct_size;
@@ -863,6 +911,9 @@ typedef struct {
863911
FlutterLayersPresentCallback present_layers_callback;
864912
} FlutterCompositor;
865913

914+
/// The order, type, and size of these struct members must remain the same, and
915+
/// members should not be removed. This is to allow for forward and backward
916+
/// compatibility between the engine and the embedder.
866917
typedef struct {
867918
/// This size of this struct. Must be sizeof(FlutterLocale).
868919
size_t struct_size;
@@ -901,6 +952,9 @@ typedef enum {
901952
kFlutterEngineDartObjectTypeBuffer,
902953
} FlutterEngineDartObjectType;
903954

955+
/// The order, type, and size of these struct members must remain the same, and
956+
/// members should not be removed. This is to allow for forward and backward
957+
/// compatibility between the engine and the embedder.
904958
typedef struct {
905959
/// The size of this struct. Must be sizeof(FlutterEngineDartBuffer).
906960
size_t struct_size;
@@ -1036,6 +1090,9 @@ FlutterEngineResult FlutterEngineCreateAOTData(
10361090
FLUTTER_EXPORT
10371091
FlutterEngineResult FlutterEngineCollectAOTData(FlutterEngineAOTData data);
10381092

1093+
/// The order, type, and size of these struct members must remain the same, and
1094+
/// members should not be removed. This is to allow for forward and backward
1095+
/// compatibility between the engine and the embedder.
10391096
typedef struct {
10401097
/// The size of this struct. Must be sizeof(FlutterProjectArgs).
10411098
size_t struct_size;

shell/platform/glfw/flutter_glfw.cc

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,17 @@ static double GetScreenCoordinatesPerInch() {
196196
// Sends a window metrics update to the Flutter engine using the given
197197
// framebuffer size and the current window information in |state|.
198198
static void SendScreenMetrics(FlutterDesktopWindowControllerState* controller,
199+
int left,
200+
int top,
199201
int width,
200202
int height) {
201203
double dpi = controller->window_wrapper->pixels_per_screen_coordinate *
202204
controller->monitor_screen_coordinates_per_inch;
203205

204206
FlutterScreenMetricsEvent event = {};
205207
event.struct_size = sizeof(event);
208+
event.left = left;
209+
event.top = top;
206210
event.width = width;
207211
event.height = height;
208212
if (controller->window_wrapper->pixel_ratio_override == 0.0) {
@@ -220,13 +224,17 @@ static void SendScreenMetrics(FlutterDesktopWindowControllerState* controller,
220224
// Sends a window metrics update to the Flutter engine using the given
221225
// framebuffer size and the current window information in |state|.
222226
static void SendWindowMetrics(FlutterDesktopWindowControllerState* controller,
227+
int left,
228+
int top,
223229
int width,
224230
int height) {
225231
double dpi = controller->window_wrapper->pixels_per_screen_coordinate *
226232
controller->monitor_screen_coordinates_per_inch;
227233

228234
FlutterWindowMetricsEvent event = {};
229235
event.struct_size = sizeof(event);
236+
event.left = left;
237+
event.top = top;
230238
event.width = width;
231239
event.height = height;
232240
if (controller->window_wrapper->pixel_ratio_override == 0.0) {
@@ -264,14 +272,15 @@ static void ConfigurePlatformTaskRunner(
264272
static void GLFWFramebufferSizeCallback(GLFWwindow* window,
265273
int width_px,
266274
int height_px) {
275+
int left_px, top_px;
276+
glfwGetWindowPos(window, &left_px, &top_px);
267277
int width;
268278
glfwGetWindowSize(window, &width, nullptr);
269279
auto* controller = GetWindowController(window);
270280
controller->window_wrapper->pixels_per_screen_coordinate =
271281
width > 0 ? width_px / width : 1;
272282

273-
SendScreenMetrics(controller, width_px, height_px);
274-
SendWindowMetrics(controller, width_px, height_px);
283+
SendWindowMetrics(controller, left_px, top_px, width_px, height_px);
275284
controller->window_wrapper->skip_next_window_refresh = true;
276285
}
277286

@@ -284,11 +293,27 @@ void GLFWWindowRefreshCallback(GLFWwindow* window) {
284293
}
285294
// There's no engine API to request a redraw explicitly, so instead send a
286295
// window metrics event with the current size to trigger it.
296+
int left_px, top_px;
297+
glfwGetWindowPos(window, &left_px, &top_px);
287298
int width_px, height_px;
288299
glfwGetFramebufferSize(window, &width_px, &height_px);
289300
if (width_px > 0 && height_px > 0) {
290-
SendScreenMetrics(controller, width_px, height_px);
291-
SendWindowMetrics(controller, width_px, height_px);
301+
SendWindowMetrics(controller, left_px, top_px, width_px, height_px);
302+
}
303+
}
304+
305+
// Indicates that the window has moved.
306+
void GLFWWindowPosCallback(GLFWwindow* window, int left_px, int top_px) {
307+
auto* controller = GetWindowController(window);
308+
int width_px, height_px;
309+
glfwGetFramebufferSize(window, &width_px, &height_px);
310+
311+
if (controller->window_wrapper->skip_next_window_refresh) {
312+
controller->window_wrapper->skip_next_window_refresh = false;
313+
return;
314+
}
315+
if (width_px > 0 && height_px > 0) {
316+
SendWindowMetrics(controller, left_px, top_px, width_px, height_px);
292317
}
293318
}
294319

@@ -809,12 +834,16 @@ FlutterDesktopWindowControllerRef FlutterDesktopCreateWindow(
809834

810835
// Trigger an initial size callback to send size information to Flutter.
811836
state->monitor_screen_coordinates_per_inch = GetScreenCoordinatesPerInch();
837+
int left_px, top_px;
838+
glfwGetWindowPos(window, &left_px, &top_px);
839+
GLFWWindowPosCallback(window, left_px, top_px);
812840
int width_px, height_px;
813841
glfwGetFramebufferSize(window, &width_px, &height_px);
814842
GLFWFramebufferSizeCallback(window, width_px, height_px);
815843

816844
// Set up GLFW callbacks for the window.
817845
glfwSetFramebufferSizeCallback(window, GLFWFramebufferSizeCallback);
846+
glfwSetWindowPosCallback(window, GLFWWindowPosCallback);
818847
glfwSetWindowRefreshCallback(window, GLFWWindowRefreshCallback);
819848
GLFWAssignEventCallbacks(window);
820849

@@ -904,10 +933,12 @@ void FlutterDesktopWindowSetPixelRatioOverride(
904933
// Send a metrics update using the new pixel ratio.
905934
int width_px, height_px;
906935
glfwGetFramebufferSize(flutter_window->window, &width_px, &height_px);
936+
int left_px, top_px;
937+
glfwGetWindowPos(flutter_window->window, &left_px, &top_px);
907938
if (width_px > 0 && height_px > 0) {
908939
auto* controller = GetWindowController(flutter_window->window);
909-
SendScreenMetrics(controller, width_px, height_px);
910-
SendWindowMetrics(controller, width_px, height_px);
940+
SendScreenMetrics(controller, left_px, top_px, width_px, height_px);
941+
SendWindowMetrics(controller, left_px, top_px, width_px, height_px);
911942
}
912943
}
913944

0 commit comments

Comments
 (0)