widget/gtk/nsIdleServiceGTK.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gtk/nsIdleServiceGTK.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     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 +#include <gtk/gtk.h>
    1.12 +
    1.13 +#include "nsIdleServiceGTK.h"
    1.14 +#include "nsIServiceManager.h"
    1.15 +#include "nsDebug.h"
    1.16 +#include "prlink.h"
    1.17 +#include "prlog.h"
    1.18 +
    1.19 +#ifdef PR_LOGGING
    1.20 +static PRLogModuleInfo* sIdleLog = nullptr;
    1.21 +#endif
    1.22 +
    1.23 +typedef bool (*_XScreenSaverQueryExtension_fn)(Display* dpy, int* event_base,
    1.24 +                                                 int* error_base);
    1.25 +
    1.26 +typedef XScreenSaverInfo* (*_XScreenSaverAllocInfo_fn)(void);
    1.27 +
    1.28 +typedef void (*_XScreenSaverQueryInfo_fn)(Display* dpy, Drawable drw,
    1.29 +                                          XScreenSaverInfo *info);
    1.30 +
    1.31 +static bool sInitialized = false;
    1.32 +static _XScreenSaverQueryExtension_fn _XSSQueryExtension = nullptr;
    1.33 +static _XScreenSaverAllocInfo_fn _XSSAllocInfo = nullptr;
    1.34 +static _XScreenSaverQueryInfo_fn _XSSQueryInfo = nullptr;
    1.35 +
    1.36 +NS_IMPL_ISUPPORTS_INHERITED0(nsIdleServiceGTK, nsIdleService)
    1.37 +
    1.38 +static void Initialize()
    1.39 +{
    1.40 +    // This will leak - See comments in ~nsIdleServiceGTK().
    1.41 +    PRLibrary* xsslib = PR_LoadLibrary("libXss.so.1");
    1.42 +    if (!xsslib) // ouch.
    1.43 +    {
    1.44 +#ifdef PR_LOGGING
    1.45 +        PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to find libXss.so!\n"));
    1.46 +#endif
    1.47 +        return;
    1.48 +    }
    1.49 +
    1.50 +    _XSSQueryExtension = (_XScreenSaverQueryExtension_fn)
    1.51 +        PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryExtension");
    1.52 +    _XSSAllocInfo = (_XScreenSaverAllocInfo_fn)
    1.53 +        PR_FindFunctionSymbol(xsslib, "XScreenSaverAllocInfo");
    1.54 +    _XSSQueryInfo = (_XScreenSaverQueryInfo_fn)
    1.55 +        PR_FindFunctionSymbol(xsslib, "XScreenSaverQueryInfo");
    1.56 +#ifdef PR_LOGGING
    1.57 +    if (!_XSSQueryExtension)
    1.58 +        PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to get XSSQueryExtension!\n"));
    1.59 +    if (!_XSSAllocInfo)
    1.60 +        PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to get XSSAllocInfo!\n"));
    1.61 +    if (!_XSSQueryInfo)
    1.62 +        PR_LOG(sIdleLog, PR_LOG_WARNING, ("Failed to get XSSQueryInfo!\n"));
    1.63 +#endif
    1.64 +
    1.65 +    sInitialized = true;
    1.66 +}
    1.67 +
    1.68 +nsIdleServiceGTK::nsIdleServiceGTK()
    1.69 +    : mXssInfo(nullptr)
    1.70 +{
    1.71 +#ifdef PR_LOGGING
    1.72 +    if (!sIdleLog)
    1.73 +        sIdleLog = PR_NewLogModule("nsIIdleService");
    1.74 +#endif
    1.75 +
    1.76 +    Initialize();
    1.77 +}
    1.78 +
    1.79 +nsIdleServiceGTK::~nsIdleServiceGTK()
    1.80 +{
    1.81 +    if (mXssInfo)
    1.82 +        XFree(mXssInfo);
    1.83 +
    1.84 +// It is not safe to unload libXScrnSaver until each display is closed because
    1.85 +// the library registers callbacks through XESetCloseDisplay (Bug 397607).
    1.86 +// (Also the library and its functions are scoped for the file not the object.)
    1.87 +#if 0
    1.88 +    if (xsslib) {
    1.89 +        PR_UnloadLibrary(xsslib);
    1.90 +        xsslib = nullptr;
    1.91 +    }
    1.92 +#endif
    1.93 +}
    1.94 +
    1.95 +bool
    1.96 +nsIdleServiceGTK::PollIdleTime(uint32_t *aIdleTime)
    1.97 +{
    1.98 +    if (!sInitialized) {
    1.99 +        // For some reason, we could not find xscreensaver.
   1.100 +        return false;
   1.101 +    }
   1.102 +
   1.103 +    // Ask xscreensaver about idle time:
   1.104 +    *aIdleTime = 0;
   1.105 +
   1.106 +    // We might not have a display (cf. in xpcshell)
   1.107 +    Display *dplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
   1.108 +    if (!dplay) {
   1.109 +#ifdef PR_LOGGING
   1.110 +        PR_LOG(sIdleLog, PR_LOG_WARNING, ("No display found!\n"));
   1.111 +#endif
   1.112 +        return false;
   1.113 +    }
   1.114 +
   1.115 +    if (!_XSSQueryExtension || !_XSSAllocInfo || !_XSSQueryInfo) {
   1.116 +        return false;
   1.117 +    }
   1.118 +
   1.119 +    int event_base, error_base;
   1.120 +    if (_XSSQueryExtension(dplay, &event_base, &error_base))
   1.121 +    {
   1.122 +        if (!mXssInfo)
   1.123 +            mXssInfo = _XSSAllocInfo();
   1.124 +        if (!mXssInfo)
   1.125 +            return false;
   1.126 +        _XSSQueryInfo(dplay, GDK_ROOT_WINDOW(), mXssInfo);
   1.127 +        *aIdleTime = mXssInfo->idle;
   1.128 +        return true;
   1.129 +    }
   1.130 +    // If we get here, we couldn't get to XScreenSaver:
   1.131 +#ifdef PR_LOGGING
   1.132 +    PR_LOG(sIdleLog, PR_LOG_WARNING, ("XSSQueryExtension returned false!\n"));
   1.133 +#endif
   1.134 +    return false;
   1.135 +}
   1.136 +
   1.137 +bool
   1.138 +nsIdleServiceGTK::UsePollMode()
   1.139 +{
   1.140 +    return sInitialized;
   1.141 +}
   1.142 +

mercurial