Ventto / mons

POSIX Shell script to quickly manage monitors on X
MIT License
632 stars 39 forks source link

Feature Request: Auto detect previously used display and apply last setting #21

Open AdamSLevy opened 6 years ago

AdamSLevy commented 6 years ago

I use Mons on my laptop on a daily basis. I love the simplicity but I wish there was a way that it could be totally automated. I would appreciate if the -a option which daemonizes the process would also detect when a monitor was plugged in and set the display to some pre configured state, from either a configuration file or just by remembering how the user had it set previously.

I'm going to start perusing your code and see if there is a simple way for me to add this feature and submit a pull request. Let me know if this is something you feel fits your project and if so please suggest how you think this should be implemented. Thank you!

Ventto commented 6 years ago

Hi @AdamSLevy, Thanks for the feedback and future contributions perhaps.

set the display to some pre configured state, from either ... just by remembering how the user had it set previously.

It seems to be linked with #20. Initially, @pkhamutou had set his two monitors via a specific xrandr command but after running mons, he got an unexpected display. Indeed, mons turns off monitors and resets display in certain cases with xrandr --auto that definitely breaks the previous very-specific settings (but of course, not the simplest ones).

Right. I've been investigating for few days on xrandr options but none have turned up yet.

from either a configuration file

Priority: keeping mons as simplest as possible. Assigning a rc file to mons sounds like a risky bet. Leveraging the Xorg /etc/X11/xorg.conf.d/*.conf configuration files or X11 existing features as post-detection hook could help.

To be honest, I feel like let's make out the tip of the iceberg to finally decide.

AdamSLevy commented 6 years ago

Thanks for the reply.

I understand and appreciate wanting to keep mons as simple as possible.

Instead of a configuration file, how do you feel about parsing the -e option along with the -a option. This would then daemonize mons and look for an added display to extend and run the -o option when a display is removed.

I suppose this could be implemented with a systemd service or udev rules calling mons. That might be something to package with mons in /usr/share

Just brainstorming as this is something I have wanted to automate for myself.

On Thu, Jan 11, 2018, 1:20 PM Thomas Venriès notifications@github.com wrote:

Hi @AdamSLevy https://github.com/adamslevy, Thanks for the feedback and future contributions perhaps.

set the display to some pre configured state, from either ... just by remembering how the user had it set previously.

It seems to be linked with #20 https://github.com/Ventto/mons/issues/20. Initially, @pkhamutou https://github.com/pkhamutou had set his two monitors via a specific xrandr command but after running mons, he got an unexpected display. Indeed, mons turns off monitors and resets display in certain cases with xrandr --auto that definitely breaks the previous very-specific settings (but of course, not the simplest ones).

Right. Last week, I've been investigating for few days on xrandr options but none have turned up yet.

from either a configuration file

Priority: keeping mons as simplest as possible. Assigning a rc file to mons sounds like a risky bet. Leveraging the Xorg /etc/X11/xorg.conf.d/*.conf configuration files or X11 existing features as post-detection hook could help.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Ventto/mons/issues/21#issuecomment-357080449, or mute the thread https://github.com/notifications/unsubscribe-auth/AF2XcKwQCqIYMfui6WYZmSAjSWTJFPgvks5tJokogaJpZM4RYths .

Ventto commented 6 years ago

how do you feel about parsing the -e option along with the -a option.

EDIT: Why not ? Adding an option in conjunction with -a that will be triggered after plugging-in a monitor:

# mons -a [OPTION]
$ mons -a -e left
$ mons -a -d
$ mons -a -S 0,2:R
[and so on...]
AdamSLevy commented 6 years ago

I'm just commenting to share a script that I wrote to solve this issue for my specific use case. I have a udev rule that detects changes to my specific monitor and then calls this script. When two displays are detected, and it is not already in extend mode, it extends the display and moves all of my i3 windows over to the new display.

I doubt much of this will be useful to you for use in mons but it might give others who come here searching for a solution an idea of how they can solve it themselves. Thanks again for your awesome minimalist software! I am using mons, xpub, batify, and pug!

My script, called by my udev rule on display change:

#!/bin/bash
logfile="/tmp/setup-display"
if [[ -f "$logfile" ]]; then
    cat $logfile | tail -n10 > $logfile.tmp
    mv $logfile.tmp $logfile
fi

exec >> $logfile
exec 2>&1

sleep 3

mons_out=$(mons)
num_monitors=$(echo "$mons_out" | grep "Monitors" | awk '{print $2}')
mons_mode=$(echo "$mons_out" | grep "Mode" | awk '{print $2}')
edp=$(echo "$mons_out" | grep "eDP" | awk '{print $2}')
hdmi=$(echo "$mons_out" | grep "HDMI" | awk '{print $2}')

echo
date
echo "$mons_out"
if [[ $num_monitors -eq 2 ]]; then
    echo "Two displays."
    if [[ "$mons_mode" == "extend" ]]; then
        echo "Already extended."
        exit 0
    fi
    echo "Extending..."
    mons -e left
    while [[ "$ret" != '[{"success":true}]' ]]; do
        echo "Moving workspaces to output $hdmi..."
        ret=$(i3-msg "[class=\".*\"] move workspace to output $hdmi" | grep '"success"')
        sleep .25
    done
else
    echo "One display."
    mons -o
fi
Ventto commented 6 years ago

Thanks @AdamSLevy for contribution. I've just read your answer. I'll handle it soon.