Skip to content

Commit

Permalink
support multi display, disable framebuffer function
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Apr 28, 2024
1 parent aa97ff6 commit 2d6143d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
36 changes: 24 additions & 12 deletions adbutils/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,38 @@ class ScreenshotExtesion(AbstractDevice):
def __init__(self):
self.__framebuffer_ok = True

def screenshot(self) -> Image.Image:
def screenshot(self, display_id: Optional[int] = None) -> Image.Image:
""" Take a screenshot and return PIL.Image.Image object
If capture failed, return a black image
Args:
display_id: int, default None, see "dumpsys SurfaceFlinger --display-id" for valid display IDs
Returns:
PIL.Image.Image object, If capture failed, return a black image
"""
try:
pil_image = self.__screencap()
pil_image = self.__screencap(display_id)
if pil_image.mode == "RGBA":
pil_image = pil_image.convert("RGB")
return pil_image
except UnidentifiedImageError as e:
wsize = self.window_size()
return Image.new("RGB", wsize, (0, 0, 0))

def __screencap(self) -> Image.Image:
if self.__framebuffer_ok:
try:
return self.framebuffer()
except NotImplementedError:
self.__framebuffer_ok = False
except UnidentifiedImageError as e:
logger.warning("framebuffer error: %s", e)
png_bytes = self.shell('screencap -p', encoding=None)
def __screencap(self, display_id: int = None) -> Image.Image:
""" Take a screenshot and return PIL.Image.Image object
"""
# framebuffer is not stable, so we disable it
# MemoryError may occur when using framebuffer

# if self.__framebuffer_ok and display_id is None:
# try:
# return self.framebuffer()
# except NotImplementedError:
# self.__framebuffer_ok = False
# except UnidentifiedImageError as e:
# logger.warning("framebuffer error: %s", e)
cmdargs = ['screencap', '-p']
if display_id is not None:
cmdargs.extend(['-d', str(display_id)])
png_bytes = self.shell(cmdargs, encoding=None)
return Image.open(io.BytesIO(png_bytes))
8 changes: 6 additions & 2 deletions adbutils/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,23 @@ def swipe(self, sx, sy, ex, ey, duration: float = 1.0) -> None:
x1, y1, x2, y2 = map(str, [sx, sy, ex, ey])
self.shell(["input", "swipe", x1, y1, x2, y2, str(int(duration * 1000))])

def click(self, x, y) -> None:
def click(self, x, y, display_id: Optional[int] = None) -> None:
"""
simulate android tap
Args:
x, y: int
display_id: int, default None, see "dumpsys SurfaceFlinger --display-id" for valid display IDs
"""
if any(map(is_percent, [x, y])):
w, h = self.window_size()
x = int(x * w) if is_percent(x) else x
y = int(y * h) if is_percent(y) else y
x, y = map(str, [x, y])
self.shell(["input", "tap", x, y])
cmdargs = ["input"]
if display_id is not None:
cmdargs.extend(['-d', str(display_id)])
self.shell(cmdargs + ["tap", x, y])

def send_keys(self, text: str):
"""
Expand Down

0 comments on commit 2d6143d

Please sign in to comment.