Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import subprocess, re, os, sys, Xlib.display, math
- #TODO: Automatically get the height of the titlebar, maybe using themerc?
- # There's going to be gaps if windows are undecorated.
- # Take over the world!
- if len(sys.argv) > 1: options = sys.argv[1]
- else: options = "-h"
- screen_x = Xlib.display.Display().screen()["width_in_pixels"]
- screen_y = Xlib.display.Display().screen()["height_in_pixels"]
- #screen_x, screen_y = 1440, 900
- if not screen_x or not screen_y:
- print "Cannot determine screen resolution, exiting."
- sys.exit(1)
- ob_config = os.path.expanduser("~") + "/.config/openbox/rc.xml"
- if os.path.isfile(ob_config):
- ob_config = open(ob_config, "r").read()
- margins = {
- "top" : re.search(r"<top>\s*(\d+)\s*</top>", ob_config, re.M),
- "bottom" : re.search(r"<bottom>\s*(\d+)\s*</bottom>", ob_config, re.M),
- "left" : re.search(r"<left>\s*(\d+)\s*</left>", ob_config, re.M),
- "right" : re.search(r"<right>\s*(\d+)\s*</right>", ob_config, re.M)
- }
- for position, match in margins.iteritems():
- if match: margins[position] = int(match.group(1))
- else:
- print "Warning: Regex fail or margins not defined, defaulting to 0"
- margins[position] = 0
- else:
- print "Warning: Openbox config not found, margins defaulting to 0"
- margins["top"] = margins["bottom"] = margins["left"] = margins["right"] = 0
- titlebar = 22 #Approximate pixel ammount taken up by the titlebar.
- #Get current desktop, using the output of wmctrl. If not found, default to 0
- desktops = subprocess.Popen(["wmctrl", "-d"], stdout=subprocess.PIPE).communicate()[0]
- current_desk = 0
- match = re.search(r"(\d+)\s*\*", desktops)
- if match: current_desk = int(match.group(1))
- all_windows = subprocess.Popen(["wmctrl", "-l"], stdout=subprocess.PIPE).communicate()[0].split("\n") #One window per item in list.
- current_wins = []
- for window in all_windows: #Gets windows on current desktop. Appends them to current_wins
- match = re.search(r"(0x\w{8})\s*?" + str(current_desk), window) #Assuming that all window ID's are in the form "0x########"
- if match: current_wins.append(match.group(1))
- num_of_wins = len(current_wins)
- if options == "-h":
- #Tile horizontally. Windows appear next to each other.
- win_x = (screen_x - margins["right"] - margins["left"]) / num_of_wins #Width per app
- win_y = screen_y - margins["top"] - margins["bottom"] - titlebar #Height per app
- positions = [win_x*x+margins["right"] for x in range(0, num_of_wins)] #List of x-value positions
- for i, win in enumerate(current_wins): #Resize each window, yay!
- subprocess.Popen(["wmctrl", "-i", "-r", win, "-b", "remove,maximized_vert,maximized_horz"])
- subprocess.Popen(["wmctrl", "-i", "-r", win, "-e", "0,%s,%s,%s,%s" % (positions[i],margins["top"],win_x,win_y)])
- elif options == "-v":
- #Tile windows vertically. Windows stack on top of each other.
- win_x = screen_x - margins["right"] - margins["left"]
- win_y = (screen_y - margins["top"] - margins["bottom"] - (titlebar*num_of_wins)) / num_of_wins
- positions = [((win_y*y)+(titlebar*y)+margins["top"]) for y in range(0, num_of_wins)]
- print positions
- for i, win in enumerate(current_wins):
- subprocess.Popen(["wmctrl", "-i", "-r", win, "-b", "remove,maximized_vert,maximized_horz"])
- subprocess.Popen(["wmctrl", "-i", "-r", win, "-e", "0,%s,%s,%s,%s" % (margins["left"],positions[i],win_x,win_y)])
- elif options == "-g":
- #Tiles windows in a grid.
- try: rows = int(sys.argv[2])
- except (ValueError, NameError, IndexError): rows = 2
- cols = int(math.ceil(float(num_of_wins) / rows))
- win_x = (screen_x - margins["right"] - margins["left"]) / cols
- win_y = (screen_y - margins["top"] - margins["bottom"] - (titlebar*rows)) / rows
- pos_x = [win_x*x for x in range(0, cols)]
- pos_y = [win_y*y+(titlebar*y) for y in range(0, rows)]
- grid = []
- for row in pos_x:
- for col in pos_y:
- grid.append((row,col))
- for i, win in enumerate(current_wins):
- subprocess.Popen(["wmctrl", "-i", "-r", win, "-b", "remove,maximized_vert,maximized_horz"])
- subprocess.Popen(["wmctrl", "-i", "-r", win, "-e", "0,%s,%s,%s,%s" % (grid[i][0], grid[i][1], win_x, win_y)])
- else:
- print """
- Tiles windows in Openbox, either vertically or horizontally.
- Usage: tile.py [options]
- options:
- -h Tiles horizontally
- -v Tiles vertically
- -g [rows] Tiles in a grid. If unspecified, rows = 2.
- --help Print this help thingy.
- Defaults to horizontal if no options specified
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement