Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim:expandtab:shiftwidth=4:tabstop=4: |
michael@0 | 3 | */ |
michael@0 | 4 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #include <gtk/gtk.h> |
michael@0 | 9 | |
michael@0 | 10 | #include "nsIdleServiceGTK.h" |
michael@0 | 11 | #include "nsIServiceManager.h" |
michael@0 | 12 | #include "nsDebug.h" |
michael@0 | 13 | #include "prlink.h" |
michael@0 | 14 | #include "prlog.h" |
michael@0 | 15 | |
michael@0 | 16 | #ifdef PR_LOGGING |
michael@0 | 17 | static PRLogModuleInfo* sIdleLog = nullptr; |
michael@0 | 18 | #endif |
michael@0 | 19 | |
michael@0 | 20 | typedef bool (*_XScreenSaverQueryExtension_fn)(Display* dpy, int* event_base, |
michael@0 | 21 | int* error_base); |
michael@0 | 22 | |
michael@0 | 23 | typedef XScreenSaverInfo* (*_XScreenSaverAllocInfo_fn)(void); |
michael@0 | 24 | |
michael@0 | 25 | typedef void (*_XScreenSaverQueryInfo_fn)(Display* dpy, Drawable drw, |
michael@0 | 26 | XScreenSaverInfo *info); |
michael@0 | 27 | |
michael@0 | 28 | static bool sInitialized = false; |
michael@0 | 29 | static _XScreenSaverQueryExtension_fn _XSSQueryExtension = nullptr; |
michael@0 | 30 | static _XScreenSaverAllocInfo_fn _XSSAllocInfo = nullptr; |
michael@0 | 31 | static _XScreenSaverQueryInfo_fn _XSSQueryInfo = nullptr; |
michael@0 | 32 | |
michael@0 | 33 | NS_IMPL_ISUPPORTS_INHERITED0(nsIdleServiceGTK, nsIdleService) |
michael@0 | 34 | |
michael@0 | 35 | static void Initialize() |
michael@0 | 36 | { |
michael@0 | 37 | // This will leak - See comments in ~nsIdleServiceGTK(). |
michael@0 | 38 | PRLibrary* xsslib = PR_LoadLibrary("libXss.so.1"); |
michael@0 | 39 | if (!xsslib) // ouch. |
michael@0 | 40 | { |
michael@0 | 41 | #ifdef PR_LOGGING |
michael@0 | 42 | PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to find libXss.so!\n")); |
michael@0 | 43 | #endif |
michael@0 | 44 | return; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | _XSSQueryExtension = (_XScreenSaverQueryExtension_fn) |
michael@0 | 48 | PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryExtension"); |
michael@0 | 49 | _XSSAllocInfo = (_XScreenSaverAllocInfo_fn) |
michael@0 | 50 | PR_FindFunctionSymbol(xsslib, "XScreenSaverAllocInfo"); |
michael@0 | 51 | _XSSQueryInfo = (_XScreenSaverQueryInfo_fn) |
michael@0 | 52 | PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryInfo"); |
michael@0 | 53 | #ifdef PR_LOGGING |
michael@0 | 54 | if (!_XSSQueryExtension) |
michael@0 | 55 | PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to get XSSQueryExtension!\n")); |
michael@0 | 56 | if (!_XSSAllocInfo) |
michael@0 | 57 | PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to get XSSAllocInfo!\n")); |
michael@0 | 58 | if (!_XSSQueryInfo) |
michael@0 | 59 | PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to get XSSQueryInfo!\n")); |
michael@0 | 60 | #endif |
michael@0 | 61 | |
michael@0 | 62 | sInitialized = true; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | nsIdleServiceGTK::nsIdleServiceGTK() |
michael@0 | 66 | : mXssInfo(nullptr) |
michael@0 | 67 | { |
michael@0 | 68 | #ifdef PR_LOGGING |
michael@0 | 69 | if (!sIdleLog) |
michael@0 | 70 | sIdleLog = PR_NewLogModule("nsIIdleService"); |
michael@0 | 71 | #endif |
michael@0 | 72 | |
michael@0 | 73 | Initialize(); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | nsIdleServiceGTK::~nsIdleServiceGTK() |
michael@0 | 77 | { |
michael@0 | 78 | if (mXssInfo) |
michael@0 | 79 | XFree(mXssInfo); |
michael@0 | 80 | |
michael@0 | 81 | // It is not safe to unload libXScrnSaver until each display is closed because |
michael@0 | 82 | // the library registers callbacks through XESetCloseDisplay (Bug 397607). |
michael@0 | 83 | // (Also the library and its functions are scoped for the file not the object.) |
michael@0 | 84 | #if 0 |
michael@0 | 85 | if (xsslib) { |
michael@0 | 86 | PR_UnloadLibrary(xsslib); |
michael@0 | 87 | xsslib = nullptr; |
michael@0 | 88 | } |
michael@0 | 89 | #endif |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | bool |
michael@0 | 93 | nsIdleServiceGTK::PollIdleTime(uint32_t *aIdleTime) |
michael@0 | 94 | { |
michael@0 | 95 | if (!sInitialized) { |
michael@0 | 96 | // For some reason, we could not find xscreensaver. |
michael@0 | 97 | return false; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | // Ask xscreensaver about idle time: |
michael@0 | 101 | *aIdleTime = 0; |
michael@0 | 102 | |
michael@0 | 103 | // We might not have a display (cf. in xpcshell) |
michael@0 | 104 | Display *dplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default()); |
michael@0 | 105 | if (!dplay) { |
michael@0 | 106 | #ifdef PR_LOGGING |
michael@0 | 107 | PR_LOG(sIdleLog, PR_LOG_WARNING, ("No display found!\n")); |
michael@0 | 108 | #endif |
michael@0 | 109 | return false; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | if (!_XSSQueryExtension || !_XSSAllocInfo || !_XSSQueryInfo) { |
michael@0 | 113 | return false; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | int event_base, error_base; |
michael@0 | 117 | if (_XSSQueryExtension(dplay, &event_base, &error_base)) |
michael@0 | 118 | { |
michael@0 | 119 | if (!mXssInfo) |
michael@0 | 120 | mXssInfo = _XSSAllocInfo(); |
michael@0 | 121 | if (!mXssInfo) |
michael@0 | 122 | return false; |
michael@0 | 123 | _XSSQueryInfo(dplay, GDK_ROOT_WINDOW(), mXssInfo); |
michael@0 | 124 | *aIdleTime = mXssInfo->idle; |
michael@0 | 125 | return true; |
michael@0 | 126 | } |
michael@0 | 127 | // If we get here, we couldn't get to XScreenSaver: |
michael@0 | 128 | #ifdef PR_LOGGING |
michael@0 | 129 | PR_LOG(sIdleLog, PR_LOG_WARNING, ("XSSQueryExtension returned false!\n")); |
michael@0 | 130 | #endif |
michael@0 | 131 | return false; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | bool |
michael@0 | 135 | nsIdleServiceGTK::UsePollMode() |
michael@0 | 136 | { |
michael@0 | 137 | return sInitialized; |
michael@0 | 138 | } |
michael@0 | 139 |