2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2007-01-08 21:13:16 +03:00
|
|
|
/* vim:expandtab:shiftwidth=4:tabstop=4:
|
|
|
|
*/
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2012-09-14 05:56:59 +04:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2007-01-08 21:13:16 +03:00
|
|
|
#include "nsIdleServiceGTK.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "prlink.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2016-01-31 06:48:00 +03:00
|
|
|
using mozilla::LogLevel;
|
|
|
|
|
2016-05-18 22:55:42 +03:00
|
|
|
static mozilla::LazyLogModule sIdleLog("nsIIdleService");
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
typedef bool (*_XScreenSaverQueryExtension_fn)(Display* dpy, int* event_base,
|
2007-01-08 21:13:16 +03:00
|
|
|
int* error_base);
|
|
|
|
|
|
|
|
typedef XScreenSaverInfo* (*_XScreenSaverAllocInfo_fn)(void);
|
|
|
|
|
|
|
|
typedef void (*_XScreenSaverQueryInfo_fn)(Display* dpy, Drawable drw,
|
|
|
|
XScreenSaverInfo* info);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool sInitialized = false;
|
2012-07-30 18:20:58 +04:00
|
|
|
static _XScreenSaverQueryExtension_fn _XSSQueryExtension = nullptr;
|
|
|
|
static _XScreenSaverAllocInfo_fn _XSSAllocInfo = nullptr;
|
|
|
|
static _XScreenSaverQueryInfo_fn _XSSQueryInfo = nullptr;
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2007-09-27 20:53:48 +04:00
|
|
|
static void Initialize() {
|
2015-03-05 05:56:00 +03:00
|
|
|
if (!GDK_IS_X11_DISPLAY(gdk_display_get_default())) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-09-27 20:53:48 +04:00
|
|
|
// This will leak - See comments in ~nsIdleServiceGTK().
|
|
|
|
PRLibrary* xsslib = PR_LoadLibrary("libXss.so.1");
|
2007-01-08 21:13:16 +03:00
|
|
|
if (!xsslib) // ouch.
|
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sIdleLog, LogLevel::Warning, ("Failed to find libXss.so!\n"));
|
2007-01-08 21:13:16 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-01-08 21:13:16 +03:00
|
|
|
_XSSQueryExtension = (_XScreenSaverQueryExtension_fn)PR_FindFunctionSymbol(
|
|
|
|
xsslib, "XScreenSaverQueryExtension");
|
|
|
|
_XSSAllocInfo = (_XScreenSaverAllocInfo_fn)PR_FindFunctionSymbol(
|
|
|
|
xsslib, "XScreenSaverAllocInfo");
|
|
|
|
_XSSQueryInfo = (_XScreenSaverQueryInfo_fn)PR_FindFunctionSymbol(
|
|
|
|
xsslib, "XScreenSaverQueryInfo");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-01-08 21:13:16 +03:00
|
|
|
if (!_XSSQueryExtension)
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sIdleLog, LogLevel::Warning,
|
|
|
|
("Failed to get XSSQueryExtension!\n"));
|
2007-01-08 21:13:16 +03:00
|
|
|
if (!_XSSAllocInfo)
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sIdleLog, LogLevel::Warning, ("Failed to get XSSAllocInfo!\n"));
|
2007-01-08 21:13:16 +03:00
|
|
|
if (!_XSSQueryInfo)
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sIdleLog, LogLevel::Warning, ("Failed to get XSSQueryInfo!\n"));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-10-03 11:56:21 +04:00
|
|
|
sInitialized = true;
|
2009-09-17 21:13:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIdleServiceGTK::nsIdleServiceGTK() : mXssInfo(nullptr) { Initialize(); }
|
2007-01-08 21:13:16 +03:00
|
|
|
|
|
|
|
nsIdleServiceGTK::~nsIdleServiceGTK() {
|
|
|
|
if (mXssInfo) XFree(mXssInfo);
|
2007-09-27 20:53:48 +04: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-16 02:22:58 +04:00
|
|
|
if (xsslib) {
|
2007-01-08 21:13:16 +03:00
|
|
|
PR_UnloadLibrary(xsslib);
|
2012-07-30 18:20:58 +04:00
|
|
|
xsslib = nullptr;
|
2007-04-16 02:22:58 +04:00
|
|
|
}
|
2007-09-27 20:53:48 +04:00
|
|
|
#endif
|
2007-01-08 21:13:16 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool nsIdleServiceGTK::PollIdleTime(uint32_t* aIdleTime) {
|
2009-09-17 21:13:45 +04:00
|
|
|
if (!sInitialized) {
|
2013-08-26 03:56:53 +04:00
|
|
|
// For some reason, we could not find xscreensaver.
|
2010-04-16 21:37:16 +04:00
|
|
|
return false;
|
2009-09-17 21:13:45 +04:00
|
|
|
}
|
|
|
|
|
2007-01-08 21:13:16 +03:00
|
|
|
// Ask xscreensaver about idle time:
|
2010-04-16 21:37:16 +04:00
|
|
|
*aIdleTime = 0;
|
2007-01-08 21:13:16 +03:00
|
|
|
|
|
|
|
// We might not have a display (cf. in xpcshell)
|
2012-09-14 05:56:59 +04:00
|
|
|
Display* dplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
|
2007-09-27 20:53:48 +04:00
|
|
|
if (!dplay) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sIdleLog, LogLevel::Warning, ("No display found!\n"));
|
2010-04-16 21:37:16 +04:00
|
|
|
return false;
|
2007-09-27 20:53:48 +04:00
|
|
|
}
|
|
|
|
|
2007-01-08 21:13:16 +03:00
|
|
|
if (!_XSSQueryExtension || !_XSSAllocInfo || !_XSSQueryInfo) {
|
2010-04-16 21:37:16 +04:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2007-09-27 20:53:48 +04:00
|
|
|
int event_base, error_base;
|
2011-09-29 10:19:26 +04:00
|
|
|
if (_XSSQueryExtension(dplay, &event_base, &error_base)) {
|
2007-09-27 20:53:48 +04:00
|
|
|
if (!mXssInfo) mXssInfo = _XSSAllocInfo();
|
2015-06-04 01:25:57 +03:00
|
|
|
if (!mXssInfo) return false;
|
2007-01-08 21:13:16 +03:00
|
|
|
_XSSQueryInfo(dplay, GDK_ROOT_WINDOW(), mXssInfo);
|
2010-04-16 21:37:16 +04:00
|
|
|
*aIdleTime = mXssInfo->idle;
|
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2007-01-08 21:13:16 +03:00
|
|
|
// If we get here, we couldn't get to XScreenSaver:
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sIdleLog, LogLevel::Warning, ("XSSQueryExtension returned false!\n"));
|
2010-04-16 21:37:16 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsIdleServiceGTK::UsePollMode() { return sInitialized; }
|