Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # settings
- TOP_MARGIN=0
- BOTTOM_MARGIN=35
- LEFT_MARGIN=0
- RIGHT_MARGIN=0
- TITLE_BAR_HEIGHT=17
- #either set these in a file or calculate them from the screen properties
- MASTER_WIDTH=1344
- MASTER_HEIGHT=400
- #set the number of rows we want for the grid mode
- NUMBER_OF_ROWS=2
- #looks nice :)
- USELESS_GAPS=0
- display_info="$(xwininfo -root)"
- #get the desktop parameters
- HEIGHT=`grep 'Height:' <<< "$display_info" | awk '{print $2}'`
- WIDTH=`grep 'Width:' <<< "$display_info" | awk '{print $2}'`
- #get the current desktop
- CURR_DESK=`wmctrl -d | grep '* DG:'| awk '{print $1}'`
- #get the total number of windows.
- windows_info="$(wmctrl -lx)"
- #NOTE: we are not directly using grep to get the windows from the current desktop as it may serve up some false positives
- TOTAL_WINDOWS=`wc -l <<< "$windows_info"`
- #counter
- #Assume that there are no windows in the current desktop
- WINDOWS_IN_DESK=0
- while IFS= read -r CURR_LINE
- do
- WIN_DESK=`echo $CURR_LINE | awk '{print $2}'`
- if [ $WIN_DESK -eq $CURR_DESK ] ; then
- WIN_XID[$WINDOWS_IN_DESK]=`echo $CURR_LINE | awk '{print $1}'`
- MAP_STATE=`xwininfo -id ${WIN_XID[$WINDOWS_IN_DESK]} | grep "Map State:" | awk '{print $3}'`
- if [ "$MAP_STATE" == "IsViewable" ]; then
- WINDOWS_IN_DESK=$((WINDOWS_IN_DESK+1))
- fi
- fi
- done <<< "$windows_info"
- #find the number of windows in the top row and in each subsequent row except for the bottom row.
- NORMAL_ROW_WINDOWS=$((WINDOWS_IN_DESK/NUMBER_OF_ROWS))
- #the bottom row ould have as many windows as the top row and any left over
- BOTTOM_ROW_WINDOWS=$((NORMAL_ROW_WINDOWS + WINDOWS_IN_DESK%NUMBER_OF_ROWS))
- WINDOWS_NOT_IN_BOTTOM_ROW=$((WINDOWS_IN_DESK-BOTTOM_ROW_WINDOWS))
- #set the co-ordinates for the top row
- X=$LEFT_MARGIN
- Y=$TOP_MARGIN
- #the height of each window would remain the same regardless of which row it is in
- H=$(((HEIGHT-TOP_MARGIN-BOTTOM_MARGIN)/NUMBER_OF_ROWS - TITLE_BAR_HEIGHT))
- #Find the width of each window in the top row, this would be the same for each row except for the bottom row which may contain more windows
- NORMAL_ROW_WIDTH=$((((WIDTH-LEFT_MARGIN-RIGHT_MARGIN)/NORMAL_ROW_WINDOWS)-USELESS_GAPS*NORMAL_ROW_WINDOWS))
- BOTTOM_ROW_WIDTH=$((((WIDTH-LEFT_MARGIN-RIGHT_MARGIN)/BOTTOM_ROW_WINDOWS)-USELESS_GAPS*NORMAL_ROW_WINDOWS))
- #we havent processed any windows yet
- CURRENT_ROW_WINDOWS=0
- #we will be processing the 1st row
- CURRENT_ROW=1
- for((i=0; i < $WINDOWS_IN_DESK; i++))
- do
- if [[ "$CURRENT_ROW" -lt "$NUMBER_OF_ROWS" ]]; then
- if [[ "$CURRENT_ROW_WINDOWS" -eq "NORMAL_ROW_WINDOWS " ]]; then
- CURRENT_ROW=$((CURRENT_ROW+1))
- if [[ "$CURRENT_ROW" -eq "$NUMBER_OF_ROWS" ]] ; then
- X=$LEFT_MARGIN
- Y=$((Y+H+TITLE_BAR_HEIGHT+USELESS_GAPS))
- W=$BOTTOM_ROW_WIDTH
- else
- CURRENT_ROW_WINDOWS=0
- fi
- fi
- if [[ "$CURRENT_ROW_WINDOWS" -eq "0" ]] ; then
- X=$LEFT_MARGIN
- W=$NORMAL_ROW_WIDTH
- if [[ "$CURRENT_ROW" -ne "1" ]]; then
- Y=$((Y+H+TITLE_BAR_HEIGHT+USELESS_GAPS))
- fi
- fi
- fi
- echo wmctrl -i -r ${WIN_XID[$i]} -e "0,$X,$Y,$W,$H"
- X=$((X+W+USELESS_GAPS))
- CURRENT_ROW_WINDOWS=$((CURRENT_ROW_WINDOWS+1))
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement