Command line to tell if monitor is off in wayland?

I used to do this in a bash script to tell if a monitor was on or off (“off” meaning off, or what happens after xset dpms force off is run):

xset q | grep "Monitor is Off"

…what is an equivalent under Wayland? I found ddccontrol -p on another answer, and it seems like it would work but it is far too slow (takes multiple seconds to return).

In Wayland, detecting the monitor’s on/off status is trickier than in X11 since Wayland has tighter control over hardware to improve security, limiting direct access to monitor state. Here are some potential approaches you can try:

  1. Using swaymsg (if using Sway compositor): If you’re using Sway (a Wayland compositor compatible with i3), you can check the output status with swaymsg:
swaymsg -t get_outputs | grep "DPMS"

This will show the DPMS state for each output. “DPMS” being off generally indicates the monitor is off.

  1. Using wlr-randr: This tool is part of the Wayland utilities for wlroots-based compositors and can show output status, including DPMS state:
wlr-randr | grep "DPMS"

Like swaymsg, this only works if you’re on a wlroots compositor (like Sway or River).

  1. ddcutil for hardware-based check: If your monitor supports DDC/CI (Display Data Channel Command Interface), you could use ddcutil to query the monitor directly. This can be slower but will work independently of Wayland:
ddcutil getvcp 0x10

This command checks power status directly from the monitor but may still be slower than desired.

  1. Polling method with a small interval: As a workaround, you could create a script that checks every few seconds, caching the status to avoid multiple runs of a slow command.

If you’re comfortable with a specific Wayland compositor, there might be an additional option tailored to that environment. However, overall, Wayland’s design limits direct access, so solutions may vary in reliability and speed.