-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnap
More file actions
executable file
·417 lines (362 loc) · 13.4 KB
/
snap
File metadata and controls
executable file
·417 lines (362 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/bash
#
# Window Snapping Utility for Openbox
#
# Makes some assumptions about multi-monitor setups:
# - screens arranged horizontally
# - screens have same resolutions
# - panels are present on every screen, in the same location, and of the same size
# For unassigned variables in fetch_current_window_geometry()
# shellcheck disable=SC2154
function usage()
{
echo "Usage: $0 [-s <l|r|u|d>] [-m <l|r>]"
echo " -s snap direction"
echo " supports left, right, up, and down"
echo " -m move direction"
echo " supports left and right"
echo
}
############################################################
# Echos to STDOUT with prepended calling function name.
#
# Args:
# full message to be logged to STDOUT
############################################################
function log()
{
echo "${FUNCNAME[1]}: $*"
}
############################################################
# Initializes global variables
############################################################
function init()
{
log "Initializing global variables"
WINDOW=0 # Active window to be snapped
NUM_MONITORS=0 # Number of monitors comprising desktop
# Desktop geometry
SCREEN_X_START=0
SCREEN_Y_START=0
SCREEN_WIDTH=0
SCREEN_HEIGHT=0
# Active windows current geometry
CURR_X=0
CURR_Y=0
CURR_W=0
CURR_H=0
# Active window borders
BORDER_LEFT=0
BORDER_RIGHT=0
BORDER_TOP=0
BORDER_BOTTOM=0
ORIG_X=0
ORIG_Y=0
ORIG_W=0
ORIG_H=0
LAST_X=0
LAST_Y=0
LAST_W=0
LAST_H=0
LAST_X_QUAD=0
LAST_Y_QUAD=0
}
############################################################
# Determines the ID of the Active Window
############################################################
function fetch_active_window_id()
{
WINDOW=$(xdotool getactivewindow)
log "(xdotool) $WINDOW"
}
############################################################
# Determines the current geometry of the Active Window
############################################################
function fetch_current_window_geometry()
{
# The most accurate representation of X and Y coordinates I could find is using
# xwininfo and calculating based off the absolute and relative positions.
eval "$(xwininfo -id "$WINDOW" |
sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
-e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
-e "s/^ \+Relative upper-left X: \+\([0-9]\+\).*/a=\1/p" \
-e "s/^ \+Relative upper-left Y: \+\([0-9]\+\).*/b=\1/p" \
-e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
-e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" )"
log "(xwininfo) AbsX=$x AbsY=$y RelX=$a RelY=$b W=$w H=$h"
# The actual X and Y coordinates of the window need to be calculated by subtracting
# the relative X and Y coordinates from the Absolute X and Y coordinates.
CURR_X=$((x-a))
CURR_Y=$((y-b))
CURR_W=$w
CURR_H=$h
log "(calculated) X=$CURR_X Y=$CURR_Y W=$CURR_W H=$CURR_H"
}
############################################################
# Determines the Active Windows borders.
############################################################
function fetch_window_borders()
{
# _NET_FRAME_EXTENTS contains the left, right, top, and bottom borders, in that order
window_frame=$(xprop -id "$WINDOW" _NET_FRAME_EXTENTS | sed 's/,//g' | cut -d' ' -f3-)
BORDER_LEFT=$(echo "$window_frame" | cut -d' ' -f1)
BORDER_RIGHT=$(echo "$window_frame" | cut -d' ' -f2)
BORDER_TOP=$(echo "$window_frame" | cut -d' ' -f3)
BORDER_BOTTOM=$(echo "$window_frame" | cut -d' ' -f4)
log "(xprop _NET_FRAME_EXTENTS) Left=$BORDER_LEFT Right=$BORDER_RIGHT Top=$BORDER_TOP Bottom=$BORDER_BOTTOM"
}
############################################################
# Determines if window has already been snapped and sets original geometries
############################################################
function fetch_previous_snap_info()
{
if xprop -id "$WINDOW" | grep "_SNAP_STATE" >/dev/null; then
# _SNAP_STATE stores original x, y, w, h, and the last x, y, w, and h.
local window_state=$(xprop -id "$WINDOW" _SNAP_STATE | sed 's/,//g' | cut -d' ' -f3-)
ORIG_X=$(echo "$window_state" | cut -d' ' -f1)
ORIG_Y=$(echo "$window_state" | cut -d' ' -f2)
ORIG_W=$(echo "$window_state" | cut -d' ' -f3)
ORIG_H=$(echo "$window_state" | cut -d' ' -f4)
LAST_X=$(echo "$window_state" | cut -d' ' -f5)
LAST_Y=$(echo "$window_state" | cut -d' ' -f6)
LAST_W=$(echo "$window_state" | cut -d' ' -f7)
LAST_H=$(echo "$window_state" | cut -d' ' -f8)
WAS_SNAPPED=1
log "(xprop _SNAP_STATE) Window has stored snap info:"
log " origX=$ORIG_X origY=$ORIG_Y origW=$ORIG_W origH=$ORIG_H"
log " lastX=$LAST_X lastY=$LAST_Y lastW=$LAST_W lastH=$LAST_H"
local saved_quadrant_info=$(xprop -id "$WINDOW" _SNAP_QUADRANT | sed 's/,//g' | cut -d' ' -f3-)
LAST_X_QUAD=$(echo "$saved_quadrant_info" | cut -d' ' -f1)
LAST_Y_QUAD=$(echo "$saved_quadrant_info" | cut -d' ' -f2)
log "(xprop _SNAP_QUADRANT) Window has stored snap quadrant of: [$LAST_X_QUAD, $LAST_Y_QUAD]"
else
log "Window does not have stored snap info."
ORIG_X=$CURR_X
ORIG_Y=$CURR_Y
ORIG_W=$CURR_W
ORIG_H=$CURR_H
fi
}
############################################################
# Determines if window has moved since previous snap.
# If it has, reset the stored geometries.
############################################################
function has_window_moved_since_snap()
{
if [ "$LAST_X" -ne "$CURR_X" ] || [ "$LAST_Y" -ne "$CURR_Y" ] ||
[ "$LAST_W" -ne "$CURR_W" ] || [ "$LAST_H" -ne "$CURR_H" ]; then
log "Window has moved since previous snap."
remove_stored_geometry
ORIG_X=$CURR_X
ORIG_Y=$CURR_Y
ORIG_W=$CURR_W
ORIG_H=$CURR_H
LAST_X_QUAD=0
LAST_Y_QUAD=0
fi
}
############################################################
# Determines the number of monitors connected to the PC.
############################################################
function get_monitor_count()
{
NUM_MONITORS=$(xrandr -q | grep -c " connected")
log "(xrandr) $NUM_MONITORS"
}
############################################################
# Determines the geometry of a single monitor (screen).
#
# SCREEN_X_START starting X coordinate of the primary monitor, not including panels and docks
# SCREEN_Y_START starting Y coordinate of the primary monitor, not including panels and docks
# SCREEN_WIDTH usable width of each screen, not including panels and docks
# SCREEN_HEIGHT usable height of each screen, not including panels and docks
############################################################
function get_desktop_geometry()
{
# _NET_WORKAREA contains the starting x coordinate, starting y coordinate, width, and height
# of each desktop workspace.
# Docks and Panels are taken into account with all 4 values, not including their sizes as
# usable realestate.
# All desktop workspaces should have the same geometries, as far as I know, so we only need
# the first set of measurements.
local desktop_geom_qry_results=$(xprop -root _NET_WORKAREA | sed 's/,//g' | cut -d' ' -f3-)
SCREEN_X_START=$(echo "$desktop_geom_qry_results" | cut -d' ' -f1)
SCREEN_Y_START=$(echo "$desktop_geom_qry_results" | cut -d' ' -f2)
local desktop_width=$(echo "$desktop_geom_qry_results" | cut -d' ' -f3)
SCREEN_WIDTH=$((desktop_width/NUM_MONITORS))
SCREEN_HEIGHT=$(echo "$desktop_geom_qry_results" | cut -d' ' -f4)
log "(xprop _NET_WORKAREA) X=$SCREEN_X_START Y=$SCREEN_Y_START W=$SCREEN_WIDTH H=$SCREEN_HEIGHT"
}
############################################################
# Determines the monitor being used by the window.
############################################################
function get_active_window_monitor()
{
MONITOR=1
if [ "$CURR_X" -ge "$SCREEN_WIDTH" ]; then
MONITOR=2
fi
}
############################################################
# Determines new X and Y Quadrants based on direction and current Quadrant.
############################################################
function get_new_quadrant()
{
if [ "$DIRECTION" == 'l' ]; then
NEW_X_QUAD=$((LAST_X_QUAD-1))
elif [ "$DIRECTION" == 'r' ]; then
NEW_X_QUAD=$((LAST_X_QUAD+1))
elif [ "$DIRECTION" == 'u' ]; then
NEW_Y_QUAD=$((LAST_Y_QUAD+1))
elif [ "$DIRECTION" == 'd' ]; then
NEW_Y_QUAD=$((LAST_Y_QUAD-1))
fi
if [ "$NEW_X_QUAD" -gt 1 ] || [ "$NEW_X_QUAD" -lt -1 ]; then
NEW_X_QUAD=$LAST_X_QUAD
log "New X Quadrant ($NEW_X_QUAD) is out of range. Continuing to use $LAST_X_QUAD."
fi
if [ "$NEW_Y_QUAD" -gt 1 ] || [ "$NEW_Y_QUAD" -lt -1 ]; then
if [ "$NEW_X_QUAD" -eq 0 ] && [ "$NEW_Y_QUAD" -eq 2 ]; then
log "New Y Quadrant = 2. Snapping full screen."
else
NEW_Y_QUAD=$LAST_Y_QUAD
log "New Y Quadrant ($NEW_Y_QUAD) is out of range. Continuing to use $LAST_Y_QUAD."
fi
fi
}
############################################################
# Clears any stored _SNAP_STATE Geometry of the Active Window.
############################################################
function remove_stored_geometry()
{
log "Removing saved xprop _SNAP_STATE"
xprop -id "$WINDOW" -remove _SNAP_STATE
log "Removing saved xprop _SNAP_QUADRANT"
xprop -id "$WINDOW" -remove _SNAP_QUADRANT
}
############################################################
#
# Main
#
############################################################
MOVE=0
while getopts s:m: option; do
case "${option}" in
s)
MOVE=0
DIRECTION="$OPTARG"
;;
m)
MOVE=1
DIRECTION="$OPTARG"
;;
*)
usage
exit
;;
esac
done
if [ "$MOVE" == "0" ]; then
if [ "$DIRECTION" != "l" ] && [ "$DIRECTION" != "r" ] &&
[ "$DIRECTION" != "u" ] && [ "$DIRECTION" != "d" ]; then
usage
exit
fi
fi
if [ "$MOVE" == "1" ]; then
if [ "$DIRECTION" != "l" ] && [ "$DIRECTION" != "r" ]; then
usage
exit
fi
fi
init
# Determine active windows current attributes
fetch_active_window_id
fetch_current_window_geometry
fetch_window_borders
# Determine if window has previous snap state
WAS_SNAPPED=0
fetch_previous_snap_info
if [ "$WAS_SNAPPED" == 1 ]; then
has_window_moved_since_snap
fi
# Determine display information
get_monitor_count
get_desktop_geometry
if [ "$MOVE" == "1" ]; then
get_active_window_monitor
new_x=$CURR_X
new_y=$CURR_Y
new_w=$CURR_W
new_h=$CURR_H
NEW_X_QUAD=$LAST_X_QUAD
NEW_Y_QUAD=$LAST_Y_QUAD
if [ "$DIRECTION" == "l" ] && [ "$MONITOR" == 2 ]; then
new_x=$((CURR_X-SCREEN_WIDTH))
ORIG_X=$((ORIG_X-SCREEN_WIDTH))
elif [ "$DIRECTION" == "r" ] && [ "$MONITOR" == 1 ]; then
new_x=$((CURR_X+SCREEN_WIDTH))
ORIG_X=$((ORIG_X+SCREEN_WIDTH))
else
log "Invalid move operation"
fi
else
NEW_X_QUAD=$LAST_X_QUAD
NEW_Y_QUAD=$LAST_Y_QUAD
get_new_quadrant
# If the new quadrant is [0,0], the window is back in it's original position.
# Set the new coordinates to the original coordinates and remove any stored
# snap info from xprop.
if [ "$NEW_X_QUAD" -eq 0 ] && [ "$NEW_Y_QUAD" -eq 0 ]; then
log "New Quadrant = [0,0]. Returning to original position."
new_x=$ORIG_X
new_y=$ORIG_Y
new_w=$ORIG_W
new_h=$ORIG_H
remove_stored_geometry
else
case $NEW_X_QUAD in
-1)
new_x=$SCREEN_X_START
new_w=$((SCREEN_WIDTH/2-BORDER_LEFT-BORDER_RIGHT))
;;
0)
new_x=$SCREEN_X_START
new_w=$((SCREEN_WIDTH-BORDER_LEFT-BORDER_RIGHT))
;;
1)
new_x=$((SCREEN_WIDTH/2))
new_w=$((SCREEN_WIDTH/2-BORDER_LEFT-BORDER_RIGHT))
;;
esac
case $NEW_Y_QUAD in
-1)
new_y=$((SCREEN_HEIGHT/2+BORDER_TOP+BORDER_BOTTOM+1))
new_h=$((SCREEN_HEIGHT/2-BORDER_TOP-BORDER_BOTTOM))
;;
0)
new_y=$SCREEN_Y_START
new_h=$((SCREEN_HEIGHT-BORDER_TOP-BORDER_BOTTOM-1))
;;
1)
new_y=$SCREEN_Y_START
new_h=$((SCREEN_HEIGHT/2-BORDER_TOP-BORDER_BOTTOM))
;;
2)
# Special case for maximized window
new_y=$SCREEN_Y_START
new_h=$((SCREEN_HEIGHT-BORDER_TOP-BORDER_BOTTOM))
;;
esac
get_active_window_monitor
if [ "$MONITOR" == 2 ]; then
new_x=$((new_x+SCREEN_WIDTH))
fi
fi
fi
xprop -id "$WINDOW" -f _SNAP_QUADRANT 32i -set _SNAP_QUADRANT "$NEW_X_QUAD, $NEW_Y_QUAD"
xprop -id "$WINDOW" -f _SNAP_STATE 32i -set _SNAP_STATE "$ORIG_X, $ORIG_Y, $ORIG_W, $ORIG_H, $new_x, $new_y, $new_w, $new_h"
log "Snapping to: Quadrant=[$NEW_X_QUAD,$NEW_Y_QUAD] X=$new_x Y=$new_y W=$new_w H=$new_h"
xdotool windowsize "$WINDOW" "$new_w" "$new_h"
xdotool windowmove "$WINDOW" "$new_x" "$new_y"
fetch_current_window_geometry