summaryrefslogtreecommitdiff
path: root/plugins/screen_detach.py
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/screen_detach.py')
-rw-r--r--plugins/screen_detach.py87
1 files changed, 70 insertions, 17 deletions
diff --git a/plugins/screen_detach.py b/plugins/screen_detach.py
index 53827c11..2b42d01a 100644
--- a/plugins/screen_detach.py
+++ b/plugins/screen_detach.py
@@ -1,35 +1,91 @@
"""
This plugin will set your status to **away** if you detach your screen.
+The default behaviour is to check for both tmux and screen (in that order).
+
+Configuration options
+---------------------
+
+.. glossary::
+
+ use_screen
+ **Default:** ``true``
+
+ Try to find an attached screen.
+
+ use_tmux
+ **Default:** ``true``
+
+ Try to find and attached tmux.
+
"""
+
from plugin import BasePlugin
import os
import stat
import pyinotify
+import asyncio
+
+DEFAULT_CONFIG = {
+ 'screen_detach': {
+ 'use_tmux': True,
+ 'use_screen': True
+ }
+}
+
+
+# overload if this is not how your stuff
+# is configured
+try:
+ LOGIN = os.getlogin()
+ LOGIN_TMUX = os.getuid()
+except Exception:
+ LOGIN = os.getenv('USER')
+ LOGIN_TMUX = os.getuid()
+
+SCREEN_DIR = '/var/run/screens/S-%s' % LOGIN
+TMUX_DIR = '/tmp/tmux-%s' % LOGIN_TMUX
+
+def find_screen(path):
+ if not os.path.isdir(path):
+ return
+ for f in os.listdir(path):
+ path = os.path.join(path, f)
+ if screen_attached(path):
+ return path
+
+def screen_attached(socket):
+ return (os.stat(socket).st_mode & stat.S_IXUSR) != 0
+
+class Plugin(BasePlugin, pyinotify.Notifier):
+
+ default_config = DEFAULT_CONFIG
-class Plugin(BasePlugin):
def init(self):
- screen_dir = '/var/run/screen/S-%s' % (os.getlogin(),)
- self.timed_event = None
sock_path = None
- self.thread = None
- for f in os.listdir(screen_dir):
- path = os.path.join(screen_dir, f)
- if screen_attached(path):
- sock_path = path
- self.attached = True
- break
+ if self.config.get('use_tmux'):
+ sock_path = find_screen(TMUX_DIR)
+ if sock_path is None and self.config.get('use_screen'):
+ sock_path = find_screen(SCREEN_DIR)
# Only actually do something if we found an attached screen (assuming only one)
if sock_path:
+ self.attached = True
wm = pyinotify.WatchManager()
wm.add_watch(sock_path, pyinotify.EventsCodes.ALL_FLAGS['IN_ATTRIB'])
- self.thread = pyinotify.ThreadedNotifier(wm, default_proc_fun=HandleScreen(plugin=self))
- self.thread.start()
+ pyinotify.Notifier.__init__(self, wm, default_proc_fun=HandleScreen(plugin=self))
+ asyncio.get_event_loop().add_reader(self._fd, self.process)
+ else:
+ self.api.information('screen_detach plugin: No tmux or screen found',
+ 'Warning')
+ self.attached = False
+
+ def process(self):
+ self.read_events()
+ self.process_events()
def cleanup(self):
- if self.thread:
- self.thread.stop()
+ asyncio.get_event_loop().remove_reader(self._fd)
def update_screen_state(self, socket):
attached = screen_attached(socket)
@@ -38,9 +94,6 @@ class Plugin(BasePlugin):
status = 'available' if self.attached else 'away'
self.core.command_status(status)
-def screen_attached(socket):
- return (os.stat(socket).st_mode & stat.S_IXUSR) != 0
-
class HandleScreen(pyinotify.ProcessEvent):
def my_init(self, **kwargs):
self.plugin = kwargs['plugin']