|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# This code is an example for a tutorial on Ubuntu Unity/Gnome AppIndicators: |
| 4 | +# http://candidtim.github.io/appindicator/2014/09/13/ubuntu-appindicator-step-by-step.html |
| 5 | +# https://gist.github.com/candidtim/7290a1ad6e465d680b68 |
| 6 | + |
| 7 | +import os |
| 8 | +import signal |
| 9 | +import json |
| 10 | +import subprocess |
| 11 | +import re |
| 12 | +import gi |
| 13 | + |
| 14 | +from urllib2 import Request, urlopen # URLError |
| 15 | + |
| 16 | +gi.require_version('Gtk', '3.0') |
| 17 | +gi.require_version('AppIndicator3', '0.1') |
| 18 | +gi.require_version('Notify', '0.7') |
| 19 | + |
| 20 | +from gi.repository import Gtk as gtk |
| 21 | +from gi.repository import AppIndicator3 as appindicator |
| 22 | +from gi.repository import Notify as notify |
| 23 | + |
| 24 | + |
| 25 | +APPINDICATOR_ID = 'micindicator' |
| 26 | +# amixer get Capture | egrep 'Front Left.*?\[o' | egrep -o '\[o.+\]' |
| 27 | + |
| 28 | +class Indicator(): |
| 29 | + def __init__(self): |
| 30 | + self.indicator = appindicator.Indicator.new(APPINDICATOR_ID, self.get_current_state_icon(), appindicator.IndicatorCategory.SYSTEM_SERVICES) |
| 31 | + self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE) |
| 32 | + self.indicator.set_menu(self.build_menu()) |
| 33 | + notify.init(APPINDICATOR_ID) |
| 34 | + |
| 35 | + def get_current_state_icon(self): |
| 36 | + if self.get_current_mic_state() == "[off]": |
| 37 | + icon_name = 'mic-mute.png' |
| 38 | + else: |
| 39 | + icon_name = 'mic-on.png' |
| 40 | + return os.path.join(os.path.dirname(os.path.abspath(__file__)), icon_name) |
| 41 | + |
| 42 | + def get_current_mic_state(self): |
| 43 | + ps = subprocess.Popen(("amixer", "get", "Capture"), stdout=subprocess.PIPE) |
| 44 | + output = subprocess.check_output(('egrep', '-o', '\[o.+\]', '-m', '1'), stdin=ps.stdout) |
| 45 | + ps.wait() |
| 46 | + return filter(lambda x: not re.match(r'^\s*$', x), output) |
| 47 | + |
| 48 | + |
| 49 | + def build_menu(self): |
| 50 | + menu = gtk.Menu() |
| 51 | + |
| 52 | + self.item_toggle = gtk.MenuItem('Toggle Microphone') |
| 53 | + self.item_toggle.connect('activate', self.toggle_mic) |
| 54 | + menu.append(self.item_toggle) |
| 55 | + |
| 56 | + self.update_menu_toggle_label() |
| 57 | + |
| 58 | + item_quit1 = gtk.MenuItem('Quit') |
| 59 | + item_quit1.connect('activate', self.quit1) |
| 60 | + menu.append(item_quit1) |
| 61 | + |
| 62 | + menu.show_all() |
| 63 | + return menu |
| 64 | + |
| 65 | + |
| 66 | + def update_menu_toggle_label(self): |
| 67 | + if self.get_current_mic_state() == "[off]": |
| 68 | + self.item_toggle.set_label("Turn Microphone On") |
| 69 | + else: |
| 70 | + self.item_toggle.set_label("Turn Microphone Off") |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + def toggle_mic(self, _): |
| 75 | + subprocess.call('amixer set Capture toggle', shell=True) |
| 76 | + self.indicator.set_icon(self.get_current_state_icon()) |
| 77 | + self.update_menu_toggle_label() |
| 78 | + |
| 79 | + |
| 80 | + def quit1(self, _): |
| 81 | + notify.uninit() |
| 82 | + gtk.main_quit() |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + Indicator() |
| 86 | + signal.signal(signal.SIGINT, signal.SIG_DFL) |
| 87 | + gtk.main() |
| 88 | + |
0 commit comments