-
Notifications
You must be signed in to change notification settings - Fork 10
add linux_dpms package #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good, but I'd separate it a bit more. It's good practice to make this a Also maybe shorten the method names a bit, I think it's still clear enough what Also removed the base class DPMS {
void setMode(PowerMode mode);
PowerMode getMode();
static DPMS _createInstance() {
try {
return FlutterpiDPMS.fromLocalProcess();
} on UnsupportedError {
return null;
}
}
/// Returns the concrete DPMS instance, or null if not available.
static late final DPMS? instance = _createInstance();
} and then in a separate file, e.g. class FlutterpiDPMS extends DPMS {
FlutterpiDPMS(this._get, this._set);
factory FlutterpiDPMS.fromLocalProcess() {
try {
// ...
// DpmsIsAvailable() is false, throw an UnsupportedError too
} on ArgumentError {
throw UnsupportedError('Flutter-Pi DPMS is not supported on this platform.');
}
}
final DpmsGetType _getMode;
final DpmsSetType _setMode;
@override
PowerMode getMode() {
// ...
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I called |
||
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<DpmsIsAvailableFuncType, DpmsIsAvailableType>("dpms_isAvailable"); | ||
final dpmsGetProperty = lib.lookupFunction<DpmsGetPropertyFuncType, DpmsGetPropertyType>("dpms_getProperty"); | ||
final dpmsSetProperty = lib.lookupFunction<DpmsSetPropertyFuncType, DpmsSetPropertyType>("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!; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
ardera marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:linux_dpms/src/dpms_power_state.dart'; | ||
|
||
class DpmsLinux { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you allow |
||
bool isAvailable() { | ||
return false; | ||
} | ||
|
||
void setPowerState(DpmsPowerState state) {} | ||
|
||
DpmsPowerState getPowerState() { | ||
return DpmsPowerState.on; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about this, it seems like atomic KMS drivers (which is basically every driver nowadays) only differentiate between
on
andoff
:https://github.com/torvalds/linux/blob/92a09c47464d040866cf2b4cd052bc60555185fb/drivers/gpu/drm/drm_atomic_uapi.c#L958-L959
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also,
DpmsPowerState
is a bit verbose, maybe call itPowerState
orPowerMode
(If people have naming conflicts they can just import the package with a prefix)