61
Scripts / gtk3 Volume Control for tint2
« on: December 17, 2015, 01:24:04 AM »
My attempt to make a volume control context menu that launches from tint2.
Uses amixer
It wasn't showing properly untill I've installed libgtk-3-0
Here is the launcher =>
/usr/local/bin/tint2launchers/07-sound.desktop
py script
/usr/local/bin/menu.py
There is a mute switch but will work as a button also.
reference from here >>> http://vsido.org/index.php?topic=1089.msg12301#msg12301
Uses amixer
It wasn't showing properly untill I've installed libgtk-3-0
Here is the launcher =>
/usr/local/bin/tint2launchers/07-sound.desktop
Code: [Select]
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=Sound
Exec=python3 /usr/local/bin/menu.py
Icon=audio-headphones
NoDisplay=false
py script
/usr/local/bin/menu.py
Code: [Select]
#!/usr/bin/env python3
# Milos Pavlovic 2015 <mpsrbija@gmail.com>
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import os
import sys
import string
import subprocess
import shlex
from gi import require_version
require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository.GdkPixbuf import Pixbuf
def execute(command):
"""function to exec switch"""
p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return p.stdout
class Menu:
def destroy(self, widget, data=None):
Gtk.main_quit()
def action(self, widget, event, execc, data=None):
command = execc
subprocess.Popen(shlex.split(command), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
Gtk.main_quit()
def __init__(self):
self.menu = Gtk.Menu()
""" This is how we close the script
connecting to the hide signal
"""
self.menu.connect("hide", self.destroy)
it=Gtk.IconTheme.get_default()
height = 100 # Height workaround
button= Gtk.Switch()
button.connect("notify::active", self.on_switch_activated)
cmd = 'amixer get Master | grep -q off ; echo $?'
result1 = os.popen(cmd)
result1 = result1.readline()
if int(result1) == 0:
button.set_active(False)
else:
button.set_active(True)
box = Gtk.Box(homogeneous =True)
img = Gtk.Image(xalign=0)
pixbuf=it.load_icon("emblem-sound",24,0)
img.set_from_pixbuf(pixbuf)
box.add(img)
box.add(button)
menuitem = Gtk.MenuItem()
menuitem.add(box)
menuitem.connect("button-press-event", self.mute_clicked)
menuitem.show()
self.menu.append(menuitem)
# adjustment (initial value, min value, max value,
# step increment - press cursor keys to see!,
# page increment - click around the handle to see!,
# page size - not used here)
ad1 = Gtk.Adjustment(0, 0, 100, 5, 10, 0)
cmd = "/usr/bin/amixer sget Master | grep '\[' "
result = os.popen(cmd)
result = result.readline()
find_start = result.find('[') + 1
find_end = result.find('%]', find_start)
audio_level = int(result[find_start:find_end])
ad1. set_value(audio_level)
# an horizontal scale
self.h_scale = Gtk.Scale(
orientation=Gtk.Orientation.HORIZONTAL, adjustment=ad1)
# of integers (no digits)
self.h_scale.set_digits(0)
# that can expand horizontally if there is space in the grid
self.h_scale.set_hexpand(True)
# that is aligned at the top of the space allowed in the grid
self.h_scale.set_valign(Gtk.Align.START)
# we connect the signal "value-changed" emitted by the scale with the callback
# function scale_moved
self.h_scale.connect("value-changed", self.scale_moved)
box = Gtk.Box()
box.add(self.h_scale)
menuitem = Gtk.MenuItem()
menuitem.add(box)
menuitem.show()
self.menu.append(menuitem)
self.menu.set_size_request(180, height) # Workaround for height
self.menu.popup(None, None, None, None, 0, Gtk.get_current_event_time())
self.menu.show_all()
# Let's set the voulume here
def scale_moved(self, event):
val = "{0}".format(int(self.h_scale.get_value()))
proc = subprocess.Popen('/usr/bin/amixer sset Master ' + str(val) + '% 2>&1 >/dev/null', shell=True, stdout=subprocess.PIPE)
proc.wait()
def on_switch_activated(self, switch, gparam):
if switch.get_active():
execute('amixer set Master unmute 2>&1 >/dev/null')
state = "on"
else:
execute('amixer set Master mute 2>&1 >/dev/null')
state = "off"
# Here we toogle the mute on and off when menuitem is clicked
def mute_clicked(self, event, data=None):
val = "toggle"
proc = subprocess.Popen('/usr/bin/amixer set Master ' + str(val) + ' 2>&1 >/dev/null', shell=True, stdout=subprocess.PIPE)
proc.wait()
def main(self):
Gtk.main()
if __name__ == "__main__":
app = Menu()
app.main()
The script closes when clicked outside of the menu.There is a mute switch but will work as a button also.
reference from here >>> http://vsido.org/index.php?topic=1089.msg12301#msg12301