Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/xopower.py
blob: eec6779b3035b2173468623095861baaa2f285ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! /usr/bin/env python

# Copyright (C) 2009 James D. Simmons
#
# This program is free 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 2 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import logging
import dbus
from gi.repository import GObject

_HARDWARE_MANAGER_INTERFACE = 'org.laptop.HardwareManager'
_HARDWARE_MANAGER_SERVICE = 'org.laptop.HardwareManager'
_HARDWARE_MANAGER_OBJECT_PATH = '/org/laptop/HardwareManager'

_logger = logging.getLogger('read-etexts-activity')

# start with sleep off
sleep_inhibit = True
service_activated = False
_idle_timer = 0
_service = None

def setup_idle_timeout():
    # Set up for idle suspend
    global _service
    global service_activated
        
    fname = os.path.join('/etc', 'inhibit-ebook-sleep')
    if not os.path.exists(fname):
        try:
            bus = dbus.SystemBus()
            proxy = bus.get_object(_HARDWARE_MANAGER_SERVICE,
                                   _HARDWARE_MANAGER_OBJECT_PATH)
            _service = dbus.Interface(proxy, _HARDWARE_MANAGER_INTERFACE)
            service_activated = True
            logging.debug('Suspend on idle enabled')
        except dbus.DBusException, e:
            _logger.info('Hardware manager service not found, no idle suspend.')
    else:
        logging.debug('Suspend on idle disabled')

def turn_on_sleep_timer():
    global sleep_inhibit
    sleep_inhibit = False
    reset_sleep_timer()

def turn_off_sleep_timer():
    global sleep_inhibit
    sleep_inhibit = True

def reset_sleep_timer():
    global _idle_timer
    if _idle_timer > 0:
        GObject.source_remove(_idle_timer)
    _idle_timer = GObject.timeout_add(5000, _suspend)

def suspend():
    # If the machine has been idle for 5 seconds, suspend
    global _idle_timer
    global _service
    _idle_timer = 0
    if not sleep_inhibit and _service is not None:
        _service.set_kernel_suspend()