Skip to content

Commit 0e23fba

Browse files
committed
helper: Use pyscreenshot for cross-platform QR capture
pyscreenshot supports capturing screenshots via numerous backends and can automatically select the best one depending on platform. This enables capturing of the QR code on wayland-based desktops while maintaining support for Windows and X11
1 parent f5100e1 commit 0e23fba

File tree

5 files changed

+64
-64
lines changed

5 files changed

+64
-64
lines changed

build-helper.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ rmdir /s /q ..\build\windows\helper-license-venv
1111
poetry build || goto :error
1212
poetry run python -m venv ..\build\windows\helper-license-venv || goto :error
1313
..\build\windows\helper-license-venv\Scripts\python -m pip install --upgrade pip wheel || goto :error
14-
..\build\windows\helper-license-venv\Scripts\python -m pip install dist\authenticator_helper-0.1.0-py3-none-any.whl pip-licenses || goto :error
14+
..\build\windows\helper-license-venv\Scripts\python -m pip install dist\authenticator_helper-0.2.0-py3-none-any.whl pip-licenses || goto :error
1515
..\build\windows\helper-license-venv\Scripts\pip-licenses --format=json --no-license-path --with-license-file --ignore-packages authenticator-helper zxing-cpp --output-file ..\assets\licenses\helper.json || goto :error
1616

1717
cd ..

build-helper.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ VENV="../$OUTPUT/helper-license-venv"
4040
rm -rf $VENV
4141
poetry run python -m venv $VENV
4242
$VENV/bin/pip install --upgrade pip wheel
43-
$VENV/bin/pip install dist/authenticator_helper-0.1.0-py3-none-any.whl pip-licenses
43+
$VENV/bin/pip install dist/authenticator_helper-0.2.0-py3-none-any.whl pip-licenses
4444
$VENV/bin/pip-licenses --format=json --no-license-path --with-license-file --ignore-packages authenticator-helper zxing-cpp --output-file ../assets/licenses/helper.json
4545
cd ..
4646

helper/helper/qr.py

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,24 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import mss
1615
import zxingcpp
1716
import base64
1817
import io
19-
import os
20-
import sys
21-
import subprocess # nosec
22-
import tempfile
23-
from mss.exception import ScreenShotError
18+
import pyscreenshot as ImageGrab
19+
from pyscreenshot import FailedBackendError
2420
from PIL import Image
25-
import numpy.core.multiarray # noqa
26-
27-
28-
def _capture_screen():
29-
try:
30-
with mss.mss() as sct:
31-
monitor = sct.monitors[0] # 0 is the special "all monitors" value.
32-
sct_img = sct.grab(monitor) # mss format
33-
return Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
34-
except ScreenShotError:
35-
# One common error is that mss doesn't work with Wayland
36-
if sys.platform.startswith("linux"):
37-
# Try calling screenshot tools, with original library path
38-
env = dict(os.environ)
39-
lp = env.get("LD_LIBRARY_PATH_ORIG")
40-
if lp is not None:
41-
env["LD_LIBRARY_PATH"] = lp
42-
else:
43-
env.pop("LD_LIBRARY_PATH", None)
44-
fd, fname = tempfile.mkstemp(suffix=".png")
45-
46-
try:
47-
# Try using gnome-screenshot
48-
rc = subprocess.call( # nosec
49-
["gnome-screenshot", "-f", fname], env=env
50-
)
51-
if rc == 0:
52-
return Image.open(fname)
53-
except FileNotFoundError:
54-
# Try using spectacle (KDE)
55-
try:
56-
rc = subprocess.call( # nosec
57-
["spectacle", "-b", "-n", "-o", fname], env=env
58-
)
59-
if rc == 0:
60-
return Image.open(fname)
61-
except FileNotFoundError:
62-
pass # Fall through to ValueError
63-
finally:
64-
os.unlink(fname)
65-
raise ValueError("Unable to capture screenshot")
66-
6721

6822
def scan_qr(image_data=None):
23+
img = None
6924
if image_data:
7025
msg = base64.b64decode(image_data)
7126
buf = io.BytesIO(msg)
7227
img = Image.open(buf)
7328
else:
74-
img = _capture_screen()
29+
try:
30+
img = ImageGrab.grab()
31+
except FailedBackendError:
32+
raise ValueError("Unable to capture screenshot")
7533

7634
result = zxingcpp.read_barcode(img)
7735
if result and result.valid:

helper/poetry.lock

Lines changed: 53 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helper/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "authenticator-helper"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
description = "Yubico Authenticator Helper"
55
authors = ["Dain Nilsson <[email protected]>"]
66
packages = [
@@ -11,9 +11,9 @@ packages = [
1111
[tool.poetry.dependencies]
1212
python = "^3.8"
1313
yubikey-manager = "5.1.0"
14-
mss = "^8.0.3"
1514
zxing-cpp = "^2.0.0"
1615
Pillow = "^9.5.0"
16+
pyscreenshot = "^3.1"
1717

1818
[tool.poetry.dev-dependencies]
1919
pyinstaller = {version = "^5.10.1", python = "<3.12"}

0 commit comments

Comments
 (0)