Skip to content

Instantly share code, notes, and snippets.

@jvarho
Created November 8, 2016 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvarho/ed39e42d7ce67cfe75600e3f654c400e to your computer and use it in GitHub Desktop.
Save jvarho/ed39e42d7ce67cfe75600e3f654c400e to your computer and use it in GitHub Desktop.
Window Saver
#!/usr/bin/env python2
#
# Copyright (c) 2016, Jan Varho
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from subprocess import call, check_output
import json
import sys
def get_pos(window = None):
windows = []
for w in check_output(['wmctrl', '-lG']).split('\n'):
if not w:
continue
parts = w.split()[:6]
parts = [str(parts[0])] + [int(i) for i in parts[1:]]
if window and parts[0] == window:
return parts
windows.append(parts)
return windows
def save():
return json.dumps(get_pos())
def reset(state):
windows = json.loads(state)
for w in windows:
# Attempt to restore position
call(['wmctrl', '-ir', w[0], '-e', ','.join(str(i) for i in w[1:])])
# Compute how far off it was due to window decorations etc. and adjust
w2 = get_pos(w[0])
d = [2*i-j for i, j in zip(w[1:], w2[1:])]
# HACK: Unity does not always like to position over the top bar
d2 = d[:2] + [max(d[2], 34)] + d[3:]
call(['wmctrl', '-ir', w[0], '-e', ','.join(str(i) for i in d2)])
# Set window position to corrected one
call(['wmctrl', '-ir', w[0], '-e', ','.join(str(i) for i in d)])
if __name__ == '__main__':
if '-h' in sys.argv:
print sys.argv[0] + ' Copyright (c) 2016, Jan Varho'
print
print 'Saves and restores window positions'
print ' use ' + sys.argv[0] + ' -s print out window state as JSON'
print ' use ' + sys.argv[0] + ' -s to read window state from STDIN'
print
print 'Example:'
print ' $ ' + sys.argv[0] + ' -s > tmp'
print ' $ ' + sys.argv[0] + ' -r < tmp'
if '-s' in sys.argv:
print save()
if '-r' in sys.argv:
reset(sys.stdin.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment