Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import subprocess
- import shutil
- # Check dependencies
- for tool in ["xprop", "xdpyinfo", "xdotool", "wmctrl"]:
- if not shutil.which(tool):
- print(f"Error: {tool} is not installed.")
- sys.exit(1)
- # Usage
- if len(sys.argv) < 5:
- print("Usage: tiling.py <width%> <height%> <x_offset%> <y_offset%>")
- sys.exit(1)
- def validate(val, name):
- if not 0 <= val <= 100:
- print(f"Error: {name} must be between 0 and 100.")
- sys.exit(1)
- # Parse and validate args
- w_pct, h_pct, x_pct, y_pct = map(int, sys.argv[1:5])
- for val, name in zip([w_pct, h_pct, x_pct, y_pct], ["Width", "Height", "X Offset", "Y Offset"]):
- validate(val, name)
- BUFFER_W = 10
- BUFFER_H = 33
- # Get active window ID
- try:
- win_id = subprocess.check_output(["xdotool", "getactivewindow"]).decode().strip()
- except subprocess.CalledProcessError:
- print("Error: Cannot get active window.")
- sys.exit(1)
- # Get window class
- def get_class(wid):
- try:
- output = subprocess.check_output(["xprop", "-id", wid, "WM_CLASS"]).decode()
- return output.split('=')[1].split(',')[1].strip().strip('"')
- except Exception as e:
- print(f"Error retrieving class: {e}")
- return ""
- app_class = get_class(win_id)
- print(f"Window class: {app_class}") # Debugging output
- # Unmaximize window
- subprocess.call(["wmctrl", "-ir", win_id, "-b", "remove,maximized_vert,maximized_horz"])
- # Get screen workarea
- def get_workarea():
- try:
- out = subprocess.check_output(["xprop", "-root", "_NET_WORKAREA"]).decode()
- return list(map(int, out.split('=')[1].split(',')[:4]))
- except subprocess.CalledProcessError:
- # fallback to screen size if xprop fails
- for line in subprocess.check_output(["xdpyinfo"]).decode().splitlines():
- if "dimensions:" in line:
- w, h = map(int, line.split()[1].split('x'))
- return [0, 0, w, h]
- wx, wy, ww, wh = get_workarea()
- # Calculate size and position
- width = ww * w_pct // 100 - BUFFER_W
- if x_pct == 0 and app_class in ["Brave-browser", "Google-chrome"]:
- width = ww * (w_pct + 1) // 100 - BUFFER_W # Add extra width for Brave
- height = wh * h_pct // 100
- if app_class not in ["Brave-browser", "Google-chrome"]:
- height -= BUFFER_H
- height = max(1, height)
- x = wx + ww * x_pct // 100
- y = wy + wh * y_pct // 100
- # Debugging output
- print(f"Calculated dimensions -> Width: {width}, Height: {height}, Position: ({x}, {y})")
- # Resize and move window in one command
- subprocess.call([
- "xdotool", "windowmove", win_id, str(x), str(y),
- "windowsize", win_id, str(width), str(height)
- ])
- print(f"Window moved and resized to: {x},{y},{width},{height}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement