<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4777219628179820972</id><updated>2024-09-13T08:39:47.512+02:00</updated><category term="python"/><category term="C"/><category term="gtk"/><category term="script"/><category term="thread"/><category term="C++"/><category term="boost"/><category term="check"/><category term="gnome-screensaver-command"/><category term="hack"/><category term="html"/><category term="inhibit"/><category term="iso9660"/><category term="linux"/><category term="mntent"/><category term="mount"/><category term="mtab"/><category term="regex"/><title type='text'>Program-Nix</title><subtitle type='html'>Programming tutorials &amp;amp; snippets for Linux</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://program-nix.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://program-nix.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Tvrtko</name><uri>http://www.blogger.com/profile/04956047142776324060</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4777219628179820972.post-339238178916174628</id><published>2011-01-18T19:20:00.005+01:00</published><updated>2011-01-18T20:10:35.884+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="gtk"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="thread"/><title type='text'>Simple countdown progress bar remake</title><content type='html'>I&#39;ve been warned on &lt;a href=&#39;www.reddit.com&#39;&gt;reddit&lt;/a&gt; my countdown progress bar implementation explained &lt;a href=&#39;http://program-nix.blogspot.com/2011/01/simple-countdown-progress-bar-using.html&#39;&gt;here&lt;/a&gt; is messy and wouldn&#39;t work on Windows OS because changing widgets is valid only from the GUI thread and I must admit, a special thread object for the purpose of updating the widget really looks messy, but I really didn&#39;t test it to see if it works on Windows. Today I reworked the code anyway, merging all the previous stuff in one object that builds GUI and acts as a timer at the same time. Also I made some minor improvements. Simple countdown timer is now even more simpler:&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;gtk&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;threading&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Event&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gtk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gdk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;threads_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# initialize threads right away&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;cdProgressBar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tot_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gtk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_from_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;progressbar_countdown.glade&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;window&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;cd_window&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;progressbar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;cd_progressbar&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_progressbar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;connect_signals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt;             &lt;span class=&quot;s&quot;&gt;&amp;#39;on_cd_window_delete_event&amp;#39;&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;19&lt;/span&gt;             &lt;span class=&quot;s&quot;&gt;&amp;#39;on_cd_startbutton_clicked&amp;#39;&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startbutton_clicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;20&lt;/span&gt;             &lt;span class=&quot;s&quot;&gt;&amp;#39;on_cd_pausebutton_clicked&amp;#39;&lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pausebutton_clicked&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;21&lt;/span&gt;         &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;22&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;23&lt;/span&gt;         &lt;span class=&quot;c&quot;&gt;# threading&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;24&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpause&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;25&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;restart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;26&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setDaemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# stop the thread on exit&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;27&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;28&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;29&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;30&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# start the thread&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;31&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;gtk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;32&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;33&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;quit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;34&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;gtk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main_quit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;35&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;36&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;startbutton_clicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;37&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;38&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;39&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;restart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;40&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;41&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pausebutton_clicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;42&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isSet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;43&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# pause the countdown timer&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;44&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;45&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;46&lt;/span&gt;             &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;47&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;set_progressbar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;48&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;progressbar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;49&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;progressbar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_fraction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tot_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;50&lt;/span&gt;          &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;51&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;52&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;53&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# wait the self.unpause.isSet()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;54&lt;/span&gt;             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;55&lt;/span&gt;                 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tot_time&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;56&lt;/span&gt;                 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;restart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;57&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_progressbar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;58&lt;/span&gt;             &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;59&lt;/span&gt;             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;60&lt;/span&gt;                 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;61&lt;/span&gt;             &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;62&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cd_window&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cdProgressBar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;63&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cd_window&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;    &lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;a href=&#39;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiH-0pnVJ9YIBr1ab4w9XMVsqhMbSqYzxP3N5tB_oS13R-gWOKHY3SKQ1nCOnb8K-InDitvX3065uywfs_uNbpuMnAfSBvZ52yrDniMNvDBx6jdrLwgqoQBvI2E0Roq5X3_HHOrGeonsg0I/s800/cdpb01.png&#39;&gt;Countdown Progress Bar&lt;/a&gt; is designed using glade interface designer like &lt;a href=&#39;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_Hl0YYNkZ8VaZ1xF5AfFLOuQVVuEVQ0mbIAVkTTF7wEt3tPYTk1Z5JSS2iOlkA6NbekN95AXwZbqB32WLSZN7lElZ_GsIcY6lBOpQabN3RQga4x5IV2Yez9qUEInj0zV05Ubg4QJfVmAX/s800/cdpb02.png&#39;&gt;this&lt;/a&gt;. Code is tested and it&#39;s working under Windows and under Linux as well.</content><link rel='replies' type='application/atom+xml' href='http://program-nix.blogspot.com/feeds/339238178916174628/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/4777219628179820972/339238178916174628' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/339238178916174628'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/339238178916174628'/><link rel='alternate' type='text/html' href='http://program-nix.blogspot.com/2011/01/simple-countdown-progress-bar-remake.html' title='Simple countdown progress bar remake'/><author><name>Tvrtko</name><uri>http://www.blogger.com/profile/04956047142776324060</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4777219628179820972.post-8982213798605890196</id><published>2011-01-15T23:09:00.017+01:00</published><updated>2011-01-18T19:53:32.675+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="gtk"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="thread"/><title type='text'>Simple countdown progress bar using pygtk</title><content type='html'>Here I will explain how to build a progress bar that behaves like a simple countdown timer. This kind of progress bar at the start will be 100% filled and will empty itself every second until it&#39;s empty and reaches zero. Buttons to start and pause the countdown will be implemented as well. The picture below demonstrates the final product:&lt;br /&gt;&lt;br /&gt;&lt;img src=&#39;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiH-0pnVJ9YIBr1ab4w9XMVsqhMbSqYzxP3N5tB_oS13R-gWOKHY3SKQ1nCOnb8K-InDitvX3065uywfs_uNbpuMnAfSBvZ52yrDniMNvDBx6jdrLwgqoQBvI2E0Roq5X3_HHOrGeonsg0I/s800/cdpb01.png&#39; /&gt;&lt;br /&gt;&lt;br /&gt;For this to work, a background countdown timer thread must be implemented. Its also important to implement pause and restart function for the timer. Here is the full code for the &lt;i&gt;countdownThread&lt;/i&gt; object (countdownThread.py):&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;threading&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Event&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;countdownThread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callback_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__fulltime&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__unpause&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__paused&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__restart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__callback_func&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callback_func&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setDaemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pause&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__paused&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__paused&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__paused&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;19&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;20&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;21&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;22&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__time&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;23&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;24&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;25&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isAlive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;26&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;27&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;28&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__restart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;29&lt;/span&gt;             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__paused&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;30&lt;/span&gt;                 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pause&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;31&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;32&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;33&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;34&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;35&lt;/span&gt;             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;36&lt;/span&gt;                 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;37&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__unpause&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;38&lt;/span&gt;             &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__restart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;39&lt;/span&gt;                 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__fulltime&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;40&lt;/span&gt;                 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__restart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;41&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__callback_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;42&lt;/span&gt;             &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;43&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;/div&gt; &lt;br /&gt;&lt;br /&gt;Countdown timer object is inherited from Thread object, and more functionality is added. Its constructor accepts 2 arguments: &lt;i&gt;callback_func&lt;/i&gt; (a callable object) and &lt;i&gt;time&lt;/i&gt; (integer). They are used to initialize timer&#39;s member variables. The &lt;i&gt;self.__time&lt;/i&gt; is total time to start the countdown from, and it should decrement every second by 1; variable &lt;i&gt;self.__fulltime&lt;/i&gt; is used to restart the counter.&lt;br /&gt;Furthermore an Event object &lt;i&gt;self.__unpause&lt;/i&gt; is used internally for pausing/unpausing the counter. Callback function passed to constructor is function that should execute every one second till time hits zero. In this case it&#39;s used to update the progress bar in GUI.&lt;br /&gt;Timer is set to daemon thread using &lt;i&gt;self.setDaemon(True)&lt;/i&gt; which means the main thread doesn&#39;t need to wait for the timer to quit.&lt;br /&gt;Pause function on line 14 changes the &lt;i&gt;self.__paused&lt;/i&gt;, and sets/clears &lt;i&gt;self.__unpause&lt;/i&gt; Event object. When this object calls its set() function, it sets its internal flag, and any thread waiting for this event to happen continues its work until the clear() function &#39;unsets&#39; the internal flag again. In this case. CountdownTimer repeatedly checks the &lt;i&gt;self.__unpause&lt;/i&gt; event by calling &lt;i&gt;self.__unpause.wait()&lt;/i&gt;. If internal flag is set timer continues the loop, and if it&#39;s not it waits for the Event to &#39;unset&#39; the flag so it can continue the work.&lt;br /&gt;Restart function at line 24 is used to start the thread or restart the counter if the thread is already running. Run function is the timer itself, at the beginning it calls &lt;i&gt;self.__unpause.set()&lt;/i&gt;, meaning timer is unpaused at the start. While loop loops till the program exits, performs a couple of checks: sets time to full again if &lt;i&gt;self.__restart&lt;/i&gt; is true and sets time to 0 if it tries to go negative. Finally calls the callback function, sleeps a second and decrements time by one.&lt;br /&gt;&lt;br /&gt;Next, it is time to build a GUI. I did it using Glade Interface Designer program. Just try to build something similar to the pic above using GtkButton, GtkProgressBar. GtkHBox, and GtkVBox. Important thing is to set the handler names under &#39;Signals&#39; properties of each button so they can be connected to appropriate callback functions. It should look like something like &lt;a href=&#39;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_Hl0YYNkZ8VaZ1xF5AfFLOuQVVuEVQ0mbIAVkTTF7wEt3tPYTk1Z5JSS2iOlkA6NbekN95AXwZbqB32WLSZN7lElZ_GsIcY6lBOpQabN3RQga4x5IV2Yez9qUEInj0zV05Ubg4QJfVmAX/s800/cdpb02.png&#39;&gt;this&lt;/a&gt;.&lt;br /&gt;And here is the code for the gui (gui.py):&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lineno&quot;&gt; 1&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;gtk&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 2&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;gtk&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gdk&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 3&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 4&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;gtkCountdown.countdownThread&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countdownThread&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 5&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 6&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;cdProgressBar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 7&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 8&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tot_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt; 9&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cd_thread&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countdownThread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__countdown_cb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;10&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;11&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gtk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;12&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_from_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;progress_countdown.glade&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;13&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;window&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;cd_window&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;14&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;15&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;progressbar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;cd_progressbar&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;16&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;17&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;handlers_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;18&lt;/span&gt;             &lt;span class=&quot;s&quot;&gt;&amp;#39;on_cd_window_delete_event&amp;#39;&lt;/span&gt;     &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;19&lt;/span&gt;             &lt;span class=&quot;s&quot;&gt;&amp;#39;on_cd_startbutton_clicked&amp;#39;&lt;/span&gt;     &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startbutton_clicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;20&lt;/span&gt;             &lt;span class=&quot;s&quot;&gt;&amp;#39;on_cd_pausebutton_clicked&amp;#39;&lt;/span&gt;     &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pausebutton_clicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;21&lt;/span&gt;         &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;22&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;23&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;connect_signals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handlers_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;24&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;25&lt;/span&gt; &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;26&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;27&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;28&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;gdk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;threads_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;29&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;gtk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;30&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;31&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;startbutton_clicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;32&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cd_thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;33&lt;/span&gt;     &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;34&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;pausebutton_clicked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;35&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cd_thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isAlive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;36&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cd_thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pause&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;37&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;38&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;quit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;widget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;39&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;gtk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main_quit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;40&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;41&lt;/span&gt;     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__countdown_cb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;42&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;curr_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cd_thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;43&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;progressbar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curr_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;44&lt;/span&gt;         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;progressbar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_fraction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curr_time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tot_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;45&lt;/span&gt;         &lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;46&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cd_window&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cdProgressBar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;lineno&quot;&gt;47&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cd_window&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Notice I put those files in the same python package called &lt;b&gt;gtkCountdown&lt;/b&gt;.&lt;br /&gt;GUI construction needs only one parameter and it&#39;s time, which will be used along with &lt;i&gt;self.__countdown_cb&lt;/i&gt; callable object to construct a countdown timer thread &lt;i&gt;self.cd_thread&lt;/i&gt;. GUI is build and appropriate signals are connected in lines from 11 to 23. Function main() is used to start the gtk main loop. Important thing is to initialize the threads with &lt;i&gt;gdk.threads_init()&lt;/i&gt; before &lt;i&gt;gtk.main()&lt;/i&gt; is called.&lt;br /&gt;Clicking on the &#39;start&#39; button, restart() function of the countdown time thread is called, and by clicking on &#39;pause&#39; button timer is paused. Function &lt;i&gt;__countdown_cb&lt;/i&gt; as said before, is used by the timer and executes every second. It gets current time from &lt;i&gt;self.cd_thread&lt;/i&gt;, prints its value onto progress bar and updates it. GUI is initialized and ran in last two lines of code.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Notice that this code shouldn&#39;t be used as reference.&lt;/b&gt; Changing widget from thread outside the GUI thread is probably bad idea.&lt;br /&gt;&lt;br /&gt;See the improved countdown progress bar code &lt;a href=&#39;http://program-nix.blogspot.com/2011/01/simple-countdown-progress-bar-remake.html&#39;&gt;here&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://program-nix.blogspot.com/feeds/8982213798605890196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/4777219628179820972/8982213798605890196' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/8982213798605890196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/8982213798605890196'/><link rel='alternate' type='text/html' href='http://program-nix.blogspot.com/2011/01/simple-countdown-progress-bar-using.html' title='Simple countdown progress bar using pygtk'/><author><name>Tvrtko</name><uri>http://www.blogger.com/profile/04956047142776324060</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiH-0pnVJ9YIBr1ab4w9XMVsqhMbSqYzxP3N5tB_oS13R-gWOKHY3SKQ1nCOnb8K-InDitvX3065uywfs_uNbpuMnAfSBvZ52yrDniMNvDBx6jdrLwgqoQBvI2E0Roq5X3_HHOrGeonsg0I/s72-c/cdpb01.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4777219628179820972.post-1665786564310553534</id><published>2010-08-05T19:52:00.010+02:00</published><updated>2010-08-05T21:06:45.120+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="gnome-screensaver-command"/><category scheme="http://www.blogger.com/atom/ns#" term="inhibit"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="script"/><title type='text'>Python: &#39;Inhibiting&#39; the gnome-screensaver while watching videos</title><content type='html'>As user Styelz noted in the &lt;a href=&#39;http://program-nix.blogspot.com/2008/09/python-hacks-stopping-gnome-screensaver.html&#39;&gt;last blog post about this&lt;/a&gt;, the script with &lt;i&gt;&quot;gnome-screensaver-command -p&quot;&lt;/i&gt; uses 100% CPU while running.&lt;br /&gt;So I made a new script for this purpose that uses the &quot;&lt;b&gt;gnome-screensaver-command -i&lt;/b&gt;&quot; call. The &lt;i&gt;-i&lt;/i&gt; or &lt;i&gt;--inhibit&lt;/i&gt; command argument stops/inhibits gnome screensaver and gnome power manager at the same time. Here is the full script:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&#39;simple&#39;&gt;#!/usr/bin/python&lt;br /&gt;import subprocess&lt;br /&gt;&lt;br /&gt;#run inhibition&lt;br /&gt;ss_inhibit = subprocess.Popen([&quot;gnome-screensaver-command&quot;, &quot;-i&quot;, &quot;-n&quot;, &lt;br /&gt;                               &quot;mplayer&quot;, &quot;-r&quot;, &quot;video&quot;])&lt;br /&gt;&lt;br /&gt;#run gmplayer&lt;br /&gt;player = subprocess.Popen(&quot;gmplayer&quot;)&lt;br /&gt;&lt;br /&gt;#wait for mplayer to exit&lt;br /&gt;player.wait()&lt;br /&gt;&lt;br /&gt;#kill the inhibition&lt;br /&gt;ss_inhibit.kill()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Inhibition command is:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;gnome-screensaver-command -i -n mplayer -r video&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Options used here are:&lt;br /&gt;&lt;b&gt;-i&lt;/b&gt; -&gt; inhibit the screensaver from activating.&lt;br /&gt;&lt;b&gt;-n mplayer&lt;/b&gt; -&gt; the calling application that is inhibiting the screensaver is mplayer.&lt;br /&gt;&lt;b&gt;-r video&lt;/b&gt; -&gt; reason for inhibiting the screensaver - video</content><link rel='replies' type='application/atom+xml' href='http://program-nix.blogspot.com/feeds/1665786564310553534/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/4777219628179820972/1665786564310553534' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/1665786564310553534'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/1665786564310553534'/><link rel='alternate' type='text/html' href='http://program-nix.blogspot.com/2010/08/python-inhibiting-gnome-screensaver.html' title='Python: &#39;Inhibiting&#39; the gnome-screensaver while watching videos'/><author><name>Tvrtko</name><uri>http://www.blogger.com/profile/04956047142776324060</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4777219628179820972.post-140061524442114293</id><published>2008-09-03T23:34:00.009+02:00</published><updated>2010-08-05T21:23:06.895+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="hack"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="script"/><title type='text'>Python hacks: stopping gnome-screensaver while watching videos</title><content type='html'>I got really tired of pressing keys while watching a movie when the screen goes black so today I made those two &lt;b&gt;python&lt;/b&gt; scripts.&lt;br /&gt;&lt;br /&gt;First one simply shuts down &lt;b&gt;gnome-screensaver&lt;/b&gt; daemon, runs &lt;b&gt;gmplayer&lt;/b&gt; command, and than turns the screensaver back on after gmplayer exits. Here is the first:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;simple&quot;&gt;#!/usr/bin/python&lt;br /&gt;&lt;br /&gt;import subprocess&lt;br /&gt;&lt;br /&gt;# close gnome-screensaver&lt;br /&gt;subprocess.Popen([&quot;gnome-screensaver-command&quot;, &quot;--exit&quot;])&lt;br /&gt;&lt;br /&gt;# run gmplayer (put command which runs your favorite video player instead &quot;gmplayer&quot;&lt;br /&gt;player = subprocess.Popen(&quot;gmplayer&quot;)&lt;br /&gt;&lt;br /&gt;# wait till mplayer exits&lt;br /&gt;player.wait()&lt;br /&gt;&lt;br /&gt;# start gnome-screensaver again&lt;br /&gt;subprocess.Popen(&quot;gnome-screensaver&quot;)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Module subprocess contains neat Popen object that can run Linux commands within python code. This scripts simple calls &lt;i&gt;gnome-screensaver-commad --exit&lt;/i&gt;, &lt;i&gt;gmplayer&lt;/i&gt; and &lt;i&gt;gnome-screensaver&lt;/i&gt; command sequence like you would type it in the terminal.&lt;br /&gt;But this script has one flaw. It doesn&#39;t stop &lt;b&gt;gnome-power-manager&lt;/b&gt; so it will put display to sleep according to preferences. More lines can be added here that handles shutting the power manager and than turning it back on... you can do that.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;u&gt;Note: As pointed out by user Styelz, the script below uses 100% CPU cause it constantly polls the subprocess to complete. Lately I made the new script that uses the &lt;b&gt;--inhibit&lt;/b&gt; to stop the screensaver and powermanager at the same time. Read about it &lt;a href=&#39;http://program-nix.blogspot.com/2010/08/python-inhibiting-gnome-screensaver.html&#39;&gt;here&lt;/a&gt;&lt;/i&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;But the smarter idea came to my mind exploring the &lt;b&gt;gnome-screensaver-command&lt;/b&gt;. There is a way to notify gnome-screensaver of user activity with the -p (or --poke) option. Next script opens up a player and runs &lt;b&gt;&quot;gnome-screensaver-command -p&quot;&lt;/b&gt; in intervals till the video player closes:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;simple&quot;&gt;#!/usr/bin/python&lt;br /&gt;&lt;br /&gt;import time, subprocess&lt;br /&gt;&lt;br /&gt;# set interval in which screensaver should be poked&lt;br /&gt;poke_interval = 540&lt;br /&gt;&lt;br /&gt;# which command to use for starting a player &lt;br /&gt;player_command = &quot;gmplayer&quot;&lt;br /&gt;&lt;br /&gt;# run player&lt;br /&gt;try:&lt;br /&gt;    player = subprocess.Popen(player_command)&lt;br /&gt;except OSError:&lt;br /&gt;    print &quot;Could not open player&quot;&lt;br /&gt;&lt;br /&gt;# run &quot;gnome-screensaver-command -p&quot; in intervals&lt;br /&gt;t = time.time()&lt;br /&gt;&lt;br /&gt;while player.poll() == None:&lt;br /&gt;    if t + poke_interval &lt; time.time():&lt;br /&gt;        poke = subprocess.Popen([&quot;gnome-screensaver-command&quot;,&quot;-p&quot;])&lt;br /&gt;        poke.wait()&lt;br /&gt;        t=time.time()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Note that this two hacks works only for &lt;b&gt;gnome-screensaver&lt;/b&gt; and &lt;b&gt;MPlayer&lt;/b&gt;, but you can customize it for your own setup, you just need to figure out the right commands and replace them.</content><link rel='replies' type='application/atom+xml' href='http://program-nix.blogspot.com/feeds/140061524442114293/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/4777219628179820972/140061524442114293' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/140061524442114293'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/140061524442114293'/><link rel='alternate' type='text/html' href='http://program-nix.blogspot.com/2008/09/python-hacks-stopping-gnome-screensaver.html' title='Python hacks: stopping gnome-screensaver while watching videos'/><author><name>Tvrtko</name><uri>http://www.blogger.com/profile/04956047142776324060</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4777219628179820972.post-8513898667378309326</id><published>2008-08-18T18:34:00.001+02:00</published><updated>2010-08-05T19:46:32.121+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C"/><category scheme="http://www.blogger.com/atom/ns#" term="iso9660"/><category scheme="http://www.blogger.com/atom/ns#" term="linux"/><title type='text'>C/C++: Get the CD/DVD volume name</title><content type='html'>ISO 9660 is a international standard that defines a filesystem for CD/DVD media. To get the volume name from such system in Linux we&#39;ll be dealing with the its device path once again. On my system the device path of the CD/DVD device is &lt;b&gt;/dev/scd0&lt;/b&gt;.&lt;br /&gt;Also, it is important to know where is this information stored on the CD. Surfing and google-ing around I found &lt;a href=&quot;http://www.invisiblepixels.org/concepts/iso9660.html&quot;&gt;the site that explains ISO 9660&lt;/a&gt; in detail: types, modes, sectors etc. &lt;br /&gt;Field of interest (called &lt;b&gt;Volume ID&lt;/b&gt;) is located in the sector 16 called &lt;b&gt;Primary Volume Descriptor&lt;/b&gt; on the 32808. byte of the CD, and its length is 32 bytes.&lt;br /&gt;&lt;br /&gt;Those include directives are important for the compilation:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;ol&gt;&lt;li&gt;&lt;font color = &quot;#006699&quot;&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/font&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;font color = &quot;#006699&quot;&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;/font&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;font color = &quot;#006699&quot;&gt;#include &amp;lt;fcntl.h&amp;gt;&lt;/font&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;font color = &quot;#006699&quot;&gt;#include &amp;lt;string.h&amp;gt;&lt;/font&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;font color = &quot;#006699&quot;&gt;#include &amp;lt;unistd.h&amp;gt;&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Next, function that can read chunk of data from the filesystem at specific byte is needed:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;ol&gt;&lt;li&gt;&lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; fs_read_data ( &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt; * fs, &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; seek, &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; len, &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt; * data) &lt;/li&gt;&lt;br /&gt;&lt;li&gt;{&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;unsigned&lt;/b&gt;&lt;/font&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; fd;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; result = -&lt;font color = &quot;#993366&quot;&gt;1&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; ( fs != &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;NULL&lt;/b&gt;&lt;/font&gt;) &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt; {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; ( ( fd = open (fs, O_RDONLY)) != -&lt;font color = &quot;#993366&quot;&gt;1&lt;/font&gt;) &lt;/li&gt;&lt;br /&gt;&lt;li&gt;  {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;   &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; ( lseek (fd, seek, SEEK_SET) != -&lt;font color = &quot;#993366&quot;&gt;1&lt;/font&gt;) &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;   {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;    &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; ( read ( fd, data, len) != -&lt;font color = &quot;#993366&quot;&gt;1&lt;/font&gt;) &lt;/li&gt;&lt;br /&gt;&lt;li&gt;    {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;     data[len] = &lt;font color = &quot;#009900&quot;&gt;&#39;\0&#39;&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;     result = &lt;font color = &quot;#993366&quot;&gt;0&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;    }&lt;/li&gt;&lt;br /&gt;&lt;li&gt;   }&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  }&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  close ( fd);&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; result;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;}&lt;/li&gt;&lt;/ol&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Function &lt;b&gt;fs_read_data&lt;/b&gt; takes four arguments. String &lt;b&gt;fs&lt;/b&gt; is a device path of a iso9660 filesystem, variable &lt;b&gt;seek&lt;/b&gt; is a byte number at which we start reading data, &lt;b&gt;len&lt;/b&gt; is number of bytes to read and &lt;b&gt;data&lt;/b&gt; is a character buffer that stores the read data.&lt;br /&gt;If the &lt;b&gt;fs&lt;/b&gt; string is NULL, function returns -1 as a result, otherwise the device file is opened and data is being read from it. &lt;br /&gt;&lt;br /&gt;Function &lt;b&gt;open&lt;/b&gt; (&lt;a href=&quot;http://www.opengroup.org/onlinepubs/000095399/functions/open.html&quot;&gt;read more&lt;/a&gt;) tries to open the device file representing our filesystem (&lt;i&gt;/dev/scd0&lt;/i&gt;). Upon successful completion, the function shall open the file and return a non-negative integer representing the lowest numbered unused file descriptor; otherwise, -1 shall be returned. Second argument of the &lt;b&gt;open&lt;/b&gt; function says to open the file for rading only.&lt;br /&gt;&lt;br /&gt;Function &lt;b&gt;lseek&lt;/b&gt; (&lt;a href=&quot;http://www.opengroup.org/onlinepubs/000095399/functions/lseek.html&quot;&gt;read more&lt;/a&gt;) at line 9 sets the number of bytes to skip for the current file descriptor. In this case the number 32808 will be passed.&lt;br /&gt;&lt;br /&gt;Finally, data is being read with the function &lt;b&gt;read&lt;/b&gt; (&lt;a href=&quot;http://www.opengroup.org/onlinepubs/000095399/functions/lseek.html&quot;&gt;read more&lt;/a&gt;) at line 11. It reads the &lt;b&gt;len&lt;/b&gt; number of bytes from the file associated with the &lt;b&gt;fd&lt;/b&gt; file descriptor and puts that chunk of bytes into &lt;b&gt;data&lt;/b&gt; buffer. In our case the &lt;b&gt;len&lt;/b&gt; will be 32.&lt;br /&gt;&lt;br /&gt;If all the conditions of the &lt;i&gt;if statements&lt;/i&gt; are true the function &lt;b&gt;fs_read_data&lt;/b&gt; will return 0 and &lt;b&gt;data&lt;/b&gt; will be filled. Character &#39;\0&#39; marks the end of the string.&lt;br /&gt;&lt;br /&gt;The &lt;b&gt;main&lt;/b&gt; function is now constructed like this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;ol&gt;&lt;li&gt;&lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; main() {&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt; * fs = &lt;font color=&quot;#009900&quot;&gt;&quot;/dev/scd0&quot;&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; seek =&lt;font color=&quot;#993366&quot;&gt;32808&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt; &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; len = &lt;font color=&quot;#993366&quot;&gt;32&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt; * buff = &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;NULL&lt;/b&gt;&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt; * volume_name = &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;NULL&lt;/b&gt;&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt; buff = malloc( (&lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;sizeof&lt;/b&gt;&lt;/font&gt; (buff)) * (len+&lt;font color=&quot;#993366&quot;&gt;1&lt;/font&gt;));&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; ( fs_read_data ( fs, seek, len, buff) != -&lt;font color=&quot;#993366&quot;&gt;1&lt;/font&gt;)&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt; {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; ( (strncmp ( buff, &lt;font color=&quot;#009900&quot;&gt;&quot;NO NAME&quot;&lt;/font&gt;, &lt;font color=&quot;#993366&quot;&gt;7&lt;/font&gt;) == &lt;font color=&quot;#993366&quot;&gt;0&lt;/font&gt;) || ( strncmp (buff, &lt;font color=&quot;#009900&quot;&gt;&quot; &quot;&lt;/font&gt;,&lt;font color=&quot;#993366&quot;&gt;1&lt;/font&gt;) == &lt;font color=&quot;#993366&quot;&gt;0&lt;/font&gt;))&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;  {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;   free ( buff);&lt;/li&gt;&lt;br /&gt;&lt;li&gt;   buff=&lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;NULL&lt;/b&gt;&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;   volume_name = &lt;font color=&quot;#009900&quot;&gt;&quot;None&quot;&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  }&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;  &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;else&lt;/b&gt;&lt;/font&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;   volume_name = strdup ( buff);&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; printf(&lt;font color=&quot;#009900&quot;&gt;&quot;%s&quot;&lt;/font&gt;, volume_name);&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt; free (buff);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; buff=&lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;NULL&lt;/b&gt;&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color=&quot;#003399&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; &lt;font color=&quot;#993366&quot;&gt;0&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;}&lt;/li&gt;&lt;/ol&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;All important variables are declared in first 7 lines. Notice that &lt;b&gt;malloc&lt;/b&gt; function is used to allocate 32 bytes in the buffer &lt;b&gt;buff&lt;/b&gt;.&lt;br /&gt;The &lt;i&gt;if statement&lt;/i&gt; at line 9 tries to read wanted &lt;i&gt;Volume ID&lt;/i&gt; field from the /dev/scd0 device file representing my CD/DVD device. Next, &lt;b&gt;strcmp&lt;/b&gt; function is used to check if the string on that field is empty string (&quot;&quot;) or it says &quot;NO NAME&quot;. If either of that is true the volume name of the ISO 9660 filesystem isn&#39;t set, the &lt;b&gt;buff&lt;/b&gt; is freed and &quot;None&quot; is assigned to the &lt;b&gt;volume_name&lt;/b&gt; char array. Otherwise, the content of the &lt;b&gt;buff&lt;/b&gt; is duplicated into &lt;b&gt;volume_name&lt;/b&gt;. At the end volume name is printed on the screen.</content><link rel='replies' type='application/atom+xml' href='http://program-nix.blogspot.com/feeds/8513898667378309326/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/4777219628179820972/8513898667378309326' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/8513898667378309326'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/8513898667378309326'/><link rel='alternate' type='text/html' href='http://program-nix.blogspot.com/2008/08/cc-get-cddvd-volume-name.html' title='C/C++: Get the CD/DVD volume name'/><author><name>Tvrtko</name><uri>http://www.blogger.com/profile/04956047142776324060</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4777219628179820972.post-767296834276974886</id><published>2008-08-15T23:10:00.001+02:00</published><updated>2010-08-05T19:47:52.564+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="boost"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="html"/><category scheme="http://www.blogger.com/atom/ns#" term="regex"/><title type='text'>Convert C++ to syntax colored HTML code with line numbers</title><content type='html'>Today I made a small C++ program which converts C/C++ code to syntax highlighted HTML code. Changes on the code snippets on previous post are already been made. You can also see the program in action in this post. Like it? :)&lt;br /&gt;This code is not so small so I will not explain it in detail. For the code compilation the &lt;a href=&quot;http://www.boost.org/&quot;&gt;Boost C++&lt;/a&gt; and its regular expression libraries must be installed on a system. Boost can be either built from source or installed via some package manager; your choice. When compiling program that uses boost regex (like this one) use the &lt;b&gt;-lboost_regex&lt;/b&gt; flag when linking with GCC C++ linker.&lt;br /&gt;OK, here&#39;s the code:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;long&quot;&gt;&lt;ol&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 102, 153);&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 102, 153);&quot;&gt;#include &amp;lt;fstream&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 102, 153);&quot;&gt;#include &amp;lt;string&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;using&lt;/b&gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;namespace&lt;/b&gt;&lt;/span&gt; std;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 102, 153);&quot;&gt;#include &amp;lt;boost/regex.hpp&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/span&gt; string help = &lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;Usage:&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;\n\tccode2html [input file] [output file]&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;\n\tIf output filename is omitted it will be saved as [input file].html\n&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/span&gt; string pre_expression = &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(&amp;gt;)|(&amp;lt;)|(&amp;amp;)&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/span&gt; string pre_format = &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(?1\\&amp;amp;gt;)(?2\\&amp;amp;lt;)(?3\\&amp;amp;amp;)&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/span&gt; string line_expression = &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;^.*?$&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/span&gt; string line_format = &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;&amp;lt;li&amp;gt;$&amp;amp;&amp;lt;/li&amp;gt;&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/span&gt; string whole_code_expression = &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;^.*$&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/span&gt; string whole_code_format = &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;&amp;lt;pre&amp;gt;&amp;lt;ol&amp;gt;$&amp;amp;&amp;lt;/ol&amp;gt;&amp;lt;/pre&amp;gt;&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/span&gt; string expressions =&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// single line comments&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(//.*?(?=&amp;lt;/li&amp;gt;))|&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// multi-line comments&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(/\\*.*?\\*/)|&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// string literals&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(\&quot;(?:[^\\\\\&quot;]|\\\\.)*\&quot;|&#39;(?:[^\\\\&#39;]|\\\\.)*&#39;)|&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// precompile directives&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(#.*?(?=&amp;lt;/li&amp;gt;))|&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// floating point numbers&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(\\&amp;lt;[[:digit:]]+\\.[[:digit:]]+)|&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// integer numbers&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(\\&amp;lt;[[:digit:]]+\\&amp;gt;)|&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// boolean literals&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;((?:\\&amp;lt;true\\&amp;gt;)|(?:\\&amp;lt;false\\&amp;gt;))|&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// keywords&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(\\&amp;lt;(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;|using|virtual|void|volatile|wchar_t|while|NULL)\\&amp;gt;)&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;const&lt;/b&gt;&lt;/span&gt; string formats =&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(?1&amp;lt;font color = \&quot;#999999\&quot;&amp;gt;&amp;lt;i&amp;gt;$&amp;amp;&amp;lt;/i&amp;gt;&amp;lt;/font&amp;gt;)&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(?2&amp;lt;font color = \&quot;#D3D3D3\&quot;&amp;gt;$&amp;amp;&amp;lt;/font&amp;gt;)&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(?3&amp;lt;font color = \&quot;#009900\&quot;&amp;gt;$&amp;amp;&amp;lt;/font&amp;gt;)&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(?4&amp;lt;font color = \&quot;#006699\&quot;&amp;gt;$&amp;amp;&amp;lt;/font&amp;gt;)&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(?5&amp;lt;font color = \&quot;#996600\&quot;&amp;gt;$&amp;amp;&amp;lt;/font&amp;gt;)&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(?6&amp;lt;font color = \&quot;#993366\&quot;&amp;gt;$&amp;amp;&amp;lt;/font&amp;gt;)&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(?7&amp;lt;font color = \&quot;#990000\&quot;&amp;gt;&amp;lt;b&amp;gt;$&amp;amp;&amp;lt;/b&amp;gt;&amp;lt;/font&amp;gt;)&quot;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;(?8&amp;lt;font color = \&quot;#003399\&quot;&amp;gt;&amp;lt;b&amp;gt;$&amp;amp;&amp;lt;/b&amp;gt;&amp;lt;/font&amp;gt;)&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/span&gt; main (&lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/span&gt; argc, &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/span&gt;* argv[]) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt; string input_filenm;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; string output_filenm;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; ( argc &amp;gt; &lt;span style=&quot;color: rgb(153, 51, 102);&quot;&gt;3&lt;/span&gt; || argc == &lt;span style=&quot;color: rgb(153, 51, 102);&quot;&gt;1&lt;/span&gt; ) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  cout &amp;lt;&amp;lt; help;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  exit (-&lt;span style=&quot;color: rgb(153, 51, 102);&quot;&gt;1&lt;/span&gt;);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; ( argc == &lt;span style=&quot;color: rgb(153, 51, 102);&quot;&gt;2&lt;/span&gt;) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  input_filenm = argv[&lt;span style=&quot;color: rgb(153, 51, 102);&quot;&gt;1&lt;/span&gt;];&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  output_filenm = input_filenm + &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;.html&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt; {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  string input_filenm = argv[&lt;span style=&quot;color: rgb(153, 51, 102);&quot;&gt;1&lt;/span&gt;];&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  string output_filenm = argv[&lt;span style=&quot;color: rgb(153, 51, 102);&quot;&gt;2&lt;/span&gt;];&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; ifstream in ( input_filenm.c_str() );&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; (!in.is_open()) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  cout &amp;lt;&amp;lt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot;Failed to open: &quot;&lt;/span&gt; &amp;lt;&amp;lt; input_filenm &amp;lt;&amp;lt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&#39;\n&#39;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt; {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  cout &amp;lt;&amp;lt; input_filenm &amp;lt;&amp;lt; &lt;span style=&quot;color: rgb(0, 153, 0);&quot;&gt;&quot; opened successfully\nProcessing...\n&quot;&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; ofstream out ( output_filenm.c_str() );&lt;/li&gt;&lt;br /&gt;&lt;li&gt; string in_string;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/span&gt; c;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;while&lt;/b&gt;&lt;/span&gt; (in.get(c)) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  in_string.append(&lt;span style=&quot;color: rgb(153, 51, 102);&quot;&gt;1&lt;/span&gt;,c);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; boost::regex reg;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// replace &amp;lt;, &amp;gt; and &amp;amp; signs with appropriate html escape characters&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; reg.assign(pre_expression);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; in_string = boost::regex_replace(in_string, reg, pre_format, boost::match_default | boost::format_all);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// add &amp;lt;li&amp;gt; ... &amp;lt;/li&amp;gt; tags on each line&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; reg.assign(line_expression);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; in_string = boost::regex_replace(in_string, reg, line_format, boost::match_default | &lt;/li&gt;&lt;br /&gt;&lt;li&gt;   boost::format_all | boost::match_not_dot_newline);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// format and color code syntax&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; reg.assign(expressions);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; in_string = boost::regex_replace(in_string, reg, formats, boost::match_default | boost::match_default | &lt;/li&gt;&lt;br /&gt;&lt;li&gt;   boost::format_all);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(153, 153, 153);&quot;&gt;&lt;i&gt;// add &amp;lt;pre&amp;gt;&amp;lt;ol&amp;gt; on start and &amp;lt;/ol&amp;gt;&amp;lt;/pre&amp;gt; at the end of the file&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; reg.assign(whole_code_expression);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; in_string = boost::regex_replace(in_string, reg, whole_code_format);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;/li&gt;&lt;br /&gt;&lt;li&gt; in.close();&lt;/li&gt;&lt;br /&gt;&lt;li&gt; out &amp;lt;&amp;lt; in_string;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; out.close();&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;span style=&quot;color: rgb(0, 51, 153);&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(153, 51, 102);&quot;&gt;0&lt;/span&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;}&lt;/li&gt;&lt;/ol&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;First three lines include important header files for the program. Second include is required for file i/o operations on lines 72, 73 and 79. On the 6. line the file &lt;b&gt;boost/regex.hpp&lt;/b&gt; is included. That file is required for regular expression functions and objects. I used only &lt;b&gt;regex&lt;/b&gt; class and &lt;b&gt;regex_replace&lt;/b&gt; functions in this code. String help is declared from lines 8 to 11 and it explains the usage of the program if zero or more than two command-line arguments are passed when program is ran.&lt;br /&gt;&lt;br /&gt;The heart of the program is in the lines 13-55 and 85-103. See the &lt;a href=&quot;http://www.boost.org/doc/libs/1_36_0/libs/regex/doc/html/index.html&quot;&gt;Boost Regex Documentation&lt;/a&gt; for more information about regular expressions used in C++. Many regular expression tutorials and explanations can be found &lt;a href=&quot;http://www.regular-expressions.info&quot;&gt;here&lt;/a&gt; also. There is even a book dedicated to that topic called: &lt;a href=&quot;http://oreilly.com/catalog/9781565922570/&quot;&gt;Mastering Regular Expressions&lt;/a&gt; which is strongly suggested.&lt;br /&gt;&lt;br /&gt;I&#39;ll just shortly explain how I used regexes in this program. Two strings are declared at 13. and 14. lines. The first string &lt;b&gt;pre_expression&lt;/b&gt; is passed to &lt;b&gt;assign&lt;/b&gt; function of the &lt;b&gt;regex&lt;/b&gt; class (see 88. line). It describes search pattern, in this case the document is searched for &#39;&lt;&#39;,&#39;&gt;&#39; and &#39;&amp;amp;&#39; signs that needs to be replaced with the HTML escape sequences. Those escape sequences are passed to the &lt;b&gt;pre_format&lt;/b&gt; string inside. The &lt;i&gt;?1&lt;/i&gt;, &lt;i&gt;?2&lt;/i&gt; and &lt;i&gt;?3&lt;/i&gt; are representing the sub-expression indexes. After assigning the &lt;b&gt;pre_expression&lt;/b&gt; string to &lt;b&gt;reg&lt;/b&gt; regex on line 88 the function &lt;b&gt;regex_replace&lt;/b&gt;, which takes four arguments, is called.&lt;br /&gt;First argument &lt;b&gt;in_string&lt;/b&gt; is the string filled with the whole code from the file. That string is searched with the &lt;b&gt;pre_expression&lt;/b&gt; patterns. When a match is found it is replaced with the data in the &lt;b&gt;pre_format&lt;/b&gt; string. Last parameter is used for boost specific flags.&lt;br /&gt;&lt;br /&gt;First, the &#39;&lt;&#39;,&#39;&gt;&#39; and &#39;&amp;amp;&#39; signs are replaced with the HTML escape sequence equivalents. The &lt;i&gt;&amp;lt;li&amp;gt;&lt;/i&gt; and &lt;i&gt;&amp;lt;/li&amp;gt;&lt;/i&gt; tags are added at start and end of each line for numbering. Than the code syntax is highlighted with appropriate HTML &lt;i&gt;&amp;lt;font&amp;gt;&lt;/i&gt; tags declared in string &lt;b&gt;formats&lt;/b&gt; on lines 47-55. Finally &lt;i&gt;&amp;lt;pre&amp;gt;&lt;/i&gt; and &lt;i&gt;&amp;lt;ol&amp;gt;&lt;/i&gt; and their closing tags are put on the start and the end of the file.&lt;br /&gt;&lt;br /&gt;Only problem in this code is regex for multi-line comments. The expression &#39;/\\*.*?\\*/&#39; matches all text between the &#39;/*&#39; and &#39;*/&#39; including &lt;i&gt;&amp;lt;li&amp;gt;&lt;/i&gt; and &lt;i&gt;&amp;lt;/li&amp;gt;&lt;/i&gt; tags inside. Never figured out how to exclude those tags from formating. If some regex professional is reading this please help out!</content><link rel='replies' type='application/atom+xml' href='http://program-nix.blogspot.com/feeds/767296834276974886/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/4777219628179820972/767296834276974886' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/767296834276974886'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/767296834276974886'/><link rel='alternate' type='text/html' href='http://program-nix.blogspot.com/2008/08/convert-c-to-syntax-colored-html-code.html' title='Convert C++ to syntax colored HTML code with line numbers'/><author><name>Tvrtko</name><uri>http://www.blogger.com/profile/04956047142776324060</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4777219628179820972.post-469617277038524162</id><published>2008-08-13T16:24:00.003+02:00</published><updated>2010-08-05T19:48:49.441+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C"/><category scheme="http://www.blogger.com/atom/ns#" term="check"/><category scheme="http://www.blogger.com/atom/ns#" term="mntent"/><category scheme="http://www.blogger.com/atom/ns#" term="mount"/><category scheme="http://www.blogger.com/atom/ns#" term="mtab"/><title type='text'>C/C++: Check the filesystem is mounted</title><content type='html'>I&#39;ll write and explain the function that checks the &lt;b&gt;/etc/mtab&lt;/b&gt; file for the information if the given device is mounted. For this the default C &lt;b&gt;FILE&lt;/b&gt; pointer and the &lt;b&gt;mntent&lt;/b&gt; structure will be used. More information about the &lt;b&gt;mntent&lt;/b&gt; struct and what elements it holds see &lt;a href=&quot;http://www.cptec.inpe.br/sx4/sx4man2/g1ab02e/mntent.4.html&quot;&gt;this page&lt;/a&gt;. Three includes are needed before the coding starts:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;ol&gt;&lt;li&gt;&lt;font color = &quot;#006699&quot;&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/font&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;font color = &quot;#006699&quot;&gt;#include &amp;lt;mntent.h&amp;gt;&lt;/font&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;font color = &quot;#006699&quot;&gt;#include &amp;lt;string.h&amp;gt;&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;First line doesn&#39;t need much explanation; just notice it is needed for the file i/o. Second line includes &lt;b&gt;mntent.h&lt;/b&gt; where the mntent structure and functions are declared and the third line includes &lt;b&gt;string.h&lt;/b&gt; for string manipulation functions (see the &lt;a href=&quot;http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html#string.h&quot;&gt;string.h documentation&lt;/a&gt;).&lt;br /&gt;Here is the code of the &lt;b&gt;is_mounted&lt;/b&gt; function:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;&lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; is_mounted (&lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;char&lt;/b&gt;&lt;/font&gt; * dev_path) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt; FILE * mtab = NULL;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;struct&lt;/b&gt;&lt;/font&gt; mntent * part = NULL;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; is_mounted = &lt;font color = &quot;#993366&quot;&gt;0&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; ( ( mtab = setmntent (&lt;font color = &quot;#009900&quot;&gt;&quot;/etc/mtab&quot;&lt;/font&gt;, &lt;font color = &quot;#009900&quot;&gt;&quot;r&quot;&lt;/font&gt;) ) != NULL) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;while&lt;/b&gt;&lt;/font&gt; ( ( part = getmntent ( mtab) ) != NULL) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;   &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; ( ( part-&amp;gt;mnt_fsname != NULL ) &lt;/li&gt;&lt;br /&gt;&lt;li&gt;    &amp;amp;&amp;amp; ( strcmp ( part-&amp;gt;mnt_fsname, dev_path ) ) == &lt;font color = &quot;#993366&quot;&gt;0&lt;/font&gt; ) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;    is_mounted = &lt;font color = &quot;#993366&quot;&gt;1&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;   }&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  }&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  endmntent ( mtab);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; is_mounted;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;}&lt;/li&gt;&lt;/ol&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In first three lines of the &lt;b&gt;is_mounted&lt;/b&gt; function file pointer &lt;b&gt;mtab&lt;/b&gt;, mntent structure pointer &lt;b&gt;part&lt;/b&gt; and integer &lt;b&gt;is_mounted&lt;/b&gt; are declared; we assign NULL value to pointers and a zero to the &lt;b&gt;is_mounted&lt;/b&gt; integer (assumption is the device isn&#39;t mounted). Program would work without assigning NULLs to pointers but it&#39;s there for security reasons.&lt;br /&gt;&lt;br /&gt;In the fourth line the &lt;b&gt;setmntent&lt;/b&gt; function is introduced. This function takes two arguments, first is the path to the &lt;b&gt;mtab&lt;/b&gt; file (it&#39;s location is &lt;i&gt;/etc/mtab&lt;/i&gt; on most Linux systems) and the second is short string determines how the file is opened by the program. In this case file &lt;i&gt;/etc/mtab&lt;/i&gt; is trying to be opened for reading only. &lt;b&gt;setmntent&lt;/b&gt; function is similar to the &lt;a href=&quot;http://man.he.net/man3/fopen&quot;&gt;&lt;b&gt;fopen&lt;/b&gt;&lt;/a&gt; function declared in &lt;i&gt;stdio.h&lt;/i&gt;.&lt;br /&gt;Next, the &lt;b&gt;getmntent&lt;/b&gt; function takes a file pointer as argument, reads a line from the &lt;b&gt;mtab&lt;/b&gt; file and fills the &lt;b&gt;mntent&lt;/b&gt; structure.&lt;br /&gt;While loop in this code reads the &lt;i&gt;/etc/mtab&lt;/i&gt; file line by line and checks if it contains the required information. We are actually comparing given device path &lt;b&gt;char * dev_path&lt;/b&gt; with the each of the device paths inside the mtab file (&lt;b&gt;part-&gt;mnt_fsname&lt;/b&gt;). We are using function &lt;b&gt;strcmp&lt;/b&gt; for this. If the mtab contains it that means the filesystem (device) is mounted and we assign number one to our &lt;b&gt;is_mounted&lt;/b&gt; variable. Also the &lt;b&gt;mnt_fsname&lt;/b&gt; element also must not be NULL.&lt;br /&gt;After the while loop the function &lt;b&gt;endmntent&lt;/b&gt; closes the file system description file &lt;i&gt;mtab&lt;/i&gt;.&lt;br /&gt;At the end the &lt;b&gt;is_mounted&lt;/b&gt; variable is returned.&lt;br /&gt;&lt;br /&gt;Now the famous main function can be constructed like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;&lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/font&gt; main() {&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; (is_mounted(&lt;font color = &quot;#009900&quot;&gt;&quot;/dev/scd0&quot;&lt;/font&gt;)) {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  printf(&lt;font color = &quot;#009900&quot;&gt;&quot;CDROM mounted!\n&quot;&lt;/font&gt;);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;else&lt;/b&gt;&lt;/font&gt; {&lt;/li&gt;&lt;br /&gt;&lt;li&gt;  printf(&lt;font color = &quot;#009900&quot;&gt;&quot;CDROM not mounted!\n&quot;&lt;/font&gt;);&lt;/li&gt;&lt;br /&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;li&gt; &lt;font color = &quot;#003399&quot;&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; &lt;font color = &quot;#993366&quot;&gt;0&lt;/font&gt;;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;}&lt;/li&gt;&lt;/ol&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The /dev/scd0 is a device path of my cd/dvd device. This should be the same on all Debian based systems. If the device path of the filesystem is unknown this check can still be performed by comparing the mount path with the &lt;b&gt;mnt_dir&lt;/b&gt; element of the mntent structure.</content><link rel='replies' type='application/atom+xml' href='http://program-nix.blogspot.com/feeds/469617277038524162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/4777219628179820972/469617277038524162' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/469617277038524162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4777219628179820972/posts/default/469617277038524162'/><link rel='alternate' type='text/html' href='http://program-nix.blogspot.com/2008/08/c-language-check-filesystem-is-mounted.html' title='C/C++: Check the filesystem is mounted'/><author><name>Tvrtko</name><uri>http://www.blogger.com/profile/04956047142776324060</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry></feed>