forked from srevinsaju/guiscrcpy
/
launcher.py
1281 lines (1165 loc) · 49.8 KB
/
launcher.py
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
GUISCRCPY by srevinsaju
Get it on : https://github.com/srevinsaju/guiscrcpy
Licensed under GNU Public License
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Icon made by Dave Gandy from www.flaticon.com used under
Creative Commons 3.0 Unported. The original SVG black work
by Dave Gandy has been re-oriented, flipped or color-changed.
The rest of Terms and Conditions put forward by
CC-3.0:Unported has been feverently followed by the developer.
Icons have been adapted in all the three windows.
Icons pack obtained from www.flaticon.com
All rights reserved.
"""
import hashlib
import os
import subprocess
import sys
import time
import webbrowser
from subprocess import PIPE
from subprocess import Popen
from qtpy import QtCore, QtWidgets
from qtpy.QtCore import QModelIndex, QPoint
from qtpy.QtGui import QPixmap, QIcon, QFont, QFontDatabase
from qtpy.QtWidgets import QMainWindow, QListWidgetItem, QMenu
from qtpy.QtWidgets import QMessageBox
from .lib.bridge.audio.sndcpy import SndcpyBridge
from .lib.bridge.audio.usbaudio import USBAudioBridge
from .lib.config import InterfaceConfig
from .lib.utils import format_colors as fc
from .constants import FONTS
from .install.finder import open_exe_name_dialog
from .lib.process import is_running
from .lib.toolkit import UXMapper
from .lib.utils import log, get_self
from .platform import platform
from .theme.desktop_shortcut import desktop_device_shortcut_svg
from .theme.linux_desktop_shortcut_template import GUISCRCPY_DEVICE
from .theme.style import dark_stylesheet
from .ux import Ui_MainWindow
from .ux.panel import Panel
from .ux.swipe import SwipeUX
from .ux.toolkit import InterfaceToolkit
from .version import VERSION
from .lib.bridge import AndroidDebugBridge, ScrcpyBridge
from .lib.bridge.exceptions import ScrcpyServerNotFoundError
environment = platform.System()
# ============================================================================
# Load cairo-svg conditionally
if environment.system() == "Linux":
try:
from cairosvg import svg2png # noqa:
has_cairo = True
except Exception as e:
print("Failed to load cairo:", e)
print("Some features are likely to be disabled")
has_cairo = False
class InterfaceGuiscrcpy(QMainWindow, Ui_MainWindow):
"""
Main class for guiscrcpy object.
All the processes to spawn to scrcpy are handled here
"""
# noinspection PyArgumentList
def __init__(
self,
config_manager: InterfaceConfig,
adb: AndroidDebugBridge,
scrcpy: ScrcpyBridge,
force_window_frame: bool = False,
panels_not_always_on_top: bool = False,
debug_no_scrcpy: bool = False,
):
QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self._adb = adb
self._scrcpy = scrcpy
self.config_manager = config_manager
config = self.config_manager.get_config()
self._config = config
self.panels_not_always_on_top = panels_not_always_on_top
self.force_window_frame = force_window_frame
self.debug__no_scrcpy = debug_no_scrcpy
self.cmx = None
self.sm = None
self.mp = None
self.nm = None
self.swipe_instance = None
self.panel_instance = None
self.side_instance = None
self.child_windows = list()
self.options = ""
log(
"Options received by class are : {} {} {} {} {} ".format(
config["bitrate"],
config["dimension"],
config["swtouches"],
config["dispRO"],
config["fullscreen"],
)
)
# ====================================================================
# Rotation; read config, update UI
self.device_rotation.setCurrentIndex(config.get("rotation", 0))
self.dial.setValue(int(config["bitrate"]))
if config["swtouches"]:
self.showTouches.setChecked(True)
else:
self.showTouches.setChecked(False)
if config["dispRO"]:
self.displayForceOn.setChecked(True)
else:
self.displayForceOn.setChecked(False)
# panels
if config["panels"].get("swipe"):
self.check_swipe_panel.setChecked(True)
else:
self.check_swipe_panel.setChecked(False)
if config["panels"].get("tookit"):
self.check_side_panel.setChecked(True)
else:
self.check_side_panel.setChecked(False)
if config["panels"].get("bottom"):
self.check_bottom_panel.setChecked(True)
else:
self.check_bottom_panel.setChecked(False)
# dimension
if config["dimension"] is not None:
self.dimensionDefaultCheckbox.setChecked(False)
try:
self.dimensionSlider.setValue(config["dimension"])
except TypeError:
self.dimensionDefaultCheckbox.setChecked(True)
if config["fullscreen"]:
self.fullscreen.setChecked(True)
else:
self.fullscreen.setChecked(False)
if is_running("scrcpy"):
log("SCRCPY RUNNING")
self.display_public_message("SCRCPY SERVER RUNNING")
else:
log("SCRCPY SERVER IS INACTIVE")
self.display_public_message("SCRCPY SERVER NOT RUNNING")
# CONNECT DIMENSION CHECK BOX TO STATE CHANGE
self.dimensionDefaultCheckbox.stateChanged.connect(self.__dimension_change_cb)
self.build_label.setText("Build {} by srevinsaju".format(VERSION))
# DIAL CTRL GRP
self.dial.sliderMoved.connect(self.__dial_change_cb)
self.dial.sliderReleased.connect(self.__dial_change_cb)
# DIAL CTRL GRP
# MAIN EXECUTE ACTION
self.executeaction.clicked.connect(self.start_act)
try:
if config["extra"]:
self.flaglineedit.setText(config["extra"])
except Exception as err:
log(f"Exception: flaglineedit.text(config[extra]) {err}")
self.quit.clicked.connect(self.quit_window)
self.dimensionText.setText("DEFAULT")
config["bitrate"] = int(self.dial.value())
self.bitrateText.setText(str(config["bitrate"]) + "KB/s")
self.pushButton.setText("RESET")
self.pushButton.clicked.connect(self.reset)
self.abtgit.clicked.connect(self.launch_web_github)
self.usbaud.clicked.connect(self.launch_usb_audio)
self.initmapnow.clicked.connect(self.bootstrap_mapper)
self.network_button.clicked.connect(self.network_mgr)
self.settings_button.clicked.connect(self.settings_mgr)
# self.devices_view.itemChanged.connect(self.update_rotation_combo_cb)
self.devices_view.itemClicked.connect(self.update_rotation_combo_cb)
self.refreshdevices.clicked.connect(self.scan_devices_update_list_view)
self.restart_adb_server.clicked.connect(self.restart_adb_server_guiscrcpy)
self.devices_view.itemClicked.connect(self.more_options_device_view)
self.devices_view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.scan_config_devices_update_list_view()
self.refresh_devices()
@property
def adb(self) -> AndroidDebugBridge:
return self._adb
@property
def scrcpy(self) -> ScrcpyBridge:
return self._scrcpy
@property
def config(self) -> dict:
return self._config
def restart_adb_server_guiscrcpy(self):
self.adb.kill_adb_server()
def refresh_devices(self):
"""
A slot for refreshing the QListView
:return:
"""
self.scan_devices_update_list_view()
def update_rotation_combo_cb(self):
"""
A proposed method for refreshing the rotation combobox on item
change in the QListBox
:return:
"""
if self.devices_view.currentItem():
_, device_id = self.current_device_identifier()
_rotation = (
self.config.get("device")
.get(device_id, dict())
.get("rotation", self.device_rotation.currentIndex())
)
else:
_rotation = self.config.get("rotation", self.device_rotation.currentIndex())
self.device_rotation.setCurrentIndex(_rotation)
def settings_mgr(self):
from guiscrcpy.ux.settings import InterfaceSettings
self.sm = InterfaceSettings(self)
self.sm.init()
self.sm.show()
def network_mgr(self):
from guiscrcpy.ux.network import InterfaceNetwork
self.nm = InterfaceNetwork(self.adb)
self.nm.init()
self.nm.show()
def bootstrap_mapper(self):
mapper_config_path = os.path.join(
self.config_manager.get_cfgpath(), "guiscrcpy.mapper.json"
)
if os.path.exists(mapper_config_path):
from guiscrcpy.lib.mapper.mapper import MapperAsync
_, identifier = self.current_device_identifier()
self.mp = MapperAsync(
self,
device_id=identifier,
config_path=mapper_config_path,
adb=self.adb,
initialize=False,
)
self.mp.start()
self.private_message_box_adb.setText("guiscrcpy-mapper has started")
else:
message_box = QMessageBox()
message_box.setText(
"guiscrcpy mapper is not initialized yet. Do you want to "
"initialize it now?"
)
message_box.setInformativeText(
"Before you initialize, make sure your phone is connected and "
"the display is switched on to map the points."
)
message_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
user_message_box_response = message_box.exec()
values_devices_list = self.scan_devices_update_list_view()
self.check_devices_status_and_select_first_if_only_one(
values_devices_list=values_devices_list
)
# TODO: allow enabling mapper from inside
if user_message_box_response == QMessageBox.Yes:
self.private_message_box_adb.setText("Initializing mapper...")
print("Make sure your phone is connected and display is " "switched on")
print(
"Reset mapper if you missed any "
"steps by 'guiscrcpy --mapper-reset'"
)
print()
print(
"If at first you don't succeed... "
"reset, reset and reset again! :D"
)
print()
_, identifier = self.current_device_identifier()
executable = get_self()
from .lib.utils import shellify as sx, open_process
open_process(
sx("{} mapper".format(executable)),
stdout=sys.stdout,
stdin=sys.stdin,
stderr=sys.stderr,
cwd=os.getcwd(),
)
print("Mapper started")
self.private_message_box_adb.setText("Mapper initialized")
def launch_usb_audio(self):
android_api_level = self.adb.get_target_android_version()
if android_api_level == -1:
return
print("Detected device API level: " + str(android_api_level))
if android_api_level >= 29:
print("Using Sndcpy as audio bridge")
audio_bridge = SndcpyBridge()
else:
print("Using USBAudio as audio bridge")
audio_bridge = USBAudioBridge()
# FIXME: provide the right device id
audio_bridge.run(device_id=None)
@staticmethod
def launch_web_github():
webbrowser.open("https://github.com/srevinsaju/guiscrcpy")
def about(self):
"""
Reset message box is based on aboutWindow object
For some reason, I did not get time to fix that
:return:
"""
about_message_box = QMessageBox().window()
about_message_box.about(
self.pushButton,
"Info",
"Please restart guiscrcpy to reset the settings. "
"guiscrcpy will now exit",
)
about_message_box.addButton("OK", about_message_box.hide) # noqa:
about_message_box.show()
def reset(self):
"""
Remove configuration files; Reset the mapper and guiscrcpy.json
:return:
"""
self.config_manager.reset_config()
log("CONFIGURATION FILE REMOVED SUCCESSFULLY")
log("RESTART")
message_box = QMessageBox().window()
message_box.about(
self.pushButton,
"Info",
"Please restart guiscrcpy to reset the settings. "
"guiscrcpy will now exit",
)
QMessageBox.ButtonRole()
message_box.addButton("OK", self.quit_window) # noqa:
message_box.show()
def quit_window(self):
"""
A method to quit the main window
:return:
"""
try:
self.mp.exit()
except (AttributeError, KeyboardInterrupt):
pass
sys.exit()
def forget_paired_device(self):
"""
Forgets / Removes the configuration for saved
:return: popped item / False
"""
try:
_, identifier = self.current_device_identifier()
popped_device = self.config["device"].pop(identifier)
self.refresh_devices()
self.config_manager.update_config(self.config)
self.config_manager.write_file()
return popped_device
except KeyError:
return False
def more_options_device_view(self, button):
if "Disconnect" in button.text():
menu = QMenu("Menu", self)
menu.addAction("Pair / Ping", self.ping_paired_device)
menu.addAction("Attempt TCPIP on device", self.tcpip_paired_device)
menu.addAction("Forget device", self.forget_paired_device)
else:
menu = QMenu("Menu", self)
menu.addAction("Attempt TCPIP on device", self.tcpip_paired_device)
menu.addAction("Attempt reconnection", self.ping_paired_device)
menu.addAction("Refresh", self.refresh_devices)
_, identifier = self.current_device_identifier()
if platform.System.system() == "Linux" and identifier.count(".") >= 3:
menu.addAction(
"Add Desktop Shortcut to this device",
self.create_desktop_shortcut_linux_os,
)
menu.exec_(
self.devices_view.mapToGlobal(
QPoint(
self.devices_view.visualItemRect(button).x() + 22,
self.devices_view.visualItemRect(button).y() + 22,
)
)
)
def create_desktop_shortcut_linux_os(self) -> bool:
"""
Creates a desktop shortcut for Linux OS
:return: bool
"""
# just a check before anything further happens because of an
# unrelated OS
if environment.system() != "Linux":
log(
"Tried to run create_desktop_shortcut_linux_os on an " "unsupported OS."
)
return False
# get device specific configuration
model, identifier = self.current_device_identifier()
picture_file_path = self.config_manager.get_cfgpath()
__sha_shift = self.config.get("sha_shift", 5)
sha = hashlib.sha256(str(identifier).encode()).hexdigest()[
__sha_shift : __sha_shift + 6
]
log(f"Creating desktop shortcut sha: {sha}")
path_to_image = os.path.join(picture_file_path, identifier + ".png")
if has_cairo:
svg2png(
bytestring=desktop_device_shortcut_svg().format(f"#{sha}"),
write_to=path_to_image,
)
else:
print("Trying to use Plain SVG as renderer" " instead of cairo")
with open(path_to_image, "w") as fp:
svg_str = desktop_device_shortcut_svg().format(f"#{sha}")
fp.write(svg_str)
# go through all args; break when we find guiscrcpy
for args_i in range(len(sys.argv)):
if "guiscrcpy" in sys.argv[args_i]:
aend = args_i + 1
break
else:
aend = None
sys_args_desktop = sys.argv[:aend]
# check if its a python file
# experimental support for AppImages / snaps
# I am not sure; if it would work indeed
for i in sys_args_desktop:
if i.endswith(".py"):
needs_python = True
break
else:
needs_python = False
if needs_python:
sys_args_desktop = ["python3"] + sys_args_desktop
# convert the list into a string
sys_args_desktop = " ".join(sys_args_desktop)
auto_connect_run_command = (
"{executable} --connect={ip} "
"--start --start-scrcpy-device-id={ip}".format(
executable=sys_args_desktop, ip=identifier
)
)
# create the desktop file using linux's desktop file gen method
path_to_desktop_file = platform.System().create_desktop(
desktop_file=GUISCRCPY_DEVICE.format(
identifier=model,
command=auto_connect_run_command,
icon_path=path_to_image,
),
desktop_file_name=f"{model}.guiscrcpy.desktop",
)
# announce it to developers / users
log(f"Path to desktop file : {path_to_desktop_file}")
print("Desktop file generated successfully")
self.display_public_message("Desktop file has been created")
return True
def is_connection_success_handler(self, output: Popen, ip=None):
out, err = output.communicate()
if "failed" in out.decode() or "failed" in err.decode():
self.display_public_message(
"Failed to connect to {}. See the logs for more "
"information".format(ip)
)
print("adb:", out.decode(), err.decode())
else:
self.display_public_message("Connection command completed successfully")
def ping_paired_device(self, device_id=None):
# update the configuration file first
if not device_id:
_, identifier = self.current_device_identifier()
if identifier.count(".") == 3:
wifi_device = True
else:
wifi_device = False
try:
self.config["device"][identifier]["wifi"] = wifi_device
except KeyError:
log(f"Failed writing the configuration " f"'wifi' key to {identifier}")
if wifi_device:
ip = self.current_device_identifier()[1]
output = self.adb.command("connect {}".format(ip))
self.is_connection_success_handler(output, ip=ip)
else:
self.adb.command("reconnect offline")
# As we have attempted to connect; refresh the panel
self.refresh_devices()
else:
output = self.adb.command("connect {}".format(device_id))
self.is_connection_success_handler(output, ip=device_id)
def tcpip_paired_device(self):
if self.devices_view.currentItem():
_, identifier = self.current_device_identifier()
else:
identifier = ""
__exit_code = self.adb.tcpip(identifier=identifier)
if __exit_code != 0:
self.display_public_message(
"TCP/IP failed on device. " "Please reconnect USB and try again"
)
else:
self.display_public_message("TCP/IP completed successfully.")
time.sleep(0.1) # wait for everything to get settled
if identifier.count(".") >= 3:
self.ping_paired_device(device_id=identifier)
else:
self.ping_paired_device()
def current_device_identifier(self, need_status=False):
if self.devices_view.currentItem():
if need_status:
return (
self.devices_view.currentItem().text().split()[0],
self.devices_view.currentItem().text().split()[1],
self.devices_view.currentItem().text().split()[2],
)
else:
return (
self.devices_view.currentItem().text().split()[0],
self.devices_view.currentItem().text().split()[1],
)
else:
raise ValueError("No item is selected in QListView")
def scan_config_devices_update_list_view(self):
"""
Scans for saved devices
:return:
"""
self.devices_view.clear()
paired_devices = self.config["device"]
for i in paired_devices:
if paired_devices[i].get("wifi"):
icon = ":/icons/icons/portrait_mobile_disconnect.svg"
devices_view_list_item = QListWidgetItem(
QIcon(icon),
"{device}\n{mode}\n{status}".format(
device=paired_devices[i].get("model"),
mode=i,
status="Disconnected",
),
)
__sha_shift = self.config.get("sha_shift", 5)
__sha = hashlib.sha256(str(i).encode()).hexdigest()[
__sha_shift : __sha_shift + 6
]
devices_view_list_item.setToolTip(
"<span style='color: #{color}'>Device</snap>: <b>{d}</b>\n"
"Status: {s}".format(
d=i,
s="Disconnected. Right click 'ping' to attempt " "reconnect",
color=__sha,
)
)
devices_view_list_item.setFont(QFont("Noto Sans", 8))
self.devices_view.addItem(devices_view_list_item)
return paired_devices
def scan_devices_update_list_view(self):
"""
Scan for new devices; and update the list view
:return:
"""
# self.devices_view.clear()
device_exists_in_view = False
paired_devices = []
for index in range(self.devices_view.count()):
paired_devices.append(self.devices_view.item(index))
__devices = self.adb.devices_detailed()
log(__devices)
for i in __devices:
device_is_wifi = i["identifier"].count(".") >= 3 and (
":" in i["identifier"]
)
if i["identifier"] not in self.config["device"].keys():
device_paired_and_exists = False
self.config["device"][i["identifier"]] = {"rotation": 0}
else:
device_paired_and_exists = True
if device_is_wifi:
_icon_suffix = "_wifi"
else:
_icon_suffix = "_usb"
icon = ":/icons/icons/portrait_mobile_white{}.svg".format(_icon_suffix)
if i["status"] == "offline":
icon = ":/icons/icons/portrait_mobile_error.svg"
elif i["status"] == "unauthorized":
icon = ":/icons/icons/portrait_mobile_warning.svg"
if i["status"] == "no_permission":
log("pairfilter: 5")
# https://stackoverflow.com/questions/
# 53887322/adb-devices-no-permissions-user-in-
# plugdev-group-are-your-udev-rules-wrong
udev_error = (
"Error connecting to device. Your udev rules are"
" incorrect. See https://stackoverflow.com/questions"
"/53887322/adb-devices-no-permissions-user-in-plugdev-"
"group-are-your-udev-rules-wrong"
)
self.display_public_message(udev_error)
print(udev_error)
return []
# Check if device is unauthorized
elif i["status"] == "unauthorized":
log("unauthorized device detected: Click Allow on your device")
log("pairfilter: 4")
# The device is connected; and might/might't paired in the past
# And is connected to the same IP address
# It is possibly a bug with the connection;
# Temporarily create a new QListItem to display the
# device with the error
paired = False
device_paired_and_exists = False
self.display_public_message(
f"{i['identifier']} is unauthorized. Please click allow "
f"on your device."
)
# Remove other devices with the same id and offline and
# unauthorized
self.remove_device_device_view(
i["identifier"], statuses=["offline", "unauthorized"]
)
# Unauthorized device cannot be considered as a paired device
devices_view_list_item = QListWidgetItem()
else:
# check if device is paired
# if yes, just update the list item
if not device_paired_and_exists:
paired = False
devices_view_list_item = QListWidgetItem()
else:
for paired_device in paired_devices:
if paired_device.text().split()[0] == i["model"]:
log("pairfilter: 1")
paired = True
devices_view_list_item = paired_device
# as we have found a paired device
# we know by assumption; there cannot be two
# devices with the same local IP address;
# lets scan the devices_view once more in a loop
# to check for any device with the same
# identifier and remove them; based on this same
# assumption
self.remove_device_device_view(
i["identifier"], statuses=["offline", "unauthorized"]
)
break
elif paired_device.text().split()[1] == i["identifier"]:
log("pairfilter: 2")
self.remove_device_device_view(
i["identifier"], statuses=["offline", "unauthorized"]
)
devices_view_list_item = QListWidgetItem()
paired = False
break
else:
log("pairfilter: 3")
paired = False
devices_view_list_item = QListWidgetItem()
devices_view_list_item.setIcon(QIcon(icon))
devices_view_list_item.setText(
"{device}\n{mode}\n{status}".format(
device=i["model"], mode=i["identifier"], status=i["status"]
)
)
__sha_shift = self.config.get("sha_shift", 5)
__sha = hashlib.sha256(str(i["identifier"]).encode()).hexdigest()[
__sha_shift : __sha_shift + 6
]
devices_view_list_item.setToolTip(
"Device: "
"<span style='color: #{inv_color};background-color: #{color}'>"
"<b>{d}</b></span>\n"
"<br>"
"Model: {m}\n<br>"
"Alias: {a}\n<br>"
"Status: {s}\n<br>"
"Transport ID: {t}\n<br>"
"Paired: {p}".format(
d=i["identifier"],
m=i["model"],
a=i["product"],
s=i["status"],
t=i["transport_id"],
p=paired,
color=__sha,
inv_color=str(hex(0xFFFFFF - int(__sha, 16))[2:]),
)
)
devices_view_list_item.setFont(QFont("Noto Sans", 8))
log(f"Pairing status: {device_paired_and_exists}")
if device_paired_and_exists and device_is_wifi:
# we need to only neglect wifi devices
# paired usb device need to still show in the display
continue
elif device_exists_in_view:
# devices exists in the list with the same status and
# we should not add the new detected list item
continue
# If and only if the device doesn't exist; add it
self.devices_view.addItem(devices_view_list_item)
return __devices
def remove_device_device_view(self, identifier: str = "", statuses=()):
"""
Removes all QListWidgetItems from the device_view for all matching
identifier
:param identifier: str
:param statuses: Iterable
:return:
"""
for index in range(self.devices_view.count() - 1, -1, -1):
for status in statuses:
if self.devices_view.item(index):
if (
str(identifier) in self.devices_view.item(index).text()
and str(status) in self.devices_view.item(index).text()
):
self.devices_view.takeItem(index)
return
def __dimension_change_cb(self):
if self.dimensionDefaultCheckbox.isChecked():
self.dimensionSlider.setEnabled(False)
self.config["dimension"] = None
self.dimensionText.setInputMask("")
self.dimensionText.setText("DEFAULT")
else:
self.dimensionSlider.setEnabled(True)
self.config["dimension"] = int(self.dimensionSlider.value())
self.dimensionText.setText(" " + str(self.config["dimension"]) + "px")
self.dimensionSlider.sliderMoved.connect(self.__slider_change_cb)
self.dimensionSlider.sliderReleased.connect(self.__slider_change_cb)
def __slider_change_cb(self):
self.config["dimension"] = int(self.dimensionSlider.value())
self.dimensionText.setText(str(self.config["dimension"]) + "px")
def __dial_change_cb(self):
self.config["bitrate"] = int(self.dial.value())
self.bitrateText.setText(str(self.config["bitrate"]) + "KB/s")
def progress(self, val):
self.progressBar.setValue(int(val))
if (val + 4) >= 100:
return 100
else:
return val + 100 / 20
@staticmethod
def is_device_unusable(status):
if any(("unauth" in status, "offline" in status)):
return True
else:
return False
def show_device_status_failure(self, status):
self.display_public_message(
f"Device is {status}. Please reconnect / press allow."
)
def __reset_message_box_stylesheet(self):
stylesheet = (
"background-color: qlineargradient("
"spread:pad, x1:0, y1:0, x2:1, y2:1, "
"stop:0 rgba(0, 255, 255, 255), "
"stop:1 rgba(0, 255, 152, 255)); "
"border-radius: 10px;"
)
self.private_message_box_adb.setStyleSheet(stylesheet)
def __select_first_device(self):
self.devices_view.setCurrentIndex(
QModelIndex(self.devices_view.model().index(0, 0))
)
def display_public_message(self, message):
"""
sets the message box information (GUI) with the string message
:param message: str
:return: None
"""
self.private_message_box_adb.setText(message)
def check_devices_status_and_select_first_if_only_one(self, values_devices_list):
"""
Checks the devices in the Grid View, and then checks if any device
is available or offline accordingly display the error message. If
only one device was detected, automatically select the first device
and check its status of connectivity. Return the selected device if
multiple devices were detected by adb. Return device_id, status and
a bool more_devices if more than one device was found
:param values_devices_list:
:type values_devices_list:
:return:
:rtype:
"""
if len(values_devices_list) == 0:
# Could not detect any device
self.display_public_message("Could not find any devices")
return 0
elif self.devices_view.currentIndex() is None and len(values_devices_list) != 1:
# No device is selected and more than one device found
self.display_public_message("Please select a device below.")
return 0
else:
# Device selected
log("DEVICE LIST", len(values_devices_list), values_devices_list)
if len(values_devices_list) == 1:
# found only one device
# ======================================================
# Store the current rotation temporarily
__selected_rotation = self.device_rotation.currentIndex()
self.__select_first_device()
# Restore the selected rotation
self.device_rotation.setCurrentIndex(__selected_rotation)
# =======================================================
# get the status and identifier of the device;
# return if device is not in a connectable state
try:
_, device_id, _stat = self.current_device_identifier(
need_status=True
)
if self.is_device_unusable(_stat):
self.show_device_status_failure(_stat)
return 0
except ValueError:
self.display_public_message(
"Please select a device from the list view"
)
return 0
more_devices = False
elif self.devices_view.currentItem() is None:
# no item is selected
self.display_public_message("Please select a device below.")
return 0
else:
_, device_id, _stat = self.current_device_identifier(need_status=True)
if self.is_device_unusable(_stat):
self.show_device_status_failure(_stat)
return 0
log("Device_id = {}".format(device_id))
more_devices = True
return device_id, more_devices, _stat
def start_act(self):
"""
Main brain of guiscrcpy; handles what to do when
:return:
"""
# prepare launch of scrcpy,
# reset colors
# reset vars
# 1: reset
self.options = ""
progress = self.progress(0)
self.__reset_message_box_stylesheet()
# ====================================================================
# 2: Update UI to start checking
self.display_public_message("CHECKING DEVICE CONNECTION")
initial_time = time.time()
progress = self.progress(progress)
# ====================================================================
# 3: Check devices
values_devices_list = self.scan_devices_update_list_view()
_e = self.check_devices_status_and_select_first_if_only_one(values_devices_list)
if _e is None or isinstance(_e, int):
return _e
device_id, more_devices, _stat = _e
progress = self.progress(progress)
# ====================================================================
# 4: Parse dimension slider
# check if the defaultDimension is checked or not for giving signal
if self.dimensionDefaultCheckbox.isChecked():
self.dimensionSlider.setEnabled(False)
self.dimensionText.setText("DEFAULT")
self.config["dimension"] = None
else:
self.dimensionSlider.setEnabled(True)
self.config["dimension"] = int(self.dimensionSlider.value())
self.dimensionSlider.setValue(self.config["dimension"])
self.dimensionText.setText(str(self.config["dimension"]) + "px")
# edit configuration files to update dimension key
if self.config["dimension"] is None:
self.options = " "
elif self.config["dimension"] is not None:
self.options = " -m " + str(self.config["dimension"])
else:
self.options = ""
progress = self.progress(progress)
# ====================================================================
# 5: Check if always_on and fullscreen switches are on
if self.aotop.isChecked():
self.options += " --always-on-top"
if self.fullscreen.isChecked():
self.options += " -f"
self.config["fullscreen"] = True
else:
self.config["fullscreen"] = False
progress = self.progress(progress)
# ====================================================================
# 6: Check if show touches / recording are on
if self.showTouches.isChecked():
self.options += " --show-touches"
self.config["swtouches"] = True
else:
self.config["swtouches"] = False
progress = self.progress(progress)
# ====================================================================
# 7: Check if the record option is selected
if self.recScui.isChecked():
self.options += " -r " + str(int(time.time())) + ".mp4 "
progress = self.progress(progress)
# ====================================================================
# 8: Check if the display is forced to be on
if self.displayForceOn.isChecked():
self.options += " -S"
self.config["dispRO"] = True
else:
self.config["dispRO"] = False
progress = self.progress(progress)