⬆️ ⬇️

Turn off the monitor from the keyboard

The previous topic about ddccontrol , opened for me that you can turn off monitors by DDC . Do not drive to sleep, do not turn on the energy-saving mode, but really turn it off. So that the power supply light on the face goes out (and does not blink at all!). Like pushing off on the same face. Of course, you can not only turn it off, but also turn it on. Programmatically!



1) This can be done programmatically (ddccontrol)

2) It can be written in the script

3) The script can be hung on the hotkey

...

5) PROFIT ??



In the list of profits, you can record the ability to enable / disable ONE button on the keyboard of ALL monitors (I have two now and I plan more in the future). The button on the keyboard is much more pleasant to click than the button of the monitor (at which it is necessary to aim, and the monitor can turn slightly when pressed, which is annoying).

')



The task consists of three stages:

0) Configure ddccontrol

1) write a script that will adequately work on / off the monitors in any combination of the turned-off monitors.

2) Find an unused button on the keyboard (and its code).

3) Assign script launch to this code.



Ddccontrol setup



The setup is simple: you need to register the i2c-dev module in / etc / modules (modules that are loaded automatically when you boot) For the first run, you can get by with the modprobe i2c-dev command, which tries to load the module here and now.



Next, we need to check that the monitor turns off the shutdown code, and the enable code turns on.



I previously told how to work with ddccontrol, so I will not retell everything. For my samsung (both) register 0xe1 enables / disables the monitor:



ddccontrol -r 0xe1 -w 1 dev:/dev/i2c-2 - on; -w 0 - off

Similarly for dev: / dev / i2c-1.



We can also get this value: ddccontrol -r 0xe1 dev:/dev/i2c-1



Dropping all unnecessary, the desired line looks like this:



Control 0xe1: + / 0/1 [???] (off)

Control 0xe1: + / 1/1 [???] (on)



We need to set up a simple grep for this:

ddccontrol -r 0xe1 dev:/dev/i2c-1|grep -o +/./1|cut -b 3

In addition to the garbage on stderr (he does not care about us), we get a number at the output: 1 - on, 0 - off. The “empty string” situation will be treated with an ugly, but working summation with zero. (empty + zero = zero).



Script



TK for the script: if at least one monitor is on, turn off all monitors. If there are no included monitors, try to turn on all monitors. Due to the specifics of ddcontrol, we will have to either allow ourselves to do sudo ddccontrol without a password (you can add the script yourself), or change the access rights for / dev / i2c- * files (I just changed the group for my own, this was enough). Just in case, I remind you that SUID bits for shell scripts do not work. You can change the group of device files for ddccontrol using the sudo chgrp `id -gn` /dev/i2c-* .



Script text (I called it / usr / local / bin / ddc-powerswitch ):



 #! / bin / sh
 reg = 0xe1
 begin = 1
 end = 2
 seq = `seq -f" dev: / dev / i2c -%. 0f "$ begin $ end`
 for dev in $ seq
 do
         state = $ ((($ state + 0) | (`ddccontrol -r $ reg $ dev | grep -o + /. / 1 ​​| cut -b 3` + 0)))
 done
 echo state = $ state
 for dev in $ seq
 do
         ddccontrol -r $ reg -w $ ((! $ state)) $ dev &
 done






Comments on the code: begin / end - customizable. For the first time you can register 0 and 6.

If you remove the ampersand, it will be easier to debug, but the script will be slower to work.

I do not recommend parallel poll of monitors, there will be race condition.



Button search



I decided to use an unused multimedia button on the keyboard. For this, the xev program was used (run from the shell). Press the desired button, see the code. In my case, it was the “my computer” button (code 198) with a system icon with a monitor (monitor icon — what can be better suited for on / off monitors, especially since it is from the very edge and is difficult to hit on it?)



Script assignment to hot key



I used the following method (the gurus will kick the curvature, asking for a demand-kick to make more direct decisions, with a system-wide binding):



Edit (create) ~ / .xbindkeysrc file

 "ddc-powerswitch"
  F31


(attention: quotes, space before F31)



Now we will write the launch of everything that is needed in the autorun script. For KDE, this is ~ / .kde / Autostart / ddc-hotkeys (do not forget to make it + x), for gnome ~ / .config / autostart (for older versions of the gnome is more difficult, here ).



 xmodmap -e 'keycode 198 = F31'
 xbindkeys






Instead of 198, you need to specify the key code that you liked when communicating with xev.



Actually, everything. We have registered a hot key, have it registered where necessary. Now, at the press of a button, if there is at least one monitor turned on, all monitors turn off. If all monitors are turned off - turn on.

Source: https://habr.com/ru/post/95934/



All Articles