(tweak/fix) Make Linux actually work/Async

This commit is contained in:
Benjamin Evans committed 2025-01-16 23:37:40 -06:00
1 parent 375eb12dff
commit a40da89ff3
2 files changed
+93 -33

No files matched your search

+40 -33
View File
@@ -21,20 +21,19 @@ import PySimpleGUI as sg
import queue
import requests
import threading
import webbrowser
from pathlib import Path
import asyncio
from ctypes import c_int
from babble_model_loader import *
from camera_widget import CameraWidget
from config import BabbleConfig
from tab import CamInfo, Tab
from tab import Tab
from osc import VRChatOSCReceiver, VRChatOSC
from general_settings_widget import SettingsWidget
from algo_settings_widget import AlgoSettingsWidget
from calib_settings_widget import CalibSettingsWidget
from notification_manager import NotificationManager
from utils.misc_utils import EnsurePath, is_nt, bg_color_highlight, bg_color_clear
from lang_manager import LocaleStringManager as lang
from desktop_notifier import DesktopNotifierSync, Urgency, Button, Icon, DEFAULT_SOUND
winmm = None
@@ -60,7 +59,7 @@ SETTINGS_RADIO_NAME = "-SETTINGSRADIO-"
ALGO_SETTINGS_RADIO_NAME = "-ALGOSETTINGSRADIO-"
CALIB_SETTINGS_RADIO_NAME = "-CALIBSETTINGSRADIO-"
page_url = "https://github.com/SummerSigh/ProjectBabble/releases/latest"
page_url = "https://github.com/Project-Babble/ProjectBabble/releases/latest"
appversion = "Babble v2.0.7"
def timerResolution(toggle):
@@ -73,26 +72,29 @@ def timerResolution(toggle):
else:
winmm.timeEndPeriod(1)
def send_notification(latestversion):
logo = Icon(
path=Path(os.path.join(os.getcwd(), "Images", "logo.ico"))
)
notifier = DesktopNotifierSync(app_name="Babble App")
notifier.send(
title=lang._instance.get_string("babble.updatePresent"),
message=f'{lang._instance.get_string("babble.updateTo")} {latestversion}',
urgency=Urgency.Normal,
buttons=[
Button(
title=lang._instance.get_string("babble.downloadPage"),
on_pressed=lambda: webbrowser.open("https://github.com/SummerSigh/ProjectBabble/releases/latest"),
async def check_for_updates(config, notification_manager):
if config.settings.gui_update_check:
try:
response = requests.get(
"https://api.github.com/repos/Project-Babble/ProjectBabble/releases/latest"
)
],
icon=logo,
sound=DEFAULT_SOUND
)
latestversion = response.json()["name"]
def main():
if appversion == latestversion:
print(
f'\033[92m[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.latestVersion")}! [{latestversion}]\033[0m'
)
else:
print(
f'\033[93m[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.needUpdateOne")} [{appversion}] {lang._instance.get_string("babble.needUpdateTwo")} [{latestversion}] {lang._instance.get_string("babble.needUpdateThree")}.\033[0m'
)
await notification_manager.show_notification(appversion, latestversion, page_url)
except Exception as e:
print(
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.noInternet")}. Error: {e}'
)
async def async_main():
EnsurePath()
# Get Configuration
@@ -102,10 +104,15 @@ def main():
lang("Locale", config.settings.gui_language)
config.save()
notification_manager = NotificationManager()
await notification_manager.initialize()
# Run the update check
await check_for_updates(config, notification_manager)
cancellation_event = threading.Event()
ROSC = False
# Check to see if we can connect to our video source first. If not, bring up the camera dialog.
if config.settings.gui_update_check:
try:
@@ -124,10 +131,11 @@ def main():
f'\033[93m[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.needUpdateOne")} [{appversion}] {lang._instance.get_string("babble.needUpdateTwo")} [{latestversion}] {lang._instance.get_string("babble.needUpdateThree")}.\033[0m'
)
try:
send_notification(latestversion)
# Run notification in a separate thread to avoid blocking
await notification_manager.show_notification(latestversion)
except Exception as e:
print(
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.noToast")}'
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.noToast")} Error: {e}'
)
except Exception as e:
print(
@@ -136,13 +144,9 @@ def main():
timerResolution(True)
# At this, check to see if we have an ROI. If not, bring up ROI finder GUI.
# Spawn worker threads
osc_queue: queue.Queue[tuple[bool, int, int]] = queue.Queue(maxsize=10)
osc = VRChatOSC(cancellation_event, osc_queue, config)
osc_thread = threading.Thread(target=osc.run)
# start worker threads
osc_thread.start()
cams = [
@@ -243,7 +247,6 @@ def main():
tint = 33
fs = False
# GUI Render loop
while True:
# First off, check for any events from the GUI
event, values = window.read(timeout=tint)
@@ -352,8 +355,12 @@ def main():
for setting in settings:
if setting.started():
setting.render(window, event, values)
# Does adding this help?
# await asyncio.sleep(0)
def main():
asyncio.run(async_main())
if __name__ == "__main__":
main()
+53
View File
@@ -0,0 +1,53 @@
import os
import webbrowser
import signal
import asyncio
from pathlib import Path
from desktop_notifier import DesktopNotifier, Urgency, Button, Icon, DEFAULT_SOUND
from lang_manager import LocaleStringManager as lang
from utils.misc_utils import is_nt
class NotificationManager:
def __init__(self):
self.notifier = DesktopNotifier(app_name="Babble App")
self.loop = None
self.stop_event = None
async def show_notification(self, appversion, latestversion, page_url):
logo = Icon(
path=Path(os.path.join(os.getcwd(), "Images", "logo.ico"))
)
notification_message = (
f'{lang._instance.get_string("babble.needUpdateOne")} '
f'{appversion} '
f'{lang._instance.get_string("babble.needUpdateTwo")} '
f'{latestversion} '
f'{lang._instance.get_string("babble.needUpdateThree")}!'
)
await self.notifier.send(
title=lang._instance.get_string("babble.updatePresent"),
message=notification_message,
urgency=Urgency.Normal,
buttons=[
Button(
title=lang._instance.get_string("babble.downloadPage"),
on_pressed=lambda: webbrowser.open(page_url)
)
],
icon=logo,
sound=DEFAULT_SOUND,
on_dismissed=lambda: self.stop_event.set()
)
await self.stop_event.wait()
async def initialize(self):
self.stop_event = asyncio.Event()
self.loop = asyncio.get_running_loop()
# Add non nt signal handlers
if not is_nt:
self.loop.add_signal_handler(signal.SIGINT, self.stop_event.set)
self.loop.add_signal_handler(signal.SIGTERM, self.stop_event.set)