Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/linux_dpms/.gitignore
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
21 changes: 21 additions & 0 deletions packages/linux_dpms/LICENSE
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.
30 changes: 30 additions & 0 deletions packages/linux_dpms/analysis_options.yaml
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
4 changes: 4 additions & 0 deletions packages/linux_dpms/lib/linux_dpms.dart
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';
17 changes: 17 additions & 0 deletions packages/linux_dpms/lib/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);
}
Comment on lines +2 to +17
Copy link
Owner

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 and off:
https://github.com/torvalds/linux/blob/92a09c47464d040866cf2b4cd052bc60555185fb/drivers/gpu/drm/drm_atomic_uapi.c#L958-L959

Suggested change
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);
}
enum DpmsPowerState { on, off };

Copy link
Owner

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 it PowerState or PowerMode (If people have naming conflicts they can just import the package with a prefix)

83 changes: 83 additions & 0 deletions packages/linux_dpms/lib/src/linux_dpms.dart
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 {
Copy link
Owner

@ardera ardera May 5, 2025

Choose a reason for hiding this comment

The 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 base class that just defines an interface and doesn't actually implement anything, and then have a concrete implementation for each platform (so only flutter-pi atm, but maybe the sony embedder or maybe some full-screen wayland embedder want to implement this too.)

Also maybe shorten the method names a bit, I think it's still clear enough what setMode and getMode do, even without the Power in the name.

Also removed the isAvailable method, and instead make DPMS.instance return null if not available. So that means if you have a DPMS instance, you know it's valid & you can use it, instead of having to check isAvailable (and keeping track of whether you've checked it)

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. lib/src/platform/flutterpi.dart, a concrete implementation:

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() {
    // ...
  }
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I called DpmsPowerState ==> PowerMode here, just because the function would otherwise be called getState and setState, which sounds a bit like it does more than just set some property

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!;
}
}
15 changes: 15 additions & 0 deletions packages/linux_dpms/lib/src/linux_dpms_stub.dart
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();
}
}
13 changes: 13 additions & 0 deletions packages/linux_dpms/lib/src/linux_dpms_web.dart
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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you allow DPMS.instance returning null, you don't need to provide a DPMS implementation for web, just make it return null there. But that's your call

bool isAvailable() {
return false;
}

void setPowerState(DpmsPowerState state) {}

DpmsPowerState getPowerState() {
return DpmsPowerState.on;
}
}
13 changes: 13 additions & 0 deletions packages/linux_dpms/pubspec.yaml
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