Skip to content

Commit

Permalink
switch to pamixer #24
Browse files Browse the repository at this point in the history
  • Loading branch information
nwg-piotr committed Mar 3, 2021
1 parent 66943f6 commit fcf0dbe
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 106 deletions.
67 changes: 40 additions & 27 deletions nwg_panel/modules/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ def get_output(self):

if "volume" in self.settings["components"] and dependencies["pyalsa"] or dependencies["amixer"]:
try:
value, switch = get_volume()
GLib.idle_add(self.update_volume, value, switch)
value, muted = get_volume()
GLib.idle_add(self.update_volume, value, muted)
except Exception as e:
print(e)

Expand Down Expand Up @@ -209,8 +209,8 @@ def update_battery(self, value, charging):
if self.bat_label:
self.bat_label.set_text("{}%".format(value))

def update_volume(self, value, switch):
icon_name = vol_icon_name(value, switch)
def update_volume(self, value, muted):
icon_name = vol_icon_name(value, muted)

if icon_name != self.vol_icon_name:
update_image(self.vol_image, icon_name, self.settings["icon-size"], self.icons_path)
Expand Down Expand Up @@ -269,6 +269,9 @@ def __init__(self, position, alignment, settings, width, monitor=None, icons_pat

self.menu_box = None
self.sink_box = None

self.bri_scale = None
self.vol_scale = None

check_key(settings, "output-switcher", False)
self.sinks = []
Expand Down Expand Up @@ -325,12 +328,12 @@ def __init__(self, position, alignment, settings, width, monitor=None, icons_pat

inner_hbox.pack_start(self.bri_image, False, False, 6)

scale = Gtk.Scale.new_with_range(orientation=Gtk.Orientation.HORIZONTAL, min=0, max=100, step=1)
self.bri_scale = Gtk.Scale.new_with_range(orientation=Gtk.Orientation.HORIZONTAL, min=0, max=100, step=1)
value = get_brightness()
scale.set_value(value)
scale.connect("value-changed", self.set_bri)
self.bri_scale.set_value(value)
self.bri_scale.connect("value-changed", self.set_bri)

inner_hbox.pack_start(scale, True, True, 5)
inner_hbox.pack_start(self.bri_scale, True, True, 5)
add_sep = True

if "volume" in settings["components"] and dependencies["pyalsa"] or dependencies["amixer"]:
Expand All @@ -340,21 +343,20 @@ def __init__(self, position, alignment, settings, width, monitor=None, icons_pat
self.vol_icon_name = "view-refresh-symbolic"
self.vol_image = Gtk.Image.new_from_icon_name(self.vol_icon_name, Gtk.IconSize.MENU)

vol, switch = get_volume()
icon_name = vol_icon_name(vol, switch)
vol, muted = get_volume()
icon_name = vol_icon_name(vol, muted)

if icon_name != self.vol_icon_name:
update_image(self.vol_image, icon_name, self.icon_size, self.icons_path)
self.vol_icon_name = icon_name

inner_hbox.pack_start(self.vol_image, False, False, 6)

scale = Gtk.Scale.new_with_range(orientation=Gtk.Orientation.HORIZONTAL, min=0, max=100, step=1)
value, switch = get_volume()
scale.set_value(value)
scale.connect("value-changed", self.set_vol)
self.vol_scale = Gtk.Scale.new_with_range(orientation=Gtk.Orientation.HORIZONTAL, min=0, max=100, step=1)
self.vol_scale.set_value(vol)
self.vol_scale.connect("value-changed", self.set_vol)

inner_hbox.pack_start(scale, True, True, 5)
inner_hbox.pack_start(self.vol_scale, True, True, 5)
if is_command("pactl") and settings["output-switcher"]:
pactl_eb = Gtk.EventBox()
image = Gtk.Image()
Expand Down Expand Up @@ -610,6 +612,26 @@ def refresh(self):
self.bat_icon_name = icon_name

self.bat_label.set_text("{}% {}".format(level, msg))

if "volume" in self.settings["components"]:
vol, muted = get_volume()
icon_name = vol_icon_name(vol, muted)

if icon_name != self.vol_icon_name:
update_image(self.vol_image, icon_name, self.icon_size, self.icons_path)
self.vol_icon_name = icon_name

self.vol_scale.set_value(vol)

if "brightness" in self.settings["components"]:
value = get_brightness()
icon_name = bri_icon_name(int(value))
if icon_name != self.bri_icon_name:
update_image(self.bri_image, icon_name, self.icon_size, self.icons_path)
self.bri_icon_name = icon_name

self.bri_scale.set_value(value)


return True

Expand All @@ -621,18 +643,9 @@ def on_leave_notify_event(self, widget, event):

def set_bri(self, slider):
set_brightness(slider)
icon_name = bri_icon_name(int(slider.get_value()))
if icon_name != self.bri_icon_name:
update_image(self.bri_image, icon_name, self.icon_size, self.icons_path)
self.bri_icon_name = icon_name

def set_vol(self, slider):
set_volume(slider)
vol, switch = get_volume()
icon_name = vol_icon_name(vol, switch)
if icon_name != self.vol_icon_name:
update_image(self.vol_image, icon_name, self.icon_size, self.icons_path)
self.vol_icon_name = icon_name

def close_win(self, w, e):
self.hide()
Expand Down Expand Up @@ -689,7 +702,7 @@ def on_leave_notify_event(self, widget, event):

def switch_sink(self, w, e, sink):
print("Sink: '{}'".format(sink))
subprocess.Popen('exec pacmd set-default-sink "{}"'.format(sink), shell=True)
subprocess.Popen('exec pamixer --sink "{}"'.format(sink), shell=True)
self.hide()


Expand All @@ -703,9 +716,9 @@ def bri_icon_name(value):
return icon_name


def vol_icon_name(value, switch):
def vol_icon_name(value, muted):
icon_name = "audio-volume-muted-symbolic"
if switch:
if not muted:
if value is not None:
if value > 70:
icon_name = "audio-volume-high-symbolic"
Expand Down
104 changes: 25 additions & 79 deletions nwg_panel/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,65 +311,20 @@ def is_command(cmd):

def get_volume():
vol = 0
switch = False
if nwg_panel.common.dependencies["pyalsa"]:
mixer = alsamixer.Mixer()
mixer.attach()
mixer.load()
# https://github.com/nwg-piotr/nwg-panel/issues/24
try:
element = alsamixer.Element(mixer, nwg_panel.common.defaults["master"])
max_vol = element.get_volume_range()[1]
vol = int(round(element.get_volume() * 100 / max_vol, 0))
switch = element.get_switch()
except:
try:
element = alsamixer.Element(mixer, mixer.list()[0][0])
# Overwrite user-defined name if caused error
print("'{}' didn't work, using {} instead".format(nwg_panel.common.defaults["master"], element.name))
nwg_panel.common.defaults["master"] = element.name

max_vol = element.get_volume_range()[1]
vol = int(round(element.get_volume() * 100 / max_vol, 0))
switch = element.get_switch()
except:
return 0, False
muted = False
try:
vol = int(cmd2string("pamixer --get-volume"))
except Exception as e:
eprint(e)

del mixer
try:
muted = subprocess.check_output("pamixer --get-mute", shell=True).decode(
"utf-8").strip() == "true"
except subprocess.CalledProcessError:
# the command above returns the 'disabled` status w/ CalledProcessError, exit status 1
pass

elif nwg_panel.common.dependencies["amixer"]:
# Same issue as above
result = cmd2string("amixer sget {}".format(nwg_panel.common.defaults["master"]))
if not result:
try:
nwg_panel.common.defaults["master"] = get_scontrol()
result = cmd2string("amixer sget {}".format(nwg_panel.common.defaults["master"]))
except:
result = None

if result:
lines = result.splitlines()
for line in lines:
if line.strip().startswith("Mono:"):
try:
vol = int(line.split()[3][1:-2])
try:
switch = "on" in line.split()[5]
except:
switch = "on" in line.split()[4]
break
except:
pass

if line.strip().startswith("Front Left:"):
try:
vol = int(line.split()[4][1:-2])
switch = "on" in line.split()[5]
break
except:
pass

return vol, switch
return vol, muted


def get_scontrol():
Expand All @@ -379,28 +334,19 @@ def get_scontrol():

def list_sinks():
sinks = []
if is_command("pactl"):
output = cmd2string("pactl list short sinks")
if output:
for line in output.splitlines():
details = line.split()
name = details[1]
if details[-1].upper() == "RUNNING":
sinks.insert(0, {"name": name})
else:
sinks.append({"name": name, "desc": "unknown"})

if is_command("pacmd"):
output = cmd2string("pacmd list-sinks").splitlines()
for item in sinks:
im_in = False
for line in output:
if item["name"] in line:
im_in = True
if im_in and "device.description" in line:
desc = line.split("=")[-1].strip()[1:-1]
item["desc"] = desc
break
if is_command("pamixer"):
try:
output = cmd2string("pamixer --list-sinks")
if output:
lines = output.splitlines()[1:]
for line in lines:
details = line.split()
name = details[1][1:-1]
desc = " ".join(details[2:])[1:-1]
sinks.append({"name": name, "desc": desc})
except Exception as e:
eprint(e)

return sinks


Expand Down

0 comments on commit fcf0dbe

Please sign in to comment.