Advertisement
Darkonis

Untitled

Nov 8th, 2024 (edited)
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.28 KB | Source Code | 0 0
  1. #Pscreens v2.1 - tool for quickly changing resolutions, turning screens on/off, in a handy terminal
  2. #!/usr/bin/env python3
  3. import subprocess
  4. import json
  5.  
  6. def run_command(command):
  7.     try:
  8.         output = subprocess.check_output(command, shell=True, text=True)
  9.         return output.strip()
  10.     except subprocess.CalledProcessError as e:
  11.         print(f"Command failed: {e}")
  12.         return ""
  13.  
  14. def list_monitors():
  15.     command = "wlr-randr"
  16.     output = run_command(command)
  17.     if output:
  18.         monitors = output.split('\n')
  19.         print("\nAvailable Monitors:")
  20.         for i, monitor in enumerate(monitors, 1):
  21.             print(f"{i}: {monitor}")
  22.         return monitors
  23.     else:
  24.         print("\nFailed to list monitors.")
  25.         return []
  26.  
  27. def toggle_monitor():
  28.     command = "swaymsg -t get_outputs"
  29.     output = run_command(command)
  30.     if output:
  31.         monitors = json.loads(output)
  32.         print("\nAvailable Monitors:")
  33.         for i, monitor in enumerate(monitors, 1):
  34.             name = monitor.get("name")
  35.             make = monitor.get("make", "Unknown")
  36.             model = monitor.get("model", "Unknown")
  37.             active = "Active" if monitor.get("active") else "Inactive"
  38.             print(f"{i}: {name} - {make} {model} [{active}]")
  39.        
  40.         monitor_index = int(input("\nEnter the monitor number to toggle (on/off): ")) - 1
  41.         if 0 <= monitor_index < len(monitors):
  42.             monitor = monitors[monitor_index]["name"]
  43.             status = input("Enter 'on' to enable or 'off' to disable the monitor: ").strip().lower()
  44.             if status == "off":
  45.                 command = f"wlr-randr --output {monitor} --off"
  46.             elif status == "on":
  47.                 command = f"wlr-randr --output {monitor} --auto"
  48.             else:
  49.                 print("\nInvalid input. Please enter 'on' or 'off'.")
  50.                 return
  51.             result = run_command(command)
  52.             if result == "":
  53.                 print(f"\nMonitor {monitor} turned {status} successfully.")
  54.             else:
  55.                 print(f"\nFailed to toggle monitor {monitor}.")
  56.         else:
  57.             print("\nInvalid monitor number.")
  58.     else:
  59.         print("\nFailed to list monitors.")
  60.  
  61. def list_available_resolutions():
  62.     monitors = list_monitors()
  63.     if not monitors:
  64.         return
  65.     monitor_index = int(input("\nEnter the monitor number to list resolutions: ")) - 1
  66.     if 0 <= monitor_index < len(monitors):
  67.         monitor = monitors[monitor_index].split()[0]
  68.         command = f"wlr-randr --output {monitor}"
  69.         output = run_command(command)
  70.         if output:
  71.             resolutions = output.split('\n')
  72.             print(f"\nAvailable Resolutions for {monitor}:")
  73.             for i, resolution in enumerate(resolutions, 1):
  74.                 print(f"{i}: {resolution}")
  75.         else:
  76.             print(f"\nFailed to list resolutions for {monitor}.")
  77.     else:
  78.         print("\nInvalid monitor number.")
  79. #kind of sloppy but it works!
  80. def change_resolution():
  81.     monitors = list_monitors()
  82.     if not monitors:
  83.         return
  84.     monitor_index = int(input("\nEnter the monitor number to change resolution: ")) - 1
  85.     if 0 <= monitor_index < len(monitors):
  86.         monitor = monitors[monitor_index].split()[0]
  87.         command = f"wlr-randr --output {monitor}"
  88.         output = run_command(command)
  89.         if output:
  90.             resolutions = output.split('\n')
  91.             print(f"\nAvailable Resolutions for {monitor}:")
  92.             for i, resolution in enumerate(resolutions, 1):
  93.                 print(f"{i}: {resolution}")
  94.             resolution_index = int(input("\nEnter the desired resolution number: ")) - 1
  95.             if 0 <= resolution_index < len(resolutions):
  96.                 resolution = resolutions[resolution_index].split()[0]
  97.                 command = f"wlr-randr --output {monitor} --mode {resolution}"
  98.                 result = run_command(command)
  99.                 if result == "":
  100.                     print(f"\nResolution for {monitor} changed to {resolution} successfully.")
  101.                 else:
  102.                     print(f"\nFailed to change resolution for {monitor}.")
  103.             else:
  104.                 print("\nInvalid resolution number.")
  105.         else:
  106.             print(f"\nFailed to list resolutions for {monitor}.")
  107.     else:
  108.         print("\nInvalid monitor number.")
  109. #So this works, but.. it ends up being virtually the same as the resolution option
  110. def choose_refresh_rate():
  111.     monitors = list_monitors()
  112.     if not monitors:
  113.         return
  114.     monitor_index = int(input("\nEnter the monitor number to change refresh rate: ")) - 1
  115.     if 0 <= monitor_index < len(monitors):
  116.         monitor = monitors[monitor_index].split()[0]
  117.         command = f"wlr-randr --output {monitor}"
  118.         output = run_command(command)
  119.         if output:
  120.             modes = output.split('\n')
  121.             print(f"\nAvailable Modes for {monitor} (including refresh rates):")
  122.             for i, mode in enumerate(modes, 1):
  123.                 print(f"{i}: {mode}")
  124.             mode_index = int(input("\nEnter the desired mode number: ")) - 1
  125.             if 0 <= mode_index < len(modes):
  126.                 mode = modes[mode_index].split()[0]
  127.                 command = f"wlr-randr --output {monitor} --mode {mode}"
  128.                 result = run_command(command)
  129.                 if result == "":
  130.                     print(f"\nMode for {monitor} changed to {mode} successfully.")
  131.                 else:
  132.                     print(f"\nFailed to change mode for {monitor}.")
  133.             else:
  134.                 print("\nInvalid mode number.")
  135.         else:
  136.             print(f"\nFailed to list modes for {monitor}.")
  137.     else:
  138.         print("\nInvalid monitor number.")
  139.  
  140. def choose_multi_display_mode():
  141.     print("\nMulti-Display Modes:")
  142.     print("1: Mirrored")
  143.     print("2: Extended")
  144.     mode_choice = input("\nEnter multi-display mode number: ")
  145.     if mode_choice == "1":
  146.         command = "wlr-randr --mirror"
  147.     elif mode_choice == "2":
  148.         command = "wlr-randr --auto"
  149.     else:
  150.         print("\nInvalid mode selected.")
  151.         return
  152.     result = run_command(command)
  153.     if result == "":
  154.         mode = "mirrored" if mode_choice == "1" else "extended"
  155.         print(f"\nMulti-display mode set to {mode} successfully.")
  156.     else:
  157.         print(f"\nFailed to set multi-display mode.")
  158.  
  159. def main():
  160.     while True:
  161.         print("\nMonitor Configuration Menu:")
  162.         print("1: List Monitors")
  163.         print("2: Toggle Monitor On/Off")
  164.         print("3: List Available Resolutions")
  165.         print("4: Change Resolution")
  166.         print("5: Choose Refresh Rate")
  167.         print("6: Choose Multi-Display Mode")
  168.         print("7: Exit")
  169.  
  170.         choice = input("\nEnter your choice: ")
  171.  
  172.         if choice == '1':
  173.             list_monitors()
  174.         elif choice == '2':
  175.             toggle_monitor()
  176.         elif choice == '3':
  177.             list_available_resolutions()
  178.         elif choice == '4':
  179.             change_resolution()
  180.         elif choice == '5':
  181.             choose_refresh_rate()
  182.         elif choice == '6':
  183.             choose_multi_display_mode()
  184.         elif choice == '7':
  185.             break
  186.         else:
  187.             print("\nInvalid choice. Please try again.")
  188.  
  189. if __name__ == "__main__":
  190.     main()
  191.  
Tags: pscreens
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement