Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
fix: Disable Fullscreen, copy to device and copy from device buttons …
Browse files Browse the repository at this point in the history
…if wmctrl and xdotool is not found

#173 post
@Justinzobel suggested that it would be great if the buttons are disabled if the accompanying helper scripts, xdotool and wmctrl are not present on the host system. The shortcuts to the helper button was added as tooltip hence
  • Loading branch information
srevinsaju committed Sep 17, 2020
1 parent a09bb75 commit 026866f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
56 changes: 41 additions & 15 deletions guiscrcpy/lib/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import sys
import hashlib
import logging
import os

import shutil
import subprocess
import shlex
from guiscrcpy.platform.platform import System

if System.system() == "Windows":
Expand All @@ -36,6 +39,36 @@
getWindowsWithTitle = None


def wmctrl_xdotool_linux_send_key(key):
assert isinstance(key, str)
assert System.system() == "Linux"
wmctrl = shutil.which("wmctrl")
xdotool = shutil.which("xdotool")
if not wmctrl or not xdotool:
print('E: Could not find {} on PATH. Make sure '
'that a compatible package is installed '
'on your system for this function')
return
_proc = subprocess.Popen(shlex.split(
'wmctrl -x -a scrcpy'),
stdout=sys.stdout,
stderr=sys.stderr)
if _proc.wait() == 0:
_xdotool_proc = subprocess.Popen(shlex.split(
'xdotool key --clearmodifiers {}+{}'.format(
os.getenv('GUISCRCPY_MODIFIER') or 'alt', key)
),
stdout=sys.stdout,
stderr=sys.stderr
)
if _xdotool_proc.wait() != 0:
print("E (xdotool): Failed to send key {} to "
"scrcpy window.".format(key))
else:
print("E (wmctrl): Failed to get scrcpy window. "
"(Is scrcpy running?)")


class UXMapper:
def __init__(self, adb, device_id=None, sha_shift=5):
"""
Expand Down Expand Up @@ -100,10 +133,9 @@ def copy_devpc(self):
if self.has_modules:
scrcpywindow = getWindowsWithTitle("scrcpy")[0]
scrcpywindow.focus()
auto.hotkey("ctrl", "c")
auto.hotkey("alt", "c")
else:
os.system(
"wmctrl -x -a scrcpy && xdotool key --clearmodifiers ctrl+c")
wmctrl_xdotool_linux_send_key('c')

def key_power(self):
logging.debug("Passing POWER")
Expand Down Expand Up @@ -162,21 +194,15 @@ def copy_pc2dev(self):
if self.has_modules:
scrcpywindow = getWindowsWithTitle("scrcpy")[0]
scrcpywindow.focus()
auto.hotkey("ctrl", "shift", "c")
logging.warning("NOT SUPPORTED ON WINDOWS")
auto.hotkey("alt", "shift", "c")
else:
os.system(
"wmctrl -x -a scrcpy && "
"xdotool key --clearmodifiers ctrl+shift+c"
)
wmctrl_xdotool_linux_send_key('shift+c')

def fullscreen(self):
if self.has_modules:
scrcpy_window = getWindowsWithTitle("scrcpy")[0]
scrcpy_window.focus()
auto.hotkey("ctrl", "f")
auto.hotkey("alt", "f")
else:
os.system(
"wmctrl -x -a scrcpy && "
"xdotool key --clearmodifiers ctrl+f"
)
wmctrl_xdotool_linux_send_key('f')

23 changes: 20 additions & 3 deletions guiscrcpy/ux/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

import shutil
import uuid
import platform

from qtpy import QtCore
from qtpy.QtCore import QPoint
Expand Down Expand Up @@ -57,16 +59,31 @@ def __init__(self, ux_mapper=None, parent=None, frame=False,
self.ux = UXMapper()

def init(self):
self.clipD2PC.clicked.connect(self.ux.copy_devpc)
self.clipPC2D.clicked.connect(self.ux.copy_pc2dev)
if platform.system() != "Linux" or (shutil.which('wmctrl') and
shutil.which('xdotool') and platform.system() == "Linux"):
self.clipD2PC.clicked.connect(self.ux.copy_devpc)
self.clipPC2D.clicked.connect(self.ux.copy_pc2dev)
self.fullscreenUI.clicked.connect(self.ux.fullscreen)
else:
# the tools do not exist on Linux. user has to manually install
for button, helper in (
(self.clipD2PC, 'Alt + C'),
(self.clipPC2D, 'Alt + Shift + C'),
(self.fullscreenUI, 'Alt + F')):
button.setDisabled(True)
button.setToolTip(
'This function is disabled because "wmctrl"'
' or "xdotool" is not found on your installation.'
'Keyboard shortcut can be used instead'
': <b>{}</b>'.format(helper))

self.back.clicked.connect(self.ux.key_back)
self.screenfreeze.clicked.connect(self.quit_window)
self.appswi.clicked.connect(self.ux.key_switch)
self.menuUI.clicked.connect(self.ux.key_menu)
self.home.clicked.connect(self.ux.key_home)
self.notif_pull.clicked.connect(self.ux.expand_notifications)
self.notif_collapse.clicked.connect(self.ux.collapse_notifications)
self.fullscreenUI.clicked.connect(self.ux.fullscreen)
self.powerUI.clicked.connect(self.ux.key_power)
self.vup.clicked.connect(self.ux.key_volume_up)
self.vdown.clicked.connect(self.ux.key_volume_down)
Expand Down

0 comments on commit 026866f

Please sign in to comment.