summaryrefslogtreecommitdiff
path: root/plugins/screen_detach.py
blob: 6ebc77f2f60205fcb6f961333c6c3861a4c12ff6 (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
import os
import stat
import pyinotify

SCREEN_DIR = '/var/run/screen/S-%s' % (os.getlogin(),)

class Plugin(BasePlugin):
    def init(self):
        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)