1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "nsIdleServiceGTK.h"
11 #include "nsIServiceManager.h"
14 #include "mozilla/Logging.h"
16 using mozilla::LogLevel
;
18 static mozilla::LazyLogModule
sIdleLog("nsIIdleService");
20 typedef bool (*_XScreenSaverQueryExtension_fn
)(Display
* dpy
, int* event_base
,
23 typedef XScreenSaverInfo
* (*_XScreenSaverAllocInfo_fn
)(void);
25 typedef void (*_XScreenSaverQueryInfo_fn
)(Display
* dpy
, Drawable drw
,
26 XScreenSaverInfo
*info
);
28 static bool sInitialized
= false;
29 static _XScreenSaverQueryExtension_fn _XSSQueryExtension
= nullptr;
30 static _XScreenSaverAllocInfo_fn _XSSAllocInfo
= nullptr;
31 static _XScreenSaverQueryInfo_fn _XSSQueryInfo
= nullptr;
33 static void Initialize()
35 if (!GDK_IS_X11_DISPLAY(gdk_display_get_default()))
38 // This will leak - See comments in ~nsIdleServiceGTK().
39 PRLibrary
* xsslib
= PR_LoadLibrary("libXss.so.1");
42 MOZ_LOG(sIdleLog
, LogLevel::Warning
, ("Failed to find libXss.so!\n"));
46 _XSSQueryExtension
= (_XScreenSaverQueryExtension_fn
)
47 PR_FindFunctionSymbol(xsslib
, "XScreenSaverQueryExtension");
48 _XSSAllocInfo
= (_XScreenSaverAllocInfo_fn
)
49 PR_FindFunctionSymbol(xsslib
, "XScreenSaverAllocInfo");
50 _XSSQueryInfo
= (_XScreenSaverQueryInfo_fn
)
51 PR_FindFunctionSymbol(xsslib
, "XScreenSaverQueryInfo");
53 if (!_XSSQueryExtension
)
54 MOZ_LOG(sIdleLog
, LogLevel::Warning
, ("Failed to get XSSQueryExtension!\n"));
56 MOZ_LOG(sIdleLog
, LogLevel::Warning
, ("Failed to get XSSAllocInfo!\n"));
58 MOZ_LOG(sIdleLog
, LogLevel::Warning
, ("Failed to get XSSQueryInfo!\n"));
63 nsIdleServiceGTK::nsIdleServiceGTK()
69 nsIdleServiceGTK::~nsIdleServiceGTK()
74 // It is not safe to unload libXScrnSaver until each display is closed because
75 // the library registers callbacks through XESetCloseDisplay (Bug 397607).
76 // (Also the library and its functions are scoped for the file not the object.)
79 PR_UnloadLibrary(xsslib
);
86 nsIdleServiceGTK::PollIdleTime(uint32_t *aIdleTime
)
89 // For some reason, we could not find xscreensaver.
93 // Ask xscreensaver about idle time:
96 // We might not have a display (cf. in xpcshell)
97 Display
*dplay
= GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
99 MOZ_LOG(sIdleLog
, LogLevel::Warning
, ("No display found!\n"));
103 if (!_XSSQueryExtension
|| !_XSSAllocInfo
|| !_XSSQueryInfo
) {
107 int event_base
, error_base
;
108 if (_XSSQueryExtension(dplay
, &event_base
, &error_base
))
111 mXssInfo
= _XSSAllocInfo();
114 _XSSQueryInfo(dplay
, GDK_ROOT_WINDOW(), mXssInfo
);
115 *aIdleTime
= mXssInfo
->idle
;
118 // If we get here, we couldn't get to XScreenSaver:
119 MOZ_LOG(sIdleLog
, LogLevel::Warning
, ("XSSQueryExtension returned false!\n"));
124 nsIdleServiceGTK::UsePollMode()