displaysettings.cpp 14.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * Copyright (C) 2013 Jolla Ltd. <pekka.vuorela@jollamobile.com>
 *
 * You may use this file under the terms of the BSD license as follows:
 *
 * "Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Nemo Mobile nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 */

#include <mce/dbus-names.h>
#include <mce/mode-names.h>
#include "mceiface.h"
#include "displaysettings.h"
36
#include <MGConfItem>
37 38
#include <QDebug>

39 40 41
static const char *MceDisplayBrightness = "/system/osso/dsm/display/display_brightness";
static const char *MceDisplayDimTimeout = "/system/osso/dsm/display/display_dim_timeout";
static const char *MceDisplayBlankTimeout = "/system/osso/dsm/display/display_blank_timeout";
42
static const char *MceDisplayInhibitMode = "/system/osso/dsm/display/inhibit_blank_mode";
43
static const char *MceDisplayUseAdaptiveDimming = "/system/osso/dsm/display/use_adaptive_display_dimming";
44
static const char *MceDisplayUseLowPowerMode = "/system/osso/dsm/display/use_low_power_mode";
45
static const char *MceDisplayUseAmbientLightSensor = "/system/osso/dsm/display/als_enabled";
46
static const char *MceDisplayAutoBrightnessEnabled = "/system/osso/dsm/display/als_autobrightness";
47
static const char *MceDoubleTapMode = "/system/osso/dsm/doubletap/mode";
48
static const char *MceLidSensorEnabled = "/system/osso/dsm/locks/lid_sensor_enabled";
49
static const char *MceLidSensorFilteringEnabled = "/system/osso/dsm/locks/filter_lid_with_als";
50
static const char *MceFlipOverGestureEnabled = "/system/osso/dsm/display/flipover_gesture_enabled";
51 52 53
static const char *McePowerSaveModeForced = "/system/osso/dsm/energymanagement/force_power_saving";
static const char *McePowerSaveModeEnabled = "/system/osso/dsm/energymanagement/enable_power_saving";
static const char *McePowerSaveModeThreshold = "/system/osso/dsm/energymanagement/psm_threshold";
54

55
DisplaySettings::DisplaySettings(QObject *parent)
56
    : QObject(parent)
57
{
58 59 60
    m_orientationLock = new MGConfItem("/lipstick/orientationLock", this);
    connect(m_orientationLock, SIGNAL(valueChanged()), SIGNAL(orientationLockChanged()));

61 62 63 64 65 66 67 68 69 70 71 72 73
    /* Initialize to defaults */
    m_autoBrightnessEnabled     = true;
    m_ambientLightSensorEnabled = true;
    m_blankTimeout              = 3;
    m_brightness                = 60;
    m_dimTimeout                = 30;
    m_flipoverGestureEnabled    = true;
    m_inhibitMode               = InhibitOff;
    m_adaptiveDimmingEnabled    = true;
    m_lowPowerModeEnabled       = false;
    m_doubleTapMode             = true;
    m_lidSensorFilteringEnabled = true;
    m_lidSensorEnabled          = true;
74 75 76
    m_powerSaveModeForced       = false;
    m_powerSaveModeEnabled      = false;
    m_powerSaveModeThreshold    = 20;
77
    m_populated                 = false;
78 79 80 81

    /* Setup change listener & get current values via async query */
    m_mceSignalIface = new ComNokiaMceSignalInterface(MCE_SERVICE, MCE_SIGNAL_PATH, QDBusConnection::systemBus(), this);
    connect(m_mceSignalIface, SIGNAL(config_change_ind(QString,QDBusVariant)), this, SLOT(configChange(QString,QDBusVariant)));
82

83 84 85 86 87 88
    m_mceIface = new ComNokiaMceRequestInterface(MCE_SERVICE, MCE_REQUEST_PATH, QDBusConnection::systemBus(), this);
    QDBusPendingReply<QVariantMap> call = m_mceIface->get_config_all();
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
    QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher *)),
                     this, SLOT(configReply(QDBusPendingCallWatcher *)));
}
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103
void DisplaySettings::configReply(QDBusPendingCallWatcher *watcher)
{
    QDBusPendingReply<QVariantMap> reply =  *watcher;

    if (reply.isError()) {
        qWarning("Could not retrieve mce settings: '%s'",
                 reply.error().message().toStdString().c_str());
    } else {
        QVariantMap map = reply.value();
        QMapIterator<QString, QVariant> i(map);
        while (i.hasNext()) {
            i.next();
            updateConfig(i.key(), i.value());
        }
104 105
        m_populated = true;
        emit populatedChanged();
106
    }
107

108
    watcher->deleteLater();
109 110
}

111
int DisplaySettings::brightness() const
112 113 114 115 116 117 118 119
{
    return m_brightness;
}

void DisplaySettings::setBrightness(int value)
{
    if (m_brightness != value) {
        m_brightness = value;
120
        m_mceIface->set_config(QDBusObjectPath(MceDisplayBrightness), QDBusVariant(value));
121 122 123 124 125 126 127 128 129 130 131 132
        emit brightnessChanged();
    }
}

int DisplaySettings::maximumBrightness()
{
    QDBusPendingReply<QDBusVariant> result = m_mceIface->get_config(QDBusObjectPath("/system/osso/dsm/display/max_display_brightness_levels"));
    result.waitForFinished();

    return result.value().variant().toInt();
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
int DisplaySettings::dimTimeout() const
{
    return m_dimTimeout;
}

void DisplaySettings::setDimTimeout(int value)
{
    if (m_dimTimeout != value) {
        m_dimTimeout = value;
        m_mceIface->set_config(QDBusObjectPath(MceDisplayDimTimeout), QDBusVariant(value));
        emit dimTimeoutChanged();
    }
}

int DisplaySettings::blankTimeout() const
{
    return m_blankTimeout;
}

void DisplaySettings::setBlankTimeout(int value)
{
    if (m_blankTimeout != value) {
        m_blankTimeout = value;
        m_mceIface->set_config(QDBusObjectPath(MceDisplayBlankTimeout), QDBusVariant(value));
        emit blankTimeoutChanged();
    }
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174
DisplaySettings::InhibitMode DisplaySettings::inhibitMode() const
{
    return m_inhibitMode;
}

void DisplaySettings::setInhibitMode(InhibitMode mode)
{
    if (m_inhibitMode != mode) {
        m_inhibitMode = mode;
        m_mceIface->set_config(QDBusObjectPath(MceDisplayInhibitMode), QDBusVariant(static_cast<int>(mode)));
        emit inhibitModeChanged();
    }
}

175
bool DisplaySettings::adaptiveDimmingEnabled() const
176
{
177
    return m_adaptiveDimmingEnabled;
178 179
}

180
void DisplaySettings::setAdaptiveDimmingEnabled(bool enabled)
181
{
182 183
    if (m_adaptiveDimmingEnabled != enabled) {
        m_adaptiveDimmingEnabled = enabled;
184
        m_mceIface->set_config(QDBusObjectPath(MceDisplayUseAdaptiveDimming), QDBusVariant(enabled));
185 186 187 188
        emit adaptiveDimmingEnabledChanged();
    }
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202
bool DisplaySettings::lowPowerModeEnabled() const
{
    return m_lowPowerModeEnabled;
}

void DisplaySettings::setLowPowerModeEnabled(bool enabled)
{
    if (m_lowPowerModeEnabled != enabled) {
        m_lowPowerModeEnabled = enabled;
        m_mceIface->set_config(QDBusObjectPath(MceDisplayUseLowPowerMode), QDBusVariant(enabled));
        emit lowPowerModeEnabledChanged();
    }
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216
bool DisplaySettings::ambientLightSensorEnabled() const
{
    return m_ambientLightSensorEnabled;
}

void DisplaySettings::setAmbientLightSensorEnabled(bool enabled)
{
    if (m_ambientLightSensorEnabled != enabled) {
        m_ambientLightSensorEnabled = enabled;
        m_mceIface->set_config(QDBusObjectPath(MceDisplayUseAmbientLightSensor), QDBusVariant(enabled));
        emit ambientLightSensorEnabledChanged();
    }
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230
bool DisplaySettings::autoBrightnessEnabled() const
{
    return m_autoBrightnessEnabled;
}

void DisplaySettings::setAutoBrightnessEnabled(bool enabled)
{
    if (m_autoBrightnessEnabled != enabled) {
        m_autoBrightnessEnabled = enabled;
        m_mceIface->set_config(QDBusObjectPath(MceDisplayAutoBrightnessEnabled), QDBusVariant(enabled));
        emit autoBrightnessEnabledChanged();
    }
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244
int DisplaySettings::doubleTapMode() const
{
    return m_doubleTapMode;
}

void DisplaySettings::setDoubleTapMode(int mode)
{
    if (m_doubleTapMode != mode) {
        m_doubleTapMode = mode;
        m_mceIface->set_config(QDBusObjectPath(MceDoubleTapMode), QDBusVariant(mode));
        emit doubleTapModeChanged();
    }
}

245 246
QVariant DisplaySettings::orientationLock() const
{
247
    return m_orientationLock->value("dynamic");
248 249 250 251
}

void DisplaySettings::setOrientationLock(const QVariant &orientationLock)
{
252
    m_orientationLock->set(orientationLock);
253 254
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268
bool DisplaySettings::lidSensorEnabled() const
{
    return m_lidSensorEnabled;
}

void DisplaySettings::setLidSensorEnabled(bool enabled)
{
    if (m_lidSensorEnabled != enabled) {
        m_lidSensorEnabled = enabled;
        m_mceIface->set_config(QDBusObjectPath(MceLidSensorEnabled), QDBusVariant(enabled));
        emit lidSensorEnabledChanged();
    }
}

269 270 271 272 273 274 275 276 277 278 279 280 281
bool DisplaySettings::lidSensorFilteringEnabled() const
{
    return m_lidSensorFilteringEnabled;
}

void DisplaySettings::setLidSensorFilteringEnabled(bool enabled)
{
    if (m_lidSensorFilteringEnabled != enabled) {
        m_lidSensorFilteringEnabled = enabled;
        m_mceIface->set_config(QDBusObjectPath(MceLidSensorFilteringEnabled), QDBusVariant(enabled));
        emit lidSensorFilteringEnabledChanged();
    }
}
282 283 284 285 286 287 288 289 290 291 292 293 294
bool DisplaySettings::flipoverGestureEnabled() const
{
    return m_flipoverGestureEnabled;
}

void DisplaySettings::setFlipoverGestureEnabled(bool enabled)
{
    if (m_flipoverGestureEnabled != enabled) {
        m_flipoverGestureEnabled = enabled;
        m_mceIface->set_config(QDBusObjectPath(MceFlipOverGestureEnabled), QDBusVariant(enabled));
        emit flipoverGestureEnabledChanged();
    }
}
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
bool DisplaySettings::powerSaveModeForced() const
{
    return m_powerSaveModeForced;
}

void DisplaySettings::setPowerSaveModeForced(bool force)
{
    if (m_powerSaveModeForced != force) {
        m_powerSaveModeForced = force;
        m_mceIface->set_config(QDBusObjectPath(McePowerSaveModeForced), QDBusVariant(force));
        emit powerSaveModeForcedChanged();
    }
}

bool DisplaySettings::powerSaveModeEnabled() const
{
    return m_powerSaveModeEnabled;
}

void DisplaySettings::setPowerSaveModeEnabled(bool enabled)
{
    if (m_powerSaveModeEnabled != enabled) {
        m_powerSaveModeEnabled = enabled;
        m_mceIface->set_config(QDBusObjectPath(McePowerSaveModeEnabled), QDBusVariant(enabled));
        emit powerSaveModeEnabledChanged();
    }
}

int DisplaySettings::powerSaveModeThreshold() const
{
    return m_powerSaveModeThreshold;
}

void DisplaySettings::setPowerSaveModeThreshold(int value)
{
    if (m_powerSaveModeThreshold != value) {
        m_powerSaveModeThreshold = value;
        m_mceIface->set_config(QDBusObjectPath(McePowerSaveModeThreshold), QDBusVariant(value));
        emit powerSaveModeThresholdChanged();
    }
}

338 339 340 341 342
bool DisplaySettings::populated() const
{
    return m_populated;
}

343
void DisplaySettings::configChange(const QString &key, const QDBusVariant &value)
344 345 346 347 348
{
    updateConfig(key, value.variant());
}

void DisplaySettings::updateConfig(const QString &key, const QVariant &value)
349 350
{
    if (key == MceDisplayBrightness) {
351
        int val = value.toInt();
352 353 354 355 356
        if (val != m_brightness) {
            m_brightness = val;
            emit brightnessChanged();
        }
    } else if (key == MceDisplayDimTimeout) {
357
        int val = value.toInt();
358 359 360 361 362
        if (val != m_dimTimeout) {
            m_dimTimeout = val;
            emit dimTimeoutChanged();
        }
    } else if (key == MceDisplayBlankTimeout) {
363
        int val = value.toInt();
364 365 366 367
        if (val != m_blankTimeout) {
            m_blankTimeout = val;
            emit blankTimeoutChanged();
        }
368
    } else if (key == MceDisplayInhibitMode) {
369
        InhibitMode val = static_cast<InhibitMode>(value.toInt());
370 371 372 373
        if (val != m_inhibitMode) {
            m_inhibitMode = val;
            emit inhibitModeChanged();
        }
374
    } else if (key == MceDisplayUseAdaptiveDimming) {
375
        bool val = value.toBool();
376 377 378 379
        if (val != m_adaptiveDimmingEnabled) {
            m_adaptiveDimmingEnabled = val;
            emit adaptiveDimmingEnabledChanged();
        }
380
    } else if (key == MceDisplayUseLowPowerMode) {
381
        bool val = value.toBool();
382 383 384 385
        if (val != m_lowPowerModeEnabled) {
            m_lowPowerModeEnabled = val;
            emit lowPowerModeEnabledChanged();
        }
386
    } else if (key == MceDisplayUseAmbientLightSensor) {
387
        bool val = value.toBool();
388 389 390 391
        if (val != m_ambientLightSensorEnabled) {
            m_ambientLightSensorEnabled = val;
            emit ambientLightSensorEnabledChanged();
        }
392
    } else if (key == MceDisplayAutoBrightnessEnabled) {
393
        bool val = value.toBool();
394 395 396 397
        if (val != m_autoBrightnessEnabled) {
            m_autoBrightnessEnabled = val;
            emit autoBrightnessEnabledChanged();
        }
398
    } else if (key == MceDoubleTapMode) {
399
        int val = value.toInt();
400 401 402 403
        if (val != m_doubleTapMode) {
            m_doubleTapMode = val;
            emit doubleTapModeChanged();
        }
404
    } else if (key == MceLidSensorEnabled) {
405
        bool val = value.toBool();
406 407 408 409
        if (val != m_lidSensorEnabled) {
            m_lidSensorEnabled = val;
            emit lidSensorEnabledChanged();
        }
410
    } else if (key == MceLidSensorFilteringEnabled) {
411
        bool val = value.toBool();
412 413 414 415
        if (val != m_lidSensorFilteringEnabled) {
            m_lidSensorFilteringEnabled = val;
            emit lidSensorFilteringEnabledChanged();
        }
416
    } else if (key == MceFlipOverGestureEnabled) {
417
        bool val = value.toBool();
418 419 420 421
        if (val != m_flipoverGestureEnabled) {
            m_flipoverGestureEnabled = val;
            emit flipoverGestureEnabledChanged();
        }
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    } else if (key == McePowerSaveModeForced) {
        bool val = value.toBool();
        if (val != m_powerSaveModeForced) {
            m_powerSaveModeForced = val;
            emit powerSaveModeForcedChanged();
        }
    } else if (key == McePowerSaveModeEnabled) {
        bool val = value.toBool();
        if (val != m_powerSaveModeEnabled) {
            m_powerSaveModeEnabled = val;
            emit powerSaveModeEnabledChanged();
        }
    } else if (key == McePowerSaveModeThreshold) {
        int val = value.toInt();
        if (val != m_powerSaveModeThreshold) {
            m_powerSaveModeThreshold = val;
            emit powerSaveModeThresholdChanged();
        }
440 441
    }
}