You are not logged in.

#1 2011-04-10 07:26:54

xentalion
Member
From: Canada
Registered: 2010-01-06
Posts: 11
Website

cbatticon - A lightweight GTK+ battery status icon

Hello all!

I've been using various GTK+ battery status icons written in Python and other languages for a while, and they've all served me well. But more recently I've decided to try and fit my OS in as little memory as possible, and that meant that the 15 MBs used to watch my battery had to go. So I wrote a quick status icon in C, which uses libacpi and libnotify. It's a bit of a hack job, but it works and runs in around 3.7 MBs according to the memuse.py script.

Obligatory screenshot:
cbatticon.png

cbatticon is now in the AUR: http://aur.archlinux.org/packages.php?ID=48200

Hope someone finds this useful!

Last edited by xentalion (2011-04-13 01:21:36)

Offline

#2 2011-04-10 14:08:55

pogeymanz
Member
Registered: 2008-03-11
Posts: 1,020

Re: cbatticon - A lightweight GTK+ battery status icon

Does it also tell us when we are plugged in?

Offline

#3 2011-04-10 15:07:26

sargas83
Member
Registered: 2011-02-26
Posts: 9

Re: cbatticon - A lightweight GTK+ battery status icon

Hello,

nice job, this could be a very good replacement for batterymon/batti. The only problem is with the icons. I don't have the gpm-battery-* icons installed, so i get the generic missing icon. It would be nice to have a package for this app in AUR that includes a set of defaults icons.

Thanks for this, finaly a battery monitoring app writen in good all C!

Offline

#4 2011-04-10 18:54:23

sargas83
Member
Registered: 2011-02-26
Posts: 9

Re: cbatticon - A lightweight GTK+ battery status icon

I've made a few changes to the code, for my own use:

1. changed the get_icon_name to work with the icons from batterymon. Those icons look nice with any theme i suppose and they are free for personal use.
2. added some #define lines for battery states and used them instead of plain numbers.
3. added a popup menu with two items: about and quit.
4. added an about dialog.

If you want you can add any of these changes to your code. My code is quite a mess, but anyway, here is the diff file:

28a29,34
> #define BS_MISSING -1
> #define BS_CRITICAL 0
> #define BS_NORMAL 1
> #define BS_CHARGING 2
> #define BS_CHARGED 3
> 
31,36c37,59
< gint battery_state = 1; 
< // -1 - Battery missing
< // 0 - Critical, notified
< // 1 - normal
< // 2 - charging
< // 3 - charged
---
> gint battery_state = BS_NORMAL; 
> 
> gchar* copyright = "Copyright (c) 2011 Colin Jones\n"
>         "Based on code by Matteo Marchesotti\n"
>         "Copyright (c) 2007 Matteo Marchesotti <matteo.marchesotti@fsfe.org>";
> gchar* website = "https://github.com/ColinJones/cbatticon";
> gchar* license = "Copyright (c) 2011 Colin Jones\n"
>         "Based on code by Matteo Marchesotti\n"
>         "Copyright (c) 2007 Matteo Marchesotti <matteo.marchesotti@fsfe.org>\n"
>         "\n"
>         "This program is free software; you can redistribute it and/or modify it\n"
>         "under the terms of the GNU General Public License as published\n"
>         "by the Free Software Foundation; either version 2 of the License, or (at\n"
>         "your option) any later version.\n"
>         "\n"
>         "This program is distributed in the hope that it will be useful, but\n"
>         "WITHOUT ANY WARRANTY; without even the implied warranty of\n"
>         "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
>         "General Public License for more details.\n"
>         "\n"
>         "You should have received a copy of the GNU General Public\n"
>         "License along with this program; if not, write to the Free Software\n"
>         "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.";
44c67
< gchar * get_icon_name(gint state, gint percent, gchar *time);
---
> gchar * get_icon_name(gint state, gint percent);
100c123
<         set_icon(tray_icon, 3, -1, "");
---
>         set_icon(tray_icon, BS_MISSING, -1, "");
108c131
<         if (battery_state != 3)
---
>         if (battery_state != BS_CHARGED)
110,111c133,134
<             battery_state = 3;
<             notify_user(3, binfo->percentage, "");
---
>             battery_state = BS_CHARGED;
>             notify_user(BS_CHARGED, binfo->percentage, "");
113c136
<         set_icon(tray_icon, 3, 100, "");
---
>         set_icon(tray_icon, BS_CHARGED, 100, "");
123c146
<         else if (battery_state != 2)
---
>         else if (battery_state != BS_CHARGING)
125,126c148,149
<             battery_state = 2;
<             notify_user(2, binfo->percentage, get_time(binfo->charge_time));
---
>             battery_state = BS_CHARGING;
>             notify_user(BS_CHARGING, binfo->percentage, get_time(binfo->charge_time));
128c151
<         set_icon(tray_icon, 2, binfo->percentage, get_time(binfo->charge_time));
---
>         set_icon(tray_icon, BS_CHARGING, binfo->percentage, get_time(binfo->charge_time));
133,134c156,157
<         battery_state = 0;
<         notify_user(0, binfo->percentage, get_time(binfo->remaining_time));
---
>         battery_state = BS_CRITICAL;
>         notify_user(BS_CRITICAL, binfo->percentage, get_time(binfo->remaining_time));
136c159
<     set_icon(tray_icon, 1, binfo->percentage,  get_time(binfo->remaining_time));
---
>     set_icon(tray_icon, BS_NORMAL, binfo->percentage,  get_time(binfo->remaining_time));
174a198
> 
177c201
<     g_print("MENU! %i\n", button);
---
>     gtk_menu_popup(GTK_MENU(user_data), NULL, NULL, NULL, NULL, button, activate_time);
184c208
<     if (state == 3)
---
>     if (state == BS_CHARGED)
190c214
<          if (state == 2)
---
>          if (state == BS_CHARGING)
208c232
<     note = notify_notification_new("Battery Monitor", message->str, get_icon_name(state, percent, time));
---
>     note = notify_notification_new("Battery Monitor", message->str, get_icon_name(state, percent));
210c234
<     if (state == 0)
---
>     if (state == BS_NORMAL)
236c260
< gchar * get_icon_name(gint state, gint percent, gchar *time)
---
> gchar * get_icon_name(gint state, gint percent)
239c263
<     filename = g_string_new("gpm-battery-");
---
>     /*filename = g_string_new("gpm-battery-");
260a285,291
>     }*/
> 
>     filename = g_string_new("/usr/share/cbatticon/battery_");
>     
>     if (state == BS_CHARGING)
>     {
>         g_string_append(filename, "charging_");
262a294,310
>     if (percent < 5)
>         g_string_append(filename, "empty");
>     else if (percent < 20)
>         g_string_append(filename, "1");
>     else if (percent < 40)
>         g_string_append(filename, "2");
>     else if (percent < 60)
>         g_string_append(filename, "3");
>     else if (percent < 80)
>         g_string_append(filename, "4");
>     else if (percent < 100)
>         g_string_append(filename, "5");
>     else g_string_append(filename, "full");
>         
>     g_string_append(filename, ".png");
>     
>     
276c324
<     if (state == 2)
---
>     if (state == BS_CHARGING)
280c328
<     else if (state == 3)
---
>     else if (state == BS_CHARGED)
285d332
<     //g_string_append(tooltip, percent);
295,298c342,343
<     //g_print("File: %s\n", filename->str);
<     
<     //gtk_status_icon_set_from_file(icon, filename->str);
<     gtk_status_icon_set_from_icon_name(tray_icon, get_icon_name(state, percent, time));
---
>     gtk_status_icon_set_from_file(tray_icon, get_icon_name(state, percent));
>     //gtk_status_icon_set_from_icon_name(tray_icon, get_icon_name(state, percent, time));
300a346,388
> static GtkAboutDialog* create_about_dlg()
> {
>     GtkAboutDialog* abt_dlg;
> 
>     abt_dlg = GTK_ABOUT_DIALOG(gtk_about_dialog_new());
>     gtk_about_dialog_set_program_name(abt_dlg,"cbatticon");
>     gtk_about_dialog_set_version(abt_dlg,"alpha");
>     gtk_about_dialog_set_copyright(abt_dlg, copyright);
>     gtk_about_dialog_set_website(abt_dlg,website);
>     gtk_about_dialog_set_license(abt_dlg, license);
> 
>     return abt_dlg;
> }
> 
> void run_abt_dlg(GtkWidget* widget, gpointer dlg)
> {
>     gtk_dialog_run(dlg);
>     gtk_widget_hide(dlg);
> }
> 
> static GtkWidget* create_menu()
> {
>     GtkWidget *menu;
>     GtkWidget *menu_about;
>     GtkWidget *menu_quit;
>     GtkAboutDialog *abt_dlg;
>     
>     abt_dlg = create_about_dlg();
>     menu_about = gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT, NULL);
>     g_signal_connect(G_OBJECT(menu_about),"activate",G_CALLBACK(run_abt_dlg),abt_dlg);
> 
>     menu_quit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
>     g_signal_connect(G_OBJECT(menu_quit),"activate", G_CALLBACK(gtk_main_quit), NULL);
> 
>     menu = gtk_menu_new();
>     gtk_menu_append(menu,menu_about);
>     gtk_menu_append(menu,menu_quit);
> 
>     gtk_widget_show(menu_about);
>     gtk_widget_show(menu_quit);
> 
>     return menu;
> }
305a394
>     GtkWidget *menu;
307a397
>     menu = create_menu();
310c400
<     g_signal_connect(G_OBJECT(tray_icon), "popup-menu", G_CALLBACK(tray_icon_on_menu), NULL);
---
>     g_signal_connect(G_OBJECT(tray_icon), "popup-menu", G_CALLBACK(tray_icon_on_menu), menu);

Cheers and thanks again for this!

Offline

#5 2011-04-10 19:30:54

xentalion
Member
From: Canada
Registered: 2010-01-06
Posts: 11
Website

Re: cbatticon - A lightweight GTK+ battery status icon

pogeymanz: Yes, it'll tell you if you're charging or charged (and the icon will change to reflect this)

sargas83: I've just updated the git repo to fix the lack of  constants (I used an enum instead of #defines), and made it use the GNOME battery icons packaged in the GNOME theme. Unfortunately Faenza doesn't follow this. Could you tell me what icon theme  you're using, so I can see if there's overlap?

Glad you find it useful! smile

Offline

#6 2011-04-11 16:24:26

sargas83
Member
Registered: 2011-02-26
Posts: 9

Re: cbatticon - A lightweight GTK+ battery status icon

I have now only the default hicolor theme, which doesn't seem to have any icon for batteries (i might be wrong). Anyway, i made a quick fix: took the icons from batterymon and changed your code to use those instead. Works great.

The only problem i have (with both the original and my modified version) is that when the battery is charging the remaining time is allways zero, but that appears to be a problem with my battery. Issuing an acpi -V while charging shows a few seconds remaining. This is annoying so i removed the remaining time from the tooltip when charging.

While looking into decreasing the interval from 5 seconds to one second I found this:
http://developer.gnome.org/glib/unstabl … imeout-add
It says that the g_timeout_add_seconds function allows for more efficient power usage, but gives no other proof of this. Do you think it could be better to use this function?

Anyway, right now cbatticon is the best battery monitor for my fluxbox setup, because it's so easy to modify for my own needs.

Offline

#7 2011-04-13 01:31:50

xentalion
Member
From: Canada
Registered: 2010-01-06
Posts: 11
Website

Re: cbatticon - A lightweight GTK+ battery status icon

sargas83: I took a look at what was written about g_timeout_add_seconds() and agree that it's probably better. I've also reduced the polling time to 2 seconds from 5, and I've put the package up on the AUR (http://aur.archlinux.org/packages.php?ID=48200). This'll make installing your patches a bit easier! smile

I'll do something about the lack of icons after I finish my exams.

Offline

#8 2011-10-25 17:46:25

dobedo
Member
From: Belgium
Registered: 2008-10-04
Posts: 113

Re: cbatticon - A lightweight GTK+ battery status icon

As agreed with Xentalion, the deprecated libacpi has been replaced by libudev to read /sys/class/power_supply information.
The PKGBUILD will soon be adapted by xentalion to replace libacpi by udev.

There is still a problem I have to look at: it can take a bit of time to have the refreshed info in /sys/... so the value is not always accurate when the notification is sent.

Offline

#9 2011-10-25 18:23:10

Mad Fish
Member
Registered: 2009-09-22
Posts: 295

Re: cbatticon - A lightweight GTK+ battery status icon

Shouldn't it be using upower?

Offline

#10 2011-10-25 18:42:21

dobedo
Member
From: Belgium
Registered: 2008-10-04
Posts: 113

Re: cbatticon - A lightweight GTK+ battery status icon

Well, upower is a(nother) way to get the information. Upower uses udev.
Here, cbatticon get the info directly via udev.

Offline

#11 2011-10-25 18:59:45

Mad Fish
Member
Registered: 2009-09-22
Posts: 295

Re: cbatticon - A lightweight GTK+ battery status icon

dobedo wrote:

Well, upower is a(nother) way to get the information. Upower uses udev.
Here, cbatticon get the info directly via udev.

I think (correct me if I'm wrong), that the point of upower was to have a single daemon that polls that kind of information, instead of every interested process.

Offline

#12 2011-10-25 19:12:17

dobedo
Member
From: Belgium
Registered: 2008-10-04
Posts: 113

Re: cbatticon - A lightweight GTK+ battery status icon

Probably upower polls the info and send it to every process registered via dbus, but how many processes do really need the battery info?
For that kind of info I would have preferred a lib instead of a daemon.

Offline

#13 2011-10-25 19:18:54

dobedo
Member
From: Belgium
Registered: 2008-10-04
Posts: 113

Re: cbatticon - A lightweight GTK+ battery status icon

Finally, upower does much more than simply querying the battery info. And those extra features (and dependencies) are not needed by cbatticon.

Last edited by dobedo (2011-10-25 19:19:59)

Offline

#14 2011-10-25 21:55:19

Mad Fish
Member
Registered: 2009-09-22
Posts: 295

Re: cbatticon - A lightweight GTK+ battery status icon

Also, doesn't integrate well with Faenza theme. I suggest using gpm-primary-* icons if available (they also offer more precision than battery-* icons).

Offline

#15 2011-10-30 19:34:46

Queseuq
Member
Registered: 2010-11-28
Posts: 18

Re: cbatticon - A lightweight GTK+ battery status icon

I'm still getting a no battery device found error after updating cbatticon. In /sys/class/power_supply there is ACAD and BAT1. Any idea what's wrong, or anything else I should check?

Offline

#16 2011-10-31 10:50:08

dobedo
Member
From: Belgium
Registered: 2008-10-04
Posts: 113

Re: cbatticon - A lightweight GTK+ battery status icon

Ha, shit!

The new version takes as assumption that the first battery is named BAT0...which is not true in your case.
Please, try calling it like this:

cbatticon 1

or

cbatticon BAT1

Whatever you put as (first and only) argument to cbatticon will be used as suffix of the battery name.
This should be documented.

Edit: this is a bug in fact. What I wanted to do is that cbatticon uses the first battery it finds when nothing is passed as argument, otherwise it should uses the argument as suffix of the battery name. This is the bug:

get_battery (argc > 1 ? argv[1] : "0");

it should be NULL i.o. "0". I will fix this tonight.

Edit 2: bug is fixed.

Last edited by dobedo (2011-10-31 17:57:09)

Offline

#17 2012-01-03 05:34:08

jord4n313
Member
Registered: 2011-10-24
Posts: 2

Re: cbatticon - A lightweight GTK+ battery status icon

sargas83 wrote:

I have now only the default hicolor theme, which doesn't seem to have any icon for batteries (i might be wrong). Anyway, i made a quick fix: took the icons from batterymon and changed your code to use those instead. Works great.

The only problem i have (with both the original and my modified version) is that when the battery is charging the remaining time is allways zero, but that appears to be a problem with my battery. Issuing an acpi -V while charging shows a few seconds remaining. This is annoying so i removed the remaining time from the tooltip when charging.

While looking into decreasing the interval from 5 seconds to one second I found this:
http://developer.gnome.org/glib/unstabl … imeout-add
It says that the g_timeout_add_seconds function allows for more efficient power usage, but gives no other proof of this. Do you think it could be better to use this function?

Anyway, right now cbatticon is the best battery monitor for my fluxbox setup, because it's so easy to modify for my own needs.

im trying to get cbatticon working with faenza icon set, can u share ur patches? thanks

Offline

#18 2012-04-18 20:06:10

elliott
Member
Registered: 2006-03-07
Posts: 296

Re: cbatticon - A lightweight GTK+ battery status icon

I have the Faenza icons too, any tips on making cbatticon use them?

Offline

#19 2012-04-19 07:18:56

valr
Member
From: Belgium
Registered: 2012-01-08
Posts: 62

Re: cbatticon - A lightweight GTK+ battery status icon

elliott wrote:

I have the Faenza icons too, any tips on making cbatticon use them?

I think I've found the correct way to make the faenza theme work. I'll adapt cbatticon code and test it and post here when the github code will be ready (in a couple of hours when I'm back home).

Last edited by valr (2012-04-19 07:20:15)

Offline

#20 2012-04-19 19:14:07

valr
Member
From: Belgium
Registered: 2012-01-08
Posts: 62

Re: cbatticon - A lightweight GTK+ battery status icon

Hi all,

cbatticon has just been changed in github.
New options are available:

Usage:
  cbatticon [OPTION...] [BATTERY ID]

Options:
  -h, --help                Show help options
  -u, --update-interval     Set update interval (in seconds)
  -i, --icon-type           Set icon type ('standard', 'notification' or 'symbolic')
  -t, --list-icon-types     List available icon types
  -b, --list-batteries      List available batteries

Example:
  cbatticon
  cbatticon -t
  cbatticon -b
  cbatticon -u 5 -i symbolic

Defaults:
  If you call cbatticon without any parameter, it will use these defaults:

  update interval : 5 seconds
  icon type       : the first one available (search is done in this sequence : standard, notification and symbolic)
  battery id      : the first one that is reported by udev

Don't hesitate to post feedbacks, bugs, etc...
Thanks

Last edited by valr (2012-04-19 19:18:33)

Offline

#21 2012-04-19 19:24:13

elliott
Member
Registered: 2006-03-07
Posts: 296

Re: cbatticon - A lightweight GTK+ battery status icon

That works great, thank you. I finally have a battery tray icon, I've been doing without one for months because I hadn't found one I liked.

Offline

#22 2012-04-19 19:33:06

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 816

Re: cbatticon - A lightweight GTK+ battery status icon

Hey good work!
But only a little question: Doesn't update-interval too low makes notebooks wake up too often? My hd will always be on, won't sleep, even if i'm not using it. Or am i mistaking?
Thanks!

Offline

#23 2012-04-19 19:47:39

valr
Member
From: Belgium
Registered: 2012-01-08
Posts: 62

Re: cbatticon - A lightweight GTK+ battery status icon

nierro wrote:

Doesn't update-interval too low makes notebooks wake up too often? My hd will always be on, won't sleep, even if i'm not using it. Or am i mistaking?

If you find that 5 seconds is too low, feel free to change the update interval with option -u (or --update-interval).
Regarding hdd, I don't think it will be used as sysfs is not a filesystem on disk but in memory (correct me if I'm wrong).

Offline

#24 2012-04-20 16:50:49

nierro
Member
From: Milan, Italy
Registered: 2011-09-02
Posts: 816

Re: cbatticon - A lightweight GTK+ battery status icon

I don't know big_smile
Ok, then, i trust you!
Thanks again for the good work and for the quick reply!

Offline

#25 2012-09-21 03:21:12

ecolinux
Member
Registered: 2012-09-21
Posts: 1

Re: cbatticon - A lightweight GTK+ battery status icon

hello...
cbatticon seem good but i don't know how can display icons....I have icons but I don't know where i put them.  we must change the code cbatticon?
thank you....

Offline

Board footer

Powered by FluxBB