Skip to content

Commit

Permalink
chore: Add screen_capturer_windows package
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Jan 25, 2024
1 parent d9a87de commit 5e8edcc
Show file tree
Hide file tree
Showing 18 changed files with 727 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/screen_capturer_windows/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
build/
30 changes: 30 additions & 0 deletions packages/screen_capturer_windows/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "ef1af02aead6fe2414f3aafa5a61087b610e1332"
channel: "stable"

project_type: plugin

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: ef1af02aead6fe2414f3aafa5a61087b610e1332
base_revision: ef1af02aead6fe2414f3aafa5a61087b610e1332
- platform: windows
create_revision: ef1af02aead6fe2414f3aafa5a61087b610e1332
base_revision: ef1af02aead6fe2414f3aafa5a61087b610e1332

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
3 changes: 3 additions & 0 deletions packages/screen_capturer_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.2.0

* First release.
21 changes: 21 additions & 0 deletions packages/screen_capturer_windows/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022-2024 LiJianying <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions packages/screen_capturer_windows/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# screen_capturer_windows

[![pub version][pub-image]][pub-url]

[pub-image]: https://img.shields.io/pub/v/screen_capturer_windows.svg
[pub-url]: https://pub.dev/packages/screen_capturer_windows

The windows implementation of [screen_capturer](https://pub.dev/packages/screen_capturer).

## License

[MIT](./LICENSE)
1 change: 1 addition & 0 deletions packages/screen_capturer_windows/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:mostly_reasonable_lints/flutter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'src/screen_capturer_windows.dart';
105 changes: 105 additions & 0 deletions packages/screen_capturer_windows/lib/src/commands/ms_screenclip.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:flutter/services.dart';
import 'package:screen_capturer_platform_interface/screen_capturer_platform_interface.dart';
import 'package:win32/win32.dart';

final Map<CaptureMode, String> _knownCaptureModeArgs = {
CaptureMode.region: 'Rectangle',
CaptureMode.screen: '',
CaptureMode.window: 'Window',
};

bool _isScreenClipping() {
final int hWnd = GetForegroundWindow();
final lpdwProcessId = calloc<Uint32>();

GetWindowThreadProcessId(hWnd, lpdwProcessId);
// Get a handle to the process.
final hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
lpdwProcessId.value,
);

if (hProcess == 0) {
return false;
}

// Get a list of all the modules in this process.
final hModules = calloc<HMODULE>(1024);
final cbNeeded = calloc<DWORD>();

try {
int r = EnumProcessModules(
hProcess,
hModules,
sizeOf<HMODULE>() * 1024,
cbNeeded,
);

if (r == 1) {
for (var i = 0; i < (cbNeeded.value ~/ sizeOf<HMODULE>()); i++) {
final szModName = wsalloc(MAX_PATH);
// Get the full path to the module's file.
final hModule = hModules.elementAt(i).value;
if (GetModuleFileNameEx(hProcess, hModule, szModName, MAX_PATH) != 0) {
String moduleName = szModName.toDartString();
if (moduleName.contains('ScreenClippingHost.exe')) {
free(szModName);
return true;
}
}
free(szModName);
}
}
} finally {
free(hModules);
free(cbNeeded);
CloseHandle(hProcess);
}

return false;
}

class _MsScreenclip with SystemScreenCapturer {
@override
Future<void> capture({
required CaptureMode mode,
String? imagePath,
bool copyToClipboard = true,
bool silent = true,
}) async {
if (mode == CaptureMode.screen) {
assert(imagePath != null);
await ScreenCapturerPlatform.instance.captureScreen(
imagePath: imagePath!,
);
return;
}
await Clipboard.setData(const ClipboardData(text: ''));
ShellExecute(
0,
'open'.toNativeUtf16(),
'ms-screenclip://?clippingMode=${_knownCaptureModeArgs[mode]}'
.toNativeUtf16(),
nullptr,
nullptr,
SW_SHOWNORMAL,
);
await Future.delayed(const Duration(seconds: 1));

while (_isScreenClipping()) {
await Future.delayed(const Duration(milliseconds: 200));
}

if (imagePath != null) {
await ScreenCapturerPlatform.instance.saveClipboardImageAsPngFile(
imagePath: imagePath,
);
}
}
}

final msScreenclip = _MsScreenclip();
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:screen_capturer_platform_interface/screen_capturer_platform_interface.dart';
import 'package:screen_capturer_windows/src/commands/ms_screenclip.dart';

class ScreenCapturerWindows extends MethodChannelScreenCapturer {
/// The [ScreenCapturerWindows] constructor.
ScreenCapturerWindows() : super();

/// Registers this class as the default instance of [ScreenCapturerWindows].
static void registerWith() {
ScreenCapturerPlatform.instance = ScreenCapturerWindows();
}

@override
SystemScreenCapturer get systemScreenCapturer => msScreenclip;
}
28 changes: 28 additions & 0 deletions packages/screen_capturer_windows/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: screen_capturer_windows
description: Windows implementation of the screen_capturer plugin.
version: 0.2.0
repository: https://github.com/leanflutter/screen_capturer/tree/main/packages/screen_capturer_windows

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.3.0"

dependencies:
ffi: ^2.0.2
flutter:
sdk: flutter
screen_capturer_platform_interface: ^0.2.0
win32: '>=2.6.0 <6.0.0'

dev_dependencies:
flutter_test:
sdk: flutter
mostly_reasonable_lints: ^0.1.1

flutter:
plugin:
implements: screen_capturer
platforms:
windows:
dartPluginClass: ScreenCapturerWindows
pluginClass: ScreenCapturerWindowsPluginCApi
17 changes: 17 additions & 0 deletions packages/screen_capturer_windows/windows/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
flutter/

# Visual Studio user-specific files.
*.suo
*.user
*.userosscache
*.sln.docstates

# Visual Studio build-related files.
x64/
x86/

# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
101 changes: 101 additions & 0 deletions packages/screen_capturer_windows/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# The Flutter tooling requires that developers have a version of Visual Studio
# installed that includes CMake 3.14 or later. You should not increase this
# version, as doing so will cause the plugin to fail to compile for some
# customers of the plugin.
cmake_minimum_required(VERSION 3.14)

# Project-level configuration.
set(PROJECT_NAME "screen_capturer_windows")
project(${PROJECT_NAME} LANGUAGES CXX)

# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
# versions of CMake.
cmake_policy(VERSION 3.14...3.25)

# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "screen_capturer_windows_plugin")

# Any new source files that you add to the plugin should be added here.
list(APPEND PLUGIN_SOURCES
"screen_capturer_windows_plugin.cpp"
"screen_capturer_windows_plugin.h"
)

# Define the plugin library target. Its name must not be changed (see comment
# on PLUGIN_NAME above).
add_library(${PLUGIN_NAME} SHARED
"include/screen_capturer_windows/screen_capturer_windows_plugin_c_api.h"
"screen_capturer_windows_plugin_c_api.cpp"
${PLUGIN_SOURCES}
)

# Apply a standard set of build settings that are configured in the
# application-level CMakeLists.txt. This can be removed for plugins that want
# full control over build settings.
apply_standard_settings(${PLUGIN_NAME})

# Symbols are hidden by default to reduce the chance of accidental conflicts
# between plugins. This should not be removed; any symbols that should be
# exported should be explicitly exported with the FLUTTER_PLUGIN_EXPORT macro.
set_target_properties(${PLUGIN_NAME} PROPERTIES
CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_compile_definitions(${PLUGIN_NAME} PRIVATE _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING)

# Source include directories and library dependencies. Add any plugin-specific
# dependencies here.
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)

# List of absolute paths to libraries that should be bundled with the plugin.
# This list could contain prebuilt libraries, or libraries created by an
# external build triggered from this build file.
set(screen_capturer_windows_bundled_libraries
""
PARENT_SCOPE
)

# === Tests ===
# These unit tests can be run from a terminal after building the example, or
# from Visual Studio after opening the generated solution file.

# Only enable test builds when building the example (which sets this variable)
# so that plugin clients aren't building the tests.
if (${include_${PROJECT_NAME}_tests})
set(TEST_RUNNER "${PROJECT_NAME}_test")
enable_testing()

# Add the Google Test dependency.
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/release-1.11.0.zip
)
# Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Disable install commands for gtest so it doesn't end up in the bundle.
set(INSTALL_GTEST OFF CACHE BOOL "Disable installation of googletest" FORCE)
FetchContent_MakeAvailable(googletest)

# The plugin's C API is not very useful for unit testing, so build the sources
# directly into the test binary rather than using the DLL.
add_executable(${TEST_RUNNER}
test/screen_capturer_windows_plugin_test.cpp
${PLUGIN_SOURCES}
)
apply_standard_settings(${TEST_RUNNER})
target_include_directories(${TEST_RUNNER} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(${TEST_RUNNER} PRIVATE flutter_wrapper_plugin)
target_link_libraries(${TEST_RUNNER} PRIVATE gtest_main gmock)
# flutter_wrapper_plugin has link dependencies on the Flutter DLL.
add_custom_command(TARGET ${TEST_RUNNER} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${FLUTTER_LIBRARY}" $<TARGET_FILE_DIR:${TEST_RUNNER}>
)

# Enable automatic test discovery.
include(GoogleTest)
gtest_discover_tests(${TEST_RUNNER})
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef FLUTTER_PLUGIN_screen_capturer_WINDOWS_PLUGIN_C_API_H_
#define FLUTTER_PLUGIN_screen_capturer_WINDOWS_PLUGIN_C_API_H_

#include <flutter_plugin_registrar.h>

#ifdef FLUTTER_PLUGIN_IMPL
#define FLUTTER_PLUGIN_EXPORT __declspec(dllexport)
#else
#define FLUTTER_PLUGIN_EXPORT __declspec(dllimport)
#endif

#if defined(__cplusplus)
extern "C" {
#endif

FLUTTER_PLUGIN_EXPORT void ScreenCapturerWindowsPluginCApiRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar);

#if defined(__cplusplus)
} // extern "C"
#endif

#endif // FLUTTER_PLUGIN_screen_capturer_WINDOWS_PLUGIN_C_API_H_
Loading

0 comments on commit 5e8edcc

Please sign in to comment.