add initial support for automatically detecting wm and setting options

accordingly
long-opts
angelic-sedition 10 years ago
parent 8130446433
commit 2a938d9adf

73
tdrop

@ -13,13 +13,14 @@ options:
-h height specify a height for a newly created term (default: 100%)
-w width specify a width for a newly created term (default: 45%)
-x pos specify x offset for a newly created term (default: 0)
-y pos specify y offset for a newly created term (default: 1)
-y pos specify y offset for a newly created term (default: 0)
-s name name for tmux or tmuxinator session (if not given, will not use tmux)
-n num num or extra text; only needed if want multiple dropdowns of same program (default: "")
-p cmd provide a pre command to float the window if necessary
-P cmd provide a post command to float the window if necessary
-O cmd provide a one time command only for when a dropdown is created/initiated (useful for 'tdrop -c')
-d XxY give decoration/border size to accurately save position; only applicable with auto_show; on applicable with a few window managers (e.g. blackbox)
-a automatically detect window manager and if settings exist for it, automatically set -p, -P, and -d values as necessary (default: false)
-W the given program is not a terminal (or lacks an -e flag) (default: assume it IS a terminal)
--clear clear saved window id; useful accidentally make a window a dropdown (e.g. '$ tdrop --clear current')
--no-cancel don't cancel auto-showing
@ -39,7 +40,7 @@ See man page for more details.
width="100%"
height="45%"
xoff=0
yoff=1
yoff=0
float_pre=""
float_post=""
oneshot_post=""
@ -49,13 +50,15 @@ normal_window=false
clearwid=false
cancel_auto_show=true
term=${*:0-1}
while getopts :h:w:x:y:s:n:p:P:O:d:-:W opt
auto_detect_wm=false
while getopts :h:w:x:y:s:n:p:P:O:d:-:Wa opt
do
case $opt in
h) height=$OPTARG;;
w) width=$OPTARG;;
x) xoff=$OPTARG;;
y) yoff=$OPTARG;;
y) yoff=$OPTARG
user_set_yoff=true;;
s) session_name=$OPTARG;;
n) num=$OPTARG;;
p) float_pre=$OPTARG;;
@ -63,6 +66,7 @@ do
O) oneshot_post=$OPTARG;;
d) dec_fix=$OPTARG;;
W) normal_window=true;;
a) auto_detect_wm=true;;
-)
OPTION=$(echo "$OPTARG" | awk -F '=' '{print $1}')
OPTARG=$(echo "$OPTARG" | awk -F '=' '{print $2}')
@ -70,7 +74,8 @@ do
height) height=$OPTARG;;
width) width=$OPTARG;;
x-offset) xoff=$OPTARG;;
y-offset) yoff=$OPTARG;;
y-offset) yoff=$OPTARG
user_set_yoff=true;;
session) session_name=$OPTARG;;
number) num=$OPTARG;;
pre-command) float_pre=$OPTARG;;
@ -78,6 +83,7 @@ do
oneshot-post) oneshot_post=$OPTARG;;
decoration-fix) dec_fix=$OPTARG;;
normal-window) normal_window=true;;
auto-detect-wm) auto_detect_wm=true;;
clear) clearwid=true;;
no-cancel) cancel_auto_show=false;;
help) print_help;;
@ -86,13 +92,47 @@ do
esac
done
wm_autodetect_settings() {
wm=$(xprop -notype -id "$(xprop -root -notype | awk '$1=="_NET_SUPPORTING_WM_CHECK:"{print $5}')" -f _NET_WM_NAME 8u | awk -F "\"" '/WM_NAME/ {print $2}')
# floating or position saving setup
if [[ $wm == bspwm ]]; then
# change yoff only if not set by user
# deals with potential bug where window will be moved to middle
if ! $user_set_yoff; then
yoff=1
fi
float_pre() {
bspc rule -a "$1" -o floating=on
}
# floating window managers that may both move and resize a window after unmapping then mapping it
elif [[ $wm == Openbox ]]; then
float_post() {
xdotool windowmove "$1" "$xoff" "$yoff" windowsize "$1" "$width" "$height"
}
# floating window managers that may both move a window after unmapping then mapping it
elif [[ $wm == pekwm ]] || [[ $wm == fluxbox ]]; then
float_post() {
xdotool windowmove "$1" "$xoff" "$yoff"
}
fi
# for auto_show proper positioning
if [[ $wm == Blackbox ]]; then
dec_fix_auto="1x22"
fi
}
if $auto_detect_wm; then
wm_autodetect_settings
fi
float_pre_command() {
# a user set option has higher priority
if [[ -n $float_pre ]]; then
if [[ $float_pre == bspwm ]]; then
bspc rule -a "$1" -o floating=on
else
eval "$float_pre"
fi
eval "$float_pre"
# use automatically set function if exists
elif [[ -n $(type float_pre) ]]; then
float_pre "$1"
fi
}
@ -100,11 +140,9 @@ float_post_command() {
if [[ -n $oneshot_post ]] && [[ $1 == oneshot ]]; then
eval "$oneshot_post"
elif [[ -n $float_post ]]; then
if [[ $float_post == openbox ]]; then
xdotool getactivewindow windowmove "$wid" "$xoff" "$yoff" windowsize "$wid" "$width" "$height"
else
eval "$float_post"
fi
eval "$float_post"
elif [[ -n $(type float_post) ]]; then
float_post "$1"
fi
}
@ -212,6 +250,11 @@ set_geometry() {
y_fix=$(echo "$dec_fix" | awk -F "x" '{print $2}')
X=$((X-x_fix))
Y=$((Y-y_fix))
elif [[ -n $dec_fix_auto ]]; then
x_fix=$(echo "$dec_fix_auto" | awk -F "x" '{print $1}')
y_fix=$(echo "$dec_fix_auto" | awk -F "x" '{print $2}')
X=$((X-x_fix))
Y=$((Y-y_fix))
fi
xdotool windowmove "$1" "$X" "$Y"
}

@ -28,7 +28,7 @@ Specify a width for a created window as a number or percentage. (default: 100%)
Specify the x position for a created window as a number or percentage. (default: 0)
.TP
\fB\-y\fR, \fB \-\-y-offset\fR
Specify the y position for a created window as a number or percentage. (default: 1, see Bugs)
Specify the y position for a created window as a number or percentage. (default: 0)
.TP
\fB\-s\fR, \fB \-\-session\fR
Specify a tmuxinator or tmux session name to start. An existing tmux session has highest precedence and will be connected to with '-d', detaching other attached clients. If a there is no tmuxinator session of the given name, a normal tmux session with the name will be created. If this option is not given, tmux will not be used. (default: none)
@ -51,6 +51,9 @@ Specify a window decoration/border size in the form <x decoration size>x<y decor
\fB\-W\fR, \fB \-\-normal-window\fR
Specifies that the program is not a terminal or does not have the '-e' flag; takes no argument. If using 'tdrop current', this option will have no effect either way. (default: false)
.TP
\fB\-a\fR, \fB \-\-auto-detect-wm\fR
If there are available settings for the detected window manager for the -p, -P, and/or -d options, automatically set them; takes no argument. User set settings will still override these. (default: false)
.TP
\fB \-\-clear\fR
Used to clear a saved window id for the given program or 'current' instead of creating a dropdown; takes no argument.
.TP
@ -63,7 +66,7 @@ Print help that is less awesome than this manpage; takes no argument.
.SS Making Dropdowns
Use a key binding program such as sxhkd to bind keys to these commands.
The simplest example. Make a dropdown for an xterm.
The simplest example to make a dropdown for an xterm:
.br
$ tdrop xterm
@ -71,17 +74,17 @@ When using a tiling window manager like bspwm, dropdowns like guake will by defa
.br
$ tdrop -p "bspc rule -a xterm -o floating=on" xterm
Tdrop also has a short command for bspwm. Note that '-p bspwm' does not require specifying the program name and will also work for creating oneshot floating rules with 'tdrop current' and 'tdrop auto_show'.
Tdrop also provides tested settings for certain window managers. One can use the '-a' flag if settings exist for the current window manager. For example, if bspwm is the window manager, the following command is the same as the above command and will work for whatever terminal/program is specified and will also work with 'tdrop auto_show'. For a list of window managers with tested settings see the readme or the script itself.
.br
$ tdrop -p bspwm xterm
$ tdrop -a xterm
Tdrop supports controlling the initial size and placement of a terminal. The border of a window may need to be taken into an account. For example, I use a border size of 2, so I use 4 less than my screen size. I also use a y-offset of 14 so that the dropdown doesn't hide my panel:
.br
$ tdrop -p bspwm -w 1362 -y 14 termite
$ tdrop -a -w 1362 -y 14 xterm
Tdrop can also create a tmux or tmuxinator session if it does not exist:
.br
$ tdrop -p bspwm -w 1362 -y 14 -s dropdown termite
$ tdrop -a -w 1362 -y 14 -s dropdown xterm
Tdrop allows for having multiple dropdowns of the same type:
.br
@ -99,7 +102,7 @@ $ tdrop zathura
Tdrop can also turn the current window into a dropdown. '-O' is useful in this case to execute a command only once when turning the active window into a dropdown.
.br
$ tdrop -p bspwm -O "bspc window -t floating=on && xdotool getactivewindow windowmove 0 14 windowsize 1362 45%" current
$ tdrop -a -O "bspc window -t floating=on && xdotool getactivewindow windowmove 0 14 windowsize 1362 45%" current
Once a window is turned into a dropdown, the key bound to 'tdrop ... current' will continue to toggle that window until it is closed. Then the key can be used to create a new dropdown. '-n' can also be used to have multiple 'current' keys. If an active window is accidentally turned into a dropdown, it can be cleared:
.br
@ -117,13 +120,13 @@ To use it in an alias when writing a commit message in an graphical $EDITOR star
.br
alias gc='hide_on_open git commit'
This will hide the terminal window when opening the editor and then show it once the editor is closed. If primarily used from floating dropdowns instead, something like 'tdrop -p bspwm auto_show' can be used instead to float the window when re-showing.
This will hide the terminal window when opening the editor and then show it once the editor is closed. It should also maintain the window's position and size when showing it. If the window moves down and to the right every time it is auto-hidden and then shown again, the user may need to specify a '-d' value. Alternatively, if one already exists for the user's window manager, '-a' can be used to automatically set it. The '-p' and '-P' options are also used with auto_show and can be set automatically with '-a' if tested settings exist for the current window manager
This functionality might lead to some unwanted "re-shows" of dropdown. Consider a situation in which one opens an image viewer from a dropdown and leaves it open for a while, resuming normal use of the dropdown. When the image viewer is closed, the dropdown appears, unwanted. Tdrop is smart about this and won't "re-show" a dropdown if it has been manually toggled since an auto-hide. If you don't want this check to happen, use '--no-cancel' in your dropdown key binding.
Auto-hiding functionality is particularly nice to use with a file opener like rifle:
.br
mime ^image, has sxiv, X, flag f = tdrop auto_hide ; sxiv -a -- "$@" && tdrop -p bspwm auto_show
mime ^image, has sxiv, X, flag f = tdrop auto_hide ; sxiv -a -- "$@" && tdrop -a auto_show
.SH SEE ALSO
xdotool(1), sxhkd(1), xprop(1), xwininfo(1), tmux(1)
.SH BUGS

Loading…
Cancel
Save