Package common :: Module sleepy
[hide private]
[frames] | no frames]

Source Code for Module common.sleepy

  1  # -*- coding:utf-8 -*- 
  2  ## src/common/sleepy.py 
  3  ## 
  4  ## Copyright (C) 2003-2010 Yann Leboulanger <asterix AT lagaule.org> 
  5  ## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com> 
  6  ## Copyright (C) 2007 Jean-Marie Traissard <jim AT lapin.org> 
  7  ## Copyright (C) 2008 Mateusz Biliński <mateusz AT bilinski.it> 
  8  ## 
  9  ## This file is part of Gajim. 
 10  ## 
 11  ## Gajim is free software; you can redistribute it and/or modify 
 12  ## it under the terms of the GNU General Public License as published 
 13  ## by the Free Software Foundation; version 3 only. 
 14  ## 
 15  ## Gajim is distributed in the hope that it will be useful, 
 16  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 18  ## GNU General Public License for more details. 
 19  ## 
 20  ## You should have received a copy of the GNU General Public License 
 21  ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. 
 22  ## 
 23   
 24  from common import gajim 
 25  import os, sys 
 26   
 27   
 28  STATE_UNKNOWN  = 'OS probably not supported' 
 29  STATE_XA   = 'extended away' 
 30  STATE_AWAY   = 'away' 
 31  STATE_AWAKE     = 'awake' 
 32   
 33  SUPPORTED = True 
 34  try: 
 35      if os.name == 'nt': 
 36          import ctypes 
 37   
 38          GetTickCount = ctypes.windll.kernel32.GetTickCount 
 39          GetLastInputInfo = ctypes.windll.user32.GetLastInputInfo 
 40   
41 - class LASTINPUTINFO(ctypes.Structure):
42 _fields_ = [('cbSize', ctypes.c_uint), ('dwTime', ctypes.c_uint)]
43 44 lastInputInfo = LASTINPUTINFO() 45 lastInputInfo.cbSize = ctypes.sizeof(lastInputInfo) 46 47 # one or more of these may not be supported before XP. 48 OpenInputDesktop = ctypes.windll.user32.OpenInputDesktop 49 CloseDesktop = ctypes.windll.user32.CloseDesktop 50 SystemParametersInfo = ctypes.windll.user32.SystemParametersInfoW 51 else: # unix 52 from common import idle 53 except Exception: 54 gajim.log.debug('Unable to load idle module') 55 SUPPORTED = False 56
57 -class SleepyWindows:
58 - def __init__(self, away_interval = 60, xa_interval = 120):
59 self.away_interval = away_interval 60 self.xa_interval = xa_interval 61 self.state = STATE_AWAKE # assume we are awake
62
63 - def getIdleSec(self):
64 GetLastInputInfo(ctypes.byref(lastInputInfo)) 65 idleDelta = float(GetTickCount() - lastInputInfo.dwTime) / 1000 66 return idleDelta
67
68 - def poll(self):
69 """ 70 Check to see if we should change state 71 """ 72 if not SUPPORTED: 73 return False 74 75 # screen saver, in windows >= XP 76 saver_runing = ctypes.c_int(0) 77 # 0x72 is SPI_GETSCREENSAVERRUNNING 78 if SystemParametersInfo(0x72, 0, ctypes.byref(saver_runing), 0) and \ 79 saver_runing.value: 80 self.state = STATE_XA 81 return True 82 83 desk = OpenInputDesktop(0, False, 0) 84 if not desk: 85 # Screen locked 86 self.state = STATE_XA 87 return True 88 CloseDesktop(desk) 89 90 idleTime = self.getIdleSec() 91 92 # xa is stronger than away so check for xa first 93 if idleTime > self.xa_interval: 94 self.state = STATE_XA 95 elif idleTime > self.away_interval: 96 self.state = STATE_AWAY 97 else: 98 self.state = STATE_AWAKE 99 return True
100
101 - def getState(self):
102 return self.state
103
104 - def setState(self, val):
105 self.state = val
106
107 -class SleepyUnix:
108 - def __init__(self, away_interval = 60, xa_interval = 120):
109 global SUPPORTED 110 self.away_interval = away_interval 111 self.xa_interval = xa_interval 112 self.state = STATE_AWAKE # assume we are awake
113
114 - def getIdleSec(self):
115 return idle.getIdleSec()
116
117 - def poll(self):
118 """ 119 Check to see if we should change state 120 """ 121 if not SUPPORTED: 122 return False 123 124 idleTime = self.getIdleSec() 125 126 # xa is stronger than away so check for xa first 127 if idleTime > self.xa_interval: 128 self.state = STATE_XA 129 elif idleTime > self.away_interval: 130 self.state = STATE_AWAY 131 else: 132 self.state = STATE_AWAKE 133 return True
134
135 - def getState(self):
136 return self.state
137
138 - def setState(self, val):
139 self.state = val
140 141 if os.name == 'nt': 142 Sleepy = SleepyWindows 143 else: 144 Sleepy = SleepyUnix 145