Description
Petit script pour avoir une icône dans la barre de notifications et régler le volume sonore. Il est destiné aux environnements minimalistes comme fluxbox, et ne dépend donc de rien en particulier sauf python et gi.
Il est tout vilain, n’hésitez pas à l’améliorer ;)
#!/usr/local/bin/python3
import os
import sys
from gi import require_version
require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
name="Volume"
def get_lvl():
cmd = "mixerctl -n outputs.master"
result = os.popen(cmd)
result = result.readline().split(",")[0]
lvl= int(int(result)/255*100)
return lvl
def get_mute():
cmd = "mixerctl -n outputs.master.mute"
result = os.popen(cmd)
result = result.readline().strip()
return(result)
class Menu:
def destroy(self, widget, data=None):
Gtk.main_quit()
def set_icon(self):
if get_mute() == "on":
self.tray.set_from_icon_name("stock_volume-mute")
else:
lvl = get_lvl()
if lvl == 0:
self.tray.set_from_icon_name("stock_volume-0")
elif lvl < 20:
self.tray.set_from_icon_name("stock_volume-min")
elif lvl < 70:
self.tray.set_from_icon_name("stock_volume-med")
elif lvl > 70:
self.tray.set_from_icon_name("stock_volume-max")
def __init__(self):
get_mute()
self.tray = Gtk.StatusIcon()
self.set_icon()
self.tray.connect('popup-menu', self.on_right_click)
self.tray.connect('activate', self.on_left_click)
self.tray.connect('scroll_event', self.on_scroll)
self.tray.set_tooltip_text('Volume : {}%'.format(get_lvl()))
def on_scroll(self, widget, event):
if event.direction == Gdk.ScrollDirection.DOWN:
os.system("mixerctl outputs.master=-10")
elif event.direction == Gdk.ScrollDirection.UP:
os.system("mixerctl outputs.master=+10")
self.set_icon()
def on_left_click(self, event):
os.system("mixerctl outputs.master.mute=toggle")
self.set_icon()
def on_right_click(self, icon, button, time):
# Quit-menu item
self.menu = Gtk.Menu()
box = Gtk.Box()
box.set_spacing(10)
img = Gtk.Image()
img.set_from_stock(Gtk.STOCK_CANCEL, Gtk.IconSize.MENU)
label = Gtk.Label("Quit")
box.add(img)
box.add(label)
quit = Gtk.MenuItem()
quit.add(box)
quit.connect("activate", Gtk.main_quit)
self.menu.append(quit)
self.menu.show_all()
self.menu.popup(None, None, None, self.tray, button, time)
def main(self):
Gtk.main()
if __name__ == "__main__":
app = Menu()
app.main()
Historique
J’ai écrit historiquement cette documentation de manière collaborative sur le wiki de la communauté “OpenBSD Pour Tous”.