Notification for Idle Time?

  • Hello all,

    I'm working on something that needs to know when the computer has had no user input for a period of time.  How does the Screensaver Engine know when the user has been idle long enough to start?  I need to do the same thing and I can't find any Cocoa methods that provide that information.

    I found something that suggested I could get this information from IOKit, but it looks like I'd have to constantly poll the idle time, and polling sucks.  Anyone know if there is an NSNotification I can receive for idle times?

    Thanks,
    Christopher
  • On 11/12/2007, at 7:42 AM, Christopher Hickman wrote:

    > Hello all,
    >
    > I'm working on something that needs to know when the computer has
    > had no user input for a period of time.  How does the Screensaver
    > Engine know when the user has been idle long enough to start?  I
    > need to do the same thing and I can't find any Cocoa methods that
    > provide that information.
    >
    > I found something that suggested I could get this information from
    > IOKit, but it looks like I'd have to constantly poll the idle time,
    > and polling sucks.  Anyone know if there is an NSNotification I can
    > receive for idle times?
    >
    > Thanks,
    > Christopher

    Hi Christopher,

    I think this document will help:<http://developer.apple.com/qa/qa2006/
    qa1442.html
    >.  You could use a timer and reset it whenever one of the
    notifications is received, but fire your own method after whatever
    time you want.  In the referenced document, it looks like NSWorkspace
    notification center will do for you.

    HTH,

    Ron
  • Christopher Hickman wrote:

    > I'm working on something that needs to know when the computer has
    > had no user input for a period of time.  How does the Screensaver
    > Engine know when the user has been idle long enough to start?  I
    > need to do the same thing and I can't find any Cocoa methods that
    > provide that information.
    >
    > I found something that suggested I could get this information from
    > IOKit, but it looks like I'd have to constantly poll the idle time,
    > and polling sucks.  Anyone know if there is an NSNotification I can
    > receive for idle times?

    Not an NSNotification.

    InstallEventLoopIdleTimer

    <http://developer.apple.com/documentation/Carbon/Conceptual/
    Carbon_Event_Manager/Tasks/chapter_3_section_11.html
    >
    <http://developer.apple.com/documentation/Carbon/Reference/
    Carbon_Event_Manager_Ref/Reference/reference.html#//apple_ref/c/func/
    InstallEventLoopIdleTimer
    >
  • On Tuesday, December 11, 2007, at 06:23AM, "Gregory Weston" <gweston...> wrote:
    > Christopher Hickman wrote:
    >
    >> I'm working on something that needs to know when the computer has
    >> had no user input for a period of time.  How does the Screensaver
    >> Engine know when the user has been idle long enough to start?  I
    >> need to do the same thing and I can't find any Cocoa methods that
    >> provide that information.
    >>
    >> I found something that suggested I could get this information from
    >> IOKit, but it looks like I'd have to constantly poll the idle time,
    >> and polling sucks.  Anyone know if there is an NSNotification I can
    >> receive for idle times?
    >
    >
    > Not an NSNotification.
    >
    > InstallEventLoopIdleTimer
    >
    > < http://developer.apple.com/documentation/Carbon/Conceptual/
    >Carbon_Event_Manager/Tasks/chapter_3_section_11.html>
    >Carbon_Event_Manager_Ref/Reference/reference.html#//apple_ref/c/func/
    > InstallEventLoopIdleTimer>

    Does anybody know if it is possible to get an EventLoopRef from my NSApplication's run loop?  Greg found something that does exactly what I need, but it requires that I specify the run loop "the Carbon way" as an EventLoopRef.  I just don't know how to reference my application's run loop as a Carbon EventLoopRef.
  • I've found out the hard way that CFRunLoops and NSRunLoops have subtle
    differences; one really can't substitute for another. (e.g. Cocoa
    callbacks will not fire on a CFRunLoop, or calling a CF routine to stop
    a run-loop will fail to stop an NSRunLoop)
    It's unfortunate since the NSRunLoop API and CFRunLoop API each have
    interesting functionality that would be useful to have on the "other side."

    Christopher Hickman wrote:
    > On Tuesday, December 11, 2007, at 06:23AM, "Gregory Weston" <gweston...> wrote:
    >
    >> Christopher Hickman wrote:
    >>
    >>
    >>> I'm working on something that needs to know when the computer has
    >>> had no user input for a period of time.  How does the Screensaver
    >>> Engine know when the user has been idle long enough to start?  I
    >>> need to do the same thing and I can't find any Cocoa methods that
    >>> provide that information.
    >>>
    >>> I found something that suggested I could get this information from
    >>> IOKit, but it looks like I'd have to constantly poll the idle time,
    >>> and polling sucks.  Anyone know if there is an NSNotification I can
    >>> receive for idle times?
    >>>
    >> Not an NSNotification.
    >>
    >> InstallEventLoopIdleTimer
    >>
    >> <http://developer.apple.com/documentation/Carbon/Conceptual/
    >> Carbon_Event_Manager/Tasks/chapter_3_section_11.html>
    >> Carbon_Event_Manager_Ref/Reference/reference.html#//apple_ref/c/func/
    >> InstallEventLoopIdleTimer>
    >>
    >
    > Does anybody know if it is possible to get an EventLoopRef from my NSApplication's run loop?  Greg found something that does exactly what I need, but it requires that I specify the run loop "the Carbon way" as an EventLoopRef.  I just don't know how to reference my application's run loop as a Carbon EventLoopRef.
    >
  • >>
    >
    > Does anybody know if it is possible to get an EventLoopRef from my
    > NSApplication's run loop?  Greg found something that does exactly
    > what I need, but it requires that I specify the run loop "the Carbon
    > way" as an EventLoopRef.  I just don't know how to reference my
    > application's run loop as a Carbon EventLoopRef.
    > _______________________________________________

    GetMainEventLoop() should works.
  • On Dec 11, 2007, at 1:24 PM, Christopher Hickman wrote:

    > On Tuesday, December 11, 2007, at 06:23AM, "Gregory Weston"
    > <gweston...> wrote:
    >> Not an NSNotification.
    >>
    >> InstallEventLoopIdleTimer
    >>
    >> <http://developer.apple.com/documentation/Carbon/Conceptual/
    >> Carbon_Event_Manager/Tasks/chapter_3_section_11.html>
    >> Carbon_Event_Manager_Ref/Reference/reference.html#//apple_ref/c/func/
    >> InstallEventLoopIdleTimer>
    >
    > Does anybody know if it is possible to get an EventLoopRef from my
    > NSApplication's run loop?  Greg found something that does exactly
    > what I need, but it requires that I specify the run loop "the
    > Carbon way" as an EventLoopRef.  I just don't know how to reference
    > my application's run loop as a Carbon EventLoopRef.

    GetMainEventLoop() will work as the first argument.

    But I've realized that InstallEventLoopIdleTimer probably won't do
    what you're looking for, so please accept my apologies. IOKit looks
    like it. Polling does suck, but if you poll with only the frequency
    you need you can mitigate the horribleness somewhat. Like if you're
    looking for 5 minutes of idle time, you might find it acceptable to
    check only every 3 seconds or so - an error of 1%.
  • On 11.12.2007, at 20:06, Gregory Weston wrote:

    >
    > On Dec 11, 2007, at 1:24 PM, Christopher Hickman wrote:
    >
    >> On Tuesday, December 11, 2007, at 06:23AM, "Gregory Weston" <gweston...>
    >>> wrote:
    >>> Not an NSNotification.
    >>>
    >>> InstallEventLoopIdleTimer
    >>>
    >>> <http://developer.apple.com/documentation/Carbon/Conceptual/
    >>> Carbon_Event_Manager/Tasks/chapter_3_section_11.html>
    >>> Carbon_Event_Manager_Ref/Reference/reference.html#//apple_ref/c/
    >>> func/
    >>> InstallEventLoopIdleTimer>
    >>
    >> Does anybody know if it is possible to get an EventLoopRef from my
    >> NSApplication's run loop?  Greg found something that does exactly
    >> what I need, but it requires that I specify the run loop "the
    >> Carbon way" as an EventLoopRef.  I just don't know how to reference
    >> my application's run loop as a Carbon EventLoopRef.
    >
    > GetMainEventLoop() will work as the first argument.
    >
    > But I've realized that InstallEventLoopIdleTimer probably won't do
    > what you're looking for, so please accept my apologies. IOKit looks
    > like it. Polling does suck, but if you poll with only the frequency
    > you need you can mitigate the horribleness somewhat. Like if you're
    > looking for 5 minutes of idle time, you might find it acceptable to
    > check only every 3 seconds or so - an error of 1%.

    When I asked a similar question recently, someone suggested using
    CGEventSourceSecondsSinceLastEventType(). I don't know what this
    function does internally, but it's sure a lot less code than fetching
    stuff from IOKit. (It's available Tiger and later, according to the
    docs)...
    And no, this doesn't solve the polling issue :(

    HTH,
    </jum>
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today