2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:expandtab:shiftwidth=4:tabstop=4:
|
|
|
|
*/
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (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.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Gijs Kruitbosch <gijskruitbosch@gmail.com>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Gijs Kruitbosch <gijskruitbosch@gmail.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsIdleServiceGTK.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "prlink.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo* sIdleLog = nsnull;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef PRBool (*_XScreenSaverQueryExtension_fn)(Display* dpy, int* event_base,
|
|
|
|
int* error_base);
|
|
|
|
|
|
|
|
typedef XScreenSaverInfo* (*_XScreenSaverAllocInfo_fn)(void);
|
|
|
|
|
|
|
|
typedef void (*_XScreenSaverQueryInfo_fn)(Display* dpy, Drawable drw,
|
|
|
|
XScreenSaverInfo *info);
|
|
|
|
|
2007-09-27 09:53:48 -07:00
|
|
|
static PRBool sInitialized = PR_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
static _XScreenSaverQueryExtension_fn _XSSQueryExtension = nsnull;
|
|
|
|
static _XScreenSaverAllocInfo_fn _XSSAllocInfo = nsnull;
|
|
|
|
static _XScreenSaverQueryInfo_fn _XSSQueryInfo = nsnull;
|
|
|
|
|
2010-03-31 07:07:14 -07:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsIdleServiceGTK, nsIIdleService)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-27 09:53:48 -07:00
|
|
|
static void Initialize()
|
|
|
|
{
|
|
|
|
// This will leak - See comments in ~nsIdleServiceGTK().
|
|
|
|
PRLibrary* xsslib = PR_LoadLibrary("libXss.so.1");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!xsslib) // ouch.
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to find libXss.so!\n"));
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_XSSQueryExtension = (_XScreenSaverQueryExtension_fn)
|
|
|
|
PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryExtension");
|
|
|
|
_XSSAllocInfo = (_XScreenSaverAllocInfo_fn)
|
|
|
|
PR_FindFunctionSymbol(xsslib, "XScreenSaverAllocInfo");
|
|
|
|
_XSSQueryInfo = (_XScreenSaverQueryInfo_fn)
|
|
|
|
PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryInfo");
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!_XSSQueryExtension)
|
|
|
|
PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to get XSSQueryExtension!\n"));
|
|
|
|
if (!_XSSAllocInfo)
|
|
|
|
PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to get XSSAllocInfo!\n"));
|
|
|
|
if (!_XSSQueryInfo)
|
|
|
|
PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to get XSSQueryInfo!\n"));
|
|
|
|
#endif
|
2009-09-17 10:13:45 -07:00
|
|
|
|
|
|
|
sInitialized = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIdleServiceGTK::nsIdleServiceGTK()
|
|
|
|
: mXssInfo(nsnull)
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!sIdleLog)
|
|
|
|
sIdleLog = PR_NewLogModule("nsIIdleService");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Initialize();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIdleServiceGTK::~nsIdleServiceGTK()
|
|
|
|
{
|
|
|
|
if (mXssInfo)
|
|
|
|
XFree(mXssInfo);
|
2007-09-27 09:53:48 -07:00
|
|
|
|
|
|
|
// It is not safe to unload libXScrnSaver until each display is closed because
|
|
|
|
// the library registers callbacks through XESetCloseDisplay (Bug 397607).
|
|
|
|
// (Also the library and its functions are scoped for the file not the object.)
|
|
|
|
#if 0
|
2007-04-15 15:22:58 -07:00
|
|
|
if (xsslib) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_UnloadLibrary(xsslib);
|
2007-04-15 15:22:58 -07:00
|
|
|
xsslib = nsnull;
|
|
|
|
}
|
2007-09-27 09:53:48 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-31 07:07:14 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIdleServiceGTK::GetIdleTime(PRUint32 *aTimeDiff)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-09-17 10:13:45 -07:00
|
|
|
if (!sInitialized) {
|
2010-03-31 07:07:14 -07:00
|
|
|
// For some reason, we could not find xscreensaver. This this might be because
|
|
|
|
// we are on a mobile platforms (e.g. Maemo/OSSO). In this case, fall back to
|
|
|
|
// using gLastInputEventTime which is
|
|
|
|
|
|
|
|
// The last user input event time in microseconds. If there are any pending
|
|
|
|
// native toolkit input events it returns the current time. The value is
|
|
|
|
// compatible with PR_IntervalToMicroseconds(PR_IntervalNow()).
|
|
|
|
// DEFINED IN widget/src/gtk2/nsWindow.cpp
|
|
|
|
extern PRUint32 gLastInputEventTime;
|
|
|
|
|
|
|
|
PRUint32 nowTime = PR_IntervalToMicroseconds(PR_IntervalNow());
|
|
|
|
*aTimeDiff = (nowTime - gLastInputEventTime) / 1000;
|
|
|
|
return NS_OK;
|
2009-09-17 10:13:45 -07:00
|
|
|
}
|
|
|
|
|
2010-03-31 07:07:14 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Ask xscreensaver about idle time:
|
2010-03-31 07:07:14 -07:00
|
|
|
*aTimeDiff = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// We might not have a display (cf. in xpcshell)
|
|
|
|
Display *dplay = GDK_DISPLAY();
|
2007-09-27 09:53:48 -07:00
|
|
|
if (!dplay) {
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
2007-09-27 09:53:48 -07:00
|
|
|
PR_LOG(sIdleLog, PR_LOG_WARNING, ("No display found!\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2010-03-31 07:07:14 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-27 09:53:48 -07:00
|
|
|
if (!_XSSQueryExtension || !_XSSAllocInfo || !_XSSQueryInfo) {
|
2010-03-31 07:07:14 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-09-27 09:53:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int event_base, error_base;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (_XSSQueryExtension(dplay, &event_base, &error_base))
|
|
|
|
{
|
|
|
|
if (!mXssInfo)
|
|
|
|
mXssInfo = _XSSAllocInfo();
|
|
|
|
if (!mXssInfo)
|
2010-03-31 07:07:14 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
_XSSQueryInfo(dplay, GDK_ROOT_WINDOW(), mXssInfo);
|
2010-03-31 07:07:14 -07:00
|
|
|
*aTimeDiff = mXssInfo->idle;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// If we get here, we couldn't get to XScreenSaver:
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PR_LOG(sIdleLog, PR_LOG_WARNING, ("XSSQueryExtension returned false!\n"));
|
|
|
|
#endif
|
2010-03-31 07:07:14 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|