1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/qt/nsIdleServiceQt.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,123 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim:expandtab:shiftwidth=4:tabstop=4: 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#ifdef MOZ_X11 1.12 +#include "mozilla/X11Util.h" 1.13 +#endif 1.14 +#include "nsIdleServiceQt.h" 1.15 +#include "nsIServiceManager.h" 1.16 +#include "nsDebug.h" 1.17 +#include "prlink.h" 1.18 + 1.19 +#if defined(MOZ_X11) 1.20 +typedef bool (*_XScreenSaverQueryExtension_fn)(Display* dpy, int* event_base, 1.21 + int* error_base); 1.22 + 1.23 +typedef XScreenSaverInfo* (*_XScreenSaverAllocInfo_fn)(void); 1.24 + 1.25 +typedef void (*_XScreenSaverQueryInfo_fn)(Display* dpy, Drawable drw, 1.26 + XScreenSaverInfo *info); 1.27 + 1.28 +static _XScreenSaverQueryExtension_fn _XSSQueryExtension = nullptr; 1.29 +static _XScreenSaverAllocInfo_fn _XSSAllocInfo = nullptr; 1.30 +static _XScreenSaverQueryInfo_fn _XSSQueryInfo = nullptr; 1.31 +#endif 1.32 + 1.33 +static bool sInitialized = false; 1.34 + 1.35 +NS_IMPL_ISUPPORTS_INHERITED0(nsIdleServiceQt, nsIdleService) 1.36 + 1.37 +nsIdleServiceQt::nsIdleServiceQt() 1.38 +#if defined(MOZ_X11) 1.39 + : mXssInfo(nullptr) 1.40 +#endif 1.41 +{ 1.42 +} 1.43 + 1.44 +static void Initialize() 1.45 +{ 1.46 + sInitialized = true; 1.47 + 1.48 +#if defined(MOZ_X11) 1.49 + // This will leak - See comments in ~nsIdleServiceQt(). 1.50 + PRLibrary* xsslib = PR_LoadLibrary("libXss.so.1"); 1.51 + if (!xsslib) { 1.52 + return; 1.53 + } 1.54 + 1.55 + _XSSQueryExtension = (_XScreenSaverQueryExtension_fn) 1.56 + PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryExtension"); 1.57 + _XSSAllocInfo = (_XScreenSaverAllocInfo_fn) 1.58 + PR_FindFunctionSymbol(xsslib, "XScreenSaverAllocInfo"); 1.59 + _XSSQueryInfo = (_XScreenSaverQueryInfo_fn) 1.60 + PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryInfo"); 1.61 +#endif 1.62 +} 1.63 + 1.64 +nsIdleServiceQt::~nsIdleServiceQt() 1.65 +{ 1.66 +#if defined(MOZ_X11) 1.67 + if (mXssInfo) 1.68 + XFree(mXssInfo); 1.69 + 1.70 +// It is not safe to unload libXScrnSaver until each display is closed because 1.71 +// the library registers callbacks through XESetCloseDisplay (Bug 397607). 1.72 +// (Also the library and its functions are scoped for the file not the object.) 1.73 +#if 0 1.74 + if (xsslib) { 1.75 + PR_UnloadLibrary(xsslib); 1.76 + xsslib = nullptr; 1.77 + } 1.78 +#endif 1.79 +#endif 1.80 +} 1.81 + 1.82 +bool 1.83 +nsIdleServiceQt::PollIdleTime(uint32_t *aIdleTime) 1.84 +{ 1.85 +#if defined(MOZ_X11) 1.86 + // Ask xscreensaver about idle time: 1.87 + *aIdleTime = 0; 1.88 + 1.89 + // We might not have a display (cf. in xpcshell) 1.90 + Display *dplay = mozilla::DefaultXDisplay(); 1.91 + if (!dplay) { 1.92 + return false; 1.93 + } 1.94 + 1.95 + if (!sInitialized) { 1.96 + Initialize(); 1.97 + } 1.98 + if (!_XSSQueryExtension || !_XSSAllocInfo || !_XSSQueryInfo) { 1.99 + return false; 1.100 + } 1.101 + 1.102 + int event_base, error_base; 1.103 + if (_XSSQueryExtension(dplay, &event_base, &error_base)) { 1.104 + if (!mXssInfo) 1.105 + mXssInfo = _XSSAllocInfo(); 1.106 + if (!mXssInfo) 1.107 + return false; 1.108 + 1.109 + _XSSQueryInfo(dplay, RootWindowOfScreen(DefaultScreenOfDisplay(mozilla::DefaultXDisplay())), mXssInfo); 1.110 + *aIdleTime = mXssInfo->idle; 1.111 + return true; 1.112 + } 1.113 +#endif 1.114 + 1.115 + return false; 1.116 +} 1.117 + 1.118 +bool 1.119 +nsIdleServiceQt::UsePollMode() 1.120 +{ 1.121 +#if defined(MOZ_X11) 1.122 + return false; 1.123 +#endif 1.124 + return true; 1.125 +} 1.126 +