[ale] Gnome Applets

Christopher Fowler cfowler at outpostsentinel.com
Fri Aug 1 17:26:40 EDT 2003




Does anyone have a quick and dirty guide to writing gnome applets?
I need to create a small ACPI compatible battery monitor for my laptop.
Below is C code that gets status. It seems the gnome battery applet only 
supports APM.  My laptop does not use APM.

------------------------------------------------------------

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>

/*
 * Function returns the next battery
 * number
 */
static int
getBattery(void) {
    static DIR *bInfo = NULL;
    struct dirent *bs;
    
    if(bInfo == NULL) {
        if((bInfo = opendir("/proc/acpi/battery/")) == NULL) {
            return -1;
        }
    }
    
    while((bs = readdir(bInfo)) != NULL) {
        if(isdigit(*bs->d_name)) {
            return atoi(bs->d_name);
        }
    }

    closedir(bInfo);
    bInfo = NULL; 
    return -2;
}


/*
 * Main Function for program
 *
 * Currently takes no arguments
 *
 */
int
main(int argc, char **argv) {
    int store = 0;
    int cur = 0;
    int interval = 0;
    int bat;
    int found = 0;

    while((bat = getBattery()) >= 0) {
        FILE *fp;
        found = 1;

        if ((fp = fopen("/proc/acpi/battery/0/info", "r")) != NULL) {
            char buf[100] ;
            int i ;
            while (fgets(buf, sizeof(buf), fp) != NULL) {
                if(sscanf(buf, "Design Capacity:         %d mAh\n",
                    &i) == 1) {
                    store = i;
                    break ;
                }
            }
            fclose(fp) ;
        } else {
            perror("Get Batter Status");
            exit(1);
        }

        if ((fp = fopen("/proc/acpi/battery/0/status", "r")) != NULL) {
            char buf[100] ;
            int i ;
            while (fgets(buf, sizeof(buf), fp) != NULL) {
                if(sscanf(buf, "Remaining Capacity:         %d mAh\n",
                    &i) == 1) {
                    cur = i;
                    break ;
                }
            }
            fclose(fp) ;
        } else {
            perror("Get Battery Status");
            exit(1);
        }

        /*
         * If we do not know capacity we can not do
         * to calculations.
         */
        if(store == 0) {
            printf("Failed to get batter status\n");
            exit(1);
        }

        interval = store / 100;
        printf("Battery #%d Remaining: %d\%\n", (bat + 1) , (cur / interval));
    }

    if(bat == -1) {
    } else if(bat == -2 && found == 0) {
        fprintf(stderr, "ACPI loaded but no batteries found!\n");
        exit(1);
    }


    return 0;
}

/* vi: set ts=4 sw=4 :*/

------------------------------------------------------------

Thanks,
Chris
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale





More information about the Ale mailing list