PureBasic Forum
https://www.purebasic.fr/english/

App Nap
https://www.purebasic.fr/english/viewtopic.php?f=19&t=73774
Page 1 of 1

Author:  Rinzwind [ Thu Oct 10, 2019 4:48 am ]
Post subject:  App Nap

It seems that macOS still naps an app even if it requested not too?

Code:
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?

Author:  mk-soft [ Thu Oct 10, 2019 8:10 pm ]
Post subject:  Re: App Nap

Its only for threads, not work at MainScope

Code:
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)

Author:  Rinzwind [ Fri Oct 11, 2019 2:02 am ]
Post subject:  Re: App Nap

Good to know. And works as expected in Catalina.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/