├── .gitignore ├── addon.js ├── test.js ├── src ├── idle.h ├── module.h ├── module.cc ├── win │ └── idle.cc ├── linux │ └── idle.cc └── mac │ └── idle.cc ├── README.md ├── LICENSE ├── package.json └── binding.gyp /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | build 4 | npm-debug.log 5 | -------------------------------------------------------------------------------- /addon.js: -------------------------------------------------------------------------------- 1 | module.exports = require('bindings')('system_idle_time'); 2 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var system = require('./addon'); 2 | 3 | setInterval(function () { 4 | console.log(system.getIdleTime()); 5 | }, 1000); 6 | -------------------------------------------------------------------------------- /src/idle.h: -------------------------------------------------------------------------------- 1 | #define MAC 1 2 | #define WIN 2 3 | #define LIN 3 4 | 5 | #if OS != MAC 6 | #include 7 | #endif 8 | 9 | uint32_t SystemIdleTime(void); 10 | -------------------------------------------------------------------------------- /src/module.h: -------------------------------------------------------------------------------- 1 | #ifndef SRC_MODULE_H_ 2 | #define SRC_MODULE_H_ 3 | 4 | #include "nan.h" 5 | 6 | using namespace v8; 7 | 8 | class IdleTime { 9 | public: 10 | static void Init(Local exports); 11 | static NAN_METHOD(GetIdleTime); 12 | }; 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A native node addon for obtaining system idle time in seconds. 2 | 3 | OS implementations derived from the following resources: 4 | 5 | * http://www.danandcheryl.com/2010/06/how-to-check-the-system-idle-time-using-cocoa 6 | * https://github.com/jojobyte/idlerun 7 | * https://github.com/rosedu/Pidgin/blob/master/pidgin/gtkidle.c 8 | -------------------------------------------------------------------------------- /src/module.cc: -------------------------------------------------------------------------------- 1 | #include "module.h" 2 | #include "idle.h" 3 | 4 | using namespace v8; 5 | 6 | NAN_METHOD(IdleTime::GetIdleTime) { 7 | Nan::HandleScope scope; 8 | 9 | uint32_t idle; 10 | idle = SystemIdleTime(); 11 | info.GetReturnValue().Set(idle); 12 | } 13 | 14 | void IdleTime::Init(Local exports) { 15 | Nan::SetMethod(exports, "getIdleTime", IdleTime::GetIdleTime); 16 | } 17 | 18 | NODE_MODULE(system_idle_time, IdleTime::Init) 19 | -------------------------------------------------------------------------------- /src/win/idle.cc: -------------------------------------------------------------------------------- 1 | #ifndef BUILDING_NODE_EXTENSION 2 | #define BUILDING_NODE_EXTENSION 3 | #endif 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | uint32_t SystemIdleTime(void) { 10 | LASTINPUTINFO lif; 11 | lif.cbSize = sizeof(lif); 12 | if (!GetLastInputInfo(&lif)) return -1; 13 | uint64_t tickCount = GetTickCount64(); 14 | uint32_t IdleTime = (uint32_t)((tickCount - (uint64_t)lif.dwTime)); 15 | return IdleTime; 16 | } 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright © 2014 Atlassian Pty Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@paulcbetts/system-idle-time", 3 | "version": "1.0.5", 4 | "description": "Returns system idle time in seconds", 5 | "main": "addon.js", 6 | "scripts": { 7 | "test": "node test.js", 8 | "install": "node-gyp rebuild" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/paulcbetts/node-system-idle-time" 13 | }, 14 | "keywords": [ 15 | "system", 16 | "idle", 17 | "time" 18 | ], 19 | "author": "", 20 | "license": "Apache-2.0", 21 | "gypfile": true, 22 | "homepage": "https://bitbucket.org/rbergman/system-idle-time#readme", 23 | "dependencies": { 24 | "nan": "^2.0.0", 25 | "bindings": "~1.2.1" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/linux/idle.cc: -------------------------------------------------------------------------------- 1 | #ifndef BUILDING_NODE_EXTENSION 2 | #define BUILDING_NODE_EXTENSION 3 | #endif 4 | 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | static int has_extension = -1; 13 | 14 | uint32_t SystemIdleTime(void) { 15 | Display* dpy = XOpenDisplay(NULL); 16 | int event_base, error_base; 17 | uint32_t idle_time = 0; 18 | XScreenSaverInfo info; 19 | Window wnd; 20 | 21 | if (!dpy) { 22 | return 0; 23 | } 24 | 25 | if (has_extension == -1) { 26 | has_extension = XScreenSaverQueryExtension(dpy, &event_base, &error_base); 27 | } 28 | 29 | if (!has_extension) { 30 | goto out; 31 | } 32 | 33 | wnd = RootWindow(dpy, DefaultScreen(dpy)); 34 | 35 | if (!wnd) { 36 | idle_time = 60*60*1000; 37 | goto out; 38 | } 39 | 40 | XScreenSaverQueryInfo(dpy, wnd, &info); 41 | idle_time = info.idle; 42 | 43 | out: 44 | XCloseDisplay(dpy); 45 | return idle_time; 46 | } 47 | -------------------------------------------------------------------------------- /src/mac/idle.cc: -------------------------------------------------------------------------------- 1 | #import 2 | #import 3 | 4 | /** 5 | Returns the number of seconds the machine has been idle or -1 if an error occurs. 6 | The code is compatible with Tiger/10.4 and later (but not iOS). 7 | */ 8 | int32_t SystemIdleTime(void) { 9 | int32_t idlesecs = -1; 10 | io_iterator_t iter = 0; 11 | if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) { 12 | io_registry_entry_t entry = IOIteratorNext(iter); 13 | if (entry) { 14 | CFMutableDictionaryRef dict = NULL; 15 | if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) { 16 | CFNumberRef obj = (CFNumberRef) CFDictionaryGetValue(dict, CFSTR("HIDIdleTime")); 17 | if (obj) { 18 | int64_t nanoseconds = 0; 19 | if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) { 20 | idlesecs = (int32_t) (nanoseconds / (1000L*1000L)); 21 | } 22 | } 23 | CFRelease(dict); 24 | } 25 | IOObjectRelease(entry); 26 | } 27 | IOObjectRelease(iter); 28 | } 29 | return idlesecs; 30 | } 31 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [{ 3 | "target_name": "system_idle_time", 4 | "sources": [ 5 | "src/module.cc" 6 | ], 7 | "include_dirs": [ 8 | "