add lcdproc config file support
This commit is contained in:
@@ -0,0 +1,85 @@
|
|||||||
|
# LCDproc client configuration file
|
||||||
|
|
||||||
|
## general options ##
|
||||||
|
[lcdproc]
|
||||||
|
# address of the LCDd server to connect to
|
||||||
|
Server=localhost
|
||||||
|
|
||||||
|
# Port of the server to connect to
|
||||||
|
Port=13666
|
||||||
|
|
||||||
|
# set reporting level
|
||||||
|
ReportLevel=2
|
||||||
|
|
||||||
|
# report to to syslog ?
|
||||||
|
ReportToSyslog=false
|
||||||
|
|
||||||
|
# stay in foreground ?
|
||||||
|
Foreground=false
|
||||||
|
|
||||||
|
# slow down initial announcement of modes (in 1/100s)
|
||||||
|
#delay=2
|
||||||
|
|
||||||
|
|
||||||
|
## screen specific configuration options ##
|
||||||
|
|
||||||
|
[CPU]
|
||||||
|
# Show screen
|
||||||
|
Active = True
|
||||||
|
OnTime = 1
|
||||||
|
OffTime = 2
|
||||||
|
ShowInvisible = false
|
||||||
|
|
||||||
|
[Memory]
|
||||||
|
# Show screen
|
||||||
|
Active = True
|
||||||
|
|
||||||
|
[Load]
|
||||||
|
# Show screen
|
||||||
|
Active = True
|
||||||
|
|
||||||
|
[TimeDate]
|
||||||
|
# Show screen
|
||||||
|
Active = True
|
||||||
|
|
||||||
|
[About]
|
||||||
|
# Show screen
|
||||||
|
Active = false
|
||||||
|
|
||||||
|
[SMP-CPU]
|
||||||
|
# Show screen
|
||||||
|
Active = false
|
||||||
|
|
||||||
|
[OldTime]
|
||||||
|
# Show screen
|
||||||
|
Active = false
|
||||||
|
|
||||||
|
[BigClock]
|
||||||
|
# Show screen
|
||||||
|
Active = false
|
||||||
|
|
||||||
|
[Uptime]
|
||||||
|
# Show screen
|
||||||
|
Active = false
|
||||||
|
|
||||||
|
[Battery]
|
||||||
|
# Show screen
|
||||||
|
Active = false
|
||||||
|
|
||||||
|
[CPUGraph]
|
||||||
|
# Show screen
|
||||||
|
Active = false
|
||||||
|
|
||||||
|
[ProcSize]
|
||||||
|
# Show screen
|
||||||
|
Active = false
|
||||||
|
|
||||||
|
[Disk]
|
||||||
|
# Show screen
|
||||||
|
Active = false
|
||||||
|
|
||||||
|
[MiniClock]
|
||||||
|
# Show screen
|
||||||
|
Active = True
|
||||||
|
|
||||||
|
# EOF
|
||||||
+145
-43
@@ -22,6 +22,7 @@
|
|||||||
#include "shared/sockets.h"
|
#include "shared/sockets.h"
|
||||||
#include "shared/debug.h"
|
#include "shared/debug.h"
|
||||||
#include "shared/report.h"
|
#include "shared/report.h"
|
||||||
|
#include "shared/configfile.h"
|
||||||
|
|
||||||
#include "batt.h"
|
#include "batt.h"
|
||||||
#include "chrono.h"
|
#include "chrono.h"
|
||||||
@@ -54,6 +55,10 @@ static struct utsname unamebuf;
|
|||||||
static void HelpScreen(int exit_state);
|
static void HelpScreen(int exit_state);
|
||||||
static void exit_program(int val);
|
static void exit_program(int val);
|
||||||
static void main_loop();
|
static void main_loop();
|
||||||
|
static int process_command_line(int argc, char **argv);
|
||||||
|
static int process_configfile(char *cfgfile);
|
||||||
|
#define CHAIN(e,f) { if (e>=0 ) { e=(f); }}
|
||||||
|
#define CHAIN_END(e,msg) { if (e<0 ) { report( RPT_CRIT,(msg)); exit(e); }}
|
||||||
|
|
||||||
static int islow = -1;
|
static int islow = -1;
|
||||||
|
|
||||||
@@ -79,12 +84,25 @@ mode sequence[] =
|
|||||||
{ "ProcSize", 'S', 16, 256, 1, 0xffff, 0, mem_top_screen }, // [S]ize of biggest processes
|
{ "ProcSize", 'S', 16, 256, 1, 0xffff, 0, mem_top_screen }, // [S]ize of biggest processes
|
||||||
{ "Disk", 'D', 256, 256, 1, 0xffff, 0, disk_screen }, // [D]isk stats
|
{ "Disk", 'D', 256, 256, 1, 0xffff, 0, disk_screen }, // [D]isk stats
|
||||||
{ "MiniClock", 'N', 4, 64, 0, 0xffff, 0, mini_clock_screen }, // Mi[n]i clock
|
{ "MiniClock", 'N', 4, 64, 0, 0xffff, 0, mini_clock_screen }, // Mi[n]i clock
|
||||||
{ NULL, 0, 0, 0, 0, 0,}, // No more.. all done.
|
{ NULL, 0, 0, 0, 0, 0, 0, NULL}, // No more.. all done.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: Config file; not just command line
|
// TODO: Config file; not just command line
|
||||||
|
/* All variables are set to 'unset' values*/
|
||||||
|
#define UNSET_INT -1
|
||||||
|
#define UNSET_STR "\01"
|
||||||
|
#define DEFAULT_SERVER "127.0.0.1"
|
||||||
|
#define DEFAULT_CONFIGFILE "/usr/local/etc/lcdproc.conf"
|
||||||
|
char * progname = "lcdproc";
|
||||||
|
char * server = NULL;
|
||||||
|
int port = LCDPORT;
|
||||||
|
int daemonize = FALSE;
|
||||||
|
static int report_level = UNSET_INT;
|
||||||
|
static int report_dest = UNSET_INT;
|
||||||
|
char configfile[256]; /* a lot of space in the executable. */
|
||||||
|
|
||||||
|
|
||||||
const char *get_hostname()
|
const char *get_hostname()
|
||||||
{
|
{
|
||||||
@@ -135,11 +153,8 @@ void clear_modes()
|
|||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
int c;
|
|
||||||
char *server = NULL;
|
|
||||||
int port = LCDPORT;
|
|
||||||
int daemonize = FALSE;
|
int daemonize = FALSE;
|
||||||
|
int error = 0;
|
||||||
|
|
||||||
/* get uname information */
|
/* get uname information */
|
||||||
if (uname(&unamebuf) == -1) {
|
if (uname(&unamebuf) == -1) {
|
||||||
@@ -153,12 +168,77 @@ main(int argc, char **argv)
|
|||||||
signal(SIGHUP, exit_program); // kill -HUP
|
signal(SIGHUP, exit_program); // kill -HUP
|
||||||
signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but ...]
|
signal(SIGKILL, exit_program); // kill -9 [cannot be trapped; but ...]
|
||||||
|
|
||||||
|
/* Set default config file, command line may overwrite */
|
||||||
|
strncpy(configfile, DEFAULT_CONFIGFILE, sizeof(configfile));
|
||||||
|
|
||||||
|
/* Read command line*/
|
||||||
|
CHAIN( error, process_command_line(argc, argv) );
|
||||||
|
|
||||||
|
/* Read config file*/
|
||||||
|
if (strcmp(configfile, UNSET_STR) != 0)
|
||||||
|
CHAIN( error, process_configfile(configfile) );
|
||||||
|
|
||||||
|
CHAIN_END( error, "Error in config file or command line\n" );
|
||||||
|
|
||||||
|
if (server == NULL)
|
||||||
|
server = DEFAULT_SERVER;
|
||||||
|
|
||||||
|
// Connect to the server...
|
||||||
|
usleep(500000); // wait for the server to start up
|
||||||
|
sock = sock_connect(server, port);
|
||||||
|
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.
|
||||||
|
|
||||||
|
// We grab the real values below, from the "connect" line.
|
||||||
|
lcd_wid = 20;
|
||||||
|
lcd_hgt = 4;
|
||||||
|
lcd_cellwid = 5;
|
||||||
|
lcd_cellhgt = 8;
|
||||||
|
|
||||||
|
if (daemonize) {
|
||||||
|
if (daemon(1,0) != 0) {
|
||||||
|
fprintf(stderr, "Error: daemonize failed");
|
||||||
|
return(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init the status gatherers...
|
||||||
|
mode_init();
|
||||||
|
|
||||||
|
// And spew stuff!
|
||||||
|
main_loop();
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
exit_program(EXIT_SUCCESS);
|
||||||
|
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* parses arguments given on command line */
|
||||||
|
static int
|
||||||
|
process_command_line(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int c;
|
||||||
|
|
||||||
/* No error output from getopt */
|
/* No error output from getopt */
|
||||||
opterr = 0;
|
opterr = 0;
|
||||||
|
|
||||||
/* get options */
|
/* get options */
|
||||||
while ((c = getopt( argc, argv, "s:p:e:dhv")) > 0) {
|
while ((c = getopt( argc, argv, "s:p:e:c:dhv")) > 0) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
// c is for config file
|
||||||
|
case 'c':
|
||||||
|
strncpy(configfile, optarg, sizeof(configfile));
|
||||||
|
configfile[sizeof(configfile)-1] = 0; /* Terminate string */
|
||||||
|
break;
|
||||||
// s is for server
|
// s is for server
|
||||||
case 's':
|
case 's':
|
||||||
if (server == NULL)
|
if (server == NULL)
|
||||||
@@ -204,50 +284,71 @@ main(int argc, char **argv)
|
|||||||
int found = set_mode(shortname, argv[i], 1);
|
int found = set_mode(shortname, argv[i], 1);
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
fprintf(stderr, "Invalid Mode: %c, ignoring\n", argv[i][0]);
|
fprintf(stderr, "Invalid Mode: %c\n", argv[i][0]);
|
||||||
return(EXIT_FAILURE);
|
return(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
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) {
|
|
||||||
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");
|
/* reads and parses configuration file */
|
||||||
usleep(500000); // wait for the server to say hi.
|
static int
|
||||||
|
process_configfile(char *configfile)
|
||||||
|
{
|
||||||
|
int k;
|
||||||
|
|
||||||
// We grab the real values below, from the "connect" line.
|
debug(RPT_DEBUG, "%s(%s)", __FUNCTION__, configfile);
|
||||||
lcd_wid = 20;
|
|
||||||
lcd_hgt = 4;
|
|
||||||
lcd_cellwid = 5;
|
|
||||||
lcd_cellhgt = 8;
|
|
||||||
|
|
||||||
if (daemonize) {
|
/* Read server settings*/
|
||||||
if (daemon(1,0) != 0) {
|
|
||||||
fprintf(stderr, "Error: daemonize failed");
|
if (strcmp(configfile, UNSET_STR) == 0) {
|
||||||
return(EXIT_FAILURE);
|
configfile = DEFAULT_CONFIGFILE;
|
||||||
|
}
|
||||||
|
if (config_read_file(configfile) != 0 ) {
|
||||||
|
report(RPT_CRIT, "Could not read config file: %s", configfile);
|
||||||
|
return -1;
|
||||||
|
//report(RPT_WARNING, "Could not read config file: %s", configfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (server == NULL) {
|
||||||
|
server = strdup(config_get_string(progname, "Server", 0, DEFAULT_SERVER));
|
||||||
|
}
|
||||||
|
if (port == UNSET_INT) {
|
||||||
|
port = config_get_int(progname, "Port", 0, LCDPORT);
|
||||||
|
}
|
||||||
|
if(report_level == UNSET_INT) {
|
||||||
|
report_level = config_get_int(progname, "ReportLevel", 0, RPT_WARNING);
|
||||||
|
}
|
||||||
|
if(report_dest == UNSET_INT) {
|
||||||
|
if (config_get_bool(progname, "ReportToSyslog", 0, 0)) {
|
||||||
|
report_dest = RPT_DEST_SYSLOG;
|
||||||
|
} else {
|
||||||
|
report_dest = RPT_DEST_STDERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (daemonize == UNSET_INT) {
|
||||||
|
daemonize = config_get_bool(progname, "Foreground", 0, 1);
|
||||||
|
}
|
||||||
|
if (islow < 0) {
|
||||||
|
islow = config_get_int(progname, "delay", 0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* check for config file variables to override all the sequence defaults
|
||||||
|
*/
|
||||||
|
for (k = 0; sequence[k].which != 0; k++) {
|
||||||
|
if (sequence[k].longname != NULL) {
|
||||||
|
sequence[k].on_time = config_get_int(sequence[k].longname, "OnTime", 0, sequence[k].on_time);
|
||||||
|
sequence[k].off_time = config_get_int(sequence[k].longname, "OffTime", 0, sequence[k].off_time);
|
||||||
|
sequence[k].show_invisible = config_get_bool(sequence[k].longname, "ShowInvisible", 0, sequence[k].show_invisible);
|
||||||
|
if (config_get_bool( sequence[k].longname, "Active", 0, sequence[k].flags & ACTIVE))
|
||||||
|
sequence[k].flags |= ACTIVE;
|
||||||
|
else
|
||||||
|
sequence[k].flags &= (~ACTIVE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init the status gatherers...
|
return 0;
|
||||||
mode_init();
|
|
||||||
|
|
||||||
// And spew stuff!
|
|
||||||
main_loop();
|
|
||||||
|
|
||||||
// Clean up
|
|
||||||
exit_program(EXIT_SUCCESS);
|
|
||||||
|
|
||||||
return(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -260,6 +361,7 @@ HelpScreen (int exit_state)
|
|||||||
" -p <port> connect to LCDd daemon using <port>\n"
|
" -p <port> connect to LCDd daemon using <port>\n"
|
||||||
" -d daemonize\n"
|
" -d daemonize\n"
|
||||||
" -e <delay> slow down initial announcement of modes (in 1/100s)\n"
|
" -e <delay> slow down initial announcement of modes (in 1/100s)\n"
|
||||||
|
" -c <config> use a configuration file other than %s\n"
|
||||||
" -h show this help screen\n"
|
" -h show this help screen\n"
|
||||||
" -v display program version\n"
|
" -v display program version\n"
|
||||||
" and <modes> are\n"
|
" and <modes> are\n"
|
||||||
@@ -273,14 +375,14 @@ HelpScreen (int exit_state)
|
|||||||
" B Battery battery status\n"
|
" B Battery battery status\n"
|
||||||
" T TimeDate time & date information\n"
|
" T TimeDate time & date information\n"
|
||||||
" O OldTime old time screen\n"
|
" O OldTime old time screen\n"
|
||||||
" U Uptime old uptime screen\n"
|
" U Uptime uptime screen\n"
|
||||||
" K BigClock big clock\n"
|
" K BigClock big clock\n"
|
||||||
" N MiniClock minimal clock\n"
|
" N MiniClock minimal clock\n"
|
||||||
" A About credits page\n"
|
" A About credits page\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Example:\n"
|
"Example:\n"
|
||||||
" lcdproc -s my.lcdproc.server.com p 13666 C M X\n"
|
" lcdproc -s my.lcdproc.server.com -p 13666 C M X\n"
|
||||||
);
|
, DEFAULT_CONFIGFILE);
|
||||||
|
|
||||||
exit(exit_state);
|
exit(exit_state);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user