@@ -13974,12 +13974,82 @@ def __activate_standard_virtual_display(self):
1397413974            self.headless_active = True
1397513975            sb_config.headless_active = True
1397613976
13977+     def __activate_virtual_display(self):
13978+         if self.undetectable and not (self.headless or self.headless2):
13979+             from sbvirtualdisplay import Display
13980+             import Xlib.display
13981+             try:
13982+                 if not self._xvfb_width:
13983+                     self._xvfb_width = 1366
13984+                 if not self._xvfb_height:
13985+                     self._xvfb_height = 768
13986+                 self._xvfb_display = Display(
13987+                     visible=True,
13988+                     size=(self._xvfb_width, self._xvfb_height),
13989+                     backend="xvfb",
13990+                     use_xauth=True,
13991+                 )
13992+                 self._xvfb_display.start()
13993+                 if "DISPLAY" not in os.environ.keys():
13994+                     print(
13995+                         "\nX11 display failed! Will use regular xvfb!"
13996+                     )
13997+                     self.__activate_standard_virtual_display()
13998+             except Exception as e:
13999+                 if hasattr(e, "msg"):
14000+                     print("\n" + str(e.msg))
14001+                 else:
14002+                     print(e)
14003+                 print("\nX11 display failed! Will use regular xvfb!")
14004+                 self.__activate_standard_virtual_display()
14005+                 return
14006+             pyautogui_is_installed = False
14007+             try:
14008+                 import pyautogui
14009+                 with suppress(Exception):
14010+                     use_pyautogui_ver = constants.PyAutoGUI.VER
14011+                     if pyautogui.__version__ != use_pyautogui_ver:
14012+                         del pyautogui  # To get newer ver
14013+                         shared_utils.pip_install(
14014+                             "pyautogui", version=use_pyautogui_ver
14015+                         )
14016+                         import pyautogui
14017+                 pyautogui_is_installed = True
14018+             except Exception:
14019+                 message = (
14020+                     "PyAutoGUI is required for UC Mode on Linux! "
14021+                     "Installing now..."
14022+                 )
14023+                 print("\n" + message)
14024+                 shared_utils.pip_install(
14025+                     "pyautogui", version=constants.PyAutoGUI.VER
14026+                 )
14027+                 import pyautogui
14028+                 pyautogui_is_installed = True
14029+             if (
14030+                 pyautogui_is_installed
14031+                 and hasattr(pyautogui, "_pyautogui_x11")
14032+             ):
14033+                 try:
14034+                     pyautogui._pyautogui_x11._display = (
14035+                         Xlib.display.Display(os.environ['DISPLAY'])
14036+                     )
14037+                     sb_config._pyautogui_x11_display = (
14038+                         pyautogui._pyautogui_x11._display
14039+                     )
14040+                 except Exception as e:
14041+                     if hasattr(e, "msg"):
14042+                         print("\n" + str(e.msg))
14043+                     else:
14044+                         print(e)
14045+         else:
14046+             self.__activate_standard_virtual_display()
14047+ 
1397714048    def __activate_virtual_display_as_needed(self):
1397814049        """This is only needed on Linux.
1397914050        The "--xvfb" arg is still useful, as it prevents headless mode,
1398014051        which is the default mode on Linux unless using another arg."""
1398114052        if "linux" in sys.platform and (not self.headed or self.xvfb):
13982-             from sbvirtualdisplay import Display
1398314053            pip_find_lock = fasteners.InterProcessLock(
1398414054                constants.PipInstall.FINDLOCK
1398514055            )
@@ -13992,75 +14062,15 @@ def __activate_virtual_display_as_needed(self):
1399214062                    mode = os.stat(constants.PipInstall.FINDLOCK).st_mode
1399314063                    mode |= (mode & 0o444) >> 1  # copy R bits to W
1399414064                    os.chmod(constants.PipInstall.FINDLOCK, mode)
14065+                 try:
14066+                     with pip_find_lock:
14067+                         pass
14068+                 except Exception:
14069+                     # Since missing permissions, skip the locks
14070+                     self.__activate_virtual_display()
14071+                     return
1399514072            with pip_find_lock:  # Prevent issues with multiple processes
13996-                 if self.undetectable and not (self.headless or self.headless2):
13997-                     import Xlib.display
13998-                     try:
13999-                         if not self._xvfb_width:
14000-                             self._xvfb_width = 1366
14001-                         if not self._xvfb_height:
14002-                             self._xvfb_height = 768
14003-                         self._xvfb_display = Display(
14004-                             visible=True,
14005-                             size=(self._xvfb_width, self._xvfb_height),
14006-                             backend="xvfb",
14007-                             use_xauth=True,
14008-                         )
14009-                         self._xvfb_display.start()
14010-                         if "DISPLAY" not in os.environ.keys():
14011-                             print(
14012-                                 "\nX11 display failed! Will use regular xvfb!"
14013-                             )
14014-                             self.__activate_standard_virtual_display()
14015-                     except Exception as e:
14016-                         if hasattr(e, "msg"):
14017-                             print("\n" + str(e.msg))
14018-                         else:
14019-                             print(e)
14020-                         print("\nX11 display failed! Will use regular xvfb!")
14021-                         self.__activate_standard_virtual_display()
14022-                         return
14023-                     pyautogui_is_installed = False
14024-                     try:
14025-                         import pyautogui
14026-                         with suppress(Exception):
14027-                             use_pyautogui_ver = constants.PyAutoGUI.VER
14028-                             if pyautogui.__version__ != use_pyautogui_ver:
14029-                                 del pyautogui  # To get newer ver
14030-                                 shared_utils.pip_install(
14031-                                     "pyautogui", version=use_pyautogui_ver
14032-                                 )
14033-                                 import pyautogui
14034-                         pyautogui_is_installed = True
14035-                     except Exception:
14036-                         message = (
14037-                             "PyAutoGUI is required for UC Mode on Linux! "
14038-                             "Installing now..."
14039-                         )
14040-                         print("\n" + message)
14041-                         shared_utils.pip_install(
14042-                             "pyautogui", version=constants.PyAutoGUI.VER
14043-                         )
14044-                         import pyautogui
14045-                         pyautogui_is_installed = True
14046-                     if (
14047-                         pyautogui_is_installed
14048-                         and hasattr(pyautogui, "_pyautogui_x11")
14049-                     ):
14050-                         try:
14051-                             pyautogui._pyautogui_x11._display = (
14052-                                 Xlib.display.Display(os.environ['DISPLAY'])
14053-                             )
14054-                             sb_config._pyautogui_x11_display = (
14055-                                 pyautogui._pyautogui_x11._display
14056-                             )
14057-                         except Exception as e:
14058-                             if hasattr(e, "msg"):
14059-                                 print("\n" + str(e.msg))
14060-                             else:
14061-                                 print(e)
14062-                 else:
14063-                     self.__activate_standard_virtual_display()
14073+                 self.__activate_virtual_display()
1406414074
1406514075    def __ad_block_as_needed(self):
1406614076        """This is an internal method for handling ad-blocking.
0 commit comments