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

add linux_dpms package #41

wants to merge 2 commits into from

Conversation

DisDis
Copy link

@DisDis DisDis commented May 3, 2025

ardera/flutter-pi#478
Adds the ability to use dpms(Screen On/Off) in projects on flutter-pi with KMS

Copy link
Owner

@ardera ardera left a comment

Choose a reason for hiding this comment

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

Generally looks good!

I think it'd also be fine to call the package dpms instead of linux_dpms. I know the other packages are called that way, but that's just because I couldn't claim they work on windows/mac/android/ios. But since there's no way to control DPMS directly on windows/mac/android etc, this package is technically feature complete even on those platforms. :)

Comment on lines +2 to +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);
}
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)

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

@@ -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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants