~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to platform/osx/modules/idletime.c

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
# Miro - an RSS based video player application
3
 
# Copyright (C) 2005-2010 Participatory Culture Foundation
4
 
#
5
 
# This program is free software; you can redistribute it and/or modify
6
 
# it under the terms of the GNU General Public License as published by
7
 
# the Free Software Foundation; either version 2 of the License, or
8
 
# (at your option) any later version.
9
 
#
10
 
# This program is distributed in the hope that it will be useful,
11
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
# GNU General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License
16
 
# along with this program; if not, write to the Free Software
17
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18
 
#
19
 
# In addition, as a special exception, the copyright holders give
20
 
# permission to link the code of portions of this program with the OpenSSL
21
 
# library.
22
 
#
23
 
# You must obey the GNU General Public License in all respects for all of
24
 
# the code used other than OpenSSL. If you modify file(s) with this
25
 
# exception, you may extend this exception to your version of the file(s),
26
 
# but you are not obligated to do so. If you do not wish to do so, delete
27
 
# this exception statement from your version. If you delete this exception
28
 
# statement from all source files in the program, then also delete it here.
29
 
*/
30
 
 
31
 
#include <CoreFoundation/CoreFoundation.h>
32
 
#include <IOKit/IOKitLib.h>
33
 
#include <Python.h>
34
 
 
35
 
#define RETURN_ZERO_IF(e) if ((e)) return Py_BuildValue("f", 0.0f)
36
 
 
37
 
 
38
 
static PyObject* idletime_get(PyObject* self, PyObject* args)
39
 
{
40
 
    kern_return_t   result;
41
 
    mach_port_t     masterPort;
42
 
    
43
 
    result = IOMasterPort(kIOMasterPortDefault, &masterPort);
44
 
    RETURN_ZERO_IF(result != KERN_SUCCESS);
45
 
    
46
 
    CFMutableDictionaryRef  matchingDict = IOServiceMatching("IOHIDSystem");
47
 
    io_iterator_t           hidIterator;
48
 
    
49
 
    result = IOServiceGetMatchingServices(masterPort, matchingDict, &hidIterator);
50
 
    RETURN_ZERO_IF(hidIterator == 0);
51
 
    
52
 
    io_registry_entry_t hidEntry = IOIteratorNext(hidIterator);
53
 
    RETURN_ZERO_IF(hidEntry == 0);
54
 
    
55
 
    CFMutableDictionaryRef  hidProperties = NULL;
56
 
    result = IORegistryEntryCreateCFProperties(hidEntry, &hidProperties, kCFAllocatorDefault, 0);
57
 
    RETURN_ZERO_IF(result != KERN_SUCCESS || hidProperties == NULL);
58
 
    
59
 
    CFTypeRef   hidIdleTime = CFDictionaryGetValue(hidProperties, CFSTR("HIDIdleTime"));
60
 
    float       seconds = 0.0f;
61
 
    
62
 
    if (hidIdleTime != NULL)
63
 
    {
64
 
        CFTypeID    type = CFGetTypeID(hidIdleTime);
65
 
        uint64_t    handle = 0;
66
 
 
67
 
        if (type == CFDataGetTypeID())
68
 
        {
69
 
            CFRange range = CFRangeMake(0, sizeof(handle));
70
 
            CFDataGetBytes((CFDataRef)hidIdleTime, range, (UInt8*)&handle);
71
 
        } 
72
 
        else if (type == CFNumberGetTypeID()) 
73
 
        {
74
 
            CFNumberGetValue((CFNumberRef)hidIdleTime, kCFNumberSInt64Type, &handle);
75
 
        }
76
 
 
77
 
        seconds = handle / 1000000000.0;
78
 
    }
79
 
 
80
 
    IOObjectRelease(hidEntry);
81
 
    IOObjectRelease(hidIterator);
82
 
    CFRelease(hidProperties);
83
 
 
84
 
    return Py_BuildValue("f", seconds);
85
 
}
86
 
 
87
 
static PyMethodDef IdleTimeMethods[] = 
88
 
{
89
 
    { "get", idletime_get, METH_VARARGS, "Returns the system idle time in seconds" },
90
 
    { NULL, NULL, 0, NULL }
91
 
};
92
 
 
93
 
PyMODINIT_FUNC initidletime(void)
94
 
{
95
 
    Py_InitModule("idletime", IdleTimeMethods);
96
 
}