feat: 1920 1080 解析度切換

This commit is contained in:
2026-01-31 09:32:03 +08:00
parent 9fc783c3db
commit ccc4d2644a

View File

@@ -10,59 +10,104 @@ def notify(title, message):
except: except:
pass pass
def strip_ansi(text):
"""移除 ANSI 顏色跳脫字元"""
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
return ansi_escape.sub('', text)
# 1. 取得 kscreen-doctor 輸出 # 1. 取得 kscreen-doctor 輸出
try: try:
output = subprocess.check_output(["kscreen-doctor", "-o"], text=True) raw_output = subprocess.check_output(["kscreen-doctor", "-o"], text=True)
output = strip_ansi(raw_output)
except Exception as e: except Exception as e:
print(f"執行失敗: {e}") print(f"執行失敗: {e}")
sys.exit(1) sys.exit(1)
# 2. 取得螢幕名稱與目前狀態 # 2. 取得螢幕名稱
try: try:
# 取得第一行第三個單字作為螢幕名稱 (例如 eDP-1)
screen = output.split('\n')[0].split()[2].strip() screen = output.split('\n')[0].split()[2].strip()
except Exception as e: except Exception:
print(f"失敗: 無法解析螢幕名稱\n{output}") screen = "eDP-1"
sys.exit(1)
# 3. 解析所有模式與目前狀態 # 3. 解析模式與狀態
# 3. 解析模式與目前狀態 res_map = {}
# 取得目前使用的 ID 與解析度 (最後帶有 *) curr_res = None
curr_match = re.search(r"(\d+):(\d+x\d+)@[^*]*\*", output)
if not curr_match: for part in output.split():
if ":" in part and "@" in part:
try:
# 移除符號
clean_part = part.replace("!", "").replace("*", "")
mid_res_part = clean_part.split("@")[0]
mid = mid_res_part.split(":")[0]
res = mid_res_part.split(":")[1]
if res not in res_map:
res_map[res] = mid
if "*" in part:
curr_res = res
except:
continue
# 備援:若沒抓到目前的模式,嘗試從 Geometry 抓
if not curr_res:
geo_match = re.search(r"Geometry: \d+,\d+ (\d+x\d+)", output)
curr_res = geo_match.group(1) if geo_match else None
if not curr_res:
print("失敗: 無法辨識目前解析度模式") print("失敗: 無法辨識目前解析度模式")
sys.exit(1) sys.exit(1)
curr_id, curr_res = curr_match.groups() # 4. 判斷切換目標 (家族規則)
target_res = None
# 取得 4K 與 1200p 的模式 ID if curr_res == "3840x2400":
id_4k = None target_res = "1920x1200"
id_1200p = None elif curr_res == "1920x1200":
target_res = "3840x2400"
for mid, res in re.findall(r"(\d+):(\d+x\d+)@", output): elif curr_res == "3200x1800":
if res == "3840x2400": target_res = "1920x1080"
id_4k = mid elif curr_res == "1920x1080":
if res == "1920x1200": target_res = "3200x1800"
id_1200p = mid
# 4. 切換邏輯
# 如果目前解析度是 4K (3840x2400),則切換往 1200p
if curr_res == "3840x2400" and id_1200p:
target, scale, msg = id_1200p, "1", "1200p (100%)"
else: else:
# 否則(目前在 1200p 或其他模式)切換往 4K # Fallback 到 16:9
target, scale, msg = id_4k, "2", "4K (200%)" target_res = "3200x1800" if not curr_res.startswith("3840") else "1920x1080"
if not target: target_id = res_map.get(target_res)
print(f"失敗: 找不到目標模式 (3840x2400={id_4k}, 1920x1200={id_1200p})")
# 如果找不到目標,嘗試在家族內尋找替代品
if not target_id:
fallback_order = ["1920x1200", "3840x2400", "3200x1800", "1920x1080"]
for res in fallback_order:
if res in res_map and res != curr_res:
target_res = res
target_id = res_map[res]
break
if not target_id:
print(f"失敗: 在系統中找不到解析度 {target_res}")
sys.exit(1) sys.exit(1)
# 5. 執行指令 # 5. 決定比例
subprocess.run([ if target_res == "3840x2400":
"kscreen-doctor", target_scale = "2"
f"output.{screen}.mode.{target}", percent = "200%"
f"output.{screen}.scale.{scale}" elif target_res == "3200x1800":
]) target_scale = "1.6666666666666667"
percent = "167%"
else:
target_scale = "1"
percent = "100%"
# 6. 顯示通知 msg = f"{target_res} ({percent})"
notify("解析度切換完成", f"已切換至 {msg}")
# 6. 執行
cmd = ["kscreen-doctor", f"output.{screen}.mode.{target_id}", f"output.{screen}.scale.{target_scale}"]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
# 修復驅動相衝:嘗試僅切換解析度
subprocess.run(["kscreen-doctor", f"output.{screen}.mode.{target_id}"])
notify("解析度切換成功 (排除比例)", msg)
else:
notify("解析度切換成功", msg)