Skip to content

Commit a40da89

Browse files
committed
(tweak/fix) Make Linux actually work/Async
1 parent 375eb12 commit a40da89

File tree

2 files changed

+93
-33
lines changed

2 files changed

+93
-33
lines changed

BabbleApp/babbleapp.py

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,19 @@
2121
import queue
2222
import requests
2323
import threading
24-
import webbrowser
25-
from pathlib import Path
24+
import asyncio
2625
from ctypes import c_int
2726
from babble_model_loader import *
2827
from camera_widget import CameraWidget
2928
from config import BabbleConfig
30-
from tab import CamInfo, Tab
29+
from tab import Tab
3130
from osc import VRChatOSCReceiver, VRChatOSC
3231
from general_settings_widget import SettingsWidget
3332
from algo_settings_widget import AlgoSettingsWidget
3433
from calib_settings_widget import CalibSettingsWidget
34+
from notification_manager import NotificationManager
3535
from utils.misc_utils import EnsurePath, is_nt, bg_color_highlight, bg_color_clear
3636
from lang_manager import LocaleStringManager as lang
37-
from desktop_notifier import DesktopNotifierSync, Urgency, Button, Icon, DEFAULT_SOUND
3837

3938
winmm = None
4039

@@ -60,7 +59,7 @@
6059
ALGO_SETTINGS_RADIO_NAME = "-ALGOSETTINGSRADIO-"
6160
CALIB_SETTINGS_RADIO_NAME = "-CALIBSETTINGSRADIO-"
6261

63-
page_url = "https://github.com/SummerSigh/ProjectBabble/releases/latest"
62+
page_url = "https://github.com/Project-Babble/ProjectBabble/releases/latest"
6463
appversion = "Babble v2.0.7"
6564

6665
def timerResolution(toggle):
@@ -73,26 +72,29 @@ def timerResolution(toggle):
7372
else:
7473
winmm.timeEndPeriod(1)
7574

76-
def send_notification(latestversion):
77-
logo = Icon(
78-
path=Path(os.path.join(os.getcwd(), "Images", "logo.ico"))
79-
)
80-
notifier = DesktopNotifierSync(app_name="Babble App")
81-
notifier.send(
82-
title=lang._instance.get_string("babble.updatePresent"),
83-
message=f'{lang._instance.get_string("babble.updateTo")} {latestversion}',
84-
urgency=Urgency.Normal,
85-
buttons=[
86-
Button(
87-
title=lang._instance.get_string("babble.downloadPage"),
88-
on_pressed=lambda: webbrowser.open("https://github.com/SummerSigh/ProjectBabble/releases/latest"),
75+
async def check_for_updates(config, notification_manager):
76+
if config.settings.gui_update_check:
77+
try:
78+
response = requests.get(
79+
"https://api.github.com/repos/Project-Babble/ProjectBabble/releases/latest"
8980
)
90-
],
91-
icon=logo,
92-
sound=DEFAULT_SOUND
93-
)
81+
latestversion = response.json()["name"]
9482

95-
def main():
83+
if appversion == latestversion:
84+
print(
85+
f'\033[92m[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.latestVersion")}! [{latestversion}]\033[0m'
86+
)
87+
else:
88+
print(
89+
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'
90+
)
91+
await notification_manager.show_notification(appversion, latestversion, page_url)
92+
except Exception as e:
93+
print(
94+
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.noInternet")}. Error: {e}'
95+
)
96+
97+
async def async_main():
9698
EnsurePath()
9799

98100
# Get Configuration
@@ -102,10 +104,15 @@ def main():
102104
lang("Locale", config.settings.gui_language)
103105

104106
config.save()
107+
108+
notification_manager = NotificationManager()
109+
await notification_manager.initialize()
110+
111+
# Run the update check
112+
await check_for_updates(config, notification_manager)
105113

106114
cancellation_event = threading.Event()
107115
ROSC = False
108-
# Check to see if we can connect to our video source first. If not, bring up the camera dialog.
109116

110117
if config.settings.gui_update_check:
111118
try:
@@ -124,10 +131,11 @@ def main():
124131
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'
125132
)
126133
try:
127-
send_notification(latestversion)
134+
# Run notification in a separate thread to avoid blocking
135+
await notification_manager.show_notification(latestversion)
128136
except Exception as e:
129137
print(
130-
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.noToast")}'
138+
f'[{lang._instance.get_string("log.info")}] {lang._instance.get_string("babble.noToast")} Error: {e}'
131139
)
132140
except Exception as e:
133141
print(
@@ -136,13 +144,9 @@ def main():
136144

137145
timerResolution(True)
138146

139-
# At this, check to see if we have an ROI. If not, bring up ROI finder GUI.
140-
141-
# Spawn worker threads
142147
osc_queue: queue.Queue[tuple[bool, int, int]] = queue.Queue(maxsize=10)
143148
osc = VRChatOSC(cancellation_event, osc_queue, config)
144149
osc_thread = threading.Thread(target=osc.run)
145-
# start worker threads
146150
osc_thread.start()
147151

148152
cams = [
@@ -243,7 +247,6 @@ def main():
243247

244248
tint = 33
245249
fs = False
246-
# GUI Render loop
247250
while True:
248251
# First off, check for any events from the GUI
249252
event, values = window.read(timeout=tint)
@@ -352,8 +355,12 @@ def main():
352355
for setting in settings:
353356
if setting.started():
354357
setting.render(window, event, values)
355-
358+
359+
# Does adding this help?
360+
# await asyncio.sleep(0)
361+
362+
def main():
363+
asyncio.run(async_main())
356364

357365
if __name__ == "__main__":
358366
main()
359-

BabbleApp/notification_manager.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
import webbrowser
3+
import signal
4+
import asyncio
5+
from pathlib import Path
6+
from desktop_notifier import DesktopNotifier, Urgency, Button, Icon, DEFAULT_SOUND
7+
from lang_manager import LocaleStringManager as lang
8+
from utils.misc_utils import is_nt
9+
10+
class NotificationManager:
11+
def __init__(self):
12+
self.notifier = DesktopNotifier(app_name="Babble App")
13+
self.loop = None
14+
self.stop_event = None
15+
16+
async def show_notification(self, appversion, latestversion, page_url):
17+
logo = Icon(
18+
path=Path(os.path.join(os.getcwd(), "Images", "logo.ico"))
19+
)
20+
21+
notification_message = (
22+
f'{lang._instance.get_string("babble.needUpdateOne")} '
23+
f'{appversion} '
24+
f'{lang._instance.get_string("babble.needUpdateTwo")} '
25+
f'{latestversion} '
26+
f'{lang._instance.get_string("babble.needUpdateThree")}!'
27+
)
28+
29+
await self.notifier.send(
30+
title=lang._instance.get_string("babble.updatePresent"),
31+
message=notification_message,
32+
urgency=Urgency.Normal,
33+
buttons=[
34+
Button(
35+
title=lang._instance.get_string("babble.downloadPage"),
36+
on_pressed=lambda: webbrowser.open(page_url)
37+
)
38+
],
39+
icon=logo,
40+
sound=DEFAULT_SOUND,
41+
on_dismissed=lambda: self.stop_event.set()
42+
)
43+
44+
await self.stop_event.wait()
45+
46+
async def initialize(self):
47+
self.stop_event = asyncio.Event()
48+
self.loop = asyncio.get_running_loop()
49+
50+
# Add non nt signal handlers
51+
if not is_nt:
52+
self.loop.add_signal_handler(signal.SIGINT, self.stop_event.set)
53+
self.loop.add_signal_handler(signal.SIGTERM, self.stop_event.set)

0 commit comments

Comments
 (0)