You are not logged in.
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 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
Does it also tell us when we are plugged in?
Offline
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
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
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!
Offline
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
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!
I'll do something about the lack of icons after I finish my exams.
Offline
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
Shouldn't it be using upower?
Offline
Well, upower is a(nother) way to get the information. Upower uses udev.
Here, cbatticon get the info directly via udev.
Offline
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
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
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
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
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
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
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
I have the Faenza icons too, any tips on making cbatticon use them?
Offline
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
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
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
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
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
I don't know
Ok, then, i trust you!
Thanks again for the good work and for the quick reply!
Offline
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