From 677058eb65b2d73fa9f8524550ab195561db809b Mon Sep 17 00:00:00 2001 From: novist Date: Thu, 27 Nov 2014 15:13:35 +0200 Subject: [PATCH] Reorganized platform-dependent code Auto-away timer check reduced to 1s --- qtox.pro | 6 ++- src/platform/osx_timer.cpp | 45 ++++++++++++++++++++ src/{platform.h => platform/timer.h} | 7 ++- src/platform/win_timer.cpp | 31 ++++++++++++++ src/{platform.cpp => platform/x11_timer.cpp} | 39 ++--------------- src/widget/widget.cpp | 4 +- 6 files changed, 89 insertions(+), 43 deletions(-) create mode 100644 src/platform/osx_timer.cpp rename src/{platform.h => platform/timer.h} (88%) create mode 100644 src/platform/win_timer.cpp rename src/{platform.cpp => platform/x11_timer.cpp} (53%) diff --git a/qtox.pro b/qtox.pro index d06ce4603..7778d7cdd 100644 --- a/qtox.pro +++ b/qtox.pro @@ -156,7 +156,7 @@ HEADERS += src/widget/form/addfriendform.h \ src/misc/serialize.h \ src/widget/form/settings/advancedform.h \ src/audio.h \ - src/platform.h + src/platform/timer.h SOURCES += \ src/widget/form/addfriendform.cpp \ @@ -224,4 +224,6 @@ SOURCES += \ src/misc/serialize.cpp \ src/widget/form/settings/advancedform.cpp \ src/audio.cpp \ - src/platform.cpp + src/platform/osx_timer.cpp \ + src/platform/win_timer.cpp \ + src/platform/x11_timer.cpp diff --git a/src/platform/osx_timer.cpp b/src/platform/osx_timer.cpp new file mode 100644 index 000000000..43f731e5a --- /dev/null +++ b/src/platform/osx_timer.cpp @@ -0,0 +1,45 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#if defined(__APPLE__) && defined(__MACH__) +#include "src/platform/timer.h" +#include +#include +#include + + +uint32_t Platform::getIdleTime() +{ + // https://hg.pidgin.im/pidgin/main/file/13e4ae613a6a/pidgin/gtkidle.c + static io_service_t service = NULL; + CFTypeRef property; + uint64_t idleTime_ns = 0; + + if (!service) + { + mach_port_t master; + IOMasterPort(MACH_PORT_NULL, &master); + service = IOServiceGetMatchingService(master, IOServiceMatching("IOHIDSystem")); + } + + property = IORegistryEntryCreateCFProperty(service, CFSTR("HIDIdleTime"), kCFAllocatorDefault, 0); + CFNumberGetValue((CFNumberRef)property, kCFNumberSInt64Type, &idleTime_ns); + CFRelease(property); + + return idleTime_ns / 1000000; +} + +#endif // defined(__APPLE__) && defined(__MACH__) diff --git a/src/platform.h b/src/platform/timer.h similarity index 88% rename from src/platform.h rename to src/platform/timer.h index b1be2d945..b6576bc61 100644 --- a/src/platform.h +++ b/src/platform/timer.h @@ -14,16 +14,15 @@ See the COPYING file for more details. */ -#ifndef PLATFORM_H -#define PLATFORM_H +#ifndef PLATFORM_TIMER_H +#define PLATFORM_TIMER_H #include -/* Platform-dependent code */ namespace Platform { uint32_t getIdleTime(); } -#endif // PLATFORM_H +#endif // PLATFORM_TIMER_H diff --git a/src/platform/win_timer.cpp b/src/platform/win_timer.cpp new file mode 100644 index 000000000..c15bf0047 --- /dev/null +++ b/src/platform/win_timer.cpp @@ -0,0 +1,31 @@ +/* + Copyright (C) 2014 by Project Tox + + This file is part of qTox, a Qt-based graphical interface for Tox. + + This program is libre software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the COPYING file for more details. +*/ + +#include +#ifdef Q_OS_WIN32 +#include "src/platform/timer.h" +#include + + +uint32_t Platform::getIdleTime() +{ + LASTINPUTINFO info = { 0 }; + if(GetLastInputInfo(&info)) + return info.dwTime / 1000; + return 0; +} + +#endif // Q_OS_WIN32 diff --git a/src/platform.cpp b/src/platform/x11_timer.cpp similarity index 53% rename from src/platform.cpp rename to src/platform/x11_timer.cpp index ed3cea9e3..9d01adb41 100644 --- a/src/platform.cpp +++ b/src/platform/x11_timer.cpp @@ -14,47 +14,16 @@ See the COPYING file for more details. */ -#include "platform.h" #include -#if defined(Q_OS_WIN32) -#include -#elif defined(__APPLE__) && defined(__MACH__) -#include -#include -#else // Q_OS_UNIX +#ifdef Q_OS_UNIX +#include "src/platform/timer.h" #include -#endif + uint32_t Platform::getIdleTime() { - // http://qt-project.org/faq/answer/how_can_i_detect_a_period_of_no_user_interaction - // Detecting global inactivity, like Skype, is possible but not via Qt: - // http://stackoverflow.com/a/21905027/1497645 - // https://hg.pidgin.im/pidgin/main/file/13e4ae613a6a/pidgin/gtkidle.c uint32_t idleTime = 0; -#if defined(Q_OS_WIN32) - LASTINPUTINFO info = { 0 }; - if(GetLastInputInfo(&info)) - idleTime = info.dwTime / 1000; -#elif defined(__APPLE__) && defined(__MACH__) - static io_service_t service = NULL; - CFTypeRef property; - uint64_t idleTime_ns = 0; - - if (!service) - { - mach_port_t master; - IOMasterPort(MACH_PORT_NULL, &master); - service = IOServiceGetMatchingService(master, IOServiceMatching("IOHIDSystem")); - } - - property = IORegistryEntryCreateCFProperty(service, CFSTR("HIDIdleTime"), kCFAllocatorDefault, 0); - CFNumberGetValue((CFNumberRef)property, kCFNumberSInt64Type, &idleTime_ns); - CFRelease(property); - - idleTime = idleTime_ns / 1000000; -#else // Q_OS_UNIX Display *display = XOpenDisplay(NULL); if(!display) { @@ -77,7 +46,7 @@ uint32_t Platform::getIdleTime() qDebug() << "XScreenSaverAllocInfo() failed"; } XCloseDisplay(display); -#endif return idleTime; } +#endif // Q_OS_UNIX diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index b7d0bfdf3..bb2136d1a 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -35,7 +35,7 @@ #include "form/inputpassworddialog.h" #include "src/autoupdate.h" #include "src/audio.h" -#include "src/platform.h" +#include "src/platform/timer.h" #include #include #include @@ -125,7 +125,7 @@ void Widget::init() ui->menubar->hide(); idleTimer = new QTimer(); - idleTimer->start(10000); + idleTimer->start(1000); //restore window state restoreGeometry(Settings::getInstance().getWindowGeometry());