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?
Any suggestins are invited.
With regards.
`Inline Code Example Here
#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();
}
`