GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Oct 26, 2014 12:20 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: battery status applet using gtk+
PostPosted: Sun Jul 01, 2012 8:48 am 
Offline

Joined: Sun Jul 01, 2012 8:40 am
Posts: 1
Hi,
I'm trying to make a simple applet that will display battery status using gtk+ . For that purpose I have written code given below. It continously check for battery status using acpi command and display it. Do you think that I have done things in proper way? I want to modify it so that it will check for battery status in every two second. for that purpose i have used sleep function. But when I use sleep function program don't show gtk window. So what should I do now?
And also please tell me the proper way(I have used a doubtful way in this program :-)) to use an infinite loop in this type programs.
Any suggestins are invited.
With regards.

Code:
    #include<stdio.h>
    #include<string.h>
    #include<gtk/gtk.h>
    //statstics_variable_declaration.
    int charger_status;
    int charge;
    struct time
    {
    int hour;
    int minute;
    int second;
    }remaining_time;
    //Gtk_widget_declaration
    GtkWidget *window;
    GtkWidget *bg;
    GtkWidget *layout;
    GtkWidget *vbox;
    GtkWidget *label[4];
    //Gtk_widget initialization
    void gtk_initialize()
    {
    int i;
    window=gtk_window_new(GTK_WINDOW_POPUP);
    bg=gtk_image_new_from_file("./images/bg.png");
    vbox=gtk_vbox_new(0,0);
    layout=gtk_layout_new(NULL,NULL);
    for(i=0;i<4;i++)
    {
    label[i]=gtk_label_new("test");
    }
    }
    void gtk_signals()
    {
    }
    void gtk_packing()
    {
    int i;
    gtk_container_add(GTK_CONTAINER(window),layout);
    gtk_layout_put(GTK_LAYOUT(layout),bg,0,0);
    gtk_layout_put(GTK_LAYOUT(layout),vbox,0,0);
    for(i=0;i<4;i++)
    {
    gtk_box_pack_start(GTK_BOX(vbox),label[i],1,1,0);
    }
    }
    void display_gtk()
    {
    char text[100];
    if(charger_status==0)
    {
    sprintf(text,"charger-offline\n");
    }
    else
    {
    sprintf(text,"charger-online\n");
    }
    gtk_label_set_text(GTK_LABEL(label[0]),text);
    sprintf(text,"Charge : %d%%",charge);
    gtk_label_set_text(GTK_LABEL(label[1]),text);
    sprintf(text,"%d:%d:%d",remaining_time.hour,remaining_time.minute,remaining_time.second);
    gtk_label_set_text(GTK_LABEL(label[2]),text);
    }
    void decode(char status[][15])
    {
    if(strcmp(status[0],"off-line")==0)
    {
    charger_status=0;
    }
    else
    {
    charger_status=1;
    }
    charge=(int)(status[1][0]-48)*10+(int)(status[1][1]-48);
    remaining_time.hour=(int)(status[2][0]-48)*10+(int)(status[2][1]-48);
    remaining_time.minute=(int)(status[2][3]-48)*10+(int)(status[2][4]-48);
    remaining_time.second=(int)(status[2][6]-48)*10+(int)(status[2][7]-48);
    }
    void display_text()
    {
    if(charger_status==0)
    {
    printf("charger is offline\n");
    }
    else
    {
    printf("charger is online\n");
    }
    printf("Charge on battery is %d%%\n",charge);
    printf("Remaining time is %d hours %d minutes and %d seconds\n",remaining_time.hour,remaining_time.minute,remaining_time.second);
    }
    void read_status()
    {
    FILE *fp;
    char ch;
    char status[4][15];
    int i=0,j=0;
    //execute system command and store result in file
    system("acpi -a|cut -d' ' -f3 > temp");
    system("acpi|cut -d' ' -f4 >> temp");
    system("acpi|cut -d' ' -f5 >> temp");
    // system("acpi|cut -d' ' -f6 > temp");
    //open file and read result
    fp=fopen("temp","r");
    while(fscanf(fp,"%c",&ch)!=EOF)
    {
    if(ch=='\n')
    {
    status[i][j]=0;
    i++;
    j=0;
    }
    else
    {
    status[i][j]=ch;
    j++;
    }
    }
    status[i][j]=0;
    fclose(fp);
    //printf("no issue with reading\n");
    decode(status);
    //printf("no issue with decoding\n");
    //return 0;
    }
    int repeater()
    {
    static int i=0;
    char text[10];
    while(1)
    {
    int j;
    system("clear");
    printf("\n");
    read_status();
    display_text();
    display_gtk();
    for(j=i;j>=0;j--)
    {
    printf("=");
    }
    sprintf(text,"%d",i);
    gtk_label_set_text(GTK_LABEL(label[3]),text);
    i++;
    sleep(2);
    gtk_main_iteration();
    }
    //sleep(2);
    return 1;
    }
    int main(int argc,char *argv[])
    {
    gtk_init(&argc,&argv);
    gtk_initialize();
    gtk_signals();
    gtk_packing();
    //repeater();
    gtk_widget_show_all(window);
    repeater();
    }


Top
 Profile  
 
 Post subject: Re: battery status applet using gtk+
PostPosted: Sun Jul 01, 2012 7:12 pm 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
1)Click on your gtk version here and go to the "Getting Started".
Version 2: http://developer.gnome.org/gtk-tutorial/
Version 3: http://developer.gnome.org/gtk3/

2) Read the tutorials section in Gtkforums.

3)When posting:
a) List your O.S. and version.
b) List your gtk version.

Suggestions:
Replace sleep() with g_timeout_add ()
http://developer.gnome.org/glib/2.31/glib-The-Main-Event-Loop.html#g-timeout-add

Several other of the functions you've used have Glib versions such as: printf, sprintf, fopen
http://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-printf
http://developer.gnome.org/glib/stable/glib-File-Utilities.html#g-fopen


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group