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 # 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 ! # module that is to be loaded, including the case of the letters !
# (That is: unless a file= option is given in the driver section) # (That is: unless a file= option is given in the driver section)
Driver=curses Driver=CFontzPacket
#Driver=hd44780 #Driver=hd44780
#Driver=IOWarrior #Driver=IOWarrior
#Driver=MtxOrb #Driver=MtxOrb
@@ -187,13 +187,13 @@ Reboot=yes
[CFontzPacket] [CFontzPacket]
# Select the LCD model [default: 633; legal: 631, 633, 635] # Select the LCD model [default: 633; legal: 631, 633, 635]
Model=633 Model=635
# Select the output device to use [default: /dev/lcd] # 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] # 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] # Set the initial contrast [default: 140; legal: 0 - 1000]
Contrast=140 Contrast=350
# Set the initial brightness [default: 1000; legal: 0 - 1000] # Set the initial brightness [default: 1000; legal: 0 - 1000]
Brightness=1000 Brightness=1000
# Set the initial off-brightness [default: 0; legal: 0 - 1000] # Set the initial off-brightness [default: 0; legal: 0 - 1000]
@@ -201,7 +201,7 @@ Brightness=1000
# switched off in case LCDd is inactive # switched off in case LCDd is inactive
OffBrightness=50 OffBrightness=50
# Set the communication speed [default: 9600; legal: 1200, 2400, 9600, 19200, 115200] # 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] # 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. ;=) # Currently this flag is not in use, there is no such thing as NewFirmware. ;=)
#NewFirmware=no #NewFirmware=no
+117 -111
View File
@@ -1,3 +1,5 @@
#include "getopt.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
@@ -12,7 +14,7 @@
#include <sys/param.h> #include <sys/param.h>
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" # include "config.h"
#endif #endif
#include "main.h" #include "main.h"
@@ -49,29 +51,29 @@ static int islow = -1;
// 1/8th second is a single time unit... // 1/8th second is a single time unit...
#define TIME_UNIT 125000 #define TIME_UNIT 125000
// Contains a list of modes to run /* list of modes to run */
static mode default_sequence[] = static mode default_sequence[] =
{ {
// which on off inv timer/visible // longname which on off inv timer visible
{'C', 1, 2, 0, 0xffff, 0,}, // [C]PU { "CPU", 'C', 1, 2, 0, 0xffff, 0,}, // [C]PU
{'M', 4, 16, 0, 0xffff, 0,}, // [M]emory { "Memory", 'M', 4, 16, 0, 0xffff, 0,}, // [M]emory
{'X', 64, 128, 1, 0xffff, 0,}, // [X]-load (load histogram) { "Load", 'X', 64, 128, 1, 0xffff, 0,}, // [X]-load (load histogram)
{'T', 4, 64, 0, 0xffff, 0,}, // [T]ime/Date { "TimeDate", 'T', 4, 64, 0, 0xffff, 0,}, // [T]ime/Date
{'A', 999, 9999, 0, 0xffff, 0,}, // [A]bout (credits) { "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! // ... all non-default modes must be in here!
// ... they will not show up otherwise. // ... they will not show up otherwise.
{'P', 1, 2, 0, 0xffff, 0,}, // [O]ld Timescreen { "SMP-CPU", 'P', 1, 2, 0, 0xffff, 0,}, // CPU_SM[P]
{'O', 4, 64, 0, 0xffff, 0,}, // [O]ld Timescreen { "OldTime", 'O', 4, 64, 0, 0xffff, 0,}, // [O]ld Timescreen
{'K', 4, 64, 0, 0xffff, 0,}, // big cloc[K] { "BigClock", 'K', 4, 64, 0, 0xffff, 0,}, // big cloc[K]
{'U', 4, 128, 0, 0xffff, 0,}, // Old [U]ptime Screen { "Uptime", 'U', 4, 128, 0, 0xffff, 0,}, // Old [U]ptime Screen
{'B', 32, 256, 1, 0xffff, 0,}, // [B]attery Status { "Battery", 'B', 32, 256, 1, 0xffff, 0,}, // [B]attery Status
{'G', 1, 2, 0, 0xffff, 0,}, // Cpu histogram [G]raph { "CPUGraph", 'G', 1, 2, 0, 0xffff, 0,}, // CPU histogram [G]raph
{'S', 16, 256, 1, 0xffff, 0,}, // [S]ize of biggest programs { "ProcSize", 'S', 16, 256, 1, 0xffff, 0,}, // [S]ize of biggest programs
{'D', 256, 256, 1, 0xffff, 0,}, // [D]isk stats { "Disk", 'D', 256, 256, 1, 0xffff, 0,}, // [D]isk stats
{'E', 4, 64, 0, 0xffff, 0,}, // [E]ssential clock { "MiniClock", 'E', 4, 64, 0, 0xffff, 0,}, // [E]ssential clock
{0, 0, 0, 0, 0,}, // No more.. all done. { NULL, 0, 0, 0, 0, 0,}, // No more.. all done.
}; };
// TODO: Config file; not just command line // TODO: Config file; not just command line
@@ -94,129 +96,130 @@ const char *get_sysrelease()
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int i, j;
int c;
char *server = NULL; char *server = NULL;
int i, j, seqlen;
int port = LCDPORT; int port = LCDPORT;
int daemonize = FALSE; 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"); perror("uname");
return(EXIT_FAILURE); return(EXIT_FAILURE);
} }
seqlen = 0; /* allocate sequence */
while(default_sequence[seqlen].which != 0) sequence = (mode *) malloc(seqlen * sizeof(mode));
seqlen++; if (sequence == NULL) {
seqlen++;
sequence = (mode*)malloc(seqlen*sizeof(mode));
if(sequence == NULL)
{
perror("sequence malloc"); perror("sequence malloc");
return(EXIT_FAILURE); 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]; //sequence[i] = default_sequence[i];
memcpy(&sequence[i], &default_sequence[i], sizeof(mode)); memcpy(&sequence[i], &default_sequence[i], sizeof(mode));
} }
// Ctrl-C will cause a clean exit... /* setup error handlers */
signal(SIGINT, exit_program); signal(SIGINT, exit_program); // Ctrl-C
// and "kill"... signal(SIGTERM, exit_program); // "regular" kill
signal(SIGTERM, exit_program); signal(SIGHUP, exit_program); // kill -HUP
// and "kill -HUP" (hangup)... signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but ...]
signal(SIGHUP, exit_program);
// and just in case, "kill -KILL" (which cannot be trapped; but oh well)
signal(SIGKILL, exit_program);
// Command line /* No error output from getopt */
for(i = 1, j = 0; i < argc; i++) opterr = 0;
{
if(argv[i][0] == '-') /* get options */
{ while ((c = getopt( argc, argv, "s:p:e:d")) > 0) {
switch(argv[i][1]) switch (c) {
{
// s is for server // s is for server
case 'S':
case 's': case 's':
if(argc < i + 1) if (server == NULL)
HelpScreen(); server = optarg;
if(server == NULL)
server = argv[++i];
else else
fprintf(stderr, "Ignoring additional server: %s\n", argv[++i]); fprintf(stderr, "Ignoring additional server: %s\n", optarg);
break; break;
// p is for port // p is for port
case 'P':
case 'p': case 'p':
if(argc < i + 1) port = atoi(optarg);
HelpScreen(); if ((port < 1) && (port > 0xFFFF)) {
port = atoi(argv[++i]); fprintf(stderr, "Warning: Port %d outside of legal range\n", port);
if(port < 1 && port > 0xffff) return(EXIT_FAILURE);
fprintf(stderr, "Warning: Port %d outside of standard range\n", port); }
break; break;
case 'e': case 'e':
case 'E': islow = atoi(optarg);
if(argc < i + 1)
HelpScreen();
islow = atoi(argv[++i]);
break; break;
case 'd': case 'd':
case 'D':
daemonize = TRUE; daemonize = TRUE;
break; break;
// otherwise... Get help! // otherwise... Get help!
case '?': // unknown option or missing argument
default: default:
HelpScreen(); HelpScreen();
break; break;
} }
} }
else if(strlen(argv[i]) == 1)
{ /* 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; int k, found = FALSE;
if(j >= seqlen) /* skip this round if we used all allocated slots */
if (j >= seqlen)
continue; continue;
// Grab the mode letter... /* ignore already selected modes */
sequence[j].which = argv[i][0]; 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;
}
}
if (found) {
fprintf(stderr, "Warning: ignoring duplicate mode\n");
continue;
}
for(k = 0; k < seqlen; k++) /* try to find the mode from the mode list */
{ for (k = 0; k < seqlen; k++) {
if(toupper(sequence[j].which) == default_sequence[k].which) if (((default_sequence[k].longname != NULL) &&
{ (0 == strcasecmp(argv[i], default_sequence[k].longname))) ||
(shortname == default_sequence[k].which)) {
//sequence[j] = default_sequence[k]; //sequence[j] = default_sequence[k];
memcpy(&sequence[j], &default_sequence[k], sizeof(mode)); memcpy(&sequence[j], &default_sequence[k], sizeof(mode));
j++;
//sequence[j] = default_sequence[seqlen-1];
memcpy(&sequence[j], &default_sequence[seqlen-1], sizeof(mode));
found = TRUE; found = TRUE;
break; break;
} }
} }
if(!found) if (!found) {
{
fprintf(stderr, "Invalid Mode: %c, ignoring\n", argv[i][0]); fprintf(stderr, "Invalid Mode: %c, ignoring\n", argv[i][0]);
} return(EXIT_FAILURE);
else
{
j++;
memcpy(&sequence[j], &default_sequence[seqlen-1], sizeof(mode));
//sequence[j] = default_sequence[seqlen-1];
}
} }
} }
if(server == NULL) if (server == NULL)
server = "localhost"; server = "localhost";
// Connect to the server... // Connect to the server...
usleep(500000); // wait for the server to start up usleep(500000); // wait for the server to start up
sock = sock_connect(server, port); sock = sock_connect(server, port);
if(sock <= 0) if (sock <= 0) {
{ fprintf(stderr, "Error connecting to LCD server %s on port %d.\n"
printf("Error connecting to LCD server %s on port %i.\nCheck to see that the server is running and operating normally.\n", server, port); "Check to see that the server is running and operating normally.\n",
return 0; server, port);
return(EXIT_FAILURE);
} }
sock_send_string(sock, "hello\n"); sock_send_string(sock, "hello\n");
usleep(500000); // wait for the server to say hi. usleep(500000); // wait for the server to say hi.
@@ -226,9 +229,12 @@ main(int argc, char **argv)
lcd_cellwid = 5; lcd_cellwid = 5;
lcd_cellhgt = 8; lcd_cellhgt = 8;
if(daemonize) if (daemonize) {
if(daemon(1,0) != 0) if (daemon(1,0) != 0) {
fprintf(stderr, "Error: daemonize failed"); fprintf(stderr, "Error: daemonize failed");
return(EXIT_FAILURE);
}
}
// Init the status gatherers... // Init the status gatherers...
mode_init(); mode_init();
@@ -289,89 +295,89 @@ main_loop()
// Main loop // Main loop
// Run whatever screen we want, then wait. Woo-hoo! // Run whatever screen we want, then wait. Woo-hoo!
while(!Quit) while (!Quit)
{ {
// Check for server input... // Check for server input...
len = sock_recv(sock, buf, 8000); len = sock_recv(sock, buf, 8000);
// Handle server input... // Handle server input...
while(len > 0) while (len > 0)
{ {
// Now split the string into tokens... // Now split the string into tokens...
//for(i=0; i<argc; i++) argv[i]=NULL; // Get rid of old tokens //for(i=0; i<argc; i++) argv[i]=NULL; // Get rid of old tokens
argc = 0; argc = 0;
newtoken = 1; newtoken = 1;
for(i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
switch(buf[i]) switch (buf[i])
{ {
case ' ': case ' ':
newtoken = 1; newtoken = 1;
buf[i] = 0; buf[i] = 0;
break; break;
default: // regular chars, keep tokenizing default: // regular chars, keep tokenizing
if(newtoken) if (newtoken)
argv[argc++] = buf + i; argv[argc++] = buf + i;
newtoken = 0; newtoken = 0;
break; break;
case '\0': case '\0':
case '\n': case '\n':
buf[i] = 0; buf[i] = 0;
if(argc > 0) if (argc > 0)
{ {
//printf("%s %s\n", argv[0], argv[1]); //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++) 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; sequence[j].visible = 1;
//debug("Listen %s\n", argv[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++) 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; sequence[j].visible = 0;
//debug("Ignore %s\n", argv[1]); //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]); 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; int a;
for(a = 1; a < argc; a++) for(a = 1; a < argc; a++)
{ {
if(0 == strcmp(argv[a], "wid")) if (0 == strcmp(argv[a], "wid"))
lcd_wid = atoi(argv[++a]); lcd_wid = atoi(argv[++a]);
else if(0 == strcmp(argv[a], "hgt")) else if (0 == strcmp(argv[a], "hgt"))
lcd_hgt = atoi(argv[++a]); lcd_hgt = atoi(argv[++a]);
else if(0 == strcmp(argv[a], "cellwid")) else if (0 == strcmp(argv[a], "cellwid"))
lcd_cellwid = atoi(argv[++a]); lcd_cellwid = atoi(argv[++a]);
else if(0 == strcmp(argv[a], "cellhgt")) else if (0 == strcmp(argv[a], "cellhgt"))
lcd_cellhgt = atoi(argv[++a]); lcd_cellhgt = atoi(argv[++a]);
} }
connected = 1; connected = 1;
sock_send_string(sock, "client_set -name LCDproc\n"); 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"); //printf("Exiting LCDproc\n");
exit_program(EXIT_SUCCESS); exit_program(EXIT_SUCCESS);
} }
else if(0 == strcmp(argv[0], "success")) else if (0 == strcmp(argv[0], "success"))
{ {
} }
else else
@@ -401,9 +407,9 @@ main_loop()
for(i = 0; sequence[i].which > 1; i++) for(i = 0; sequence[i].which > 1; i++)
{ {
sequence[i].timer++; 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; sequence[i].timer = 0;
// Now, update the screen... // Now, update the screen...
@@ -412,14 +418,14 @@ main_loop()
} }
else else
{ {
if(sequence[i].timer >= sequence[i].off_time) if (sequence[i].timer >= sequence[i].off_time)
{ {
sequence[i].timer = 0; sequence[i].timer = 0;
// Now, update the screen... // Now, update the screen...
update_screen(&sequence[i], sequence[i].show_invisible); update_screen(&sequence[i], sequence[i].show_invisible);
} }
} }
if(islow > 0) if (islow > 0)
usleep(islow * 10000); usleep(islow * 10000);
} }
} }
+3 -4
View File
@@ -2,10 +2,10 @@
#define MAIN_H #define MAIN_H
#ifndef TRUE #ifndef TRUE
#define TRUE 1 # define TRUE 1
#endif #endif
#ifndef FALSE #ifndef FALSE
#define FALSE 0 # define FALSE 0
#endif #endif
#define LCDP_BATT_HIGH 0x00 #define LCDP_BATT_HIGH 0x00
@@ -32,6 +32,7 @@ extern int lcd_cellhgt;
typedef struct mode typedef struct mode
{ {
char *longname; // Which screen is it?
char which; // Which screen is it? char which; // Which screen is it?
int on_time; // How often to update while visible? int on_time; // How often to update while visible?
int off_time; // How often to get stats while not visible? int off_time; // How often to get stats while not visible?
@@ -62,6 +63,4 @@ const char *get_sysrelease();
# define max(a,b) (((a) > (b)) ? (a) : (b)) # define max(a,b) (((a) > (b)) ? (a) : (b))
#endif #endif
#endif #endif