Advertisement
Guest User

Untitled

a guest
May 23rd, 2025
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4. import subprocess
  5. import shutil
  6.  
  7. # Check dependencies
  8. for tool in ["xprop", "xdpyinfo", "xdotool", "wmctrl"]:
  9. if not shutil.which(tool):
  10. print(f"Error: {tool} is not installed.")
  11. sys.exit(1)
  12.  
  13. # Usage
  14. if len(sys.argv) < 5:
  15. print("Usage: tiling.py <width%> <height%> <x_offset%> <y_offset%>")
  16. sys.exit(1)
  17.  
  18. def validate(val, name):
  19. if not 0 <= val <= 100:
  20. print(f"Error: {name} must be between 0 and 100.")
  21. sys.exit(1)
  22.  
  23. # Parse and validate args
  24. w_pct, h_pct, x_pct, y_pct = map(int, sys.argv[1:5])
  25. for val, name in zip([w_pct, h_pct, x_pct, y_pct], ["Width", "Height", "X Offset", "Y Offset"]):
  26. validate(val, name)
  27.  
  28. BUFFER_W = 10
  29. BUFFER_H = 33
  30.  
  31. # Get active window ID
  32. try:
  33. win_id = subprocess.check_output(["xdotool", "getactivewindow"]).decode().strip()
  34. except subprocess.CalledProcessError:
  35. print("Error: Cannot get active window.")
  36. sys.exit(1)
  37.  
  38. # Get window class
  39. def get_class(wid):
  40. try:
  41. output = subprocess.check_output(["xprop", "-id", wid, "WM_CLASS"]).decode()
  42. return output.split('=')[1].split(',')[1].strip().strip('"')
  43. except Exception as e:
  44. print(f"Error retrieving class: {e}")
  45. return ""
  46.  
  47. app_class = get_class(win_id)
  48. print(f"Window class: {app_class}") # Debugging output
  49.  
  50. # Unmaximize window
  51. subprocess.call(["wmctrl", "-ir", win_id, "-b", "remove,maximized_vert,maximized_horz"])
  52.  
  53. # Get screen workarea
  54. def get_workarea():
  55. try:
  56. out = subprocess.check_output(["xprop", "-root", "_NET_WORKAREA"]).decode()
  57. return list(map(int, out.split('=')[1].split(',')[:4]))
  58. except subprocess.CalledProcessError:
  59. # fallback to screen size if xprop fails
  60. for line in subprocess.check_output(["xdpyinfo"]).decode().splitlines():
  61. if "dimensions:" in line:
  62. w, h = map(int, line.split()[1].split('x'))
  63. return [0, 0, w, h]
  64.  
  65. wx, wy, ww, wh = get_workarea()
  66.  
  67. # Calculate size and position
  68. width = ww * w_pct // 100 - BUFFER_W
  69. if x_pct == 0 and app_class in ["Brave-browser", "Google-chrome"]:
  70. width = ww * (w_pct + 1) // 100 - BUFFER_W # Add extra width for Brave
  71.  
  72. height = wh * h_pct // 100
  73. if app_class not in ["Brave-browser", "Google-chrome"]:
  74. height -= BUFFER_H
  75. height = max(1, height)
  76.  
  77. x = wx + ww * x_pct // 100
  78. y = wy + wh * y_pct // 100
  79.  
  80. # Debugging output
  81. print(f"Calculated dimensions -> Width: {width}, Height: {height}, Position: ({x}, {y})")
  82.  
  83. # Resize and move window in one command
  84. subprocess.call([
  85. "xdotool", "windowmove", win_id, str(x), str(y),
  86. "windowsize", win_id, str(width), str(height)
  87. ])
  88.  
  89. print(f"Window moved and resized to: {x},{y},{width},{height}")
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement