Отсутствие действий:
C++ (Qt)
int CommonUtils::getUserIdle()
{
#if defined(Q_WS_X11)
XScreenSaverInfo *info = XScreenSaverAllocInfo();
if(!info)
{
return 0;
}
Display *display = QX11Info::display();
XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
int idle = info->idle;
XFree(info);
return idle;
#elif defined(Q_WS_WIN)
LASTINPUTINFO lif;
lif.cbSize = sizeof(LASTINPUTINFO);
if(!GetLastInputInfo(&lif))
{
return 0;
}
return GetTickCount() - lif.dwTime;
#elif defined(Q_WS_MAC)
int64_t idlesecs = 0;
io_iterator_t iter = 0;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS)
{
io_registry_entry_t entry = IOIteratorNext(iter);
if (entry)
{
CFMutableDictionaryRef dict = NULL;
if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS)
{
CFNumberRef obj = (CFNumberRef)CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
if (obj)
{
int64_t nanoseconds = 0;
if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds))
{
idlesecs = (nanoseconds * 1000000); // Divide by 10^9 to convert from nanoseconds to seconds.
}
}
CFRelease(dict);
}
IOObjectRelease(entry);
}
IOObjectRelease(iter);
}
return idlesecs;
#endif
}
Хранитель экрана:
C++ (Qt)
static QString getProcessesOutput
(
const QString& strUtility0,
const QStringList argsList0,
const QString& strUtility1,
const QStringList argsList1
)
{
QProcess process0, process1;
process0.setStandardOutputProcess( &process1 );
process0.start( strUtility0, argsList0 );
process1.start( strUtility1, argsList1 );
QString strRes;
if( process0.waitForStarted() )
{
if( process1.waitForStarted() && process1.waitForReadyRead() )
{
strRes = QString( process1.readAllStandardOutput().trimmed() );
process1.waitForFinished();
}
process0.waitForFinished();
}
// qDebug( "getProcessesOutput: 0: %d", process0.exitCode() );
// qDebug( "getProcessesOutput: 1: %d", process1.exitCode() );
// qDebug( "getProcessesOutput: res: [%s]", qPrintable( strRes ) );
return strRes;
}
bool CommonUtils::isScreenSaverActive()
{
bool bRes = false;
#if defined(Q_WS_X11)
XScreenSaverInfo *info = XScreenSaverAllocInfo();
if(info)
{
Display *display = QX11Info::display();
XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
bRes = info->state == ScreenSaverOn;
XFree(info);
}
#elif defined(Q_WS_WIN)
BOOL isOn;
BOOL cmdResult =SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, NULL, &isOn, FALSE);
bRes = isOn && cmdResult;
#elif defined(Q_WS_MAC)
static const QString path2ps = "ps";
static const QString psArg = "ax";
static const QString path2grep = "grep";
static const QString grepArg = "[S]creenSaverEngine";
QString output = getProcessesOutput
(
path2ps,
QStringList() << psArg,
path2grep,
QStringList() << grepArg
);
bRes = !output.isEmpty();
#endif
return bRes;
}