aboutsummaryrefslogtreecommitdiffstats
path: root/src/redshift-gtk
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/redshift-gtk/statusicon.py71
1 files changed, 43 insertions, 28 deletions
diff --git a/src/redshift-gtk/statusicon.py b/src/redshift-gtk/statusicon.py
index b766175..9d9835d 100644
--- a/src/redshift-gtk/statusicon.py
+++ b/src/redshift-gtk/statusicon.py
@@ -29,11 +29,15 @@ import signal
import re
import gettext
+import gi
+gi.require_version('Gtk', '3.0')
+
from gi.repository import Gtk, GLib, GObject
try:
+ gi.require_version('AppIndicator3', '0.1')
from gi.repository import AppIndicator3 as appindicator
-except ImportError:
+except (ImportError, ValueError):
appindicator = None
from . import defs
@@ -139,9 +143,13 @@ class RedshiftController(GObject.GObject):
if inhibit != self._inhibited:
self._child_toggle_inhibit()
+ def _child_signal(self, sg):
+ """Send signal to child process."""
+ os.kill(self._process[0], sg)
+
def _child_toggle_inhibit(self):
'''Sends a request to the child process to toggle state'''
- os.kill(self._process[0], signal.SIGUSR1)
+ self._child_signal(signal.SIGUSR1)
def _child_cb(self, pid, status, data=None):
'''Called when the child process exists'''
@@ -159,13 +167,12 @@ class RedshiftController(GObject.GObject):
report_errors = False
try:
GLib.spawn_check_exit_status(status)
- Gtk.main_quit()
except GLib.GError:
- report_errors = True
-
- if report_errors:
self.emit('error-occured', self._errors)
+ GLib.spawn_close_pid(self._process[0])
+ Gtk.main_quit()
+
def _child_key_change_cb(self, key, value):
'''Called when the child process reports a change of internal state'''
@@ -223,14 +230,13 @@ class RedshiftController(GObject.GObject):
return True
- def termwait(self):
- '''Send SIGINT and wait for the child process to quit'''
- try:
- os.kill(self._process[0], signal.SIGINT)
- os.waitpid(self._process[0], 0)
- except ProcessLookupError:
- # Process has apparently already disappeared
- pass
+ def terminate_child(self):
+ """Send SIGINT to child process."""
+ self._child_signal(signal.SIGINT)
+
+ def kill_child(self):
+ """Send SIGKILL to child process."""
+ self._child_signal(signal.SIGKILL)
class RedshiftStatusIcon(object):
@@ -468,15 +474,23 @@ class RedshiftStatusIcon(object):
def change_temperature(self, temperature):
'''Change interface to new temperature'''
self.temperature_label.set_markup('<b>{}:</b> {}K'.format(_('Color temperature'), temperature))
+ self.update_tooltip_text()
def change_period(self, period):
'''Change interface to new period'''
self.period_label.set_markup('<b>{}:</b> {}'.format(_('Period'), period))
+ self.update_tooltip_text()
def change_location(self, location):
'''Change interface to new location'''
self.location_label.set_markup('<b>{}:</b> {}, {}'.format(_('Location'), *location))
+ def update_tooltip_text(self):
+ '''Update text of tooltip status icon '''
+ if not appindicator:
+ self.status_icon.set_tooltip_text('{}: {}K, {}: {}'.format(
+ _('Color temperature'), self._controller.temperature,
+ _('Period'), self._controller.period))
def autostart_cb(self, widget, data=None):
'''Callback when a request to toggle autostart is made'''
@@ -486,35 +500,36 @@ class RedshiftStatusIcon(object):
'''Callback when a request to quit the application is made'''
if not appindicator:
self.status_icon.set_visible(False)
- Gtk.main_quit()
+ self._controller.terminate_child()
return False
-def sigterm_handler(data=None):
- sys.exit(0)
-
-
def run():
utils.setproctitle('redshift-gtk')
- # Install TERM signal handler
- GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGTERM,
- sigterm_handler, None)
- GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT,
- sigterm_handler, None)
-
# Internationalisation
gettext.bindtextdomain('redshift', defs.LOCALEDIR)
gettext.textdomain('redshift')
# Create redshift child process controller
c = RedshiftController(sys.argv[1:])
+
+ def terminate_child(data=None):
+ c.terminate_child()
+ return False
+
+ # Install signal handlers
+ GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGTERM,
+ terminate_child, None)
+ GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT,
+ terminate_child, None)
+
try:
# Create status icon
s = RedshiftStatusIcon(c)
# Run main loop
Gtk.main()
- finally:
- # Always make sure that the child process is closed
- c.termwait()
+ except:
+ c.kill_child()
+ raise