| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Copyright © 2009-2020 The Caffeine Developers |
| 4 |
# |
| 5 |
# This program is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
import argparse |
| 19 |
import builtins |
| 20 |
import gettext |
| 21 |
import importlib.metadata |
| 22 |
import locale |
| 23 |
import logging |
| 24 |
import signal |
| 25 |
import sys |
| 26 |
from os.path import abspath, dirname, exists, join, pardir |
| 27 |
from subprocess import call |
| 28 |
|
| 29 |
import gi |
| 30 |
from Xlib import display |
| 31 |
|
| 32 |
|
| 33 |
gi.require_version('Gtk', '3.0') |
| 34 |
gi.require_version('AyatanaAppIndicator3', '0.1') |
| 35 |
from gi.repository import AyatanaAppIndicator3 as AppIndicator3 |
| 36 |
from gi.repository import GLib, GObject, Gtk |
| 37 |
|
| 38 |
|
| 39 |
PROGRAM_NAME = "caffeine-indicator" |
| 40 |
VERSION = importlib.metadata.version("cups_of_caffeine") |
| 41 |
|
| 42 |
# Register the gettext function for the whole interpreter as "_" |
| 43 |
builtins._ = gettext.gettext |
| 44 |
|
| 45 |
def get_base_path(): |
| 46 |
c = abspath(dirname(__file__)) |
| 47 |
while True: |
| 48 |
if exists(join(c, "share", PROGRAM_NAME)): |
| 49 |
return c |
| 50 |
|
| 51 |
c = join(c, pardir) |
| 52 |
if not exists(c): |
| 53 |
raise Exception("Can't determine BASE_PATH") |
| 54 |
|
| 55 |
BASE_PATH = get_base_path() |
| 56 |
GLADE_PATH = join(BASE_PATH, 'share', PROGRAM_NAME, 'glade') |
| 57 |
|
| 58 |
# Set up translations |
| 59 |
LOCALE_PATH = join(BASE_PATH, "share", "locale") |
| 60 |
|
| 61 |
locale.setlocale(locale.LC_ALL, '') |
| 62 |
|
| 63 |
for module in locale, gettext: |
| 64 |
module.bindtextdomain(PROGRAM_NAME, LOCALE_PATH) |
| 65 |
module.textdomain(PROGRAM_NAME) |
| 66 |
|
| 67 |
# Handle command line arguments |
| 68 |
parser = argparse.ArgumentParser(prog=PROGRAM_NAME, description='Toggle desktop idleness inhibition') |
| 69 |
parser.add_argument('-V', '--version', action='version', version=PROGRAM_NAME + ' ' + VERSION) |
| 70 |
parser.parse_args() |
| 71 |
|
| 72 |
class GUI: |
| 73 |
def __init__(self, caffeine): |
| 74 |
self.Caffeine = caffeine |
| 75 |
self.Caffeine.connect("activation-toggled", self.on_activation_toggled) |
| 76 |
self.labels = [_("Activate"), _("Deactivate")] |
| 77 |
|
| 78 |
builder = Gtk.Builder() |
| 79 |
builder.add_from_file(join(GLADE_PATH, "GUI.glade")) |
| 80 |
|
| 81 |
get = builder.get_object |
| 82 |
|
| 83 |
self.AppInd = AppIndicator3.Indicator.new("caffeine-cup-empty", |
| 84 |
"caffeine", |
| 85 |
AppIndicator3.IndicatorCategory.APPLICATION_STATUS) |
| 86 |
self.AppInd.set_status(AppIndicator3.IndicatorStatus.ACTIVE) |
| 87 |
|
| 88 |
self.activate_menuitem = get("activate_menuitem") |
| 89 |
self.set_icon_is_activated(self.Caffeine.get_activated()) |
| 90 |
|
| 91 |
# Popup menu |
| 92 |
self.menu = get("popup_menu") |
| 93 |
self.menu.show() |
| 94 |
self.AppInd.set_menu(self.menu) |
| 95 |
|
| 96 |
# About dialog |
| 97 |
self.about_dialog = get("aboutdialog") |
| 98 |
self.about_dialog.set_version(VERSION) |
| 99 |
self.about_dialog.set_translator_credits(_("translator-credits")) |
| 100 |
|
| 101 |
# Activate menu item shortcut |
| 102 |
self.AppInd.set_secondary_activate_target(self.activate_menuitem) |
| 103 |
|
| 104 |
builder.connect_signals(self) |
| 105 |
|
| 106 |
def on_activation_toggled(self, source, active, tooltip): |
| 107 |
self.set_icon_is_activated(active) |
| 108 |
|
| 109 |
def set_icon_is_activated(self, activated): |
| 110 |
# Toggle the icon, indexing with a bool. |
| 111 |
icon_name = ["caffeine-cup-empty", "caffeine-cup-full"][activated] |
| 112 |
self.AppInd.set_icon_full(icon_name, "Caffeine is {}".format("activated" if activated else "deactivated")) |
| 113 |
self.activate_menuitem.set_label(self.labels[self.Caffeine.get_activated()]) |
| 114 |
|
| 115 |
# Menu callbacks |
| 116 |
def on_activate_menuitem_activate(self, menuitem, data=None): |
| 117 |
self.Caffeine.toggle_activated() |
| 118 |
menuitem.set_label(self.labels[self.Caffeine.get_activated()]) |
| 119 |
|
| 120 |
def on_about_menuitem_activate(self, menuitem, data=None): |
| 121 |
self.about_dialog.set_position(Gtk.WindowPosition.CENTER_ALWAYS) |
| 122 |
self.about_dialog.run() |
| 123 |
self.about_dialog.hide() |
| 124 |
|
| 125 |
def on_quit_menuitem_activate(self, menuitem, data=None): |
| 126 |
# Make sure desktop idleness is uninhibited |
| 127 |
self.Caffeine.release() |
| 128 |
Gtk.main_quit() |
| 129 |
|
| 130 |
def make_unmapped_window(wm_name): |
| 131 |
screen = display.Display().screen() |
| 132 |
window = screen.root.create_window(0, 0, 1, 1, 0, screen.root_depth) |
| 133 |
window.set_wm_name(wm_name) |
| 134 |
window.set_wm_protocols([]) |
| 135 |
return window |
| 136 |
|
| 137 |
class Caffeine(GObject.GObject): |
| 138 |
def __init__(self): |
| 139 |
GObject.GObject.__init__(self) |
| 140 |
self.window = make_unmapped_window("Caffeine indicator") |
| 141 |
self.status_string = None |
| 142 |
self.screenSaverWindowID = None |
| 143 |
|
| 144 |
def get_activated(self): |
| 145 |
return self.screenSaverWindowID is not None |
| 146 |
|
| 147 |
def toggle_activated(self): |
| 148 |
if self.screenSaverWindowID is None: |
| 149 |
self.screenSaverWindowID = hex(self.window.id) |
| 150 |
self.status_string = _(PROGRAM_NAME + " is inhibiting desktop idleness") |
| 151 |
logging.info(self.status_string) |
| 152 |
call(['xdg-screensaver', 'suspend', self.screenSaverWindowID]) |
| 153 |
else: |
| 154 |
self.release() |
| 155 |
|
| 156 |
self.emit("activation-toggled", self.get_activated(), self.status_string) |
| 157 |
|
| 158 |
def release(self): |
| 159 |
if self.screenSaverWindowID is not None: |
| 160 |
call(['xdg-screensaver', 'resume', self.screenSaverWindowID]) |
| 161 |
self.screenSaverWindowID = None |
| 162 |
self.status_string = _(PROGRAM_NAME + " is inactive") |
| 163 |
logging.info(self.status_string) |
| 164 |
|
| 165 |
# Adapted from http://stackoverflow.com/questions/26388088/python-gtk-signal-handler-not-working |
| 166 |
def InitSignal(gui): |
| 167 |
def signal_action(signal): |
| 168 |
caffeine.release() |
| 169 |
sys.exit(1) |
| 170 |
|
| 171 |
def idle_handler(*args): |
| 172 |
GLib.idle_add(signal_action, priority=GLib.PRIORITY_HIGH) |
| 173 |
|
| 174 |
def handler(*args): |
| 175 |
signal_action(args[0]) |
| 176 |
|
| 177 |
def install_glib_handler(sig): |
| 178 |
# GLib.unix_signal_add was added in glib 2.36 |
| 179 |
GLib.unix_signal_add(GLib.PRIORITY_HIGH, sig, handler, sig) |
| 180 |
|
| 181 |
for sig in [signal.SIGINT, signal.SIGTERM, signal.SIGHUP]: |
| 182 |
signal.signal(sig, idle_handler) |
| 183 |
GLib.idle_add(install_glib_handler, sig, priority=GLib.PRIORITY_HIGH) |
| 184 |
|
| 185 |
|
| 186 |
# Set up and run |
| 187 |
logging.basicConfig(level=logging.INFO) |
| 188 |
GObject.signal_new("activation-toggled", Caffeine, |
| 189 |
GObject.SignalFlags.RUN_FIRST, None, [bool, str]) |
| 190 |
caffeine = Caffeine() |
| 191 |
gui = GUI(caffeine) |
| 192 |
InitSignal(gui) |
| 193 |
Gtk.main() |