3
\$\begingroup\$

DmenuWacom

During my raids, I came across and solved several problems, obviously in my style, very similar to the suckless.org approach and which still works quite well. In fact, I started writing a script and at the moment it works very well and has already received some contributions even though I am trying to improve it as much as possible and also to add more features.

Project page: https://github.com/NF02/dmenuWacom

Functionality

  1. ScreenLocking - allows you to select the screen you want to work on.
ScreenLocking(){
    launcher_app=$(echo -e "$MONITORS" | dmenu -i -p "Select display: ")
    xinput map-to-output "$STYLUS" $launcher_app
}
  1. ScreenRotation - allows you to rotate the surface of the tablet, but at the moment it is still not very functional since it does not allow you to select the monitor you want to work on yet.
ScreenRotation(){
    rotate="left\nright\nnormal"
    launcher_app=$(echo -e "$rotate" | dmenu -i -p "Select the direction of rotation: ")
    case $launcher_app in
        "left") 
            xinput set-prop "$STYLUS" --type=float "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1;;
        "right")
            xinput set-prop "$STYLUS" --type=float "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1;;
        "normal")
            xinput set-prop "$STYLUS" --type=float "Coordinate Transformation Matrix" 0 0 0 0 0 0 0 0 0;;

    esac
}
  1. Button_setting - allows you to program the keys of the pad
Button_setting() {
    nbut=`xsetwacom -s get "$PAD" all | grep "Button" | wc -l`

    for number in {1..$nbut}
    do
        buttons+="$number\n"
    done
    launcher_button=$(echo -e "$buttons" | dmenu -i -p "Button: ")
    if [[ $launcher_button -lt "0" ]] || [[ $launcher_button > $nbut ]];then
        notify-send ""
        exit -1
    fi
    if [[ $launcher_button > "3" ]]; then
        launcher_button=$(echo "$launcher_button+4" | bc)
        Button_set $launcher_button
    else if [[ $launcher_button < $nbut ]]; then
            Button_set $launcher_button
        fi
    fi

}

Button_set(){
    tmp=`cat dmw_history`
    if [[ $1 > "7" ]]; then
        nbut=$(echo "$1-4" | bc)
        launcher_but=$(echo -e "" | dmenu -i -p "Function $nbut: ")
    else
        launcher_but=$(echo -e "$tmp\n" | dmenu -i -p "Function $1: ")
    fi
    xsetwacom set "$PAD" Button $1 key $launcher_but
    echo "$launcher_but" >> dmw_history
}

As mentioned this was a convenient solution but it is standard and is based on wacom's xorg drivers.

\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

Use native shell features for simple math

Using native shell features to do simple math operations is faster than using the bc program, which has the overhead of running another process, and it's just another dependency to worry about.

Instead of this:

launcher_button=$(echo "$launcher_button+4" | bc)

nbut=$(echo "$1-4" | bc)

You can write:

launcher_button=$((launcher_button + 4))

nbut=$(($1 - 4))

Or alternatively:

((launcher_button += 4))

((nbut = $1 - 4))

Use $(...) instead of `...`

Both of these forms of syntax achieve the same thing, but the $(...) form is more modern, and easier to read, especially when using it in nested calls.

The posted code uses both, in all cases you can use simply one way, the better way, $(...).

Use elif instead if else if

Instead of this:

if [[ $launcher_button > "3" ]]; then
    launcher_button=$(echo "$launcher_button+4" | bc)
    Button_set $launcher_button
else if [[ $launcher_button < $nbut ]]; then
        Button_set $launcher_button
    fi
fi

Use an elif like this:

if [[ $launcher_button > "3" ]]; then
    launcher_button=$(echo "$launcher_button+4" | bc)
    Button_set $launcher_button
elif [[ $launcher_button < $nbut ]]; then
    Button_set $launcher_button
fi

Avoid unnecessary loading of data into variables

In this code:

Button_set(){
    tmp=`cat dmw_history`
    if [[ $1 > "7" ]]; then
        nbut=$(echo "$1-4" | bc)
        launcher_but=$(echo -e "" | dmenu -i -p "Function $nbut: ")
    else
        launcher_but=$(echo -e "$tmp\n" | dmenu -i -p "Function $1: ")
    fi
    xsetwacom set "$PAD" Button $1 key $launcher_but
    echo "$launcher_but" >> dmw_history
}

The first statement loads the content of dmw_history into tmp variable.

For one thing, this is premature, because if $1 > 7 then this variable won't be used.

Secondly, this variable is later printed with an echo to pipe into another command.

No need to load this content at all, the one statement that needs this can be written like this:

launcher_but=$(dmenu -i -p "Function $1: " < dmw_history)

The above is equivalent to, and better than this:

launcher_but=$(cat dmw_history | dmenu -i -p "Function $1: ")

Use < instead of -lt

Instead of:

[[ $launcher_button -lt "0" ]]

You can write:

[[ $launcher_button < 0 ]]

No need for the quotes around numeric literals.

Use printf instead of echo -e

echo -e is generally unreliable, because its behavior is very implementation dependent. printf on the other hand works consistently everywhere. Just remember that it doesn't add a \n automatically at the end.

If you don't need to print text with special patterns such as \n and \t, then you don't need echo -e, you don't need printf either, use a simple echo in that case.

For example there's really no reason to write echo -e "", when a simple echo (without parameters) is exactly the same.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.