Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/env python
  2. # - coding: utf-8 -
  3.  
  4. import os, sys, subprocess, readline
  5. from optparse import OptionParser
  6.  
  7. print_debug = True
  8. xdotool_def_path = "/usr/bin/xdotool"
  9.  
  10. def debug(msg):
  11.     if print_debug:
  12.         print(msg)
  13.  
  14. def test_xdotool_installed():
  15.     if not os.path.exists(xdotool_def_path):
  16.         print("xdotool not installed. Quitting")
  17.         sys.exit(1)
  18.  
  19. def get_desktop_dimensions(options):
  20.     output = subprocess.check_output([xdotool_def_path, "getdisplaygeometry"])
  21.     width, height = output.split(" ")
  22.     width = int(width) - int(options.offset_x)
  23.     height = int(height[:-1]) - int(options.offset_y) # trim last char which is \n
  24.     return ( width, height )
  25.  
  26. def get_active_window():
  27.     output = subprocess.check_output([xdotool_def_path, "getwindowfocus"])
  28.     return output[:-1] # trim last char which is \n
  29.  
  30. def move_window(win_id, coords):
  31.     debug("Moving Window %s to position %s, %s" % (win_id, coords[0], coords[1]))
  32.     subprocess.call([xdotool_def_path, "windowmove", win_id, str(coords[0]), str(coords[1])])
  33.  
  34. def resize_window(win_id, dimension):
  35.     debug("Resizing Window %s to dimension %s, %s" % (win_id, dimension.x, dimension.y ))
  36.     subprocess.call([xdotool_def_path, "windowsize", win_id, str(dimension.x), str(dimension.y)])
  37.  
  38. class Dimension(object):
  39.  
  40.     def __init__(self, dim):
  41.         self.x = dim[0]
  42.         self.y = dim[1]
  43.  
  44.     def set_half_vertical_screen(self):
  45.         self.x = self.x / 2
  46.  
  47.     def set_half_horizontal_screen(self):
  48.         self.y = self.y / 2
  49.  
  50.     def set_section(self):
  51.         self.x = self.x / 3
  52.         self.y = self.y / 2
  53.  
  54. if __name__ == '__main__':
  55.     test_xdotool_installed()
  56.  
  57.     parser = OptionParser(usage="You can combine options", version="0.1")
  58.     parser.add_option("-l", "--left", action="store_true", dest="left", help="Move window to left half of screen")
  59.     parser.add_option("-r", "--right", action="store_true", dest="right", help="Move window to right half of screen")
  60.     parser.add_option("-u", "--up", action="store_true", dest="up", help="Move window to upper half of screen")
  61.     parser.add_option("-d", "--down", action="store_true", dest="down", help="Move window to bottom half of screen")
  62.     parser.add_option("-c", "--center", action="store_true", dest="center", help="Move window to center of screen")
  63.     parser.add_option("", "--offset-x", dest="offset_x", default=0, help="Move window to center of screen")
  64.     parser.add_option("", "--offset-y", dest="offset_y", default=0, help="Move window to center of screen")
  65.     parser.add_option("-s", "--size", dest="size", help="Resizes the active window to the given size")
  66.  
  67.     (options, args) = parser.parse_args()
  68.  
  69.     desktop_dimensions = Dimension(get_desktop_dimensions(options))
  70.     active_window_id = get_active_window()
  71.  
  72.     debug("Got dimension of desktop w:%s h:%s" % ( desktop_dimensions.x, desktop_dimensions.y))
  73.     debug("Got window ID for active window %s" % active_window_id)
  74.  
  75.     if options.size:
  76.         dim = Dimension(options.size.split("x"))
  77.         resize_window(active_window_id, dim)
  78.         sys.exit(0)
  79.  
  80.     coords = (0,0)
  81.  
  82.     if options.left:
  83.         coords = (0,0)
  84.         if options.up:
  85.             desktop_dimensions.set_section()
  86.  
  87.         elif options.down:
  88.             desktop_dimensions.set_section()
  89.             coords = (0, desktop_dimensions.y)
  90.            
  91.         elif options.center:
  92.             desktop_dimensions.set_section()
  93.             coords = (0, desktop_dimensions.x / 3)
  94.  
  95.         else:
  96.             desktop_dimensions.set_half_vertical_screen()
  97.  
  98.     elif options.right:
  99.         coords = (desktop_dimensions.x / 2, 0)
  100.         if options.up:
  101.             desktop_dimensions.set_section()
  102.             coords = (desktop_dimensions.x * 2, 0)
  103.  
  104.         elif options.down:
  105.             desktop_dimensions.set_section()
  106.             coords = (desktop_dimensions.x * 2, desktop_dimensions.y)
  107.            
  108.         elif options.center:
  109.             desktop_dimensions.set_section()
  110.             coords = (desktop_dimensions.x * 2, desktop_dimensions.y / 3)
  111.  
  112.         else:
  113.             desktop_dimensions.set_half_vertical_screen()
  114.  
  115.     elif options.up:
  116.         coords = (0,0)
  117.         if options.center:
  118.             desktop_dimensions.set_section()
  119.             coords = (desktop_dimensions.x, 0)
  120.         else:
  121.             desktop_dimensions.set_half_horizontal_screen()
  122.  
  123.     elif options.down:
  124.         coords = (0,desktop_dimensions.y / 2)
  125.         if options.center:
  126.             desktop_dimensions.set_section()
  127.             coords = (desktop_dimensions.x, desktop_dimensions.y)
  128.         else:
  129.             desktop_dimensions.set_half_horizontal_screen()
  130.  
  131.     move_window(active_window_id, coords)
  132.     resize_window(active_window_id, desktop_dimensions)
  133.  
  134.     sys.exit(0)