Monday, August 11, 2008

Powered by Lithium

Sometime last month I found myself looking for a KDE4 power manager. To be more specific, I was looking for a KDE4 port of KPowersave, but didn't find any. So I took it upon myself to port KPowersave to KDE4 and Solid only to conclude that it would be much easier to write a Solid based power manager from scratch. When looking for Solid documentation, I stumbled upon solidshell which I previously had no clue existed. Later that day I had written a simple power management application that could change brightness. When I had free time a few weeks ago I decided I'd upload my little app to playground, only to discover that 1) there was already a kde4powersave plasmoid and 2) there was a kded daemon based on the plasmoid. :-/

Anyway, it seems like a waste to stop working on it completely so I'll share my progress with you. First, I cloned the tooltip from guidance power manager.

Then I created something similar to the detailed dialog from KPowersave.

As well as the context menu structure of KPowersave.

But almost everything is powered by Solid and thus works similarly to solid-shell. However, I fixed the brightness controls to work on my system. KPowersave only set the brightness of the first laptop panel it received, solid-shell sets the brightness of every brightness control. On some systems such as my system that will result in conflicting brightness settings.

One thing I didn't like about previous power managers was that they polled to check system activity. The power manager is supposed to be an application that helps _conserve_ battery power but instead previous implementations may have served to further _drain_ battery power by unnecessary polling. In designing the system activity checking system for Lithium, I looked for solutions that would limit the need for polling. Lithium checks for system activity based on the smallest period that needs the system idle time.

For example, autodim is enabled and the timeout period set for autodim is 3 mins. A timer will start once Lithium is started and if after 3 minutes, the system is still active (i.e. idle time is 0), the timer will restart for another 3 minutes. This means that at worst polling will occur every 3 minutes for a system that is under continuous use. If after 3 minutes, the system has only been idle for 1.5 minutes, the timer will restart for 1.5 minutes.

To perform the opposite task of checking if the system is resuming from an idle state, previous power managers again polled the system. Lithium uses an ingenious solution adopted from the kde4powersave plasmoid which in turn borrowed it from ksnapshot. An offscreen widget grabs mouse and keyboard support and if it detects activity, it releases control and the system performs the necessary actions (restore brightness, etc.).

Working on a power manager brought to my attention that HAL doesn't necessarily correctly set the maximum speed of the processor. I also could not find a means to get the current CPU frequency from HAL. I ended up reading the information from /sys/devices/system/cpu which isn't ideal.

There are several more features I need to add such as an OSD and KMilo like functionality, better scheme support and config dialogs. I'll get to them sooner or later. It'll also be great to integrate my work with the kded daemon and the plasmoid but one reason I wrote an application that lived in the systray was to be able to use Lithium in other desktop environments.

So for my next little project, I will address another minor annoyance. I'm quite sick of people complaining about the lack of a KDE4 knetworkmanager so if no one else does so by Friday next week I'll begin work on a system tray icon port of knetworkmanager to KDE4. Or I could help out with the plasmoid... If someone's already working on it, please tell me so we don't duplicate any efforts. I've got a lot of other interesting projects that are begging for my attention. :)


Solardeity said...

Wow... this is great.. where can i get it from? I am using KDE 4.1 on a Kubuntu Notebook.. right now i am using the kde3 app guidance-power-manager..

liquidat said...

A huge YES for a KNetworkManager based on KDE 4. And would you mind developing the code in a public SVN? While I would not be able to help you there it would be very educating to see it evolving.

[Knuckles] said...

I also could not find a means to get the current CPU frequency from HAL.

Yeah I think this sucks, the current CPU frequency should be reported by hal instead of the current "0" it reports (and consequently solid would pick it up).

kitts said...

Hi Ryan!
I am part author of the powersave plasmoid. My contribution is the idle timer and detection of user activity. The timer in the applet does also behave in the same way as you describe above AFAIK. No?

After that implementation i realized that there is no clean X/kde way of idle time measurement and then again user activity detection. I really think code for this should go into kdelibs that all applications can use. Whats more? Each application should not preferably have their own timers but should just receive a notification (dbus?) when their desired idle time is reached or gain when user activity is detected.

For the Idle time detection i checked other apps including kopete and found that kopete polls every 4 seconds. Again, not very good.

If there are enough apps that benefit from the idle time and user activity notification then i think a central timer (or something better) should be maintained perhaps in KDED and notifications sent out to subscribers.


Khashayar said...

I'm very happy to see this. In fact, I'm one of those people who's fallen in love with KDE4 but is substantially annoyed by the fact that there's no power manager and, indeed, no networkmanager frontend. When it comes to lithium, I really hope it will get included in a future release of KDE4, it seems very nice and I like the fact that it uses solid. I only hope power management and kscreensaver could be more integrated in the future. Are there any plans for this?

About a networkmanager-plasmoid: I'm quite sure someone is working on this. At least I think I have caught some information that suggests it, probably on the planet or the dot...

Finally, it's amazing seeing KDE4 evolve and the pillars come to good use ;-)

Mikko Tuomitalo said...

Why there reads "Processor" and under it both cores as "CPU 1" and "CPU 2"?

There should read "Processor"

"Core 1"
"Core 2"

Now it reads like you would have two processor inside of one processor. What is silly :-)

Branan said...

I'm also interested in getting the source for this. There are a couple power managers in playground/utils, but I figure I should try them all, including Lithium.

Branan said...

Sorry for the double-comment, but I just found it. Google is my friend =)

It's on

sourtooth said...

I Am The Developer Of KOSDWidget, Which Is As Its Name Suggests A OSD.

It Is Already In SVN:

It Can Display Text, A Progress Bar ( ie: the Current Brightness, Volume, Etc ) And A Image ( ie: Cool Sun Logo That Changes As The Brightness Changes ). It Also Has A Timer And Is Capable Of Hiding Itself.

If You Wish To Use It, Or Have Any Problems, You Can Mail Me


gpothier said...


There is something I don't understand... there is the kde4powersave plasmoid ( It is not as configurable as the kde3 kpowersave, but it is ok.

However, I think power management should be an integral part of the system, ie. you should not have to load a plasmoid or application to have it; the configuration should be part of system configuration, etc.

I was about to take kde4powermanager and transform it into a KDED module + kconfig page but I was told about PowerDevil (, that does just that.

And now, there is Lithium...

Why so many ways to handle power management???

dilettante said...

@gpothier: Anyone can hack on anything any number of times. Hence there are a lot of redundant applications. I started hacking on this before I knew of kde4powersave and before powerdevil was released... And after I found out they existed, 80% of the work was done so I continued working on it since its fun. But integrating powerdevil into this would be the way to go I suppose. :)

dilettante said...

@kitts: I know idle time handling is pain, isn't it? Idle time is handled by KRunner though, but _ONLY_ if you enable the screensaver in kcontrol/system settings. I'm thinking of replacing the functionality in KRunner so that it tracks time all the time and so we can put the inhibit method to use so we don't have to maintain blacklists a la KPowersave.

dilettante said...

@mikko tuomitalo: Well, the kernel recognizes the cores as separate processors. I wouldn't know how to convince the kernel they belong to the same CPU but I don't think it's worth the trouble finding out which instances are caused by separate physical processor packages and multiple cores on the same processor package.

darktears said...

hi ryan,

Your job is great but your tooltip is not very nice (i mean beautiful). You probably doesn't know that in plasma we have good tooltips now. Your stuff seems to be in a old approach, why don't you use plasma for that? It's simply ugly in the systray. Applet is good for that.

You can ping in plasma chan nick: darktears.


darktears said...

at ryan again :

Why porting old knetworkmanager in a systray approach? We have an applet which is near to work and use solid::control which is simply better.

Please post in mailing lists before working, i mean (and to synchronise with others developpers.

You can go #plasma and #solid chans in order to try to avoid duplicate efforts.

Kyashan said...

I'd like to have also in Lithium detailed informations about batteries,like with gnome.In Linux Mint are showed the number code of the battery,the original capacity when the battery was new and the actual one.Thank you very much for your work,you help people to enjoy their computer experience and let me say...The name "Lithium" ROCKS!

dilettante said...

@darktears: Yes I know about that class. I implemented the original rounded corners look for it because it used to be rectangular. Someone then changed it to use SVG's. Thing is, I'm not using a plasmoid. It's a system tray application so it'll work in KDE3 and Gnome and XFCE. But you are right, I should've consulted #solid. :)

dilettante said...

@kyashan: Thanks! :) I appreciate your feedback. And yeah, Lithium does rock. :D

James said...

Excellent work! Might you set up a development repository for this somewhere? It would be great to see the development continue.