Turn off the monitor from the keyboard

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

    1) It can be done programmatically (ddccontrol)
    2) It can be written in a script
    3) The script can be hung on a hotkey
    5) PROFIT ??

    In the profit list, you can record the ability to turn on / off with 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 nicer to press than the button of the monitor (on which it needs to be aimed, and the monitor may turn slightly from pressing, which is annoying).

    The task consists of three stages:
    0) Set up ddccontrol
    1) write a script that will adequately work on / off monitors in any combination of turned off monitors.
    2) Find an unused button on the keyboard (and its code).
    3) Assign a script run to this code.

    Configure ddccontrol

    The setup is simple: you need to register the i2c-dev module in / etc / modules (modules that are loaded automatically when loading). For the first start, you can get by with a command modprobe i2c-devthat 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 talked about how to work with ddccontrol, so I won’t retell everything. For my samsungs (both) the register 0xe1 turns on / off 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

    Omitting everything 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 on the output: 1 - on, 0 - off. We will handle the “empty line” situation with an ugly but working summation with zero. (empty + zero = zero).


    TK for the script: if at least one monitor is on, turn off all monitors. If there are no monitors turned on, try turning on all monitors. Due to the specifics of ddcontrol, we will either have to allow ourselves to do sudo ddccontrol without a password (you can add the script yourself), or change the access rights for the / dev / i2c- * files (I just changed the group to mine, 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 with the command 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
            state = $ ((($ state + 0) | (`ddccontrol -r $ reg $ dev | grep -o + /. / 1 ​​| cut -b 3` + 0)))
    echo state = $ state
    for dev in $ seq
            ddccontrol -r $ reg -w $ ((! $ state)) $ dev &

    Comments on the code: begin / end - customizable. For the first time, you can write 0 and 6.
    If you remove the ampersand, it will be easier to debug, but the script will work more slowly.
    I do not recommend parallel polling of monitors, there will be a race condition.

    Button search

    I decided to use the unused multimedia button on the keyboard. For this, the xev program (run from shell) was used. Press the desired button, see the code. In my case, it was a “my computer” button (code 198) with a system icon with a monitor (monitor icon - which might be better for turning monitors on / off, especially since it’s difficult to get to it from the very edge?)

    Assigning a script to a hotkey

    I used the following method (gurus will kick for curvature, request to kick on demand to apply more direct solutions, with binders at the system-wide level):

    Edit (create) the file ~ / .xbindkeysrc

    (attention: quotation marks, a space before F31)

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

    xmodmap -e 'keycode 198 = F31'

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

    Actually, that's all. We registered a hotkey, registered it where necessary. Now, at the touch of a button, if there is at least one monitor turned on, all monitors turn off. If all monitors are turned off, they turn on.

    Also popular now: