add comments, use getopt, cleanup, ...

This commit is contained in:
marschap
2006-01-18 21:17:39 +00:00
parent 5b4c15c2a6
commit 34cf0696ae
3 changed files with 163 additions and 158 deletions
+6 -6
View File
@@ -32,7 +32,7 @@
# The name of the driver is exactly the same as the name of the driver
# module that is to be loaded, including the case of the letters !
# (That is: unless a file= option is given in the driver section)
Driver=curses
Driver=CFontzPacket
#Driver=hd44780
#Driver=IOWarrior
#Driver=MtxOrb
@@ -187,13 +187,13 @@ Reboot=yes
[CFontzPacket]
# Select the LCD model [default: 633; legal: 631, 633, 635]
Model=633
Model=635
# Select the output device to use [default: /dev/lcd]
Device=/dev/ttyS0
Device=/dev/ttyUSB0
# Select the LCD size [default: depending on model: 635: 20x4, 631 & 633: 16x2]
Size=16x2
Size=20x4
# Set the initial contrast [default: 140; legal: 0 - 1000]
Contrast=140
Contrast=350
# Set the initial brightness [default: 1000; legal: 0 - 1000]
Brightness=1000
# Set the initial off-brightness [default: 0; legal: 0 - 1000]
@@ -201,7 +201,7 @@ Brightness=1000
# switched off in case LCDd is inactive
OffBrightness=50
# Set the communication speed [default: 9600; legal: 1200, 2400, 9600, 19200, 115200]
Speed=19200
Speed=115200
# Set the firmware version (New means >= 2.0) [default: no; legal: yes, no]
# Currently this flag is not in use, there is no such thing as NewFirmware. ;=)
#NewFirmware=no
+142 -136
View File
@@ -1,3 +1,5 @@
#include "getopt.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
@@ -12,7 +14,7 @@
#include <sys/param.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
# include "config.h"
#endif
#include "main.h"
@@ -49,29 +51,29 @@ static int islow = -1;
// 1/8th second is a single time unit...
#define TIME_UNIT 125000
// Contains a list of modes to run
/* list of modes to run */
static 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)
// longname which on off inv timer visible
{ "CPU", 'C', 1, 2, 0, 0xffff, 0,}, // [C]PU
{ "Memory", 'M', 4, 16, 0, 0xffff, 0,}, // [M]emory
{ "Load", 'X', 64, 128, 1, 0xffff, 0,}, // [X]-load (load histogram)
{ "TimeDate", 'T', 4, 64, 0, 0xffff, 0,}, // [T]ime/Date
{ "About", '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...
{ NULL, 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.
{'P', 1, 2, 0, 0xffff, 0,}, // [O]ld Timescreen
{'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
{'E', 4, 64, 0, 0xffff, 0,}, // [E]ssential clock
{0, 0, 0, 0, 0,}, // No more.. all done.
{ "SMP-CPU", 'P', 1, 2, 0, 0xffff, 0,}, // CPU_SM[P]
{ "OldTime", 'O', 4, 64, 0, 0xffff, 0,}, // [O]ld Timescreen
{ "BigClock", 'K', 4, 64, 0, 0xffff, 0,}, // big cloc[K]
{ "Uptime", 'U', 4, 128, 0, 0xffff, 0,}, // Old [U]ptime Screen
{ "Battery", 'B', 32, 256, 1, 0xffff, 0,}, // [B]attery Status
{ "CPUGraph", 'G', 1, 2, 0, 0xffff, 0,}, // CPU histogram [G]raph
{ "ProcSize", 'S', 16, 256, 1, 0xffff, 0,}, // [S]ize of biggest programs
{ "Disk", 'D', 256, 256, 1, 0xffff, 0,}, // [D]isk stats
{ "MiniClock", 'E', 4, 64, 0, 0xffff, 0,}, // [E]ssential clock
{ NULL, 0, 0, 0, 0, 0,}, // No more.. all done.
};
// TODO: Config file; not just command line
@@ -94,129 +96,130 @@ const char *get_sysrelease()
int
main(int argc, char **argv)
{
int i, j;
int c;
char *server = NULL;
int i, j, seqlen;
int port = LCDPORT;
int daemonize = FALSE;
/* determine length of default_sequence[] */
int seqlen = sizeof(default_sequence) / sizeof(mode);
if(uname(&unamebuf) == -1)
{
/* get uname information */
if (uname(&unamebuf) == -1) {
perror("uname");
return(EXIT_FAILURE);
}
seqlen = 0;
while(default_sequence[seqlen].which != 0)
seqlen++;
seqlen++;
sequence = (mode*)malloc(seqlen*sizeof(mode));
if(sequence == NULL)
{
/* allocate sequence */
sequence = (mode *) malloc(seqlen * sizeof(mode));
if (sequence == NULL) {
perror("sequence malloc");
return(EXIT_FAILURE);
}
for(i = 0; i < seqlen; i++)
{
/* copy default_sequence[] to sequence */
for (i = 0; i < seqlen; i++) {
//sequence[i] = default_sequence[i];
memcpy(&sequence[i], &default_sequence[i], sizeof(mode));
}
// 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);
/* setup error handlers */
signal(SIGINT, exit_program); // Ctrl-C
signal(SIGTERM, exit_program); // "regular" kill
signal(SIGHUP, exit_program); // kill -HUP
signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but ...]
// Command line
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();
if(server == NULL)
server = argv[++i];
else
fprintf(stderr, "Ignoring additional server: %s\n", 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 %d outside of standard range\n", port);
break;
case 'e':
case 'E':
if(argc < i + 1)
HelpScreen();
islow = atoi(argv[++i]);
break;
case 'd':
case 'D':
daemonize = TRUE;
break;
// otherwise... Get help!
default:
HelpScreen();
break;
/* No error output from getopt */
opterr = 0;
/* get options */
while ((c = getopt( argc, argv, "s:p:e:d")) > 0) {
switch (c) {
// s is for server
case 's':
if (server == NULL)
server = optarg;
else
fprintf(stderr, "Ignoring additional server: %s\n", optarg);
break;
// p is for port
case 'p':
port = atoi(optarg);
if ((port < 1) && (port > 0xFFFF)) {
fprintf(stderr, "Warning: Port %d outside of legal range\n", port);
return(EXIT_FAILURE);
}
break;
case 'e':
islow = atoi(optarg);
break;
case 'd':
daemonize = TRUE;
break;
// otherwise... Get help!
case '?': // unknown option or missing argument
default:
HelpScreen();
break;
}
}
/* parse arguments */
for (i = (optind > 0) ? optind : 1, j = 0; i < argc; i++) {
int shortname = (strlen(argv[i]) == 1) ? toupper(argv[i][0]) : '\0';
int k, found = FALSE;
/* skip this round if we used all allocated slots */
if (j >= seqlen)
continue;
/* ignore already selected modes */
for (k = 0; (k < j) && (sequence[k].which != 0); k++) {
if (((sequence[k].longname != NULL) &&
(0 == strcasecmp(argv[i], sequence[k].longname))) ||
(shortname == sequence[k].which)) {
found = TRUE;
}
}
else if(strlen(argv[i]) == 1)
{
int k, found = FALSE;
if (found) {
fprintf(stderr, "Warning: ignoring duplicate mode\n");
continue;
}
if(j >= seqlen)
continue;
// Grab the mode letter...
sequence[j].which = argv[i][0];
for(k = 0; k < seqlen; k++)
{
if(toupper(sequence[j].which) == default_sequence[k].which)
{
//sequence[j] = default_sequence[k];
memcpy(&sequence[j], &default_sequence[k], sizeof(mode));
found = TRUE;
break;
}
}
if(!found)
{
fprintf(stderr, "Invalid Mode: %c, ignoring\n", argv[i][0]);
}
else
{
/* try to find the mode from the mode list */
for (k = 0; k < seqlen; k++) {
if (((default_sequence[k].longname != NULL) &&
(0 == strcasecmp(argv[i], default_sequence[k].longname))) ||
(shortname == default_sequence[k].which)) {
//sequence[j] = default_sequence[k];
memcpy(&sequence[j], &default_sequence[k], sizeof(mode));
j++;
memcpy(&sequence[j], &default_sequence[seqlen-1], sizeof(mode));
//sequence[j] = default_sequence[seqlen-1];
memcpy(&sequence[j], &default_sequence[seqlen-1], sizeof(mode));
found = TRUE;
break;
}
}
if (!found) {
fprintf(stderr, "Invalid Mode: %c, ignoring\n", argv[i][0]);
return(EXIT_FAILURE);
}
}
if(server == NULL)
if (server == NULL)
server = "localhost";
// 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 LCD server %s on port %i.\nCheck to see that the server is running and operating normally.\n", server, port);
return 0;
if (sock <= 0) {
fprintf(stderr, "Error connecting to LCD server %s on port %d.\n"
"Check to see that the server is running and operating normally.\n",
server, port);
return(EXIT_FAILURE);
}
sock_send_string(sock, "hello\n");
usleep(500000); // wait for the server to say hi.
@@ -226,9 +229,12 @@ main(int argc, char **argv)
lcd_cellwid = 5;
lcd_cellhgt = 8;
if(daemonize)
if(daemon(1,0) != 0)
if (daemonize) {
if (daemon(1,0) != 0) {
fprintf(stderr, "Error: daemonize failed");
return(EXIT_FAILURE);
}
}
// Init the status gatherers...
mode_init();
@@ -289,89 +295,89 @@ main_loop()
// Main loop
// Run whatever screen we want, then wait. Woo-hoo!
while(!Quit)
while (!Quit)
{
// Check for server input...
len = sock_recv(sock, buf, 8000);
// Handle server input...
while(len > 0)
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++)
for (i = 0; i < len; i++)
{
switch(buf[i])
switch (buf[i])
{
case ' ':
newtoken = 1;
buf[i] = 0;
break;
default: // regular chars, keep tokenizing
if(newtoken)
if (newtoken)
argv[argc++] = buf + i;
newtoken = 0;
break;
case '\0':
case '\n':
buf[i] = 0;
if(argc > 0)
if (argc > 0)
{
//printf("%s %s\n", argv[0], argv[1]);
if(0 == strcmp(argv[0], "listen"))
if (0 == strcmp(argv[0], "listen"))
{
for(j = 0; sequence[j].which; j++)
{
if(sequence[j].which == argv[1][0])
if (sequence[j].which == argv[1][0])
{
sequence[j].visible = 1;
//debug("Listen %s\n", argv[1]);
}
}
}
else if(0 == strcmp(argv[0], "ignore"))
else if (0 == strcmp(argv[0], "ignore"))
{
for(j = 0; sequence[j].which; j++)
{
if(sequence[j].which == argv[1][0])
if (sequence[j].which == argv[1][0])
{
sequence[j].visible = 0;
//debug("Ignore %s\n", argv[1]);
}
}
}
else if(0 == strcmp(argv[0], "key"))
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], "menu"))
{
}
else if(0 == strcmp(argv[0], "connect"))
else if (0 == strcmp(argv[0], "connect"))
{
int a;
for(a = 1; a < argc; a++)
{
if(0 == strcmp(argv[a], "wid"))
if (0 == strcmp(argv[a], "wid"))
lcd_wid = atoi(argv[++a]);
else if(0 == strcmp(argv[a], "hgt"))
else if (0 == strcmp(argv[a], "hgt"))
lcd_hgt = atoi(argv[++a]);
else if(0 == strcmp(argv[a], "cellwid"))
else if (0 == strcmp(argv[a], "cellwid"))
lcd_cellwid = atoi(argv[++a]);
else if(0 == strcmp(argv[a], "cellhgt"))
else if (0 == strcmp(argv[a], "cellhgt"))
lcd_cellhgt = atoi(argv[++a]);
}
connected = 1;
sock_send_string(sock, "client_set -name LCDproc\n");
}
else if(0 == strcmp(argv[0], "bye"))
else if (0 == strcmp(argv[0], "bye"))
{
//printf("Exiting LCDproc\n");
exit_program(EXIT_SUCCESS);
}
else if(0 == strcmp(argv[0], "success"))
else if (0 == strcmp(argv[0], "success"))
{
}
else
@@ -401,9 +407,9 @@ main_loop()
for(i = 0; sequence[i].which > 1; i++)
{
sequence[i].timer++;
if(sequence[i].visible)
if (sequence[i].visible)
{
if(sequence[i].timer >= sequence[i].on_time)
if (sequence[i].timer >= sequence[i].on_time)
{
sequence[i].timer = 0;
// Now, update the screen...
@@ -412,14 +418,14 @@ main_loop()
}
else
{
if(sequence[i].timer >= sequence[i].off_time)
if (sequence[i].timer >= sequence[i].off_time)
{
sequence[i].timer = 0;
// Now, update the screen...
update_screen(&sequence[i], sequence[i].show_invisible);
}
}
if(islow > 0)
if (islow > 0)
usleep(islow * 10000);
}
}
+15 -16
View File
@@ -2,10 +2,10 @@
#define MAIN_H
#ifndef TRUE
#define TRUE 1
# define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
# define FALSE 0
#endif
#define LCDP_BATT_HIGH 0x00
@@ -15,8 +15,8 @@
#define LCDP_BATT_ABSENT 0x04
#define LCDP_BATT_UNKNOWN 0xFF
#define LCDP_AC_OFF 0x00
#define LCDP_AC_ON 0x01
#define LCDP_AC_OFF 0x00
#define LCDP_AC_ON 0x01
#define LCDP_AC_BACKUP 0x02
#define LCDP_AC_UNKNOWN 0xFF
@@ -32,20 +32,21 @@ 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?
char *longname; // Which screen is it?
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 BLINK_ON 0x10
#define BLINK_OFF 0x11
#define BACKLIGHT_OFF 0x20
#define BACKLIGHT_ON 0x21
#define HOLD_SCREEN 0x30
#define CONTINUE 0x31
#define HOLD_SCREEN 0x30
#define CONTINUE 0x31
#define LCD_MAX_WIDTH 80
#define LCD_MAX_HEIGHT 80
@@ -62,6 +63,4 @@ const char *get_sysrelease();
# define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#endif