|
From: <mla...@us...> - 2006-11-17 22:22:35
|
Revision: 171
http://svn.sourceforge.net/g15daemon/?rev=171&view=rev
Author: mlampard
Date: 2006-11-17 14:22:35 -0800 (Fri, 17 Nov 2006)
Log Message:
-----------
add some simple ini-style configfile routines. not heavily tested but they appear to do the job.
Modified Paths:
--------------
trunk/g15daemon-wip/g15daemon/g15daemon.h
trunk/g15daemon-wip/g15daemon/main.c
trunk/g15daemon-wip/g15daemon/utility_funcs.c
trunk/g15daemon-wip/plugins/g15_plugin_clock.c
Modified: trunk/g15daemon-wip/g15daemon/g15daemon.h
===================================================================
--- trunk/g15daemon-wip/g15daemon/g15daemon.h 2006-11-17 03:56:08 UTC (rev 170)
+++ trunk/g15daemon-wip/g15daemon/g15daemon.h 2006-11-17 22:22:35 UTC (rev 171)
@@ -102,6 +102,31 @@
typedef struct plugin_info_s plugin_info_t;
typedef struct plugin_s plugin_t;
+typedef struct config_items_s config_items_t;
+typedef struct config_section_s config_section_t;
+typedef struct configfile_s configfile_t;
+
+typedef struct config_items_s
+{
+ config_items_t *next;
+ config_items_t *head;
+ char *key;
+ char *value;
+} config_items_s;
+
+typedef struct config_section_s
+{
+ config_section_t *head;
+ config_section_t *next;
+ char *sectionname;
+ config_items_t *items;
+}config_section_s;
+
+typedef struct configfile_s
+{
+ config_section_t *sections;
+}configfile_s;
+
typedef struct plugin_info_s
{
/* type - see above for valid defines*/
@@ -173,6 +198,7 @@
void *(*keyboard_handler)(void*);
struct passwd *nobody;
volatile unsigned long numclients;
+ configfile_t *config;
}lcdlist_s;
pthread_mutex_t lcdlist_mutex;
@@ -194,11 +220,29 @@
lcdlist_t *ll_lcdlist_init();
void ll_lcdlist_destroy(lcdlist_t **displaylist);
+/* open and parse config file */
+int uf_conf_open(lcdlist_t *list, char *filename);
+/* write the config file with all keys/sections */
+int uf_conf_write(lcdlist_t *list,char *filename);
+/* free all memory used by the config subsystem */
+void uf_conf_free(lcdlist_t *list);
/* generic handler for net clients */
int internal_generic_eventhandler(plugin_event_t *myevent);
#endif
/* the following functions are available for use by plugins */
+/* create a new section */
+config_section_t *g15daemon_cfg_load_section(lcdlist_t *displaylist,char *name);
+/* return string value from key in sectionname */
+char* g15daemon_cfg_read_string(config_section_t *section, char *key, char *defaultval);
+/* return float from key in sectionname */
+double g15daemon_cfg_read_float(config_section_t *section, char *key, double defaultval);
+/* return int from key in sectionname */
+int g15daemon_cfg_read_int(config_section_t *section, char *key, int defaultval);
+/* add a new key, or update the value of an already existing key, or return -1 if section doesnt exist */
+int g15daemon_cfg_write_string(config_section_t *section, char *key, char *val);
+int g15daemon_cfg_write_float(config_section_t *section, char *key, double val);
+int g15daemon_cfg_write_int(config_section_t *section, char *key, int val);
/* send event to foreground client's eventlistener */
int g15daemon_send_event(void *caller, unsigned int event, unsigned long value);
Modified: trunk/g15daemon-wip/g15daemon/main.c
===================================================================
--- trunk/g15daemon-wip/g15daemon/main.c 2006-11-17 03:56:08 UTC (rev 170)
+++ trunk/g15daemon-wip/g15daemon/main.c 2006-11-17 22:22:35 UTC (rev 171)
@@ -62,7 +62,7 @@
if(!(value & cycle_key) && !(lastkeys & cycle_key)){
lcd_t *lcd = (lcd_t*)caller;
int *(*plugin_listener)(plugin_event_t *newevent) = (void*)lcd->g15plugin->info->event_handler;
- plugin_event_t *newevent=malloc(sizeof(plugin_event_t));
+ plugin_event_t *newevent=g15daemon_xmalloc(sizeof(plugin_event_t));
newevent->event = event;
newevent->value = value;
newevent->lcd = lcd;
@@ -88,7 +88,7 @@
}
else
{
- plugin_event_t *clickevent=malloc(sizeof(plugin_event_t));
+ plugin_event_t *clickevent=g15daemon_xmalloc(sizeof(plugin_event_t));
int *(*plugin_listener)(plugin_event_t *clickevent) = (void*)lcd->g15plugin->info->event_handler;
clickevent->event = event;
clickevent->value = value|cycle_key;
@@ -98,6 +98,7 @@
clickevent->value = value&~cycle_key;
clickevent->lcd = lcd;
(*plugin_listener)((void*)clickevent);
+ free(clickevent);
}
}
}
@@ -137,11 +138,12 @@
default: {
lcd_t *lcd = (lcd_t*)caller;
int *(*plugin_listener)(plugin_event_t *newevent) = (void*)lcd->g15plugin->info->event_handler;
- plugin_event_t *newevent=malloc(sizeof(plugin_event_t));
+ plugin_event_t *newevent=g15daemon_xmalloc(sizeof(plugin_event_t));
newevent->event = event;
newevent->value = value;
newevent->lcd = lcd;
(*plugin_listener)((void*)newevent);
+ free(newevent);
}
}
return 0;
@@ -317,15 +319,16 @@
goto exitnow;
}
- /* all other processes/threads should be seteuid nobody */
+ /* initialise the linked list */
+ lcdlist = ll_lcdlist_init();
+ lcdlist->nobody = nobody;
+
+ uf_conf_open(lcdlist, "/etc/g15daemon.conf");
+ /* all other processes/threads should be seteuid nobody */
if(nobody!=NULL) {
seteuid(nobody->pw_uid);
setegid(nobody->pw_gid);
}
-
- /* initialise the linked list */
- lcdlist = ll_lcdlist_init();
- lcdlist->nobody = nobody;
pthread_mutex_init(&g15lib_mutex, NULL);
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr,512*1024); /* set stack to 512k - dont need 8Mb !! */
@@ -350,7 +353,7 @@
g15daemon_log(LOG_INFO,"%s loaded\n",PACKAGE_STRING);
snprintf((char*)location,1024,"%s/%s",DATADIR,"g15daemon/splash/g15logo2.wbmp");
- g15canvas *canvas = (g15canvas *)malloc (sizeof (g15canvas));
+ g15canvas *canvas = (g15canvas *)g15daemon_xmalloc (sizeof (g15canvas));
memset (canvas->buffer, 0, G15_BUFFER_LEN);
canvas->mode_cache = 0;
canvas->mode_reverse = 0;
@@ -391,6 +394,8 @@
seteuid(0);
setegid(0);
closelog();
+ uf_conf_write(lcdlist,"/etc/g15daemon.conf");
+ uf_conf_free(lcdlist);
unlink("/var/run/g15daemon.pid");
}
return 0;
Modified: trunk/g15daemon-wip/g15daemon/utility_funcs.c
===================================================================
--- trunk/g15daemon-wip/g15daemon/utility_funcs.c 2006-11-17 03:56:08 UTC (rev 170)
+++ trunk/g15daemon-wip/g15daemon/utility_funcs.c 2006-11-17 22:22:35 UTC (rev 171)
@@ -33,6 +33,9 @@
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
+#include <sys/stat.h>
+#include <ctype.h>
+
#include <sys/time.h>
#ifdef HAVE_CONFIG_H
@@ -99,7 +102,6 @@
}
-
int uf_create_pidfile() {
char pidtxt[128];
@@ -128,6 +130,7 @@
return 1;
}
+
/* syslog wrapper */
int g15daemon_log (int priority, const char *fmt, ...) {
@@ -231,3 +234,264 @@
return G15_PLUGIN_OK;
}
+
+/* free all memory used by the config subsystem */
+void uf_conf_free(lcdlist_t *list)
+{
+ config_section_t *section=list->config->sections;
+ config_items_t *tmpitem=NULL;
+ config_section_t *tmpsection=section;
+
+ while(tmpsection!=NULL){
+ tmpitem=section->items;
+
+ if(section->sectionname){
+ free(section->sectionname);
+ while(section->items!=NULL){
+ if(tmpitem->key!=NULL)
+ free(tmpitem->key);
+ if(tmpitem->value!=NULL)
+ free(tmpitem->value);
+ tmpitem=section->items->next;
+ free(section->items);
+ section->items=tmpitem;
+ }
+ }
+ tmpsection=section->next;
+ free(section);
+ section=tmpsection;
+ }
+ free(section);
+ free(list->config);
+}
+
+/* write the config file with all keys/sections */
+int uf_conf_write(lcdlist_t *list,char *filename)
+{
+ int config_fd=-1;
+ config_section_t *foo=list->config->sections;
+ config_items_t * item=NULL;
+ char line[1024];
+ config_fd = open(filename,O_CREAT|O_RDWR|O_TRUNC);
+ if(config_fd){
+ snprintf(line,1024,"# G15Daemon Configuration File\n");
+ write(config_fd,line,strlen(line));
+ while(foo!=NULL){
+ item=foo->items;
+ memset(line,0,1024);
+ if(foo->sectionname){
+ snprintf(line,1024,"\n[%s]\n",foo->sectionname);
+ write(config_fd,line,strlen(line));
+ while(item!=NULL){
+ memset(line,0,1024);
+ snprintf(line,1024,"%s: %s\n",item->key, item->value);
+ write(config_fd,line,strlen(line));
+ item=item->next;
+ }
+ }
+ foo=foo->next;
+ }
+ close(config_fd);
+ return 0;
+ }
+ return -1;
+}
+
+/* search the list for valid section name return pointer to section, or NULL otherwise */
+config_section_t* uf_search_confsection(lcdlist_t *list,char *sectionname){
+ config_section_t *section=list->config->sections;
+
+ while(section!=NULL){
+ if(0==strcmp((char*)section->sectionname,(char*)sectionname))
+ break;
+ section=section->next;
+ }
+ return section;
+}
+
+/* search the list for valid key called "key" in section named "section" return pointer to item or NULL */
+config_items_t* uf_search_confitem(config_section_t *section, char *key){
+
+ config_items_t * item=NULL;
+
+ if(section!=NULL){
+ item=section->items;
+ while(item!=NULL){
+ if(0==strcmp((char*)item->key,(char*)key))
+ break;
+ item=item->next;
+ }
+ }
+ return item;
+}
+
+
+/* return pointer to section, or create a new section if it doesnt exist */
+config_section_t *g15daemon_cfg_load_section(lcdlist_t *displaylist,char *name) {
+
+ config_section_t *new = NULL;
+ if((new=uf_search_confsection(displaylist,name))!=NULL)
+ return new;
+ new = g15daemon_xmalloc(sizeof(config_section_t));
+ new->head = new;
+ new->next = NULL;;
+ new->sectionname=strdup(name);
+ if(!displaylist->config->sections){
+ displaylist->config->sections=new;
+ displaylist->config->sections->head = new;
+ } else {
+ displaylist->config->sections->head->next=new;
+ displaylist->config->sections->head = new;
+ }
+ return new;
+}
+
+/* cleanup whitespace */
+char * uf_remove_whitespace(char *str){
+ int z=0;
+ while(isspace(str[z])&&str[z])
+ z++;
+ str+=z;
+ return str;
+}
+
+/* add a new key, or update the value of an already existing key, or return -1 if section doesnt exist */
+int g15daemon_cfg_write_string(config_section_t *section, char *key, char *val){
+
+ config_items_t *new = NULL;
+
+ if(section==NULL)
+ return -1;
+
+
+ if((uf_search_confitem(section, key))){
+ free(new);
+ new=uf_search_confitem(section, key);
+ new->value=strdup(val);
+ }else{
+ new=g15daemon_xmalloc(sizeof(config_items_t));
+ new->head=new;
+ new->next=NULL;
+ new->key=strdup(key);
+ new->value=strdup(val);
+ if(!section->items){
+ section->items=new;
+ section->items->head=new;
+ }else{
+ section->items->head->next=new;
+ section->items->head=new;
+ }
+ }
+ return 0;
+}
+
+int g15daemon_cfg_write_int(config_section_t *section, char *key, int val) {
+ char tmp[1024];
+ snprintf(tmp,1024,"%i",val);
+ return g15daemon_cfg_write_string(section, key, tmp);
+}
+
+int g15daemon_cfg_write_float(config_section_t *section, char *key, double val) {
+ char tmp[1024];
+ snprintf(tmp,1024,"%f",val);
+ return g15daemon_cfg_write_string(section, key, tmp);
+}
+/* the config read functions will either return a value from the config file, or the default value, which will be written to the config file if the key doesnt exist */
+
+/* return int from key in sectionname */
+int g15daemon_cfg_read_int(config_section_t *section, char *key, int defaultval) {
+
+ config_items_t *item = uf_search_confitem(section, key);
+ if(item){
+ return atoi(item->value);
+ }
+ g15daemon_cfg_write_int(section, key, defaultval);
+ return defaultval;
+}
+
+/* return float from key in sectionname */
+double g15daemon_cfg_read_float(config_section_t *section, char *key, double defaultval) {
+
+ config_items_t *item = uf_search_confitem(section, key);
+ if(item){
+ return atof(item->value);
+ }
+ g15daemon_cfg_write_float(section, key, defaultval);
+ return defaultval;
+}
+
+/* return string value from key in sectionname */
+char* g15daemon_cfg_read_string(config_section_t *section, char *key, char *defaultval) {
+
+ config_items_t *item = uf_search_confitem(section, key);
+ if(item){
+ return item->value;
+ }
+ g15daemon_cfg_write_string(section, key, defaultval);
+ return defaultval;
+}
+
+
+int uf_conf_open(lcdlist_t *list, char *filename) {
+
+ char *buffer, *lines;
+ int config_fd=-1;
+ char *sect;
+ char *start;
+ char *bar;
+ int i;
+ struct stat stats;
+
+ list->config=g15daemon_xmalloc(sizeof(configfile_t));
+ list->config->sections=NULL;
+
+ config_fd = open(filename,O_RDWR);
+
+ if (lstat(filename, &stats) == -1)
+ return -1;
+ if (!(config_fd = open(filename, O_RDWR)))
+ return -1;
+
+ buffer = g15daemon_xmalloc(stats.st_size + 1);
+
+ if (read(config_fd, buffer, stats.st_size) != stats.st_size)
+ {
+ free(buffer);
+ close(config_fd);
+ return -1;
+ }
+ close(config_fd);
+ buffer[stats.st_size] = '\0';
+
+ lines=strtok_r(buffer,"\n",&bar);
+
+ while(lines!=NULL){
+ sect=strdup(lines);
+ config_section_t *section;
+ i=0;
+ while(isspace(sect[i])){
+ i++;
+ }
+ start=sect+i;
+ if(start[0]=='#'){
+ /* comment.. ignore */
+ }else if(strcmp(start,"]")<0) { /* section title */
+ char sectiontitle[1024];
+ memset(sectiontitle,0,1024);
+ strncpy(sectiontitle,start+1,strlen(start)-2);
+ section = g15daemon_cfg_load_section(list,sectiontitle);
+ }else{
+ /*section keys */
+ char *foo;
+ char *key = uf_remove_whitespace( strtok_r(start,":",&foo) );
+ char *val = uf_remove_whitespace( strtok_r(NULL,":", &foo) );
+
+ g15daemon_cfg_write_string(section,key,val);
+ }
+ free(sect);
+ lines=strtok_r(NULL,"\n",&bar);
+ }
+ free(buffer);
+ return 0;
+}
+
Modified: trunk/g15daemon-wip/plugins/g15_plugin_clock.c
===================================================================
--- trunk/g15daemon-wip/plugins/g15_plugin_clock.c 2006-11-17 03:56:08 UTC (rev 170)
+++ trunk/g15daemon-wip/plugins/g15_plugin_clock.c 2006-11-17 22:22:35 UTC (rev 171)
@@ -33,11 +33,14 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
-
+#include <libg15.h>
#include <config.h>
#include <g15daemon.h>
#include <libg15render.h>
+
+static int mode=24;
+
static int *lcdclock(lcd_t *lcd)
{
unsigned int col = 0;
@@ -59,8 +62,11 @@
memset(lcd->buf,0,G15_BUFFER_LEN);
memset(buf,0,10);
- strftime(buf,6,"%H:%M",localtime(&currtime));
-
+ if(mode==24){
+ strftime(buf,6,"%H:%M",localtime(&currtime));
+ }else{
+ strftime(buf,6,"%I:%M",localtime(&currtime));
+ }
if(buf[0]==49)
narrows=1;
@@ -81,11 +87,16 @@
}
static int myeventhandler(plugin_event_t *myevent) {
-// lcd_t *lcd = (lcd_t*) myevent->lcd;
+ lcd_t *lcd = (lcd_t*) myevent->lcd;
+ config_section_t *clockcfg =NULL;
switch (myevent->event)
{
case G15_EVENT_KEYPRESS:
+ clockcfg = g15daemon_cfg_load_section(lcd->masterlist,"Clock");
+ if(myevent->value&G15_KEY_L1)
+ mode=1^mode;
+ g15daemon_cfg_write_int(clockcfg, "mode", mode);
// printf("Clock plugin received keypress event : %i\n",myevent->value);
break;
case G15_EVENT_VISIBILITY_CHANGED:
@@ -104,6 +115,8 @@
/* completely unnecessary initialisation function which could just as easily have been set to NULL in the g15plugin_info struct */
static void *myinithandler(lcd_t *lcd){
+ config_section_t *clockcfg = g15daemon_cfg_load_section(lcd->masterlist,"Clock");
+ mode=g15daemon_cfg_read_int(clockcfg, "mode",0);
return NULL;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|