diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-02-22 07:08:50 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-02-22 07:08:50 +0100 |
commit | 1e357e284d132664682ce63306268aa3c9869b8a (patch) | |
tree | 7f7c5413adf3d0515facf16c7f867abfb7708a2c | |
parent | dist (diff) | |
download | blueshift-tray-1.0.tar.gz blueshift-tray-1.0.tar.bz2 blueshift-tray-1.0.tar.xz |
fix m bugs1.0
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rwxr-xr-x | src/blueshift-tray.py | 91 |
1 files changed, 74 insertions, 17 deletions
diff --git a/src/blueshift-tray.py b/src/blueshift-tray.py index e4be23e..c611fc8 100755 --- a/src/blueshift-tray.py +++ b/src/blueshift-tray.py @@ -18,6 +18,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import sys +import time import signal import subprocess @@ -34,19 +35,51 @@ gettext.textdomain('blueshift') def process_quit(signum, frame): + ''' + Invoked when a child process exists + + @param signum:int The signal (SIGCHLD) + @param frame:=None Will probably be `None` + ''' global running process.wait() if running: running = False icon.set_visible(False) gtk.main_quit() + time.sleep(0.1) + sys.exit(0) -signal.signal(signal.SIGCHLD, process_quit) -process = subprocess.Popen(['blueshift'] + sys.argv[1:]) -running = True + +def term(count = 1, kill = False): + ''' + Terminate the blueshift if alive + + @param count:int Number of times to send SIGTERM + @param kill:bool Whether to also send SIGKILL and the exit + ''' + if process is not None: + process.send_signal(signal.SIGTERM) + if count > 1: + for i in range(count - 1): + if process.poll(): + time.sleep(0.1) + process.send_signal(signal.SIGTERM) + if kill: + time.sleep(0.1) + process.send_signal(signal.SIGKILL) + sys.exit(0) def create_menu(menu, image, title, function): + ''' + Create a menu item + + @param menu:gtk.Menu The menu to place the item inside + @param image:str? The icon on the menu item + @param title:str? The text on the menu item + @param function:(gtk.Widget, (=None))→void The function invoked when the item is pressed + ''' if image is None: menu_item = gtk.MenuItem(gettext.gettext(title)) else: @@ -57,12 +90,25 @@ def create_menu(menu, image, title, function): menu.append(menu_item) +def f_popup(widget, button, time, data = None): + ''' + Invoked to open a popup menu + ''' + menu.show_all() + menu.popup(None, None, gtk.status_icon_position_menu, button, time, icon) + + def f_toggle(widget, data = None): + global paused, last_time + now = time.time() + if now < last_time + 0.2: + return + last_time = now + if paused is None: + paused = False process.send_signal(signal.SIGUSR2) - if icon.get_icon_name() == 'blueshift-on': - icon.set_from_icon_name('blueshift-off') - else: - icon.set_from_icon_name('blueshift-on') + icon.set_from_icon_name('blueshift-on' if paused else 'blueshift-off') + paused = not paused def f_reload(widget, data = None): process.send_signal(signal.SIGUSR1) @@ -73,7 +119,12 @@ def f_quit(widget, data = None): running = False icon.set_visible(False) gtk.main_quit() - process.send_signal(signal.SIGTERM) + if paused is None: + term() + elif paused: + term(2, True) + else: + term(2, True) def f_panic_quit(widget, data = None): global running @@ -81,15 +132,15 @@ def f_panic_quit(widget, data = None): running = False icon.set_visible(False) gtk.main_quit() - process.send_signal(signal.SIGTERM) - import time - time.sleep(0.01) - process.send_signal(signal.SIGTERM) + term(2, True) -def f_popup(widget, button, time, data = None): - menu.show_all() - menu.popup(None, None, gtk.status_icon_position_menu, button, time, icon) +signal.signal(signal.SIGCHLD, process_quit) + +process = subprocess.Popen(['blueshift'] + sys.argv[1:]) +running = True +paused = None +last_time = time.time() - 1 try: @@ -113,11 +164,17 @@ try: except KeyboardInterrupt: running = False icon.set_visible(False) - process.send_signal(signal.SIGTERM) + term() + if paused is not None: + time.sleep(0.1) + term() finally: try: process.wait() except KeyboardInterrupt: - process.send_signal(signal.SIGTERM) + term() + +if process.poll(): + process.send_signal(signal.KILL) |