1 /* This file is part of the KDE project
2 Copyright (C) 2006 Kevin Ottens <ervin@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include "solid-powermanagement.h"
24 #include <QStringList>
25 #include <QMetaProperty>
29 #include <kcomponentdata.h>
30 #include <kcmdlineargs.h>
32 #include <k3socketaddress.h>
35 #include <solid/device.h>
36 #include <solid/genericinterface.h>
37 #include <solid/storageaccess.h>
38 #include <solid/opticaldrive.h>
40 #include <solid/control/powermanager.h>
48 static const char appName
[] = "solid-powermanagement";
49 static const char programName
[] = I18N_NOOP("solid-powermanagement");
51 static const char description
[] = I18N_NOOP("KDE tool for querying and controlling your power management options from the command line");
53 static const char version
[] = "0.1";
55 std::ostream
&operator<<(std::ostream
&out
, const QString
&msg
)
57 return (out
<< msg
.toLocal8Bit().constData());
60 std::ostream
&operator<<(std::ostream
&out
, const QVariant
&value
)
64 case QVariant::StringList
:
68 QStringList list
= value
.toStringList();
70 QStringList::ConstIterator it
= list
.constBegin();
71 QStringList::ConstIterator end
= list
.constEnd();
75 out
<< "'" << *it
<< "'";
83 out
<< "} (string list)";
87 out
<< (value
.toBool()?"true":"false") << " (bool)";
90 out
<< value
.toString()
91 << " (0x" << QString::number(value
.toInt(), 16) << ") (int)";
94 out
<< "'" << value
.toString() << "' (string)";
101 std::ostream
&operator<<(std::ostream
&out
, const Solid::Device
&device
)
103 out
<< " parent = " << QVariant(device
.parentUdi()) << endl
;
104 out
<< " vendor = " << QVariant(device
.vendor()) << endl
;
105 out
<< " product = " << QVariant(device
.product()) << endl
;
107 int index
= Solid::DeviceInterface::staticMetaObject
.indexOfEnumerator("Type");
108 QMetaEnum typeEnum
= Solid::DeviceInterface::staticMetaObject
.enumerator(index
);
110 for (int i
=0; i
<typeEnum
.keyCount(); i
++)
112 Solid::DeviceInterface::Type type
= (Solid::DeviceInterface::Type
)typeEnum
.value(i
);
113 const Solid::DeviceInterface
*interface
= device
.asDeviceInterface(type
);
117 const QMetaObject
*meta
= interface
->metaObject();
119 for (int i
=meta
->propertyOffset(); i
<meta
->propertyCount(); i
++)
121 QMetaProperty property
= meta
->property(i
);
122 out
<< " " << QString(meta
->className()).mid(7) << "." << property
.name()
125 QVariant value
= property
.read(interface
);
127 if (property
.isEnumType()) {
128 QMetaEnum metaEnum
= property
.enumerator();
129 out
<< "'" << metaEnum
.valueToKeys(value
.toInt()).constData() << "'"
130 << " (0x" << QString::number(value
.toInt(), 16) << ") ";
131 if (metaEnum
.isFlag()) {
138 out
<< value
<< endl
;
147 std::ostream
&operator<<(std::ostream
&out
, const QMap
<QString
,QVariant
> &properties
)
149 foreach (const QString
& key
, properties
.keys())
151 out
<< " " << key
<< " = " << properties
[key
] << endl
;
157 void checkArgumentCount(int min
, int max
)
159 int count
= KCmdLineArgs::parsedArgs()->count();
163 cerr
<< i18n("Syntax Error: Not enough arguments") << endl
;
167 if ((max
> 0) && (count
> max
))
169 cerr
<< i18n("Syntax Error: Too many arguments") << endl
;
174 int main(int argc
, char **argv
)
176 KCmdLineArgs::init(argc
, argv
, appName
, 0, ki18n(programName
), version
, ki18n(description
), false);
179 KCmdLineOptions options
;
181 options
.add("commands", ki18n("Show available commands"));
183 options
.add("+command", ki18n("Command (see --commands)"));
185 options
.add("+[arg(s)]", ki18n("Arguments for command"));
187 KCmdLineArgs::addCmdLineOptions(options
);
189 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs();
191 KComponentData
componentData(appName
);
193 if (args
->isSet("commands"))
195 KCmdLineArgs::enable_i18n();
197 cout
<< endl
<< i18n("Syntax:") << endl
<< endl
;
199 cout
<< " solid-powermanagement query (suspend|scheme|cpufreq)" << endl
;
200 cout
<< i18n(" # List a particular set of information regarding power management.\n"
201 " # - If the 'suspend' option is specified, give the list of suspend\n"
202 " # method supported by the system\n"
203 " # - If the 'scheme' option is specified, give the list of\n"
204 " # supported power management schemes by this system\n"
205 " # - If the 'cpufreq' option is specified, give the list of\n"
206 " # supported CPU frequency policy\n") << endl
;
208 cout
<< " solid-powermanagement set (scheme|cpufreq) 'value'" << endl
;
209 cout
<< i18n(" # Set power management options of the system.\n"
210 " # - If the 'scheme' option is specified, the power management\n"
211 " # scheme set corresponds to 'value'\n"
212 " # - If the 'cpufreq' option is specified, the CPU frequency policy\n"
213 " # set corresponds to 'value'\n") << endl
;
215 cout
<< " solid-powermanagement suspend 'method'" << endl
;
216 cout
<< i18n(" # Suspend the computer using the given 'method'.\n") << endl
;
218 cout
<< " solid-powermanagement brightness (set|get) 'value'" << endl
;
219 cout
<< i18n(" # Set and get brightness options of the system.\n"
220 " # - If the 'set' option is specified, the brightness is\n"
221 " # set to 'value' (as a percentage)\n"
222 " # - If the 'get' option is specified, the current brightness\n"
223 " # is returned (as a percentage)'\n") << endl
;
230 return SolidPowermanagement::doIt() ? 0 : 1;
233 bool SolidPowermanagement::doIt()
235 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs();
236 checkArgumentCount(1, 0);
238 QString
command(args
->arg(0));
241 char **fake_argv
= 0;
242 SolidPowermanagement
shell(fake_argc
, fake_argv
);
244 if (command
== "suspend")
246 checkArgumentCount(2, 2);
247 QString
method(args
->arg(1));
249 return shell
.powerSuspend(method
);
251 else if (command
== "query")
253 checkArgumentCount(2, 2);
254 QString
type(args
->arg(1));
256 if (type
== "suspend")
258 return shell
.powerQuerySuspendMethods();
260 else if (type
== "scheme")
262 return shell
.powerQuerySchemes();
264 else if (type
== "cpufreq")
266 return shell
.powerQueryCpuPolicies();
270 cerr
<< i18n("Syntax Error: Unknown option '%1'" , type
) << endl
;
273 else if (command
== "set")
275 checkArgumentCount(3, 3);
276 QString
type(args
->arg(1));
277 QString
value(args
->arg(2));
279 if (type
== "scheme")
281 return shell
.powerChangeScheme(value
);
283 else if (type
== "cpufreq")
285 return shell
.powerChangeCpuPolicy(value
);
289 cerr
<< i18n("Syntax Error: Unknown option '%1'" , type
) << endl
;
292 else if (command
== "brightness")
294 QString
request(args
->arg(1));
295 if (request
== "get")
297 cout
<< endl
<< "Brightness is " << shell
.powerGetBrightness() << "%" << endl
;
299 else if (request
== "set")
301 return shell
.powerSetBrightness(args
->arg(2).toInt());
306 cerr
<< i18n("Syntax Error: Unknown command '%1'" , command
) << endl
;
312 bool SolidPowermanagement::powerQuerySuspendMethods()
314 Solid::Control::PowerManager::SuspendMethods methods
= Solid::Control::PowerManager::supportedSuspendMethods();
316 if (methods
& Solid::Control::PowerManager::ToDisk
)
318 cout
<< "to_disk" << endl
;
321 if (methods
& Solid::Control::PowerManager::ToRam
)
323 cout
<< "to_ram" << endl
;
326 if (methods
& Solid::Control::PowerManager::Standby
)
328 cout
<< "standby" << endl
;
334 bool SolidPowermanagement::powerSuspend(const QString
&strMethod
)
336 Solid::Control::PowerManager::SuspendMethods supported
337 = Solid::Control::PowerManager::supportedSuspendMethods();
339 Solid::Control::PowerManager::SuspendMethod method
= Solid::Control::PowerManager::UnknownSuspendMethod
;
341 if (strMethod
== "to_disk" && (supported
& Solid::Control::PowerManager::ToDisk
))
343 method
= Solid::Control::PowerManager::ToDisk
;
345 else if (strMethod
== "to_ram" && (supported
& Solid::Control::PowerManager::ToRam
))
347 method
= Solid::Control::PowerManager::ToRam
;
349 else if (strMethod
== "standby" && (supported
& Solid::Control::PowerManager::Standby
))
351 method
= Solid::Control::PowerManager::Standby
;
355 cerr
<< i18n("Unsupported suspend method: %1" , strMethod
) << endl
;
359 KJob
*job
= Solid::Control::PowerManager::suspend(method
);
363 cerr
<< i18n("Error: unsupported operation!") << endl
;
374 cerr
<< i18n("Error: %1" , m_errorString
) << endl
;
383 bool SolidPowermanagement::powerQuerySchemes()
385 QString current
= Solid::Control::PowerManager::scheme();
386 QStringList schemes
= Solid::Control::PowerManager::supportedSchemes();
388 foreach (const QString
& scheme
, schemes
)
390 cout
<< scheme
<< " (" << Solid::Control::PowerManager::schemeDescription(scheme
) << ")";
394 cout
<< " [*]" << endl
;
405 bool SolidPowermanagement::powerChangeScheme(const QString
&schemeName
)
407 QStringList supported
= Solid::Control::PowerManager::supportedSchemes();
409 if (!supported
.contains(schemeName
))
411 cerr
<< i18n("Unsupported scheme: %1" , schemeName
) << endl
;
415 return Solid::Control::PowerManager::setScheme(schemeName
);
418 bool SolidPowermanagement::powerQueryCpuPolicies()
420 Solid::Control::PowerManager::CpuFreqPolicy current
= Solid::Control::PowerManager::cpuFreqPolicy();
421 Solid::Control::PowerManager::CpuFreqPolicies policies
= Solid::Control::PowerManager::supportedCpuFreqPolicies();
423 QList
<Solid::Control::PowerManager::CpuFreqPolicy
> all_policies
;
424 all_policies
<< Solid::Control::PowerManager::OnDemand
425 << Solid::Control::PowerManager::Userspace
426 << Solid::Control::PowerManager::Powersave
427 << Solid::Control::PowerManager::Performance
428 << Solid::Control::PowerManager::Conservative
;
430 foreach (Solid::Control::PowerManager::CpuFreqPolicy policy
, all_policies
)
432 if (policies
& policy
)
436 case Solid::Control::PowerManager::OnDemand
:
439 case Solid::Control::PowerManager::Userspace
:
442 case Solid::Control::PowerManager::Powersave
:
445 case Solid::Control::PowerManager::Performance
:
446 cout
<< "performance";
448 case Solid::Control::PowerManager::Conservative
:
449 cout
<< "conservative";
451 case Solid::Control::PowerManager::UnknownCpuFreqPolicy
:
457 cout
<< " [*]" << endl
;
469 bool SolidPowermanagement::powerChangeCpuPolicy(const QString
&policyName
)
471 Solid::Control::PowerManager::CpuFreqPolicies supported
472 = Solid::Control::PowerManager::supportedCpuFreqPolicies();
474 Solid::Control::PowerManager::CpuFreqPolicy policy
= Solid::Control::PowerManager::UnknownCpuFreqPolicy
;
476 if (policyName
== "ondemand" && (supported
& Solid::Control::PowerManager::OnDemand
))
478 policy
= Solid::Control::PowerManager::OnDemand
;
480 else if (policyName
== "userspace" && (supported
& Solid::Control::PowerManager::Userspace
))
482 policy
= Solid::Control::PowerManager::Userspace
;
484 else if (policyName
== "performance" && (supported
& Solid::Control::PowerManager::Performance
))
486 policy
= Solid::Control::PowerManager::Performance
;
488 else if (policyName
== "powersave" && (supported
& Solid::Control::PowerManager::Powersave
))
490 policy
= Solid::Control::PowerManager::Powersave
;
492 else if (policyName
== "conservative" && (supported
& Solid::Control::PowerManager::Conservative
))
494 policy
= Solid::Control::PowerManager::Conservative
;
498 cerr
<< i18n("Unsupported cpufreq policy: %1" , policyName
) << endl
;
502 return Solid::Control::PowerManager::setCpuFreqPolicy(policy
);
505 int SolidPowermanagement::powerGetBrightness()
507 return Solid::Control::PowerManager::brightness();
510 bool SolidPowermanagement::powerSetBrightness(int brightness
)
512 cout
<< "Setting brightness to " << brightness
<< "%" << endl
;
513 return Solid::Control::PowerManager::setBrightness(brightness
);
517 void SolidPowermanagement::connectJob(KJob
*job
)
519 connect(job
, SIGNAL(result(KJob
*)),
520 this, SLOT(slotResult(KJob
*)));
521 connect(job
, SIGNAL(percent(KJob
*, unsigned long)),
522 this, SLOT(slotPercent(KJob
*, unsigned long)));
523 connect(job
, SIGNAL(infoMessage(KJob
*, const QString
&, const QString
&)),
524 this, SLOT(slotInfoMessage(KJob
*, const QString
&)));
527 void SolidPowermanagement::slotPercent(KJob */
*job */
, unsigned long percent
)
529 cout
<< i18n("Progress: %1%" , percent
) << endl
;
532 void SolidPowermanagement::slotInfoMessage(KJob */
*job */
, const QString
&message
)
534 cout
<< i18n("Info: %1" , message
) << endl
;
537 void SolidPowermanagement::slotResult(KJob
*job
)
543 m_error
= job
->error();
544 m_errorString
= job
->errorString();
550 void SolidPowermanagement::slotStorageResult(Solid::ErrorType error
, const QVariant
&errorData
)
554 m_errorString
= errorData
.toString();
559 #include "solid-powermanagement.moc"