mabox-tools/bin/mb-aerosnap
2020-02-18 17:24:18 +01:00

131 lines
3.8 KiB
Python
Executable File

#!/usr/bin/env python2.7
# bl-aerosnap:
# A script for adding aero style window snapping to Openbox.
# Repackaged for BunsenLabs by John Crawley.
# Originally written for CrunchBang Linux <http://crunchbang.org/>
# by Philip Newborough <corenominal@corenominal.org>
# ----------------------------------------------------------------------
from subprocess import Popen, PIPE, STDOUT
import sys, time, os, re
history = '/tmp/bl-aerosnap-'+str(os.getuid())
windows = {}
check_intervall = 0.2
p = Popen(['xdotool','getdisplaygeometry'], stdout=PIPE, stderr=STDOUT)
Dimensions = p.communicate()
Dimensions = Dimensions[0].replace('\n', '')
Dimensions = Dimensions.split(' ')
width = int(Dimensions[0])
height = int(Dimensions[1])
hw = width / 2
rt = width - 1
bt = height - 1
aeroLcommand="wmctrl -r :ACTIVE: -b add,maximized_vert && wmctrl -r :ACTIVE: -b remove,maximized_horz && wmctrl -r :ACTIVE: -e 0,0,0,"+str(hw)+",-1"
aeroRcommand="wmctrl -r :ACTIVE: -b add,maximized_vert && wmctrl -r :ACTIVE: -b remove,maximized_horz && wmctrl -r :ACTIVE: -e 0,"+str(hw)+",0,"+str(hw)+",-1"
if os.path.exists(history) == False:
f = open(history,'w')
f.close()
def print_usage():
print "bl-aerosnap: usage:"
print " --help show this message and exit"
print " --left attempt to snap active window to left of screen"
print " --right attempt to snap active window to right of screen"
print ""
exit()
def is_root_window():
p = Popen(['xdotool', 'getactivewindow'], stdout=PIPE, stderr=STDOUT)
ID = p.communicate()
if len(ID[0]) > 50:
return True
def window_id():
p = Popen(['xdotool', 'getactivewindow'], stdout=PIPE)
ID = p.communicate()
ID = int(ID[0])
p = Popen(['xdotool', 'getwindowpid', str(ID)], stdout=PIPE)
PID = p.communicate()
PID = int(PID[0])
return str(ID)+'-'+str(PID)
def window_lookup():
ID = window_id()
windows = history_load()
if windows.has_key(ID):
return True
def window_geometry(ID):
ID = ID.split('-')
p = Popen(['xdotool', 'getwindowgeometry', ID[0]], stdout=PIPE)
p = p.communicate()
Pos = re.search(r'\d+,\d+', p[0])
Size = re.search(r'\d+x\d+', p[0])
if Pos and Size:
Pos = Pos.group().split(',')
Size = Size.group().split('x')
return Pos[0]+'|'+Pos[1]+'|'+Size[0]+'|'+Size[1]
def window_store():
ID = window_id()
windows[ID] = window_geometry(ID)
s = ID +'|'+window_geometry(ID)+'\n'
f = open(history,'a')
f.write(s)
f.close()
def window_restore(width):
ID = window_id()
G = windows[ID].split('|')
command = 'wmctrl -r :ACTIVE: -b remove,maximized_vert && '
#FIXME: adjust horizontal placement, not sure where this discrepancy comes from?
AdjustT = int(G[1]) - 40
command += 'wmctrl -r :ACTIVE: -e 0,'+G[0]+','+str(AdjustT)+','+G[2]+','+G[3]
del windows[ID]
if len(windows) == 0:
o = ''
else:
for key in windows:
h = windows[key].split('|')
o = key+'|'+h[0]+'|'+h[1]+'|'+h[2]+'|'+h[3]+'\n'
f = open(history,'w')
f.write(o)
f.close()
os.system(command)
def history_load():
f = open(history,'r')
i = 0
for line in f:
h = line.split('|')
h[4] = h[4].replace('\n', '')
windows[h[0]] = h[1]+'|'+h[2]+'|'+h[3]+'|'+h[4]
f.close()
return windows
if len(sys.argv) < 2 or sys.argv[1] == "--help":
print_usage()
elif sys.argv[1] == "--left":
if is_root_window != True:
if window_lookup():
window_restore(width)
else:
window_store()
os.system(aeroLcommand)
elif sys.argv[1] == "--right":
if is_root_window != True:
if window_lookup():
window_restore(width)
else:
window_store()
os.system(aeroRcommand)
else:
print_usage()