Created
July 22, 2014 20:48
-
-
Save kafene/d1912221c656029ca4bc to your computer and use it in GitHub Desktop.
A script for adding aero style window snapping to Openbox.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# cb-aerosnap: | |
# A script for adding aero style window snapping to Openbox. | |
# Written for CrunchBang Linux <http://crunchbang.org/> | |
# by Philip Newborough <[email protected]> | |
# ---------------------------------------------------------------------- | |
# License: | |
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
# Version 2, December 2004 | |
# | |
# Copyright (C) 2004 Sam Hocevar <[email protected]> | |
# | |
# Everyone is permitted to copy and distribute verbatim or modified | |
# copies of this license document, and changing it is allowed as long | |
# as the name is changed. | |
# | |
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
# | |
# 0. You just DO WHAT THE FUCK YOU WANT TO. | |
# ---------------------------------------------------------------------- | |
from subprocess import Popen, PIPE, STDOUT | |
import sys, time, os, re | |
history = '/tmp/cb-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 "cb-aerosnap: usage:" | |
print " --help show this message and exit" | |
print " --aero-left attempt to snap active window to left of screen" | |
print " --aero-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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment