2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/common/sleepy.py
|
2005-01-07 22:52:38 +01:00
|
|
|
##
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:39:27 +02:00
|
|
|
## Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2007 Jean-Marie Traissard <jim AT lapin.org>
|
2008-08-15 19:31:51 +02:00
|
|
|
## Copyright (C) 2008 Mateusz Biliński <mateusz AT bilinski.it>
|
2005-01-07 22:52:38 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-01-07 22:52:38 +01:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-01-07 22:52:38 +01:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-01-07 22:52:38 +01:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-01-07 22:52:38 +01:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2004-01-02 16:21:02 +01:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2013-01-02 13:54:02 +01:00
|
|
|
import os
|
2004-01-02 16:21:02 +01:00
|
|
|
|
|
|
|
|
2005-05-21 00:05:11 +02:00
|
|
|
STATE_UNKNOWN = 'OS probably not supported'
|
2005-12-22 08:46:04 +01:00
|
|
|
STATE_XA = 'extended away'
|
2005-05-21 00:05:11 +02:00
|
|
|
STATE_AWAY = 'away'
|
2010-02-08 15:08:40 +01:00
|
|
|
STATE_AWAKE = 'awake'
|
2004-01-02 16:21:02 +01:00
|
|
|
|
2005-09-11 01:44:53 +02:00
|
|
|
SUPPORTED = True
|
2004-10-21 00:53:15 +02:00
|
|
|
try:
|
2010-02-08 15:08:40 +01:00
|
|
|
if os.name == 'nt':
|
|
|
|
import ctypes
|
2006-12-30 01:54:20 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
GetTickCount = ctypes.windll.kernel32.GetTickCount
|
|
|
|
GetLastInputInfo = ctypes.windll.user32.GetLastInputInfo
|
2006-12-30 01:54:20 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
class LASTINPUTINFO(ctypes.Structure):
|
|
|
|
_fields_ = [('cbSize', ctypes.c_uint), ('dwTime', ctypes.c_uint)]
|
2006-12-30 01:54:20 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
lastInputInfo = LASTINPUTINFO()
|
|
|
|
lastInputInfo.cbSize = ctypes.sizeof(lastInputInfo)
|
2009-10-15 19:51:49 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# one or more of these may not be supported before XP.
|
|
|
|
OpenInputDesktop = ctypes.windll.user32.OpenInputDesktop
|
|
|
|
CloseDesktop = ctypes.windll.user32.CloseDesktop
|
|
|
|
SystemParametersInfo = ctypes.windll.user32.SystemParametersInfoW
|
|
|
|
else: # unix
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import idle
|
2010-12-18 10:42:15 +01:00
|
|
|
idle.xss_available
|
2008-10-11 11:37:13 +02:00
|
|
|
except Exception:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.log.debug('Unable to load idle module')
|
2010-02-08 15:08:40 +01:00
|
|
|
SUPPORTED = False
|
2004-01-02 16:21:02 +01:00
|
|
|
|
2006-12-30 01:54:20 +01:00
|
|
|
class SleepyWindows:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, away_interval = 60, xa_interval = 120):
|
|
|
|
self.away_interval = away_interval
|
|
|
|
self.xa_interval = xa_interval
|
|
|
|
self.state = STATE_AWAKE # assume we are awake
|
|
|
|
|
|
|
|
def getIdleSec(self):
|
|
|
|
GetLastInputInfo(ctypes.byref(lastInputInfo))
|
|
|
|
idleDelta = float(GetTickCount() - lastInputInfo.dwTime) / 1000
|
|
|
|
return idleDelta
|
|
|
|
|
|
|
|
def poll(self):
|
|
|
|
"""
|
|
|
|
Check to see if we should change state
|
|
|
|
"""
|
|
|
|
if not SUPPORTED:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# screen saver, in windows >= XP
|
|
|
|
saver_runing = ctypes.c_int(0)
|
|
|
|
# 0x72 is SPI_GETSCREENSAVERRUNNING
|
|
|
|
if SystemParametersInfo(0x72, 0, ctypes.byref(saver_runing), 0) and \
|
|
|
|
saver_runing.value:
|
|
|
|
self.state = STATE_XA
|
|
|
|
return True
|
|
|
|
|
|
|
|
desk = OpenInputDesktop(0, False, 0)
|
|
|
|
if not desk:
|
|
|
|
# Screen locked
|
|
|
|
self.state = STATE_XA
|
|
|
|
return True
|
|
|
|
CloseDesktop(desk)
|
|
|
|
|
|
|
|
idleTime = self.getIdleSec()
|
|
|
|
|
|
|
|
# xa is stronger than away so check for xa first
|
|
|
|
if idleTime > self.xa_interval:
|
|
|
|
self.state = STATE_XA
|
|
|
|
elif idleTime > self.away_interval:
|
|
|
|
self.state = STATE_AWAY
|
|
|
|
else:
|
|
|
|
self.state = STATE_AWAKE
|
|
|
|
return True
|
|
|
|
|
|
|
|
def getState(self):
|
|
|
|
return self.state
|
|
|
|
|
|
|
|
def setState(self, val):
|
|
|
|
self.state = val
|
2006-12-30 01:54:20 +01:00
|
|
|
|
|
|
|
class SleepyUnix:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, away_interval = 60, xa_interval = 120):
|
|
|
|
global SUPPORTED
|
|
|
|
self.away_interval = away_interval
|
|
|
|
self.xa_interval = xa_interval
|
|
|
|
self.state = STATE_AWAKE # assume we are awake
|
|
|
|
|
|
|
|
def getIdleSec(self):
|
|
|
|
return idle.getIdleSec()
|
|
|
|
|
|
|
|
def poll(self):
|
|
|
|
"""
|
|
|
|
Check to see if we should change state
|
|
|
|
"""
|
|
|
|
if not SUPPORTED:
|
|
|
|
return False
|
|
|
|
|
|
|
|
idleTime = self.getIdleSec()
|
|
|
|
|
|
|
|
# xa is stronger than away so check for xa first
|
|
|
|
if idleTime > self.xa_interval:
|
|
|
|
self.state = STATE_XA
|
|
|
|
elif idleTime > self.away_interval:
|
|
|
|
self.state = STATE_AWAY
|
|
|
|
else:
|
|
|
|
self.state = STATE_AWAKE
|
|
|
|
return True
|
|
|
|
|
|
|
|
def getState(self):
|
|
|
|
return self.state
|
|
|
|
|
|
|
|
def setState(self, val):
|
|
|
|
self.state = val
|
2006-12-30 01:54:20 +01:00
|
|
|
|
|
|
|
if os.name == 'nt':
|
2010-02-08 15:08:40 +01:00
|
|
|
Sleepy = SleepyWindows
|
2006-12-30 01:54:20 +01:00
|
|
|
else:
|
2010-02-08 15:08:40 +01:00
|
|
|
Sleepy = SleepyUnix
|