michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim:expandtab:shiftwidth=4:tabstop=4: michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifdef MOZ_X11 michael@0: #include "mozilla/X11Util.h" michael@0: #endif michael@0: #include "nsIdleServiceQt.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsDebug.h" michael@0: #include "prlink.h" michael@0: michael@0: #if defined(MOZ_X11) michael@0: typedef bool (*_XScreenSaverQueryExtension_fn)(Display* dpy, int* event_base, michael@0: int* error_base); michael@0: michael@0: typedef XScreenSaverInfo* (*_XScreenSaverAllocInfo_fn)(void); michael@0: michael@0: typedef void (*_XScreenSaverQueryInfo_fn)(Display* dpy, Drawable drw, michael@0: XScreenSaverInfo *info); michael@0: michael@0: static _XScreenSaverQueryExtension_fn _XSSQueryExtension = nullptr; michael@0: static _XScreenSaverAllocInfo_fn _XSSAllocInfo = nullptr; michael@0: static _XScreenSaverQueryInfo_fn _XSSQueryInfo = nullptr; michael@0: #endif michael@0: michael@0: static bool sInitialized = false; michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED0(nsIdleServiceQt, nsIdleService) michael@0: michael@0: nsIdleServiceQt::nsIdleServiceQt() michael@0: #if defined(MOZ_X11) michael@0: : mXssInfo(nullptr) michael@0: #endif michael@0: { michael@0: } michael@0: michael@0: static void Initialize() michael@0: { michael@0: sInitialized = true; michael@0: michael@0: #if defined(MOZ_X11) michael@0: // This will leak - See comments in ~nsIdleServiceQt(). michael@0: PRLibrary* xsslib = PR_LoadLibrary("libXss.so.1"); michael@0: if (!xsslib) { michael@0: return; michael@0: } michael@0: michael@0: _XSSQueryExtension = (_XScreenSaverQueryExtension_fn) michael@0: PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryExtension"); michael@0: _XSSAllocInfo = (_XScreenSaverAllocInfo_fn) michael@0: PR_FindFunctionSymbol(xsslib, "XScreenSaverAllocInfo"); michael@0: _XSSQueryInfo = (_XScreenSaverQueryInfo_fn) michael@0: PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryInfo"); michael@0: #endif michael@0: } michael@0: michael@0: nsIdleServiceQt::~nsIdleServiceQt() michael@0: { michael@0: #if defined(MOZ_X11) michael@0: if (mXssInfo) michael@0: XFree(mXssInfo); michael@0: michael@0: // It is not safe to unload libXScrnSaver until each display is closed because michael@0: // the library registers callbacks through XESetCloseDisplay (Bug 397607). michael@0: // (Also the library and its functions are scoped for the file not the object.) michael@0: #if 0 michael@0: if (xsslib) { michael@0: PR_UnloadLibrary(xsslib); michael@0: xsslib = nullptr; michael@0: } michael@0: #endif michael@0: #endif michael@0: } michael@0: michael@0: bool michael@0: nsIdleServiceQt::PollIdleTime(uint32_t *aIdleTime) michael@0: { michael@0: #if defined(MOZ_X11) michael@0: // Ask xscreensaver about idle time: michael@0: *aIdleTime = 0; michael@0: michael@0: // We might not have a display (cf. in xpcshell) michael@0: Display *dplay = mozilla::DefaultXDisplay(); michael@0: if (!dplay) { michael@0: return false; michael@0: } michael@0: michael@0: if (!sInitialized) { michael@0: Initialize(); michael@0: } michael@0: if (!_XSSQueryExtension || !_XSSAllocInfo || !_XSSQueryInfo) { michael@0: return false; michael@0: } michael@0: michael@0: int event_base, error_base; michael@0: if (_XSSQueryExtension(dplay, &event_base, &error_base)) { michael@0: if (!mXssInfo) michael@0: mXssInfo = _XSSAllocInfo(); michael@0: if (!mXssInfo) michael@0: return false; michael@0: michael@0: _XSSQueryInfo(dplay, RootWindowOfScreen(DefaultScreenOfDisplay(mozilla::DefaultXDisplay())), mXssInfo); michael@0: *aIdleTime = mXssInfo->idle; michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: nsIdleServiceQt::UsePollMode() michael@0: { michael@0: #if defined(MOZ_X11) michael@0: return false; michael@0: #endif michael@0: return true; michael@0: } michael@0: