From cd4e1c612f9c78b1fd9081e28914dfd58087ccac Mon Sep 17 00:00:00 2001 From: Igor Demyanov Date: Sat, 3 May 2025 17:49:24 +0300 Subject: [PATCH 1/2] add linux_dpms package --- packages/linux_dpms/.gitignore | 7 ++ packages/linux_dpms/LICENSE | 21 +++++ packages/linux_dpms/analysis_options.yaml | 30 +++++++ packages/linux_dpms/lib/linux_dpms.dart | 3 + .../linux_dpms/lib/src/dpms_power_state.dart | 17 ++++ packages/linux_dpms/lib/src/linux_dpms.dart | 83 +++++++++++++++++++ .../linux_dpms/lib/src/linux_dpms_stub.dart | 15 ++++ .../linux_dpms/lib/src/linux_dpms_web.dart | 13 +++ packages/linux_dpms/pubspec.yaml | 13 +++ 9 files changed, 202 insertions(+) create mode 100644 packages/linux_dpms/.gitignore create mode 100644 packages/linux_dpms/LICENSE create mode 100644 packages/linux_dpms/analysis_options.yaml create mode 100644 packages/linux_dpms/lib/linux_dpms.dart create mode 100644 packages/linux_dpms/lib/src/dpms_power_state.dart create mode 100644 packages/linux_dpms/lib/src/linux_dpms.dart create mode 100644 packages/linux_dpms/lib/src/linux_dpms_stub.dart create mode 100644 packages/linux_dpms/lib/src/linux_dpms_web.dart create mode 100644 packages/linux_dpms/pubspec.yaml diff --git a/packages/linux_dpms/.gitignore b/packages/linux_dpms/.gitignore new file mode 100644 index 0000000..3cceda5 --- /dev/null +++ b/packages/linux_dpms/.gitignore @@ -0,0 +1,7 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ + +# Avoid committing pubspec.lock for library packages; see +# https://dart.dev/guides/libraries/private-files#pubspeclock. +pubspec.lock diff --git a/packages/linux_dpms/LICENSE b/packages/linux_dpms/LICENSE new file mode 100644 index 0000000..20ca2e3 --- /dev/null +++ b/packages/linux_dpms/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Klaralvdalens Datakonsult AB + +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. diff --git a/packages/linux_dpms/analysis_options.yaml b/packages/linux_dpms/analysis_options.yaml new file mode 100644 index 0000000..dee8927 --- /dev/null +++ b/packages/linux_dpms/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/packages/linux_dpms/lib/linux_dpms.dart b/packages/linux_dpms/lib/linux_dpms.dart new file mode 100644 index 0000000..d3862d6 --- /dev/null +++ b/packages/linux_dpms/lib/linux_dpms.dart @@ -0,0 +1,3 @@ +export 'src/linux_dpms_stub.dart' + if (dart.library.io) 'src/linux_dpms.dart' + if (dart.library.js) 'src/linux_dpms_web.dart'; diff --git a/packages/linux_dpms/lib/src/dpms_power_state.dart b/packages/linux_dpms/lib/src/dpms_power_state.dart new file mode 100644 index 0000000..4b48aae --- /dev/null +++ b/packages/linux_dpms/lib/src/dpms_power_state.dart @@ -0,0 +1,17 @@ +/* +https://github.com/torvalds/linux/blob/master/include/uapi/drm/drm_mode.h#L143 +/* bit compatible with the xorg definitions. */ +#define DRM_MODE_DPMS_ON 0 +#define DRM_MODE_DPMS_STANDBY 1 +#define DRM_MODE_DPMS_SUSPEND 2 +#define DRM_MODE_DPMS_OFF 3 +*/ +enum DpmsPowerState { + on(0), + standby(1), + suspend(2), + off(3); + + final int value; + const DpmsPowerState(this.value); +} diff --git a/packages/linux_dpms/lib/src/linux_dpms.dart b/packages/linux_dpms/lib/src/linux_dpms.dart new file mode 100644 index 0000000..ba1e1ad --- /dev/null +++ b/packages/linux_dpms/lib/src/linux_dpms.dart @@ -0,0 +1,83 @@ +import 'dart:ffi' as ffi; +import 'package:linux_dpms/src/dpms_power_state.dart'; + +/* +uint32_t dpms_isAvailable(); +void dpms_setProperty(uint64_t value); +uint64_t dpms_getProperty(); +*/ + +typedef DpmsGetPropertyFuncType = ffi.Uint64 Function(); +typedef DpmsGetPropertyType = int Function(); + +typedef DpmsSetPropertyFuncType = ffi.Void Function(ffi.Uint64 value); +typedef DpmsSetPropertyType = void Function(int value); + +typedef DpmsIsAvailableFuncType = ffi.Uint32 Function(); +typedef DpmsIsAvailableType = int Function(); + +class DpmsLinux { + final _DpmsLinux _service = _DpmsLinux.instance; + bool isAvailable() { + if (_service.dpmsIsAvailable == null || _service.dpmsSetProperty == null) { + return false; + } + return true; + } + + void setPowerState(DpmsPowerState state) { + if (_service.dpmsSetProperty != null) { + _service.dpmsSetProperty!(state.value); + } + return; + } + + DpmsPowerState getPowerState() { + if (_service.dpmsGetProperty != null) { + final value = _service.dpmsGetProperty!(); + return DpmsPowerState.values.firstWhere((item) => item.value == value); + } + return DpmsPowerState.on; + } +} + +class _DpmsLinux { + final DpmsIsAvailableType? dpmsIsAvailable; + final DpmsGetPropertyType? dpmsGetProperty; + final DpmsSetPropertyType? dpmsSetProperty; + + _DpmsLinux._constructor({ + required this.dpmsIsAvailable, + required this.dpmsGetProperty, + required this.dpmsSetProperty, + }); + + factory _DpmsLinux._() { + final lib = ffi.DynamicLibrary.process(); + + try { + final dpmsIsAvailable = lib.lookupFunction("dpms_isAvailable"); + final dpmsGetProperty = lib.lookupFunction("dpms_getProperty"); + final dpmsSetProperty = lib.lookupFunction("dpms_setProperty"); + + return _DpmsLinux._constructor( + dpmsIsAvailable: dpmsIsAvailable, + dpmsGetProperty: dpmsGetProperty, + dpmsSetProperty: dpmsSetProperty, + ); + } on ArgumentError { + return _DpmsLinux._constructor( + dpmsIsAvailable: null, + dpmsGetProperty: null, + dpmsSetProperty: null, + ); + } + } + + static _DpmsLinux? _instance; + + static _DpmsLinux get instance { + _instance ??= _DpmsLinux._(); + return _instance!; + } +} diff --git a/packages/linux_dpms/lib/src/linux_dpms_stub.dart b/packages/linux_dpms/lib/src/linux_dpms_stub.dart new file mode 100644 index 0000000..afca0c3 --- /dev/null +++ b/packages/linux_dpms/lib/src/linux_dpms_stub.dart @@ -0,0 +1,15 @@ +import 'package:linux_dpms/src/dpms_power_state.dart'; + +class DpmsLinux { + bool isAvailable() { + throw UnimplementedError(); + } + + void setPowerState(DpmsPowerState state) { + throw UnimplementedError(); + } + + DpmsPowerState getPowerState() { + throw UnimplementedError(); + } +} diff --git a/packages/linux_dpms/lib/src/linux_dpms_web.dart b/packages/linux_dpms/lib/src/linux_dpms_web.dart new file mode 100644 index 0000000..aaac0b6 --- /dev/null +++ b/packages/linux_dpms/lib/src/linux_dpms_web.dart @@ -0,0 +1,13 @@ +import 'package:linux_dpms/src/dpms_power_state.dart'; + +class DpmsLinux { + bool isAvailable() { + return false; + } + + void setPowerState(DpmsPowerState state) {} + + DpmsPowerState getPowerState() { + return DpmsPowerState.on; + } +} diff --git a/packages/linux_dpms/pubspec.yaml b/packages/linux_dpms/pubspec.yaml new file mode 100644 index 0000000..26e0d5e --- /dev/null +++ b/packages/linux_dpms/pubspec.yaml @@ -0,0 +1,13 @@ +name: linux_dpms +description: Package for using DPMS on linux. +version: 0.1.0+1 +repository: https://github.com/ardera/flutter_packages + +environment: + sdk: '>=3.0.0 <4.0.0' + +dev_dependencies: + lints: ^3.0.0 + test: ^1.21.0 + + \ No newline at end of file From c0945b4e7eef3201a93f868f0262222ae19315f0 Mon Sep 17 00:00:00 2001 From: Igor Demyanov Date: Sat, 3 May 2025 17:56:34 +0300 Subject: [PATCH 2/2] export DpmsPowerState --- packages/linux_dpms/lib/linux_dpms.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/linux_dpms/lib/linux_dpms.dart b/packages/linux_dpms/lib/linux_dpms.dart index d3862d6..b7b149b 100644 --- a/packages/linux_dpms/lib/linux_dpms.dart +++ b/packages/linux_dpms/lib/linux_dpms.dart @@ -1,3 +1,4 @@ export 'src/linux_dpms_stub.dart' if (dart.library.io) 'src/linux_dpms.dart' if (dart.library.js) 'src/linux_dpms_web.dart'; +export 'src/dpms_power_state.dart';