Skip to content

Commit c1fbbaf

Browse files
Merge remote-tracking branch 'upstream/main'
2 parents e20947b + 382a509 commit c1fbbaf

File tree

6 files changed

+89
-23
lines changed

6 files changed

+89
-23
lines changed

main-ui/devices/trimui/trim_ui_brick.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def __init__(self, device_name, main_ui_mode):
2424
self.audio_player = AudioPlayerDelegateSdl2()
2525
script_dir = Path(__file__).resolve().parent
2626
source = script_dir / 'brick-system.json'
27-
ConfigCopier.ensure_config("/mnt/SDCARD/Saves/brick-system.json", source)
28-
self.system_config = SystemConfig("/mnt/SDCARD/Saves/brick-system.json")
27+
ConfigCopier.ensure_config("/mnt/SDCARD/Saves/trim-ui-brick-system.json", source)
28+
self.system_config = SystemConfig("/mnt/SDCARD/Saves/trim-ui-brick-system.json")
2929

3030
if(main_ui_mode):
3131
trim_stock_json_file = script_dir / 'stock/brick.json'

main-ui/devices/trimui/trim_ui_smart_pro.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def __init__(self, device_name,main_ui_mode):
2424

2525
script_dir = Path(__file__).resolve().parent
2626
source = script_dir / 'brick-system.json'
27-
ConfigCopier.ensure_config("/mnt/SDCARD/Saves/brick-system.json", source)
28-
self.system_config = SystemConfig("/mnt/SDCARD/Saves/brick-system.json")
27+
ConfigCopier.ensure_config("/mnt/SDCARD/Saves/trim-ui-smart-pro-system.json", source)
28+
self.system_config = SystemConfig("/mnt/SDCARD/Saves/trim-ui-smart-pro-system.json")
2929
if(main_ui_mode):
3030
trim_stock_json_file = script_dir / 'stock/brick.json'
3131
ConfigCopier.ensure_config(TrimUISmartPro.TRIMUI_STOCK_CONFIG_LOCATION, trim_stock_json_file)

main-ui/devices/trimui/trim_ui_smart_pro_s.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
import os
13
from pathlib import Path
24
import threading
35
from audio.audio_player_delegate_sdl2 import AudioPlayerDelegateSdl2
@@ -9,10 +11,13 @@
911
from devices.miyoo.system_config import SystemConfig
1012
from devices.miyoo_trim_common import MiyooTrimCommon
1113
from devices.trimui.trim_ui_device import TrimUIDevice
14+
from devices.utils.file_watcher import FileWatcher
15+
from display.display import Display
1216
from utils import throttle
1317
from utils.config_copier import ConfigCopier
1418

1519
from utils.ffmpeg_image_utils import FfmpegImageUtils
20+
from utils.logger import PyUiLogger
1621
from utils.py_ui_config import PyUiConfig
1722

1823
class TrimUISmartProS(TrimUIDevice):
@@ -24,12 +29,17 @@ def __init__(self, device_name,main_ui_mode):
2429

2530
script_dir = Path(__file__).resolve().parent
2631
source = script_dir / 'brick-system.json'
27-
ConfigCopier.ensure_config("/mnt/SDCARD/Saves/brick-system.json", source)
28-
self.system_config = SystemConfig("/mnt/SDCARD/Saves/brick-system.json")
32+
ConfigCopier.ensure_config("/mnt/SDCARD/Saves/trim-ui-smart-pro-s-system.json", source)
33+
self.system_config = SystemConfig("/mnt/SDCARD/Saves/trim-ui-smart-pro-s-system.json")
34+
self.mainui_volume = 0
35+
2936
if(main_ui_mode):
37+
self.on_mainui_config_change()
3038
trim_stock_json_file = script_dir / 'stock/brick.json'
3139
ConfigCopier.ensure_config(TrimUISmartProS.TRIMUI_STOCK_CONFIG_LOCATION, trim_stock_json_file)
3240

41+
self.mainui_config_thread, self.mainui_config_thread_stop_event = FileWatcher().start_file_watcher(
42+
TrimUISmartProS.TRIMUI_STOCK_CONFIG_LOCATION, self.on_mainui_config_change, interval=0.2, repeat_trigger_for_mtime_granularity_issues=True)
3343

3444
self.miyoo_games_file_parser = MiyooGamesFileParser()
3545
self.ensure_wpa_supplicant_conf()
@@ -60,6 +70,10 @@ def startup_init(self, include_wifi=True):
6070
config_volume = self.system_config.get_volume()
6171
self._set_volume(config_volume)
6272

73+
def _set_volume(self, user_volume):
74+
# Investigate sending volume key
75+
pass
76+
6377
#Untested
6478
@throttle.limit_refresh(5)
6579
def is_hdmi_connected(self):
@@ -159,6 +173,27 @@ def get_audio_system(self):
159173
def get_core_name_overrides(self, core_name):
160174
return [core_name, core_name+"-64"]
161175

162-
def _set_lumination_to_config(self):
163-
with open("/sys/class/backlight/backlight0/brightness", "w") as f:
164-
f.write(str((self.system_config.brightness * 255) // 20))
176+
def get_volume(self):
177+
try:
178+
return self.mainui_volume * 5
179+
except:
180+
return 0
181+
182+
def on_mainui_config_change(self):
183+
path = TrimUISmartProS.TRIMUI_STOCK_CONFIG_LOCATION
184+
if not os.path.exists(path):
185+
PyUiLogger.get_logger().warning(f"File not found: {path}")
186+
return
187+
188+
try:
189+
with open(path, "r", encoding="utf-8") as f:
190+
data = json.load(f)
191+
192+
old_volume = self.mainui_volume
193+
self.mainui_volume = data.get("vol")
194+
if(old_volume != self.mainui_volume):
195+
Display.volume_changed(self.mainui_volume * 5)
196+
197+
except Exception as e:
198+
PyUiLogger.get_logger().warning(f"Error reading {path}: {e}")
199+
return None

main-ui/devices/utils/file_watcher.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,31 @@
66

77
class FileWatcher():
88

9-
def watch_file(self,path, callback, interval=1.0):
9+
def watch_file(self,path, callback, interval, repeat_trigger_for_mtime_granularity_issues):
1010
last_mtime = None
11+
prev_loop_executed_counter = 0.0
12+
last_mtime = os.stat(path).st_mtime_ns
1113
while True:
1214
try:
13-
mtime = os.path.getmtime(path)
14-
if last_mtime is None:
15-
last_mtime = mtime
16-
elif mtime != last_mtime:
15+
mtime = os.stat(path).st_mtime_ns
16+
if mtime != last_mtime:
17+
PyUiLogger.get_logger().info(f"{path} detection changed.")
1718
last_mtime = mtime
1819
callback()
20+
if repeat_trigger_for_mtime_granularity_issues:
21+
prev_loop_executed_counter = 2.0
22+
elif(prev_loop_executed_counter > 0.0):
23+
callback()
24+
prev_loop_executed_counter -= interval
1925
except FileNotFoundError:
2026
PyUiLogger.get_logger().warning(f"{path} not found for file watcher.")
2127
time.sleep(interval)
2228

23-
def start_file_watcher(self,path, callback, interval=1.0):
29+
def start_file_watcher(self,path, callback, interval=1.0, repeat_trigger_for_mtime_granularity_issues=False):
2430
stop_event = threading.Event()
2531
thread = threading.Thread(
2632
target=self.watch_file,
27-
args=(path, callback, interval),
33+
args=(path, callback, interval, repeat_trigger_for_mtime_granularity_issues),
2834
daemon=True
2935
)
3036
thread.start()

main-ui/display/display.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class Display:
8888
top_bar_text = None
8989
_image_texture_cache = ImageTextureCache()
9090
_text_texture_cache = TextTextureCache()
91+
_problematic_images = set() # Class-level set to track images that won't load properly
92+
_problematic_image_keywords = [
93+
"No such file or directory",
94+
]
9195

9296
@classmethod
9397
def init(cls):
@@ -489,12 +493,19 @@ def _render_surface_texture(cls, x, y, texture, surface, render_mode: RenderMode
489493
return render_w, render_h
490494

491495
@classmethod
492-
def log_sdl_error_and_clear_cache(cls):
496+
def log_sdl_error_and_clear_cache(cls, image_path=None):
493497
err = sdl2.sdlttf.TTF_GetError()
494498
err_msg = err.decode('utf-8') if err else "Unknown error"
495-
PyUiLogger.get_logger().warning(f"Clearing cache : {err_msg}")
496-
cls._text_texture_cache.clear_cache()
497-
cls._image_texture_cache.clear_cache()
499+
PyUiLogger.get_logger().warning(f"SDL Error received on loading {image_path} : {err_msg}")
500+
501+
if any(keyword in err_msg for keyword in cls._problematic_image_keywords):
502+
if(image_path is not None):
503+
cls._problematic_images.add(image_path)
504+
PyUiLogger.get_logger().warning(f"Marking as image to permanently stop trying to load: {image_path}")
505+
else:
506+
PyUiLogger.get_logger().warning(f"Clearing cache : {err_msg}")
507+
cls._text_texture_cache.clear_cache()
508+
cls._image_texture_cache.clear_cache()
498509

499510

500511
@classmethod
@@ -576,7 +587,7 @@ def image_load(cls, image_path):
576587

577588
@classmethod
578589
def render_image(cls, image_path: str, x: int, y: int, render_mode=RenderMode.TOP_LEFT_ALIGNED, target_width=None, target_height=None, resize_type=None):
579-
if(image_path is None):
590+
if(image_path is None or image_path in cls._problematic_images):
580591
return 0, 0
581592

582593
cache : CachedImageTexture = cls._image_texture_cache.get_texture(image_path)
@@ -587,7 +598,7 @@ def render_image(cls, image_path: str, x: int, y: int, render_mode=RenderMode.TO
587598
else:
588599
surface = Display.image_load(image_path)
589600
if not surface:
590-
cls.log_sdl_error_and_clear_cache()
601+
cls.log_sdl_error_and_clear_cache(image_path)
591602
surface = Display.image_load(image_path)
592603
if not surface:
593604
PyUiLogger.get_logger().error(f"Failed to load image: {image_path}")

main-ui/menus/games/utils/roms_list_manager.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,29 @@ def load_from_file(self):
7171

7272
with open(self.entries_file, 'r') as f:
7373
data = json.load(f)
74-
self._entries = [RomsListEntry(**entry) for entry in data]
74+
validated_entries = []
75+
for entry_data in data:
76+
entry = RomsListEntry(**entry_data)
77+
if os.path.exists(entry.rom_file_path):
78+
validated_entries.append(entry)
79+
else:
80+
PyUiLogger.get_logger().warning(
81+
f"ROM file not found, removing from list: {entry.rom_file_path}"
82+
)
83+
84+
self._entries = validated_entries
7585
self._entries_dict = {
7686
self._entry_key(entry.rom_file_path, entry.game_system_name): entry
7787
for entry in self._entries
7888
}
7989

90+
# Save back the validated list in case some entries were removed
91+
self.save_to_file()
92+
8093
except Exception as e:
8194
PyUiLogger.get_logger().error(f"Failed to load entries: {e}")
8295

96+
8397
def get_games(self) -> List[RomInfo]:
8498
return self.rom_info_list
8599

0 commit comments

Comments
 (0)