diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/caelestia/subcommands/record.py | 55 | ||||
| -rw-r--r-- | src/caelestia/utils/notify.py | 15 |
2 files changed, 44 insertions, 26 deletions
diff --git a/src/caelestia/subcommands/record.py b/src/caelestia/subcommands/record.py index 71b8c5d..3cfde82 100644 --- a/src/caelestia/subcommands/record.py +++ b/src/caelestia/subcommands/record.py @@ -5,7 +5,7 @@ import time from argparse import Namespace from datetime import datetime -from caelestia.utils.notify import notify +from caelestia.utils.notify import close_notification, notify from caelestia.utils.paths import recording_notif_path, recording_path, recordings_dir @@ -66,16 +66,28 @@ class Command: if self.args.sound: sources = subprocess.check_output(["pactl", "list", "short", "sources"], text=True).splitlines() + audio_source = None + for source in sources: if "RUNNING" in source: - if self.recorder == "wf-recorder": - args += ["-a", source.split()[1]] - else: - args += ["--audio", "--audio-device", source.split()[1]] + audio_source = source.split()[1] break - else: + + # Fallback to IDLE source if no RUNNING source + if not audio_source: + for source in sources: + if "IDLE" in source: + audio_source = source.split()[1] + break + + if not audio_source: raise ValueError("No audio source found") + if self.recorder == "wf-recorder": + args += [f"--audio={audio_source}"] + else: + args += ["--audio", "--audio-device", audio_source] + recording_path.parent.mkdir(parents=True, exist_ok=True) proc = subprocess.Popen( [self.recorder, *args, "-f", recording_path], @@ -84,13 +96,16 @@ class Command: start_new_session=True, ) - # Send notif if proc hasn't ended after a small delay - time.sleep(0.1) - if proc.poll() is None: - notif = notify("-p", "Recording started", "Recording...") - recording_notif_path.write_text(notif) - else: - notify("Recording failed", f"Recording failed to start: {proc.communicate()[1]}") + notif = notify("-p", "Recording started", "Recording...") + recording_notif_path.write_text(notif) + + for _ in range(5): + if proc.poll() is not None: + if proc.returncode != 0: + close_notification(notif) + notify("Recording failed", f"Recording error: {proc.communicate()[1]}") + return + time.sleep(0.2) def stop(self) -> None: # Start killing recording process @@ -107,19 +122,7 @@ class Command: # Close start notification try: - notif = recording_notif_path.read_text() - subprocess.run( - [ - "gdbus", - "call", - "--session", - "--dest=org.freedesktop.Notifications", - "--object-path=/org/freedesktop/Notifications", - "--method=org.freedesktop.Notifications.CloseNotification", - notif, - ], - stdout=subprocess.DEVNULL, - ) + close_notification(recording_notif_path.read_text()) except IOError: pass diff --git a/src/caelestia/utils/notify.py b/src/caelestia/utils/notify.py index ad1ef33..ee86236 100644 --- a/src/caelestia/utils/notify.py +++ b/src/caelestia/utils/notify.py @@ -3,3 +3,18 @@ import subprocess def notify(*args: list[str]) -> str: return subprocess.check_output(["notify-send", "-a", "caelestia-cli", *args], text=True).strip() + + +def close_notification(id: str) -> None: + subprocess.run( + [ + "gdbus", + "call", + "--session", + "--dest=org.freedesktop.Notifications", + "--object-path=/org/freedesktop/Notifications", + "--method=org.freedesktop.Notifications.CloseNotification", + id, + ], + stdout=subprocess.DEVNULL, + ) |