Changeset View
Changeset View
Standalone View
Standalone View
libcolorcorrect/compositorcoloradaptor.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | Copyright 2017 Roman Gilg <subdiff@gmail.com> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or modify | ||||
5 | it under the terms of the GNU General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or | ||||
7 | (at your option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | GNU General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License | ||||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
16 | *********************************************************************/ | ||||
17 | #include "compositorcoloradaptor.h" | ||||
18 | | ||||
19 | #include <KWindowSystem> | ||||
20 | #include <KLocalizedString> | ||||
21 | | ||||
22 | #include <QDBusReply> | ||||
23 | #include <QDBusInterface> | ||||
24 | | ||||
25 | namespace ColorCorrect | ||||
26 | { | ||||
27 | | ||||
28 | CompositorAdaptor::CompositorAdaptor(QObject *parent) | ||||
29 | : QObject(parent) | ||||
30 | { | ||||
31 | if(KWindowSystem::isPlatformX11()) { | ||||
32 | setError(ErrorCode::ErrorCodeXSession); | ||||
33 | } | ||||
34 | | ||||
35 | m_iface = new QDBusInterface (QStringLiteral("org.kde.KWin"), | ||||
36 | QStringLiteral("/ColorCorrect"), | ||||
37 | QStringLiteral("org.kde.kwin.ColorCorrect"), | ||||
38 | QDBusConnection::sessionBus(), | ||||
39 | this); | ||||
40 | | ||||
41 | if (!m_iface->connection().connect(QString(), QStringLiteral("/ColorCorrect"), | ||||
42 | QStringLiteral("org.kde.kwin.ColorCorrect"), QStringLiteral("nightColorConfigChanged"), | ||||
43 | this, SLOT(compDataUpdated(QHash<QString, QVariant>)))) { | ||||
44 | setError(ErrorCode::ErrorCodeConnectionFailed); | ||||
45 | return; | ||||
46 | } | ||||
47 | reloadData(); | ||||
48 | } | ||||
49 | | ||||
50 | void CompositorAdaptor::setError(ErrorCode error) { | ||||
51 | if (m_error == error || m_error == ErrorCode::ErrorCodeXSession) { | ||||
52 | return; | ||||
53 | } | ||||
54 | m_error = error; | ||||
55 | switch(error) { | ||||
56 | case ErrorCode::ErrorCodeConnectionFailed: | ||||
57 | m_errorText = i18nc("Critical error message", "Failed to connect to the Window Manager"); | ||||
58 | break; | ||||
59 | case ErrorCode::ErrorCodeBackendNoSupport: | ||||
60 | m_errorText = i18nc("Critical error message", "Rendering backend doesn't support Color Correction."); | ||||
61 | break; | ||||
62 | case ErrorCode::ErrorCodeXSession: | ||||
63 | m_errorText = i18nc("Critical error message", "You're currently using the X Windowing System. This functionality is Wayland exclusive."); | ||||
64 | break; | ||||
65 | default: | ||||
66 | m_errorText = ""; | ||||
67 | } | ||||
68 | emit errorChanged(); | ||||
69 | emit errorTextChanged(); | ||||
70 | } | ||||
71 | | ||||
72 | void CompositorAdaptor::compDataUpdated(QHash<QString, QVariant> data) | ||||
73 | { | ||||
74 | resetData(data); | ||||
75 | emit dataUpdated(); | ||||
76 | } | ||||
77 | | ||||
78 | void CompositorAdaptor::reloadData() | ||||
79 | { | ||||
80 | QHash<QString, QVariant> info = getData(); | ||||
81 | if (info.isEmpty()) { | ||||
82 | return; | ||||
83 | } | ||||
84 | resetDataAndStaged(info); | ||||
85 | } | ||||
86 | | ||||
87 | #define SETTER(out, in, emitsignal) \ | ||||
88 | if (out != in) { \ | ||||
89 | out = in; \ | ||||
90 | emit emitsignal; \ | ||||
91 | } | ||||
92 | | ||||
93 | void CompositorAdaptor::resetDataAndStaged(QHash<QString, QVariant> data) | ||||
94 | { | ||||
95 | if (!resetData(data)) { | ||||
96 | return; | ||||
97 | } | ||||
98 | | ||||
99 | SETTER(m_activeStaged, m_active, activeStagedChanged()) | ||||
100 | SETTER(m_modeStaged, m_mode, modeStagedChanged()) | ||||
101 | | ||||
102 | SETTER(m_nightTemperatureStaged, m_nightTemperature, nightTemperatureStagedChanged()) | ||||
103 | | ||||
104 | SETTER(m_latitudeFixedStaged, m_latitudeFixed, latitudeFixedStagedChanged()) | ||||
105 | SETTER(m_longitudeFixedStaged, m_longitudeFixed, longitudeFixedStagedChanged()) | ||||
106 | | ||||
107 | SETTER(m_morningBeginFixedStaged, m_morningBeginFixed, morningBeginFixedStagedChanged()) | ||||
108 | SETTER(m_eveningBeginFixedStaged, m_eveningBeginFixed, eveningBeginFixedStagedChanged()) | ||||
109 | SETTER(m_transitionTimeStaged, m_transitionTime, transitionTimeStagedChanged()) | ||||
110 | | ||||
111 | emit stagedDataReset(); | ||||
112 | } | ||||
113 | | ||||
114 | bool CompositorAdaptor::resetData(QHash<QString, QVariant> data) | ||||
115 | { | ||||
116 | m_nightColorAvailable = data["Available"].toBool(); | ||||
117 | | ||||
118 | if (!m_nightColorAvailable) { | ||||
119 | setError(ErrorCode::ErrorCodeBackendNoSupport); | ||||
120 | return false; | ||||
121 | } | ||||
122 | | ||||
123 | SETTER(m_activeEnabled, data["ActiveEnabled"].toBool(), activeEnabledChanged()) | ||||
124 | SETTER(m_active, data["Active"].toBool(), activeChanged()) | ||||
125 | SETTER(m_mode, (Mode)data["Mode"].toInt(), modeChanged()) | ||||
126 | | ||||
127 | SETTER(m_nightTemperatureEnabled, data["NightTemperatureEnabled"].toBool(), nightTemperatureEnabledChanged()) | ||||
128 | SETTER(m_nightTemperature, data["NightTemperature"].toInt(), nightTemperatureChanged()) | ||||
129 | SETTER(m_curColorT, data["CurrentColorTemperature"].toInt(), curColorTChanged()) | ||||
130 | | ||||
131 | SETTER(m_locationEnabled, data["LocationEnabled"].toBool(), locationEnabledChanged()) | ||||
132 | SETTER(m_latitudeAuto, data["LatitudeAuto"].toDouble(), latitudeAutoChanged()) | ||||
133 | SETTER(m_longitudeAuto, data["LongitudeAuto"].toDouble(), longitudeAutoChanged()) | ||||
134 | SETTER(m_latitudeFixed, data["LatitudeFixed"].toDouble(), latitudeFixedChanged()) | ||||
135 | SETTER(m_longitudeFixed, data["LongitudeFixed"].toDouble(), longitudeFixedChanged()) | ||||
136 | | ||||
137 | SETTER(m_timingsEnabled, data["TimingsEnabled"].toBool(), timingsEnabledChanged()) | ||||
138 | SETTER(m_morningBeginFixed, QTime::fromString(data["MorningBeginFixed"].toString()), morningBeginFixedChanged()) | ||||
139 | SETTER(m_eveningBeginFixed, QTime::fromString(data["EveningBeginFixed"].toString()), eveningBeginFixedChanged()) | ||||
140 | SETTER(m_transitionTime, data["TransitionTime"].toInt(), transitionTimeChanged()) | ||||
141 | | ||||
142 | return true; | ||||
143 | } | ||||
144 | | ||||
145 | #undef SETTER | ||||
146 | | ||||
147 | QHash<QString, QVariant> CompositorAdaptor::getData() | ||||
148 | { | ||||
149 | QDBusReply<QHash<QString, QVariant> > reply = m_iface->call("nightColorInfo"); | ||||
150 | if (reply.isValid()) { | ||||
151 | return reply.value(); | ||||
152 | } else { | ||||
153 | setError(ErrorCode::ErrorCodeConnectionFailed); | ||||
154 | return QHash<QString, QVariant>(); | ||||
155 | } | ||||
156 | } | ||||
157 | | ||||
158 | bool CompositorAdaptor::sendConfiguration() | ||||
159 | { | ||||
160 | if (!m_iface) { | ||||
161 | return false; | ||||
162 | } | ||||
163 | QHash<QString, QVariant> data; | ||||
164 | data["Active"] = m_activeStaged; | ||||
165 | | ||||
166 | if (m_activeStaged) { | ||||
167 | data["Mode"] = (int)m_modeStaged; | ||||
168 | | ||||
169 | data["NightTemperature"] = m_nightTemperatureStaged; | ||||
170 | | ||||
171 | if (m_modeStaged == Mode::ModeLocation) { | ||||
172 | data["LatitudeFixed"] = m_latitudeFixedStaged; | ||||
173 | data["LongitudeFixed"] = m_longitudeFixedStaged; | ||||
174 | } | ||||
175 | | ||||
176 | if (m_modeStaged == Mode::ModeTimings) { | ||||
177 | data["MorningBeginFixed"] = m_morningBeginFixedStaged.toString(Qt::ISODate); | ||||
178 | data["EveningBeginFixed"] = m_eveningBeginFixedStaged.toString(Qt::ISODate); | ||||
179 | data["TransitionTime"] = m_transitionTimeStaged; | ||||
180 | } | ||||
181 | } | ||||
182 | | ||||
183 | QDBusReply<bool> reply = m_iface->call("setNightColorConfig", data); | ||||
184 | if (reply.isValid() && reply) { | ||||
185 | m_active = m_activeStaged; | ||||
186 | if (m_activeStaged) { | ||||
187 | m_mode = m_modeStaged; | ||||
188 | m_nightTemperature = m_nightTemperatureStaged; | ||||
189 | | ||||
190 | if (m_mode == Mode::ModeLocation) { | ||||
191 | m_latitudeFixed = m_latitudeFixedStaged; | ||||
192 | m_longitudeFixed = m_longitudeFixedStaged; | ||||
193 | } | ||||
194 | | ||||
195 | if (m_mode == Mode::ModeTimings) { | ||||
196 | m_morningBeginFixed = m_morningBeginFixedStaged; | ||||
197 | m_eveningBeginFixed = m_eveningBeginFixedStaged; | ||||
198 | m_transitionTime = m_transitionTimeStaged; | ||||
199 | } | ||||
200 | } | ||||
201 | return true; | ||||
202 | } | ||||
203 | return false; | ||||
204 | } | ||||
205 | | ||||
206 | bool CompositorAdaptor::sendConfigurationAll() | ||||
207 | { | ||||
208 | if (!m_iface) { | ||||
209 | return false; | ||||
210 | } | ||||
211 | QHash<QString, QVariant> data; | ||||
212 | data["Active"] = m_activeStaged; | ||||
213 | data["Mode"] = (int)m_modeStaged; | ||||
214 | data["NightTemperature"] = m_nightTemperatureStaged; | ||||
215 | data["LatitudeFixed"] = m_latitudeFixedStaged; | ||||
216 | data["LongitudeFixed"] = m_longitudeFixedStaged; | ||||
217 | data["MorningBeginFixed"] = m_morningBeginFixedStaged.toString(Qt::ISODate); | ||||
218 | data["EveningBeginFixed"] = m_eveningBeginFixedStaged.toString(Qt::ISODate); | ||||
219 | data["TransitionTime"] = m_transitionTimeStaged; | ||||
220 | | ||||
221 | QDBusReply<bool> reply = m_iface->call("setNightColorConfig", data); | ||||
222 | if (reply.isValid() && reply) { | ||||
223 | m_active = m_activeStaged; | ||||
224 | m_mode = m_modeStaged; | ||||
225 | m_nightTemperature = m_nightTemperatureStaged; | ||||
226 | | ||||
227 | m_latitudeFixed = m_latitudeFixedStaged; | ||||
228 | m_longitudeFixed = m_longitudeFixedStaged; | ||||
229 | | ||||
230 | m_morningBeginFixed = m_morningBeginFixedStaged; | ||||
231 | m_eveningBeginFixed = m_eveningBeginFixedStaged; | ||||
232 | m_transitionTime = m_transitionTimeStaged; | ||||
233 | return true; | ||||
234 | } | ||||
235 | return false; | ||||
236 | } | ||||
237 | | ||||
238 | void CompositorAdaptor::sendAutoLocationUpdate(double latitude, double longitude) | ||||
239 | { | ||||
240 | if (m_mode == Mode::ModeAutomatic) { | ||||
241 | m_iface->call("nightColorAutoLocationUpdate", latitude, longitude); | ||||
242 | } | ||||
243 | } | ||||
244 | | ||||
245 | bool CompositorAdaptor::checkStaged() | ||||
246 | { | ||||
247 | bool actChange = m_active != m_activeStaged; | ||||
248 | if (!m_activeStaged) { | ||||
249 | return actChange; | ||||
250 | } | ||||
251 | | ||||
252 | bool baseDataChange = actChange || | ||||
253 | m_mode != m_modeStaged || | ||||
254 | m_nightTemperature != m_nightTemperatureStaged; | ||||
255 | switch(m_modeStaged) { | ||||
256 | case Mode::ModeAutomatic: | ||||
257 | return baseDataChange; | ||||
258 | case Mode::ModeLocation: | ||||
259 | return baseDataChange || | ||||
260 | m_latitudeFixed != m_latitudeFixedStaged || | ||||
261 | m_longitudeFixed != m_longitudeFixedStaged; | ||||
262 | case Mode::ModeTimings: | ||||
263 | return baseDataChange || | ||||
264 | m_morningBeginFixed != m_morningBeginFixedStaged || | ||||
265 | m_eveningBeginFixed != m_eveningBeginFixedStaged || | ||||
266 | m_transitionTime != m_transitionTimeStaged; | ||||
267 | default: | ||||
268 | // never reached | ||||
269 | return false; | ||||
270 | } | ||||
271 | } | ||||
272 | | ||||
273 | bool CompositorAdaptor::checkStagedAll() | ||||
274 | { | ||||
275 | return m_active != m_activeStaged || | ||||
276 | m_mode != m_modeStaged || | ||||
277 | m_nightTemperature != m_nightTemperatureStaged || | ||||
278 | m_latitudeFixed != m_latitudeFixedStaged || | ||||
279 | m_longitudeFixed != m_longitudeFixedStaged || | ||||
280 | m_morningBeginFixed != m_morningBeginFixedStaged || | ||||
281 | m_eveningBeginFixed != m_eveningBeginFixedStaged || | ||||
282 | m_transitionTime != m_transitionTimeStaged; | ||||
283 | } | ||||
284 | | ||||
285 | } |