summaryrefslogtreecommitdiff
path: root/plugins/screen_detach.py
blob: 53827c11f8738e568825f31edae0bb1c8642e28d (plain)
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
"""
This plugin will set your status to **away** if you detach your screen.

"""
from plugin import BasePlugin
import os
import stat
import pyinotify

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

        # Only actually do something if we found an attached screen (assuming only one)
        if sock_path:
            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()

    def cleanup(self):
        if self.thread:
            self.thread.stop()

    def update_screen_state(self, socket):
        attached = screen_attached(socket)
        if attached != self.attached:
            self.attached = attached
            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']

    def process_IN_ATTRIB(self, event):
        self.plugin.update_screen_state(event.path)