Exemplo n.º 1
0
int main(void)
{
	while(1)
	{
		int ret = 0;
		ret = getcpuusage();
	
		printf("....%d .......\n", ret);
		sleep(3);
	}
}
Exemplo n.º 2
0
int main(void) {
	char *status;
	if((status = malloc(STATUS_LENGTH*sizeof(char))) == NULL){
		printf("Cannot allocate memory for status");
		return 0;
	}

	char *datetime = NULL;
	char state_bat[12];
	int bat1, energy_full; // bat2;

	int rem_hours = 0, rem_min = 0, rem_sec = 0;
	long rem_usec = 0;

	char *net_intf, *ip = NULL;

	if((net_intf = malloc(5*sizeof(char))) == NULL){
		printf("Cannot allocate memory for net_intf");
		return 0;
	}

	if((ip = malloc(100*sizeof(char))) == NULL){
		printf("Cannot allocate memory for ip");
		return 0;
	}

	char net_displ[50];

	long left_volume;
	long right_volume;

	int mem_used;
	int mem_available;

	float cpu_avg_freq;

	long usec1, usec2 = 0;
	struct timeval time1, time2;

	int connected = 0;

	int energy1, energy2, dummy = 0;

	int count = 0;

	char *mpc_vol, *mpc_state, *mpc_song = NULL;

	if((mpc_vol = malloc(3*sizeof(char))) == NULL){
		printf("Cannot allocate memory for mpc_vol");
		return 0;
	}

	if((mpc_state = malloc(5*sizeof(char))) == NULL){
		printf("Cannot allocate memory for mpc_state");
		return 0;
	}

	if((mpc_song = malloc(100*sizeof(char))) == NULL){
		printf("Cannot allocate memory for mpc_song");
		return 0;
	}

	if (!(dpy = XOpenDisplay(NULL))) {
		fprintf(stderr, "Cannot open display.\n");
		return 0;
	}

	strcpy(state_bat, "unknown");

	for (;;sleep(1)) {

		bat1 = getbattery(&dummy, &energy_full);

		connected = net(&net_intf, &ip);

		datetime = getdatetime();
		getvolume(&left_volume, &right_volume);

		getmemoryusage(&mem_available, &mem_used);
		getcpuusage(&cpu_avg_freq);

		if (connected == 1) sprintf(net_displ, "%s: %s", net_intf, ip);
		else sprintf(net_displ, "not connected");

		//mpc_stat(&mpc_vol, &mpc_state, &mpc_song);

		if (count == 0) {
			usec1 = time_usec(&time1);
			getbattery(&energy1, &energy_full);
			count++;
		} else if (count >= 5) {

			count = 0;

			usec2 = time_usec(&time2);
			getbattery(&energy2, &energy_full);

			if ((energy2 - energy1) < 0){
				rem_usec = ((float)(usec2-usec1))*(((float)(energy1+energy2))/((float)(2*(energy1-energy2))));
				strcpy(state_bat, "discharging");
			} else if ((energy2 - energy1) > 0) {
				rem_usec = (((float)(usec2-usec1))*((float)energy_full)/((float)(energy2-energy1)))*(1-(((float)(energy2+energy1))/((float)(2*energy_full))));
				strcpy(state_bat, "charging");
			} else if ( (energy2 == energy1) && bat1 >= 100) {
				rem_usec = 0;
				strcpy(state_bat, "charged");
			} else {
				rem_usec = 0;
				strcpy(state_bat, "unknown");
			}

			rem_hours = (int)(rem_usec/(1e6*60*60));
			rem_min = (int)((rem_usec/(1e6*60)) - rem_hours*60);
			rem_sec = (int)((rem_usec/(1e6)) - rem_hours*60*60 - rem_min*60);


			if ((bat1 < 15) && (strcmp(state_bat, "discharging") == 0)) {
				system("mplayer /home/andrea/.local/statusbar/sound/siren.mp3");
			}

		} else {
			count++;
		}

//		snprintf(status, STATUS_LENGTH,  "MPC: status: %s - song: %s | %s | volume left %i - right %i | Battery %d%% (%s %d h %d min %d s) | %s", mpc_state, mpc_song, net_displ, (int)left_volume, (int)right_volume, bat1, state_bat, rem_hours, rem_min, rem_sec, datetime);
		snprintf(status, STATUS_LENGTH,  "CPU (avg) %4.3f MHz | RAM used %d%% available %d%% | %s | volume left %i - right %i | Battery %d%% (%s %d h %d min %d s) | %s", cpu_avg_freq, mem_used, mem_available, net_displ, (int)left_volume, (int)right_volume, bat1, state_bat, rem_hours, rem_min, rem_sec, datetime);

		setstatus(status);
	}

	free(mpc_song);
	free(mpc_state);
	free(mpc_vol);
	free(net_intf);
	free(ip);
	free(datetime);
	free(status);
	XCloseDisplay(dpy);

	return 0;
}