Skip to content

Commit

Permalink
set_volume(): also use pamixer only #24
Browse files Browse the repository at this point in the history
  • Loading branch information
nwg-piotr committed Mar 3, 2021
1 parent fdaed18 commit 0501274
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
6 changes: 3 additions & 3 deletions nwg_panel/modules/controls.py
Expand Up @@ -275,7 +275,7 @@ def __init__(self, position, alignment, settings, width, monitor=None, icons_pat

check_key(settings, "output-switcher", False)
self.sinks = []
if is_command("pactl") and settings["output-switcher"]:
if is_command("pamixer") and settings["output-switcher"]:
self.sinks = list_sinks()
self.connect("show", self.refresh_sinks)

Expand Down Expand Up @@ -362,7 +362,7 @@ def __init__(self, position, alignment, settings, width, monitor=None, icons_pat
self.vol_scale.connect("value-changed", self.set_vol)

inner_hbox.pack_start(self.vol_scale, True, True, 5)
if is_command("pactl") and settings["output-switcher"]:
if is_command("pamixer") and settings["output-switcher"]:
pactl_eb = Gtk.EventBox()
image = Gtk.Image()
pactl_eb.add(image)
Expand All @@ -374,7 +374,7 @@ def __init__(self, position, alignment, settings, width, monitor=None, icons_pat

add_sep = True

if is_command("pactl") and settings["output-switcher"]:
if is_command("pamixer") and settings["output-switcher"]:
self.sink_box = SinkBox()
pactl_eb.connect('button-press-event', self.sink_box.switch_visibility)
v_box.pack_start(self.sink_box, False, False, 0)
Expand Down
65 changes: 27 additions & 38 deletions nwg_panel/tools.py
Expand Up @@ -312,26 +312,24 @@ def is_command(cmd):
def get_volume():
vol = 0
muted = False
try:
vol = int(cmd2string("pamixer --get-volume"))
except Exception as e:
eprint(e)

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
if is_command("pamixer"):
try:
vol = int(cmd2string("pamixer --get-volume"))
except Exception as e:
eprint(e)

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
else:
eprint("Required 'pamixer' command not found")

return vol, muted


def get_scontrol():
result = cmd2string("amixer scontrols")
return result.split()[3].split(",")[0][1:-1]


def list_sinks():
sinks = []
if is_command("pamixer"):
Expand All @@ -346,38 +344,29 @@ def list_sinks():
sinks.append({"name": name, "desc": desc})
except Exception as e:
eprint(e)
else:
eprint("Required 'pamixer' command not found")

return sinks


def toggle_mute(*args):
vol, muted = get_volume()
if muted:
subprocess.call("pamixer -u".split())
if is_command("pamixer"):
vol, muted = get_volume()
if muted:
subprocess.call("pamixer -u".split())
else:
subprocess.call("pamixer -m".split())
else:
subprocess.call("pamixer -m".split())
eprint("Required 'pamixer' command not found")


def set_volume(slider):
percent = slider.get_value()
if nwg_panel.common.dependencies["pyalsa"]:
mixer = alsamixer.Mixer()
mixer.attach()
mixer.load()
try:
element = alsamixer.Element(mixer, nwg_panel.common.defaults["master"])
max_vol = element.get_volume_range()[1]
element.set_volume_all(int(percent * max_vol / 100))
except Exception as e:
eprint(e)
del mixer
percent = int(slider.get_value())
if is_command("pamixer"):
subprocess.call("pamixer --set-volume {}".format(percent).split())
else:
c = "amixer sset {}".format(nwg_panel.common.defaults["master"])
cmd = "{} {}% /dev/null 2>&1".format(c, percent)
try:
subprocess.call(cmd.split())
except Exception as e:
eprint(e)
eprint("Required 'pamixer' command not found")


def get_brightness():
Expand Down

0 comments on commit 0501274

Please sign in to comment.