2016年8月16日火曜日

ワークスペース毎に動くタイリング・シェルスクリプト

前回作成したシェルスクリプトを、ワークスペース毎に操作できるようにする実験


WmCtrlGtk(0)とWmCtrlGtk(1)で、それぞれワークスペース0とワークスペース1を別個に操作する。
(その分、シェルスクリプトから保存するファイルが増えてます。)

#!/bin/bash

PRE_IFS=$IFS
IFS=$'\n'

top_panel_h=0
bottom_panel_h=26
title_h=30
disp_width=$(xwininfo -root | grep 'Width' | cut -f2 -d ':')
disp_height=$(xwininfo -root | grep 'Height' | cut -f2 -d ':')
export current_ws=$(xprop -root -notype _NET_CURRENT_DESKTOP | cut -b 24)

if [ ! $(wmctrl -l | grep "WmCtrlGtk("$current_ws")") ]; then
    export MAIN_DIALOG$current_ws='
    <window title="WmCtrlGtk('$current_ws')">
        <hbox>
            <button>
                <label>h</label>
                <action>'$0' h 200</action>
            </button>
            <button>
                <label>v</label>
                <action>'$0' v 200</action>
            </button>
            <button>
                <label>r</label>
                <action>'$0' r</action>
            </button>
            <button>
                <label>+100</label>
                <action>'$0' 100</action>
            </button>
            <button>
                <label>-100</label>
                <action>'$0' -100</action>
            </button>
        </hbox>
    </window>
    '

    gtkdialog -p MAIN_DIALOG$current_ws -c
fi

rotate_flag=0
if [ $# -eq 1 ]; then
    if [ $1 = 'h' -o $1 = 'v' ]; then
        hv_flag=$1
        main_add=0
    elif [ $1 = 'r' ]; then
        rotate_flag=1
    elif [ $1 -gt 0 -o $1 -lt 0 ]; then
        rotate_flag=2
    else
        hv_flag='h'
    fi
elif [ $# -eq 2 ]; then
    hv_flag=$1
    main_add=$2
else
    exit 0
fi

temp_file='./wm-ctrl'$current_ws'.txt'
if [ $rotate_flag -gt 0 ]; then
    hv_flag=$(sed -n 1p ./wm-ctrl${current_ws}.his | cut -f1 -d',')
    main_add=$(sed -n 1p ./wm-ctrl${current_ws}.his | cut -f2 -d',')
    if [ $rotate_flag -eq 1 ]; then
        temp_1=$(sed -n 2p ./wm-ctrl${current_ws}.his)
        sed -i '$a '$temp_1'' ./wm-ctrl${current_ws}.his
        sed -i 1,2d ./wm-ctrl${current_ws}.his
    else
        sed -i 1d ./wm-ctrl${current_ws}.his
        main_add=$((main_add + $1))
    fi
    sed -i 's/_/ /g' ./wm-ctrl${current_ws}.his
    temp_file='./wm-ctrl'$current_ws'.his'
fi

wmctrl -l > wm-ctrl${current_ws}.txt
sed -i '/WmCtrlGtk/d' ./wm-ctrl${current_ws}.txt
txt_lines=$(wc $temp_file | sed 's/^[ \t]*//' | cut -f1 -d " ")
if [ $txt_lines -gt 0 ]; then
#    current_ws=$(xprop -root -notype _NET_CURRENT_DESKTOP | cut -b 24)
    #active_win=$(xprop -root -notype _NET_ACTIVE_WINDOW | cut -b 33-43)

    ws_windows=0
    for((i = 1; i <= $txt_lines; i++)); do
        ws_flag=$(sed -n $i'p' $temp_file | cut -b 12-13)
        if [ $ws_flag -eq $current_ws ]; then
            let ws_windows++
        fi
    done

    disp_width2=$((disp_width / 2))
    disp_height2=$(($(((disp_height - top_panel_h - bottom_panel_h) / 2)) - title_h))
    disp_width_main=$((disp_width2 + main_add))
    disp_height_main=$((disp_height2 + main_add))
    disp_width_sub=$((disp_width2 - main_add))
    disp_height_sub=$((disp_height2 - main_add))

    pos_width=$((disp_width / 2))
    pos_height=$(((disp_height - top_panel_h - bottom_panel_h) / 2))

    if [ $hv_flag = 'v' ]; then
        x_pos=(0 0 $pos_width $pos_width)
        y_pos=($((0 + top_panel_h)) $((pos_height + top_panel_h + main_add)) \
              $((pos_height + top_panel_h+ main_add)) $((0 + top_panel_h)))
    else
        x_pos=(0 $((pos_width + main_add)) $((pos_width + main_add)) 0)
        y_pos=($((0 + top_panel_h)) $((0 + top_panel_h)) $((pos_height + top_panel_h)) \
              $((pos_height + top_panel_h)))
    fi

    case $ws_windows in
        1)
            temp_width=$disp_width
            temp_height=$((disp_height - top_panel_h - bottom_panel_h - title_h));; 
        [2-3])
            temp_width=$disp_width2
            temp_height=$((disp_height - top_panel_h - bottom_panel_h - title_h));;
        *)
            temp_width=$disp_width2
            temp_height=$disp_height2;;
    esac

    window_array=()
    n=0
    for((i = 1; i <= $txt_lines; i++)); do
        if [ $n -gt 3 ]; then
            n=0
        fi
        ws_flag=$(sed -n $i'p' $temp_file | cut -b 11-13)
        if [ $ws_flag -eq $current_ws ]; then
            win_id=$(sed -n $i'p' $temp_file | cut -b 1-10)
            if [ $rotate_flag -eq 0 ]; then
                if [ $n -eq 0 ]; then
                    echo $hv_flag','$main_add',' > wm-ctrl${current_ws}.his
                    echo $win_id'__'$current_ws'_' >> wm-ctrl${current_ws}.his
                else
                    echo $win_id'__'$current_ws'_' >> wm-ctrl${current_ws}.his
                fi
            fi
            if [ $ws_windows -eq 2 ]; then
                if [ $n -eq 0 ]; then
                    if [ $hv_flag = 'v' ]; then
                        temp_width=$disp_width
                        temp_height=$disp_height_main
                    else
                        temp_width=$disp_width_main
                    fi
                else
                    if [ $hv_flag = 'v' ]; then
                        temp_width=$disp_width
                        temp_height=$disp_height_sub 
                    else
                        temp_width=$disp_width_sub
                        temp_height=$((disp_height - top_panel_h - bottom_panel_h - title_h))
                    fi
                fi
            fi            
            if [ $ws_windows -eq 3 ]; then
                if [ $n -eq 0 ]; then
                    if [ $hv_flag = 'v' ]; then
                        temp_width=$disp_width
                        temp_height=$disp_height_main
                    else
                        temp_width=$disp_width_main
                    fi
                fi
            fi
            if [ $ws_windows -gt 2 -a $n -gt 0 ]; then
                if [ $hv_flag = 'v' ]; then
                    temp_width=$disp_width2
                    temp_height=$disp_height_sub 
                else
                    temp_width=$disp_width_sub
                    temp_height=$disp_height2
                fi
            fi
            if [ $ws_windows -gt 3 -a $n -gt 0 ]; then
                x_pos=(0 $pos_width $pos_width 0)
                y_pos=($((0 + top_panel_h)) $((0 + top_panel_h)) $((pos_height + top_panel_h)) \
                      $((pos_height + top_panel_h)))
                temp_width=$disp_width2
                temp_height=$disp_height2
            fi
            window_array+=( $win_id','${x_pos[$n]}','${y_pos[$n]}','$temp_width','$temp_height )
            let n++
        fi
    done

    for par in ${window_array[@]}; do
        for((i = 1; i <= 5; i++)); do
            declare par$i=$(echo ${par} | cut -f$i -d ',')
        done
        wmctrl -i -r $par1 -e 0,$par2,$par3,$par4,$par5
    done
fi

if [ $rotate_flag -gt 0 ]; then
    sed -i 's/ /_/g' ./wm-ctrl${current_ws}.his
    temp=$hv_flag','$main_add','
    sed -i '1i '$temp'' ./wm-ctrl${current_ws}.his
fi

0 件のコメント :

コメントを投稿