Changeset View
Changeset View
Standalone View
Standalone View
colorcorrection/manager.cpp
Show First 20 Lines • Show All 60 Lines • ▼ Show 20 Line(s) | |||||
61 | 61 | | |||
62 | void Manager::init() | 62 | void Manager::init() | ||
63 | { | 63 | { | ||
64 | Settings::instance(kwinApp()->config()); | 64 | Settings::instance(kwinApp()->config()); | ||
65 | // we may always read in the current config | 65 | // we may always read in the current config | ||
66 | readConfig(); | 66 | readConfig(); | ||
67 | 67 | | |||
68 | if (!kwinApp()->platform()->supportsGammaControl()) { | 68 | if (!kwinApp()->platform()->supportsGammaControl()) { | ||
69 | // at least update the sun timings to make the values accessible via dbus | | |||
70 | updateSunTimings(true); | | |||
71 | return; | 69 | return; | ||
72 | } | 70 | } | ||
73 | 71 | | |||
74 | connect(Screens::self(), &Screens::countChanged, this, &Manager::hardReset); | 72 | connect(Screens::self(), &Screens::countChanged, this, &Manager::hardReset); | ||
75 | 73 | | |||
76 | connect(LogindIntegration::self(), &LogindIntegration::sessionActiveChanged, this, | 74 | connect(LogindIntegration::self(), &LogindIntegration::sessionActiveChanged, this, | ||
77 | [this](bool active) { | 75 | [this](bool active) { | ||
78 | if (active) { | 76 | if (active) { | ||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Line(s) | |||||
132 | #endif | 130 | #endif | ||
133 | 131 | | |||
134 | hardReset(); | 132 | hardReset(); | ||
135 | } | 133 | } | ||
136 | 134 | | |||
137 | void Manager::hardReset() | 135 | void Manager::hardReset() | ||
138 | { | 136 | { | ||
139 | cancelAllTimers(); | 137 | cancelAllTimers(); | ||
138 | | ||||
139 | // Timings of the Sun are not used in the constant mode. | ||||
140 | if (m_mode != NightColorMode::Constant) { | ||||
140 | updateSunTimings(true); | 141 | updateSunTimings(true); | ||
142 | } | ||||
143 | | ||||
141 | if (kwinApp()->platform()->supportsGammaControl() && m_active) { | 144 | if (kwinApp()->platform()->supportsGammaControl() && m_active) { | ||
142 | m_running = true; | 145 | m_running = true; | ||
143 | commitGammaRamps(currentTargetTemp()); | 146 | commitGammaRamps(currentTargetTemp()); | ||
144 | } | 147 | } | ||
145 | resetAllTimers(); | 148 | resetAllTimers(); | ||
146 | } | 149 | } | ||
147 | 150 | | |||
148 | void Manager::reparseConfigAndReset() | 151 | void Manager::reparseConfigAndReset() | ||
149 | { | 152 | { | ||
150 | cancelAllTimers(); | 153 | cancelAllTimers(); | ||
151 | readConfig(); | 154 | readConfig(); | ||
152 | hardReset(); | 155 | hardReset(); | ||
153 | } | 156 | } | ||
154 | 157 | | |||
155 | void Manager::readConfig() | 158 | void Manager::readConfig() | ||
156 | { | 159 | { | ||
157 | Settings *s = Settings::self(); | 160 | Settings *s = Settings::self(); | ||
158 | s->load(); | 161 | s->load(); | ||
159 | 162 | | |||
160 | m_active = s->active(); | 163 | m_active = s->active(); | ||
161 | 164 | | |||
162 | NightColorMode mode = s->mode(); | 165 | NightColorMode mode = s->mode(); | ||
163 | if (mode == NightColorMode::Location || mode == NightColorMode::Timings) { | 166 | switch (s->mode()) { | ||
167 | case NightColorMode::Automatic: | ||||
168 | case NightColorMode::Location: | ||||
169 | case NightColorMode::Timings: | ||||
170 | case NightColorMode::Constant: | ||||
164 | m_mode = mode; | 171 | m_mode = mode; | ||
165 | } else { | 172 | break; | ||
166 | // also fallback for invalid setting values | 173 | default: | ||
174 | // Fallback for invalid setting values. | ||||
167 | m_mode = NightColorMode::Automatic; | 175 | m_mode = NightColorMode::Automatic; | ||
176 | break; | ||||
168 | } | 177 | } | ||
169 | 178 | | |||
170 | m_nightTargetTemp = qBound(MIN_TEMPERATURE, s->nightTemperature(), NEUTRAL_TEMPERATURE); | 179 | m_nightTargetTemp = qBound(MIN_TEMPERATURE, s->nightTemperature(), NEUTRAL_TEMPERATURE); | ||
171 | 180 | | |||
172 | double lat, lng; | 181 | double lat, lng; | ||
173 | auto correctReadin = [&lat, &lng]() { | 182 | auto correctReadin = [&lat, &lng]() { | ||
174 | if (!checkLocation(lat, lng)) { | 183 | if (!checkLocation(lat, lng)) { | ||
175 | // out of domain | 184 | // out of domain | ||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Line(s) | 242 | { | |||
237 | 246 | | |||
238 | m_slowUpdateStartTimer = nullptr; | 247 | m_slowUpdateStartTimer = nullptr; | ||
239 | m_slowUpdateTimer = nullptr; | 248 | m_slowUpdateTimer = nullptr; | ||
240 | m_quickAdjustTimer = nullptr; | 249 | m_quickAdjustTimer = nullptr; | ||
241 | } | 250 | } | ||
242 | 251 | | |||
243 | void Manager::resetQuickAdjustTimer() | 252 | void Manager::resetQuickAdjustTimer() | ||
244 | { | 253 | { | ||
254 | // We don't use timings of the Sun in the constant mode. | ||||
255 | if (m_mode != NightColorMode::Constant) { | ||||
245 | updateSunTimings(false); | 256 | updateSunTimings(false); | ||
257 | } | ||||
246 | 258 | | |||
247 | int tempDiff = qAbs(currentTargetTemp() - m_currentTemp); | 259 | int tempDiff = qAbs(currentTargetTemp() - m_currentTemp); | ||
248 | // allow tolerance of one TEMPERATURE_STEP to compensate if a slow update is coincidental | 260 | // allow tolerance of one TEMPERATURE_STEP to compensate if a slow update is coincidental | ||
249 | if (tempDiff > TEMPERATURE_STEP) { | 261 | if (tempDiff > TEMPERATURE_STEP) { | ||
250 | cancelAllTimers(); | 262 | cancelAllTimers(); | ||
251 | m_quickAdjustTimer = new QTimer(this); | 263 | m_quickAdjustTimer = new QTimer(this); | ||
252 | m_quickAdjustTimer->setSingleShot(false); | 264 | m_quickAdjustTimer->setSingleShot(false); | ||
253 | connect(m_quickAdjustTimer, &QTimer::timeout, this, &Manager::quickAdjust); | 265 | connect(m_quickAdjustTimer, &QTimer::timeout, this, &Manager::quickAdjust); | ||
Show All 37 Lines | 302 | { | |||
291 | delete m_slowUpdateStartTimer; | 303 | delete m_slowUpdateStartTimer; | ||
292 | m_slowUpdateStartTimer = nullptr; | 304 | m_slowUpdateStartTimer = nullptr; | ||
293 | 305 | | |||
294 | if (!m_running || m_quickAdjustTimer) { | 306 | if (!m_running || m_quickAdjustTimer) { | ||
295 | // only reenable the slow update start timer when quick adjust is not active anymore | 307 | // only reenable the slow update start timer when quick adjust is not active anymore | ||
296 | return; | 308 | return; | ||
297 | } | 309 | } | ||
298 | 310 | | |||
311 | // There is no need for starting the slow update timer. Screen color temperature | ||||
312 | // will be constant all the time now. | ||||
313 | if (m_mode == NightColorMode::Constant) { | ||||
314 | return; | ||||
315 | } | ||||
316 | | ||||
299 | // set up the next slow update | 317 | // set up the next slow update | ||
300 | m_slowUpdateStartTimer = new QTimer(this); | 318 | m_slowUpdateStartTimer = new QTimer(this); | ||
301 | m_slowUpdateStartTimer->setSingleShot(true); | 319 | m_slowUpdateStartTimer->setSingleShot(true); | ||
302 | connect(m_slowUpdateStartTimer, &QTimer::timeout, this, &Manager::resetSlowUpdateStartTimer); | 320 | connect(m_slowUpdateStartTimer, &QTimer::timeout, this, &Manager::resetSlowUpdateStartTimer); | ||
303 | 321 | | |||
304 | updateSunTimings(false); | 322 | updateSunTimings(false); | ||
305 | int diff; | 323 | int diff; | ||
306 | if (m_mode == NightColorMode::Timings) { | 324 | if (m_mode == NightColorMode::Timings) { | ||
▲ Show 20 Lines • Show All 173 Lines • ▼ Show 20 Line(s) | |||||
480 | } | 498 | } | ||
481 | 499 | | |||
482 | int Manager::currentTargetTemp() const | 500 | int Manager::currentTargetTemp() const | ||
483 | { | 501 | { | ||
484 | if (!m_active) { | 502 | if (!m_active) { | ||
485 | return NEUTRAL_TEMPERATURE; | 503 | return NEUTRAL_TEMPERATURE; | ||
486 | } | 504 | } | ||
487 | 505 | | |||
506 | if (m_mode == NightColorMode::Constant) { | ||||
507 | return m_nightTargetTemp; | ||||
508 | } | ||||
509 | | ||||
488 | QDateTime todayNow = QDateTime::currentDateTimeUtc(); | 510 | QDateTime todayNow = QDateTime::currentDateTimeUtc(); | ||
489 | 511 | | |||
490 | auto f = [this, todayNow](int target1, int target2) { | 512 | auto f = [this, todayNow](int target1, int target2) { | ||
491 | if (todayNow <= m_prev.second) { | 513 | if (todayNow <= m_prev.second) { | ||
492 | double residueQuota = todayNow.msecsTo(m_prev.second) / (double)m_prev.first.msecsTo(m_prev.second); | 514 | double residueQuota = todayNow.msecsTo(m_prev.second) / (double)m_prev.first.msecsTo(m_prev.second); | ||
493 | 515 | | |||
494 | double ret = (int)((1. - residueQuota) * (double)target2 + residueQuota * (double)target1); | 516 | double ret = (int)((1. - residueQuota) * (double)target2 + residueQuota * (double)target1); | ||
495 | // remove single digits | 517 | // remove single digits | ||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Line(s) | 621 | { | |||
624 | } | 646 | } | ||
625 | 647 | | |||
626 | iter1 = data.constFind("Mode"); | 648 | iter1 = data.constFind("Mode"); | ||
627 | if (iter1 != data.constEnd()) { | 649 | if (iter1 != data.constEnd()) { | ||
628 | if (!iter1.value().canConvert<int>()) { | 650 | if (!iter1.value().canConvert<int>()) { | ||
629 | return false; | 651 | return false; | ||
630 | } | 652 | } | ||
631 | int mo = iter1.value().toInt(); | 653 | int mo = iter1.value().toInt(); | ||
632 | if (mo < 0 || 2 < mo) { | 654 | if (mo < 0 || 3 < mo) { | ||
633 | return false; | 655 | return false; | ||
634 | } | 656 | } | ||
635 | NightColorMode moM; | 657 | NightColorMode moM; | ||
636 | switch (mo) { | 658 | switch (mo) { | ||
637 | case 0: | 659 | case 0: | ||
638 | moM = NightColorMode::Automatic; | 660 | moM = NightColorMode::Automatic; | ||
639 | break; | 661 | break; | ||
640 | case 1: | 662 | case 1: | ||
641 | moM = NightColorMode::Location; | 663 | moM = NightColorMode::Location; | ||
642 | break; | 664 | break; | ||
643 | case 2: | 665 | case 2: | ||
644 | moM = NightColorMode::Timings; | 666 | moM = NightColorMode::Timings; | ||
667 | break; | ||||
668 | case 3: | ||||
669 | moM = NightColorMode::Constant; | ||||
670 | break; | ||||
645 | } | 671 | } | ||
646 | modeUpdate = m_mode != moM; | 672 | modeUpdate = m_mode != moM; | ||
647 | mode = moM; | 673 | mode = moM; | ||
648 | } | 674 | } | ||
649 | 675 | | |||
650 | iter1 = data.constFind("NightTemperature"); | 676 | iter1 = data.constFind("NightTemperature"); | ||
651 | if (iter1 != data.constEnd()) { | 677 | if (iter1 != data.constEnd()) { | ||
652 | if (!iter1.value().canConvert<int>()) { | 678 | if (!iter1.value().canConvert<int>()) { | ||
▲ Show 20 Lines • Show All 129 Lines • Show Last 20 Lines |