-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
683 lines (549 loc) · 27.2 KB
/
main.py
File metadata and controls
683 lines (549 loc) · 27.2 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
import sys
import os
import subprocess
import ctypes
import re
from ctypes import wintypes
from PyQt5.QtWidgets import QDialog, QLabel, QVBoxLayout, QApplication, QMessageBox, QWidget, QPushButton, QGridLayout, \
QPlainTextEdit
from PyQt5.QtCore import QTimer, pyqtSignal, Qt, QThread, QAbstractNativeEventFilter
from PyQt5.QtGui import QIcon
from ui_main import ScrcpyMainUI
from adb_manager import AdbManager
# --- Explicitly define ctypes argtypes and restype to prevent 64-bit OverflowError ---
user32 = ctypes.windll.user32
user32.GetWindowThreadProcessId.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.DWORD)]
user32.GetWindowThreadProcessId.restype = wintypes.DWORD
user32.GetWindowTextLengthW.argtypes = [wintypes.HWND]
user32.GetWindowTextLengthW.restype = ctypes.c_int
user32.GetWindowTextW.argtypes = [wintypes.HWND, ctypes.c_wchar_p, ctypes.c_int]
user32.GetWindowTextW.restype = ctypes.c_int
user32.IsWindow.argtypes = [wintypes.HWND]
user32.IsWindow.restype = ctypes.c_bool
user32.ShowWindow.argtypes = [wintypes.HWND, ctypes.c_int]
user32.ShowWindow.restype = ctypes.c_bool
user32.IsIconic.argtypes = [wintypes.HWND]
user32.IsIconic.restype = ctypes.c_bool
user32.IsWindowVisible.argtypes = [wintypes.HWND]
user32.IsWindowVisible.restype = ctypes.c_bool
user32.GetWindowRect.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.RECT)]
user32.GetWindowRect.restype = ctypes.c_bool
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, wintypes.HWND, wintypes.LPARAM)
user32.EnumWindows.argtypes = [EnumWindowsProc, wintypes.LPARAM]
user32.EnumWindows.restype = ctypes.c_bool
# -------------------------------------------------------------------------------------
class HotkeyFilter(QAbstractNativeEventFilter):
def __init__(self, callback):
super().__init__()
self.callback = callback
def nativeEventFilter(self, eventType, message):
if eventType == b"windows_generic_MSG":
msg = wintypes.MSG.from_address(message.__int__())
if msg.message == 0x0312:
if msg.wParam == 1:
self.callback()
return True, 0
return False, 0
class LogReaderThread(QThread):
log_signal = pyqtSignal(str)
def __init__(self, process):
super().__init__()
self.process = process
self._is_running = True
def run(self):
for line in iter(self.process.stdout.readline, ''):
if not self._is_running or not line:
break
self.log_signal.emit(line.strip())
def stop(self):
self._is_running = False
class LogWindow(QDialog):
def __init__(self, parent=None, title="Pqscrcpy Logs"):
super().__init__(parent)
self.setWindowTitle(title)
self.resize(600, 400)
layout = QVBoxLayout(self)
self.text_area = QPlainTextEdit()
self.text_area.setReadOnly(True)
base_font = "'Segoe UI', 'Microsoft YaHei', 'PingFang SC', sans-serif"
self.text_area.setStyleSheet(
f"font-family: {base_font}; font-size: 13px; background-color: #1e1e1e; color: #d4d4d4; padding: 5px;")
layout.addWidget(self.text_area)
def append_log(self, text):
self.text_area.appendPlainText(text)
bar = self.text_area.verticalScrollBar()
bar.setValue(bar.maximum())
class PerfWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.Tool | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.WindowTransparentForInput)
self.setAttribute(Qt.WA_TranslucentBackground, True)
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
base_font = "'Segoe UI', 'Microsoft YaHei', 'PingFang SC', sans-serif"
lbl_style = f"color: #00FF00; background-color: rgba(0, 0, 0, 160); font-weight: bold; font-family: {base_font}; font-size: 14px; padding: 4px 8px; border-radius: 4px;"
self.lbl_fps = QLabel("FPS: --")
self.lbl_bitrate = QLabel("Bitrate: --")
self.lbl_fps.setStyleSheet(lbl_style)
self.lbl_bitrate.setStyleSheet(lbl_style)
layout.addWidget(self.lbl_fps)
layout.addWidget(self.lbl_bitrate)
def update_perf(self, fps=None, bitrate=None):
if fps: self.lbl_fps.setText(f"FPS: {fps}")
if bitrate: self.lbl_bitrate.setText(f"Bitrate: {bitrate}")
self.adjustSize()
class NavBarWindow(QWidget):
drag_started = pyqtSignal()
drag_ended = pyqtSignal(object)
closed = pyqtSignal()
def __init__(self, send_cmd_func):
super().__init__()
self.setWindowFlags(Qt.Tool | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setAttribute(Qt.WA_TranslucentBackground, False)
self.grid = QGridLayout(self)
self.grid.setContentsMargins(5, 5, 5, 5)
self.grid.setSpacing(5)
base_font = "'Segoe UI', 'Microsoft YaHei', 'PingFang SC', sans-serif"
btn_style = f"""
QPushButton {{ background-color: #333333; color: white; border: 1px solid #555; border-radius: 4px; padding: 10px; font-weight: bold; font-family: {base_font}; }}
QPushButton:hover {{ background-color: #505050; }}
QPushButton:pressed {{ background-color: #202020; }}
"""
close_style = f"""
QPushButton {{ background-color: #880000; color: white; border: 1px solid #555; border-radius: 4px; padding: 10px; font-weight: bold; font-family: {base_font}; }}
QPushButton:hover {{ background-color: #aa0000; }}
QPushButton:pressed {{ background-color: #550000; }}
"""
self.btn_back = QPushButton("◁")
self.btn_home = QPushButton("〇")
self.btn_recent = QPushButton("□")
self.btn_close = QPushButton("×")
self.btn_back.setStyleSheet(btn_style)
self.btn_home.setStyleSheet(btn_style)
self.btn_recent.setStyleSheet(btn_style)
self.btn_close.setStyleSheet(close_style)
self.btn_back.clicked.connect(lambda *args: send_cmd_func(4))
self.btn_home.clicked.connect(lambda *args: send_cmd_func(3))
self.btn_recent.clicked.connect(lambda *args: send_cmd_func(187))
self.btn_close.clicked.connect(lambda *args: self.closed.emit())
self.current_orientation = None
self.set_orientation("right")
self._is_dragging = False
self._drag_start_pos = None
def set_orientation(self, pos):
is_horizontal = pos in ["top", "bottom", "tl_h", "tr_h", "bl_h", "br_h"]
new_orientation = "h" if is_horizontal else "v"
if self.current_orientation == new_orientation:
return
self.current_orientation = new_orientation
self.grid.removeWidget(self.btn_back)
self.grid.removeWidget(self.btn_home)
self.grid.removeWidget(self.btn_recent)
self.grid.removeWidget(self.btn_close)
if is_horizontal:
self.grid.addWidget(self.btn_back, 0, 0)
self.grid.addWidget(self.btn_home, 0, 1)
self.grid.addWidget(self.btn_recent, 0, 2)
self.grid.addWidget(self.btn_close, 0, 3)
self.setFixedSize(185, 45)
else:
self.grid.addWidget(self.btn_back, 0, 0)
self.grid.addWidget(self.btn_home, 1, 0)
self.grid.addWidget(self.btn_recent, 2, 0)
self.grid.addWidget(self.btn_close, 3, 0)
self.setFixedSize(45, 185)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self._is_dragging = True
self._drag_start_pos = event.globalPos() - self.frameGeometry().topLeft()
self.drag_started.emit()
event.accept()
def mouseMoveEvent(self, event):
if self._is_dragging:
self.move(event.globalPos() - self._drag_start_pos)
event.accept()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self._is_dragging:
self._is_dragging = False
self.drag_ended.emit(self.pos())
event.accept()
class MainApp:
def __init__(self):
self.app = QApplication(sys.argv)
self.app.setWindowIcon(QIcon("icon.ico"))
self.hotkey_filter = HotkeyFilter(self.toggle_visibility)
self.app.installNativeEventFilter(self.hotkey_filter)
self.ui = ScrcpyMainUI()
self.adb = AdbManager()
self.current_devices = []
# Key Changes: A multi-session dictionary is now used to support independent management across multiple devices; the key is `target_id`.
self.sessions = {}
self.is_hidden = False
self.tracking_timer = QTimer()
self.tracking_timer.timeout.connect(self.update_overlays_position)
self.bind_events()
self.update_device_ui()
self.start_device_polling()
self.register_boss_key()
def bind_events(self):
self.ui.btn_connect.clicked.connect(self.launch_scrcpy)
self.ui.btn_switch_wireless.clicked.connect(self.handle_switch_wireless)
self.ui.settings_saved.connect(self.on_settings_saved)
self.ui.language_changed.connect(self.update_device_ui)
self.ui.combo_devices.currentIndexChanged.connect(self.on_device_selected)
self.ui.combo_quality.currentIndexChanged.connect(self.on_quality_changed)
def on_settings_saved(self):
self.register_boss_key()
self.update_device_ui()
def on_device_selected(self, index):
if index < 0 or not self.current_devices:
return
device = self.ui.combo_devices.currentText()
saved_quality = self.ui.device_quality_map.get(device)
if saved_quality:
idx = self.ui.combo_quality.findText(saved_quality, Qt.MatchContains)
if idx >= 0:
self.ui.combo_quality.blockSignals(True)
self.ui.combo_quality.setCurrentIndex(idx)
self.ui.combo_quality.blockSignals(False)
def on_quality_changed(self, index):
if index < 0 or not self.current_devices:
return
device = self.ui.combo_devices.currentText()
if device and device in self.current_devices:
quality_name = self.ui.combo_quality.currentText().split('|')[0].strip()
self.ui.device_quality_map[device] = quality_name
self.ui.save_settings()
def clean_session(self, tid):
if tid in self.sessions:
s = self.sessions[tid]
if s['log_thread']:
s['log_thread'].stop()
s['log_thread'].wait()
if s['nav_bar']:
s['nav_bar'].hide()
s['nav_bar'].deleteLater()
if s['perf_window']:
s['perf_window'].hide()
s['perf_window'].deleteLater()
if s['log_window']:
s['log_window'].hide()
s['log_window'].deleteLater()
del self.sessions[tid]
def handle_nav_drag_started(self, tid):
if tid in self.sessions:
self.sessions[tid]['is_dragging_nav'] = True
def handle_nav_drag_ended(self, tid, drop_pos):
if tid not in self.sessions: return
session = self.sessions[tid]
session['is_dragging_nav'] = False
hwnd = self.get_scrcpy_hwnd(tid)
if not hwnd or not session['nav_bar']:
return
rect = wintypes.RECT()
user32.GetWindowRect(hwnd, ctypes.byref(rect))
w = rect.right - rect.left
h = rect.bottom - rect.top
horiz_w, horiz_h = 185, 45
vert_w, vert_h = 45, 185
positions = {
"top": (rect.left + (w // 2) - (horiz_w // 2), rect.top - horiz_h),
"bottom": (rect.left + (w // 2) - (horiz_w // 2), rect.bottom),
"left": (rect.left - vert_w, rect.top + (h // 2) - (vert_h // 2)),
"right": (rect.right, rect.top + (h // 2) - (vert_h // 2)),
"tl_v": (rect.left - vert_w, rect.top),
"tl_h": (rect.left, rect.top - horiz_h),
"tr_v": (rect.right, rect.top),
"tr_h": (rect.right - horiz_w, rect.top - horiz_h),
"bl_v": (rect.left - vert_w, rect.bottom - vert_h),
"bl_h": (rect.left, rect.bottom),
"br_v": (rect.right, rect.bottom - vert_h),
"br_h": (rect.right - horiz_w, rect.bottom)
}
closest_pos = "right"
min_dist = float('inf')
for name, (tx, ty) in positions.items():
dist = (drop_pos.x() - tx) ** 2 + (drop_pos.y() - ty) ** 2
if dist < min_dist:
min_dist = dist
closest_pos = name
self.ui.nav_bar_position = closest_pos
self.ui.save_settings()
self.update_overlays_position()
def handle_nav_closed(self, tid):
self.ui.nav_bar_enabled = False
self.ui.save_settings()
for s in self.sessions.values():
if s['nav_bar']:
s['nav_bar'].hide()
def handle_log_line(self, tid, line):
if tid not in self.sessions: return
session = self.sessions[tid]
if session['log_window'] and session['log_window'].isVisible():
session['log_window'].append_log(line)
if self.ui.perf_overlay and session['perf_window']:
fps_match = re.search(r'(\d+)\s*fps', line)
if fps_match:
session['perf_window'].update_perf(fps=fps_match.group(1))
def register_boss_key(self):
hwnd = int(self.ui.winId())
user32.UnregisterHotKey(hwnd, 1)
mods = self.ui.boss_key_mods
vk = self.ui.boss_key_vk
if vk and mods is not None:
if not user32.RegisterHotKey(hwnd, 1, mods, vk):
msg = "Boss Key conflict! Please choose another combination." if self.ui.language == "EN" else "老板键被其他程序占用,请选择其他快捷键。"
QMessageBox.warning(self.ui, "Shortcut Error" if self.ui.language == "EN" else "快捷键错误", msg)
def toggle_visibility(self):
self.is_hidden = not self.is_hidden
if self.is_hidden:
self.ui.hide()
for s in self.sessions.values():
if s['nav_bar']: s['nav_bar'].hide()
if s['perf_window']: s['perf_window'].hide()
self.hide_scrcpy(True)
else:
self.ui.show()
self.hide_scrcpy(False)
def hide_scrcpy(self, hide):
for tid in self.sessions.keys():
hwnd = self.get_scrcpy_hwnd(tid)
if hwnd:
user32.ShowWindow(hwnd, 0 if hide else 5)
def get_scrcpy_hwnd(self, tid):
session = self.sessions.get(tid)
if not session: return None
if session['hwnd']:
# This only checks whether a window exists; it does not verify whether it is visible (to prevent interference from the “boss key”).
if user32.IsWindow(session['hwnd']):
return session['hwnd']
else:
session['hwnd'] = None
target_pid = session['process'].pid
found_hwnd = None
def callback(hwnd, lParam):
nonlocal found_hwnd
pid = wintypes.DWORD()
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
if pid.value == target_pid:
# Lock only the main window that is initially visible; ignore hidden processes
if user32.IsWindowVisible(hwnd):
length = user32.GetWindowTextLengthW(hwnd)
if length > 0:
title_buf = ctypes.create_unicode_buffer(length + 1)
user32.GetWindowTextW(hwnd, title_buf, length + 1)
title = title_buf.value
if "IME" not in title and "Default" not in title:
found_hwnd = hwnd
return False
return True
enum_func = EnumWindowsProc(callback)
user32.EnumWindows(enum_func, 0)
if found_hwnd:
session['hwnd'] = found_hwnd
return found_hwnd
def update_overlays_position(self):
for tid in list(self.sessions.keys()):
session = self.sessions[tid]
# Automatically clean up dead processes to free up memory
if session['process'].poll() is not None:
self.clean_session(tid)
continue
nav_bar = session['nav_bar']
perf_window = session['perf_window']
if (not self.ui.nav_bar_enabled and not self.ui.perf_overlay) or self.is_hidden:
if nav_bar: nav_bar.hide()
if perf_window: perf_window.hide()
continue
hwnd = self.get_scrcpy_hwnd(tid)
if not hwnd or user32.IsIconic(hwnd) or not user32.IsWindowVisible(hwnd):
if nav_bar: nav_bar.hide()
if perf_window: perf_window.hide()
continue
rect = wintypes.RECT()
user32.GetWindowRect(hwnd, ctypes.byref(rect))
w = rect.right - rect.left
h = rect.bottom - rect.top
if self.ui.perf_overlay and perf_window:
y_offset = 35 if not self.ui.hide_borders else 5
tx = rect.left + 10
ty = rect.top + y_offset
perf_window.move(tx, ty)
if not perf_window.isVisible():
perf_window.show()
if self.ui.nav_bar_enabled and nav_bar and not session['is_dragging_nav']:
pos = self.ui.nav_bar_position
nav_bar.set_orientation(pos)
nav_w = nav_bar.width()
nav_h = nav_bar.height()
if pos == "top":
tx, ty = rect.left + (w // 2) - (nav_w // 2), rect.top - nav_h
elif pos == "bottom":
tx, ty = rect.left + (w // 2) - (nav_w // 2), rect.bottom
elif pos == "left":
tx, ty = rect.left - nav_w, rect.top + (h // 2) - (nav_h // 2)
elif pos == "right":
tx, ty = rect.right, rect.top + (h // 2) - (nav_h // 2)
elif pos == "tl_v":
tx, ty = rect.left - nav_w, rect.top
elif pos == "tl_h":
tx, ty = rect.left, rect.top - nav_h
elif pos == "tr_v":
tx, ty = rect.right, rect.top
elif pos == "tr_h":
tx, ty = rect.right - nav_w, rect.top - nav_h
elif pos == "bl_v":
tx, ty = rect.left - nav_w, rect.bottom - nav_h
elif pos == "bl_h":
tx, ty = rect.left, rect.bottom
elif pos == "br_v":
tx, ty = rect.right, rect.bottom - nav_h
elif pos == "br_h":
tx, ty = rect.right - nav_w, rect.bottom
else:
tx, ty = rect.right, rect.top + (h // 2) - (nav_h // 2)
nav_bar.move(tx, ty)
if not nav_bar.isVisible():
nav_bar.show()
def start_device_polling(self):
self.poll_timer = QTimer()
self.poll_timer.timeout.connect(self.check_devices)
self.poll_timer.start(2000)
def check_devices(self):
devices = self.adb.get_connected_devices()
if devices != self.current_devices:
self.current_devices = devices
self.update_device_ui()
def update_device_ui(self):
current_sel = self.ui.combo_devices.currentText()
self.ui.combo_devices.blockSignals(True)
self.ui.combo_devices.clear()
if not self.current_devices:
msg = self.ui.texts[self.ui.language]["no_devices"]
self.ui.combo_devices.addItem(msg)
self.ui.lbl_status_dot.setStyleSheet("background-color: #f44336; border-radius: 7px;")
self.ui.btn_switch_wireless.setEnabled(False)
else:
self.ui.combo_devices.addItems(self.current_devices)
self.ui.lbl_status_dot.setStyleSheet("background-color: #4CAF50; border-radius: 7px;")
if current_sel in self.current_devices:
self.ui.combo_devices.setCurrentText(current_sel)
else:
current_sel = self.current_devices[0]
is_usb = ":" not in current_sel
self.ui.btn_switch_wireless.setEnabled(is_usb)
if not is_usb and not self.ui.ip_input.text():
self.ui.ip_input.setText(current_sel)
self.ui.combo_devices.blockSignals(False)
self.on_device_selected(self.ui.combo_devices.currentIndex())
def handle_switch_wireless(self):
if not self.current_devices:
return
target_device = self.ui.combo_devices.currentText()
if not target_device or target_device not in self.current_devices:
target_device = self.current_devices[0]
self.ui.btn_switch_wireless.setEnabled(False)
wireless_ip = self.adb.switch_to_wireless(target_device)
if wireless_ip:
self.ui.ip_input.setText(wireless_ip)
self.check_devices()
else:
msg = "Failed to extract device IP or switch to TCP mode." if self.ui.language == "EN" else "无法提取设备 IP 或切换到 TCP 模式。"
QMessageBox.warning(self.ui, "Error" if self.ui.language == "EN" else "错误", msg)
self.check_devices()
def launch_scrcpy(self):
target_ip = self.ui.ip_input.text().strip()
device_sel = self.ui.combo_devices.currentText()
if not target_ip and (not self.current_devices or device_sel not in self.current_devices):
msg = "No device IP entered and no USB device detected." if self.ui.language == "EN" else "未输入设备 IP,也未检测到 USB 设备。"
QMessageBox.warning(self.ui, "Error" if self.ui.language == "EN" else "错误", msg)
return
target_id = target_ip if target_ip else device_sel
# Core Duplicate Detection: Checks whether the target device is already casting
if target_id in self.sessions:
if self.sessions[target_id]['process'].poll() is None:
return # The device process is still running; simply ignore the click
else:
self.clean_session(target_id) # The process has terminated; it will be allowed to restart after residual data has been cleared
quality_opt = self.ui.combo_quality.currentData()
if not quality_opt:
quality_opt = self.ui.default_quality
scrcpy_dir = self.ui.custom_scrcpy_path
if not scrcpy_dir:
scrcpy_dir = "scrcpy"
scrcpy_exe = os.path.join(scrcpy_dir, "scrcpy.exe")
cmd = [scrcpy_exe]
if target_ip:
cmd.extend(["-s", target_ip])
elif device_sel in self.current_devices:
cmd.extend(["-s", device_sel])
if quality_opt.get("bitrate"): cmd.extend(["--video-bit-rate", quality_opt["bitrate"]])
if quality_opt.get("resolution"): cmd.extend(["--max-size", quality_opt["resolution"]])
if quality_opt.get("codec"): cmd.extend([f"--video-codec={quality_opt['codec']}"])
fps_to_use = self.ui.max_fps if (self.ui.max_fps and self.ui.max_fps.isdigit()) else quality_opt.get("fps")
if fps_to_use and str(fps_to_use).isdigit(): cmd.extend(["--max-fps", str(fps_to_use)])
if not self.ui.audio_enabled: cmd.append("--no-audio")
if self.ui.keyboard_uhid: cmd.append("--keyboard=uhid")
if self.ui.controller: cmd.append("--gamepad=uhid")
if self.ui.stay_awake: cmd.append("--stay-awake")
if self.ui.hide_borders: cmd.append("--window-borderless")
if self.ui.show_touches: cmd.append("--show-touches")
if self.ui.fullscreen: cmd.append("--fullscreen")
if self.ui.always_on_top: cmd.append("--always-on-top")
if self.ui.read_only: cmd.append("--no-control")
if self.ui.turn_screen_off: cmd.append("--turn-screen-off")
if self.ui.perf_overlay: cmd.append("--print-fps")
kwargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.STDOUT, 'text': True, 'bufsize': 1}
if os.name == 'nt' and not self.ui.program_logs:
kwargs['creationflags'] = subprocess.CREATE_NO_WINDOW
try:
process = subprocess.Popen(cmd, **kwargs)
# Initialize a standalone session environment on the target device
session = {
'process': process,
'log_thread': None,
'log_window': None,
'nav_bar': None,
'perf_window': None,
'hwnd': None,
'is_dragging_nav': False
}
self.sessions[target_id] = session
# Log Thread Binding
thread = LogReaderThread(process)
thread.log_signal.connect(lambda line, tid=target_id: self.handle_log_line(tid, line))
session['log_thread'] = thread
thread.start()
# Floating Window Binding
if self.ui.nav_bar_enabled:
nb = NavBarWindow(lambda keycode, tid=target_id: self.adb.send_keyevent(tid, keycode))
nb.drag_started.connect(lambda tid=target_id: self.handle_nav_drag_started(tid))
nb.drag_ended.connect(lambda drop_pos, tid=target_id: self.handle_nav_drag_ended(tid, drop_pos))
nb.closed.connect(lambda tid=target_id: self.handle_nav_closed(tid))
session['nav_bar'] = nb
if self.ui.perf_overlay:
pw = PerfWindow()
pw.update_perf(bitrate=quality_opt.get("bitrate", "Auto"))
session['perf_window'] = pw
if self.ui.program_logs:
title = f"Pqscrcpy Logs - {target_id}"
lw = LogWindow(title=title)
lw.show()
session['log_window'] = lw
if not self.tracking_timer.isActive():
self.tracking_timer.start(30)
except FileNotFoundError:
err_en = f"scrcpy executable not found in {scrcpy_dir} directory."
err_zh = f"在 {scrcpy_dir} 目录中未找到 scrcpy 可执行文件。"
msg = err_en if self.ui.language == "EN" else err_zh
QMessageBox.critical(self.ui, "Execution Error" if self.ui.language == "EN" else "执行错误", msg)
except Exception as e:
msg = f"Failed to launch Pqscrcpy: {str(e)}" if self.ui.language == "EN" else f"启动 Pqscrcpy 失败: {str(e)}"
QMessageBox.critical(self.ui, "Execution Error" if self.ui.language == "EN" else "执行错误", msg)
def run(self):
self.ui.show()
return self.app.exec_()
if __name__ == "__main__":
app = MainApp()
sys.exit(app.run())