aboutsummaryrefslogtreecommitdiffstats
path: root/src/blueshift-tray.py
blob: cb5118ed2ffca3be63ffbfadfd6ea46cf34b210b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

# blueshift-tray – Systray wrapper for Blueshift
# Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
# 
# 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 3 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, see <http://www.gnu.org/licenses/>.

import sys
import time
import signal
import subprocess

import pygtk
import gettext

pygtk.require("2.0")
import gtk, glib


LOCALEDIR = '/usr/share/locale'
gettext.bindtextdomain('blueshift', LOCALEDIR)
gettext.textdomain('blueshift')


def process_quit(signum, frame):
    '''
    Invoked when a child process exits or continues
    
    @param  signum:int   The signal (SIGCHLD)
    @param  frame:=None  Will probably be `None`
    '''
    global running, frozen_sig
    if frozen_sig:
        frozen_sig = False
        return
    process.wait()
    if running:
        running = False
        icon.set_visible(False)
        gtk.main_quit()
    time.sleep(0.1)
    sys.exit(0)


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() is None:
                    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
    @return  :gtk.MenuItem|gtk.ImageMenuItem       The created menu
    '''
    if image is None:
        menu_item = gtk.MenuItem(gettext.gettext(title))
    else:
        menu_item = gtk.ImageMenuItem(image)
        if title is not None:
            menu_item.set_label(gettext.gettext(title))
    if function is not None:
        menu_item.connect('activate', function)
    menu.append(menu_item)
    return 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
    paused = not paused
    process.send_signal(signal.SIGUSR2)
    icon.set_from_icon_name('blueshift-off' if paused else 'blueshift-on')
    toggle_menu.set_label(gettext.gettext('_Enable' if paused else '_Disable'))

def f_suspend(widget, data = None):
    global frozen, frozen_sig, last_time
    now = time.time()
    if now < last_time + 0.2:
        return
    last_time = now
    frozen = not frozen
    frozen_sig = True
    process.send_signal(signal.SIGTSTP if frozen else signal.SIGCONT)
    suspend_menu.set_label(gettext.gettext('_Thaw' if frozen else '_Freeze'))

def f_reload(widget, data = None):
    process.send_signal(signal.SIGUSR1)

def f_quit(widget, data = None):
    global running
    if running:
        running = False
        icon.set_visible(False)
        gtk.main_quit()
        term(1)
        process.wait()

def f_panic_quit(widget, data = None):
    global running
    if running:
        running = False
        icon.set_visible(False)
        gtk.main_quit()
        term(2, True)


frozen_sig = False
running = True
paused = False
frozen = False

signal.signal(signal.SIGCHLD, process_quit)

process = subprocess.Popen(['blueshift'] + sys.argv[1:], stdout = sys.stdout, stderr = sys.stderr)
last_time = time.time() - 1


try:
    icon = gtk.StatusIcon()
    icon.set_from_icon_name('blueshift-on')
    icon.set_tooltip('Blueshift')
    
    menu = gtk.Menu()
    toggle_menu     = create_menu(menu, None, '_Disable', f_toggle)
    suspend_menu    = create_menu(menu, None, '_Freeze', f_suspend)
    reload_menu     = create_menu(menu, gtk.STOCK_REFRESH, '_Reload', f_reload)
    create_menu(menu, None, None, None)
    quit_menu       = create_menu(menu, gtk.STOCK_QUIT, '_Quit', f_quit)
    panic_quit_menu = create_menu(menu, gtk.STOCK_QUIT, '_Panic Quit', f_panic_quit)
    
    icon.connect('activate', f_toggle)
    icon.connect('popup-menu', f_popup)
    
    icon.set_visible(True)
    gtk.main()

except KeyboardInterrupt:
    running = False
    icon.set_visible(False)
    term()
    if paused is not None:
        time.sleep(0.1)
        term()

finally:
    try:
        process.wait()
    except KeyboardInterrupt:
        term()

if process.poll() is None:
    time.sleep(0.1)
    if process.poll() is None:
        process.send_signal(signal.SIGKILL)