sleep.py

Fri, 26 Jan 2018 00:11:47 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 26 Jan 2018 00:11:47 +0000
changeset 68
72f821e17123
child 69
8705e296c7a0
permissions
-rw-r--r--

Added basic sleep/wake detection code for MacOS
(Still need to make use of it to adjust timers.)

68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Wake/sleep detection for scheduling adjustments
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 #
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 import Foundation
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 import AppKit
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 import borgend
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 logger=borgend.logger.getChild(__name__)
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 class SleepHandler(Foundation.NSObject):
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 """ Handle wake/sleep notifications """
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 def handleSleepNotification(self, aNotification):
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 logger.info("System going to sleep")
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 def handleWakeNotification(self, aNotification):
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 logger.info("System waking up from sleep")
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 if platform.system()=='Darwin':
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 workspace = AppKit.NSWorkspace.sharedWorkspace()
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 notification_center = workspace.notificationCenter()
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 sleep_handler = SleepHandler.new()
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 notification_center.addObserver_selector_name_object_(
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 sleep_handler,
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 "handleSleepNotification",
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 AppKit.NSWorkspaceWillSleepNotification,
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 None)
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 notification_center.addObserver_selector_name_object_(
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 sleep_handler,
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 "handleWakeNotification",
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 AppKit.NSWorkspaceDidWakeNotification,
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 None)
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36

mercurial