1 /***************************************************************************
2 * Copyright (C) 2010 by Dario Freddi <drf@kde.org> *
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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
19
20 #include "powerdevilbackendinterface.h"
21 #include "powerdevilscreenbrightnesslogic.h"
22 #include "powerdevilkeyboardbrightnesslogic.h"
23 #include "powerdevil_debug.h"
24 #include <QDebug>
25
26 namespace PowerDevil
27 {
28
29 class BackendInterface::Private
30 {
31 public:
Private()32 Private() : acAdapterState(UnknownAcAdapterState), batteryState(NoBatteryState), batteryRemainingTime(0),
33 isReady(false), isError(false), isLidClosed(false), isLidPresent(false) {}
~Private()34 ~Private() {}
35
36 AcAdapterState acAdapterState;
37 BatteryState batteryState;
38 qulonglong batteryRemainingTime;
39 QHash< BrightnessControlType, BrightnessLogic* > brightnessLogic;
40 BrightnessControlsList brightnessControlsAvailable;
41 Capabilities capabilities;
42 SuspendMethods suspendMethods;
43 QString errorString;
44 bool isReady;
45 bool isError;
46 bool isLidClosed;
47 bool isLidPresent;
48 QHash< QString, uint > capacities;
49 };
50
BackendInterface(QObject * parent)51 BackendInterface::BackendInterface(QObject* parent)
52 : QObject(parent)
53 , d(new Private)
54 {
55 d->brightnessLogic[Screen] = new ScreenBrightnessLogic();
56 d->brightnessLogic[Keyboard] = new KeyboardBrightnessLogic();
57 }
58
~BackendInterface()59 BackendInterface::~BackendInterface()
60 {
61 delete d->brightnessLogic[Keyboard];
62 delete d->brightnessLogic[Screen];
63 delete d;
64 }
65
acAdapterState() const66 BackendInterface::AcAdapterState BackendInterface::acAdapterState() const
67 {
68 return d->acAdapterState;
69 }
70
batteryRemainingTime() const71 qulonglong BackendInterface::batteryRemainingTime() const
72 {
73 return d->batteryRemainingTime;
74 }
75
batteryState() const76 BackendInterface::BatteryState BackendInterface::batteryState() const
77 {
78 return d->batteryState;
79 }
80
setBrightness(int brightness,BackendInterface::BrightnessControlType type)81 void BackendInterface::setBrightness(int brightness, BackendInterface::BrightnessControlType type)
82 {
83 if (type == Screen) {
84 qCDebug(POWERDEVIL) << "set screen brightness: " << brightness;
85 } else {
86 qCDebug(POWERDEVIL) << "set kbd backlight: " << brightness;
87 }
88
89 d->brightnessLogic.value(type)->setValue(brightness);
90 }
91
brightness(BackendInterface::BrightnessControlType type) const92 int BackendInterface::brightness(BackendInterface::BrightnessControlType type) const
93 {
94 return d->brightnessLogic.value(type)->value();
95 }
96
brightnessMax(BackendInterface::BrightnessControlType type) const97 int BackendInterface::brightnessMax(BackendInterface::BrightnessControlType type) const
98 {
99 return d->brightnessLogic.value(type)->valueMax();
100 }
101
brightnessSteps(BackendInterface::BrightnessControlType type) const102 int BackendInterface::brightnessSteps(BackendInterface::BrightnessControlType type) const
103 {
104 BrightnessLogic *logic = d->brightnessLogic.value(type);
105 logic->setValueMax(brightnessMax(type));
106 return logic->steps();
107 }
108
brightnessControlsAvailable() const109 BackendInterface::BrightnessControlsList BackendInterface::brightnessControlsAvailable() const
110 {
111 return d->brightnessControlsAvailable;
112 }
113
capacities() const114 QHash< QString, uint > BackendInterface::capacities() const
115 {
116 return d->capacities;
117 }
118
supportedSuspendMethods() const119 BackendInterface::SuspendMethods BackendInterface::supportedSuspendMethods() const
120 {
121 return d->suspendMethods;
122 }
123
isLidClosed() const124 bool BackendInterface::isLidClosed() const
125 {
126 return d->isLidClosed;
127 }
128
isLidPresent() const129 bool BackendInterface::isLidPresent() const
130 {
131 return d->isLidPresent;
132 }
133
setLidPresent(bool present)134 void BackendInterface::setLidPresent(bool present)
135 {
136 d->isLidPresent = present;
137 }
138
setAcAdapterState(PowerDevil::BackendInterface::AcAdapterState state)139 void BackendInterface::setAcAdapterState(PowerDevil::BackendInterface::AcAdapterState state)
140 {
141 d->acAdapterState = state;
142 Q_EMIT acAdapterStateChanged(state);
143 }
144
setBackendHasError(const QString & errorDetails)145 void BackendInterface::setBackendHasError(const QString& errorDetails)
146 {
147 Q_UNUSED(errorDetails)
148 }
149
setBackendIsReady(BrightnessControlsList availableBrightnessControls,BackendInterface::SuspendMethods supportedSuspendMethods)150 void BackendInterface::setBackendIsReady(BrightnessControlsList availableBrightnessControls,
151 BackendInterface::SuspendMethods supportedSuspendMethods)
152 {
153 d->brightnessControlsAvailable = availableBrightnessControls;
154 d->suspendMethods = supportedSuspendMethods;
155 d->isReady = true;
156
157 Q_EMIT backendReady();
158 }
159
setBatteryRemainingTime(qulonglong time)160 void BackendInterface::setBatteryRemainingTime(qulonglong time)
161 {
162 if (d->batteryRemainingTime != time) {
163 d->batteryRemainingTime = time;
164 Q_EMIT batteryRemainingTimeChanged(time);
165 }
166 }
167
setBatteryState(PowerDevil::BackendInterface::BatteryState state)168 void BackendInterface::setBatteryState(PowerDevil::BackendInterface::BatteryState state)
169 {
170 d->batteryState = state;
171 Q_EMIT batteryStateChanged(state);
172 }
173
setButtonPressed(PowerDevil::BackendInterface::ButtonType type)174 void BackendInterface::setButtonPressed(PowerDevil::BackendInterface::ButtonType type)
175 {
176 if (type == LidClose && !d->isLidClosed) {
177 d->isLidClosed = true;
178 Q_EMIT lidClosedChanged(true);
179 } else if (type == LidOpen && d->isLidClosed) {
180 d->isLidClosed = false;
181 Q_EMIT lidClosedChanged(false);
182 }
183 Q_EMIT buttonPressed(type);
184 }
185
setCapacityForBattery(const QString & batteryId,uint percent)186 void BackendInterface::setCapacityForBattery(const QString& batteryId, uint percent)
187 {
188 d->capacities.insert(batteryId, percent);
189 }
190
onBrightnessChanged(BrightnessControlType type,int value,int valueMax)191 void BackendInterface::onBrightnessChanged(BrightnessControlType type, int value, int valueMax)
192 {
193 BrightnessLogic *logic = d->brightnessLogic.value(type);
194 logic->setValueMax(valueMax);
195 logic->setValue(value);
196
197 Q_EMIT brightnessChanged(logic->info(), type);
198 }
199
capabilities() const200 BackendInterface::Capabilities BackendInterface::capabilities() const
201 {
202 return d->capabilities;
203 }
204
setCapabilities(BackendInterface::Capabilities capabilities)205 void BackendInterface::setCapabilities(BackendInterface::Capabilities capabilities)
206 {
207 d->capabilities = capabilities;
208 }
209
calculateNextStep(int value,int valueMax,BrightnessControlType type,BrightnessLogic::BrightnessKeyType keyType)210 int BackendInterface::calculateNextStep(int value, int valueMax, BrightnessControlType type, BrightnessLogic::BrightnessKeyType keyType)
211 {
212 BrightnessLogic *logic = d->brightnessLogic.value(type);
213 logic->setValueMax(valueMax);
214 logic->setValue(value);
215
216 return logic->action(keyType);
217 }
218
219 }
220