Initial revision
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
include ../../Makefile.config
|
||||
|
||||
########################################################################
|
||||
# You shouldn't need to touch anything below here.
|
||||
########################################################################
|
||||
|
||||
TARGET = lcdproc
|
||||
OBJ = main.o mode.o batt.o chrono.o cpu.o disk.o load.o mem.o
|
||||
LIB += -L../../shared -lLCDstuff
|
||||
|
||||
|
||||
###################################################################
|
||||
# Compilation...
|
||||
#
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJ) Makefile
|
||||
$(GCC) -s $(MISC) -o $(TARGET) $(OBJ) $(LIB)
|
||||
|
||||
%.o: %.c %.h Makefile
|
||||
$(GCC) -c $(MISC) $<
|
||||
|
||||
|
||||
##################################################################
|
||||
# Installation
|
||||
#
|
||||
install: $(TARGET)
|
||||
@echo ... lcdproc main client ...
|
||||
install -m 0755 -o root -g root $(TARGET) /usr/local/bin/
|
||||
|
||||
|
||||
##################################################################
|
||||
# Other stuff...
|
||||
#
|
||||
clean:
|
||||
rm -f $(OBJ) $(TARGET) *~ core
|
||||
|
||||
edit:
|
||||
emacs . &
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#include "../../shared/sockets.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "mode.h"
|
||||
#include "batt.h"
|
||||
|
||||
int batt_fd = 0;
|
||||
|
||||
|
||||
static int get_batt_stat(int *acstat, int *battstat,
|
||||
int *battflag, int *percent);
|
||||
|
||||
static int get_batt_stat(int *acstat, int *battstat,
|
||||
int *battflag, int *percent)
|
||||
{
|
||||
char str[64];
|
||||
|
||||
if(!batt_fd)
|
||||
batt_fd = open("/proc/apm", O_RDONLY);
|
||||
if(batt_fd <= 0) return -1;
|
||||
|
||||
if(lseek(batt_fd, 0, 0) != 0) return -1;
|
||||
|
||||
if(read(batt_fd, str, sizeof(str)-1) < 0) return -1;
|
||||
|
||||
if(3 > sscanf(str+13, "0x%x 0x%x 0x%x %d",
|
||||
acstat, battstat, battflag, percent))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int batt_init()
|
||||
{
|
||||
static int first = 1;
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
sock_send_string(sock, "screen_add B\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int batt_close()
|
||||
{
|
||||
if(batt_fd) close(batt_fd);
|
||||
batt_fd = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Battery Screen shows apm battery status...
|
||||
//
|
||||
|
||||
//####################
|
||||
//## Battery: 100% ###
|
||||
//AC: Unknown
|
||||
//Batt: Low (Charging)
|
||||
//E------------------F
|
||||
|
||||
int battery_screen(int rep, int display)
|
||||
{
|
||||
static int first=1;
|
||||
|
||||
int acstat=0, battstat=0, battflag=0, percent=0;
|
||||
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sprintf(buffer, "screen_set B name {APM stats: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add B title title\n");
|
||||
sprintf(tmp, "widget_set B title {LCDPROC %s}\n", version);
|
||||
sock_send_string(sock, tmp);
|
||||
sock_send_string(sock, "widget_add B one string\n");
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sock_send_string(sock, "widget_add B two string\n");
|
||||
sock_send_string(sock, "widget_add B three string\n");
|
||||
sock_send_string(sock, "widget_add B gauge hbar\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set B one 1 2 {AC: Unknown}\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set B two 1 3 {Batt: Unknown}\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set B three 1 4 {E F}\n");
|
||||
sock_send_string(sock, "widget_set B gauge 2 4 0\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Only run once every 16 frames...
|
||||
//if(rep&0x0f) return 0;
|
||||
|
||||
get_batt_stat(&acstat, &battstat, &battflag, &percent);
|
||||
|
||||
|
||||
if(percent >= 0) sprintf(buffer, "%i%%", percent);
|
||||
else sprintf(buffer, "??%%");
|
||||
sprintf(tmp, "widget_set B title {Batt: %s: %s}\n", buffer, host);
|
||||
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
if(lcd_hgt >= 4) // 4-line version of the screen
|
||||
{
|
||||
sprintf(tmp, "widget_set B one 1 2 {");
|
||||
switch(acstat)
|
||||
{
|
||||
case 0: sprintf(tmp+strlen(tmp), "AC: Off}\n");
|
||||
break;
|
||||
case 1: sprintf(tmp+strlen(tmp), "AC: On}\n");
|
||||
break;
|
||||
case 2: sprintf(tmp+strlen(tmp), "AC: Backup}\n");
|
||||
break;
|
||||
default: sprintf(tmp+strlen(tmp), "AC: Unknown}\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
|
||||
sprintf(tmp, "widget_set B two 1 3 {");
|
||||
if(battflag == 0xff)
|
||||
{
|
||||
sprintf(tmp+strlen(tmp), "Battery Stat Unknown");
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(tmp+strlen(tmp), "Batt:");
|
||||
if(battflag & 1) sprintf(tmp+strlen(tmp), " High");
|
||||
if(battflag & 2) sprintf(tmp+strlen(tmp), " Low");
|
||||
if(battflag & 4) sprintf(tmp+strlen(tmp), " Critical");
|
||||
if(battflag & 8
|
||||
||battstat == 3) sprintf(tmp+strlen(tmp), " Charging");
|
||||
if(battflag & 128) sprintf(tmp+strlen(tmp), " (NONE)");
|
||||
}
|
||||
sprintf(tmp+strlen(tmp), "}\n");
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
if(percent > 0)
|
||||
{
|
||||
sprintf(tmp, "widget_set B gauge 2 4 %i\n",
|
||||
(percent * ((lcd_wid-2)*lcd_cellwid)/100));
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
}
|
||||
} // end if(lcd_hgt >= 4)
|
||||
else // two-line version of the screen
|
||||
{
|
||||
sprintf(tmp, "widget_set B one 1 2 {");
|
||||
if(acstat == 1)
|
||||
sprintf(tmp+strlen(tmp), "AC, ");
|
||||
|
||||
if(battflag == 0xff)
|
||||
{
|
||||
sprintf(tmp+strlen(tmp), "No battery???");
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(tmp+strlen(tmp), "Batt:");
|
||||
if(battflag & 1) sprintf(tmp+strlen(tmp), " High");
|
||||
if(battflag & 2) sprintf(tmp+strlen(tmp), " Low");
|
||||
if(battflag & 4) sprintf(tmp+strlen(tmp), " Critical");
|
||||
if(battflag & 8
|
||||
||battstat == 3) sprintf(tmp+strlen(tmp), " Charging");
|
||||
if(battflag & 128) sprintf(tmp+strlen(tmp), " (NONE)");
|
||||
}
|
||||
sprintf(tmp+strlen(tmp), "}\n");
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef BATT_H
|
||||
#define BATT_H
|
||||
|
||||
int batt_init();
|
||||
int batt_close();
|
||||
|
||||
int battery_screen(int rep, int display);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,566 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "../../shared/sockets.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "mode.h"
|
||||
#include "chrono.h"
|
||||
|
||||
int TwentyFourHour=1;
|
||||
int uptime_fd = 0;
|
||||
|
||||
static char kver[SYS_NMLN];
|
||||
static char sysname[SYS_NMLN];
|
||||
|
||||
static double get_uptime(double *up, double *idle);
|
||||
|
||||
|
||||
static double get_uptime(double *up, double *idle)
|
||||
{
|
||||
double uptime;
|
||||
|
||||
reread(uptime_fd, "get_uptime:");
|
||||
sscanf(buffer, "%lf %lf", &uptime, idle);
|
||||
|
||||
*up = uptime;
|
||||
|
||||
return uptime;
|
||||
}
|
||||
|
||||
|
||||
// A couple of tables for later use...
|
||||
static char *days[] = {
|
||||
"Sunday, ",
|
||||
"Monday, ",
|
||||
"Tuesday, ",
|
||||
"Wednesday,",
|
||||
"Thursday, ",
|
||||
"Friday, ",
|
||||
"Saturday, ",
|
||||
};
|
||||
static char *shortdays[] = {
|
||||
"Sun",
|
||||
"Mon",
|
||||
"Tue",
|
||||
"Wed",
|
||||
"Thu",
|
||||
"Fri",
|
||||
"Sat",
|
||||
};
|
||||
|
||||
static char *months[] = {
|
||||
" January",
|
||||
" February",
|
||||
" March",
|
||||
" April",
|
||||
" May",
|
||||
" June",
|
||||
" July",
|
||||
" August",
|
||||
"September",
|
||||
" October",
|
||||
" November",
|
||||
" December",
|
||||
};
|
||||
static char *shortmonths[] = {
|
||||
"Jan",
|
||||
"Feb",
|
||||
"Mar",
|
||||
"Apr",
|
||||
"May",
|
||||
"Jun",
|
||||
"Jul",
|
||||
"Aug",
|
||||
"Sep",
|
||||
"Oct",
|
||||
"Nov",
|
||||
"Dec",
|
||||
};
|
||||
|
||||
|
||||
|
||||
int chrono_init()
|
||||
{
|
||||
struct utsname *unamebuf;
|
||||
|
||||
if(!uptime_fd)
|
||||
{
|
||||
unamebuf = (struct utsname *) malloc( sizeof(struct utsname) );
|
||||
uptime_fd = open("/proc/uptime",O_RDONLY);
|
||||
|
||||
#if 0
|
||||
kversion_fd = open("/proc/sys/kernel/osrelease",O_RDONLY);
|
||||
|
||||
reread(kversion_fd, "main:");
|
||||
sscanf(buffer, "%s", kver);
|
||||
|
||||
close(kversion_fd);
|
||||
# endif
|
||||
|
||||
/* Get OS name and version from uname() */
|
||||
if( uname( unamebuf ) != 0 ) {
|
||||
perror( "Error calling uname:" );
|
||||
}
|
||||
strcpy( kver, unamebuf->release );
|
||||
strcpy( sysname, unamebuf->sysname );
|
||||
|
||||
free(unamebuf);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int chrono_close()
|
||||
{
|
||||
if(uptime_fd)
|
||||
close(uptime_fd);
|
||||
|
||||
uptime_fd = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Time Screen displays current time and date, uptime, OS ver...
|
||||
//
|
||||
//+--------------------+
|
||||
//|## Linux 2.0.33 ###@|
|
||||
//|Up xxx days hh:mm:ss|
|
||||
//| Wed May 17, 1998 |
|
||||
//|11:32:57a 100% idle|
|
||||
//+--------------------+
|
||||
//
|
||||
|
||||
int time_screen(int rep, int display)
|
||||
{
|
||||
char hr[8], min[8], sec[8], ampm[8];
|
||||
char day[16], month[16];
|
||||
static int first = 1;
|
||||
static int colons = 0;
|
||||
time_t thetime;
|
||||
struct tm *rtime;
|
||||
double uptime, idle;
|
||||
int i = 0;
|
||||
char colon;
|
||||
|
||||
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sock_send_string(sock, "screen_add T\n");
|
||||
sprintf(buffer, "screen_set T name {Time Screen: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add T title title\n");
|
||||
sock_send_string(sock, "widget_set T title {Time Screen}\n");
|
||||
sock_send_string(sock, "widget_add T one string\n");
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sock_send_string(sock, "widget_add T two string\n");
|
||||
sock_send_string(sock, "widget_add T three string\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(buffer, "widget_set T title {TIME: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(colons)
|
||||
colon = ':';
|
||||
else
|
||||
colon = ' ';
|
||||
|
||||
|
||||
time(&thetime);
|
||||
rtime = localtime(&thetime);
|
||||
|
||||
|
||||
if(TwentyFourHour)
|
||||
{
|
||||
sprintf(hr, "%02d", rtime->tm_hour);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rtime->tm_hour > 12)
|
||||
{
|
||||
i = 1; sprintf(hr, "%02d", (rtime->tm_hour - 12));
|
||||
}
|
||||
else if(rtime->tm_hour == 0)
|
||||
{
|
||||
i = 0; sprintf(hr, "12");
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0; sprintf(hr, "%02d", rtime->tm_hour);
|
||||
}
|
||||
}
|
||||
if(rtime->tm_hour >= 12) sprintf(ampm, "%s", "P");
|
||||
else sprintf(ampm, "%s", "A");
|
||||
|
||||
sprintf(min, "%02d", rtime->tm_min);
|
||||
sprintf(sec, "%02d", rtime->tm_sec);
|
||||
|
||||
|
||||
strcpy(day, shortdays[rtime->tm_wday]);
|
||||
strcpy(month, shortmonths[rtime->tm_mon]);
|
||||
|
||||
|
||||
|
||||
///////////////////// Write the title bar (os name and version)
|
||||
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sprintf(tmp, "widget_set T title {");
|
||||
sprintf(tmp+strlen(tmp), "%s %s: %s}\n",
|
||||
sysname, kver, host);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////// Display the time...
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
get_uptime(&uptime, &idle);
|
||||
idle = (idle * 100) / uptime;
|
||||
|
||||
|
||||
if(TwentyFourHour)
|
||||
sprintf(tmp, "%s%c%s%c%s %2i%% idle",
|
||||
hr, colon, min, colon, sec, (int)idle);
|
||||
else
|
||||
sprintf(tmp, "%s%c%s%c%s%s %2i%% idle",
|
||||
hr, colon, min, colon, sec, ampm, (int)idle);
|
||||
// Center the output line...
|
||||
i = ((lcd_wid - strlen(tmp)) / 2) + 1;
|
||||
sprintf(buffer, "widget_set T three %i 4 {%s}\n", i, tmp);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
|
||||
|
||||
sprintf(tmp, "%s %s %d, %d", day, month,
|
||||
rtime->tm_mday, (rtime->tm_year + 1900));
|
||||
sprintf(buffer, "widget_set T two 3 3 {%s}\n", tmp);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
|
||||
/////////////////////// Display the uptime...
|
||||
i = (int)uptime / 86400;
|
||||
sprintf(day, "Up %d day%s,", i, (i != 1 ? "s" : ""));
|
||||
i = ((int)uptime % 86400) / 60 / 60;
|
||||
sprintf(hr, "%02i",i);
|
||||
i = (((int)uptime % 86400) % 3600) / 60;
|
||||
sprintf(min, "%02i",i);
|
||||
i = ((int)uptime % 60);
|
||||
sprintf(sec, "%02i",i);
|
||||
if (colons)
|
||||
sprintf(tmp, "%s %s:%s:%s", day, hr, min, sec);
|
||||
else
|
||||
sprintf(tmp, "%s %s %s %s", day, hr, min, sec);
|
||||
//// Center this line automatically...
|
||||
//i = ((lcd_wid - strlen(tmp)) / 2) + 1;
|
||||
sprintf(buffer, "widget_set T one 1 2 {%s}\n", tmp);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
|
||||
}
|
||||
else // 20x2 version of the screen
|
||||
{
|
||||
sprintf(tmp, "widget_set T one 1 2 {");
|
||||
if(lcd_wid >= 20)
|
||||
sprintf(tmp+strlen(tmp), "%d-%02d-%02d ",
|
||||
rtime->tm_year+1900, rtime->tm_mon+1, rtime->tm_mday);
|
||||
else // 16x2 version...
|
||||
sprintf(tmp+strlen(tmp), "%02d/%02d ",
|
||||
rtime->tm_mon+1, rtime->tm_mday);
|
||||
sprintf(tmp+strlen(tmp), "%s%c%s%c%s", hr, colon, min, colon, sec);
|
||||
if(! TwentyFourHour)
|
||||
sprintf(tmp+strlen(tmp), "%s", ampm);
|
||||
sprintf(tmp+strlen(tmp), "}\n");
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
colons = colons ^ 1;
|
||||
|
||||
return 0;
|
||||
} // End time_screen()
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Clock Screen displays current time and date...
|
||||
//
|
||||
int clock_screen(int rep, int display)
|
||||
{
|
||||
char hr[8], min[8], sec[8], ampm[8];
|
||||
char day[16], month[16];
|
||||
static int first = 1;
|
||||
static int colons = 0;
|
||||
time_t thetime;
|
||||
struct tm *rtime;
|
||||
int i=0;
|
||||
char colon;
|
||||
|
||||
if(lcd_hgt < 4) return 0;
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sock_send_string(sock, "screen_add O\n");
|
||||
sprintf(buffer, "screen_set O name {Clock Screen: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add O title title\n");
|
||||
sock_send_string(sock, "widget_add O one string\n");
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sock_send_string(sock, "widget_set O title {DATE & TIME}\n");
|
||||
sock_send_string(sock, "widget_add O two string\n");
|
||||
sock_send_string(sock, "widget_add O three string\n");
|
||||
sprintf(buffer, "widget_set O one 3 2 {%s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(buffer, "widget_set O title {TIME: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(colons)
|
||||
colon = ':';
|
||||
else
|
||||
colon = ' ';
|
||||
|
||||
time(&thetime);
|
||||
rtime = localtime(&thetime);
|
||||
|
||||
strcpy(day, days[rtime->tm_wday]);
|
||||
|
||||
if(TwentyFourHour)
|
||||
{
|
||||
sprintf(hr, "%02d", rtime->tm_hour);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rtime->tm_hour > 12)
|
||||
{
|
||||
i = 1; sprintf(hr, "%02d", (rtime->tm_hour - 12));
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0; sprintf(hr, "%02d", rtime->tm_hour);
|
||||
}
|
||||
}
|
||||
if(rtime->tm_hour == 12) i=1;
|
||||
sprintf(min, "%02d", rtime->tm_min);
|
||||
sprintf(sec, "%02d", rtime->tm_sec);
|
||||
if (i == 1) { sprintf(ampm, "%s", "P"); }
|
||||
else { sprintf(ampm, "%s", "A"); }
|
||||
strcpy(month, months[rtime->tm_mon]);
|
||||
|
||||
|
||||
if(lcd_hgt >= 4) // 4-line version of the screen
|
||||
{
|
||||
sprintf(tmp, "widget_set O two 1 3 {");
|
||||
if(TwentyFourHour)
|
||||
sprintf(tmp+strlen(tmp), "%s%c%s%c%s %s",
|
||||
hr, colon, min, colon, sec, day);
|
||||
else
|
||||
sprintf(tmp+strlen(tmp), "%s%c%s%c%s%s %s",
|
||||
hr, colon, min, colon, sec, ampm, day);
|
||||
sprintf(tmp+strlen(tmp), "}\n");
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
sprintf(tmp, "widget_set O three 2 4 {");
|
||||
sprintf(tmp+strlen(tmp),
|
||||
"%s %d, %d",
|
||||
month,
|
||||
rtime->tm_mday,
|
||||
(rtime->tm_year + 1900));
|
||||
sprintf(tmp+strlen(tmp), "}\n");
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
} // end if(lcd_hgt >= 4)
|
||||
else // 20x2 version of the screen
|
||||
{
|
||||
sprintf(tmp, "widget_set O one 1 2 {");
|
||||
sprintf(tmp+strlen(tmp), "%d-%02d-%02d ",
|
||||
rtime->tm_year+1900, rtime->tm_mon+1, rtime->tm_mday);
|
||||
sprintf(tmp+strlen(tmp), "%s%c%s%c%s", hr, colon, min, colon, sec);
|
||||
if(! TwentyFourHour)
|
||||
sprintf(tmp+strlen(tmp), "%s", ampm);
|
||||
sprintf(tmp+strlen(tmp), "}\n");
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
}
|
||||
|
||||
colons = colons ^ 1;
|
||||
|
||||
return 0;
|
||||
} // End clock_screen()
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Uptime Screen shows info about system uptime and OS version
|
||||
//
|
||||
int uptime_screen(int rep, int display)
|
||||
{
|
||||
int i;
|
||||
char date[16], hour[8], min[8], sec[8];
|
||||
double uptime, idle;
|
||||
static int first = 1;
|
||||
static int colons = 0;
|
||||
char colon;
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sock_send_string(sock, "screen_add U\n");
|
||||
sprintf(buffer, "screen_set U name {Uptime Screen: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add U title title\n");
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sock_send_string(sock, "widget_set U title {SYSTEM UPTIME}\n");
|
||||
sock_send_string(sock, "widget_add U one string\n");
|
||||
sock_send_string(sock, "widget_add U two string\n");
|
||||
sock_send_string(sock, "widget_add U three string\n");
|
||||
|
||||
sprintf(buffer, "widget_set U one 3 2 {%s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
sprintf(tmp, "widget_set U three 5 4 {%s %s}\n", sysname, kver);
|
||||
sock_send_string(sock, tmp);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(tmp, "widget_set U title {%s %s: %s}\n",
|
||||
sysname, kver, host);
|
||||
sock_send_string(sock, tmp);
|
||||
sock_send_string(sock, "widget_add U one string\n");
|
||||
}
|
||||
}
|
||||
|
||||
if(colons)
|
||||
colon = ':';
|
||||
else
|
||||
colon = ' ';
|
||||
|
||||
get_uptime(&uptime, &idle);
|
||||
i = (int)uptime / 86400;
|
||||
sprintf(date, "%d day%s,", i, (i != 1 ? "s" : ""));
|
||||
i = ((int)uptime % 86400) / 60 / 60;
|
||||
sprintf(hour, "%02i",i);
|
||||
i = (((int)uptime % 86400) % 3600) / 60;
|
||||
sprintf(min, "%02i",i);
|
||||
i = ((int)uptime % 60);
|
||||
sprintf(sec, "%02i",i);
|
||||
sprintf(tmp, "%s %s%c%s%c%s", date, hour, colon, min, colon, sec);
|
||||
i = ((20 - strlen(tmp)) / 2) + 1;
|
||||
|
||||
if(lcd_hgt >= 4)
|
||||
sprintf(buffer, "widget_set U two %i 3 {%s}\n", i, tmp);
|
||||
else
|
||||
sprintf(buffer, "widget_set U one %i 2 {%s}\n", i, tmp);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
colons = colons ^ 1;
|
||||
|
||||
return 0;
|
||||
} // End uptime_screen()
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Big Clock Screen displays current time...
|
||||
//
|
||||
//Curses display is ugly, but should look nice on Matrix Orbital.
|
||||
//
|
||||
//+--------------------+
|
||||
//|111555 444222 111333|
|
||||
//|111555 444222 111333|
|
||||
//|111555 444222 111333|
|
||||
//|111555 444222 111333|
|
||||
//+--------------------+
|
||||
//
|
||||
int big_clock_screen(int rep, int display)
|
||||
{
|
||||
static int first = 1;
|
||||
static int colons = 0;
|
||||
time_t thetime;
|
||||
struct tm *rtime;
|
||||
int pos[]={1,4,8,11,15,18};
|
||||
// int i=0;
|
||||
|
||||
char fulltxt[16], old_fulltxt[16];
|
||||
int j=0;
|
||||
|
||||
if(lcd_hgt < 4) return 0;
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sock_send_string(sock, "screen_add K\n");
|
||||
sock_send_string(sock, "screen_set K name {Big Clock Screen}\n");
|
||||
sock_send_string(sock, "widget_del K heartbeat\n");
|
||||
sock_send_string(sock, "widget_add K d0 num\n");
|
||||
sock_send_string(sock, "widget_add K d1 num\n");
|
||||
sock_send_string(sock, "widget_add K d2 num\n");
|
||||
sock_send_string(sock, "widget_add K d3 num\n");
|
||||
sock_send_string(sock, "widget_add K d4 num\n");
|
||||
sock_send_string(sock, "widget_add K d5 num\n");
|
||||
// sock_send_string(sock, "widget_add K one string\n");
|
||||
sock_send_string(sock, "widget_set K d0 1 0\n");
|
||||
sock_send_string(sock, "widget_set K d1 4 0\n");
|
||||
sock_send_string(sock, "widget_set K d2 8 0\n");
|
||||
sock_send_string(sock, "widget_set K d3 11 0\n");
|
||||
sock_send_string(sock, "widget_set K d4 15 0\n");
|
||||
sock_send_string(sock, "widget_set K d5 18 0\n");
|
||||
old_fulltxt[0]='0';
|
||||
old_fulltxt[1]='0';
|
||||
old_fulltxt[2]='0';
|
||||
old_fulltxt[3]='0';
|
||||
old_fulltxt[4]='0';
|
||||
old_fulltxt[5]='0';
|
||||
old_fulltxt[6]=0;
|
||||
// sock_send_string(sock, "widget_set K one 1 4 {000000}\n");
|
||||
}
|
||||
|
||||
time(&thetime);
|
||||
rtime = localtime(&thetime);
|
||||
|
||||
sprintf(fulltxt, "%02d%02d%02d",
|
||||
rtime->tm_hour, rtime->tm_min, rtime->tm_sec);
|
||||
for(j=0;j<6;j++){
|
||||
if (fulltxt[j]!=old_fulltxt[j]){
|
||||
sprintf(tmp+strlen(tmp), "widget_set K d%d %d %c\n",
|
||||
j, pos[j], fulltxt[j]);
|
||||
old_fulltxt[j]=fulltxt[j];
|
||||
}
|
||||
}
|
||||
|
||||
if(display)
|
||||
sock_send_string(sock, tmp);
|
||||
|
||||
return 0;
|
||||
} // End big_clock_screen()
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef CHRONO_H
|
||||
#define CHRONO_H
|
||||
|
||||
int chrono_init();
|
||||
int chrono_close();
|
||||
|
||||
int clock_screen(int rep, int display);
|
||||
int uptime_screen(int rep, int display);
|
||||
int time_screen(int rep, int display);
|
||||
int big_clock_screen(int rep, int display);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,365 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../../shared/sockets.h"
|
||||
#include "../../shared/debug.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "mode.h"
|
||||
#include "cpu.h"
|
||||
|
||||
struct load { unsigned long total, user, system, nice, idle; };
|
||||
|
||||
int load_fd = 0;
|
||||
static void get_load(struct load * result);
|
||||
|
||||
|
||||
static void get_load(struct load * result)
|
||||
{
|
||||
static struct load last_load = { 0, 0, 0, 0, 0 }; struct load curr_load;
|
||||
|
||||
reread(load_fd, "get_load:");
|
||||
sscanf(buffer, "%*s %lu %lu %lu %lu\n",
|
||||
&curr_load.user,
|
||||
&curr_load.nice,
|
||||
&curr_load.system,
|
||||
&curr_load.idle);
|
||||
curr_load.total = curr_load.user
|
||||
+ curr_load.nice
|
||||
+ curr_load.system
|
||||
+ curr_load.idle;
|
||||
result->total = curr_load.total - last_load.total;
|
||||
result->user = curr_load.user - last_load.user;
|
||||
result->nice = curr_load.nice - last_load.nice;
|
||||
result->system = curr_load.system - last_load.system;
|
||||
result->idle = curr_load.idle - last_load.idle;
|
||||
last_load.total = curr_load.total;
|
||||
last_load.user = curr_load.user;
|
||||
last_load.nice = curr_load.nice;
|
||||
last_load.system = curr_load.system;
|
||||
last_load.idle = curr_load.idle;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int cpu_init()
|
||||
{
|
||||
if(!load_fd)
|
||||
{
|
||||
load_fd = open("/proc/stat",O_RDONLY);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cpu_close()
|
||||
{
|
||||
if(load_fd)
|
||||
close(load_fd);
|
||||
|
||||
load_fd = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// CPU screen shows info about percentage of the CPU being used
|
||||
//
|
||||
int cpu_screen(int rep, int display)
|
||||
{
|
||||
#undef CPU_BUF_SIZE
|
||||
#define CPU_BUF_SIZE 4
|
||||
int i, j, n;
|
||||
static int first = 1;
|
||||
float value;
|
||||
static float cpu[CPU_BUF_SIZE + 1][5];// last buffer is scratch
|
||||
struct load load;
|
||||
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sock_send_string(sock, "screen_add C\n");
|
||||
sprintf(buffer, "screen_set C name {CPU Use: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sock_send_string(sock, "widget_add C title title\n");
|
||||
sock_send_string(sock, "widget_set C title {CPU LOAD}\n");
|
||||
sock_send_string(sock, "widget_add C one string\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set C one 1 2 {0 1 Sys 0.0%}\n");
|
||||
sock_send_string(sock, "widget_add C two string\n");
|
||||
sock_send_string(sock, "widget_add C three string\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set C one 1 2 {Usr 0.0% Nice 0.0%}\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set C two 1 3 {Sys 0.0% Idle 0.0%}\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set C three 1 4 {0% 100%}\n");
|
||||
sock_send_string(sock, "widget_add C usr string\n");
|
||||
sock_send_string(sock, "widget_add C nice string\n");
|
||||
sock_send_string(sock, "widget_add C idle string\n");
|
||||
sock_send_string(sock, "widget_add C sys string\n");
|
||||
sock_send_string(sock, "widget_add C bar hbar\n");
|
||||
sock_send_string(sock, "widget_set C bar 3 4 0\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
sock_send_string(sock, "widget_add C cpu0 string\n");
|
||||
if(lcd_wid >= 20)
|
||||
sock_send_string(sock, "widget_set C cpu0 1 1 {CPU0[ ]}\n");
|
||||
else
|
||||
sock_send_string(sock, "widget_set C cpu0 1 1 {CPU0[ ]}\n");
|
||||
sock_send_string(sock, "widget_add C cpu0% string\n");
|
||||
sprintf(buffer, "widget_set C cpu0%% 1 %i { 0.0%%}\n", lcd_wid-4);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add C usni string\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set C usni 1 2 { U S N I}\n");
|
||||
sock_send_string(sock, "widget_add C usr hbar\n");
|
||||
sock_send_string(sock, "widget_add C sys hbar\n");
|
||||
sock_send_string(sock, "widget_add C nice hbar\n");
|
||||
sock_send_string(sock, "widget_add C idle hbar\n");
|
||||
sock_send_string(sock, "widget_add C total hbar\n");
|
||||
sock_send_string(sock, "widget_set C total 6 1 0\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
//else return 0;
|
||||
|
||||
get_load(&load);
|
||||
|
||||
// Shift values over by one
|
||||
for(i=0; i<(CPU_BUF_SIZE-1); i++)
|
||||
for(j=0; j<5; j++)
|
||||
cpu[i][j] = cpu[i+1][j];
|
||||
|
||||
// Read new data
|
||||
cpu[CPU_BUF_SIZE-1][0] = ((float)load.user / (float)load.total) * 100.0;
|
||||
cpu[CPU_BUF_SIZE-1][1] = ((float)load.system / (float)load.total) * 100.0;
|
||||
cpu[CPU_BUF_SIZE-1][2] = ((float)load.nice / (float)load.total) * 100.0;
|
||||
cpu[CPU_BUF_SIZE-1][3] = ((float)load.idle / (float)load.total) * 100.0;
|
||||
cpu[CPU_BUF_SIZE-1][4] = (((float)load.user + (float)load.system +
|
||||
(float)load.nice) / (float)load.total) * 100.0;
|
||||
|
||||
// Only clear on first display...
|
||||
if(!rep)
|
||||
{
|
||||
/*
|
||||
// Make all the same, if this is the first time...
|
||||
for(i=0; i<CPU_BUF_SIZE-1; i++)
|
||||
for(j=0; j<5; j++)
|
||||
cpu[i][j] = cpu[CPU_BUF_SIZE-1][j];
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Average values for final result
|
||||
for(i=0; i<5; i++)
|
||||
{
|
||||
value = 0;
|
||||
for(j=0; j<CPU_BUF_SIZE; j++)
|
||||
{
|
||||
value += cpu[j][i];
|
||||
}
|
||||
value /= CPU_BUF_SIZE;
|
||||
cpu[CPU_BUF_SIZE][i] = value;
|
||||
}
|
||||
|
||||
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
value = cpu[CPU_BUF_SIZE][4];
|
||||
n = (int)(value * 70.0);
|
||||
if (value >= 99.9) { sprintf(tmp, "100%%"); }
|
||||
else { sprintf(tmp, "%4.1f%%", value); }
|
||||
sprintf(buffer, "widget_set C title {CPU %s: %s}\n", tmp, host);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
value = cpu[CPU_BUF_SIZE][0];
|
||||
if (value >= 99.9) { sprintf(tmp, " 100%%"); }
|
||||
else { sprintf(tmp, "%4.1f%%", value); }
|
||||
sprintf(buffer, "widget_set C usr 5 2 {%s}\n", tmp);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
value = cpu[CPU_BUF_SIZE][1];
|
||||
if (value >= 99.9) { sprintf(tmp, " 100%%"); }
|
||||
else { sprintf(tmp, "%4.1f%%", value); }
|
||||
sprintf(buffer, "widget_set C sys 5 3 {%s}\n", tmp);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
value = cpu[CPU_BUF_SIZE][2];
|
||||
if (value >= 99.9) { sprintf(tmp, " 100%%"); }
|
||||
else { sprintf(tmp, "%4.1f%%", value); }
|
||||
sprintf(buffer, "widget_set C nice 16 2 {%s}\n", tmp);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
value = cpu[CPU_BUF_SIZE][3];
|
||||
if (value >= 99.9) { sprintf(tmp, " 100%%"); }
|
||||
else { sprintf(tmp, "%4.1f%%", value); }
|
||||
sprintf(buffer, "widget_set C idle 16 3 {%s}\n", tmp);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
value = cpu[CPU_BUF_SIZE][4];
|
||||
n = (int)(value * (lcd_cellwid * 14) / 100.0);
|
||||
sprintf(buffer, "widget_set C bar 3 4 %i\n", n);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
} // end if(lcd_hgt >= 4)
|
||||
else // 20x2 version
|
||||
{
|
||||
value = cpu[CPU_BUF_SIZE][4];
|
||||
if (value >= 99.9) { sprintf(tmp, "100%%"); }
|
||||
else { sprintf(tmp, "%4.1f%%", value); }
|
||||
sprintf(buffer, "widget_set C cpu0%% %i 1 {%s}\n", lcd_wid-4, tmp);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
n = (float)(value * lcd_cellwid * (float)(lcd_wid-11)) / 100.0;
|
||||
sprintf(buffer, "widget_set C total 6 1 %i\n", n);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
value = cpu[CPU_BUF_SIZE][0];
|
||||
n = (float)(value * lcd_cellwid * 4.0) / 100.0;
|
||||
sprintf(buffer, "widget_set C usr 1 2 %i\n", n);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
value = cpu[CPU_BUF_SIZE][1];
|
||||
n = (float)(value * lcd_cellwid * 3.0) / 100.0;
|
||||
sprintf(buffer, "widget_set C sys 7 2 %i\n", n);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
value = cpu[CPU_BUF_SIZE][2];
|
||||
n = (float)(value * lcd_cellwid * 3.0) / 100.0;
|
||||
sprintf(buffer, "widget_set C nice 12 2 %i\n", n);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
value = cpu[CPU_BUF_SIZE][3];
|
||||
n = (float)(value * lcd_cellwid * 3.0) / 100.0;
|
||||
sprintf(buffer, "widget_set C idle 17 2 %i\n", n);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
} // End cpu_screen()
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// Cpu Graph Screen shows a quick-moving histogram of CPU use.
|
||||
//
|
||||
int cpu_graph_screen(int rep, int display)
|
||||
{
|
||||
int i, j, n=0;
|
||||
static int first=1;
|
||||
float value, maxload;
|
||||
#undef CPU_BUF_SIZE
|
||||
#define CPU_BUF_SIZE 2
|
||||
static float cpu[CPU_BUF_SIZE + 1];// last buffer is scratch
|
||||
static int cpu_past[LCD_MAX_WIDTH];
|
||||
struct load load;
|
||||
int status=0;
|
||||
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
sock_send_string(sock, "screen_add G\n");
|
||||
sprintf(buffer, "screen_set G name {CPU Graph: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sock_send_string(sock, "widget_add G title title\n");
|
||||
sprintf(buffer, "widget_set G title {CPU: %s}\n", host);
|
||||
}
|
||||
else
|
||||
{
|
||||
sock_send_string(sock, "widget_add G title string\n");
|
||||
sprintf(buffer, "widget_set G title 1 1 {CPU: %s}\n", host);
|
||||
}
|
||||
sock_send_string(sock, buffer);
|
||||
|
||||
for(i=1; i<=lcd_wid; i++)
|
||||
{
|
||||
sprintf(tmp, "widget_add G %i vbar\n", i);
|
||||
sock_send_string(sock, tmp);
|
||||
sprintf(tmp, "widget_set G %i %i %i 0\n", i, i, lcd_hgt);
|
||||
sock_send_string(sock, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
get_load(&load);
|
||||
|
||||
// Shift values over by one
|
||||
for(i=0; i<(CPU_BUF_SIZE-1); i++)
|
||||
cpu[i] = cpu[i+1];
|
||||
|
||||
// Read new data
|
||||
cpu[CPU_BUF_SIZE-1] = ((float)load.user + (float)load.system
|
||||
+ (float)load.nice) / (float)load.total;
|
||||
|
||||
|
||||
// Only clear on first display...
|
||||
if(!rep)
|
||||
{
|
||||
/*
|
||||
// Make all the same, if this is the first time...
|
||||
for(i=0; i<CPU_BUF_SIZE-1; i++)
|
||||
cpu[i] = cpu[CPU_BUF_SIZE-1];
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
// Average values for final result
|
||||
value = 0;
|
||||
for(j=0; j<CPU_BUF_SIZE; j++)
|
||||
{
|
||||
value += cpu[j];
|
||||
}
|
||||
value /= (float)CPU_BUF_SIZE;
|
||||
cpu[CPU_BUF_SIZE] = value;
|
||||
|
||||
|
||||
maxload=0;
|
||||
for(i=0; i<lcd_wid-1; i++)
|
||||
{
|
||||
cpu_past[i] = cpu_past[i+1];
|
||||
|
||||
j = cpu_past[i];
|
||||
sprintf(tmp, "widget_set G %i %i %i %i\n",
|
||||
i+1, i+1, lcd_hgt, j);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
if(cpu_past[i] > maxload) maxload = cpu_past[i];
|
||||
}
|
||||
|
||||
value = cpu[CPU_BUF_SIZE];
|
||||
if(lcd_hgt > 2)
|
||||
n = (int)(value * (float)(lcd_cellhgt) * (float)(lcd_hgt-1));
|
||||
else
|
||||
n = (int)(value * (float)(lcd_cellhgt) * (float)(lcd_hgt));
|
||||
|
||||
cpu_past[lcd_wid-1] = n;
|
||||
sprintf(tmp, "widget_set G %i %i %i %i\n",
|
||||
lcd_wid, lcd_wid, lcd_hgt, n);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
|
||||
if(n > maxload) maxload = n;
|
||||
|
||||
if(cpu_past[lcd_wid-1] > 0 ) status = BACKLIGHT_ON;
|
||||
if(maxload < 1) status = BACKLIGHT_OFF;
|
||||
|
||||
// return status;
|
||||
return 0;
|
||||
} // End cpu_graph_screen()
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef CPU_H
|
||||
#define CPU_H
|
||||
|
||||
|
||||
int cpu_init();
|
||||
int cpu_close();
|
||||
|
||||
|
||||
int cpu_screen(int rep, int display);
|
||||
int cpu_graph_screen(int rep, int display);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,322 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#ifdef LINUX
|
||||
#include <sys/vfs.h>
|
||||
#else
|
||||
#ifdef xBSD
|
||||
#include <sys/param.h>
|
||||
#include <sys/mount.h>
|
||||
#else
|
||||
#include <sys/statfs.h>
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#include "../../shared/sockets.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "mode.h"
|
||||
#include "disk.h"
|
||||
|
||||
FILE *mtab_fd;
|
||||
|
||||
|
||||
typedef struct mounts
|
||||
{
|
||||
char dev[256], type[64], mpoint[256];
|
||||
long bsize, blocks, bfree, files, ffree;
|
||||
} mounts;
|
||||
|
||||
|
||||
|
||||
static int get_fs(mounts fs[])
|
||||
{
|
||||
struct statfs fsinfo;
|
||||
char line[256];
|
||||
int x = 0, y;
|
||||
|
||||
mtab_fd = fopen("/etc/mtab", "r");
|
||||
|
||||
// Get rid of old, unmounted filesystems...
|
||||
memset(fs, 0, sizeof(mounts)*256);
|
||||
|
||||
while (x < 256)
|
||||
{
|
||||
if(fgets(line, 256, mtab_fd) == NULL)
|
||||
{
|
||||
fclose(mtab_fd);
|
||||
return x;
|
||||
}
|
||||
|
||||
sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type);
|
||||
|
||||
if(strcmp(fs[x].type, "proc")
|
||||
#ifndef STAT_NFS
|
||||
&& strcmp(fs[x].type, "nfs")
|
||||
#endif
|
||||
#ifndef STAT_SMBFS
|
||||
&& strcmp(fs[x].type, "smbfs")
|
||||
#endif
|
||||
)
|
||||
{
|
||||
#if LINUX || BSD
|
||||
y = statfs(fs[x].mpoint, &fsinfo);
|
||||
#else
|
||||
y = statfs(fs[x].mpoint, &fsinfo, sizeof(fsinfo), 0);
|
||||
#endif
|
||||
fs[x].blocks = fsinfo.f_blocks;
|
||||
if(fs[x].blocks > 0)
|
||||
{
|
||||
fs[x].bsize = fsinfo.f_bsize;
|
||||
fs[x].bfree = fsinfo.f_bfree;
|
||||
fs[x].files = fsinfo.f_files;
|
||||
fs[x].ffree = fsinfo.f_ffree;
|
||||
x++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(mtab_fd);
|
||||
return x;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static int get_fs(mounts fs[])
|
||||
{
|
||||
struct statfs fsinfo;
|
||||
char line[256];
|
||||
int x = 0, y;
|
||||
|
||||
mtab_fd = fopen("/etc/mtab", "r");
|
||||
|
||||
// Get rid of old, unmounted filesystems...
|
||||
memset(fs, 0, sizeof(mounts)*256);
|
||||
|
||||
while (x < 256)
|
||||
{
|
||||
if(fgets(line, 256, mtab_fd) == NULL)
|
||||
{
|
||||
fclose(mtab_fd);
|
||||
return x;
|
||||
}
|
||||
|
||||
sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type);
|
||||
|
||||
if( strcmp(fs[x].type, "proc")
|
||||
#ifndef STAT_NFS
|
||||
&& strcmp(fs[x].type, "nfs")
|
||||
#endif
|
||||
#ifndef STAT_SMBFS
|
||||
&& strcmp(fs[x].type, "smbfs")
|
||||
#endif
|
||||
)
|
||||
{
|
||||
#ifdef LINUX
|
||||
y = statfs(fs[x].mpoint, &fsinfo);
|
||||
#else
|
||||
y = statfs(fs[x].mpoint, &fsinfo, sizeof(fsinfo), 0);
|
||||
#endif
|
||||
fs[x].bsize = fsinfo.f_bsize;
|
||||
fs[x].blocks = fsinfo.f_blocks;
|
||||
fs[x].bfree = fsinfo.f_bfree;
|
||||
fs[x].files = fsinfo.f_files;
|
||||
fs[x].ffree = fsinfo.f_ffree;
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(mtab_fd);
|
||||
return x;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
int disk_init()
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int disk_close()
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Gives disk stats.
|
||||
//
|
||||
// Stays onscreen until it is done.
|
||||
//
|
||||
|
||||
// TODO: Disk screen! Requires virtual pages in the server, though...
|
||||
|
||||
int disk_screen(int rep, int display)
|
||||
{
|
||||
static mounts mnt[256];
|
||||
static int count=0;
|
||||
|
||||
// Holds info to display (avoid recalculating it)
|
||||
struct disp
|
||||
{
|
||||
char dev[8];
|
||||
char cap[8];
|
||||
int full;
|
||||
} table[256];
|
||||
int i;
|
||||
static int num_disks=0;
|
||||
static int first=1; // First line to display, sort of.
|
||||
|
||||
#define huge long long int
|
||||
huge size;
|
||||
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sock_send_string(sock, "screen_add D\n");
|
||||
sprintf(buffer, "screen_set D name {Disk Use: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add D title title\n");
|
||||
sprintf(buffer, "widget_set D title {DISKS: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add D f frame\n");
|
||||
//sock_send_string(sock, "widget_set D f 1 2 20 4 20 3 v 8\n");
|
||||
sprintf(buffer, "widget_set D f 1 2 %i %i %i %i v 12\n",
|
||||
lcd_wid, lcd_hgt, lcd_wid, lcd_hgt-1);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add D err1 string\n");
|
||||
sock_send_string(sock, "widget_add D err2 string\n");
|
||||
sock_send_string(sock, "widget_set D err1 5 2 { Reading }\n");
|
||||
sock_send_string(sock, "widget_set D err2 5 3 {Filesystems}\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Grab disk stats on first display, and fill "table".
|
||||
// Get rid of old, unmounted filesystems...
|
||||
memset(table, 0, sizeof(struct disp)*256);
|
||||
|
||||
count = get_fs(mnt);
|
||||
first = 0;
|
||||
|
||||
// Fill the display structure...
|
||||
if(count)
|
||||
{
|
||||
sock_send_string(sock, "widget_set D err1 30 5 .\n");
|
||||
sock_send_string(sock, "widget_set D err2 30 5 .\n");
|
||||
for(i=0; i<count; i++)
|
||||
{
|
||||
if(strlen(mnt[i].mpoint) > 6)
|
||||
{
|
||||
sprintf(table[i].dev, "%c%s", ELLIPSIS,
|
||||
(mnt[i].mpoint)+(strlen(mnt[i].mpoint)-5));
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(table[i].dev, "%s", mnt[i].mpoint);
|
||||
}
|
||||
|
||||
//table[i].full = (lcd.cellwid * 4)
|
||||
table[i].full = (huge)(lcd_cellwid * 4)
|
||||
* (huge)(mnt[i].blocks - mnt[i].bfree)
|
||||
/ (huge)mnt[i].blocks;
|
||||
|
||||
size = (huge)mnt[i].bsize * (huge)mnt[i].blocks;
|
||||
memset(table[i].cap, 0, 8);
|
||||
|
||||
// Kilobytes
|
||||
if(size > 0 && size < (huge)1000*(huge)1000)
|
||||
sprintf(table[i].cap, "%3.1fk",
|
||||
(double)(size)/1024.0);
|
||||
// Megabytes
|
||||
else if(size >= (huge)1000*(huge)1000
|
||||
&& size < (huge)1000*(huge)1000*(huge)1000)
|
||||
sprintf(table[i].cap, "%3.1fM",
|
||||
(float)(size/(huge)1024)/1024.0);
|
||||
// Gigabytes
|
||||
else if(size >= (huge)1000*(huge)1000*(huge)1000
|
||||
&& size < (huge)1000*(huge)1000*(huge)1000*(huge)1000)
|
||||
sprintf(table[i].cap, "%3.1fG",
|
||||
(float)(size/((huge)1024*(huge)1024))/1024.0);
|
||||
// Terabytes
|
||||
else if(size >= (huge)1000*(huge)1000*(huge)1000*(huge)1000
|
||||
&& size < (huge)1000*(huge)1000*(huge)1000*(huge)1000*(huge)1000)
|
||||
sprintf(table[i].cap, "%3.1fT",
|
||||
(float)(size/((huge)1024*(huge)1024*(huge)1024))/1024.0);
|
||||
|
||||
// PectaBytes -- Yeah! I want some!
|
||||
else if(size >= (huge)1000*(huge)1000*(huge)1000*(huge)1000*(huge)1000
|
||||
&& size < (huge)1000*(huge)1000*(huge)1000*(huge)1000*(huge)1000*(huge)1000)
|
||||
sprintf(table[i].cap, "%3.1fP",
|
||||
(float)(size/((huge)1024*(huge)1024*(huge)1024*(huge)1024))/1024.0);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!count)
|
||||
{
|
||||
sock_send_string(sock, "widget_set D err1 1 2 {Error Retrieving}\n");
|
||||
sock_send_string(sock, "widget_set D err2 1 3 {Filesystem Stats}\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// Display stuff... (show for two seconds, then scroll once per
|
||||
// second, then hold at the end for two seconds)
|
||||
sprintf(buffer, "widget_set D f 1 2 %i %i %i %i v 12\n",
|
||||
lcd_wid, lcd_hgt, lcd_wid, count);
|
||||
sock_send_string(sock, buffer);
|
||||
//sprintf(tmp, "widget_set D f 1 2 20 4 20 %i v 8\n", count);
|
||||
//sock_send_string(sock, tmp);
|
||||
for(i=0; i<count; i++)
|
||||
{
|
||||
if(table[i].dev[0] == 0) continue;
|
||||
if(i >= num_disks) // Make sure we have enough lines...
|
||||
{
|
||||
sprintf(tmp, "widget_add D s%i string -in f\n", i);
|
||||
sock_send_string(sock, tmp);
|
||||
sprintf(tmp, "widget_add D h%i hbar -in f\n", i);
|
||||
sock_send_string(sock, tmp);
|
||||
|
||||
}
|
||||
sprintf(tmp, "widget_set D s%i 1 %i {%-6s %6s E F}\n",
|
||||
i, i+1, table[i].dev, table[i].cap);
|
||||
sock_send_string(sock, tmp);
|
||||
sprintf(tmp, "widget_set D h%i 16 %i %i\n",
|
||||
i, i+1, table[i].full);
|
||||
sock_send_string(sock, tmp);
|
||||
}
|
||||
|
||||
// Now remove extra widgets...
|
||||
for(; i < num_disks; i++)
|
||||
{
|
||||
sprintf(tmp, "widget_del D s%i\n", i);
|
||||
sock_send_string(sock, tmp);
|
||||
sprintf(tmp, "widget_del D h%i\n", i);
|
||||
sock_send_string(sock, tmp);
|
||||
}
|
||||
|
||||
|
||||
num_disks = count;
|
||||
|
||||
#undef huge
|
||||
|
||||
/*
|
||||
// ** FILESYSTEMS *****
|
||||
// / 543.2M E----F
|
||||
// /dos/c 2.1G E----F
|
||||
// /stuff 4.3G E----F
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef DISK_H
|
||||
#define DISK_H
|
||||
|
||||
int disk_init();
|
||||
int disk_close();
|
||||
|
||||
int disk_screen(int rep, int display);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,152 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "../../shared/sockets.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "mode.h"
|
||||
#include "load.h"
|
||||
|
||||
int loadavg_fd = 0;
|
||||
|
||||
static double get_loadavg(void);
|
||||
|
||||
static double get_loadavg(void)
|
||||
{
|
||||
double load;
|
||||
|
||||
reread(loadavg_fd, "get_load:");
|
||||
sscanf(buffer, "%lf", &load);
|
||||
return load;
|
||||
}
|
||||
|
||||
|
||||
int load_init()
|
||||
{
|
||||
if(!loadavg_fd)
|
||||
loadavg_fd = open("/proc/loadavg",O_RDONLY);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int load_close()
|
||||
{
|
||||
if(loadavg_fd)
|
||||
close(loadavg_fd);
|
||||
|
||||
loadavg_fd = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// Shows a display very similar to "xload"'s histogram.
|
||||
//
|
||||
|
||||
int xload_screen(int rep, int display)
|
||||
{
|
||||
static int first = 1;
|
||||
static float loads[LCD_MAX_WIDTH];
|
||||
int n, i;
|
||||
float loadmax=0, factor, x;
|
||||
int status = 0;
|
||||
|
||||
|
||||
if(first) // Only the first time this is ever called...
|
||||
{
|
||||
first = 0;
|
||||
memset(loads, 0, sizeof(float)*LCD_MAX_WIDTH);
|
||||
|
||||
sock_send_string(sock, "screen_add X\n");
|
||||
sprintf(buffer, "screen_set X name {X-Load: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
// Add the vbars...
|
||||
for(i=1; i<lcd_wid; i++)
|
||||
{
|
||||
sprintf(tmp, "widget_add X %i vbar\n", i);
|
||||
sock_send_string(sock, tmp);
|
||||
sprintf(tmp, "widget_set X %i %i %i 0\n", i, i, lcd_hgt);
|
||||
sock_send_string(sock, tmp);
|
||||
}
|
||||
// And add a title...
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sock_send_string(sock, "widget_add X title title\n");
|
||||
sock_send_string(sock, "widget_set X title {LOAD }\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
sock_send_string(sock, "widget_add X title string\n");
|
||||
sock_send_string(sock, "widget_set X title 1 1 {LOAD}\n");
|
||||
sock_send_string(sock, "widget_del X heartbeat\n");
|
||||
}
|
||||
sock_send_string(sock, "widget_add X zero string\n");
|
||||
sock_send_string(sock, "widget_add X top string\n");
|
||||
sprintf(tmp, "widget_set X zero %i %i 0\n", lcd_wid, lcd_hgt);
|
||||
sock_send_string(sock, tmp);
|
||||
sprintf(tmp, "widget_set X top %i %i 1\n",
|
||||
lcd_wid, (lcd_hgt==2)?1:2);
|
||||
sock_send_string(sock, tmp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
for(n=0; n<(lcd_wid-2); n++) loads[n] = loads[n+1];
|
||||
loads[lcd_wid-2] = get_loadavg();
|
||||
|
||||
for(n=0; n<lcd_wid-1; n++)
|
||||
if(loads[n] > loadmax) loadmax = loads[n];
|
||||
|
||||
n = (int)loadmax;
|
||||
if ((float)n < loadmax) { n++; }
|
||||
sprintf(tmp, "widget_set X top %i %i %i\n",
|
||||
lcd_wid, (lcd_hgt==2)?1:2, n);
|
||||
//if(display) sock_send_string(sock, tmp);
|
||||
sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
if (loadmax < 1.0) factor = (float)(lcd_cellhgt) * ((lcd_hgt==2)?2.0:3.0);
|
||||
else factor = (float)(lcd_cellhgt) * ((lcd_hgt==2)?2.0:3.0) / (float)n;
|
||||
|
||||
for(n=0; n<lcd_wid-1; n++)
|
||||
{
|
||||
x = (loads[n] * factor);
|
||||
|
||||
sprintf(tmp, "widget_set X %i %i %i %i\n", n+1, n+1, lcd_hgt, (int)x);
|
||||
//if(display) sock_send_string(sock, tmp);
|
||||
sock_send_string(sock, tmp);
|
||||
}
|
||||
|
||||
if(loadmax < LOAD_MIN)
|
||||
{
|
||||
status = BACKLIGHT_OFF;
|
||||
}
|
||||
if(loadmax > LOAD_MIN)
|
||||
{
|
||||
status = BACKLIGHT_ON;
|
||||
}
|
||||
if(loads[lcd_wid-2] > LOAD_MAX)
|
||||
{
|
||||
status = BLINK_ON;
|
||||
}
|
||||
|
||||
// And now the title...
|
||||
if(lcd_hgt >= 4)
|
||||
sprintf(tmp, "widget_set X title {LOAD %2.2f: %s}\n",
|
||||
loads[lcd_wid-2], host);
|
||||
else
|
||||
sprintf(tmp, "widget_set X title 1 1 {%s %2.2f}\n",
|
||||
host, loads[lcd_wid-2]);
|
||||
//if(display) sock_send_string(sock, tmp);
|
||||
sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
return status;
|
||||
} // End xload_screen()
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef LOAD_H
|
||||
#define LOAD_H
|
||||
|
||||
#ifndef LOAD_MAX
|
||||
#define LOAD_MAX 1.3
|
||||
#endif
|
||||
#ifndef LOAD_MIN
|
||||
#define LOAD_MIN 0.5
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
int load_init();
|
||||
int load_close();
|
||||
|
||||
int xload_screen(int rep, int display);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,440 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/time.h>
|
||||
#include <termios.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "mode.h"
|
||||
#include "../../shared/sockets.h"
|
||||
#include "../../shared/debug.h"
|
||||
|
||||
|
||||
// TODO: Commenting... Everything!
|
||||
|
||||
|
||||
int Quit = 0;
|
||||
int sock;
|
||||
|
||||
char *version = VERSION;
|
||||
char *build_date = __DATE__;
|
||||
|
||||
int lcd_wid;
|
||||
int lcd_hgt;
|
||||
int lcd_cellwid;
|
||||
int lcd_cellhgt;
|
||||
|
||||
|
||||
/*
|
||||
Mode List:
|
||||
See below... (default_sequence[])
|
||||
*/
|
||||
|
||||
void HelpScreen();
|
||||
void exit_program(int val);
|
||||
void main_loop(mode *sequence);
|
||||
|
||||
#define MAX_SEQUENCE 256
|
||||
// 1/8th second is a single time unit...
|
||||
#define TIME_UNIT 125000
|
||||
|
||||
|
||||
// Contains a list of modes to run
|
||||
mode default_sequence[] =
|
||||
{
|
||||
// which on off inv timer/visible
|
||||
{ 'C', 1, 2, 0, 0xffff,0,},// [C]PU
|
||||
{ 'M', 4, 16, 0, 0xffff,0,},// [M]emory
|
||||
{ 'X', 64, 128, 1, 0xffff,0,},// [X]-load (load histogram)
|
||||
{ 'T', 4, 64, 0, 0xffff,0,},// [T]ime/Date
|
||||
{ 'A', 999,9999, 0, 0xffff,0,},// [A]bout (credits)
|
||||
|
||||
{ 1 , 0, 0, 0, 0,0,},// Modes after this line will not be run by default...
|
||||
// ... all non-default modes must be in here!
|
||||
// ... they will not show up otherwise.
|
||||
{ 'O', 4, 64, 0, 0xffff,0,},// [O]ld Timescreen
|
||||
{ 'K', 4, 64, 0, 0xffff,0,},// big cloc[K]
|
||||
{ 'U', 4, 128, 0, 0xffff,0,},// Old [U]ptime Screen
|
||||
{ 'B', 32, 256, 1, 0xffff,0,},// [B]attery Status
|
||||
{ 'G', 1, 2, 0, 0xffff,0,},// Cpu histogram [G]raph
|
||||
{ 'S', 16, 256, 1, 0xffff,0,},// [S]ize of biggest programs
|
||||
{ 'D', 256, 256, 1, 0xffff,0,},// [D]isk stats
|
||||
{ 0, 0, 0, 0,0,},// No more.. all done.
|
||||
};
|
||||
|
||||
// TODO: Clean up main()... It is still way too big.
|
||||
|
||||
// TODO: Config file; not just command line
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
mode sequence[MAX_SEQUENCE];
|
||||
char cfgfile[256] = "/etc/lcdproc.cf";
|
||||
char server[256] = "localhost";
|
||||
int port = LCDPORT;
|
||||
int i, j, k;
|
||||
int tmp;
|
||||
|
||||
memset(sequence, 0, sizeof(mode) * MAX_SEQUENCE);
|
||||
|
||||
// Ctrl-C will cause a clean exit...
|
||||
signal(SIGINT, exit_program);
|
||||
// and "kill"...
|
||||
signal(SIGTERM, exit_program);
|
||||
// and "kill -HUP" (hangup)...
|
||||
signal(SIGHUP, exit_program);
|
||||
// and just in case, "kill -KILL" (which cannot be trapped; but oh well)
|
||||
signal(SIGKILL, exit_program);
|
||||
|
||||
|
||||
|
||||
// Command line
|
||||
memcpy(sequence, default_sequence, sizeof(default_sequence));
|
||||
for(i=1, j=0; i<argc; i++)
|
||||
{
|
||||
if(argv[i][0] == '-') switch(argv[i][1])
|
||||
{
|
||||
// s is for server
|
||||
case 'S':
|
||||
case 's': if(argc < i+1) HelpScreen();
|
||||
strcpy(server, argv[++i]);
|
||||
break;
|
||||
// p is for port
|
||||
case 'P':
|
||||
case 'p': if(argc < i+1) HelpScreen();
|
||||
port = atoi(argv[++i]);
|
||||
if(port < 1 && port > 0xffff)
|
||||
fprintf(stderr, "Warning: Port %i outside of standard range\n",
|
||||
port);
|
||||
break;
|
||||
|
||||
// otherwise... Get help!
|
||||
default: HelpScreen(); break;
|
||||
}
|
||||
// Parse command line here... read the man page.
|
||||
else if(strlen(argv[i]) == 1)
|
||||
{
|
||||
// Grab the mode letter...
|
||||
sequence[j].which = argv[i][0];
|
||||
|
||||
// If we have just a letter with no numbers...
|
||||
// Set the defaults...
|
||||
for(tmp=0, k=0; default_sequence[k].which; k++)
|
||||
{
|
||||
if(toupper(sequence[j].which) == default_sequence[k].which)
|
||||
{
|
||||
memcpy(&sequence[j], &default_sequence[k], sizeof(mode));
|
||||
tmp=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!tmp) { printf("Invalid Mode: %c\n", argv[i][0]); exit(0); }
|
||||
|
||||
j++;
|
||||
// Set the last element to 0...
|
||||
memset(sequence + j, 0, sizeof(mode));
|
||||
} // End if(strlen(argv == 1))
|
||||
else
|
||||
{
|
||||
// A multicharacter parameter by itself
|
||||
// is assumed to be a config file..
|
||||
strcpy(cfgfile, argv[i]);
|
||||
printf("Ignoring config file: %s\n", cfgfile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Connect to the server...
|
||||
usleep(500000); // wait for the server to start up
|
||||
sock = sock_connect(server, port);
|
||||
if (sock <= 0)
|
||||
{
|
||||
printf("Error connecting to server %s on port %i.\n",
|
||||
server, port);
|
||||
return 0;
|
||||
}
|
||||
sock_send_string(sock, "hello\n");
|
||||
usleep(500000); // wait for the server to say hi.
|
||||
|
||||
|
||||
// We grab the real values below, from the "connect" line.
|
||||
lcd_wid = 20;
|
||||
lcd_hgt = 4;
|
||||
lcd_cellwid = 5;
|
||||
lcd_cellhgt = 8;
|
||||
|
||||
// Init the status gatherers...
|
||||
mode_init(sequence);
|
||||
|
||||
// And spew stuff!
|
||||
main_loop(sequence);
|
||||
|
||||
// Clean up
|
||||
exit_program(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void HelpScreen()
|
||||
{
|
||||
printf("LCDproc, %s\n", version);
|
||||
printf("Usage: lcdproc [-s server] [-p port] [modelist]\n");
|
||||
printf("\tOptions in []'s are optional.\n");
|
||||
printf("\tmodelist is \"mode [mode mode ...]\"\n");
|
||||
printf("\tMode letters: \t[C]pu [G]raph [T]ime [M]emory [X]load [D]isk [B]attery\n\t\t\tproc_[S]izes [O]ld_time big_cloc[K] [U]ptime [A]bout\n");
|
||||
printf("\n");
|
||||
printf("\tUse \"man lcdproc\" for more info.\n");
|
||||
printf("Example:\n");
|
||||
printf("\tlcdproc -s my.lcdproc.server.com C M X -p 13666\n");
|
||||
printf("\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
// Called upon TERM and INTR signals...
|
||||
//
|
||||
void exit_program(int val)
|
||||
{
|
||||
Quit = 1;
|
||||
sock_close(sock);
|
||||
mode_close();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
// Main program loop...
|
||||
//
|
||||
void main_loop(mode *sequence)
|
||||
{
|
||||
int i=0, j;
|
||||
//int status=0;
|
||||
char buf[8192];
|
||||
char *argv[256];
|
||||
int argc, newtoken;
|
||||
int len;
|
||||
|
||||
|
||||
// Main loop
|
||||
// Run whatever screen we want, then wait. Woo-hoo!
|
||||
while(!Quit)
|
||||
{
|
||||
// Check for server input...
|
||||
len = sock_recv(sock, buf, 8000);
|
||||
|
||||
// Handle server input...
|
||||
while(len > 0)
|
||||
{
|
||||
// Now split the string into tokens...
|
||||
//for(i=0; i<argc; i++) argv[i]=NULL; // Get rid of old tokens
|
||||
argc = 0; newtoken = 1;
|
||||
for(i=0; i<len; i++)
|
||||
{
|
||||
if(buf[i]) // For regular letters, keep tokenizing...
|
||||
{
|
||||
switch(buf[i])
|
||||
{
|
||||
case ' ':
|
||||
newtoken = 1;
|
||||
buf[i] = 0;
|
||||
break;
|
||||
case '\n':
|
||||
buf[i] = 0;
|
||||
default:
|
||||
if(newtoken)
|
||||
{
|
||||
argv[argc] = buf+i;
|
||||
argc++;
|
||||
}
|
||||
newtoken = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else // If we've got a zero, it's the end of a string...
|
||||
{
|
||||
if(argc > 0)
|
||||
{
|
||||
//printf("%s %s\n", argv[0], argv[1]);
|
||||
if(0 == strcmp(argv[0], "listen"))
|
||||
{
|
||||
for(j=0; sequence[j].which; j++)
|
||||
{
|
||||
if(sequence[j].which == argv[1][0])
|
||||
{
|
||||
sequence[j].visible = 1;
|
||||
//debug("Listen %s\n", argv[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(0 == strcmp(argv[0], "ignore"))
|
||||
{
|
||||
for(j=0; sequence[j].which; j++)
|
||||
{
|
||||
if(sequence[j].which == argv[1][0])
|
||||
{
|
||||
sequence[j].visible = 0;
|
||||
//debug("Ignore %s\n", argv[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(0 == strcmp(argv[0], "key"))
|
||||
{
|
||||
debug("Key %s\n", argv[1]);
|
||||
}
|
||||
else if(0 == strcmp(argv[0], "menu"))
|
||||
{
|
||||
}
|
||||
else if(0 == strcmp(argv[0], "connect"))
|
||||
{
|
||||
int a;
|
||||
for(a=1; a<argc; a++)
|
||||
{
|
||||
if(0 == strcmp(argv[a], "wid"))
|
||||
lcd_wid = atoi(argv[++a]);
|
||||
else if(0 == strcmp(argv[a], "hgt"))
|
||||
lcd_hgt = atoi(argv[++a]);
|
||||
else if(0 == strcmp(argv[a], "cellwid"))
|
||||
lcd_cellwid = atoi(argv[++a]);
|
||||
else if(0 == strcmp(argv[a], "cellhgt"))
|
||||
lcd_cellhgt = atoi(argv[++a]);
|
||||
}
|
||||
}
|
||||
else if(0 == strcmp(argv[0], "bye"))
|
||||
{
|
||||
//printf("Exiting LCDproc\n");
|
||||
exit_program(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
int j;
|
||||
for(j=0; j<argc; j++)
|
||||
printf("%s ", argv[j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
argc = 0; newtoken = 1;
|
||||
}
|
||||
}
|
||||
|
||||
len = sock_recv(sock, buf, 8000);
|
||||
//debug("\n");
|
||||
}
|
||||
|
||||
// Gather stats...
|
||||
// Update screens...
|
||||
for(i=0; sequence[i].which > 1; i++)
|
||||
{
|
||||
sequence[i].timer++;
|
||||
if(sequence[i].visible)
|
||||
{
|
||||
if(sequence[i].timer >= sequence[i].on_time)
|
||||
{
|
||||
sequence[i].timer = 0;
|
||||
// Now, update the screen...
|
||||
update_screen(sequence+i, 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(sequence[i].show_invisible)
|
||||
{
|
||||
if(sequence[i].timer >= sequence[i].off_time)
|
||||
{
|
||||
sequence[i].timer = 0;
|
||||
// Now, update the screen...
|
||||
update_screen(sequence+i, 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(sequence[i].timer >= sequence[i].off_time)
|
||||
{
|
||||
sequence[i].timer = 0;
|
||||
// Now, update the screen...
|
||||
update_screen(sequence+i, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now sleep...
|
||||
usleep(TIME_UNIT);
|
||||
|
||||
/* Old stuff
|
||||
timer=0;
|
||||
for(j=0; hold || (j<sequence[i].num_times && !Quit); j++)
|
||||
{
|
||||
switch(sequence[i].which)
|
||||
{
|
||||
case 'g':
|
||||
case 'G': status = cpu_graph_screen(j); break;
|
||||
case 'c':
|
||||
case 'C': status = cpu_screen(j); break;
|
||||
case 'o':
|
||||
case 'O': status = clock_screen(j); break;
|
||||
case 'k':
|
||||
case 'K': status = big_clock_screen(j); break;
|
||||
case 'm':
|
||||
case 'M': status = mem_screen(j); break;
|
||||
case 'u':
|
||||
case 'U': status = uptime_screen(j); break;
|
||||
case 't':
|
||||
case 'T': status = time_screen(j); break;
|
||||
case 'd':
|
||||
case 'D': status = disk_screen(j); break;
|
||||
case 'x':
|
||||
case 'X': status = xload_screen(j); break;
|
||||
case 'b':
|
||||
case 'B': status = battery_screen(j); break;
|
||||
case 'a':
|
||||
case 'A': status = credit_screen(j); break;
|
||||
default: status = dumbass_screen(j); break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
for(k=0; k<sequence[i].delay_time; k++)
|
||||
{
|
||||
usleep(TIME_UNIT); timer++;
|
||||
// Modify lcd status here... (blinking, for example)
|
||||
switch(status)
|
||||
{
|
||||
case BLINK_ON : blink=1; break;
|
||||
case BLINK_OFF: blink=0; lcd.backlight(1); break;
|
||||
case BACKLIGHT_OFF: blink=0; lcd.backlight(0); break;
|
||||
case BACKLIGHT_ON : blink=0; lcd.backlight(1); break;
|
||||
case CONTINUE : hold=0; break;
|
||||
}
|
||||
status=0;
|
||||
|
||||
if(blink) lcd.backlight(! ((timer&15) == 15));
|
||||
|
||||
if(heartbeat)
|
||||
{
|
||||
// Set this to pulsate like a real heart beat...
|
||||
// (binary is fun... :)
|
||||
lcd.icon(!((timer+4)&5), 0);
|
||||
lcd.chr(lcd.wid, 1, 0);
|
||||
}
|
||||
|
||||
lcd.flush();
|
||||
|
||||
PollSockets();
|
||||
}
|
||||
}
|
||||
i++;
|
||||
if(sequence[i].which < 2) i=0;
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
extern int Quit;
|
||||
extern int sock;
|
||||
extern char * version;
|
||||
extern char * build_date;
|
||||
|
||||
extern int lcd_wid;
|
||||
extern int lcd_hgt;
|
||||
extern int lcd_cellwid;
|
||||
extern int lcd_cellhgt;
|
||||
|
||||
|
||||
typedef struct mode {
|
||||
char which; // Which screen is it?
|
||||
int on_time; // How often to update while visible?
|
||||
int off_time; // How often to get stats while not visible?
|
||||
int show_invisible; // Send stats while not visible?
|
||||
int timer; // Time since last update
|
||||
int visible; // Can we be seen right now?
|
||||
} mode;
|
||||
|
||||
|
||||
|
||||
#define BLINK_ON 0x10
|
||||
#define BLINK_OFF 0x11
|
||||
#define BACKLIGHT_OFF 0x20
|
||||
#define BACKLIGHT_ON 0x21
|
||||
#define HOLD_SCREEN 0x30
|
||||
#define CONTINUE 0x31
|
||||
|
||||
|
||||
#define LCD_MAX_WIDTH 80
|
||||
#define LCD_MAX_HEIGHT 80
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,466 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifdef IRIX
|
||||
#include <strings.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include "../../shared/sockets.h"
|
||||
#include "../../shared/LL.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "mode.h"
|
||||
#include "mem.h"
|
||||
|
||||
struct meminfo { int total, cache, buffers, free, shared; };
|
||||
|
||||
int meminfo_fd = 0;
|
||||
|
||||
static void get_mem_info(struct meminfo *result);
|
||||
|
||||
static void get_mem_info(struct meminfo *result)
|
||||
{
|
||||
// int i, res; char *bufptr;
|
||||
|
||||
reread(meminfo_fd, "get_meminfo:");
|
||||
result[0].total = getentry("MemTotal:", buffer);
|
||||
result[0].free = getentry("MemFree:", buffer);
|
||||
result[0].shared = getentry("MemShared:", buffer);
|
||||
result[0].buffers = getentry("Buffers:", buffer);
|
||||
result[0].cache = getentry("Cached:", buffer);
|
||||
result[1].total = getentry("SwapTotal:", buffer);
|
||||
result[1].free = getentry("SwapFree:", buffer);
|
||||
}
|
||||
|
||||
|
||||
int mem_init()
|
||||
{
|
||||
if(!meminfo_fd)
|
||||
{
|
||||
meminfo_fd = open("/proc/meminfo",O_RDONLY);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mem_close()
|
||||
{
|
||||
if(meminfo_fd)
|
||||
meminfo_fd = open("/proc/meminfo",O_RDONLY);
|
||||
|
||||
meminfo_fd = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// Mem Screen displays info about memory and swap usage...
|
||||
//
|
||||
int mem_screen(int rep, int display)
|
||||
{
|
||||
int n;
|
||||
struct meminfo mem[2];
|
||||
static int first = 1;
|
||||
static int which_title=0;
|
||||
float value;
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sock_send_string(sock, "screen_add M\n");
|
||||
sprintf(buffer, "screen_set M name {Memory & Swap: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sock_send_string(sock, "widget_add M title title\n");
|
||||
sock_send_string(sock, "widget_set M title { MEM -==- SWAP}\n");
|
||||
sock_send_string(sock, "widget_add M totl string\n");
|
||||
sock_send_string(sock, "widget_add M used string\n");
|
||||
sock_send_string(sock, "widget_set M totl 9 2 Totl\n");
|
||||
sock_send_string(sock, "widget_set M used 9 3 Free\n");
|
||||
sock_send_string(sock, "widget_add M EF string\n");
|
||||
sock_send_string(sock, "widget_set M EF 1 4 {E F E F}\n");
|
||||
sock_send_string(sock, "widget_add M memused string\n");
|
||||
sock_send_string(sock, "widget_add M swapused string\n");
|
||||
//sock_send_string(sock, "widget_set M memgauge 2 4 0\n");
|
||||
//sock_send_string(sock, "widget_set M swapgauge 13 4 0\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
sock_send_string(sock, "widget_add M m string\n");
|
||||
sock_send_string(sock, "widget_add M s string\n");
|
||||
if(lcd_wid >= 20)
|
||||
{
|
||||
sock_send_string(sock, "widget_set M m 1 1 {M [ ]}\n");
|
||||
sock_send_string(sock, "widget_set M s 1 2 {S [ ]}\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
sock_send_string(sock, "widget_set M m 1 1 {M [ ]}\n");
|
||||
sock_send_string(sock, "widget_set M s 1 2 {S [ ]}\n");
|
||||
}
|
||||
sock_send_string(sock, "widget_add M mem% string\n");
|
||||
sock_send_string(sock, "widget_add M swap% string\n");
|
||||
sock_send_string(sock, "widget_set M mem% 16 1 { 0.0%}\n");
|
||||
sock_send_string(sock, "widget_set M swap% 16 2 { 0.0%}\n");
|
||||
|
||||
//sock_send_string(sock, "widget_set M memgauge 8 1 0\n");
|
||||
//sock_send_string(sock, "widget_set M swapgauge 8 2 0\n");
|
||||
}
|
||||
|
||||
sock_send_string(sock, "widget_add M memtotl string\n");
|
||||
sock_send_string(sock, "widget_add M swaptotl string\n");
|
||||
|
||||
sock_send_string(sock, "widget_add M memgauge hbar\n");
|
||||
sock_send_string(sock, "widget_add M swapgauge hbar\n");
|
||||
|
||||
//sock_send_string(sock, "\n");
|
||||
}
|
||||
|
||||
|
||||
get_mem_info(mem);
|
||||
|
||||
|
||||
// flip the title back and forth...
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
if(which_title & 4)
|
||||
{
|
||||
sprintf(buffer, "widget_set M title {%s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
}
|
||||
else sock_send_string(sock, "widget_set M title { MEM -==- SWAP}\n");
|
||||
which_title = (which_title + 1)&7;
|
||||
|
||||
|
||||
// Total memory
|
||||
sprintf(tmp, "widget_set M memtotl 1 2 {%6dk}\n", mem[0].total);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
// Free memory (plus buffers and cache)
|
||||
sprintf(tmp, "widget_set M memused 1 3 {%6dk}\n",
|
||||
mem[0].free + mem[0].buffers + mem[0].cache);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
// Total swap
|
||||
sprintf(tmp, "widget_set M swaptotl 14 2 {%6dk}\n", mem[1].total);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
// Free swap
|
||||
sprintf(tmp, "widget_set M swapused 14 3 {%6dk}\n", mem[1].free);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
// Free memory graph
|
||||
n = (int)(35.0 -
|
||||
((float)mem[0].free + (float)mem[0].buffers + (float)mem[0].cache)
|
||||
/ (float)mem[0].total
|
||||
* 35.0);
|
||||
sprintf(tmp, "widget_set M memgauge 2 4 %i\n", n);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
// Free swap graph
|
||||
n = (int)(35.0 -
|
||||
(float)mem[1].free / (float)mem[1].total
|
||||
* 35.0);
|
||||
sprintf(tmp, "widget_set M swapgauge 13 4 %i\n", n);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Total memory
|
||||
sprintf(tmp, "widget_set M memtotl 2 1 {%4dM}\n",
|
||||
(int)(mem[0].total / 1024.0));
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
// Total swap
|
||||
sprintf(tmp, "widget_set M swaptotl 2 2 {%4dM}\n",
|
||||
(int)(mem[1].total / 1024.0));
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
// Free memory graph
|
||||
value = ((float)mem[0].free + (float)mem[0].buffers + (float)mem[0].cache)
|
||||
/ (float)mem[0].total;
|
||||
value = 1.0 - value;
|
||||
n = (int)((lcd_cellwid * (float)(lcd_wid-13))*(value));
|
||||
sprintf(tmp, "widget_set M memgauge 8 1 %i\n", n);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
value *= 100.0;
|
||||
if (value >= 99.9) { sprintf(buffer, "100%%"); }
|
||||
else { sprintf(buffer, "%4.1f%%", value); }
|
||||
sprintf(tmp, "widget_set M mem%% %i 1 {%s}\n", lcd_wid-4, buffer);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
// Free swap graph
|
||||
value = ((float)mem[1].free / (float)mem[1].total);
|
||||
value = 1.0 - value;
|
||||
n = (int)((lcd_cellwid * (float)(lcd_wid-13))*(value));
|
||||
sprintf(tmp, "widget_set M swapgauge 8 2 %i\n", n);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
value *= 100.0;
|
||||
if (value >= 99.9) { sprintf(buffer, "100%%"); }
|
||||
else { sprintf(buffer, "%4.1f%%", value); }
|
||||
sprintf(tmp, "widget_set M swap%% %i 2 {%s}\n", lcd_wid-4, buffer);
|
||||
if(display) sock_send_string(sock, tmp);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
} // End mem_screen()
|
||||
|
||||
|
||||
|
||||
typedef struct proc_mem_info
|
||||
{
|
||||
char name[16]; // Is this really long enough?
|
||||
// Size isn't used any more...
|
||||
// Totl stores the "size" of the program now...
|
||||
int size, totl;
|
||||
int number;
|
||||
} proc_mem_info;
|
||||
|
||||
|
||||
static int sort_procs(void *a, void *b)
|
||||
{
|
||||
proc_mem_info *one, *two;
|
||||
|
||||
if(!a) return 0;
|
||||
if(!b) return 0;
|
||||
|
||||
one = (proc_mem_info *)a;
|
||||
two = (proc_mem_info *)b;
|
||||
|
||||
return (two->totl > one->totl);
|
||||
}
|
||||
|
||||
|
||||
int mem_top_screen(int rep, int display)
|
||||
{
|
||||
// Much of this code was ripped from "gmemusage"
|
||||
char buf[128];
|
||||
|
||||
DIR *proc;
|
||||
FILE *StatusFile;
|
||||
struct dirent * procdir;
|
||||
|
||||
char procName[16];
|
||||
int
|
||||
procSize ,
|
||||
procRSS ,
|
||||
procData ,
|
||||
procStk ,
|
||||
procExe ;
|
||||
const char
|
||||
*NameLine = "Name:" ,
|
||||
*VmSizeLine = "VmSize:" ,
|
||||
*VmRSSLine = "VmRSS" ,
|
||||
*VmDataLine = "VmData" ,
|
||||
*VmStkLine = "VmStk" ,
|
||||
*VmExeLine = "VmExe" ;
|
||||
const int
|
||||
NameLineLen = strlen ( NameLine ) ,
|
||||
VmSizeLineLen = strlen ( VmSizeLine ) ,
|
||||
VmDataLineLen = strlen ( VmDataLine ) ,
|
||||
VmStkLineLen = strlen ( VmStkLine ) ,
|
||||
VmExeLineLen = strlen ( VmExeLine ) ,
|
||||
VmRSSLineLen = strlen ( VmRSSLine ) ;
|
||||
|
||||
|
||||
int threshold = 400, unique;
|
||||
int i;
|
||||
proc_mem_info *p;
|
||||
LL * procs;
|
||||
static int first = 1;
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sock_send_string(sock, "screen_add S\n");
|
||||
sprintf(buffer, "screen_set S name {Top Memory Use: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add S title title\n");
|
||||
sprintf(buffer, "widget_set S title {TOP MEM: %s}\n", host);
|
||||
sock_send_string(sock, buffer);
|
||||
sock_send_string(sock, "widget_add S f frame\n");
|
||||
if(lcd_hgt >= 4)
|
||||
sock_send_string(sock, "widget_set S f 1 2 20 4 20 5 v 8\n");
|
||||
else
|
||||
sock_send_string(sock, "widget_set S f 1 2 20 2 20 5 v 16\n");
|
||||
for(i=1; i<=5; i++)
|
||||
{
|
||||
sprintf(buffer, "widget_add S %i string -in f\n", i);
|
||||
sock_send_string(sock, buffer);
|
||||
}
|
||||
sock_send_string(sock, "widget_set S 1 1 1 Checking...\n");
|
||||
}
|
||||
|
||||
|
||||
procs = LL_new();
|
||||
if(!procs)
|
||||
{
|
||||
fprintf(stderr, "mem_top_screen: Error allocating list\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if ( ( proc = opendir ( "/proc" ) ) == NULL )
|
||||
{
|
||||
fprintf ( stderr , "mem_top_screen: unable to open /proc" ) ;
|
||||
perror ( "" ) ;
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ( (procdir = readdir ( proc )) )
|
||||
{
|
||||
if ( !index ( "1234567890" , procdir -> d_name [0] ) )
|
||||
{
|
||||
continue ;
|
||||
}
|
||||
sprintf ( buf , "/proc/%s/status" , procdir -> d_name ) ;
|
||||
if ( ( StatusFile = fopen ( buf , "r" ) ) == NULL )
|
||||
{
|
||||
// Not a serious error; process has finished before we could
|
||||
// examine it:
|
||||
//fprintf ( stderr , "mem_top_screen: cannot open %s for reading" ,
|
||||
// buf ) ;
|
||||
//perror ( "" ) ;
|
||||
continue ;
|
||||
}
|
||||
procRSS = procSize = procData = procStk = procExe = 0 ;
|
||||
while ( fgets ( buf , sizeof ( buf ) , StatusFile ) )
|
||||
{
|
||||
if ( !strncmp ( buf , NameLine , NameLineLen ) )
|
||||
{
|
||||
/* Name: procName */
|
||||
sscanf ( buf , "%*s %s" , procName ) ;
|
||||
}
|
||||
else if ( !strncmp ( buf , VmSizeLine , VmSizeLineLen ) )
|
||||
{
|
||||
/* VmSize: procSize kB */
|
||||
sscanf ( buf , "%*s %d" , &procSize ) ;
|
||||
}
|
||||
else if ( !strncmp ( buf , VmRSSLine , VmRSSLineLen ) )
|
||||
{
|
||||
/* VmRSS: procRSS kB */
|
||||
sscanf ( buf , "%*s %d" , &procRSS ) ;
|
||||
}
|
||||
else if ( !strncmp ( buf , VmDataLine , VmDataLineLen ) )
|
||||
{
|
||||
/* VmData: procData kB */
|
||||
sscanf ( buf , "%*s %d" , &procData ) ;
|
||||
}
|
||||
else if ( !strncmp ( buf , VmStkLine , VmStkLineLen ) )
|
||||
{
|
||||
/* VmStk: procStk kB */
|
||||
sscanf ( buf , "%*s %d" , &procStk ) ;
|
||||
}
|
||||
else if ( !strncmp ( buf , VmExeLine , VmExeLineLen ) )
|
||||
{
|
||||
/* VmExe: procExe kB */
|
||||
sscanf ( buf , "%*s %d" , &procExe ) ;
|
||||
}
|
||||
}
|
||||
fclose ( StatusFile ) ;
|
||||
if(procSize > threshold)
|
||||
{
|
||||
// Figure out if it's sharing any memory...
|
||||
unique = 1;
|
||||
LL_Rewind(procs);
|
||||
do {
|
||||
p = LL_Get(procs);
|
||||
if(p)
|
||||
{
|
||||
if(0 == strcmp(p->name, procName))
|
||||
{
|
||||
unique = 0;
|
||||
p->number ++;
|
||||
p->totl += procData + procStk + procExe;
|
||||
}
|
||||
}
|
||||
} while(LL_Next(procs) == 0);
|
||||
|
||||
// If this is the first one by this name...
|
||||
if(unique)
|
||||
{
|
||||
p = malloc(sizeof(proc_mem_info));
|
||||
if(!p)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"mem_top_screen: Error allocating process entry\n");
|
||||
goto end; // Ack! I hate goto's!
|
||||
}
|
||||
strcpy(p->name, procName);
|
||||
p->size = procSize;
|
||||
p->totl = procData + procStk + procExe;
|
||||
p->number = 1;
|
||||
// TODO: Check for errors here?
|
||||
LL_Push(procs, (void *)p);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
closedir ( proc ) ;
|
||||
|
||||
|
||||
// Now, print some info...
|
||||
LL_Rewind(procs);
|
||||
LL_Sort(procs, sort_procs);
|
||||
LL_Rewind(procs);
|
||||
for(i=1; i<=5; i++)
|
||||
{
|
||||
p = LL_Get(procs);
|
||||
if(p)
|
||||
{
|
||||
//printf("Mem hog: %s: %ik\n", p->name, p->size);
|
||||
if(p->number > 1)
|
||||
sprintf(buffer, "widget_set S %i 1 %i {%i%6ik %s(%i)}\n", i, i, i,
|
||||
p->totl, p->name, p->number);
|
||||
else
|
||||
sprintf(buffer, "widget_set S %i 1 %i {%i%6ik %s}\n", i, i, i,
|
||||
p->totl, p->name);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
//printf("Mem hog: none?\n");
|
||||
sprintf(buffer, "widget_set S %i 1 %i {}\n", i, i);
|
||||
if(display) sock_send_string(sock, buffer);
|
||||
}
|
||||
|
||||
LL_Next(procs);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
end: // Ack! I hate using labels!
|
||||
// Now clean it all up...
|
||||
LL_Rewind(procs);
|
||||
do {
|
||||
p = (proc_mem_info *)LL_Get(procs);
|
||||
if(p)
|
||||
{
|
||||
//printf("Proc: %6ik %s\n", p->size, p->name);
|
||||
free(p);
|
||||
}
|
||||
} while(LL_Next(procs) == 0);
|
||||
LL_Destroy(procs);
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
} // End mem_top_screen()
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef MEM_H
|
||||
#define MEM_H
|
||||
|
||||
int mem_init();
|
||||
int mem_close();
|
||||
|
||||
int mem_screen(int rep, int display);
|
||||
int mem_top_screen(int rep, int display);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,282 @@
|
||||
/*
|
||||
* This file contains status-gathering code *and* modescreen functions.
|
||||
* It's long, and messy.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifdef IRIX
|
||||
#include <strings.h>
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#include "../../shared/sockets.h"
|
||||
|
||||
#include "mode.h"
|
||||
#include "main.h"
|
||||
|
||||
#include "batt.h"
|
||||
#include "chrono.h"
|
||||
#include "cpu.h"
|
||||
#include "disk.h"
|
||||
#include "load.h"
|
||||
#include "mem.h"
|
||||
|
||||
|
||||
int ELLIPSIS='-';
|
||||
|
||||
// TODO: Clean this up... Support multiple display sizes..
|
||||
|
||||
char buffer[1024];
|
||||
char host[16];
|
||||
|
||||
void reread(int f, char *errmsg);
|
||||
int getentry(const char *tag, const char *bufptr);
|
||||
|
||||
|
||||
int mode_init(mode *sequence)
|
||||
{
|
||||
int i;
|
||||
struct utsname Uname;
|
||||
|
||||
// Grab the host name
|
||||
if(0 > uname(&Uname))
|
||||
{
|
||||
perror("Can't get hostname");
|
||||
strcpy(host, "host");
|
||||
}
|
||||
else
|
||||
{
|
||||
strncpy(host, Uname.nodename, 15);
|
||||
host[16]=0;
|
||||
if(index(host,'.')) // truncate at the first dot
|
||||
*(index(host, '.'))='\0';
|
||||
for(i=0; i<16; i++)
|
||||
if(host[i] == '\n') host[i]=0;
|
||||
}
|
||||
|
||||
// Init all the modescreens we need
|
||||
for(i=0; sequence[i].which > 1; i++)
|
||||
{
|
||||
switch(sequence[i].which)
|
||||
{
|
||||
case 'g':
|
||||
case 'G': // cpu_graph_screen
|
||||
cpu_init();
|
||||
break;
|
||||
case 'c':
|
||||
case 'C': // cpu_screen
|
||||
cpu_init();
|
||||
break;
|
||||
case 'o':
|
||||
case 'O': // clock_screen
|
||||
chrono_init();
|
||||
break;
|
||||
case 'k':
|
||||
case 'K': // big_clock_screen
|
||||
chrono_init();
|
||||
break;
|
||||
case 'm':
|
||||
case 'M': // mem_screen
|
||||
mem_init();
|
||||
break;
|
||||
case 's':
|
||||
case 'S': // mem_info_screen
|
||||
mem_init();
|
||||
break;
|
||||
case 'u':
|
||||
case 'U': // uptime_screen
|
||||
chrono_init();
|
||||
break;
|
||||
case 't':
|
||||
case 'T': // time_screen
|
||||
chrono_init();
|
||||
break;
|
||||
case 'd':
|
||||
case 'D': // disk_screen
|
||||
disk_init();
|
||||
break;
|
||||
case 'x':
|
||||
case 'X': // xload_screen
|
||||
load_init();
|
||||
break;
|
||||
case 'b':
|
||||
case 'B': // battery_screen
|
||||
batt_init();
|
||||
break;
|
||||
case 'a':
|
||||
case 'A': // credit_screen
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mode_close()
|
||||
{
|
||||
batt_close();
|
||||
chrono_close();
|
||||
cpu_close();
|
||||
disk_close();
|
||||
load_close();
|
||||
mem_close();
|
||||
|
||||
}
|
||||
|
||||
|
||||
int update_screen(mode *m, int display)
|
||||
{
|
||||
static int status = -1;
|
||||
int old_status = status;
|
||||
|
||||
if(m)
|
||||
{
|
||||
switch(m->which)
|
||||
{
|
||||
case 'g':
|
||||
case 'G': status = cpu_graph_screen(m->timer, display); break;
|
||||
case 'c':
|
||||
case 'C': status = cpu_screen(m->timer, display); break;
|
||||
case 'o':
|
||||
case 'O': status = clock_screen(m->timer, display); break;
|
||||
case 'k':
|
||||
case 'K': status = big_clock_screen(m->timer, display); break;
|
||||
case 'm':
|
||||
case 'M': status = mem_screen(m->timer, display); break;
|
||||
case 's':
|
||||
case 'S': status = mem_top_screen(m->timer, display); break;
|
||||
case 'u':
|
||||
case 'U': status = uptime_screen(m->timer, display); break;
|
||||
case 't':
|
||||
case 'T': status = time_screen(m->timer, display); break;
|
||||
case 'd':
|
||||
case 'D': status = disk_screen(m->timer, display); break;
|
||||
case 'x':
|
||||
case 'X': status = xload_screen(m->timer, display); break;
|
||||
case 'b':
|
||||
case 'B': status = battery_screen(m->timer, display); break;
|
||||
case 'a':
|
||||
case 'A': status = credit_screen(m->timer, display); break;
|
||||
default: break;
|
||||
}
|
||||
/* Debugging tool...
|
||||
if(display)
|
||||
{
|
||||
printf("Displayed Mode %c\n", m->which);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
if(status != old_status)
|
||||
{
|
||||
if(status == BACKLIGHT_OFF)
|
||||
{
|
||||
sock_send_string(sock, "backlight off\n");
|
||||
}
|
||||
if(status == BACKLIGHT_ON)
|
||||
{
|
||||
sock_send_string(sock, "backlight on\n");
|
||||
}
|
||||
if(status == BLINK_ON)
|
||||
{
|
||||
sock_send_string(sock, "backlight blink\n");
|
||||
}
|
||||
}
|
||||
return status;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void reread(int f, char *errmsg)
|
||||
{
|
||||
if (lseek(f, 0L, 0) == 0 && read(f, buffer, sizeof(buffer) - 1 ) > 0 )
|
||||
return;
|
||||
perror(errmsg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int getentry(const char *tag, const char *bufptr)
|
||||
{
|
||||
char *tail;
|
||||
int retval, len = strlen(tag);
|
||||
|
||||
while (bufptr)
|
||||
{
|
||||
if (*bufptr == '\n') bufptr++;
|
||||
if (!strncmp(tag, bufptr, len))
|
||||
{
|
||||
retval = strtol(bufptr + len, &tail, 10);
|
||||
if (tail == bufptr + len) return -1;
|
||||
else return retval;
|
||||
}
|
||||
bufptr = strchr( bufptr, '\n');
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////// Let the Modes Begin! /////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Er, these have been moved.. :)
|
||||
|
||||
char tmp[1024];
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Credit Screen shows who wrote this...
|
||||
//
|
||||
int credit_screen(int rep, int display)
|
||||
{
|
||||
static int first = 1;
|
||||
|
||||
if(first)
|
||||
{
|
||||
first = 0;
|
||||
|
||||
sock_send_string(sock, "screen_add A\n");
|
||||
sock_send_string(sock,
|
||||
"screen_set A name {Credits for LCDproc}\n");
|
||||
sock_send_string(sock, "widget_add A title title\n");
|
||||
sprintf(tmp, "widget_set A title {LCDPROC %s}\n", version);
|
||||
sock_send_string(sock, tmp);
|
||||
if(lcd_hgt >= 4)
|
||||
{
|
||||
sock_send_string(sock, "widget_add A one string\n");
|
||||
sock_send_string(sock, "widget_add A two string\n");
|
||||
sock_send_string(sock, "widget_add A three string\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set A one 1 2 { for Linux }\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set A two 1 3 { by William Ferrell}\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set A three 1 4 { and Scott Scriven}\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
sock_send_string(sock, "widget_add A text scroller\n");
|
||||
sock_send_string(sock,
|
||||
"widget_set A text 1 2 20 2 v 8 { for Linux by William Ferrell and Scott Scriven}\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
} // End credit_screen()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef MODE_H
|
||||
#define MODE_H
|
||||
|
||||
#include "main.h"
|
||||
|
||||
//TODO: Net stats screen...
|
||||
//TODO: "Who"
|
||||
//TODO: biff / mail checking, etc...
|
||||
|
||||
extern char tmp[];
|
||||
extern char buffer[];
|
||||
extern char host[];
|
||||
|
||||
// Character to use for padding title bars, etc...
|
||||
extern int PAD;
|
||||
// Character for the "..." symbol.
|
||||
extern int ELLIPSIS;
|
||||
|
||||
int mode_init(mode *sequence);
|
||||
void mode_close();
|
||||
|
||||
int update_screen(mode *m, int display);
|
||||
|
||||
int credit_screen(int rep, int display);
|
||||
|
||||
|
||||
// These are for the modescreens' use only..
|
||||
void reread(int f, char *errmsg);
|
||||
int getentry(const char *tag, const char *bufptr);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user