Coverage for larch/wxlib/allow_idle_macosx.py: 0%

33 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-10-16 21:04 +0000

1#----------------------------------------------------------------------------- 

2# adopted from IPython.lib.external._nope.py from IPython 

3# [ Copyright (C) 2013 Min RK 

4# Distributed under the terms of the 2-clause BSD License. 

5# ] 

6#----------------------------------------------------------------------------- 

7 

8from contextlib import contextmanager 

9 

10import ctypes 

11import ctypes.util 

12 

13void_p = ctypes.c_void_p 

14uint64 = ctypes.c_uint64 

15 

16objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc')) 

17 

18objc.objc_getClass.restype = void_p 

19objc.sel_registerName.restype = void_p 

20objc.objc_msgSend.restype = void_p 

21objc.objc_msgSend.argtypes = [void_p, void_p] 

22 

23msgSend = objc.objc_msgSend 

24 

25def as_utf8(s): 

26 """ensure utf8 bytes""" 

27 if not isinstance(s, bytes): 

28 s = s.encode('utf8') 

29 return s 

30 

31def SelName(name): 

32 """create a selector name (for methods)""" 

33 return objc.sel_registerName(as_utf8(name)) 

34 

35def GetClass(classname): 

36 """get an ObjC Class by name""" 

37 return objc.objc_getClass(as_utf8(classname)) 

38 

39# constants from Foundation 

40 

41NSActivityIdleDisplaySleepDisabled = (1 << 40) 

42NSActivityIdleSystemSleepDisabled = (1 << 20) 

43NSActivitySuddenTerminationDisabled = (1 << 14) 

44NSActivityAutomaticTerminationDisabled = (1 << 15) 

45NSActivityUserInitiated = (0x00FFFFFF | NSActivityIdleSystemSleepDisabled) 

46NSActivityUserInitiatedAllowingIdleSystemSleep = (NSActivityUserInitiated & ~NSActivityIdleSystemSleepDisabled) 

47NSActivityBackground = 0x000000FF 

48NSActivityLatencyCritical = 0xFF00000000 

49 

50_activity = None 

51 

52def allow_idle(): 

53 """disable App Nap by setting NSActivityUserInitiatedAllowingIdleSystemSleep""" 

54 global _activity 

55 

56 reason = msgSend(GetClass('NSString'), 

57 SelName("stringWithUTF8String:"), 

58 as_utf8('reason')) 

59 info = msgSend(GetClass('NSProcessInfo'), 

60 SelName('processInfo')) 

61 options = uint64(NSActivityUserInitiatedAllowingIdleSystemSleep) 

62 _activity = msgSend(info, 

63 SelName('beginActivityWithOptions:reason:'), 

64 options, void_p(reason))