-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
346 lines (284 loc) · 12.4 KB
/
main.py
File metadata and controls
346 lines (284 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# nuitka-project: --mode=standalone
# nuitka-project: --include-data-files=icon.ico=icon.ico
# nuitka-project: --output-dir=build
# nuitka-project: --output-file=DisplaySwitcher.exe
# nuitka-project: --assume-yes-for-downloads
# nuitka-project: --enable-plugins=pyside6
# nuitka-project: --windows-product-version=1.0.0
# nuitka-project: --windows-file-version=1.0.0
# nuitka-project: --windows-product-name=DisplaySwitcher
# nuitka-project: --windows-icon-from-ico=icon.ico
# nuitka-project: --windows-console-mode=disable
import argparse
import json
import os
import sys
import threading
import winreg as reg
import keyboard
from PySide6.QtCore import Qt
from PySide6.QtGui import QIcon, QAction, QKeySequence
from PySide6.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QCheckBox,
QTreeWidget, QTreeWidgetItem, QSystemTrayIcon, QMenu, QLabel
)
from monitorcontrol import get_monitors, InputSource
# ========== 常量定义 ==========
CONFIG_PATH = "config.json"
APP_NAME = "DisplayInputSwitcher"
APP_TITLE = "多显示器输入源切换工具"
ICON_PATH = "icon.ico"
# 设置工作目录为程序所在目录
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
# ========== 开机自启类 ==========
class Autostart:
"""处理程序的开机自启设置"""
def __init__(self, name: str, path: str = None, hidden: bool = False):
self.name = name
self.path = os.path.abspath(path or sys.argv[0])
self.hidden = hidden
self.key_path = r"Software\Microsoft\Windows\CurrentVersion\Run"
def exists(self) -> bool:
try:
with reg.OpenKey(reg.HKEY_CURRENT_USER, self.key_path) as key:
reg.QueryValueEx(key, self.name)
return True
except Exception:
return False
def enable(self) -> bool:
try:
exec_path = f'"{self.path}"' if ' ' in self.path else self.path
if self.hidden:
exec_path += " --hidden"
with reg.OpenKey(reg.HKEY_CURRENT_USER, self.key_path, 0, reg.KEY_SET_VALUE) as key:
reg.SetValueEx(key, self.name, 0, reg.REG_SZ, exec_path)
return True
except Exception as e:
print(f"Error enabling autostart: {e}")
return False
def disable(self) -> bool:
try:
with reg.OpenKey(reg.HKEY_CURRENT_USER, self.key_path, 0, reg.KEY_SET_VALUE) as key:
reg.DeleteValue(key, self.name)
return True
except FileNotFoundError:
return True
except Exception as e:
print(f"Error disabling autostart: {e}")
return False
def set_autostart(self, switch: bool, hidden: bool = None) -> bool:
if hidden is not None:
self.hidden = hidden
return self.enable() if switch else self.disable()
# ========== 自定义树控件 ==========
class HotkeyTree(QTreeWidget):
"""用于显示器输入源配置的树状控件"""
def __init__(self, parent):
super().__init__(parent)
self.setHeaderLabels(["显示器/输入源", "热键", "备注", "状态"])
self.setColumnWidth(0, 250)
def keyPressEvent(self, event):
item = self.currentItem()
if not item or item.parent() or self.currentColumn() != 1:
return super().keyPressEvent(event)
mods = []
if event.modifiers() & Qt.ControlModifier:
mods.append("Ctrl")
if event.modifiers() & Qt.AltModifier:
mods.append("Alt")
if event.modifiers() & Qt.ShiftModifier:
mods.append("Shift")
key = QKeySequence(event.key()).toString()
if key and key not in mods:
mods.append(key)
item.setText(1, "+".join(mods))
self.parent().update_config()
# ========== 主窗口类 ==========
class MonitorSwitcher(QWidget):
"""主应用窗口"""
def __init__(self, hidden=False):
super().__init__()
self.hidden_mode = hidden
self.setWindowTitle(APP_TITLE)
self.resize(800, 500)
self.app = Autostart(name=APP_NAME)
self.monitor_data = self.get_monitor_data()
self.config_map = {}
self.tree = HotkeyTree(self)
self.tree.itemChanged.connect(self.update_config)
self.tray = None
self.init_ui()
self.init_tray()
self.load_config()
self.hotkey_listener()
# ========== 初始化界面 ==========
def init_ui(self):
layout = QVBoxLayout(self)
btns = QHBoxLayout()
btn_add = QPushButton("添加配置项")
btn_add.clicked.connect(self.add_item)
btns.addWidget(btn_add)
btn_del = QPushButton("删除选中行")
btn_del.clicked.connect(self.delete_item)
btns.addWidget(btn_del)
layout.addLayout(btns)
layout.addWidget(self.tree)
self.autostart_box = QCheckBox("开机自启")
self.autostart_box.setChecked(self.app.exists())
self.autostart_box.stateChanged.connect(lambda s: self.app.set_autostart(s == 2, hidden=True))
layout.addWidget(self.autostart_box)
# ========== 托盘图标 ==========
def init_tray(self):
self.tray = QSystemTrayIcon(QIcon(self.resource_path(ICON_PATH)), self)
self.tray.setToolTip(APP_TITLE)
menu = QMenu(self)
menu.addAction(QAction("打开主界面", self, triggered=self.showNormal))
menu.addAction(QAction("退出", self, triggered=QApplication.quit))
self.tray.setContextMenu(menu)
self.tray.activated.connect(lambda r: self.showNormal() if r == QSystemTrayIcon.Trigger else None)
self.tray.show()
if self.hidden_mode:
self.tray.showMessage("程序已启动", "程序正在后台运行", QSystemTrayIcon.Information, 3000)
def closeEvent(self, event):
event.ignore()
self.hide()
self.tray.showMessage("最小化到托盘", "程序仍在后台运行", QSystemTrayIcon.Information, 3000)
# ========== 热键监听 ==========
def hotkey_listener(self):
def listen():
while True:
event = keyboard.read_event()
if event.event_type == keyboard.KEY_DOWN:
combo = []
if keyboard.is_pressed("ctrl"): combo.append("Ctrl")
if keyboard.is_pressed("alt"): combo.append("Alt")
if keyboard.is_pressed("shift"): combo.append("Shift")
combo.append(event.name.upper())
combo_str = "+".join(combo)
cfg = self.config_map.get(combo_str)
if cfg:
self.switch_monitor_input(cfg)
threading.Thread(target=listen, daemon=True).start()
# ========== 配置操作 ==========
def add_item(self):
parent = QTreeWidgetItem(self.tree, ["配置", "双击编辑后按下热键", "", ""])
parent.setFlags(parent.flags() | Qt.ItemIsEditable)
self.tree.setItemWidget(parent, 0, QLabel(""))
parent.setCheckState(3, Qt.Checked)
parent.setExpanded(True)
for mid, mon in self.monitor_data.items():
m_item = QTreeWidgetItem(parent, [mon["display_name"], "", ""])
m_item.setData(0, Qt.UserRole, mid)
for src in mon["sources"]:
s_item = QTreeWidgetItem(m_item, [src, "", ""])
s_item.setCheckState(0, Qt.Unchecked)
self.update_config()
def delete_item(self):
item = self.tree.currentItem()
if item and item.parent() is None:
self.tree.takeTopLevelItem(self.tree.indexOfTopLevelItem(item))
self.update_config()
def update_config(self):
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(self.collect_config(), f, ensure_ascii=False, indent=2)
def collect_config(self):
configs = []
self.config_map.clear()
for i in range(self.tree.topLevelItemCount()):
top_item = self.tree.topLevelItem(i)
if not top_item:
continue
hotkey = top_item.text(1).strip()
remark = top_item.text(2).strip()
enabled = top_item.checkState(3) == Qt.Checked
models = []
for j in range(top_item.childCount()):
mon_item = top_item.child(j)
monitor_id = mon_item.data(0, Qt.UserRole)
display_name = mon_item.text(0)
selected_sources = [
mon_item.child(k).text(0)
for k in range(mon_item.childCount())
if mon_item.child(k).checkState(0) == Qt.Checked
]
if selected_sources:
models.append({
"monitor_id": monitor_id,
"display_name": display_name,
"available_sources": selected_sources
})
config_entry = {
"hotkeys": hotkey,
"enabled": enabled,
"remark": remark,
"models": models
}
if enabled and hotkey:
self.config_map[hotkey] = config_entry
configs.append(config_entry)
return configs
def load_config(self):
if not os.path.exists(CONFIG_PATH):
return
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
for entry in data:
parent = QTreeWidgetItem(self.tree, ["配置", entry.get("hotkeys", ""), entry.get("remark", ""), ""])
parent.setFlags(parent.flags() | Qt.ItemIsEditable)
self.tree.setItemWidget(parent, 0, QLabel(""))
parent.setCheckState(3, Qt.Checked if entry.get("enabled", True) else Qt.Unchecked)
parent.setExpanded(True)
for monitor_id, model_cfg in self.monitor_data.items():
models = entry.get("models", [])
target_model = next((m for m in models if m.get("monitor_id") == monitor_id), None)
model_item = QTreeWidgetItem(parent, [model_cfg["display_name"], "", ""])
model_item.setData(0, Qt.UserRole, monitor_id)
sources = target_model.get("available_sources", []) if target_model else []
for source in model_cfg['available_sources']:
input_item = QTreeWidgetItem(model_item, [source, "", ""])
input_item.setCheckState(0, Qt.Checked if source in sources else Qt.Unchecked)
# ========== 输入源切换 ==========
def switch_monitor_input(self, cfg):
for monitor in get_monitors():
with monitor:
model = monitor.get_vcp_capabilities().get("model", "")
for c in cfg["models"]:
if c["display_name"] == model:
sources = c["available_sources"]
try:
cur = InputSource(monitor.get_input_source()).name
idx = (sources.index(cur) + 1) % len(sources)
except Exception:
idx = 0
monitor.set_input_source(InputSource[sources[idx]])
print(f"切换 {model} 至 {sources[idx]}")
# ========== 工具函数 ==========
@staticmethod
def resource_path(relative_path):
return os.path.join(getattr(sys, '_MEIPASS', os.path.dirname(__file__)), relative_path)
@staticmethod
def get_monitor_data():
monitor_data = {}
for idx, monitor in enumerate(get_monitors()):
try:
with monitor:
caps = monitor.get_vcp_capabilities()
monitor_data[f"monitor_{idx}"] = {
"monitor_id": f"monitor_{idx}",
"display_name": caps.get("model", f"Unknown Monitor {idx}"),
"sources": [s.name for s in caps.get("inputs", [])],
"available_sources": [s.name for s in caps.get("inputs", [])]
}
except Exception:
pass
return monitor_data
# ========== 程序入口 ==========
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--hidden', action='store_true', help='以隐藏模式启动')
args = parser.parse_args()
app = QApplication(sys.argv)
win = MonitorSwitcher(hidden=args.hidden)
if not args.hidden:
win.show()
app.exec()