make it use fabric audio service instead of pactl
This commit is contained in:
1 parent
628e544e3d
commit
2e78d911fc
1 file changed
+37
-168
@@ -1,5 +1,4 @@
|
|||||||
# Standard library imports
|
# Standard library imports
|
||||||
import subprocess
|
|
||||||
from gi.repository import GLib
|
from gi.repository import GLib
|
||||||
|
|
||||||
# Fabric imports
|
# Fabric imports
|
||||||
@@ -66,7 +65,7 @@ class PerAppVolumeControl(Box):
|
|||||||
# Initial population
|
# Initial population
|
||||||
self._populate_apps()
|
self._populate_apps()
|
||||||
|
|
||||||
# Set up auto-refresh timer for PulseAudio sinks
|
# Set up auto-refresh timer for audio streams
|
||||||
self._refresh_timer = GLib.timeout_add_seconds(2, self._auto_refresh)
|
self._refresh_timer = GLib.timeout_add_seconds(2, self._auto_refresh)
|
||||||
|
|
||||||
def _auto_refresh(self):
|
def _auto_refresh(self):
|
||||||
@@ -78,47 +77,6 @@ class PerAppVolumeControl(Box):
|
|||||||
"""Return to main control center view"""
|
"""Return to main control center view"""
|
||||||
self.control_center.close_per_app_volume()
|
self.control_center.close_per_app_volume()
|
||||||
|
|
||||||
def _get_pulse_sinks(self):
|
|
||||||
"""Get PulseAudio sink inputs (application audio streams)"""
|
|
||||||
try:
|
|
||||||
result = subprocess.run(
|
|
||||||
["pactl", "list", "sink-inputs"],
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
sinks = []
|
|
||||||
current_sink = {}
|
|
||||||
|
|
||||||
for line in result.stdout.split("\n"):
|
|
||||||
line = line.strip()
|
|
||||||
|
|
||||||
if line.startswith("Sink Input #"):
|
|
||||||
if current_sink:
|
|
||||||
sinks.append(current_sink)
|
|
||||||
current_sink = {
|
|
||||||
"index": line.split("#")[1],
|
|
||||||
"name": "Unknown",
|
|
||||||
"volume_raw": 100,
|
|
||||||
}
|
|
||||||
elif "application.name = " in line:
|
|
||||||
current_sink["name"] = line.split("= ")[1].strip('"')
|
|
||||||
elif "Volume:" in line and "front-left:" in line:
|
|
||||||
parts = line.split()
|
|
||||||
for part in parts:
|
|
||||||
if part.endswith("%"):
|
|
||||||
current_sink["volume_raw"] = int(part.replace("%", ""))
|
|
||||||
break
|
|
||||||
|
|
||||||
if current_sink:
|
|
||||||
sinks.append(current_sink)
|
|
||||||
|
|
||||||
return sinks
|
|
||||||
|
|
||||||
except (subprocess.CalledProcessError, FileNotFoundError):
|
|
||||||
return []
|
|
||||||
|
|
||||||
def _format_app_name(self, name):
|
def _format_app_name(self, name):
|
||||||
"""Format application name with proper capitalization"""
|
"""Format application name with proper capitalization"""
|
||||||
if not name:
|
if not name:
|
||||||
@@ -151,25 +109,13 @@ class PerAppVolumeControl(Box):
|
|||||||
self.apps_container.children = []
|
self.apps_container.children = []
|
||||||
self._app_widgets.clear()
|
self._app_widgets.clear()
|
||||||
|
|
||||||
# Try Fabric audio service first
|
# Use fabric audio service for applications
|
||||||
applications = []
|
if not audio_service:
|
||||||
if audio_service and hasattr(audio_service, "applications"):
|
self._show_no_apps_message()
|
||||||
applications = audio_service.applications
|
return
|
||||||
|
|
||||||
# If no Fabric applications, try PulseAudio directly
|
applications = getattr(audio_service, "applications", [])
|
||||||
if not applications:
|
|
||||||
pulse_sinks = self._get_pulse_sinks()
|
|
||||||
|
|
||||||
if pulse_sinks:
|
|
||||||
for sink in pulse_sinks:
|
|
||||||
app_widget = self._create_pulse_app_control(sink)
|
|
||||||
self.apps_container.children = list(
|
|
||||||
self.apps_container.children
|
|
||||||
) + [app_widget]
|
|
||||||
self._app_widgets[sink["name"]] = (app_widget, sink)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Use Fabric applications if available
|
|
||||||
if applications:
|
if applications:
|
||||||
for app in applications:
|
for app in applications:
|
||||||
if hasattr(app, "name") and hasattr(app, "volume"):
|
if hasattr(app, "name") and hasattr(app, "volume"):
|
||||||
@@ -191,77 +137,27 @@ class PerAppVolumeControl(Box):
|
|||||||
)
|
)
|
||||||
self.apps_container.children = [message]
|
self.apps_container.children = [message]
|
||||||
|
|
||||||
def _create_pulse_app_control(self, sink):
|
|
||||||
"""Create volume control for a PulseAudio sink input"""
|
|
||||||
# Format and truncate app name
|
|
||||||
app_name = self._format_app_name(sink["name"])
|
|
||||||
if len(app_name) > 20:
|
|
||||||
app_name = app_name[:17] + "..."
|
|
||||||
|
|
||||||
# Volume scale with Apple-like styling
|
|
||||||
volume_scale = Scale(
|
|
||||||
value=sink["volume_raw"],
|
|
||||||
min_value=0,
|
|
||||||
max_value=150,
|
|
||||||
increments=(5, 5),
|
|
||||||
name="apple-volume-slider",
|
|
||||||
size=28,
|
|
||||||
h_expand=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Connect volume change handler
|
|
||||||
volume_scale.connect(
|
|
||||||
"change-value",
|
|
||||||
lambda scale, scroll_type, value, sink_data=sink: self._set_pulse_volume(
|
|
||||||
sink_data, value
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create the app control widget
|
|
||||||
app_control = Box(
|
|
||||||
orientation="vertical",
|
|
||||||
spacing=8,
|
|
||||||
style_classes="apple-app-volume-item",
|
|
||||||
children=[
|
|
||||||
Label(
|
|
||||||
label=app_name,
|
|
||||||
style_classes="apple-app-name",
|
|
||||||
h_align="start",
|
|
||||||
),
|
|
||||||
volume_scale,
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
return app_control
|
|
||||||
|
|
||||||
def _create_app_control(self, app):
|
def _create_app_control(self, app):
|
||||||
"""Create volume control for a single application (Fabric)"""
|
"""Create volume control for a single application"""
|
||||||
# Format and truncate app name
|
# Format and truncate app name
|
||||||
app_name = self._format_app_name(app.name)
|
app_name = self._format_app_name(app.name)
|
||||||
if len(app_name) > 20:
|
if len(app_name) > 20:
|
||||||
app_name = app_name[:17] + "..."
|
app_name = app_name[:17] + "..."
|
||||||
|
|
||||||
# Handle different volume formats
|
# Get current volume from fabric audio service
|
||||||
volume_value = app.volume
|
# Fabric returns volume as a float, typically 0.0 to max_volume (default 100)
|
||||||
if isinstance(volume_value, float):
|
current_volume = getattr(app, "volume", 0.0)
|
||||||
if 0.0 <= volume_value <= 1.0:
|
max_vol = getattr(audio_service, "max_volume", 100) if audio_service else 100
|
||||||
volume_percent = volume_value * 100
|
|
||||||
else:
|
# Ensure volume is in valid range
|
||||||
volume_percent = min(max(volume_value, 0), 100)
|
volume_percent = max(0, min(current_volume, max_vol))
|
||||||
elif isinstance(volume_value, int):
|
|
||||||
if volume_value <= 1:
|
|
||||||
volume_percent = volume_value * 100
|
|
||||||
else:
|
|
||||||
volume_percent = min(max(volume_value, 0), 100)
|
|
||||||
else:
|
|
||||||
volume_percent = 50
|
|
||||||
|
|
||||||
# Volume scale with Apple-like styling
|
# Volume scale - use fabric's max_volume as range
|
||||||
volume_scale = Scale(
|
volume_scale = Scale(
|
||||||
value=volume_percent,
|
value=volume_percent,
|
||||||
min_value=0,
|
min_value=0,
|
||||||
max_value=100,
|
max_value=max_vol,
|
||||||
increments=(5, 5),
|
increments=(1, 5),
|
||||||
name="apple-volume-slider",
|
name="apple-volume-slider",
|
||||||
size=28,
|
size=28,
|
||||||
h_expand=True,
|
h_expand=True,
|
||||||
@@ -290,54 +186,25 @@ class PerAppVolumeControl(Box):
|
|||||||
|
|
||||||
return app_control
|
return app_control
|
||||||
|
|
||||||
def _set_pulse_volume(self, sink, volume_percent):
|
def _set_app_volume(self, app, volume_value):
|
||||||
"""Set volume for a PulseAudio sink input"""
|
"""Set volume for a specific application using fabric audio service"""
|
||||||
if sink["name"] in self._updating_volumes:
|
|
||||||
return
|
|
||||||
|
|
||||||
self._updating_volumes.add(sink["name"])
|
|
||||||
|
|
||||||
try:
|
|
||||||
subprocess.run(
|
|
||||||
[
|
|
||||||
"pactl",
|
|
||||||
"set-sink-input-volume",
|
|
||||||
sink["index"],
|
|
||||||
f"{int(volume_percent)}%",
|
|
||||||
],
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
pass
|
|
||||||
finally:
|
|
||||||
GLib.timeout_add(100, lambda: self._updating_volumes.discard(sink["name"]))
|
|
||||||
|
|
||||||
def _set_app_volume(self, app, volume_percent):
|
|
||||||
"""Set volume for a specific application (Fabric)"""
|
|
||||||
if app.name in self._updating_volumes:
|
if app.name in self._updating_volumes:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._updating_volumes.add(app.name)
|
self._updating_volumes.add(app.name)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
current_volume = app.volume
|
# Get max volume from audio service
|
||||||
|
max_vol = getattr(audio_service, "max_volume", 100) if audio_service else 100
|
||||||
if isinstance(current_volume, float):
|
|
||||||
if 0.0 <= current_volume <= 1.0:
|
# Ensure volume is within bounds
|
||||||
volume_value = volume_percent / 100.0
|
volume_value = max(0, min(volume_value, max_vol))
|
||||||
else:
|
|
||||||
volume_value = volume_percent
|
# Set volume directly - fabric expects the actual volume value, not percentage
|
||||||
elif isinstance(current_volume, int):
|
|
||||||
if current_volume <= 1:
|
|
||||||
volume_value = int(volume_percent / 100.0)
|
|
||||||
else:
|
|
||||||
volume_value = int(volume_percent)
|
|
||||||
else:
|
|
||||||
volume_value = volume_percent
|
|
||||||
|
|
||||||
app.volume = volume_value
|
app.volume = volume_value
|
||||||
except Exception:
|
|
||||||
pass
|
except Exception as e:
|
||||||
|
print(f"Error setting volume for {app.name}: {e}")
|
||||||
finally:
|
finally:
|
||||||
GLib.timeout_add(100, lambda: self._updating_volumes.discard(app.name))
|
GLib.timeout_add(100, lambda: self._updating_volumes.discard(app.name))
|
||||||
|
|
||||||
@@ -354,11 +221,13 @@ class PerAppVolumeControl(Box):
|
|||||||
if hasattr(self, "_refresh_timer"):
|
if hasattr(self, "_refresh_timer"):
|
||||||
GLib.source_remove(self._refresh_timer)
|
GLib.source_remove(self._refresh_timer)
|
||||||
|
|
||||||
for connection in self._signal_connections:
|
# Disconnect fabric audio service signals
|
||||||
try:
|
if audio_service:
|
||||||
connection.disconnect()
|
for connection in self._signal_connections:
|
||||||
except:
|
try:
|
||||||
pass
|
audio_service.disconnect(connection)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
self._signal_connections.clear()
|
self._signal_connections.clear()
|
||||||
self._app_widgets.clear()
|
self._app_widgets.clear()
|
||||||
|
|||||||
Reference in new issue
Block a user