<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>OS Admin Solutions &#187; My projects</title>
	<atom:link href="http://osadmin.com/category/software/my-projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://osadmin.com</link>
	<description>Free people, free mind, free software</description>
	<lastBuildDate>Tue, 25 May 2010 19:44:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>C++ Hibernate Daemon</title>
		<link>http://osadmin.com/2008/04/30/cpp-hibernate-daemon/</link>
		<comments>http://osadmin.com/2008/04/30/cpp-hibernate-daemon/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 03:06:28 +0000</pubDate>
		<dc:creator>mire</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[My projects]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[System Administration]]></category>

		<guid isPermaLink="false">http://osadmin.com/?p=163</guid>
		<description><![CDATA[I use Fluxbox window manager on Ubuntu Linux where I don&#8217;t have all the bells and whistles of GNOME or KDE. so I thought I&#8217;d make myself a daemon program that will automatically put my computer to hibernate when my laptop battery is almost empty usually I take care that my battery doesn&#8217;t get to [...]]]></description>
			<content:encoded><![CDATA[<p>I use Fluxbox window manager on Ubuntu Linux where I don&#8217;t have all the bells and whistles of GNOME or KDE.</p>
<p>so I thought I&#8217;d make myself a daemon program that will automatically put my computer to hibernate when my laptop battery is almost empty</p>
<p>usually I take care that my battery doesn&#8217;t get to the red zone but this is for those few cases I forget</p>
<p>I wrote this small programm in C++</p>
<p>why c++</p>
<p>well because I don&#8217;t programm in c++ and I found it interesting to do so <img src='http://osadmin.com/mire_blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>main.cpp looks like this</p>
<p><a href="http://osadmin.com/mire_blog/wp-content/uploads/2008/04/main.jpg"><img class="alignnone size-full wp-image-164" title="main.cpp" src="http://osadmin.com/mire_blog/wp-content/uploads/2008/04/main.jpg" alt="" width="432" height="376" /></a></p>
<p>so, you see the locations of the info and state file for the laptop battery, the percent at which we go to hibernation (I&#8217;ve set it to 12%), and the sleep interval before checking the battery state again (10 seconds)</p>
<p>I use my own hibernate script but you could use something like /usr/sbin/hibernate</p>
<p>you can change all of these</p>
<p>in the main function you can se that:</p>
<ul>
<li>we work only as a root user</li>
<li>we daemonize (detach from the terminal and run in background)</li>
<li>instantiate a worker class object</li>
<li>and do our work <img src='http://osadmin.com/mire_blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p>and what is our work? check out this picture</p>
<p><a href="http://osadmin.com/mire_blog/wp-content/uploads/2008/04/do_work.jpg"><img class="alignnone size-full wp-image-165" title="do_work function" src="http://osadmin.com/mire_blog/wp-content/uploads/2008/04/do_work.jpg" alt="worker.cpp" width="434" height="578" /></a></p>
<p>well we get the battery total capacity and current capacity and then we calculate the percent</p>
<p>if the percent is less that what I want (12%) then I want my laptop to go to hibernaton</p>
<p>but I also made a security check (that you can remove if you like)</p>
<p>that makes sure my laptop doesn&#8217;t go twice into hibernation in succession</p>
<p>why did I do that?</p>
<p>well because I parse data from some files I have no influence over</p>
<p>and those files can change their format anytime</p>
<p>so this is just a precaution</p>
<p>I have a simple makefile that compiles this code, copies it to /usr/local/bin/medved (medved in my language means a bear and bears hibernate <img src='http://osadmin.com/mire_blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ) and adds an entry to /etc/rc.local so that it starts next time you reboot your box automatically</p>
<p>you can also start it by hand and see how it works before the reboot</p>
<p>and that&#8217;s it, now I have a daemon that puts my laptop to hibernation before my battery runs dry</p>
<p>I&#8217;ve published the source code under GPL licence version 3</p>
<p>you can get source code <a title="medved source code" href="http://osadmin.com/projects/medved.tar.bz2">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://osadmin.com/2008/04/30/cpp-hibernate-daemon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
