I'm not sure I'm even using the right terminology to describe the question, but I'll try to explain.
I have a macOS utility application (LSUIElement=true
) that does various things, some on a repeated schedule in the background. If there are multiple accounts on the computer, and "Fast User Switching" is enabled, then my application runs these background tasks for multiple users at a time -- fast switching to another user doesn't stop the application from doing things on a timer in the background (which I guess makes sense).
One of the background repeating tasks is expensive and makes no sense to do if the user isn't the "current" or "foreground", or "active" (terminology?) user. So I found myself wanting to ask the question programatically "does my user have the screen?"
I've done a lot of googling and exploring partially undocumented Swift and Objective-C APIs trying to figure this out, and I'm not sure if I'm on the right track, but was hoping someone could correct me or point me to some better concepts/resources/APIs, if possible.
The best I've been able to come up with is querying who the "Console User" is -- which seems almost always to map to the user "with the screen". But I honestly don't even know what the Console User really indicates, and if it's an accurate proxy for the question I'm trying to ask, or if it's some vestigial unix red-herring. Here's some working Swift code that sort-of seems to answer the question I'm asking:
import Foundation
import SystemConfiguration
func currentUserHasScreen() -> Bool {
var uid: uid_t = 0
SCDynamicStoreCopyConsoleUser(nil, &uid, nil)
return uid == getuid()
}
All that said, I'm hoping someone could share their knowledge on some specific questions related to this issue:
- What exactly is the
ConsoleUser
, and what information is it meant to convey? - Does the Swift function shown above seem like an appropriate/correct/safe way to figure out if the current user has the screen?
- Is it even correct to ask "who has the screen?" -- could multiple users have the screen? (I experimented and was able to be logged in normally with one user on a machine, and then I screen-shared a different user, both worked at the same time -- who "has the screen" in that scenario?)
- Would it be better or more correct to ask "does this user have ANY screen/display?" For my use case, the background task has to do with taking screenshots, I don't want to take screenshots if nobody is logged in or looking at a screen (even though the OS happily will do so).
- When running the experiment mentioned in number 3 above, the screen sharing app prompted me saying
"harriet" is currently using the display on "mini"
-- what core concepts are being leveraged here? -- how does the OS know who "has the display", and is that what I should be querying instead?
Would greatly appreciate any insight on as many of these issues as anyone has knowledge of, and I think future googlers would as well. 🙏