Advertisement
Guest User

premek

a guest
Jan 31st, 2008
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.94 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3.     ##   #  #  #         #                  
  4.    #  #  #  #     ###   ####  ###   ###   #  #  
  5.    ####  #  #  #  #  #   #    #    #  #   # #  
  6.    #  #  #  #  #  #  #   ###  #     ###    #  
  7.                                          ##    
  8. ##################################################
  9. #                                                #
  10. #                  ALLINTRAY                     #
  11. #          Date/time, CPU/RAM usage,             #
  12. #       network traffic, volume control          #
  13. #                                                #
  14. ##################################################
  15. #                                                #
  16. #   Copyright (c) 2008 Premek Vyhnal             #
  17. #   <premysl.vyhnal gmail.com>                   #
  18. #                                                #
  19. #   Used code:                                   #
  20. #    - volwheel.pl                               #
  21. #       - thanks to Olivier Duclos - oliwer.net  #
  22. #    - gkrellm-bfm plugin                        #
  23. #                                                #
  24. ##################################################
  25.  
  26. # This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.  
  27. # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  
  28. # You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  29.  
  30.  
  31.  
  32. # ##################
  33. # co to pot&#345;ebuje:
  34. # ##################
  35. # amixer
  36. # + to co se nastav na kliknut:
  37. #     xmessage
  38. #     alsamixergui (alsamixer v xtermu)
  39. #     ccal (gcal)
  40. # ##################
  41. # /proc/stat
  42. # /proc/net/dev
  43. # ##################
  44. # perl
  45. # Gtk2
  46. # Gtk2::TrayIcon
  47. # ##################
  48. #
  49. # funguje v trayeru
  50.  
  51.  
  52.  
  53.  
  54.  
  55. use warnings;                                  
  56. use Gtk2 '-init';
  57. use Gtk2::TrayIcon;
  58.  
  59. # argumenty z prikazovy radky:
  60. $delay = shift (@ARGV) || 1000; # refresh rate (ms)
  61. $rxmax = shift (@ARGV) || 100;  # max download (kBps)
  62. $txmax = shift (@ARGV) || 40;   # max upload (kBps)
  63.  
  64.  
  65. $MIXER = "alsamixergui";
  66. $MIXER = "xterm -e 'alsamixer'";
  67. $PROCMAN = "xterm -e 'htop'";
  68. $PROCMAN = "gtaskmanager";
  69. $CAL1 = "xmessage \"`ccal`\"";
  70. $CAL2 = "xmessage \"`ccal ".((localtime(time))[5] + 1900)."`\"";
  71. $MEM = "xmessage \"`free`\"";
  72.  
  73.  
  74. $PROCSTAT = "/proc/stat";
  75. $PROCNET = "/proc/net/dev";
  76.  
  77.  
  78. %labels = (
  79.         tx=>'tx',
  80.         rx=>'rx',
  81.         r=>'R',
  82.         c=>'C',
  83.         vm=>'m',
  84.         vp=>'p',
  85.         );
  86. =long labels:
  87. %labels = (
  88.         tx=>'Upload',
  89.         rx=>'Download',
  90.         r=>'RAM',
  91.         c=>'CPU',
  92.         vm=>'Master',
  93.         vp=>'PCM',
  94.         );
  95. =cut
  96.  
  97.  
  98.  
  99.  
  100. $tray = Gtk2::TrayIcon->new("allintray");
  101. $eventbox = Gtk2::EventBox->new;
  102. $tooltip= Gtk2::Tooltips->new;
  103. $hbox = Gtk2::HBox->new(0,0);
  104. $tlabel = Gtk2::Label->new;
  105.  
  106.  
  107.  
  108. @bars = sort keys %labels;
  109.  
  110. foreach (@bars) {
  111.     $bar{$_} = Gtk2::ProgressBar->new;
  112.     $bar{$_}->set_orientation('bottom-to-top');
  113.     $bar{$_}->set_text($labels{$_});
  114.     $eventbox{$_} = Gtk2::EventBox->new;
  115.     $hbox->add($eventbox{$_});
  116.     $eventbox{$_}->add($bar{$_});
  117. }
  118.  
  119.  
  120. $eventbox{t} = Gtk2::EventBox->new;
  121. $hbox->add($eventbox{t});
  122. $eventbox{t}->add($tlabel);
  123.  
  124.  
  125. $tray->add($eventbox);
  126. $eventbox->add($hbox);
  127.  
  128. $eventbox->signal_connect( 'button_release_event', \&click );
  129.  
  130.  
  131. $eventbox{vm}->signal_connect( 'button_release_event', \&vm_click );
  132. $eventbox{vm}->signal_connect( 'scroll_event', \&vm_scroll);
  133. $eventbox{vp}->signal_connect( 'button_release_event', \&vp_click );
  134. $eventbox{vp}->signal_connect( 'scroll_event', \&vp_scroll);
  135. $eventbox{r}->signal_connect( 'button_release_event', \&r_click );
  136. $eventbox{c}->signal_connect( 'button_release_event', \&c_click );
  137. $eventbox{t}->signal_connect( 'button_release_event', \&t_click );
  138.  
  139. $tray->show_all;
  140.  
  141.  
  142. open(STAT,$PROCSTAT);
  143. open(NET,$PROCNET);
  144.  
  145.  
  146.  
  147. &update;
  148.  
  149. Glib::Timeout->add($delay, \&update);
  150. Gtk2->main();
  151.  
  152.  
  153.  
  154. ##########################################################################
  155.  
  156. sub click {
  157.     ($check, $event) = @_;
  158.     &popup if (3 eq $event->button)
  159. }
  160.  
  161.  
  162. sub vm_click {
  163.     ($check, $event) = @_;
  164.     &launchMixer if (1 eq $event->button);
  165.     &mute ("vm") if (2 eq $event->button);
  166. }
  167.  
  168. sub vm_scroll {
  169.     ($check, $event) = @_;
  170.     &volup ("Master") if ("up" eq $event->direction);
  171.     &voldown ("Master") if ("down" eq $event->direction);
  172.     &update;
  173. }
  174.  
  175. sub vp_click {
  176.     ($check, $event) = @_;
  177.     &launchMixer if (1 eq $event->button);
  178.     &mute ("vp") if (2 eq $event->button);
  179. }
  180.  
  181. sub vp_scroll {
  182.     ($check, $event) = @_;
  183.     &volup ("PCM") if ("up" eq $event->direction);
  184.     &voldown ("PCM") if ("down" eq $event->direction);
  185.     &update;
  186. }
  187.  
  188. sub t_click {
  189.     ($check, $event) = @_;
  190.     if (1 eq $event->button) { exec $CAL1 unless fork; }
  191.     if (2 eq $event->button) { exec $CAL2 unless fork; }
  192. }
  193.  
  194. sub r_click {
  195.     ($check, $event) = @_;
  196.     if (1 eq $event->button) { exec $MEM unless fork; }
  197. }
  198. sub c_click {
  199.     ($check, $event) = @_;
  200.     if (1 eq $event->button) { exec $PROCMAN unless fork; }
  201. }
  202.  
  203.  
  204.  
  205. ##########################################################################
  206.  
  207. sub launchMixer { exec $MIXER unless fork; }
  208. sub volup { system "amixer set $_[0] 5%+ &> /dev/null"; }
  209. sub voldown { system "amixer set $_[0] 5%- &> /dev/null"; }
  210. sub mute {
  211.     if($bar{$_[0]}->get_text eq "x"){
  212.         $bar{$_[0]}->set_text($was{$_[0]});
  213.     } else {
  214.         $was{$_[0]}=$bar{$_[0]}->get_text;
  215.         $bar{$_[0]}->set_text("x");
  216.     }
  217.     system "amixer set ".($_[0]eq"vm" ? "Master":"PCM")." toggle> /dev/null";
  218. }
  219.  
  220. sub popup {
  221.     $item_factory = Gtk2::ItemFactory->new("Gtk2::Menu", '<main>', undef);
  222.     $popup_menu = $item_factory->get_widget('<main>');
  223.     @menu_items = (
  224.             { path => '/Exit',        item_type => '<Item>', callback => \&out}
  225.             );
  226.  
  227.     $item_factory->create_items(undef, @menu_items);
  228.     $popup_menu->show_all;
  229.     $popup_menu->popup(undef, undef, undef, undef, 0, 0);
  230. }
  231.  
  232.  
  233.  
  234.  
  235. ###########################################################################################
  236.  
  237. sub cpu {
  238.     seek STAT,0,0;
  239.     <STAT> =~ m/^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+).*$/;
  240.  
  241.     $load = $1+$2+$3;
  242.     $total = $load+$4+$5+$6+$7;
  243.  
  244.     unless (defined $lasttotal){ $ret=0; }
  245.     elsif (($total - $lasttotal) <= 0){ $ret=100; } # bez tohohle to obcas zdechne ;)
  246.     else { $ret = int((100 * ($load - $lastload))/($total - $lasttotal)); }
  247.  
  248.     ($lastload, $lasttotal) = ($load,$total);
  249.     return $ret;
  250. }
  251.  
  252. sub net { # return (Receive,Transmit) in kBps
  253.     seek NET,0,0;
  254.     ($rx, $tx) = (grep(/^\s*eth0/,<NET>))[0] =~ m/eth0:\s*(\d+)\s+(?:\d+\s+){7}(\d+)/;
  255.     not defined $lastrx and @ret = (0,0) or @ret = (int(($rx-$lastrx)/1024/($delay/1000)), int(($tx-$lasttx)/1024/($delay/1000)));
  256.     ($lastrx, $lasttx) = ($rx, $tx);
  257.     return (@ret);
  258. }
  259.  
  260.  
  261. sub mem {
  262.     `free` =~ m/Mem:\s*(\d+).*cache:\s+(\d+)/ms;
  263.     return 100*$2/$1;
  264. }
  265.  
  266. sub vol {
  267.     $_ = `amixer`;
  268.     s/\n\s+/ /g;
  269.     return (/'Master'.*\[(\d+)%\]/, /'PCM'.*\[(\d+)%\]/);
  270. }
  271.  
  272.  
  273. sub update {
  274.     $mem=&mem;
  275.     $cpu=&cpu;
  276.     ($mvol,$pvol) = &vol;
  277.     ($nrx, $ntx) = &net;
  278.     ($minute,$hour,$day,$month,$year) = (localtime)[1 .. 5]; $year+=1900; $month++;
  279.  
  280.     $bar{rx}->set_fraction($nrx/$rxmax>1?1:$nrx/$rxmax);
  281.     $bar{tx}->set_fraction($ntx/$txmax>1?1:$ntx/$txmax);
  282.     $bar{r}->set_fraction($mem/100);
  283.     $bar{c}->set_fraction($cpu/100);
  284.     $bar{vm}->set_fraction($mvol/100);
  285.     $bar{vp}->set_fraction($pvol/100);
  286.  
  287.     $tooltip->set_tip($tray,
  288.             sprintf("%d. %d. %d\n%d:%02d\n\nRAM: %d%%\nCPU: %d%%\n\nNet:\nUPL: %d kB/s\nDWN: %d kB/s\n\nVol:\nMaster: %d%%\nPCM: %d%%",
  289.                 $day,$month,$year,$hour,$minute,$mem,$cpu,$ntx,$nrx,$mvol,$pvol));
  290.     $tlabel->set_text(sprintf(" %d:%02d " , $hour,$minute));
  291.     return 1;
  292. }
  293.  
  294.  
  295. ############################
  296. sub out {
  297.     close(STAT);
  298.     close(NET);
  299.     Gtk2->main_quit;
  300. }
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement