我最近從 14.04 Trusty 升級到 16.04 Xenial。在升級之前,我使用caffeine-plus
指示器圖示來告訴我的筆記型電腦何時可以睡眠。我通常使用的模式是啟用咖啡因,這樣計算機只會在蓋子關閉時才會睡眠/掛起。然而,有時我希望能夠讓空閒計時器發揮其預期的效果。
升級後,咖啡因似乎不再執行任何操作。我可以讓我的電腦運行一個長時間運行的進程,故意保持蓋子打開,結果回來時發現它正在睡覺並且進程未完成。
我怎樣才能恢復以前的行為?請注意,我正在尋找切換,不是永久性的改變。作為一個開關,它應該有一個視覺指示來表明它是否啟用。如果有一個指示器圖示就太好了。
筆記
在提出這個問題之前我所做的搜索要么是:a)關於如何使用咖啡因的舊(過時)帖子,要么b)永久禁用睡眠以解決各種硬件錯誤。我的問題只是關於恢復 14.04 中的功能,這是我沒有找到解決的主題。
答案1
編輯
經過一番工作後,我寫了一個比下面找到的更完整且更易於使用的解決方案。你可以下載程式在 GitHub 上。您還需要安裝依賴項:
sudo apt install xdotool xprintidle
原答案
在 Jacob Vlijm 向我指出部分解決方案,我將他的腳本的修改版本與一些咖啡因和我自己的一些程式碼結合起來,並提出了咖啡因的替代品。
指示
確保安裝了必要的軟體包。注意:
caffeine-plus
僅用於圖示。如果您不關心正確的圖標,則不需要它。sudo apt install caffeine-plus xprintidle xdotool
將以下腳本保存在某處並使其可執行。
#!/usr/bin/python3 # coding=utf-8 # # Copyright © 2016 Scott Severance # Code mixed in from Caffeine Plus and Jacob Vlijm # # 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 argparse import os import signal import time from subprocess import Popen, check_output, call import gi gi.require_version('Gtk', '3.0') gi.require_version('AppIndicator3', '0.1') from gi.repository import GObject, Gtk, AppIndicator3 class SleepInhibit(GObject.GObject): def __init__(self): GObject.GObject.__init__(self) self.inhibited = False self._add_indicator() self.inhibit_proc = None def _add_indicator(self): self.AppInd = AppIndicator3.Indicator.new("sleep-inhibit-off", "sleep-inhibit", AppIndicator3.IndicatorCategory.APPLICATION_STATUS) self.AppInd.set_status(AppIndicator3.IndicatorStatus.ACTIVE) self.AppInd.set_icon ("caffeine-cup-empty") self._build_indicator_menu(self.AppInd) def _build_indicator_menu(self, indicator): menu = Gtk.Menu() menu_item = Gtk.MenuItem("Toggle Sleep Inhibition") menu.append(menu_item) menu_item.connect("activate", self.on_toggle) menu_item.show() menu_item = Gtk.MenuItem("Quit") menu.append(menu_item) menu_item.connect("activate", self.on_quit) menu_item.show() indicator.set_menu(menu) def on_toggle(self, menuitem): self.inhibited = not self.inhibited self.flip_switch() def on_quit(self, menuitem): if self.inhibit_proc: self.kill_inhibit_proc() exit(0) def _set_icon_disabled(self): self.AppInd.set_icon('caffeine-cup-empty') def _set_icon_enabled(self): self.AppInd.set_icon('caffeine-cup-full') def flip_switch(self): if self.inhibited: self._set_icon_enabled() self.inhibit_proc = Popen([__file__, "--mode=inhibit-process"]) else: self.kill_inhibit_proc() self._set_icon_disabled() def kill_inhibit_proc(self): self.inhibit_proc.terminate() self.inhibit_proc.wait() self.inhibit_proc = None def inhibit(): seconds = 120 # number of seconds to start preventing blank screen / suspend while True: try: curr_idle = check_output(["xprintidle"]).decode("utf-8").strip() if int(curr_idle) > seconds*1000: call(["xdotool", "key", "Control_L"]) time.sleep(10) except FileNotFoundError: exit('ERROR: This program depends on xprintidle and xdotool being installed.') except KeyboardInterrupt: exit(0) def parse_args(): parser = argparse.ArgumentParser(description='''Indicator to prevent computer from sleeping. It depends on the commands xprintidle and xdotool being properly installed on your system. If they aren't installed already, please install them. Also, the icons are taken from caffeine-plus, so if it isn't installed, you will probably see a broken icon.''') mode = '''The mode can be either indicator (default) or inhibit-process. If mode is indicator, then an indicator icon is created. inhibit-process is to be called by the indicator. When sleep is inhibited, it runs, preventing sleep.''' parser.add_argument('--mode', type=str, default='indicator', help=mode) return parser.parse_args() def main(): args = parse_args() if args.mode == 'indicator': signal.signal(signal.SIGINT, signal.SIG_DFL) GObject.threads_init() SleepInhibit() Gtk.main() elif args.mode == 'inhibit-process': inhibit() else: exit('ERROR: Invalid value for --mode!') if __name__ == '__main__': main()
運行腳本。