Bug 1449132 [wpt PR 10194] - [css-grid] Fix resolution of percentage paddings and...
[gecko.git] / widget / gtk / nsIdleServiceGTK.cpp
blob1eee489f83373e6a24e0fe94ea8060f6efb46193
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
3 */
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/. */
8 #include <gtk/gtk.h>
10 #include "nsIdleServiceGTK.h"
11 #include "nsIServiceManager.h"
12 #include "nsDebug.h"
13 #include "prlink.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,
21 int* error_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()))
36 return;
38 // This will leak - See comments in ~nsIdleServiceGTK().
39 PRLibrary* xsslib = PR_LoadLibrary("libXss.so.1");
40 if (!xsslib) // ouch.
42 MOZ_LOG(sIdleLog, LogLevel::Warning, ("Failed to find libXss.so!\n"));
43 return;
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"));
55 if (!_XSSAllocInfo)
56 MOZ_LOG(sIdleLog, LogLevel::Warning, ("Failed to get XSSAllocInfo!\n"));
57 if (!_XSSQueryInfo)
58 MOZ_LOG(sIdleLog, LogLevel::Warning, ("Failed to get XSSQueryInfo!\n"));
60 sInitialized = true;
63 nsIdleServiceGTK::nsIdleServiceGTK()
64 : mXssInfo(nullptr)
66 Initialize();
69 nsIdleServiceGTK::~nsIdleServiceGTK()
71 if (mXssInfo)
72 XFree(mXssInfo);
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.)
77 #if 0
78 if (xsslib) {
79 PR_UnloadLibrary(xsslib);
80 xsslib = nullptr;
82 #endif
85 bool
86 nsIdleServiceGTK::PollIdleTime(uint32_t *aIdleTime)
88 if (!sInitialized) {
89 // For some reason, we could not find xscreensaver.
90 return false;
93 // Ask xscreensaver about idle time:
94 *aIdleTime = 0;
96 // We might not have a display (cf. in xpcshell)
97 Display *dplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
98 if (!dplay) {
99 MOZ_LOG(sIdleLog, LogLevel::Warning, ("No display found!\n"));
100 return false;
103 if (!_XSSQueryExtension || !_XSSAllocInfo || !_XSSQueryInfo) {
104 return false;
107 int event_base, error_base;
108 if (_XSSQueryExtension(dplay, &event_base, &error_base))
110 if (!mXssInfo)
111 mXssInfo = _XSSAllocInfo();
112 if (!mXssInfo)
113 return false;
114 _XSSQueryInfo(dplay, GDK_ROOT_WINDOW(), mXssInfo);
115 *aIdleTime = mXssInfo->idle;
116 return true;
118 // If we get here, we couldn't get to XScreenSaver:
119 MOZ_LOG(sIdleLog, LogLevel::Warning, ("XSSQueryExtension returned false!\n"));
120 return false;
123 bool
124 nsIdleServiceGTK::UsePollMode()
126 return sInitialized;