From 3757510ecd22223daf94ae6bee91656af1190601 Mon Sep 17 00:00:00 2001 From: LmeSzinc <37934724+LmeSzinc@users.noreply.github.com> Date: Sun, 21 Apr 2024 23:36:23 +0800 Subject: [PATCH] Fix: [ALAS] Re-init MaaTouch when orientation changed --- module/device/device.py | 10 ++++++++++ module/device/method/maatouch.py | 31 ++++++++++++++++++++++++++++++- module/device/method/minitouch.py | 9 +++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/module/device/device.py b/module/device/device.py index 038b6719b..26b631323 100644 --- a/module/device/device.py +++ b/module/device/device.py @@ -183,6 +183,16 @@ class Device(Screenshot, Control, AppControl): if self.config.Emulator_ScreenshotMethod == 'nemu_ipc': self.nemu_ipc_release() + def get_orientation(self): + """ + Callbacks when orientation changed. + """ + o = super().get_orientation() + + self.on_orientation_change_maatouch() + + return o + def stuck_record_add(self, button): self.detect_record.add(str(button)) diff --git a/module/device/method/maatouch.py b/module/device/method/maatouch.py index 300e8ff9c..c168a8f1a 100644 --- a/module/device/method/maatouch.py +++ b/module/device/method/maatouch.py @@ -102,9 +102,10 @@ class MaaTouch(Connection): """ max_x: int max_y: int - _maatouch_stream = socket.socket + _maatouch_stream: socket.socket = None _maatouch_stream_storage = None _maatouch_init_thread = None + _maatouch_orientation: int = None @cached_property def _maatouch_builder(self): @@ -136,12 +137,40 @@ class MaaTouch(Connection): self._maatouch_init_thread = thread thread.start() + def on_orientation_change_maatouch(self): + """ + MaaTouch caches devices orientation at its startup + A restart is required when orientation changed + """ + if self._maatouch_orientation is None: + return + if self.orientation == self._maatouch_orientation: + return + + logger.info(f'Orientation changed {self._maatouch_orientation} => {self.orientation}, re-init MaaTouch') + del_cached_property(self, '_maatouch_builder') + self.early_maatouch_init() + def maatouch_init(self): logger.hr('MaaTouch init') max_x, max_y = 1280, 720 max_contacts = 2 max_pressure = 50 + # Try to close existing stream + if self._maatouch_stream is not None: + try: + self._maatouch_stream.close() + except Exception as e: + logger.error(e) + del self._maatouch_stream + if self._maatouch_stream_storage is not None: + del self._maatouch_stream_storage + + # MaaTouch caches devices orientation at its startup + super(MaaTouch, self).get_orientation() + self._maatouch_orientation = self.orientation + # CLASSPATH=/data/local/tmp/maatouch app_process / com.shxyke.MaaTouch.App stream = self.adb_shell( ['CLASSPATH=/data/local/tmp/maatouch', 'app_process', '/', 'com.shxyke.MaaTouch.App'], diff --git a/module/device/method/minitouch.py b/module/device/method/minitouch.py index 18a09009f..d24cfa6bb 100644 --- a/module/device/method/minitouch.py +++ b/module/device/method/minitouch.py @@ -408,6 +408,15 @@ class Minitouch(Connection): max_x, max_y = 1280, 720 max_contacts = 2 max_pressure = 50 + + # Try to close existing stream + if self._minitouch_client is not None: + try: + self._minitouch_client.close() + except Exception as e: + logger.error(e) + del self._minitouch_client + self.get_orientation() self._minitouch_port = self.adb_forward("localabstract:minitouch")