Page 1 of 1

App Nap

Posted: Thu Oct 10, 2019 4:48 am
by Rinzwind
It seems that macOS still naps an app even if it requested not too?

Code: Select all

EnableExplicit

Global w, e, a


; Author : Danilo
; Date   : 25.03.2014
; Link   : https://www.purebasic.fr/english/viewtopic.php?f=19&t=58828
; Info   : NSActivityOptions is a 64bit typedef - use it with quads (.q) !!!

#NSActivityIdleDisplaySleepDisabled             = 1 << 40
#NSActivityIdleSystemSleepDisabled              = 1 << 20
#NSActivitySuddenTerminationDisabled            = (1 << 14)
#NSActivityAutomaticTerminationDisabled         = (1 << 15)
#NSActivityUserInitiated                        = ($00FFFFFF | #NSActivityIdleSystemSleepDisabled)
#NSActivityUserInitiatedAllowingIdleSystemSleep = (#NSActivityUserInitiated & ~#NSActivityIdleSystemSleepDisabled)
#NSActivityBackground                           = $000000FF
#NSActivityLatencyCritical                      = $FF00000000

Procedure BeginWork(Option.q, Reason.s= "MyReason")
  Protected NSProcessInfo = CocoaMessage(0,0,"NSProcessInfo processInfo")
  If NSProcessInfo
    ProcedureReturn CocoaMessage(0, NSProcessInfo, "beginActivityWithOptions:@", @Option, "reason:$", @Reason)
  EndIf
EndProcedure

Procedure EndWork(activity)
  Protected NSProcessInfo = CocoaMessage(0, 0, "NSProcessInfo processInfo")
  If NSProcessInfo
    CocoaMessage(0, NSProcessInfo, "endActivity:", activity)
  EndIf
EndProcedure

w = OpenWindow(0, 0, 0, 400, 300, "Test")
a = BeginWork(#NSActivityUserInitiatedAllowingIdleSystemSleep, "No napping")
Debug a

Repeat
  e = WaitWindowEvent()
Until e = #PB_Event_CloseWindow

EndWork(a) ;Invalid memory access...
ps. EndWork causes a crash in this small example, but not when put directly after BeginWork.

Any way to make it work as intended? As in no app napping as seen in activity monitor/Energy when app is in background and other app active/used?

Re: App Nap

Posted: Thu Oct 10, 2019 8:10 pm
by mk-soft
Its only for threads, not work at MainScope

Code: Select all

EnableExplicit

Global w, e, a


; Author : Danilo
; Date   : 25.03.2014
; Link   : https://www.purebasic.fr/english/viewtopic.php?f=19&t=58828
; Info   : NSActivityOptions is a 64bit typedef - use it with quads (.q) !!!

#NSActivityIdleDisplaySleepDisabled             = 1 << 40
#NSActivityIdleSystemSleepDisabled              = 1 << 20
#NSActivitySuddenTerminationDisabled            = (1 << 14)
#NSActivityAutomaticTerminationDisabled         = (1 << 15)
#NSActivityUserInitiated                        = ($00FFFFFF | #NSActivityIdleSystemSleepDisabled)
#NSActivityUserInitiatedAllowingIdleSystemSleep = (#NSActivityUserInitiated & ~#NSActivityIdleSystemSleepDisabled)
#NSActivityBackground                           = $000000FF
#NSActivityLatencyCritical                      = $FF00000000

Procedure BeginWork(Option.q, Reason.s= "MyReason")
  Protected NSProcessInfo = CocoaMessage(0,0,"NSProcessInfo processInfo")
  If NSProcessInfo
    ProcedureReturn CocoaMessage(0, NSProcessInfo, "beginActivityWithOptions:@", @Option, "reason:$", @Reason)
  EndIf
EndProcedure

Procedure EndWork(activity)
  Protected NSProcessInfo = CocoaMessage(0, 0, "NSProcessInfo processInfo")
  If NSProcessInfo
    CocoaMessage(0, NSProcessInfo, "endActivity:", activity)
  EndIf
EndProcedure


Procedure thWork(*Exit.Integer)
  Protected no_nap
  no_nap = BeginWork(#NSActivityUserInitiatedAllowingIdleSystemSleep)
  
  Repeat
    If *Exit\i
      Break
    EndIf
    Delay(100)
  ForEver
  
  EndWork(no_nap)
EndProcedure

w = OpenWindow(0, 0, 0, 400, 300, "Test")

Define exit
Define thread = CreateThread(@thWork(), @exit)

Repeat
  e = WaitWindowEvent()
Until e = #PB_Event_CloseWindow

exit = #True
WaitThread(thread)

Re: App Nap

Posted: Fri Oct 11, 2019 2:02 am
by Rinzwind
Good to know. And works as expected in Catalina.