]> git.donarmstrong.com Git - deb_pkgs/autorandr.git/blob - contrib/autorandr_monitor/autorandr_monitor
pm-utils script: Potential fixes for bugs #30 and #39
[deb_pkgs/autorandr.git] / contrib / autorandr_monitor / autorandr_monitor
1 #!/usr/bin/env python
2 """"
3 Author:  Tomasz Bogdal (a.k.a. QueezyTheGreat)
4 Home:    https://github.com/queezythegreat/autorandr
5 License: This Source Code Form is subject to the terms of the
6          Mozilla Public License, v. 2.0
7 """
8 import os
9 import pyinotify
10 from pyinotify import ProcessEvent
11
12 #TODO: Fork off when started
13 #TODO: Add configuration file
14 #TODO: Add command line options
15
16 SYS_VIDEO_OUTPUTS='/sys/class/drm/'
17 DEFAULT_PROFILE='default'
18 AUTORANDR_CMD='autorandr --change --default %s' % DEFAULT_PROFILE
19
20 class VideoOutputMonitor(ProcessEvent):
21     """ Launch autorandr when video card output status is changed. """
22
23     def process_IN_ACCESS(self, event):
24         """ Handle IN_ACCESS events to `status` file. """
25         if event.name == 'status':
26             print 'Change status of %s' % os.path.basename(event.path)
27             os.system(AUTORANDR_CMD)
28
29
30 def register_video_cards(manager):
31     """ Register all video card ouptus for monitoring. """
32     if not os.path.exists(SYS_VIDEO_OUTPUTS):
33         return
34
35     for directory in os.listdir(SYS_VIDEO_OUTPUTS):
36         path = os.path.join(SYS_VIDEO_OUTPUTS, directory)
37         status = os.path.join(path, 'status')
38         if os.path.exists(status):
39             print 'Monitoring %s' % path
40             manager.add_watch(path, pyinotify.ALL_EVENTS)
41
42 def main():
43     # pyinotify.log.setLevel(10)
44     manager = pyinotify.WatchManager()
45     handler = VideoOutputMonitor()
46     notifier = pyinotify.Notifier(manager, default_proc_fun=handler)
47
48     register_video_cards(manager)
49
50     notifier.loop()
51
52 if __name__ == '__main__':
53     main()