diff options
-rw-r--r-- | CodingStyle | 22 | ||||
-rw-r--r-- | cfg.c | 892 | ||||
-rw-r--r-- | cfg.h | 22 | ||||
-rwxr-xr-x | configure | 2 | ||||
-rw-r--r-- | debug.c | 76 | ||||
-rw-r--r-- | debug.h | 7 | ||||
-rw-r--r-- | drv.c | 98 | ||||
-rw-r--r-- | drv.h | 19 | ||||
-rw-r--r-- | drv_BWCT.c | 471 | ||||
-rw-r--r-- | drv_BeckmannEgle.c | 1008 | ||||
-rw-r--r-- | drv_Crystalfontz.c | 1403 | ||||
-rw-r--r-- | drv_Curses.c | 360 | ||||
-rw-r--r-- | drv_Cwlinux.c | 496 | ||||
-rw-r--r-- | drv_HD44780.c | 1865 | ||||
-rw-r--r-- | drv_Image.c | 672 | ||||
-rw-r--r-- | drv_LCDLinux.c | 437 | ||||
-rw-r--r-- | drv_LCDLinux.h | 38 | ||||
-rw-r--r-- | drv_LCDTerm.c | 330 | ||||
-rw-r--r-- | drv_M50530.c | 651 | ||||
-rw-r--r-- | drv_MatrixOrbital.c | 869 | ||||
-rw-r--r-- | drv_MilfordInstruments.c | 319 | ||||
-rw-r--r-- | drv_NULL.c | 173 | ||||
-rw-r--r-- | drv_Noritake.c | 381 | ||||
-rw-r--r-- | drv_RouterBoard.c | 656 | ||||
-rw-r--r-- | drv_SimpleLCD.c | 334 | ||||
-rw-r--r-- | drv_T6963.c | 848 | ||||
-rw-r--r-- | drv_Trefon.c | 471 | ||||
-rw-r--r-- | drv_USBLCD.c | 623 | ||||
-rw-r--r-- | drv_X11.c | 525 | ||||
-rw-r--r-- | drv_generic_graphic.c | 475 | ||||
-rw-r--r-- | drv_generic_graphic.h | 27 | ||||
-rw-r--r-- | drv_generic_i2c.c | 159 | ||||
-rw-r--r-- | drv_generic_i2c.h | 13 | ||||
-rw-r--r-- | drv_generic_parport.c | 660 | ||||
-rw-r--r-- | drv_generic_parport.h | 31 | ||||
-rw-r--r-- | drv_generic_serial.c | 479 | ||||
-rw-r--r-- | drv_generic_serial.h | 15 | ||||
-rw-r--r-- | drv_generic_text.c | 1277 | ||||
-rw-r--r-- | drv_generic_text.h | 41 | ||||
-rw-r--r-- | evaluator.c | 1763 | ||||
-rw-r--r-- | evaluator.h | 39 | ||||
-rw-r--r-- | font_6x8.h | 1559 | ||||
-rw-r--r-- | hash.c | 599 | ||||
-rw-r--r-- | hash.h | 57 | ||||
-rwxr-xr-x | indent.sh | 7 | ||||
-rw-r--r-- | layout.c | 93 | ||||
-rw-r--r-- | lcd4linux.c | 511 | ||||
-rw-r--r-- | pid.c | 152 | ||||
-rw-r--r-- | pid.h | 9 | ||||
-rw-r--r-- | plugin.c | 222 | ||||
-rw-r--r-- | plugin.h | 7 | ||||
-rw-r--r-- | plugin_apm.c | 144 | ||||
-rw-r--r-- | plugin_cfg.c | 149 | ||||
-rw-r--r-- | plugin_cpuinfo.c | 133 | ||||
-rw-r--r-- | plugin_diskstats.c | 175 | ||||
-rw-r--r-- | plugin_dvb.c | 151 | ||||
-rw-r--r-- | plugin_exec.c | 361 | ||||
-rw-r--r-- | plugin_i2c_sensors.c | 453 | ||||
-rwxr-xr-x | plugin_imon.c | 702 | ||||
-rw-r--r-- | plugin_isdn.c | 275 | ||||
-rw-r--r-- | plugin_loadavg.c | 138 | ||||
-rw-r--r-- | plugin_math.c | 127 | ||||
-rw-r--r-- | plugin_meminfo.c | 139 | ||||
-rw-r--r-- | plugin_mysql.c | 230 | ||||
-rw-r--r-- | plugin_netdev.c | 237 | ||||
-rw-r--r-- | plugin_pop3.c | 416 | ||||
-rw-r--r-- | plugin_ppp.c | 114 | ||||
-rw-r--r-- | plugin_proc_stat.c | 461 | ||||
-rw-r--r-- | plugin_python.c | 158 | ||||
-rw-r--r-- | plugin_sample.c | 204 | ||||
-rw-r--r-- | plugin_seti.c | 177 | ||||
-rw-r--r-- | plugin_statfs.c | 85 | ||||
-rw-r--r-- | plugin_string.c | 23 | ||||
-rw-r--r-- | plugin_test.c | 94 | ||||
-rw-r--r-- | plugin_time.c | 39 | ||||
-rw-r--r-- | plugin_uname.c | 69 | ||||
-rw-r--r-- | plugin_uptime.c | 315 | ||||
-rwxr-xr-x | plugin_wireless.c | 959 | ||||
-rw-r--r-- | plugin_xmms.c | 132 | ||||
-rw-r--r-- | qprintf.c | 311 | ||||
-rw-r--r-- | thread.c | 145 | ||||
-rw-r--r-- | thread.h | 19 | ||||
-rw-r--r-- | timer.c | 233 | ||||
-rw-r--r-- | timer.h | 9 | ||||
-rw-r--r-- | udelay.c | 235 | ||||
-rw-r--r-- | udelay.h | 15 | ||||
-rw-r--r-- | widget.c | 207 | ||||
-rw-r--r-- | widget.h | 33 | ||||
-rw-r--r-- | widget_bar.c | 328 | ||||
-rw-r--r-- | widget_bar.h | 39 | ||||
-rw-r--r-- | widget_icon.c | 264 | ||||
-rw-r--r-- | widget_icon.h | 27 | ||||
-rw-r--r-- | widget_text.c | 601 | ||||
-rw-r--r-- | widget_text.h | 37 |
94 files changed, 16121 insertions, 15305 deletions
diff --git a/CodingStyle b/CodingStyle new file mode 100644 index 0000000..c47c015 --- /dev/null +++ b/CodingStyle @@ -0,0 +1,22 @@ +LCD4Linux Coding Style + +We decided to use a common coding style to make patches and diffs +easier to read and apply. + +The style we use is based on Kernighan & Ritchie and the coding style +of the linux kernel (see /usr/src/linux/Documentation/CodingStyle), +with some differences: + +- we use a indentation level of 4 spaces (Linus prefers 8) +- we allow lines up to 150 chars (Linus uses 80) + +The resulting indent command is: +indent -kr -l150 + +There's a script called 'indent.sh' that comes with the LCD4Linux +source package; all it does is call indent with all .c and .h files. + +It is always a good idea to call 'indent.sh' before any CVS operation, +this ensures that diffs and updates won't get mixed up because of +cosmetic changes. + @@ -1,4 +1,4 @@ -/* $Id: cfg.c,v 1.46 2005/05/02 05:15:46 reinelt Exp $^ +/* $Id: cfg.c,v 1.47 2005/05/08 04:32:43 reinelt Exp $^ * * config file stuff * @@ -23,6 +23,9 @@ * * * $Log: cfg.c,v $ + * Revision 1.47 2005/05/08 04:32:43 reinelt + * CodingStyle added and applied + * * Revision 1.46 2005/05/02 05:15:46 reinelt * make busy-flag checking configurable for LCD-Linux driver * @@ -293,538 +296,561 @@ #endif typedef struct { - char *key; - char *val; - int lock; + char *key; + char *val; + int lock; } ENTRY; -static char *Config_File=NULL; -static ENTRY *Config=NULL; -static int nConfig=0; +static char *Config_File = NULL; +static ENTRY *Config = NULL; +static int nConfig = 0; /* bsearch compare function for config entries */ -static int c_lookup (const void *a, const void *b) +static int c_lookup(const void *a, const void *b) { - char *key=(char*)a; - ENTRY *entry=(ENTRY*)b; + char *key = (char *) a; + ENTRY *entry = (ENTRY *) b; - return strcasecmp(key, entry->key); + return strcasecmp(key, entry->key); } /* qsort compare function for variables */ -static int c_sort (const void *a, const void *b) +static int c_sort(const void *a, const void *b) { - ENTRY *ea=(ENTRY*)a; - ENTRY *eb=(ENTRY*)b; + ENTRY *ea = (ENTRY *) a; + ENTRY *eb = (ENTRY *) b; - return strcasecmp(ea->key, eb->key); + return strcasecmp(ea->key, eb->key); } /* remove leading and trailing whitespace */ -static char *strip (char *s, const int strip_comments) +static char *strip(char *s, const int strip_comments) { - char *p; - - while (isblank(*s)) s++; - for (p=s; *p; p++) { - if (*p=='"') do p++; while (*p && *p!='\n' && *p!='"'); - if (*p=='\'') do p++; while (*p && *p!='\n' && *p!='\''); - if (*p=='\n' || (strip_comments && *p=='#' && (p==s || *(p-1)!='\\'))) { - *p='\0'; - break; + char *p; + + while (isblank(*s)) + s++; + for (p = s; *p; p++) { + if (*p == '"') + do + p++; + while (*p && *p != '\n' && *p != '"'); + if (*p == '\'') + do + p++; + while (*p && *p != '\n' && *p != '\''); + if (*p == '\n' || (strip_comments && *p == '#' && (p == s || *(p - 1) != '\\'))) { + *p = '\0'; + break; + } } - } - for (p--; p>s && isblank(*p); p--) *p='\0'; - return s; + for (p--; p > s && isblank(*p); p--) + *p = '\0'; + return s; } /* unquote a string */ -static char *dequote (char *string) +static char *dequote(char *string) { - int quote=0; - char *s = string; - char *p = string; - - do { - if (*s == '\'') { - quote = !quote; - *p++ = *s; - } - else if (quote && *s == '\\') { - s++; - if (*s >= '0' && *s <= '7') { - int n; - unsigned int c = 0; - sscanf (s, "%3o%n", &c, &n); - if (c == 0 || c > 255) { - error ("WARNING: illegal '\\' in <%s>", string); + int quote = 0; + char *s = string; + char *p = string; + + do { + if (*s == '\'') { + quote = !quote; + *p++ = *s; + } else if (quote && *s == '\\') { + s++; + if (*s >= '0' && *s <= '7') { + int n; + unsigned int c = 0; + sscanf(s, "%3o%n", &c, &n); + if (c == 0 || c > 255) { + error("WARNING: illegal '\\' in <%s>", string); + } else { + *p++ = c; + s += n - 1; + } + } else { + *p++ = *s; + } } else { - *p++ = c; - s += n-1; + *p++ = *s; } - } else { - *p++ = *s; - } - } - else { - *p++ = *s; - } - } while (*s++); - - return string; + } while (*s++); + + return string; } /* which if a string contains only valid chars */ /* i.e. start with a char and contains chars and nums */ -static int validchars (const char *string) +static int validchars(const char *string) { - const char *c; - - for (c=string; *c; c++) { - /* first and following chars */ - if ((*c>='A' && *c<='Z') || (*c>='a' && *c<='z') || (*c=='_')) continue; - /* only following chars */ - if ((c>string) && ((*c>='0' && *c<='9') || (*c=='.') || (*c=='-'))) continue; - return 0; - } - return 1; + const char *c; + + for (c = string; *c; c++) { + /* first and following chars */ + if ((*c >= 'A' && *c <= 'Z') || (*c >= 'a' && *c <= 'z') || (*c == '_')) + continue; + /* only following chars */ + if ((c > string) && ((*c >= '0' && *c <= '9') || (*c == '.') || (*c == '-'))) + continue; + return 0; + } + return 1; } -static void cfg_add (const char *section, const char *key, const char *val, const int lock) +static void cfg_add(const char *section, const char *key, const char *val, const int lock) { - char *buffer; - ENTRY *entry; - - /* allocate buffer */ - buffer=malloc(strlen(section)+strlen(key)+2); - *buffer='\0'; - - /* prepare section.key */ - if (section!=NULL && *section!='\0') { - strcpy(buffer, section); - strcat(buffer, "."); - } - strcat (buffer, key); - - /* does the key already exist? */ - entry=bsearch(buffer, Config, nConfig, sizeof(ENTRY), c_lookup); - - if (entry!=NULL) { - if (entry->lock>lock) return; - debug ("Warning: key <%s>: value <%s> overwritten with <%s>", buffer, entry->val, val); - free (buffer); - if (entry->val) free (entry->val); - entry->val=dequote(strdup(val)); - return; - } - - nConfig++; - Config=realloc(Config, nConfig*sizeof(ENTRY)); - Config[nConfig-1].key=buffer; - Config[nConfig-1].val=dequote(strdup(val)); - Config[nConfig-1].lock=lock; - - qsort(Config, nConfig, sizeof(ENTRY), c_sort); + char *buffer; + ENTRY *entry; + + /* allocate buffer */ + buffer = malloc(strlen(section) + strlen(key) + 2); + *buffer = '\0'; + + /* prepare section.key */ + if (section != NULL && *section != '\0') { + strcpy(buffer, section); + strcat(buffer, "."); + } + strcat(buffer, key); + + /* does the key already exist? */ + entry = bsearch(buffer, Config, nConfig, sizeof(ENTRY), c_lookup); + + if (entry != NULL) { + if (entry->lock > lock) + return; + debug("Warning: key <%s>: value <%s> overwritten with <%s>", buffer, entry->val, val); + free(buffer); + if (entry->val) + free(entry->val); + entry->val = dequote(strdup(val)); + return; + } + + nConfig++; + Config = realloc(Config, nConfig * sizeof(ENTRY)); + Config[nConfig - 1].key = buffer; + Config[nConfig - 1].val = dequote(strdup(val)); + Config[nConfig - 1].lock = lock; + + qsort(Config, nConfig, sizeof(ENTRY), c_sort); } -int cfg_cmd (const char *arg) +int cfg_cmd(const char *arg) { - char *key, *val; - char buffer[256]; - - strncpy (buffer, arg, sizeof(buffer)); - key=strip(buffer, 0); - for (val=key; *val; val++) { - if (*val=='=') { - *val++='\0'; - break; + char *key, *val; + char buffer[256]; + + strncpy(buffer, arg, sizeof(buffer)); + key = strip(buffer, 0); + for (val = key; *val; val++) { + if (*val == '=') { + *val++ = '\0'; + break; + } } - } - if (*key=='\0' || *val=='\0') return -1; - if (!validchars(key)) return -1; - cfg_add ("", key, val, 1); - return 0; + if (*key == '\0' || *val == '\0') + return -1; + if (!validchars(key)) + return -1; + cfg_add("", key, val, 1); + return 0; } -char *cfg_list (const char *section) +char *cfg_list(const char *section) { - int i, len; - char *key, *list; - - /* calculate key length */ - len=strlen(section)+1; - - /* prepare search key */ - key=malloc(len+1); - strcpy (key, section); - strcat (key, "."); - - /* start with empty string */ - list=malloc(1); - *list='\0'; - - /* search matching entries */ - for (i=0; i<nConfig; i++) { - if (strncasecmp(Config[i].key, key, len)==0) { - list=realloc(list, strlen(list)+strlen(Config[i].key)-len+2); - if (*list!='\0') strcat (list, "|"); - strcat (list, Config[i].key+len); + int i, len; + char *key, *list; + + /* calculate key length */ + len = strlen(section) + 1; + + /* prepare search key */ + key = malloc(len + 1); + strcpy(key, section); + strcat(key, "."); + + /* start with empty string */ + list = malloc(1); + *list = '\0'; + + /* search matching entries */ + for (i = 0; i < nConfig; i++) { + if (strncasecmp(Config[i].key, key, len) == 0) { + list = realloc(list, strlen(list) + strlen(Config[i].key) - len + 2); + if (*list != '\0') + strcat(list, "|"); + strcat(list, Config[i].key + len); + } } - } - - free (key); - return list; + + free(key); + return list; } -static char *cfg_lookup (const char *section, const char *key) +static char *cfg_lookup(const char *section, const char *key) { - int len; - char *buffer; - ENTRY *entry; - - /* calculate key length */ - len=strlen(key)+1; - if (section!=NULL) - len+=strlen(section)+1; - - /* allocate buffer */ - buffer=malloc(len); - *buffer='\0'; - - /* prepare section:key */ - if (section!=NULL && *section!='\0') { - strcpy(buffer, section); - strcat(buffer, "."); - } - strcat (buffer, key); - - /* search entry */ - entry=bsearch(buffer, Config, nConfig, sizeof(ENTRY), c_lookup); - - /* free buffer again */ - free (buffer); - - if (entry!=NULL) - return entry->val; - - return NULL; + int len; + char *buffer; + ENTRY *entry; + + /* calculate key length */ + len = strlen(key) + 1; + if (section != NULL) + len += strlen(section) + 1; + + /* allocate buffer */ + buffer = malloc(len); + *buffer = '\0'; + + /* prepare section:key */ + if (section != NULL && *section != '\0') { + strcpy(buffer, section); + strcat(buffer, "."); + } + strcat(buffer, key); + + /* search entry */ + entry = bsearch(buffer, Config, nConfig, sizeof(ENTRY), c_lookup); + + /* free buffer again */ + free(buffer); + + if (entry != NULL) + return entry->val; + + return NULL; } -char *cfg_get_raw (const char *section, const char *key, const char *defval) +char *cfg_get_raw(const char *section, const char *key, const char *defval) { - char *val=cfg_lookup(section, key); - - if (val!=NULL) return val; - return (char *)defval; + char *val = cfg_lookup(section, key); + + if (val != NULL) + return val; + return (char *) defval; } -char *cfg_get (const char *section, const char *key, const char *defval) +char *cfg_get(const char *section, const char *key, const char *defval) { - char *expression; - char *retval; - void *tree = NULL; - RESULT result = {0, 0, 0, NULL}; - - expression=cfg_lookup(section, key); - - if (expression!=NULL) { - if (*expression=='\0') return ""; - if (Compile(expression, &tree)==0 && Eval(tree, &result)==0) { - retval=strdup(R2S(&result)); - DelTree(tree); - DelResult(&result); - return(retval); + char *expression; + char *retval; + void *tree = NULL; + RESULT result = { 0, 0, 0, NULL }; + + expression = cfg_lookup(section, key); + + if (expression != NULL) { + if (*expression == '\0') + return ""; + if (Compile(expression, &tree) == 0 && Eval(tree, &result) == 0) { + retval = strdup(R2S(&result)); + DelTree(tree); + DelResult(&result); + return (retval); + } + DelTree(tree); + DelResult(&result); } - DelTree(tree); - DelResult(&result); - } - if (defval) return strdup(defval); - return NULL; + if (defval) + return strdup(defval); + return NULL; } -int cfg_number (const char *section, const char *key, const int defval, const int min, const int max, int *value) +int cfg_number(const char *section, const char *key, const int defval, const int min, const int max, int *value) { - char *expression; - void *tree = NULL; - RESULT result = {0, 0, 0, NULL}; - - /* start with default value */ - /* in case of an (uncatched) error, you have the */ - /* default value set, which may be handy... */ - *value=defval; - - expression=cfg_get_raw(section, key, NULL); - if (expression==NULL || *expression=='\0') { - return 0; - } - - if (Compile(expression, &tree) != 0) { - DelTree(tree); - return -1; - } - if (Eval(tree, &result) != 0) { + char *expression; + void *tree = NULL; + RESULT result = { 0, 0, 0, NULL }; + + /* start with default value */ + /* in case of an (uncatched) error, you have the */ + /* default value set, which may be handy... */ + *value = defval; + + expression = cfg_get_raw(section, key, NULL); + if (expression == NULL || *expression == '\0') { + return 0; + } + + if (Compile(expression, &tree) != 0) { + DelTree(tree); + return -1; + } + if (Eval(tree, &result) != 0) { + DelTree(tree); + DelResult(&result); + return -1; + } + *value = R2N(&result); DelTree(tree); DelResult(&result); - return -1; - } - *value=R2N(&result); - DelTree (tree); - DelResult(&result); - - if (*value<min) { - error ("bad '%s' value '%d' in %s, minimum is %d", key, *value, cfg_source(), min); - *value=min; - return -1; - } - - if (max > min && max != -1 && *value > max) { - error ("bad '%s' value '%d' in %s, maximum is %d", key, *value, cfg_source(), max); - *value=max; - return -1; - } - - return 1; + + if (*value < min) { + error("bad '%s' value '%d' in %s, minimum is %d", key, *value, cfg_source(), min); + *value = min; + return -1; + } + + if (max > min && max != -1 && *value > max) { + error("bad '%s' value '%d' in %s, maximum is %d", key, *value, cfg_source(), max); + *value = max; + return -1; + } + + return 1; } static int cfg_check_source(const char *file) { - /* as passwords and commands are stored in the config file, - * we will check that: - * - file is a normal file (or /dev/null) - * - file owner is owner of program - * - file is not accessible by group - * - file is not accessible by other - */ - - struct stat stbuf; - uid_t uid, gid; - int error; - - uid = geteuid(); - gid = getegid(); - - if (stat(file, &stbuf) == -1) { - error ("stat(%s) failed: %s", file, strerror(errno)); - return -1; - } - if (S_ISCHR(stbuf.st_mode) && strcmp(file, "/dev/null") == 0) - return 0; - - error=0; - if (!S_ISREG(stbuf.st_mode)) { - error ("security error: '%s' is not a regular file", file); - error=-1; - } - if (stbuf.st_uid != uid || stbuf.st_gid != gid) { - error ("security error: owner and/or group of '%s' don't match", file); - error=-1; - } - if (stbuf.st_mode & S_IRWXG || stbuf.st_mode & S_IRWXO) { - error ("security error: group or other have access to '%s'", file); - error=-1; - } - return error; + /* as passwords and commands are stored in the config file, + * we will check that: + * - file is a normal file (or /dev/null) + * - file owner is owner of program + * - file is not accessible by group + * - file is not accessible by other + */ + + struct stat stbuf; + uid_t uid, gid; + int error; + + uid = geteuid(); + gid = getegid(); + + if (stat(file, &stbuf) == -1) { + error("stat(%s) failed: %s", file, strerror(errno)); + return -1; + } + if (S_ISCHR(stbuf.st_mode) && strcmp(file, "/dev/null") == 0) + return 0; + + error = 0; + if (!S_ISREG(stbuf.st_mode)) { + error("security error: '%s' is not a regular file", file); + error = -1; + } + if (stbuf.st_uid != uid || stbuf.st_gid != gid) { + error("security error: owner and/or group of '%s' don't match", file); + error = -1; + } + if (stbuf.st_mode & S_IRWXG || stbuf.st_mode & S_IRWXO) { + error("security error: group or other have access to '%s'", file); + error = -1; + } + return error; } -static int cfg_read (const char *file) +static int cfg_read(const char *file) { - FILE *stream; - char buffer[256]; - char section[256]; - char *line, *key, *val, *end; - int section_open, section_close; - int error, lineno; - - stream=fopen (file, "r"); - if (stream==NULL) { - error ("open(%s) failed: %s", file, strerror(errno)); - return -1; - } - - /* start with empty section */ - strcpy(section, ""); - - error=0; - lineno=0; - while ((line=fgets(buffer,256,stream))!=NULL) { - - /* increment line number */ - lineno++; - - /* skip empty lines */ - if (*(line=strip(line, 1))=='\0') continue; - - /* reset section flags */ - section_open=0; - section_close=0; - - /* key is first word */ - key=line; - - /* search first blank between key and value */ - for (val=line; *val; val++) { - if (isblank(*val)) { - *val++='\0'; - break; - } + FILE *stream; + char buffer[256]; + char section[256]; + char *line, *key, *val, *end; + int section_open, section_close; + int error, lineno; + + stream = fopen(file, "r"); + if (stream == NULL) { + error("open(%s) failed: %s", file, strerror(errno)); + return -1; } - /* strip value */ - val=strip(val, 1); + /* start with empty section */ + strcpy(section, ""); - /* search end of value */ - if (*val) for (end=val; *(end+1); end++); - else end=val; + error = 0; + lineno = 0; + while ((line = fgets(buffer, 256, stream)) != NULL) { - /* if last char is '{', a section has been opened */ - if (*end=='{') { - section_open=1; - *end='\0'; - val=strip(val, 0); - } + /* increment line number */ + lineno++; - /* provess "value" in double-quotes */ - if (*val=='"' && *end=='"') { - *end='\0'; - val++; - } + /* skip empty lines */ + if (*(line = strip(line, 1)) == '\0') + continue; - /* if key is '}', a section has been closed */ - if (strcmp(key, "}")==0) { - section_close=1; - *key='\0'; - } + /* reset section flags */ + section_open = 0; + section_close = 0; - /* sanity check: '}' should be the only char in a line */ - if (section_close && (section_open || *val!='\0')) { - error ("error in config file '%s' line %d: garbage after '}'", file, lineno); - error=1; - break; - } - - /* check key for valid chars */ - if (!validchars(key)) { - error ("error in config file '%s' line %d: key '%s' is invalid", file, lineno, key); - error=1; - break; - } + /* key is first word */ + key = line; - /* on section-open, check value for valid chars */ - if (section_open && !validchars(val)) { - error ("error in config file '%s' line %d: section '%s' is invalid", file, lineno, val); - error=1; - break; - } + /* search first blank between key and value */ + for (val = line; *val; val++) { + if (isblank(*val)) { + *val++ = '\0'; + break; + } + } - /* on section-open, append new section name */ - if (section_open) { - /* is the section[] array big enough? */ - if (strlen(section)+strlen(key)+3 > sizeof(section)) { - error ("error in config file '%s' line %d: section buffer overflow", file, lineno); - error=1; - break; - } - if (*section!='\0') strcat (section, "."); - strcat (section, key); - if (*val!='\0') { - strcat (section, ":"); - strcat (section, val); - } - continue; - } + /* strip value */ + val = strip(val, 1); + + /* search end of value */ + if (*val) + for (end = val; *(end + 1); end++); + else + end = val; + + /* if last char is '{', a section has been opened */ + if (*end == '{') { + section_open = 1; + *end = '\0'; + val = strip(val, 0); + } + + /* provess "value" in double-quotes */ + if (*val == '"' && *end == '"') { + *end = '\0'; + val++; + } + + /* if key is '}', a section has been closed */ + if (strcmp(key, "}") == 0) { + section_close = 1; + *key = '\0'; + } + + /* sanity check: '}' should be the only char in a line */ + if (section_close && (section_open || *val != '\0')) { + error("error in config file '%s' line %d: garbage after '}'", file, lineno); + error = 1; + break; + } + + /* check key for valid chars */ + if (!validchars(key)) { + error("error in config file '%s' line %d: key '%s' is invalid", file, lineno, key); + error = 1; + break; + } + + /* on section-open, check value for valid chars */ + if (section_open && !validchars(val)) { + error("error in config file '%s' line %d: section '%s' is invalid", file, lineno, val); + error = 1; + break; + } + + /* on section-open, append new section name */ + if (section_open) { + /* is the section[] array big enough? */ + if (strlen(section) + strlen(key) + 3 > sizeof(section)) { + error("error in config file '%s' line %d: section buffer overflow", file, lineno); + error = 1; + break; + } + if (*section != '\0') + strcat(section, "."); + strcat(section, key); + if (*val != '\0') { + strcat(section, ":"); + strcat(section, val); + } + continue; + } + + /* on section-close, remove last section name */ + if (section_close) { + /* sanity check: section already empty? */ + if (*section == '\0') { + error("error in config file '%s' line %d: unmatched closing brace", file, lineno); + error = 1; + break; + } + + end = strrchr(section, '.'); + if (end == NULL) + *section = '\0'; + else + *end = '\0'; + continue; + } + + /* finally: add key */ + cfg_add(section, key, val, 0); - /* on section-close, remove last section name */ - if (section_close) { - /* sanity check: section already empty? */ - if (*section=='\0') { - error ("error in config file '%s' line %d: unmatched closing brace", file, lineno); - error=1; - break; - } - - end=strrchr(section, '.'); - if (end==NULL) - *section='\0'; - else - *end='\0'; - continue; } - /* finally: add key */ - cfg_add (section, key, val, 0); - - } - - /* sanity check: are the braces balanced? */ - if (!error && *section!='\0') { - error ("error in config file '%s' line %d: unbalanced braces", file, lineno); - error=1; - } + /* sanity check: are the braces balanced? */ + if (!error && *section != '\0') { + error("error in config file '%s' line %d: unbalanced braces", file, lineno); + error = 1; + } - fclose (stream); + fclose(stream); - return -error; + return -error; } -int cfg_init (const char *file) +int cfg_init(const char *file) { - if (cfg_check_source(file) == -1) { - return -1; - } - - if (cfg_read(file)<0) return -1; - - if (Config_File) free (Config_File); - Config_File=strdup(file); - - return 0; + if (cfg_check_source(file) == -1) { + return -1; + } + + if (cfg_read(file) < 0) + return -1; + + if (Config_File) + free(Config_File); + Config_File = strdup(file); + + return 0; } -char *cfg_source (void) +char *cfg_source(void) { - if (Config_File) - return Config_File; - else - return ""; + if (Config_File) + return Config_File; + else + return ""; } -int cfg_exit (void) +int cfg_exit(void) { - int i; - for (i=0; i<nConfig; i++) { - if (Config[i].key) free (Config[i].key); - if (Config[i].val) free (Config[i].val); - } - - if (Config) { - free (Config); - Config=NULL; - } - - if (Config_File) { - free (Config_File); - Config_File=NULL; - } - - return 0; -} + int i; + for (i = 0; i < nConfig; i++) { + if (Config[i].key) + free(Config[i].key); + if (Config[i].val) + free(Config[i].val); + } + + if (Config) { + free(Config); + Config = NULL; + } + + if (Config_File) { + free(Config_File); + Config_File = NULL; + } + return 0; +} @@ -1,4 +1,4 @@ -/* $Id: cfg.h,v 1.12 2005/01/18 06:30:21 reinelt Exp $ +/* $Id: cfg.h,v 1.13 2005/05/08 04:32:43 reinelt Exp $ * * config file stuff * @@ -23,6 +23,9 @@ * * * $Log: cfg.h,v $ + * Revision 1.13 2005/05/08 04:32:43 reinelt + * CodingStyle added and applied + * * Revision 1.12 2005/01/18 06:30:21 reinelt * added (C) to all copyright statements * @@ -77,14 +80,13 @@ #ifndef _CFG_H_ #define _CFG_H_ -int cfg_init (const char *file); -char *cfg_source (void); -int cfg_cmd (const char *arg); -char *cfg_list (const char *section); -char *cfg_get_raw (const char *section, const char *key, const char *defval); -char *cfg_get (const char *section, const char *key, const char *defval); -int cfg_number (const char *section, const char *key, const int defval, - const int min, const int max, int *value); -int cfg_exit (void); +int cfg_init(const char *file); +char *cfg_source(void); +int cfg_cmd(const char *arg); +char *cfg_list(const char *section); +char *cfg_get_raw(const char *section, const char *key, const char *defval); +char *cfg_get(const char *section, const char *key, const char *defval); +int cfg_number(const char *section, const char *key, const int defval, const int min, const int max, int *value); +int cfg_exit(void); #endif @@ -5593,7 +5593,7 @@ done # Find any Python interpreter. - for ac_prog in python python2 python2.3 python2.2 python2.1 python2.0 python1.6 python1.5 + for ac_prog in python python2 python2.4 python2.3 python2.2 python2.1 python2.0 python1.6 python1.5 do # Extract the first word of "$ac_prog", so it can be a program name with args. set dummy $ac_prog; ac_word=$2 @@ -1,4 +1,4 @@ -/* $Id: debug.c,v 1.12 2005/01/18 06:30:22 reinelt Exp $ +/* $Id: debug.c,v 1.13 2005/05/08 04:32:43 reinelt Exp $ * * debug() and error() functions * @@ -23,6 +23,9 @@ * * * $Log: debug.c,v $ + * Revision 1.13 2005/05/08 04:32:43 reinelt + * CodingStyle added and applied + * * Revision 1.12 2005/01/18 06:30:22 reinelt * added (C) to all copyright statements * @@ -96,43 +99,44 @@ int running_background = 0; int verbose_level = 0; -void message (const int level, const char *format, ...) +void message(const int level, const char *format, ...) { - va_list ap; - char buffer[256]; - static int log_open=0; + va_list ap; + char buffer[256]; + static int log_open = 0; + + if (level > verbose_level) + return; + + va_start(ap, format); + vsnprintf(buffer, sizeof(buffer), format, ap); + va_end(ap); - if (level>verbose_level) return; + if (!running_background) { - va_start(ap, format); - vsnprintf(buffer, sizeof(buffer), format, ap); - va_end(ap); - - if (!running_background) { - #ifdef WITH_CURSES - extern int curses_error(char *); - if (!curses_error(buffer)) -#endif - fprintf (level?stdout:stderr, "%s\n", buffer); - } - - if (running_foreground) - return; - - if (!log_open) { - openlog ("LCD4Linux", LOG_PID, LOG_USER); - log_open = 1; - } - - switch (level) { - case 0: - syslog (LOG_ERR, "%s", buffer); - break; - case 1: - syslog (LOG_INFO, "%s", buffer); - break; - default: - syslog (LOG_DEBUG, "%s", buffer); - } + extern int curses_error(char *); + if (!curses_error(buffer)) +#endif + fprintf(level ? stdout : stderr, "%s\n", buffer); + } + + if (running_foreground) + return; + + if (!log_open) { + openlog("LCD4Linux", LOG_PID, LOG_USER); + log_open = 1; + } + + switch (level) { + case 0: + syslog(LOG_ERR, "%s", buffer); + break; + case 1: + syslog(LOG_INFO, "%s", buffer); + break; + default: + syslog(LOG_DEBUG, "%s", buffer); + } } @@ -1,4 +1,4 @@ -/* $Id: debug.h,v 1.9 2005/01/18 06:30:22 reinelt Exp $ +/* $Id: debug.h,v 1.10 2005/05/08 04:32:43 reinelt Exp $ * * debug messages * @@ -23,6 +23,9 @@ * * * $Log: debug.h,v $ + * Revision 1.10 2005/05/08 04:32:43 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:22 reinelt * added (C) to all copyright statements * @@ -70,7 +73,7 @@ extern int running_foreground; extern int running_background; extern int verbose_level; -void message (const int level, const char *format, ...); +void message(const int level, const char *format, ...); #define debug(args...) message (2, __FILE__ ": " args) #define info(args...) message (1, args) @@ -1,4 +1,4 @@ -/* $Id: drv.c,v 1.30 2005/05/04 05:42:38 reinelt Exp $ +/* $Id: drv.c,v 1.31 2005/05/08 04:32:43 reinelt Exp $ * * new framework for display drivers * @@ -23,6 +23,9 @@ * * * $Log: drv.c,v $ + * Revision 1.31 2005/05/08 04:32:43 reinelt + * CodingStyle added and applied + * * Revision 1.30 2005/05/04 05:42:38 reinelt * Noritake driver added * @@ -216,109 +219,112 @@ extern DRIVER drv_X11; * has to be defined here because it's referenced * even if the raster driver is not included! */ -char *output=NULL; +char *output = NULL; DRIVER *Driver[] = { #ifdef WITH_BECKMANNEGLE - &drv_BeckmannEgle, + &drv_BeckmannEgle, #endif #ifdef WITH_BWCT - &drv_BWCT, + &drv_BWCT, #endif #ifdef WITH_CRYSTALFONTZ - &drv_Crystalfontz, + &drv_Crystalfontz, #endif #ifdef WITH_CWLINUX - &drv_Cwlinux, + &drv_Cwlinux, #endif #ifdef WITH_CURSES - &drv_Curses, + &drv_Curses, #endif #ifdef WITH_HD44780 - &drv_HD44780, + &drv_HD44780, #endif #if defined (WITH_PNG) || defined(WITH_PPM) - &drv_Image, + &drv_Image, #endif #ifdef WITH_LCDLINUX - &drv_LCDLinux, + &drv_LCDLinux, #endif #ifdef WITH_LCDTERM - &drv_LCDTerm, + &drv_LCDTerm, #endif #ifdef WITH_M50530 - &drv_M50530, + &drv_M50530, #endif #ifdef WITH_MATRIXORBITAL - &drv_MatrixOrbital, + &drv_MatrixOrbital, #endif #ifdef WITH_MILINST - &drv_MilfordInstruments, + &drv_MilfordInstruments, #endif #ifdef WITH_NORITAKE - &drv_Noritake, + &drv_Noritake, #endif #ifdef WITH_NULL - &drv_NULL, + &drv_NULL, #endif #ifdef WITH_ROUTERBOARD - &drv_RouterBoard, + &drv_RouterBoard, #endif #ifdef WITH_SIMPLELCD - &drv_SimpleLCD, + &drv_SimpleLCD, #endif #ifdef WITH_T6963 - &drv_T6963, + &drv_T6963, #endif #ifdef WITH_TREFON - &drv_Trefon, + &drv_Trefon, #endif #ifdef WITH_USBLCD - &drv_USBLCD, + &drv_USBLCD, #endif #ifdef WITH_X11 - &drv_X11, + &drv_X11, #endif - NULL, + NULL, }; static DRIVER *Drv = NULL; -int drv_list (void) +int drv_list(void) { - int i; + int i; + + printf("available display drivers:"); - printf ("available display drivers:"); - - for (i = 0; Driver[i]; i++) { - printf ("\n %-20s: ", Driver[i]->name); - if (Driver[i]->list) Driver[i]->list(); - } - printf ("\n"); - return 0; + for (i = 0; Driver[i]; i++) { + printf("\n %-20s: ", Driver[i]->name); + if (Driver[i]->list) + Driver[i]->list(); + } + printf("\n"); + return 0; } -int drv_init (const char *section, const char *driver, const int quiet) +int drv_init(const char *section, const char *driver, const int quiet) { - int i; - for (i = 0; Driver[i]; i++) { - if (strcmp (Driver[i]->name, driver) == 0) { - Drv = Driver[i]; - if (Drv->init == NULL) return 0; - return Drv->init(section, quiet); + int i; + for (i = 0; Driver[i]; i++) { + if (strcmp(Driver[i]->name, driver) == 0) { + Drv = Driver[i]; + if (Drv->init == NULL) + return 0; + return Drv->init(section, quiet); + } } - } - error ("drv_init(%s) failed: no such driver", driver); - return -1; + error("drv_init(%s) failed: no such driver", driver); + return -1; } -int drv_quit (const int quiet) +int drv_quit(const int quiet) { - if (Drv->quit == NULL) return 0; - return Drv->quit(quiet); + if (Drv->quit == NULL) + return 0; + return Drv->quit(quiet); } @@ -1,4 +1,4 @@ -/* $Id: drv.h,v 1.9 2005/01/18 06:30:22 reinelt Exp $ +/* $Id: drv.h,v 1.10 2005/05/08 04:32:43 reinelt Exp $ * * new framework for display drivers * @@ -23,6 +23,9 @@ * * * $Log: drv.h,v $ + * Revision 1.10 2005/05/08 04:32:43 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:22 reinelt * added (C) to all copyright statements * @@ -71,10 +74,10 @@ #define _DRV_H_ typedef struct DRIVER { - char *name; - int (*list) (void); - int (*init) (const char *section, const int quiet); - int (*quit) (const int quiet); + char *name; + int (*list) (void); + int (*init) (const char *section, const int quiet); + int (*quit) (const int quiet); } DRIVER; @@ -84,8 +87,8 @@ typedef struct DRIVER { */ extern char *output; -int drv_list (void); -int drv_init (const char *section, const char *driver, const int quiet); -int drv_quit (const int quiet); +int drv_list(void); +int drv_init(const char *section, const char *driver, const int quiet); +int drv_quit(const int quiet); #endif @@ -1,4 +1,4 @@ -/* $Id: drv_BWCT.c,v 1.2 2005/01/18 06:30:22 reinelt Exp $ +/* $Id: drv_BWCT.c,v 1.3 2005/05/08 04:32:43 reinelt Exp $ * * new style driver for BWCT USB LCD displays * @@ -23,6 +23,9 @@ * * * $Log: drv_BWCT.c,v $ + * Revision 1.3 2005/05/08 04:32:43 reinelt + * CodingStyle added and applied + * * Revision 1.2 2005/01/18 06:30:22 reinelt * added (C) to all copyright statements * @@ -74,7 +77,7 @@ #define LCD_CONTRAST 4 -static char Name[]="BWCT"; +static char Name[] = "BWCT"; static usb_dev_handle *lcd; static int interface; @@ -87,186 +90,190 @@ extern int got_signal; /*** hardware dependant functions ***/ /****************************************/ -static int drv_BW_open (void) +static int drv_BW_open(void) { - struct usb_bus *busses, *bus; - struct usb_device *dev; - - lcd = NULL; - - info ("%s: scanning USB for BWCT LCD...", Name); - - usb_debug = 0; - - usb_init(); - usb_find_busses(); - usb_find_devices(); - busses = usb_get_busses(); - - for (bus = busses; bus; bus = bus->next) { - for (dev = bus->devices; dev; dev = dev->next) { - if (dev->descriptor.idVendor != LCD_USB_VENDOR) continue; - /* Loop through all of the configurations */ - int c; - for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { - int i; - for (i = 0; i < dev->config[c].bNumInterfaces; i++) { - int a; - for (a = 0; a < dev->config[c].interface[i].num_altsetting; a++) { - if ((dev->descriptor.idProduct == LCD_USB_DEVICE) || - ((dev->config[c].interface[i].altsetting[a].bInterfaceClass == 0xff) && - (dev->config[c].interface[i].altsetting[a].bInterfaceSubClass == 0x01))) { - info ("%s: found BWCT USB LCD on bus %s device %s", Name, bus->dirname, dev->filename); - interface = i; - lcd = usb_open(dev); - if (usb_claim_interface(lcd, interface) < 0) { - error ("%s: usb_claim_interface() failed!", Name); - return -1; - } - return 0; + struct usb_bus *busses, *bus; + struct usb_device *dev; + + lcd = NULL; + + info("%s: scanning USB for BWCT LCD...", Name); + + usb_debug = 0; + + usb_init(); + usb_find_busses(); + usb_find_devices(); + busses = usb_get_busses(); + + for (bus = busses; bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + if (dev->descriptor.idVendor != LCD_USB_VENDOR) + continue; + /* Loop through all of the configurations */ + int c; + for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { + int i; + for (i = 0; i < dev->config[c].bNumInterfaces; i++) { + int a; + for (a = 0; a < dev->config[c].interface[i].num_altsetting; a++) { + if ((dev->descriptor.idProduct == LCD_USB_DEVICE) || + ((dev->config[c].interface[i].altsetting[a].bInterfaceClass == 0xff) && + (dev->config[c].interface[i].altsetting[a].bInterfaceSubClass == 0x01))) { + info("%s: found BWCT USB LCD on bus %s device %s", Name, bus->dirname, dev->filename); + interface = i; + lcd = usb_open(dev); + if (usb_claim_interface(lcd, interface) < 0) { + error("%s: usb_claim_interface() failed!", Name); + return -1; + } + return 0; + } + } + } } - } } - } } - } - return -1; + return -1; } -static int drv_BW_close (void) +static int drv_BW_close(void) { - usb_release_interface(lcd, interface); - usb_close(lcd); + usb_release_interface(lcd, interface); + usb_close(lcd); - return 0; + return 0; } -static int drv_BW_send (int request, int value) +static int drv_BW_send(int request, int value) { - static int errors = 0; - - if (errors > 20) return -1; - - if (usb_control_msg(lcd, USB_TYPE_VENDOR, request, value, interface, NULL, 0, 1000) < 0) { - error("%s: USB request failed!", Name); - if (++errors > 20) { - error("%s: too many USB errors, aborting.", Name); - got_signal = -1; + static int errors = 0; + + if (errors > 20) + return -1; + + if (usb_control_msg(lcd, USB_TYPE_VENDOR, request, value, interface, NULL, 0, 1000) < 0) { + error("%s: USB request failed!", Name); + if (++errors > 20) { + error("%s: too many USB errors, aborting.", Name); + got_signal = -1; + } + return -1; } - return -1; - } - errors = 0; - return 0; + errors = 0; + return 0; } -static void drv_BW_command (const unsigned char cmd) +static void drv_BW_command(const unsigned char cmd) { - drv_BW_send (LCD_CMD, cmd); + drv_BW_send(LCD_CMD, cmd); } -static void drv_BW_clear (void) +static void drv_BW_clear(void) { - drv_BW_command (0x01); /* clear display */ - drv_BW_command (0x03); /* return home */ + drv_BW_command(0x01); /* clear display */ + drv_BW_command(0x03); /* return home */ } -static void drv_BW_write (const int row, const int col, const char *data, int len) +static void drv_BW_write(const int row, const int col, const char *data, int len) { - int pos; - - /* 16x4 Displays use a slightly different layout */ - if (DCOLS==16 && DROWS==4) { - pos = (row%2)*64+(row/2)*16+col; - } else { - pos = (row%2)*64+(row/2)*20+col; - } - - drv_BW_command (0x80|pos); - - while (len--) { - drv_BW_send (LCD_DATA, *data++); - } + int pos; + + /* 16x4 Displays use a slightly different layout */ + if (DCOLS == 16 && DROWS == 4) { + pos = (row % 2) * 64 + (row / 2) * 16 + col; + } else { + pos = (row % 2) * 64 + (row / 2) * 20 + col; + } + + drv_BW_command(0x80 | pos); + + while (len--) { + drv_BW_send(LCD_DATA, *data++); + } } -static void drv_BW_defchar (const int ascii, const unsigned char *matrix) +static void drv_BW_defchar(const int ascii, const unsigned char *matrix) { - int i; - - drv_BW_command (0x40|8*ascii); + int i; + + drv_BW_command(0x40 | 8 * ascii); - for (i = 0; i < 8; i++) { - drv_BW_send (LCD_DATA, *matrix++ & 0x1f); - } + for (i = 0; i < 8; i++) { + drv_BW_send(LCD_DATA, *matrix++ & 0x1f); + } } -static int drv_BW_contrast (int contrast) +static int drv_BW_contrast(int contrast) { - if (contrast < 0) contrast = 0; - if (contrast > 255) contrast = 255; + if (contrast < 0) + contrast = 0; + if (contrast > 255) + contrast = 255; - drv_BW_send (LCD_CONTRAST, contrast); + drv_BW_send(LCD_CONTRAST, contrast); - return contrast; + return contrast; } - -static int drv_BW_start (const char *section, const int quiet) + +static int drv_BW_start(const char *section, const int quiet) { - int contrast; - int rows=-1, cols=-1; - char *s; + int contrast; + int rows = -1, cols = -1; + char *s; + + s = cfg_get(section, "Size", NULL); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + return -1; + } + if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } - s=cfg_get(section, "Size", NULL); - if (s==NULL || *s=='\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - return -1; - } - if (sscanf(s,"%dx%d",&cols,&rows)!=2 || rows<1 || cols<1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); - free (s); - return -1; - } - - DROWS = rows; - DCOLS = cols; - - if (drv_BW_open() < 0) { - error ("%s: could not find a BWCT USB LCD", Name); - return -1; - } - - /* reset */ - drv_BW_send (LCD_RESET, 0); - - /* initialize display */ - drv_BW_command (0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ - drv_BW_command (0x08); /* Display off, cursor off, blink off */ - drv_BW_command (0x0c); /* Display on, cursor off, blink off */ - drv_BW_command (0x06); /* curser moves to right, no shift */ - - - if (cfg_number(section, "Contrast", 0, 0, 100, &contrast) > 0) { - drv_BW_contrast(contrast); - } - - drv_BW_clear(); /* clear display */ - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, "www.bwct.de")) { - sleep (3); - drv_BW_clear(); + DROWS = rows; + DCOLS = cols; + + if (drv_BW_open() < 0) { + error("%s: could not find a BWCT USB LCD", Name); + return -1; } - } - - return 0; + + /* reset */ + drv_BW_send(LCD_RESET, 0); + + /* initialize display */ + drv_BW_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ + drv_BW_command(0x08); /* Display off, cursor off, blink off */ + drv_BW_command(0x0c); /* Display on, cursor off, blink off */ + drv_BW_command(0x06); /* curser moves to right, no shift */ + + + if (cfg_number(section, "Contrast", 0, 0, 100, &contrast) > 0) { + drv_BW_contrast(contrast); + } + + drv_BW_clear(); /* clear display */ + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, "www.bwct.de")) { + sleep(3); + drv_BW_clear(); + } + } + + return 0; } @@ -274,12 +281,12 @@ static int drv_BW_start (const char *section, const int quiet) /*** plugins ***/ /****************************************/ -static void plugin_contrast (RESULT *result, RESULT *arg1) +static void plugin_contrast(RESULT * result, RESULT * arg1) { - double contrast; - - contrast = drv_BW_contrast(R2N(arg1)); - SetResult(&result, R_NUMBER, &contrast); + double contrast; + + contrast = drv_BW_contrast(R2N(arg1)); + SetResult(&result, R_NUMBER, &contrast); } @@ -299,107 +306,105 @@ static void plugin_contrast (RESULT *result, RESULT *arg1) /* list models */ -int drv_BW_list (void) +int drv_BW_list(void) { - printf ("generic"); - return 0; + printf("generic"); + return 0; } /* initialize driver & display */ -int drv_BW_init (const char *section, const int quiet) +int drv_BW_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int asc255bug; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = 2; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_BW_write; - drv_generic_text_real_defchar = drv_BW_defchar; - - - /* start display */ - if ((ret=drv_BW_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - /* most displays have a full block on ascii 255, but some have kind of */ - /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ - /* char will not be used, but rendered by the bar driver */ - cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - if (!asc255bug) - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - AddFunction ("LCD::contrast", 1, plugin_contrast); - - return 0; + WIDGET_CLASS wc; + int asc255bug; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 2; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_BW_write; + drv_generic_text_real_defchar = drv_BW_defchar; + + + /* start display */ + if ((ret = drv_BW_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + /* most displays have a full block on ascii 255, but some have kind of */ + /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ + /* char will not be used, but rendered by the bar driver */ + cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + if (!asc255bug) + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + AddFunction("LCD::contrast", 1, plugin_contrast); + + return 0; } /* close driver & display */ -int drv_BW_quit (const int quiet) +int drv_BW_quit(const int quiet) { - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_BW_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - debug ("closing USB connection"); - drv_BW_close(); - - return (0); + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_BW_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + debug("closing USB connection"); + drv_BW_close(); + + return (0); } DRIVER drv_BWCT = { - name: Name, - list: drv_BW_list, - init: drv_BW_init, - quit: drv_BW_quit, + name:Name, + list:drv_BW_list, + init:drv_BW_init, + quit:drv_BW_quit, }; - - diff --git a/drv_BeckmannEgle.c b/drv_BeckmannEgle.c index cb16960..07a6ca7 100644 --- a/drv_BeckmannEgle.c +++ b/drv_BeckmannEgle.c @@ -1,4 +1,4 @@ -/* $Id: drv_BeckmannEgle.c,v 1.14 2005/01/18 06:30:22 reinelt Exp $ +/* $Id: drv_BeckmannEgle.c,v 1.15 2005/05/08 04:32:44 reinelt Exp $ * * driver for Beckmann+Egle "Mini Terminals" and "Compact Terminals" * Copyright (C) 2000 Michael Reinelt <reinelt@eunet.at> @@ -22,6 +22,9 @@ * * * $Log: drv_BeckmannEgle.c,v $ + * Revision 1.15 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.14 2005/01/18 06:30:22 reinelt * added (C) to all copyright statements * @@ -119,39 +122,39 @@ #define ESC "\033" -static char Name[]="Beckmann+Egle"; +static char Name[] = "Beckmann+Egle"; typedef struct { - char *name; - int rows; - int cols; - int protocol; - int type; + char *name; + int rows; + int cols; + int protocol; + int type; } MODEL; static MODEL Models[] = { - /* MultiTerminals */ - { "MT16x1", 1, 16, 1, 0 }, - { "MT16x2", 2, 16, 1, 1 }, - { "MT16x4", 4, 16, 1, 2 }, - { "MT20x1", 1, 20, 1, 3 }, - { "MT20x2", 2, 20, 1, 4 }, - { "MT20x4", 4, 20, 1, 5 }, - { "MT24x1", 1, 24, 1, 6 }, - { "MT24x2", 2, 24, 1, 7 }, - { "MT32x1", 1, 32, 1, 8 }, - { "MT32x2", 2, 32, 1, 9 }, - { "MT40x1", 1, 40, 1, 10 }, - { "MT40x2", 2, 40, 1, 11 }, - { "MT40x4", 4, 40, 1, 12 }, - - /* CompactTerminal */ - { "CT20x4", 4, 20, 2, 0 }, - - { NULL, 0, 0, 0, 0 }, + /* MultiTerminals */ + {"MT16x1", 1, 16, 1, 0}, + {"MT16x2", 2, 16, 1, 1}, + {"MT16x4", 4, 16, 1, 2}, + {"MT20x1", 1, 20, 1, 3}, + {"MT20x2", 2, 20, 1, 4}, + {"MT20x4", 4, 20, 1, 5}, + {"MT24x1", 1, 24, 1, 6}, + {"MT24x2", 2, 24, 1, 7}, + {"MT32x1", 1, 32, 1, 8}, + {"MT32x2", 2, 32, 1, 9}, + {"MT40x1", 1, 40, 1, 10}, + {"MT40x2", 2, 40, 1, 11}, + {"MT40x4", 4, 40, 1, 12}, + + /* CompactTerminal */ + {"CT20x4", 4, 20, 2, 0}, + + {NULL, 0, 0, 0, 0}, }; @@ -164,378 +167,395 @@ static int Protocol; /*** hardware dependant functions ***/ /****************************************/ -static void drv_BuE_clear (void) +static void drv_BuE_clear(void) { - switch (Protocol) { - case 1: - drv_generic_serial_write (ESC "&#", 3); /* clear display */ - break; - case 2: - drv_generic_serial_write (ESC "LL", 3); /* clear display */ - break; - } + switch (Protocol) { + case 1: + drv_generic_serial_write(ESC "&#", 3); /* clear display */ + break; + case 2: + drv_generic_serial_write(ESC "LL", 3); /* clear display */ + break; + } } -static void drv_BuE_MT_write (const int row, const int col, const char *data, const int len) +static void drv_BuE_MT_write(const int row, const int col, const char *data, const int len) { - char cmd[] = ESC "[y;xH"; + char cmd[] = ESC "[y;xH"; - cmd[2] = (char)row; - cmd[4] = (char)col; + cmd[2] = (char) row; + cmd[4] = (char) col; - drv_generic_serial_write (cmd, 6); - drv_generic_serial_write (data, len); + drv_generic_serial_write(cmd, 6); + drv_generic_serial_write(data, len); } -static void drv_BuE_MT_defchar (const int ascii, const unsigned char *matrix) +static void drv_BuE_MT_defchar(const int ascii, const unsigned char *matrix) { - int i; - char cmd[22] = ESC "&T"; /* enter transparent mode */ + int i; + char cmd[22] = ESC "&T"; /* enter transparent mode */ - cmd[3] = '\0'; /* write cmd */ - cmd[4] = 0x40|8*ascii; /* write CGRAM */ + cmd[3] = '\0'; /* write cmd */ + cmd[4] = 0x40 | 8 * ascii; /* write CGRAM */ - for (i = 0; i < 8; i++) { - cmd[2*i+5] = '\1'; /* write data */ - cmd[2*i+6] = matrix[i] & 0x1f; /* character bitmap */ - } - cmd[21] = '\377'; /* leave transparent mode */ + for (i = 0; i < 8; i++) { + cmd[2 * i + 5] = '\1'; /* write data */ + cmd[2 * i + 6] = matrix[i] & 0x1f; /* character bitmap */ + } + cmd[21] = '\377'; /* leave transparent mode */ - drv_generic_serial_write (cmd, 22); + drv_generic_serial_write(cmd, 22); } -static void drv_BuE_CT_write (const int row, const int col, const char *data, const int len) +static void drv_BuE_CT_write(const int row, const int col, const char *data, const int len) { - char cmd[] = ESC "LCzs\001"; - cmd[3] = (char)row + 1; - cmd[4] = (char)col + 1; + char cmd[] = ESC "LCzs\001"; + cmd[3] = (char) row + 1; + cmd[4] = (char) col + 1; - drv_generic_serial_write (cmd, 6); - drv_generic_serial_write (data, len); + drv_generic_serial_write(cmd, 6); + drv_generic_serial_write(data, len); } -static void drv_BuE_CT_defchar (const int ascii, const unsigned char *matrix) +static void drv_BuE_CT_defchar(const int ascii, const unsigned char *matrix) { - int i; - char cmd[13] = ESC "LZ"; /* set custom char */ + int i; + char cmd[13] = ESC "LZ"; /* set custom char */ - /* number of user-defined char (0..7) */ - cmd[3] = (char) ascii - CHAR0; + /* number of user-defined char (0..7) */ + cmd[3] = (char) ascii - CHAR0; - /* ASCII code to replace */ - cmd[4] = (char) ascii; - - for (i = 0; i < 8; i++) { - cmd[i+5] = matrix[i] & 0x1f; - } + /* ASCII code to replace */ + cmd[4] = (char) ascii; - drv_generic_serial_write (cmd, 13); + for (i = 0; i < 8; i++) { + cmd[i + 5] = matrix[i] & 0x1f; + } + + drv_generic_serial_write(cmd, 13); } -static int drv_BuE_CT_contrast (int contrast) +static int drv_BuE_CT_contrast(int contrast) { - static char Contrast = 7; - char cmd[4] = ESC "LKn"; + static char Contrast = 7; + char cmd[4] = ESC "LKn"; + + /* -1 is used to query the current contrast */ + if (contrast == -1) + return Contrast; - /* -1 is used to query the current contrast */ - if (contrast == -1) return Contrast; + if (contrast < 0) + contrast = 0; + if (contrast > 15) + contrast = 15; + Contrast = contrast; - if (contrast < 0 ) contrast = 0; - if (contrast > 15) contrast = 15; - Contrast = contrast; - - cmd[3] = Contrast; + cmd[3] = Contrast; - drv_generic_serial_write (cmd, 4); - - return Contrast; + drv_generic_serial_write(cmd, 4); + + return Contrast; } -static int drv_BuE_CT_backlight (int backlight) +static int drv_BuE_CT_backlight(int backlight) { - static char Backlight=0; - char cmd[4] = ESC "LBn"; + static char Backlight = 0; + char cmd[4] = ESC "LBn"; + + /* -1 is used to query the current backlight */ + if (backlight == -1) + return Backlight; - /* -1 is used to query the current backlight */ - if (backlight == -1) return Backlight; + if (backlight < 0) + backlight = 0; + if (backlight > 1) + backlight = 1; + Backlight = backlight; - if (backlight < 0) backlight = 0; - if (backlight > 1) backlight = 1; - Backlight = backlight; - - cmd[3] = Backlight; + cmd[3] = Backlight; - drv_generic_serial_write (cmd, 4); + drv_generic_serial_write(cmd, 4); - return Backlight; + return Backlight; } -static int drv_BuE_CT_gpo (int num, int val) +static int drv_BuE_CT_gpo(int num, int val) { - static int GPO[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; - char cmd[4] = ESC "Pnx"; - - if (num < 0) num = 0; - if (num > 7) num = 7; - - /* -1 is used to query the current GPO */ - if (val == -1) return GPO[num]; - - if (val < 0 ) val = 0; - if (val > 255) val = 255; - GPO[num] = val; - - cmd[2] = (char)num; - cmd[3] = (char)val; - - drv_generic_serial_write (cmd, 4); - - return GPO[num]; + static int GPO[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; + char cmd[4] = ESC "Pnx"; + + if (num < 0) + num = 0; + if (num > 7) + num = 7; + + /* -1 is used to query the current GPO */ + if (val == -1) + return GPO[num]; + + if (val < 0) + val = 0; + if (val > 255) + val = 255; + GPO[num] = val; + + cmd[2] = (char) num; + cmd[3] = (char) val; + + drv_generic_serial_write(cmd, 4); + + return GPO[num]; } -static int drv_BuE_CT_gpi (int num) +static int drv_BuE_CT_gpi(int num) { - char cmd[4] = ESC "?Pn"; - char buffer[4]; - - if (num < 0) num = 0; - if (num > 7) num = 7; - - cmd[3] = (char)num; - drv_generic_serial_write (cmd, 4); - - usleep (10000); - - if (drv_generic_serial_read (buffer, 4) != 4) { - error ("%s: error reading port %d", Name, num); - return -1; - } - - return buffer[3]; + char cmd[4] = ESC "?Pn"; + char buffer[4]; + + if (num < 0) + num = 0; + if (num > 7) + num = 7; + + cmd[3] = (char) num; + drv_generic_serial_write(cmd, 4); + + usleep(10000); + + if (drv_generic_serial_read(buffer, 4) != 4) { + error("%s: error reading port %d", Name, num); + return -1; + } + + return buffer[3]; } -static int drv_BuE_CT_adc (void) +static int drv_BuE_CT_adc(void) { - char buffer[4]; - - drv_generic_serial_write (ESC "?A", 3); - - usleep (10000); - - if ((drv_generic_serial_read (buffer, 4) != 4) || - (buffer[0] != 'A') || - (buffer[1] != ':') - ) { - error ("%s: error reading ADC", Name); - return -1; - } - - /* 10 bit value: 8 bit high, 2 bit low */ - return 4 * (unsigned char) buffer[2] + (unsigned char) buffer[3]; + char buffer[4]; + + drv_generic_serial_write(ESC "?A", 3); + + usleep(10000); + + if ((drv_generic_serial_read(buffer, 4) != 4) || (buffer[0] != 'A') || (buffer[1] != ':') + ) { + error("%s: error reading ADC", Name); + return -1; + } + + /* 10 bit value: 8 bit high, 2 bit low */ + return 4 * (unsigned char) buffer[2] + (unsigned char) buffer[3]; } -static int drv_BuE_CT_pwm (int val) +static int drv_BuE_CT_pwm(int val) { - static int PWM = -1; - char cmd[4] = ESC "Adm"; - - /* -1 is used to query the current PWM */ - if (val == -1) return PWM; + static int PWM = -1; + char cmd[4] = ESC "Adm"; + + /* -1 is used to query the current PWM */ + if (val == -1) + return PWM; - if (val < 0) val = 0; - if (val > 255) val = 255; - PWM = val; + if (val < 0) + val = 0; + if (val > 255) + val = 255; + PWM = val; - cmd[2] = (char) val; - cmd[3] = val == 0 ? 1 : 2; - drv_generic_serial_write (cmd, 4); + cmd[2] = (char) val; + cmd[3] = val == 0 ? 1 : 2; + drv_generic_serial_write(cmd, 4); - return val; + return val; } -static int drv_BuE_MT_start (const char *section) +static int drv_BuE_MT_start(const char *section) { - char cmd[] = ESC "&sX"; - - /* CSTOPB: 2 stop bits */ - if (drv_generic_serial_open(section, Name, CSTOPB) < 0) return -1; - - cmd[4] = Models[Model].type; - drv_generic_serial_write (cmd, 4); /* select display type */ - drv_generic_serial_write (ESC "&D", 3); /* cursor off */ - - return 0; + char cmd[] = ESC "&sX"; + + /* CSTOPB: 2 stop bits */ + if (drv_generic_serial_open(section, Name, CSTOPB) < 0) + return -1; + + cmd[4] = Models[Model].type; + drv_generic_serial_write(cmd, 4); /* select display type */ + drv_generic_serial_write(ESC "&D", 3); /* cursor off */ + + return 0; } -static int drv_BuE_CT_start (const char *section) +static int drv_BuE_CT_start(const char *section) { - char buffer[16]; - char *size; - int i, len; - - if (drv_generic_serial_open(section, Name, 0) < 0) return -1; - + char buffer[16]; + char *size; + int i, len; + + if (drv_generic_serial_open(section, Name, 0) < 0) + return -1; + #if 0 - /* restart terminal */ - drv_generic_serial_write (ESC "Kr", 3); - usleep(10000); + /* restart terminal */ + drv_generic_serial_write(ESC "Kr", 3); + usleep(10000); #endif - - /* Fixme: the CT does not return a serial number in byte mode */ - /* set parameter mode 'decimal' */ - drv_generic_serial_write (ESC "KM\073", 4); - - /* read version */ - drv_generic_serial_write (ESC "?V", 3); - usleep(100000); - if ((len = drv_generic_serial_read (buffer, -sizeof(buffer))) > 0) { - int v, r, s; - if (sscanf(buffer, "V:%d.%d,%d;", &v, &r, &s) != 3) { - error ("%s: error parsing display identification <%*s>", Name, len, buffer); - } else { - info ("%s: display identified as version %d.%d, S/N %d", Name, v, r, s); + + /* Fixme: the CT does not return a serial number in byte mode */ + /* set parameter mode 'decimal' */ + drv_generic_serial_write(ESC "KM\073", 4); + + /* read version */ + drv_generic_serial_write(ESC "?V", 3); + usleep(100000); + if ((len = drv_generic_serial_read(buffer, -sizeof(buffer))) > 0) { + int v, r, s; + if (sscanf(buffer, "V:%d.%d,%d;", &v, &r, &s) != 3) { + error("%s: error parsing display identification <%*s>", Name, len, buffer); + } else { + info("%s: display identified as version %d.%d, S/N %d", Name, v, r, s); + } } - } - - /* set parameter mode 'byte' */ - drv_generic_serial_write (ESC "KM\072", 4); - - /* the CT20x4 can control smaller displays, too */ - size = cfg_get(section, "Size", NULL); - if (size != NULL && *size != '\0') { - int r, c; - char cmd[6] = ESC "LArc"; - if (sscanf(size, "%dx%d", &c, &r) != 2 || r < 1 || c < 1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, size, cfg_source()); - return -1; + + /* set parameter mode 'byte' */ + drv_generic_serial_write(ESC "KM\072", 4); + + /* the CT20x4 can control smaller displays, too */ + size = cfg_get(section, "Size", NULL); + if (size != NULL && *size != '\0') { + int r, c; + char cmd[6] = ESC "LArc"; + if (sscanf(size, "%dx%d", &c, &r) != 2 || r < 1 || c < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, size, cfg_source()); + return -1; + } + info("%s: display size: %d rows %d columns", Name, r, c); + /* set display size */ + cmd[3] = (char) r; + cmd[4] = (char) c; + drv_generic_serial_write(cmd, 5); + DCOLS = c; + DROWS = r; } - info ("%s: display size: %d rows %d columns", Name, r, c); - /* set display size */ - cmd[3] = (char) r; - cmd[4] = (char) c; - drv_generic_serial_write (cmd, 5); - DCOLS = c; - DROWS = r; - } - - /* set contrast */ - if (cfg_number(section, "Contrast", 7, 0, 15, &i) > 0) { - drv_BuE_CT_contrast(i); - } - - /* set backlight */ - if (cfg_number(section, "Backlight", 0, 0, 1, &i) > 0) { - drv_BuE_CT_backlight(i); - } - - - /* identify modules */ - - for (i = 0; i < 8; i++) { - char cmd[5] = ESC "K?Pn"; - cmd[4] = (char)i; - drv_generic_serial_write (cmd, 5); /* query I/O port */ - usleep(10000); - if ((len = drv_generic_serial_read (buffer, 4)) == 4) { - char *type = NULL; - if (i == 0) { - if( buffer[3] == 8) { - /* internal port */ - type = "CT 20x4 internal port"; + + /* set contrast */ + if (cfg_number(section, "Contrast", 7, 0, 15, &i) > 0) { + drv_BuE_CT_contrast(i); + } + + /* set backlight */ + if (cfg_number(section, "Backlight", 0, 0, 1, &i) > 0) { + drv_BuE_CT_backlight(i); + } + + + /* identify modules */ + + for (i = 0; i < 8; i++) { + char cmd[5] = ESC "K?Pn"; + cmd[4] = (char) i; + drv_generic_serial_write(cmd, 5); /* query I/O port */ + usleep(10000); + if ((len = drv_generic_serial_read(buffer, 4)) == 4) { + char *type = NULL; + if (i == 0) { + if (buffer[3] == 8) { + /* internal port */ + type = "CT 20x4 internal port"; + } else { + error("%s: internal error: port 0 type %d should be type 8", Name, buffer[3]); + continue; + } + } else { + switch (buffer[3]) { + case 1: /* Key Module */ + type = "XM-KEY-2x4-LED"; + break; + case 8: /* I/O Module */ + type = "XM-IO8-T"; + break; + case 9: /* I/O Module */ + type = "XM-IO4-R"; + break; + case 15: /* nothing */ + continue; + default: /* unhandled */ + type = NULL; + break; + } + } + if (type != NULL) { + info("%s: Port %d: %s", Name, i, type); + } else { + error("%s: internal error: port % unknown type %d", Name, i, cmd[3]); + } } else { - error ("%s: internal error: port 0 type %d should be type 8", Name, buffer[3]); - continue; + error("%s: error fetching type of port %d", Name, i); } - } else { - switch (buffer[3]) { - case 1: /* Key Module */ - type = "XM-KEY-2x4-LED"; - break; - case 8: /* I/O Module */ - type = "XM-IO8-T"; - break; - case 9: /* I/O Module */ - type = "XM-IO4-R"; - break; - case 15: /* nothing */ - continue; - default: /* unhandled */ - type = NULL; - break; - } - } - if (type != NULL) { - info ("%s: Port %d: %s", Name, i, type); - } else { - error ("%s: internal error: port % unknown type %d", Name, i, cmd[3]); - } - } else { - error ("%s: error fetching type of port %d", Name, i); } - } - return 0; + return 0; } -static int drv_BuE_start (const char *section) +static int drv_BuE_start(const char *section) { - int i, ret; - char *model; - - model = cfg_get(section, "Model", NULL); - if (model == NULL && *model == '\0') { - error ("%s: no '%s.Model' entry from %s", Name, section, cfg_source()); - return -1; - } - - for (i = 0; Models[i].name != NULL; i++) { - if (strcasecmp(Models[i].name, model) == 0) break; - } - - if (Models[i].name == NULL) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; - } - - Model = i; - Protocol = Models[Model].protocol; - - info ("%s: using model '%s'", Name, Models[Model].name); - - /* initialize global variables */ - DROWS = Models[Model].rows; - DCOLS = Models[Model].cols; - - ret = 0; - switch (Protocol) { - case 1: - ret = drv_BuE_MT_start(section); - break; - case 2: - ret = drv_BuE_CT_start(section); - break; - } - if (ret != 0) { - return ret; - } - - drv_BuE_clear(); - - return 0; + int i, ret; + char *model; + + model = cfg_get(section, "Model", NULL); + if (model == NULL && *model == '\0') { + error("%s: no '%s.Model' entry from %s", Name, section, cfg_source()); + return -1; + } + + for (i = 0; Models[i].name != NULL; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; + } + + if (Models[i].name == NULL) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + + Model = i; + Protocol = Models[Model].protocol; + + info("%s: using model '%s'", Name, Models[Model].name); + + /* initialize global variables */ + DROWS = Models[Model].rows; + DCOLS = Models[Model].cols; + + ret = 0; + switch (Protocol) { + case 1: + ret = drv_BuE_MT_start(section); + break; + case 2: + ret = drv_BuE_CT_start(section); + break; + } + if (ret != 0) { + return ret; + } + + drv_BuE_clear(); + + return 0; } @@ -543,101 +563,101 @@ static int drv_BuE_start (const char *section) /*** plugins ***/ /****************************************/ -static void plugin_contrast (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_contrast(RESULT * result, const int argc, RESULT * argv[]) { - double contrast; - - switch (argc) { - case 0: - contrast = drv_BuE_CT_contrast(-1); - SetResult(&result, R_NUMBER, &contrast); - break; - case 1: - contrast = drv_BuE_CT_contrast(R2N(argv[0])); - SetResult(&result, R_NUMBER, &contrast); - break; - default: - error ("%s::contrast(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double contrast; + + switch (argc) { + case 0: + contrast = drv_BuE_CT_contrast(-1); + SetResult(&result, R_NUMBER, &contrast); + break; + case 1: + contrast = drv_BuE_CT_contrast(R2N(argv[0])); + SetResult(&result, R_NUMBER, &contrast); + break; + default: + error("%s::contrast(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } -static void plugin_backlight (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_backlight(RESULT * result, const int argc, RESULT * argv[]) { - double backlight; - - switch (argc) { - case 0: - backlight = drv_BuE_CT_backlight(-1); - SetResult(&result, R_NUMBER, &backlight); - break; - case 1: - backlight = drv_BuE_CT_backlight(R2N(argv[0])); - SetResult(&result, R_NUMBER, &backlight); - break; - default: - error ("%s::backlight(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double backlight; + + switch (argc) { + case 0: + backlight = drv_BuE_CT_backlight(-1); + SetResult(&result, R_NUMBER, &backlight); + break; + case 1: + backlight = drv_BuE_CT_backlight(R2N(argv[0])); + SetResult(&result, R_NUMBER, &backlight); + break; + default: + error("%s::backlight(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } -static void plugin_gpo (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_gpo(RESULT * result, const int argc, RESULT * argv[]) { - double gpo; - - switch (argc) { - case 1: - gpo = drv_BuE_CT_gpo(R2N(argv[0]), -1); - SetResult(&result, R_NUMBER, &gpo); - break; - case 2: - gpo = drv_BuE_CT_gpo(R2N(argv[0]), R2N(argv[1])); - SetResult(&result, R_NUMBER, &gpo); - break; - default: - error ("%s::gpo(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double gpo; + + switch (argc) { + case 1: + gpo = drv_BuE_CT_gpo(R2N(argv[0]), -1); + SetResult(&result, R_NUMBER, &gpo); + break; + case 2: + gpo = drv_BuE_CT_gpo(R2N(argv[0]), R2N(argv[1])); + SetResult(&result, R_NUMBER, &gpo); + break; + default: + error("%s::gpo(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } -static void plugin_gpi (RESULT *result, RESULT *arg1) +static void plugin_gpi(RESULT * result, RESULT * arg1) { - double gpi; - - gpi = drv_BuE_CT_gpi(R2N(arg1)); - SetResult(&result, R_NUMBER, &gpi); + double gpi; + + gpi = drv_BuE_CT_gpi(R2N(arg1)); + SetResult(&result, R_NUMBER, &gpi); } -static void plugin_adc (RESULT *result) +static void plugin_adc(RESULT * result) { - double adc; - - adc = drv_BuE_CT_adc(); - SetResult(&result, R_NUMBER, &adc); + double adc; + + adc = drv_BuE_CT_adc(); + SetResult(&result, R_NUMBER, &adc); } -static void plugin_pwm (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_pwm(RESULT * result, const int argc, RESULT * argv[]) { - double pwm; - - switch (argc) { - case 0: - pwm = drv_BuE_CT_pwm(-1); - SetResult(&result, R_NUMBER, &pwm); - break; - case 1: - pwm = drv_BuE_CT_pwm(R2N(argv[0])); - SetResult(&result, R_NUMBER, &pwm); - break; - default: - error ("%s::pwm(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double pwm; + + switch (argc) { + case 0: + pwm = drv_BuE_CT_pwm(-1); + SetResult(&result, R_NUMBER, &pwm); + break; + case 1: + pwm = drv_BuE_CT_pwm(R2N(argv[0])); + SetResult(&result, R_NUMBER, &pwm); + break; + default: + error("%s::pwm(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } @@ -656,126 +676,126 @@ static void plugin_pwm (RESULT *result, const int argc, RESULT *argv[]) /* list models */ -int drv_BuE_list (void) +int drv_BuE_list(void) { - int i; - - for (i = 0; Models[i].name != NULL; i++) { - printf ("%s ", Models[i].name); - } - return 0; + int i; + + for (i = 0; Models[i].name != NULL; i++) { + printf("%s ", Models[i].name); + } + return 0; } /* initialize driver & display */ -int drv_BuE_init (const char *section, const int quiet) +int drv_BuE_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* start display */ - if ((ret=drv_BuE_start (section)) != 0) { - return ret; - } - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - - /* real worker functions */ - switch (Protocol) { - case 1: - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = 6; /* number of bytes a goto command requires */ - drv_generic_text_real_write = drv_BuE_MT_write; - drv_generic_text_real_defchar = drv_BuE_MT_defchar; - break; - case 2: - CHAR0 = 128; /* ASCII of first user-defineable char */ - GOTO_COST = 6; /* number of bytes a goto command requires */ - drv_generic_text_real_write = drv_BuE_CT_write; - drv_generic_text_real_defchar = drv_BuE_CT_defchar; - break; - } - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %s", Name, Models[Model].name); - if (drv_generic_text_greet (buffer, "www.bue.com")) { - sleep (3); - drv_BuE_clear(); + WIDGET_CLASS wc; + int ret; + + /* start display */ + if ((ret = drv_BuE_start(section)) != 0) { + return ret; + } + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + + /* real worker functions */ + switch (Protocol) { + case 1: + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 6; /* number of bytes a goto command requires */ + drv_generic_text_real_write = drv_BuE_MT_write; + drv_generic_text_real_defchar = drv_BuE_MT_defchar; + break; + case 2: + CHAR0 = 128; /* ASCII of first user-defineable char */ + GOTO_COST = 6; /* number of bytes a goto command requires */ + drv_generic_text_real_write = drv_BuE_CT_write; + drv_generic_text_real_defchar = drv_BuE_CT_defchar; + break; } - } - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - AddFunction ("LCD::contrast", -1, plugin_contrast); - AddFunction ("LCD::backlight", -1, plugin_backlight); - AddFunction ("LCD::gpo", -1, plugin_gpo); - AddFunction ("LCD::gpi", 1, plugin_gpi); - AddFunction ("LCD::adc", 0, plugin_adc); - AddFunction ("LCD::pwm", -1, plugin_pwm); - - return 0; + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %s", Name, Models[Model].name); + if (drv_generic_text_greet(buffer, "www.bue.com")) { + sleep(3); + drv_BuE_clear(); + } + } + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + AddFunction("LCD::contrast", -1, plugin_contrast); + AddFunction("LCD::backlight", -1, plugin_backlight); + AddFunction("LCD::gpo", -1, plugin_gpo); + AddFunction("LCD::gpi", 1, plugin_gpi); + AddFunction("LCD::adc", 0, plugin_adc); + AddFunction("LCD::pwm", -1, plugin_pwm); + + return 0; } /* close driver & display */ -int drv_BuE_quit (const int quiet) { +int drv_BuE_quit(const int quiet) +{ + + info("%s: shutting down.", Name); - info("%s: shutting down.", Name); + drv_generic_text_quit(); - drv_generic_text_quit(); - - /* clear display */ - drv_BuE_clear(); + /* clear display */ + drv_BuE_clear(); - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - drv_generic_serial_close(); + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } - return (0); + drv_generic_serial_close(); + + return (0); } DRIVER drv_BeckmannEgle = { - name: Name, - list: drv_BuE_list, - init: drv_BuE_init, - quit: drv_BuE_quit, + name:Name, + list:drv_BuE_list, + init:drv_BuE_init, + quit:drv_BuE_quit, }; - diff --git a/drv_Crystalfontz.c b/drv_Crystalfontz.c index 1dc6c71..6ac7fe0 100644 --- a/drv_Crystalfontz.c +++ b/drv_Crystalfontz.c @@ -1,4 +1,4 @@ -/* $Id: drv_Crystalfontz.c,v 1.33 2005/04/02 05:28:58 reinelt Exp $ +/* $Id: drv_Crystalfontz.c,v 1.34 2005/05/08 04:32:44 reinelt Exp $ * * new style driver for Crystalfontz display modules * @@ -23,6 +23,9 @@ * * * $Log: drv_Crystalfontz.c,v $ + * Revision 1.34 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.33 2005/04/02 05:28:58 reinelt * fixed gcc4 warnings about signed/unsigned mismatches * @@ -185,7 +188,7 @@ #include "drv_generic_serial.h" -static char Name[]="Crystalfontz"; +static char Name[] = "Crystalfontz"; static int Model; static int Protocol; @@ -193,24 +196,24 @@ static int Payload; /* ring buffer for bytes received from the display */ static unsigned char RingBuffer[256]; -static unsigned int RingRPos = 0; -static unsigned int RingWPos = 0; +static unsigned int RingRPos = 0; +static unsigned int RingWPos = 0; /* packet from the display */ struct { - unsigned char type; - unsigned char size; - unsigned char data[16+1]; /* trailing '\0' */ + unsigned char type; + unsigned char size; + unsigned char data[16 + 1]; /* trailing '\0' */ } Packet; /* Line Buffer for 633 displays */ -static unsigned char Line[2*16]; +static unsigned char Line[2 * 16]; /* Fan RPM */ -static double Fan_RPM[4] = {0.0,}; +static double Fan_RPM[4] = { 0.0, }; /* Temperature sensors */ -static double Temperature[32] = {0.0,}; +static double Temperature[32] = { 0.0, }; /* Fixme: GPO's not yet implemented */ @@ -219,26 +222,26 @@ static int GPOS; typedef struct { - int type; - char *name; - int rows; - int cols; - int gpos; - int protocol; - int payload; + int type; + char *name; + int rows; + int cols; + int gpos; + int protocol; + int payload; } MODEL; /* Fixme #1: number of gpo's should be verified */ /* Fixme #2: protocol should be verified */ static MODEL Models[] = { - { 626, "626", 2, 16, 0, 1, 0 }, - { 631, "631", 2, 20, 0, 3, 22 }, - { 632, "632", 2, 16, 0, 1, 0 }, - { 633, "633", 2, 16, 0, 2, 18 }, - { 634, "634", 4, 20, 0, 1, 0 }, - { 636, "636", 2, 16, 0, 1, 0 }, - { -1, "Unknown", -1, -1, 0, 0, 0 } + {626, "626", 2, 16, 0, 1, 0}, + {631, "631", 2, 20, 0, 3, 22}, + {632, "632", 2, 16, 0, 1, 0}, + {633, "633", 2, 16, 0, 2, 18}, + {634, "634", 4, 20, 0, 1, 0}, + {636, "636", 2, 16, 0, 1, 0}, + {-1, "Unknown", -1, -1, 0, 0, 0} }; @@ -247,632 +250,660 @@ static MODEL Models[] = { /****************************************/ /* x^0 + x^5 + x^12 */ -#define CRCPOLY 0x8408 +#define CRCPOLY 0x8408 -static unsigned short CRC (const unsigned char *p, size_t len, unsigned short seed) +static unsigned short CRC(const unsigned char *p, size_t len, unsigned short seed) { - int i; - while (len--) { - seed ^= *p++; - for (i = 0; i < 8; i++) - seed = (seed >> 1) ^ ((seed & 1) ? CRCPOLY : 0); - } - return ~seed; + int i; + while (len--) { + seed ^= *p++; + for (i = 0; i < 8; i++) + seed = (seed >> 1) ^ ((seed & 1) ? CRCPOLY : 0); + } + return ~seed; } -static unsigned char LSB (const unsigned short word) +static unsigned char LSB(const unsigned short word) { - return word & 0xff; + return word & 0xff; } -static unsigned char MSB (const unsigned short word) +static unsigned char MSB(const unsigned short word) { - return word >> 8; + return word >> 8; } -static unsigned char byte (unsigned int pos) +static unsigned char byte(unsigned int pos) { - pos += RingRPos; - if (pos >= sizeof(RingBuffer)) - pos -= sizeof(RingBuffer); - return RingBuffer[pos]; + pos += RingRPos; + if (pos >= sizeof(RingBuffer)) + pos -= sizeof(RingBuffer); + return RingBuffer[pos]; } -static void drv_CF_process_packet (void) +static void drv_CF_process_packet(void) { - switch (Packet.type) { - - case 0x80: /* Key Activity */ - debug ("Key Activity: %d", Packet.data[0]); - break; - - case 0x81: /* Fan Speed Report */ - if (Packet.data[1] == 0xff) { - Fan_RPM[Packet.data[0]] = -1.0; - } else if (Packet.data[1] < 4) { - Fan_RPM[Packet.data[0]] = 0.0; - } else { - Fan_RPM[Packet.data[0]] = (double) 27692308L * (Packet.data[1]-3) / (Packet.data[2]+256*Packet.data[3]); - } - break; + switch (Packet.type) { + + case 0x80: /* Key Activity */ + debug("Key Activity: %d", Packet.data[0]); + break; + + case 0x81: /* Fan Speed Report */ + if (Packet.data[1] == 0xff) { + Fan_RPM[Packet.data[0]] = -1.0; + } else if (Packet.data[1] < 4) { + Fan_RPM[Packet.data[0]] = 0.0; + } else { + Fan_RPM[Packet.data[0]] = (double) 27692308L *(Packet.data[1] - 3) / (Packet.data[2] + 256 * Packet.data[3]); + } + break; + + case 0x82: /* Temperature Sensor Report */ + switch (Packet.data[3]) { + case 0: + error("%s: 1-Wire device #%d: CRC error", Name, Packet.data[0]); + break; + case 1: + case 2: + Temperature[Packet.data[0]] = (Packet.data[1] + 256 * Packet.data[2]) / 16.0; + break; + default: + error("%s: 1-Wire device #%d: unknown CRC status %d", Name, Packet.data[0], Packet.data[3]); + break; + } + break; - case 0x82: /* Temperature Sensor Report */ - switch (Packet.data[3]) { - case 0: - error ("%s: 1-Wire device #%d: CRC error", Name, Packet.data[0]); - break; - case 1: - case 2: - Temperature[Packet.data[0]] = (Packet.data[1] + 256*Packet.data[2])/16.0; - break; default: - error ("%s: 1-Wire device #%d: unknown CRC status %d", Name, Packet.data[0], Packet.data[3]); - break; + /* just ignore packet */ + break; } - break; - default: - /* just ignore packet */ - break; - } - } -static int drv_CF_poll (void) +static int drv_CF_poll(void) { - /* read into RingBuffer */ - while (1) { - char buffer[32]; - int num, n; - num = drv_generic_serial_poll(buffer, sizeof(buffer)); - if (num <= 0) break; - /* put result into RingBuffer */ - for (n = 0; n < num; n++) { - RingBuffer[RingWPos++] = (unsigned char)buffer[n]; - if (RingWPos >= sizeof(RingBuffer)) RingWPos = 0; + /* read into RingBuffer */ + while (1) { + char buffer[32]; + int num, n; + num = drv_generic_serial_poll(buffer, sizeof(buffer)); + if (num <= 0) + break; + /* put result into RingBuffer */ + for (n = 0; n < num; n++) { + RingBuffer[RingWPos++] = (unsigned char) buffer[n]; + if (RingWPos >= sizeof(RingBuffer)) + RingWPos = 0; + } } - } - - /* process RingBuffer */ - while (1) { - unsigned char buffer[32]; - int n, num, size; - unsigned short crc; - /* packet size */ - num = RingWPos - RingRPos; - if (num < 0) num += sizeof(RingBuffer); - /* minimum packet size=4 */ - if (num < 4) return 0; - /* valid response types: 01xxxxx 10.. 11.. */ - /* therefore: 00xxxxxx is invalid */ - if (byte(0) >> 6 == 0) goto GARBAGE; - /* command length */ - size = byte(1); - /* valid command length is 0 to 16 */ - if (size > 16) goto GARBAGE; - /* all bytes available? */ - if (num < size+4) return 0; - /* check CRC */ - for (n = 0; n < size+4; n++) buffer[n] = byte(n); - crc = CRC(buffer, size+2, 0xffff); - if (LSB(crc) != buffer[size+2]) goto GARBAGE; - if (MSB(crc) != buffer[size+3]) goto GARBAGE; - /* process packet */ - Packet.type = buffer[0]; - Packet.size = size; - memcpy(Packet.data, buffer+2, size); - Packet.data[size] = '\0'; /* trailing zero */ - /* increment read pointer */ - RingRPos += size+4; - if (RingRPos >= sizeof(RingBuffer)) RingRPos -= sizeof(RingBuffer); - /* a packet arrived */ - return 1; - GARBAGE: - debug ("dropping garbage byte %02x", byte(0)); - RingRPos++; - if (RingRPos >= sizeof(RingBuffer)) RingRPos = 0; - continue; - } - - /* not reached */ - return 0; + + /* process RingBuffer */ + while (1) { + unsigned char buffer[32]; + int n, num, size; + unsigned short crc; + /* packet size */ + num = RingWPos - RingRPos; + if (num < 0) + num += sizeof(RingBuffer); + /* minimum packet size=4 */ + if (num < 4) + return 0; + /* valid response types: 01xxxxx 10.. 11.. */ + /* therefore: 00xxxxxx is invalid */ + if (byte(0) >> 6 == 0) + goto GARBAGE; + /* command length */ + size = byte(1); + /* valid command length is 0 to 16 */ + if (size > 16) + goto GARBAGE; + /* all bytes available? */ + if (num < size + 4) + return 0; + /* check CRC */ + for (n = 0; n < size + 4; n++) + buffer[n] = byte(n); + crc = CRC(buffer, size + 2, 0xffff); + if (LSB(crc) != buffer[size + 2]) + goto GARBAGE; + if (MSB(crc) != buffer[size + 3]) + goto GARBAGE; + /* process packet */ + Packet.type = buffer[0]; + Packet.size = size; + memcpy(Packet.data, buffer + 2, size); + Packet.data[size] = '\0'; /* trailing zero */ + /* increment read pointer */ + RingRPos += size + 4; + if (RingRPos >= sizeof(RingBuffer)) + RingRPos -= sizeof(RingBuffer); + /* a packet arrived */ + return 1; + GARBAGE: + debug("dropping garbage byte %02x", byte(0)); + RingRPos++; + if (RingRPos >= sizeof(RingBuffer)) + RingRPos = 0; + continue; + } + + /* not reached */ + return 0; } -static void drv_CF_timer (void __attribute__((unused)) *notused) +static void drv_CF_timer(void __attribute__ ((unused)) * notused) { - while (drv_CF_poll()) { - drv_CF_process_packet(); - } + while (drv_CF_poll()) { + drv_CF_process_packet(); + } } -static void drv_CF_send (const int cmd, int len, const unsigned char *data) +static void drv_CF_send(const int cmd, int len, const unsigned char *data) { - unsigned char buffer[22]; - unsigned short crc; - - if (len > Payload) { - error ("%s: internal error: packet length %d exceeds payload size %d", Name, len, Payload); - len = sizeof(buffer)-1; - } - - buffer[0] = cmd; - buffer[1] = len; - memcpy (buffer+2, data, len); - crc = CRC(buffer, len+2, 0xffff); - buffer[len+2] = LSB(crc); - buffer[len+3] = MSB(crc); - + unsigned char buffer[22]; + unsigned short crc; + + if (len > Payload) { + error("%s: internal error: packet length %d exceeds payload size %d", Name, len, Payload); + len = sizeof(buffer) - 1; + } + + buffer[0] = cmd; + buffer[1] = len; + memcpy(buffer + 2, data, len); + crc = CRC(buffer, len + 2, 0xffff); + buffer[len + 2] = LSB(crc); + buffer[len + 3] = MSB(crc); + #if 0 - debug ("Tx Packet %d len=%d", buffer[0], buffer[1]); + debug("Tx Packet %d len=%d", buffer[0], buffer[1]); #endif - - drv_generic_serial_write ((char*)buffer, len+4); - + + drv_generic_serial_write((char *) buffer, len + 4); + } -static void drv_CF_write1 (const int row, const int col, const char *data, const int len) +static void drv_CF_write1(const int row, const int col, const char *data, const int len) { - char cmd[3]="\021xy"; /* set cursor position */ - - if (row==0 && col==0) { - drv_generic_serial_write ("\001", 1); /* cursor home */ - } else { - cmd[1]=(char)col; - cmd[2]=(char)row; - drv_generic_serial_write (cmd, 3); - } - - drv_generic_serial_write (data, len); + char cmd[3] = "\021xy"; /* set cursor position */ + + if (row == 0 && col == 0) { + drv_generic_serial_write("\001", 1); /* cursor home */ + } else { + cmd[1] = (char) col; + cmd[2] = (char) row; + drv_generic_serial_write(cmd, 3); + } + + drv_generic_serial_write(data, len); } -static void drv_CF_write2 (const int row, const int col, const char *data, const int len) +static void drv_CF_write2(const int row, const int col, const char *data, const int len) { - int l = len; - - /* limit length */ - if (col + l > 16) l = 16 - col; - if (l < 0) l = 0; - - /* sanity check */ - if (row >= 2 || col + l > 16) { - error ("%s: internal error: write outside linebuffer bounds!", Name); - return; - } - memcpy (Line + 16 * row + col, data, l); - drv_CF_send (7 + row, 16, (unsigned char *)(Line + 16 * row)); + int l = len; + + /* limit length */ + if (col + l > 16) + l = 16 - col; + if (l < 0) + l = 0; + + /* sanity check */ + if (row >= 2 || col + l > 16) { + error("%s: internal error: write outside linebuffer bounds!", Name); + return; + } + memcpy(Line + 16 * row + col, data, l); + drv_CF_send(7 + row, 16, (unsigned char *) (Line + 16 * row)); } -static void drv_CF_write3 (const int row, const int col, const char *data, const int len) +static void drv_CF_write3(const int row, const int col, const char *data, const int len) { - int l = len; - unsigned char cmd[23]; - - /* limit length */ - if (col + l > 20) l = 20 - col; - if (l < 0) l = 0; + int l = len; + unsigned char cmd[23]; + + /* limit length */ + if (col + l > 20) + l = 20 - col; + if (l < 0) + l = 0; + + /* sanity check */ + if (row >= 2 || col + l > 20) { + error("%s: internal error: write outside display bounds!", Name); + return; + } - /* sanity check */ - if (row >= 2 || col + l > 20) { - error ("%s: internal error: write outside display bounds!", Name); - return; - } + cmd[0] = col; + cmd[1] = row; + memcpy(cmd + 2, data, l); - cmd[0] = col; - cmd[1] = row; - memcpy (cmd+2, data, l); + drv_CF_send(31, l + 2, cmd); - drv_CF_send (31, l+2, cmd); - } -static void drv_CF_defchar1 (const int ascii, const unsigned char *matrix) +static void drv_CF_defchar1(const int ascii, const unsigned char *matrix) { - int i; - char cmd[10]="\031n"; /* set custom char bitmap */ - - /* user-defineable chars start at 128, but are defined at 0 */ - cmd[1]=(char)(ascii-CHAR0); - for (i = 0; i < 8; i++) { - cmd[i+2] = matrix[i] & 0x3f; - } - drv_generic_serial_write (cmd, 10); + int i; + char cmd[10] = "\031n"; /* set custom char bitmap */ + + /* user-defineable chars start at 128, but are defined at 0 */ + cmd[1] = (char) (ascii - CHAR0); + for (i = 0; i < 8; i++) { + cmd[i + 2] = matrix[i] & 0x3f; + } + drv_generic_serial_write(cmd, 10); } -static void drv_CF_defchar23 (const int ascii, const unsigned char *matrix) +static void drv_CF_defchar23(const int ascii, const unsigned char *matrix) { - int i; - unsigned char buffer[9]; - - /* user-defineable chars start at 128, but are defined at 0 */ - buffer[0] = (char)(ascii-CHAR0); - - /* clear bit 6 and 7 of the bitmap (blinking) */ - for (i = 0; i < 8; i++) { - buffer[i+1] = matrix[i] & 0x3f; - } - - drv_CF_send (9, 9, buffer); + int i; + unsigned char buffer[9]; + + /* user-defineable chars start at 128, but are defined at 0 */ + buffer[0] = (char) (ascii - CHAR0); + + /* clear bit 6 and 7 of the bitmap (blinking) */ + for (i = 0; i < 8; i++) { + buffer[i + 1] = matrix[i] & 0x3f; + } + + drv_CF_send(9, 9, buffer); } -static int drv_CF_contrast (int contrast) +static int drv_CF_contrast(int contrast) { - static unsigned char Contrast=0; - char buffer[2]; - - /* -1 is used to query the current contrast */ - if (contrast == -1) return Contrast; - - if (contrast < 0 ) contrast = 0; - if (contrast > 255) contrast = 255; - Contrast=contrast; - - switch (Protocol) { - - case 1: - /* contrast range 0 to 100 */ - if (Contrast > 100) Contrast = 100; - buffer[0] = 15; /* Set LCD Contrast */ - buffer[1] = Contrast; - drv_generic_serial_write (buffer, 2); - break; - - case 2: - /* contrast range 0 to 50 */ - if (Contrast > 50) Contrast = 50; - drv_CF_send (13, 1, &Contrast); - break; - - case 3: - /* contrast range 0 to 255 */ - drv_CF_send (13, 1, &Contrast); - break; - } - - return Contrast; + static unsigned char Contrast = 0; + char buffer[2]; + + /* -1 is used to query the current contrast */ + if (contrast == -1) + return Contrast; + + if (contrast < 0) + contrast = 0; + if (contrast > 255) + contrast = 255; + Contrast = contrast; + + switch (Protocol) { + + case 1: + /* contrast range 0 to 100 */ + if (Contrast > 100) + Contrast = 100; + buffer[0] = 15; /* Set LCD Contrast */ + buffer[1] = Contrast; + drv_generic_serial_write(buffer, 2); + break; + + case 2: + /* contrast range 0 to 50 */ + if (Contrast > 50) + Contrast = 50; + drv_CF_send(13, 1, &Contrast); + break; + + case 3: + /* contrast range 0 to 255 */ + drv_CF_send(13, 1, &Contrast); + break; + } + + return Contrast; } -static int drv_CF_backlight (int backlight) +static int drv_CF_backlight(int backlight) { - static unsigned char Backlight=0; - char buffer[2]; - - /* -1 is used to query the current backlight */ - if (backlight == -1) return Backlight; - - if (backlight<0 ) backlight=0; - if (backlight>100) backlight=100; - Backlight=backlight; - - switch (Protocol) { - - case 1: - buffer[0] = 14; /* Set LCD Backlight */ - buffer[1] = Backlight; - drv_generic_serial_write (buffer, 2); - break; - - case 2: - case 3: - drv_CF_send (14, 1, &Backlight); - break; - } - - return Backlight; + static unsigned char Backlight = 0; + char buffer[2]; + + /* -1 is used to query the current backlight */ + if (backlight == -1) + return Backlight; + + if (backlight < 0) + backlight = 0; + if (backlight > 100) + backlight = 100; + Backlight = backlight; + + switch (Protocol) { + + case 1: + buffer[0] = 14; /* Set LCD Backlight */ + buffer[1] = Backlight; + drv_generic_serial_write(buffer, 2); + break; + + case 2: + case 3: + drv_CF_send(14, 1, &Backlight); + break; + } + + return Backlight; } -static int drv_CF_fan_pwm (int fan, int power) +static int drv_CF_fan_pwm(int fan, int power) { - static unsigned char PWM[4] = {100,}; - - /* sanity check */ - if (fan<1 || fan>4) return -1; - - /* fan ranges from 1 to 4 */ - fan--; - - /* -1 is used to query the current power */ - if (power == -1) return PWM[fan]; - - if (power<0 ) power=0; - if (power>100) power=100; - PWM[fan]=power; - - switch (Protocol) { - case 2: - drv_CF_send (17, 4, PWM); - break; - } - - return PWM[fan]; + static unsigned char PWM[4] = { 100, }; + + /* sanity check */ + if (fan < 1 || fan > 4) + return -1; + + /* fan ranges from 1 to 4 */ + fan--; + + /* -1 is used to query the current power */ + if (power == -1) + return PWM[fan]; + + if (power < 0) + power = 0; + if (power > 100) + power = 100; + PWM[fan] = power; + + switch (Protocol) { + case 2: + drv_CF_send(17, 4, PWM); + break; + } + + return PWM[fan]; } -static int drv_CF_autodetect (void) +static int drv_CF_autodetect(void) { - int i, m; - - /* only autodetect newer displays */ - if (Protocol<2) return -1; - - /* read display type */ - drv_CF_send (1, 0, NULL); - - i=0; - while (1) { - /* wait 10 msec */ - usleep(10*1000); - /* packet available? */ - if (drv_CF_poll()) { - /* display type */ - if (Packet.type==0x41) { - char t[7], c; float h, v; - info ("%s: display identifies itself as '%s'", Name, Packet.data); - if (sscanf((char*)Packet.data, "%6s:h%f,%c%f", t, &h, &c, &v)!=4) { - error ("%s: error parsing display identification string", Name); - return -1; + int i, m; + + /* only autodetect newer displays */ + if (Protocol < 2) + return -1; + + /* read display type */ + drv_CF_send(1, 0, NULL); + + i = 0; + while (1) { + /* wait 10 msec */ + usleep(10 * 1000); + /* packet available? */ + if (drv_CF_poll()) { + /* display type */ + if (Packet.type == 0x41) { + char t[7], c; + float h, v; + info("%s: display identifies itself as '%s'", Name, Packet.data); + if (sscanf((char *) Packet.data, "%6s:h%f,%c%f", t, &h, &c, &v) != 4) { + error("%s: error parsing display identification string", Name); + return -1; + } + info("%s: display type '%s', hardware version %3.1f, firmware version %c%3.1f", Name, t, h, c, v); + if (strncmp(t, "CFA", 3) == 0) { + for (m = 0; Models[m].type != -1; m++) { + /* omit the 'CFA' */ + if (strcasecmp(Models[m].name, t + 3) == 0) + return m; + } + } + error("%s: display type '%s' may be not supported!", Name, t); + return -1; + } + drv_CF_process_packet(); } - info ("%s: display type '%s', hardware version %3.1f, firmware version %c%3.1f", Name, t, h, c, v); - if (strncmp(t, "CFA", 3)==0) { - for (m=0; Models[m].type!=-1; m++) { - /* omit the 'CFA' */ - if (strcasecmp(Models[m].name, t+3)==0) - return m; - } + /* wait no longer than 300 msec */ + if (++i > 30) { + error("%s: display detection timed out", Name); + return -1; } - error ("%s: display type '%s' may be not supported!", Name, t); - return -1; - } - drv_CF_process_packet(); } - /* wait no longer than 300 msec */ - if (++i > 30) { - error ("%s: display detection timed out", Name); - return -1; - } - } - - /* not reached */ - return -1; + + /* not reached */ + return -1; } -static char* drv_CF_print_ROM (void) +static char *drv_CF_print_ROM(void) { - static char buffer[17]; + static char buffer[17]; - snprintf(buffer, sizeof(buffer), "0x%02x%02x%02x%02x%02x%02x%02x%02x", - Packet.data[1], Packet.data[2], Packet.data[3], Packet.data[4], - Packet.data[5], Packet.data[6], Packet.data[7], Packet.data[8]); + snprintf(buffer, sizeof(buffer), "0x%02x%02x%02x%02x%02x%02x%02x%02x", + Packet.data[1], Packet.data[2], Packet.data[3], Packet.data[4], Packet.data[5], Packet.data[6], Packet.data[7], Packet.data[8]); - return buffer; + return buffer; } -static int drv_CF_scan_DOW (unsigned char index) +static int drv_CF_scan_DOW(unsigned char index) { - int i; - - /* Read DOW Device Information */ - drv_CF_send (18, 1, &index); - - i=0; - while (1) { - /* wait 10 msec */ - usleep(10*1000); - /* packet available? */ - if (drv_CF_poll()) { - /* DOW Device Info */ - if (Packet.type==0x52) { - switch (Packet.data[1]) { - case 0x00: - /* no device found */ - return 0; - case 0x22: - info ("%s: 1-Wire device #%d: DS1822 temperature sensor found at %s", - Name, Packet.data[0], drv_CF_print_ROM()); - return 1; - case 0x28: - info ("%s: 1-Wire device #%d: DS18B20 temperature sensor found at %s", - Name, Packet.data[0], drv_CF_print_ROM()); - return 1; - default: - info ("%s: 1-Wire device #%d: unknown device found at %s", - Name, Packet.data[0], drv_CF_print_ROM()); - return 0; + int i; + + /* Read DOW Device Information */ + drv_CF_send(18, 1, &index); + + i = 0; + while (1) { + /* wait 10 msec */ + usleep(10 * 1000); + /* packet available? */ + if (drv_CF_poll()) { + /* DOW Device Info */ + if (Packet.type == 0x52) { + switch (Packet.data[1]) { + case 0x00: + /* no device found */ + return 0; + case 0x22: + info("%s: 1-Wire device #%d: DS1822 temperature sensor found at %s", Name, Packet.data[0], drv_CF_print_ROM()); + return 1; + case 0x28: + info("%s: 1-Wire device #%d: DS18B20 temperature sensor found at %s", Name, Packet.data[0], drv_CF_print_ROM()); + return 1; + default: + info("%s: 1-Wire device #%d: unknown device found at %s", Name, Packet.data[0], drv_CF_print_ROM()); + return 0; + } + } else { + drv_CF_process_packet(); + } + } + /* wait no longer than 300 msec */ + if (++i > 30) { + error("%s: 1-Wire device #%d detection timed out", Name, index); + return -1; } - } else { - drv_CF_process_packet(); - } - } - /* wait no longer than 300 msec */ - if (++i > 30) { - error ("%s: 1-Wire device #%d detection timed out", Name, index); - return -1; } - } - - /* not reached */ - return -1; + + /* not reached */ + return -1; } /* clear display */ -static void drv_CF_clear (void) +static void drv_CF_clear(void) { - switch (Protocol) { - case 1: - drv_generic_serial_write ("\014", 1); - break; - case 2: - case 3: - drv_CF_send ( 6, 0, NULL); - break; - } + switch (Protocol) { + case 1: + drv_generic_serial_write("\014", 1); + break; + case 2: + case 3: + drv_CF_send(6, 0, NULL); + break; + } } /* init sequences for 626, 632, 634, 636 */ -static void drv_CF_start_1 (void) +static void drv_CF_start_1(void) { - drv_generic_serial_write ("\014", 1); /* Form Feed (Clear Display) */ - drv_generic_serial_write ("\004", 1); /* hide cursor */ - drv_generic_serial_write ("\024", 1); /* scroll off */ - drv_generic_serial_write ("\030", 1); /* wrap off */ + drv_generic_serial_write("\014", 1); /* Form Feed (Clear Display) */ + drv_generic_serial_write("\004", 1); /* hide cursor */ + drv_generic_serial_write("\024", 1); /* scroll off */ + drv_generic_serial_write("\030", 1); /* wrap off */ } /* init sequences for 633 */ -static void drv_CF_start_2 (void) +static void drv_CF_start_2(void) { - int i; - unsigned long mask; - unsigned char buffer[4]; - - /* Clear Display */ - drv_CF_send ( 6, 0, NULL); - - /* Set LCD Cursor Style */ - buffer[0] = 0; - drv_CF_send (12, 1, buffer); - - /* enable Fan Reporting */ - buffer[0] = 15; - drv_CF_send (16, 1, buffer); - - /* Set Fan Power to 100% */ - buffer[0] = buffer[1] = buffer[2] = buffer[3] = 100; - drv_CF_send (17, 4, buffer); - - /* Read DOW Device Information */ - mask = 0; - for (i = 0; i < 32; i++) { - if (drv_CF_scan_DOW(i) == 1) { - mask |= 1<<i; + int i; + unsigned long mask; + unsigned char buffer[4]; + + /* Clear Display */ + drv_CF_send(6, 0, NULL); + + /* Set LCD Cursor Style */ + buffer[0] = 0; + drv_CF_send(12, 1, buffer); + + /* enable Fan Reporting */ + buffer[0] = 15; + drv_CF_send(16, 1, buffer); + + /* Set Fan Power to 100% */ + buffer[0] = buffer[1] = buffer[2] = buffer[3] = 100; + drv_CF_send(17, 4, buffer); + + /* Read DOW Device Information */ + mask = 0; + for (i = 0; i < 32; i++) { + if (drv_CF_scan_DOW(i) == 1) { + mask |= 1 << i; + } } - } - - /* enable Temperature Reporting */ - buffer[0] = mask & 0xff; - buffer[1] = (mask>>8) & 0xff; - buffer[2] = (mask>>16) & 0xff; - buffer[3] = (mask>>24) & 0xff; - drv_CF_send (19, 4, buffer); + + /* enable Temperature Reporting */ + buffer[0] = mask & 0xff; + buffer[1] = (mask >> 8) & 0xff; + buffer[2] = (mask >> 16) & 0xff; + buffer[3] = (mask >> 24) & 0xff; + drv_CF_send(19, 4, buffer); } /* init sequences for 631 */ -static void drv_CF_start_3 (void) +static void drv_CF_start_3(void) { - unsigned char buffer[1]; + unsigned char buffer[1]; - /* Clear Display */ - drv_CF_send ( 6, 0, NULL); + /* Clear Display */ + drv_CF_send(6, 0, NULL); - /* Set LCD Cursor Style */ - buffer[0] = 0; - drv_CF_send (12, 1, buffer); + /* Set LCD Cursor Style */ + buffer[0] = 0; + drv_CF_send(12, 1, buffer); } -static int drv_CF_start (const char *section) +static int drv_CF_start(const char *section) { - int i; - char *model; - - model=cfg_get(section, "Model", NULL); - if (model!=NULL && *model!='\0') { - for (i=0; Models[i].type!=-1; i++) { - if (strcasecmp(Models[i].name, model)==0) break; + int i; + char *model; + + model = cfg_get(section, "Model", NULL); + if (model != NULL && *model != '\0') { + for (i = 0; Models[i].type != -1; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; + } + if (Models[i].type == -1) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + Model = i; + Protocol = Models[Model].protocol; + info("%s: using model '%s'", Name, Models[Model].name); + } else { + Model = -1; + Protocol = 2; /*auto-detect only newer displays */ + info("%s: no '%s.Model' entry from %s, auto-detecting", Name, section, cfg_source()); + } + + /* open serial port */ + if (drv_generic_serial_open(section, Name, 0) < 0) + return -1; + + /* Fixme: why such a large delay? */ + usleep(350 * 1000); + + /* display autodetection */ + i = drv_CF_autodetect(); + if (Model == -1) + Model = i; + if (Model == -1) { + error("%s: auto-detection failed, please specify a '%s.Model' entry in %s", Name, section, cfg_source()); + return -1; } - if (Models[i].type==-1) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; + if (i != -1 && Model != i) { + error("%s: %s.Model '%s' from %s does not match detected model '%s'", Name, section, model, cfg_source(), Models[i].name); + return -1; } - Model = i; + + /* initialize global variables */ + DROWS = Models[Model].rows; + DCOLS = Models[Model].cols; + GPOS = Models[Model].gpos; Protocol = Models[Model].protocol; - info ("%s: using model '%s'", Name, Models[Model].name); - } else { - Model = -1; - Protocol = 2; /*auto-detect only newer displays */ - info ("%s: no '%s.Model' entry from %s, auto-detecting", Name, section, cfg_source()); - } - - /* open serial port */ - if (drv_generic_serial_open(section, Name, 0)<0) return -1; - - /* Fixme: why such a large delay? */ - usleep(350*1000); - - /* display autodetection */ - i=drv_CF_autodetect(); - if (Model==-1) Model=i; - if (Model==-1) { - error ("%s: auto-detection failed, please specify a '%s.Model' entry in %s", Name, section, cfg_source()); - return -1; - } - if (i!=-1 && Model!=i) { - error ("%s: %s.Model '%s' from %s does not match detected model '%s'", - Name, section, model, cfg_source(), Models[i].name); - return -1; - } - - /* initialize global variables */ - DROWS = Models[Model].rows; - DCOLS = Models[Model].cols; - GPOS = Models[Model].gpos; - Protocol = Models[Model].protocol; - Payload = Models[Model].payload; - - - switch (Protocol) { - - case 1: - drv_CF_start_1(); - break; - - case 2: - /* regularly process display answers */ - /* Fixme: make 100msec configurable */ - timer_add (drv_CF_timer, NULL, 100, 0); - drv_CF_start_2(); - /* clear 633 linebuffer */ - memset (Line, ' ', sizeof(Line)); - break; - - case 3: - /* regularly process display answers */ - /* Fixme: make 100msec configurable */ - timer_add (drv_CF_timer, NULL, 100, 0); - drv_CF_start_3(); - break; - } - - /* set contrast */ - if (cfg_number(section, "Contrast", 0, 0, 255, &i)>0) { - drv_CF_contrast(i); - } - - /* set backlight */ - if (cfg_number(section, "Backlight", 0, 0, 100, &i)>0) { - drv_CF_backlight(i); - } - - return 0; + Payload = Models[Model].payload; + + + switch (Protocol) { + + case 1: + drv_CF_start_1(); + break; + + case 2: + /* regularly process display answers */ + /* Fixme: make 100msec configurable */ + timer_add(drv_CF_timer, NULL, 100, 0); + drv_CF_start_2(); + /* clear 633 linebuffer */ + memset(Line, ' ', sizeof(Line)); + break; + + case 3: + /* regularly process display answers */ + /* Fixme: make 100msec configurable */ + timer_add(drv_CF_timer, NULL, 100, 0); + drv_CF_start_3(); + break; + } + + /* set contrast */ + if (cfg_number(section, "Contrast", 0, 0, 255, &i) > 0) { + drv_CF_contrast(i); + } + + /* set backlight */ + if (cfg_number(section, "Backlight", 0, 0, 100, &i) > 0) { + drv_CF_backlight(i); + } + + return 0; } @@ -881,63 +912,63 @@ static int drv_CF_start (const char *section) /****************************************/ -static void plugin_contrast (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_contrast(RESULT * result, const int argc, RESULT * argv[]) { - double contrast; - - switch (argc) { - case 0: - contrast = drv_CF_contrast(-1); - SetResult(&result, R_NUMBER, &contrast); - break; - case 1: - contrast = drv_CF_contrast(R2N(argv[0])); - SetResult(&result, R_NUMBER, &contrast); - break; - default: - error ("%s.contrast(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double contrast; + + switch (argc) { + case 0: + contrast = drv_CF_contrast(-1); + SetResult(&result, R_NUMBER, &contrast); + break; + case 1: + contrast = drv_CF_contrast(R2N(argv[0])); + SetResult(&result, R_NUMBER, &contrast); + break; + default: + error("%s.contrast(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } -static void plugin_backlight (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_backlight(RESULT * result, const int argc, RESULT * argv[]) { - double backlight; - - switch (argc) { - case 0: - backlight = drv_CF_backlight(-1); - SetResult(&result, R_NUMBER, &backlight); - break; - case 1: - backlight = drv_CF_backlight(R2N(argv[0])); - SetResult(&result, R_NUMBER, &backlight); - break; - default: - error ("%s.backlight(): wrong number of parameters"); - SetResult(&result, R_STRING, ""); - } + double backlight; + + switch (argc) { + case 0: + backlight = drv_CF_backlight(-1); + SetResult(&result, R_NUMBER, &backlight); + break; + case 1: + backlight = drv_CF_backlight(R2N(argv[0])); + SetResult(&result, R_NUMBER, &backlight); + break; + default: + error("%s.backlight(): wrong number of parameters"); + SetResult(&result, R_STRING, ""); + } } -static void plugin_fan_pwm (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_fan_pwm(RESULT * result, const int argc, RESULT * argv[]) { - double pwm; - - switch (argc) { - case 1: - pwm = drv_CF_fan_pwm(R2N(argv[0]), -1); - SetResult(&result, R_NUMBER, &pwm); - break; - case 2: - pwm = drv_CF_fan_pwm(R2N(argv[0]), R2N(argv[1])); - SetResult(&result, R_NUMBER, &pwm); - break; - default: - error ("%s.pwm(): wrong number of parameters"); - SetResult(&result, R_STRING, ""); - } + double pwm; + + switch (argc) { + case 1: + pwm = drv_CF_fan_pwm(R2N(argv[0]), -1); + SetResult(&result, R_NUMBER, &pwm); + break; + case 2: + pwm = drv_CF_fan_pwm(R2N(argv[0]), R2N(argv[1])); + SetResult(&result, R_NUMBER, &pwm); + break; + default: + error("%s.pwm(): wrong number of parameters"); + SetResult(&result, R_STRING, ""); + } } /* Fixme: other plugins for Fans, Temperature sensors, ... */ @@ -959,132 +990,132 @@ static void plugin_fan_pwm (RESULT *result, const int argc, RESULT *argv[]) /* list models */ -int drv_CF_list (void) +int drv_CF_list(void) { - int i; - - for (i=0; Models[i].type!=-1; i++) { - printf ("%s ", Models[i].name); - } - return 0; + int i; + + for (i = 0; Models[i].type != -1; i++) { + printf("%s ", Models[i].name); + } + return 0; } /* initialize driver & display */ -int drv_CF_init (const char *section, const int quiet) +int drv_CF_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* start display */ - if ((ret = drv_CF_start (section)) != 0) { - return ret; - } - - /* display preferences */ - XRES = 6; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - - /* real worker functions */ - switch (Protocol) { - case 1: - CHAR0 = 128; /* ASCII of first user-defineable char */ - GOTO_COST = 3; /* number of bytes a goto command requires */ - drv_generic_text_real_write = drv_CF_write1; - drv_generic_text_real_defchar = drv_CF_defchar1; - break; - case 2: - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = -1; /* there is no goto on 633 */ - drv_generic_text_real_write = drv_CF_write2; - drv_generic_text_real_defchar = drv_CF_defchar23; - break; - case 3: - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = 3; /* number of bytes a goto command requires */ - drv_generic_text_real_write = drv_CF_write3; - drv_generic_text_real_defchar = drv_CF_defchar23; - break; - } - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %s", Name, Models[Model].name); - if (drv_generic_text_greet (buffer, "www.crystalfontz.com")) { - sleep (3); - drv_CF_clear(); + WIDGET_CLASS wc; + int ret; + + /* start display */ + if ((ret = drv_CF_start(section)) != 0) { + return ret; + } + + /* display preferences */ + XRES = 6; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + + /* real worker functions */ + switch (Protocol) { + case 1: + CHAR0 = 128; /* ASCII of first user-defineable char */ + GOTO_COST = 3; /* number of bytes a goto command requires */ + drv_generic_text_real_write = drv_CF_write1; + drv_generic_text_real_defchar = drv_CF_defchar1; + break; + case 2: + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = -1; /* there is no goto on 633 */ + drv_generic_text_real_write = drv_CF_write2; + drv_generic_text_real_defchar = drv_CF_defchar23; + break; + case 3: + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 3; /* number of bytes a goto command requires */ + drv_generic_text_real_write = drv_CF_write3; + drv_generic_text_real_defchar = drv_CF_defchar23; + break; + } + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %s", Name, Models[Model].name); + if (drv_generic_text_greet(buffer, "www.crystalfontz.com")) { + sleep(3); + drv_CF_clear(); + } } - } - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - drv_generic_text_bar_add_segment (0, 0, 255, 32); /* ASCII 32 = blank */ - if (Protocol==2) - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - AddFunction ("LCD::contrast", -1, plugin_contrast); - AddFunction ("LCD::backlight", -1, plugin_backlight); - if (Protocol == 2) { - AddFunction ("LCD::fan_pwm", -1, plugin_fan_pwm); - } - - return 0; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + if (Protocol == 2) + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + AddFunction("LCD::contrast", -1, plugin_contrast); + AddFunction("LCD::backlight", -1, plugin_backlight); + if (Protocol == 2) { + AddFunction("LCD::fan_pwm", -1, plugin_fan_pwm); + } + + return 0; } /* close driver & display */ -int drv_CF_quit (const int quiet) { +int drv_CF_quit(const int quiet) +{ - info("%s: shutting down.", Name); + info("%s: shutting down.", Name); - drv_generic_text_quit(); + drv_generic_text_quit(); - /* clear display */ - drv_CF_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } + /* clear display */ + drv_CF_clear(); - drv_generic_serial_close(); - - return (0); + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + drv_generic_serial_close(); + + return (0); } DRIVER drv_Crystalfontz = { - name: Name, - list: drv_CF_list, - init: drv_CF_init, - quit: drv_CF_quit, + name:Name, + list:drv_CF_list, + init:drv_CF_init, + quit:drv_CF_quit, }; - diff --git a/drv_Curses.c b/drv_Curses.c index d70b750..31d018b 100644 --- a/drv_Curses.c +++ b/drv_Curses.c @@ -1,4 +1,4 @@ -/* $Id: drv_Curses.c,v 1.9 2005/01/18 06:30:22 reinelt Exp $ +/* $Id: drv_Curses.c,v 1.10 2005/05/08 04:32:44 reinelt Exp $ * * pure ncurses based text driver * @@ -26,6 +26,9 @@ * * * $Log: drv_Curses.c,v $ + * Revision 1.10 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:22 reinelt * added (C) to all copyright statements * @@ -93,7 +96,7 @@ #include "drv_generic_text.h" -static char Name[]="Curses"; +static char Name[] = "Curses"; static WINDOW *w = NULL; static WINDOW *e = NULL; @@ -105,133 +108,138 @@ static int EROWS; /*** hardware dependant functions ***/ /****************************************/ -static void drv_Curs_clear (void) +static void drv_Curs_clear(void) { - werase (w); - box (w, 0, 0); - wrefresh (w); + werase(w); + box(w, 0, 0); + wrefresh(w); } -static void drv_Curs_write (const int row, const int col, const char *data, const int len) +static void drv_Curs_write(const int row, const int col, const char *data, const int len) { - int l = len; - char *p; - - while ((p = strpbrk(data, "\r\n")) != NULL) { - *p = '\0'; - } - - if (col < DCOLS) { - if (DCOLS-col < l ) l = DCOLS-col; - mvwprintw(w, row+1 , col+1, "%.*s", l, data); - wmove(w, DROWS+1, 0); - wrefresh(w); - } + int l = len; + char *p; + + while ((p = strpbrk(data, "\r\n")) != NULL) { + *p = '\0'; + } + + if (col < DCOLS) { + if (DCOLS - col < l) + l = DCOLS - col; + mvwprintw(w, row + 1, col + 1, "%.*s", l, data); + wmove(w, DROWS + 1, 0); + wrefresh(w); + } } -static void drv_Curs_defchar (const __attribute__((unused)) int ascii, - const __attribute__((unused)) unsigned char *buffer) +static void drv_Curs_defchar(const __attribute__ ((unused)) + int ascii, const __attribute__ ((unused)) + unsigned char *buffer) { - /* empty */ + /* empty */ } /* ncures scroll SIGSEGVs on my system, so this is a workaroud */ -int curses_error(char *buffer) +int curses_error(char *buffer) { - static int lines = 0; - static char *lb[100]; - int start, i; - char *p; - - if (e == NULL) return 0; - - /* replace \r, \n with underscores */ - while ((p = strpbrk(buffer, "\r\n")) != NULL) { - *p='_'; - } - - if (lines >= EROWS) { - free(lb[0]); - for (i = 1; i <= EROWS; i++) { - lb[i-1] = lb[i]; + static int lines = 0; + static char *lb[100]; + int start, i; + char *p; + + if (e == NULL) + return 0; + + /* replace \r, \n with underscores */ + while ((p = strpbrk(buffer, "\r\n")) != NULL) { + *p = '_'; + } + + if (lines >= EROWS) { + free(lb[0]); + for (i = 1; i <= EROWS; i++) { + lb[i - 1] = lb[i]; + } + start = 0; + } else { + start = lines; + } + + lb[lines] = strdup(buffer); + for (i = start; i <= lines; i++) { + mvwprintw(e, i + 1, 1, "%s", lb[i]); + wclrtoeol(e); } - start = 0; - } else { - start = lines; - } - - lb[lines] = strdup(buffer); - for (i = start; i <= lines; i++) { - mvwprintw(e, i+1, 1, "%s", lb[i]); - wclrtoeol(e); - } - - box(e, 0, 0); - mvwprintw(e, 0, 3, "Stderr:"); - wrefresh(e); - - if (lines < EROWS) lines++; - - return 1; + + box(e, 0, 0); + mvwprintw(e, 0, 3, "Stderr:"); + wrefresh(e); + + if (lines < EROWS) + lines++; + + return 1; } -static int drv_Curs_start (const char *section, const int quiet) +static int drv_Curs_start(const char *section, const int quiet) { - char *s; - - if (!running_foreground) { - error("%s: You want me to display on /dev/null? Sorry, I can't ...", Name); - error("%s: Maybe you want me to run in foreground? Try '-F'", Name); - return -1; - } - - s = cfg_get(section, "Size", "20x4"); - if (s == NULL || *s == '\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - free(s); - return -1; - } - if (sscanf(s, "%dx%d", &DCOLS, &DROWS) != 2 || DROWS < 1 || DCOLS < 1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source); + char *s; + + if (!running_foreground) { + error("%s: You want me to display on /dev/null? Sorry, I can't ...", Name); + error("%s: Maybe you want me to run in foreground? Try '-F'", Name); + return -1; + } + + s = cfg_get(section, "Size", "20x4"); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + free(s); + return -1; + } + if (sscanf(s, "%dx%d", &DCOLS, &DROWS) != 2 || DROWS < 1 || DCOLS < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source); + free(s); + return -1; + } free(s); - return -1; - } - free (s); - - initscr(); - debug ("%s: curses thinks that COLS=%d LINES=%d", Name, COLS, LINES); - w = newwin(DROWS+2, DCOLS+2, 0, 0); - - EROWS = LINES-DROWS-3; - if (EROWS > 99) EROWS = 99; - debug ("EROWS=%d", EROWS); - - if (EROWS >= 4) { - e = newwin (EROWS, COLS, DROWS+3, 0); - EROWS -= 3; - box(e, 0, 0); - mvwprintw (e, 0, 3, "Stderr:"); - wmove(e, 1 , 0); - wrefresh(e); - } - drv_Curs_clear(); + initscr(); + debug("%s: curses thinks that COLS=%d LINES=%d", Name, COLS, LINES); + w = newwin(DROWS + 2, DCOLS + 2, 0, 0); + + EROWS = LINES - DROWS - 3; + if (EROWS > 99) + EROWS = 99; + debug("EROWS=%d", EROWS); + + if (EROWS >= 4) { + e = newwin(EROWS, COLS, DROWS + 3, 0); + EROWS -= 3; + box(e, 0, 0); + mvwprintw(e, 0, 3, "Stderr:"); + wmove(e, 1, 0); + wrefresh(e); + } + + drv_Curs_clear(); - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, NULL)) { - sleep (3); - drv_Curs_clear(); + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, NULL)) { + sleep(3); + drv_Curs_clear(); + } } - } - - return 0; + + return 0; } @@ -256,89 +264,89 @@ static int drv_Curs_start (const char *section, const int quiet) /* list models */ -int drv_Curs_list (void) +int drv_Curs_list(void) { - printf ("any"); - return 0; + printf("any"); + return 0; } /* initialize driver & display */ -int drv_Curs_init (const char *section, const int quiet) +int drv_Curs_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* display preferences */ - XRES = 1; /* pixel width of one char */ - YRES = 1; /* pixel height of one char */ - CHARS = 0; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = 0; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_Curs_write; - drv_generic_text_real_defchar = drv_Curs_defchar; - - /* start display */ - if ((ret = drv_Curs_start (section, quiet)) != 0) { - return ret; - } - - /* initialize generic text driver */ - if ((ret = drv_generic_text_init(section, Name)) != 0) - return ret; - - /* initialize generic bar driver */ - if ((ret = drv_generic_text_bar_init(1)) != 0) - return ret; - - /* add fixed chars to the bar driver */ - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - drv_generic_text_bar_add_segment (255,255,255,'*'); /* asterisk */ - - /* register text widget */ - wc = Widget_Text; - wc.draw = drv_generic_text_draw; - widget_register(&wc); - - /* register bar widget */ - wc = Widget_Bar; - wc.draw = drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none at the moment... */ - - return 0; + WIDGET_CLASS wc; + int ret; + + /* display preferences */ + XRES = 1; /* pixel width of one char */ + YRES = 1; /* pixel height of one char */ + CHARS = 0; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 0; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_Curs_write; + drv_generic_text_real_defchar = drv_Curs_defchar; + + /* start display */ + if ((ret = drv_Curs_start(section, quiet)) != 0) { + return ret; + } + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(1)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + drv_generic_text_bar_add_segment(255, 255, 255, '*'); /* asterisk */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none at the moment... */ + + return 0; } /* close driver & display */ -int drv_Curs_quit (const int quiet) { - - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_Curs_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - endwin(); - - return (0); +int drv_Curs_quit(const int quiet) +{ + + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_Curs_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + endwin(); + + return (0); } DRIVER drv_Curses = { - name: Name, - list: drv_Curs_list, - init: drv_Curs_init, - quit: drv_Curs_quit, + name:Name, + list:drv_Curs_list, + init:drv_Curs_init, + quit:drv_Curs_quit, }; - diff --git a/drv_Cwlinux.c b/drv_Cwlinux.c index 1088a8a..cb1fa41 100644 --- a/drv_Cwlinux.c +++ b/drv_Cwlinux.c @@ -1,4 +1,4 @@ -/* $Id: drv_Cwlinux.c,v 1.21 2005/01/18 06:30:22 reinelt Exp $ +/* $Id: drv_Cwlinux.c,v 1.22 2005/05/08 04:32:44 reinelt Exp $ * * new style driver for Cwlinux display modules * @@ -23,6 +23,9 @@ * * * $Log: drv_Cwlinux.c,v $ + * Revision 1.22 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.21 2005/01/18 06:30:22 reinelt * added (C) to all copyright statements * @@ -146,7 +149,7 @@ #include "drv_generic_serial.h" -static char Name[]="Cwlinux"; +static char Name[] = "Cwlinux"; static int Model; static int Protocol; @@ -157,22 +160,22 @@ static int GPOS; typedef struct { - int type; - char *name; - int rows; - int cols; - int xres; - int gpos; - int protocol; + int type; + char *name; + int rows; + int cols; + int xres; + int gpos; + int protocol; } MODEL; /* Fixme: number of gpo's should be verified */ static MODEL Models[] = { - { 0x01, "CW1602", 2, 16, 5, 0, 1 }, - { 0x02, "CW12232", 4, 20, 6, 0, 2 }, - { 0xff, "Unknown", -1, -1, -1, -1, -1 } + {0x01, "CW1602", 2, 16, 5, 0, 1}, + {0x02, "CW12232", 4, 20, 6, 0, 2}, + {0xff, "Unknown", -1, -1, -1, -1, -1} }; @@ -180,165 +183,170 @@ static MODEL Models[] = { /*** hardware dependant functions ***/ /****************************************/ -static void drv_CW_send (const char *string, const int len) +static void drv_CW_send(const char *string, const int len) { - drv_generic_serial_write (string, len); - usleep (20); + drv_generic_serial_write(string, len); + usleep(20); } -static void drv_CW_write (const int row, const int col, const char *data, const int len) +static void drv_CW_write(const int row, const int col, const char *data, const int len) { - char cmd[6]="\376Gxy\375"; - - cmd[2]=(char)col; - cmd[3]=(char)row; - drv_CW_send (cmd, 5); - drv_CW_send (data, len); + char cmd[6] = "\376Gxy\375"; + + cmd[2] = (char) col; + cmd[3] = (char) row; + drv_CW_send(cmd, 5); + drv_CW_send(data, len); } -static void drv_CW1602_defchar (const int ascii, const unsigned char *buffer) +static void drv_CW1602_defchar(const int ascii, const unsigned char *buffer) { - int i; - char cmd[12]="\376Nn12345678\375"; + int i; + char cmd[12] = "\376Nn12345678\375"; - cmd[2]=(char)ascii; + cmd[2] = (char) ascii; - for (i=0; i<8; i++) { - cmd[3+i] = buffer[i] & 0x1f; - } - drv_CW_send(cmd,12); + for (i = 0; i < 8; i++) { + cmd[3 + i] = buffer[i] & 0x1f; + } + drv_CW_send(cmd, 12); } -static void drv_CW12232_defchar (const int ascii, const unsigned char *buffer) +static void drv_CW12232_defchar(const int ascii, const unsigned char *buffer) { - int i, j; - char cmd[10]="\376Nn123456\375"; - - cmd[2]=(char)ascii; - - /* The CW12232 uses a vertical bitmap layout, */ - /* so we have to 'rotate' the bitmap. */ - - for (i=0; i<6;i++) { - cmd[3+i]=0; - for (j=0; j<8;j++) { - if (buffer[j] & (1<<(5-i))) { - cmd[3+i]|=(1<<j); - } + int i, j; + char cmd[10] = "\376Nn123456\375"; + + cmd[2] = (char) ascii; + + /* The CW12232 uses a vertical bitmap layout, */ + /* so we have to 'rotate' the bitmap. */ + + for (i = 0; i < 6; i++) { + cmd[3 + i] = 0; + for (j = 0; j < 8; j++) { + if (buffer[j] & (1 << (5 - i))) { + cmd[3 + i] |= (1 << j); + } + } } - } - drv_CW_send (cmd, 10); + drv_CW_send(cmd, 10); } -static void drv_CW_clear (void) +static void drv_CW_clear(void) { #if 1 - drv_CW_send("\376X\375",3); /* Clear Display */ - usleep(500000); + drv_CW_send("\376X\375", 3); /* Clear Display */ + usleep(500000); #else - /* for some mysterious reason, we have to sleep after */ - /* the command _and_ after the CMD_END... */ - drv_CW_send("\376X",2); /* Clear Display */ - drv_CW_send("\375",1); /* Command End */ + /* for some mysterious reason, we have to sleep after */ + /* the command _and_ after the CMD_END... */ + drv_CW_send("\376X", 2); /* Clear Display */ + drv_CW_send("\375", 1); /* Command End */ #endif } -static int drv_CW_brightness (int brightness) +static int drv_CW_brightness(int brightness) { - static unsigned char Brightness = 0; - char cmd[5] = "\376A_\375"; - - /* -1 is used to query the current brightness */ - if (brightness == -1) return Brightness; - - if (brightness < 0 ) brightness = 0; - if (brightness > 255) brightness = 255; - Brightness = brightness; - - switch (Brightness) { - case 0: - /* backlight off */ - drv_CW_send ("\376F\375", 3); - break; - case 8: - /* backlight on */ - drv_CW_send ("\376B\375", 3); - break; - default: - /* backlight level */ - cmd[2] = (char)Brightness; - drv_CW_send (cmd, 4); - break; - } - - return Brightness; + static unsigned char Brightness = 0; + char cmd[5] = "\376A_\375"; + + /* -1 is used to query the current brightness */ + if (brightness == -1) + return Brightness; + + if (brightness < 0) + brightness = 0; + if (brightness > 255) + brightness = 255; + Brightness = brightness; + + switch (Brightness) { + case 0: + /* backlight off */ + drv_CW_send("\376F\375", 3); + break; + case 8: + /* backlight on */ + drv_CW_send("\376B\375", 3); + break; + default: + /* backlight level */ + cmd[2] = (char) Brightness; + drv_CW_send(cmd, 4); + break; + } + + return Brightness; } -static int drv_CW_start (const char *section) +static int drv_CW_start(const char *section) { - int i; - char *model; - - model=cfg_get(section, "Model", NULL); - if (model!=NULL && *model!='\0') { - for (i=0; Models[i].type!=0xff; i++) { - if (strcasecmp(Models[i].name, model)==0) break; - } - if (Models[i].type==0xff) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; + int i; + char *model; + + model = cfg_get(section, "Model", NULL); + if (model != NULL && *model != '\0') { + for (i = 0; Models[i].type != 0xff; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; + } + if (Models[i].type == 0xff) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + Model = i; + info("%s: using model '%s'", Name, Models[Model].name); + } else { + error("%s: no '%s.Model' entry from %s", Name, section, cfg_source()); + return -1; } - Model=i; - info ("%s: using model '%s'", Name, Models[Model].name); - } else { - error ("%s: no '%s.Model' entry from %s", Name, section, cfg_source()); - return -1; - } - - /* open serial port */ - if (drv_generic_serial_open(section, Name, 0) < 0) return -1; - - /* this does not work as I'd expect it... */ + + /* open serial port */ + if (drv_generic_serial_open(section, Name, 0) < 0) + return -1; + + /* this does not work as I'd expect it... */ #if 0 - /* read firmware version */ - generic_serial_read(buffer,sizeof(buffer)); - usleep(100000); - generic_serial_write ("\3761", 2); - usleep(100000); - generic_serial_write ("\375", 1); - usleep(100000); - if (generic_serial_read(buffer,2)!=2) { - info ("unable to read firmware version!"); - } - info ("Cwlinux Firmware %d.%d", (int)buffer[0], (int)buffer[1]); + /* read firmware version */ + generic_serial_read(buffer, sizeof(buffer)); + usleep(100000); + generic_serial_write("\3761", 2); + usleep(100000); + generic_serial_write("\375", 1); + usleep(100000); + if (generic_serial_read(buffer, 2) != 2) { + info("unable to read firmware version!"); + } + info("Cwlinux Firmware %d.%d", (int) buffer[0], (int) buffer[1]); #endif - /* initialize global variables */ - DROWS = Models[Model].rows; - DCOLS = Models[Model].cols; - XRES = Models[Model].xres; - GPOS = Models[Model].gpos; - Protocol = Models[Model].protocol; + /* initialize global variables */ + DROWS = Models[Model].rows; + DCOLS = Models[Model].cols; + XRES = Models[Model].xres; + GPOS = Models[Model].gpos; + Protocol = Models[Model].protocol; - drv_CW_clear(); + drv_CW_clear(); - drv_CW_send ("\376D\375", 3); /* auto line wrap off */ - drv_CW_send ("\376R\375", 3); /* auto scroll off */ - drv_CW_send ("\376K\375", 3); /* underline cursor off */ - drv_CW_send ("\376B\375", 3); /* backlight on */ + drv_CW_send("\376D\375", 3); /* auto line wrap off */ + drv_CW_send("\376R\375", 3); /* auto scroll off */ + drv_CW_send("\376K\375", 3); /* underline cursor off */ + drv_CW_send("\376B\375", 3); /* backlight on */ - /* set brightness */ - if (cfg_number(section, "Brightness", 0, 0, 8, &i) > 0) { - drv_CW_brightness(i); - } + /* set brightness */ + if (cfg_number(section, "Brightness", 0, 0, 8, &i) > 0) { + drv_CW_brightness(i); + } - return 0; + return 0; } @@ -347,23 +355,23 @@ static int drv_CW_start (const char *section) /****************************************/ -static void plugin_brightness (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_brightness(RESULT * result, const int argc, RESULT * argv[]) { - double brightness; - - switch (argc) { - case 0: - brightness = drv_CW_brightness(-1); - SetResult(&result, R_NUMBER, &brightness); - break; - case 1: - brightness = drv_CW_brightness(R2N(argv[0])); - SetResult(&result, R_NUMBER, &brightness); - break; - default: - error ("%s.brightness(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double brightness; + + switch (argc) { + case 0: + brightness = drv_CW_brightness(-1); + SetResult(&result, R_NUMBER, &brightness); + break; + case 1: + brightness = drv_CW_brightness(R2N(argv[0])); + SetResult(&result, R_NUMBER, &brightness); + break; + default: + error("%s.brightness(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } @@ -382,117 +390,117 @@ static void plugin_brightness (RESULT *result, const int argc, RESULT *argv[]) /* list models */ -int drv_CW_list (void) +int drv_CW_list(void) { - int i; - - for (i=0; Models[i].type!=0xff; i++) { - printf ("%s ", Models[i].name); - } - return 0; + int i; + + for (i = 0; Models[i].type != 0xff; i++) { + printf("%s ", Models[i].name); + } + return 0; } /* initialize driver & display */ -int drv_CW_init (const char *section, const int quiet) +int drv_CW_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* display preferences */ - XRES = 6; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 16; /* number of user-defineable characters */ - CHAR0 = 1; /* ASCII of first user-defineable char */ - GOTO_COST = 3; /* number of bytes a goto command requires */ - INVALIDATE = 1; /* re-defined chars must be re-sent to the display */ - - /* start display */ - if ((ret=drv_CW_start (section))!=0) - return ret; - - /* real worker functions */ - drv_generic_text_real_write = drv_CW_write; - - switch (Protocol) { + WIDGET_CLASS wc; + int ret; + + /* display preferences */ + XRES = 6; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 16; /* number of user-defineable characters */ + CHAR0 = 1; /* ASCII of first user-defineable char */ + GOTO_COST = 3; /* number of bytes a goto command requires */ + INVALIDATE = 1; /* re-defined chars must be re-sent to the display */ + + /* start display */ + if ((ret = drv_CW_start(section)) != 0) + return ret; + + /* real worker functions */ + drv_generic_text_real_write = drv_CW_write; + + switch (Protocol) { case 1: - drv_generic_text_real_defchar = drv_CW1602_defchar; - break; + drv_generic_text_real_defchar = drv_CW1602_defchar; + break; case 2: - drv_generic_text_real_defchar = drv_CW12232_defchar; - break; - } - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %s", Name, Models[Model].name); - if (drv_generic_text_greet (buffer, "www.cwlinux.com")) { - sleep (3); - drv_CW_clear(); + drv_generic_text_real_defchar = drv_CW12232_defchar; + break; + } + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %s", Name, Models[Model].name); + if (drv_generic_text_greet(buffer, "www.cwlinux.com")) { + sleep(3); + drv_CW_clear(); + } } - } - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - drv_generic_text_bar_add_segment (0, 0, 255, 32); /* ASCII 32 = blank */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - AddFunction ("LCD::brightness", -1, plugin_brightness); - - return 0; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + AddFunction("LCD::brightness", -1, plugin_brightness); + + return 0; } /* close driver & display */ -int drv_CW_quit (const int quiet) { - - info("%s: shutting down.", Name); - drv_generic_text_quit(); - - /* clear display */ - drv_CW_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - drv_generic_serial_close(); - - return (0); +int drv_CW_quit(const int quiet) +{ + + info("%s: shutting down.", Name); + drv_generic_text_quit(); + + /* clear display */ + drv_CW_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + drv_generic_serial_close(); + + return (0); } DRIVER drv_Cwlinux = { - name: Name, - list: drv_CW_list, - init: drv_CW_init, - quit: drv_CW_quit, + name:Name, + list:drv_CW_list, + init:drv_CW_init, + quit:drv_CW_quit, }; - diff --git a/drv_HD44780.c b/drv_HD44780.c index 881a0de..e032a5d 100644 --- a/drv_HD44780.c +++ b/drv_HD44780.c @@ -1,4 +1,4 @@ -/* $Id: drv_HD44780.c,v 1.48 2005/05/05 08:36:12 reinelt Exp $ +/* $Id: drv_HD44780.c,v 1.49 2005/05/08 04:32:44 reinelt Exp $ * * new style driver for HD44780-based displays * @@ -32,6 +32,9 @@ * * * $Log: drv_HD44780.c,v $ + * Revision 1.49 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.48 2005/05/05 08:36:12 reinelt * changed SELECT to SLCTIN * @@ -265,7 +268,7 @@ #include "drv_generic_i2c.h" #endif -static char Name[]="HD44780"; +static char Name[] = "HD44780"; static int Bus; static int Model; @@ -277,11 +280,11 @@ static int Capabilities; * we use the worst-case values. */ -#define T_CYCLE 1000 /* Enable cycle time */ -#define T_PW 450 /* Enable pulse width */ -#define T_AS 60 /* Address setup time */ -#define T_H 40 /* Data hold time */ -#define T_AH 20 /* Address hold time */ +#define T_CYCLE 1000 /* Enable cycle time */ +#define T_PW 450 /* Enable pulse width */ +#define T_AS 60 /* Address setup time */ +#define T_H 40 /* Data hold time */ +#define T_AH 20 /* Address hold time */ /* HD44780 execution timings [microseconds] @@ -289,14 +292,14 @@ static int Capabilities; * we use the worst-case values. */ -#define T_INIT1 4100 /* first init sequence: 4.1 msec */ -#define T_INIT2 100 /* second init sequence: 100 usec */ -#define T_EXEC 80 /* normal execution time */ -#define T_WRCG 120 /* CG RAM Write */ -#define T_CLEAR 2250 /* Clear Display */ +#define T_INIT1 4100 /* first init sequence: 4.1 msec */ +#define T_INIT2 100 /* second init sequence: 100 usec */ +#define T_EXEC 80 /* normal execution time */ +#define T_WRCG 120 /* CG RAM Write */ +#define T_CLEAR 2250 /* Clear Display */ -static int Bits=0; +static int Bits = 0; static int numControllers = 0; static int allControllers = 0; static int currController = 0; @@ -330,9 +333,9 @@ static int GPOS; typedef struct { - int type; - char *name; - int capabilities; + int type; + char *name; + int capabilities; } MODEL; #define CAP_PARPORT (1<<0) @@ -349,12 +352,12 @@ typedef struct { static MODEL Models[] = { - { 0x01, "generic", CAP_PARPORT|CAP_I2C|CAP_GPO|CAP_BACKLIGHT }, - { 0x02, "Noritake", CAP_PARPORT|CAP_I2C|CAP_GPO|CAP_BRIGHTNESS }, - { 0x03, "Soekris", CAP_PARPORT|CAP_BUSY4BIT }, - { 0x04, "HD66712", CAP_PARPORT|CAP_I2C|CAP_GPO|CAP_BACKLIGHT|CAP_HD66712 }, - { 0x05, "LCM-162", CAP_PARPORT|CAP_LCM162 }, - { 0xff, "Unknown", 0 } + {0x01, "generic", CAP_PARPORT | CAP_I2C | CAP_GPO | CAP_BACKLIGHT}, + {0x02, "Noritake", CAP_PARPORT | CAP_I2C | CAP_GPO | CAP_BRIGHTNESS}, + {0x03, "Soekris", CAP_PARPORT | CAP_BUSY4BIT}, + {0x04, "HD66712", CAP_PARPORT | CAP_I2C | CAP_GPO | CAP_BACKLIGHT | CAP_HD66712}, + {0x05, "LCM-162", CAP_PARPORT | CAP_LCM162}, + {0xff, "Unknown", 0} }; @@ -362,10 +365,10 @@ static MODEL Models[] = { /*** generic functions ***/ /****************************************/ -static int (*drv_HD_load) (const char *section); +static int (*drv_HD_load) (const char *section); static void (*drv_HD_command) (const unsigned char controller, const unsigned char cmd, const int delay); -static void (*drv_HD_data) (const unsigned char controller, const char *string, const int len, const int delay); -static void (*drv_HD_stop) (void); +static void (*drv_HD_data) (const unsigned char controller, const char *string, const int len, const int delay); +static void (*drv_HD_stop) (void); @@ -373,409 +376,446 @@ static void (*drv_HD_stop) (void); /*** parport dependant functions ***/ /****************************************/ -static void drv_HD_PP_busy (const int controller) +static void drv_HD_PP_busy(const int controller) { - static unsigned int errors = 0; - - unsigned char enable; - unsigned char data=0xFF; - unsigned char busymask; - unsigned char ctrlmask; - unsigned int counter; - - if (Bits == 8) { - busymask = 0x80; - } else { - /* Since in 4-Bit mode DB0 on the parport is mapped to DB4 on the LCD - * (and consequently, DB3 on the partport is mapped to DB7 on the LCD) - * we need to listen for DB3 on the parport to go low - */ - busymask = 0x08; - } - - ctrlmask = 0x08; - while (ctrlmask > 0) { - - if (controller & ctrlmask) { - - enable = 0; - if (ctrlmask & 0x01) enable = SIGNAL_ENABLE; - else if (ctrlmask & 0x02) enable = SIGNAL_ENABLE2; - else if (ctrlmask & 0x04) enable = SIGNAL_ENABLE3; - else if (ctrlmask & 0x08) enable = SIGNAL_ENABLE4; - - /* set data-lines to input*/ - drv_generic_parport_direction(1); - - if (Bits == 8) { - /* Set RW, clear RS */ - drv_generic_parport_control(SIGNAL_RW|SIGNAL_RS, SIGNAL_RW); - } else { - drv_generic_parport_data(SIGNAL_RW); - } - - /* Address set-up time */ - ndelay(T_AS); - - /* rise ENABLE */ - if (Bits == 8) { - drv_generic_parport_control(enable,enable); - } else { - drv_generic_parport_data(SIGNAL_RW|enable); - } - - counter=0; - while (1) { - /* read the busy flag */ - data = drv_generic_parport_read(); - if ((data & busymask) == 0) { - errors = 0; - break; - } - - /* make sure we don't wait forever - * - but only check after 5 iterations - * that way, we won't slow down normal mode - * (where we don't need the timeout anyway) + static unsigned int errors = 0; + + unsigned char enable; + unsigned char data = 0xFF; + unsigned char busymask; + unsigned char ctrlmask; + unsigned int counter; + + if (Bits == 8) { + busymask = 0x80; + } else { + /* Since in 4-Bit mode DB0 on the parport is mapped to DB4 on the LCD + * (and consequently, DB3 on the partport is mapped to DB7 on the LCD) + * we need to listen for DB3 on the parport to go low */ - counter++; - - if (counter >= 5) { - struct timeval now, end; - - if (counter == 5) { - /* determine the time when the timeout has expired */ - gettimeofday (&end, NULL); - end.tv_usec += MAX_BUSYFLAG_WAIT; - while (end.tv_usec > 1000000) { - end.tv_usec -= 1000000; - end.tv_sec++; + busymask = 0x08; + } + + ctrlmask = 0x08; + while (ctrlmask > 0) { + + if (controller & ctrlmask) { + + enable = 0; + if (ctrlmask & 0x01) + enable = SIGNAL_ENABLE; + else if (ctrlmask & 0x02) + enable = SIGNAL_ENABLE2; + else if (ctrlmask & 0x04) + enable = SIGNAL_ENABLE3; + else if (ctrlmask & 0x08) + enable = SIGNAL_ENABLE4; + + /* set data-lines to input */ + drv_generic_parport_direction(1); + + if (Bits == 8) { + /* Set RW, clear RS */ + drv_generic_parport_control(SIGNAL_RW | SIGNAL_RS, SIGNAL_RW); + } else { + drv_generic_parport_data(SIGNAL_RW); } - } - - /* get the current time */ - gettimeofday(&now, NULL); - if (now.tv_sec == end.tv_sec ? now.tv_usec >= end.tv_usec : now.tv_sec >= end.tv_sec) { - error ("%s: timeout waiting for busy flag on controller %x (%x)", Name, ctrlmask, data); - if (++errors >= MAX_BUSYFLAG_ERRORS) { - error ("%s: too many busy flag failures, turning off busy flag checking.", Name); - UseBusy = 0; + + /* Address set-up time */ + ndelay(T_AS); + + /* rise ENABLE */ + if (Bits == 8) { + drv_generic_parport_control(enable, enable); + } else { + drv_generic_parport_data(SIGNAL_RW | enable); } - break; - } - } - } - - /* RS=low, RW=low, EN=low */ - if (Bits == 8) { - /* Lower EN */ - drv_generic_parport_control(enable,0); - - /* Address hold time */ - ndelay(T_AH); - - drv_generic_parport_control(SIGNAL_RW|SIGNAL_RS,0); - } else { - /* Lower EN */ - drv_generic_parport_data(SIGNAL_RW); - ndelay(T_AH); - drv_generic_parport_data(0); - } - - /* set data-lines to output*/ - drv_generic_parport_direction(0); - } - ctrlmask >>= 1; - } -} + counter = 0; + while (1) { + /* read the busy flag */ + data = drv_generic_parport_read(); + if ((data & busymask) == 0) { + errors = 0; + break; + } + + /* make sure we don't wait forever + * - but only check after 5 iterations + * that way, we won't slow down normal mode + * (where we don't need the timeout anyway) + */ + counter++; + + if (counter >= 5) { + struct timeval now, end; + + if (counter == 5) { + /* determine the time when the timeout has expired */ + gettimeofday(&end, NULL); + end.tv_usec += MAX_BUSYFLAG_WAIT; + while (end.tv_usec > 1000000) { + end.tv_usec -= 1000000; + end.tv_sec++; + } + } + + /* get the current time */ + gettimeofday(&now, NULL); + if (now.tv_sec == end.tv_sec ? now.tv_usec >= end.tv_usec : now.tv_sec >= end.tv_sec) { + error("%s: timeout waiting for busy flag on controller %x (%x)", Name, ctrlmask, data); + if (++errors >= MAX_BUSYFLAG_ERRORS) { + error("%s: too many busy flag failures, turning off busy flag checking.", Name); + UseBusy = 0; + } + break; + } + } + } -static void drv_HD_PP_nibble (const unsigned char controller, const unsigned char nibble) -{ - unsigned char enable; - - /* enable signal: 'controller' is a bitmask */ - /* bit n .. send to controller #n */ - /* so we can send a byte to more controllers at the same time! */ - enable=0; - if (controller & 0x01) enable |= SIGNAL_ENABLE; - if (controller & 0x02) enable |= SIGNAL_ENABLE2; - if (controller & 0x04) enable |= SIGNAL_ENABLE3; - if (controller & 0x08) enable |= SIGNAL_ENABLE4; - - /* clear ENABLE */ - /* put data on DB1..DB4 */ - /* nibble already contains RS bit! */ - drv_generic_parport_data(nibble); - - /* Address set-up time */ - ndelay(T_AS); - - /* rise ENABLE */ - drv_generic_parport_data(nibble | enable); - - /* Enable pulse width */ - ndelay(T_PW); - - /* lower ENABLE */ - drv_generic_parport_data(nibble); -} + /* RS=low, RW=low, EN=low */ + if (Bits == 8) { + /* Lower EN */ + drv_generic_parport_control(enable, 0); + /* Address hold time */ + ndelay(T_AH); -static void drv_HD_PP_byte (const unsigned char controller, const unsigned char data, const unsigned char RS) -{ - /* send high nibble of the data */ - drv_HD_PP_nibble (controller, ((data >> 4) & 0x0f) | RS); - - /* Make sure we honour T_CYCLE */ - ndelay(T_CYCLE-T_AS-T_PW); - - /* send low nibble of the data */ - drv_HD_PP_nibble (controller, (data & 0x0f) | RS); + drv_generic_parport_control(SIGNAL_RW | SIGNAL_RS, 0); + } else { + /* Lower EN */ + drv_generic_parport_data(SIGNAL_RW); + ndelay(T_AH); + drv_generic_parport_data(0); + } + + /* set data-lines to output */ + drv_generic_parport_direction(0); + } + ctrlmask >>= 1; + } } -static void drv_HD_PP_command (const unsigned char controller, const unsigned char cmd, const int delay) +static void drv_HD_PP_nibble(const unsigned char controller, const unsigned char nibble) { - unsigned char enable; - - if (UseBusy) drv_HD_PP_busy (controller); - - if (Bits == 8) { - + unsigned char enable; + /* enable signal: 'controller' is a bitmask */ /* bit n .. send to controller #n */ /* so we can send a byte to more controllers at the same time! */ - enable=0; - if (controller & 0x01) enable |= SIGNAL_ENABLE; - if (controller & 0x02) enable |= SIGNAL_ENABLE2; - if (controller & 0x04) enable |= SIGNAL_ENABLE3; - if (controller & 0x08) enable |= SIGNAL_ENABLE4; - - /* put data on DB1..DB8 */ - drv_generic_parport_data (cmd); - - /* clear RW and RS */ - drv_generic_parport_control (SIGNAL_RW | SIGNAL_RS, 0); - + enable = 0; + if (controller & 0x01) + enable |= SIGNAL_ENABLE; + if (controller & 0x02) + enable |= SIGNAL_ENABLE2; + if (controller & 0x04) + enable |= SIGNAL_ENABLE3; + if (controller & 0x08) + enable |= SIGNAL_ENABLE4; + + /* clear ENABLE */ + /* put data on DB1..DB4 */ + /* nibble already contains RS bit! */ + drv_generic_parport_data(nibble); + /* Address set-up time */ ndelay(T_AS); - - /* send command */ - drv_generic_parport_toggle (enable, 1, T_PW); - - } else { - - drv_HD_PP_byte (controller, cmd, 0); - - } - - /* wait for command completion */ - if (!UseBusy) udelay (delay); - + + /* rise ENABLE */ + drv_generic_parport_data(nibble | enable); + + /* Enable pulse width */ + ndelay(T_PW); + + /* lower ENABLE */ + drv_generic_parport_data(nibble); } -static void drv_HD_PP_data (const unsigned char controller, const char *string, const int len, const int delay) +static void drv_HD_PP_byte(const unsigned char controller, const unsigned char data, const unsigned char RS) { - int l = len; - unsigned char enable; - - /* sanity check */ - if (len <= 0) return; - - if (Bits == 8) { - - /* enable signal: 'controller' is a bitmask */ - /* bit n .. send to controller #n */ - /* so we can send a byte to more controllers at the same time! */ - enable=0; - if (controller & 0x01) enable |= SIGNAL_ENABLE; - if (controller & 0x02) enable |= SIGNAL_ENABLE2; - if (controller & 0x04) enable |= SIGNAL_ENABLE3; - if (controller & 0x08) enable |= SIGNAL_ENABLE4; - - if (!UseBusy) { - /* clear RW, set RS */ - drv_generic_parport_control (SIGNAL_RW | SIGNAL_RS, SIGNAL_RS); - /* Address set-up time */ - ndelay(T_AS); - } - - while (l--) { - - if (UseBusy) { - drv_HD_PP_busy (controller); - /* clear RW, set RS */ - drv_generic_parport_control (SIGNAL_RW | SIGNAL_RS, SIGNAL_RS); + /* send high nibble of the data */ + drv_HD_PP_nibble(controller, ((data >> 4) & 0x0f) | RS); + + /* Make sure we honour T_CYCLE */ + ndelay(T_CYCLE - T_AS - T_PW); + + /* send low nibble of the data */ + drv_HD_PP_nibble(controller, (data & 0x0f) | RS); +} + + +static void drv_HD_PP_command(const unsigned char controller, const unsigned char cmd, const int delay) +{ + unsigned char enable; + + if (UseBusy) + drv_HD_PP_busy(controller); + + if (Bits == 8) { + + /* enable signal: 'controller' is a bitmask */ + /* bit n .. send to controller #n */ + /* so we can send a byte to more controllers at the same time! */ + enable = 0; + if (controller & 0x01) + enable |= SIGNAL_ENABLE; + if (controller & 0x02) + enable |= SIGNAL_ENABLE2; + if (controller & 0x04) + enable |= SIGNAL_ENABLE3; + if (controller & 0x08) + enable |= SIGNAL_ENABLE4; + + /* put data on DB1..DB8 */ + drv_generic_parport_data(cmd); + + /* clear RW and RS */ + drv_generic_parport_control(SIGNAL_RW | SIGNAL_RS, 0); + /* Address set-up time */ ndelay(T_AS); - } - - /* put data on DB1..DB8 */ - drv_generic_parport_data (*(string++)); - - /* send command */ - drv_generic_parport_toggle (enable, 1, T_PW); - - /* wait for command completion */ - if (!UseBusy) udelay (delay); + + /* send command */ + drv_generic_parport_toggle(enable, 1, T_PW); + + } else { + + drv_HD_PP_byte(controller, cmd, 0); + } - - } else { /* 4 bit mode */ - - while (l--) { - if (UseBusy) drv_HD_PP_busy (controller); - - /* send data with RS enabled */ - drv_HD_PP_byte (controller, *(string++), SIGNAL_RS); - - /* wait for command completion */ - if (!UseBusy) udelay (delay); + + /* wait for command completion */ + if (!UseBusy) + udelay(delay); + +} + + +static void drv_HD_PP_data(const unsigned char controller, const char *string, const int len, const int delay) +{ + int l = len; + unsigned char enable; + + /* sanity check */ + if (len <= 0) + return; + + if (Bits == 8) { + + /* enable signal: 'controller' is a bitmask */ + /* bit n .. send to controller #n */ + /* so we can send a byte to more controllers at the same time! */ + enable = 0; + if (controller & 0x01) + enable |= SIGNAL_ENABLE; + if (controller & 0x02) + enable |= SIGNAL_ENABLE2; + if (controller & 0x04) + enable |= SIGNAL_ENABLE3; + if (controller & 0x08) + enable |= SIGNAL_ENABLE4; + + if (!UseBusy) { + /* clear RW, set RS */ + drv_generic_parport_control(SIGNAL_RW | SIGNAL_RS, SIGNAL_RS); + /* Address set-up time */ + ndelay(T_AS); + } + + while (l--) { + + if (UseBusy) { + drv_HD_PP_busy(controller); + /* clear RW, set RS */ + drv_generic_parport_control(SIGNAL_RW | SIGNAL_RS, SIGNAL_RS); + /* Address set-up time */ + ndelay(T_AS); + } + + /* put data on DB1..DB8 */ + drv_generic_parport_data(*(string++)); + + /* send command */ + drv_generic_parport_toggle(enable, 1, T_PW); + + /* wait for command completion */ + if (!UseBusy) + udelay(delay); + } + + } else { /* 4 bit mode */ + + while (l--) { + if (UseBusy) + drv_HD_PP_busy(controller); + + /* send data with RS enabled */ + drv_HD_PP_byte(controller, *(string++), SIGNAL_RS); + + /* wait for command completion */ + if (!UseBusy) + udelay(delay); + } } - } } -static int drv_HD_PP_load (const char *section) +static int drv_HD_PP_load(const char *section) { - if (cfg_number (section, "Bits", 8, 4, 8, &Bits) < 0) return -1; - - if (Bits != 4 && Bits != 8) { - error ("%s: bad %s.Bits '%d' from %s, should be '4' or '8'", Name, section, Bits, cfg_source()); - return -1; - } - - /* LCM-162 only supports 8-bit-mode */ - if (Capabilities & CAP_LCM162 && Bits != 8) { - error ("%s: Model '%s' does not support %d bit mode!", Name, Models[Model].name, Bits); - Bits = 8; - } - info ("%s: using %d bit mode", Name, Bits); - - if (drv_generic_parport_open (section, Name) != 0) { - error ("%s: could not initialize parallel port!", Name); - return -1; - } - - /* Soft-Wiring */ - if (Capabilities & CAP_LCM162) { - /* the LCM-162 is hardwired */ - if ((SIGNAL_RS = drv_generic_parport_hardwire_ctrl ("RS", "SLCTIN")) == 0xff) return -1; - if ((SIGNAL_RW = drv_generic_parport_hardwire_ctrl ("RW", "INIT" )) == 0xff) return -1; - if ((SIGNAL_ENABLE = drv_generic_parport_hardwire_ctrl ("ENABLE", "AUTOFD")) == 0xff) return -1; - if ((SIGNAL_ENABLE2 = drv_generic_parport_hardwire_ctrl ("ENABLE2", "GND" )) == 0xff) return -1; - if ((SIGNAL_ENABLE3 = drv_generic_parport_hardwire_ctrl ("ENABLE3", "GND" )) == 0xff) return -1; - if ((SIGNAL_ENABLE4 = drv_generic_parport_hardwire_ctrl ("ENABLE4", "GND" )) == 0xff) return -1; - if ((SIGNAL_BACKLIGHT = drv_generic_parport_hardwire_ctrl ("BACKLIGHT", "GND" )) == 0xff) return -1; - if ((SIGNAL_GPO = drv_generic_parport_hardwire_ctrl ("GPO", "GND" )) == 0xff) return -1; - } else { + if (cfg_number(section, "Bits", 8, 4, 8, &Bits) < 0) + return -1; + + if (Bits != 4 && Bits != 8) { + error("%s: bad %s.Bits '%d' from %s, should be '4' or '8'", Name, section, Bits, cfg_source()); + return -1; + } + + /* LCM-162 only supports 8-bit-mode */ + if (Capabilities & CAP_LCM162 && Bits != 8) { + error("%s: Model '%s' does not support %d bit mode!", Name, Models[Model].name, Bits); + Bits = 8; + } + info("%s: using %d bit mode", Name, Bits); + + if (drv_generic_parport_open(section, Name) != 0) { + error("%s: could not initialize parallel port!", Name); + return -1; + } + + /* Soft-Wiring */ + if (Capabilities & CAP_LCM162) { + /* the LCM-162 is hardwired */ + if ((SIGNAL_RS = drv_generic_parport_hardwire_ctrl("RS", "SLCTIN")) == 0xff) + return -1; + if ((SIGNAL_RW = drv_generic_parport_hardwire_ctrl("RW", "INIT")) == 0xff) + return -1; + if ((SIGNAL_ENABLE = drv_generic_parport_hardwire_ctrl("ENABLE", "AUTOFD")) == 0xff) + return -1; + if ((SIGNAL_ENABLE2 = drv_generic_parport_hardwire_ctrl("ENABLE2", "GND")) == 0xff) + return -1; + if ((SIGNAL_ENABLE3 = drv_generic_parport_hardwire_ctrl("ENABLE3", "GND")) == 0xff) + return -1; + if ((SIGNAL_ENABLE4 = drv_generic_parport_hardwire_ctrl("ENABLE4", "GND")) == 0xff) + return -1; + if ((SIGNAL_BACKLIGHT = drv_generic_parport_hardwire_ctrl("BACKLIGHT", "GND")) == 0xff) + return -1; + if ((SIGNAL_GPO = drv_generic_parport_hardwire_ctrl("GPO", "GND")) == 0xff) + return -1; + } else { + if (Bits == 8) { + if ((SIGNAL_RS = drv_generic_parport_wire_ctrl("RS", "AUTOFD")) == 0xff) + return -1; + if ((SIGNAL_RW = drv_generic_parport_wire_ctrl("RW", "GND")) == 0xff) + return -1; + if ((SIGNAL_ENABLE = drv_generic_parport_wire_ctrl("ENABLE", "STROBE")) == 0xff) + return -1; + if ((SIGNAL_ENABLE2 = drv_generic_parport_wire_ctrl("ENABLE2", "GND")) == 0xff) + return -1; + if ((SIGNAL_ENABLE3 = drv_generic_parport_wire_ctrl("ENABLE3", "GND")) == 0xff) + return -1; + if ((SIGNAL_ENABLE4 = drv_generic_parport_wire_ctrl("ENABLE4", "GND")) == 0xff) + return -1; + } else { + if ((SIGNAL_RS = drv_generic_parport_wire_data("RS", "DB4")) == 0xff) + return -1; + if ((SIGNAL_RW = drv_generic_parport_wire_data("RW", "DB5")) == 0xff) + return -1; + if ((SIGNAL_ENABLE = drv_generic_parport_wire_data("ENABLE", "DB6")) == 0xff) + return -1; + if ((SIGNAL_ENABLE2 = drv_generic_parport_wire_data("ENABLE2", "GND")) == 0xff) + return -1; + if ((SIGNAL_ENABLE3 = drv_generic_parport_wire_data("ENABLE3", "GND")) == 0xff) + return -1; + if ((SIGNAL_ENABLE4 = drv_generic_parport_wire_data("ENABLE4", "GND")) == 0xff) + return -1; + } + /* backlight and GPO are always control signals */ + if ((SIGNAL_BACKLIGHT = drv_generic_parport_wire_ctrl("BACKLIGHT", "GND")) == 0xff) + return -1; + if ((SIGNAL_GPO = drv_generic_parport_wire_ctrl("GPO", "GND")) == 0xff) + return -1; + } + + /* clear capabilities if corresponding signal is GND */ + if (SIGNAL_BACKLIGHT == 0) { + Capabilities &= ~CAP_BACKLIGHT; + } + if (SIGNAL_GPO == 0) { + Capabilities &= ~CAP_GPO; + } + + + /* clear all signals */ if (Bits == 8) { - if ((SIGNAL_RS = drv_generic_parport_wire_ctrl ("RS", "AUTOFD")) == 0xff) return -1; - if ((SIGNAL_RW = drv_generic_parport_wire_ctrl ("RW", "GND" )) == 0xff) return -1; - if ((SIGNAL_ENABLE = drv_generic_parport_wire_ctrl ("ENABLE", "STROBE")) == 0xff) return -1; - if ((SIGNAL_ENABLE2 = drv_generic_parport_wire_ctrl ("ENABLE2", "GND" )) == 0xff) return -1; - if ((SIGNAL_ENABLE3 = drv_generic_parport_wire_ctrl ("ENABLE3", "GND" )) == 0xff) return -1; - if ((SIGNAL_ENABLE4 = drv_generic_parport_wire_ctrl ("ENABLE4", "GND" )) == 0xff) return -1; + drv_generic_parport_control(SIGNAL_RS | + SIGNAL_RW | SIGNAL_ENABLE | SIGNAL_ENABLE2 | SIGNAL_ENABLE3 | SIGNAL_ENABLE4 | SIGNAL_BACKLIGHT | SIGNAL_GPO, 0); } else { - if ((SIGNAL_RS = drv_generic_parport_wire_data ("RS", "DB4")) == 0xff) return -1; - if ((SIGNAL_RW = drv_generic_parport_wire_data ("RW", "DB5")) == 0xff) return -1; - if ((SIGNAL_ENABLE = drv_generic_parport_wire_data ("ENABLE", "DB6")) == 0xff) return -1; - if ((SIGNAL_ENABLE2 = drv_generic_parport_wire_data ("ENABLE2", "GND")) == 0xff) return -1; - if ((SIGNAL_ENABLE3 = drv_generic_parport_wire_data ("ENABLE3", "GND")) == 0xff) return -1; - if ((SIGNAL_ENABLE4 = drv_generic_parport_wire_data ("ENABLE4", "GND")) == 0xff) return -1; + drv_generic_parport_data(0); } - /* backlight and GPO are always control signals */ - if ((SIGNAL_BACKLIGHT = drv_generic_parport_wire_ctrl ("BACKLIGHT", "GND")) == 0xff) return -1; - if ((SIGNAL_GPO = drv_generic_parport_wire_ctrl ("GPO", "GND")) == 0xff) return -1; - } - - /* clear capabilities if corresponding signal is GND */ - if (SIGNAL_BACKLIGHT == 0) { - Capabilities &= ~CAP_BACKLIGHT; - } - if (SIGNAL_GPO == 0) { - Capabilities &= ~CAP_GPO; - } - - - /* clear all signals */ - if (Bits == 8) { - drv_generic_parport_control (SIGNAL_RS | - SIGNAL_RW | - SIGNAL_ENABLE | - SIGNAL_ENABLE2 | - SIGNAL_ENABLE3 | - SIGNAL_ENABLE4 | - SIGNAL_BACKLIGHT | - SIGNAL_GPO, 0); - } else { - drv_generic_parport_data (0); - } - - /* set direction: write */ - drv_generic_parport_direction (0); - - /* initialize *all* controllers */ - if (Bits == 8) { - drv_HD_PP_command (allControllers, 0x30, T_INIT1); /* 8 Bit mode, wait 4.1 ms */ - drv_HD_PP_command (allControllers, 0x30, T_INIT2); /* 8 Bit mode, wait 100 us */ - drv_HD_PP_command (allControllers, 0x38, T_EXEC); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ - } else { - drv_HD_PP_nibble (allControllers, 0x03); udelay (T_INIT1); /* 4 Bit mode, wait 4.1 ms */ - drv_HD_PP_nibble (allControllers, 0x03); udelay (T_INIT2); /* 4 Bit mode, wait 100 us */ - drv_HD_PP_nibble (allControllers, 0x03); udelay (T_INIT1); /* 4 Bit mode, wait 4.1 ms */ - drv_HD_PP_nibble (allControllers, 0x02); udelay (T_INIT2); /* 4 Bit mode, wait 100 us */ - drv_HD_PP_command (allControllers, 0x28, T_EXEC); /* 4 Bit mode, 1/16 duty cycle, 5x8 font */ - } - - /* maybe use busy-flag from now on */ - /* (we can't use the busy flag during the init sequence) */ - cfg_number(section, "UseBusy", 0, 0, 1, &UseBusy); - - /* make sure we don't use the busy flag with RW wired to GND */ - if (UseBusy && !SIGNAL_RW) { - error("%s: busy-flag checking is impossible with RW wired to GND!", Name); - UseBusy=0; - } - - /* make sure the display supports busy-flag checking in 4-Bit-Mode */ - /* at the moment this is inly possible with Martin Hejl's gpio driver, */ - /* which allows to use 4 bits as input and 4 bits as output */ - if (UseBusy && Bits == 4 && !(Capabilities & CAP_BUSY4BIT)) { - error("%s: Model '%s' does not support busy-flag checking in 4 bit mode", Name, Models[Model].name); - UseBusy=0; - } - - info("%s: %susing busy-flag checking", Name, UseBusy ? "" : "not "); - - /* The LCM-162 should really use BusyFlag checking */ - if (!UseBusy && (Capabilities & CAP_LCM162)) { - error("%s: Model '%s' should definitely use busy-flag checking!", Name, Models[Model].name); - } - - return 0; + + /* set direction: write */ + drv_generic_parport_direction(0); + + /* initialize *all* controllers */ + if (Bits == 8) { + drv_HD_PP_command(allControllers, 0x30, T_INIT1); /* 8 Bit mode, wait 4.1 ms */ + drv_HD_PP_command(allControllers, 0x30, T_INIT2); /* 8 Bit mode, wait 100 us */ + drv_HD_PP_command(allControllers, 0x38, T_EXEC); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ + } else { + drv_HD_PP_nibble(allControllers, 0x03); + udelay(T_INIT1); /* 4 Bit mode, wait 4.1 ms */ + drv_HD_PP_nibble(allControllers, 0x03); + udelay(T_INIT2); /* 4 Bit mode, wait 100 us */ + drv_HD_PP_nibble(allControllers, 0x03); + udelay(T_INIT1); /* 4 Bit mode, wait 4.1 ms */ + drv_HD_PP_nibble(allControllers, 0x02); + udelay(T_INIT2); /* 4 Bit mode, wait 100 us */ + drv_HD_PP_command(allControllers, 0x28, T_EXEC); /* 4 Bit mode, 1/16 duty cycle, 5x8 font */ + } + + /* maybe use busy-flag from now on */ + /* (we can't use the busy flag during the init sequence) */ + cfg_number(section, "UseBusy", 0, 0, 1, &UseBusy); + + /* make sure we don't use the busy flag with RW wired to GND */ + if (UseBusy && !SIGNAL_RW) { + error("%s: busy-flag checking is impossible with RW wired to GND!", Name); + UseBusy = 0; + } + + /* make sure the display supports busy-flag checking in 4-Bit-Mode */ + /* at the moment this is inly possible with Martin Hejl's gpio driver, */ + /* which allows to use 4 bits as input and 4 bits as output */ + if (UseBusy && Bits == 4 && !(Capabilities & CAP_BUSY4BIT)) { + error("%s: Model '%s' does not support busy-flag checking in 4 bit mode", Name, Models[Model].name); + UseBusy = 0; + } + + info("%s: %susing busy-flag checking", Name, UseBusy ? "" : "not "); + + /* The LCM-162 should really use BusyFlag checking */ + if (!UseBusy && (Capabilities & CAP_LCM162)) { + error("%s: Model '%s' should definitely use busy-flag checking!", Name, Models[Model].name); + } + + return 0; } -static void drv_HD_PP_stop (void) +static void drv_HD_PP_stop(void) { - /* clear all signals */ - if (Bits == 8) { - drv_generic_parport_control (SIGNAL_RS | - SIGNAL_RW | - SIGNAL_ENABLE | - SIGNAL_ENABLE2 | - SIGNAL_ENABLE3 | - SIGNAL_ENABLE4 | - SIGNAL_BACKLIGHT | - SIGNAL_GPO, 0); - } else { - drv_generic_parport_data (0); - } - - drv_generic_parport_close(); - + /* clear all signals */ + if (Bits == 8) { + drv_generic_parport_control(SIGNAL_RS | + SIGNAL_RW | SIGNAL_ENABLE | SIGNAL_ENABLE2 | SIGNAL_ENABLE3 | SIGNAL_ENABLE4 | SIGNAL_BACKLIGHT | SIGNAL_GPO, 0); + } else { + drv_generic_parport_data(0); + } + + drv_generic_parport_close(); + } @@ -786,491 +826,518 @@ static void drv_HD_PP_stop (void) #ifdef WITH_I2C -static void drv_HD_I2C_nibble (unsigned char controller, unsigned char nibble) +static void drv_HD_I2C_nibble(unsigned char controller, unsigned char nibble) { - unsigned char enable; - - /* enable signal: 'controller' is a bitmask */ - /* bit n .. send to controller #n */ - /* so we can send a byte to more controllers at the same time! */ - enable = 0; - if (controller & 0x01) enable |= SIGNAL_ENABLE; - if (controller & 0x02) enable |= SIGNAL_ENABLE2; - if (controller & 0x04) enable |= SIGNAL_ENABLE3; - if (controller & 0x08) enable |= SIGNAL_ENABLE4; - - - /* clear ENABLE */ - /* put data on DB1..DB4 */ - /* nibble already contains RS bit! */ - drv_generic_i2c_data(nibble); - - /* Address set-up time */ - ndelay(T_AS); - - /* rise ENABLE */ - drv_generic_i2c_data(nibble | enable); - - /* Enable pulse width */ - ndelay(T_PW); - - /* lower ENABLE */ - drv_generic_i2c_data(nibble); - - /* Address hold time */ - ndelay(T_H); -} + unsigned char enable; + + /* enable signal: 'controller' is a bitmask */ + /* bit n .. send to controller #n */ + /* so we can send a byte to more controllers at the same time! */ + enable = 0; + if (controller & 0x01) + enable |= SIGNAL_ENABLE; + if (controller & 0x02) + enable |= SIGNAL_ENABLE2; + if (controller & 0x04) + enable |= SIGNAL_ENABLE3; + if (controller & 0x08) + enable |= SIGNAL_ENABLE4; + + + /* clear ENABLE */ + /* put data on DB1..DB4 */ + /* nibble already contains RS bit! */ + drv_generic_i2c_data(nibble); + /* Address set-up time */ + ndelay(T_AS); -static void drv_HD_I2C_byte (const unsigned char controller, const unsigned char data) -{ - /* send data with RS disabled */ - /* send high nibble of the data */ - drv_HD_I2C_nibble (controller, ((data>>4)&0x0f)|SIGNAL_RS); - - /* Make sure we honour T_CYCLE */ - ndelay(T_CYCLE-T_AS-T_PW); - - /* send low nibble of the data */ - drv_HD_I2C_nibble(controller, (data&0x0f)|SIGNAL_RS); + /* rise ENABLE */ + drv_generic_i2c_data(nibble | enable); + + /* Enable pulse width */ + ndelay(T_PW); + + /* lower ENABLE */ + drv_generic_i2c_data(nibble); + + /* Address hold time */ + ndelay(T_H); } -static void drv_HD_I2C_command (const unsigned char controller, const unsigned char cmd, const int delay) +static void drv_HD_I2C_byte(const unsigned char controller, const unsigned char data) { - /* send data with RS disabled */ - drv_HD_I2C_nibble (controller, ((cmd>>4)&0x0f)); - - ndelay(T_CYCLE-T_AS-T_PW); - - drv_HD_I2C_nibble (controller, ((cmd)&0x0f)); - - /* wait for command completion */ - udelay(delay); + /* send data with RS disabled */ + /* send high nibble of the data */ + drv_HD_I2C_nibble(controller, ((data >> 4) & 0x0f) | SIGNAL_RS); + + /* Make sure we honour T_CYCLE */ + ndelay(T_CYCLE - T_AS - T_PW); + + /* send low nibble of the data */ + drv_HD_I2C_nibble(controller, (data & 0x0f) | SIGNAL_RS); } -static void drv_HD_I2C_data (const unsigned char controller, const char *string, const int len, const int delay) + +static void drv_HD_I2C_command(const unsigned char controller, const unsigned char cmd, const int delay) { - int l = len; - - /* sanity check */ - if (len<=0) return; - - while (l--) { - /* send data with RS enabled */ - drv_HD_I2C_byte (controller, *(string++)); - + /* send data with RS disabled */ + drv_HD_I2C_nibble(controller, ((cmd >> 4) & 0x0f)); + + ndelay(T_CYCLE - T_AS - T_PW); + + drv_HD_I2C_nibble(controller, ((cmd) & 0x0f)); + /* wait for command completion */ udelay(delay); - } +} + +static void drv_HD_I2C_data(const unsigned char controller, const char *string, const int len, const int delay) +{ + int l = len; + + /* sanity check */ + if (len <= 0) + return; + + while (l--) { + /* send data with RS enabled */ + drv_HD_I2C_byte(controller, *(string++)); + + /* wait for command completion */ + udelay(delay); + } } -static int drv_HD_I2C_load (const char *section) +static int drv_HD_I2C_load(const char *section) { - if (cfg_number(section, "Bits", 8, 4, 8, &Bits)<0) return -1; - if (Bits!=4) { - error ("%s: bad %s.Bits '%d' from %s, should be '4'", Name, section, Bits, cfg_source()); - return -1; - } - - info ("%s: using %d bit mode", Name, Bits); - - if (drv_generic_i2c_open(section, Name) != 0) { - error ("%s: could not initialize i2c attached device!", Name); - return -1; - } - - if ((SIGNAL_RS = drv_generic_i2c_wire ("RS", "DB4"))==0xff) return -1; - if ((SIGNAL_RW = drv_generic_i2c_wire ("RW", "DB5"))==0xff) return -1; - if ((SIGNAL_ENABLE = drv_generic_i2c_wire ("ENABLE", "DB6"))==0xff) return -1; - if ((SIGNAL_ENABLE2 = drv_generic_i2c_wire ("ENABLE2", "GND"))==0xff) return -1; - if ((SIGNAL_GPO = drv_generic_i2c_wire ("GPO", "GND"))==0xff) return -1; - - /* initialize display */ - drv_HD_I2C_nibble (allControllers, 0x03); udelay(T_INIT1); /* 4 Bit mode, wait 4.1 ms */ - drv_HD_I2C_nibble (allControllers, 0x03); udelay(T_INIT2); /* 4 Bit mode, wait 100 us */ - drv_HD_I2C_nibble (allControllers, 0x03); udelay(T_INIT2); /* 4 Bit mode, wait 4.1 ms */ - drv_HD_I2C_nibble (allControllers, 0x02); udelay(T_INIT2); /* 4 Bit mode, wait 100 us */ - drv_HD_I2C_command (allControllers, 0x28, T_EXEC); /* 4 Bit mode, 1/16 duty cycle, 5x8 font */ - - info("%s: I2C initialization done", Name); - - return 0; + if (cfg_number(section, "Bits", 8, 4, 8, &Bits) < 0) + return -1; + if (Bits != 4) { + error("%s: bad %s.Bits '%d' from %s, should be '4'", Name, section, Bits, cfg_source()); + return -1; + } + + info("%s: using %d bit mode", Name, Bits); + + if (drv_generic_i2c_open(section, Name) != 0) { + error("%s: could not initialize i2c attached device!", Name); + return -1; + } + + if ((SIGNAL_RS = drv_generic_i2c_wire("RS", "DB4")) == 0xff) + return -1; + if ((SIGNAL_RW = drv_generic_i2c_wire("RW", "DB5")) == 0xff) + return -1; + if ((SIGNAL_ENABLE = drv_generic_i2c_wire("ENABLE", "DB6")) == 0xff) + return -1; + if ((SIGNAL_ENABLE2 = drv_generic_i2c_wire("ENABLE2", "GND")) == 0xff) + return -1; + if ((SIGNAL_GPO = drv_generic_i2c_wire("GPO", "GND")) == 0xff) + return -1; + + /* initialize display */ + drv_HD_I2C_nibble(allControllers, 0x03); + udelay(T_INIT1); /* 4 Bit mode, wait 4.1 ms */ + drv_HD_I2C_nibble(allControllers, 0x03); + udelay(T_INIT2); /* 4 Bit mode, wait 100 us */ + drv_HD_I2C_nibble(allControllers, 0x03); + udelay(T_INIT2); /* 4 Bit mode, wait 4.1 ms */ + drv_HD_I2C_nibble(allControllers, 0x02); + udelay(T_INIT2); /* 4 Bit mode, wait 100 us */ + drv_HD_I2C_command(allControllers, 0x28, T_EXEC); /* 4 Bit mode, 1/16 duty cycle, 5x8 font */ + + info("%s: I2C initialization done", Name); + + return 0; } -static void drv_HD_I2C_stop (void) +static void drv_HD_I2C_stop(void) { - /* clear all signals */ - drv_generic_i2c_data (0); - - /* close port */ - drv_generic_i2c_close(); + /* clear all signals */ + drv_generic_i2c_data(0); + + /* close port */ + drv_generic_i2c_close(); } -#endif /* WITH_I2C */ +#endif /* WITH_I2C */ /****************************************/ /*** display dependant functions ***/ /****************************************/ -static void drv_HD_clear (void) +static void drv_HD_clear(void) { - drv_HD_command (allControllers, 0x01, T_CLEAR); /* clear *all* displays */ + drv_HD_command(allControllers, 0x01, T_CLEAR); /* clear *all* displays */ } -static int drv_HD_goto (int row, int col) +static int drv_HD_goto(int row, int col) { - int pos, controller; - - /* handle multiple controllers */ - for (pos = 0, controller = 0; controller < numControllers; controller++) { - pos += CROWS[controller]; - if (row < pos) { - currController = (1 << controller); - break; + int pos, controller; + + /* handle multiple controllers */ + for (pos = 0, controller = 0; controller < numControllers; controller++) { + pos += CROWS[controller]; + if (row < pos) { + currController = (1 << controller); + break; + } + row -= CROWS[controller]; + } + + /* column outside of current display's width */ + if (col >= CCOLS[controller]) + return -1; + + if (0) { + debug("goto: [%d,%d] mask=%d, controller=%d, size:%dx%d", row, col, currController, controller, CROWS[controller], CCOLS[controller]); + } + + /* 16x1 Displays are organized as 8x2 :-( */ + if (CCOLS[controller] == 16 && CROWS[controller] == 1 && col > 7) { + row++; + col -= 8; } - row -= CROWS[controller]; - } - - /* column outside of current display's width */ - if (col >= CCOLS[controller]) return -1; - - if (0) { - debug ("goto: [%d,%d] mask=%d, controller=%d, size:%dx%d", - row, col, currController, controller, CROWS[controller], CCOLS[controller]); - } - - /* 16x1 Displays are organized as 8x2 :-( */ - if (CCOLS[controller] == 16 && CROWS[controller] == 1 && col > 7) { - row++; - col -= 8; - } - - if (Capabilities & CAP_HD66712) { - /* the HD66712 doesn't have a braindamadged RAM layout */ - pos = row*32 + col; - } else { - /* 16x4 Controllers use a slightly different layout */ - if (CCOLS[controller] == 16 && CROWS[controller] == 4) { - pos = (row%2)*64 + (row/2)*16 + col; + + if (Capabilities & CAP_HD66712) { + /* the HD66712 doesn't have a braindamadged RAM layout */ + pos = row * 32 + col; } else { - pos = (row%2)*64 + (row/2)*20 + col; + /* 16x4 Controllers use a slightly different layout */ + if (CCOLS[controller] == 16 && CROWS[controller] == 4) { + pos = (row % 2) * 64 + (row / 2) * 16 + col; + } else { + pos = (row % 2) * 64 + (row / 2) * 20 + col; + } } - } - drv_HD_command (currController, (0x80 | pos), T_EXEC); + drv_HD_command(currController, (0x80 | pos), T_EXEC); + + /* return columns left on current display */ + return CCOLS[controller] - col; - /* return columns left on current display */ - return CCOLS[controller] - col; - } -static void drv_HD_write (const int row, const int col, const char *data, const int len) +static void drv_HD_write(const int row, const int col, const char *data, const int len) { - int space = drv_HD_goto (row, col); - if (space > 0) { - drv_HD_data (currController, data, len > space ? space : len, T_EXEC); - } + int space = drv_HD_goto(row, col); + if (space > 0) { + drv_HD_data(currController, data, len > space ? space : len, T_EXEC); + } } -static void drv_HD_defchar (const int ascii, const unsigned char *matrix) +static void drv_HD_defchar(const int ascii, const unsigned char *matrix) { - int i; - char buffer[8]; - - for (i = 0; i < 8; i++) { - buffer[i] = matrix[i] & 0x1f; - } - - /* define chars on *all* controllers! */ - drv_HD_command (allControllers, 0x40 | 8 * ascii, T_EXEC); - drv_HD_data (allControllers, buffer, 8, T_WRCG); + int i; + char buffer[8]; + + for (i = 0; i < 8; i++) { + buffer[i] = matrix[i] & 0x1f; + } + + /* define chars on *all* controllers! */ + drv_HD_command(allControllers, 0x40 | 8 * ascii, T_EXEC); + drv_HD_data(allControllers, buffer, 8, T_WRCG); } -static int drv_HD_backlight (int backlight) +static int drv_HD_backlight(int backlight) { - if (!(Capabilities & CAP_BACKLIGHT)) return -1; - - if (backlight < 0) backlight = 0; - if (backlight > 1) backlight = 1; - - drv_generic_parport_control (SIGNAL_BACKLIGHT, backlight ? SIGNAL_BACKLIGHT : 0); - - return backlight; + if (!(Capabilities & CAP_BACKLIGHT)) + return -1; + + if (backlight < 0) + backlight = 0; + if (backlight > 1) + backlight = 1; + + drv_generic_parport_control(SIGNAL_BACKLIGHT, backlight ? SIGNAL_BACKLIGHT : 0); + + return backlight; } -static int drv_HD_brightness (int brightness) +static int drv_HD_brightness(int brightness) { - char cmd; - - if (!(Capabilities & CAP_BRIGHTNESS)) return -1; - - if (brightness < 0) brightness = 0; - if (brightness > 3) brightness = 3; - - cmd='0'+brightness; - - drv_HD_command (allControllers, 0x38, T_EXEC); /* enable function */ - drv_HD_data (allControllers, &cmd, 1, T_WRCG); /* set brightness */ - - return brightness; + char cmd; + + if (!(Capabilities & CAP_BRIGHTNESS)) + return -1; + + if (brightness < 0) + brightness = 0; + if (brightness > 3) + brightness = 3; + + cmd = '0' + brightness; + + drv_HD_command(allControllers, 0x38, T_EXEC); /* enable function */ + drv_HD_data(allControllers, &cmd, 1, T_WRCG); /* set brightness */ + + return brightness; } /* Fixme: GPO's */ #if 0 -static void drv_HD_setGPO (const int bits) +static void drv_HD_setGPO(const int bits) { - if (Lcd.gpos > 0) { - - /* put data on DB1..DB8 */ - drv_generic_parport_data (bits); - - /* 74HCT573 set-up time */ - ndelay(20); - - /* send data */ - /* 74HCT573 enable pulse width = 24ns */ - drv_generic_parport_toggle (SIGNAL_GPO, 1, 230); - } + if (Lcd.gpos > 0) { + + /* put data on DB1..DB8 */ + drv_generic_parport_data(bits); + + /* 74HCT573 set-up time */ + ndelay(20); + + /* send data */ + /* 74HCT573 enable pulse width = 24ns */ + drv_generic_parport_toggle(SIGNAL_GPO, 1, 230); + } } #endif -static void drv_HD_LCM162_timer (void __attribute__((unused)) *notused) +static void drv_HD_LCM162_timer(void __attribute__ ((unused)) * notused) { - static unsigned char data = 0x00; - - /* Bit 3+5 : key number */ - /* Bit 6 : key press/release */ - unsigned char mask3 = 1<<3; - unsigned char mask5 = 1<<5; - unsigned char mask6 = 1<<6; - unsigned char mask = mask3 | mask5 | mask6; - - int keynum; - int updown; - - unsigned char temp; - - temp = drv_generic_parport_status() & mask; - - if (data != temp) { - data = temp; - - keynum = (data & mask3 ? 1 : 0) + (data & mask5 ? 2 : 0); - updown = (data & mask6 ? 1 : 0); - - debug ("key %d press %d", keynum, updown); - } + static unsigned char data = 0x00; + + /* Bit 3+5 : key number */ + /* Bit 6 : key press/release */ + unsigned char mask3 = 1 << 3; + unsigned char mask5 = 1 << 5; + unsigned char mask6 = 1 << 6; + unsigned char mask = mask3 | mask5 | mask6; + + int keynum; + int updown; + + unsigned char temp; + + temp = drv_generic_parport_status() & mask; + + if (data != temp) { + data = temp; + + keynum = (data & mask3 ? 1 : 0) + (data & mask5 ? 2 : 0); + updown = (data & mask6 ? 1 : 0); + + debug("key %d press %d", keynum, updown); + } } -static int drv_HD_start (const char *section, const int quiet) +static int drv_HD_start(const char *section, const int quiet) { - char *model, *size, *bus; - int rows = -1, cols = -1, gpos = -1, i; - int size_defined = 0; - int size_missing = 0; - - model=cfg_get(section, "Model", "generic"); - if (model != NULL && *model != '\0') { - int i; - for (i = 0; Models[i].type != 0xff; i++) { - if (strcasecmp (Models[i].name, model) == 0) break; + char *model, *size, *bus; + int rows = -1, cols = -1, gpos = -1, i; + int size_defined = 0; + int size_missing = 0; + + model = cfg_get(section, "Model", "generic"); + if (model != NULL && *model != '\0') { + int i; + for (i = 0; Models[i].type != 0xff; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; + } + if (Models[i].type == 0xff) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + Model = i; + Capabilities = Models[Model].capabilities; + info("%s: using model '%s'", Name, Models[Model].name); + } else { + error("%s: empty '%s.Model' entry from %s", Name, section, cfg_source()); + if (model) + free(model); + return -1; } - if (Models[i].type == 0xff) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; + free(model); + + bus = cfg_get(section, "Bus", "parport"); + if (bus == NULL && *bus == '\0') { + error("%s: empty '%s.Bus' entry from %s", Name, section, cfg_source()); + if (bus) + free(bus); + return -1; } - Model=i; - Capabilities=Models[Model].capabilities; - info ("%s: using model '%s'", Name, Models[Model].name); - } else { - error ("%s: empty '%s.Model' entry from %s", Name, section, cfg_source()); - if (model) free (model); - return -1; - } - free (model); - - bus=cfg_get(section, "Bus", "parport"); - if (bus == NULL && *bus == '\0') { - error ("%s: empty '%s.Bus' entry from %s", Name, section, cfg_source()); - if (bus) free (bus); - return -1; - } - - if (strcasecmp (bus, "parport") == 0) { - info ("%s: using parallel port", Name); - Bus = BUS_PP; - drv_HD_load = drv_HD_PP_load; - drv_HD_command = drv_HD_PP_command; - drv_HD_data = drv_HD_PP_data; - drv_HD_stop = drv_HD_PP_stop; - - } else if (strcasecmp(bus, "i2c") == 0) { + + if (strcasecmp(bus, "parport") == 0) { + info("%s: using parallel port", Name); + Bus = BUS_PP; + drv_HD_load = drv_HD_PP_load; + drv_HD_command = drv_HD_PP_command; + drv_HD_data = drv_HD_PP_data; + drv_HD_stop = drv_HD_PP_stop; + + } else if (strcasecmp(bus, "i2c") == 0) { #ifdef WITH_I2C - info ("%s: using I2C bus", Name); - Bus = BUS_I2C; - drv_HD_load = drv_HD_I2C_load; - drv_HD_command = drv_HD_I2C_command; - drv_HD_data = drv_HD_I2C_data; - drv_HD_stop = drv_HD_I2C_stop; + info("%s: using I2C bus", Name); + Bus = BUS_I2C; + drv_HD_load = drv_HD_I2C_load; + drv_HD_command = drv_HD_I2C_command; + drv_HD_data = drv_HD_I2C_data; + drv_HD_stop = drv_HD_I2C_stop; #else - error ("%s: %s.Bus '%s' from %s not available:", Name, section, bus, cfg_source()); - error ("%s: lcd4linux was compiled without i2c support!", Name); - free (bus); - return -1; + error("%s: %s.Bus '%s' from %s not available:", Name, section, bus, cfg_source()); + error("%s: lcd4linux was compiled without i2c support!", Name); + free(bus); + return -1; #endif - - } else { - error ("%s: bad %s.Bus '%s' from %s, should be 'parport' or 'i2c'", Name, section, bus, cfg_source()); - free (bus); - return -1; - } - - /* sanity check: Model can use bus */ - if (!(Capabilities & Bus)) { - error ("%s: Model '%s' cannot be used on the %s bus!", Name, Models[Model].name, bus); - free (bus); - return -1; - } - free(bus); - - if (cfg_number (section, "Controllers", 1, 1, 4, (int *) &numControllers) < 0) return -1; - info ("%s: using %d Controller(s)", Name, numControllers); - - /* current Controller */ - currController = 1; - - /* Bitmask for *all* Controllers */ - allControllers = (1 << numControllers) - 1; - - - DCOLS = 0; - DROWS = 0; - - for (i = 0; i < numControllers; i++) { - char key[6]; - qprintf(key, sizeof(key), "Size%d", i+1); - size = cfg_get (section, key, NULL); - if (size == NULL || *size == '\0') { - size_missing++; - free(size); - continue; + + } else { + error("%s: bad %s.Bus '%s' from %s, should be 'parport' or 'i2c'", Name, section, bus, cfg_source()); + free(bus); + return -1; + } + + /* sanity check: Model can use bus */ + if (!(Capabilities & Bus)) { + error("%s: Model '%s' cannot be used on the %s bus!", Name, Models[Model].name, bus); + free(bus); + return -1; + } + free(bus); + + if (cfg_number(section, "Controllers", 1, 1, 4, (int *) &numControllers) < 0) + return -1; + info("%s: using %d Controller(s)", Name, numControllers); + + /* current Controller */ + currController = 1; + + /* Bitmask for *all* Controllers */ + allControllers = (1 << numControllers) - 1; + + + DCOLS = 0; + DROWS = 0; + + for (i = 0; i < numControllers; i++) { + char key[6]; + qprintf(key, sizeof(key), "Size%d", i + 1); + size = cfg_get(section, key, NULL); + if (size == NULL || *size == '\0') { + size_missing++; + free(size); + continue; + } + if (sscanf(size, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad %s.%s '%s' from %s", Name, section, key, size, cfg_source()); + free(size); + return -1; + } + free(size); + CCOLS[i] = cols; + CROWS[i] = rows; + size_defined++; + info("%s: Controller %d: %dx%d", Name, i + 1, cols, rows); + /* grow the size */ + if (cols > DCOLS) + DCOLS = cols; + DROWS += rows; } - if (sscanf (size, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { - error ("%s: bad %s.%s '%s' from %s", Name, section, key, size, cfg_source ()); - free(size); - return -1; + if (size_defined && size_missing) { + error("%s: bad %s.Size* definition in %s:", Name, section, cfg_source()); + error("%s: either you specify the size for *all* controllers or for none.", Name); + return -1; + } + + size = cfg_get(section, "Size", NULL); + if (size != NULL && *size != '\0') { + if (sscanf(size, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, size, cfg_source()); + free(size); + return -1; + } + if (DCOLS == 0 && DROWS == 0) { + for (i = 0; i < numControllers; i++) { + CCOLS[i] = cols; + CROWS[i] = rows / numControllers; + DCOLS = CCOLS[i]; + DROWS += CROWS[i]; + } + } + if (rows != DROWS || cols != DCOLS) { + error("%s: bad %s.Size definition in %s:", Name, section, cfg_source()); + error("%s: Size %dx%d should be %dx%d", Name, cols, rows, DCOLS, DROWS); + return -1; + } } free(size); - CCOLS[i] = cols; - CROWS[i] = rows; - size_defined++; - info ("%s: Controller %d: %dx%d", Name, i+1, cols, rows); - /* grow the size */ - if (cols > DCOLS) DCOLS = cols; - DROWS += rows; - } - if (size_defined && size_missing) { - error ("%s: bad %s.Size* definition in %s:", Name, section, cfg_source()); - error ("%s: either you specify the size for *all* controllers or for none.", Name); - return -1; - } - - size = cfg_get(section, "Size", NULL); - if (size != NULL && *size != '\0') { - if (sscanf(size,"%dx%d",&cols,&rows)!=2 || rows<1 || cols<1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, size, cfg_source()); - free(size); - return -1; + + + if (cfg_number(section, "GPOs", 0, 0, 8, &gpos) < 0) + return -1; + if (gpos > 0 && !(Capabilities & CAP_GPO)) { + error("%s: Model '%s' does not support GPO's!", Name, Models[Model].name); + gpos = 0; } - if (DCOLS == 0 && DROWS == 0) { - for (i = 0; i < numControllers; i++) { - CCOLS[i] = cols; - CROWS[i] = rows/numControllers; - DCOLS = CCOLS[i]; - DROWS += CROWS[i]; - } + GPOS = gpos; + if (gpos > 0) { + info("%s: using %d GPO's", Name, GPOS); } - if (rows != DROWS || cols != DCOLS) { - error ("%s: bad %s.Size definition in %s:", Name, section, cfg_source()); - error ("%s: Size %dx%d should be %dx%d", Name, cols, rows, DCOLS, DROWS); - return -1; + + if (drv_HD_load(section) < 0) { + error("%s: start display failed!", Name); + return -1; } - } - free(size); - - - if (cfg_number (section, "GPOs", 0, 0, 8, &gpos) < 0) return -1; - if (gpos > 0 && !(Capabilities & CAP_GPO)) { - error ("%s: Model '%s' does not support GPO's!", Name, Models[Model].name); - gpos = 0; - } - GPOS = gpos; - if (gpos > 0) { - info ("%s: using %d GPO's", Name, GPOS); - } - - if (drv_HD_load (section) < 0) { - error ("%s: start display failed!", Name); - return -1; - } - - drv_HD_command (allControllers, 0x08, T_EXEC); /* Controller off, cursor off, blink off */ - drv_HD_command (allControllers, 0x0c, T_CLEAR); /* Display on, cursor off, blink off, wait 1.64 ms */ - drv_HD_command (allControllers, 0x06, T_EXEC); /* curser moves to right, no shift */ - - if ((Capabilities & CAP_HD66712) && DROWS > 2) { - drv_HD_command (allControllers, Bits == 8 ? 0x3c : 0x2c, T_EXEC); /* set extended register enable bit */ - drv_HD_command (allControllers, 0x09, T_EXEC); /* set 4-line mode */ - drv_HD_command (allControllers, Bits == 8 ? 0x38 : 0x28, T_EXEC); /* clear extended register enable bit */ - } - - drv_HD_clear(); /* clear *all* displays */ - drv_HD_command (allControllers, 0x03, T_CLEAR); /* return home */ - - /* maybe set backlight */ - if (Capabilities & CAP_BACKLIGHT) { - int backlight; - if (cfg_number (section, "Backlight", 0, 0, 1, &backlight) > 0) { - info ("%s: backlight %s", Name, backlight ? "enabled" : "disabled"); - drv_HD_backlight(backlight); + + drv_HD_command(allControllers, 0x08, T_EXEC); /* Controller off, cursor off, blink off */ + drv_HD_command(allControllers, 0x0c, T_CLEAR); /* Display on, cursor off, blink off, wait 1.64 ms */ + drv_HD_command(allControllers, 0x06, T_EXEC); /* curser moves to right, no shift */ + + if ((Capabilities & CAP_HD66712) && DROWS > 2) { + drv_HD_command(allControllers, Bits == 8 ? 0x3c : 0x2c, T_EXEC); /* set extended register enable bit */ + drv_HD_command(allControllers, 0x09, T_EXEC); /* set 4-line mode */ + drv_HD_command(allControllers, Bits == 8 ? 0x38 : 0x28, T_EXEC); /* clear extended register enable bit */ } - } - - /* maybe set brightness */ - if (Capabilities & CAP_BRIGHTNESS) { - int brightness; - if (cfg_number (section, "Brightness", 0, 0, 3, &brightness) > 0) { - info ("%s: brightness level %d", Name, brightness); - drv_HD_brightness(brightness); + + drv_HD_clear(); /* clear *all* displays */ + drv_HD_command(allControllers, 0x03, T_CLEAR); /* return home */ + + /* maybe set backlight */ + if (Capabilities & CAP_BACKLIGHT) { + int backlight; + if (cfg_number(section, "Backlight", 0, 0, 1, &backlight) > 0) { + info("%s: backlight %s", Name, backlight ? "enabled" : "disabled"); + drv_HD_backlight(backlight); + } } - } - - /* install keypad polling timer for LCM-162 */ - if (Capabilities & CAP_LCM162) { - timer_add (drv_HD_LCM162_timer, NULL, 10, 0); - } - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, NULL)) { - sleep (3); - drv_HD_clear(); + + /* maybe set brightness */ + if (Capabilities & CAP_BRIGHTNESS) { + int brightness; + if (cfg_number(section, "Brightness", 0, 0, 3, &brightness) > 0) { + info("%s: brightness level %d", Name, brightness); + drv_HD_brightness(brightness); + } } - } - - return 0; + + /* install keypad polling timer for LCM-162 */ + if (Capabilities & CAP_LCM162) { + timer_add(drv_HD_LCM162_timer, NULL, 10, 0); + } + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, NULL)) { + sleep(3); + drv_HD_clear(); + } + } + + return 0; } @@ -1279,21 +1346,21 @@ static int drv_HD_start (const char *section, const int quiet) /****************************************/ -static void plugin_backlight (RESULT *result, RESULT *arg1) +static void plugin_backlight(RESULT * result, RESULT * arg1) { - double backlight; - - backlight = drv_HD_backlight(R2N(arg1)); - SetResult(&result, R_NUMBER, &backlight); + double backlight; + + backlight = drv_HD_backlight(R2N(arg1)); + SetResult(&result, R_NUMBER, &backlight); } -static void plugin_brightness (RESULT *result, RESULT *arg1) +static void plugin_brightness(RESULT * result, RESULT * arg1) { - double brightness; - - brightness = drv_HD_brightness(R2N(arg1)); - SetResult(&result, R_NUMBER, &brightness); + double brightness; + + brightness = drv_HD_brightness(R2N(arg1)); + SetResult(&result, R_NUMBER, &brightness); } @@ -1313,113 +1380,113 @@ static void plugin_brightness (RESULT *result, RESULT *arg1) /* list models */ -int drv_HD_list (void) +int drv_HD_list(void) { - int i; - - for (i = 0; Models[i].type != 0xff; i++) { - printf ("%s ", Models[i].name); - } - return 0; + int i; + + for (i = 0; Models[i].type != 0xff; i++) { + printf("%s ", Models[i].name); + } + return 0; } /* initialize driver & display */ -int drv_HD_init (const char *section, const int quiet) +int drv_HD_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int asc255bug; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = 1; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_HD_write; - drv_generic_text_real_defchar = drv_HD_defchar; - - - /* start display */ - if ((ret=drv_HD_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - /* most displays have a full block on ascii 255, but some have kind of */ - /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ - /* char will not be used, but rendered by the bar driver */ - cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - if (!asc255bug) - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - if (Capabilities & CAP_BACKLIGHT) { - AddFunction ("LCD::backlight", 1, plugin_backlight); - } - if (Capabilities & CAP_BRIGHTNESS) { - AddFunction ("LCD::brightness", 1, plugin_brightness); - } - - return 0; + WIDGET_CLASS wc; + int asc255bug; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 1; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_HD_write; + drv_generic_text_real_defchar = drv_HD_defchar; + + + /* start display */ + if ((ret = drv_HD_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + /* most displays have a full block on ascii 255, but some have kind of */ + /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ + /* char will not be used, but rendered by the bar driver */ + cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + if (!asc255bug) + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + if (Capabilities & CAP_BACKLIGHT) { + AddFunction("LCD::backlight", 1, plugin_backlight); + } + if (Capabilities & CAP_BRIGHTNESS) { + AddFunction("LCD::brightness", 1, plugin_brightness); + } + + return 0; } /* close driver & display */ -int drv_HD_quit (const int quiet) +int drv_HD_quit(const int quiet) { - - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_HD_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - drv_HD_stop(); - - return (0); + + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_HD_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + drv_HD_stop(); + + return (0); } DRIVER drv_HD44780 = { - name: Name, - list: drv_HD_list, - init: drv_HD_init, - quit: drv_HD_quit, + name:Name, + list:drv_HD_list, + init:drv_HD_init, + quit:drv_HD_quit, }; diff --git a/drv_Image.c b/drv_Image.c index 196ead1..de4e3e7 100644 --- a/drv_Image.c +++ b/drv_Image.c @@ -1,4 +1,4 @@ -/* $Id: drv_Image.c,v 1.11 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: drv_Image.c,v 1.12 2005/05/08 04:32:44 reinelt Exp $ * * new style Image (PPM/PNG) Driver for LCD4Linux * @@ -23,6 +23,9 @@ * * * $Log: drv_Image.c,v $ + * Revision 1.12 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.11 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -120,19 +123,19 @@ #include <dmalloc.h> #endif -static char Name[]="Image"; +static char Name[] = "Image"; -static enum {PPM, PNG} Format; +static enum { PPM, PNG } Format; static unsigned int fg_col, bg_col, hg_col; -static int pixel = -1; /* pointsize in pixel */ -static int pgap = 0; /* gap between points */ -static int rgap = 0; /* row gap between lines */ -static int cgap = 0; /* column gap between characters */ -static int border = 0; /* window border */ +static int pixel = -1; /* pointsize in pixel */ +static int pgap = 0; /* gap between points */ +static int rgap = 0; /* row gap between lines */ +static int cgap = 0; /* column gap between characters */ +static int border = 0; /* window border */ -static int dimx, dimy; /* total window dimension in pixel */ +static int dimx, dimy; /* total window dimension in pixel */ static unsigned char *drv_IMG_FB = NULL; @@ -143,336 +146,330 @@ static int dirty = 1; /****************************************/ #ifdef WITH_PPM -static int drv_IMG_flush_PPM (void) +static int drv_IMG_flush_PPM(void) { - static int seq=0; - static unsigned char *bitbuf=NULL; - static unsigned char *rowbuf=NULL; - int xsize, ysize, row, col; - unsigned char R[3], G[3], B[3]; - char path[256], tmp[256], buffer[256]; - int fd; - - xsize = 2*border + (DCOLS/XRES-1)*cgap + DCOLS*pixel + (DCOLS-1)*pgap; - ysize = 2*border + (DROWS/YRES-1)*rgap + DROWS*pixel + (DROWS-1)*pgap; - - if (bitbuf == NULL) { - if ((bitbuf = malloc(xsize*ysize*sizeof(*bitbuf))) == NULL) { - error ("%s: malloc(%d) failed: %s", Name, xsize*ysize*sizeof(*bitbuf), strerror(errno)); - return -1; + static int seq = 0; + static unsigned char *bitbuf = NULL; + static unsigned char *rowbuf = NULL; + int xsize, ysize, row, col; + unsigned char R[3], G[3], B[3]; + char path[256], tmp[256], buffer[256]; + int fd; + + xsize = 2 * border + (DCOLS / XRES - 1) * cgap + DCOLS * pixel + (DCOLS - 1) * pgap; + ysize = 2 * border + (DROWS / YRES - 1) * rgap + DROWS * pixel + (DROWS - 1) * pgap; + + if (bitbuf == NULL) { + if ((bitbuf = malloc(xsize * ysize * sizeof(*bitbuf))) == NULL) { + error("%s: malloc(%d) failed: %s", Name, xsize * ysize * sizeof(*bitbuf), strerror(errno)); + return -1; + } } - } - - if (rowbuf == NULL) { - if ((rowbuf = malloc(3*xsize*sizeof(*rowbuf))) == NULL) { - error ("Raster: malloc(%d) failed: %s", 3*xsize*sizeof(*rowbuf), strerror(errno)); - return -1; + + if (rowbuf == NULL) { + if ((rowbuf = malloc(3 * xsize * sizeof(*rowbuf))) == NULL) { + error("Raster: malloc(%d) failed: %s", 3 * xsize * sizeof(*rowbuf), strerror(errno)); + return -1; + } } - } - - memset (bitbuf, 0, xsize*ysize*sizeof(*bitbuf)); - - for (row = 0; row < DROWS; row++) { - int y = border + (row/YRES)*rgap + row*(pixel+pgap); - for (col = 0; col < DCOLS; col++) { - int x = border + (col/XRES)*cgap + col*(pixel+pgap); - int a, b; - for (a = 0; a < pixel; a++) - for (b = 0; b < pixel; b++) - bitbuf[y*xsize+x+a*xsize+b] = drv_IMG_FB[row*DCOLS+col]+1; + + memset(bitbuf, 0, xsize * ysize * sizeof(*bitbuf)); + + for (row = 0; row < DROWS; row++) { + int y = border + (row / YRES) * rgap + row * (pixel + pgap); + for (col = 0; col < DCOLS; col++) { + int x = border + (col / XRES) * cgap + col * (pixel + pgap); + int a, b; + for (a = 0; a < pixel; a++) + for (b = 0; b < pixel; b++) + bitbuf[y * xsize + x + a * xsize + b] = drv_IMG_FB[row * DCOLS + col] + 1; + } + } + + snprintf(path, sizeof(path), output, seq++); + qprintf(tmp, sizeof(tmp), "%s.tmp", path); + + /* remove the file */ + unlink(tmp); + + /* avoid symlink security hole: */ + /* open it with O_EXCL will fail if the file exists. */ + /* This should not happen because we just unlinked it. */ + if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) { + error("%s: open(%s) failed: %s", Name, tmp, strerror(errno)); + return -1; } - } - - snprintf (path, sizeof(path), output, seq++); - qprintf(tmp, sizeof(tmp), "%s.tmp", path); - - /* remove the file */ - unlink (tmp); - - /* avoid symlink security hole: */ - /* open it with O_EXCL will fail if the file exists. */ - /* This should not happen because we just unlinked it. */ - if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0644))<0) { - error ("%s: open(%s) failed: %s", Name, tmp, strerror(errno)); - return -1; - } - - qprintf(buffer, sizeof(buffer), "P6\n%d %d\n255\n", xsize, ysize); - if (write (fd, buffer, strlen(buffer)) < 0) { - error ("%s: write(%s) failed: %s", Name, tmp, strerror(errno)); - return -1; - } - - R[0] = 0xff & bg_col>>16; - G[0] = 0xff & bg_col>>8; - B[0] = 0xff & bg_col; - - R[1] = 0xff & hg_col>>16; - G[1] = 0xff & hg_col>>8; - B[1] = 0xff & hg_col; - - R[2] = 0xff & fg_col>>16; - G[2] = 0xff & fg_col>>8; - B[2] = 0xff & fg_col; - - for (row = 0; row < ysize; row++) { - int c = 0; - for (col = 0; col < xsize; col++) { - int i = bitbuf[row*xsize+col]; - rowbuf[c++] = R[i]; - rowbuf[c++] = G[i]; - rowbuf[c++] = B[i]; + + qprintf(buffer, sizeof(buffer), "P6\n%d %d\n255\n", xsize, ysize); + if (write(fd, buffer, strlen(buffer)) < 0) { + error("%s: write(%s) failed: %s", Name, tmp, strerror(errno)); + return -1; } - if (write (fd, rowbuf, c) < 0) { - error ("%s: write(%s) failed: %s", Name, tmp, strerror(errno)); - break; + + R[0] = 0xff & bg_col >> 16; + G[0] = 0xff & bg_col >> 8; + B[0] = 0xff & bg_col; + + R[1] = 0xff & hg_col >> 16; + G[1] = 0xff & hg_col >> 8; + B[1] = 0xff & hg_col; + + R[2] = 0xff & fg_col >> 16; + G[2] = 0xff & fg_col >> 8; + B[2] = 0xff & fg_col; + + for (row = 0; row < ysize; row++) { + int c = 0; + for (col = 0; col < xsize; col++) { + int i = bitbuf[row * xsize + col]; + rowbuf[c++] = R[i]; + rowbuf[c++] = G[i]; + rowbuf[c++] = B[i]; + } + if (write(fd, rowbuf, c) < 0) { + error("%s: write(%s) failed: %s", Name, tmp, strerror(errno)); + break; + } } - } - - if (close (fd) < 0) { - error ("%s: close(%s) failed: %s", Name, tmp, strerror(errno)); - return -1; - } - if (rename (tmp, path)<0) { - error ("%s: rename(%s) failed: %s", Name, tmp, strerror(errno)); - return -1; - } - - return 0; + + if (close(fd) < 0) { + error("%s: close(%s) failed: %s", Name, tmp, strerror(errno)); + return -1; + } + if (rename(tmp, path) < 0) { + error("%s: rename(%s) failed: %s", Name, tmp, strerror(errno)); + return -1; + } + + return 0; } #endif #ifdef WITH_PNG -static int drv_IMG_flush_PNG (void) +static int drv_IMG_flush_PNG(void) { - static int seq = 0; - int xsize, ysize, row, col; - char path[256], tmp[256]; - FILE *fp; int fd; - gdImagePtr im; - int bg, hg, fg; - - xsize = 2*border + (DCOLS/XRES-1)*cgap + DCOLS*pixel + (DCOLS-1)*pgap; - ysize = 2*border + (DROWS/YRES-1)*rgap + DROWS*pixel + (DROWS-1)*pgap; - - im = gdImageCreate(xsize, ysize); - - /* first color = background */ - bg = gdImageColorAllocate(im, - 0xff & bg_col>>16, - 0xff & bg_col>>8, - 0xff & bg_col); - - hg = gdImageColorAllocate(im, - 0xff & hg_col>>16, - 0xff & hg_col>>8, - 0xff & hg_col); - - - fg = gdImageColorAllocate(im, - 0xff & fg_col>>16, - 0xff & fg_col>>8, - 0xff & fg_col); - - - for (row = 0; row < DROWS; row++) { - int y = border + (row/YRES)*rgap + row*(pixel+pgap); - for (col = 0; col < DCOLS; col++) { - int x = border + (col/XRES)*cgap + col*(pixel+pgap); - gdImageFilledRectangle(im, x, y, x + pixel - 1 , y + pixel - 1, - drv_IMG_FB[row*DCOLS+col]? fg : hg); + static int seq = 0; + int xsize, ysize, row, col; + char path[256], tmp[256]; + FILE *fp; + int fd; + gdImagePtr im; + int bg, hg, fg; + + xsize = 2 * border + (DCOLS / XRES - 1) * cgap + DCOLS * pixel + (DCOLS - 1) * pgap; + ysize = 2 * border + (DROWS / YRES - 1) * rgap + DROWS * pixel + (DROWS - 1) * pgap; + + im = gdImageCreate(xsize, ysize); + + /* first color = background */ + bg = gdImageColorAllocate(im, 0xff & bg_col >> 16, 0xff & bg_col >> 8, 0xff & bg_col); + + hg = gdImageColorAllocate(im, 0xff & hg_col >> 16, 0xff & hg_col >> 8, 0xff & hg_col); + + + fg = gdImageColorAllocate(im, 0xff & fg_col >> 16, 0xff & fg_col >> 8, 0xff & fg_col); + + + for (row = 0; row < DROWS; row++) { + int y = border + (row / YRES) * rgap + row * (pixel + pgap); + for (col = 0; col < DCOLS; col++) { + int x = border + (col / XRES) * cgap + col * (pixel + pgap); + gdImageFilledRectangle(im, x, y, x + pixel - 1, y + pixel - 1, drv_IMG_FB[row * DCOLS + col] ? fg : hg); + } + } + + snprintf(path, sizeof(path), output, seq++); + qprintf(tmp, sizeof(tmp), "%s.tmp", path); + + /* remove the file */ + unlink(tmp); + + /* avoid symlink security hole: */ + /* open it with O_EXCL will fail if the file exists. */ + /* This should not happen because we just unlinked it. */ + if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) { + error("%s: open(%s) failed: %s", Name, tmp, strerror(errno)); + return -1; + } + + if ((fp = fdopen(fd, "w")) == NULL) { + error("%s: fdopen(%s) failed: %s\n", Name, tmp, strerror(errno)); + close(fd); + return -1; + } + + gdImagePng(im, fp); + gdImageDestroy(im); + + + if (fclose(fp) != 0) { + error("%s: fclose(%s) failed: %s\n", Name, tmp, strerror(errno)); + return -1; + } + + if (rename(tmp, path) < 0) { + error("%s: rename(%s) failed: %s\n", Name, tmp, strerror(errno)); + return -1; } - } - - snprintf (path, sizeof(path), output, seq++); - qprintf (tmp, sizeof(tmp), "%s.tmp", path); - - /* remove the file */ - unlink (tmp); - - /* avoid symlink security hole: */ - /* open it with O_EXCL will fail if the file exists. */ - /* This should not happen because we just unlinked it. */ - if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0644))<0) { - error ("%s: open(%s) failed: %s", Name, tmp, strerror(errno)); - return -1; - } - - if ((fp = fdopen(fd, "w")) == NULL) { - error("%s: fdopen(%s) failed: %s\n", Name, tmp, strerror(errno)); - close (fd); - return -1; - } - - gdImagePng(im, fp); - gdImageDestroy(im); - - - if (fclose (fp) != 0) { - error("%s: fclose(%s) failed: %s\n", Name, tmp, strerror(errno)); - return -1; - } - - if (rename (tmp, path) < 0) { - error("%s: rename(%s) failed: %s\n", Name, tmp, strerror(errno)); - return -1; - } - - return 0; + + return 0; } #endif -static void drv_IMG_flush (void) +static void drv_IMG_flush(void) { - switch (Format) { - case PPM: + switch (Format) { + case PPM: #ifdef WITH_PPM - drv_IMG_flush_PPM(); + drv_IMG_flush_PPM(); #endif - break; - case PNG: + break; + case PNG: #ifdef WITH_PNG - drv_IMG_flush_PNG(); + drv_IMG_flush_PNG(); #endif - break; - } + break; + } } -static void drv_IMG_timer (void *notused) +static void drv_IMG_timer(void *notused) { - /* avoid compiler warning */ - notused = notused; + /* avoid compiler warning */ + notused = notused; - if (dirty) { - drv_IMG_flush(); - dirty = 0; - } + if (dirty) { + drv_IMG_flush(); + dirty = 0; + } } static void drv_IMG_blit(const int row, const int col, const int height, const int width) { - int r, c; - - for (r=row; r<row+height && r<DROWS; r++) { - for (c=col; c<col+width && c<DCOLS; c++) { - unsigned char p = drv_generic_graphic_FB[r*LCOLS+c]; - if (drv_IMG_FB[r*DCOLS+c] != p) { - drv_IMG_FB[r*DCOLS+c] = p; - dirty = 1; - } + int r, c; + + for (r = row; r < row + height && r < DROWS; r++) { + for (c = col; c < col + width && c < DCOLS; c++) { + unsigned char p = drv_generic_graphic_FB[r * LCOLS + c]; + if (drv_IMG_FB[r * DCOLS + c] != p) { + drv_IMG_FB[r * DCOLS + c] = p; + dirty = 1; + } + } } - } } -static int drv_IMG_start (const char *section) +static int drv_IMG_start(const char *section) { - char *s; - - if (output==NULL || *output=='\0') { - error ("%s: no output file specified (use -o switch)", Name); - return -1; - } - - /* read file format from config */ - s = cfg_get(section, "Format", NULL); - if (s == NULL || *s == '\0') { - error ("%s: no '%s.Format' entry from %s", Name, section, cfg_source()); - free(s); - return -1; - } - - if (strcmp (s, "PPM") == 0) { - Format = PPM; - } else if (strcmp (s, "PNG") == 0) { - Format = PNG; - } else { - error ("%s: bad %s.Format '%s' from %s", Name, section, s, cfg_source()); + char *s; + + if (output == NULL || *output == '\0') { + error("%s: no output file specified (use -o switch)", Name); + return -1; + } + + /* read file format from config */ + s = cfg_get(section, "Format", NULL); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Format' entry from %s", Name, section, cfg_source()); + free(s); + return -1; + } + + if (strcmp(s, "PPM") == 0) { + Format = PPM; + } else if (strcmp(s, "PNG") == 0) { + Format = PNG; + } else { + error("%s: bad %s.Format '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - /* read display size from config */ - if (sscanf(s=cfg_get(section, "Size", "120x32"), "%dx%d", &DCOLS, &DROWS)!=2 || DCOLS<1 || DROWS<1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + /* read display size from config */ + if (sscanf(s = cfg_get(section, "Size", "120x32"), "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - - if (sscanf(s=cfg_get(section, "font", "5x8"), "%dx%d", &XRES, &YRES)!=2 || XRES<1|| YRES<1) { - error ("%s: bad %s.Font '%s' from %s", Name, section, s, cfg_source()); + + if (sscanf(s = cfg_get(section, "font", "5x8"), "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) { + error("%s: bad %s.Font '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - - if (sscanf(s=cfg_get(section, "pixel", "4+1"), "%d+%d", &pixel, &pgap)!=2 || pixel<1 || pgap<0) { - error ("%s: bad %s.Pixel '%s' from %s", Name, section, s, cfg_source()); + + if (sscanf(s = cfg_get(section, "pixel", "4+1"), "%d+%d", &pixel, &pgap) != 2 || pixel < 1 || pgap < 0) { + error("%s: bad %s.Pixel '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - - if (sscanf(s=cfg_get(section, "gap", "-1x-1"), "%dx%d", &cgap, &rgap)!=2 || cgap<-1 || rgap<-1) { - error ("%s: bad %s.Gap '%s' from %s", Name, section, s, cfg_source()); + + if (sscanf(s = cfg_get(section, "gap", "-1x-1"), "%dx%d", &cgap, &rgap) != 2 || cgap < -1 || rgap < -1) { + error("%s: bad %s.Gap '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - if (rgap<0) rgap=pixel+pgap; - if (cgap<0) cgap=pixel+pgap; - - if (cfg_number(section, "border", 0, 0, -1, &border)<0) return -1; + if (rgap < 0) + rgap = pixel + pgap; + if (cgap < 0) + cgap = pixel + pgap; + + if (cfg_number(section, "border", 0, 0, -1, &border) < 0) + return -1; - if (sscanf(s=cfg_get(NULL, "foreground", "#102000"), "#%x", &fg_col)!=1) { - error ("%s: bad %s.foreground color '%s' from %s", Name, section, s, cfg_source()); + if (sscanf(s = cfg_get(NULL, "foreground", "#102000"), "#%x", &fg_col) != 1) { + error("%s: bad %s.foreground color '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - if (sscanf(s=cfg_get(NULL, "halfground", "#70c000"), "#%x", &hg_col)!=1) { - error ("%s: bad %s.halfground color '%s' from %s", Name, section, s, cfg_source()); + if (sscanf(s = cfg_get(NULL, "halfground", "#70c000"), "#%x", &hg_col) != 1) { + error("%s: bad %s.halfground color '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - if (sscanf(s=cfg_get(NULL, "background", "#80d000"), "#%x", &bg_col)!=1) { - error ("%s: bad %s.background color '%s' from %s", Name, section, s, cfg_source()); + if (sscanf(s = cfg_get(NULL, "background", "#80d000"), "#%x", &bg_col) != 1) { + error("%s: bad %s.background color '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - drv_IMG_FB = malloc(DCOLS*DROWS); - if (drv_IMG_FB == NULL) { - error ("%s: framebuffer could not be allocated: malloc() failed", Name); - return -1; - } + drv_IMG_FB = malloc(DCOLS * DROWS); + if (drv_IMG_FB == NULL) { + error("%s: framebuffer could not be allocated: malloc() failed", Name); + return -1; + } - memset(drv_IMG_FB, 0, DCOLS*DROWS*sizeof(*drv_IMG_FB)); + memset(drv_IMG_FB, 0, DCOLS * DROWS * sizeof(*drv_IMG_FB)); - - dimx = DCOLS*pixel + (DCOLS-1)*pgap + (DCOLS/XRES-1)*cgap; - dimy = DROWS*pixel + (DROWS-1)*pgap + (DROWS/YRES-1)*rgap; + dimx = DCOLS * pixel + (DCOLS - 1) * pgap + (DCOLS / XRES - 1) * cgap; + dimy = DROWS * pixel + (DROWS - 1) * pgap + (DROWS / YRES - 1) * rgap; - /* initially flush the image to a file */ - drv_IMG_flush(); - /* regularly flush the image to a file */ - /* Fixme: make 100msec configurable */ - timer_add (drv_IMG_timer, NULL, 100, 0); + /* initially flush the image to a file */ + drv_IMG_flush(); + + /* regularly flush the image to a file */ + /* Fixme: make 100msec configurable */ + timer_add(drv_IMG_timer, NULL, 100, 0); - return 0; + return 0; } @@ -500,73 +497,74 @@ static int drv_IMG_start (const char *section) /* list models */ -int drv_IMG_list (void) +int drv_IMG_list(void) { - printf ("PPM PNG"); - return 0; + printf("PPM PNG"); + return 0; } /* initialize driver & display */ -int drv_IMG_init (const char *section, const __attribute__((unused)) int quiet) +int drv_IMG_init(const char *section, const __attribute__ ((unused)) + int quiet) { - WIDGET_CLASS wc; - int ret; - - /* real worker functions */ - drv_generic_graphic_real_blit = drv_IMG_blit; - - /* start display */ - if ((ret=drv_IMG_start (section))!=0) - return ret; - - /* initialize generic graphic driver */ - if ((ret=drv_generic_graphic_init(section, Name))!=0) - return ret; - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_graphic_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_graphic_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_graphic_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none at the moment... */ - - - return 0; + WIDGET_CLASS wc; + int ret; + + /* real worker functions */ + drv_generic_graphic_real_blit = drv_IMG_blit; + + /* start display */ + if ((ret = drv_IMG_start(section)) != 0) + return ret; + + /* initialize generic graphic driver */ + if ((ret = drv_generic_graphic_init(section, Name)) != 0) + return ret; + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_graphic_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_graphic_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_graphic_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none at the moment... */ + + + return 0; } /* close driver & display */ -int drv_IMG_quit (const __attribute__((unused)) int quiet) { - - info("%s: shutting down.", Name); - drv_generic_graphic_quit(); - - if (drv_IMG_FB) { - free (drv_IMG_FB); - drv_IMG_FB = NULL; - } - - return (0); +int drv_IMG_quit(const __attribute__ ((unused)) + int quiet) +{ + + info("%s: shutting down.", Name); + drv_generic_graphic_quit(); + + if (drv_IMG_FB) { + free(drv_IMG_FB); + drv_IMG_FB = NULL; + } + + return (0); } DRIVER drv_Image = { - name: Name, - list: drv_IMG_list, - init: drv_IMG_init, - quit: drv_IMG_quit, + name:Name, + list:drv_IMG_list, + init:drv_IMG_init, + quit:drv_IMG_quit, }; - - diff --git a/drv_LCDLinux.c b/drv_LCDLinux.c index 29731fb..50688b0 100644 --- a/drv_LCDLinux.c +++ b/drv_LCDLinux.c @@ -1,4 +1,4 @@ -/* $Id: drv_LCDLinux.c,v 1.7 2005/05/02 05:15:46 reinelt Exp $ +/* $Id: drv_LCDLinux.c,v 1.8 2005/05/08 04:32:44 reinelt Exp $ * * driver for the LCD-Linux HD44780 kernel driver * http://lcd-linux.sourceforge.net @@ -24,6 +24,9 @@ * * * $Log: drv_LCDLinux.c,v $ + * Revision 1.8 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.7 2005/05/02 05:15:46 reinelt * make busy-flag checking configurable for LCD-Linux driver * @@ -81,8 +84,8 @@ #include "drv_LCDLinux.h" -static char Name[] = "LCD-Linux"; -static char Device[] = "/dev/lcd"; +static char Name[] = "LCD-Linux"; +static char Device[] = "/dev/lcd"; static int lcdlinux_fd = -1; @@ -90,159 +93,159 @@ static int lcdlinux_fd = -1; /*** hardware dependant functions ***/ /****************************************/ -static void drv_LL_send (const char *string, const int len) +static void drv_LL_send(const char *string, const int len) { - int run, ret; - - for (run = 0; run < 10; run++) { - ret = write (lcdlinux_fd, string, len); - if (ret >= 0 || errno != EAGAIN) break; - if (run > 0) info ("%s: write(%s): EAGAIN #%d", Name, Device, run); - usleep(1000); - } - - if (ret < 0) { - error ("%s: write(%s) failed: %s", Name, Device, strerror(errno)); - } else if (ret != len) { - error ("%s: partial write(%s): len=%d ret=%d", Name, Device, len, ret); - } - - return; + int run, ret; + + for (run = 0; run < 10; run++) { + ret = write(lcdlinux_fd, string, len); + if (ret >= 0 || errno != EAGAIN) + break; + if (run > 0) + info("%s: write(%s): EAGAIN #%d", Name, Device, run); + usleep(1000); + } + + if (ret < 0) { + error("%s: write(%s) failed: %s", Name, Device, strerror(errno)); + } else if (ret != len) { + error("%s: partial write(%s): len=%d ret=%d", Name, Device, len, ret); + } + + return; } -static void drv_LL_clear (void) +static void drv_LL_clear(void) { - /* Fixme: is there no otherway to clear the display? */ - drv_LL_send ("\14", 1); /* Form Feed */ + /* Fixme: is there no otherway to clear the display? */ + drv_LL_send("\14", 1); /* Form Feed */ } -static void drv_LL_write (const int row, const int col, const char *data, int len) +static void drv_LL_write(const int row, const int col, const char *data, int len) { - int pos = row * DCOLS + col; - - if (lseek(lcdlinux_fd, pos, SEEK_SET) == (off_t)-1) { - error ("%s: lseek(%s) failed: %s", Name, Device, strerror(errno)); - } - drv_LL_send (data, len); + int pos = row * DCOLS + col; + + if (lseek(lcdlinux_fd, pos, SEEK_SET) == (off_t) - 1) { + error("%s: lseek(%s) failed: %s", Name, Device, strerror(errno)); + } + drv_LL_send(data, len); } -static void drv_LL_defchar (const int ascii, const unsigned char *matrix) +static void drv_LL_defchar(const int ascii, const unsigned char *matrix) { - char buffer[8]; - int pos = 1024 + ascii; - int i; - - for (i = 0; i < 8; i++) { - buffer[i] = *matrix++ & 0x1f; - } + char buffer[8]; + int pos = 1024 + ascii; + int i; - if (lseek(lcdlinux_fd, pos, SEEK_SET) == (off_t)-1) { - error ("%s: lseek(%s) failed: %s", Name, Device, strerror(errno)); - } + for (i = 0; i < 8; i++) { + buffer[i] = *matrix++ & 0x1f; + } + + if (lseek(lcdlinux_fd, pos, SEEK_SET) == (off_t) - 1) { + error("%s: lseek(%s) failed: %s", Name, Device, strerror(errno)); + } - drv_LL_send (buffer, 8); + drv_LL_send(buffer, 8); } -static int drv_LL_start (const char *section, const int quiet) +static int drv_LL_start(const char *section, const int quiet) { - char *s; - int rows = -1, cols = -1; - int use_busy = 0; - struct lcd_driver buf; - - /* emit version information */ - info ("%s: Version %s", Name, LCD_LINUX_VERSION); - - /* get size from config file */ - s=cfg_get(section, "Size", NULL); - if (s != NULL || *s != '\0') { - if (sscanf(s, "%dx%d",&cols,&rows) != 2 || rows < 1 || cols < 1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); - free (s); - return -1; + char *s; + int rows = -1, cols = -1; + int use_busy = 0; + struct lcd_driver buf; + + /* emit version information */ + info("%s: Version %s", Name, LCD_LINUX_VERSION); + + /* get size from config file */ + s = cfg_get(section, "Size", NULL); + if (s != NULL || *s != '\0') { + if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } } - } - free (s); - - /* open device */ - lcdlinux_fd = open(Device, O_WRONLY); - if (lcdlinux_fd == -1) { - error ("%s: open(%s) failed: %s", Name, Device, strerror(errno)); - return -1; - } - - /* get display size */ - memset(&buf, 0, sizeof(buf)); - if (ioctl(lcdlinux_fd, IOCTL_GET_PARAM, &buf) != 0) { - error ("%s: ioctl(IOCTL_GET_PARAM) failed: %s", Name, strerror(errno)); - error ("%s: Could not query display information!", Name); - return -1; - } - info("%s: %dx%d display at 0x%x, %d controllers, flags=0x%02x:", - Name, buf.disp_cols, buf.cntr_rows, buf.io, buf.num_cntr, buf.flags); - - info("%s: /proc support %sabled", Name, buf.flags & LCD_PROC_ON ? "en" : "dis"); - info("%s: tty support %sabled", Name, buf.flags & LCD_ETTY_ON ? "en" : "dis"); - info("%s: console support %sabled", Name, buf.flags & LCD_CONSOLE ? "en" : "dis"); - info("%s: bus width %d bits", Name, buf.flags & LCD_4BITS_BUS ? 4 : 8); - info("%s: font size %s", Name, buf.flags & LCD_5X10_FONT ? "5x10" : "5x8"); - info("%s: busy-flag checking %sabled", Name, buf.flags & LCD_CHECK_BF ? "en" : "dis"); - - - /* overwrite with size from lcd4linux.conf */ - if ((rows > 0 && rows != buf.cntr_rows) || (cols > 0 && cols != buf.disp_cols)) { - info("%s: changing size to %dx%d", Name, cols, rows); - buf.cntr_rows = rows; - buf.disp_cols = cols; - if (ioctl(lcdlinux_fd, IOCTL_SET_PARAM, &buf) != 0) { - error ("%s: ioctl(IOCTL_SET_PARAM) failed: %s", Name, strerror(errno)); - error ("%s: Could not set display geometry!", Name); - return -1; + free(s); + + /* open device */ + lcdlinux_fd = open(Device, O_WRONLY); + if (lcdlinux_fd == -1) { + error("%s: open(%s) failed: %s", Name, Device, strerror(errno)); + return -1; } - } - - DROWS = buf.cntr_rows; - DCOLS = buf.disp_cols; - - /* overwrite busy-flag checking from lcd4linux.conf */ - cfg_number(section, "UseBusy", 0, 0, 1, &use_busy); - if (use_busy && !(buf.flags & LCD_CHECK_BF)) { - info ("%s: activating busy-flag checking", Name); - buf.flags |= LCD_CHECK_BF; - if (ioctl(lcdlinux_fd, IOCTL_SET_PARAM, &buf) != 0) { - error ("%s: ioctl(IOCTL_SET_PARAM) failed: %s", Name, strerror(errno)); - error ("%s: Could not activate busy-flag checking!", Name); - return -1; + + /* get display size */ + memset(&buf, 0, sizeof(buf)); + if (ioctl(lcdlinux_fd, IOCTL_GET_PARAM, &buf) != 0) { + error("%s: ioctl(IOCTL_GET_PARAM) failed: %s", Name, strerror(errno)); + error("%s: Could not query display information!", Name); + return -1; } - } - else if (!use_busy && (buf.flags & LCD_CHECK_BF)) { - info ("%s: deactivating busy-flag checking", Name); - buf.flags &= ~LCD_CHECK_BF; - if (ioctl(lcdlinux_fd, IOCTL_SET_PARAM, &buf) != 0) { - error ("%s: ioctl(IOCTL_SET_PARAM) failed: %s", Name, strerror(errno)); - error ("%s: Could not deactivate busy-flag checking!", Name); - return -1; + info("%s: %dx%d display at 0x%x, %d controllers, flags=0x%02x:", Name, buf.disp_cols, buf.cntr_rows, buf.io, buf.num_cntr, buf.flags); + + info("%s: /proc support %sabled", Name, buf.flags & LCD_PROC_ON ? "en" : "dis"); + info("%s: tty support %sabled", Name, buf.flags & LCD_ETTY_ON ? "en" : "dis"); + info("%s: console support %sabled", Name, buf.flags & LCD_CONSOLE ? "en" : "dis"); + info("%s: bus width %d bits", Name, buf.flags & LCD_4BITS_BUS ? 4 : 8); + info("%s: font size %s", Name, buf.flags & LCD_5X10_FONT ? "5x10" : "5x8"); + info("%s: busy-flag checking %sabled", Name, buf.flags & LCD_CHECK_BF ? "en" : "dis"); + + + /* overwrite with size from lcd4linux.conf */ + if ((rows > 0 && rows != buf.cntr_rows) || (cols > 0 && cols != buf.disp_cols)) { + info("%s: changing size to %dx%d", Name, cols, rows); + buf.cntr_rows = rows; + buf.disp_cols = cols; + if (ioctl(lcdlinux_fd, IOCTL_SET_PARAM, &buf) != 0) { + error("%s: ioctl(IOCTL_SET_PARAM) failed: %s", Name, strerror(errno)); + error("%s: Could not set display geometry!", Name); + return -1; + } } - } - - /* initialize display */ - drv_LL_clear(); /* clear display */ - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, "lcd-linux.sf.net")) { - sleep (3); - drv_LL_clear(); + + DROWS = buf.cntr_rows; + DCOLS = buf.disp_cols; + + /* overwrite busy-flag checking from lcd4linux.conf */ + cfg_number(section, "UseBusy", 0, 0, 1, &use_busy); + if (use_busy && !(buf.flags & LCD_CHECK_BF)) { + info("%s: activating busy-flag checking", Name); + buf.flags |= LCD_CHECK_BF; + if (ioctl(lcdlinux_fd, IOCTL_SET_PARAM, &buf) != 0) { + error("%s: ioctl(IOCTL_SET_PARAM) failed: %s", Name, strerror(errno)); + error("%s: Could not activate busy-flag checking!", Name); + return -1; + } + } else if (!use_busy && (buf.flags & LCD_CHECK_BF)) { + info("%s: deactivating busy-flag checking", Name); + buf.flags &= ~LCD_CHECK_BF; + if (ioctl(lcdlinux_fd, IOCTL_SET_PARAM, &buf) != 0) { + error("%s: ioctl(IOCTL_SET_PARAM) failed: %s", Name, strerror(errno)); + error("%s: Could not deactivate busy-flag checking!", Name); + return -1; + } } - } - - return 0; + + /* initialize display */ + drv_LL_clear(); /* clear display */ + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, "lcd-linux.sf.net")) { + sleep(3); + drv_LL_clear(); + } + } + + return 0; } @@ -269,107 +272,105 @@ static int drv_LL_start (const char *section, const int quiet) /* list models */ -int drv_LL_list (void) +int drv_LL_list(void) { - printf ("generic"); - return 0; + printf("generic"); + return 0; } /* initialize driver & display */ -int drv_LL_init (const char *section, const int quiet) +int drv_LL_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int asc255bug; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = -1; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_LL_write; - drv_generic_text_real_defchar = drv_LL_defchar; - - - /* start display */ - if ((ret=drv_LL_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - /* most displays have a full block on ascii 255, but some have kind of */ - /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ - /* char will not be used, but rendered by the bar driver */ - cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - if (!asc255bug) - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none */ - - return 0; + WIDGET_CLASS wc; + int asc255bug; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = -1; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_LL_write; + drv_generic_text_real_defchar = drv_LL_defchar; + + + /* start display */ + if ((ret = drv_LL_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + /* most displays have a full block on ascii 255, but some have kind of */ + /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ + /* char will not be used, but rendered by the bar driver */ + cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + if (!asc255bug) + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none */ + + return 0; } /* close driver & display */ -int drv_LL_quit (const int quiet) +int drv_LL_quit(const int quiet) { - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_LL_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - /* close device */ - close (lcdlinux_fd); - - return (0); + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_LL_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + /* close device */ + close(lcdlinux_fd); + + return (0); } DRIVER drv_LCDLinux = { - name: Name, - list: drv_LL_list, - init: drv_LL_init, - quit: drv_LL_quit, + name:Name, + list:drv_LL_list, + init:drv_LL_init, + quit:drv_LL_quit, }; - - diff --git a/drv_LCDLinux.h b/drv_LCDLinux.h index ae7f4cb..af638c4 100644 --- a/drv_LCDLinux.h +++ b/drv_LCDLinux.h @@ -1,6 +1,6 @@ /* lcd.h * - * $Id: drv_LCDLinux.h,v 1.3 2005/05/02 05:15:46 reinelt Exp $ + * $Id: drv_LCDLinux.h,v 1.4 2005/05/08 04:32:44 reinelt Exp $ * * LCD driver for HD44780 compatible displays connected to the parallel port. * @@ -36,17 +36,17 @@ #include <linux/types.h> struct lcd_driver { - /* Hardware */ - unsigned short io; /* Parport base address */ - unsigned short flags; /* Flags (see Documentation) */ + /* Hardware */ + unsigned short io; /* Parport base address */ + unsigned short flags; /* Flags (see Documentation) */ - /* Display geometry */ - unsigned short num_cntr; /* Number of available controllers */ - unsigned short cntr_rows; /* Rows per controller */ - unsigned short disp_cols; /* Columns */ - unsigned short frames; /* Framebuffer frames */ + /* Display geometry */ + unsigned short num_cntr; /* Number of available controllers */ + unsigned short cntr_rows; /* Rows per controller */ + unsigned short disp_cols; /* Columns */ + unsigned short frames; /* Framebuffer frames */ - unsigned short tabstop; /* Length of tab character */ + unsigned short tabstop; /* Length of tab character */ }; /* IOCTLs */ @@ -54,16 +54,16 @@ struct lcd_driver { #define IOCTL_SET_PARAM _IOW(LCD_MAJOR, 0, struct lcd_driver *) #define IOCTL_GET_PARAM _IOR(LCD_MAJOR, 1, struct lcd_driver *) -#define LCD_PROC_ON 0x0001 /* Enable the /proc filesystem support */ -#define LCD_ETTY_ON 0x0002 /* Enable the tty support */ -#define LCD_CONSOLE 0x0004 /* Enable the console support */ -#define LCD_4BITS_BUS 0x0008 /* Set the bus length to 4 bits */ -#define LCD_5X10_FONT 0x0010 /* Use 5x10 dots fonts */ -#define LCD_CHECK_BF 0x0020 /* Do busy flag checking */ +#define LCD_PROC_ON 0x0001 /* Enable the /proc filesystem support */ +#define LCD_ETTY_ON 0x0002 /* Enable the tty support */ +#define LCD_CONSOLE 0x0004 /* Enable the console support */ +#define LCD_4BITS_BUS 0x0008 /* Set the bus length to 4 bits */ +#define LCD_5X10_FONT 0x0010 /* Use 5x10 dots fonts */ +#define LCD_CHECK_BF 0x0020 /* Do busy flag checking */ -#ifdef __KERNEL__ /* The rest is for kernel only */ +#ifdef __KERNEL__ /* The rest is for kernel only */ #include <linux/version.h> @@ -87,6 +87,6 @@ int lcd_write(const char *, int, unsigned int); int lcd_read(char *, int, unsigned int); #endif -#endif /* __KERNEL__ */ +#endif /* __KERNEL__ */ -#endif /* External interface included */ +#endif /* External interface included */ diff --git a/drv_LCDTerm.c b/drv_LCDTerm.c index 51c7ff1..133079e 100644 --- a/drv_LCDTerm.c +++ b/drv_LCDTerm.c @@ -1,4 +1,4 @@ -/* $Id: drv_LCDTerm.c,v 1.3 2005/01/22 22:57:57 reinelt Exp $ +/* $Id: drv_LCDTerm.c,v 1.4 2005/05/08 04:32:44 reinelt Exp $ * * driver for the LCDTerm serial-to-HD44780 adapter boards * http://www.bobblick.com/techref/projects/lcdterm/lcdterm.html @@ -24,6 +24,9 @@ * * * $Log: drv_LCDTerm.c,v $ + * Revision 1.4 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.3 2005/01/22 22:57:57 reinelt * LCD-Linux driver added * @@ -60,113 +63,114 @@ #include "drv_generic_text.h" #include "drv_generic_serial.h" -#define LCD_CLEAR 0x03 +#define LCD_CLEAR 0x03 #define LCD_CMD 0x12 #define LCD_DATA 0x14 -static char Name[]="LCDTerm"; +static char Name[] = "LCDTerm"; /****************************************/ /*** hardware dependant functions ***/ /****************************************/ -static void drv_LT_clear (void) +static void drv_LT_clear(void) { - char cmd[1]; - - cmd[0] = LCD_CLEAR; /* clear display */ - drv_generic_serial_write (cmd, 1); /* clear screen */ + char cmd[1]; + + cmd[0] = LCD_CLEAR; /* clear display */ + drv_generic_serial_write(cmd, 1); /* clear screen */ } -static int drv_LT_send (const char request, const char value) +static int drv_LT_send(const char request, const char value) { - char buf[2]; + char buf[2]; - buf[0] = request; - buf[1] = value; - drv_generic_serial_write (buf, 2); - - return 0; + buf[0] = request; + buf[1] = value; + drv_generic_serial_write(buf, 2); + + return 0; } -static void drv_LT_command (const char cmd) +static void drv_LT_command(const char cmd) { - drv_LT_send (LCD_CMD, cmd); + drv_LT_send(LCD_CMD, cmd); } -static void drv_LT_write (const int row, const int col, const char *data, int len) +static void drv_LT_write(const int row, const int col, const char *data, int len) { - int pos; - - /* 16x4 Displays use a slightly different layout */ - if (DCOLS==16 && DROWS==4) { - pos = (row%2)*64+(row/2)*16+col; - } else { - pos = (row%2)*64+(row/2)*20+col; - } - - drv_LT_command (0x80|pos); - - while (len--) { - drv_LT_send (LCD_DATA, *data++); - } + int pos; + + /* 16x4 Displays use a slightly different layout */ + if (DCOLS == 16 && DROWS == 4) { + pos = (row % 2) * 64 + (row / 2) * 16 + col; + } else { + pos = (row % 2) * 64 + (row / 2) * 20 + col; + } + + drv_LT_command(0x80 | pos); + + while (len--) { + drv_LT_send(LCD_DATA, *data++); + } } -static void drv_LT_defchar (const int ascii, const unsigned char *matrix) +static void drv_LT_defchar(const int ascii, const unsigned char *matrix) { - int i; - - drv_LT_command (0x40|8*ascii); + int i; + + drv_LT_command(0x40 | 8 * ascii); - for (i = 0; i < 8; i++) { - drv_LT_send (LCD_DATA, *matrix++ & 0x1f); - } + for (i = 0; i < 8; i++) { + drv_LT_send(LCD_DATA, *matrix++ & 0x1f); + } } -static int drv_LT_start (const char *section, const int quiet) +static int drv_LT_start(const char *section, const int quiet) { - int rows=-1, cols=-1; - char *s; - - if (drv_generic_serial_open(section, Name, 0) < 0) return -1; - - s=cfg_get(section, "Size", NULL); - if (s==NULL || *s=='\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - return -1; - } - if (sscanf(s,"%dx%d",&cols,&rows)!=2 || rows<1 || cols<1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); - free (s); - return -1; - } - - DROWS = rows; - DCOLS = cols; - - /* initialize display */ - drv_LT_command (0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ - drv_LT_command (0x08); /* Display off, cursor off, blink off */ - drv_LT_command (0x0c); /* Display on, cursor off, blink off */ - drv_LT_command (0x06); /* curser moves to right, no shift */ - - drv_LT_clear(); /* clear display */ - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, "www.bwct.de")) { - sleep (3); - drv_LT_clear(); + int rows = -1, cols = -1; + char *s; + + if (drv_generic_serial_open(section, Name, 0) < 0) + return -1; + + s = cfg_get(section, "Size", NULL); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + return -1; + } + if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; } - } - - return 0; + + DROWS = rows; + DCOLS = cols; + + /* initialize display */ + drv_LT_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ + drv_LT_command(0x08); /* Display off, cursor off, blink off */ + drv_LT_command(0x0c); /* Display on, cursor off, blink off */ + drv_LT_command(0x06); /* curser moves to right, no shift */ + + drv_LT_clear(); /* clear display */ + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, "www.bwct.de")) { + sleep(3); + drv_LT_clear(); + } + } + + return 0; } @@ -193,108 +197,106 @@ static int drv_LT_start (const char *section, const int quiet) /* list models */ -int drv_LT_list (void) +int drv_LT_list(void) { - printf ("generic"); - return 0; + printf("generic"); + return 0; } /* initialize driver & display */ -int drv_LT_init (const char *section, const int quiet) +int drv_LT_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int asc255bug; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - - /* Fixme: */ - GOTO_COST = 2; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_LT_write; - drv_generic_text_real_defchar = drv_LT_defchar; - - - /* start display */ - if ((ret=drv_LT_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - /* most displays have a full block on ascii 255, but some have kind of */ - /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ - /* char will not be used, but rendered by the bar driver */ - cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - if (!asc255bug) - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none */ - - return 0; + WIDGET_CLASS wc; + int asc255bug; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + + /* Fixme: */ + GOTO_COST = 2; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_LT_write; + drv_generic_text_real_defchar = drv_LT_defchar; + + + /* start display */ + if ((ret = drv_LT_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + /* most displays have a full block on ascii 255, but some have kind of */ + /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ + /* char will not be used, but rendered by the bar driver */ + cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + if (!asc255bug) + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none */ + + return 0; } /* close driver & display */ -int drv_LT_quit (const int quiet) +int drv_LT_quit(const int quiet) { - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_LT_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - drv_generic_serial_close(); - - return (0); + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_LT_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + drv_generic_serial_close(); + + return (0); } DRIVER drv_LCDTerm = { - name: Name, - list: drv_LT_list, - init: drv_LT_init, - quit: drv_LT_quit, + name:Name, + list:drv_LT_list, + init:drv_LT_init, + quit:drv_LT_quit, }; - - diff --git a/drv_M50530.c b/drv_M50530.c index 93e6cd4..7e8bb75 100644 --- a/drv_M50530.c +++ b/drv_M50530.c @@ -1,104 +1,107 @@ -#/* $Id: drv_M50530.c,v 1.17 2005/05/05 08:36:12 reinelt Exp $ - * - * new style driver for M50530-based displays - * - * Copyright (C) 2003 Michael Reinelt <reinelt@eunet.at> - * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> - * - * This file is part of LCD4Linux. - * - * LCD4Linux is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * LCD4Linux is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * - * $Log: drv_M50530.c,v $ - * Revision 1.17 2005/05/05 08:36:12 reinelt - * changed SELECT to SLCTIN - * - * Revision 1.16 2005/01/18 06:30:23 reinelt - * added (C) to all copyright statements - * - * Revision 1.15 2005/01/06 16:54:53 reinelt - * M50530 fixes - * - * Revision 1.14 2004/06/26 12:04:59 reinelt - * - * uh-oh... the last CVS log message messed up things a lot... - * - * Revision 1.13 2004/06/26 09:27:20 reinelt - * - * added '-W' to CFLAGS - * changed all C++ comments to C ones - * cleaned up a lot of signed/unsigned mistakes - * - * Revision 1.12 2004/06/20 10:09:54 reinelt - * - * 'const'ified the whole source - * - * Revision 1.11 2004/06/06 06:51:59 reinelt - * - * do not display end splash screen if quiet=1 - * - * Revision 1.10 2004/06/05 06:41:39 reinelt - * - * chancged splash screen again - * - * Revision 1.9 2004/06/05 06:13:12 reinelt - * - * splash screen for all text-based display drivers - * - * Revision 1.8 2004/06/02 09:41:19 reinelt - * - * prepared support for startup splash screen - * - * Revision 1.7 2004/06/01 06:45:29 reinelt - * - * some Fixme's processed - * documented some code - * - * Revision 1.6 2004/05/31 05:38:02 reinelt - * - * fixed possible bugs with user-defined chars (clear high bits) - * thanks to Andy Baxter for debugging the MilfordInstruments driver! - * - * Revision 1.5 2004/05/29 15:53:28 reinelt - * - * M50530: reset parport signals on exit - * plugin_ppp: ppp() has two parameters, not three - * lcd4linux.conf.sample: diskstats() corrected - * - * Revision 1.4 2004/05/26 11:37:36 reinelt - * - * Curses driver ported. - * - * Revision 1.3 2004/03/19 09:17:46 reinelt - * - * removed the extra 'goto' function, row and col are additional parameters - * of the write() function now. - * - * Revision 1.2 2004/02/15 21:43:43 reinelt - * T6963 driver nearly finished - * framework for graphic displays done - * i2c_sensors patch from Xavier - * some more old generation files removed - * - * Revision 1.1 2004/02/15 08:22:47 reinelt - * ported USBLCD driver to NextGeneration - * added drv_M50530.c (I forgot yesterday, sorry) - * removed old drivers M50530.c and USBLCD.c - * - */ +# /* $Id: drv_M50530.c,v 1.18 2005/05/08 04:32:44 reinelt Exp $ + * + * new style driver for M50530-based displays + * + * Copyright (C) 2003 Michael Reinelt <reinelt@eunet.at> + * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> + * + * This file is part of LCD4Linux. + * + * LCD4Linux is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * LCD4Linux is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * $Log: drv_M50530.c,v $ + * Revision 1.18 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * + * Revision 1.17 2005/05/05 08:36:12 reinelt + * changed SELECT to SLCTIN + * + * Revision 1.16 2005/01/18 06:30:23 reinelt + * added (C) to all copyright statements + * + * Revision 1.15 2005/01/06 16:54:53 reinelt + * M50530 fixes + * + * Revision 1.14 2004/06/26 12:04:59 reinelt + * + * uh-oh... the last CVS log message messed up things a lot... + * + * Revision 1.13 2004/06/26 09:27:20 reinelt + * + * added '-W' to CFLAGS + * changed all C++ comments to C ones + * cleaned up a lot of signed/unsigned mistakes + * + * Revision 1.12 2004/06/20 10:09:54 reinelt + * + * 'const'ified the whole source + * + * Revision 1.11 2004/06/06 06:51:59 reinelt + * + * do not display end splash screen if quiet=1 + * + * Revision 1.10 2004/06/05 06:41:39 reinelt + * + * chancged splash screen again + * + * Revision 1.9 2004/06/05 06:13:12 reinelt + * + * splash screen for all text-based display drivers + * + * Revision 1.8 2004/06/02 09:41:19 reinelt + * + * prepared support for startup splash screen + * + * Revision 1.7 2004/06/01 06:45:29 reinelt + * + * some Fixme's processed + * documented some code + * + * Revision 1.6 2004/05/31 05:38:02 reinelt + * + * fixed possible bugs with user-defined chars (clear high bits) + * thanks to Andy Baxter for debugging the MilfordInstruments driver! + * + * Revision 1.5 2004/05/29 15:53:28 reinelt + * + * M50530: reset parport signals on exit + * plugin_ppp: ppp() has two parameters, not three + * lcd4linux.conf.sample: diskstats() corrected + * + * Revision 1.4 2004/05/26 11:37:36 reinelt + * + * Curses driver ported. + * + * Revision 1.3 2004/03/19 09:17:46 reinelt + * + * removed the extra 'goto' function, row and col are additional parameters + * of the write() function now. + * + * Revision 1.2 2004/02/15 21:43:43 reinelt + * T6963 driver nearly finished + * framework for graphic displays done + * i2c_sensors patch from Xavier + * some more old generation files removed + * + * Revision 1.1 2004/02/15 08:22:47 reinelt + * ported USBLCD driver to NextGeneration + * added drv_M50530.c (I forgot yesterday, sorry) + * removed old drivers M50530.c and USBLCD.c + * + */ /* * @@ -132,7 +135,7 @@ #include "drv_generic_text.h" #include "drv_generic_parport.h" -static char Name[]="M50530"; +static char Name[] = "M50530"; static int Model; @@ -147,13 +150,13 @@ static int GPOS; typedef struct { - int type; - char *name; + int type; + char *name; } MODEL; static MODEL Models[] = { - { 0x01, "generic" }, - { 0xff, "Unknown" } + {0x01, "generic"}, + {0xff, "Unknown"} }; @@ -161,164 +164,169 @@ static MODEL Models[] = { /*** hardware dependant functions ***/ /****************************************/ -static void drv_M5_command (const unsigned int cmd, const int delay) +static void drv_M5_command(const unsigned int cmd, const int delay) { - - /* put data on DB1..DB8 */ - drv_generic_parport_data (cmd & 0xff); - - /* set I/OC1 */ - /* set I/OC2 */ - drv_generic_parport_control (SIGNAL_IOC1 | SIGNAL_IOC2, - (cmd & 0x100 ? SIGNAL_IOC1 : 0) | - (cmd & 0x200 ? SIGNAL_IOC2 : 0)); - - /* Control data setup time */ - ndelay(200); - - /* send command */ - /* EX signal pulse width = 500ns */ - /* Fixme: why 500 ns? Datasheet says 200ns */ - drv_generic_parport_toggle (SIGNAL_EX, 1, 500); - - /* wait */ - udelay(delay); + + /* put data on DB1..DB8 */ + drv_generic_parport_data(cmd & 0xff); + + /* set I/OC1 */ + /* set I/OC2 */ + drv_generic_parport_control(SIGNAL_IOC1 | SIGNAL_IOC2, (cmd & 0x100 ? SIGNAL_IOC1 : 0) | (cmd & 0x200 ? SIGNAL_IOC2 : 0)); + + /* Control data setup time */ + ndelay(200); + + /* send command */ + /* EX signal pulse width = 500ns */ + /* Fixme: why 500 ns? Datasheet says 200ns */ + drv_generic_parport_toggle(SIGNAL_EX, 1, 500); + + /* wait */ + udelay(delay); } -static void drv_M5_clear (void) +static void drv_M5_clear(void) { - drv_M5_command (0x0001, 1250); /* clear display */ + drv_M5_command(0x0001, 1250); /* clear display */ } -static void drv_M5_write (const int row, const int col, const char *data, const int len) +static void drv_M5_write(const int row, const int col, const char *data, const int len) { - int l = len; - unsigned int cmd; - unsigned int pos; - - pos = row * 48 + col; - if (row > 3) pos -= 168; - drv_M5_command (0x300 | pos, 20); - - while (l--) { - cmd = *(unsigned char*)data++; - drv_M5_command (0x200 | cmd, 20); - } + int l = len; + unsigned int cmd; + unsigned int pos; + + pos = row * 48 + col; + if (row > 3) + pos -= 168; + drv_M5_command(0x300 | pos, 20); + + while (l--) { + cmd = *(unsigned char *) data++; + drv_M5_command(0x200 | cmd, 20); + } } -static void drv_M5_defchar (const int ascii, const unsigned char *matrix) +static void drv_M5_defchar(const int ascii, const unsigned char *matrix) { - int i; - - drv_M5_command (0x300 + 192 + 8 * (ascii - CHAR0), 20); - - /* Fixme: looks like the M50530 cannot control the bottom line */ - /* therefore we have only 7 bytes here */ - for (i = 0; i < 7; i++) { - drv_M5_command (0x200 | (matrix[i] & 0x3f), 20); - } + int i; + + drv_M5_command(0x300 + 192 + 8 * (ascii - CHAR0), 20); + + /* Fixme: looks like the M50530 cannot control the bottom line */ + /* therefore we have only 7 bytes here */ + for (i = 0; i < 7; i++) { + drv_M5_command(0x200 | (matrix[i] & 0x3f), 20); + } } /* Fixme: GPO's */ #if 0 -static void drv_M5_setGPO (const int bits) +static void drv_M5_setGPO(const int bits) { - if (Lcd.gpos>0) { + if (Lcd.gpos > 0) { - /* put data on DB1..DB8 */ - drv_generic_parport_data (bits); - - /* 74HCT573 set-up time */ - ndelay(20); - - /* send data */ - /* 74HCT573 enable pulse width = 24ns */ - drv_generic_parport_toggle (SIGNAL_GPO, 1, 24); - } + /* put data on DB1..DB8 */ + drv_generic_parport_data(bits); + + /* 74HCT573 set-up time */ + ndelay(20); + + /* send data */ + /* 74HCT573 enable pulse width = 24ns */ + drv_generic_parport_toggle(SIGNAL_GPO, 1, 24); + } } #endif -static int drv_M5_start (const char *section, const int quiet) +static int drv_M5_start(const char *section, const int quiet) { - char *model, *s; - int rows=-1, cols=-1, gpos=-1; - - model=cfg_get(section, "Model", "generic"); - if (model!=NULL && *model!='\0') { - int i; - for (i=0; Models[i].type!=0xff; i++) { - if (strcasecmp(Models[i].name, model)==0) break; + char *model, *s; + int rows = -1, cols = -1, gpos = -1; + + model = cfg_get(section, "Model", "generic"); + if (model != NULL && *model != '\0') { + int i; + for (i = 0; Models[i].type != 0xff; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; + } + if (Models[i].type == 0xff) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + Model = i; + info("%s: using model '%s'", Name, Models[Model].name); + } else { + error("%s: empty '%s.Model' entry from %s", Name, section, cfg_source()); + return -1; + } + + s = cfg_get(section, "Size", NULL); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + return -1; + } + if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad size '%s'", Name, s); + return -1; } - if (Models[i].type==0xff) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; + + if (cfg_number(section, "GPOs", 0, 0, 8, &gpos) < 0) + return -1; + info("%s: controlling %d GPO's", Name, gpos); + + DROWS = rows; + DCOLS = cols; + GPOS = gpos; + + if (drv_generic_parport_open(section, Name) != 0) { + error("%s: could not initialize parallel port!", Name); + return -1; } - Model=i; - info ("%s: using model '%s'", Name, Models[Model].name); - } else { - error ("%s: empty '%s.Model' entry from %s", Name, section, cfg_source()); - return -1; - } - - s=cfg_get(section, "Size", NULL); - if (s==NULL || *s=='\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - return -1; - } - if (sscanf(s,"%dx%d",&cols,&rows)!=2 || rows<1 || cols<1) { - error ("%s: bad size '%s'", Name, s); - return -1; - } - - if (cfg_number(section, "GPOs", 0, 0, 8, &gpos)<0) return -1; - info ("%s: controlling %d GPO's", Name, gpos); - - DROWS = rows; - DCOLS = cols; - GPOS = gpos; - - if (drv_generic_parport_open(section, Name) != 0) { - error ("%s: could not initialize parallel port!", Name); - return -1; - } - - if ((SIGNAL_EX = drv_generic_parport_wire_ctrl ("EX", "STROBE"))==0xff) return -1; - if ((SIGNAL_IOC1 = drv_generic_parport_wire_ctrl ("IOC1", "SLCTIN"))==0xff) return -1; - if ((SIGNAL_IOC2 = drv_generic_parport_wire_ctrl ("IOC2", "AUTOFD"))==0xff) return -1; - if ((SIGNAL_GPO = drv_generic_parport_wire_ctrl ("GPO", "GND" ))==0xff) return -1; - - /* clear all signals */ - drv_generic_parport_control (SIGNAL_EX|SIGNAL_IOC1|SIGNAL_IOC2|SIGNAL_GPO, 0); - - /* for any mysterious reason, this delay is necessary... */ - udelay(2000); - - /* set direction: write */ - drv_generic_parport_direction (0); - - drv_M5_command (0x00FA, 20); /* set function mode */ - drv_M5_command (0x0020, 20); /* set display mode */ - drv_M5_command (0x0050, 20); /* set entry mode */ - drv_M5_command (0x0030, 20); /* set display mode */ - - drv_M5_clear(); - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, NULL)) { - sleep (3); - drv_M5_clear(); + + if ((SIGNAL_EX = drv_generic_parport_wire_ctrl("EX", "STROBE")) == 0xff) + return -1; + if ((SIGNAL_IOC1 = drv_generic_parport_wire_ctrl("IOC1", "SLCTIN")) == 0xff) + return -1; + if ((SIGNAL_IOC2 = drv_generic_parport_wire_ctrl("IOC2", "AUTOFD")) == 0xff) + return -1; + if ((SIGNAL_GPO = drv_generic_parport_wire_ctrl("GPO", "GND")) == 0xff) + return -1; + + /* clear all signals */ + drv_generic_parport_control(SIGNAL_EX | SIGNAL_IOC1 | SIGNAL_IOC2 | SIGNAL_GPO, 0); + + /* for any mysterious reason, this delay is necessary... */ + udelay(2000); + + /* set direction: write */ + drv_generic_parport_direction(0); + + drv_M5_command(0x00FA, 20); /* set function mode */ + drv_M5_command(0x0020, 20); /* set display mode */ + drv_M5_command(0x0050, 20); /* set entry mode */ + drv_M5_command(0x0030, 20); /* set display mode */ + + drv_M5_clear(); + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, NULL)) { + sleep(3); + drv_M5_clear(); + } } - } - return 0; + return 0; } @@ -344,106 +352,105 @@ static int drv_M5_start (const char *section, const int quiet) /* list models */ -int drv_M5_list (void) +int drv_M5_list(void) { - int i; - - for (i=0; Models[i].type!=0xff; i++) { - printf ("%s ", Models[i].name); - } - return 0; + int i; + + for (i = 0; Models[i].type != 0xff; i++) { + printf("%s ", Models[i].name); + } + return 0; } /* initialize driver & display */ -int drv_M5_init (const char *section, const int quiet) +int drv_M5_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 248; /* ASCII of first user-defineable char */ - GOTO_COST = 1; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_M5_write; - drv_generic_text_real_defchar = drv_M5_defchar; - - - /* start display */ - if ((ret = drv_M5_start (section, quiet)) != 0) - return ret; - - /* initialize generic text driver */ - if ((ret = drv_generic_text_init(section, Name)) != 0) - return ret; - - /* initialize generic icon driver */ - if ((ret = drv_generic_text_icon_init()) != 0) - return ret; - - /* initialize generic bar driver */ - if ((ret = drv_generic_text_bar_init(0)) != 0) - return ret; - - /* add fixed chars to the bar driver */ - drv_generic_text_bar_add_segment (0, 0, 255, 32); /* ASCII 32 = blank */ - - /* register text widget */ - wc = Widget_Text; - wc.draw = drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc = Widget_Icon; - wc.draw = drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc = Widget_Bar; - wc.draw = drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none at the moment */ - - return 0; + WIDGET_CLASS wc; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 248; /* ASCII of first user-defineable char */ + GOTO_COST = 1; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_M5_write; + drv_generic_text_real_defchar = drv_M5_defchar; + + + /* start display */ + if ((ret = drv_M5_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none at the moment */ + + return 0; } /* close driver & display */ -int drv_M5_quit (const int quiet) { - - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_M5_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - /* clear all signals */ - drv_generic_parport_control (SIGNAL_EX|SIGNAL_IOC1|SIGNAL_IOC2|SIGNAL_GPO, 0); - - /* close port */ - drv_generic_parport_close(); - - return (0); +int drv_M5_quit(const int quiet) +{ + + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_M5_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + /* clear all signals */ + drv_generic_parport_control(SIGNAL_EX | SIGNAL_IOC1 | SIGNAL_IOC2 | SIGNAL_GPO, 0); + + /* close port */ + drv_generic_parport_close(); + + return (0); } DRIVER drv_M50530 = { - name: Name, - list: drv_M5_list, - init: drv_M5_init, - quit: drv_M5_quit, + name:Name, + list:drv_M5_list, + init:drv_M5_init, + quit:drv_M5_quit, }; - - diff --git a/drv_MatrixOrbital.c b/drv_MatrixOrbital.c index d89ec0f..8ea5b24 100644 --- a/drv_MatrixOrbital.c +++ b/drv_MatrixOrbital.c @@ -1,4 +1,4 @@ -/* $Id: drv_MatrixOrbital.c,v 1.38 2005/01/22 12:44:41 reinelt Exp $ +/* $Id: drv_MatrixOrbital.c,v 1.39 2005/05/08 04:32:44 reinelt Exp $ * * new style driver for Matrix Orbital serial display modules * @@ -23,6 +23,9 @@ * * * $Log: drv_MatrixOrbital.c,v $ + * Revision 1.39 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.38 2005/01/22 12:44:41 reinelt * MatrixOrbital backlight micro-fix * @@ -213,7 +216,7 @@ #include "drv_generic_serial.h" -static char Name[]="MatrixOrbital"; +static char Name[] = "MatrixOrbital"; static int Model; static int Protocol; @@ -224,44 +227,44 @@ static int GPOS; typedef struct { - int type; - char *name; - int rows; - int cols; - int gpos; - int protocol; + int type; + char *name; + int rows; + int cols; + int gpos; + int protocol; } MODEL; /* Fixme #1: number of gpo's should be verified */ /* Fixme #2: protocol should be verified */ static MODEL Models[] = { - { 0x01, "LCD0821", 2, 8, 0, 1 }, - { 0x03, "LCD2021", 2, 20, 0, 1 }, - { 0x04, "LCD1641", 4, 16, 0, 1 }, - { 0x05, "LCD2041", 4, 20, 0, 1 }, - { 0x06, "LCD4021", 2, 40, 0, 1 }, - { 0x07, "LCD4041", 4, 40, 0, 1 }, - { 0x08, "LK202-25", 2, 20, 0, 2 }, - { 0x09, "LK204-25", 4, 20, 0, 2 }, - { 0x0a, "LK404-55", 4, 40, 0, 2 }, - { 0x0b, "VFD2021", 2, 20, 0, 1 }, - { 0x0c, "VFD2041", 4, 20, 0, 1 }, - { 0x0d, "VFD4021", 2, 40, 0, 1 }, - { 0x0e, "VK202-25", 2, 20, 0, 1 }, - { 0x0f, "VK204-25", 4, 20, 0, 1 }, - { 0x10, "GLC12232", -1, -1, 0, 1 }, - { 0x13, "GLC24064", -1, -1, 0, 1 }, - { 0x15, "GLK24064-25", -1, -1, 0, 1 }, - { 0x22, "GLK12232-25", -1, -1, 0, 1 }, - { 0x31, "LK404-AT", 4, 40, 0, 2 }, - { 0x32, "VFD1621", 2, 16, 0, 1 }, - { 0x33, "LK402-12", 2, 40, 0, 2 }, - { 0x34, "LK162-12", 2, 16, 0, 2 }, - { 0x35, "LK204-25PC", 4, 20, 0, 2 }, - { 0x36, "LK202-24-USB", 2, 20, 0, 2 }, - { 0x38, "LK204-24-USB", 4, 20, 0, 2 }, - { 0xff, "Unknown", -1, -1, 0, 0 } + {0x01, "LCD0821", 2, 8, 0, 1}, + {0x03, "LCD2021", 2, 20, 0, 1}, + {0x04, "LCD1641", 4, 16, 0, 1}, + {0x05, "LCD2041", 4, 20, 0, 1}, + {0x06, "LCD4021", 2, 40, 0, 1}, + {0x07, "LCD4041", 4, 40, 0, 1}, + {0x08, "LK202-25", 2, 20, 0, 2}, + {0x09, "LK204-25", 4, 20, 0, 2}, + {0x0a, "LK404-55", 4, 40, 0, 2}, + {0x0b, "VFD2021", 2, 20, 0, 1}, + {0x0c, "VFD2041", 4, 20, 0, 1}, + {0x0d, "VFD4021", 2, 40, 0, 1}, + {0x0e, "VK202-25", 2, 20, 0, 1}, + {0x0f, "VK204-25", 4, 20, 0, 1}, + {0x10, "GLC12232", -1, -1, 0, 1}, + {0x13, "GLC24064", -1, -1, 0, 1}, + {0x15, "GLK24064-25", -1, -1, 0, 1}, + {0x22, "GLK12232-25", -1, -1, 0, 1}, + {0x31, "LK404-AT", 4, 40, 0, 2}, + {0x32, "VFD1621", 2, 16, 0, 1}, + {0x33, "LK402-12", 2, 40, 0, 2}, + {0x34, "LK162-12", 2, 16, 0, 2}, + {0x35, "LK204-25PC", 4, 20, 0, 2}, + {0x36, "LK202-24-USB", 2, 20, 0, 2}, + {0x38, "LK204-24-USB", 4, 20, 0, 2}, + {0xff, "Unknown", -1, -1, 0, 0} }; @@ -269,284 +272,304 @@ static MODEL Models[] = { /*** hardware dependant functions ***/ /****************************************/ -static void drv_MO_clear (void) +static void drv_MO_clear(void) { - switch (Protocol) { - case 1: - drv_generic_serial_write ("\014", 1); /* Clear Screen */ - break; - case 2: - drv_generic_serial_write ("\376\130", 2); /* Clear Screen */ - break; - } + switch (Protocol) { + case 1: + drv_generic_serial_write("\014", 1); /* Clear Screen */ + break; + case 2: + drv_generic_serial_write("\376\130", 2); /* Clear Screen */ + break; + } } -static void drv_MO_write (const int row, const int col, const char *data, const int len) +static void drv_MO_write(const int row, const int col, const char *data, const int len) { - char cmd[5]="\376Gyx"; + char cmd[5] = "\376Gyx"; - cmd[2]=(char)col+1; - cmd[3]=(char)row+1; - drv_generic_serial_write(cmd,4); + cmd[2] = (char) col + 1; + cmd[3] = (char) row + 1; + drv_generic_serial_write(cmd, 4); - drv_generic_serial_write (data, len); + drv_generic_serial_write(data, len); } -static void drv_MO_defchar (const int ascii, const unsigned char *matrix) +static void drv_MO_defchar(const int ascii, const unsigned char *matrix) { - int i; - char cmd[11]="\376N"; - - cmd[2]=(char)ascii; - for (i = 0; i < 8; i++) { - cmd[i+3] = matrix[i] & 0x1f; - } - drv_generic_serial_write (cmd, 11); + int i; + char cmd[11] = "\376N"; + + cmd[2] = (char) ascii; + for (i = 0; i < 8; i++) { + cmd[i + 3] = matrix[i] & 0x1f; + } + drv_generic_serial_write(cmd, 11); } -static int drv_MO_contrast (int contrast) +static int drv_MO_contrast(int contrast) { - static unsigned char Contrast=0; - char cmd[3] = "\376Pn"; - - /* -1 is used to query the current contrast */ - if (contrast == -1) return Contrast; - - if (contrast < 0 ) contrast = 0; - if (contrast > 255) contrast = 255; - Contrast = contrast; - - cmd[2] = Contrast; - - drv_generic_serial_write (cmd, 3); - - return Contrast; -} + static unsigned char Contrast = 0; + char cmd[3] = "\376Pn"; + /* -1 is used to query the current contrast */ + if (contrast == -1) + return Contrast; -static int drv_MO_backlight (int backlight) -{ - static unsigned char Backlight=0; - char cmd[3] = "\376Bn"; - - /* -1 is used to query the current backlight */ - if (backlight == -1) return Backlight; - - if (backlight < 0 ) backlight = 0; - if (backlight > 255) backlight = 255; - Backlight = backlight; - - if (backlight <= 0) { - /* backlight off */ - drv_generic_serial_write ("\376F", 2); - } else { - /* backlight on for n minutes */ - cmd[2] = Backlight; - drv_generic_serial_write (cmd, 3); - } - - return Backlight; + if (contrast < 0) + contrast = 0; + if (contrast > 255) + contrast = 255; + Contrast = contrast; + + cmd[2] = Contrast; + + drv_generic_serial_write(cmd, 3); + + return Contrast; } -static int drv_MO_gpo (int num, int val) +static int drv_MO_backlight(int backlight) { - static int GPO[6] = { -1, -1, -1, -1, -1, -1 }; - char cmd[3]="\376"; - - if (num < 1) num = 1; - if (num > 6) num = 6; - - /* -1 is used to query the current GPO */ - if (val == -1) return GPO[num-1]; - - if (val < 0) val = 0; - if (val > 1) val = 1; - GPO[num-1] = val; - - switch (Protocol) { - case 1: - if (num == 1) { - if (val > 0) { - drv_generic_serial_write ("\376W", 2); /* GPO on */ - } else { - drv_generic_serial_write ("\376V", 2); /* GPO off */ - } + static unsigned char Backlight = 0; + char cmd[3] = "\376Bn"; + + /* -1 is used to query the current backlight */ + if (backlight == -1) + return Backlight; + + if (backlight < 0) + backlight = 0; + if (backlight > 255) + backlight = 255; + Backlight = backlight; + + if (backlight <= 0) { + /* backlight off */ + drv_generic_serial_write("\376F", 2); } else { - GPO[num-1] = -1; + /* backlight on for n minutes */ + cmd[2] = Backlight; + drv_generic_serial_write(cmd, 3); } - break; - - case 2: - if (val > 0) { - cmd[1] = 'W'; /* GPO on */ - } else { - cmd[1] = 'V'; /* GPO off */ + + return Backlight; +} + + +static int drv_MO_gpo(int num, int val) +{ + static int GPO[6] = { -1, -1, -1, -1, -1, -1 }; + char cmd[3] = "\376"; + + if (num < 1) + num = 1; + if (num > 6) + num = 6; + + /* -1 is used to query the current GPO */ + if (val == -1) + return GPO[num - 1]; + + if (val < 0) + val = 0; + if (val > 1) + val = 1; + GPO[num - 1] = val; + + switch (Protocol) { + case 1: + if (num == 1) { + if (val > 0) { + drv_generic_serial_write("\376W", 2); /* GPO on */ + } else { + drv_generic_serial_write("\376V", 2); /* GPO off */ + } + } else { + GPO[num - 1] = -1; + } + break; + + case 2: + if (val > 0) { + cmd[1] = 'W'; /* GPO on */ + } else { + cmd[1] = 'V'; /* GPO off */ + } + cmd[2] = (char) num; + drv_generic_serial_write(cmd, 3); + break; } - cmd[2] = (char)num; - drv_generic_serial_write (cmd, 3); - break; - } - - return GPO[num-1]; + + return GPO[num - 1]; } -static int drv_MO_pwm (int num, int val) +static int drv_MO_pwm(int num, int val) { - static int PWM[6] = { -1, -1, -1, -1, -1, -1 }; - char cmd[4]="\376\300"; - - if (num < 1) num = 1; - if (num > 6) num = 6; - - /* -1 is used to query the current PWM */ - if (val == -1) return PWM[num-1]; - - if (val < 0) val = 0; - if (val > 255) val = 255; - PWM[num-1] = val; - - cmd[2] = (char)num; - cmd[3] = (char)val; - drv_generic_serial_write (cmd, 4); - - return val; + static int PWM[6] = { -1, -1, -1, -1, -1, -1 }; + char cmd[4] = "\376\300"; + + if (num < 1) + num = 1; + if (num > 6) + num = 6; + + /* -1 is used to query the current PWM */ + if (val == -1) + return PWM[num - 1]; + + if (val < 0) + val = 0; + if (val > 255) + val = 255; + PWM[num - 1] = val; + + cmd[2] = (char) num; + cmd[3] = (char) val; + drv_generic_serial_write(cmd, 4); + + return val; } -static int drv_MO_rpm (int num) +static int drv_MO_rpm(int num) { - static int RPM[6] = { -1, -1, -1, -1, -1, -1 }; - char cmd[3] = "\376\301"; - char buffer[7]; - - if (num < 1) num = 1; - if (num > 6) num = 6; - - cmd[2] = (char)num; - drv_generic_serial_write (cmd, 3); - - usleep(100000); - drv_generic_serial_read (buffer, 7); - - debug ("rpm: buffer[0]=0x%01x", buffer[0]); - debug ("rpm: buffer[1]=0x%01x", buffer[1]); - debug ("rpm: buffer[2]=0x%01x", buffer[2]); - debug ("rpm: buffer[3]=0x%01x", buffer[3]); - debug ("rpm: buffer[4]=0x%01x", buffer[4]); - debug ("rpm: buffer[5]=0x%01x", buffer[5]); - debug ("rpm: buffer[6]=0x%01x", buffer[6]); - - /* Fixme: RPM calculations??? */ - RPM[num-1] = 42; - - return RPM[num-1]; + static int RPM[6] = { -1, -1, -1, -1, -1, -1 }; + char cmd[3] = "\376\301"; + char buffer[7]; + + if (num < 1) + num = 1; + if (num > 6) + num = 6; + + cmd[2] = (char) num; + drv_generic_serial_write(cmd, 3); + + usleep(100000); + drv_generic_serial_read(buffer, 7); + + debug("rpm: buffer[0]=0x%01x", buffer[0]); + debug("rpm: buffer[1]=0x%01x", buffer[1]); + debug("rpm: buffer[2]=0x%01x", buffer[2]); + debug("rpm: buffer[3]=0x%01x", buffer[3]); + debug("rpm: buffer[4]=0x%01x", buffer[4]); + debug("rpm: buffer[5]=0x%01x", buffer[5]); + debug("rpm: buffer[6]=0x%01x", buffer[6]); + + /* Fixme: RPM calculations??? */ + RPM[num - 1] = 42; + + return RPM[num - 1]; } -static int drv_MO_start (const char *section, const int quiet) +static int drv_MO_start(const char *section, const int quiet) { - int i; - char *model; - char buffer[256]; - - model=cfg_get(section, "Model", NULL); - if (model!=NULL && *model!='\0') { - for (i=0; Models[i].type!=0xff; i++) { - if (strcasecmp(Models[i].name, model)==0) break; - } - if (Models[i].type==0xff) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; + int i; + char *model; + char buffer[256]; + + model = cfg_get(section, "Model", NULL); + if (model != NULL && *model != '\0') { + for (i = 0; Models[i].type != 0xff; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; + } + if (Models[i].type == 0xff) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + Model = i; + info("%s: using model '%s'", Name, Models[Model].name); + } else { + info("%s: no '%s.Model' entry from %s, auto-dedecting", Name, section, cfg_source()); + Model = -1; } - Model=i; - info ("%s: using model '%s'", Name, Models[Model].name); - } else { - info ("%s: no '%s.Model' entry from %s, auto-dedecting", Name, section, cfg_source()); - Model=-1; - } - - - if (drv_generic_serial_open(section, Name, 0)<0) return -1; - - if (Model == -1 || Models[Model].protocol > 1) { - /* read module type */ - drv_generic_serial_write ("\3767", 2); - usleep(1000); - if (drv_generic_serial_read (buffer, 1)==1) { - for (i=0; Models[i].type!=0xff; i++) { - if (Models[i].type == (int)*buffer) break; - } - info ("%s: display reports model '%s' (type 0x%02x)", - Name, Models[i].name, Models[i].type); - - /* auto-dedection */ - if (Model==-1) Model=i; - - /* auto-dedection matches specified model? */ - if (Models[i].type!=0xff && Model!=i) { - error ("%s: %s.Model '%s' from %s does not match dedected Model '%s'", - Name, section, model, cfg_source(), Models[i].name); + + + if (drv_generic_serial_open(section, Name, 0) < 0) return -1; - } - } else { - info ("%s: display detection failed.", Name); + if (Model == -1 || Models[Model].protocol > 1) { + /* read module type */ + drv_generic_serial_write("\3767", 2); + usleep(1000); + if (drv_generic_serial_read(buffer, 1) == 1) { + for (i = 0; Models[i].type != 0xff; i++) { + if (Models[i].type == (int) *buffer) + break; + } + info("%s: display reports model '%s' (type 0x%02x)", Name, Models[i].name, Models[i].type); + + /* auto-dedection */ + if (Model == -1) + Model = i; + + /* auto-dedection matches specified model? */ + if (Models[i].type != 0xff && Model != i) { + error("%s: %s.Model '%s' from %s does not match dedected Model '%s'", Name, section, model, cfg_source(), Models[i].name); + return -1; + } + + } else { + info("%s: display detection failed.", Name); + } } - } - /* initialize global variables */ - DROWS = Models[Model].rows; - DCOLS = Models[Model].cols; - GPOS = Models[Model].gpos; - Protocol = Models[Model].protocol; + /* initialize global variables */ + DROWS = Models[Model].rows; + DCOLS = Models[Model].cols; + GPOS = Models[Model].gpos; + Protocol = Models[Model].protocol; + + if (Protocol > 1) { + /* read serial number */ + drv_generic_serial_write("\3765", 2); + usleep(100000); + if (drv_generic_serial_read(buffer, 2) == 2) { + info("%s: display reports serial number 0x%x", Name, *(short *) buffer); + } + + /* read version number */ + drv_generic_serial_write("\3766", 2); + usleep(100000); + if (drv_generic_serial_read(buffer, 1) == 1) { + info("%s: display reports firmware version 0x%x", Name, *buffer); + } + } - if (Protocol > 1) { - /* read serial number */ - drv_generic_serial_write ("\3765", 2); - usleep(100000); - if (drv_generic_serial_read (buffer, 2)==2) { - info ("%s: display reports serial number 0x%x", Name, *(short*)buffer); + drv_MO_clear(); + + drv_generic_serial_write("\376B", 3); /* backlight on */ + drv_generic_serial_write("\376K", 2); /* cursor off */ + drv_generic_serial_write("\376T", 2); /* blink off */ + drv_generic_serial_write("\376D", 2); /* line wrapping off */ + drv_generic_serial_write("\376R", 2); /* auto scroll off */ + + /* set contrast */ + if (cfg_number(section, "Contrast", 0, 0, 255, &i) > 0) { + drv_MO_contrast(i); } - - /* read version number */ - drv_generic_serial_write ("\3766", 2); - usleep(100000); - if (drv_generic_serial_read (buffer, 1)==1) { - info ("%s: display reports firmware version 0x%x", Name, *buffer); + + /* set backlight */ + if (cfg_number(section, "Backlight", 0, 0, 255, &i) > 0) { + drv_MO_backlight(i); } - } - - drv_MO_clear(); - - drv_generic_serial_write ("\376B", 3); /* backlight on */ - drv_generic_serial_write ("\376K", 2); /* cursor off */ - drv_generic_serial_write ("\376T", 2); /* blink off */ - drv_generic_serial_write ("\376D", 2); /* line wrapping off */ - drv_generic_serial_write ("\376R", 2); /* auto scroll off */ - - /* set contrast */ - if (cfg_number(section, "Contrast", 0, 0, 255, &i)>0) { - drv_MO_contrast(i); - } - - /* set backlight */ - if (cfg_number(section, "Backlight", 0, 0, 255, &i)>0) { - drv_MO_backlight(i); - } - - if (!quiet) { - if (drv_generic_text_greet (Models[Model].name, "MatrixOrbital")) { - sleep (3); - drv_MO_clear(); + + if (!quiet) { + if (drv_generic_text_greet(Models[Model].name, "MatrixOrbital")) { + sleep(3); + drv_MO_clear(); + } } - } - - return 0; + + return 0; } @@ -555,92 +578,92 @@ static int drv_MO_start (const char *section, const int quiet) /****************************************/ -static void plugin_contrast (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_contrast(RESULT * result, const int argc, RESULT * argv[]) { - double contrast; - - switch (argc) { - case 0: - contrast = drv_MO_contrast(-1); - SetResult(&result, R_NUMBER, &contrast); - break; - case 1: - contrast = drv_MO_contrast(R2N(argv[0])); - SetResult(&result, R_NUMBER, &contrast); - break; - default: - error ("%s::contrast(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double contrast; + + switch (argc) { + case 0: + contrast = drv_MO_contrast(-1); + SetResult(&result, R_NUMBER, &contrast); + break; + case 1: + contrast = drv_MO_contrast(R2N(argv[0])); + SetResult(&result, R_NUMBER, &contrast); + break; + default: + error("%s::contrast(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } -static void plugin_backlight (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_backlight(RESULT * result, const int argc, RESULT * argv[]) { - double backlight; - - switch (argc) { - case 0: - backlight = drv_MO_backlight(-1); - SetResult(&result, R_NUMBER, &backlight); - break; - case 1: - backlight = drv_MO_backlight(R2N(argv[0])); - SetResult(&result, R_NUMBER, &backlight); - break; - default: - error ("%s::backlight(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double backlight; + + switch (argc) { + case 0: + backlight = drv_MO_backlight(-1); + SetResult(&result, R_NUMBER, &backlight); + break; + case 1: + backlight = drv_MO_backlight(R2N(argv[0])); + SetResult(&result, R_NUMBER, &backlight); + break; + default: + error("%s::backlight(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } -static void plugin_gpo (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_gpo(RESULT * result, const int argc, RESULT * argv[]) { - double gpo; - - switch (argc) { - case 1: - gpo = drv_MO_gpo(R2N(argv[0]), -1); - SetResult(&result, R_NUMBER, &gpo); - break; - case 2: - gpo = drv_MO_gpo(R2N(argv[0]), R2N(argv[1])); - SetResult(&result, R_NUMBER, &gpo); - break; - default: - error ("%s:gpo(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double gpo; + + switch (argc) { + case 1: + gpo = drv_MO_gpo(R2N(argv[0]), -1); + SetResult(&result, R_NUMBER, &gpo); + break; + case 2: + gpo = drv_MO_gpo(R2N(argv[0]), R2N(argv[1])); + SetResult(&result, R_NUMBER, &gpo); + break; + default: + error("%s:gpo(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } -static void plugin_pwm (RESULT *result, const int argc, RESULT *argv[]) +static void plugin_pwm(RESULT * result, const int argc, RESULT * argv[]) { - double pwm; - - switch (argc) { - case 1: - pwm = drv_MO_pwm(R2N(argv[0]), -1); - SetResult(&result, R_NUMBER, &pwm); - break; - case 2: - pwm = drv_MO_pwm(R2N(argv[0]), R2N(argv[1])); - SetResult(&result, R_NUMBER, &pwm); - break; - default: - error ("%s:pwm(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double pwm; + + switch (argc) { + case 1: + pwm = drv_MO_pwm(R2N(argv[0]), -1); + SetResult(&result, R_NUMBER, &pwm); + break; + case 2: + pwm = drv_MO_pwm(R2N(argv[0]), R2N(argv[1])); + SetResult(&result, R_NUMBER, &pwm); + break; + default: + error("%s:pwm(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } -static void plugin_rpm (RESULT *result, RESULT *arg1) +static void plugin_rpm(RESULT * result, RESULT * arg1) { - double rpm; + double rpm; - rpm = drv_MO_rpm(R2N(arg1)); - SetResult(&result, R_NUMBER, &rpm); + rpm = drv_MO_rpm(R2N(arg1)); + SetResult(&result, R_NUMBER, &rpm); } @@ -659,106 +682,106 @@ static void plugin_rpm (RESULT *result, RESULT *arg1) /* list models */ -int drv_MO_list (void) +int drv_MO_list(void) { - int i; - - for (i=0; Models[i].type!=0xff; i++) { - printf ("%s ", Models[i].name); - } - return 0; + int i; + + for (i = 0; Models[i].type != 0xff; i++) { + printf("%s ", Models[i].name); + } + return 0; } /* initialize driver & display */ -int drv_MO_init (const char *section, const int quiet) +int drv_MO_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* display preferences */ - XRES=5; /* pixel width of one char */ - YRES=8; /* pixel height of one char */ - CHARS=8; /* number of user-defineable characters */ - CHAR0=0; /* ASCII of first user-defineable char */ - GOTO_COST=4; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_MO_write; - drv_generic_text_real_defchar = drv_MO_defchar; - - - /* start display */ - if ((ret=drv_MO_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - AddFunction ("LCD::contrast", -1, plugin_contrast); - AddFunction ("LCD::backlight", -1, plugin_backlight); - AddFunction ("LCD::gpo", -1, plugin_gpo); - AddFunction ("LCD::pwm", -1, plugin_pwm); - AddFunction ("LCD::rpm", 1, plugin_rpm); - - return 0; + WIDGET_CLASS wc; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 4; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_MO_write; + drv_generic_text_real_defchar = drv_MO_defchar; + + + /* start display */ + if ((ret = drv_MO_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + AddFunction("LCD::contrast", -1, plugin_contrast); + AddFunction("LCD::backlight", -1, plugin_backlight); + AddFunction("LCD::gpo", -1, plugin_gpo); + AddFunction("LCD::pwm", -1, plugin_pwm); + AddFunction("LCD::rpm", 1, plugin_rpm); + + return 0; } /* close driver & display */ -int drv_MO_quit (const int quiet) { - - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_MO_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - drv_generic_serial_close(); - - return (0); +int drv_MO_quit(const int quiet) +{ + + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_MO_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + drv_generic_serial_close(); + + return (0); } DRIVER drv_MatrixOrbital = { - name: Name, - list: drv_MO_list, - init: drv_MO_init, - quit: drv_MO_quit, + name:Name, + list:drv_MO_list, + init:drv_MO_init, + quit:drv_MO_quit, }; - diff --git a/drv_MilfordInstruments.c b/drv_MilfordInstruments.c index f1cb0e4..c18def3 100644 --- a/drv_MilfordInstruments.c +++ b/drv_MilfordInstruments.c @@ -1,4 +1,4 @@ -/* $Id: drv_MilfordInstruments.c,v 1.13 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: drv_MilfordInstruments.c,v 1.14 2005/05/08 04:32:44 reinelt Exp $ * * driver for Milford Instruments 'BPK' piggy-back serial interface board * for standard Hitachi 44780 compatible lcd modules. @@ -27,6 +27,9 @@ * * * $Log: drv_MilfordInstruments.c,v $ + * Revision 1.14 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.13 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -105,106 +108,108 @@ #include "drv_generic_serial.h" -static char Name[]="MilfordInstruments"; +static char Name[] = "MilfordInstruments"; typedef struct { - int type; - char *name; - int rows; - int cols; + int type; + char *name; + int rows; + int cols; } MODEL; static MODEL Models[] = { - { 216, "MI216", 2, 16 }, - { 220, "MI220", 2, 20 }, - { 240, "MI240", 2, 40 }, - { 420, "MI420", 4, 20 }, - { -1, "unknown", -1, -1 }, + {216, "MI216", 2, 16}, + {220, "MI220", 2, 20}, + {240, "MI240", 2, 40}, + {420, "MI420", 4, 20}, + {-1, "unknown", -1, -1}, }; -static int Model; +static int Model; /****************************************/ /*** hardware dependant functions ***/ /****************************************/ -static void drv_MI_clear (void) +static void drv_MI_clear(void) { - drv_generic_serial_write ("\376\001", 2); /* clear screen */ + drv_generic_serial_write("\376\001", 2); /* clear screen */ } -static void drv_MI_write (const int row, const int col, const char *data, const int len) +static void drv_MI_write(const int row, const int col, const char *data, const int len) { - char cmd[2] = "\376x"; - int ddbase = 128; - if (row & 1) { /* i.e. if row is 1 or 3 */ - ddbase += 64; - } - if (row & 2) { /* i.e. if row is 0 or 2. */ - ddbase += 20; - } - cmd[1] = (char)(ddbase+col); - drv_generic_serial_write(cmd,2); - - drv_generic_serial_write (data, len); + char cmd[2] = "\376x"; + int ddbase = 128; + if (row & 1) { /* i.e. if row is 1 or 3 */ + ddbase += 64; + } + if (row & 2) { /* i.e. if row is 0 or 2. */ + ddbase += 20; + } + cmd[1] = (char) (ddbase + col); + drv_generic_serial_write(cmd, 2); + + drv_generic_serial_write(data, len); } -static void drv_MI_defchar (const int ascii, const unsigned char *matrix) +static void drv_MI_defchar(const int ascii, const unsigned char *matrix) { - int i; - char cmd[10]="\376x"; - - if (ascii<8) { - cmd[1]=(char)(64+ascii*8); - for ( i=0; i<8; i++) { - cmd[i+2] = matrix[i] & 0x1f; - }; - drv_generic_serial_write (cmd, 10); - } + int i; + char cmd[10] = "\376x"; + + if (ascii < 8) { + cmd[1] = (char) (64 + ascii * 8); + for (i = 0; i < 8; i++) { + cmd[i + 2] = matrix[i] & 0x1f; + }; + drv_generic_serial_write(cmd, 10); + } } -static int drv_MI_start (const char *section, const int quiet) +static int drv_MI_start(const char *section, const int quiet) { - int i; - char *model; - - model = cfg_get(section, "Model", NULL); - if (model == NULL && *model == '\0') { - error ("%s: no '%s.Model' entry from %s", Name, section, cfg_source()); - return -1; - } - - for (i=0; Models[i].type!=0xff; i++) { - if (strcasecmp(Models[i].name, model)==0) break; - } - if (Models[i].type==0xff) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; - } - Model=i; - info ("%s: using model '%s'", Name, Models[Model].name); - - if (drv_generic_serial_open(section, Name, 0) < 0) return -1; - - /* initialize global variables */ - DROWS = Models[Model].rows; - DCOLS = Models[Model].cols; - - drv_MI_clear(); - drv_generic_serial_write ("\376\014", 2); /* cursor off */ - - if (!quiet) { - if (drv_generic_text_greet (Models[Model].name, "Milford Instruments")) { - sleep (3); - drv_MI_clear(); + int i; + char *model; + + model = cfg_get(section, "Model", NULL); + if (model == NULL && *model == '\0') { + error("%s: no '%s.Model' entry from %s", Name, section, cfg_source()); + return -1; + } + + for (i = 0; Models[i].type != 0xff; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; } - } + if (Models[i].type == 0xff) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + Model = i; + info("%s: using model '%s'", Name, Models[Model].name); + + if (drv_generic_serial_open(section, Name, 0) < 0) + return -1; - return 0; + /* initialize global variables */ + DROWS = Models[Model].rows; + DCOLS = Models[Model].cols; + + drv_MI_clear(); + drv_generic_serial_write("\376\014", 2); /* cursor off */ + + if (!quiet) { + if (drv_generic_text_greet(Models[Model].name, "Milford Instruments")) { + sleep(3); + drv_MI_clear(); + } + } + + return 0; } @@ -230,102 +235,102 @@ static int drv_MI_start (const char *section, const int quiet) /* list models */ -int drv_MI_list (void) +int drv_MI_list(void) { - int i; - - for (i = 0; Models[i].type > 0; i++) { - printf ("%s ", Models[i].name); - } - return 0; + int i; + + for (i = 0; Models[i].type > 0; i++) { + printf("%s ", Models[i].name); + } + return 0; } /* initialize driver & display */ -int drv_MI_init (const char *section, const int quiet) +int drv_MI_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = 4; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_MI_write; - drv_generic_text_real_defchar = drv_MI_defchar; - - - /* start display */ - if ((ret=drv_MI_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none at the moment... */ - - return 0; + WIDGET_CLASS wc; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 4; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_MI_write; + drv_generic_text_real_defchar = drv_MI_defchar; + + + /* start display */ + if ((ret = drv_MI_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none at the moment... */ + + return 0; } /* close driver & display */ -int drv_MI_quit (const int quiet) { - - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_MI_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - drv_generic_serial_close(); - - return (0); +int drv_MI_quit(const int quiet) +{ + + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_MI_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + drv_generic_serial_close(); + + return (0); } DRIVER drv_MilfordInstruments = { - name: Name, - list: drv_MI_list, - init: drv_MI_init, - quit: drv_MI_quit, + name:Name, + list:drv_MI_list, + init:drv_MI_init, + quit:drv_MI_quit, }; - @@ -1,4 +1,4 @@ -/* $Id: drv_NULL.c,v 1.7 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: drv_NULL.c,v 1.8 2005/05/08 04:32:44 reinelt Exp $ * * NULL driver (for testing) * @@ -23,6 +23,9 @@ * * * $Log: drv_NULL.c,v $ + * Revision 1.8 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.7 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -87,40 +90,42 @@ static char Name[] = "NULL"; /*** hardware dependant functions ***/ /****************************************/ -static void drv_NULL_write (const __attribute__((unused)) int row, - const __attribute__((unused)) int col, - const __attribute__((unused)) char *data, - const __attribute__((unused)) int len) +static void drv_NULL_write(const __attribute__ ((unused)) + int row, const __attribute__ ((unused)) + int col, const __attribute__ ((unused)) + char *data, const __attribute__ ((unused)) + int len) { - /* empty */ + /* empty */ } -static void drv_NULL_defchar (const __attribute__((unused)) int ascii, - const __attribute__((unused)) unsigned char *matrix) +static void drv_NULL_defchar(const __attribute__ ((unused)) + int ascii, const __attribute__ ((unused)) + unsigned char *matrix) { - /* empty */ + /* empty */ } -static int drv_NULL_start (const char *section) +static int drv_NULL_start(const char *section) { - char *s; - - s = cfg_get(section, "Size", "20x4"); - if (s == NULL || *s == '\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - free(s); - return -1; - } - if (sscanf(s, "%dx%d", &DCOLS, &DROWS) != 2 || DROWS < 1 || DCOLS < 1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source); + char *s; + + s = cfg_get(section, "Size", "20x4"); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + free(s); + return -1; + } + if (sscanf(s, "%dx%d", &DCOLS, &DROWS) != 2 || DROWS < 1 || DCOLS < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source); + free(s); + return -1; + } free(s); - return -1; - } - free (s); - - return 0; + + return 0; } @@ -145,77 +150,79 @@ static int drv_NULL_start (const char *section) /* list models */ -int drv_NULL_list (void) +int drv_NULL_list(void) { - printf ("generic"); - return 0; + printf("generic"); + return 0; } /* initialize driver & display */ -int drv_NULL_init (const char *section, const __attribute__((unused)) int quiet) +int drv_NULL_init(const char *section, const __attribute__ ((unused)) + int quiet) { - WIDGET_CLASS wc; - int ret; - - /* display preferences */ - XRES = 6; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = 2; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_NULL_write; - drv_generic_text_real_defchar = drv_NULL_defchar; - - /* start display */ - if ((ret = drv_NULL_start (section)) != 0) - return ret; - - /* initialize generic text driver */ - if ((ret = drv_generic_text_init(section, Name)) != 0) - return ret; - - /* initialize generic bar driver */ - if ((ret = drv_generic_text_bar_init(1)) != 0) - return ret; - - /* add fixed chars to the bar driver */ - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - drv_generic_text_bar_add_segment (255,255,255,'*'); /* asterisk */ - - /* register text widget */ - wc = Widget_Text; - wc.draw = drv_generic_text_draw; - widget_register(&wc); - - /* register bar widget */ - wc = Widget_Bar; - wc.draw = drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none at the moment... */ - - return 0; + WIDGET_CLASS wc; + int ret; + + /* display preferences */ + XRES = 6; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 2; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_NULL_write; + drv_generic_text_real_defchar = drv_NULL_defchar; + + /* start display */ + if ((ret = drv_NULL_start(section)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(1)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + drv_generic_text_bar_add_segment(255, 255, 255, '*'); /* asterisk */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none at the moment... */ + + return 0; } /* close driver & display */ -int drv_NULL_quit (const __attribute__((unused)) int quiet) { +int drv_NULL_quit(const __attribute__ ((unused)) + int quiet) +{ + + info("%s: shutting down.", Name); + drv_generic_text_quit(); - info("%s: shutting down.", Name); - drv_generic_text_quit(); - - return (0); + return (0); } DRIVER drv_NULL = { - name: Name, - list: drv_NULL_list, - init: drv_NULL_init, - quit: drv_NULL_quit, + name:Name, + list:drv_NULL_list, + init:drv_NULL_init, + quit:drv_NULL_quit, }; - diff --git a/drv_Noritake.c b/drv_Noritake.c index 9b1678d..048b3d9 100644 --- a/drv_Noritake.c +++ b/drv_Noritake.c @@ -1,4 +1,4 @@ -/* $Id: drv_Noritake.c,v 1.3 2005/05/05 08:36:12 reinelt Exp $ +/* $Id: drv_Noritake.c,v 1.4 2005/05/08 04:32:44 reinelt Exp $ * * Driver for a Noritake GU128x32-311 graphical display. * @@ -22,6 +22,9 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Log: drv_Noritake.c,v $ + * Revision 1.4 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.3 2005/05/05 08:36:12 reinelt * changed SELECT to SLCTIN * @@ -94,33 +97,33 @@ #include "drv_generic_parport.h" -static char Name[]="Noritake"; +static char Name[] = "Noritake"; typedef struct { - int type; - char *name; - int rows; - int cols; - int xres; - int yrex; - int goto_cost; - int protocol; + int type; + char *name; + int rows; + int cols; + int xres; + int yrex; + int goto_cost; + int protocol; } MODEL; -static int Model,Protocol; +static int Model, Protocol; static MODEL Models[] = { - { 0x01, "GU311", 4, 21, 6, 8, 5, 1 }, - { 0x02, "GU311_Graphic", 4, 21, 6, 8, 6, 1 }, - { 0xff, "Unknown", -1, -1, -1, -1, -1, -1 } + {0x01, "GU311", 4, 21, 6, 8, 5, 1}, + {0x02, "GU311_Graphic", 4, 21, 6, 8, 6, 1}, + {0xff, "Unknown", -1, -1, -1, -1, -1, -1} }; -static unsigned char SIGNAL_CS; /* Chip select, OUTPUT, negative logic, pport AUTOFEED */ -static unsigned char SIGNAL_WR; /* Write OUTPUT, negative logic, pport STOBE */ -static unsigned char SIGNAL_RESET; /* Reset, OUTPUT, negative logic, pport INIT */ -static unsigned char SIGNAL_BLANK; /* Blank, OUTPUT , negative logic, pport SELECT-IN */ -/* static unsigned char SIGNAL_BUSY;*/ /* Busy, INPUT , positive logic, pport BUSY, not used */ -/* static unsigned char SIGNAL_FRP;*/ /* Frame Pulse, INPUT , positive logic, pport ACK, not used */ -void (*drv_Noritake_clear) (void) ; +static unsigned char SIGNAL_CS; /* Chip select, OUTPUT, negative logic, pport AUTOFEED */ +static unsigned char SIGNAL_WR; /* Write OUTPUT, negative logic, pport STOBE */ +static unsigned char SIGNAL_RESET; /* Reset, OUTPUT, negative logic, pport INIT */ +static unsigned char SIGNAL_BLANK; /* Blank, OUTPUT , negative logic, pport SELECT-IN */ + /* static unsigned char SIGNAL_BUSY; *//* Busy, INPUT , positive logic, pport BUSY, not used */ + /* static unsigned char SIGNAL_FRP; *//* Frame Pulse, INPUT , positive logic, pport ACK, not used */ +void (*drv_Noritake_clear) (void); /* Data port is positive logic */ @@ -138,159 +141,170 @@ with a timed wait, this function is not necessary, and is kept just in case.*/ static void drv_GU311_wait_busy(void) { char c; - + c = drv_generic_parport_status(); - while ( (c & SIGNAL_BUSY) == 0 ) - { - ndelay(200); /* Wait 100ns before next consultation of BUSY line - if the first one was not successful */ - c = drv_generic_parport_status(); + while ((c & SIGNAL_BUSY) == 0) { + ndelay(200); /* Wait 100ns before next consultation of BUSY line + if the first one was not successful */ + c = drv_generic_parport_status(); } } -#endif +#endif static void drv_GU311_send_char(char c) { - //drv_GU311_wait_busy(); /* ensuite the display is ready to take the command */ - /* Disabled because all the cables does not have the busy line linked. */ - drv_generic_parport_data(c); - ndelay(30); /* delay to ensure data line stabilisation on long cables */ - drv_generic_parport_control(SIGNAL_WR,0); /* write line to enable */ - ndelay(150); /* data hold time */ - drv_generic_parport_control(SIGNAL_WR,0xff); /* write line to disable */ - ndelay(75); /* From spec : minimum time before next command */ + //drv_GU311_wait_busy(); /* ensuite the display is ready to take the command */ + /* Disabled because all the cables does not have the busy line linked. */ + drv_generic_parport_data(c); + ndelay(30); /* delay to ensure data line stabilisation on long cables */ + drv_generic_parport_control(SIGNAL_WR, 0); /* write line to enable */ + ndelay(150); /* data hold time */ + drv_generic_parport_control(SIGNAL_WR, 0xff); /* write line to disable */ + ndelay(75); /* From spec : minimum time before next command */ } -static void drv_GU311_send_string(char * str, int size) +static void drv_GU311_send_string(char *str, int size) { - int i; - for (i=0;i<size;i++) - drv_GU311_send_char(str[i]); - + int i; + for (i = 0; i < size; i++) + drv_GU311_send_char(str[i]); + } /* Command-string elaboration functions */ static void drv_GU311_make_text_string(const int row, const int col, const char *data, int len) { - static char cmd[96] = { 0x01,'C',0,0,'S',0 }; - unsigned char start_addr; - - /* Cols are 0x00..0x15, on 4 lines. */ - start_addr = ( 0x16 * row ) + col; - if ( start_addr > 0x57 ) return; - if ( len > 0x57 ) return ; - - cmd[2] = start_addr; - cmd[3] = len; - - memcpy(cmd+5,data,len); - - drv_GU311_send_string(cmd,len+5); - + static char cmd[96] = { 0x01, 'C', 0, 0, 'S', 0 }; + unsigned char start_addr; + + /* Cols are 0x00..0x15, on 4 lines. */ + start_addr = (0x16 * row) + col; + if (start_addr > 0x57) + return; + if (len > 0x57) + return; + + cmd[2] = start_addr; + cmd[3] = len; + + memcpy(cmd + 5, data, len); + + drv_GU311_send_string(cmd, len + 5); + } /* API functions */ -static void drv_GU311_clear (void) +static void drv_GU311_clear(void) { - static char clear_cmd[] = { 0x01, 'O', 'P' }; - drv_GU311_send_string( clear_cmd, sizeof(clear_cmd) ); - ndelay(500); /* Delay for execution - this command is the longuest */ + static char clear_cmd[] = { 0x01, 'O', 'P' }; + drv_GU311_send_string(clear_cmd, sizeof(clear_cmd)); + ndelay(500); /* Delay for execution - this command is the longuest */ } -static void drv_GU311_write (const int row, const int col, const char *data, int len) +static void drv_GU311_write(const int row, const int col, const char *data, int len) { - drv_GU311_make_text_string(row,col,data, len); + drv_GU311_make_text_string(row, col, data, len); } -static void drv_GU311_reset (void) +static void drv_GU311_reset(void) { - drv_generic_parport_control(SIGNAL_RESET,0); /* initiate reset */ - ndelay(1000); /* reset hold time 1ms */ - drv_generic_parport_control(SIGNAL_RESET,0xff); - ndelay(200000); /* reset ready time 200ms */ + drv_generic_parport_control(SIGNAL_RESET, 0); /* initiate reset */ + ndelay(1000); /* reset hold time 1ms */ + drv_generic_parport_control(SIGNAL_RESET, 0xff); + ndelay(200000); /* reset ready time 200ms */ } static int drv_GU311_start(const char *section) { - char cmd[3] = { 0x01, 'O' }; - - /* Parallel port opening and association */ - if (drv_generic_parport_open(section, Name) < 0) return -1; - if ((SIGNAL_CS=drv_generic_parport_wire_ctrl ("CS", "AUTOFD"))==0xff) return -1; - if ((SIGNAL_WR=drv_generic_parport_wire_ctrl ("WR", "STROBE"))==0xff) return -1; - if ((SIGNAL_RESET=drv_generic_parport_wire_ctrl ("RESET", "INIT"))==0xff) return -1; - if ((SIGNAL_BLANK=drv_generic_parport_wire_ctrl ("BLANK", "SLCTIN") )==0xff) return -1; - /* SIGNAL_BUSY=PARPORT_STATUS_BUSY; */ /* Not currently needed */ - /* SIGNAL_FRP=PARPORT_STATUS_ACK; */ /* Not currently needed */ - - /* Signals configuration */ - drv_generic_parport_direction(0); /* parallel port in output mode */ - drv_generic_parport_control(SIGNAL_CS|SIGNAL_WR|SIGNAL_RESET|SIGNAL_BLANK, 0xff); - /* All lines to "deactivate", -> 1 level on the wire */ - drv_generic_parport_control(SIGNAL_CS,0); /* CS to 0 all the time, write done by WR */ - drv_GU311_reset(); - - /* Ready for commands from now on. */ - - /* Display configuration */ - cmd[2] = '0' ; drv_GU311_send_string(cmd, sizeof(cmd) ); /* Select char. page 0 */ - cmd[2] = 'Q' ; drv_GU311_send_string(cmd, sizeof(cmd) ); /* Select 'Quick Mode' */ - cmd[2] = 'a' ; drv_GU311_send_string(cmd, sizeof(cmd) ); /* Brightness at 100% */ - cmd[2] = 'T' ; drv_GU311_send_string(cmd, sizeof(cmd) ); /* Ensure display ON */ - - drv_Noritake_clear(); - return 0; + char cmd[3] = { 0x01, 'O' }; + + /* Parallel port opening and association */ + if (drv_generic_parport_open(section, Name) < 0) + return -1; + if ((SIGNAL_CS = drv_generic_parport_wire_ctrl("CS", "AUTOFD")) == 0xff) + return -1; + if ((SIGNAL_WR = drv_generic_parport_wire_ctrl("WR", "STROBE")) == 0xff) + return -1; + if ((SIGNAL_RESET = drv_generic_parport_wire_ctrl("RESET", "INIT")) == 0xff) + return -1; + if ((SIGNAL_BLANK = drv_generic_parport_wire_ctrl("BLANK", "SLCTIN")) == 0xff) + return -1; + /* SIGNAL_BUSY=PARPORT_STATUS_BUSY; *//* Not currently needed */ + /* SIGNAL_FRP=PARPORT_STATUS_ACK; *//* Not currently needed */ + + /* Signals configuration */ + drv_generic_parport_direction(0); /* parallel port in output mode */ + drv_generic_parport_control(SIGNAL_CS | SIGNAL_WR | SIGNAL_RESET | SIGNAL_BLANK, 0xff); + /* All lines to "deactivate", -> 1 level on the wire */ + drv_generic_parport_control(SIGNAL_CS, 0); /* CS to 0 all the time, write done by WR */ + drv_GU311_reset(); + + /* Ready for commands from now on. */ + + /* Display configuration */ + cmd[2] = '0'; + drv_GU311_send_string(cmd, sizeof(cmd)); /* Select char. page 0 */ + cmd[2] = 'Q'; + drv_GU311_send_string(cmd, sizeof(cmd)); /* Select 'Quick Mode' */ + cmd[2] = 'a'; + drv_GU311_send_string(cmd, sizeof(cmd)); /* Brightness at 100% */ + cmd[2] = 'T'; + drv_GU311_send_string(cmd, sizeof(cmd)); /* Ensure display ON */ + + drv_Noritake_clear(); + return 0; } -static int drv_Noritake_start (const char *section) +static int drv_Noritake_start(const char *section) { - char * model=0; - int i; - model=cfg_get(section, "Model", NULL); - if (model!=NULL && *model!='\0') { - for (i=0; Models[i].type!=0xff; i++) { - if (strcasecmp(Models[i].name, model)==0) break; - } - if (Models[i].type==0xff) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; - } - Model=i; - info ("%s: using model '%s'", Name, Models[Model].name); - } else { - error ("%s: no '%s.Model' entry from %s", Name, section, cfg_source()); - return -1; - } - - DROWS = Models[Model].rows; - DCOLS = Models[Model].cols; - XRES = Models[Model].xres; - YRES = Models[Model].xres; - GOTO_COST = Models[Model].goto_cost; - Protocol = Models[Model].protocol; + char *model = 0; + int i; + model = cfg_get(section, "Model", NULL); + if (model != NULL && *model != '\0') { + for (i = 0; Models[i].type != 0xff; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; + } + if (Models[i].type == 0xff) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + Model = i; + info("%s: using model '%s'", Name, Models[Model].name); + } else { + error("%s: no '%s.Model' entry from %s", Name, section, cfg_source()); + return -1; + } + + DROWS = Models[Model].rows; + DCOLS = Models[Model].cols; + XRES = Models[Model].xres; + YRES = Models[Model].xres; + GOTO_COST = Models[Model].goto_cost; + Protocol = Models[Model].protocol; /* display preferences */ - CHARS = 0; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - - - - /* real worker functions */ - drv_Noritake_clear = drv_GU311_clear; - if ( Models[Model].type == 0x01 ) { - drv_generic_text_real_write = drv_GU311_write; - } else { - error("%s: Unsupported display. Currently supported are : GU311.",Name); - return -1; - } - return drv_GU311_start(section); + CHARS = 0; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + + + + /* real worker functions */ + drv_Noritake_clear = drv_GU311_clear; + if (Models[Model].type == 0x01) { + drv_generic_text_real_write = drv_GU311_write; + } else { + error("%s: Unsupported display. Currently supported are : GU311.", Name); + return -1; + } + return drv_GU311_start(section); } @@ -315,72 +329,71 @@ static int drv_Noritake_start (const char *section) /* list models */ -int drv_Noritake_list (void) +int drv_Noritake_list(void) { - printf ("GU311 GU311_Graphic"); - return 0; + printf("GU311 GU311_Graphic"); + return 0; } /* initialize driver & display */ -int drv_Noritake_init (const char *section, const int quiet) +int drv_Noritake_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* start display */ - if ((ret=drv_Noritake_start (section))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register plugins */ - /* none */ - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, NULL)) { - sleep (3); - drv_Noritake_clear(); + WIDGET_CLASS wc; + int ret; + + /* start display */ + if ((ret = drv_Noritake_start(section)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register plugins */ + /* none */ + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, NULL)) { + sleep(3); + drv_Noritake_clear(); + } } - } - return 0; + return 0; } /* close driver & display */ -int drv_Noritake_quit (const int quiet) +int drv_Noritake_quit(const int quiet) { - info("%s: shutting down.", Name); - - /* clear display */ - drv_Noritake_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - drv_generic_parport_close(); - drv_generic_text_quit(); - return (0); + info("%s: shutting down.", Name); + + /* clear display */ + drv_Noritake_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + drv_generic_parport_close(); + drv_generic_text_quit(); + return (0); } DRIVER drv_Noritake = { - name: Name, - list: drv_Noritake_list, - init: drv_Noritake_init, - quit: drv_Noritake_quit, + name:Name, + list:drv_Noritake_list, + init:drv_Noritake_init, + quit:drv_Noritake_quit, }; - diff --git a/drv_RouterBoard.c b/drv_RouterBoard.c index e22e69b..fc65d5a 100644 --- a/drv_RouterBoard.c +++ b/drv_RouterBoard.c @@ -1,4 +1,4 @@ -/* $Id: drv_RouterBoard.c,v 1.4 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: drv_RouterBoard.c,v 1.5 2005/05/08 04:32:44 reinelt Exp $ * * driver for the "Router Board LCD port" * see port details at http://www.routerboard.com @@ -26,6 +26,9 @@ * * * $Log: drv_RouterBoard.c,v $ + * Revision 1.5 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.4 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -134,23 +137,23 @@ /* #define RB_WITH_LEDS 1 */ -#ifdef RB_WITH_LEDS /* Build without socket&led support */ +#ifdef RB_WITH_LEDS /* Build without socket&led support */ #include <arpa/inet.h> #include <sys/socket.h> #include <sys/poll.h> -#define POLL_TIMEOUT 10 /* Socket poll timeout */ -#define MAXMSG_SIZE 100 /* Max messagge we can receive */ +#define POLL_TIMEOUT 10 /* Socket poll timeout */ +#define MAXMSG_SIZE 100 /* Max messagge we can receive */ -static int sock_c; /* Socket handler */ +static int sock_c; /* Socket handler */ static struct sockaddr_in *sacl; -char sock_msg[MAXMSG_SIZE]; +char sock_msg[MAXMSG_SIZE]; #endif -static char Name[]="RouterBoard"; +static char Name[] = "RouterBoard"; static int Model; static int Capabilities; @@ -172,12 +175,12 @@ static int Capabilities; * we use the worst-case values. */ -#define T_INIT1 4100 /* first init sequence: 4.1 msec */ -#define T_INIT2 100 /* second init sequence: 100 usec */ -#define T_EXEC 80 /* normal execution time */ -#define T_WRCG 120 /* CG RAM Write */ -#define T_CLEAR 1680 /* Clear Display */ -#define T_AS 60 /* Address setup time */ +#define T_INIT1 4100 /* first init sequence: 4.1 msec */ +#define T_INIT2 100 /* second init sequence: 100 usec */ +#define T_EXEC 80 /* normal execution time */ +#define T_WRCG 120 /* CG RAM Write */ +#define T_CLEAR 1680 /* Clear Display */ +#define T_AS 60 /* Address setup time */ /* Fixme: GPO's not yet implemented */ static int GPOS; @@ -189,17 +192,17 @@ static unsigned RB_Leds = 0; static unsigned RB_BackLight = 0; typedef struct { - int type; - char *name; - int capabilities; + int type; + char *name; + int capabilities; } MODEL; #define CAP_HD66712 (1<<0) static MODEL Models[] = { - { 0x01, "HD44780", 0 }, - { 0x02, "HD66712", CAP_HD66712 }, - { 0xff, "Unknown", 0 } + {0x01, "HD44780", 0}, + {0x02, "HD66712", CAP_HD66712}, + {0xff, "Unknown", 0} }; @@ -209,48 +212,48 @@ static MODEL Models[] = { #ifdef RB_WITH_LEDS -static int drv_RB_sock_init() +static int drv_RB_sock_init() { - - if ((sacl = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in))) == NULL) { - return -1; - } - - memset(sacl, 0, sizeof(struct sockaddr_in)); - sacl->sin_family = AF_INET; - sacl->sin_port = htons(3333);//Listen Port - sacl->sin_addr.s_addr = inet_addr("127.0.0.1");//Listen Address - - if ((sock_c = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - error ("Socket open failed"); - free(sacl); - return -1; - } - - if (bind(sock_c, (struct sockaddr *) sacl, sizeof(struct sockaddr_in)) < 0) { - error ("Socket bind open failed"); - free(sacl); - return -1; - } - return 0; + + if ((sacl = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in))) == NULL) { + return -1; + } + + memset(sacl, 0, sizeof(struct sockaddr_in)); + sacl->sin_family = AF_INET; + sacl->sin_port = htons(3333); //Listen Port + sacl->sin_addr.s_addr = inet_addr("127.0.0.1"); //Listen Address + + if ((sock_c = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + error("Socket open failed"); + free(sacl); + return -1; + } + + if (bind(sock_c, (struct sockaddr *) sacl, sizeof(struct sockaddr_in)) < 0) { + error("Socket bind open failed"); + free(sacl); + return -1; + } + return 0; } -static void drv_RB_poll_data ( void __attribute__((unused)) *notused) +static void drv_RB_poll_data(void __attribute__ ((unused)) * notused) { - int len,size; - struct pollfd usfd; - usfd.fd = sock_c; - usfd.events = POLLIN | POLLPRI; - while (poll(&usfd, 1, POLL_TIMEOUT) > 0) { - len = sizeof(struct sockaddr_in); - if ((size = recvfrom(sock_c, sock_msg, MAXMSG_SIZE, 0, (struct sockaddr *) sacl,(socklen_t*) &len)) < 0); - else { - RB_Leds = sock_msg[0]&0x0F; - RB_Leds = RB_Leds << 12; - /* fprintf(stderr, "Received data %s\n",sock_msg); */ + int len, size; + struct pollfd usfd; + usfd.fd = sock_c; + usfd.events = POLLIN | POLLPRI; + while (poll(&usfd, 1, POLL_TIMEOUT) > 0) { + len = sizeof(struct sockaddr_in); + if ((size = recvfrom(sock_c, sock_msg, MAXMSG_SIZE, 0, (struct sockaddr *) sacl, (socklen_t *) & len)) < 0); + else { + RB_Leds = sock_msg[0] & 0x0F; + RB_Leds = RB_Leds << 12; + /* fprintf(stderr, "Received data %s\n",sock_msg); */ + } } - } } #endif @@ -258,251 +261,258 @@ static void drv_RB_poll_data ( void __attribute__((unused)) *notused) /* IOCS0 port number can read from PCI Configuration Space Function 0 (F0) */ /* at index 74h as 16 bit value (see [GEODE] 5.3.1 pg.151 and pg.176 Table 5-29 */ -static unsigned getIocs0Port (void) +static unsigned getIocs0Port(void) { - static unsigned ret = 0; + static unsigned ret = 0; - /*get IO permission, here you can't use ioperm command */ - iopl(3); + /*get IO permission, here you can't use ioperm command */ + iopl(3); - if (!ret) { - outl(0x80009074, CAR); - ret = inw(CDR); - } - return ret; -} + if (!ret) { + outl(0x80009074, CAR); + ret = inw(CDR); + } + return ret; +} -static int drv_RB_backlight ( int backlight) +static int drv_RB_backlight(int backlight) { - /* -1 is used to query the current Backlight */ - if(backlight == -1 ) { - if (RB_BackLight > 0) return 1; // because RB_Backlight actually is 0x0800 or 0 - return 0; - } - - if (backlight < 0) backlight = 0; - if (backlight > 1) backlight = 1; + /* -1 is used to query the current Backlight */ + if (backlight == -1) { + if (RB_BackLight > 0) + return 1; // because RB_Backlight actually is 0x0800 or 0 + return 0; + } - RB_BackLight = backlight ? LCD_BACKLIGHT : 0; - - /* Set backlight output */ - outw( RB_Leds | RB_BackLight , getIocs0Port()); - - return backlight; + if (backlight < 0) + backlight = 0; + if (backlight > 1) + backlight = 1; + + RB_BackLight = backlight ? LCD_BACKLIGHT : 0; + + /* Set backlight output */ + outw(RB_Leds | RB_BackLight, getIocs0Port()); + + return backlight; } -static void drv_RB_command ( const unsigned char cmd, const int delay) +static void drv_RB_command(const unsigned char cmd, const int delay) { - outw( RB_Leds | LCD_INITX | RB_BackLight | cmd, getIocs0Port()); + outw(RB_Leds | LCD_INITX | RB_BackLight | cmd, getIocs0Port()); + + ndelay(T_AS); + + outw(RB_Leds | RB_BackLight | cmd, getIocs0Port()); + + // wait for command completion + udelay(delay); - ndelay(T_AS); - - outw( RB_Leds | RB_BackLight | cmd, getIocs0Port()); - - // wait for command completion - udelay(delay); - } -static void drv_RB_data ( const char *string, const int len, const int delay) +static void drv_RB_data(const char *string, const int len, const int delay) { - int l = len; - unsigned char ch; + int l = len; + unsigned char ch; - /* sanity check */ - if (len <= 0) return; + /* sanity check */ + if (len <= 0) + return; - while (l--) { + while (l--) { - ch = *(string++); - - outw( RB_Leds | LCD_AFDX | LCD_INITX | RB_BackLight | ch, getIocs0Port()); + ch = *(string++); - ndelay(T_AS); - - outw( RB_Leds | LCD_AFDX | RB_BackLight | ch, getIocs0Port()); + outw(RB_Leds | LCD_AFDX | LCD_INITX | RB_BackLight | ch, getIocs0Port()); - // wait for command completion - udelay(delay); + ndelay(T_AS); + + outw(RB_Leds | LCD_AFDX | RB_BackLight | ch, getIocs0Port()); + + // wait for command completion + udelay(delay); - } + } } -static void drv_RB_clear (void) +static void drv_RB_clear(void) { - drv_RB_command ( 0x01, T_CLEAR); + drv_RB_command(0x01, T_CLEAR); } -static void drv_RB_goto (int row, int col) +static void drv_RB_goto(int row, int col) { - int pos; - - /* 16x1 Displays are organized as 8x2 :-( */ - if (DCOLS==16 && DROWS==1 && col>7) { - row++; - col-=8; - } - - if (Capabilities & CAP_HD66712) { - /* the HD66712 doesn't have a braindamadged RAM layout */ - pos = row*32 + col; - } else { - /* 16x4 Displays use a slightly different layout */ - if (DCOLS==16 && DROWS==4) { - pos = (row%2)*64+(row/2)*16+col; - } else { - pos = (row%2)*64+(row/2)*20+col; + int pos; + + /* 16x1 Displays are organized as 8x2 :-( */ + if (DCOLS == 16 && DROWS == 1 && col > 7) { + row++; + col -= 8; } - } - drv_RB_command ( (0x80|pos), T_EXEC); + + if (Capabilities & CAP_HD66712) { + /* the HD66712 doesn't have a braindamadged RAM layout */ + pos = row * 32 + col; + } else { + /* 16x4 Displays use a slightly different layout */ + if (DCOLS == 16 && DROWS == 4) { + pos = (row % 2) * 64 + (row / 2) * 16 + col; + } else { + pos = (row % 2) * 64 + (row / 2) * 20 + col; + } + } + drv_RB_command((0x80 | pos), T_EXEC); } -static void drv_RB_write (const int row, const int col, const char *data, const int len) +static void drv_RB_write(const int row, const int col, const char *data, const int len) { - drv_RB_goto (row, col); - drv_RB_data ( data, len, T_EXEC); + drv_RB_goto(row, col); + drv_RB_data(data, len, T_EXEC); } -static void drv_RB_defchar (const int ascii, const unsigned char *matrix) +static void drv_RB_defchar(const int ascii, const unsigned char *matrix) { - int i; - char buffer[8]; + int i; + char buffer[8]; - for (i = 0; i < 8; i++) { - buffer[i] = matrix[i] & 0x1f; - } - - drv_RB_command ( 0x40|8*ascii, T_EXEC); - drv_RB_data ( buffer, 8, T_WRCG); + for (i = 0; i < 8; i++) { + buffer[i] = matrix[i] & 0x1f; + } + + drv_RB_command(0x40 | 8 * ascii, T_EXEC); + drv_RB_data(buffer, 8, T_WRCG); } - + /* Fixme: GPO's */ #if 0 -static void drv_RB_setGPO (const int bits) -{} +static void drv_RB_setGPO(const int bits) +{ +} #endif -static int drv_RB_start (const char *section, const int quiet) +static int drv_RB_start(const char *section, const int quiet) { - char *model, *strsize; - int rows=-1, cols=-1, gpos=-1; - int l; - - model=cfg_get(section, "Model", "HD44780"); - if (model!=NULL && *model!='\0') { - int i; - for (i=0; Models[i].type!=0xff; i++) { - if (strcasecmp(Models[i].name, model)==0) break; + char *model, *strsize; + int rows = -1, cols = -1, gpos = -1; + int l; + + model = cfg_get(section, "Model", "HD44780"); + if (model != NULL && *model != '\0') { + int i; + for (i = 0; Models[i].type != 0xff; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; + } + if (Models[i].type == 0xff) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + Model = i; + Capabilities = Models[Model].capabilities; + info("%s: using model '%s'", Name, Models[Model].name); + } else { + error("%s: empty '%s.Model' entry from %s", Name, section, cfg_source()); + return -1; } - if (Models[i].type==0xff) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; + free(model); + + strsize = cfg_get(section, "Size", NULL); + if (strsize == NULL || *strsize == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + free(strsize); + return -1; + } + if (sscanf(strsize, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, strsize, cfg_source()); + free(strsize); + return -1; } - Model=i; - Capabilities=Models[Model].capabilities; - info ("%s: using model '%s'", Name, Models[Model].name); - } else { - error ("%s: empty '%s.Model' entry from %s", Name, section, cfg_source()); - return -1; - } - free(model); - - strsize = cfg_get(section, "Size", NULL); - if (strsize == NULL || *strsize == '\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - free(strsize); - return -1; - } - if (sscanf(strsize,"%dx%d",&cols,&rows)!=2 || rows<1 || cols<1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, strsize, cfg_source()); free(strsize); - return -1; - } - free(strsize); - - /*set backlight*/ - if (cfg_number(section, "Backlight", 1, 0, 1, &l) > 0 ) { - drv_RB_backlight(l); - } - - if (cfg_number(section, "GPOs", 0, 0, 8, &gpos)<0) return -1; - info ("%s: controlling %d GPO's", Name, gpos); - + + /*set backlight */ + if (cfg_number(section, "Backlight", 1, 0, 1, &l) > 0) { + drv_RB_backlight(l); + } + + if (cfg_number(section, "GPOs", 0, 0, 8, &gpos) < 0) + return -1; + info("%s: controlling %d GPO's", Name, gpos); + #ifdef RB_WITH_LEDS - - if( drv_RB_sock_init() < 0 ) - { - error ("Sock error"); - return -1; - } else timer_add (drv_RB_poll_data, NULL, 500, 0); - + + if (drv_RB_sock_init() < 0) { + error("Sock error"); + return -1; + } else + timer_add(drv_RB_poll_data, NULL, 500, 0); + #endif - - DROWS = rows; - DCOLS = cols; - GPOS = gpos; - - drv_RB_command (0x30, T_INIT1); /* 8 Bit mode, wait 4.1 ms */ - drv_RB_command (0x30, T_INIT2); /* 8 Bit mode, wait 100 us */ - drv_RB_command (0x38, T_EXEC); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ - - drv_RB_command (0x08, T_EXEC); /* Display off, cursor off, blink off */ - drv_RB_command (0x0c, T_CLEAR); /* Display on, cursor off, blink off, wait 1.64 ms */ - drv_RB_command (0x06, T_EXEC); /* curser moves to right, no shift */ - - if ((Capabilities & CAP_HD66712) && DROWS > 2) { - drv_RB_command ( 0x3c, T_EXEC); /* set extended register enable bit */ - drv_RB_command ( 0x09, T_EXEC); /* set 4-line mode */ - drv_RB_command ( 0x38, T_EXEC); /* clear extended register enable bit */ - } - - drv_RB_clear(); - drv_RB_command (0x03, T_CLEAR); /* return home */ - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, NULL)) { - sleep (3); - drv_RB_clear(); + + DROWS = rows; + DCOLS = cols; + GPOS = gpos; + + drv_RB_command(0x30, T_INIT1); /* 8 Bit mode, wait 4.1 ms */ + drv_RB_command(0x30, T_INIT2); /* 8 Bit mode, wait 100 us */ + drv_RB_command(0x38, T_EXEC); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ + + drv_RB_command(0x08, T_EXEC); /* Display off, cursor off, blink off */ + drv_RB_command(0x0c, T_CLEAR); /* Display on, cursor off, blink off, wait 1.64 ms */ + drv_RB_command(0x06, T_EXEC); /* curser moves to right, no shift */ + + if ((Capabilities & CAP_HD66712) && DROWS > 2) { + drv_RB_command(0x3c, T_EXEC); /* set extended register enable bit */ + drv_RB_command(0x09, T_EXEC); /* set 4-line mode */ + drv_RB_command(0x38, T_EXEC); /* clear extended register enable bit */ } - } - - return 0; + + drv_RB_clear(); + drv_RB_command(0x03, T_CLEAR); /* return home */ + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, NULL)) { + sleep(3); + drv_RB_clear(); + } + } + + return 0; } /****************************************/ /*** plugins ***/ /****************************************/ -static void plugin_backlight (RESULT *result , const int argc, RESULT *argv[]) +static void plugin_backlight(RESULT * result, const int argc, RESULT * argv[]) { - double backlight; - - switch (argc) { - case 0: - backlight = drv_RB_backlight(-1); - SetResult(&result, R_NUMBER, &backlight); - break; - case 1: - backlight = drv_RB_backlight(R2N(argv[0])); - SetResult(&result, R_NUMBER, &backlight); - break; - default: - error ("%s::backlight(): wrong number of parameters", Name); - SetResult(&result, R_STRING, ""); - } + double backlight; + + switch (argc) { + case 0: + backlight = drv_RB_backlight(-1); + SetResult(&result, R_NUMBER, &backlight); + break; + case 1: + backlight = drv_RB_backlight(R2N(argv[0])); + SetResult(&result, R_NUMBER, &backlight); + break; + default: + error("%s::backlight(): wrong number of parameters", Name); + SetResult(&result, R_STRING, ""); + } } @@ -523,113 +533,113 @@ static void plugin_backlight (RESULT *result , const int argc, RESULT *argv[]) /* list models */ -int drv_RB_list (void) +int drv_RB_list(void) { - int i; - - for (i=0; Models[i].type!=0xff; i++) { - printf ("%s ", Models[i].name); - } - return 0; + int i; + + for (i = 0; Models[i].type != 0xff; i++) { + printf("%s ", Models[i].name); + } + return 0; } /* initialize driver & display */ -int drv_RB_init (const char *section, const int quiet) +int drv_RB_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int asc255bug; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = 2; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_RB_write; - drv_generic_text_real_defchar = drv_RB_defchar; + WIDGET_CLASS wc; + int asc255bug; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 2; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_RB_write; + drv_generic_text_real_defchar = drv_RB_defchar; + + + /* start display */ + if ((ret = drv_RB_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + /* most displays have a full block on ascii 255, but some have kind of */ + /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ + /* char will not be used, but rendered by the bar driver */ + cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + if (!asc255bug) + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + AddFunction("LCD::backlight", -1, plugin_backlight); - - /* start display */ - if ((ret=drv_RB_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - /* most displays have a full block on ascii 255, but some have kind of */ - /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ - /* char will not be used, but rendered by the bar driver */ - cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - if (!asc255bug) - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins*/ - AddFunction ("LCD::backlight", -1, plugin_backlight); - - return 0; + return 0; } /* close driver & display */ -int drv_RB_quit (const int quiet) { +int drv_RB_quit(const int quiet) +{ - info("%s: shutting down.", Name); + info("%s: shutting down.", Name); - drv_generic_text_quit(); + drv_generic_text_quit(); - /* clear *both* displays */ - drv_RB_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } + /* clear *both* displays */ + drv_RB_clear(); + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } #ifdef RB_WITH_LEDS - close(sock_c); - free(sacl); /*close network socket*/ + close(sock_c); + free(sacl); /*close network socket */ -#endif - - return (0); +#endif + + return (0); } DRIVER drv_RouterBoard = { - name: Name, - list: drv_RB_list, - init: drv_RB_init, - quit: drv_RB_quit, + name:Name, + list:drv_RB_list, + init:drv_RB_init, + quit:drv_RB_quit, }; diff --git a/drv_SimpleLCD.c b/drv_SimpleLCD.c index 017b5da..872eff5 100644 --- a/drv_SimpleLCD.c +++ b/drv_SimpleLCD.c @@ -1,4 +1,4 @@ -/* $Id: drv_SimpleLCD.c,v 1.3 2005/04/20 05:49:21 reinelt Exp $ +/* $Id: drv_SimpleLCD.c,v 1.4 2005/05/08 04:32:44 reinelt Exp $ * * driver for a simple serial terminal. * @@ -22,6 +22,9 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Log: drv_SimpleLCD.c,v $ + * Revision 1.4 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.3 2005/04/20 05:49:21 reinelt * Changed the code to add some VT100-compatible control sequences (see the comments above). * A configfile boolean option 'VT100_Support' (default to 1) indicate if the display in @@ -110,39 +113,43 @@ #include "drv_generic_serial.h" -static char Name[]="SimpleLCD"; -static char *backbuffer=0; -static int backbuffer_size=0; -static int vt100_mode=0; -static unsigned char bar_char=0; +static char Name[] = "SimpleLCD"; +static char *backbuffer = 0; +static int backbuffer_size = 0; +static int vt100_mode = 0; +static unsigned char bar_char = 0; /****************************************/ /*** hardware dependant functions ***/ /****************************************/ /** No clear function on SimpleLCD : Just send CR-LF * number of lines **/ -void drv_SL_simple_clear (void) +void drv_SL_simple_clear(void) { - char cmd[2]; - int i; - cmd[0] = '\r' ; cmd[1] = '\n'; - for (i=0;i<DROWS;++i) { - drv_generic_serial_write (cmd, 2); - } - memset(backbuffer,' ',backbuffer_size); + char cmd[2]; + int i; + cmd[0] = '\r'; + cmd[1] = '\n'; + for (i = 0; i < DROWS; ++i) { + drv_generic_serial_write(cmd, 2); + } + memset(backbuffer, ' ', backbuffer_size); } /** vt-100 mode : send the ESC-code **/ -void drv_SL_vt100_clear (void) +void drv_SL_vt100_clear(void) { - char cmd[4]; - cmd[0] = 0x1b; cmd[1] = '[' ; cmd[2] = '2' ; cmd[3] = 'J'; - drv_generic_serial_write (cmd, 4); + char cmd[4]; + cmd[0] = 0x1b; + cmd[1] = '['; + cmd[2] = '2'; + cmd[3] = 'J'; + drv_generic_serial_write(cmd, 4); } -void drv_SL_clear (void) +void drv_SL_clear(void) { - vt100_mode==1?drv_SL_vt100_clear():drv_SL_simple_clear(); + vt100_mode == 1 ? drv_SL_vt100_clear() : drv_SL_simple_clear(); } @@ -151,103 +158,105 @@ void drv_SL_clear (void) */ static void drv_SL_commit(int full_commit) { - int row; - char cmd[2] = { '\r', '\n' }; - if (full_commit) { - for (row=0;row < DROWS; row++) { - drv_generic_serial_write (cmd, 2); - drv_generic_serial_write(backbuffer+(DCOLS*row),DCOLS); + int row; + char cmd[2] = { '\r', '\n' }; + if (full_commit) { + for (row = 0; row < DROWS; row++) { + drv_generic_serial_write(cmd, 2); + drv_generic_serial_write(backbuffer + (DCOLS * row), DCOLS); + } + } else { + drv_generic_serial_write(cmd, 1); /* Go to the beginning of the line only */ + drv_generic_serial_write(backbuffer + (DCOLS * (DROWS - 1)), DCOLS); } - } else { - drv_generic_serial_write (cmd, 1); /* Go to the beginning of the line only */ - drv_generic_serial_write(backbuffer+(DCOLS*(DROWS-1)),DCOLS); - } } -static void drv_SL_simple_write (const int row, const int col, const char *data, int len) +static void drv_SL_simple_write(const int row, const int col, const char *data, int len) { - memcpy(backbuffer+(row*DCOLS)+col,data,len); - if (row == DROWS-1) - drv_SL_commit(0); - else - drv_SL_commit(1); + memcpy(backbuffer + (row * DCOLS) + col, data, len); + if (row == DROWS - 1) + drv_SL_commit(0); + else + drv_SL_commit(1); } -static void drv_SL_vt100_write (const int row, const int col, const char *data, int len) +static void drv_SL_vt100_write(const int row, const int col, const char *data, int len) { - char cmd[8]; - cmd[0] = 0x1b; cmd[1] = '['; - cmd[2] = row + '1'; cmd[3] = ';'; - cmd[4] = (col/10) + '0' ; cmd[5] = (col%10) + '1' ; + char cmd[8]; + cmd[0] = 0x1b; + cmd[1] = '['; + cmd[2] = row + '1'; + cmd[3] = ';'; + cmd[4] = (col / 10) + '0'; + cmd[5] = (col % 10) + '1'; cmd[6] = 'H'; - drv_generic_serial_write (cmd, 7); - drv_generic_serial_write (data,len); + drv_generic_serial_write(cmd, 7); + drv_generic_serial_write(data, len); } -static int drv_SL_start (const char *section, const int quiet) +static int drv_SL_start(const char *section, const int quiet) { - int rows=-1, cols=-1; - unsigned int flags=0; - char *s; - char * model=0; - - vt100_mode = 0; - model = cfg_get(section,"Model","generic"); - if (model != NULL && *model != '\0') - { - if ( strcasecmp("vt100",model) == 0 ) - vt100_mode = 1; - } - - cfg_number(section,"BarCharValue",0,0,255,&flags); - bar_char = flags; - cfg_number(section,"Options",0,0,0xffff,&flags); - if (drv_generic_serial_open(section, Name, flags) < 0) return -1; - - s=cfg_get(section, "Size", NULL); - if (s==NULL || *s=='\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - return -1; - } - if (sscanf(s,"%dx%d",&cols,&rows)!=2 || rows<1 || cols<1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); - free (s); - return -1; - } - - DROWS = rows; - DCOLS = cols; - - if ( ! vt100_mode) { - backbuffer_size = DROWS * DCOLS; - backbuffer = malloc(backbuffer_size); - if ( ! backbuffer ){ - return -1; - } - } - - /* real worker functions */ - if ( vt100_mode ) { - drv_generic_text_real_write = drv_SL_vt100_write; - } - else { - drv_generic_text_real_write = drv_SL_simple_write; - } - - drv_SL_clear(); /* clear */ - - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, NULL)) { - sleep (3); - drv_SL_clear(); + int rows = -1, cols = -1; + unsigned int flags = 0; + char *s; + char *model = 0; + + vt100_mode = 0; + model = cfg_get(section, "Model", "generic"); + if (model != NULL && *model != '\0') { + if (strcasecmp("vt100", model) == 0) + vt100_mode = 1; + } + + cfg_number(section, "BarCharValue", 0, 0, 255, &flags); + bar_char = flags; + cfg_number(section, "Options", 0, 0, 0xffff, &flags); + if (drv_generic_serial_open(section, Name, flags) < 0) + return -1; + + s = cfg_get(section, "Size", NULL); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + return -1; + } + if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } + + DROWS = rows; + DCOLS = cols; + + if (!vt100_mode) { + backbuffer_size = DROWS * DCOLS; + backbuffer = malloc(backbuffer_size); + if (!backbuffer) { + return -1; + } } - } - - return 0; + + /* real worker functions */ + if (vt100_mode) { + drv_generic_text_real_write = drv_SL_vt100_write; + } else { + drv_generic_text_real_write = drv_SL_simple_write; + } + + drv_SL_clear(); /* clear */ + + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, NULL)) { + sleep(3); + drv_SL_clear(); + } + } + + return 0; } @@ -272,78 +281,79 @@ static int drv_SL_start (const char *section, const int quiet) /* list models */ -int drv_SL_list (void) +int drv_SL_list(void) { - printf ("generic vt100"); - return 0; + printf("generic vt100"); + return 0; } /* initialize driver & display */ -int drv_SL_init (const char *section, const int quiet) +int drv_SL_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 0; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - - GOTO_COST = -1; /* number of bytes a goto command requires */ - - - - /* start display */ - if ((ret=drv_SL_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register plugins */ - /* none */ - - return 0; + WIDGET_CLASS wc; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 0; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + + GOTO_COST = -1; /* number of bytes a goto command requires */ + + + + /* start display */ + if ((ret = drv_SL_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register plugins */ + /* none */ + + return 0; } /* close driver & display */ -int drv_SL_quit (const int quiet) +int drv_SL_quit(const int quiet) { - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_SL_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - drv_generic_serial_close(); - - if ( backbuffer ) { - free(backbuffer) ; backbuffer=0; backbuffer_size=0; - } - return (0); + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_SL_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + drv_generic_serial_close(); + + if (backbuffer) { + free(backbuffer); + backbuffer = 0; + backbuffer_size = 0; + } + return (0); } DRIVER drv_SimpleLCD = { - name: Name, - list: drv_SL_list, - init: drv_SL_init, - quit: drv_SL_quit, + name:Name, + list:drv_SL_list, + init:drv_SL_init, + quit:drv_SL_quit, }; - diff --git a/drv_T6963.c b/drv_T6963.c index 6338a54..f156b90 100644 --- a/drv_T6963.c +++ b/drv_T6963.c @@ -1,4 +1,4 @@ -/* $Id: drv_T6963.c,v 1.15 2005/05/05 08:36:12 reinelt Exp $ +/* $Id: drv_T6963.c,v 1.16 2005/05/08 04:32:44 reinelt Exp $ * * new style driver for T6963-based displays * @@ -23,6 +23,9 @@ * * * $Log: drv_T6963.c,v $ + * Revision 1.16 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.15 2005/05/05 08:36:12 reinelt * changed SELECT to SLCTIN * @@ -122,17 +125,17 @@ #include <dmalloc.h> #endif -static char Name[]="T6963"; +static char Name[] = "T6963"; static int Model; typedef struct { - int type; - char *name; + int type; + char *name; } MODEL; static MODEL Models[] = { - { 0x01, "generic" }, - { 0xff, "Unknown" } + {0x01, "generic"}, + {0xff, "Unknown"} }; @@ -143,7 +146,7 @@ static unsigned char SIGNAL_WR; unsigned char *Buffer1, *Buffer2; -static int bug=0; +static int bug = 0; /****************************************/ @@ -151,394 +154,402 @@ static int bug=0; /****************************************/ /* perform normal status check */ -static void drv_T6_status1 (void) +static void drv_T6_status1(void) { - int n; - - /* turn off data line drivers */ - drv_generic_parport_direction (1); - - /* lower CE and RD */ - drv_generic_parport_control (SIGNAL_CE | SIGNAL_RD, 0); - - /* Access Time: 150 ns */ - ndelay(150); - - /* wait for STA0=1 and STA1=1 */ - n=0; - do { - rep_nop(); - if (++n>1000) { - debug("hang in status1"); - bug=1; - break; - } - } while ((drv_generic_parport_read() & 0x03) != 0x03); - - /* rise RD and CE */ - drv_generic_parport_control (SIGNAL_RD | SIGNAL_CE, SIGNAL_RD | SIGNAL_CE); - - /* Output Hold Time: 50 ns */ - ndelay(50); - - /* turn on data line drivers */ - drv_generic_parport_direction (0); + int n; + + /* turn off data line drivers */ + drv_generic_parport_direction(1); + + /* lower CE and RD */ + drv_generic_parport_control(SIGNAL_CE | SIGNAL_RD, 0); + + /* Access Time: 150 ns */ + ndelay(150); + + /* wait for STA0=1 and STA1=1 */ + n = 0; + do { + rep_nop(); + if (++n > 1000) { + debug("hang in status1"); + bug = 1; + break; + } + } while ((drv_generic_parport_read() & 0x03) != 0x03); + + /* rise RD and CE */ + drv_generic_parport_control(SIGNAL_RD | SIGNAL_CE, SIGNAL_RD | SIGNAL_CE); + + /* Output Hold Time: 50 ns */ + ndelay(50); + + /* turn on data line drivers */ + drv_generic_parport_direction(0); } /* perform status check in "auto mode" */ -static void drv_T6_status2 (void) +static void drv_T6_status2(void) { - int n; - - /* turn off data line drivers */ - drv_generic_parport_direction (1); - - /* lower RD and CE */ - drv_generic_parport_control (SIGNAL_RD | SIGNAL_CE, 0); - - /* Access Time: 150 ns */ - ndelay(150); - - /* wait for STA3=1 */ - n=0; - do { - rep_nop(); - if (++n>1000) { - debug("hang in status2"); - bug=1; - break; - } - } while ((drv_generic_parport_read() & 0x08) != 0x08); + int n; + + /* turn off data line drivers */ + drv_generic_parport_direction(1); + + /* lower RD and CE */ + drv_generic_parport_control(SIGNAL_RD | SIGNAL_CE, 0); + + /* Access Time: 150 ns */ + ndelay(150); - /* rise RD and CE */ - drv_generic_parport_control (SIGNAL_RD | SIGNAL_CE, SIGNAL_RD | SIGNAL_CE); + /* wait for STA3=1 */ + n = 0; + do { + rep_nop(); + if (++n > 1000) { + debug("hang in status2"); + bug = 1; + break; + } + } while ((drv_generic_parport_read() & 0x08) != 0x08); - /* Output Hold Time: 50 ns */ - ndelay(50); + /* rise RD and CE */ + drv_generic_parport_control(SIGNAL_RD | SIGNAL_CE, SIGNAL_RD | SIGNAL_CE); - /* turn on data line drivers */ - drv_generic_parport_direction (0); + /* Output Hold Time: 50 ns */ + ndelay(50); + + /* turn on data line drivers */ + drv_generic_parport_direction(0); } -static void drv_T6_write_cmd (const unsigned char cmd) +static void drv_T6_write_cmd(const unsigned char cmd) { - /* wait until the T6963 is idle */ - drv_T6_status1(); - - /* put data on DB1..DB8 */ - drv_generic_parport_data (cmd); - - /* lower WR and CE */ - drv_generic_parport_control (SIGNAL_WR | SIGNAL_CE, 0); - - /* Pulse width */ - ndelay(80); - - /* rise WR and CE */ - drv_generic_parport_control (SIGNAL_WR | SIGNAL_CE, SIGNAL_WR | SIGNAL_CE); - - /* Data Hold Time */ - ndelay(40); + /* wait until the T6963 is idle */ + drv_T6_status1(); + + /* put data on DB1..DB8 */ + drv_generic_parport_data(cmd); + + /* lower WR and CE */ + drv_generic_parport_control(SIGNAL_WR | SIGNAL_CE, 0); + + /* Pulse width */ + ndelay(80); + + /* rise WR and CE */ + drv_generic_parport_control(SIGNAL_WR | SIGNAL_CE, SIGNAL_WR | SIGNAL_CE); + + /* Data Hold Time */ + ndelay(40); } -static void drv_T6_write_data (const unsigned char data) +static void drv_T6_write_data(const unsigned char data) { - /* wait until the T6963 is idle */ - drv_T6_status1(); - - /* put data on DB1..DB8 */ - drv_generic_parport_data (data); - - /* lower C/D */ - drv_generic_parport_control (SIGNAL_CD, 0); - - /* C/D Setup Time */ - ndelay(20); - - /* lower WR and CE */ - drv_generic_parport_control (SIGNAL_WR | SIGNAL_CE, 0); - - /* Pulse Width */ - ndelay(80); - - /* rise WR and CE */ - drv_generic_parport_control (SIGNAL_WR | SIGNAL_CE, SIGNAL_WR | SIGNAL_CE); - - /* Data Hold Time */ - ndelay(40); - - /* rise CD */ - drv_generic_parport_control (SIGNAL_CD, SIGNAL_CD); + /* wait until the T6963 is idle */ + drv_T6_status1(); + + /* put data on DB1..DB8 */ + drv_generic_parport_data(data); + + /* lower C/D */ + drv_generic_parport_control(SIGNAL_CD, 0); + + /* C/D Setup Time */ + ndelay(20); + + /* lower WR and CE */ + drv_generic_parport_control(SIGNAL_WR | SIGNAL_CE, 0); + + /* Pulse Width */ + ndelay(80); + + /* rise WR and CE */ + drv_generic_parport_control(SIGNAL_WR | SIGNAL_CE, SIGNAL_WR | SIGNAL_CE); + + /* Data Hold Time */ + ndelay(40); + + /* rise CD */ + drv_generic_parport_control(SIGNAL_CD, SIGNAL_CD); } -static void drv_T6_write_auto (const unsigned char data) +static void drv_T6_write_auto(const unsigned char data) { - /* wait until the T6963 is idle */ - drv_T6_status2(); - - /* put data on DB1..DB8 */ - drv_generic_parport_data (data); - - /* lower C/D */ - drv_generic_parport_control (SIGNAL_CD, 0); - - /* C/D Setup Time */ - ndelay(20); - - /* lower WR and CE */ - drv_generic_parport_control (SIGNAL_WR | SIGNAL_CE, 0); - - /* Pulse Width */ - ndelay(80); - - /* rise WR and CE */ - drv_generic_parport_control (SIGNAL_WR | SIGNAL_CE, SIGNAL_WR | SIGNAL_CE); - - /* Data Hold Time */ - ndelay(40); - - /* rise CD */ - drv_generic_parport_control (SIGNAL_CD, SIGNAL_CD); + /* wait until the T6963 is idle */ + drv_T6_status2(); + + /* put data on DB1..DB8 */ + drv_generic_parport_data(data); + + /* lower C/D */ + drv_generic_parport_control(SIGNAL_CD, 0); + + /* C/D Setup Time */ + ndelay(20); + + /* lower WR and CE */ + drv_generic_parport_control(SIGNAL_WR | SIGNAL_CE, 0); + + /* Pulse Width */ + ndelay(80); + + /* rise WR and CE */ + drv_generic_parport_control(SIGNAL_WR | SIGNAL_CE, SIGNAL_WR | SIGNAL_CE); + + /* Data Hold Time */ + ndelay(40); + + /* rise CD */ + drv_generic_parport_control(SIGNAL_CD, SIGNAL_CD); } -#if 0 /* not used */ -static void drv_T6_send_byte (const unsigned char cmd, const unsigned char data) +#if 0 /* not used */ +static void drv_T6_send_byte(const unsigned char cmd, const unsigned char data) { - drv_T6_write_data(data); - drv_T6_write_cmd(cmd); + drv_T6_write_data(data); + drv_T6_write_cmd(cmd); } #endif -static void drv_T6_send_word (const unsigned char cmd, const unsigned short data) +static void drv_T6_send_word(const unsigned char cmd, const unsigned short data) { - drv_T6_write_data(data&0xff); - drv_T6_write_data(data>>8); - drv_T6_write_cmd(cmd); + drv_T6_write_data(data & 0xff); + drv_T6_write_data(data >> 8); + drv_T6_write_cmd(cmd); } static void drv_T6_clear(const unsigned short addr, const int len) { - int i; - - drv_T6_send_word (0x24, addr); /* Set Adress Pointer */ - drv_T6_write_cmd(0xb0); /* Set Data Auto Write */ - for (i=0; i<len; i++) { - drv_T6_write_auto(0); - if (bug) { - bug=0; - debug("bug occured at byte %d of %d", i, len); + int i; + + drv_T6_send_word(0x24, addr); /* Set Adress Pointer */ + drv_T6_write_cmd(0xb0); /* Set Data Auto Write */ + for (i = 0; i < len; i++) { + drv_T6_write_auto(0); + if (bug) { + bug = 0; + debug("bug occured at byte %d of %d", i, len); + } } - } - drv_T6_status2(); - drv_T6_write_cmd(0xb2); /* Auto Reset */ + drv_T6_status2(); + drv_T6_write_cmd(0xb2); /* Auto Reset */ } static void drv_T6_copy(const unsigned short addr, const unsigned char *data, const int len) { - int i; - - drv_T6_send_word (0x24, 0x0200+addr); /* Set Adress Pointer */ - drv_T6_write_cmd(0xb0); /* Set Data Auto Write */ - for (i=0; i<len; i++) { - drv_T6_write_auto(*(data++)); - if (bug) { - bug=0; - debug("bug occured at byte %d of %d, addr=%d", i, len, addr); + int i; + + drv_T6_send_word(0x24, 0x0200 + addr); /* Set Adress Pointer */ + drv_T6_write_cmd(0xb0); /* Set Data Auto Write */ + for (i = 0; i < len; i++) { + drv_T6_write_auto(*(data++)); + if (bug) { + bug = 0; + debug("bug occured at byte %d of %d, addr=%d", i, len, addr); + } } - } - drv_T6_status2(); - drv_T6_write_cmd(0xb2); /* Auto Reset */ + drv_T6_status2(); + drv_T6_write_cmd(0xb2); /* Auto Reset */ } static void drv_T6_blit(const int row, const int col, const int height, const int width) { - int i, j, e, m; - int r, c; - - for (r = row; r < row + height; r++) { - for (c = col; c < col + width; c++) { - unsigned char mask = 1 << (XRES - 1 - c % XRES); - if (drv_generic_graphic_FB[r * LCOLS + c]) { - /* set bit */ - Buffer1[(r * DCOLS + c) / XRES] |= mask; - } else { - /* clear bit */ - Buffer1[(r * DCOLS + c) / XRES] &= ~mask; - } + int i, j, e, m; + int r, c; + + for (r = row; r < row + height; r++) { + for (c = col; c < col + width; c++) { + unsigned char mask = 1 << (XRES - 1 - c % XRES); + if (drv_generic_graphic_FB[r * LCOLS + c]) { + /* set bit */ + Buffer1[(r * DCOLS + c) / XRES] |= mask; + } else { + /* clear bit */ + Buffer1[(r * DCOLS + c) / XRES] &= ~mask; + } + } } - } - - /* upper half */ - /* max address */ - if (row + height - 1 < 64) { - m = ((row + height - 1) * DCOLS + col + width) / XRES; - } else { - m = ( 64 * DCOLS + col + width) / XRES; - } - - for (i = (row * DCOLS + col) / XRES; i <= m; i++) { - if (Buffer1[i] == Buffer2[i]) continue; - for (j = i, e = 0; i <= m; i++) { - if (Buffer1[i] == Buffer2[i]) { - if (++e > 4) break; - } else { - e = 0; - } + /* upper half */ + + /* max address */ + if (row + height - 1 < 64) { + m = ((row + height - 1) * DCOLS + col + width) / XRES; + } else { + m = (64 * DCOLS + col + width) / XRES; } - memcpy (Buffer2 + j, Buffer1 + j, i - j - e + 1); - drv_T6_copy (j, Buffer1 + j, i - j - e + 1); - } - - /* lower half */ - - /* max address */ - m = ((row + height - 1) * DCOLS + col + width) / XRES; - - for (i = (64 * DCOLS + col) / XRES; i <= m; i++) { - if (Buffer1[i] == Buffer2[i]) continue; - for (j = i, e = 0; i <= m; i++) { - if (Buffer1[i] == Buffer2[i]) { - if (++e > 4) break; - } else { - e = 0; - } + + for (i = (row * DCOLS + col) / XRES; i <= m; i++) { + if (Buffer1[i] == Buffer2[i]) + continue; + for (j = i, e = 0; i <= m; i++) { + if (Buffer1[i] == Buffer2[i]) { + if (++e > 4) + break; + } else { + e = 0; + } + } + memcpy(Buffer2 + j, Buffer1 + j, i - j - e + 1); + drv_T6_copy(j, Buffer1 + j, i - j - e + 1); + } + + /* lower half */ + + /* max address */ + m = ((row + height - 1) * DCOLS + col + width) / XRES; + + for (i = (64 * DCOLS + col) / XRES; i <= m; i++) { + if (Buffer1[i] == Buffer2[i]) + continue; + for (j = i, e = 0; i <= m; i++) { + if (Buffer1[i] == Buffer2[i]) { + if (++e > 4) + break; + } else { + e = 0; + } + } + memcpy(Buffer2 + j, Buffer1 + j, i - j - e + 1); + drv_T6_copy(j, Buffer1 + j, i - j - e + 1); } - memcpy (Buffer2 + j, Buffer1 + j, i - j - e + 1); - drv_T6_copy (j, Buffer1 + j, i - j - e + 1); - } } -static int drv_T6_start (const char *section) +static int drv_T6_start(const char *section) { - char *model, *s; - int rows, TROWS, TCOLS; - - model=cfg_get(section, "Model", "generic"); - if (model!=NULL && *model!='\0') { - int i; - for (i=0; Models[i].type!=0xff; i++) { - if (strcasecmp(Models[i].name, model)==0) break; + char *model, *s; + int rows, TROWS, TCOLS; + + model = cfg_get(section, "Model", "generic"); + if (model != NULL && *model != '\0') { + int i; + for (i = 0; Models[i].type != 0xff; i++) { + if (strcasecmp(Models[i].name, model) == 0) + break; + } + if (Models[i].type == 0xff) { + error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); + return -1; + } + Model = i; + info("%s: using model '%s'", Name, Models[Model].name); + } else { + error("%s: empty '%s.Model' entry from %s", Name, section, cfg_source()); + return -1; + } + + /* read display size from config */ + s = cfg_get(section, "Size", NULL); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + return -1; } - if (Models[i].type==0xff) { - error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source()); - return -1; + + DROWS = -1; + DCOLS = -1; + if (sscanf(s, "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) { + error("%s: bad Size '%s' from %s", Name, s, cfg_source()); + return -1; + } + + s = cfg_get(section, "Font", "6x8"); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Font' entry from %s", Name, section, cfg_source()); + return -1; + } + + XRES = -1; + YRES = -1; + if (sscanf(s, "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) { + error("%s: bad Font '%s' from %s", Name, s, cfg_source()); + return -1; + } + + /* Fixme: provider other fonts someday... */ + if (XRES != 6 && YRES != 8) { + error("%s: bad Font '%s' from %s (only 6x8 at the moment)", Name, s, cfg_source()); + return -1; + } + + TROWS = DROWS / YRES; /* text rows */ + TCOLS = DCOLS / XRES; /* text cols */ + + Buffer1 = malloc(TCOLS * DROWS); + if (Buffer1 == NULL) { + error("%s: framebuffer #1 could not be allocated: malloc() failed", Name); + return -1; + } + + debug("malloc buffer 2 (%d*%d)=%d", TCOLS, DROWS, TCOLS * DROWS); + Buffer2 = malloc(TCOLS * DROWS); + if (Buffer2 == NULL) { + error("%s: framebuffer #2 could not be allocated: malloc() failed", Name); + return -1; + } + + memset(Buffer1, 0, TCOLS * DROWS * sizeof(*Buffer1)); + memset(Buffer2, 0, TCOLS * DROWS * sizeof(*Buffer2)); + + if (drv_generic_parport_open(section, Name) != 0) { + error("%s: could not initialize parallel port!", Name); + return -1; + } + + if ((SIGNAL_CE = drv_generic_parport_wire_ctrl("CE", "STROBE")) == 0xff) + return -1; + if ((SIGNAL_CD = drv_generic_parport_wire_ctrl("CD", "SLCTIN")) == 0xff) + return -1; + if ((SIGNAL_RD = drv_generic_parport_wire_ctrl("RD", "AUTOFD")) == 0xff) + return -1; + if ((SIGNAL_WR = drv_generic_parport_wire_ctrl("WR", "INIT")) == 0xff) + return -1; + + /* rise CE, CD, RD and WR */ + drv_generic_parport_control(SIGNAL_CE | SIGNAL_CD | SIGNAL_RD | SIGNAL_WR, SIGNAL_CE | SIGNAL_CD | SIGNAL_RD | SIGNAL_WR); + /* set direction: write */ + drv_generic_parport_direction(0); + + + /* initialize display */ + + drv_T6_send_word(0x40, 0x0000); /* Set Text Home Address */ + drv_T6_send_word(0x41, TCOLS); /* Set Text Area */ + + drv_T6_send_word(0x42, 0x0200); /* Set Graphic Home Address */ + drv_T6_send_word(0x43, TCOLS); /* Set Graphic Area */ + + drv_T6_write_cmd(0x80); /* Mode Set: OR mode, Internal CG RAM mode */ + drv_T6_send_word(0x22, 0x0002); /* Set Offset Register */ + drv_T6_write_cmd(0x98); /* Set Display Mode: Curser off, Text off, Graphics on */ + drv_T6_write_cmd(0xa0); /* Set Cursor Pattern: 1 line cursor */ + drv_T6_send_word(0x21, 0x0000); /* Set Cursor Pointer to (0,0) */ + + + /* clear display */ + + /* upper half */ + rows = TROWS > 8 ? 8 : TROWS; + drv_T6_clear(0x0000, TCOLS * rows); /* clear text area */ + drv_T6_clear(0x0200, TCOLS * rows * 8); /* clear graphic area */ + + /* lower half */ + if (TROWS > 8) { + rows = TROWS - 8; + drv_T6_clear(0x8000, TCOLS * rows); /* clear text area #2 */ + drv_T6_clear(0x8200, TCOLS * rows * 8); /* clear graphic area #2 */ } - Model=i; - info ("%s: using model '%s'", Name, Models[Model].name); - } else { - error ("%s: empty '%s.Model' entry from %s", Name, section, cfg_source()); - return -1; - } - - /* read display size from config */ - s=cfg_get(section, "Size", NULL); - if (s==NULL || *s=='\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - return -1; - } - - DROWS = -1; - DCOLS = -1; - if (sscanf(s, "%dx%d", &DCOLS, &DROWS)!=2 || DCOLS<1 || DROWS<1) { - error ("%s: bad Size '%s' from %s", Name, s, cfg_source()); - return -1; - } - - s=cfg_get(section, "Font", "6x8"); - if (s==NULL || *s=='\0') { - error ("%s: no '%s.Font' entry from %s", Name, section, cfg_source()); - return -1; - } - - XRES = -1; - YRES = -1; - if (sscanf(s, "%dx%d", &XRES, &YRES)!=2 || XRES<1 || YRES<1) { - error ("%s: bad Font '%s' from %s", Name, s, cfg_source()); - return -1; - } - - /* Fixme: provider other fonts someday... */ - if (XRES!=6 && YRES!=8) { - error ("%s: bad Font '%s' from %s (only 6x8 at the moment)", Name, s, cfg_source()); - return -1; - } - - TROWS=DROWS/YRES; /* text rows */ - TCOLS=DCOLS/XRES; /* text cols */ - - Buffer1=malloc(TCOLS*DROWS); - if (Buffer1==NULL) { - error ("%s: framebuffer #1 could not be allocated: malloc() failed", Name); - return -1; - } - - debug ("malloc buffer 2 (%d*%d)=%d", TCOLS, DROWS, TCOLS*DROWS); - Buffer2=malloc(TCOLS*DROWS); - if (Buffer2==NULL) { - error ("%s: framebuffer #2 could not be allocated: malloc() failed", Name); - return -1; - } - - memset(Buffer1,0,TCOLS*DROWS*sizeof(*Buffer1)); - memset(Buffer2,0,TCOLS*DROWS*sizeof(*Buffer2)); - - if (drv_generic_parport_open(section, Name) != 0) { - error ("%s: could not initialize parallel port!", Name); - return -1; - } - - if ((SIGNAL_CE=drv_generic_parport_wire_ctrl ("CE", "STROBE"))==0xff) return -1; - if ((SIGNAL_CD=drv_generic_parport_wire_ctrl ("CD", "SLCTIN"))==0xff) return -1; - if ((SIGNAL_RD=drv_generic_parport_wire_ctrl ("RD", "AUTOFD"))==0xff) return -1; - if ((SIGNAL_WR=drv_generic_parport_wire_ctrl ("WR", "INIT") )==0xff) return -1; - - /* rise CE, CD, RD and WR */ - drv_generic_parport_control (SIGNAL_CE | SIGNAL_CD | SIGNAL_RD | SIGNAL_WR, - SIGNAL_CE | SIGNAL_CD | SIGNAL_RD | SIGNAL_WR); - /* set direction: write */ - drv_generic_parport_direction (0); - - - /* initialize display */ - - drv_T6_send_word (0x40, 0x0000); /* Set Text Home Address */ - drv_T6_send_word (0x41, TCOLS); /* Set Text Area */ - - drv_T6_send_word (0x42, 0x0200); /* Set Graphic Home Address */ - drv_T6_send_word (0x43, TCOLS); /* Set Graphic Area */ - - drv_T6_write_cmd (0x80); /* Mode Set: OR mode, Internal CG RAM mode */ - drv_T6_send_word (0x22, 0x0002); /* Set Offset Register */ - drv_T6_write_cmd (0x98); /* Set Display Mode: Curser off, Text off, Graphics on */ - drv_T6_write_cmd (0xa0); /* Set Cursor Pattern: 1 line cursor */ - drv_T6_send_word (0x21, 0x0000); /* Set Cursor Pointer to (0,0) */ - - - /* clear display */ - - /* upper half */ - rows=TROWS>8?8:TROWS; - drv_T6_clear(0x0000, TCOLS*rows); /* clear text area */ - drv_T6_clear(0x0200, TCOLS*rows*8); /* clear graphic area */ - - /* lower half */ - if (TROWS>8) { - rows=TROWS-8; - drv_T6_clear(0x8000, TCOLS*rows); /* clear text area #2 */ - drv_T6_clear(0x8200, TCOLS*rows*8); /* clear graphic area #2 */ - } - - return 0; + + return 0; } @@ -565,99 +576,98 @@ static int drv_T6_start (const char *section) /* list models */ -int drv_T6_list (void) +int drv_T6_list(void) { - int i; - - for (i=0; Models[i].type!=0xff; i++) { - printf ("%s ", Models[i].name); - } - return 0; + int i; + + for (i = 0; Models[i].type != 0xff; i++) { + printf("%s ", Models[i].name); + } + return 0; } /* initialize driver & display */ -int drv_T6_init (const char *section, const int quiet) +int drv_T6_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* real worker functions */ - drv_generic_graphic_real_blit = drv_T6_blit; - - /* start display */ - if ((ret=drv_T6_start (section))!=0) - return ret; - - /* initialize generic graphic driver */ - if ((ret=drv_generic_graphic_init(section, Name))!=0) - return ret; - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_graphic_greet (buffer, NULL)) { - sleep (3); - drv_generic_graphic_clear(); + WIDGET_CLASS wc; + int ret; + + /* real worker functions */ + drv_generic_graphic_real_blit = drv_T6_blit; + + /* start display */ + if ((ret = drv_T6_start(section)) != 0) + return ret; + + /* initialize generic graphic driver */ + if ((ret = drv_generic_graphic_init(section, Name)) != 0) + return ret; + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_graphic_greet(buffer, NULL)) { + sleep(3); + drv_generic_graphic_clear(); + } } - } - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_graphic_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_graphic_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_graphic_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none at the moment... */ - - - return 0; + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_graphic_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_graphic_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_graphic_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none at the moment... */ + + + return 0; } /* close driver & display */ -int drv_T6_quit (const int quiet) { - - info("%s: shutting down.", Name); - - drv_generic_graphic_clear(); - - if (!quiet) { - drv_generic_graphic_greet ("goodbye!", NULL); - } - - drv_generic_graphic_quit(); - drv_generic_parport_close(); - - if (Buffer1) { - free (Buffer1); - Buffer1=NULL; - } - - if (Buffer2) { - free (Buffer2); - Buffer2=NULL; - } - - return (0); +int drv_T6_quit(const int quiet) +{ + + info("%s: shutting down.", Name); + + drv_generic_graphic_clear(); + + if (!quiet) { + drv_generic_graphic_greet("goodbye!", NULL); + } + + drv_generic_graphic_quit(); + drv_generic_parport_close(); + + if (Buffer1) { + free(Buffer1); + Buffer1 = NULL; + } + + if (Buffer2) { + free(Buffer2); + Buffer2 = NULL; + } + + return (0); } DRIVER drv_T6963 = { - name: Name, - list: drv_T6_list, - init: drv_T6_init, - quit: drv_T6_quit, + name:Name, + list:drv_T6_list, + init:drv_T6_init, + quit:drv_T6_quit, }; - - diff --git a/drv_Trefon.c b/drv_Trefon.c index a7fac91..bdf4670 100644 --- a/drv_Trefon.c +++ b/drv_Trefon.c @@ -1,4 +1,4 @@ -/* $Id: drv_Trefon.c,v 1.2 2005/04/24 05:27:09 reinelt Exp $ +/* $Id: drv_Trefon.c,v 1.3 2005/05/08 04:32:44 reinelt Exp $ * * driver for TREFON USB LCD displays * @@ -23,6 +23,9 @@ * * * $Log: drv_Trefon.c,v $ + * Revision 1.3 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.2 2005/04/24 05:27:09 reinelt * Trefon Backlight added * @@ -74,7 +77,7 @@ #define PKT_CTRL 0x06 #define PKT_END 0xff -static char Name[]="TREFON"; +static char Name[] = "TREFON"; static usb_dev_handle *lcd; static int interface; @@ -87,189 +90,190 @@ extern int got_signal; /*** hardware dependant functions ***/ /****************************************/ -static int drv_TF_open (void) +static int drv_TF_open(void) { - struct usb_bus *busses, *bus; - struct usb_device *dev; - - lcd = NULL; - - info ("%s: scanning USB for TREFON LCD...", Name); - - usb_debug = 0; - - usb_init(); - usb_find_busses(); - usb_find_devices(); - busses = usb_get_busses(); - - for (bus = busses; bus; bus = bus->next) { - for (dev = bus->devices; dev; dev = dev->next) { - if ((dev->descriptor.idVendor == LCD_USB_VENDOR) && - (dev->descriptor.idProduct == LCD_USB_DEVICE)) { - info ("%s: found TREFON USB LCD on bus %s device %s", Name, bus->dirname, dev->filename); - lcd = usb_open(dev); - if (usb_set_configuration(lcd, 1) < 0) { - error ("%s: usb_set_configuration() failed!", Name); - return -1; - } - interface = 0; - if (usb_claim_interface(lcd, interface) < 0) { - error ("%s: usb_claim_interface() failed!", Name); - return -1; + struct usb_bus *busses, *bus; + struct usb_device *dev; + + lcd = NULL; + + info("%s: scanning USB for TREFON LCD...", Name); + + usb_debug = 0; + + usb_init(); + usb_find_busses(); + usb_find_devices(); + busses = usb_get_busses(); + + for (bus = busses; bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + if ((dev->descriptor.idVendor == LCD_USB_VENDOR) && (dev->descriptor.idProduct == LCD_USB_DEVICE)) { + info("%s: found TREFON USB LCD on bus %s device %s", Name, bus->dirname, dev->filename); + lcd = usb_open(dev); + if (usb_set_configuration(lcd, 1) < 0) { + error("%s: usb_set_configuration() failed!", Name); + return -1; + } + interface = 0; + if (usb_claim_interface(lcd, interface) < 0) { + error("%s: usb_claim_interface() failed!", Name); + return -1; + } + return 0; + } } - return 0; - } } - } - return -1; + return -1; } -static int drv_TF_close (void) +static int drv_TF_close(void) { - usb_release_interface(lcd, interface); - usb_close(lcd); + usb_release_interface(lcd, interface); + usb_close(lcd); - return 0; + return 0; } -static void drv_TF_send (char *data, int size) +static void drv_TF_send(char *data, int size) { - char buffer[64]; + char buffer[64]; - /* the controller always wants a 64-byte packet */ - memset (buffer, 0, 64); - memcpy (buffer, data, size); + /* the controller always wants a 64-byte packet */ + memset(buffer, 0, 64); + memcpy(buffer, data, size); - // Endpoint hardcoded to 2 - usb_bulk_write(lcd, 2, buffer, 64, 2000); + // Endpoint hardcoded to 2 + usb_bulk_write(lcd, 2, buffer, 64, 2000); } -static void drv_TF_command (const unsigned char cmd) +static void drv_TF_command(const unsigned char cmd) { - char buffer[4] = { PKT_START, PKT_CTRL, 0, PKT_END }; - buffer[2] = cmd; - drv_TF_send(buffer, 4); + char buffer[4] = { PKT_START, PKT_CTRL, 0, PKT_END }; + buffer[2] = cmd; + drv_TF_send(buffer, 4); } -static void drv_TF_clear (void) +static void drv_TF_clear(void) { - drv_TF_command (0x01); + drv_TF_command(0x01); } -static void drv_TF_write (const int row, const int col, const char *data, const int len) +static void drv_TF_write(const int row, const int col, const char *data, const int len) { - char buffer[64]; - char *p; - int pos; - - /* 16x4 Displays use a slightly different layout */ - if (DCOLS==16 && DROWS==4) { - pos = (row%2)*64+(row/2)*16+col; - } else { - pos = (row%2)*64+(row/2)*20+col; - } - - /* I'd like to combine the GOTO and the data into one packet, - * unfortunately the Trefon doesn't like it :-( - */ - - drv_TF_command (0x80|pos); - - p = buffer; - *p++ = PKT_START; - *p++ = PKT_DATA; - *p++ = (char) len; - for (pos = 0; pos < len; pos++) { - *p++ = *data++; - } - *p++ = PKT_END; - - drv_TF_send(buffer, len+3); + char buffer[64]; + char *p; + int pos; + + /* 16x4 Displays use a slightly different layout */ + if (DCOLS == 16 && DROWS == 4) { + pos = (row % 2) * 64 + (row / 2) * 16 + col; + } else { + pos = (row % 2) * 64 + (row / 2) * 20 + col; + } + + /* I'd like to combine the GOTO and the data into one packet, + * unfortunately the Trefon doesn't like it :-( + */ + + drv_TF_command(0x80 | pos); + + p = buffer; + *p++ = PKT_START; + *p++ = PKT_DATA; + *p++ = (char) len; + for (pos = 0; pos < len; pos++) { + *p++ = *data++; + } + *p++ = PKT_END; + + drv_TF_send(buffer, len + 3); } -static void drv_TF_defchar (const int ascii, const unsigned char *matrix) +static void drv_TF_defchar(const int ascii, const unsigned char *matrix) { - char buffer[14] = "\002\006x\002x01234567\377"; - char *p; - int i; - - p = buffer; - *p++ = PKT_START; - *p++ = PKT_CTRL; - *p++ = 0x40|8*ascii; - *p++ = PKT_DATA; - *p++ = 8; /* data length */ - for (i = 0; i < 8; i++) { - *p++ = *matrix++ & 0x1f; - } - *p++ = PKT_END; - - drv_TF_send(buffer, 14); + char buffer[14] = "\002\006x\002x01234567\377"; + char *p; + int i; + + p = buffer; + *p++ = PKT_START; + *p++ = PKT_CTRL; + *p++ = 0x40 | 8 * ascii; + *p++ = PKT_DATA; + *p++ = 8; /* data length */ + for (i = 0; i < 8; i++) { + *p++ = *matrix++ & 0x1f; + } + *p++ = PKT_END; + + drv_TF_send(buffer, 14); } -static int drv_TF_backlight (int backlight) +static int drv_TF_backlight(int backlight) { - char buffer[4] = { PKT_START, PKT_BACKLIGHT, 0, PKT_END }; + char buffer[4] = { PKT_START, PKT_BACKLIGHT, 0, PKT_END }; - if (backlight < 0) backlight = 0; - if (backlight > 1) backlight = 1; + if (backlight < 0) + backlight = 0; + if (backlight > 1) + backlight = 1; - buffer[2] = backlight; - drv_TF_send(buffer, 4); + buffer[2] = backlight; + drv_TF_send(buffer, 4); - return backlight; + return backlight; } - -static int drv_TF_start (const char *section, const int quiet) + +static int drv_TF_start(const char *section, const int quiet) { - int backlight; - int rows=-1, cols=-1; - char *s; + int backlight; + int rows = -1, cols = -1; + char *s; + + s = cfg_get(section, "Size", NULL); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + return -1; + } + if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } - s=cfg_get(section, "Size", NULL); - if (s==NULL || *s=='\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - return -1; - } - if (sscanf(s,"%dx%d",&cols,&rows)!=2 || rows<1 || cols<1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); - free (s); - return -1; - } - - DROWS = rows; - DCOLS = cols; - - if (drv_TF_open() < 0) { - error ("%s: could not find a TREFON USB LCD", Name); - return -1; - } - - if (cfg_number(section, "Backlight", 1, 0, 1, &backlight) > 0) { - drv_TF_backlight (backlight); - } - - drv_TF_clear(); /* clear display */ - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, "www.trefon.de")) { - sleep (3); - drv_TF_clear(); + DROWS = rows; + DCOLS = cols; + + if (drv_TF_open() < 0) { + error("%s: could not find a TREFON USB LCD", Name); + return -1; + } + + if (cfg_number(section, "Backlight", 1, 0, 1, &backlight) > 0) { + drv_TF_backlight(backlight); + } + + drv_TF_clear(); /* clear display */ + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, "www.trefon.de")) { + sleep(3); + drv_TF_clear(); + } } - } - - return 0; + + return 0; } @@ -277,12 +281,12 @@ static int drv_TF_start (const char *section, const int quiet) /*** plugins ***/ /****************************************/ -static void plugin_backlight (RESULT *result, RESULT *arg1) +static void plugin_backlight(RESULT * result, RESULT * arg1) { - double backlight; - - backlight = drv_TF_backlight(R2N(arg1)); - SetResult(&result, R_NUMBER, &backlight); + double backlight; + + backlight = drv_TF_backlight(R2N(arg1)); + SetResult(&result, R_NUMBER, &backlight); } @@ -302,106 +306,105 @@ static void plugin_backlight (RESULT *result, RESULT *arg1) /* list models */ -int drv_TF_list (void) +int drv_TF_list(void) { - printf ("generic"); - return 0; + printf("generic"); + return 0; } /* initialize driver & display */ -int drv_TF_init (const char *section, const int quiet) +int drv_TF_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int asc255bug; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 1; /* ASCII of first user-defineable char */ - GOTO_COST = 64; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_TF_write; - drv_generic_text_real_defchar = drv_TF_defchar; - - - /* start display */ - if ((ret=drv_TF_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - /* most displays have a full block on ascii 255, but some have kind of */ - /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ - /* char will not be used, but rendered by the bar driver */ - cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - if (!asc255bug) - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - AddFunction ("LCD::backlight", 1, plugin_backlight); - - return 0; + WIDGET_CLASS wc; + int asc255bug; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 1; /* ASCII of first user-defineable char */ + GOTO_COST = 64; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_TF_write; + drv_generic_text_real_defchar = drv_TF_defchar; + + + /* start display */ + if ((ret = drv_TF_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + /* most displays have a full block on ascii 255, but some have kind of */ + /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ + /* char will not be used, but rendered by the bar driver */ + cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + if (!asc255bug) + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + AddFunction("LCD::backlight", 1, plugin_backlight); + + return 0; } /* close driver & display */ -int drv_TF_quit (const int quiet) +int drv_TF_quit(const int quiet) { - info("%s: shutting down.", Name); - - drv_generic_text_quit(); - - /* clear display */ - drv_TF_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - debug ("closing USB connection"); - drv_TF_close(); - - return (0); + info("%s: shutting down.", Name); + + drv_generic_text_quit(); + + /* clear display */ + drv_TF_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + debug("closing USB connection"); + drv_TF_close(); + + return (0); } DRIVER drv_Trefon = { - name: Name, - list: drv_TF_list, - init: drv_TF_init, - quit: drv_TF_quit, + name:Name, + list:drv_TF_list, + init:drv_TF_init, + quit:drv_TF_quit, }; - diff --git a/drv_USBLCD.c b/drv_USBLCD.c index aa5b927..dd179c3 100644 --- a/drv_USBLCD.c +++ b/drv_USBLCD.c @@ -1,4 +1,4 @@ -/* $Id: drv_USBLCD.c,v 1.22 2005/04/24 04:33:46 reinelt Exp $ +/* $Id: drv_USBLCD.c,v 1.23 2005/05/08 04:32:44 reinelt Exp $ * * new style driver for USBLCD displays * @@ -26,6 +26,9 @@ * * * $Log: drv_USBLCD.c,v $ + * Revision 1.23 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.22 2005/04/24 04:33:46 reinelt * driver for TREFON USB LCD's added * @@ -194,270 +197,270 @@ extern int got_signal; #ifdef HAVE_USB_H -static int drv_UL_open (void) +static int drv_UL_open(void) { - struct usb_bus *busses, *bus; - struct usb_device *dev; - - lcd = NULL; - - info ("%s: scanning for USBLCD...", Name); - - usb_debug = 0; - - usb_init(); - usb_find_busses(); - usb_find_devices(); - busses = usb_get_busses(); - - for (bus = busses; bus; bus = bus->next) { - for (dev = bus->devices; dev; dev = dev->next) { - if (((dev->descriptor.idVendor == USBLCD_VENDOR) || - (dev->descriptor.idVendor == USBLCD_VENDOR2)) && - (dev->descriptor.idProduct == USBLCD_DEVICE)) { - - unsigned int v = dev->descriptor.bcdDevice; - - info ("%s: found USBLCD V%1d%1d.%1d%1d on bus %s device %s", Name, - (v & 0xF000)>>12,(v & 0xF00)>>8, (v & 0xF0)>>4, (v & 0xF), - bus->dirname, dev->filename); - - interface = 0; - lcd = usb_open(dev); - if (usb_claim_interface(lcd, interface) < 0) { - error ("%s: usb_claim_interface() failed!", Name); - error ("%s: maybe you have the usblcd module loaded?", Name); - return -1; + struct usb_bus *busses, *bus; + struct usb_device *dev; + + lcd = NULL; + + info("%s: scanning for USBLCD...", Name); + + usb_debug = 0; + + usb_init(); + usb_find_busses(); + usb_find_devices(); + busses = usb_get_busses(); + + for (bus = busses; bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + if (((dev->descriptor.idVendor == USBLCD_VENDOR) || + (dev->descriptor.idVendor == USBLCD_VENDOR2)) && (dev->descriptor.idProduct == USBLCD_DEVICE)) { + + unsigned int v = dev->descriptor.bcdDevice; + + info("%s: found USBLCD V%1d%1d.%1d%1d on bus %s device %s", Name, + (v & 0xF000) >> 12, (v & 0xF00) >> 8, (v & 0xF0) >> 4, (v & 0xF), bus->dirname, dev->filename); + + interface = 0; + lcd = usb_open(dev); + if (usb_claim_interface(lcd, interface) < 0) { + error("%s: usb_claim_interface() failed!", Name); + error("%s: maybe you have the usblcd module loaded?", Name); + return -1; + } + return 0; + } } - return 0; - } } - } - error ("%s: could not find a USBLCD", Name); - return -1; + error("%s: could not find a USBLCD", Name); + return -1; } -static int drv_UL_close (void) +static int drv_UL_close(void) { - usb_release_interface(lcd, interface); - usb_close(lcd); + usb_release_interface(lcd, interface); + usb_close(lcd); - return 0; + return 0; } #endif -static void drv_UL_send (void) +static void drv_UL_send(void) { #if 0 - struct timeval now, end; - gettimeofday (&now, NULL); + struct timeval now, end; + gettimeofday(&now, NULL); #endif - - if (use_libusb) { + + if (use_libusb) { #ifdef HAVE_USB_H - // Fixme: Endpoint hardcoded to 1 ??? - usb_bulk_write(lcd, 1, Buffer, BufPtr-Buffer, 1000); + // Fixme: Endpoint hardcoded to 1 ??? + usb_bulk_write(lcd, 1, Buffer, BufPtr - Buffer, 1000); #endif - } else { - write(usblcd_file,Buffer,BufPtr-Buffer); - } - - + } else { + write(usblcd_file, Buffer, BufPtr - Buffer); + } + + #if 0 - gettimeofday (&end, NULL); - debug ("send %d bytes in %d usec (%d usec/byte)", BufPtr-Buffer, - (1000000*(end.tv_sec-now.tv_sec)+end.tv_usec-now.tv_usec), - (1000000*(end.tv_sec-now.tv_sec)+end.tv_usec-now.tv_usec)/(BufPtr-Buffer)); + gettimeofday(&end, NULL); + debug("send %d bytes in %d usec (%d usec/byte)", BufPtr - Buffer, + (1000000 * (end.tv_sec - now.tv_sec) + end.tv_usec - now.tv_usec), + (1000000 * (end.tv_sec - now.tv_sec) + end.tv_usec - now.tv_usec) / (BufPtr - Buffer)); #endif - - BufPtr = Buffer; + + BufPtr = Buffer; } -static void drv_UL_command (const unsigned char cmd) +static void drv_UL_command(const unsigned char cmd) { - *BufPtr++='\0'; - *BufPtr++=cmd; + *BufPtr++ = '\0'; + *BufPtr++ = cmd; } -static void drv_UL_clear (void) +static void drv_UL_clear(void) { - drv_UL_command (0x01); /* clear display */ - drv_UL_command (0x03); /* return home */ - drv_UL_send(); /* flush buffer */ + drv_UL_command(0x01); /* clear display */ + drv_UL_command(0x03); /* return home */ + drv_UL_send(); /* flush buffer */ } -static void drv_UL_write (const int row, const int col, const char *data, int len) +static void drv_UL_write(const int row, const int col, const char *data, int len) { - int pos; - - /* 16x4 Displays use a slightly different layout */ - if (DCOLS==16 && DROWS==4) { - pos = (row%2)*64+(row/2)*16+col; - } else { - pos = (row%2)*64+(row/2)*20+col; - } - - drv_UL_command (0x80|pos); - - while (len--) { - if(*data == 0) *BufPtr++ = 0; - *BufPtr++ = *data++; - } - - drv_UL_send(); + int pos; + + /* 16x4 Displays use a slightly different layout */ + if (DCOLS == 16 && DROWS == 4) { + pos = (row % 2) * 64 + (row / 2) * 16 + col; + } else { + pos = (row % 2) * 64 + (row / 2) * 20 + col; + } + + drv_UL_command(0x80 | pos); + + while (len--) { + if (*data == 0) + *BufPtr++ = 0; + *BufPtr++ = *data++; + } + + drv_UL_send(); } -static void drv_UL_defchar (const int ascii, const unsigned char *matrix) +static void drv_UL_defchar(const int ascii, const unsigned char *matrix) { - int i; - - drv_UL_command (0x40|8*ascii); + int i; + + drv_UL_command(0x40 | 8 * ascii); - for (i = 0; i < 8; i++) { - if ((*matrix & 0x1f) == 0) *BufPtr++ = 0; - *BufPtr++ = *matrix++ & 0x1f; - } + for (i = 0; i < 8; i++) { + if ((*matrix & 0x1f) == 0) + *BufPtr++ = 0; + *BufPtr++ = *matrix++ & 0x1f; + } - drv_UL_send(); + drv_UL_send(); } -static int drv_UL_start (const char *section, const int quiet) +static int drv_UL_start(const char *section, const int quiet) { - int rows = -1, cols = -1; - int major, minor; - char *port, *s; - char buf[128]; - - if (Port) { - free(Port); - Port = NULL; - } - - if ((port = cfg_get(section, "Port", NULL)) == NULL || *port == '\0') { - error ("%s: no '%s.Port' entry from %s", Name, section, cfg_source()); - return -1; - } + int rows = -1, cols = -1; + int major, minor; + char *port, *s; + char buf[128]; + + if (Port) { + free(Port); + Port = NULL; + } + + if ((port = cfg_get(section, "Port", NULL)) == NULL || *port == '\0') { + error("%s: no '%s.Port' entry from %s", Name, section, cfg_source()); + return -1; + } - if (strcasecmp (port, "libusb") == 0) { + if (strcasecmp(port, "libusb") == 0) { #ifdef HAVE_USB_H - use_libusb = 1; - debug ("using libusb"); + use_libusb = 1; + debug("using libusb"); #else - error ("%s: cannot use 'libusb' port.", Name); - error ("%s: lcd4linux was compiled without libusb support!", Name); - return -1; + error("%s: cannot use 'libusb' port.", Name); + error("%s: lcd4linux was compiled without libusb support!", Name); + return -1; #endif - } else { - if (port[0] == '/') { - Port = strdup(port); } else { - int len = 5 + strlen(port) + 1; - Port = malloc(len); - qprintf(Port, len, "/dev/%s", port); - } - debug ("using device %s ", Port); - } - - s=cfg_get(section, "Size", NULL); - if (s==NULL || *s=='\0') { - error ("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); - return -1; - } - if (sscanf(s,"%dx%d",&cols,&rows)!=2 || rows<1 || cols<1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); - free (s); - return -1; - } - - DROWS = rows; - DCOLS = cols; - - if (use_libusb) { - -#ifdef HAVE_USB_H - if (drv_UL_open() < 0) { - return -1; - } -#endif - - } else { - - /* open port */ - usblcd_file=open(Port,O_WRONLY); - if (usblcd_file==-1) { - error ("%s: open(%s) failed: %s", Name, Port, strerror(errno)); - return -1; - } - - /* get driver version */ - memset(buf, 0, sizeof(buf)); - if (ioctl(usblcd_file, IOC_GET_DRV_VERSION, buf) != 0) { - error ("%s: ioctl() failed, could not get Driver Version!", Name); - return -1; + if (port[0] == '/') { + Port = strdup(port); + } else { + int len = 5 + strlen(port) + 1; + Port = malloc(len); + qprintf(Port, len, "/dev/%s", port); + } + debug("using device %s ", Port); } - info("%s: Driver Version: %s", Name, buf); - - if (sscanf(buf,"USBLCD Driver Version %d.%d", &major, &minor) != 2) { - error("%s: could not read Driver Version!", Name); - return -1; + + s = cfg_get(section, "Size", NULL); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); + return -1; } - if (major != 1) { - error("%s: Driver Version %d not supported!", Name, major); - return -1; + if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; } - memset(buf, 0, sizeof(buf)); - if (ioctl(usblcd_file, IOC_GET_HARD_VERSION, buf) != 0) { - error ("%s: ioctl() failed, could not get Hardware Version!", Name); - return -1; - } - info("%s: Hardware Version: %s", Name, buf); + DROWS = rows; + DCOLS = cols; + + if (use_libusb) { + +#ifdef HAVE_USB_H + if (drv_UL_open() < 0) { + return -1; + } +#endif + + } else { + + /* open port */ + usblcd_file = open(Port, O_WRONLY); + if (usblcd_file == -1) { + error("%s: open(%s) failed: %s", Name, Port, strerror(errno)); + return -1; + } + + /* get driver version */ + memset(buf, 0, sizeof(buf)); + if (ioctl(usblcd_file, IOC_GET_DRV_VERSION, buf) != 0) { + error("%s: ioctl() failed, could not get Driver Version!", Name); + return -1; + } + info("%s: Driver Version: %s", Name, buf); + + if (sscanf(buf, "USBLCD Driver Version %d.%d", &major, &minor) != 2) { + error("%s: could not read Driver Version!", Name); + return -1; + } + if (major != 1) { + error("%s: Driver Version %d not supported!", Name, major); + return -1; + } + + memset(buf, 0, sizeof(buf)); + if (ioctl(usblcd_file, IOC_GET_HARD_VERSION, buf) != 0) { + error("%s: ioctl() failed, could not get Hardware Version!", Name); + return -1; + } + info("%s: Hardware Version: %s", Name, buf); + + if (sscanf(buf, "%d.%d", &major, &minor) != 2) { + error("%s: could not read Hardware Version!", Name); + return -1; + } - if (sscanf(buf, "%d.%d", &major, &minor) != 2) { - error("%s: could not read Hardware Version!", Name); - return -1; + if (major != 1) { + error("%s: Hardware Version %d not supported!", Name, major); + return -1; + } } - - if (major!=1) { - error("%s: Hardware Version %d not supported!", Name, major); - return -1; + + /* Init the command buffer */ + Buffer = (char *) malloc(1024); + if (Buffer == NULL) { + error("%s: coommand buffer could not be allocated: malloc() failed", Name); + return -1; } - } - - /* Init the command buffer */ - Buffer = (char*)malloc(1024); - if (Buffer == NULL) { - error ("%s: coommand buffer could not be allocated: malloc() failed", Name); - return -1; - } - BufPtr = Buffer; - - /* initialize display */ - drv_UL_command (0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ - drv_UL_command (0x08); /* Display off, cursor off, blink off */ - drv_UL_command (0x0c); /* Display on, cursor off, blink off */ - drv_UL_command (0x06); /* curser moves to right, no shift */ - - drv_UL_clear(); /* clear display */ - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_text_greet (buffer, "http://www.usblcd.de")) { - sleep (3); - drv_UL_clear(); + BufPtr = Buffer; + + /* initialize display */ + drv_UL_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ + drv_UL_command(0x08); /* Display off, cursor off, blink off */ + drv_UL_command(0x0c); /* Display on, cursor off, blink off */ + drv_UL_command(0x06); /* curser moves to right, no shift */ + + drv_UL_clear(); /* clear display */ + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_text_greet(buffer, "http://www.usblcd.de")) { + sleep(3); + drv_UL_clear(); + } } - } - - return 0; + + return 0; } @@ -484,123 +487,121 @@ static int drv_UL_start (const char *section, const int quiet) /* list models */ -int drv_UL_list (void) +int drv_UL_list(void) { - printf ("generic"); - return 0; + printf("generic"); + return 0; } /* initialize driver & display */ -int drv_UL_init (const char *section, const int quiet) +int drv_UL_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int asc255bug; - int ret; - - /* display preferences */ - XRES = 5; /* pixel width of one char */ - YRES = 8; /* pixel height of one char */ - CHARS = 8; /* number of user-defineable characters */ - CHAR0 = 0; /* ASCII of first user-defineable char */ - GOTO_COST = 2; /* number of bytes a goto command requires */ - - /* real worker functions */ - drv_generic_text_real_write = drv_UL_write; - drv_generic_text_real_defchar = drv_UL_defchar; - - - /* start display */ - if ((ret=drv_UL_start (section, quiet))!=0) - return ret; - - /* initialize generic text driver */ - if ((ret=drv_generic_text_init(section, Name))!=0) - return ret; - - /* initialize generic icon driver */ - if ((ret=drv_generic_text_icon_init())!=0) - return ret; - - /* initialize generic bar driver */ - if ((ret=drv_generic_text_bar_init(0))!=0) - return ret; - - /* add fixed chars to the bar driver */ - /* most displays have a full block on ascii 255, but some have kind of */ - /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ - /* char will not be used, but rendered by the bar driver */ - cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); - drv_generic_text_bar_add_segment ( 0, 0,255, 32); /* ASCII 32 = blank */ - if (!asc255bug) - drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */ - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_text_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_text_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_text_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none at the moment... */ - - - return 0; + WIDGET_CLASS wc; + int asc255bug; + int ret; + + /* display preferences */ + XRES = 5; /* pixel width of one char */ + YRES = 8; /* pixel height of one char */ + CHARS = 8; /* number of user-defineable characters */ + CHAR0 = 0; /* ASCII of first user-defineable char */ + GOTO_COST = 2; /* number of bytes a goto command requires */ + + /* real worker functions */ + drv_generic_text_real_write = drv_UL_write; + drv_generic_text_real_defchar = drv_UL_defchar; + + + /* start display */ + if ((ret = drv_UL_start(section, quiet)) != 0) + return ret; + + /* initialize generic text driver */ + if ((ret = drv_generic_text_init(section, Name)) != 0) + return ret; + + /* initialize generic icon driver */ + if ((ret = drv_generic_text_icon_init()) != 0) + return ret; + + /* initialize generic bar driver */ + if ((ret = drv_generic_text_bar_init(0)) != 0) + return ret; + + /* add fixed chars to the bar driver */ + /* most displays have a full block on ascii 255, but some have kind of */ + /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ + /* char will not be used, but rendered by the bar driver */ + cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); + drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ + if (!asc255bug) + drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_text_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_text_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_text_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none at the moment... */ + + + return 0; } /* close driver & display */ -int drv_UL_quit (const int quiet) +int drv_UL_quit(const int quiet) { - info("%s: shutting down.", Name); - - /* flush buffer */ - drv_UL_send(); - - drv_generic_text_quit(); - - /* clear display */ - drv_UL_clear(); - - /* say goodbye... */ - if (!quiet) { - drv_generic_text_greet ("goodbye!", NULL); - } - - if (use_libusb) { + info("%s: shutting down.", Name); + + /* flush buffer */ + drv_UL_send(); + + drv_generic_text_quit(); + + /* clear display */ + drv_UL_clear(); + + /* say goodbye... */ + if (!quiet) { + drv_generic_text_greet("goodbye!", NULL); + } + + if (use_libusb) { #ifdef HAVE_USB_H - drv_UL_close(); + drv_UL_close(); #endif - } else { - debug ("closing port %s", Port); - close(usblcd_file); - } - - if (Buffer) { - free(Buffer); - Buffer=NULL; - BufPtr=Buffer; - } - - return (0); + } else { + debug("closing port %s", Port); + close(usblcd_file); + } + + if (Buffer) { + free(Buffer); + Buffer = NULL; + BufPtr = Buffer; + } + + return (0); } DRIVER drv_USBLCD = { - name: Name, - list: drv_UL_list, - init: drv_UL_init, - quit: drv_UL_quit, + name:Name, + list:drv_UL_list, + init:drv_UL_init, + quit:drv_UL_quit, }; - - @@ -1,4 +1,4 @@ -/* $Id: drv_X11.c,v 1.10 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: drv_X11.c,v 1.11 2005/05/08 04:32:44 reinelt Exp $ * * new style X11 Driver for LCD4Linux * @@ -26,6 +26,9 @@ * * * $Log: drv_X11.c,v $ + * Revision 1.11 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.10 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -107,19 +110,19 @@ #include <dmalloc.h> #endif -static char Name[]="X11"; +static char Name[] = "X11"; -static char *fg_col,*bg_col,*hg_col; +static char *fg_col, *bg_col, *hg_col; -static int pixel = -1; /* pointsize in pixel */ -static int pgap = 0; /* gap between points */ -static int rgap = 0; /* row gap between lines */ -static int cgap = 0; /* column gap between characters */ -static int border = 0; /* window border */ +static int pixel = -1; /* pointsize in pixel */ +static int pgap = 0; /* gap between points */ +static int rgap = 0; /* row gap between lines */ +static int cgap = 0; /* column gap between characters */ +static int border = 0; /* window border */ -static int dimx, dimy; /* total window dimension in pixel */ +static int dimx, dimy; /* total window dimension in pixel */ -static unsigned char *drv_X11_FB=NULL; +static unsigned char *drv_X11_FB = NULL; static Display *dp; static int sc, dd; @@ -137,204 +140,214 @@ static Pixmap pm; static void drv_X11_blit(const int row, const int col, const int height, const int width) { - int r, c; - int dirty = 0; - - for (r=row; r<row+height && r<DROWS; r++) { - int y = border + (r/YRES)*rgap + r*(pixel+pgap); - for (c=col; c<col+width && c<DCOLS; c++) { - int x = border + (c/XRES)*cgap + c*(pixel+pgap); - unsigned char p = drv_generic_graphic_FB[r*LCOLS+c]; - if (drv_X11_FB[r*DCOLS+c] != p) { - XFillRectangle( dp, w, p? fg_gc:hg_gc, x, y, pixel, pixel); - drv_X11_FB[r*DCOLS+c] = p; - dirty=1; - } + int r, c; + int dirty = 0; + + for (r = row; r < row + height && r < DROWS; r++) { + int y = border + (r / YRES) * rgap + r * (pixel + pgap); + for (c = col; c < col + width && c < DCOLS; c++) { + int x = border + (c / XRES) * cgap + c * (pixel + pgap); + unsigned char p = drv_generic_graphic_FB[r * LCOLS + c]; + if (drv_X11_FB[r * DCOLS + c] != p) { + XFillRectangle(dp, w, p ? fg_gc : hg_gc, x, y, pixel, pixel); + drv_X11_FB[r * DCOLS + c] = p; + dirty = 1; + } + } + } + if (dirty) { + XSync(dp, False); } - } - if (dirty) { - XSync(dp, False); - } } static void drv_X11_expose(const int x, const int y, const int width, const int height) { - /* - * theory of operation: - * instead of the old, fully-featured but complicated update - * region calculation, we do an update of the whole display, - * but check before every pixel if the pixel region is inside - * the update region. - */ - - int r, c; - int x0, y0; - int x1, y1; - - x0 = x-pixel; - x1 = x+pixel+width; - y0 = y-pixel; - y1 = y+pixel+height; - - for (r=0; r<DROWS; r++) { - int yc = border + (r/YRES)*rgap + r*(pixel+pgap); - if (yc<y0 || yc>y1) continue; - for (c=0; c<DCOLS; c++) { - int xc = border + (c/XRES)*cgap + c*(pixel+pgap); - if (xc<x0 || xc>x1) continue; - XFillRectangle( dp, w, drv_generic_graphic_FB[r*LCOLS+c]? fg_gc:hg_gc, xc, yc, pixel, pixel); + /* + * theory of operation: + * instead of the old, fully-featured but complicated update + * region calculation, we do an update of the whole display, + * but check before every pixel if the pixel region is inside + * the update region. + */ + + int r, c; + int x0, y0; + int x1, y1; + + x0 = x - pixel; + x1 = x + pixel + width; + y0 = y - pixel; + y1 = y + pixel + height; + + for (r = 0; r < DROWS; r++) { + int yc = border + (r / YRES) * rgap + r * (pixel + pgap); + if (yc < y0 || yc > y1) + continue; + for (c = 0; c < DCOLS; c++) { + int xc = border + (c / XRES) * cgap + c * (pixel + pgap); + if (xc < x0 || xc > x1) + continue; + XFillRectangle(dp, w, drv_generic_graphic_FB[r * LCOLS + c] ? fg_gc : hg_gc, xc, yc, pixel, pixel); + } } - } - XSync(dp, False); + XSync(dp, False); } -static void drv_X11_timer (__attribute__((unused)) void *notused) +static void drv_X11_timer( __attribute__ ((unused)) + void *notused) { - XEvent ev; - - if (XCheckWindowEvent(dp, w, ExposureMask, &ev)==0) return; - if (ev.type==Expose) { - drv_X11_expose (ev.xexpose.x, ev.xexpose.y, ev.xexpose.width, ev.xexpose.height); - } + XEvent ev; + + if (XCheckWindowEvent(dp, w, ExposureMask, &ev) == 0) + return; + if (ev.type == Expose) { + drv_X11_expose(ev.xexpose.x, ev.xexpose.y, ev.xexpose.width, ev.xexpose.height); + } } -static int drv_X11_start (const char *section) +static int drv_X11_start(const char *section) { - char *s; - XSetWindowAttributes wa; - XSizeHints sh; - XColor dummy; - XEvent ev; - - - /* read display size from config */ - if (sscanf(s=cfg_get(section, "Size", "120x32"), "%dx%d", &DCOLS, &DROWS)!=2 || DCOLS<1 || DROWS<1) { - error ("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + char *s; + XSetWindowAttributes wa; + XSizeHints sh; + XColor dummy; + XEvent ev; + + + /* read display size from config */ + if (sscanf(s = cfg_get(section, "Size", "120x32"), "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) { + error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - - if (sscanf(s=cfg_get(section, "font", "5x8"), "%dx%d", &XRES, &YRES)!=2 || XRES<1|| YRES<1) { - error ("%s: bad %s.font '%s' from %s", Name, section, s, cfg_source()); + + if (sscanf(s = cfg_get(section, "font", "5x8"), "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) { + error("%s: bad %s.font '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - - if (sscanf(s=cfg_get(section, "pixel", "4+1"), "%d+%d", &pixel, &pgap)!=2 || pixel<1 || pgap<0) { - error ("%s: bad %s.pixel '%s' from %s", Name, section, s, cfg_source()); + + if (sscanf(s = cfg_get(section, "pixel", "4+1"), "%d+%d", &pixel, &pgap) != 2 || pixel < 1 || pgap < 0) { + error("%s: bad %s.pixel '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - - if (sscanf(s=cfg_get(section, "gap", "-1x-1"), "%dx%d", &cgap, &rgap)!=2 || cgap<-1 || rgap<-1) { - error ("%s: bad %s.gap '%s' from %s", Name, section, s, cfg_source()); + + if (sscanf(s = cfg_get(section, "gap", "-1x-1"), "%dx%d", &cgap, &rgap) != 2 || cgap < -1 || rgap < -1) { + error("%s: bad %s.gap '%s' from %s", Name, section, s, cfg_source()); + free(s); + return -1; + } free(s); - return -1; - } - free(s); - - if (rgap<0) rgap=pixel+pgap; - if (cgap<0) cgap=pixel+pgap; - - if (cfg_number(section, "border", 0, 0, -1, &border)<0) return -1; - - fg_col = cfg_get(section, "foreground", "#000000"); - bg_col = cfg_get(section, "background", "#80d000"); - hg_col = cfg_get(section, "halfground", "#70c000"); - if (*fg_col=='\\') fg_col++; - if (*bg_col=='\\') bg_col++; - if (*hg_col=='\\') hg_col++; - - - drv_X11_FB = malloc(DCOLS*DROWS); - if (drv_X11_FB==NULL) { - error ("%s: framebuffer could not be allocated: malloc() failed", Name); - return -1; - } - - /* init with 255 so all 'halfground' pixels will be drawn */ - memset(drv_X11_FB, 255, DCOLS*DROWS*sizeof(*drv_X11_FB)); - - if ((dp=XOpenDisplay(NULL))==NULL) { - error ("%s: can't open display", Name); - return -1; - } - - sc = DefaultScreen(dp); - fg_gc = DefaultGC(dp, sc); - vi = DefaultVisual(dp, sc); - dd = DefaultDepth(dp, sc); - rw = DefaultRootWindow(dp); - cm = DefaultColormap(dp, sc); - - if (XAllocNamedColor(dp, cm, fg_col, &fg_xc, &dummy) == False) { - error ("%s: can't alloc foreground color '%s'", Name, fg_col); - return -1; - } - - if (XAllocNamedColor(dp, cm, bg_col, &bg_xc, &dummy)==False) { - error ("%s: can't alloc background color '%s'", Name, bg_col); - return -1; - } - - if (XAllocNamedColor(dp, cm, hg_col, &hg_xc, &dummy)==False) { - error ("%s: can't alloc halfground color '%s'", Name, hg_col); - return -1; - } - - dimx = DCOLS*pixel + (DCOLS-1)*pgap + (DCOLS/XRES-1)*cgap; - dimy = DROWS*pixel + (DROWS-1)*pgap + (DROWS/YRES-1)*rgap; - - wa.event_mask=ExposureMask; - - w = XCreateWindow(dp, rw, 0, 0, dimx+2*border, dimy+2*border, 0, 0, InputOutput, vi, CWEventMask, &wa); - - pm = XCreatePixmap(dp, w, dimx, dimy, dd); - - sh.min_width = sh.max_width = dimx+2*border; - sh.min_height = sh.max_height = dimy+2*border; - sh.flags = PPosition|PSize|PMinSize|PMaxSize; - - XSetWMProperties(dp, w, NULL, NULL, NULL, 0, &sh, NULL, NULL); - - - XSetForeground(dp, fg_gc, fg_xc.pixel); - XSetBackground(dp, fg_gc, bg_xc.pixel); - - bg_gc = XCreateGC(dp, w, 0, NULL); - XSetForeground(dp, bg_gc, bg_xc.pixel); - XSetBackground(dp, bg_gc, fg_xc.pixel); - - hg_gc = XCreateGC(dp, w, 0, NULL); - XSetForeground(dp, hg_gc, hg_xc.pixel); - XSetBackground(dp, hg_gc, fg_xc.pixel); - - XFillRectangle(dp, pm, bg_gc, 0, 0, dimx+2*border, dimy+2*border); - XSetWindowBackground(dp, w, bg_xc.pixel); - XClearWindow(dp, w); - - XStoreName(dp, w, "LCD4Linux"); - XMapWindow(dp, w); - - XFlush(dp); - - while(1) { - XNextEvent(dp,&ev); - if (ev.type==Expose && ev.xexpose.count==0) - break; - } - - /* regularly process X events */ - /* Fixme: make 20msec configurable */ - timer_add (drv_X11_timer, NULL, 20, 0); - - return 0; + + if (rgap < 0) + rgap = pixel + pgap; + if (cgap < 0) + cgap = pixel + pgap; + + if (cfg_number(section, "border", 0, 0, -1, &border) < 0) + return -1; + + fg_col = cfg_get(section, "foreground", "#000000"); + bg_col = cfg_get(section, "background", "#80d000"); + hg_col = cfg_get(section, "halfground", "#70c000"); + if (*fg_col == '\\') + fg_col++; + if (*bg_col == '\\') + bg_col++; + if (*hg_col == '\\') + hg_col++; + + + drv_X11_FB = malloc(DCOLS * DROWS); + if (drv_X11_FB == NULL) { + error("%s: framebuffer could not be allocated: malloc() failed", Name); + return -1; + } + + /* init with 255 so all 'halfground' pixels will be drawn */ + memset(drv_X11_FB, 255, DCOLS * DROWS * sizeof(*drv_X11_FB)); + + if ((dp = XOpenDisplay(NULL)) == NULL) { + error("%s: can't open display", Name); + return -1; + } + + sc = DefaultScreen(dp); + fg_gc = DefaultGC(dp, sc); + vi = DefaultVisual(dp, sc); + dd = DefaultDepth(dp, sc); + rw = DefaultRootWindow(dp); + cm = DefaultColormap(dp, sc); + + if (XAllocNamedColor(dp, cm, fg_col, &fg_xc, &dummy) == False) { + error("%s: can't alloc foreground color '%s'", Name, fg_col); + return -1; + } + + if (XAllocNamedColor(dp, cm, bg_col, &bg_xc, &dummy) == False) { + error("%s: can't alloc background color '%s'", Name, bg_col); + return -1; + } + + if (XAllocNamedColor(dp, cm, hg_col, &hg_xc, &dummy) == False) { + error("%s: can't alloc halfground color '%s'", Name, hg_col); + return -1; + } + + dimx = DCOLS * pixel + (DCOLS - 1) * pgap + (DCOLS / XRES - 1) * cgap; + dimy = DROWS * pixel + (DROWS - 1) * pgap + (DROWS / YRES - 1) * rgap; + + wa.event_mask = ExposureMask; + + w = XCreateWindow(dp, rw, 0, 0, dimx + 2 * border, dimy + 2 * border, 0, 0, InputOutput, vi, CWEventMask, &wa); + + pm = XCreatePixmap(dp, w, dimx, dimy, dd); + + sh.min_width = sh.max_width = dimx + 2 * border; + sh.min_height = sh.max_height = dimy + 2 * border; + sh.flags = PPosition | PSize | PMinSize | PMaxSize; + + XSetWMProperties(dp, w, NULL, NULL, NULL, 0, &sh, NULL, NULL); + + + XSetForeground(dp, fg_gc, fg_xc.pixel); + XSetBackground(dp, fg_gc, bg_xc.pixel); + + bg_gc = XCreateGC(dp, w, 0, NULL); + XSetForeground(dp, bg_gc, bg_xc.pixel); + XSetBackground(dp, bg_gc, fg_xc.pixel); + + hg_gc = XCreateGC(dp, w, 0, NULL); + XSetForeground(dp, hg_gc, hg_xc.pixel); + XSetBackground(dp, hg_gc, fg_xc.pixel); + + XFillRectangle(dp, pm, bg_gc, 0, 0, dimx + 2 * border, dimy + 2 * border); + XSetWindowBackground(dp, w, bg_xc.pixel); + XClearWindow(dp, w); + + XStoreName(dp, w, "LCD4Linux"); + XMapWindow(dp, w); + + XFlush(dp); + + while (1) { + XNextEvent(dp, &ev); + if (ev.type == Expose && ev.xexpose.count == 0) + break; + } + + /* regularly process X events */ + /* Fixme: make 20msec configurable */ + timer_add(drv_X11_timer, NULL, 20, 0); + + return 0; } @@ -362,86 +375,86 @@ static int drv_X11_start (const char *section) /* list models */ -int drv_X11_list (void) +int drv_X11_list(void) { - printf ("any"); - return 0; + printf("any"); + return 0; } /* initialize driver & display */ -int drv_X11_init (const char *section, const int quiet) +int drv_X11_init(const char *section, const int quiet) { - WIDGET_CLASS wc; - int ret; - - /* real worker functions */ - drv_generic_graphic_real_blit = drv_X11_blit; - - /* start display */ - if ((ret=drv_X11_start (section))!=0) - return ret; - - /* initialize generic graphic driver */ - if ((ret=drv_generic_graphic_init(section, Name))!=0) - return ret; - - /* initially expose window */ - drv_generic_graphic_clear(); - drv_X11_expose (0, 0, dimx+2*border, dimy+2*border); - - if (!quiet) { - char buffer[40]; - qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); - if (drv_generic_graphic_greet (buffer, NULL)) { - sleep (3); - drv_generic_graphic_clear(); + WIDGET_CLASS wc; + int ret; + + /* real worker functions */ + drv_generic_graphic_real_blit = drv_X11_blit; + + /* start display */ + if ((ret = drv_X11_start(section)) != 0) + return ret; + + /* initialize generic graphic driver */ + if ((ret = drv_generic_graphic_init(section, Name)) != 0) + return ret; + + /* initially expose window */ + drv_generic_graphic_clear(); + drv_X11_expose(0, 0, dimx + 2 * border, dimy + 2 * border); + + if (!quiet) { + char buffer[40]; + qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); + if (drv_generic_graphic_greet(buffer, NULL)) { + sleep(3); + drv_generic_graphic_clear(); + } } - } - - /* register text widget */ - wc=Widget_Text; - wc.draw=drv_generic_graphic_draw; - widget_register(&wc); - - /* register icon widget */ - wc=Widget_Icon; - wc.draw=drv_generic_graphic_icon_draw; - widget_register(&wc); - - /* register bar widget */ - wc=Widget_Bar; - wc.draw=drv_generic_graphic_bar_draw; - widget_register(&wc); - - /* register plugins */ - /* none at the moment... */ - - - return 0; + + /* register text widget */ + wc = Widget_Text; + wc.draw = drv_generic_graphic_draw; + widget_register(&wc); + + /* register icon widget */ + wc = Widget_Icon; + wc.draw = drv_generic_graphic_icon_draw; + widget_register(&wc); + + /* register bar widget */ + wc = Widget_Bar; + wc.draw = drv_generic_graphic_bar_draw; + widget_register(&wc); + + /* register plugins */ + /* none at the moment... */ + + + return 0; } /* close driver & display */ -int drv_X11_quit (const __attribute__((unused)) int quiet) { - - info("%s: shutting down.", Name); - drv_generic_graphic_quit(); - - if (drv_X11_FB) { - free (drv_X11_FB); - drv_X11_FB=NULL; - } - - return (0); +int drv_X11_quit(const __attribute__ ((unused)) + int quiet) +{ + + info("%s: shutting down.", Name); + drv_generic_graphic_quit(); + + if (drv_X11_FB) { + free(drv_X11_FB); + drv_X11_FB = NULL; + } + + return (0); } DRIVER drv_X11 = { - name: Name, - list: drv_X11_list, - init: drv_X11_init, - quit: drv_X11_quit, + name:Name, + list:drv_X11_list, + init:drv_X11_init, + quit:drv_X11_quit, }; - - diff --git a/drv_generic_graphic.c b/drv_generic_graphic.c index b7240b7..80fe3cd 100644 --- a/drv_generic_graphic.c +++ b/drv_generic_graphic.c @@ -23,6 +23,9 @@ * * * $Log: drv_generic_graphic.c,v $ + * Revision 1.15 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.14 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -137,12 +140,12 @@ #endif -static char *Section=NULL; -static char *Driver=NULL; +static char *Section = NULL; +static char *Driver = NULL; -int DROWS, DCOLS; /* display size (pixels!) */ -int LROWS, LCOLS; /* layout size (pixels!) */ -int XRES, YRES; /* pixels of one char cell */ +int DROWS, DCOLS; /* display size (pixels!) */ +int LROWS, LCOLS; /* layout size (pixels!) */ +int XRES, YRES; /* pixels of one char cell */ unsigned char *drv_generic_graphic_FB = NULL; @@ -151,44 +154,46 @@ unsigned char *drv_generic_graphic_FB = NULL; /*** generic Framebuffer stuff ***/ /****************************************/ -static void drv_generic_graphic_resizeFB (int rows, int cols) +static void drv_generic_graphic_resizeFB(int rows, int cols) { - unsigned char *newFB; - int row, col; - - /* Layout FB is large enough */ - if (rows<=LROWS && cols<=LCOLS) - return; - - /* get maximum values */ - if (rows<LROWS) rows=LROWS; - if (cols<LCOLS) cols=LCOLS; - - /* allocate new Layout FB */ - newFB = malloc(cols*rows*sizeof(char)); - memset (newFB, 0, rows*cols*sizeof(char)); - - /* transfer contents */ - if (drv_generic_graphic_FB!=NULL) { - for (row=0; row<LROWS; row++) { - for (col=0; col<LCOLS; col++) { - newFB[row*cols+col]=drv_generic_graphic_FB[row*LCOLS+col]; - } + unsigned char *newFB; + int row, col; + + /* Layout FB is large enough */ + if (rows <= LROWS && cols <= LCOLS) + return; + + /* get maximum values */ + if (rows < LROWS) + rows = LROWS; + if (cols < LCOLS) + cols = LCOLS; + + /* allocate new Layout FB */ + newFB = malloc(cols * rows * sizeof(char)); + memset(newFB, 0, rows * cols * sizeof(char)); + + /* transfer contents */ + if (drv_generic_graphic_FB != NULL) { + for (row = 0; row < LROWS; row++) { + for (col = 0; col < LCOLS; col++) { + newFB[row * cols + col] = drv_generic_graphic_FB[row * LCOLS + col]; + } + } + free(drv_generic_graphic_FB); } - free (drv_generic_graphic_FB); - } - drv_generic_graphic_FB = newFB; - - LCOLS = cols; - LROWS = rows; + drv_generic_graphic_FB = newFB; + + LCOLS = cols; + LROWS = rows; } -int drv_generic_graphic_clear (void) +int drv_generic_graphic_clear(void) { - memset(drv_generic_graphic_FB, 0, DCOLS*DROWS*sizeof(*drv_generic_graphic_FB)); - drv_generic_graphic_real_blit (0, 0, DROWS, DCOLS); - return 0; + memset(drv_generic_graphic_FB, 0, DCOLS * DROWS * sizeof(*drv_generic_graphic_FB)); + drv_generic_graphic_real_blit(0, 0, DROWS, DCOLS); + return 0; } @@ -196,99 +201,101 @@ int drv_generic_graphic_clear (void) /*** generic text handling ***/ /****************************************/ -static void drv_generic_graphic_render (const int row, const int col, const char *txt) +static void drv_generic_graphic_render(const int row, const int col, const char *txt) { - int c, r, x, y; - int len = strlen(txt); - - /* maybe grow layout framebuffer */ - drv_generic_graphic_resizeFB (row + YRES, col + XRES * len); - - r = row; - c = col; - - /* render text into layout FB */ - while (*txt != '\0') { - for (y = 0; y < YRES; y++) { - int mask = 1 << XRES; - for (x = 0; x < XRES; x++) { - mask >>= 1; - drv_generic_graphic_FB[(r+y) * LCOLS + c + x] = Font_6x8[(int)*txt][y]&mask ? 1:0; - } + int c, r, x, y; + int len = strlen(txt); + + /* maybe grow layout framebuffer */ + drv_generic_graphic_resizeFB(row + YRES, col + XRES * len); + + r = row; + c = col; + + /* render text into layout FB */ + while (*txt != '\0') { + for (y = 0; y < YRES; y++) { + int mask = 1 << XRES; + for (x = 0; x < XRES; x++) { + mask >>= 1; + drv_generic_graphic_FB[(r + y) * LCOLS + c + x] = Font_6x8[(int) *txt][y] & mask ? 1 : 0; + } + } + c += XRES; + txt++; } - c += XRES; - txt++; - } - - /* flush area */ - drv_generic_graphic_real_blit (row, col, YRES, XRES*len); + + /* flush area */ + drv_generic_graphic_real_blit(row, col, YRES, XRES * len); } /* say hello to the user */ -int drv_generic_graphic_greet (const char *msg1, const char *msg2) +int drv_generic_graphic_greet(const char *msg1, const char *msg2) { - char *line1[] = { "* LCD4Linux " VERSION " *", - "LCD4Linux " VERSION, - "* LCD4Linux *", - "LCD4Linux", - "L4Linux", - NULL }; - - char *line2[] = { "http://lcd4linux.bulix.org", - "lcd4linux.bulix.org", - NULL }; - - int i; - int flag = 0; - - unsigned int cols = DCOLS/XRES; - unsigned int rows = DROWS/YRES; - - for (i = 0; line1[i]; i++) { - if (strlen(line1[i]) <= cols) { - drv_generic_graphic_render (YRES * 0 , XRES * (cols-strlen(line1[i]))/2, line1[i]); - flag = 1; - break; + char *line1[] = { "* LCD4Linux " VERSION " *", + "LCD4Linux " VERSION, + "* LCD4Linux *", + "LCD4Linux", + "L4Linux", + NULL + }; + + char *line2[] = { "http://lcd4linux.bulix.org", + "lcd4linux.bulix.org", + NULL + }; + + int i; + int flag = 0; + + unsigned int cols = DCOLS / XRES; + unsigned int rows = DROWS / YRES; + + for (i = 0; line1[i]; i++) { + if (strlen(line1[i]) <= cols) { + drv_generic_graphic_render(YRES * 0, XRES * (cols - strlen(line1[i])) / 2, line1[i]); + flag = 1; + break; + } } - } - if (rows >= 2) { - for (i = 0; line2[i]; i++) { - if (strlen(line2[i]) <= cols) { - drv_generic_graphic_render (YRES * 1, XRES * (cols-strlen(line2[i]))/2, line2[i]); - flag = 1; - break; - } + if (rows >= 2) { + for (i = 0; line2[i]; i++) { + if (strlen(line2[i]) <= cols) { + drv_generic_graphic_render(YRES * 1, XRES * (cols - strlen(line2[i])) / 2, line2[i]); + flag = 1; + break; + } + } } - } - - if (msg1 && rows >= 3) { - unsigned int len = strlen(msg1); - if ( len <= cols) { - drv_generic_graphic_render (YRES * 2, XRES * (cols-len)/2, msg1); - flag = 1; + + if (msg1 && rows >= 3) { + unsigned int len = strlen(msg1); + if (len <= cols) { + drv_generic_graphic_render(YRES * 2, XRES * (cols - len) / 2, msg1); + flag = 1; + } } - } - - if (msg2 && rows >= 4) { - unsigned int len = strlen(msg2); - if ( len <= cols) { - drv_generic_graphic_render (YRES * 3, XRES * (cols-len)/2, msg2); - flag = 1; + + if (msg2 && rows >= 4) { + unsigned int len = strlen(msg2); + if (len <= cols) { + drv_generic_graphic_render(YRES * 3, XRES * (cols - len) / 2, msg2); + flag = 1; + } } - } - return flag; + return flag; } -int drv_generic_graphic_draw (WIDGET *W) +int drv_generic_graphic_draw(WIDGET * W) { - WIDGET_TEXT *Text = W->data; - drv_generic_graphic_render (YRES * W->row, XRES * W->col, Text->buffer); - return 0; + WIDGET_TEXT *Text = W->data; + drv_generic_graphic_render(YRES * W->row, XRES * W->col, Text->buffer); + return 0; } @@ -296,38 +303,38 @@ int drv_generic_graphic_draw (WIDGET *W) /*** generic icon handling ***/ /****************************************/ -int drv_generic_graphic_icon_draw (WIDGET *W) +int drv_generic_graphic_icon_draw(WIDGET * W) { - WIDGET_ICON *Icon = W->data; - unsigned char *bitmap = Icon->bitmap+YRES*Icon->curmap; - int row, col; - int x, y; - - row = YRES*W->row; - col = XRES*W->col; - - /* maybe grow layout framebuffer */ - drv_generic_graphic_resizeFB (row+YRES, col+XRES); - - /* render icon */ - for (y=0; y<YRES; y++) { - int mask=1<<XRES; - for (x=0; x<XRES; x++) { - int i = (row+y)*LCOLS+col+x; - mask >>= 1; - if (Icon->visible) { - drv_generic_graphic_FB[i] = bitmap[y]&mask ? 1 : 0; - } else { - drv_generic_graphic_FB[i] = 0; - } + WIDGET_ICON *Icon = W->data; + unsigned char *bitmap = Icon->bitmap + YRES * Icon->curmap; + int row, col; + int x, y; + + row = YRES * W->row; + col = XRES * W->col; + + /* maybe grow layout framebuffer */ + drv_generic_graphic_resizeFB(row + YRES, col + XRES); + + /* render icon */ + for (y = 0; y < YRES; y++) { + int mask = 1 << XRES; + for (x = 0; x < XRES; x++) { + int i = (row + y) * LCOLS + col + x; + mask >>= 1; + if (Icon->visible) { + drv_generic_graphic_FB[i] = bitmap[y] & mask ? 1 : 0; + } else { + drv_generic_graphic_FB[i] = 0; + } + } } - } - /* flush area */ - drv_generic_graphic_real_blit (row, col, YRES, XRES); + /* flush area */ + drv_generic_graphic_real_blit(row, col, YRES, XRES); + + return 0; - return 0; - } @@ -335,76 +342,80 @@ int drv_generic_graphic_icon_draw (WIDGET *W) /*** generic bar handling ***/ /****************************************/ -int drv_generic_graphic_bar_draw (WIDGET *W) +int drv_generic_graphic_bar_draw(WIDGET * W) { - WIDGET_BAR *Bar = W->data; - int row, col, len, res, rev, max, val1, val2; - int x, y; - DIRECTION dir; - - row = YRES*W->row; - col = XRES*W->col; - dir = Bar->direction; - len = Bar->length; - - /* maybe grow layout framebuffer */ - if (dir & (DIR_EAST|DIR_WEST)) { - drv_generic_graphic_resizeFB (row+YRES, col+XRES*len); - } else { - drv_generic_graphic_resizeFB (row+YRES*len, col+XRES); - } - - res = dir & (DIR_EAST|DIR_WEST)?XRES:YRES; - max = len * res; - val1 = Bar->val1 * (double)(max); - val2 = Bar->val2 * (double)(max); - - if (val1<1) val1=1; - else if (val1>max) val1=max; - - if (val2<1) val2=1; - else if (val2>max) val2=max; - - rev=0; - - switch (dir) { - case DIR_WEST: - val1=max-val1; - val2=max-val2; - rev=1; - - case DIR_EAST: - for (y=0; y<YRES; y++) { - int val=y<YRES/2 ? val1 : val2; - for (x=0; x<max; x++) { - drv_generic_graphic_FB[(row+y)*LCOLS+col+x] = x<val ? !rev : rev; - } + WIDGET_BAR *Bar = W->data; + int row, col, len, res, rev, max, val1, val2; + int x, y; + DIRECTION dir; + + row = YRES * W->row; + col = XRES * W->col; + dir = Bar->direction; + len = Bar->length; + + /* maybe grow layout framebuffer */ + if (dir & (DIR_EAST | DIR_WEST)) { + drv_generic_graphic_resizeFB(row + YRES, col + XRES * len); + } else { + drv_generic_graphic_resizeFB(row + YRES * len, col + XRES); } - break; - - case DIR_SOUTH: - val1=max-val1; - val2=max-val2; - rev=1; - - case DIR_NORTH: - for (y=0; y<max; y++) { - for (x=0; x<XRES; x++) { - int val=x<XRES/2 ? val1 : val2; - drv_generic_graphic_FB[(row+y)*LCOLS+col+x] = y<val ? !rev : rev; - } + + res = dir & (DIR_EAST | DIR_WEST) ? XRES : YRES; + max = len * res; + val1 = Bar->val1 * (double) (max); + val2 = Bar->val2 * (double) (max); + + if (val1 < 1) + val1 = 1; + else if (val1 > max) + val1 = max; + + if (val2 < 1) + val2 = 1; + else if (val2 > max) + val2 = max; + + rev = 0; + + switch (dir) { + case DIR_WEST: + val1 = max - val1; + val2 = max - val2; + rev = 1; + + case DIR_EAST: + for (y = 0; y < YRES; y++) { + int val = y < YRES / 2 ? val1 : val2; + for (x = 0; x < max; x++) { + drv_generic_graphic_FB[(row + y) * LCOLS + col + x] = x < val ? !rev : rev; + } + } + break; + + case DIR_SOUTH: + val1 = max - val1; + val2 = max - val2; + rev = 1; + + case DIR_NORTH: + for (y = 0; y < max; y++) { + for (x = 0; x < XRES; x++) { + int val = x < XRES / 2 ? val1 : val2; + drv_generic_graphic_FB[(row + y) * LCOLS + col + x] = y < val ? !rev : rev; + } + } + break; + } + + /* flush area */ + if (dir & (DIR_EAST | DIR_WEST)) { + drv_generic_graphic_real_blit(row, col, YRES, XRES * len); + } else { + drv_generic_graphic_real_blit(row, col, YRES * len, XRES); } - break; - } - - /* flush area */ - if (dir & (DIR_EAST|DIR_WEST)) { - drv_generic_graphic_real_blit (row, col, YRES, XRES*len); - } else { - drv_generic_graphic_real_blit (row, col, YRES*len, XRES); - } - - return 0; + + return 0; } @@ -412,34 +423,34 @@ int drv_generic_graphic_bar_draw (WIDGET *W) /*** generic init/quit ***/ /****************************************/ -int drv_generic_graphic_init (const char *section, const char *driver) +int drv_generic_graphic_init(const char *section, const char *driver) { - Section = (char*)section; - Driver = (char*)driver; - - /* init layout framebuffer */ - LROWS = 0; - LCOLS = 0; - drv_generic_graphic_FB=NULL; - drv_generic_graphic_resizeFB (DROWS, DCOLS); - - /* sanity check */ - if (drv_generic_graphic_FB==NULL) { - error ("%s: framebuffer could not be allocated: malloc() failed", Driver); - return -1; - } - - return 0; + Section = (char *) section; + Driver = (char *) driver; + + /* init layout framebuffer */ + LROWS = 0; + LCOLS = 0; + drv_generic_graphic_FB = NULL; + drv_generic_graphic_resizeFB(DROWS, DCOLS); + + /* sanity check */ + if (drv_generic_graphic_FB == NULL) { + error("%s: framebuffer could not be allocated: malloc() failed", Driver); + return -1; + } + + return 0; } -int drv_generic_graphic_quit (void) +int drv_generic_graphic_quit(void) { - if (drv_generic_graphic_FB) { - free(drv_generic_graphic_FB); - drv_generic_graphic_FB=NULL; - } - - widget_unregister(); - return (0); + if (drv_generic_graphic_FB) { + free(drv_generic_graphic_FB); + drv_generic_graphic_FB = NULL; + } + + widget_unregister(); + return (0); } diff --git a/drv_generic_graphic.h b/drv_generic_graphic.h index 0a25815..9eb8180 100644 --- a/drv_generic_graphic.h +++ b/drv_generic_graphic.h @@ -1,4 +1,4 @@ -/* $Id: drv_generic_graphic.h,v 1.8 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: drv_generic_graphic.h,v 1.9 2005/05/08 04:32:44 reinelt Exp $ * * generic driver helper for graphic displays * @@ -23,6 +23,9 @@ * * * $Log: drv_generic_graphic.h,v $ + * Revision 1.9 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.8 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -69,23 +72,23 @@ #include "widget.h" -extern int DROWS, DCOLS; /* display size */ -extern int LROWS, LCOLS; /* layout size */ -extern int XRES, YRES; /* pixel width/height of one char */ +extern int DROWS, DCOLS; /* display size */ +extern int LROWS, LCOLS; /* layout size */ +extern int XRES, YRES; /* pixel width/height of one char */ /* framebuffer */ extern unsigned char *drv_generic_graphic_FB; /* these functions must be implemented by the real driver */ -void (*drv_generic_graphic_real_blit)(const int row, const int col, const int height, const int width); +void (*drv_generic_graphic_real_blit) (const int row, const int col, const int height, const int width); /* generic functions and widget callbacks */ -int drv_generic_graphic_init (const char *section, const char *driver); -int drv_generic_graphic_clear (void); -int drv_generic_graphic_greet (const char *msg1, const char *msg2); -int drv_generic_graphic_draw (WIDGET *W); -int drv_generic_graphic_icon_draw (WIDGET *W); -int drv_generic_graphic_bar_draw (WIDGET *W); -int drv_generic_graphic_quit (void); +int drv_generic_graphic_init(const char *section, const char *driver); +int drv_generic_graphic_clear(void); +int drv_generic_graphic_greet(const char *msg1, const char *msg2); +int drv_generic_graphic_draw(WIDGET * W); +int drv_generic_graphic_icon_draw(WIDGET * W); +int drv_generic_graphic_bar_draw(WIDGET * W); +int drv_generic_graphic_quit(void); #endif diff --git a/drv_generic_i2c.c b/drv_generic_i2c.c index 052800a..538b19a 100644 --- a/drv_generic_i2c.c +++ b/drv_generic_i2c.c @@ -1,4 +1,4 @@ -/* $Id: drv_generic_i2c.c,v 1.2 2005/04/05 05:12:57 reinelt Exp $ +/* $Id: drv_generic_i2c.c,v 1.3 2005/05/08 04:32:44 reinelt Exp $ * * generic driver helper for i2c displays * @@ -23,6 +23,9 @@ * * * $Log: drv_generic_i2c.c,v $ + * Revision 1.3 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.2 2005/04/05 05:12:57 reinelt * i2c patch from Paul (still does not work here :-( * @@ -57,105 +60,105 @@ #include "udelay.h" #include "drv_generic_i2c.h" -static char *Driver=""; -static char *Section=""; +static char *Driver = ""; +static char *Section = ""; static int i2c_device; static void my_i2c_smbus_write_byte_data(const int device, const unsigned char data) { - struct i2c_smbus_ioctl_data args; - args.read_write = I2C_SMBUS_WRITE; - args.command = data; - args.size = I2C_SMBUS_BYTE; - args.data = 0; - ioctl(device,I2C_SMBUS,&args); + struct i2c_smbus_ioctl_data args; + args.read_write = I2C_SMBUS_WRITE; + args.command = data; + args.size = I2C_SMBUS_BYTE; + args.data = 0; + ioctl(device, I2C_SMBUS, &args); } static void my_i2c_smbus_read_byte_data(const int device, const unsigned char data) { - struct i2c_smbus_ioctl_data args; - args.read_write = I2C_SMBUS_READ; - args.command = data; - args.size = I2C_SMBUS_BYTE; - args.data = 0; - ioctl(device,I2C_SMBUS,&args); + struct i2c_smbus_ioctl_data args; + args.read_write = I2C_SMBUS_READ; + args.command = data; + args.size = I2C_SMBUS_BYTE; + args.data = 0; + ioctl(device, I2C_SMBUS, &args); } -int drv_generic_i2c_open (const char *section, const char *driver) +int drv_generic_i2c_open(const char *section, const char *driver) { - int dev; - char *bus,*device; - - //SIGNAL_ENABLE = 0x40; - //SIGNAL_RW = 0x10; - //SIGNAL_RS = 0x20; - - udelay_init(); - - Section = (char*)section; - Driver = (char*)driver; - - bus = cfg_get(Section, "Port", NULL); - device = cfg_get(Section, "Device", NULL); - dev = atoi(device); - info ("%s: initializing I2C bus %s",Driver,bus); - info ("device %d",dev); - if ((i2c_device = open(bus,O_WRONLY)) < 0) { - error("%s: I2C bus %s open failed !\n",Driver,bus); - return -1; - } - - info ("%s: initializing I2C slave device 0x%x",Driver,dev); - if (ioctl(i2c_device,I2C_SLAVE, dev ) < 0) { - error("%s: error initializing device 0x%x\n",Driver,dev); - close(i2c_device); - return -1; - } - - return 0; + int dev; + char *bus, *device; + + //SIGNAL_ENABLE = 0x40; + //SIGNAL_RW = 0x10; + //SIGNAL_RS = 0x20; + + udelay_init(); + + Section = (char *) section; + Driver = (char *) driver; + + bus = cfg_get(Section, "Port", NULL); + device = cfg_get(Section, "Device", NULL); + dev = atoi(device); + info("%s: initializing I2C bus %s", Driver, bus); + info("device %d", dev); + if ((i2c_device = open(bus, O_WRONLY)) < 0) { + error("%s: I2C bus %s open failed !\n", Driver, bus); + return -1; + } + + info("%s: initializing I2C slave device 0x%x", Driver, dev); + if (ioctl(i2c_device, I2C_SLAVE, dev) < 0) { + error("%s: error initializing device 0x%x\n", Driver, dev); + close(i2c_device); + return -1; + } + + return 0; } -int drv_generic_i2c_close (void) +int drv_generic_i2c_close(void) { - close(i2c_device); + close(i2c_device); - return 0; + return 0; } -unsigned char drv_generic_i2c_wire (const char *name, const char *deflt) +unsigned char drv_generic_i2c_wire(const char *name, const char *deflt) { - unsigned char w; - char wire[256]; - char *s; - - qprintf(wire, sizeof(wire), "Wire.%s", name); - s=cfg_get (Section, wire, deflt); - if(strlen(s)==3 && strncasecmp(s,"DB",2)==0 && s[2]>='0' && s[2]<='7') { - w=s[2]-'0'; - } else if(strcasecmp(s,"GND")==0) { - w=0; - } else { - error ("%s: unknown signal <%s> for wire <%s>", Driver, s, name); - error ("%s: should be DB0..7 or GND", Driver); - return 0xff; - } - free(s); - if (w==0) { - info ("%s: wiring: [DISPLAY:%s]<==>[i2c:GND]", Driver, name); - } else { - info ("%s: wiring: [DISPLAY:%s]<==>[i2c:DB%d]", Driver, name, w); - } - - w=1<<w; - - return w; + unsigned char w; + char wire[256]; + char *s; + + qprintf(wire, sizeof(wire), "Wire.%s", name); + s = cfg_get(Section, wire, deflt); + if (strlen(s) == 3 && strncasecmp(s, "DB", 2) == 0 && s[2] >= '0' && s[2] <= '7') { + w = s[2] - '0'; + } else if (strcasecmp(s, "GND") == 0) { + w = 0; + } else { + error("%s: unknown signal <%s> for wire <%s>", Driver, s, name); + error("%s: should be DB0..7 or GND", Driver); + return 0xff; + } + free(s); + if (w == 0) { + info("%s: wiring: [DISPLAY:%s]<==>[i2c:GND]", Driver, name); + } else { + info("%s: wiring: [DISPLAY:%s]<==>[i2c:DB%d]", Driver, name, w); + } + + w = 1 << w; + + return w; } -void drv_generic_i2c_data (const unsigned char data) +void drv_generic_i2c_data(const unsigned char data) { - my_i2c_smbus_write_byte_data(i2c_device, data); - + my_i2c_smbus_write_byte_data(i2c_device, data); + } diff --git a/drv_generic_i2c.h b/drv_generic_i2c.h index 731bacc..64f4ed0 100644 --- a/drv_generic_i2c.h +++ b/drv_generic_i2c.h @@ -1,4 +1,4 @@ -/* $Id: drv_generic_i2c.h,v 1.1 2005/03/28 19:39:23 reinelt Exp $ +/* $Id: drv_generic_i2c.h,v 1.2 2005/05/08 04:32:44 reinelt Exp $ * * generic driver helper for i2c displays * @@ -23,6 +23,9 @@ * * * $Log: drv_generic_i2c.h,v $ + * Revision 1.2 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.1 2005/03/28 19:39:23 reinelt * HD44780/I2C patch from Luis merged (still does not work for me) * @@ -53,9 +56,9 @@ #ifndef _DRV_GENERIC_I2C_H_ #define _DRV_GENERIC_I2C_H_ -int drv_generic_i2c_open (const char *section, const char *driver); -int drv_generic_i2c_close (void); -unsigned char drv_generic_i2c_wire (const char *name, const char *deflt); -void drv_generic_i2c_data (const unsigned char data); +int drv_generic_i2c_open(const char *section, const char *driver); +int drv_generic_i2c_close(void); +unsigned char drv_generic_i2c_wire(const char *name, const char *deflt); +void drv_generic_i2c_data(const unsigned char data); #endif diff --git a/drv_generic_parport.c b/drv_generic_parport.c index cb173bd..bbc5cf0 100644 --- a/drv_generic_parport.c +++ b/drv_generic_parport.c @@ -1,4 +1,4 @@ -/* $Id: drv_generic_parport.c,v 1.15 2005/05/05 08:36:12 reinelt Exp $ +/* $Id: drv_generic_parport.c,v 1.16 2005/05/08 04:32:44 reinelt Exp $ * * generic driver helper for serial and parport access * @@ -23,6 +23,9 @@ * * * $Log: drv_generic_parport.c,v $ + * Revision 1.16 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.15 2005/05/05 08:36:12 reinelt * changed SELECT to SLCTIN * @@ -143,448 +146,439 @@ #include "drv_generic_parport.h" -static char *Driver=""; -static char *Section=""; -static unsigned short Port=0; -static char *PPdev=NULL; +static char *Driver = ""; +static char *Section = ""; +static unsigned short Port = 0; +static char *PPdev = NULL; /* initial value taken from linux/parport_pc.c */ static unsigned char ctr = 0xc; #ifdef WITH_PPDEV -static int PPfd=-1; +static int PPfd = -1; #endif -int drv_generic_parport_open (const char *section, const char *driver) +int drv_generic_parport_open(const char *section, const char *driver) { - char *s, *e; - - Section = (char*)section; - Driver = (char*)driver; - - udelay_init(); - + char *s, *e; + + Section = (char *) section; + Driver = (char *) driver; + + udelay_init(); + #ifndef WITH_PPDEV - error ("The files include/linux/parport.h and/or include/linux/ppdev.h"); - error ("were missing at compile time. Even if your system supports"); - error ("ppdev, it will not be used."); - error ("You *really* should install these files and recompile LCD4linux!"); + error("The files include/linux/parport.h and/or include/linux/ppdev.h"); + error("were missing at compile time. Even if your system supports"); + error("ppdev, it will not be used."); + error("You *really* should install these files and recompile LCD4linux!"); #endif - - s=cfg_get (Section, "Port", NULL); - if (s==NULL || *s=='\0') { - error ("%s: no '%s.Port' entry from %s", Driver, Section, cfg_source()); - return -1; - } - - PPdev=NULL; - if ((Port=strtol(s, &e, 0))==0 || *e!='\0') { + + s = cfg_get(Section, "Port", NULL); + if (s == NULL || *s == '\0') { + error("%s: no '%s.Port' entry from %s", Driver, Section, cfg_source()); + return -1; + } + + PPdev = NULL; + if ((Port = strtol(s, &e, 0)) == 0 || *e != '\0') { #ifdef WITH_PPDEV - Port=0; - PPdev=s; + Port = 0; + PPdev = s; #else - error ("%s: bad %s.Port '%s' from %s", Driver, Section, s, cfg_source()); - free(s); - return -1; + error("%s: bad %s.Port '%s' from %s", Driver, Section, s, cfg_source()); + free(s); + return -1; #endif - } - - -#ifdef WITH_PPDEV - - if (PPdev) { - info ("%s: using ppdev %s", Driver, PPdev); - PPfd=open(PPdev, O_RDWR); - if (PPfd==-1) { - error ("%s: open(%s) failed: %s", Driver, PPdev, strerror(errno)); - return -1; } - +#ifdef WITH_PPDEV + + if (PPdev) { + info("%s: using ppdev %s", Driver, PPdev); + PPfd = open(PPdev, O_RDWR); + if (PPfd == -1) { + error("%s: open(%s) failed: %s", Driver, PPdev, strerror(errno)); + return -1; + } #if 0 - /* Fixme: this always fails here... */ - if (ioctl(PPfd, PPEXCL)) { - debug ("ioctl(%s, PPEXCL) failed: %s", PPdev, strerror(errno)); - } else { - debug ("ioctl(%s, PPEXCL) succeded."); - } + /* Fixme: this always fails here... */ + if (ioctl(PPfd, PPEXCL)) { + debug("ioctl(%s, PPEXCL) failed: %s", PPdev, strerror(errno)); + } else { + debug("ioctl(%s, PPEXCL) succeded."); + } #endif - - if (ioctl(PPfd, PPCLAIM)) { - error ("%s: ioctl(%s, PPCLAIM) failed: %d %s", Driver, PPdev, errno, strerror(errno)); - return -1; - } - } else - + + if (ioctl(PPfd, PPCLAIM)) { + error("%s: ioctl(%s, PPCLAIM) failed: %d %s", Driver, PPdev, errno, strerror(errno)); + return -1; + } + } else #endif - + { - error ("using raw port 0x%x (deprecated!)", Port); - error ("You *really* should change your setup and use ppdev!"); - if ((Port+3)<=0x3ff) { - if (ioperm(Port, 3, 1)!=0) { - error ("%s: ioperm(0x%x) failed: %s", Driver, Port, strerror(errno)); - return -1; - } - } else { - if (iopl(3)!=0) { - error ("%s: iopl(1) failed: %s", Driver, strerror(errno)); - return -1; + error("using raw port 0x%x (deprecated!)", Port); + error("You *really* should change your setup and use ppdev!"); + if ((Port + 3) <= 0x3ff) { + if (ioperm(Port, 3, 1) != 0) { + error("%s: ioperm(0x%x) failed: %s", Driver, Port, strerror(errno)); + return -1; + } + } else { + if (iopl(3) != 0) { + error("%s: iopl(1) failed: %s", Driver, strerror(errno)); + return -1; + } } - } } - return 0; + return 0; } -int drv_generic_parport_close (void) +int drv_generic_parport_close(void) { #ifdef WITH_PPDEV - if (PPdev) { - debug ("closing ppdev %s", PPdev); - if (ioctl(PPfd, PPRELEASE)) { - error ("%s: ioctl(%s, PPRELEASE) failed: %s", Driver, PPdev, strerror(errno)); - } - if (close(PPfd)==-1) { - error ("%s: close(%s) failed: %s", Driver, PPdev, strerror(errno)); - return -1; - } - free(PPdev); - } else -#endif + if (PPdev) { + debug("closing ppdev %s", PPdev); + if (ioctl(PPfd, PPRELEASE)) { + error("%s: ioctl(%s, PPRELEASE) failed: %s", Driver, PPdev, strerror(errno)); + } + if (close(PPfd) == -1) { + error("%s: close(%s) failed: %s", Driver, PPdev, strerror(errno)); + return -1; + } + free(PPdev); + } else +#endif { - debug ("closing raw port 0x%x", Port); - if ((Port+3)<=0x3ff) { - if (ioperm(Port, 3, 0)!=0) { - error ("%s: ioperm(0x%x) failed: %s", Driver, Port, strerror(errno)); - return -1; - } - } else { - if (iopl(0)!=0) { - error ("%s: iopl(0) failed: %s", Driver, strerror(errno)); - return -1; + debug("closing raw port 0x%x", Port); + if ((Port + 3) <= 0x3ff) { + if (ioperm(Port, 3, 0) != 0) { + error("%s: ioperm(0x%x) failed: %s", Driver, Port, strerror(errno)); + return -1; + } + } else { + if (iopl(0) != 0) { + error("%s: iopl(0) failed: %s", Driver, strerror(errno)); + return -1; + } } - } } - return 0; + return 0; } -static unsigned char drv_generic_parport_signal_ctrl (const char *name, const char *signal) +static unsigned char drv_generic_parport_signal_ctrl(const char *name, const char *signal) { - unsigned char wire; - - if (strcasecmp(signal,"STROBE") == 0) { - wire = PARPORT_CONTROL_STROBE; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:STROBE (Pin 1)]", Driver, name); - } else if(strcasecmp(signal,"AUTOFD") == 0) { - wire = PARPORT_CONTROL_AUTOFD; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:AUTOFD (Pin 14)]", Driver, name); - } else if(strcasecmp(signal,"INIT") == 0) { - wire = PARPORT_CONTROL_INIT; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:INIT (Pin 16)]", Driver, name); - } else if(strcasecmp(signal,"SLCTIN") == 0) { - wire = PARPORT_CONTROL_SELECT; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:SLCTIN (Pin 17)]", Driver, name); - } else if(strcasecmp(signal,"SELECT") == 0) { - wire = PARPORT_CONTROL_SELECT; - error ("%s: SELECT is deprecated. Please use SLCTIN instead!", Driver); - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:SLCTIN (Pin 17)]", Driver, name); - } else if(strcasecmp(signal,"GND") == 0) { - wire = 0; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:GND]", Driver, name); - } else { - error ("%s: unknown signal <%s> for control line <%s>", Driver, signal, name); - error ("%s: should be STROBE, AUTOFD, INIT, SLCTIN or GND", Driver); - return 0xff; - } - - return wire; + unsigned char wire; + + if (strcasecmp(signal, "STROBE") == 0) { + wire = PARPORT_CONTROL_STROBE; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:STROBE (Pin 1)]", Driver, name); + } else if (strcasecmp(signal, "AUTOFD") == 0) { + wire = PARPORT_CONTROL_AUTOFD; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:AUTOFD (Pin 14)]", Driver, name); + } else if (strcasecmp(signal, "INIT") == 0) { + wire = PARPORT_CONTROL_INIT; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:INIT (Pin 16)]", Driver, name); + } else if (strcasecmp(signal, "SLCTIN") == 0) { + wire = PARPORT_CONTROL_SELECT; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:SLCTIN (Pin 17)]", Driver, name); + } else if (strcasecmp(signal, "SELECT") == 0) { + wire = PARPORT_CONTROL_SELECT; + error("%s: SELECT is deprecated. Please use SLCTIN instead!", Driver); + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:SLCTIN (Pin 17)]", Driver, name); + } else if (strcasecmp(signal, "GND") == 0) { + wire = 0; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:GND]", Driver, name); + } else { + error("%s: unknown signal <%s> for control line <%s>", Driver, signal, name); + error("%s: should be STROBE, AUTOFD, INIT, SLCTIN or GND", Driver); + return 0xff; + } + + return wire; } -unsigned char drv_generic_parport_wire_ctrl (const char *name, const char *deflt) +unsigned char drv_generic_parport_wire_ctrl(const char *name, const char *deflt) { - unsigned char wire; - char key[256]; - char *val; - - qprintf(key, sizeof(key), "Wire.%s", name); - val = cfg_get (Section, key, deflt); - - wire = drv_generic_parport_signal_ctrl (name, val); - - free (val); - - return wire; + unsigned char wire; + char key[256]; + char *val; + + qprintf(key, sizeof(key), "Wire.%s", name); + val = cfg_get(Section, key, deflt); + + wire = drv_generic_parport_signal_ctrl(name, val); + + free(val); + + return wire; } -unsigned char drv_generic_parport_hardwire_ctrl (const char *name, const char *signal) +unsigned char drv_generic_parport_hardwire_ctrl(const char *name, const char *signal) { - unsigned char wire; - char key[256]; - char *val; - - qprintf(key, sizeof(key), "Wire.%s", name); - val = cfg_get (Section, key, ""); - - /* maybe warn the user */ - if (*val != '\0' && strcasecmp (signal, val) != 0) { - error ("%s: ignoring configured signal <%s> for control line <%s>", Driver, val, name); - } - free (val); - - wire = drv_generic_parport_signal_ctrl (name, signal); - - return wire; + unsigned char wire; + char key[256]; + char *val; + + qprintf(key, sizeof(key), "Wire.%s", name); + val = cfg_get(Section, key, ""); + + /* maybe warn the user */ + if (*val != '\0' && strcasecmp(signal, val) != 0) { + error("%s: ignoring configured signal <%s> for control line <%s>", Driver, val, name); + } + free(val); + + wire = drv_generic_parport_signal_ctrl(name, signal); + + return wire; } -static unsigned char drv_generic_parport_signal_status (const char *name, const char *signal) +static unsigned char drv_generic_parport_signal_status(const char *name, const char *signal) { - unsigned char wire; - - if (strcasecmp(signal,"ERROR") == 0) { - wire = PARPORT_STATUS_ERROR; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:ERROR (Pin 15)]", Driver, name); - } else if(strcasecmp(signal,"SELECT") == 0) { - wire = PARPORT_STATUS_SELECT; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:SELECT (Pin 13)]", Driver, name); - } else if(strcasecmp(signal,"PAPEROUT") == 0) { - wire = PARPORT_STATUS_PAPEROUT; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:PAPEROUT (Pin 12)]", Driver, name); - } else if(strcasecmp(signal,"ACK") == 0) { - wire = PARPORT_STATUS_ACK; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:ACK (Pin 10)]", Driver, name); - } else if(strcasecmp(signal,"BUSY") == 0) { - wire = PARPORT_STATUS_BUSY; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:BUSY (Pin 11)]", Driver, name); - } else if(strcasecmp(signal,"GND") == 0) { - wire = 0; - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:GND]", Driver, name); - } else { - error ("%s: unknown signal <%s> for status line <%s>", Driver, signal, name); - error ("%s: should be ERROR, SELECT, PAPEROUT, ACK, BUSY or GND", Driver); - return 0xff; - } - - return wire; + unsigned char wire; + + if (strcasecmp(signal, "ERROR") == 0) { + wire = PARPORT_STATUS_ERROR; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:ERROR (Pin 15)]", Driver, name); + } else if (strcasecmp(signal, "SELECT") == 0) { + wire = PARPORT_STATUS_SELECT; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:SELECT (Pin 13)]", Driver, name); + } else if (strcasecmp(signal, "PAPEROUT") == 0) { + wire = PARPORT_STATUS_PAPEROUT; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:PAPEROUT (Pin 12)]", Driver, name); + } else if (strcasecmp(signal, "ACK") == 0) { + wire = PARPORT_STATUS_ACK; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:ACK (Pin 10)]", Driver, name); + } else if (strcasecmp(signal, "BUSY") == 0) { + wire = PARPORT_STATUS_BUSY; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:BUSY (Pin 11)]", Driver, name); + } else if (strcasecmp(signal, "GND") == 0) { + wire = 0; + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:GND]", Driver, name); + } else { + error("%s: unknown signal <%s> for status line <%s>", Driver, signal, name); + error("%s: should be ERROR, SELECT, PAPEROUT, ACK, BUSY or GND", Driver); + return 0xff; + } + + return wire; } -unsigned char drv_generic_parport_wire_status (const char *name, const char *deflt) +unsigned char drv_generic_parport_wire_status(const char *name, const char *deflt) { - unsigned char wire; - char key[256]; - char *val; - - qprintf(key, sizeof(key), "Wire.%s", name); - val = cfg_get (Section, key, deflt); - - wire = drv_generic_parport_signal_status (name, val); - - free (val); - - return wire; + unsigned char wire; + char key[256]; + char *val; + + qprintf(key, sizeof(key), "Wire.%s", name); + val = cfg_get(Section, key, deflt); + + wire = drv_generic_parport_signal_status(name, val); + + free(val); + + return wire; } -unsigned char drv_generic_parport_wire_data (const char *name, const char *deflt) +unsigned char drv_generic_parport_wire_data(const char *name, const char *deflt) { - unsigned char w; - char wire[256]; - char *s; - - qprintf(wire, sizeof(wire), "Wire.%s", name); - s=cfg_get (Section, wire, deflt); - if(strlen(s)==3 && strncasecmp(s,"DB",2)==0 && s[2]>='0' && s[2]<='7') { - w=s[2]-'0'; - } else if(strcasecmp(s,"GND")==0) { - w=0; - } else { - error ("%s: unknown signal <%s> for data line <%s>", Driver, s, name); - error ("%s: should be DB0..7 or GND", Driver); - return 0xff; - } - free(s); - if (w==0) { - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:GND]", Driver, name); - } else { - info ("%s: wiring: [DISPLAY:%s]<==>[PARPORT:DB%d (Pin %d)]", Driver, name, w, w+2); - } - - w=1<<w; - - return w; + unsigned char w; + char wire[256]; + char *s; + + qprintf(wire, sizeof(wire), "Wire.%s", name); + s = cfg_get(Section, wire, deflt); + if (strlen(s) == 3 && strncasecmp(s, "DB", 2) == 0 && s[2] >= '0' && s[2] <= '7') { + w = s[2] - '0'; + } else if (strcasecmp(s, "GND") == 0) { + w = 0; + } else { + error("%s: unknown signal <%s> for data line <%s>", Driver, s, name); + error("%s: should be DB0..7 or GND", Driver); + return 0xff; + } + free(s); + if (w == 0) { + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:GND]", Driver, name); + } else { + info("%s: wiring: [DISPLAY:%s]<==>[PARPORT:DB%d (Pin %d)]", Driver, name, w, w + 2); + } + + w = 1 << w; + + return w; } -void drv_generic_parport_direction (const int direction) +void drv_generic_parport_direction(const int direction) { #ifdef WITH_PPDEV - if (PPdev) { - ioctl (PPfd, PPDATADIR, &direction); - } else + if (PPdev) { + ioctl(PPfd, PPDATADIR, &direction); + } else #endif { - /* code stolen from linux/parport_pc.h */ - ctr = (ctr & ~0x20) ^ (direction?0x20:0x00); - outb (ctr, Port+2); + /* code stolen from linux/parport_pc.h */ + ctr = (ctr & ~0x20) ^ (direction ? 0x20 : 0x00); + outb(ctr, Port + 2); } } -unsigned char drv_generic_parport_status (void) +unsigned char drv_generic_parport_status(void) { - unsigned char mask = - PARPORT_STATUS_ERROR | - PARPORT_STATUS_SELECT | - PARPORT_STATUS_PAPEROUT | - PARPORT_STATUS_ACK | - PARPORT_STATUS_BUSY; - - unsigned char data; - + unsigned char mask = PARPORT_STATUS_ERROR | PARPORT_STATUS_SELECT | PARPORT_STATUS_PAPEROUT | PARPORT_STATUS_ACK | PARPORT_STATUS_BUSY; + + unsigned char data; + #ifdef WITH_PPDEV - if (PPdev) { - ioctl (PPfd, PPRSTATUS, &data); - } else + if (PPdev) { + ioctl(PPfd, PPRSTATUS, &data); + } else #endif { - data = inb (Port+1); + data = inb(Port + 1); } - - /* clear unused bits */ - data &= mask; - - return data; + + /* clear unused bits */ + data &= mask; + + return data; } -void drv_generic_parport_control (const unsigned char mask, const unsigned char value) +void drv_generic_parport_control(const unsigned char mask, const unsigned char value) { - unsigned char val; - - /* any signal affected? */ - /* Note: this may happen in case a signal is hardwired to GND */ - if (mask==0) return; + unsigned char val; + + /* any signal affected? */ + /* Note: this may happen in case a signal is hardwired to GND */ + if (mask == 0) + return; - /* Strobe, Select and AutoFeed are inverted! */ - val = mask & (value ^ (PARPORT_CONTROL_STROBE|PARPORT_CONTROL_SELECT|PARPORT_CONTROL_AUTOFD)); + /* Strobe, Select and AutoFeed are inverted! */ + val = mask & (value ^ (PARPORT_CONTROL_STROBE | PARPORT_CONTROL_SELECT | PARPORT_CONTROL_AUTOFD)); #ifdef WITH_PPDEV - if (PPdev) { - struct ppdev_frob_struct frob; - frob.mask=mask; - frob.val=val; - ioctl (PPfd, PPFCONTROL, &frob); - } else + if (PPdev) { + struct ppdev_frob_struct frob; + frob.mask = mask; + frob.val = val; + ioctl(PPfd, PPFCONTROL, &frob); + } else #endif { - /* code stolen from linux/parport_pc.h */ - ctr = (ctr & ~mask) ^ val; - outb (ctr, Port+2); + /* code stolen from linux/parport_pc.h */ + ctr = (ctr & ~mask) ^ val; + outb(ctr, Port + 2); } } -void drv_generic_parport_toggle (const unsigned char bits, const int level, const int delay) +void drv_generic_parport_toggle(const unsigned char bits, const int level, const int delay) { - unsigned char value1, value2; - - /* any signal affected? */ - /* Note: this may happen in case a signal is hardwired to GND */ - if (bits == 0) return; - - /* prepare value */ - value1 = level ? bits : 0; - value2 = level ? 0 : bits; - - /* Strobe, Select and AutoFeed are inverted! */ - value1 = bits & (value1 ^ (PARPORT_CONTROL_STROBE|PARPORT_CONTROL_SELECT|PARPORT_CONTROL_AUTOFD)); - value2 = bits & (value2 ^ (PARPORT_CONTROL_STROBE|PARPORT_CONTROL_SELECT|PARPORT_CONTROL_AUTOFD)); - - + unsigned char value1, value2; + + /* any signal affected? */ + /* Note: this may happen in case a signal is hardwired to GND */ + if (bits == 0) + return; + + /* prepare value */ + value1 = level ? bits : 0; + value2 = level ? 0 : bits; + + /* Strobe, Select and AutoFeed are inverted! */ + value1 = bits & (value1 ^ (PARPORT_CONTROL_STROBE | PARPORT_CONTROL_SELECT | PARPORT_CONTROL_AUTOFD)); + value2 = bits & (value2 ^ (PARPORT_CONTROL_STROBE | PARPORT_CONTROL_SELECT | PARPORT_CONTROL_AUTOFD)); + + #ifdef WITH_PPDEV - if (PPdev) { - struct ppdev_frob_struct frob; - frob.mask = bits; - - /* rise */ - frob.val = value1; - ioctl (PPfd, PPFCONTROL, &frob); - - /* pulse width */ - ndelay(delay); - - /* lower */ - frob.val = value2; - ioctl (PPfd, PPFCONTROL, &frob); - - } else + if (PPdev) { + struct ppdev_frob_struct frob; + frob.mask = bits; + + /* rise */ + frob.val = value1; + ioctl(PPfd, PPFCONTROL, &frob); + + /* pulse width */ + ndelay(delay); + + /* lower */ + frob.val = value2; + ioctl(PPfd, PPFCONTROL, &frob); + + } else #endif { - /* rise */ - ctr = (ctr & ~bits) ^ value1; - outb (ctr, Port+2); - - /* pulse width */ - ndelay(delay); - - /* lower */ - ctr = (ctr & ~bits) ^ value2; - outb (ctr, Port+2); + /* rise */ + ctr = (ctr & ~bits) ^ value1; + outb(ctr, Port + 2); + + /* pulse width */ + ndelay(delay); + + /* lower */ + ctr = (ctr & ~bits) ^ value2; + outb(ctr, Port + 2); } } -void drv_generic_parport_data (const unsigned char data) +void drv_generic_parport_data(const unsigned char data) { #ifdef WITH_PPDEV - if (PPdev) { - ioctl(PPfd, PPWDATA, &data); - } else + if (PPdev) { + ioctl(PPfd, PPWDATA, &data); + } else #endif { - outb (data, Port); + outb(data, Port); } } -unsigned char drv_generic_parport_read (void) +unsigned char drv_generic_parport_read(void) { - unsigned char data; - + unsigned char data; + #ifdef WITH_PPDEV - if (PPdev) { - ioctl (PPfd, PPRDATA, &data); - } else + if (PPdev) { + ioctl(PPfd, PPRDATA, &data); + } else #endif { - data=inb (Port); + data = inb(Port); } - return data; + return data; } void drv_generic_parport_debug(void) { - unsigned char control; - + unsigned char control; + #ifdef WITH_PPDEV - if (PPdev) { - ioctl (PPfd, PPRCONTROL, &control); - } else + if (PPdev) { + ioctl(PPfd, PPRCONTROL, &control); + } else #endif { - control=ctr; + control = ctr; } - - debug ("%cSTROBE %cAUTOFD %cINIT %cSLCTIN", - control & PARPORT_CONTROL_STROBE ? '-':'+', - control & PARPORT_CONTROL_AUTOFD ? '-':'+', - control & PARPORT_CONTROL_INIT ? '+':'-', - control & PARPORT_CONTROL_SELECT ? '-':'+'); - + + debug("%cSTROBE %cAUTOFD %cINIT %cSLCTIN", + control & PARPORT_CONTROL_STROBE ? '-' : '+', + control & PARPORT_CONTROL_AUTOFD ? '-' : '+', control & PARPORT_CONTROL_INIT ? '+' : '-', control & PARPORT_CONTROL_SELECT ? '-' : '+'); + } diff --git a/drv_generic_parport.h b/drv_generic_parport.h index 52aa63b..089085c 100644 --- a/drv_generic_parport.h +++ b/drv_generic_parport.h @@ -1,4 +1,4 @@ -/* $Id: drv_generic_parport.h,v 1.10 2005/05/04 06:13:05 reinelt Exp $ +/* $Id: drv_generic_parport.h,v 1.11 2005/05/08 04:32:44 reinelt Exp $ * * generic driver helper for parallel port displays * @@ -23,6 +23,9 @@ * * * $Log: drv_generic_parport.h,v $ + * Revision 1.11 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.10 2005/05/04 06:13:05 reinelt * parport_wire_status() added * @@ -120,18 +123,18 @@ #ifndef _DRV_GENERIC_PARPORT_H_ #define _DRV_GENERIC_PARPORT_H_ -int drv_generic_parport_open (const char *section, const char *driver); -int drv_generic_parport_close (void); -unsigned char drv_generic_parport_wire_ctrl (const char *name, const char *deflt); -unsigned char drv_generic_parport_hardwire_ctrl (const char *name, const char *deflt); -unsigned char drv_generic_parport_wire_status (const char *name, const char *deflt); -unsigned char drv_generic_parport_wire_data (const char *name, const char *deflt); -void drv_generic_parport_direction (const int direction); -unsigned char drv_generic_parport_status (void); -void drv_generic_parport_control (const unsigned char mask, const unsigned char value); -void drv_generic_parport_toggle (const unsigned char bit, const int level, const int delay); -void drv_generic_parport_data (const unsigned char data); -unsigned char drv_generic_parport_read (void); -void drv_generic_parport_debug (void); +int drv_generic_parport_open(const char *section, const char *driver); +int drv_generic_parport_close(void); +unsigned char drv_generic_parport_wire_ctrl(const char *name, const char *deflt); +unsigned char drv_generic_parport_hardwire_ctrl(const char *name, const char *deflt); +unsigned char drv_generic_parport_wire_status(const char *name, const char *deflt); +unsigned char drv_generic_parport_wire_data(const char *name, const char *deflt); +void drv_generic_parport_direction(const int direction); +unsigned char drv_generic_parport_status(void); +void drv_generic_parport_control(const unsigned char mask, const unsigned char value); +void drv_generic_parport_toggle(const unsigned char bit, const int level, const int delay); +void drv_generic_parport_data(const unsigned char data); +unsigned char drv_generic_parport_read(void); +void drv_generic_parport_debug(void); #endif diff --git a/drv_generic_serial.c b/drv_generic_serial.c index de42bb8..4644903 100644 --- a/drv_generic_serial.c +++ b/drv_generic_serial.c @@ -1,4 +1,4 @@ -/* $Id: drv_generic_serial.c,v 1.16 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: drv_generic_serial.c,v 1.17 2005/05/08 04:32:44 reinelt Exp $ * * generic driver helper for serial and usbserial displays * @@ -23,6 +23,9 @@ * * * $Log: drv_generic_serial.c,v $ + * Revision 1.17 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.16 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -150,11 +153,11 @@ #include "drv_generic_serial.h" -static char *Section; -static char *Driver; -static char *Port; +static char *Section; +static char *Driver; +static char *Port; static speed_t Speed; -static int Device=-1; +static int Device = -1; #define LOCK "/var/lock/LCK..%s" @@ -164,265 +167,287 @@ static int Device=-1; /*** generic serial/USB communication ***/ /****************************************/ -static pid_t drv_generic_serial_lock_port (const char *Port) +static pid_t drv_generic_serial_lock_port(const char *Port) { - char lockfile[256]; - char tempfile[256]; - char buffer[16]; - char *port, *p; - int fd, len, pid; - - if (strncmp(Port, "/dev/", 5)==0) { - port=strdup(Port+5); - } else { - port=strdup(Port); - } - - while ((p=strchr(port, '/'))!=NULL) { - *p='_'; - } - - qprintf(lockfile, sizeof(lockfile), LOCK, port); - qprintf(tempfile, sizeof(tempfile), LOCK, "TMP.XXXXXX"); - - free (port); - - if ((fd=mkstemp(tempfile))==-1) { - error ("mkstemp(%s) failed: %s", tempfile, strerror(errno)); - return -1; - } - - if (fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)==-1) { - error ("fchmod(%s) failed: %s", tempfile, strerror(errno)); - close(fd); - unlink(tempfile); - return -1; - } - - snprintf (buffer, sizeof(buffer), "%10d\n", (int)getpid()); - len = strlen(buffer); - if (write(fd, buffer, len) != len) { - error ("write(%s) failed: %s", tempfile, strerror(errno)); - close(fd); - unlink(tempfile); - return -1; - } - close (fd); - - - while (link(tempfile, lockfile)==-1) { - - if (errno!=EEXIST) { - error ("link(%s, %s) failed: %s", tempfile, lockfile, strerror(errno)); - unlink(tempfile); - return -1; + char lockfile[256]; + char tempfile[256]; + char buffer[16]; + char *port, *p; + int fd, len, pid; + + if (strncmp(Port, "/dev/", 5) == 0) { + port = strdup(Port + 5); + } else { + port = strdup(Port); } - if ((fd=open(lockfile, O_RDONLY))==-1) { - if (errno==ENOENT) continue; /* lockfile disappared */ - error ("open(%s) failed: %s", lockfile, strerror(errno)); - unlink (tempfile); - return -1; + while ((p = strchr(port, '/')) != NULL) { + *p = '_'; } - len=read(fd, buffer, sizeof(buffer)-1); - if (len<0) { - error ("read(%s) failed: %s", lockfile, strerror(errno)); - unlink (tempfile); - return -1; + qprintf(lockfile, sizeof(lockfile), LOCK, port); + qprintf(tempfile, sizeof(tempfile), LOCK, "TMP.XXXXXX"); + + free(port); + + if ((fd = mkstemp(tempfile)) == -1) { + error("mkstemp(%s) failed: %s", tempfile, strerror(errno)); + return -1; } - - buffer[len]='\0'; - if (sscanf(buffer, "%d", &pid)!=1 || pid==0) { - error ("scan(%s) failed.", lockfile); - unlink (tempfile); - return -1; + + if (fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) { + error("fchmod(%s) failed: %s", tempfile, strerror(errno)); + close(fd); + unlink(tempfile); + return -1; } - if (pid==getpid()) { - error ("%s already locked by us. uh-oh...", lockfile); - unlink(tempfile); - return 0; + snprintf(buffer, sizeof(buffer), "%10d\n", (int) getpid()); + len = strlen(buffer); + if (write(fd, buffer, len) != len) { + error("write(%s) failed: %s", tempfile, strerror(errno)); + close(fd); + unlink(tempfile); + return -1; } - - if ((kill(pid, 0)==-1) && errno==ESRCH) { - error ("removing stale lockfile %s", lockfile); - if (unlink(lockfile)==-1 && errno!=ENOENT) { - error ("unlink(%s) failed: %s", lockfile, strerror(errno)); + close(fd); + + + while (link(tempfile, lockfile) == -1) { + + if (errno != EEXIST) { + error("link(%s, %s) failed: %s", tempfile, lockfile, strerror(errno)); + unlink(tempfile); + return -1; + } + + if ((fd = open(lockfile, O_RDONLY)) == -1) { + if (errno == ENOENT) + continue; /* lockfile disappared */ + error("open(%s) failed: %s", lockfile, strerror(errno)); + unlink(tempfile); + return -1; + } + + len = read(fd, buffer, sizeof(buffer) - 1); + if (len < 0) { + error("read(%s) failed: %s", lockfile, strerror(errno)); + unlink(tempfile); + return -1; + } + + buffer[len] = '\0'; + if (sscanf(buffer, "%d", &pid) != 1 || pid == 0) { + error("scan(%s) failed.", lockfile); + unlink(tempfile); + return -1; + } + + if (pid == getpid()) { + error("%s already locked by us. uh-oh...", lockfile); + unlink(tempfile); + return 0; + } + + if ((kill(pid, 0) == -1) && errno == ESRCH) { + error("removing stale lockfile %s", lockfile); + if (unlink(lockfile) == -1 && errno != ENOENT) { + error("unlink(%s) failed: %s", lockfile, strerror(errno)); + unlink(tempfile); + return pid; + } + continue; + } unlink(tempfile); return pid; - } - continue; } - unlink (tempfile); - return pid; - } - - unlink (tempfile); - return 0; + + unlink(tempfile); + return 0; } -static pid_t drv_generic_serial_unlock_port (const char *Port) +static pid_t drv_generic_serial_unlock_port(const char *Port) { - char lockfile[256]; - char *port, *p; - - if (strncmp(Port, "/dev/", 5)==0) { - port=strdup(Port+5); - } else { - port=strdup(Port); - } - - while ((p=strchr(port, '/'))!=NULL) { - *p='_'; - } - - qprintf(lockfile, sizeof(lockfile), LOCK, port); - unlink (lockfile); - free (port); - - return 0; + char lockfile[256]; + char *port, *p; + + if (strncmp(Port, "/dev/", 5) == 0) { + port = strdup(Port + 5); + } else { + port = strdup(Port); + } + + while ((p = strchr(port, '/')) != NULL) { + *p = '_'; + } + + qprintf(lockfile, sizeof(lockfile), LOCK, port); + unlink(lockfile); + free(port); + + return 0; } -int drv_generic_serial_open (const char *section, const char *driver, const unsigned int flags) +int drv_generic_serial_open(const char *section, const char *driver, const unsigned int flags) { - int i, fd; - pid_t pid; - struct termios portset; - - Section = (char*)section; - Driver = (char*)driver; - - Port=cfg_get(section, "Port", NULL); - if (Port==NULL || *Port=='\0') { - error ("%s: no '%s.Port' entry from %s", Driver, section, cfg_source()); - return -1; - } - - if (cfg_number(section, "Speed", 19200, 1200, 115200, &i)<0) return -1; - switch (i) { - case 1200: Speed = B1200; break; - case 2400: Speed = B2400; break; - case 4800: Speed = B4800; break; - case 9600: Speed = B9600; break; - case 19200: Speed = B19200; break; - case 38400: Speed = B38400; break; - case 57600: Speed = B57600; break; - case 115200: Speed = B115200; break; - default: - error ("%s: unsupported speed '%d' from %s", Driver, i, cfg_source()); - return -1; - } - - info ("%s: using port '%s' at %d baud", Driver, Port, i); - - if ((pid=drv_generic_serial_lock_port(Port))!=0) { - if (pid==-1) - error ("%s: port %s could not be locked", Driver, Port); - else - error ("%s: port %s is locked by process %d", Driver, Port, pid); - return -1; - } - - fd = open(Port, O_RDWR | O_NOCTTY | O_NDELAY); - if (fd==-1) { - error ("%s: open(%s) failed: %s", Driver, Port, strerror(errno)); - drv_generic_serial_unlock_port(Port); - return -1; - } + int i, fd; + pid_t pid; + struct termios portset; - if (tcgetattr(fd, &portset)==-1) { - error ("%s: tcgetattr(%s) failed: %s", Driver, Port, strerror(errno)); - drv_generic_serial_unlock_port(Port); - return -1; - } - - cfmakeraw(&portset); - portset.c_cflag |= flags; - cfsetispeed(&portset, Speed); - cfsetospeed(&portset, Speed); - if (tcsetattr(fd, TCSANOW, &portset)==-1) { - error ("%s: tcsetattr(%s) failed: %s", Driver, Port, strerror(errno)); - drv_generic_serial_unlock_port(Port); - return -1; - } - - Device=fd; - return Device; + Section = (char *) section; + Driver = (char *) driver; + + Port = cfg_get(section, "Port", NULL); + if (Port == NULL || *Port == '\0') { + error("%s: no '%s.Port' entry from %s", Driver, section, cfg_source()); + return -1; + } + + if (cfg_number(section, "Speed", 19200, 1200, 115200, &i) < 0) + return -1; + switch (i) { + case 1200: + Speed = B1200; + break; + case 2400: + Speed = B2400; + break; + case 4800: + Speed = B4800; + break; + case 9600: + Speed = B9600; + break; + case 19200: + Speed = B19200; + break; + case 38400: + Speed = B38400; + break; + case 57600: + Speed = B57600; + break; + case 115200: + Speed = B115200; + break; + default: + error("%s: unsupported speed '%d' from %s", Driver, i, cfg_source()); + return -1; + } + + info("%s: using port '%s' at %d baud", Driver, Port, i); + + if ((pid = drv_generic_serial_lock_port(Port)) != 0) { + if (pid == -1) + error("%s: port %s could not be locked", Driver, Port); + else + error("%s: port %s is locked by process %d", Driver, Port, pid); + return -1; + } + + fd = open(Port, O_RDWR | O_NOCTTY | O_NDELAY); + if (fd == -1) { + error("%s: open(%s) failed: %s", Driver, Port, strerror(errno)); + drv_generic_serial_unlock_port(Port); + return -1; + } + + if (tcgetattr(fd, &portset) == -1) { + error("%s: tcgetattr(%s) failed: %s", Driver, Port, strerror(errno)); + drv_generic_serial_unlock_port(Port); + return -1; + } + + cfmakeraw(&portset); + portset.c_cflag |= flags; + cfsetispeed(&portset, Speed); + cfsetospeed(&portset, Speed); + if (tcsetattr(fd, TCSANOW, &portset) == -1) { + error("%s: tcsetattr(%s) failed: %s", Driver, Port, strerror(errno)); + drv_generic_serial_unlock_port(Port); + return -1; + } + + Device = fd; + return Device; } -int drv_generic_serial_poll (char *string, const int len) +int drv_generic_serial_poll(char *string, const int len) { - int ret; - if (Device == -1) return -1; - ret = read (Device, string, len); - if (ret < 0 && errno != EAGAIN) { - error("%s: read(%s) failed: %s", Driver, Port, strerror(errno)); - } - return ret; + int ret; + if (Device == -1) + return -1; + ret = read(Device, string, len); + if (ret < 0 && errno != EAGAIN) { + error("%s: read(%s) failed: %s", Driver, Port, strerror(errno)); + } + return ret; } -int drv_generic_serial_read (char *string, const int len) +int drv_generic_serial_read(char *string, const int len) { - int count, run, ret; - - count = len < 0 ? -len : len; - - for (run = 0; run < 10; run ++) { - ret = drv_generic_serial_poll(string, count); - if (ret >= 0 || errno != EAGAIN) break; - info ("%s: read(%s): EAGAIN", Driver, Port); - usleep(1000); - } - - if (ret > 0 && ret != count && len > 0) { - error ("%s: partial read(%s): len=%d ret=%d", Driver, Port, len, ret); - } - - return ret; + int count, run, ret; + + count = len < 0 ? -len : len; + + for (run = 0; run < 10; run++) { + ret = drv_generic_serial_poll(string, count); + if (ret >= 0 || errno != EAGAIN) + break; + info("%s: read(%s): EAGAIN", Driver, Port); + usleep(1000); + } + + if (ret > 0 && ret != count && len > 0) { + error("%s: partial read(%s): len=%d ret=%d", Driver, Port, len, ret); + } + + return ret; } -void drv_generic_serial_write (const char *string, const int len) +void drv_generic_serial_write(const char *string, const int len) { - int run, ret; - + int run, ret; + #if 0 - int i; - for (i = 0; i < len; i++) { - int c = string[i]; - debug ("serial_write: %03d %03o 0x%02x %c", c, c, c, iscntrl(c) ? '*' : c); - } + int i; + for (i = 0; i < len; i++) { + int c = string[i]; + debug("serial_write: %03d %03o 0x%02x %c", c, c, c, iscntrl(c) ? '*' : c); + } #endif - - if (Device == -1) return; - for (run = 0; run < 10; run++) { - ret = write (Device, string, len); - if (ret >= 0 || errno != EAGAIN) break; - if (run > 0) info ("%s: write(%s): EAGAIN #%d", Driver, Port, run); - usleep(1000); - } - - if (ret < 0) { - error ("%s: write(%s) failed: %s", Driver, Port, strerror(errno)); - } else if (ret != len) { - error ("%s: partial write(%s): len=%d ret=%d", Driver, Port, len, ret); - } - - return; + + if (Device == -1) + return; + for (run = 0; run < 10; run++) { + ret = write(Device, string, len); + if (ret >= 0 || errno != EAGAIN) + break; + if (run > 0) + info("%s: write(%s): EAGAIN #%d", Driver, Port, run); + usleep(1000); + } + + if (ret < 0) { + error("%s: write(%s) failed: %s", Driver, Port, strerror(errno)); + } else if (ret != len) { + error("%s: partial write(%s): len=%d ret=%d", Driver, Port, len, ret); + } + + return; } -int drv_generic_serial_close (void) +int drv_generic_serial_close(void) { - info ("%s: closing port %s", Driver, Port); - close (Device); - drv_generic_serial_unlock_port(Port); - free(Port); - return 0; + info("%s: closing port %s", Driver, Port); + close(Device); + drv_generic_serial_unlock_port(Port); + free(Port); + return 0; } - diff --git a/drv_generic_serial.h b/drv_generic_serial.h index 933b250..102a778 100644 --- a/drv_generic_serial.h +++ b/drv_generic_serial.h @@ -1,4 +1,4 @@ -/* $Id: drv_generic_serial.h,v 1.9 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: drv_generic_serial.h,v 1.10 2005/05/08 04:32:44 reinelt Exp $ * * generic driver helper for serial and usbserial displays * @@ -23,6 +23,9 @@ * * * $Log: drv_generic_serial.h,v $ + * Revision 1.10 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -76,10 +79,10 @@ #ifndef _DRV_GENERIC_SERIALH_ #define _DRV_GENERIC_SERIAL_H_ -int drv_generic_serial_open (const char *section, const char *driver, const unsigned int flags); -int drv_generic_serial_poll (char *string, const int len); -int drv_generic_serial_read (char *string, const int len); -void drv_generic_serial_write (const char *string, const int len); -int drv_generic_serial_close (void); +int drv_generic_serial_open(const char *section, const char *driver, const unsigned int flags); +int drv_generic_serial_poll(char *string, const int len); +int drv_generic_serial_read(char *string, const int len); +void drv_generic_serial_write(const char *string, const int len); +int drv_generic_serial_close(void); #endif diff --git a/drv_generic_text.c b/drv_generic_text.c index 36309f4..f250ca2 100644 --- a/drv_generic_text.c +++ b/drv_generic_text.c @@ -1,4 +1,4 @@ -/* $Id: drv_generic_text.c,v 1.26 2005/05/06 06:37:34 reinelt Exp $ +/* $Id: drv_generic_text.c,v 1.27 2005/05/08 04:32:44 reinelt Exp $ * * generic driver helper for text-based displays * @@ -23,6 +23,9 @@ * * * $Log: drv_generic_text.c,v $ + * Revision 1.27 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.26 2005/05/06 06:37:34 reinelt * hollow bar patch from geronet * @@ -213,43 +216,43 @@ typedef struct { - int val1; - int val2; - DIRECTION dir; - STYLE style; - int segment; - int invalid; + int val1; + int val2; + DIRECTION dir; + STYLE style; + int segment; + int invalid; } BAR; typedef struct { - int val1; - int val2; - DIRECTION dir; - STYLE style; - int used; - int ascii; + int val1; + int val2; + DIRECTION dir; + STYLE style; + int used; + int ascii; } SEGMENT; -static char *Section=NULL; -static char *Driver=NULL; +static char *Section = NULL; +static char *Driver = NULL; -int DROWS = 20; /* display size: rows */ -int DCOLS = 4; /* display size: columns */ -int LROWS = 20; /* layout size: rows */ -int LCOLS = 4; /* layout size: columns */ -int XRES = 6; /* pixels of one char cell */ -int YRES = 8; /* pixels of one char cell */ -int CHARS = 0; /* number of user-defineable characters */ -int CHAR0 = 0; /* ASCII of first user-defineable char */ -int ICONS = 0; /* number of user-defineable characters reserved for icons */ +int DROWS = 20; /* display size: rows */ +int DCOLS = 4; /* display size: columns */ +int LROWS = 20; /* layout size: rows */ +int LCOLS = 4; /* layout size: columns */ +int XRES = 6; /* pixels of one char cell */ +int YRES = 8; /* pixels of one char cell */ +int CHARS = 0; /* number of user-defineable characters */ +int CHAR0 = 0; /* ASCII of first user-defineable char */ +int ICONS = 0; /* number of user-defineable characters reserved for icons */ -int GOTO_COST = 0; /* number of bytes a goto command requires */ -int INVALIDATE = 0; /* re-send a modified userdefined char? */ +int GOTO_COST = 0; /* number of bytes a goto command requires */ +int INVALIDATE = 0; /* re-send a modified userdefined char? */ -static char *LayoutFB = NULL; -static char *DisplayFB = NULL; +static char *LayoutFB = NULL; +static char *DisplayFB = NULL; static int Single_Segments = 0; @@ -264,62 +267,64 @@ static BAR *BarFB = NULL; /*** generic Framebuffer stuff ***/ /****************************************/ -static void drv_generic_text_resizeFB (int rows, int cols) +static void drv_generic_text_resizeFB(int rows, int cols) { - char *newFB; - BAR *newBar; - int i, row, col; - - /* Layout FB is large enough */ - if (rows <= LROWS && cols <= LCOLS) - return; - - /* get maximum values */ - if (rows < LROWS) rows = LROWS; - if (cols < LCOLS) cols = LCOLS; - - /* allocate new Layout FB */ - newFB = malloc(cols * rows * sizeof(char)); - memset (newFB, ' ', rows * cols * sizeof(char)); - - /* transfer contents */ - if (LayoutFB != NULL) { - for (row = 0; row < LROWS; row++) { - for (col = 0; col < LCOLS; col++) { - newFB[row * cols + col] = LayoutFB[row * LCOLS + col]; - } - } - free (LayoutFB); - } - LayoutFB = newFB; - - - /* resize Bar buffer */ - if (BarFB) { - - newBar = malloc (rows * cols * sizeof(BAR)); - - for (i = 0; i < rows * cols; i++) { - newBar[i].val1 = -1; - newBar[i].val2 = -1; - newBar[i].dir = 0; - newBar[i].segment = -1; - newBar[i].invalid = 0; - } - + char *newFB; + BAR *newBar; + int i, row, col; + + /* Layout FB is large enough */ + if (rows <= LROWS && cols <= LCOLS) + return; + + /* get maximum values */ + if (rows < LROWS) + rows = LROWS; + if (cols < LCOLS) + cols = LCOLS; + + /* allocate new Layout FB */ + newFB = malloc(cols * rows * sizeof(char)); + memset(newFB, ' ', rows * cols * sizeof(char)); + /* transfer contents */ - for (row = 0; row < LROWS; row++) { - for (col = 0; col < LCOLS; col++) { - newBar[row * cols + col] = BarFB[row * LCOLS + col]; - } + if (LayoutFB != NULL) { + for (row = 0; row < LROWS; row++) { + for (col = 0; col < LCOLS; col++) { + newFB[row * cols + col] = LayoutFB[row * LCOLS + col]; + } + } + free(LayoutFB); } + LayoutFB = newFB; + + + /* resize Bar buffer */ + if (BarFB) { + + newBar = malloc(rows * cols * sizeof(BAR)); + + for (i = 0; i < rows * cols; i++) { + newBar[i].val1 = -1; + newBar[i].val2 = -1; + newBar[i].dir = 0; + newBar[i].segment = -1; + newBar[i].invalid = 0; + } + + /* transfer contents */ + for (row = 0; row < LROWS; row++) { + for (col = 0; col < LCOLS; col++) { + newBar[row * cols + col] = BarFB[row * LCOLS + col]; + } + } - free (BarFB); - BarFB = newBar; - } - - LCOLS = cols; - LROWS = rows; + free(BarFB); + BarFB = newBar; + } + + LCOLS = cols; + LROWS = rows; } @@ -327,152 +332,157 @@ static void drv_generic_text_resizeFB (int rows, int cols) /*** generic text handling ***/ /****************************************/ -int drv_generic_text_init (const char *section, const char *driver) +int drv_generic_text_init(const char *section, const char *driver) { - Section = (char*)section; - Driver = (char*)driver; - - /* init display framebuffer */ - DisplayFB = (char*)malloc(DCOLS * DROWS * sizeof(char)); - memset (DisplayFB, ' ', DROWS * DCOLS * sizeof(char)); - - /* init layout framebuffer */ - LROWS = 0; - LCOLS = 0; - LayoutFB = NULL; - drv_generic_text_resizeFB (DROWS, DCOLS); - - /* sanity check */ - if (LayoutFB == NULL || DisplayFB == NULL) { - error ("%s: framebuffer could not be allocated: malloc() failed", Driver); - return -1; - } - - return 0; + Section = (char *) section; + Driver = (char *) driver; + + /* init display framebuffer */ + DisplayFB = (char *) malloc(DCOLS * DROWS * sizeof(char)); + memset(DisplayFB, ' ', DROWS * DCOLS * sizeof(char)); + + /* init layout framebuffer */ + LROWS = 0; + LCOLS = 0; + LayoutFB = NULL; + drv_generic_text_resizeFB(DROWS, DCOLS); + + /* sanity check */ + if (LayoutFB == NULL || DisplayFB == NULL) { + error("%s: framebuffer could not be allocated: malloc() failed", Driver); + return -1; + } + + return 0; } /* say hello to the user */ -int drv_generic_text_greet (const char *msg1, const char *msg2) +int drv_generic_text_greet(const char *msg1, const char *msg2) { - int i; - int flag = 0; - - char *line1[] = { "* LCD4Linux " VERSION " *", - "LCD4Linux " VERSION, - "* LCD4Linux *", - "LCD4Linux", - "L4Linux", - NULL }; - - char *line2[] = { "http://lcd4linux.bulix.org", - "lcd4linux.bulix.org", - NULL }; - - - for (i = 0; line1[i]; i++) { - if (strlen(line1[i]) <= (unsigned)DCOLS) { - drv_generic_text_real_write (0, (DCOLS - strlen(line1[i])) / 2, line1[i], strlen(line1[i])); - flag = 1; - break; + int i; + int flag = 0; + + char *line1[] = { "* LCD4Linux " VERSION " *", + "LCD4Linux " VERSION, + "* LCD4Linux *", + "LCD4Linux", + "L4Linux", + NULL + }; + + char *line2[] = { "http://lcd4linux.bulix.org", + "lcd4linux.bulix.org", + NULL + }; + + + for (i = 0; line1[i]; i++) { + if (strlen(line1[i]) <= (unsigned) DCOLS) { + drv_generic_text_real_write(0, (DCOLS - strlen(line1[i])) / 2, line1[i], strlen(line1[i])); + flag = 1; + break; + } } - } - if (DROWS >= 2) { - for (i = 0; line2[i]; i++) { - if (strlen(line2[i]) <= (unsigned)DCOLS) { - drv_generic_text_real_write (1, (DCOLS - strlen(line2[i])) / 2, line2[i], strlen(line2[i])); - flag = 1; - break; - } + if (DROWS >= 2) { + for (i = 0; line2[i]; i++) { + if (strlen(line2[i]) <= (unsigned) DCOLS) { + drv_generic_text_real_write(1, (DCOLS - strlen(line2[i])) / 2, line2[i], strlen(line2[i])); + flag = 1; + break; + } + } } - } - - if (msg1 && DROWS >= 3) { - int len = strlen(msg1); - if ( len <= DCOLS) { - drv_generic_text_real_write (2, (DCOLS-len) / 2, msg1, len); - flag = 1; + + if (msg1 && DROWS >= 3) { + int len = strlen(msg1); + if (len <= DCOLS) { + drv_generic_text_real_write(2, (DCOLS - len) / 2, msg1, len); + flag = 1; + } } - } - - if (msg2 && DROWS >= 4) { - int len = strlen(msg2); - if ( len <= DCOLS) { - drv_generic_text_real_write (3, (DCOLS-len) / 2, msg2, len); - flag = 1; + + if (msg2 && DROWS >= 4) { + int len = strlen(msg2); + if (len <= DCOLS) { + drv_generic_text_real_write(3, (DCOLS - len) / 2, msg2, len); + flag = 1; + } } - } - return flag; + return flag; } -int drv_generic_text_draw (WIDGET *W) +int drv_generic_text_draw(WIDGET * W) { - WIDGET_TEXT *Text = W->data; - char *txt, *fb1, *fb2; - int row, col, col0, len, end; - - row = W->row; - col = W->col; - txt = Text->buffer; - len = strlen(txt); - end = col + len; - - /* maybe grow layout framebuffer */ - drv_generic_text_resizeFB (row+1, col+len); - - fb1 = LayoutFB + row*LCOLS; - fb2 = DisplayFB + row*DCOLS; - - /* transfer new text into layout buffer */ - memcpy (fb1 + col, txt, len); - - if (row<DROWS) { - for (; col <= end && col < DCOLS; col++) { - int pos1, pos2, equal; - if (fb1[col] == fb2[col]) continue; - col0 = col; - for (pos1 = col, pos2 = pos1, col++, equal = 0; col <= end && col < DCOLS; col++) { - if (fb1[col] == fb2[col]) { - /* If we find just one equal byte, we don't break, because this */ - /* would require a goto, which takes several bytes, too. */ - if (GOTO_COST >= 0 && ++equal > GOTO_COST) break; - } else { - pos2 = col; - equal = 0; + WIDGET_TEXT *Text = W->data; + char *txt, *fb1, *fb2; + int row, col, col0, len, end; + + row = W->row; + col = W->col; + txt = Text->buffer; + len = strlen(txt); + end = col + len; + + /* maybe grow layout framebuffer */ + drv_generic_text_resizeFB(row + 1, col + len); + + fb1 = LayoutFB + row * LCOLS; + fb2 = DisplayFB + row * DCOLS; + + /* transfer new text into layout buffer */ + memcpy(fb1 + col, txt, len); + + if (row < DROWS) { + for (; col <= end && col < DCOLS; col++) { + int pos1, pos2, equal; + if (fb1[col] == fb2[col]) + continue; + col0 = col; + for (pos1 = col, pos2 = pos1, col++, equal = 0; col <= end && col < DCOLS; col++) { + if (fb1[col] == fb2[col]) { + /* If we find just one equal byte, we don't break, because this */ + /* would require a goto, which takes several bytes, too. */ + if (GOTO_COST >= 0 && ++equal > GOTO_COST) + break; + } else { + pos2 = col; + equal = 0; + } + } + memcpy(fb2 + pos1, fb1 + pos1, pos2 - pos1 + 1); + drv_generic_text_real_write(row, col0, fb2 + pos1, pos2 - pos1 + 1); } - } - memcpy ( fb2+pos1, fb1+pos1, pos2-pos1+1); - drv_generic_text_real_write (row, col0, fb2+pos1, pos2-pos1+1); } - } - return 0; + return 0; } -int drv_generic_text_quit (void) { - - if (LayoutFB) { - free(LayoutFB); - LayoutFB = NULL; - } - - if (DisplayFB) { - free(DisplayFB); - DisplayFB = NULL; - } - - if (BarFB) { - free (BarFB); - BarFB = NULL; - } - widget_unregister(); - - return (0); +int drv_generic_text_quit(void) +{ + + if (LayoutFB) { + free(LayoutFB); + LayoutFB = NULL; + } + + if (DisplayFB) { + free(DisplayFB); + DisplayFB = NULL; + } + + if (BarFB) { + free(BarFB); + BarFB = NULL; + } + widget_unregister(); + + return (0); } @@ -480,70 +490,72 @@ int drv_generic_text_quit (void) { /*** generic icon handling ***/ /****************************************/ -int drv_generic_text_icon_init (void) +int drv_generic_text_icon_init(void) { - if (cfg_number(Section, "Icons", 0, 0, CHARS, &ICONS) < 0) return -1; - if (ICONS>0) { - info ("%s: reserving %d of %d user-defined characters for icons", Driver, ICONS, CHARS); - } - return 0; + if (cfg_number(Section, "Icons", 0, 0, CHARS, &ICONS) < 0) + return -1; + if (ICONS > 0) { + info("%s: reserving %d of %d user-defined characters for icons", Driver, ICONS, CHARS); + } + return 0; } -int drv_generic_text_icon_draw (WIDGET *W) +int drv_generic_text_icon_draw(WIDGET * W) { - static int icon_counter = 0; - WIDGET_ICON *Icon = W->data; - int row, col; - int l_idx, d_idx; - int invalidate = 0; - unsigned char ascii; - - row = W->row; - col = W->col; - - /* maybe grow layout framebuffer */ - drv_generic_text_resizeFB (row+1, col+1); - - /* icon deactivated? */ - if (Icon->ascii == -2) return 0; - - /* ASCII already assigned? */ - if (Icon->ascii == -1) { - if (icon_counter >= ICONS) { - error ("cannot process icon '%s': out of icons", W->name); - Icon->ascii = -2; - return -1; + static int icon_counter = 0; + WIDGET_ICON *Icon = W->data; + int row, col; + int l_idx, d_idx; + int invalidate = 0; + unsigned char ascii; + + row = W->row; + col = W->col; + + /* maybe grow layout framebuffer */ + drv_generic_text_resizeFB(row + 1, col + 1); + + /* icon deactivated? */ + if (Icon->ascii == -2) + return 0; + + /* ASCII already assigned? */ + if (Icon->ascii == -1) { + if (icon_counter >= ICONS) { + error("cannot process icon '%s': out of icons", W->name); + Icon->ascii = -2; + return -1; + } + icon_counter++; + Icon->ascii = CHAR0 + CHARS - icon_counter; + } + + /* maybe redefine icon */ + if (Icon->curmap != Icon->prvmap && Icon->visible) { + Icon->prvmap = Icon->curmap; + drv_generic_text_real_defchar(Icon->ascii, Icon->bitmap + YRES * Icon->curmap); + invalidate = INVALIDATE; } - icon_counter++; - Icon->ascii = CHAR0 + CHARS - icon_counter; - } - - /* maybe redefine icon */ - if (Icon->curmap != Icon->prvmap && Icon->visible) { - Icon->prvmap = Icon->curmap; - drv_generic_text_real_defchar(Icon->ascii, Icon->bitmap + YRES * Icon->curmap); - invalidate = INVALIDATE; - } - - /* use blank if invisible */ - ascii = Icon->visible ? Icon->ascii : ' '; - - /* index into the two framebuffers */ - l_idx = row * LCOLS + col; - d_idx = row * DCOLS + col; - - /* transfer icon into layout buffer */ - LayoutFB[l_idx] = ascii; - - /* maybe send icon to the display */ - if (row < DROWS && col < DCOLS && (DisplayFB[d_idx] != ascii || invalidate)) { - DisplayFB[d_idx] = ascii; - drv_generic_text_real_write (row, col, DisplayFB + d_idx, 1); - } - - return 0; - + + /* use blank if invisible */ + ascii = Icon->visible ? Icon->ascii : ' '; + + /* index into the two framebuffers */ + l_idx = row * LCOLS + col; + d_idx = row * DCOLS + col; + + /* transfer icon into layout buffer */ + LayoutFB[l_idx] = ascii; + + /* maybe send icon to the display */ + if (row < DROWS && col < DCOLS && (DisplayFB[d_idx] != ascii || invalidate)) { + DisplayFB[d_idx] = ascii; + drv_generic_text_real_write(row, col, DisplayFB + d_idx, 1); + } + + return 0; + } @@ -553,152 +565,154 @@ int drv_generic_text_icon_draw (WIDGET *W) static void drv_generic_text_bar_clear(void) { - int i; - - for (i = 0; i < LROWS*LCOLS; i++) { - BarFB[i].val1 = -1; - BarFB[i].val2 = -1; - BarFB[i].dir = 0; - BarFB[i].style = 0; - BarFB[i].segment = -1; - BarFB[i].invalid = 0; - } - - for (i = 0; i < nSegment; i++) { - Segment[i].used = 0; - } + int i; + + for (i = 0; i < LROWS * LCOLS; i++) { + BarFB[i].val1 = -1; + BarFB[i].val2 = -1; + BarFB[i].dir = 0; + BarFB[i].style = 0; + BarFB[i].segment = -1; + BarFB[i].invalid = 0; + } + + for (i = 0; i < nSegment; i++) { + Segment[i].used = 0; + } } -int drv_generic_text_bar_init (const int single_segments) +int drv_generic_text_bar_init(const int single_segments) { - if (BarFB) free (BarFB); - - if ((BarFB = malloc (LROWS * LCOLS * sizeof(BAR))) == NULL) { - error ("bar buffer allocation failed: out of memory"); - return -1; - } - - Single_Segments = single_segments; - - nSegment = 0; - fSegment = 0; - - drv_generic_text_bar_clear(); - - return 0; + if (BarFB) + free(BarFB); + + if ((BarFB = malloc(LROWS * LCOLS * sizeof(BAR))) == NULL) { + error("bar buffer allocation failed: out of memory"); + return -1; + } + + Single_Segments = single_segments; + + nSegment = 0; + fSegment = 0; + + drv_generic_text_bar_clear(); + + return 0; } void drv_generic_text_bar_add_segment(const int val1, const int val2, const DIRECTION dir, const int ascii) { - Segment[fSegment].val1 = val1; - Segment[fSegment].val2 = val2; - Segment[fSegment].dir = dir; - if (val1 == 0 && val2 == 0) - Segment[fSegment].style = 0; - else - Segment[fSegment].style = 255; - Segment[fSegment].used = 0; - Segment[fSegment].ascii = ascii; - - fSegment++; - nSegment = fSegment; + Segment[fSegment].val1 = val1; + Segment[fSegment].val2 = val2; + Segment[fSegment].dir = dir; + if (val1 == 0 && val2 == 0) + Segment[fSegment].style = 0; + else + Segment[fSegment].style = 255; + Segment[fSegment].used = 0; + Segment[fSegment].ascii = ascii; + + fSegment++; + nSegment = fSegment; } -static void drv_generic_text_bar_create_bar (int row, int col, const DIRECTION dir, STYLE style, int len, int val1, int val2) +static void drv_generic_text_bar_create_bar(int row, int col, const DIRECTION dir, STYLE style, int len, int val1, int val2) { - int rev = 0, max; - if (style) - BarFB[row * LCOLS + col].style = STYLE_FIRST; - - switch (dir) { - case DIR_WEST: - max = len * XRES; - val1 = max - val1; - val2 = max - val2; - rev = 1; - - case DIR_EAST: - while (len > 0 && col < LCOLS) { - BarFB[row*LCOLS+col].dir = dir; - BarFB[row*LCOLS+col].segment = -1; - if (style && BarFB[row * LCOLS + col].style == 0) - BarFB[row * LCOLS + col].style = STYLE_HOLLOW; - if (val1 >= XRES) { - BarFB[row*LCOLS+col].val1 = rev ? 0 : XRES; - val1 -= XRES; - } else { - BarFB[row*LCOLS+col].val1 = rev ? XRES-val1 : val1; - val1 = 0; - } - if (val2 >= XRES) { - BarFB[row*LCOLS+col].val2 = rev ? 0 : XRES; - val2 -= XRES; - } else { - BarFB[row*LCOLS+col].val2 = rev ? XRES-val2 : val2; - val2 = 0; - } - len--; - col++; - } + int rev = 0, max; if (style) - BarFB[row * LCOLS + col - 1].style = STYLE_LAST; - break; - - case DIR_SOUTH: - max = len * YRES; - val1 = max - val1; - val2 = max - val2; - rev = 1; - - case DIR_NORTH: - while (len > 0 && row < LROWS) { - BarFB[row*LCOLS+col].dir = dir; - BarFB[row*LCOLS+col].segment = -1; - if (val1 >= YRES) { - BarFB[row*LCOLS+col].val1 = rev ? 0 : YRES; - val1 -= YRES; - } else { - BarFB[row*LCOLS+col].val1 = rev ? YRES-val1 : val1; - val1 = 0; - } - if (val2 >= YRES) { - BarFB[row*LCOLS+col].val2 = rev ? 0 : YRES; - val2 -= YRES; - } else { - BarFB[row*LCOLS+col].val2 = rev ? YRES - val2 : val2; - val2 = 0; - } - len--; - row++; + BarFB[row * LCOLS + col].style = STYLE_FIRST; + + switch (dir) { + case DIR_WEST: + max = len * XRES; + val1 = max - val1; + val2 = max - val2; + rev = 1; + + case DIR_EAST: + while (len > 0 && col < LCOLS) { + BarFB[row * LCOLS + col].dir = dir; + BarFB[row * LCOLS + col].segment = -1; + if (style && BarFB[row * LCOLS + col].style == 0) + BarFB[row * LCOLS + col].style = STYLE_HOLLOW; + if (val1 >= XRES) { + BarFB[row * LCOLS + col].val1 = rev ? 0 : XRES; + val1 -= XRES; + } else { + BarFB[row * LCOLS + col].val1 = rev ? XRES - val1 : val1; + val1 = 0; + } + if (val2 >= XRES) { + BarFB[row * LCOLS + col].val2 = rev ? 0 : XRES; + val2 -= XRES; + } else { + BarFB[row * LCOLS + col].val2 = rev ? XRES - val2 : val2; + val2 = 0; + } + len--; + col++; + } + if (style) + BarFB[row * LCOLS + col - 1].style = STYLE_LAST; + break; + + case DIR_SOUTH: + max = len * YRES; + val1 = max - val1; + val2 = max - val2; + rev = 1; + + case DIR_NORTH: + while (len > 0 && row < LROWS) { + BarFB[row * LCOLS + col].dir = dir; + BarFB[row * LCOLS + col].segment = -1; + if (val1 >= YRES) { + BarFB[row * LCOLS + col].val1 = rev ? 0 : YRES; + val1 -= YRES; + } else { + BarFB[row * LCOLS + col].val1 = rev ? YRES - val1 : val1; + val1 = 0; + } + if (val2 >= YRES) { + BarFB[row * LCOLS + col].val2 = rev ? 0 : YRES; + val2 -= YRES; + } else { + BarFB[row * LCOLS + col].val2 = rev ? YRES - val2 : val2; + val2 = 0; + } + len--; + row++; + } + break; } - break; - } } -static void drv_generic_text_bar_create_segments (void) +static void drv_generic_text_bar_create_segments(void) { - int i, j, n; - int res, l1, l2; - - /* find first unused segment */ - for (i = fSegment; i < nSegment && Segment[i].used; i++); - - /* pack unused segments */ - for (j = i+1; j < nSegment; j++) { - if (Segment[j].used) - Segment[i++] = Segment[j]; - } - nSegment = i; - - /* create needed segments */ - for (n = 0; n < LROWS*LCOLS; n++) { - if (BarFB[n].dir == 0) continue; - res = BarFB[n].dir & (DIR_EAST|DIR_WEST) ? XRES:YRES; - for (i = 0; i < nSegment; i++) { + int i, j, n; + int res, l1, l2; + + /* find first unused segment */ + for (i = fSegment; i < nSegment && Segment[i].used; i++); + + /* pack unused segments */ + for (j = i + 1; j < nSegment; j++) { + if (Segment[j].used) + Segment[i++] = Segment[j]; + } + nSegment = i; + + /* create needed segments */ + for (n = 0; n < LROWS * LCOLS; n++) { + if (BarFB[n].dir == 0) + continue; + res = BarFB[n].dir & (DIR_EAST | DIR_WEST) ? XRES : YRES; + for (i = 0; i < nSegment; i++) { l1 = Segment[i].val1; if (l1 > res) l1 = res; @@ -725,141 +739,163 @@ static void drv_generic_text_bar_create_segments (void) break; /* hollow style, val(1,2) == 1, like '[' */ /* if (l1 == 1 && l2 == 1 && Segment[i].style == STYLE_FIRST && BarFB[n].style == STYLE_HOLLOW) - break; + break; *//* hollow style, val(1,2) == 1, like ']' */ /* if (l1 == 1 && l2 == 1 && Segment[i].style == STYLE_LAST && BarFB[n].style == STYLE_HOLLOW) break; */ } } - if (i == nSegment) { - nSegment++; - Segment[i].val1 = BarFB[n].val1; - Segment[i].val2 = BarFB[n].val2; - Segment[i].dir = BarFB[n].dir; - Segment[i].style = BarFB[n].style; - Segment[i].used = 0; - Segment[i].ascii = -1; + if (i == nSegment) { + nSegment++; + Segment[i].val1 = BarFB[n].val1; + Segment[i].val2 = BarFB[n].val2; + Segment[i].dir = BarFB[n].dir; + Segment[i].style = BarFB[n].style; + Segment[i].used = 0; + Segment[i].ascii = -1; + } + BarFB[n].segment = i; } - BarFB[n].segment = i; - } } -static int drv_generic_text_bar_segment_error (const int i, const int j) +static int drv_generic_text_bar_segment_error(const int i, const int j) { - int res; - int i1, i2, j1, j2; - - if (i == j) return 65535; - if (!(Segment[i].dir & Segment[j].dir)) return 65535; - if (Segment[i].style != Segment[j].style) return 65535; - - res = Segment[i].dir&(DIR_EAST|DIR_WEST) ? XRES:YRES; - - i1 = Segment[i].val1; if (i1 > res) i1 = res; - i2 = Segment[i].val2; if (i2 > res) i2 = res; - j1 = Segment[j].val1; if (j1 > res) j1 = res; - j2 = Segment[j].val2; if (j2 > res) j2 = res; - - if (i1 == 0 && j1 != 0) return 65535; - if (i2 == 0 && j2 != 0) return 65535; - if (i1 == res && j1 < res) return 65535; - if (i2 == res && j2 < res) return 65535; - if (i1 == 1 && j1 != 1 && i2 > 0) return 65535; - if (i2 == 1 && j2 != 1 && j1 > 0) return 65535; - if (i1 == i2 && j1 != j2) return 65535; - - return (i1-j1)*(i1-j1) + (i2-j2)*(i2-j2); + int res; + int i1, i2, j1, j2; + + if (i == j) + return 65535; + if (!(Segment[i].dir & Segment[j].dir)) + return 65535; + if (Segment[i].style != Segment[j].style) + return 65535; + + res = Segment[i].dir & (DIR_EAST | DIR_WEST) ? XRES : YRES; + + i1 = Segment[i].val1; + if (i1 > res) + i1 = res; + i2 = Segment[i].val2; + if (i2 > res) + i2 = res; + j1 = Segment[j].val1; + if (j1 > res) + j1 = res; + j2 = Segment[j].val2; + if (j2 > res) + j2 = res; + + if (i1 == 0 && j1 != 0) + return 65535; + if (i2 == 0 && j2 != 0) + return 65535; + if (i1 == res && j1 < res) + return 65535; + if (i2 == res && j2 < res) + return 65535; + if (i1 == 1 && j1 != 1 && i2 > 0) + return 65535; + if (i2 == 1 && j2 != 1 && j1 > 0) + return 65535; + if (i1 == i2 && j1 != j2) + return 65535; + + return (i1 - j1) * (i1 - j1) + (i2 - j2) * (i2 - j2); } -static void drv_generic_text_bar_pack_segments (void) +static void drv_generic_text_bar_pack_segments(void) { - int i, j, n, min; - int pack_i, pack_j; - int pass1 = 1; - int error[nSegment][nSegment]; - - if (nSegment <= fSegment + CHARS - ICONS) { - return; - } - - for (i = 0; i < nSegment; i++) { - for (j = 0; j < nSegment; j++) { - error[i][j] = drv_generic_text_bar_segment_error(i, j); + int i, j, n, min; + int pack_i, pack_j; + int pass1 = 1; + int error[nSegment][nSegment]; + + if (nSegment <= fSegment + CHARS - ICONS) { + return; } - } - - while (nSegment > fSegment + CHARS - ICONS) { - - min = 65535; - pack_i = -1; - pack_j = -1; - for (i = fSegment; i < nSegment; i++) { - if (pass1 && Segment[i].used) continue; - for (j = 0; j < nSegment; j++) { - if (error[i][j] < min) { - min = error[i][j]; - pack_i = i; - pack_j = j; + + for (i = 0; i < nSegment; i++) { + for (j = 0; j < nSegment; j++) { + error[i][j] = drv_generic_text_bar_segment_error(i, j); } - } } - if (pack_i == -1) { - if (pass1) { - pass1 = 0; - continue; - } else { - error ("unable to compact bar characters"); - error ("nSegment=%d fSegment=%d CHARS=%d ICONS=%d", nSegment, fSegment, CHARS, ICONS); - error ("Segment[0].val1=%d val2=%d", Segment[0].val1, Segment[0].val2); - error ("Segment[1].val1=%d val2=%d", Segment[1].val1, Segment[1].val2); - error ("Segment[2].val1=%d val2=%d", Segment[2].val1, Segment[2].val2); - nSegment = CHARS - ICONS; - break; - } - } + while (nSegment > fSegment + CHARS - ICONS) { + + min = 65535; + pack_i = -1; + pack_j = -1; + for (i = fSegment; i < nSegment; i++) { + if (pass1 && Segment[i].used) + continue; + for (j = 0; j < nSegment; j++) { + if (error[i][j] < min) { + min = error[i][j]; + pack_i = i; + pack_j = j; + } + } + } + if (pack_i == -1) { + if (pass1) { + pass1 = 0; + continue; + } else { + error("unable to compact bar characters"); + error("nSegment=%d fSegment=%d CHARS=%d ICONS=%d", nSegment, fSegment, CHARS, ICONS); + error("Segment[0].val1=%d val2=%d", Segment[0].val1, Segment[0].val2); + error("Segment[1].val1=%d val2=%d", Segment[1].val1, Segment[1].val2); + error("Segment[2].val1=%d val2=%d", Segment[2].val1, Segment[2].val2); + nSegment = CHARS - ICONS; + break; + } + } #if 0 - debug ("pack_segment: n=%d i=%d j=%d min=%d", nSegment, pack_i, pack_j, min); - debug ("Pack_segment: i1=%d i2=%d j1=%d j2=%d\n", - Segment[pack_i].val1, Segment[pack_i].val2, - Segment[pack_j].val1, Segment[pack_j].val2); + debug("pack_segment: n=%d i=%d j=%d min=%d", nSegment, pack_i, pack_j, min); + debug("Pack_segment: i1=%d i2=%d j1=%d j2=%d\n", Segment[pack_i].val1, Segment[pack_i].val2, Segment[pack_j].val1, Segment[pack_j].val2); #endif - - nSegment--; - Segment[pack_i] = Segment[nSegment]; - - for (i = 0; i < nSegment; i++) { - error[pack_i][i] = error[nSegment][i]; - error[i][pack_i] = error[i][nSegment]; - } - - for (n = 0; n < LROWS * LCOLS; n++) { - if (BarFB[n].segment == pack_i) BarFB[n].segment = pack_j; - if (BarFB[n].segment == nSegment) BarFB[n].segment = pack_i; + + nSegment--; + Segment[pack_i] = Segment[nSegment]; + + for (i = 0; i < nSegment; i++) { + error[pack_i][i] = error[nSegment][i]; + error[i][pack_i] = error[i][nSegment]; + } + + for (n = 0; n < LROWS * LCOLS; n++) { + if (BarFB[n].segment == pack_i) + BarFB[n].segment = pack_j; + if (BarFB[n].segment == nSegment) + BarFB[n].segment = pack_i; + } } - } } static void drv_generic_text_bar_define_chars(void) { - int c, i, j; - unsigned char buffer[8]; - - for (i = fSegment; i < nSegment; i++) { - if (Segment[i].used) continue; - if (Segment[i].ascii != -1) continue; - for (c = 0; c < CHARS - ICONS; c++) { - for (j = fSegment; j < nSegment; j++) { - if (Segment[j].ascii == c) break; - } - if (j == nSegment) break; - } - Segment[i].ascii = c; - switch (Segment[i].dir) { - case DIR_WEST: + int c, i, j; + unsigned char buffer[8]; + + for (i = fSegment; i < nSegment; i++) { + if (Segment[i].used) + continue; + if (Segment[i].ascii != -1) + continue; + for (c = 0; c < CHARS - ICONS; c++) { + for (j = fSegment; j < nSegment; j++) { + if (Segment[j].ascii == c) + break; + } + if (j == nSegment) + break; + } + Segment[i].ascii = c; + switch (Segment[i].dir) { + case DIR_WEST: if (Segment[i].style) { buffer[0] = 255; buffer[7] = 255; @@ -868,8 +904,8 @@ static void drv_generic_text_bar_define_chars(void) buffer[j] |= Segment[i].style & STYLE_FIRST ? 16 : 1; } } - break; - case DIR_EAST: + break; + case DIR_EAST: for (j = 0; j < 4; j++) { buffer[j] = 255 << (XRES - Segment[i].val1); buffer[j + 4] = 255 << (XRES - Segment[i].val2); @@ -882,130 +918,141 @@ static void drv_generic_text_bar_define_chars(void) buffer[j] |= Segment[i].style & STYLE_FIRST ? 16 : 1; } } - break; - case DIR_NORTH: - for (j = 0; j < Segment[i].val1; j++) { - buffer[7-j] = (1<<XRES)-1; - } - for (; j < YRES; j++) { - buffer[7-j] = 0; - } - break; - case DIR_SOUTH: - for (j = 0; j < Segment[i].val1; j++) { - buffer[j] = (1<<XRES)-1; - } - for (; j < YRES; j++) { - buffer[j] = 0; - } - break; - } - drv_generic_text_real_defchar(CHAR0+c, buffer); - - /* maybe invalidate framebuffer */ - if (INVALIDATE) { - for (j = 0; j < LROWS*LCOLS; j++) { - if (BarFB[j].segment == i) { - BarFB[j].invalid = 1; + break; + case DIR_NORTH: + for (j = 0; j < Segment[i].val1; j++) { + buffer[7 - j] = (1 << XRES) - 1; + } + for (; j < YRES; j++) { + buffer[7 - j] = 0; + } + break; + case DIR_SOUTH: + for (j = 0; j < Segment[i].val1; j++) { + buffer[j] = (1 << XRES) - 1; + } + for (; j < YRES; j++) { + buffer[j] = 0; + } + break; + } + drv_generic_text_real_defchar(CHAR0 + c, buffer); + + /* maybe invalidate framebuffer */ + if (INVALIDATE) { + for (j = 0; j < LROWS * LCOLS; j++) { + if (BarFB[j].segment == i) { + BarFB[j].invalid = 1; + } + } } - } } - } } -int drv_generic_text_bar_draw (WIDGET *W) +int drv_generic_text_bar_draw(WIDGET * W) { - WIDGET_BAR *Bar = W->data; - int row, col, col0, len, res, max, val1, val2; - int c, n, s; - DIRECTION dir; - STYLE style; - - row = W->row; - col = W->col; - dir = Bar->direction; - style = Bar->style; - len = Bar->length; - - /* maybe grow layout framebuffer */ - /* bars *always* grow heading North or East! */ - if (dir & (DIR_EAST|DIR_WEST)) { - drv_generic_text_resizeFB (row+1, col+len); - } else { - drv_generic_text_resizeFB (row+1, col+1); - } - - res = dir & (DIR_EAST|DIR_WEST) ? XRES : YRES; - max = len * res; - val1 = Bar->val1 * (double)(max); - val2 = Bar->val2 * (double)(max); - - if (val1 < 1) val1 = 1; - else if (val1 > max) val1 = max; - - if (val2 < 1) val2 = 1; - else if (val2 > max) val2 = max; - - if (Single_Segments) val2 = val1; - - /* create this bar */ - drv_generic_text_bar_create_bar (row, col, dir, style, len, val1, val2); - - /* process all bars */ - drv_generic_text_bar_create_segments (); - drv_generic_text_bar_pack_segments (); - drv_generic_text_bar_define_chars(); - - /* reset usage flags */ - for (s = 0; s < nSegment; s++) { - Segment[s].used = 0; - } - - /* set usage flags */ - for (n = 0; n < LROWS*LCOLS; n++) { - if ((s = BarFB[n].segment) != -1) Segment[s].used = 1; - } - - /* transfer bars into layout buffer */ - for (row = 0; row < LROWS; row++) { - for (col = 0; col < LCOLS; col++) { - n = row*LCOLS+col; - s = BarFB[n].segment; - if (s == -1) continue; - c = Segment[s].ascii; - if (c == -1) continue; - if (s >= fSegment) c += CHAR0; /* ascii offset for user-defineable chars */ - LayoutFB[n] = c; - /* maybe invalidate display framebuffer */ - if (BarFB[n].invalid) { - BarFB[n].invalid = 0; - DisplayFB[row*DCOLS+col] = ~LayoutFB[n]; - } + WIDGET_BAR *Bar = W->data; + int row, col, col0, len, res, max, val1, val2; + int c, n, s; + DIRECTION dir; + STYLE style; + + row = W->row; + col = W->col; + dir = Bar->direction; + style = Bar->style; + len = Bar->length; + + /* maybe grow layout framebuffer */ + /* bars *always* grow heading North or East! */ + if (dir & (DIR_EAST | DIR_WEST)) { + drv_generic_text_resizeFB(row + 1, col + len); + } else { + drv_generic_text_resizeFB(row + 1, col + 1); + } + + res = dir & (DIR_EAST | DIR_WEST) ? XRES : YRES; + max = len * res; + val1 = Bar->val1 * (double) (max); + val2 = Bar->val2 * (double) (max); + + if (val1 < 1) + val1 = 1; + else if (val1 > max) + val1 = max; + + if (val2 < 1) + val2 = 1; + else if (val2 > max) + val2 = max; + + if (Single_Segments) + val2 = val1; + + /* create this bar */ + drv_generic_text_bar_create_bar(row, col, dir, style, len, val1, val2); + + /* process all bars */ + drv_generic_text_bar_create_segments(); + drv_generic_text_bar_pack_segments(); + drv_generic_text_bar_define_chars(); + + /* reset usage flags */ + for (s = 0; s < nSegment; s++) { + Segment[s].used = 0; + } + + /* set usage flags */ + for (n = 0; n < LROWS * LCOLS; n++) { + if ((s = BarFB[n].segment) != -1) + Segment[s].used = 1; } - } - - /* transfer differences to the display */ - for (row = 0; row < DROWS; row++) { - for (col = 0; col < DCOLS; col++) { - int pos1, pos2, equal; - if (LayoutFB[row*LCOLS+col] == DisplayFB[row*DCOLS+col]) continue; - col0 = col; - for (pos1 = col, pos2 = pos1, col++, equal = 0; col < DCOLS; col++) { - if (LayoutFB[row*LCOLS+col] == DisplayFB[row*DCOLS+col]) { - /* If we find just one equal byte, we don't break, because this */ - /* would require a goto, which takes several bytes, too. */ - if (++equal > GOTO_COST) break; - } else { - pos2 = col; - equal = 0; + + /* transfer bars into layout buffer */ + for (row = 0; row < LROWS; row++) { + for (col = 0; col < LCOLS; col++) { + n = row * LCOLS + col; + s = BarFB[n].segment; + if (s == -1) + continue; + c = Segment[s].ascii; + if (c == -1) + continue; + if (s >= fSegment) + c += CHAR0; /* ascii offset for user-defineable chars */ + LayoutFB[n] = c; + /* maybe invalidate display framebuffer */ + if (BarFB[n].invalid) { + BarFB[n].invalid = 0; + DisplayFB[row * DCOLS + col] = ~LayoutFB[n]; + } } - } - memcpy ( DisplayFB+row*DCOLS+pos1, LayoutFB+row*LCOLS+pos1, pos2-pos1+1); - drv_generic_text_real_write (row, col0, DisplayFB+row*DCOLS+pos1, pos2-pos1+1); } - } - - return 0; + + /* transfer differences to the display */ + for (row = 0; row < DROWS; row++) { + for (col = 0; col < DCOLS; col++) { + int pos1, pos2, equal; + if (LayoutFB[row * LCOLS + col] == DisplayFB[row * DCOLS + col]) + continue; + col0 = col; + for (pos1 = col, pos2 = pos1, col++, equal = 0; col < DCOLS; col++) { + if (LayoutFB[row * LCOLS + col] == DisplayFB[row * DCOLS + col]) { + /* If we find just one equal byte, we don't break, because this */ + /* would require a goto, which takes several bytes, too. */ + if (++equal > GOTO_COST) + break; + } else { + pos2 = col; + equal = 0; + } + } + memcpy(DisplayFB + row * DCOLS + pos1, LayoutFB + row * LCOLS + pos1, pos2 - pos1 + 1); + drv_generic_text_real_write(row, col0, DisplayFB + row * DCOLS + pos1, pos2 - pos1 + 1); + } + } + + return 0; } diff --git a/drv_generic_text.h b/drv_generic_text.h index e468fc6..18b01db 100644 --- a/drv_generic_text.h +++ b/drv_generic_text.h @@ -1,4 +1,4 @@ -/* $Id: drv_generic_text.h,v 1.17 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: drv_generic_text.h,v 1.18 2005/05/08 04:32:44 reinelt Exp $ * * generic driver helper for text-based displays * @@ -23,6 +23,9 @@ * * * $Log: drv_generic_text.h,v $ + * Revision 1.18 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.17 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -103,28 +106,28 @@ #include "widget.h" -extern int DROWS, DCOLS; /* display size */ -extern int LROWS, LCOLS; /* layout size */ -extern int XRES, YRES; /* pixel width/height of one char */ -extern int CHARS, CHAR0; /* number of user-defineable characters, ASCII of first char */ -extern int ICONS; /* number of user-defineable characters reserved for icons */ -extern int GOTO_COST; /* number of bytes a goto command requires */ -extern int INVALIDATE; /* re-send a modified userdefined char? */ +extern int DROWS, DCOLS; /* display size */ +extern int LROWS, LCOLS; /* layout size */ +extern int XRES, YRES; /* pixel width/height of one char */ +extern int CHARS, CHAR0; /* number of user-defineable characters, ASCII of first char */ +extern int ICONS; /* number of user-defineable characters reserved for icons */ +extern int GOTO_COST; /* number of bytes a goto command requires */ +extern int INVALIDATE; /* re-send a modified userdefined char? */ /* these functions must be implemented by the real driver */ -void (*drv_generic_text_real_write)(const int row, const int col, const char *data, const int len); -void (*drv_generic_text_real_defchar)(const int ascii, const unsigned char *matrix); +void (*drv_generic_text_real_write) (const int row, const int col, const char *data, const int len); +void (*drv_generic_text_real_defchar) (const int ascii, const unsigned char *matrix); /* generic functions and widget callbacks */ -int drv_generic_text_init (const char *section, const char *driver); -int drv_generic_text_greet (const char *msg1, const char *msg2); -int drv_generic_text_draw (WIDGET *W); -int drv_generic_text_icon_init (void); -int drv_generic_text_icon_draw (WIDGET *W); -int drv_generic_text_bar_init (const int single_segments); -void drv_generic_text_bar_add_segment (const int val1, const int val2, const DIRECTION dir, const int ascii); -int drv_generic_text_bar_draw (WIDGET *W); -int drv_generic_text_quit (void); +int drv_generic_text_init(const char *section, const char *driver); +int drv_generic_text_greet(const char *msg1, const char *msg2); +int drv_generic_text_draw(WIDGET * W); +int drv_generic_text_icon_init(void); +int drv_generic_text_icon_draw(WIDGET * W); +int drv_generic_text_bar_init(const int single_segments); +void drv_generic_text_bar_add_segment(const int val1, const int val2, const DIRECTION dir, const int ascii); +int drv_generic_text_bar_draw(WIDGET * W); +int drv_generic_text_quit(void); #endif diff --git a/evaluator.c b/evaluator.c index a4e373a..abb3278 100644 --- a/evaluator.c +++ b/evaluator.c @@ -1,4 +1,4 @@ -/* $Id: evaluator.c,v 1.24 2005/03/30 04:57:50 reinelt Exp $ +/* $Id: evaluator.c,v 1.25 2005/05/08 04:32:44 reinelt Exp $ * * expression evaluation * @@ -23,6 +23,9 @@ * * * $Log: evaluator.c,v $ + * Revision 1.25 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.24 2005/03/30 04:57:50 reinelt * Evaluator speedup: use bsearch for finding functions and variables * @@ -192,68 +195,68 @@ #define CHUNK_SIZE 16 typedef enum { - T_UNDEF, - T_NAME, - T_NUMBER, - T_STRING, - T_OPERATOR, - T_VARIABLE, - T_FUNCTION + T_UNDEF, + T_NAME, + T_NUMBER, + T_STRING, + T_OPERATOR, + T_VARIABLE, + T_FUNCTION } TOKEN; typedef enum { - O_UNDEF, /* undefined */ - O_LST, /* expression lists */ - O_SET, /* variable assignements */ - O_CND, /* conditional a?b:c */ - O_COL, /* colon in a?b:c */ - O_OR, /* logical OR */ - O_AND, /* logical AND */ - O_EQ, /* equal */ - O_NE, /* not equal */ - O_LT, /* less than */ - O_LE, /* less or equal */ - O_GT, /* greater than */ - O_GE, /* greater or equal */ - O_ADD, /* addition */ - O_SUB, /* subtraction */ - O_SGN, /* sign '-' */ - O_CAT, /* string concatenation */ - O_MUL, /* multiplication */ - O_DIV, /* division */ - O_MOD, /* modulo */ - O_POW, /* power */ - O_NOT, /* logical NOT */ - O_BRO, /* open brace */ - O_COM, /* comma (argument seperator) */ - O_BRC /* closing brace */ + O_UNDEF, /* undefined */ + O_LST, /* expression lists */ + O_SET, /* variable assignements */ + O_CND, /* conditional a?b:c */ + O_COL, /* colon in a?b:c */ + O_OR, /* logical OR */ + O_AND, /* logical AND */ + O_EQ, /* equal */ + O_NE, /* not equal */ + O_LT, /* less than */ + O_LE, /* less or equal */ + O_GT, /* greater than */ + O_GE, /* greater or equal */ + O_ADD, /* addition */ + O_SUB, /* subtraction */ + O_SGN, /* sign '-' */ + O_CAT, /* string concatenation */ + O_MUL, /* multiplication */ + O_DIV, /* division */ + O_MOD, /* modulo */ + O_POW, /* power */ + O_NOT, /* logical NOT */ + O_BRO, /* open brace */ + O_COM, /* comma (argument seperator) */ + O_BRC /* closing brace */ } OPERATOR; typedef struct { - char *pattern; - int len; - OPERATOR op; + char *pattern; + int len; + OPERATOR op; } PATTERN; typedef struct { - char *name; - RESULT *value; + char *name; + RESULT *value; } VARIABLE; typedef struct { - char *name; - int argc; - void (*func)(); + char *name; + int argc; + void (*func) (); } FUNCTION; typedef struct _NODE { - TOKEN Token; - OPERATOR Operator; - RESULT *Result; - VARIABLE *Variable; - FUNCTION *Function; - int Children; - struct _NODE **Child; + TOKEN Token; + OPERATOR Operator; + RESULT *Result; + VARIABLE *Variable; + FUNCTION *Function; + int Children; + struct _NODE **Child; } NODE; @@ -261,324 +264,327 @@ typedef struct _NODE { /* operators */ /* IMPORTANT! list must be sorted by length! */ static PATTERN Pattern[] = { - { ";", 1, O_LST }, /* expression lists */ - { "=", 1, O_SET }, /* variable assignements */ - { "?", 1, O_CND }, /* conditional a?b:c */ - { ":", 1, O_COL }, /* colon a?b:c */ - { "|", 1, O_OR }, /* logical OR */ - { "&", 1, O_AND }, /* logical AND */ - { "<", 1, O_LT }, /* less than */ - { ">", 1, O_GT }, /* greater than */ - { "+", 1, O_ADD }, /* addition */ - { "-", 1, O_SUB }, /* subtraction or sign */ - { ".", 1, O_CAT }, /* string concatenation */ - { "*", 1, O_MUL }, /* multiplication */ - { "/", 1, O_DIV }, /* division */ - { "%", 1, O_MOD }, /* modulo */ - { "^", 1, O_POW }, /* power */ - { "!", 1, O_NOT }, /* logical NOT */ - { "(", 1, O_BRO }, /* open brace */ - { ",", 1, O_COM }, /* comma (argument seperator) */ - { ")", 1, O_BRC }, /* closing brace */ - { "==", 2, O_EQ }, /* equal */ - { "!=", 2, O_NE }, /* not equal */ - { "<=", 2, O_LE }, /* less or equal */ - { ">=", 2, O_GE } /* greater or equal */ + {";", 1, O_LST}, /* expression lists */ + {"=", 1, O_SET}, /* variable assignements */ + {"?", 1, O_CND}, /* conditional a?b:c */ + {":", 1, O_COL}, /* colon a?b:c */ + {"|", 1, O_OR}, /* logical OR */ + {"&", 1, O_AND}, /* logical AND */ + {"<", 1, O_LT}, /* less than */ + {">", 1, O_GT}, /* greater than */ + {"+", 1, O_ADD}, /* addition */ + {"-", 1, O_SUB}, /* subtraction or sign */ + {".", 1, O_CAT}, /* string concatenation */ + {"*", 1, O_MUL}, /* multiplication */ + {"/", 1, O_DIV}, /* division */ + {"%", 1, O_MOD}, /* modulo */ + {"^", 1, O_POW}, /* power */ + {"!", 1, O_NOT}, /* logical NOT */ + {"(", 1, O_BRO}, /* open brace */ + {",", 1, O_COM}, /* comma (argument seperator) */ + {")", 1, O_BRC}, /* closing brace */ + {"==", 2, O_EQ}, /* equal */ + {"!=", 2, O_NE}, /* not equal */ + {"<=", 2, O_LE}, /* less or equal */ + {">=", 2, O_GE} /* greater or equal */ }; -static char *Expression = NULL; -static char *ExprPtr = NULL; -static char *Word = NULL; -static TOKEN Token = T_UNDEF; +static char *Expression = NULL; +static char *ExprPtr = NULL; +static char *Word = NULL; +static TOKEN Token = T_UNDEF; static OPERATOR Operator = O_UNDEF; static VARIABLE *Variable = NULL; -static int nVariable = 0; +static int nVariable = 0; static FUNCTION *Function = NULL; -static int nFunction = 0; +static int nFunction = 0; -void DelResult (RESULT *result) +void DelResult(RESULT * result) { - result->type = 0; - result->number = 0.0; - result->length = -1; - if (result->string) { - free (result->string); - result->string = NULL; - } + result->type = 0; + result->number = 0.0; + result->length = -1; + if (result->string) { + free(result->string); + result->string = NULL; + } } -static void FreeResult (RESULT *result) +static void FreeResult(RESULT * result) { - if (result != NULL) { - DelResult(result); - free (result); - } + if (result != NULL) { + DelResult(result); + free(result); + } } -static RESULT* NewResult (void) +static RESULT *NewResult(void) { - RESULT *result = malloc(sizeof(RESULT)); - if (result == NULL) { - error ("Evaluator: cannot allocate result: out of memory!"); - return NULL; - } - result->type = 0; - result->number = 0.0; - result->length = -1; - result->string = NULL; - - return result; + RESULT *result = malloc(sizeof(RESULT)); + if (result == NULL) { + error("Evaluator: cannot allocate result: out of memory!"); + return NULL; + } + result->type = 0; + result->number = 0.0; + result->length = -1; + result->string = NULL; + + return result; } -static RESULT* DupResult (RESULT *result) +static RESULT *DupResult(RESULT * result) { - RESULT *result2; - - if ((result2 = NewResult()) == NULL) return NULL; - - result2->type = result->type; - result2->number = result->number; - - if (result->length >= 0) { - result2->length = result->length; - result2->string = malloc(result2->length); - strcpy(result2->string, result->string); - } else { - result2->length = -1; - result2->string = NULL; - } - - return result2; + RESULT *result2; + + if ((result2 = NewResult()) == NULL) + return NULL; + + result2->type = result->type; + result2->number = result->number; + + if (result->length >= 0) { + result2->length = result->length; + result2->string = malloc(result2->length); + strcpy(result2->string, result->string); + } else { + result2->length = -1; + result2->string = NULL; + } + + return result2; } -RESULT* SetResult (RESULT **result, const int type, const void *value) +RESULT *SetResult(RESULT ** result, const int type, const void *value) { - if (*result == NULL) { - if ((*result = NewResult()) == NULL) - return NULL; - } else if (type == R_NUMBER) { - DelResult(*result); - } - - if (type == R_NUMBER) { - (*result)->type = R_NUMBER; - (*result)->number = *(double*)value; - (*result)->length = -1; - (*result)->string = NULL; - } - - else if (type == R_STRING) { - int len = strlen((char*)value); - (*result)->type = R_STRING; - (*result)->number = 0.0; - if ((*result)->string == NULL || len > (*result)->length) { - /* buffer is either empty or too small */ - if ((*result)->string) free((*result)->string); - /* allocate memory in multiples of CHUNK_SIZE */ - /* note that length does not count the trailing \0 */ - (*result)->length = CHUNK_SIZE*(len/CHUNK_SIZE+1)-1; - (*result)->string = malloc((*result)->length+1); + if (*result == NULL) { + if ((*result = NewResult()) == NULL) + return NULL; + } else if (type == R_NUMBER) { + DelResult(*result); } - strcpy((*result)->string, value); - } else { - error ("Evaluator: internal error: invalid result type %d", type); - return NULL; - } - - return *result; + + if (type == R_NUMBER) { + (*result)->type = R_NUMBER; + (*result)->number = *(double *) value; + (*result)->length = -1; + (*result)->string = NULL; + } + + else if (type == R_STRING) { + int len = strlen((char *) value); + (*result)->type = R_STRING; + (*result)->number = 0.0; + if ((*result)->string == NULL || len > (*result)->length) { + /* buffer is either empty or too small */ + if ((*result)->string) + free((*result)->string); + /* allocate memory in multiples of CHUNK_SIZE */ + /* note that length does not count the trailing \0 */ + (*result)->length = CHUNK_SIZE * (len / CHUNK_SIZE + 1) - 1; + (*result)->string = malloc((*result)->length + 1); + } + strcpy((*result)->string, value); + } else { + error("Evaluator: internal error: invalid result type %d", type); + return NULL; + } + + return *result; } -double R2N (RESULT *result) +double R2N(RESULT * result) { - if (result == NULL) { - error ("Evaluator: internal error: NULL result"); + if (result == NULL) { + error("Evaluator: internal error: NULL result"); + return 0.0; + } + + if (result->type & R_NUMBER) { + return result->number; + } + + if (result->type & R_STRING) { + result->type |= R_NUMBER; + result->number = atof(result->string); + return result->number; + } + + error("Evaluator: internal error: invalid result type %d", result->type); return 0.0; - } - - if (result->type & R_NUMBER) { - return result->number; - } - - if (result->type & R_STRING) { - result->type |= R_NUMBER; - result->number = atof(result->string); - return result->number; - } - - error ("Evaluator: internal error: invalid result type %d", result->type); - return 0.0; } -char* R2S (RESULT *result) +char *R2S(RESULT * result) { - if (result==NULL) { - error ("Evaluator: internal error: NULL result"); + if (result == NULL) { + error("Evaluator: internal error: NULL result"); + return NULL; + } + + if (result->type & R_STRING) { + return result->string; + } + + if (result->type & R_NUMBER) { + result->type |= R_STRING; + if (result->string) + free(result->string); + result->length = CHUNK_SIZE - 1; + result->string = malloc(CHUNK_SIZE); + snprintf(result->string, CHUNK_SIZE, "%g", result->number); + return result->string; + } + + error("Evaluator: internal error: invalid result type %d", result->type); return NULL; - } - - if (result->type & R_STRING) { - return result->string; - } - - if (result->type & R_NUMBER) { - result->type |= R_STRING; - if (result->string) free(result->string); - result->length = CHUNK_SIZE-1; - result->string = malloc(CHUNK_SIZE); - snprintf(result->string, CHUNK_SIZE, "%g", result->number); - return result->string; - } - - error ("Evaluator: internal error: invalid result type %d", result->type); - return NULL; - + } /* bsearch compare function for variables */ -static int LookupVariable (const void *a, const void *b) +static int LookupVariable(const void *a, const void *b) { - char *key = (char*)a; - VARIABLE *var = (VARIABLE*)b; + char *key = (char *) a; + VARIABLE *var = (VARIABLE *) b; - return strcmp(key, var->name); + return strcmp(key, var->name); } /* qsort compare function for variables */ -static int SortVariable (const void *a, const void *b) +static int SortVariable(const void *a, const void *b) { - VARIABLE *va=(VARIABLE*)a; - VARIABLE *vb=(VARIABLE*)b; + VARIABLE *va = (VARIABLE *) a; + VARIABLE *vb = (VARIABLE *) b; - return strcmp(va->name, vb->name); + return strcmp(va->name, vb->name); } -static VARIABLE *FindVariable (const char *name) +static VARIABLE *FindVariable(const char *name) { - return bsearch(name, Variable, nVariable, sizeof(VARIABLE), LookupVariable); + return bsearch(name, Variable, nVariable, sizeof(VARIABLE), LookupVariable); } -int SetVariable (const char *name, RESULT *value) +int SetVariable(const char *name, RESULT * value) { - VARIABLE *V; - - V = FindVariable(name); - if (V != NULL) { - FreeResult (V->value); - V->value = DupResult(value); - return 1; - } - - nVariable++; - Variable = realloc(Variable, nVariable*sizeof(VARIABLE)); - Variable[nVariable-1].name = strdup(name); - Variable[nVariable-1].value = DupResult(value); - - qsort(Variable, nVariable, sizeof(VARIABLE), SortVariable); - - return 0; + VARIABLE *V; + + V = FindVariable(name); + if (V != NULL) { + FreeResult(V->value); + V->value = DupResult(value); + return 1; + } + + nVariable++; + Variable = realloc(Variable, nVariable * sizeof(VARIABLE)); + Variable[nVariable - 1].name = strdup(name); + Variable[nVariable - 1].value = DupResult(value); + + qsort(Variable, nVariable, sizeof(VARIABLE), SortVariable); + + return 0; } -int SetVariableNumeric (const char *name, const double value) +int SetVariableNumeric(const char *name, const double value) { - RESULT result = {0, 0, 0, NULL}; - RESULT *rp = &result; - - SetResult (&rp, R_NUMBER, &value); + RESULT result = { 0, 0, 0, NULL }; + RESULT *rp = &result; - return SetVariable (name, rp); + SetResult(&rp, R_NUMBER, &value); + + return SetVariable(name, rp); } -int SetVariableString (const char *name, const char *value) +int SetVariableString(const char *name, const char *value) { - RESULT result = {0, 0, 0, NULL}; - RESULT *rp = &result; + RESULT result = { 0, 0, 0, NULL }; + RESULT *rp = &result; - SetResult(&rp, R_STRING, value); + SetResult(&rp, R_STRING, value); - return SetVariable (name, rp); + return SetVariable(name, rp); } -void DeleteVariables(void) +void DeleteVariables(void) { - int i; - - for (i=0;i<nVariable;i++) { - free(Variable[i].name); - FreeResult(Variable[i].value); - } - free(Variable); - Variable = NULL; - nVariable = 0; + int i; + + for (i = 0; i < nVariable; i++) { + free(Variable[i].name); + FreeResult(Variable[i].value); + } + free(Variable); + Variable = NULL; + nVariable = 0; } /* bsearch compare function for functions */ -static int LookupFunction (const void *a, const void *b) +static int LookupFunction(const void *a, const void *b) { - char *n = (char*)a; - FUNCTION *f = (FUNCTION*)b; + char *n = (char *) a; + FUNCTION *f = (FUNCTION *) b; - return strcmp(n, f->name); + return strcmp(n, f->name); } /* qsort compare function for functions */ -static int SortFunction (const void *a, const void *b) +static int SortFunction(const void *a, const void *b) { - FUNCTION *fa=(FUNCTION*)a; - FUNCTION *fb=(FUNCTION*)b; + FUNCTION *fa = (FUNCTION *) a; + FUNCTION *fb = (FUNCTION *) b; - return strcmp(fa->name, fb->name); + return strcmp(fa->name, fb->name); } -static FUNCTION* FindFunction (const char *name) +static FUNCTION *FindFunction(const char *name) { - return bsearch(name, Function, nFunction, sizeof(FUNCTION), LookupFunction); + return bsearch(name, Function, nFunction, sizeof(FUNCTION), LookupFunction); } -int AddFunction (const char *name, const int argc, void (*func)()) +int AddFunction(const char *name, const int argc, void (*func) ()) { - nFunction++; - Function = realloc(Function, nFunction*sizeof(FUNCTION)); - Function[nFunction-1].name = strdup(name); - Function[nFunction-1].argc = argc; - Function[nFunction-1].func = func; - - qsort(Function, nFunction, sizeof(FUNCTION), SortFunction); - - return 0; + nFunction++; + Function = realloc(Function, nFunction * sizeof(FUNCTION)); + Function[nFunction - 1].name = strdup(name); + Function[nFunction - 1].argc = argc; + Function[nFunction - 1].func = func; + + qsort(Function, nFunction, sizeof(FUNCTION), SortFunction); + + return 0; } -void DeleteFunctions(void) +void DeleteFunctions(void) { - int i; - - for (i=0;i<nFunction;i++) { - free(Function[i].name); - } - free(Function); - Function=NULL; - nFunction=0; + int i; + + for (i = 0; i < nFunction; i++) { + free(Function[i].name); + } + free(Function); + Function = NULL; + nFunction = 0; } @@ -587,723 +593,736 @@ void DeleteFunctions(void) #define is_alpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z') || ((c) == '_')) #define is_alnum(c) (is_alpha(c) || is_digit(c)) -static void Parse (void) +static void Parse(void) { - Token = T_UNDEF; - Operator = O_UNDEF; + Token = T_UNDEF; + Operator = O_UNDEF; - if (Word) { - free (Word); - Word = NULL; - } - - /* NULL expression? */ - if (ExprPtr == NULL) { - Word = strdup(""); - return; - } - - /* skip leading whitespace */ - while (is_space(*ExprPtr)) ExprPtr++; - - /* names */ - if (is_alpha(*ExprPtr)) { - char *start = ExprPtr; - while (is_alnum(*ExprPtr)) ExprPtr++; - if (*ExprPtr==':' && *(ExprPtr+1)==':' && is_alpha(*(ExprPtr+2))) { - ExprPtr+=3; - while (is_alnum(*ExprPtr)) ExprPtr++; + if (Word) { + free(Word); + Word = NULL; + } + + /* NULL expression? */ + if (ExprPtr == NULL) { + Word = strdup(""); + return; } - Word = strndup(start, ExprPtr-start); - Token = T_NAME; - } - - /* numbers */ - else if (is_digit(*ExprPtr) || (*ExprPtr=='.' && is_digit(*(ExprPtr+1)))) { - char *start = ExprPtr; - while (is_digit(*ExprPtr)) ExprPtr++; - if (*ExprPtr=='.') { - ExprPtr++; - while (is_digit(*ExprPtr)) ExprPtr++; + + /* skip leading whitespace */ + while (is_space(*ExprPtr)) + ExprPtr++; + + /* names */ + if (is_alpha(*ExprPtr)) { + char *start = ExprPtr; + while (is_alnum(*ExprPtr)) + ExprPtr++; + if (*ExprPtr == ':' && *(ExprPtr + 1) == ':' && is_alpha(*(ExprPtr + 2))) { + ExprPtr += 3; + while (is_alnum(*ExprPtr)) + ExprPtr++; + } + Word = strndup(start, ExprPtr - start); + Token = T_NAME; } - Word = strndup(start, ExprPtr-start); - Token = T_NUMBER; - } - - /* strings */ - else if (*ExprPtr=='\'') { - char *start=++ExprPtr; - while (*ExprPtr!='\0' && *ExprPtr!='\'') ExprPtr++; - Word = strndup(start, ExprPtr-start); - Token = T_STRING; - if (*ExprPtr=='\'') ExprPtr++; - } - - /* operators */ - else { - int i; - for (i=sizeof(Pattern)/sizeof(Pattern[0])-1; i>=0; i--) { - int len=Pattern[i].len; - if (strncmp (ExprPtr, Pattern[i].pattern, Pattern[i].len)==0) { - Word = strndup(ExprPtr, len); - Token = T_OPERATOR; - Operator = Pattern[i].op; - ExprPtr += len; - break; - } + + /* numbers */ + else if (is_digit(*ExprPtr) || (*ExprPtr == '.' && is_digit(*(ExprPtr + 1)))) { + char *start = ExprPtr; + while (is_digit(*ExprPtr)) + ExprPtr++; + if (*ExprPtr == '.') { + ExprPtr++; + while (is_digit(*ExprPtr)) + ExprPtr++; + } + Word = strndup(start, ExprPtr - start); + Token = T_NUMBER; } - } - - /* syntax check */ - if (Token == T_UNDEF && *ExprPtr != '\0') { - error ("Evaluator: parse error in <%s>: garbage <%s>", Expression, ExprPtr); - } - - /* skip trailing whitespace */ - while (is_space(*ExprPtr)) ExprPtr++; - - /* empty token */ - if (Word==NULL) Word=strdup(""); + + /* strings */ + else if (*ExprPtr == '\'') { + char *start = ++ExprPtr; + while (*ExprPtr != '\0' && *ExprPtr != '\'') + ExprPtr++; + Word = strndup(start, ExprPtr - start); + Token = T_STRING; + if (*ExprPtr == '\'') + ExprPtr++; + } + + /* operators */ + else { + int i; + for (i = sizeof(Pattern) / sizeof(Pattern[0]) - 1; i >= 0; i--) { + int len = Pattern[i].len; + if (strncmp(ExprPtr, Pattern[i].pattern, Pattern[i].len) == 0) { + Word = strndup(ExprPtr, len); + Token = T_OPERATOR; + Operator = Pattern[i].op; + ExprPtr += len; + break; + } + } + } + + /* syntax check */ + if (Token == T_UNDEF && *ExprPtr != '\0') { + error("Evaluator: parse error in <%s>: garbage <%s>", Expression, ExprPtr); + } + + /* skip trailing whitespace */ + while (is_space(*ExprPtr)) + ExprPtr++; + + /* empty token */ + if (Word == NULL) + Word = strdup(""); } -static NODE* NewNode (NODE *Child) +static NODE *NewNode(NODE * Child) { - NODE *N; - - N=malloc(sizeof(NODE)); - if (N==NULL) return NULL; - - memset (N, 0, sizeof(NODE)); - N->Token = Token; - N->Operator = Operator; - - if (Child != NULL) { - N->Children = 1; - N->Child = malloc(sizeof(NODE*)); - N->Child[0] = Child; - } - - return N; - + NODE *N; + + N = malloc(sizeof(NODE)); + if (N == NULL) + return NULL; + + memset(N, 0, sizeof(NODE)); + N->Token = Token; + N->Operator = Operator; + + if (Child != NULL) { + N->Children = 1; + N->Child = malloc(sizeof(NODE *)); + N->Child[0] = Child; + } + + return N; + } -static NODE* JunkNode (void) +static NODE *JunkNode(void) { - NODE *Junk; - - Junk = NewNode(NULL); - Junk->Token = T_STRING; - SetResult (&Junk->Result, R_STRING, ""); - - return Junk; + NODE *Junk; + + Junk = NewNode(NULL); + Junk->Token = T_STRING; + SetResult(&Junk->Result, R_STRING, ""); + + return Junk; } -static void LinkNode (NODE *Root, NODE *Child) +static void LinkNode(NODE * Root, NODE * Child) { - - if (Child == NULL) return; - Root->Children++; - Root->Child = realloc (Root->Child, Root->Children*sizeof(NODE*)); - if (Root->Child==NULL) return; - Root->Child[Root->Children-1]=Child; + if (Child == NULL) + return; + + Root->Children++; + Root->Child = realloc(Root->Child, Root->Children * sizeof(NODE *)); + if (Root->Child == NULL) + return; + Root->Child[Root->Children - 1] = Child; } /* forward declaration */ -static NODE* Level01 (void); +static NODE *Level01(void); /* literal numbers, variables, functions */ -static NODE* Level12 (void) +static NODE *Level12(void) { - NODE *Root = NULL; - - if (Token == T_OPERATOR && Operator == O_BRO) { - Parse(); - Root = Level01(); - if (Token != T_OPERATOR || Operator != O_BRC) { - error ("Evaluator: unbalanced parentheses in <%s>", Expression); - LinkNode (Root, JunkNode()); - } - } - - else if (Token == T_NUMBER) { - double value = atof(Word); - Root = NewNode(NULL); - SetResult (&Root->Result, R_NUMBER, &value); - } - - else if (Token == T_STRING) { - Root = NewNode(NULL); - SetResult (&Root->Result, R_STRING, Word); - } - - else if (Token == T_NAME) { - - /* look-ahead for opening brace */ - if (*ExprPtr == '(') { - int argc=0; - Root = NewNode(NULL); - Root->Token = T_FUNCTION; - Root->Result = NewResult(); - Root->Function = FindFunction(Word); - if (Root->Function == NULL) { - error ("Evaluator: unknown function '%s' in <%s>", Word, Expression); - Root->Token = T_STRING; - SetResult (&Root->Result, R_STRING, ""); - } - - /* opening brace */ - Parse(); - do { - Parse(); /* read argument */ - if (Token == T_OPERATOR && Operator == O_BRC) { - break; - } - else if (Token == T_OPERATOR && Operator == O_COM) { - error ("Evaluator: empty argument in <%s>", Expression); - LinkNode (Root, JunkNode()); - } - else { - LinkNode (Root, Level01()); + NODE *Root = NULL; + + if (Token == T_OPERATOR && Operator == O_BRO) { + Parse(); + Root = Level01(); + if (Token != T_OPERATOR || Operator != O_BRC) { + error("Evaluator: unbalanced parentheses in <%s>", Expression); + LinkNode(Root, JunkNode()); } - argc++; - } while (Token == T_OPERATOR && Operator == O_COM); - - /* check for closing brace */ - if (Token != T_OPERATOR || Operator != O_BRC) { - error ("Evaluator: missing closing brace in <%s>", Expression); - } - - /* check number of arguments */ - if (Root->Function != NULL && Root->Function->argc >= 0 && Root->Function->argc != argc) { - error ("Evaluator: wrong number of arguments in <%s>", Expression); - while (argc < Root->Function->argc) { - LinkNode (Root, JunkNode()); - argc++; + } + + else if (Token == T_NUMBER) { + double value = atof(Word); + Root = NewNode(NULL); + SetResult(&Root->Result, R_NUMBER, &value); + } + + else if (Token == T_STRING) { + Root = NewNode(NULL); + SetResult(&Root->Result, R_STRING, Word); + } + + else if (Token == T_NAME) { + + /* look-ahead for opening brace */ + if (*ExprPtr == '(') { + int argc = 0; + Root = NewNode(NULL); + Root->Token = T_FUNCTION; + Root->Result = NewResult(); + Root->Function = FindFunction(Word); + if (Root->Function == NULL) { + error("Evaluator: unknown function '%s' in <%s>", Word, Expression); + Root->Token = T_STRING; + SetResult(&Root->Result, R_STRING, ""); + } + + /* opening brace */ + Parse(); + do { + Parse(); /* read argument */ + if (Token == T_OPERATOR && Operator == O_BRC) { + break; + } else if (Token == T_OPERATOR && Operator == O_COM) { + error("Evaluator: empty argument in <%s>", Expression); + LinkNode(Root, JunkNode()); + } else { + LinkNode(Root, Level01()); + } + argc++; + } while (Token == T_OPERATOR && Operator == O_COM); + + /* check for closing brace */ + if (Token != T_OPERATOR || Operator != O_BRC) { + error("Evaluator: missing closing brace in <%s>", Expression); + } + + /* check number of arguments */ + if (Root->Function != NULL && Root->Function->argc >= 0 && Root->Function->argc != argc) { + error("Evaluator: wrong number of arguments in <%s>", Expression); + while (argc < Root->Function->argc) { + LinkNode(Root, JunkNode()); + argc++; + } + } + + } else { + Root = NewNode(NULL); + Root->Token = T_VARIABLE; + Root->Result = NewResult(); + Root->Variable = FindVariable(Word); + if (Root->Variable == NULL) { + SetVariableString(Word, ""); + Root->Variable = FindVariable(Word); + } } - } - - } else { - Root = NewNode(NULL); - Root->Token = T_VARIABLE; - Root->Result = NewResult(); - Root->Variable = FindVariable(Word); - if (Root->Variable == NULL) { - SetVariableString (Word, ""); - Root->Variable = FindVariable(Word); - } } - } - - else { - error ("Evaluator: syntax error in <%s>: <%s>", Expression, Word); - Root = NewNode(NULL); - Root->Token = T_STRING; - SetResult (&Root->Result, R_STRING, ""); - } - - Parse(); - return Root; - + + else { + error("Evaluator: syntax error in <%s>: <%s>", Expression, Word); + Root = NewNode(NULL); + Root->Token = T_STRING; + SetResult(&Root->Result, R_STRING, ""); + } + + Parse(); + return Root; + } /* unary + or - signs or logical 'not' */ -static NODE* Level11 (void) +static NODE *Level11(void) { - NODE *Root; - TOKEN sign = T_UNDEF; - - if (Token == T_OPERATOR && (Operator == O_ADD || Operator == O_SUB || Operator == O_NOT)) { - sign = Operator; - if (sign == O_SUB) sign = O_SGN; - Parse(); - } - - Root = Level12(); - - if (sign == O_SGN || sign == O_NOT) { - Root = NewNode (Root); - Root->Token = T_OPERATOR; - Root->Operator = sign; - } - - return Root; + NODE *Root; + TOKEN sign = T_UNDEF; + + if (Token == T_OPERATOR && (Operator == O_ADD || Operator == O_SUB || Operator == O_NOT)) { + sign = Operator; + if (sign == O_SUB) + sign = O_SGN; + Parse(); + } + + Root = Level12(); + + if (sign == O_SGN || sign == O_NOT) { + Root = NewNode(Root); + Root->Token = T_OPERATOR; + Root->Operator = sign; + } + + return Root; } /* x^y */ -static NODE* Level10 (void) +static NODE *Level10(void) { - NODE *Root; + NODE *Root; - Root = Level11(); - - while (Token == T_OPERATOR && Operator == O_POW) { - Root = NewNode (Root); - Parse(); - LinkNode (Root, Level11()); - } - - return Root; + Root = Level11(); + + while (Token == T_OPERATOR && Operator == O_POW) { + Root = NewNode(Root); + Parse(); + LinkNode(Root, Level11()); + } + + return Root; } /* multiplication, division, modulo */ -static NODE* Level09 (void) +static NODE *Level09(void) { - NODE *Root; + NODE *Root; - Root = Level10(); - - while (Token == T_OPERATOR && (Operator == O_MUL || Operator == O_DIV || Operator == O_MOD)) { - Root = NewNode (Root); - Parse(); - LinkNode (Root, Level10()); - } - - return Root; + Root = Level10(); + + while (Token == T_OPERATOR && (Operator == O_MUL || Operator == O_DIV || Operator == O_MOD)) { + Root = NewNode(Root); + Parse(); + LinkNode(Root, Level10()); + } + + return Root; } /* addition, subtraction, string concatenation */ -static NODE* Level08 (void) +static NODE *Level08(void) { - NODE *Root; + NODE *Root; - Root = Level09(); - - while (Token == T_OPERATOR && (Operator == O_ADD || Operator == O_SUB || Operator == O_CAT)) { - Root = NewNode (Root); - Parse(); - LinkNode (Root, Level09()); - } - - return Root; + Root = Level09(); + + while (Token == T_OPERATOR && (Operator == O_ADD || Operator == O_SUB || Operator == O_CAT)) { + Root = NewNode(Root); + Parse(); + LinkNode(Root, Level09()); + } + + return Root; } /* relational operators */ -static NODE* Level07 (void) +static NODE *Level07(void) { - NODE *Root; + NODE *Root; - Root = Level08(); - - while (Token == T_OPERATOR && (Operator == O_GT || Operator == O_GE || Operator == O_LT || Operator == O_LE)) { - Root = NewNode (Root); - Parse(); - LinkNode (Root, Level08()); - } - - return Root; + Root = Level08(); + + while (Token == T_OPERATOR && (Operator == O_GT || Operator == O_GE || Operator == O_LT || Operator == O_LE)) { + Root = NewNode(Root); + Parse(); + LinkNode(Root, Level08()); + } + + return Root; } /* equal, not equal */ -static NODE* Level06 (void) +static NODE *Level06(void) { - NODE *Root; + NODE *Root; - Root = Level07(); - - while (Token == T_OPERATOR && (Operator == O_EQ || Operator == O_NE)) { - Root = NewNode (Root); - Parse(); - LinkNode (Root, Level07()); - } - - return Root; + Root = Level07(); + + while (Token == T_OPERATOR && (Operator == O_EQ || Operator == O_NE)) { + Root = NewNode(Root); + Parse(); + LinkNode(Root, Level07()); + } + + return Root; } /* logical 'and' */ -static NODE* Level05 (void) +static NODE *Level05(void) { - NODE *Root; + NODE *Root; - Root = Level06(); - - while (Token == T_OPERATOR && Operator == O_AND) { - Root = NewNode (Root); - Parse(); - LinkNode (Root, Level06()); - } - - return Root; + Root = Level06(); + + while (Token == T_OPERATOR && Operator == O_AND) { + Root = NewNode(Root); + Parse(); + LinkNode(Root, Level06()); + } + + return Root; } /* logical 'or' */ -static NODE* Level04 (void) +static NODE *Level04(void) { - NODE *Root; + NODE *Root; - Root = Level05(); - - while (Token == T_OPERATOR && Operator == O_OR) { - Root = NewNode (Root); - Parse(); - LinkNode (Root, Level05()); - } - - return Root; + Root = Level05(); + + while (Token == T_OPERATOR && Operator == O_OR) { + Root = NewNode(Root); + Parse(); + LinkNode(Root, Level05()); + } + + return Root; } /* conditional expression a?b:c */ -static NODE* Level03 (void) +static NODE *Level03(void) { - NODE *Root; - - Root = Level04(); - - if (Token == T_OPERATOR && Operator == O_CND) { - Root = NewNode (Root); - Parse(); - LinkNode (Root, Level04()); - if (Token == T_OPERATOR && Operator == O_COL) { - Parse(); - LinkNode (Root, Level04()); - } else { - error ("Evaluator: syntax error in <%s>: expecting ':' got '%s'", Expression, Word); - LinkNode (Root, JunkNode()); + NODE *Root; + + Root = Level04(); + + if (Token == T_OPERATOR && Operator == O_CND) { + Root = NewNode(Root); + Parse(); + LinkNode(Root, Level04()); + if (Token == T_OPERATOR && Operator == O_COL) { + Parse(); + LinkNode(Root, Level04()); + } else { + error("Evaluator: syntax error in <%s>: expecting ':' got '%s'", Expression, Word); + LinkNode(Root, JunkNode()); + } } - } - return Root; + return Root; } /* variable assignments */ -static NODE* Level02 (void) +static NODE *Level02(void) { - NODE *Root; - - /* we have to do a look-ahead if it's really an assignment */ - if ((Token == T_NAME) && (*ExprPtr == '=') && (*(ExprPtr+1) != '=')) { - char *name = strdup(Word); - VARIABLE *V = FindVariable (name); - if (V == NULL) { - SetVariableString (name, ""); - V = FindVariable (name); + NODE *Root; + + /* we have to do a look-ahead if it's really an assignment */ + if ((Token == T_NAME) && (*ExprPtr == '=') && (*(ExprPtr + 1) != '=')) { + char *name = strdup(Word); + VARIABLE *V = FindVariable(name); + if (V == NULL) { + SetVariableString(name, ""); + V = FindVariable(name); + } + Parse(); + Root = NewNode(NULL); + Root->Variable = V; + Parse(); + LinkNode(Root, Level03()); + free(name); + } else { + Root = Level03(); } - Parse(); - Root = NewNode (NULL); - Root->Variable = V; - Parse(); - LinkNode (Root, Level03()); - free (name); - } else { - Root = Level03(); - } - - return Root; + + return Root; } /* expression lists */ -static NODE* Level01 (void) +static NODE *Level01(void) { - NODE *Root; + NODE *Root; - Root = Level02(); - - while (Token == T_OPERATOR && Operator == O_LST) { - Root = NewNode (Root); - Parse(); - LinkNode (Root, Level02()); - } - - return Root; + Root = Level02(); + + while (Token == T_OPERATOR && Operator == O_LST) { + Root = NewNode(Root); + Parse(); + LinkNode(Root, Level02()); + } + + return Root; } -static int EvalTree (NODE *Root) +static int EvalTree(NODE * Root) { - int i; - int argc; - int type = -1; - double number = 0.0; - double dummy; - int freeme = 0; - char *string = NULL; - char *s1, *s2; - RESULT *param[10]; - - for (i = 0; i < Root->Children; i++) { - EvalTree (Root->Child[i]); - } - - switch (Root->Token) { - - case T_NUMBER: - case T_STRING: - /* Root->Result already contains the value */ - return 0; - - case T_VARIABLE: - DelResult (Root->Result); - Root->Result = DupResult (Root->Variable->value); - return 0; - - case T_FUNCTION: - DelResult (Root->Result); - /* prepare parameter list */ - argc = Root->Children; - if (argc>10) argc=10; - for (i = 0; i < argc; i++) { - param[i]=Root->Child[i]->Result; - } - if (Root->Function->argc < 0) { - /* Function with variable argument list: */ - /* pass number of arguments as first parameter */ - Root->Function->func(Root->Result, argc, ¶m); - } else { - Root->Function->func(Root->Result, - param[0], param[1], param[2], param[3], param[4], - param[5], param[6], param[7], param[8], param[9]); + int i; + int argc; + int type = -1; + double number = 0.0; + double dummy; + int freeme = 0; + char *string = NULL; + char *s1, *s2; + RESULT *param[10]; + + for (i = 0; i < Root->Children; i++) { + EvalTree(Root->Child[i]); } - return 0; - - case T_OPERATOR: - switch (Root->Operator) { - - case O_LST: /* expression list: result is last expression */ - i = Root->Children-1; - type = Root->Child[i]->Result->type; - number = Root->Child[i]->Result->number; - string = Root->Child[i]->Result->string; - break; - - case O_SET: /* variable assignment */ - DelResult(Root->Variable->value); - Root->Variable->value = DupResult (Root->Child[0]->Result); - type = Root->Child[0]->Result->type; - number = Root->Child[0]->Result->number; - string = Root->Child[0]->Result->string; - break; - - case O_CND: /* conditional expression */ - i = 1+(R2N(Root->Child[0]->Result) == 0.0); - type = Root->Child[i]->Result->type; - number = Root->Child[i]->Result->number; - string = Root->Child[i]->Result->string; - break; - - case O_OR: /* logical OR */ - type = R_NUMBER; - number = ((R2N(Root->Child[0]->Result) != 0.0) || (R2N(Root->Child[1]->Result) != 0.0)); - break; - - case O_AND: /* logical AND */ - type = R_NUMBER; - number = ((R2N(Root->Child[0]->Result) != 0.0) && (R2N(Root->Child[1]->Result) != 0.0)); - break; - - case O_EQ: /* numeric equal */ - type = R_NUMBER; - number = (R2N(Root->Child[0]->Result) == R2N(Root->Child[1]->Result)); - break; - - case O_NE: /* numeric not equal */ - type = R_NUMBER; - number = (R2N(Root->Child[0]->Result) != R2N(Root->Child[1]->Result)); - break; - - case O_LT: /* numeric less than */ - type = R_NUMBER; - number = (R2N(Root->Child[0]->Result) < R2N(Root->Child[1]->Result)); - break; - - case O_LE: /* numeric less equal */ - type = R_NUMBER; - number = (R2N(Root->Child[0]->Result) <= R2N(Root->Child[1]->Result)); - break; - - case O_GT: /* numeric greater than */ - type = R_NUMBER; - number = (R2N(Root->Child[0]->Result) > R2N(Root->Child[1]->Result)); - break; - - case O_GE: /* numeric greater equal */ - type = R_NUMBER; - number = (R2N(Root->Child[0]->Result) >= R2N(Root->Child[1]->Result)); - break; - - case O_ADD: /* addition */ - type = R_NUMBER; - number = R2N(Root->Child[0]->Result) + R2N(Root->Child[1]->Result); - break; - - case O_SUB: /* subtraction */ - type = R_NUMBER; - number = R2N(Root->Child[0]->Result) - R2N(Root->Child[1]->Result); - break; - - case O_SGN: /* sign */ - type = R_NUMBER; - number = -R2N(Root->Child[0]->Result); - break; - - case O_CAT: /* string concatenation */ - type = R_STRING; - s1 = R2S(Root->Child[0]->Result); - s2 = R2S(Root->Child[1]->Result); - string = malloc(strlen(s1)+strlen(s2)+1); - strcpy (string, s1); - strcat (string, s2); - freeme = 1; - break; - - case O_MUL: /* multiplication */ - type = R_NUMBER; - number = R2N(Root->Child[0]->Result) * R2N(Root->Child[1]->Result); - break; - - case O_DIV: /* division */ - type = R_NUMBER; - dummy = R2N(Root->Child[1]->Result); - if (dummy == 0) { - error ("Evaluator: warning: division by zero"); - number = 0.0; - } else { - number = R2N(Root->Child[0]->Result) / R2N(Root->Child[1]->Result); - } - break; - - case O_MOD: /* modulo */ - type = R_NUMBER; - dummy = R2N(Root->Child[1]->Result); - if (dummy == 0) { - error ("Evaluator: warning: division by zero"); - number = 0.0; - } else { - number = fmod(R2N(Root->Child[0]->Result), R2N(Root->Child[1]->Result)); - } - break; - - case O_POW: /* x^y */ - type = R_NUMBER; - number = pow(R2N(Root->Child[0]->Result), R2N(Root->Child[1]->Result)); - break; - - case O_NOT: /* logical NOT */ - type = R_NUMBER; - number = (R2N(Root->Child[0]->Result) == 0.0); - break; + + switch (Root->Token) { + + case T_NUMBER: + case T_STRING: + /* Root->Result already contains the value */ + return 0; + + case T_VARIABLE: + DelResult(Root->Result); + Root->Result = DupResult(Root->Variable->value); + return 0; + + case T_FUNCTION: + DelResult(Root->Result); + /* prepare parameter list */ + argc = Root->Children; + if (argc > 10) + argc = 10; + for (i = 0; i < argc; i++) { + param[i] = Root->Child[i]->Result; + } + if (Root->Function->argc < 0) { + /* Function with variable argument list: */ + /* pass number of arguments as first parameter */ + Root->Function->func(Root->Result, argc, ¶m); + } else { + Root->Function->func(Root->Result, param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7], param[8], param[9]); + } + return 0; + + case T_OPERATOR: + switch (Root->Operator) { + + case O_LST: /* expression list: result is last expression */ + i = Root->Children - 1; + type = Root->Child[i]->Result->type; + number = Root->Child[i]->Result->number; + string = Root->Child[i]->Result->string; + break; + + case O_SET: /* variable assignment */ + DelResult(Root->Variable->value); + Root->Variable->value = DupResult(Root->Child[0]->Result); + type = Root->Child[0]->Result->type; + number = Root->Child[0]->Result->number; + string = Root->Child[0]->Result->string; + break; + + case O_CND: /* conditional expression */ + i = 1 + (R2N(Root->Child[0]->Result) == 0.0); + type = Root->Child[i]->Result->type; + number = Root->Child[i]->Result->number; + string = Root->Child[i]->Result->string; + break; + + case O_OR: /* logical OR */ + type = R_NUMBER; + number = ((R2N(Root->Child[0]->Result) != 0.0) || (R2N(Root->Child[1]->Result) != 0.0)); + break; + + case O_AND: /* logical AND */ + type = R_NUMBER; + number = ((R2N(Root->Child[0]->Result) != 0.0) && (R2N(Root->Child[1]->Result) != 0.0)); + break; + + case O_EQ: /* numeric equal */ + type = R_NUMBER; + number = (R2N(Root->Child[0]->Result) == R2N(Root->Child[1]->Result)); + break; + + case O_NE: /* numeric not equal */ + type = R_NUMBER; + number = (R2N(Root->Child[0]->Result) != R2N(Root->Child[1]->Result)); + break; + + case O_LT: /* numeric less than */ + type = R_NUMBER; + number = (R2N(Root->Child[0]->Result) < R2N(Root->Child[1]->Result)); + break; + + case O_LE: /* numeric less equal */ + type = R_NUMBER; + number = (R2N(Root->Child[0]->Result) <= R2N(Root->Child[1]->Result)); + break; + + case O_GT: /* numeric greater than */ + type = R_NUMBER; + number = (R2N(Root->Child[0]->Result) > R2N(Root->Child[1]->Result)); + break; + + case O_GE: /* numeric greater equal */ + type = R_NUMBER; + number = (R2N(Root->Child[0]->Result) >= R2N(Root->Child[1]->Result)); + break; + + case O_ADD: /* addition */ + type = R_NUMBER; + number = R2N(Root->Child[0]->Result) + R2N(Root->Child[1]->Result); + break; + + case O_SUB: /* subtraction */ + type = R_NUMBER; + number = R2N(Root->Child[0]->Result) - R2N(Root->Child[1]->Result); + break; + + case O_SGN: /* sign */ + type = R_NUMBER; + number = -R2N(Root->Child[0]->Result); + break; + + case O_CAT: /* string concatenation */ + type = R_STRING; + s1 = R2S(Root->Child[0]->Result); + s2 = R2S(Root->Child[1]->Result); + string = malloc(strlen(s1) + strlen(s2) + 1); + strcpy(string, s1); + strcat(string, s2); + freeme = 1; + break; + + case O_MUL: /* multiplication */ + type = R_NUMBER; + number = R2N(Root->Child[0]->Result) * R2N(Root->Child[1]->Result); + break; + + case O_DIV: /* division */ + type = R_NUMBER; + dummy = R2N(Root->Child[1]->Result); + if (dummy == 0) { + error("Evaluator: warning: division by zero"); + number = 0.0; + } else { + number = R2N(Root->Child[0]->Result) / R2N(Root->Child[1]->Result); + } + break; + + case O_MOD: /* modulo */ + type = R_NUMBER; + dummy = R2N(Root->Child[1]->Result); + if (dummy == 0) { + error("Evaluator: warning: division by zero"); + number = 0.0; + } else { + number = fmod(R2N(Root->Child[0]->Result), R2N(Root->Child[1]->Result)); + } + break; + + case O_POW: /* x^y */ + type = R_NUMBER; + number = pow(R2N(Root->Child[0]->Result), R2N(Root->Child[1]->Result)); + break; + + case O_NOT: /* logical NOT */ + type = R_NUMBER; + number = (R2N(Root->Child[0]->Result) == 0.0); + break; + + default: + error("Evaluator: internal error: unhandled operator <%d>", Root->Operator); + SetResult(&Root->Result, R_STRING, ""); + return -1; + } + + if (type == R_NUMBER) { + SetResult(&Root->Result, R_NUMBER, &number); + return 0; + } + if (type == R_STRING) { + SetResult(&Root->Result, R_STRING, string); + if (freeme) + free(string); + return 0; + } + error("Evaluator: internal error: unhandled type <%d>", type); + SetResult(&Root->Result, R_STRING, ""); + return -1; default: - error ("Evaluator: internal error: unhandled operator <%d>", Root->Operator); - SetResult (&Root->Result, R_STRING, ""); - return -1; - } - - if (type==R_NUMBER) { - SetResult (&Root->Result, R_NUMBER, &number); - return 0; - } - if (type==R_STRING) { - SetResult (&Root->Result, R_STRING, string); - if (freeme) free (string); - return 0; + error("Evaluator: internal error: unhandled token <%d>", Root->Token); + SetResult(&Root->Result, R_STRING, ""); + return -1; + } - error ("Evaluator: internal error: unhandled type <%d>", type); - SetResult (&Root->Result, R_STRING, ""); - return -1; - - default: - error ("Evaluator: internal error: unhandled token <%d>", Root->Token); - SetResult (&Root->Result, R_STRING, ""); - return -1; - - } - - return 0; + + return 0; } -int Compile (const char* expression, void **tree) +int Compile(const char *expression, void **tree) { - NODE *Root; - - *tree = NULL; - - Expression = (char*) expression; - ExprPtr = Expression; - - Parse(); - if (*Word=='\0') { - /* error ("Evaluator: empty expression <%s>", Expression); */ - free (Word); - Word = NULL; - return -1; - } - - Root = Level01(); - - if (*Word!='\0') { - error ("Evaluator: syntax error in <%s>: garbage <%s>", Expression, Word); - free (Word); + NODE *Root; + + *tree = NULL; + + Expression = (char *) expression; + ExprPtr = Expression; + + Parse(); + if (*Word == '\0') { + /* error ("Evaluator: empty expression <%s>", Expression); */ + free(Word); + Word = NULL; + return -1; + } + + Root = Level01(); + + if (*Word != '\0') { + error("Evaluator: syntax error in <%s>: garbage <%s>", Expression, Word); + free(Word); + Word = NULL; + return -1; + } + + free(Word); Word = NULL; - return -1; - } - - free (Word); - Word = NULL; - - *(NODE**)tree = Root; - - return 0; + + *(NODE **) tree = Root; + + return 0; } -int Eval (void *tree, RESULT *result) +int Eval(void *tree, RESULT * result) { - int ret; - NODE *Tree = (NODE*)tree; + int ret; + NODE *Tree = (NODE *) tree; - DelResult (result); - - if (Tree==NULL) { - SetResult (&result, R_STRING, ""); - return 0; - } - - ret = EvalTree(Tree); - - result->type = Tree->Result->type; - result->number = Tree->Result->number; - result->length = Tree->Result->length; - if (result->length >= 0) { - result->string = malloc(result->length); - if (Tree->Result->string != NULL) - strcpy(result->string, Tree->Result->string); - else - result->string[0]='\0'; - } else { - result->string = NULL; - } - - return ret; + DelResult(result); + + if (Tree == NULL) { + SetResult(&result, R_STRING, ""); + return 0; + } + + ret = EvalTree(Tree); + + result->type = Tree->Result->type; + result->number = Tree->Result->number; + result->length = Tree->Result->length; + if (result->length >= 0) { + result->string = malloc(result->length); + if (Tree->Result->string != NULL) + strcpy(result->string, Tree->Result->string); + else + result->string[0] = '\0'; + } else { + result->string = NULL; + } + + return ret; } -void DelTree (void *tree) +void DelTree(void *tree) { - int i; - NODE *Tree = (NODE*)tree; - - if (Tree == NULL) return; - - for (i=0; i<Tree->Children; i++) { - DelTree (Tree->Child[i]); - } - - if (Tree->Result) FreeResult (Tree->Result); - free(Tree); + int i; + NODE *Tree = (NODE *) tree; + + if (Tree == NULL) + return; + + for (i = 0; i < Tree->Children; i++) { + DelTree(Tree->Child[i]); + } + + if (Tree->Result) + FreeResult(Tree->Result); + free(Tree); } diff --git a/evaluator.h b/evaluator.h index fa6eab0..6c3016c 100644 --- a/evaluator.h +++ b/evaluator.h @@ -1,4 +1,4 @@ -/* $Id: evaluator.h,v 1.10 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: evaluator.h,v 1.11 2005/05/08 04:32:44 reinelt Exp $ * * expression evaluation * @@ -23,6 +23,9 @@ * * * $Log: evaluator.h,v $ + * Revision 1.11 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.10 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -86,30 +89,30 @@ #define R_STRING 2 typedef struct { - int type; - double number; - int length; - char *string; + int type; + double number; + int length; + char *string; } RESULT; -int SetVariable (const char *name, RESULT *value); -int SetVariableNumeric (const char *name, const double value); -int SetVariableString (const char *name, const char *value); +int SetVariable(const char *name, RESULT * value); +int SetVariableNumeric(const char *name, const double value); +int SetVariableString(const char *name, const char *value); -int AddFunction (const char *name, const int argc, void (*func)()); +int AddFunction(const char *name, const int argc, void (*func) ()); -void DeleteVariables (void); -void DeleteFunctions (void); +void DeleteVariables(void); +void DeleteFunctions(void); -void DelResult (RESULT *result); -RESULT* SetResult (RESULT **result, const int type, const void *value); +void DelResult(RESULT * result); +RESULT *SetResult(RESULT ** result, const int type, const void *value); -double R2N (RESULT *result); -char* R2S (RESULT *result); +double R2N(RESULT * result); +char *R2S(RESULT * result); -int Compile (const char *expression, void **tree); -int Eval (void *tree, RESULT *result); -void DelTree (void *tree); +int Compile(const char *expression, void **tree); +int Eval(void *tree, RESULT * result); +void DelTree(void *tree); #endif @@ -1,4 +1,4 @@ -/* $Id: font_6x8.h,v 1.2 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: font_6x8.h,v 1.3 2005/05/08 04:32:44 reinelt Exp $ * * 6x8 font * @@ -23,6 +23,9 @@ * * * $Log: font_6x8.h,v $ + * Revision 1.3 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.2 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -67,782 +70,782 @@ #define _OOOO_ 0x1e #define _OOOOO 0x1f -unsigned char Font_6x8[256][8]={ - [0x20] { ______, - ______, - ______, - ______, - ______, - ______, - ______, - ______ }, - [0x21] { ___O__, - ___O__, - ___O__, - ___O__, - ______, - ______, - ___O__, - ______ }, - [0x22] { __O_O_, - __O_O_, - __O_O_, - ______, - ______, - ______, - ______, - ______ }, - [0x23] { __O_O_, - __O_O_, - _OOOOO, - __O_O_, - _OOOOO, - __O_O_, - __O_O_, - ______ }, - [0x24] { ___O__, - __OOOO, - _O_O__, - __OOO_, - ___O_O, - _OOOO_, - ___O__, - ______ }, - [0x25] { _OO___, - _OO__O, - ____O_, - ___O__, - __O___, - _O__OO, - ____OO, - ______ }, - [0x26] { __OO__, - _O__O_, - _O_O__, - __O___, - _O_O_O, - _O__O_, - __OO_O, - ______ }, - [0x27] { __OO__, - ___O__, - __O___, - ______, - ______, - ______, - ______, - ______ }, - [0x28] { ____O_, - ___O__, - __O___, - __O___, - __O___, - ___O__, - ____O_, - ______ }, - [0x29] { __O___, - ___O__, - ____O_, - ____O_, - ____O_, - ___O__, - __O___, - ______ }, - [0x2a] { ______, - ___O__, - _O_O_O, - __OOO_, - _O_O_O, - ___O__, - ______, - ______ }, - [0x2b] { ______, - ___O__, - ___O__, - _OOOOO, - ___O__, - ___O__, - ______, - ______ }, - [0x2c] { ______, - ______, - ______, - ______, - __OO__, - ___O__, - __O___, - ______ }, - [0x2d] { ______, - ______, - ______, - _OOOOO, - ______, - ______, - ______, - ______ }, - [0x2e] { ______, - ______, - ______, - ______, - ______, - __OO__, - __OO__, - ______ }, - [0x2f] { ______, - _____O, - ____O_, - ___O__, - __O___, - _O____, - ______, - ______ }, - [0x30] { __OOO_, - _O___O, - _O__OO, - _O_O_O, - _OO__O, - _O___O, - __OOO_, - ______ }, - [0x31] { ___O__, - __OO__, - ___O__, - ___O__, - ___O__, - ___O__, - __OOO_, - ______ }, - [0x32] { __OOO_, - _O___O, - _____O, - ____O_, - ___O__, - __O___, - _OOOOO, - ______ }, - [0x33] { _OOOOO, - ____O_, - ___O__, - ____O_, - _____O, - _O___O, - __OOO_, - ______ }, - [0x34] { ____O_, - ___OO_, - __O_O_, - _O__O_, - _OOOOO, - ____O_, - ____O_, - ______ }, - [0x35] { _OOOOO, - _O____, - _O____, - _OOOO_, - _____O, - _O___O, - __OOO_, - ______ }, - [0x36] { ___OO_, - __O___, - _O____, - _OOOO_, - _O___O, - _O___O, - __OOO_, - ______ }, - [0x37] { _OOOOO, - _____O, - ____O_, - ___O__, - __O___, - __O___, - __O___, - ______ }, - [0x38] { __OOO_, - _O___O, - _O___O, - __OOO_, - _O___O, - _O___O, - __OOO_, - ______ }, - [0x39] { __OOO_, - _O___O, - _O___O, - __OOOO, - _____O, - ____O_, - __OO__, - ______ }, - [0x3a] { ______, - __OO__, - __OO__, - ______, - __OO__, - __OO__, - ______, - ______ }, - [0x3b] { ______, - __OO__, - __OO__, - ______, - __OO__, - ___O__, - __O___, - ______ }, - [0x3c] { ____O_, - ___O__, - __O___, - _O____, - __O___, - ___O__, - ____O_, - ______ }, - [0x3d] { ______, - ______, - _OOOOO, - ______, - _OOOOO, - ______, - ______, - ______ }, - [0x3e] { _O____, - __O___, - ___O__, - ____O_, - ___O__, - __O___, - _O____, - ______ }, - [0x3f] { __OOO_, - _O___O, - _____O, - ____O_, - ___O__, - ______, - ___O__, - ______ }, - [0x40] { __OOO_, - _O___O, - _____O, - __OO_O, - _O_O_O, - _O_O_O, - __OOO_, - ______ }, - [0x41] { __OOO_, - _O___O, - _O___O, - _O___O, - _OOOOO, - _O___O, - _O___O, - ______ }, - [0x42] { _OOOO_, - _O___O, - _O___O, - _OOOO_, - _O___O, - _O___O, - _OOOO_, - ______ }, - [0x43] { __OOO_, - _O___O, - _O____, - _O____, - _O____, - _O___O, - __OOO_, - ______ }, - [0x44] { _OOO__, - _O__O_, - _O___O, - _O___O, - _O___O, - _O__O_, - _OOO__, - ______ }, - [0x45] { _OOOOO, - _O____, - _O____, - _OOOO_, - _O____, - _O____, - _OOOOO, - ______ }, - [0x46] { _OOOOO, - _O____, - _O____, - _OOOO_, - _O____, - _O____, - _O____, - ______ }, - [0x47] { __OOO_, - _O___O, - _O____, - _O_OOO, - _O___O, - _O___O, - __OOOO, - ______ }, - [0x48] { _O___O, - _O___O, - _O___O, - _OOOOO, - _O___O, - _O___O, - _O___O, - ______ }, - [0x49] { __OOO_, - ___O__, - ___O__, - ___O__, - ___O__, - ___O__, - __OOO_, - ______ }, - [0x4a] { ___OOO, - ____O_, - ____O_, - ____O_, - ____O_, - _O__O_, - __OO__, - ______ }, - [0x4b] { _O___O, - _O__O_, - _O_O__, - _OO___, - _O_O__, - _O__O_, - _O___O, - ______ }, - [0x4c] { _O____, - _O____, - _O____, - _O____, - _O____, - _O____, - _OOOOO, - ______ }, - [0x4d] { _O___O, - _OO_OO, - _O_O_O, - _O_O_O, - _O___O, - _O___O, - _O___O, - ______ }, - [0x4e] { _O___O, - _O___O, - _OO__O, - _O_O_O, - _O__OO, - _O___O, - _O___O, - ______ }, - [0x4f] { __OOO_, - _O___O, - _O___O, - _O___O, - _O___O, - _O___O, - __OOO_, - ______ }, - [0x50] { _OOOO_, - _O___O, - _O___O, - _OOOO_, - _O____, - _O____, - _O____, - ______ }, - [0x51] { __OOO_, - _O___O, - _O___O, - _O___O, - _O_O_O, - _O__O_, - __OO_O, - ______ }, - [0x52] { _OOOO_, - _O___O, - _O___O, - _OOOO_, - _O_O__, - _O__O_, - _O___O, - ______ }, - [0x53] { __OOOO, - _O____, - _O____, - __OOO_, - _____O, - _____O, - _OOOO_, - ______ }, - [0x54] { _OOOOO, - ___O__, - ___O__, - ___O__, - ___O__, - ___O__, - ___O__, - ______ }, - [0x55] { _O___O, - _O___O, - _O___O, - _O___O, - _O___O, - _O___O, - __OOO_, - ______ }, - [0x56] { _O___O, - _O___O, - _O___O, - _O___O, - _O___O, - __O_O_, - ___O__, - ______ }, - [0x57] { _O___O, - _O___O, - _O___O, - _O_O_O, - _O_O_O, - _O_O_O, - __O_O_, - ______ }, - [0x58] { _O___O, - _O___O, - __O_O_, - ___O__, - __O_O_, - _O___O, - _O___O, - ______ }, - [0x59] { _O___O, - _O___O, - _O___O, - __O_O_, - ___O__, - ___O__, - ___O__, - ______ }, - [0x5a] { _OOOOO, - _____O, - ____O_, - ___O__, - __O___, - _O____, - _OOOOO, - ______ }, - [0x5b] { __OOO_, - __O___, - __O___, - __O___, - __O___, - __O___, - __OOO_, - ______ }, - [0x5c] { _O___O, - __O_O_, - _OOOOO, - ___O__, - _OOOOO, - ___O__, - ___O__, - ______ }, - [0x5d] { __OOO_, - ____O_, - ____O_, - ____O_, - ____O_, - ____O_, - __OOO_, - ______ }, - [0x5e] { ___O__, - __O_O_, - _O___O, - ______, - ______, - ______, - ______, - ______ }, - [0x5f] { ______, - ______, - ______, - ______, - ______, - ______, - _OOOOO, - ______ }, - [0x60] { __O___, - ___O__, - ____O_, - ______, - ______, - ______, - ______, - ______ }, - [0x61] { ______, - ______, - __OOO_, - _____O, - __OOOO, - _O___O, - __OOOO, - ______ }, - [0x62] { _O____, - _O____, - _O____, - _O_OO_, - _OO__O, - _O___O, - _OOOO_, - ______ }, - [0x63] { ______, - ______, - __OOO_, - _O____, - _O____, - _O___O, - __OOO_, - ______ }, - [0x64] { _____O, - _____O, - _____O, - __OO_O, - _O__OO, - _O___O, - __OOOO, - ______ }, - [0x65] { ______, - ______, - __OOO_, - _O___O, - _OOOOO, - _O____, - __OOO_, - ______ }, - [0x66] { ___OO_, - __O__O, - __O___, - _OOO__, - __O___, - __O___, - __O___, - ______ }, - [0x67] { ______, - __OOOO, - _O___O, - _O___O, - __OOOO, - _____O, - __OOO_, - ______ }, - [0x68] { _O____, - _O____, - _O_OO_, - _OO__O, - _O___O, - _O___O, - _O___O, - ______ }, - [0x69] { ___O__, - ______, - __OO__, - ___O__, - ___O__, - ___O__, - __OOO_, - ______ }, - [0x6a] { ____O_, - ______, - ___OO_, - ____O_, - ____O_, - _O__O_, - __OO__, - ______ }, - [0x6b] { __O___, - __O___, - __O__O, - __O_O_, - __OO__, - __O_O_, - __O__O, - ______ }, - [0x6c] { __OO__, - ___O__, - ___O__, - ___O__, - ___O__, - ___O__, - __OOO_, - ______ }, - [0x6d] { ______, - ______, - _OO_O_, - _O_O_O, - _O_O_O, - _O___O, - _O___O, - ______ }, - [0x6e] { ______, - ______, - _OOOO_, - _O___O, - _O___O, - _O___O, - _O___O, - ______ }, - [0x6f] { ______, - ______, - __OOO_, - _O___O, - _O___O, - _O___O, - __OOO_, - ______ }, - [0x70] { ______, - ______, - _OOOO_, - _O___O, - _OOOO_, - _O____, - _O____, - ______ }, - [0x71] { ______, - ______, - __OO_O, - _O__OO, - __OOOO, - _____O, - _____O, - ______ }, - [0x72] { ______, - ______, - _O_OO_, - _OO__O, - _O____, - _O____, - _O____, - ______ }, - [0x73] { ______, - ______, - __OOO_, - _O____, - __OOO_, - _____O, - _OOOO_, - ______ }, - [0x74] { __O___, - _OOO__, - __O___, - __O___, - __O___, - __O__O, - ___OO_, - ______ }, - [0x75] { ______, - ______, - _O___O, - _O___O, - _O___O, - _O__OO, - __OO_O, - ______ }, - [0x76] { ______, - ______, - _O___O, - _O___O, - _O___O, - __O_O_, - ___O__, - ______ }, - [0x77] { ______, - ______, - _O___O, - _O___O, - _O___O, - _O_O_O, - __O_O_, - ______ }, - [0x78] { ______, - ______, - _O___O, - __O_O_, - ___O__, - __O_O_, - _O___O, - ______ }, - [0x79] { ______, - ______, - _O___O, - _O___O, - __OOOO, - _____O, - __OOO_, - ______ }, - [0x7a] { ______, - ______, - _OOOOO, - ____O_, - ___O__, - __O___, - _OOOOO, - ______ }, - [0x7b] { ____O_, - ___O__, - ___O__, - __O___, - ___O__, - ___O__, - ____O_, - ______ }, - [0x7c] { ___O__, - ___O__, - ___O__, - ___O__, - ___O__, - ___O__, - ___O__, - ______ }, - [0x7d] { __O___, - ___O__, - ___O__, - ____O_, - ___O__, - ___O__, - __O___, - ______ }, - [0x7e] { ______, - ___O__, - ____O_, - _OOOOO, - ____O_, - ___O__, - ______, - ______ }, +unsigned char Font_6x8[256][8] = { + [0x20] {______, + ______, + ______, + ______, + ______, + ______, + ______, + ______}, + [0x21] {___O__, + ___O__, + ___O__, + ___O__, + ______, + ______, + ___O__, + ______}, + [0x22] {__O_O_, + __O_O_, + __O_O_, + ______, + ______, + ______, + ______, + ______}, + [0x23] {__O_O_, + __O_O_, + _OOOOO, + __O_O_, + _OOOOO, + __O_O_, + __O_O_, + ______}, + [0x24] {___O__, + __OOOO, + _O_O__, + __OOO_, + ___O_O, + _OOOO_, + ___O__, + ______}, + [0x25] {_OO___, + _OO__O, + ____O_, + ___O__, + __O___, + _O__OO, + ____OO, + ______}, + [0x26] {__OO__, + _O__O_, + _O_O__, + __O___, + _O_O_O, + _O__O_, + __OO_O, + ______}, + [0x27] {__OO__, + ___O__, + __O___, + ______, + ______, + ______, + ______, + ______}, + [0x28] {____O_, + ___O__, + __O___, + __O___, + __O___, + ___O__, + ____O_, + ______}, + [0x29] {__O___, + ___O__, + ____O_, + ____O_, + ____O_, + ___O__, + __O___, + ______}, + [0x2a] {______, + ___O__, + _O_O_O, + __OOO_, + _O_O_O, + ___O__, + ______, + ______}, + [0x2b] {______, + ___O__, + ___O__, + _OOOOO, + ___O__, + ___O__, + ______, + ______}, + [0x2c] {______, + ______, + ______, + ______, + __OO__, + ___O__, + __O___, + ______}, + [0x2d] {______, + ______, + ______, + _OOOOO, + ______, + ______, + ______, + ______}, + [0x2e] {______, + ______, + ______, + ______, + ______, + __OO__, + __OO__, + ______}, + [0x2f] {______, + _____O, + ____O_, + ___O__, + __O___, + _O____, + ______, + ______}, + [0x30] {__OOO_, + _O___O, + _O__OO, + _O_O_O, + _OO__O, + _O___O, + __OOO_, + ______}, + [0x31] {___O__, + __OO__, + ___O__, + ___O__, + ___O__, + ___O__, + __OOO_, + ______}, + [0x32] {__OOO_, + _O___O, + _____O, + ____O_, + ___O__, + __O___, + _OOOOO, + ______}, + [0x33] {_OOOOO, + ____O_, + ___O__, + ____O_, + _____O, + _O___O, + __OOO_, + ______}, + [0x34] {____O_, + ___OO_, + __O_O_, + _O__O_, + _OOOOO, + ____O_, + ____O_, + ______}, + [0x35] {_OOOOO, + _O____, + _O____, + _OOOO_, + _____O, + _O___O, + __OOO_, + ______}, + [0x36] {___OO_, + __O___, + _O____, + _OOOO_, + _O___O, + _O___O, + __OOO_, + ______}, + [0x37] {_OOOOO, + _____O, + ____O_, + ___O__, + __O___, + __O___, + __O___, + ______}, + [0x38] {__OOO_, + _O___O, + _O___O, + __OOO_, + _O___O, + _O___O, + __OOO_, + ______}, + [0x39] {__OOO_, + _O___O, + _O___O, + __OOOO, + _____O, + ____O_, + __OO__, + ______}, + [0x3a] {______, + __OO__, + __OO__, + ______, + __OO__, + __OO__, + ______, + ______}, + [0x3b] {______, + __OO__, + __OO__, + ______, + __OO__, + ___O__, + __O___, + ______}, + [0x3c] {____O_, + ___O__, + __O___, + _O____, + __O___, + ___O__, + ____O_, + ______}, + [0x3d] {______, + ______, + _OOOOO, + ______, + _OOOOO, + ______, + ______, + ______}, + [0x3e] {_O____, + __O___, + ___O__, + ____O_, + ___O__, + __O___, + _O____, + ______}, + [0x3f] {__OOO_, + _O___O, + _____O, + ____O_, + ___O__, + ______, + ___O__, + ______}, + [0x40] {__OOO_, + _O___O, + _____O, + __OO_O, + _O_O_O, + _O_O_O, + __OOO_, + ______}, + [0x41] {__OOO_, + _O___O, + _O___O, + _O___O, + _OOOOO, + _O___O, + _O___O, + ______}, + [0x42] {_OOOO_, + _O___O, + _O___O, + _OOOO_, + _O___O, + _O___O, + _OOOO_, + ______}, + [0x43] {__OOO_, + _O___O, + _O____, + _O____, + _O____, + _O___O, + __OOO_, + ______}, + [0x44] {_OOO__, + _O__O_, + _O___O, + _O___O, + _O___O, + _O__O_, + _OOO__, + ______}, + [0x45] {_OOOOO, + _O____, + _O____, + _OOOO_, + _O____, + _O____, + _OOOOO, + ______}, + [0x46] {_OOOOO, + _O____, + _O____, + _OOOO_, + _O____, + _O____, + _O____, + ______}, + [0x47] {__OOO_, + _O___O, + _O____, + _O_OOO, + _O___O, + _O___O, + __OOOO, + ______}, + [0x48] {_O___O, + _O___O, + _O___O, + _OOOOO, + _O___O, + _O___O, + _O___O, + ______}, + [0x49] {__OOO_, + ___O__, + ___O__, + ___O__, + ___O__, + ___O__, + __OOO_, + ______}, + [0x4a] {___OOO, + ____O_, + ____O_, + ____O_, + ____O_, + _O__O_, + __OO__, + ______}, + [0x4b] {_O___O, + _O__O_, + _O_O__, + _OO___, + _O_O__, + _O__O_, + _O___O, + ______}, + [0x4c] {_O____, + _O____, + _O____, + _O____, + _O____, + _O____, + _OOOOO, + ______}, + [0x4d] {_O___O, + _OO_OO, + _O_O_O, + _O_O_O, + _O___O, + _O___O, + _O___O, + ______}, + [0x4e] {_O___O, + _O___O, + _OO__O, + _O_O_O, + _O__OO, + _O___O, + _O___O, + ______}, + [0x4f] {__OOO_, + _O___O, + _O___O, + _O___O, + _O___O, + _O___O, + __OOO_, + ______}, + [0x50] {_OOOO_, + _O___O, + _O___O, + _OOOO_, + _O____, + _O____, + _O____, + ______}, + [0x51] {__OOO_, + _O___O, + _O___O, + _O___O, + _O_O_O, + _O__O_, + __OO_O, + ______}, + [0x52] {_OOOO_, + _O___O, + _O___O, + _OOOO_, + _O_O__, + _O__O_, + _O___O, + ______}, + [0x53] {__OOOO, + _O____, + _O____, + __OOO_, + _____O, + _____O, + _OOOO_, + ______}, + [0x54] {_OOOOO, + ___O__, + ___O__, + ___O__, + ___O__, + ___O__, + ___O__, + ______}, + [0x55] {_O___O, + _O___O, + _O___O, + _O___O, + _O___O, + _O___O, + __OOO_, + ______}, + [0x56] {_O___O, + _O___O, + _O___O, + _O___O, + _O___O, + __O_O_, + ___O__, + ______}, + [0x57] {_O___O, + _O___O, + _O___O, + _O_O_O, + _O_O_O, + _O_O_O, + __O_O_, + ______}, + [0x58] {_O___O, + _O___O, + __O_O_, + ___O__, + __O_O_, + _O___O, + _O___O, + ______}, + [0x59] {_O___O, + _O___O, + _O___O, + __O_O_, + ___O__, + ___O__, + ___O__, + ______}, + [0x5a] {_OOOOO, + _____O, + ____O_, + ___O__, + __O___, + _O____, + _OOOOO, + ______}, + [0x5b] {__OOO_, + __O___, + __O___, + __O___, + __O___, + __O___, + __OOO_, + ______}, + [0x5c] {_O___O, + __O_O_, + _OOOOO, + ___O__, + _OOOOO, + ___O__, + ___O__, + ______}, + [0x5d] {__OOO_, + ____O_, + ____O_, + ____O_, + ____O_, + ____O_, + __OOO_, + ______}, + [0x5e] {___O__, + __O_O_, + _O___O, + ______, + ______, + ______, + ______, + ______}, + [0x5f] {______, + ______, + ______, + ______, + ______, + ______, + _OOOOO, + ______}, + [0x60] {__O___, + ___O__, + ____O_, + ______, + ______, + ______, + ______, + ______}, + [0x61] {______, + ______, + __OOO_, + _____O, + __OOOO, + _O___O, + __OOOO, + ______}, + [0x62] {_O____, + _O____, + _O____, + _O_OO_, + _OO__O, + _O___O, + _OOOO_, + ______}, + [0x63] {______, + ______, + __OOO_, + _O____, + _O____, + _O___O, + __OOO_, + ______}, + [0x64] {_____O, + _____O, + _____O, + __OO_O, + _O__OO, + _O___O, + __OOOO, + ______}, + [0x65] {______, + ______, + __OOO_, + _O___O, + _OOOOO, + _O____, + __OOO_, + ______}, + [0x66] {___OO_, + __O__O, + __O___, + _OOO__, + __O___, + __O___, + __O___, + ______}, + [0x67] {______, + __OOOO, + _O___O, + _O___O, + __OOOO, + _____O, + __OOO_, + ______}, + [0x68] {_O____, + _O____, + _O_OO_, + _OO__O, + _O___O, + _O___O, + _O___O, + ______}, + [0x69] {___O__, + ______, + __OO__, + ___O__, + ___O__, + ___O__, + __OOO_, + ______}, + [0x6a] {____O_, + ______, + ___OO_, + ____O_, + ____O_, + _O__O_, + __OO__, + ______}, + [0x6b] {__O___, + __O___, + __O__O, + __O_O_, + __OO__, + __O_O_, + __O__O, + ______}, + [0x6c] {__OO__, + ___O__, + ___O__, + ___O__, + ___O__, + ___O__, + __OOO_, + ______}, + [0x6d] {______, + ______, + _OO_O_, + _O_O_O, + _O_O_O, + _O___O, + _O___O, + ______}, + [0x6e] {______, + ______, + _OOOO_, + _O___O, + _O___O, + _O___O, + _O___O, + ______}, + [0x6f] {______, + ______, + __OOO_, + _O___O, + _O___O, + _O___O, + __OOO_, + ______}, + [0x70] {______, + ______, + _OOOO_, + _O___O, + _OOOO_, + _O____, + _O____, + ______}, + [0x71] {______, + ______, + __OO_O, + _O__OO, + __OOOO, + _____O, + _____O, + ______}, + [0x72] {______, + ______, + _O_OO_, + _OO__O, + _O____, + _O____, + _O____, + ______}, + [0x73] {______, + ______, + __OOO_, + _O____, + __OOO_, + _____O, + _OOOO_, + ______}, + [0x74] {__O___, + _OOO__, + __O___, + __O___, + __O___, + __O__O, + ___OO_, + ______}, + [0x75] {______, + ______, + _O___O, + _O___O, + _O___O, + _O__OO, + __OO_O, + ______}, + [0x76] {______, + ______, + _O___O, + _O___O, + _O___O, + __O_O_, + ___O__, + ______}, + [0x77] {______, + ______, + _O___O, + _O___O, + _O___O, + _O_O_O, + __O_O_, + ______}, + [0x78] {______, + ______, + _O___O, + __O_O_, + ___O__, + __O_O_, + _O___O, + ______}, + [0x79] {______, + ______, + _O___O, + _O___O, + __OOOO, + _____O, + __OOO_, + ______}, + [0x7a] {______, + ______, + _OOOOO, + ____O_, + ___O__, + __O___, + _OOOOO, + ______}, + [0x7b] {____O_, + ___O__, + ___O__, + __O___, + ___O__, + ___O__, + ____O_, + ______}, + [0x7c] {___O__, + ___O__, + ___O__, + ___O__, + ___O__, + ___O__, + ___O__, + ______}, + [0x7d] {__O___, + ___O__, + ___O__, + ____O_, + ___O__, + ___O__, + __O___, + ______}, + [0x7e] {______, + ___O__, + ____O_, + _OOOOO, + ____O_, + ___O__, + ______, + ______}, - [0x7f] { ______, - ___O__, - __O___, - _OOOOO, - __O___, - ___O__, - ______, - ______ }, - [0xb0] { __OOO_, - __O_O_, - __OOO_, - ______, - ______, - ______, - ______, - ______ }, + [0x7f] {______, + ___O__, + __O___, + _OOOOO, + __O___, + ___O__, + ______, + ______}, + [0xb0] {__OOO_, + __O_O_, + __OOO_, + ______, + ______, + ______, + ______, + ______}, }; @@ -1,4 +1,4 @@ -/* $Id: hash.c,v 1.25 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: hash.c,v 1.26 2005/05/08 04:32:44 reinelt Exp $ * * hashes (associative arrays) * @@ -23,6 +23,9 @@ * * * $Log: hash.c,v $ + * Revision 1.26 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.25 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -184,94 +187,99 @@ /* initialize a new hash table */ -void hash_create (HASH *Hash) +void hash_create(HASH * Hash) { - Hash->sorted = 0; + Hash->sorted = 0; + + Hash->timestamp.tv_sec = 0; + Hash->timestamp.tv_usec = 0; - Hash->timestamp.tv_sec = 0; - Hash->timestamp.tv_usec = 0; - - Hash->nItems = 0; - Hash->Items = NULL; + Hash->nItems = 0; + Hash->Items = NULL; - Hash->nColumns = 0; - Hash->Columns = NULL; + Hash->nColumns = 0; + Hash->Columns = NULL; - Hash->delimiter = strdup (" \t\n"); + Hash->delimiter = strdup(" \t\n"); } /* bsearch compare function for hash items */ -static int hash_lookup_item (const void *a, const void *b) +static int hash_lookup_item(const void *a, const void *b) { - char *key = (char*)a; - HASH_ITEM *item = (HASH_ITEM*)b; + char *key = (char *) a; + HASH_ITEM *item = (HASH_ITEM *) b; - return strcasecmp(key, item->key); + return strcasecmp(key, item->key); } /* qsort compare function for hash items */ -static int hash_sort_item (const void *a, const void *b) +static int hash_sort_item(const void *a, const void *b) { - HASH_ITEM *ha = (HASH_ITEM*)a; - HASH_ITEM *hb = (HASH_ITEM*)b; + HASH_ITEM *ha = (HASH_ITEM *) a; + HASH_ITEM *hb = (HASH_ITEM *) b; - return strcasecmp(ha->key, hb->key); + return strcasecmp(ha->key, hb->key); } /* bsearch compare function for hash headers */ -static int hash_lookup_column (const void *a, const void *b) +static int hash_lookup_column(const void *a, const void *b) { - char *key = (char*)a; - HASH_COLUMN *column = (HASH_COLUMN*)b; + char *key = (char *) a; + HASH_COLUMN *column = (HASH_COLUMN *) b; - return strcasecmp(key, column->key); + return strcasecmp(key, column->key); } /* qsort compare function for hash headers */ -static int hash_sort_column (const void *a, const void *b) +static int hash_sort_column(const void *a, const void *b) { - HASH_COLUMN *ha = (HASH_COLUMN*)a; - HASH_COLUMN *hb = (HASH_COLUMN*)b; + HASH_COLUMN *ha = (HASH_COLUMN *) a; + HASH_COLUMN *hb = (HASH_COLUMN *) b; - return strcasecmp(ha->key, hb->key); + return strcasecmp(ha->key, hb->key); } /* split a value into columns and */ /* return the nth column in a string */ /* WARNING: does return a pointer to a static string!! */ -static char* split (const char *val, const int column, const char *delimiter) +static char *split(const char *val, const int column, const char *delimiter) { - static char buffer[256]; - int num; - size_t len; - const char *beg, *end; - - if (column < 0) return (char*)val; - if (val == NULL) return NULL; - - num = 0; - len = 0; - beg = val; - end = beg; - while (beg && *beg) { - while (strchr(delimiter, *beg)) beg++; - end = strpbrk (beg, delimiter); - if (num++ == column) break; - beg = end ? end+1 : NULL; - } - if (beg != NULL) { - len = end ? (size_t)(end - beg) : strlen(beg); - if (len >= sizeof(buffer)) len = sizeof(buffer)-1; - strncpy (buffer, beg, len); - } - - buffer[len] = '\0'; - return buffer; + static char buffer[256]; + int num; + size_t len; + const char *beg, *end; + + if (column < 0) + return (char *) val; + if (val == NULL) + return NULL; + + num = 0; + len = 0; + beg = val; + end = beg; + while (beg && *beg) { + while (strchr(delimiter, *beg)) + beg++; + end = strpbrk(beg, delimiter); + if (num++ == column) + break; + beg = end ? end + 1 : NULL; + } + if (beg != NULL) { + len = end ? (size_t) (end - beg) : strlen(beg); + if (len >= sizeof(buffer)) + len = sizeof(buffer) - 1; + strncpy(buffer, beg, len); + } + + buffer[len] = '\0'; + return buffer; } @@ -279,203 +287,216 @@ static char* split (const char *val, const int column, const char *delimiter) /* If the table is flagged "sorted", the entry is looked */ /* up using the bsearch function. If the table is */ /* unsorted, it will be searched in a linear way */ -static HASH_ITEM* hash_lookup (HASH *Hash, const char *key, const int do_sort) +static HASH_ITEM *hash_lookup(HASH * Hash, const char *key, const int do_sort) { - HASH_ITEM *Item = NULL; - - /* maybe sort the array */ - if (do_sort && !Hash->sorted) { - qsort(Hash->Items, Hash->nItems, sizeof(HASH_ITEM), hash_sort_item); - Hash->sorted = 1; - } - - /* no key was passed */ - if (key == NULL) return NULL; - - /* lookup using bsearch */ - if (Hash->sorted) { - Item = bsearch(key, Hash->Items, Hash->nItems, sizeof(HASH_ITEM), hash_lookup_item); - } - - /* linear search */ - if (Item == NULL) { - int i; - for (i = 0; i < Hash->nItems; i++) { - if (strcmp(key, Hash->Items[i].key) == 0) { - Item = &(Hash->Items[i]); - break; - } + HASH_ITEM *Item = NULL; + + /* maybe sort the array */ + if (do_sort && !Hash->sorted) { + qsort(Hash->Items, Hash->nItems, sizeof(HASH_ITEM), hash_sort_item); + Hash->sorted = 1; } - } - - return Item; - + + /* no key was passed */ + if (key == NULL) + return NULL; + + /* lookup using bsearch */ + if (Hash->sorted) { + Item = bsearch(key, Hash->Items, Hash->nItems, sizeof(HASH_ITEM), hash_lookup_item); + } + + /* linear search */ + if (Item == NULL) { + int i; + for (i = 0; i < Hash->nItems; i++) { + if (strcmp(key, Hash->Items[i].key) == 0) { + Item = &(Hash->Items[i]); + break; + } + } + } + + return Item; + } /* return the age in milliseconds of an entry from the hash table */ /* or from the hash table itself if key is NULL */ /* returns -1 if entry does not exist */ -int hash_age (HASH *Hash, const char *key) +int hash_age(HASH * Hash, const char *key) { - HASH_ITEM *Item; - struct timeval now, *timestamp; - - if (key == NULL) { - timestamp = &(Hash->timestamp); - } else { - Item = hash_lookup(Hash, key, 1); - if (Item == NULL) return -1; - timestamp = &(Item->Slot[Item->index].timestamp); - } - - gettimeofday(&now, NULL); - - return (now.tv_sec - timestamp->tv_sec)*1000 + (now.tv_usec - timestamp->tv_usec)/1000; + HASH_ITEM *Item; + struct timeval now, *timestamp; + + if (key == NULL) { + timestamp = &(Hash->timestamp); + } else { + Item = hash_lookup(Hash, key, 1); + if (Item == NULL) + return -1; + timestamp = &(Item->Slot[Item->index].timestamp); + } + + gettimeofday(&now, NULL); + + return (now.tv_sec - timestamp->tv_sec) * 1000 + (now.tv_usec - timestamp->tv_usec) / 1000; } /* add an entry to the column header table */ -void hash_set_column (HASH *Hash, const int number, const char *column) +void hash_set_column(HASH * Hash, const int number, const char *column) { - if (Hash == NULL) return; - - Hash->nColumns++; - Hash->Columns = realloc(Hash->Columns, Hash->nColumns * sizeof(HASH_COLUMN)); - Hash->Columns[Hash->nColumns-1].key = strdup(column); - Hash->Columns[Hash->nColumns-1].val = number; - - qsort(Hash->Columns, Hash->nColumns, sizeof(HASH_COLUMN), hash_sort_column); - + if (Hash == NULL) + return; + + Hash->nColumns++; + Hash->Columns = realloc(Hash->Columns, Hash->nColumns * sizeof(HASH_COLUMN)); + Hash->Columns[Hash->nColumns - 1].key = strdup(column); + Hash->Columns[Hash->nColumns - 1].val = number; + + qsort(Hash->Columns, Hash->nColumns, sizeof(HASH_COLUMN), hash_sort_column); + } /* fetch a column number by column header */ -static int hash_get_column (HASH *Hash, const char *key) +static int hash_get_column(HASH * Hash, const char *key) { - HASH_COLUMN *Column; + HASH_COLUMN *Column; - if (key == NULL || *key == '\0') return -1; - - Column = bsearch(key, Hash->Columns, Hash->nColumns, sizeof(HASH_COLUMN), hash_lookup_column); - if (Column == NULL) return -1; + if (key == NULL || *key == '\0') + return -1; - return Column->val; + Column = bsearch(key, Hash->Columns, Hash->nColumns, sizeof(HASH_COLUMN), hash_lookup_column); + if (Column == NULL) + return -1; + + return Column->val; } /* set column delimiters */ -void hash_set_delimiter (HASH *Hash, const char *delimiter) +void hash_set_delimiter(HASH * Hash, const char *delimiter) { - if (Hash->delimiter != NULL) free (Hash->delimiter); - Hash->delimiter = strdup(delimiter); + if (Hash->delimiter != NULL) + free(Hash->delimiter); + Hash->delimiter = strdup(delimiter); } /* get a string from the hash table */ -char *hash_get (HASH *Hash, const char *key, const char *column) +char *hash_get(HASH * Hash, const char *key, const char *column) { - HASH_ITEM *Item; - int c; - - Item = hash_lookup(Hash, key, 1); - if (Item == NULL) return NULL; - - c = hash_get_column(Hash, column); - return split(Item->Slot[Item->index].value, c, Hash->delimiter); + HASH_ITEM *Item; + int c; + + Item = hash_lookup(Hash, key, 1); + if (Item == NULL) + return NULL; + + c = hash_get_column(Hash, column); + return split(Item->Slot[Item->index].value, c, Hash->delimiter); } /* get a delta value from the delta table */ -double hash_get_delta (HASH *Hash, const char *key, const char *column, const int delay) +double hash_get_delta(HASH * Hash, const char *key, const char *column, const int delay) { - HASH_ITEM *Item; - HASH_SLOT *Slot1, *Slot2; - int i, c; - double v1, v2; - double dv, dt; - struct timeval now, end; - - /* lookup item */ - Item = hash_lookup(Hash, key, 1); - if (Item == NULL) return 0.0; - - /* this is the "current" Slot */ - Slot1 = &(Item->Slot[Item->index]); - - /* fetch column number */ - c = hash_get_column(Hash, column); - - /* if delay is zero, return absolute value */ - if (delay == 0) return atof(split(Slot1->value, c, Hash->delimiter)); - - /* prepare timing values */ - now = Slot1->timestamp; - end.tv_sec = now.tv_sec; - end.tv_usec = now.tv_usec - 1000*delay; - while (end.tv_usec < 0) { - end.tv_sec--; - end.tv_usec += 1000000; - } - - /* search delta slot */ - Slot2 = &(Item->Slot[Item->index]); - for (i = 1; i < Item->nSlot; i++) { - Slot2 = &(Item->Slot[(Item->index + i) % Item->nSlot]); - if (Slot2->timestamp.tv_sec == 0) break; - if (timercmp(&(Slot2->timestamp), &end, <)) break; - } - - /* empty slot => try the one before */ - if (Slot2->timestamp.tv_sec == 0) { - i--; - Slot2 = &(Item->Slot[(Item->index + i) % Item->nSlot]); - } - - /* not enough slots available... */ - if (i == 0) return 0.0; - - /* delta value, delta time */ - v1 = atof(split(Slot1->value, c, Hash->delimiter)); - v2 = atof(split(Slot2->value, c, Hash->delimiter)); - dv = v1 - v2; - dt = (Slot1->timestamp.tv_sec - Slot2->timestamp.tv_sec) - + (Slot1->timestamp.tv_usec - Slot2->timestamp.tv_usec)/1000000.0; - - if (dt > 0.0 && dv >= 0.0) return dv/dt; - return 0.0; + HASH_ITEM *Item; + HASH_SLOT *Slot1, *Slot2; + int i, c; + double v1, v2; + double dv, dt; + struct timeval now, end; + + /* lookup item */ + Item = hash_lookup(Hash, key, 1); + if (Item == NULL) + return 0.0; + + /* this is the "current" Slot */ + Slot1 = &(Item->Slot[Item->index]); + + /* fetch column number */ + c = hash_get_column(Hash, column); + + /* if delay is zero, return absolute value */ + if (delay == 0) + return atof(split(Slot1->value, c, Hash->delimiter)); + + /* prepare timing values */ + now = Slot1->timestamp; + end.tv_sec = now.tv_sec; + end.tv_usec = now.tv_usec - 1000 * delay; + while (end.tv_usec < 0) { + end.tv_sec--; + end.tv_usec += 1000000; + } + + /* search delta slot */ + Slot2 = &(Item->Slot[Item->index]); + for (i = 1; i < Item->nSlot; i++) { + Slot2 = &(Item->Slot[(Item->index + i) % Item->nSlot]); + if (Slot2->timestamp.tv_sec == 0) + break; + if (timercmp(&(Slot2->timestamp), &end, <)) + break; + } + + /* empty slot => try the one before */ + if (Slot2->timestamp.tv_sec == 0) { + i--; + Slot2 = &(Item->Slot[(Item->index + i) % Item->nSlot]); + } + + /* not enough slots available... */ + if (i == 0) + return 0.0; + + /* delta value, delta time */ + v1 = atof(split(Slot1->value, c, Hash->delimiter)); + v2 = atof(split(Slot2->value, c, Hash->delimiter)); + dv = v1 - v2; + dt = (Slot1->timestamp.tv_sec - Slot2->timestamp.tv_sec) + + (Slot1->timestamp.tv_usec - Slot2->timestamp.tv_usec) / 1000000.0; + + if (dt > 0.0 && dv >= 0.0) + return dv / dt; + return 0.0; } /* get a delta value from the delta table */ /* key may contain regular expressions, and the sum */ /* of all matching entries is returned. */ -double hash_get_regex (HASH *Hash, const char *key, const char *column, const int delay) +double hash_get_regex(HASH * Hash, const char *key, const char *column, const int delay) { - double sum; - regex_t preg; - int i, err; - - err = regcomp(&preg, key, REG_ICASE|REG_NOSUB); - if (err != 0) { - char buffer[32]; - regerror(err, &preg, buffer, sizeof(buffer)); - error ("error in regular expression: %s", buffer); - regfree(&preg); - return 0.0; - } - - /* force the table to be sorted by requesting anything */ - hash_lookup(Hash, NULL, 1); - - sum = 0.0; - for (i = 0; i < Hash->nItems; i++) { - if (regexec(&preg, Hash->Items[i].key, 0, NULL, 0) == 0) { - sum += hash_get_delta(Hash, Hash->Items[i].key, column, delay); + double sum; + regex_t preg; + int i, err; + + err = regcomp(&preg, key, REG_ICASE | REG_NOSUB); + if (err != 0) { + char buffer[32]; + regerror(err, &preg, buffer, sizeof(buffer)); + error("error in regular expression: %s", buffer); + regfree(&preg); + return 0.0; } - } - regfree(&preg); - return sum; + + /* force the table to be sorted by requesting anything */ + hash_lookup(Hash, NULL, 1); + + sum = 0.0; + for (i = 0; i < Hash->nItems; i++) { + if (regexec(&preg, Hash->Items[i].key, 0, NULL, 0) == 0) { + sum += hash_get_delta(Hash, Hash->Items[i].key, column, delay); + } + } + regfree(&preg); + return sum; } @@ -485,107 +506,111 @@ double hash_get_regex (HASH *Hash, const char *key, const char *column, const in /* Otherwise, the entry is appended at the end, and */ /* the table will be flagged 'unsorted' afterwards */ -static HASH_ITEM* hash_set (HASH *Hash, const char *key, const char *value, const int delta) +static HASH_ITEM *hash_set(HASH * Hash, const char *key, const char *value, const int delta) { - HASH_ITEM *Item; - HASH_SLOT *Slot; - int size; - - Item = hash_lookup (Hash, key, 0); - - if (Item == NULL) { - - /* add entry */ - Hash->sorted = 0; - Hash->nItems++; - Hash->Items = realloc(Hash->Items, Hash->nItems * sizeof(HASH_ITEM)); - - Item = &(Hash->Items[Hash->nItems-1]); - Item->key = strdup(key); - Item->index = 0; - Item->nSlot = delta; - Item->Slot = malloc (Item->nSlot * sizeof(HASH_SLOT)); - memset(Item->Slot, 0, Item->nSlot * sizeof(HASH_SLOT)); - - } else { - - /* maybe enlarge delta table */ - if (Item->nSlot < delta) { - Item->nSlot = delta; - Item->Slot = realloc (Item->Slot, Item->nSlot * sizeof(HASH_SLOT)); + HASH_ITEM *Item; + HASH_SLOT *Slot; + int size; + + Item = hash_lookup(Hash, key, 0); + + if (Item == NULL) { + + /* add entry */ + Hash->sorted = 0; + Hash->nItems++; + Hash->Items = realloc(Hash->Items, Hash->nItems * sizeof(HASH_ITEM)); + + Item = &(Hash->Items[Hash->nItems - 1]); + Item->key = strdup(key); + Item->index = 0; + Item->nSlot = delta; + Item->Slot = malloc(Item->nSlot * sizeof(HASH_SLOT)); + memset(Item->Slot, 0, Item->nSlot * sizeof(HASH_SLOT)); + + } else { + + /* maybe enlarge delta table */ + if (Item->nSlot < delta) { + Item->nSlot = delta; + Item->Slot = realloc(Item->Slot, Item->nSlot * sizeof(HASH_SLOT)); + } + + } + + if (Item->nSlot > 1) { + /* move the pointer to the next free slot, wrap around if necessary */ + if (--Item->index < 0) + Item->index = Item->nSlot - 1; } - } - - if (Item->nSlot > 1) { - /* move the pointer to the next free slot, wrap around if necessary */ - if (--Item->index < 0) Item->index = Item->nSlot-1; - } - - /* create entry */ - Slot = &(Item->Slot[Item->index]); - size = strlen(value) + 1; - - /* maybe enlarge value buffer */ - if (size > Slot->size) { - /* buffer is either empty or too small */ - /* allocate memory in multiples of CHUNK_SIZE */ - Slot->size = CHUNK_SIZE * (size / CHUNK_SIZE + 1); - Slot->value = realloc(Slot->value, Slot->size); - } - - /* set value */ - strcpy(Slot->value, value); - - /* set timestamps */ - gettimeofday(&(Hash->timestamp), NULL); - Slot->timestamp = Hash->timestamp; - - return Item; + /* create entry */ + Slot = &(Item->Slot[Item->index]); + size = strlen(value) + 1; + + /* maybe enlarge value buffer */ + if (size > Slot->size) { + /* buffer is either empty or too small */ + /* allocate memory in multiples of CHUNK_SIZE */ + Slot->size = CHUNK_SIZE * (size / CHUNK_SIZE + 1); + Slot->value = realloc(Slot->value, Slot->size); + } + + /* set value */ + strcpy(Slot->value, value); + + /* set timestamps */ + gettimeofday(&(Hash->timestamp), NULL); + Slot->timestamp = Hash->timestamp; + + return Item; } /* insert a string into the hash table */ /* without delta processing */ -void hash_put (HASH *Hash, const char *key, const char *value) +void hash_put(HASH * Hash, const char *key, const char *value) { - hash_set (Hash, key, value, 1); + hash_set(Hash, key, value, 1); } /* insert a string into the hash table */ /* with delta processing */ -void hash_put_delta (HASH *Hash, const char *key, const char *value) +void hash_put_delta(HASH * Hash, const char *key, const char *value) { - hash_set (Hash, key, value, DELTA_SLOTS); + hash_set(Hash, key, value, DELTA_SLOTS); } -void hash_destroy (HASH *Hash) +void hash_destroy(HASH * Hash) { - int i; + int i; - if (Hash->Items) { - - /* free all headers */ - for (i = 0; i < Hash->nColumns; i++) { - if (Hash->Columns[i].key) free (Hash->Columns[i].key); - } + if (Hash->Items) { - /* free header table */ - free (Hash->Columns); + /* free all headers */ + for (i = 0; i < Hash->nColumns; i++) { + if (Hash->Columns[i].key) + free(Hash->Columns[i].key); + } - /* free all items */ - for (i = 0; i < Hash->nItems; i++) { - if (Hash->Items[i].key) free (Hash->Items[i].key); - if (Hash->Items[i].Slot) free (Hash->Items[i].Slot); + /* free header table */ + free(Hash->Columns); + + /* free all items */ + for (i = 0; i < Hash->nItems; i++) { + if (Hash->Items[i].key) + free(Hash->Items[i].key); + if (Hash->Items[i].Slot) + free(Hash->Items[i].Slot); + } + + /* free items table */ + free(Hash->Items); } - - /* free items table */ - free (Hash->Items); - } - - Hash->sorted = 0; - Hash->nItems = 0; - Hash->Items = NULL; + + Hash->sorted = 0; + Hash->nItems = 0; + Hash->Items = NULL; } @@ -1,4 +1,4 @@ -/* $Id: hash.h,v 1.18 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: hash.h,v 1.19 2005/05/08 04:32:44 reinelt Exp $ * * hashes (associative arrays) * @@ -23,6 +23,9 @@ * * * $Log: hash.h,v $ + * Revision 1.19 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.18 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -113,52 +116,52 @@ typedef struct { - int size; - char *value; - struct timeval timestamp; + int size; + char *value; + struct timeval timestamp; } HASH_SLOT; typedef struct { - char *key; - int val; + char *key; + int val; } HASH_COLUMN; typedef struct { - char *key; - int index; - int nSlot; - HASH_SLOT *Slot; + char *key; + int index; + int nSlot; + HASH_SLOT *Slot; } HASH_ITEM; typedef struct { - int sorted; - struct timeval timestamp; - int nItems; - HASH_ITEM *Items; - int nColumns; - HASH_COLUMN *Columns; - char *delimiter; + int sorted; + struct timeval timestamp; + int nItems; + HASH_ITEM *Items; + int nColumns; + HASH_COLUMN *Columns; + char *delimiter; } HASH; -void hash_create (HASH *Hash); +void hash_create(HASH * Hash); -int hash_age (HASH *Hash, const char *key); +int hash_age(HASH * Hash, const char *key); -void hash_set_column (HASH *Hash, const int number, const char *column); -void hash_set_delimiter (HASH *Hash, const char *delimiter); +void hash_set_column(HASH * Hash, const int number, const char *column); +void hash_set_delimiter(HASH * Hash, const char *delimiter); -char *hash_get (HASH *Hash, const char *key, const char *column); -double hash_get_delta (HASH *Hash, const char *key, const char *column, const int delay); -double hash_get_regex (HASH *Hash, const char *key, const char *column, const int delay); +char *hash_get(HASH * Hash, const char *key, const char *column); +double hash_get_delta(HASH * Hash, const char *key, const char *column, const int delay); +double hash_get_regex(HASH * Hash, const char *key, const char *column, const int delay); -void hash_put (HASH *Hash, const char *key, const char *value); -void hash_put_delta (HASH *Hash, const char *key, const char *value); +void hash_put(HASH * Hash, const char *key, const char *value); +void hash_put_delta(HASH * Hash, const char *key, const char *value); -void hash_destroy (HASH *Hash); +void hash_destroy(HASH * Hash); #endif diff --git a/indent.sh b/indent.sh new file mode 100755 index 0000000..30dd2fa --- /dev/null +++ b/indent.sh @@ -0,0 +1,7 @@ +#! /bin/bash + +# -kr Use Kernighan & Ritchie coding style. +# -l150 Set maximum line length for non-comment lines to 150. + +indent -kr -l150 *.c *.h + @@ -1,4 +1,4 @@ -/* $Id: layout.c,v 1.15 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: layout.c,v 1.16 2005/05/08 04:32:44 reinelt Exp $ * * new layouter framework * @@ -23,6 +23,9 @@ * * * $Log: layout.c,v $ + * Revision 1.16 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.15 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -113,55 +116,57 @@ #endif -int layout_addItem (const char *name, const int row, const int col) +int layout_addItem(const char *name, const int row, const int col) { - /* allocate widget */ - widget_add (name, row-1, col-1); - return 0; + /* allocate widget */ + widget_add(name, row - 1, col - 1); + return 0; } -int layout_init (const char *layout) +int layout_init(const char *layout) { - char *section; - char *list, *l; - char *widget; - int row, col; - - info ("initializing layout '%s'", layout); - - /* prepare config section */ - /* strlen("Layout:")=7 */ - section=malloc(strlen(layout)+8); - strcpy(section, "Layout:"); - strcat(section, layout); - - /* get a list of all keys in this section */ - list=cfg_list(section); + char *section; + char *list, *l; + char *widget; + int row, col; + + info("initializing layout '%s'", layout); + + /* prepare config section */ + /* strlen("Layout:")=7 */ + section = malloc(strlen(layout) + 8); + strcpy(section, "Layout:"); + strcat(section, layout); - /* map to lower char for scanf() */ - for (l=list; *l!='\0'; l++) *l=tolower(*l); + /* get a list of all keys in this section */ + list = cfg_list(section); - l=list; - while (l!=NULL) { - char *p; - int i, n; - /* list is delimited by | */ - while (*l=='|') l++; - if ((p=strchr(l, '|'))!=NULL) *p='\0'; - i=sscanf (l, "row%d.col%d%n", &row, &col, &n); - if (i==2 && l[n]=='\0') { - widget=cfg_get(section, l, NULL); - if (widget!=NULL && *widget!='\0') { - layout_addItem (widget, row, col); - } - free(widget); + /* map to lower char for scanf() */ + for (l = list; *l != '\0'; l++) + *l = tolower(*l); + + l = list; + while (l != NULL) { + char *p; + int i, n; + /* list is delimited by | */ + while (*l == '|') + l++; + if ((p = strchr(l, '|')) != NULL) + *p = '\0'; + i = sscanf(l, "row%d.col%d%n", &row, &col, &n); + if (i == 2 && l[n] == '\0') { + widget = cfg_get(section, l, NULL); + if (widget != NULL && *widget != '\0') { + layout_addItem(widget, row, col); + } + free(widget); + } + /* next field */ + l = p ? p + 1 : NULL; } - /* next field */ - l=p?p+1:NULL; - } - free (list); - free(section); - return 0; + free(list); + free(section); + return 0; } - diff --git a/lcd4linux.c b/lcd4linux.c index 65e4a66..a532b49 100644 --- a/lcd4linux.c +++ b/lcd4linux.c @@ -1,4 +1,4 @@ -/* $Id: lcd4linux.c,v 1.77 2005/03/30 04:57:50 reinelt Exp $ +/* $Id: lcd4linux.c,v 1.78 2005/05/08 04:32:44 reinelt Exp $ * * LCD4Linux * @@ -23,6 +23,9 @@ * * * $Log: lcd4linux.c,v $ + * Revision 1.78 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.77 2005/03/30 04:57:50 reinelt * Evaluator speedup: use bsearch for finding functions and variables * @@ -390,289 +393,291 @@ #define PIDFILE "/var/run/lcd4linux.pid" -static char *release = "LCD4Linux " VERSION; +static char *release = "LCD4Linux " VERSION; static char *copyright = "Copyright (C) 2005 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>"; static char **my_argv; -extern char* output; +extern char *output; -int got_signal=0; +int got_signal = 0; static void usage(void) { - printf ("%s\n", release); - printf ("%s\n", copyright); - printf ("usage: lcd4linux [-h]\n"); - printf (" lcd4linux [-l]\n"); - printf (" lcd4linux [-c key=value] [-i] [-f config-file] [-v]\n"); - printf (" lcd4linux [-c key=value] [-F] [-f config-file] [-o output-file] [-q] [-v]\n"); + printf("%s\n", release); + printf("%s\n", copyright); + printf("usage: lcd4linux [-h]\n"); + printf(" lcd4linux [-l]\n"); + printf(" lcd4linux [-c key=value] [-i] [-f config-file] [-v]\n"); + printf(" lcd4linux [-c key=value] [-F] [-f config-file] [-o output-file] [-q] [-v]\n"); } -static void interactive_mode (void) +static void interactive_mode(void) { - char line[1024]; - void *tree; - RESULT result = {0, 0, 0, NULL}; - - printf("\neval> "); - for(fgets(line, 1024, stdin); !feof(stdin); fgets(line, 1024, stdin)) { - if (line[strlen(line)-1]=='\n') line[strlen(line)-1]='\0'; - if (strlen(line)>0) { - if (Compile(line, &tree)!=-1) { - Eval (tree, &result); - if (result.type == R_NUMBER) { - printf ("%g\n", R2N(&result)); - } else if (result.type == R_STRING) { - printf ("'%s'\n", R2S(&result)); - } else if (result.type == (R_NUMBER|R_STRING)) { - printf ("'%s' (%g)\n", R2S(&result), R2N(&result)); - } else { - printf ("internal error: unknown result type %d\n", result.type); + char line[1024]; + void *tree; + RESULT result = { 0, 0, 0, NULL }; + + printf("\neval> "); + for (fgets(line, 1024, stdin); !feof(stdin); fgets(line, 1024, stdin)) { + if (line[strlen(line) - 1] == '\n') + line[strlen(line) - 1] = '\0'; + if (strlen(line) > 0) { + if (Compile(line, &tree) != -1) { + Eval(tree, &result); + if (result.type == R_NUMBER) { + printf("%g\n", R2N(&result)); + } else if (result.type == R_STRING) { + printf("'%s'\n", R2S(&result)); + } else if (result.type == (R_NUMBER | R_STRING)) { + printf("'%s' (%g)\n", R2S(&result), R2N(&result)); + } else { + printf("internal error: unknown result type %d\n", result.type); + } + DelResult(&result); + } + DelTree(tree); } - DelResult (&result); - } - DelTree(tree); + printf("eval> "); } - printf("eval> "); - } - printf ("\n"); + printf("\n"); } -void handler (int signal) +void handler(int signal) { - debug ("got signal %d", signal); - got_signal=signal; + debug("got signal %d", signal); + got_signal = signal; } -int main (int argc, char *argv[]) +int main(int argc, char *argv[]) { - char *cfg = "/etc/lcd4linux.conf"; - char *display, *driver, *layout; - char section[32]; - int c; - int quiet = 0; - int interactive = 0; - int pid; - - /* save arguments for restart */ - my_argv = malloc(sizeof(char*)*(argc+1)); - for (c = 0; c < argc; c++) { - my_argv[c] = strdup(argv[c]); - } - my_argv[c] = NULL; - - running_foreground = 0; - running_background = 0; - - while ((c=getopt (argc, argv, "c:Ff:hilo:qv"))!=EOF) { - - switch (c) { - case 'c': - if (cfg_cmd (optarg)<0) { - fprintf (stderr, "%s: illegal argument -c '%s'\n", argv[0], optarg); + char *cfg = "/etc/lcd4linux.conf"; + char *display, *driver, *layout; + char section[32]; + int c; + int quiet = 0; + int interactive = 0; + int pid; + + /* save arguments for restart */ + my_argv = malloc(sizeof(char *) * (argc + 1)); + for (c = 0; c < argc; c++) { + my_argv[c] = strdup(argv[c]); + } + my_argv[c] = NULL; + + running_foreground = 0; + running_background = 0; + + while ((c = getopt(argc, argv, "c:Ff:hilo:qv")) != EOF) { + + switch (c) { + case 'c': + if (cfg_cmd(optarg) < 0) { + fprintf(stderr, "%s: illegal argument -c '%s'\n", argv[0], optarg); + exit(2); + } + break; + case 'F': + running_foreground++; + break; + case 'f': + cfg = optarg; + break; + case 'h': + usage(); + exit(0); + case 'i': + interactive++; + break; + case 'l': + printf("%s\n", release); + printf("%s\n", copyright); + drv_list(); + exit(0); + case 'o': + output = optarg; + break; + case 'q': + quiet++; + break; + case 'v': + verbose_level++; + break; + default: + exit(2); + } + } + + if (optind < argc) { + fprintf(stderr, "%s: illegal option %s\n", argv[0], argv[optind]); exit(2); - } - break; - case 'F': - running_foreground++; - break; - case 'f': - cfg=optarg; - break; - case 'h': - usage(); - exit(0); - case 'i': - interactive++; - break; - case 'l': - printf ("%s\n", release); - printf ("%s\n", copyright); - drv_list(); - exit(0); - case 'o': - output=optarg; - break; - case 'q': - quiet++; - break; - case 'v': - verbose_level++; - break; - default: - exit(2); } - } - - if (optind < argc) { - fprintf (stderr, "%s: illegal option %s\n", argv[0], argv[optind]); - exit(2); - } - - /* do not fork in interactive mode */ - if (interactive) { - running_foreground=1; - } - - info ("Version " VERSION " starting"); - if (!running_foreground && (my_argv[0]==NULL || my_argv[0][0]!='/')) { - info ("invoked without full path; restart may not work!"); - } - - if (cfg_init(cfg)==-1) - exit (1); - - if (plugin_init()==-1) - exit (1); - - display=cfg_get(NULL, "Display", NULL); - if (display==NULL || *display=='\0') { - error ("missing 'Display' entry in %s!", cfg_source()); - exit (1); - } - - qprintf(section, sizeof(section), "Display:%s", display); - free(display); - driver=cfg_get(section, "Driver", NULL); - if (driver==NULL || *driver=='\0') { - error ("missing '%s.Driver' entry in %s!", section, cfg_source()); - exit (1); - } - - if (!running_foreground) { - pid_t i; - int fd; - debug ("going background..."); - i=fork(); - if (i<0) { - error ("fork() failed: %s", strerror(errno)); - exit (1); + + /* do not fork in interactive mode */ + if (interactive) { + running_foreground = 1; + } + + info("Version " VERSION " starting"); + if (!running_foreground && (my_argv[0] == NULL || my_argv[0][0] != '/')) { + info("invoked without full path; restart may not work!"); + } + + if (cfg_init(cfg) == -1) + exit(1); + + if (plugin_init() == -1) + exit(1); + + display = cfg_get(NULL, "Display", NULL); + if (display == NULL || *display == '\0') { + error("missing 'Display' entry in %s!", cfg_source()); + exit(1); + } + + qprintf(section, sizeof(section), "Display:%s", display); + free(display); + driver = cfg_get(section, "Driver", NULL); + if (driver == NULL || *driver == '\0') { + error("missing '%s.Driver' entry in %s!", section, cfg_source()); + exit(1); + } + + if (!running_foreground) { + pid_t i; + int fd; + debug("going background..."); + i = fork(); + if (i < 0) { + error("fork() failed: %s", strerror(errno)); + exit(1); + } + if (i != 0) + exit(0); + + /* ignore nasty signals */ + signal(SIGINT, SIG_IGN); + signal(SIGQUIT, SIG_IGN); + + /* chdir("/") */ + if (chdir("/") != 0) { + error("chdir(\"/\") failed: %s", strerror(errno)); + exit(1); + } + + /* we want full control over permissions */ + umask(0); + + /* detach stdin */ + if (freopen("/dev/null", "r", stdin) == NULL) { + error("freopen (/dev/null) failed: %s", strerror(errno)); + exit(1); + } + + /* detach stdout and stderr */ + fd = open("/dev/null", O_WRONLY, 0666); + if (fd == -1) { + error("open (/dev/null) failed: %s", strerror(errno)); + exit(1); + } + fflush(stdout); + fflush(stderr); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + close(fd); + + /* create PID file */ + if ((pid = pid_init(PIDFILE)) != 0) { + error("lcd4linux already running as process %d", pid); + exit(1); + } + + /* now we are a daemon */ + running_background = 1; + } + + /* go into interactive mode before display initialization */ + if (interactive >= 2) { + interactive_mode(); + pid_exit(PIDFILE); + cfg_exit(); + exit(0); } - if (i!=0) exit (0); - - /* ignore nasty signals */ - signal(SIGINT, SIG_IGN); - signal(SIGQUIT, SIG_IGN); - - /* chdir("/") */ - if (chdir("/")!=0) { - error ("chdir(\"/\") failed: %s", strerror(errno)); - exit (1); + + /* check the conf to see if quiet startup is wanted */ + if (!quiet) { + cfg_number(NULL, "Quiet", 0, 0, 1, &quiet); } - - /* we want full control over permissions */ - umask (0); - - /* detach stdin */ - if (freopen("/dev/null", "r", stdin)==NULL) { - error ("freopen (/dev/null) failed: %s", strerror(errno)); - exit (1); + + debug("initializing driver %s", driver); + if (drv_init(section, driver, quiet) == -1) { + pid_exit(PIDFILE); + exit(1); } + free(driver); - /* detach stdout and stderr */ - fd=open("/dev/null", O_WRONLY, 0666); - if (fd==-1) { - error ("open (/dev/null) failed: %s", strerror(errno)); - exit (1); + /* go into interactive mode (display has been initialized) */ + if (interactive >= 1) { + interactive_mode(); + drv_quit(quiet); + pid_exit(PIDFILE); + cfg_exit(); + exit(0); } - fflush(stdout); - fflush(stderr); - dup2(fd, STDOUT_FILENO); - dup2(fd, STDERR_FILENO); - close(fd); - - /* create PID file */ - if ((pid = pid_init(PIDFILE)) != 0) { - error ("lcd4linux already running as process %d", pid); - exit (1); + + /* check for new-style layout */ + layout = cfg_get(NULL, "Layout", NULL); + if (layout == NULL || *layout == '\0') { + error("missing 'Layout' entry in %s!", cfg_source()); + exit(1); } - /* now we are a daemon */ - running_background=1; - } - - /* go into interactive mode before display initialization */ - if (interactive >= 2) { - interactive_mode(); - pid_exit(PIDFILE); - cfg_exit(); - exit (0); - } - - /* check the conf to see if quiet startup is wanted */ - if (!quiet) { - cfg_number(NULL, "Quiet", 0, 0, 1, &quiet); - } - - debug ("initializing driver %s", driver); - if (drv_init(section, driver, quiet)==-1) { - pid_exit(PIDFILE); - exit (1); - } - free(driver); - - /* go into interactive mode (display has been initialized) */ - if (interactive >= 1) { - interactive_mode(); + layout_init(layout); + free(layout); + + debug("starting main loop"); + + /* now install our own signal handler */ + signal(SIGHUP, handler); + signal(SIGINT, handler); + signal(SIGQUIT, handler); + signal(SIGTERM, handler); + + while (got_signal == 0) { + struct timespec delay; + if (timer_process(&delay) < 0) + break; + nanosleep(&delay, NULL); + } + + debug("leaving main loop"); + drv_quit(quiet); pid_exit(PIDFILE); cfg_exit(); - exit (0); - } - - /* check for new-style layout */ - layout=cfg_get(NULL, "Layout", NULL); - if (layout==NULL || *layout=='\0') { - error ("missing 'Layout' entry in %s!", cfg_source()); - exit (1); - } - - layout_init(layout); - free(layout); - - debug ("starting main loop"); - - /* now install our own signal handler */ - signal(SIGHUP, handler); - signal(SIGINT, handler); - signal(SIGQUIT, handler); - signal(SIGTERM, handler); - - while (got_signal==0) { - struct timespec delay; - if (timer_process(&delay)<0) break; - nanosleep(&delay, NULL); - } - - debug ("leaving main loop"); - - drv_quit(quiet); - pid_exit(PIDFILE); - cfg_exit(); - plugin_exit(); - timer_exit(); - - if (got_signal==SIGHUP) { - long fd; - debug ("restarting..."); - /* close all files on exec */ - for (fd=sysconf(_SC_OPEN_MAX); fd>2; fd--) { - int flag; - if ((flag=fcntl(fd,F_GETFD,0))!=-1) - fcntl(fd,F_SETFD,flag|FD_CLOEXEC); + plugin_exit(); + timer_exit(); + + if (got_signal == SIGHUP) { + long fd; + debug("restarting..."); + /* close all files on exec */ + for (fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) { + int flag; + if ((flag = fcntl(fd, F_GETFD, 0)) != -1) + fcntl(fd, F_SETFD, flag | FD_CLOEXEC); + } + execv(my_argv[0], my_argv); + error("execv() failed: %s", strerror(errno)); + exit(1); } - execv (my_argv[0], my_argv); - error ("execv() failed: %s", strerror(errno)); - exit(1); - } - - for(c=0;my_argv[c]!=NULL;c++) { - free(my_argv[c]); - } - free(my_argv); - - exit (0); + + for (c = 0; my_argv[c] != NULL; c++) { + free(my_argv[c]); + } + free(my_argv); + + exit(0); } - @@ -1,4 +1,4 @@ -/* $Id: pid.c,v 1.9 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: pid.c,v 1.10 2005/05/08 04:32:44 reinelt Exp $ * * PID file handling * @@ -22,6 +22,9 @@ * * * $Log: pid.c,v $ + * Revision 1.10 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -96,91 +99,92 @@ #endif -int pid_init (const char *pidfile) +int pid_init(const char *pidfile) { - char tmpfile[256]; - char buffer[16]; - int fd, len, pid; - - qprintf(tmpfile, sizeof(tmpfile), "%s.%s", pidfile, "XXXXXX"); - - if ((fd=mkstemp(tmpfile))==-1) { - error ("mkstemp(%s) failed: %s", tmpfile, strerror(errno)); - return -1; - } - - if (fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)==-1) { - error ("fchmod(%s) failed: %s", tmpfile, strerror(errno)); - close(fd); - unlink(tmpfile); - return -1; - } - - qprintf(buffer, sizeof(buffer), "%d\n", (int)getpid()); - len = strlen(buffer); - if (write(fd, buffer, len) != len) { - error ("write(%s) failed: %s", tmpfile, strerror(errno)); - close(fd); - unlink(tmpfile); - return -1; - } - close (fd); - - - while (link(tmpfile, pidfile)==-1) { - - if (errno!=EEXIST) { - error ("link(%s, %s) failed: %s", tmpfile, pidfile, strerror(errno)); - unlink(tmpfile); - return -1; - } + char tmpfile[256]; + char buffer[16]; + int fd, len, pid; - if ((fd=open(pidfile, O_RDONLY))==-1) { - if (errno==ENOENT) continue; /* pidfile disappared */ - error ("open(%s) failed: %s", pidfile, strerror(errno)); - unlink (tmpfile); - return -1; - } + qprintf(tmpfile, sizeof(tmpfile), "%s.%s", pidfile, "XXXXXX"); - len=read(fd, buffer, sizeof(buffer)-1); - if (len<0) { - error ("read(%s) failed: %s", pidfile, strerror(errno)); - unlink (tmpfile); - return -1; + if ((fd = mkstemp(tmpfile)) == -1) { + error("mkstemp(%s) failed: %s", tmpfile, strerror(errno)); + return -1; } - - buffer[len]='\0'; - if (sscanf(buffer, "%d", &pid)!=1 || pid==0) { - error ("scan(%s) failed.", pidfile); - unlink (tmpfile); - return -1; + + if (fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) { + error("fchmod(%s) failed: %s", tmpfile, strerror(errno)); + close(fd); + unlink(tmpfile); + return -1; } - if (pid==getpid()) { - error ("%s already locked by us. uh-oh...", pidfile); - unlink(tmpfile); - return 0; + qprintf(buffer, sizeof(buffer), "%d\n", (int) getpid()); + len = strlen(buffer); + if (write(fd, buffer, len) != len) { + error("write(%s) failed: %s", tmpfile, strerror(errno)); + close(fd); + unlink(tmpfile); + return -1; } - - if ((kill(pid, 0)==-1) && errno==ESRCH) { - error ("removing stale PID file %s", pidfile); - if (unlink(pidfile)==-1 && errno!=ENOENT) { - error ("unlink(%s) failed: %s", pidfile, strerror(errno)); + close(fd); + + + while (link(tmpfile, pidfile) == -1) { + + if (errno != EEXIST) { + error("link(%s, %s) failed: %s", tmpfile, pidfile, strerror(errno)); + unlink(tmpfile); + return -1; + } + + if ((fd = open(pidfile, O_RDONLY)) == -1) { + if (errno == ENOENT) + continue; /* pidfile disappared */ + error("open(%s) failed: %s", pidfile, strerror(errno)); + unlink(tmpfile); + return -1; + } + + len = read(fd, buffer, sizeof(buffer) - 1); + if (len < 0) { + error("read(%s) failed: %s", pidfile, strerror(errno)); + unlink(tmpfile); + return -1; + } + + buffer[len] = '\0'; + if (sscanf(buffer, "%d", &pid) != 1 || pid == 0) { + error("scan(%s) failed.", pidfile); + unlink(tmpfile); + return -1; + } + + if (pid == getpid()) { + error("%s already locked by us. uh-oh...", pidfile); + unlink(tmpfile); + return 0; + } + + if ((kill(pid, 0) == -1) && errno == ESRCH) { + error("removing stale PID file %s", pidfile); + if (unlink(pidfile) == -1 && errno != ENOENT) { + error("unlink(%s) failed: %s", pidfile, strerror(errno)); + unlink(tmpfile); + return pid; + } + continue; + } unlink(tmpfile); return pid; - } - continue; } - unlink (tmpfile); - return pid; - } - - unlink (tmpfile); - return 0; + + unlink(tmpfile); + return 0; } -int pid_exit (const char *pidfile) +int pid_exit(const char *pidfile) { - return unlink(pidfile); + return unlink(pidfile); } @@ -1,4 +1,4 @@ -/* $Id: pid.h,v 1.3 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: pid.h,v 1.4 2005/05/08 04:32:44 reinelt Exp $ * * PID file handling * @@ -23,6 +23,9 @@ * * * $Log: pid.h,v $ + * Revision 1.4 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.3 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -37,7 +40,7 @@ #ifndef _PID_H_ #define _PID_H_ -int pid_init (const char *pidfile); -int pid_exit (const char *pidfile); +int pid_init(const char *pidfile); +int pid_exit(const char *pidfile); #endif @@ -1,4 +1,4 @@ -/* $Id: plugin.c,v 1.37 2005/05/02 10:29:20 reinelt Exp $ +/* $Id: plugin.c,v 1.38 2005/05/08 04:32:44 reinelt Exp $ * * plugin handler for the Evaluator * @@ -23,6 +23,9 @@ * * * $Log: plugin.c,v $ + * Revision 1.38 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.37 2005/05/02 10:29:20 reinelt * preparations for python bindings and python plugin * @@ -200,216 +203,217 @@ /* Prototypes */ -int plugin_init_cfg (void); -void plugin_exit_cfg (void); -int plugin_init_math (void); -void plugin_exit_math (void); -int plugin_init_string (void); -void plugin_exit_string (void); -int plugin_init_test (void); -void plugin_exit_test (void); -int plugin_init_time (void); -void plugin_exit_time (void); +int plugin_init_cfg(void); +void plugin_exit_cfg(void); +int plugin_init_math(void); +void plugin_exit_math(void); +int plugin_init_string(void); +void plugin_exit_string(void); +int plugin_init_test(void); +void plugin_exit_test(void); +int plugin_init_time(void); +void plugin_exit_time(void); -int plugin_init_apm (void); -void plugin_exit_apm (void); -int plugin_init_cpuinfo (void); -void plugin_exit_cpuinfo (void); -int plugin_init_diskstats (void); -void plugin_exit_diskstats (void); -int plugin_init_dvb (void); -void plugin_exit_dvb (void); -int plugin_init_exec (void); -void plugin_exit_exec (void); -int plugin_init_i2c_sensors (void); -void plugin_exit_i2c_sensors (void); -int plugin_init_imon(void); +int plugin_init_apm(void); +void plugin_exit_apm(void); +int plugin_init_cpuinfo(void); +void plugin_exit_cpuinfo(void); +int plugin_init_diskstats(void); +void plugin_exit_diskstats(void); +int plugin_init_dvb(void); +void plugin_exit_dvb(void); +int plugin_init_exec(void); +void plugin_exit_exec(void); +int plugin_init_i2c_sensors(void); +void plugin_exit_i2c_sensors(void); +int plugin_init_imon(void); void plugin_exit_imon(void); -int plugin_init_isdn(void); +int plugin_init_isdn(void); void plugin_exit_isdn(void); -int plugin_init_loadavg (void); -void plugin_exit_loadavg (void); -int plugin_init_meminfo (void); -void plugin_exit_meminfo (void); -int plugin_init_mysql(void); +int plugin_init_loadavg(void); +void plugin_exit_loadavg(void); +int plugin_init_meminfo(void); +void plugin_exit_meminfo(void); +int plugin_init_mysql(void); void plugin_exit_mysql(void); -int plugin_init_netdev (void); -void plugin_exit_netdev (void); -int plugin_init_pop3 (void); -void plugin_exit_pop3 (void); -int plugin_init_ppp (void); -void plugin_exit_ppp (void); -int plugin_init_proc_stat (void); -void plugin_exit_proc_stat (void); -int plugin_init_python (void); -void plugin_exit_python (void); -int plugin_init_seti(void); +int plugin_init_netdev(void); +void plugin_exit_netdev(void); +int plugin_init_pop3(void); +void plugin_exit_pop3(void); +int plugin_init_ppp(void); +void plugin_exit_ppp(void); +int plugin_init_proc_stat(void); +void plugin_exit_proc_stat(void); +int plugin_init_python(void); +void plugin_exit_python(void); +int plugin_init_seti(void); void plugin_exit_seti(void); -int plugin_init_statfs(void); +int plugin_init_statfs(void); void plugin_exit_statfs(void); -int plugin_init_uname (void); -void plugin_exit_uname (void); -int plugin_init_uptime (void); -void plugin_exit_uptime (void); -int plugin_init_wireless(void); +int plugin_init_uname(void); +void plugin_exit_uname(void); +int plugin_init_uptime(void); +void plugin_exit_uptime(void); +int plugin_init_wireless(void); void plugin_exit_wireless(void); -int plugin_init_xmms (void); -void plugin_exit_xmms (void); +int plugin_init_xmms(void); +void plugin_exit_xmms(void); -int plugin_init (void) +int plugin_init(void) { - plugin_init_cfg(); - plugin_init_math(); - plugin_init_string(); - plugin_init_test(); - plugin_init_time(); - + plugin_init_cfg(); + plugin_init_math(); + plugin_init_string(); + plugin_init_test(); + plugin_init_time(); + #ifdef PLUGIN_APM - plugin_init_apm(); + plugin_init_apm(); #endif #ifdef PLUGIN_CPUINFO - plugin_init_cpuinfo(); + plugin_init_cpuinfo(); #endif #ifdef PLUGIN_DISKSTATS - plugin_init_diskstats(); + plugin_init_diskstats(); #endif #ifdef PLUGIN_DVB - plugin_init_dvb(); + plugin_init_dvb(); #endif #ifdef PLUGIN_EXEC - plugin_init_exec(); + plugin_init_exec(); #endif #ifdef PLUGIN_I2C_SENSORS - plugin_init_i2c_sensors(); + plugin_init_i2c_sensors(); #endif #ifdef PLUGIN_IMON - plugin_init_imon(); + plugin_init_imon(); #endif #ifdef PLUGIN_ISDN - plugin_init_isdn(); + plugin_init_isdn(); #endif #ifdef PLUGIN_LOADAVG - plugin_init_loadavg(); + plugin_init_loadavg(); #endif #ifdef PLUGIN_MEMINFO - plugin_init_meminfo(); + plugin_init_meminfo(); #endif #ifdef PLUGIN_MYSQL - plugin_init_mysql(); + plugin_init_mysql(); #endif #ifdef PLUGIN_NETDEV - plugin_init_netdev(); + plugin_init_netdev(); #endif #ifdef PLUGIN_POP3 - plugin_init_pop3(); + plugin_init_pop3(); #endif #ifdef PLUGIN_PPP - plugin_init_ppp(); + plugin_init_ppp(); #endif #ifdef PLUGIN_PROC_STAT - plugin_init_proc_stat(); + plugin_init_proc_stat(); #endif #ifdef PLUGIN_PYTHON - plugin_init_python(); + plugin_init_python(); #endif #ifdef PLUGIN_SETI - plugin_init_seti(); + plugin_init_seti(); #endif #ifdef PLUGIN_STATFS - plugin_init_statfs(); + plugin_init_statfs(); #endif #ifdef PLUGIN_UNAME - plugin_init_uname(); + plugin_init_uname(); #endif #ifdef PLUGIN_UPTIME - plugin_init_uptime(); + plugin_init_uptime(); #endif #ifdef PLUGIN_WIRELESS - plugin_init_wireless(); + plugin_init_wireless(); #endif #ifdef PLUGIN_XMMS - plugin_init_xmms(); + plugin_init_xmms(); #endif - return 0; + return 0; } -void plugin_exit(void) { +void plugin_exit(void) +{ #ifdef PLUGIN_APM - plugin_exit_apm(); + plugin_exit_apm(); #endif #ifdef PLUGIN_CPUINFO - plugin_exit_cpuinfo(); + plugin_exit_cpuinfo(); #endif #ifdef PLUGIN_DISKSTATS - plugin_exit_diskstats(); + plugin_exit_diskstats(); #endif #ifdef PLUGIN_DVB - plugin_exit_dvb(); + plugin_exit_dvb(); #endif #ifdef PLUGIN_EXEC - plugin_exit_exec(); + plugin_exit_exec(); #endif #ifdef PLUGIN_I2C_SENSORS - plugin_exit_i2c_sensors(); + plugin_exit_i2c_sensors(); #endif #ifdef PLUGIN_IMON - plugin_exit_imon(); + plugin_exit_imon(); #endif #ifdef PLUGIN_ISDN - plugin_exit_isdn(); + plugin_exit_isdn(); #endif #ifdef PLUGIN_LOADAVG - plugin_exit_loadavg(); + plugin_exit_loadavg(); #endif #ifdef PLUGIN_MEMINFO - plugin_exit_meminfo(); + plugin_exit_meminfo(); #endif #ifdef PLUGIN_MYSQL - plugin_exit_mysql(); + plugin_exit_mysql(); #endif #ifdef PLUGIN_NETDEV - plugin_exit_netdev(); + plugin_exit_netdev(); #endif #ifdef PLUGIN_POP3 - plugin_exit_pop3(); + plugin_exit_pop3(); #endif #ifdef PLUGIN_PPP - plugin_exit_ppp(); + plugin_exit_ppp(); #endif #ifdef PLUGIN_PROC_STAT - plugin_exit_proc_stat(); + plugin_exit_proc_stat(); #endif #ifdef PLUGIN_PYTHON - plugin_exit_python(); + plugin_exit_python(); #endif #ifdef PLUGIN_SETI - plugin_exit_seti(); + plugin_exit_seti(); #endif #ifdef PLUGIN_STATFS - plugin_exit_statfs(); + plugin_exit_statfs(); #endif #ifdef PLUGIN_UNAME - plugin_exit_uname(); + plugin_exit_uname(); #endif #ifdef PLUGIN_UPTIME - plugin_exit_uptime(); + plugin_exit_uptime(); #endif #ifdef PLUGIN_WIRELESS - plugin_exit_wireless(); + plugin_exit_wireless(); #endif #ifdef PLUGIN_XMMS - plugin_exit_xmms(); + plugin_exit_xmms(); #endif - plugin_exit_cfg(); - plugin_exit_math(); - plugin_exit_string(); - plugin_exit_test(); - plugin_exit_time(); + plugin_exit_cfg(); + plugin_exit_math(); + plugin_exit_string(); + plugin_exit_test(); + plugin_exit_time(); - DeleteFunctions(); - DeleteVariables(); + DeleteFunctions(); + DeleteVariables(); } @@ -1,4 +1,4 @@ -/* $Id: plugin.h,v 1.3 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin.h,v 1.4 2005/05/08 04:32:44 reinelt Exp $ * * plugin handler for the Evaluator * @@ -23,6 +23,9 @@ * * * $Log: plugin.h,v $ + * Revision 1.4 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.3 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -54,6 +57,6 @@ #ifndef _PLUGIN_H_ #define _PLUGIN_H_ -int plugin_init (void); +int plugin_init(void); void plugin_exit(void); #endif diff --git a/plugin_apm.c b/plugin_apm.c index 293bc6a..5dffa06 100644 --- a/plugin_apm.c +++ b/plugin_apm.c @@ -1,4 +1,4 @@ -/* $Id: plugin_apm.c,v 1.5 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_apm.c,v 1.6 2005/05/08 04:32:44 reinelt Exp $ * * plugin for APM (battery status) * @@ -26,6 +26,9 @@ * * * $Log: plugin_apm.c,v $ + * Revision 1.6 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.5 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -130,87 +133,92 @@ static HASH APM; */ -static int parse_proc_apm (void) +static int parse_proc_apm(void) { - char *key[] = { "driver_version", - "bios_version", - "bios_flags", - "line_status", - "battery_status", - "battery_flag", - "battery_percent", - "battery_remaining", - "time_units" }; - - char buffer[128], *beg, *end; - int age, i; - - /* reread every 10 msec only */ - age = hash_age (&APM, NULL); - if (age > 0 && age <= 10) return 0; - - if (fd == -2) { - fd = open("/proc/apm", O_RDONLY | O_NDELAY); - if (fd == -1) { - error ("open(/proc/apm) failed: %s", strerror(errno)); - return -1; + char *key[] = { "driver_version", + "bios_version", + "bios_flags", + "line_status", + "battery_status", + "battery_flag", + "battery_percent", + "battery_remaining", + "time_units" + }; + + char buffer[128], *beg, *end; + int age, i; + + /* reread every 10 msec only */ + age = hash_age(&APM, NULL); + if (age > 0 && age <= 10) + return 0; + + if (fd == -2) { + fd = open("/proc/apm", O_RDONLY | O_NDELAY); + if (fd == -1) { + error("open(/proc/apm) failed: %s", strerror(errno)); + return -1; + } + } + + if (lseek(fd, 0L, SEEK_SET) != 0) { + error("lseek(/proc/apm) failed: %s", strerror(errno)); + fd = -1; + return -1; + } + + if (read(fd, &buffer, sizeof(buffer) - 1) == -1) { + error("read(/proc/apm) failed: %s", strerror(errno)); + fd = -1; + return -1; + } + + beg = buffer; + for (i = 0; i < 9 && beg != NULL; i++) { + while (*beg == ' ') + beg++; + if ((end = strpbrk(beg, " \n"))) + *end = '\0'; + hash_put(&APM, key[i], beg); + beg = end ? end + 1 : NULL; } - } - - if (lseek(fd, 0L, SEEK_SET) != 0) { - error ("lseek(/proc/apm) failed: %s", strerror(errno)); - fd = -1; - return -1; - } - - if (read (fd, &buffer, sizeof(buffer)-1) == -1) { - error ("read(/proc/apm) failed: %s", strerror(errno)); - fd=-1; - return -1; - } - - beg = buffer; - for (i = 0; i < 9 && beg != NULL; i++) { - while (*beg == ' ') beg++; - if ((end = strpbrk(beg, " \n"))) *end='\0'; - hash_put (&APM, key[i], beg); - beg = end ? end+1 : NULL; - } - - return 0; + + return 0; } -static void my_apm (RESULT *result, RESULT *arg1) +static void my_apm(RESULT * result, RESULT * arg1) { - char *val; - - if (parse_proc_apm() < 0) { - SetResult(&result, R_STRING, ""); - return; - } + char *val; + + if (parse_proc_apm() < 0) { + SetResult(&result, R_STRING, ""); + return; + } - val = hash_get(&APM, R2S(arg1), NULL); - if (val == NULL) val = ""; + val = hash_get(&APM, R2S(arg1), NULL); + if (val == NULL) + val = ""; - SetResult(&result, R_STRING, val); + SetResult(&result, R_STRING, val); } -int plugin_init_apm (void) +int plugin_init_apm(void) { - hash_create (&APM); - - AddFunction ("apm", 1, my_apm); + hash_create(&APM); + + AddFunction("apm", 1, my_apm); - return 0; + return 0; } -void plugin_exit_apm (void) +void plugin_exit_apm(void) { - if (fd > -1) { - close (fd); - } - fd = -2; + if (fd > -1) { + close(fd); + } + fd = -2; - hash_destroy(&APM); + hash_destroy(&APM); } diff --git a/plugin_cfg.c b/plugin_cfg.c index 2996045..323c04d 100644 --- a/plugin_cfg.c +++ b/plugin_cfg.c @@ -1,4 +1,4 @@ -/* $Id: plugin_cfg.c,v 1.13 2005/03/30 04:57:50 reinelt Exp $ +/* $Id: plugin_cfg.c,v 1.14 2005/05/08 04:32:44 reinelt Exp $ * * plugin for config file access * @@ -23,6 +23,9 @@ * * * $Log: plugin_cfg.c,v $ + * Revision 1.14 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.13 2005/03/30 04:57:50 reinelt * Evaluator speedup: use bsearch for finding functions and variables * @@ -111,89 +114,91 @@ #endif -static void load_variables (void) +static void load_variables(void) { - char *section = "Variables"; - char *list, *l, *p; - char *expression; - void *tree; - RESULT result = {0, 0, 0, NULL}; - - list=cfg_list(section); - l=list; - while (l!=NULL) { - while (*l=='|') l++; - if ((p=strchr(l, '|'))!=NULL) *p='\0'; - if (strchr(l, '.')!=NULL || strchr(l, ':') !=0) { - error ("ignoring variable '%s' from %s: structures not allowed", l, cfg_source()); - } else { - expression=cfg_get_raw (section, l, ""); - if (expression!=NULL && *expression!='\0') { - tree = NULL; - if (Compile(expression, &tree) == 0 && Eval(tree, &result)==0) { - SetVariable (l, &result); - debug ("Variable %s = '%s' (%g)", l, R2S(&result), R2N(&result)); - DelResult (&result); - } else { - error ("error evaluating variable '%s' from %s", list, cfg_source()); - } - DelTree (tree); - } + char *section = "Variables"; + char *list, *l, *p; + char *expression; + void *tree; + RESULT result = { 0, 0, 0, NULL }; + + list = cfg_list(section); + l = list; + while (l != NULL) { + while (*l == '|') + l++; + if ((p = strchr(l, '|')) != NULL) + *p = '\0'; + if (strchr(l, '.') != NULL || strchr(l, ':') != 0) { + error("ignoring variable '%s' from %s: structures not allowed", l, cfg_source()); + } else { + expression = cfg_get_raw(section, l, ""); + if (expression != NULL && *expression != '\0') { + tree = NULL; + if (Compile(expression, &tree) == 0 && Eval(tree, &result) == 0) { + SetVariable(l, &result); + debug("Variable %s = '%s' (%g)", l, R2S(&result), R2N(&result)); + DelResult(&result); + } else { + error("error evaluating variable '%s' from %s", list, cfg_source()); + } + DelTree(tree); + } + } + l = p ? p + 1 : NULL; } - l=p?p+1:NULL; - } - free (list); - + free(list); + } -static void my_cfg (RESULT *result, const int argc, RESULT *argv[]) +static void my_cfg(RESULT * result, const int argc, RESULT * argv[]) { - int i, len; - char *value; - char *buffer; - - /* calculate key length */ - len=0; - for (i=0; i<argc; i++) { - len+=strlen(R2S(argv[i]))+1; - } - - /* allocate key buffer */ - buffer=malloc(len+1); - - /* prepare key buffer */ - *buffer='\0'; - for (i=0; i<argc; i++) { - strcat (buffer, "."); - strcat (buffer, R2S(argv[i])); - } - - /* buffer starts with '.', so cut off first char */ - value=cfg_get("", buffer+1, ""); - - /* store result */ - SetResult(&result, R_STRING, value); - - /* free buffer again */ - free (buffer); - - free(value); + int i, len; + char *value; + char *buffer; + + /* calculate key length */ + len = 0; + for (i = 0; i < argc; i++) { + len += strlen(R2S(argv[i])) + 1; + } + + /* allocate key buffer */ + buffer = malloc(len + 1); + + /* prepare key buffer */ + *buffer = '\0'; + for (i = 0; i < argc; i++) { + strcat(buffer, "."); + strcat(buffer, R2S(argv[i])); + } + + /* buffer starts with '.', so cut off first char */ + value = cfg_get("", buffer + 1, ""); + + /* store result */ + SetResult(&result, R_STRING, value); + + /* free buffer again */ + free(buffer); + + free(value); } -int plugin_init_cfg (void) +int plugin_init_cfg(void) { - /* load "Variables" section from cfg */ - load_variables(); + /* load "Variables" section from cfg */ + load_variables(); + + /* register plugin */ + AddFunction("cfg", -1, my_cfg); - /* register plugin */ - AddFunction ("cfg", -1, my_cfg); - - return 0; + return 0; } -void plugin_exit_cfg(void) +void plugin_exit_cfg(void) { - /* empty */ + /* empty */ } diff --git a/plugin_cpuinfo.c b/plugin_cpuinfo.c index 3d9d9e9..e091383 100644 --- a/plugin_cpuinfo.c +++ b/plugin_cpuinfo.c @@ -1,4 +1,4 @@ -/* $Id: plugin_cpuinfo.c,v 1.13 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_cpuinfo.c,v 1.14 2005/05/08 04:32:44 reinelt Exp $ * * plugin for /proc/cpuinfo parsing * @@ -23,6 +23,9 @@ * * * $Log: plugin_cpuinfo.c,v $ + * Revision 1.14 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.13 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -111,74 +114,84 @@ static HASH CPUinfo; static FILE *stream = NULL; -static int parse_cpuinfo (void) +static int parse_cpuinfo(void) { - int age; - - /* reread every second only */ - age = hash_age(&CPUinfo, NULL); - if (age > 0 && age <= 1000) return 0; - - if (stream == NULL) stream = fopen("/proc/cpuinfo", "r"); - if (stream == NULL) { - error ("fopen(/proc/cpuinfo) failed: %s", strerror(errno)); - return -1; - } - rewind(stream); - while (!feof(stream)) { - char buffer[256]; - char *c, *key, *val; - fgets (buffer, sizeof(buffer), stream); - c = strchr(buffer, ':'); - if (c == NULL) continue; - key = buffer; val = c+1; - /* strip leading blanks from key */ - while (isspace(*key)) *key++ = '\0'; - /* strip trailing blanks from key */ - do *c = '\0'; while (isspace(*--c)); - /* strip leading blanks from value */ - while (isspace(*val)) *val++ = '\0'; - /* strip trailing blanks from value */ - for (c = val; *c != '\0'; c++); - while (isspace(*--c)) *c = '\0'; - - /* add entry to hash table */ - hash_put (&CPUinfo, key, val); - - } - return 0; + int age; + + /* reread every second only */ + age = hash_age(&CPUinfo, NULL); + if (age > 0 && age <= 1000) + return 0; + + if (stream == NULL) + stream = fopen("/proc/cpuinfo", "r"); + if (stream == NULL) { + error("fopen(/proc/cpuinfo) failed: %s", strerror(errno)); + return -1; + } + rewind(stream); + while (!feof(stream)) { + char buffer[256]; + char *c, *key, *val; + fgets(buffer, sizeof(buffer), stream); + c = strchr(buffer, ':'); + if (c == NULL) + continue; + key = buffer; + val = c + 1; + /* strip leading blanks from key */ + while (isspace(*key)) + *key++ = '\0'; + /* strip trailing blanks from key */ + do + *c = '\0'; + while (isspace(*--c)); + /* strip leading blanks from value */ + while (isspace(*val)) + *val++ = '\0'; + /* strip trailing blanks from value */ + for (c = val; *c != '\0'; c++); + while (isspace(*--c)) + *c = '\0'; + + /* add entry to hash table */ + hash_put(&CPUinfo, key, val); + + } + return 0; } - -static void my_cpuinfo (RESULT *result, RESULT *arg1) + +static void my_cpuinfo(RESULT * result, RESULT * arg1) { - char *key, *val; - - if (parse_cpuinfo() < 0) { - SetResult(&result, R_STRING, ""); - return; - } - - key = R2S(arg1); - val = hash_get(&CPUinfo, key, NULL); - if (val == NULL) val = ""; - - SetResult(&result, R_STRING, val); + char *key, *val; + + if (parse_cpuinfo() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + key = R2S(arg1); + val = hash_get(&CPUinfo, key, NULL); + if (val == NULL) + val = ""; + + SetResult(&result, R_STRING, val); } -int plugin_init_cpuinfo (void) +int plugin_init_cpuinfo(void) { - hash_create (&CPUinfo); - AddFunction ("cpuinfo", 1, my_cpuinfo); - return 0; + hash_create(&CPUinfo); + AddFunction("cpuinfo", 1, my_cpuinfo); + return 0; } -void plugin_exit_cpuinfo(void) +void plugin_exit_cpuinfo(void) { - if (stream != NULL) { - fclose (stream); - stream = NULL; - } - hash_destroy(&CPUinfo); + if (stream != NULL) { + fclose(stream); + stream = NULL; + } + hash_destroy(&CPUinfo); } diff --git a/plugin_diskstats.c b/plugin_diskstats.c index 3e0ee50..fae74f1 100644 --- a/plugin_diskstats.c +++ b/plugin_diskstats.c @@ -1,4 +1,4 @@ -/* $Id: plugin_diskstats.c,v 1.7 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_diskstats.c,v 1.8 2005/05/08 04:32:44 reinelt Exp $ * * plugin for /proc/diskstats parsing * @@ -23,6 +23,9 @@ * * * $Log: plugin_diskstats.c,v $ + * Revision 1.8 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.7 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -79,98 +82,106 @@ static HASH DISKSTATS; static FILE *stream = NULL; -static int parse_diskstats (void) +static int parse_diskstats(void) { - int age; - - /* reread every 10 msec only */ - age = hash_age(&DISKSTATS, NULL); - if (age > 0 && age <= 10) return 0; - - if (stream == NULL) stream = fopen("/proc/diskstats", "r"); - if (stream == NULL) { - error ("fopen(/proc/diskstats) failed: %s", strerror(errno)); - return -1; - } - - rewind(stream); - - while (!feof(stream)) { - char buffer[1024]; - char dev[64]; - char *beg, *end; - unsigned int num, len; - - if (fgets (buffer, sizeof(buffer), stream) == NULL) break; - - /* fetch device name (3rd column) as key */ - num = 0; - beg = buffer; - end = beg; - while (*beg) { - while (*beg == ' ') beg++; - end = beg + 1; - while (*end && *end != ' ') end++; - if (num++ == 2) break; - beg = end ? end+1 : NULL; + int age; + + /* reread every 10 msec only */ + age = hash_age(&DISKSTATS, NULL); + if (age > 0 && age <= 10) + return 0; + + if (stream == NULL) + stream = fopen("/proc/diskstats", "r"); + if (stream == NULL) { + error("fopen(/proc/diskstats) failed: %s", strerror(errno)); + return -1; + } + + rewind(stream); + + while (!feof(stream)) { + char buffer[1024]; + char dev[64]; + char *beg, *end; + unsigned int num, len; + + if (fgets(buffer, sizeof(buffer), stream) == NULL) + break; + + /* fetch device name (3rd column) as key */ + num = 0; + beg = buffer; + end = beg; + while (*beg) { + while (*beg == ' ') + beg++; + end = beg + 1; + while (*end && *end != ' ') + end++; + if (num++ == 2) + break; + beg = end ? end + 1 : NULL; + } + len = end ? (unsigned) (end - beg) : strlen(beg); + + if (len >= sizeof(dev)) + len = sizeof(dev) - 1; + strncpy(dev, beg, len); + dev[len] = '\0'; + + hash_put_delta(&DISKSTATS, dev, buffer); + } - len = end ? (unsigned)(end - beg) : strlen(beg); - - if (len >= sizeof(dev)) len = sizeof(dev)-1; - strncpy (dev, beg, len); - dev[len] = '\0'; - - hash_put_delta (&DISKSTATS, dev, buffer); - - } - return 0; + return 0; } -static void my_diskstats (RESULT *result, RESULT *arg1, RESULT *arg2, RESULT *arg3) +static void my_diskstats(RESULT * result, RESULT * arg1, RESULT * arg2, RESULT * arg3) { - char *dev, *key; - int delay; - double value; - - if (parse_diskstats() < 0) { - SetResult(&result, R_STRING, ""); - return; - } - - dev = R2S(arg1); - key = R2S(arg2); - delay = R2N(arg3); - - value = hash_get_regex(&DISKSTATS, dev, key, delay); - - SetResult(&result, R_NUMBER, &value); + char *dev, *key; + int delay; + double value; + + if (parse_diskstats() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + dev = R2S(arg1); + key = R2S(arg2); + delay = R2N(arg3); + + value = hash_get_regex(&DISKSTATS, dev, key, delay); + + SetResult(&result, R_NUMBER, &value); } -int plugin_init_diskstats (void) +int plugin_init_diskstats(void) { - int i; - char *header[] = { "major", "minor", "name", - "reads", "read_merges", "read_sectors", "read_ticks", - "writes", "write_merges", "write_sectors", "write_ticks", - "in_flight", "io_ticks", "time_in_queue", "" }; - - hash_create (&DISKSTATS); - hash_set_delimiter (&DISKSTATS, " \n"); - for (i=0; *header[i] != '\0'; i++) { - hash_set_column (&DISKSTATS, i, header[i]); - } - - AddFunction ("diskstats", 3, my_diskstats); - return 0; + int i; + char *header[] = { "major", "minor", "name", + "reads", "read_merges", "read_sectors", "read_ticks", + "writes", "write_merges", "write_sectors", "write_ticks", + "in_flight", "io_ticks", "time_in_queue", "" + }; + + hash_create(&DISKSTATS); + hash_set_delimiter(&DISKSTATS, " \n"); + for (i = 0; *header[i] != '\0'; i++) { + hash_set_column(&DISKSTATS, i, header[i]); + } + + AddFunction("diskstats", 3, my_diskstats); + return 0; } -void plugin_exit_diskstats(void) +void plugin_exit_diskstats(void) { - if (stream != NULL) { - fclose (stream); - stream = NULL; - } - hash_destroy(&DISKSTATS); + if (stream != NULL) { + fclose(stream); + stream = NULL; + } + hash_destroy(&DISKSTATS); } diff --git a/plugin_dvb.c b/plugin_dvb.c index 7e42254..5042df3 100644 --- a/plugin_dvb.c +++ b/plugin_dvb.c @@ -1,4 +1,4 @@ -/* $Id: plugin_dvb.c,v 1.8 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_dvb.c,v 1.9 2005/05/08 04:32:44 reinelt Exp $ * * plugin for DVB status * @@ -23,6 +23,9 @@ * * * $Log: plugin_dvb.c,v $ + * Revision 1.9 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.8 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -93,92 +96,94 @@ #include "plugin.h" #include "hash.h" -static char *frontend="/dev/dvb/adapter0/frontend0"; +static char *frontend = "/dev/dvb/adapter0/frontend0"; static HASH DVB; -static int get_dvb_stats (void) +static int get_dvb_stats(void) { - int age; - int fd; - unsigned short snr, sig; - unsigned long ber, ucb; - char val[16]; - - /* reread every 1000 msec only */ - age = hash_age(&DVB, NULL); - if (age > 0 && age <= 1000) return 0; - - /* open frontend */ - fd = open(frontend, O_RDONLY); - if (fd == -1) { - error ("open(%s) failed: %s", frontend, strerror(errno)); - return -1; - } - - if (ioctl(fd, FE_READ_SIGNAL_STRENGTH, &sig) != 0) { - error("ioctl(FE_READ_SIGNAL_STRENGTH) failed: %s", strerror(errno)); - sig = 0; - } - - if (ioctl(fd, FE_READ_SNR, &snr) != 0) { - error("ioctl(FE_READ_SNR) failed: %s", strerror(errno)); - snr = 0; - } - - if (ioctl(fd, FE_READ_BER, &ber) != 0) { - error("ioctl(FE_READ_BER) failed: %s", strerror(errno)); - ber = 0; - } - - if (ioctl(fd, FE_READ_UNCORRECTED_BLOCKS, &ucb) != 0) { - error("ioctl(FE_READ_UNCORRECTED_BLOCKS) failed: %s", strerror(errno)); - ucb = 0; - } - - close (fd); - - snprintf (val, sizeof(val), "%f", sig/65535.0); - hash_put (&DVB, "signal_strength", val); - - snprintf (val, sizeof(val), "%f", snr/65535.0); - hash_put (&DVB, "snr", val); - - snprintf (val, sizeof(val), "%lu", ber); - hash_put (&DVB, "ber", val); - - snprintf (val, sizeof(val), "%lu", ucb); - hash_put (&DVB, "uncorrected_blocks", val); - - return 0; + int age; + int fd; + unsigned short snr, sig; + unsigned long ber, ucb; + char val[16]; + + /* reread every 1000 msec only */ + age = hash_age(&DVB, NULL); + if (age > 0 && age <= 1000) + return 0; + + /* open frontend */ + fd = open(frontend, O_RDONLY); + if (fd == -1) { + error("open(%s) failed: %s", frontend, strerror(errno)); + return -1; + } + + if (ioctl(fd, FE_READ_SIGNAL_STRENGTH, &sig) != 0) { + error("ioctl(FE_READ_SIGNAL_STRENGTH) failed: %s", strerror(errno)); + sig = 0; + } + + if (ioctl(fd, FE_READ_SNR, &snr) != 0) { + error("ioctl(FE_READ_SNR) failed: %s", strerror(errno)); + snr = 0; + } + + if (ioctl(fd, FE_READ_BER, &ber) != 0) { + error("ioctl(FE_READ_BER) failed: %s", strerror(errno)); + ber = 0; + } + + if (ioctl(fd, FE_READ_UNCORRECTED_BLOCKS, &ucb) != 0) { + error("ioctl(FE_READ_UNCORRECTED_BLOCKS) failed: %s", strerror(errno)); + ucb = 0; + } + + close(fd); + + snprintf(val, sizeof(val), "%f", sig / 65535.0); + hash_put(&DVB, "signal_strength", val); + + snprintf(val, sizeof(val), "%f", snr / 65535.0); + hash_put(&DVB, "snr", val); + + snprintf(val, sizeof(val), "%lu", ber); + hash_put(&DVB, "ber", val); + + snprintf(val, sizeof(val), "%lu", ucb); + hash_put(&DVB, "uncorrected_blocks", val); + + return 0; } -static void my_dvb (RESULT *result, RESULT *arg1) +static void my_dvb(RESULT * result, RESULT * arg1) { - char *val; - - if (get_dvb_stats()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - val=hash_get(&DVB, R2S(arg1), NULL); - if (val==NULL) val=""; - - SetResult(&result, R_STRING, val); + char *val; + + if (get_dvb_stats() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + val = hash_get(&DVB, R2S(arg1), NULL); + if (val == NULL) + val = ""; + + SetResult(&result, R_STRING, val); } -int plugin_init_dvb (void) +int plugin_init_dvb(void) { - hash_create(&DVB); - AddFunction ("dvb", 1, my_dvb); - return 0; + hash_create(&DVB); + AddFunction("dvb", 1, my_dvb); + return 0; } -void plugin_exit_dvb(void) +void plugin_exit_dvb(void) { - hash_destroy(&DVB); + hash_destroy(&DVB); } diff --git a/plugin_exec.c b/plugin_exec.c index 8b4af36..f9576e2 100644 --- a/plugin_exec.c +++ b/plugin_exec.c @@ -1,4 +1,4 @@ -/* $Id: plugin_exec.c,v 1.8 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_exec.c,v 1.9 2005/05/08 04:32:44 reinelt Exp $ * * plugin for external processes * @@ -27,6 +27,9 @@ * * * $Log: plugin_exec.c,v $ + * Revision 1.9 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.8 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -90,13 +93,13 @@ #define SHM_SIZE 4096 typedef struct { - int delay; - int mutex; - pid_t pid; - int shmid; - char *cmd; - char *key; - char *ret; + int delay; + int mutex; + pid_t pid; + int shmid; + char *cmd; + char *key; + char *ret; } EXEC_THREAD; static EXEC_THREAD Thread[NUM_THREADS]; @@ -106,204 +109,210 @@ static HASH EXEC; /* x^0 + x^5 + x^12 */ -#define CRCPOLY 0x8408 - -static unsigned short CRC (const char *s) +#define CRCPOLY 0x8408 + +static unsigned short CRC(const char *s) { - int i; - unsigned short crc; - - /* seed value */ - crc = 0xffff; - - while (*s != '\0') { - crc ^= *s++; - for (i = 0; i < 8; i++) - crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY : 0); - } - return crc; + int i; + unsigned short crc; + + /* seed value */ + crc = 0xffff; + + while (*s != '\0') { + crc ^= *s++; + for (i = 0; i < 8; i++) + crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY : 0); + } + return crc; } -static void exec_thread (void *data) +static void exec_thread(void *data) { - EXEC_THREAD *Thread = (EXEC_THREAD*)data; - FILE *pipe; - char buffer[SHM_SIZE]; - int len; - - /* use a safe path */ - putenv ("PATH=/usr/local/bin:/usr/bin:/bin"); - - /* forever... */ - while (1) { - pipe = popen(Thread->cmd, "r"); - if (pipe == NULL) { - error("exec error: could not run pipe '%s': %s", Thread->cmd, strerror(errno)); - len = 0; - } else { - len = fread(buffer, 1, SHM_SIZE-1, pipe); - if (len <= 0) { - error("exec error: could not read from pipe '%s': %s", Thread->cmd, strerror(errno)); - len = 0; - } - pclose(pipe); - } - - /* force trailing zero */ - buffer[len] = '\0'; - - /* remove trailing CR/LF */ - while (len>0 && (buffer[len-1]=='\n' || buffer[len-1]=='\r')) { - buffer[--len]='\0'; + EXEC_THREAD *Thread = (EXEC_THREAD *) data; + FILE *pipe; + char buffer[SHM_SIZE]; + int len; + + /* use a safe path */ + putenv("PATH=/usr/local/bin:/usr/bin:/bin"); + + /* forever... */ + while (1) { + pipe = popen(Thread->cmd, "r"); + if (pipe == NULL) { + error("exec error: could not run pipe '%s': %s", Thread->cmd, strerror(errno)); + len = 0; + } else { + len = fread(buffer, 1, SHM_SIZE - 1, pipe); + if (len <= 0) { + error("exec error: could not read from pipe '%s': %s", Thread->cmd, strerror(errno)); + len = 0; + } + pclose(pipe); + } + + /* force trailing zero */ + buffer[len] = '\0'; + + /* remove trailing CR/LF */ + while (len > 0 && (buffer[len - 1] == '\n' || buffer[len - 1] == '\r')) { + buffer[--len] = '\0'; + } + + /* lock shared memory */ + mutex_lock(Thread->mutex); + /* write data */ + strncpy(Thread->ret, buffer, SHM_SIZE); + /* unlock shared memory */ + mutex_unlock(Thread->mutex); + usleep(Thread->delay); } - - /* lock shared memory */ - mutex_lock(Thread->mutex); - /* write data */ - strncpy(Thread->ret, buffer, SHM_SIZE); - /* unlock shared memory */ - mutex_unlock(Thread->mutex); - usleep (Thread->delay); - } } -static void destroy_exec_thread (const int n) +static void destroy_exec_thread(const int n) { - if (Thread[n].mutex != 0) mutex_destroy(Thread[n].mutex); - if (Thread[n].cmd) free (Thread[n].cmd); - if (Thread[n].key) free (Thread[n].key); - if (Thread[n].ret) shm_destroy (Thread[n].shmid, Thread[n].ret); - - Thread[n].delay = 0; - Thread[n].mutex = 0; - Thread[n].pid = 0; - Thread[n].shmid = 0; - Thread[n].cmd = NULL; - Thread[n].key = NULL; - Thread[n].ret = NULL; + if (Thread[n].mutex != 0) + mutex_destroy(Thread[n].mutex); + if (Thread[n].cmd) + free(Thread[n].cmd); + if (Thread[n].key) + free(Thread[n].key); + if (Thread[n].ret) + shm_destroy(Thread[n].shmid, Thread[n].ret); + + Thread[n].delay = 0; + Thread[n].mutex = 0; + Thread[n].pid = 0; + Thread[n].shmid = 0; + Thread[n].cmd = NULL; + Thread[n].key = NULL; + Thread[n].ret = NULL; } -static int create_exec_thread (const char *cmd, const char *key, const int delay) +static int create_exec_thread(const char *cmd, const char *key, const int delay) { - char name[10]; + char name[10]; - if (max_thread >= NUM_THREADS) { - error ("cannot create exec thread <%s>: thread buffer full!", cmd); - return -1; - } - - max_thread++; - Thread[max_thread].delay = delay; - Thread[max_thread].mutex = mutex_create(); - Thread[max_thread].pid = -1; - Thread[max_thread].cmd = strdup(cmd); - Thread[max_thread].key = strdup(key); - Thread[max_thread].ret = NULL; - - /* create communication buffer */ - Thread[max_thread].shmid = shm_create ((void**)&Thread[max_thread].ret, SHM_SIZE); - - /* catch error */ - if (Thread[max_thread].shmid < 0) { - error ("cannot create exec thread <%s>: shared memory allocation failed!", cmd); - destroy_exec_thread (max_thread--); - return -1; - } - - /* create thread */ - qprintf(name, sizeof(name), "exec-%s", key); - Thread[max_thread].pid = thread_create (name, exec_thread, &Thread[max_thread]); - - /* catch error */ - if (Thread[max_thread].pid < 0) { - error ("cannot create exec thread <%s>: fork failed?!", cmd); - destroy_exec_thread (max_thread--); - return -1; - } - - return 0; -} + if (max_thread >= NUM_THREADS) { + error("cannot create exec thread <%s>: thread buffer full!", cmd); + return -1; + } + max_thread++; + Thread[max_thread].delay = delay; + Thread[max_thread].mutex = mutex_create(); + Thread[max_thread].pid = -1; + Thread[max_thread].cmd = strdup(cmd); + Thread[max_thread].key = strdup(key); + Thread[max_thread].ret = NULL; -static int do_exec (const char *cmd, const char *key, int delay) -{ - int i, age; - - age = hash_age(&EXEC, key); - - if (age < 0) { - hash_put (&EXEC, key, ""); - /* first-time call: create thread */ - if (delay < 10) { - error ("exec(%s): delay %d is too short! using 10 msec", cmd, delay); - delay = 10; + /* create communication buffer */ + Thread[max_thread].shmid = shm_create((void **) &Thread[max_thread].ret, SHM_SIZE); + + /* catch error */ + if (Thread[max_thread].shmid < 0) { + error("cannot create exec thread <%s>: shared memory allocation failed!", cmd); + destroy_exec_thread(max_thread--); + return -1; } - if (create_exec_thread (cmd, key, 1000*delay)) { - return -1; + + /* create thread */ + qprintf(name, sizeof(name), "exec-%s", key); + Thread[max_thread].pid = thread_create(name, exec_thread, &Thread[max_thread]); + + /* catch error */ + if (Thread[max_thread].pid < 0) { + error("cannot create exec thread <%s>: fork failed?!", cmd); + destroy_exec_thread(max_thread--); + return -1; } + return 0; - } - - /* reread every 10 msec only */ - if (age > 0 && age <= 10) return 0; - - /* find thread */ - for (i = 0; i <= max_thread; i++) { - if (strcmp(key, Thread[i].key) == 0) { - /* lock shared memory */ - mutex_lock(Thread[i].mutex); - /* copy data */ - hash_put (&EXEC, key, Thread[i].ret); - /* unlock shared memory */ - mutex_unlock(Thread[i].mutex); - return 0; +} + + +static int do_exec(const char *cmd, const char *key, int delay) +{ + int i, age; + + age = hash_age(&EXEC, key); + + if (age < 0) { + hash_put(&EXEC, key, ""); + /* first-time call: create thread */ + if (delay < 10) { + error("exec(%s): delay %d is too short! using 10 msec", cmd, delay); + delay = 10; + } + if (create_exec_thread(cmd, key, 1000 * delay)) { + return -1; + } + return 0; } - } - - error ("internal error: could not find thread exec-%s", key); - return -1; + + /* reread every 10 msec only */ + if (age > 0 && age <= 10) + return 0; + + /* find thread */ + for (i = 0; i <= max_thread; i++) { + if (strcmp(key, Thread[i].key) == 0) { + /* lock shared memory */ + mutex_lock(Thread[i].mutex); + /* copy data */ + hash_put(&EXEC, key, Thread[i].ret); + /* unlock shared memory */ + mutex_unlock(Thread[i].mutex); + return 0; + } + } + + error("internal error: could not find thread exec-%s", key); + return -1; } -static void my_exec (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_exec(RESULT * result, RESULT * arg1, RESULT * arg2) { - char *cmd, key[5], *val; - int delay; - - cmd = R2S(arg1); - delay = (int)R2N(arg2); - - qprintf (key, sizeof(key), "%x", CRC(cmd)); - - if (do_exec(cmd, key, delay) < 0) { - SetResult(&result, R_STRING, ""); - return; - } - - val = hash_get(&EXEC, key, NULL); - if (val == NULL) val = ""; - - SetResult(&result, R_STRING, val); + char *cmd, key[5], *val; + int delay; + + cmd = R2S(arg1); + delay = (int) R2N(arg2); + + qprintf(key, sizeof(key), "%x", CRC(cmd)); + + if (do_exec(cmd, key, delay) < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + val = hash_get(&EXEC, key, NULL); + if (val == NULL) + val = ""; + + SetResult(&result, R_STRING, val); } -int plugin_init_exec (void) +int plugin_init_exec(void) { - hash_create(&EXEC); - AddFunction ("exec", 2, my_exec); - return 0; + hash_create(&EXEC); + AddFunction("exec", 2, my_exec); + return 0; } -void plugin_exit_exec(void) +void plugin_exit_exec(void) { - int i; - - for (i=0; i<=max_thread; i++) { - destroy_exec_thread(i); - } - - hash_destroy(&EXEC); + int i; + + for (i = 0; i <= max_thread; i++) { + destroy_exec_thread(i); + } + + hash_destroy(&EXEC); } diff --git a/plugin_i2c_sensors.c b/plugin_i2c_sensors.c index df5e6ab..718f1e4 100644 --- a/plugin_i2c_sensors.c +++ b/plugin_i2c_sensors.c @@ -1,4 +1,4 @@ -/* $Id: plugin_i2c_sensors.c,v 1.23 2005/04/01 05:16:04 reinelt Exp $ +/* $Id: plugin_i2c_sensors.c,v 1.24 2005/05/08 04:32:44 reinelt Exp $ * * I2C sensors plugin * @@ -23,6 +23,9 @@ * * * $Log: plugin_i2c_sensors.c,v $ + * Revision 1.24 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.23 2005/04/01 05:16:04 reinelt * moved plugin init stuff to a seperate function called on first use * @@ -178,17 +181,17 @@ #include <dmalloc.h> #endif -static char *path=NULL; +static char *path = NULL; static HASH I2Csensors; static const char *procfs_tokens[4][3] = { - {"temp_hyst", "temp_max", "temp_input"}, /* for temp# */ - {"in_min", "in_max", "in_input"}, /* for in# */ - {"fan_div1", "fan_div2", "fan_div3"}, /* for fan_div */ - {"fan_min", "fan_input", ""} /* for fan# */ + {"temp_hyst", "temp_max", "temp_input"}, /* for temp# */ + {"in_min", "in_max", "in_input"}, /* for in# */ + {"fan_div1", "fan_div2", "fan_div3"}, /* for fan_div */ + {"fan_min", "fan_input", ""} /* for fan# */ }; -static int (*parse_i2c_sensors)(const char *key); +static int (*parse_i2c_sensors) (const char *key); /***********************************************\ * Parsing for new 2.6 kernels 'sysfs' interface * @@ -196,44 +199,41 @@ static int (*parse_i2c_sensors)(const char *key); static int parse_i2c_sensors_sysfs(const char *key) { - char val[32]; - char buffer[32]; - char file[64]; - FILE *stream; - - strcpy(file, path); - strcat(file, key); - - stream=fopen(file, "r"); - if (stream==NULL) { - error ("i2c_sensors: fopen(%s) failed: %s", file, strerror(errno)); - return -1; - } - fgets (buffer, sizeof(buffer), stream); - fclose (stream); - - if (!buffer) { - error ("i2c_sensors: %s empty ?!", file); - return -1; - } - - /* now the formating stuff, depending on the file : */ - /* Some values must be divided by 1000, the others */ - /* are parsed directly (we just remove the \n). */ - if (!strncmp(key, "temp", 4) || - !strncmp(key, "curr", 4) || - !strncmp(key, "in", 2) || - !strncmp(key, "vid", 3)) { - snprintf(val, sizeof(val), "%f", strtod(buffer, NULL) / 1000.0); - } else { - qprintf(val, sizeof(val), "%s", buffer); - /* we supress this nasty \n at the end */ - val[strlen(val)-1]='\0'; - } - - hash_put (&I2Csensors, key, val); - - return 0; + char val[32]; + char buffer[32]; + char file[64]; + FILE *stream; + + strcpy(file, path); + strcat(file, key); + + stream = fopen(file, "r"); + if (stream == NULL) { + error("i2c_sensors: fopen(%s) failed: %s", file, strerror(errno)); + return -1; + } + fgets(buffer, sizeof(buffer), stream); + fclose(stream); + + if (!buffer) { + error("i2c_sensors: %s empty ?!", file); + return -1; + } + + /* now the formating stuff, depending on the file : */ + /* Some values must be divided by 1000, the others */ + /* are parsed directly (we just remove the \n). */ + if (!strncmp(key, "temp", 4) || !strncmp(key, "curr", 4) || !strncmp(key, "in", 2) || !strncmp(key, "vid", 3)) { + snprintf(val, sizeof(val), "%f", strtod(buffer, NULL) / 1000.0); + } else { + qprintf(val, sizeof(val), "%s", buffer); + /* we supress this nasty \n at the end */ + val[strlen(val) - 1] = '\0'; + } + + hash_put(&I2Csensors, key, val); + + return 0; } @@ -243,69 +243,69 @@ static int parse_i2c_sensors_sysfs(const char *key) static int parse_i2c_sensors_procfs(const char *key) { - char file[64]; - FILE *stream; - char buffer[32]; - - char *value; - char *running; - int pos=0; - const char delim[3]=" \n"; - char final_key[32]; - const char *number = &key[strlen(key)-1]; - int tokens_index; - /* debug("%s -> %s", key, number); */ - strcpy(file, path); - - if (!strncmp(key, "temp_", 5)) { - tokens_index=0; - strcat(file, "temp"); - strcat(file, number); - } else if (!strncmp(key, "in_", 3)) { - tokens_index=1; - strcat(file, "in"); - strcat(file, number); - } else if (!strncmp(key, "fan_div", 7)) { - tokens_index=2; - strcat(file, "fan_div"); - number = ""; - } else if (!strncmp(key, "fan_", 4)) { - tokens_index=3; - strcat(file, "fan"); - strcat(file, number); - } else { - return -1; - } - - stream=fopen(file, "r"); - if (stream==NULL) { - error ("i2c_sensors: fopen(%s) failed: %s", file, strerror(errno)); - return -1; - } - fgets (buffer, sizeof(buffer), stream); - fclose (stream); - - if (!buffer) { - error ("i2c_sensors: %s empty ?!",file); - return -1; - } - - running=strdupa(buffer); - while(1) { - value = strsep (&running, delim); - /* debug("%s pos %i -> %s", file, pos , value); */ - if (!value || !strcmp(value, "")) { - /* debug("%s pos %i -> BREAK", file, pos); */ - break; + char file[64]; + FILE *stream; + char buffer[32]; + + char *value; + char *running; + int pos = 0; + const char delim[3] = " \n"; + char final_key[32]; + const char *number = &key[strlen(key) - 1]; + int tokens_index; + /* debug("%s -> %s", key, number); */ + strcpy(file, path); + + if (!strncmp(key, "temp_", 5)) { + tokens_index = 0; + strcat(file, "temp"); + strcat(file, number); + } else if (!strncmp(key, "in_", 3)) { + tokens_index = 1; + strcat(file, "in"); + strcat(file, number); + } else if (!strncmp(key, "fan_div", 7)) { + tokens_index = 2; + strcat(file, "fan_div"); + number = ""; + } else if (!strncmp(key, "fan_", 4)) { + tokens_index = 3; + strcat(file, "fan"); + strcat(file, number); } else { - qprintf (final_key, sizeof(final_key), "%s%s", procfs_tokens[tokens_index][pos], number); - /* debug ("%s -> %s", final_key, value); */ - hash_put (&I2Csensors, final_key, value); - pos++; + return -1; } - } - return 0; -} + + stream = fopen(file, "r"); + if (stream == NULL) { + error("i2c_sensors: fopen(%s) failed: %s", file, strerror(errno)); + return -1; + } + fgets(buffer, sizeof(buffer), stream); + fclose(stream); + + if (!buffer) { + error("i2c_sensors: %s empty ?!", file); + return -1; + } + + running = strdupa(buffer); + while (1) { + value = strsep(&running, delim); + /* debug("%s pos %i -> %s", file, pos , value); */ + if (!value || !strcmp(value, "")) { + /* debug("%s pos %i -> BREAK", file, pos); */ + break; + } else { + qprintf(final_key, sizeof(final_key), "%s%s", procfs_tokens[tokens_index][pos], number); + /* debug ("%s -> %s", final_key, value); */ + hash_put(&I2Csensors, final_key, value); + pos++; + } + } + return 0; +} /*****************************************\ * Common functions (path search and init) * @@ -314,145 +314,146 @@ static int parse_i2c_sensors_procfs(const char *key) static void my_i2c_sensors_path(const char *method) { - struct dirent *dir; - struct dirent *file; - const char *base; - char dname[64]; - DIR *fd1; - DIR *fd2; - int done; - - if (!strcmp(method, "sysfs")) { - base="/sys/bus/i2c/devices/"; - } else if (!strcmp(method, "procfs")) { - base="/proc/sys/dev/sensors/"; - /*base="/sensors_2.4/"; // fake dir to test without rebooting 2.4 ;) */ - } else { - return; - } - - fd1 = opendir(base); - if (!fd1) { - return; - } - - while((dir = readdir(fd1))) { - /* Skip non-directories and '.' and '..' */ - if ((dir->d_type!=DT_DIR && dir->d_type!=DT_LNK) || - strcmp(dir->d_name, "." )==0 || - strcmp(dir->d_name, "..")==0) { - continue; + struct dirent *dir; + struct dirent *file; + const char *base; + char dname[64]; + DIR *fd1; + DIR *fd2; + int done; + + if (!strcmp(method, "sysfs")) { + base = "/sys/bus/i2c/devices/"; + } else if (!strcmp(method, "procfs")) { + base = "/proc/sys/dev/sensors/"; + /*base="/sensors_2.4/"; // fake dir to test without rebooting 2.4 ;) */ + } else { + return; } - /* dname is the absolute path */ - strcpy(dname, base); - strcat(dname, dir->d_name); - strcat(dname, "/"); - - fd2 = opendir(dname); - done = 0; - while((file = readdir(fd2))) { - /* FIXME : do all sensors have a temp_input1 ? */ - if (!strcmp(file->d_name, "temp_input1") || !strcmp(file->d_name, "temp1_input") || !strcmp(file->d_name, "temp1")) { - path = realloc(path, strlen(dname)+1); - strcpy(path, dname); - done=1; - break; - } + fd1 = opendir(base); + if (!fd1) { + return; } - closedir(fd2); - if (done) break; - } - closedir(fd1); + + while ((dir = readdir(fd1))) { + /* Skip non-directories and '.' and '..' */ + if ((dir->d_type != DT_DIR && dir->d_type != DT_LNK) || strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) { + continue; + } + + /* dname is the absolute path */ + strcpy(dname, base); + strcat(dname, dir->d_name); + strcat(dname, "/"); + + fd2 = opendir(dname); + done = 0; + while ((file = readdir(fd2))) { + /* FIXME : do all sensors have a temp_input1 ? */ + if (!strcmp(file->d_name, "temp_input1") || !strcmp(file->d_name, "temp1_input") || !strcmp(file->d_name, "temp1")) { + path = realloc(path, strlen(dname) + 1); + strcpy(path, dname); + done = 1; + break; + } + } + closedir(fd2); + if (done) + break; + } + closedir(fd1); } -static int configure_i2c_sensors (void) +static int configure_i2c_sensors(void) { - static int configured = 0; - char *path_cfg; - - if (configured != 0) return configured; - - path_cfg = cfg_get(NULL, "i2c_sensors-path", ""); - if (path_cfg == NULL || *path_cfg == '\0') { - /* debug("No path to i2c sensors found in the conf, calling my_i2c_sensors_path()"); */ - my_i2c_sensors_path("sysfs"); - if (!path) - my_i2c_sensors_path("procfs"); - - if (!path) { - error("i2c_sensors: unable to autodetect i2c sensors!"); - configured = -1; - return configured; - } - - debug("using i2c sensors at %s (autodetected)", path); - - } else { - if (path_cfg[strlen(path_cfg)-1] != '/') { - /* the headless user forgot the trailing slash :/ */ - error("i2c_sensors: please add a trailing slash to %s from %s", path_cfg, cfg_source()); - path_cfg = realloc(path_cfg, strlen(path_cfg)+2); - strcat(path_cfg, "/"); + static int configured = 0; + char *path_cfg; + + if (configured != 0) + return configured; + + path_cfg = cfg_get(NULL, "i2c_sensors-path", ""); + if (path_cfg == NULL || *path_cfg == '\0') { + /* debug("No path to i2c sensors found in the conf, calling my_i2c_sensors_path()"); */ + my_i2c_sensors_path("sysfs"); + if (!path) + my_i2c_sensors_path("procfs"); + + if (!path) { + error("i2c_sensors: unable to autodetect i2c sensors!"); + configured = -1; + return configured; + } + + debug("using i2c sensors at %s (autodetected)", path); + + } else { + if (path_cfg[strlen(path_cfg) - 1] != '/') { + /* the headless user forgot the trailing slash :/ */ + error("i2c_sensors: please add a trailing slash to %s from %s", path_cfg, cfg_source()); + path_cfg = realloc(path_cfg, strlen(path_cfg) + 2); + strcat(path_cfg, "/"); + } + debug("using i2c sensors at %s (from %s)", path, cfg_source()); + path = realloc(path, strlen(path_cfg) + 1); + strcpy(path, path_cfg); + } + if (path_cfg) + free(path_cfg); + + /* we activate the function only if there's a possibly path found */ + if (strncmp(path, "/sys", 4) == 0) { + parse_i2c_sensors = parse_i2c_sensors_sysfs; + } else if (strncmp(path, "/proc", 5) == 0) { + parse_i2c_sensors = parse_i2c_sensors_procfs; + } else { + error("i2c_sensors: unknown path %s, should start with /sys or /proc"); + configured = -1; + return configured; } - debug("using i2c sensors at %s (from %s)", path, cfg_source()); - path = realloc(path, strlen(path_cfg)+1); - strcpy(path, path_cfg); - } - if (path_cfg) free(path_cfg); - - /* we activate the function only if there's a possibly path found */ - if (strncmp(path, "/sys", 4)==0) { - parse_i2c_sensors = parse_i2c_sensors_sysfs; - } else if (strncmp(path, "/proc", 5)==0) { - parse_i2c_sensors = parse_i2c_sensors_procfs; - } else { - error("i2c_sensors: unknown path %s, should start with /sys or /proc"); - configured = -1; + + hash_create(&I2Csensors); + + configured = 1; return configured; - } - - hash_create(&I2Csensors); - - configured = 1; - return configured; } -void my_i2c_sensors(RESULT *result, RESULT *arg) +void my_i2c_sensors(RESULT * result, RESULT * arg) { - int age; - char *key; - char *val; - - if (configure_i2c_sensors() < 0) { - SetResult(&result, R_STRING, "??"); - return; - } - - key=R2S(arg); - age=hash_age(&I2Csensors, key); - if (age<0 || age>250) { - parse_i2c_sensors(key); - } - val=hash_get(&I2Csensors, key, NULL); - if (val) { - SetResult(&result, R_STRING, val); - } else { - SetResult(&result, R_STRING, "??"); - } + int age; + char *key; + char *val; + + if (configure_i2c_sensors() < 0) { + SetResult(&result, R_STRING, "??"); + return; + } + + key = R2S(arg); + age = hash_age(&I2Csensors, key); + if (age < 0 || age > 250) { + parse_i2c_sensors(key); + } + val = hash_get(&I2Csensors, key, NULL); + if (val) { + SetResult(&result, R_STRING, val); + } else { + SetResult(&result, R_STRING, "??"); + } } -int plugin_init_i2c_sensors (void) +int plugin_init_i2c_sensors(void) { - AddFunction ("i2c_sensors", 1, my_i2c_sensors); - return 0; + AddFunction("i2c_sensors", 1, my_i2c_sensors); + return 0; } -void plugin_exit_i2c_sensors(void) +void plugin_exit_i2c_sensors(void) { - hash_destroy(&I2Csensors); + hash_destroy(&I2Csensors); } diff --git a/plugin_imon.c b/plugin_imon.c index 2a7616e..6ed4068 100755 --- a/plugin_imon.c +++ b/plugin_imon.c @@ -1,4 +1,4 @@ -/* $Id: plugin_imon.c,v 1.16 2005/04/04 20:11:14 nicowallmeier Exp $ +/* $Id: plugin_imon.c,v 1.17 2005/05/08 04:32:44 reinelt Exp $ * * imond/telmond data processing * @@ -22,6 +22,9 @@ * * * $Log: plugin_imon.c,v $ + * Revision 1.17 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.16 2005/04/04 20:11:14 nicowallmeier * to be compatible with gcc 2.95 * @@ -81,7 +84,7 @@ * Imon/Telmon plugin ported * */ - + #include "config.h" #include "debug.h" #include "plugin.h" @@ -105,7 +108,7 @@ #include <netdb.h> #include <netinet/in.h> #include <netinet/tcp.h> -#include <arpa/inet.h> /* decl of inet_addr() */ +#include <arpa/inet.h> /* decl of inet_addr() */ #include <sys/socket.h> @@ -120,440 +123,457 @@ static char ihost[256]; static char ipass[256]; static int iport; -static int fd=0; -static int err=0; +static int fd = 0; +static int err = 0; /*---------------------------------------------------------------------------- * service_connect (host_name, port) - connect to tcp-service *---------------------------------------------------------------------------- */ -static int service_connect (const char * host_name, const int port) +static int service_connect(const char *host_name, const int port) { - struct sockaddr_in addr; - struct hostent * host_p; - int fd; - int opt = 1; - - (void) memset ((char *) &addr, 0, sizeof (addr)); - - if ((addr.sin_addr.s_addr = inet_addr ((char *) host_name)) == INADDR_NONE) { - host_p = gethostbyname (host_name); - if (!host_p) { - error ("%s: host not found\n", host_name); - return (-1); + struct sockaddr_in addr; + struct hostent *host_p; + int fd; + int opt = 1; + + (void) memset((char *) &addr, 0, sizeof(addr)); + + if ((addr.sin_addr.s_addr = inet_addr((char *) host_name)) == INADDR_NONE) { + host_p = gethostbyname(host_name); + if (!host_p) { + error("%s: host not found\n", host_name); + return (-1); + } + (void) memcpy((char *) (&addr.sin_addr), host_p->h_addr, host_p->h_length); + } + + addr.sin_family = AF_INET; + addr.sin_port = htons((unsigned short) port); + + /* open socket */ + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("socket"); + return (-1); } - (void) memcpy ((char *) (&addr.sin_addr), host_p->h_addr, host_p->h_length); - } - - addr.sin_family = AF_INET; - addr.sin_port = htons ((unsigned short) port); - - /* open socket */ - if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) { - perror ("socket"); - return (-1); - } - - (void) setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof (opt)); - - if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) != 0) { - (void) close (fd); - perror (host_name); - return (-1); - } - - return (fd); -} /* service_connect (char * host_name, int port) */ + + (void) setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); + + if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) != 0) { + (void) close(fd); + perror(host_name); + return (-1); + } + + return (fd); +} /* service_connect (char * host_name, int port) */ /*---------------------------------------------------------------------------- * send_command (int fd, char * str) - send command to imond *---------------------------------------------------------------------------- */ -static void send_command (const int fd, const char * str) +static void send_command(const int fd, const char *str) { - char buf[256]; - int len = strlen (str); + char buf[256]; + int len = strlen(str); - sprintf (buf, "%s\r\n", str); - write (fd, buf, len + 2); + sprintf(buf, "%s\r\n", str); + write(fd, buf, len + 2); - return; -} /* send_command (int fd, char * str) */ + return; +} /* send_command (int fd, char * str) */ /*---------------------------------------------------------------------------- * get_answer (int fd) - get answer from imond *---------------------------------------------------------------------------- */ -static char* get_answer (const int fd) +static char *get_answer(const int fd) { - static char buf[8192]; - int len; - - len = read (fd, buf, 8192); - - if (len <= 0) { - return ((char *) NULL); - } - - while (len > 1 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) { - buf[len - 1] = '\0'; - len--; - } - - if (! strncmp (buf, "OK ", 3)) { /* OK xxxx */ - return (buf + 3); - } - else if (len > 2 && ! strcmp (buf + len - 2, "OK")) { - *(buf + len - 2) = '\0'; - return (buf); - } - else if (len == 2 && ! strcmp (buf + len - 2, "OK")) { - return (buf); - } - - return ((char *) NULL); /* ERR xxxx */ -} /* get_answer (int fd) */ + static char buf[8192]; + int len; + + len = read(fd, buf, 8192); + + if (len <= 0) { + return ((char *) NULL); + } + + while (len > 1 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) { + buf[len - 1] = '\0'; + len--; + } + + if (!strncmp(buf, "OK ", 3)) { /* OK xxxx */ + return (buf + 3); + } else if (len > 2 && !strcmp(buf + len - 2, "OK")) { + *(buf + len - 2) = '\0'; + return (buf); + } else if (len == 2 && !strcmp(buf + len - 2, "OK")) { + return (buf); + } + + return ((char *) NULL); /* ERR xxxx */ +} /* get_answer (int fd) */ /*---------------------------------------------------------------------------- * get_value (char * cmd) - send command, get value *---------------------------------------------------------------------------- */ -static char* get_value (const char * cmd) +static char *get_value(const char *cmd) { - char *answer; - - send_command (fd, cmd); - - answer = get_answer (fd); - - if (answer) { - return (answer); - } - - return (""); -} /* get_value (char * cmd, int arg) */ + char *answer; + + send_command(fd, cmd); + + answer = get_answer(fd); + + if (answer) { + return (answer); + } + + return (""); +} /* get_value (char * cmd, int arg) */ static void phonebook(char *number) { - FILE * fp; - char line[256]; - - fp = fopen (phoneb, "r"); - - if (! fp) return; - - while (fgets (line, 128, fp)) { - if (*line == '#') continue; - if (!strncmp(line,number,strlen(number))) { - char *komma=strchr(line,','); - char *beginn=strchr(line,'='); - if (!beginn) return; - while (strrchr(line,'\r')) strrchr(line,'\r')[0]='\0'; - while (strrchr(line,'\n')) strrchr(line,'\n')[0]='\0'; - if (komma) komma[0]='\0'; - strcpy(number,beginn+1); - break; - } - } - - fclose(fp); + FILE *fp; + char line[256]; + + fp = fopen(phoneb, "r"); + + if (!fp) + return; + + while (fgets(line, 128, fp)) { + if (*line == '#') + continue; + if (!strncmp(line, number, strlen(number))) { + char *komma = strchr(line, ','); + char *beginn = strchr(line, '='); + if (!beginn) + return; + while (strrchr(line, '\r')) + strrchr(line, '\r')[0] = '\0'; + while (strrchr(line, '\n')) + strrchr(line, '\n')[0] = '\0'; + if (komma) + komma[0] = '\0'; + strcpy(number, beginn + 1); + break; + } + } + + fclose(fp); } static int parse_telmon() { - static int telmond_fd=-2; - static char oldanswer[128]; - int age; - - /* reread every 1 sec only */ - age=hash_age(&TELMON, NULL); - if (age>0 && age<=1000) return 0; - - if (telmond_fd != -1) { - char telbuf[128]; - - telmond_fd = service_connect (thost, tport); - if (telmond_fd >= 0) { - int l = read (telmond_fd, telbuf, 127); - if ((l > 0) && (strcmp(telbuf,oldanswer))){ - char date[11]; - char time[11]; - char number[256]; - char msn[256]; - sscanf(telbuf,"%s %s %s %s",date,time,number,msn); - hash_put (&TELMON, "time", time); - date[4]='\0'; - date[7]='\0'; - qprintf(time, sizeof(time), "%s.%s.%s",date+8,date+5,date); - hash_put (&TELMON, "number", number); - hash_put (&TELMON, "msn", msn); - hash_put (&TELMON, "date", time); - phonebook(number); - phonebook(msn); - hash_put (&TELMON, "name", number); - hash_put (&TELMON, "msnname", msn); - } - close (telmond_fd); - strcpy(oldanswer,telbuf); + static int telmond_fd = -2; + static char oldanswer[128]; + int age; + + /* reread every 1 sec only */ + age = hash_age(&TELMON, NULL); + if (age > 0 && age <= 1000) + return 0; + + if (telmond_fd != -1) { + char telbuf[128]; + + telmond_fd = service_connect(thost, tport); + if (telmond_fd >= 0) { + int l = read(telmond_fd, telbuf, 127); + if ((l > 0) && (strcmp(telbuf, oldanswer))) { + char date[11]; + char time[11]; + char number[256]; + char msn[256]; + sscanf(telbuf, "%s %s %s %s", date, time, number, msn); + hash_put(&TELMON, "time", time); + date[4] = '\0'; + date[7] = '\0'; + qprintf(time, sizeof(time), "%s.%s.%s", date + 8, date + 5, date); + hash_put(&TELMON, "number", number); + hash_put(&TELMON, "msn", msn); + hash_put(&TELMON, "date", time); + phonebook(number); + phonebook(msn); + hash_put(&TELMON, "name", number); + hash_put(&TELMON, "msnname", msn); + } + close(telmond_fd); + strcpy(oldanswer, telbuf); + } } - } - return 0; + return 0; } -static int configure_telmon (void) +static int configure_telmon(void) { - static int configured = 0; - - char *s; - - if (configured != 0) return configured; - - hash_create(&TELMON); - - s=cfg_get ("Plugin:Telmon", "Host","127.0.0.1"); - if (*s=='\0') { - error ("[Telmon] empty 'Host' entry in %s", cfg_source()); - configured = -1; - return configured; - } - strcpy(thost,s); - free(s); - - if (cfg_number("Plugin:Telmon", "Port", 5001, 1, 65536, &tport) < 0) { - error ("[Telmon] no valid port definition"); - configured = -1; + static int configured = 0; + + char *s; + + if (configured != 0) + return configured; + + hash_create(&TELMON); + + s = cfg_get("Plugin:Telmon", "Host", "127.0.0.1"); + if (*s == '\0') { + error("[Telmon] empty 'Host' entry in %s", cfg_source()); + configured = -1; + return configured; + } + strcpy(thost, s); + free(s); + + if (cfg_number("Plugin:Telmon", "Port", 5001, 1, 65536, &tport) < 0) { + error("[Telmon] no valid port definition"); + configured = -1; + return configured; + } + + s = cfg_get("Plugin:Telmon", "Phonebook", "/etc/phonebook"); + strcpy(phoneb, s); + free(s); + + configured = 1; return configured; - } - - s = cfg_get ("Plugin:Telmon", "Phonebook", "/etc/phonebook"); - strcpy(phoneb, s); - free(s); - - configured = 1; - return configured; } -static void my_telmon (RESULT *result, RESULT *arg1) +static void my_telmon(RESULT * result, RESULT * arg1) { - char *val = NULL; - - if (configure_telmon() < 0) { - SetResult(&result, R_STRING, ""); - return; - } + char *val = NULL; - if (parse_telmon() < 0) { - SetResult(&result, R_STRING, ""); - return; - } - - val = hash_get(&TELMON, R2S(arg1), NULL); - if (val == NULL) val=""; - SetResult(&result, R_STRING, val); + if (configure_telmon() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + if (parse_telmon() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + val = hash_get(&TELMON, R2S(arg1), NULL); + if (val == NULL) + val = ""; + SetResult(&result, R_STRING, val); } void init() { - if (fd != 0) return; - - fd = service_connect(ihost, iport); - - if (fd < 0){ - err++; - } else if ((ipass != NULL) && (*ipass != '\0')) { /* Passwort senden */ - char buf[40]; - qprintf(buf,sizeof(buf), "pass %s", ipass); - send_command(fd, buf); - get_answer(fd); - } + if (fd != 0) + return; + + fd = service_connect(ihost, iport); + + if (fd < 0) { + err++; + } else if ((ipass != NULL) && (*ipass != '\0')) { /* Passwort senden */ + char buf[40]; + qprintf(buf, sizeof(buf), "pass %s", ipass); + send_command(fd, buf); + get_answer(fd); + } } static int parse_imon(const char *cmd) { - /* reread every half sec only */ - int age = hash_age(&IMON, cmd); - if (age > 0 && age <= 500) return 0; + /* reread every half sec only */ + int age = hash_age(&IMON, cmd); + if (age > 0 && age <= 500) + return 0; + + init(); /* establish connection */ + + if (err) + return -1; - init(); /* establish connection */ + hash_put(&IMON, cmd, get_value(cmd)); - if (err) return -1; - - hash_put (&IMON, cmd , get_value(cmd)); - - return 0; + return 0; } -static int configure_imon (void) +static int configure_imon(void) { - static int configured = 0; - - char *s; - - if (configured != 0) return configured; - - hash_create(&IMON); - - s=cfg_get ("Plugin:Imon", "Host", "127.0.0.1"); - if (*s=='\0') { - error ("[Imon] empty 'Host' entry in %s", cfg_source()); - configured = -1; - return configured; - } - strcpy(ihost,s); - free(s); - - if (cfg_number("Plugin:Imon", "Port", 5000, 1, 65536, &iport) < 0) { - error ("[Imon] no valid port definition"); - configured = -1; + static int configured = 0; + + char *s; + + if (configured != 0) + return configured; + + hash_create(&IMON); + + s = cfg_get("Plugin:Imon", "Host", "127.0.0.1"); + if (*s == '\0') { + error("[Imon] empty 'Host' entry in %s", cfg_source()); + configured = -1; + return configured; + } + strcpy(ihost, s); + free(s); + + if (cfg_number("Plugin:Imon", "Port", 5000, 1, 65536, &iport) < 0) { + error("[Imon] no valid port definition"); + configured = -1; + return configured; + } + + s = cfg_get("Plugin:Imon", "Pass", ""); + strcpy(ipass, s); + free(s); + + configured = 1; return configured; - } - - s = cfg_get ("Plugin:Imon", "Pass", ""); - strcpy(ipass, s); - free(s); - - configured = 1; - return configured; } -static void my_imon_version (RESULT *result) +static void my_imon_version(RESULT * result) { - char *val; - int age; - - if (configure_imon() < 0) { - SetResult(&result, R_STRING, ""); - return; - } + char *val; + int age; - /* read only once */ - age=hash_age(&IMON, "version"); - if (age<0){ - char *s; - init(); - if (err){ - SetResult(&result, R_STRING, ""); - return; + if (configure_imon() < 0) { + SetResult(&result, R_STRING, ""); + return; } - s=get_value("version"); - for (;;){ /* interne Versionsnummer killen */ - if (s[0]==' '){ - s=s+1; - break; - } - s=s+1; + + /* read only once */ + age = hash_age(&IMON, "version"); + if (age < 0) { + char *s; + init(); + if (err) { + SetResult(&result, R_STRING, ""); + return; + } + s = get_value("version"); + for (;;) { /* interne Versionsnummer killen */ + if (s[0] == ' ') { + s = s + 1; + break; + } + s = s + 1; + } + hash_put(&IMON, "version", s); } - hash_put (&IMON, "version", s); - } - - val=hash_get(&IMON, "version", NULL); - if (val==NULL) val=""; - SetResult(&result, R_STRING, val); + + val = hash_get(&IMON, "version", NULL); + if (val == NULL) + val = ""; + SetResult(&result, R_STRING, val); } static int parse_imon_rates(const char *channel) { - char buf[128],in[25],out[25]; - char *s; - int age; - - qprintf(buf,sizeof(buf),"rate %s in",channel); - - /* reread every half sec only */ - age=hash_age(&IMON, buf); - if (age>0 && age<=500) return 0; - - init(); /* establish connection */ - - if (err) return -1; - - qprintf(buf, sizeof(buf), "rate %s", channel); - s=get_value(buf); - - if (sscanf(s,"%s %s",in, out)!=2) return -1; - - qprintf(buf, sizeof(buf), "rate %s in", channel); - hash_put (&IMON, buf , in); - qprintf(buf, sizeof(buf), "rate %s out", channel); - hash_put (&IMON, buf , out); - - return 0; + char buf[128], in[25], out[25]; + char *s; + int age; + + qprintf(buf, sizeof(buf), "rate %s in", channel); + + /* reread every half sec only */ + age = hash_age(&IMON, buf); + if (age > 0 && age <= 500) + return 0; + + init(); /* establish connection */ + + if (err) + return -1; + + qprintf(buf, sizeof(buf), "rate %s", channel); + s = get_value(buf); + + if (sscanf(s, "%s %s", in, out) != 2) + return -1; + + qprintf(buf, sizeof(buf), "rate %s in", channel); + hash_put(&IMON, buf, in); + qprintf(buf, sizeof(buf), "rate %s out", channel); + hash_put(&IMON, buf, out); + + return 0; } -static void my_imon_rates (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_imon_rates(RESULT * result, RESULT * arg1, RESULT * arg2) { - char *val; - char buf[128]; - - if (configure_imon() < 0) { - SetResult(&result, R_STRING, ""); - return; - } + char *val; + char buf[128]; - if (parse_imon_rates(R2S(arg1)) < 0) { - SetResult(&result, R_STRING, ""); - return; - } - - qprintf(buf,sizeof(buf),"rate %s %s",R2S(arg1),R2S(arg2)); + if (configure_imon() < 0) { + SetResult(&result, R_STRING, ""); + return; + } - val = hash_get(&IMON, buf, NULL); - if (val == NULL) val = ""; - SetResult(&result, R_STRING, val); + if (parse_imon_rates(R2S(arg1)) < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + qprintf(buf, sizeof(buf), "rate %s %s", R2S(arg1), R2S(arg2)); + + val = hash_get(&IMON, buf, NULL); + if (val == NULL) + val = ""; + SetResult(&result, R_STRING, val); } -static void my_imon (RESULT *result, RESULT *arg1) +static void my_imon(RESULT * result, RESULT * arg1) { - char *val; - char *cmd; - - if (configure_imon() < 0) { - SetResult(&result, R_STRING, ""); - return; - } + char *val; + char *cmd; - cmd = R2S(arg1); - if (parse_imon(cmd)<0) { - SetResult(&result, R_STRING, ""); - return; - } - - val = hash_get(&IMON, cmd, NULL); - if (val == NULL) val = ""; - SetResult(&result, R_STRING, val); + if (configure_imon() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + cmd = R2S(arg1); + if (parse_imon(cmd) < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + val = hash_get(&IMON, cmd, NULL); + if (val == NULL) + val = ""; + SetResult(&result, R_STRING, val); } -int plugin_init_imon (void) +int plugin_init_imon(void) { - AddFunction ("imon", 1, my_imon); - AddFunction ("imon::version", 0, my_imon_version); - AddFunction ("imon::rates", 2, my_imon_rates); - AddFunction ("imon::telmon", 1, my_telmon); - - return 0; + AddFunction("imon", 1, my_imon); + AddFunction("imon::version", 0, my_imon_version); + AddFunction("imon::rates", 2, my_imon_rates); + AddFunction("imon::telmon", 1, my_telmon); + + return 0; } void plugin_exit_imon(void) { - if (fd > 0){ - send_command(fd, "quit"); - close(fd); - } - hash_destroy(&TELMON); - hash_destroy(&IMON); + if (fd > 0) { + send_command(fd, "quit"); + close(fd); + } + hash_destroy(&TELMON); + hash_destroy(&IMON); } diff --git a/plugin_isdn.c b/plugin_isdn.c index 3010d4c..7f28a5b 100644 --- a/plugin_isdn.c +++ b/plugin_isdn.c @@ -1,4 +1,4 @@ -/* $Id: plugin_isdn.c,v 1.6 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_isdn.c,v 1.7 2005/05/08 04:32:44 reinelt Exp $ * * plugin for ISDN subsystem * @@ -26,6 +26,9 @@ * * * $Log: plugin_isdn.c,v $ + * Revision 1.7 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.6 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -86,8 +89,8 @@ typedef struct { - unsigned long in; - unsigned long out; + unsigned long in; + unsigned long out; } CPS; @@ -95,178 +98,186 @@ static HASH ISDN_INFO; static HASH ISDN_CPS; -static void hash_put_info (const char *name, const int channel, const char *val) +static void hash_put_info(const char *name, const int channel, const char *val) { - char key[16]; + char key[16]; - qprintf (key, sizeof(key), "%s[%d]", name, channel); - hash_put (&ISDN_INFO, key, val); + qprintf(key, sizeof(key), "%s[%d]", name, channel); + hash_put(&ISDN_INFO, key, val); } -static int parse_isdninfo (void) +static int parse_isdninfo(void) { - int age; - FILE *stream; - long flags; - - /* reread every 10 msec only */ - age = hash_age(&ISDN_INFO, NULL); - if (age > 0 && age <= 10) return 0; - - /* open file */ - stream = fopen ("/dev/isdninfo", "r"); - if (stream == NULL) { - error ("open(/dev/isdninfo) failed: %s", strerror(errno)); - return -1; - } - - /* get flags */ - flags = fcntl(fileno(stream), F_GETFL); - if (flags < 0) { - error ("fcntl(/dev/isdninfo, F_GETFL) failed: %s", strerror(errno)); - return -1; - } - - /* set O_NONBLOCK */ - if (fcntl (fileno(stream), F_SETFL, flags | O_NONBLOCK) < 0) { - error ("fcntl(/dev/isdninfo, F_SETFL, O_NONBLOCK) failed: %s", strerror(errno)); - return -1; - } - - while (!feof(stream)) { - char buffer[4096]; - char *beg, *end; - if (fgets (buffer, sizeof(buffer), stream) == NULL) break; - beg = strchr(buffer, ':'); - if (beg!=NULL) { - char delim[] = " \t\n"; - int i = 0; - *beg++ = '\0'; - while (*beg && strchr(delim, *beg)) beg++; - while (beg && *beg) { - if ((end = strpbrk(beg, delim))) *end = '\0'; - hash_put_info(buffer, i, beg); - beg = end ? end+1 : NULL; - while (*beg && strchr(delim, *beg)) beg++; - i++; - } - } else { - error ("Huh? no colon found in <%s>", buffer); + int age; + FILE *stream; + long flags; + + /* reread every 10 msec only */ + age = hash_age(&ISDN_INFO, NULL); + if (age > 0 && age <= 10) + return 0; + + /* open file */ + stream = fopen("/dev/isdninfo", "r"); + if (stream == NULL) { + error("open(/dev/isdninfo) failed: %s", strerror(errno)); + return -1; + } + + /* get flags */ + flags = fcntl(fileno(stream), F_GETFL); + if (flags < 0) { + error("fcntl(/dev/isdninfo, F_GETFL) failed: %s", strerror(errno)); + return -1; + } + + /* set O_NONBLOCK */ + if (fcntl(fileno(stream), F_SETFL, flags | O_NONBLOCK) < 0) { + error("fcntl(/dev/isdninfo, F_SETFL, O_NONBLOCK) failed: %s", strerror(errno)); + return -1; } - } - fclose (stream); + while (!feof(stream)) { + char buffer[4096]; + char *beg, *end; + if (fgets(buffer, sizeof(buffer), stream) == NULL) + break; + beg = strchr(buffer, ':'); + if (beg != NULL) { + char delim[] = " \t\n"; + int i = 0; + *beg++ = '\0'; + while (*beg && strchr(delim, *beg)) + beg++; + while (beg && *beg) { + if ((end = strpbrk(beg, delim))) + *end = '\0'; + hash_put_info(buffer, i, beg); + beg = end ? end + 1 : NULL; + while (*beg && strchr(delim, *beg)) + beg++; + i++; + } + } else { + error("Huh? no colon found in <%s>", buffer); + } + } + + fclose(stream); - return 0; -} + return 0; +} -static void my_isdn_info (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_isdn_info(RESULT * result, RESULT * arg1, RESULT * arg2) { - char key[16], *val; - - if (parse_isdninfo()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - qprintf(key, sizeof(key), "%s[%d]", R2S(arg1), (int)R2N(arg2)); - val = hash_get(&ISDN_INFO, key, NULL); - if (val == NULL) val = ""; - SetResult(&result, R_STRING, val); + char key[16], *val; + + if (parse_isdninfo() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + qprintf(key, sizeof(key), "%s[%d]", R2S(arg1), (int) R2N(arg2)); + val = hash_get(&ISDN_INFO, key, NULL); + if (val == NULL) + val = ""; + SetResult(&result, R_STRING, val); } #ifdef HAVE_LINUX_ISDN_H -static void hash_put_cps (const int channel, const CPS *cps) +static void hash_put_cps(const int channel, const CPS * cps) { - char key[16], val[16]; + char key[16], val[16]; - qprintf (key, sizeof(key), channel < 0 ? "i" : "i%d", channel); - qprintf (val, sizeof(val), "%u", cps->in); - hash_put_delta (&ISDN_CPS, key, val); + qprintf(key, sizeof(key), channel < 0 ? "i" : "i%d", channel); + qprintf(val, sizeof(val), "%u", cps->in); + hash_put_delta(&ISDN_CPS, key, val); - qprintf (key, sizeof(key), channel < 0 ? "o" : "o%d", channel); - qprintf (val, sizeof(val), "%u", cps->out); - hash_put_delta (&ISDN_CPS, key, val); + qprintf(key, sizeof(key), channel < 0 ? "o" : "o%d", channel); + qprintf(val, sizeof(val), "%u", cps->out); + hash_put_delta(&ISDN_CPS, key, val); } static int get_cps(void) { - int age, i; - static int fd = -2; - CPS cps[ISDN_MAX_CHANNELS]; - CPS sum; - - /* reread every 10 msec only */ - age = hash_age(&ISDN_CPS, NULL); - if (age > 0 && age <= 10) return 0; - - if (fd == -1) return -1; - - if (fd == -2) { - fd = open("/dev/isdninfo", O_RDONLY | O_NDELAY); - if (fd == -1) { - error ("open(/dev/isdninfo) failed: %s", strerror(errno)); - return -1; + int age, i; + static int fd = -2; + CPS cps[ISDN_MAX_CHANNELS]; + CPS sum; + + /* reread every 10 msec only */ + age = hash_age(&ISDN_CPS, NULL); + if (age > 0 && age <= 10) + return 0; + + if (fd == -1) + return -1; + + if (fd == -2) { + fd = open("/dev/isdninfo", O_RDONLY | O_NDELAY); + if (fd == -1) { + error("open(/dev/isdninfo) failed: %s", strerror(errno)); + return -1; + } + } + + if (ioctl(fd, IIOCGETCPS, &cps)) { + error("ioctl(IIOCGETCPS) failed: %s", strerror(errno)); + fd = -1; + return -1; + } + + sum.in = 0; + sum.out = 0; + for (i = 0; i < ISDN_MAX_CHANNELS; i++) { + sum.in += cps[i].in; + sum.out += cps[i].out; + hash_put_cps(i, &cps[i]); } - } - - if (ioctl(fd, IIOCGETCPS, &cps)) { - error("ioctl(IIOCGETCPS) failed: %s", strerror(errno)); - fd = -1; - return -1; - } - - sum.in = 0; - sum.out = 0; - for (i = 0; i < ISDN_MAX_CHANNELS; i++) { - sum.in += cps[i].in; - sum.out += cps[i].out; - hash_put_cps (i, &cps[i]); - } - hash_put_cps (-1, &sum); - - return 0; + hash_put_cps(-1, &sum); + + return 0; } -static void my_isdn_cps (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_isdn_cps(RESULT * result, RESULT * arg1, RESULT * arg2) { - double value; - - if (get_cps()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - value = hash_get_delta(&ISDN_CPS, R2S(arg1), NULL, R2N(arg2)); - SetResult(&result, R_NUMBER, &value); + double value; + + if (get_cps() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + value = hash_get_delta(&ISDN_CPS, R2S(arg1), NULL, R2N(arg2)); + SetResult(&result, R_NUMBER, &value); } #endif -int plugin_init_isdn (void) +int plugin_init_isdn(void) { - hash_create(&ISDN_INFO); - hash_create(&ISDN_CPS); + hash_create(&ISDN_INFO); + hash_create(&ISDN_CPS); - AddFunction ("isdn::info", 2, my_isdn_info); + AddFunction("isdn::info", 2, my_isdn_info); #ifdef HAVE_LINUX_ISDN_H - AddFunction ("isdn::cps", 2, my_isdn_cps); + AddFunction("isdn::cps", 2, my_isdn_cps); #endif - return 0; + return 0; } -void plugin_exit_isdn(void) +void plugin_exit_isdn(void) { - hash_destroy(&ISDN_INFO); - hash_destroy(&ISDN_CPS); + hash_destroy(&ISDN_INFO); + hash_destroy(&ISDN_CPS); } diff --git a/plugin_loadavg.c b/plugin_loadavg.c index e599c68..bdb3091 100644 --- a/plugin_loadavg.c +++ b/plugin_loadavg.c @@ -1,4 +1,4 @@ -/* $Id: plugin_loadavg.c,v 1.9 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_loadavg.c,v 1.10 2005/05/08 04:32:44 reinelt Exp $ * * plugin for load average * @@ -23,6 +23,9 @@ * * * $Log: plugin_loadavg.c,v $ + * Revision 1.10 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -99,86 +102,91 @@ #ifndef HAVE_GETLOADAVG static int fd = -2; -int getloadavg (double loadavg[], int nelem) +int getloadavg(double loadavg[], int nelem) { - char buf[65], *p; - ssize_t nread; - int i; - - if (fd == -2) fd = open ("/proc/loadavg", O_RDONLY); - if (fd < 0) return -1; - - lseek(fd,0,SEEK_SET); - nread = read (fd, buf, sizeof buf - 1); - - if (nread < 0) return -1; - buf[nread - 1] = '\0'; - - if (nelem > 3) nelem = 3; - p = buf; - for (i = 0; i < nelem; ++i) { - char *endp; - loadavg[i] = strtod (p, &endp); - if (endp == NULL || endp == p) - /* This should not happen. The format of /proc/loadavg - must have changed. Don't return with what we have, - signal an error. */ - return -1; - p = endp; - } - - return i; + char buf[65], *p; + ssize_t nread; + int i; + + if (fd == -2) + fd = open("/proc/loadavg", O_RDONLY); + if (fd < 0) + return -1; + + lseek(fd, 0, SEEK_SET); + nread = read(fd, buf, sizeof buf - 1); + + if (nread < 0) + return -1; + buf[nread - 1] = '\0'; + + if (nelem > 3) + nelem = 3; + p = buf; + for (i = 0; i < nelem; ++i) { + char *endp; + loadavg[i] = strtod(p, &endp); + if (endp == NULL || endp == p) + /* This should not happen. The format of /proc/loadavg + must have changed. Don't return with what we have, + signal an error. */ + return -1; + p = endp; + } + + return i; } #endif -static void my_loadavg (RESULT *result, RESULT *arg1) +static void my_loadavg(RESULT * result, RESULT * arg1) { - static int nelem=-1; - int index,age; - static double loadavg[3]; - static struct timeval last_value; - struct timeval now; - - gettimeofday(&now,NULL); - - age = (now.tv_sec - last_value.tv_sec)*1000 + (now.tv_usec - last_value.tv_usec)/1000; - /* reread every 10 msec only */ - if (nelem==-1 || age==0 || age>10) { - - nelem=getloadavg(loadavg, 3); - if (nelem<0) { - error ("getloadavg() failed!"); - SetResult(&result, R_STRING, ""); - return; + static int nelem = -1; + int index, age; + static double loadavg[3]; + static struct timeval last_value; + struct timeval now; + + gettimeofday(&now, NULL); + + age = (now.tv_sec - last_value.tv_sec) * 1000 + (now.tv_usec - last_value.tv_usec) / 1000; + /* reread every 10 msec only */ + if (nelem == -1 || age == 0 || age > 10) { + + nelem = getloadavg(loadavg, 3); + if (nelem < 0) { + error("getloadavg() failed!"); + SetResult(&result, R_STRING, ""); + return; + } + last_value = now; } - last_value=now; - } - index=R2N(arg1); - if (index<1 || index>nelem) { - error ("loadavg(%d): index out of range!", index); - SetResult(&result, R_STRING, ""); + index = R2N(arg1); + if (index < 1 || index > nelem) { + error("loadavg(%d): index out of range!", index); + SetResult(&result, R_STRING, ""); + return; + } + + + SetResult(&result, R_NUMBER, &(loadavg[index - 1])); return; - } - - SetResult(&result, R_NUMBER, &(loadavg[index-1])); - return; - } -int plugin_init_loadavg (void) +int plugin_init_loadavg(void) { - AddFunction ("loadavg", 1, my_loadavg); - return 0; + AddFunction("loadavg", 1, my_loadavg); + return 0; } -void plugin_exit_loadavg(void) +void plugin_exit_loadavg(void) { -#ifndef HAVE_GETLOADAVG - if (fd>0) close(fd); - fd=-2; +#ifndef HAVE_GETLOADAVG + if (fd > 0) + close(fd); + fd = -2; #endif } diff --git a/plugin_math.c b/plugin_math.c index e5a3cc0..a2b53f9 100644 --- a/plugin_math.c +++ b/plugin_math.c @@ -1,4 +1,4 @@ -/* $Id: plugin_math.c,v 1.8 2005/04/05 04:46:06 reinelt Exp $ +/* $Id: plugin_math.c,v 1.9 2005/05/08 04:32:44 reinelt Exp $ * * math plugin * @@ -23,6 +23,9 @@ * * * $Log: plugin_math.c,v $ + * Revision 1.9 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.8 2005/04/05 04:46:06 reinelt * ceil/floor patch from Maxime * @@ -79,104 +82,104 @@ #include "plugin.h" -static void my_sqrt (RESULT *result, RESULT *arg1) +static void my_sqrt(RESULT * result, RESULT * arg1) { - double value=sqrt(R2N(arg1)); - SetResult(&result, R_NUMBER, &value); + double value = sqrt(R2N(arg1)); + SetResult(&result, R_NUMBER, &value); } -static void my_exp (RESULT *result, RESULT *arg1) +static void my_exp(RESULT * result, RESULT * arg1) { - double value=exp(R2N(arg1)); - SetResult(&result, R_NUMBER, &value); + double value = exp(R2N(arg1)); + SetResult(&result, R_NUMBER, &value); } -static void my_ln (RESULT *result, RESULT *arg1) +static void my_ln(RESULT * result, RESULT * arg1) { - double value=log(R2N(arg1)); - SetResult(&result, R_NUMBER, &value); + double value = log(R2N(arg1)); + SetResult(&result, R_NUMBER, &value); } -static void my_log (RESULT *result, RESULT *arg1) +static void my_log(RESULT * result, RESULT * arg1) { - double value=log10(R2N(arg1)); - SetResult(&result, R_NUMBER, &value); + double value = log10(R2N(arg1)); + SetResult(&result, R_NUMBER, &value); } -static void my_sin (RESULT *result, RESULT *arg1) +static void my_sin(RESULT * result, RESULT * arg1) { - double value=sin(R2N(arg1)); - SetResult(&result, R_NUMBER, &value); + double value = sin(R2N(arg1)); + SetResult(&result, R_NUMBER, &value); } -static void my_cos (RESULT *result, RESULT *arg1) +static void my_cos(RESULT * result, RESULT * arg1) { - double value=cos(R2N(arg1)); - SetResult(&result, R_NUMBER, &value); + double value = cos(R2N(arg1)); + SetResult(&result, R_NUMBER, &value); } -static void my_tan (RESULT *result, RESULT *arg1) +static void my_tan(RESULT * result, RESULT * arg1) { - double value=tan(R2N(arg1)); - SetResult(&result, R_NUMBER, &value); + double value = tan(R2N(arg1)); + SetResult(&result, R_NUMBER, &value); } -static void my_min (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_min(RESULT * result, RESULT * arg1, RESULT * arg2) { - double a1=R2N(arg1); - double a2=R2N(arg2); - double value=a1<a2?a1:a2; - SetResult(&result, R_NUMBER, &value); + double a1 = R2N(arg1); + double a2 = R2N(arg2); + double value = a1 < a2 ? a1 : a2; + SetResult(&result, R_NUMBER, &value); } -static void my_max (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_max(RESULT * result, RESULT * arg1, RESULT * arg2) { - double a1=R2N(arg1); - double a2=R2N(arg2); - double value=a1>a2?a1:a2; - SetResult(&result, R_NUMBER, &value); + double a1 = R2N(arg1); + double a2 = R2N(arg2); + double value = a1 > a2 ? a1 : a2; + SetResult(&result, R_NUMBER, &value); } -static void my_floor (RESULT *result, RESULT *arg) +static void my_floor(RESULT * result, RESULT * arg) { - double value = floor(R2N(arg)); - SetResult(&result, R_NUMBER, &value); + double value = floor(R2N(arg)); + SetResult(&result, R_NUMBER, &value); } -static void my_ceil (RESULT *result, RESULT *arg) +static void my_ceil(RESULT * result, RESULT * arg) { - double value = ceil(R2N(arg)); - SetResult(&result, R_NUMBER, &value); + double value = ceil(R2N(arg)); + SetResult(&result, R_NUMBER, &value); } -int plugin_init_math (void) +int plugin_init_math(void) { - /* set some handy constants */ - SetVariableNumeric ("Pi", M_PI); - SetVariableNumeric ("e", M_E); - - /* register some basic math functions */ - AddFunction ("sqrt", 1, my_sqrt); - AddFunction ("exp", 1, my_exp); - AddFunction ("ln", 1, my_ln); - AddFunction ("log", 1, my_log); - AddFunction ("sin", 1, my_sin); - AddFunction ("cos", 1, my_cos); - AddFunction ("tan", 1, my_tan); - - /* min, max */ - AddFunction ("min", 2, my_min); - AddFunction ("max", 2, my_max); - - /* floor, ceil */ - AddFunction ("floor", 1, my_floor); - AddFunction ("ceil", 1, my_ceil); - - return 0; + /* set some handy constants */ + SetVariableNumeric("Pi", M_PI); + SetVariableNumeric("e", M_E); + + /* register some basic math functions */ + AddFunction("sqrt", 1, my_sqrt); + AddFunction("exp", 1, my_exp); + AddFunction("ln", 1, my_ln); + AddFunction("log", 1, my_log); + AddFunction("sin", 1, my_sin); + AddFunction("cos", 1, my_cos); + AddFunction("tan", 1, my_tan); + + /* min, max */ + AddFunction("min", 2, my_min); + AddFunction("max", 2, my_max); + + /* floor, ceil */ + AddFunction("floor", 1, my_floor); + AddFunction("ceil", 1, my_ceil); + + return 0; } -void plugin_exit_math(void) +void plugin_exit_math(void) { } diff --git a/plugin_meminfo.c b/plugin_meminfo.c index 301878f..83c6383 100644 --- a/plugin_meminfo.c +++ b/plugin_meminfo.c @@ -1,4 +1,4 @@ -/* $Id: plugin_meminfo.c,v 1.11 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_meminfo.c,v 1.12 2005/05/08 04:32:44 reinelt Exp $ * * plugin for /proc/meminfo parsing * @@ -23,6 +23,9 @@ * * * $Log: plugin_meminfo.c,v $ + * Revision 1.12 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.11 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -102,78 +105,88 @@ static HASH MemInfo; static FILE *stream = NULL; -static int parse_meminfo (void) +static int parse_meminfo(void) { - int age; - - /* reread every 10 msec only */ - age=hash_age(&MemInfo, NULL); - if (age>0 && age<=10) return 0; - - if (stream==NULL) stream=fopen("/proc/meminfo", "r"); - if (stream==NULL) { - error ("fopen(/proc/meminfo) failed: %s", strerror(errno)); - return -1; - } - - rewind(stream); - while (!feof(stream)) { - char buffer[256]; - char *c, *key, *val; - fgets (buffer, sizeof(buffer), stream); - c=strchr(buffer, ':'); - if (c==NULL) continue; - key=buffer; val=c+1; - /* strip leading blanks from key */ - while (isspace(*key)) *key++='\0'; - /* strip trailing blanks from key */ - do *c='\0'; while (isspace(*--c)); - /* strip leading blanks from value */ - while (isspace(*val)) *val++='\0'; - /* strip trailing blanks from value */ - for (c=val; *c!='\0';c++); - while (isspace(*--c)) *c='\0'; - /* skip lines that do not end with " kB" */ - if (*c=='B' && *(c-1)=='k' && *(c-2)==' ') { - /* strip trailing " kB" from value */ - *(c-2)='\0'; - /* add entry to hash table */ - hash_put (&MemInfo, key, val); + int age; + + /* reread every 10 msec only */ + age = hash_age(&MemInfo, NULL); + if (age > 0 && age <= 10) + return 0; + + if (stream == NULL) + stream = fopen("/proc/meminfo", "r"); + if (stream == NULL) { + error("fopen(/proc/meminfo) failed: %s", strerror(errno)); + return -1; } - } - return 0; -} -static void my_meminfo (RESULT *result, RESULT *arg1) + rewind(stream); + while (!feof(stream)) { + char buffer[256]; + char *c, *key, *val; + fgets(buffer, sizeof(buffer), stream); + c = strchr(buffer, ':'); + if (c == NULL) + continue; + key = buffer; + val = c + 1; + /* strip leading blanks from key */ + while (isspace(*key)) + *key++ = '\0'; + /* strip trailing blanks from key */ + do + *c = '\0'; + while (isspace(*--c)); + /* strip leading blanks from value */ + while (isspace(*val)) + *val++ = '\0'; + /* strip trailing blanks from value */ + for (c = val; *c != '\0'; c++); + while (isspace(*--c)) + *c = '\0'; + /* skip lines that do not end with " kB" */ + if (*c == 'B' && *(c - 1) == 'k' && *(c - 2) == ' ') { + /* strip trailing " kB" from value */ + *(c - 2) = '\0'; + /* add entry to hash table */ + hash_put(&MemInfo, key, val); + } + } + return 0; +} + +static void my_meminfo(RESULT * result, RESULT * arg1) { - char *key, *val; - - if (parse_meminfo()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - key=R2S(arg1); - val=hash_get(&MemInfo, key, NULL); - if (val==NULL) val=""; - - SetResult(&result, R_STRING, val); + char *key, *val; + + if (parse_meminfo() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + key = R2S(arg1); + val = hash_get(&MemInfo, key, NULL); + if (val == NULL) + val = ""; + + SetResult(&result, R_STRING, val); } -int plugin_init_meminfo (void) +int plugin_init_meminfo(void) { - hash_create(&MemInfo); - AddFunction ("meminfo", 1, my_meminfo); - return 0; + hash_create(&MemInfo); + AddFunction("meminfo", 1, my_meminfo); + return 0; } -void plugin_exit_meminfo(void) +void plugin_exit_meminfo(void) { - if (stream != NULL) { - fclose(stream); - stream=NULL; - } - hash_destroy(&MemInfo); + if (stream != NULL) { + fclose(stream); + stream = NULL; + } + hash_destroy(&MemInfo); } diff --git a/plugin_mysql.c b/plugin_mysql.c index ad3a3c5..a55802d 100644 --- a/plugin_mysql.c +++ b/plugin_mysql.c @@ -1,4 +1,4 @@ -/* $Id: plugin_mysql.c,v 1.5 2005/04/01 05:16:04 reinelt Exp $ +/* $Id: plugin_mysql.c,v 1.6 2005/05/08 04:32:44 reinelt Exp $ * * plugin for execute SQL queries into a MySQL DBSM. * @@ -23,6 +23,9 @@ * * * $Log: plugin_mysql.c,v $ + * Revision 1.6 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.5 2005/04/01 05:16:04 reinelt * moved plugin init stuff to a seperate function called on first use * @@ -84,141 +87,142 @@ static MYSQL conex; static char Section[] = "Plugin:MySQL"; -static int configure_mysql (void) +static int configure_mysql(void) { - static int configured = 0; - - char server[256]; - unsigned int port; - char user[128]; - char password[256]; - char database[256]; - char *s; - - if (configured != 0) return configured; - - s = cfg_get (Section, "server", "localhost"); - if (*s=='\0') { - info ("[MySQL] empty '%s.server' entry from %s, assuming 'localhost'", Section, cfg_source()); - strcpy(server,"localhost"); - } - else strcpy(server,s); - free(s); - - if (cfg_number(Section, "port", 0, 1, 65536, &port)<1) { - /* using 0 as default port because mysql_real_connect() will convert it to real default one */ - info ("[MySQL] no '%s.port' entry from %s using MySQL's default", Section, cfg_source()); - } - - s = cfg_get (Section, "user", ""); - if (*s=='\0') { - /* If user is NULL or the empty string "", the lcd4linux Unix user is assumed. */ - info ("[MySQL] empty '%s.user' entry from %s, assuming lcd4linux owner", Section, cfg_source()); - strcpy(user,""); - } - else strcpy(user,s); - free(s); - - s = cfg_get (Section, "password",""); - /* Do not encrypt the password because encryption is handled automatically by the MySQL client API. */ - if (*s=='\0') { - info ("[MySQL] empty '%s.password' entry in %s, assuming none", Section, cfg_source()); - strcpy(password,""); - } - else strcpy(password,s); - free(s); - - s = cfg_get (Section, "database", ""); - if (*s=='\0') { - error ("[MySQL] no '%s:database' entry from %s, specify one", Section, cfg_source()); - free (s); - configured = -1; - return configured; - } - strcpy(database,s); - free(s); - - mysql_init(&conex); - if (!mysql_real_connect(&conex,server,user,password,database,port,NULL,0)) { - error( "[MySQL] conection error: %s",mysql_error(&conex)); - configured = -1; - return configured; - } - - configured = 1; - return configured; -} + static int configured = 0; + + char server[256]; + unsigned int port; + char user[128]; + char password[256]; + char database[256]; + char *s; + + if (configured != 0) + return configured; + + s = cfg_get(Section, "server", "localhost"); + if (*s == '\0') { + info("[MySQL] empty '%s.server' entry from %s, assuming 'localhost'", Section, cfg_source()); + strcpy(server, "localhost"); + } else + strcpy(server, s); + free(s); + + if (cfg_number(Section, "port", 0, 1, 65536, &port) < 1) { + /* using 0 as default port because mysql_real_connect() will convert it to real default one */ + info("[MySQL] no '%s.port' entry from %s using MySQL's default", Section, cfg_source()); + } + s = cfg_get(Section, "user", ""); + if (*s == '\0') { + /* If user is NULL or the empty string "", the lcd4linux Unix user is assumed. */ + info("[MySQL] empty '%s.user' entry from %s, assuming lcd4linux owner", Section, cfg_source()); + strcpy(user, ""); + } else + strcpy(user, s); + free(s); + + s = cfg_get(Section, "password", ""); + /* Do not encrypt the password because encryption is handled automatically by the MySQL client API. */ + if (*s == '\0') { + info("[MySQL] empty '%s.password' entry in %s, assuming none", Section, cfg_source()); + strcpy(password, ""); + } else + strcpy(password, s); + free(s); + + s = cfg_get(Section, "database", ""); + if (*s == '\0') { + error("[MySQL] no '%s:database' entry from %s, specify one", Section, cfg_source()); + free(s); + configured = -1; + return configured; + } + strcpy(database, s); + free(s); + + mysql_init(&conex); + if (!mysql_real_connect(&conex, server, user, password, database, port, NULL, 0)) { + error("[MySQL] conection error: %s", mysql_error(&conex)); + configured = -1; + return configured; + } -static void my_MySQLquery (RESULT *result, RESULT *query) -{ - char *q; - double value; - MYSQL_RES *res; - - if (configure_mysql() < 0) { - value = -1; - SetResult(&result, R_NUMBER, &value); - return; - } - - q = R2S(query); - - /* mysql_ping(MYSQL *mysql) checks whether the connection to the server is working. */ - /* If it has gone down, an automatic reconnection is attempted. */ - mysql_ping(&conex); - if (mysql_real_query(&conex,q,(unsigned int) strlen(q))) { - error( "[MySQL] query error: %s",mysql_error(&conex)); - value = -1; - } else { - /* We don't use res=mysql_use_result(); because mysql_num_rows() will not */ - /* return the correct value until all the rows in the result set have been retrieved */ - /* with mysql_fetch_row(), so we use res=mysql_store_result(); instead */ - res=mysql_store_result(&conex); - value = (double) mysql_num_rows(res); - mysql_free_result(res); - } - - SetResult(&result, R_NUMBER, &value); + configured = 1; + return configured; } -static void my_MySQLstatus (RESULT *result) +static void my_MySQLquery(RESULT * result, RESULT * query) { - char *value = ""; - char *status; - - if (configure_mysql > 0) { + char *q; + double value; + MYSQL_RES *res; + + if (configure_mysql() < 0) { + value = -1; + SetResult(&result, R_NUMBER, &value); + return; + } + + q = R2S(query); + /* mysql_ping(MYSQL *mysql) checks whether the connection to the server is working. */ + /* If it has gone down, an automatic reconnection is attempted. */ mysql_ping(&conex); - status=mysql_stat(&conex); - if (!status) { - error( "[MySQL] status error: %s",mysql_error(&conex)); - value="error"; + if (mysql_real_query(&conex, q, (unsigned int) strlen(q))) { + error("[MySQL] query error: %s", mysql_error(&conex)); + value = -1; } else { - value = status; + /* We don't use res=mysql_use_result(); because mysql_num_rows() will not */ + /* return the correct value until all the rows in the result set have been retrieved */ + /* with mysql_fetch_row(), so we use res=mysql_store_result(); instead */ + res = mysql_store_result(&conex); + value = (double) mysql_num_rows(res); + mysql_free_result(res); } - } - - SetResult(&result, R_STRING, value); + + SetResult(&result, R_NUMBER, &value); +} + + +static void my_MySQLstatus(RESULT * result) +{ + char *value = ""; + char *status; + + if (configure_mysql > 0) { + + mysql_ping(&conex); + status = mysql_stat(&conex); + if (!status) { + error("[MySQL] status error: %s", mysql_error(&conex)); + value = "error"; + } else { + value = status; + } + } + + SetResult(&result, R_STRING, value); } #endif -int plugin_init_mysql (void) +int plugin_init_mysql(void) { #ifdef HAVE_MYSQL_MYSQL_H - AddFunction ("MySQL::query", 1, my_MySQLquery); - AddFunction ("MySQL::status", 0, my_MySQLstatus); + AddFunction("MySQL::query", 1, my_MySQLquery); + AddFunction("MySQL::status", 0, my_MySQLstatus); #endif - return 0; + return 0; } -void plugin_exit_mysql(void) +void plugin_exit_mysql(void) { #ifdef HAVE_MYSQL_MYSQL_H - mysql_close(&conex); + mysql_close(&conex); #endif } diff --git a/plugin_netdev.c b/plugin_netdev.c index a989a0f..6d5d430 100644 --- a/plugin_netdev.c +++ b/plugin_netdev.c @@ -1,4 +1,4 @@ -/* $Id: plugin_netdev.c,v 1.13 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_netdev.c,v 1.14 2005/05/08 04:32:44 reinelt Exp $ * * plugin for /proc/net/dev parsing * @@ -23,6 +23,9 @@ * * * $Log: plugin_netdev.c,v $ + * Revision 1.14 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.13 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -108,132 +111,140 @@ static HASH NetDev; static FILE *Stream = NULL; static char *DELIMITER = " :|\t\n"; -static int parse_netdev (void) +static int parse_netdev(void) { - int age; - int row, col; - static int first_time = 1; - - /* reread every 10 msec only */ - age = hash_age(&NetDev, NULL); - if (age > 0 && age <= 10) return 0; - - if (Stream == NULL) Stream = fopen("/proc/net/dev", "r"); - if (Stream == NULL) { - error ("fopen(/proc/net/dev) failed: %s", strerror(errno)); - return -1; - } - - rewind(Stream); - row = 0; - - while (!feof(Stream)) { - char buffer[256]; - char dev[16]; - char *beg, *end; - unsigned int len; - - if (fgets (buffer, sizeof(buffer), Stream) == NULL) break; - - switch (++row) { - - case 1: - /* skip row 1 */ - continue; - - case 2: - /* row 2 used for headers */ - if (first_time) { - char *RxTx = strrchr(buffer, '|'); - first_time = 0; - col = 0; - beg = buffer; - while (beg) { - char key[32]; - - while (strchr(DELIMITER, *beg)) beg++; - if ((end = strpbrk(beg, DELIMITER)) != NULL) *end = '\0'; - qprintf(key, sizeof(key), "%s_%s", beg < RxTx ? "Rx" : "Tx", beg); - hash_set_column (&NetDev, col++, key); - beg = end ? end + 1 : NULL; + int age; + int row, col; + static int first_time = 1; + + /* reread every 10 msec only */ + age = hash_age(&NetDev, NULL); + if (age > 0 && age <= 10) + return 0; + + if (Stream == NULL) + Stream = fopen("/proc/net/dev", "r"); + if (Stream == NULL) { + error("fopen(/proc/net/dev) failed: %s", strerror(errno)); + return -1; + } + + rewind(Stream); + row = 0; + + while (!feof(Stream)) { + char buffer[256]; + char dev[16]; + char *beg, *end; + unsigned int len; + + if (fgets(buffer, sizeof(buffer), Stream) == NULL) + break; + + switch (++row) { + + case 1: + /* skip row 1 */ + continue; + + case 2: + /* row 2 used for headers */ + if (first_time) { + char *RxTx = strrchr(buffer, '|'); + first_time = 0; + col = 0; + beg = buffer; + while (beg) { + char key[32]; + + while (strchr(DELIMITER, *beg)) + beg++; + if ((end = strpbrk(beg, DELIMITER)) != NULL) + *end = '\0'; + qprintf(key, sizeof(key), "%s_%s", beg < RxTx ? "Rx" : "Tx", beg); + hash_set_column(&NetDev, col++, key); + beg = end ? end + 1 : NULL; + } + } + continue; + + default: + /* fetch interface name (1st column) as key */ + beg = buffer; + while (*beg && *beg == ' ') + beg++; + end = beg + 1; + while (*end && *end != ':') + end++; + len = end - beg; + if (len >= sizeof(dev)) + len = sizeof(dev) - 1; + strncpy(dev, beg, len); + dev[len] = '\0'; + + hash_put_delta(&NetDev, dev, buffer); } - } - continue; - - default: - /* fetch interface name (1st column) as key */ - beg = buffer; - while (*beg && *beg == ' ') beg++; - end = beg + 1; - while (*end && *end != ':') end++; - len = end - beg; - if (len >= sizeof(dev)) len = sizeof(dev)-1; - strncpy (dev, beg, len); - dev[len] = '\0'; - - hash_put_delta (&NetDev, dev, buffer); } - } - - return 0; -} -static void my_netdev (RESULT *result, RESULT *arg1, RESULT *arg2, RESULT *arg3) + return 0; +} + +static void my_netdev(RESULT * result, RESULT * arg1, RESULT * arg2, RESULT * arg3) { - char *dev, *key; - int delay; - double value; - - if (parse_netdev()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - dev = R2S(arg1); - key = R2S(arg2); - delay = R2N(arg3); - - value = hash_get_regex(&NetDev, dev, key, delay); - - SetResult(&result, R_NUMBER, &value); + char *dev, *key; + int delay; + double value; + + if (parse_netdev() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + dev = R2S(arg1); + key = R2S(arg2); + delay = R2N(arg3); + + value = hash_get_regex(&NetDev, dev, key, delay); + + SetResult(&result, R_NUMBER, &value); } -static void my_netdev_fast(RESULT *result, RESULT *arg1, RESULT *arg2, RESULT *arg3) +static void my_netdev_fast(RESULT * result, RESULT * arg1, RESULT * arg2, RESULT * arg3) { - char *dev, *key; - int delay; - double value; - - if (parse_netdev()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - dev = R2S(arg1); - key = R2S(arg2); - delay = R2N(arg3); - - value = hash_get_delta(&NetDev, dev, key, delay); - - SetResult(&result, R_NUMBER, &value); + char *dev, *key; + int delay; + double value; + + if (parse_netdev() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + dev = R2S(arg1); + key = R2S(arg2); + delay = R2N(arg3); + + value = hash_get_delta(&NetDev, dev, key, delay); + + SetResult(&result, R_NUMBER, &value); } -int plugin_init_netdev (void) +int plugin_init_netdev(void) { - hash_create(&NetDev); - hash_set_delimiter (&NetDev, " :|\t\n"); + hash_create(&NetDev); + hash_set_delimiter(&NetDev, " :|\t\n"); - AddFunction ("netdev", 3, my_netdev); - AddFunction ("netdev::fast", 3, my_netdev_fast); - return 0; + AddFunction("netdev", 3, my_netdev); + AddFunction("netdev::fast", 3, my_netdev_fast); + return 0; } -void plugin_exit_netdev(void) +void plugin_exit_netdev(void) { - if(Stream!=NULL) { - fclose (Stream); - Stream=NULL; - } - hash_destroy(&NetDev); + if (Stream != NULL) { + fclose(Stream); + Stream = NULL; + } + hash_destroy(&NetDev); } diff --git a/plugin_pop3.c b/plugin_pop3.c index d8501b9..f1cb799 100644 --- a/plugin_pop3.c +++ b/plugin_pop3.c @@ -1,4 +1,4 @@ -/* $Id: plugin_pop3.c,v 1.8 2005/04/01 05:16:04 reinelt Exp $ +/* $Id: plugin_pop3.c,v 1.9 2005/05/08 04:32:44 reinelt Exp $ * * Plugin to check POP3 mail accounts * @@ -27,6 +27,9 @@ * * * $Log: plugin_pop3.c,v $ + * Revision 1.9 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.8 2005/04/01 05:16:04 reinelt * moved plugin init stuff to a seperate function called on first use * @@ -92,13 +95,13 @@ struct check { - int id; - char *username; - char *password; - char *server; - int port; - int messages; - struct check *next; + int id; + char *username; + char *password; + char *server; + int port; + int messages; + struct check *next; }; @@ -109,7 +112,7 @@ static void check_node_add(struct check **head, struct check *new_check); static void check_destroy(struct check **head); /* pop3 */ -static void pop3_check_messages(struct check *hi,int verbose); +static void pop3_check_messages(struct check *hi, int verbose); static void pop3_recv_crlf_terminated(int sockfd, char *buf, int size); /* socket */ @@ -126,241 +129,252 @@ static struct check *head = NULL; static struct check *check_node_alloc(void) { - struct check *new_check; - new_check = (struct check *)calloc(1, sizeof(struct check)); - if (new_check == NULL) { - error( "[POP3] out of memory\n"); - } - return new_check; + struct check *new_check; + new_check = (struct check *) calloc(1, sizeof(struct check)); + if (new_check == NULL) { + error("[POP3] out of memory\n"); + } + return new_check; } static void check_node_add(struct check **head, struct check *new_check) { - new_check->next = *head ; - *head = new_check; + new_check->next = *head; + *head = new_check; } static void check_destroy(struct check **head) -{ - struct check *iter; - while (*head) { - iter = (*head)->next; - free((*head)->username); - free((*head)->password); - free((*head)->server); - free(*head); - *head = iter; - } - *head = NULL; +{ + struct check *iter; + while (*head) { + iter = (*head)->next; + free((*head)->username); + free((*head)->password); + free((*head)->server); + free(*head); + *head = iter; + } + *head = NULL; } /************************ POP3 ********************************/ -static void pop3_check_messages(struct check *hi,int verbose) +static void pop3_check_messages(struct check *hi, int verbose) { - char buf[BUFSIZE]; - int sockfd; - - if ((sockfd = tcp_connect(hi)) < 0) { - hi->messages = -1; - return; - } - - pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* server greeting */ - if (verbose) info("[POP3] %s -> %s\n", hi->server, buf); - - snprintf(buf, sizeof(buf), "USER %s\r\n", hi->username); - write(sockfd, buf, strlen(buf)); - buf[strlen(buf)-1] = '\0'; - if (verbose) info("[POP3] %s <- %s\n", hi->server, buf); - pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from USER command */ - if (verbose) info("[POP3] %s -> %s\n", hi->server, buf); - - snprintf(buf, sizeof(buf), "PASS %s\r\n", hi->password); - write(sockfd, buf, strlen(buf)); - if (verbose) info("[POP3] %s <- PASS ???\n", hi->server); - pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from PASS command */ - if (verbose) info("[POP3] %s -> %s\n", hi->server, buf); - - if (strncmp(buf, LOCKEDERR, strlen(LOCKEDERR)) == 0) { - hi->messages = -2; - close(sockfd); - return; - } - if (strncmp(buf, POPERR, strlen(POPERR)) == 0) { - error("[POP3] error logging into %s\n", hi->server); - error("[POP3] server responded: %s\n", buf); - hi->messages = -1; + char buf[BUFSIZE]; + int sockfd; + + if ((sockfd = tcp_connect(hi)) < 0) { + hi->messages = -1; + return; + } + + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* server greeting */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + + snprintf(buf, sizeof(buf), "USER %s\r\n", hi->username); + write(sockfd, buf, strlen(buf)); + buf[strlen(buf) - 1] = '\0'; + if (verbose) + info("[POP3] %s <- %s\n", hi->server, buf); + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from USER command */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + + snprintf(buf, sizeof(buf), "PASS %s\r\n", hi->password); + write(sockfd, buf, strlen(buf)); + if (verbose) + info("[POP3] %s <- PASS ???\n", hi->server); + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from PASS command */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + + if (strncmp(buf, LOCKEDERR, strlen(LOCKEDERR)) == 0) { + hi->messages = -2; + close(sockfd); + return; + } + if (strncmp(buf, POPERR, strlen(POPERR)) == 0) { + error("[POP3] error logging into %s\n", hi->server); + error("[POP3] server responded: %s\n", buf); + hi->messages = -1; + close(sockfd); + return; + } + + snprintf(buf, sizeof(buf), "STAT\r\n"); + write(sockfd, buf, strlen(buf)); + if (verbose) + info("[POP3] %s <- STAT\n", hi->server); + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from PASS command */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + + strtok(buf, " "); + hi->messages = atoi(strtok(NULL, " ")); + + snprintf(buf, sizeof(buf), "QUIT\r\n"); + write(sockfd, buf, strlen(buf)); + if (verbose) + info("[POP3] %s <- QUIT\n", hi->server); + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from QUIT command */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + close(sockfd); - return; - } - - snprintf(buf, sizeof(buf), "STAT\r\n"); - write(sockfd, buf, strlen(buf)); - if (verbose) info("[POP3] %s <- STAT\n", hi->server); - pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from PASS command */ - if (verbose) info("[POP3] %s -> %s\n", hi->server, buf); - - strtok(buf, " "); - hi->messages = atoi(strtok(NULL, " ")); - - snprintf(buf, sizeof(buf), "QUIT\r\n"); - write(sockfd, buf, strlen(buf)); - if (verbose) info("[POP3] %s <- QUIT\n", hi->server); - pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from QUIT command */ - if (verbose) info("[POP3] %s -> %s\n", hi->server, buf); - - close(sockfd); } static void pop3_recv_crlf_terminated(int sockfd, char *buf, int size) { - /* receive one line server responses terminated with CRLF */ - char *pos; - int bytes = 0; - memset(buf, 0, size); - while ((pos = strstr(buf, "\r\n")) == NULL) - bytes += read(sockfd, buf+bytes, size-bytes); - *pos = '\0'; + /* receive one line server responses terminated with CRLF */ + char *pos; + int bytes = 0; + memset(buf, 0, size); + while ((pos = strstr(buf, "\r\n")) == NULL) + bytes += read(sockfd, buf + bytes, size - bytes); + *pos = '\0'; } /************************ SOCKET ********************************/ static int tcp_connect(struct check *hi) { - struct sockaddr_in addr; - struct hostent *he = gethostbyname(hi->server); - int sockfd; - - if (hi == NULL) return -1; - - if (!he) { - error("[POP3] Failed to lookup %s\n",hi->server); - return(-1); - } - - memset((char *)&addr, 0, sizeof(struct sockaddr_in)); - addr.sin_family = AF_INET; - memcpy(&(addr.sin_addr.s_addr), he->h_addr, he->h_length); - addr.sin_port = htons(hi->port); - - if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - perror("socket()"); - return(-1); - } - - if (connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0) { - perror("connect()"); - close(sockfd); - return(-1); - } + struct sockaddr_in addr; + struct hostent *he = gethostbyname(hi->server); + int sockfd; + + if (hi == NULL) + return -1; - return(sockfd); + if (!he) { + error("[POP3] Failed to lookup %s\n", hi->server); + return (-1); + } + + memset((char *) &addr, 0, sizeof(struct sockaddr_in)); + addr.sin_family = AF_INET; + memcpy(&(addr.sin_addr.s_addr), he->h_addr, he->h_length); + addr.sin_port = htons(hi->port); + + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + perror("socket()"); + return (-1); + } + + if (connect(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0) { + perror("connect()"); + close(sockfd); + return (-1); + } + + return (sockfd); } -static int getConfig (void) +static int getConfig(void) { - struct check *node = NULL; - int i,n=0; - char *user= (char *)calloc(1, sizeof("user")+sizeof(int)); - char *password= (char *)calloc(1, sizeof("password")+sizeof(int)); - char *server=(char *)calloc(1, sizeof("server")+sizeof(int)); - char *port= (char *)calloc(1, sizeof("port")+sizeof(int)); - - for (i =1;i <= MAX_NUM_ACCOUNTS; i++) { - char *x; - sprintf(user,"user%d",i); - sprintf(password,"password%d",i); - sprintf(server,"server%d",i); - sprintf(port,"port%d",i); - - x = cfg_get (Section, server, ""); - if (*x=='\0') { - info ("[POP3] No '%s.%s' entry from %s, disabling POP3 account #%d", Section, server, cfg_source(),i); - free(x); - } else { - node = check_node_alloc(); - node->id = i; - node->server = x; - node->messages = 0; - node->next = NULL; - - x = cfg_get (Section, user, ""); - if (*x=='\0') { - info ("[POP3] No '%s.%s' entry from %s, disabling POP3 account #%d", Section, user, cfg_source(),i); - free(x); - } else { - node->username = x; - x = cfg_get (Section, password, ""); - if (*x=='\0') { - info ("[POP3] No '%s.%s' entry from %s, disabling POP3 account #%d", Section, password, cfg_source(),i); - free(x); + struct check *node = NULL; + int i, n = 0; + char *user = (char *) calloc(1, sizeof("user") + sizeof(int)); + char *password = (char *) calloc(1, sizeof("password") + sizeof(int)); + char *server = (char *) calloc(1, sizeof("server") + sizeof(int)); + char *port = (char *) calloc(1, sizeof("port") + sizeof(int)); + + for (i = 1; i <= MAX_NUM_ACCOUNTS; i++) { + char *x; + sprintf(user, "user%d", i); + sprintf(password, "password%d", i); + sprintf(server, "server%d", i); + sprintf(port, "port%d", i); + + x = cfg_get(Section, server, ""); + if (*x == '\0') { + info("[POP3] No '%s.%s' entry from %s, disabling POP3 account #%d", Section, server, cfg_source(), i); + free(x); } else { - node->password = x; - if (cfg_number(Section, port, POP3PORT, 1, 65536, &node->port)<1) { - info("[POP3] No '%s.%s' entry from %s, %d will be used for account #%d", Section, port, cfg_source(),POP3PORT,i); - } - check_node_add(&head, node); - n++; + node = check_node_alloc(); + node->id = i; + node->server = x; + node->messages = 0; + node->next = NULL; + + x = cfg_get(Section, user, ""); + if (*x == '\0') { + info("[POP3] No '%s.%s' entry from %s, disabling POP3 account #%d", Section, user, cfg_source(), i); + free(x); + } else { + node->username = x; + x = cfg_get(Section, password, ""); + if (*x == '\0') { + info("[POP3] No '%s.%s' entry from %s, disabling POP3 account #%d", Section, password, cfg_source(), i); + free(x); + } else { + node->password = x; + if (cfg_number(Section, port, POP3PORT, 1, 65536, &node->port) < 1) { + info("[POP3] No '%s.%s' entry from %s, %d will be used for account #%d", Section, port, cfg_source(), POP3PORT, i); + } + check_node_add(&head, node); + n++; + } + } } - } } - } - return(n); + return (n); } -static int configure_pop3 (void) +static int configure_pop3(void) { - static int configured = 0; - int n; - - if (configured != 0) return configured; - - n = getConfig(); - /* by now, head should point to a list of all our accounts */ - if (head) { - info("[POP3] %d POP3 accounts have been succesfully defined",n); - configured = 1; - } else { - configured = -1; - } - return configured; + static int configured = 0; + int n; + + if (configured != 0) + return configured; + + n = getConfig(); + /* by now, head should point to a list of all our accounts */ + if (head) { + info("[POP3] %d POP3 accounts have been succesfully defined", n); + configured = 1; + } else { + configured = -1; + } + return configured; } -static void my_POP3check (RESULT *result, RESULT *check) +static void my_POP3check(RESULT * result, RESULT * check) { - double param = R2N(check); - struct check *node = NULL; - double value; - - if (configure_pop3() < 0) { - value = -1; - SetResult(&result, R_NUMBER, &value); - return; - } - - for (node = head; node; node = node->next) { - if (node->id == param) break; - } - if (node == NULL) { /*Inexistent account */ - value = -1; - } else { - pop3_check_messages(node,0); - value = (double)node->messages; - } - SetResult(&result, R_NUMBER, &value); + double param = R2N(check); + struct check *node = NULL; + double value; + + if (configure_pop3() < 0) { + value = -1; + SetResult(&result, R_NUMBER, &value); + return; + } + + for (node = head; node; node = node->next) { + if (node->id == param) + break; + } + if (node == NULL) { /*Inexistent account */ + value = -1; + } else { + pop3_check_messages(node, 0); + value = (double) node->messages; + } + SetResult(&result, R_NUMBER, &value); } int plugin_init_pop3(void) { - AddFunction ("POP3check", 1, my_POP3check); - return 0; + AddFunction("POP3check", 1, my_POP3check); + return 0; } -void plugin_exit_pop3(void) +void plugin_exit_pop3(void) { - check_destroy(&head); + check_destroy(&head); } - diff --git a/plugin_ppp.c b/plugin_ppp.c index 9c8cfae..70f9963 100644 --- a/plugin_ppp.c +++ b/plugin_ppp.c @@ -1,4 +1,4 @@ -/* $Id: plugin_ppp.c,v 1.9 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_ppp.c,v 1.10 2005/05/08 04:32:44 reinelt Exp $ * * plugin for ppp throughput * @@ -23,6 +23,9 @@ * * * $Log: plugin_ppp.c,v $ + * Revision 1.10 2005/05/08 04:32:44 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -96,76 +99,77 @@ static HASH PPP; -static int get_ppp_stats (void) +static int get_ppp_stats(void) { - int age; - int unit; - unsigned ibytes, obytes; - static int fd=-2; - struct ifpppstatsreq req; - char key[16], val[16]; - - /* reread every 10 msec only */ - age=hash_age(&PPP, NULL); - if (age>0 && age<=10) return 0; - - /* open socket only once */ - if (fd==-2) { - fd = socket(AF_INET, SOCK_DGRAM, 0); - if (fd==-1) { - error ("socket() failed: %s", strerror(errno)); - return -1; + int age; + int unit; + unsigned ibytes, obytes; + static int fd = -2; + struct ifpppstatsreq req; + char key[16], val[16]; + + /* reread every 10 msec only */ + age = hash_age(&PPP, NULL); + if (age > 0 && age <= 10) + return 0; + + /* open socket only once */ + if (fd == -2) { + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd == -1) { + error("socket() failed: %s", strerror(errno)); + return -1; + } } - } - - for (unit=0; unit<8; unit++) { - memset (&req, 0, sizeof (req)); - req.stats_ptr = (caddr_t) &req.stats; - qprintf(req.ifr__name, sizeof(req.ifr__name), "ppp%d", unit); - - if (ioctl(fd, SIOCGPPPSTATS, &req) == 0) { - ibytes=req.stats.p.ppp_ibytes; - obytes=req.stats.p.ppp_obytes; - } else { - ibytes=obytes=0; + + for (unit = 0; unit < 8; unit++) { + memset(&req, 0, sizeof(req)); + req.stats_ptr = (caddr_t) & req.stats; + qprintf(req.ifr__name, sizeof(req.ifr__name), "ppp%d", unit); + + if (ioctl(fd, SIOCGPPPSTATS, &req) == 0) { + ibytes = req.stats.p.ppp_ibytes; + obytes = req.stats.p.ppp_obytes; + } else { + ibytes = obytes = 0; + } + qprintf(key, sizeof(key), "Rx:%d", unit); + qprintf(val, sizeof(val), "%d", ibytes); + hash_put_delta(&PPP, key, val); + qprintf(key, sizeof(key), "Tx:%d", unit); + qprintf(val, sizeof(val), "%d", obytes); + hash_put_delta(&PPP, key, val); + } - qprintf(key, sizeof(key), "Rx:%d", unit); - qprintf(val, sizeof(val), "%d", ibytes); - hash_put_delta (&PPP, key, val); - qprintf(key, sizeof(key), "Tx:%d", unit); - qprintf(val, sizeof(val), "%d", obytes); - hash_put_delta (&PPP, key, val); - - } - return 0; + return 0; } -static void my_ppp (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_ppp(RESULT * result, RESULT * arg1, RESULT * arg2) { - double value; - - if (get_ppp_stats()<0) { - SetResult(&result, R_STRING, ""); - return; - } - value=hash_get_delta(&PPP, R2S(arg1), NULL, R2N(arg2)); - SetResult(&result, R_NUMBER, &value); + double value; + + if (get_ppp_stats() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + value = hash_get_delta(&PPP, R2S(arg1), NULL, R2N(arg2)); + SetResult(&result, R_NUMBER, &value); } #endif -int plugin_init_ppp (void) +int plugin_init_ppp(void) { - hash_create(&PPP); + hash_create(&PPP); #ifdef HAVE_NET_IF_PPP_H - AddFunction ("ppp", 2, my_ppp); + AddFunction("ppp", 2, my_ppp); #endif - return 0; + return 0; } -void plugin_exit_ppp(void) +void plugin_exit_ppp(void) { - hash_destroy(&PPP); + hash_destroy(&PPP); } diff --git a/plugin_proc_stat.c b/plugin_proc_stat.c index 84f4810..55d7d81 100644 --- a/plugin_proc_stat.c +++ b/plugin_proc_stat.c @@ -1,4 +1,4 @@ -/* $Id: plugin_proc_stat.c,v 1.25 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_proc_stat.c,v 1.26 2005/05/08 04:32:45 reinelt Exp $ * * plugin for /proc/stat parsing * @@ -23,6 +23,9 @@ * * * $Log: plugin_proc_stat.c,v $ + * Revision 1.26 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.25 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -151,253 +154,279 @@ static HASH Stat; static FILE *stream = NULL; -static void hash_put1 (const char *key1, const char *val) +static void hash_put1(const char *key1, const char *val) { - hash_put_delta (&Stat, key1, val); + hash_put_delta(&Stat, key1, val); } -static void hash_put2 (const char *key1, const char *key2, const char *val) +static void hash_put2(const char *key1, const char *key2, const char *val) { - char key[32]; - - qprintf(key, sizeof(key), "%s.%s", key1, key2); - hash_put1 (key, val); + char key[32]; + + qprintf(key, sizeof(key), "%s.%s", key1, key2); + hash_put1(key, val); } -static void hash_put3 (const char *key1, const char *key2, const char *key3, const char *val) +static void hash_put3(const char *key1, const char *key2, const char *key3, const char *val) { - char key[32]; - - qprintf(key, sizeof(key), "%s.%s.%s", key1, key2, key3); - hash_put1 (key, val); + char key[32]; + + qprintf(key, sizeof(key), "%s.%s.%s", key1, key2, key3); + hash_put1(key, val); } -static int parse_proc_stat (void) +static int parse_proc_stat(void) { - int age; - - /* reread every 10 msec only */ - age=hash_age(&Stat, NULL); - if (age>0 && age<=10) return 0; - - if (stream==NULL) stream=fopen("/proc/stat", "r"); - if (stream==NULL) { - error ("fopen(/proc/stat) failed: %s", strerror(errno)); - return -1; - } - - rewind(stream); - - while (!feof(stream)) { - char buffer[1024]; - if (fgets (buffer, sizeof(buffer), stream) == NULL) break; - - if (strncmp(buffer, "cpu", 3)==0) { - char *key[] = { "user", "nice", "system", "idle" }; - char delim[] = " \t\n"; - char *cpu, *beg, *end; - int i; - - cpu=buffer; - - /* skip "cpu" or "cpu0" block */ - if ((end=strpbrk(buffer, delim))!=NULL) *end='\0'; - beg=end?end+1:NULL; - - for (i=0; i<4 && beg!=NULL; i++) { - while (strchr(delim, *beg)) beg++; - if ((end=strpbrk(beg, delim))) *end='\0'; - hash_put2 (cpu, key[i], beg); - beg=end?end+1:NULL; - } - } - - else if (strncmp(buffer, "page ", 5)==0) { - char *key[] = { "in", "out" }; - char delim[] = " \t\n"; - char *beg, *end; - int i; - - for (i=0, beg=buffer+5; i<2 && beg!=NULL; i++) { - while (strchr(delim, *beg)) beg++; - if ((end=strpbrk(beg, delim))) *end='\0'; - hash_put2 ("page", key[i], beg); - beg=end?end+1:NULL; - } - } - - else if (strncmp(buffer, "swap ", 5)==0) { - char *key[] = { "in", "out" }; - char delim[] = " \t\n"; - char *beg, *end; - int i; - - for (i=0, beg=buffer+5; i<2 && beg!=NULL; i++) { - while (strchr(delim, *beg)) beg++; - if ((end=strpbrk(beg, delim))) *end='\0'; - hash_put2 ("swap", key[i], beg); - beg=end?end+1:NULL; - } - } - - else if (strncmp(buffer, "intr ", 5)==0) { - char delim[]=" \t\n"; - char *beg, *end, num[4]; - int i; - - for (i=0, beg=buffer+5; i<17 && beg!=NULL; i++) { - while (strchr(delim, *beg)) beg++; - if ((end=strpbrk(beg, delim))) *end='\0'; - if (i==0) - strcpy(num, "sum"); - else - qprintf(num, sizeof(num), "%d", i-1); - hash_put2 ("intr", num, beg); - beg=end?end+1:NULL; - } - } - - else if (strncmp(buffer, "disk_io:", 8)==0) { - char *key[] = { "io", "rio", "rblk", "wio", "wblk" }; - char delim[] = " ():,\t\n"; - char *dev, *beg, *end, *p; - int i; - - dev=buffer+8; - while (dev!=NULL) { - while (strchr(delim, *dev)) dev++; - if ((end=strchr(dev, ')'))) *end='\0'; - while ((p=strchr(dev, ','))!=NULL) *p=':'; - beg=end?end+1:NULL; - for (i=0; i<5 && beg!=NULL; i++) { - while (strchr(delim, *beg)) beg++; - if ((end=strpbrk(beg, delim))) *end='\0'; - hash_put3 ("disk_io", dev, key[i], beg); - beg=end?end+1:NULL; + int age; + + /* reread every 10 msec only */ + age = hash_age(&Stat, NULL); + if (age > 0 && age <= 10) + return 0; + + if (stream == NULL) + stream = fopen("/proc/stat", "r"); + if (stream == NULL) { + error("fopen(/proc/stat) failed: %s", strerror(errno)); + return -1; + } + + rewind(stream); + + while (!feof(stream)) { + char buffer[1024]; + if (fgets(buffer, sizeof(buffer), stream) == NULL) + break; + + if (strncmp(buffer, "cpu", 3) == 0) { + char *key[] = { "user", "nice", "system", "idle" }; + char delim[] = " \t\n"; + char *cpu, *beg, *end; + int i; + + cpu = buffer; + + /* skip "cpu" or "cpu0" block */ + if ((end = strpbrk(buffer, delim)) != NULL) + *end = '\0'; + beg = end ? end + 1 : NULL; + + for (i = 0; i < 4 && beg != NULL; i++) { + while (strchr(delim, *beg)) + beg++; + if ((end = strpbrk(beg, delim))) + *end = '\0'; + hash_put2(cpu, key[i], beg); + beg = end ? end + 1 : NULL; + } + } + + else if (strncmp(buffer, "page ", 5) == 0) { + char *key[] = { "in", "out" }; + char delim[] = " \t\n"; + char *beg, *end; + int i; + + for (i = 0, beg = buffer + 5; i < 2 && beg != NULL; i++) { + while (strchr(delim, *beg)) + beg++; + if ((end = strpbrk(beg, delim))) + *end = '\0'; + hash_put2("page", key[i], beg); + beg = end ? end + 1 : NULL; + } + } + + else if (strncmp(buffer, "swap ", 5) == 0) { + char *key[] = { "in", "out" }; + char delim[] = " \t\n"; + char *beg, *end; + int i; + + for (i = 0, beg = buffer + 5; i < 2 && beg != NULL; i++) { + while (strchr(delim, *beg)) + beg++; + if ((end = strpbrk(beg, delim))) + *end = '\0'; + hash_put2("swap", key[i], beg); + beg = end ? end + 1 : NULL; + } + } + + else if (strncmp(buffer, "intr ", 5) == 0) { + char delim[] = " \t\n"; + char *beg, *end, num[4]; + int i; + + for (i = 0, beg = buffer + 5; i < 17 && beg != NULL; i++) { + while (strchr(delim, *beg)) + beg++; + if ((end = strpbrk(beg, delim))) + *end = '\0'; + if (i == 0) + strcpy(num, "sum"); + else + qprintf(num, sizeof(num), "%d", i - 1); + hash_put2("intr", num, beg); + beg = end ? end + 1 : NULL; + } + } + + else if (strncmp(buffer, "disk_io:", 8) == 0) { + char *key[] = { "io", "rio", "rblk", "wio", "wblk" }; + char delim[] = " ():,\t\n"; + char *dev, *beg, *end, *p; + int i; + + dev = buffer + 8; + while (dev != NULL) { + while (strchr(delim, *dev)) + dev++; + if ((end = strchr(dev, ')'))) + *end = '\0'; + while ((p = strchr(dev, ',')) != NULL) + *p = ':'; + beg = end ? end + 1 : NULL; + for (i = 0; i < 5 && beg != NULL; i++) { + while (strchr(delim, *beg)) + beg++; + if ((end = strpbrk(beg, delim))) + *end = '\0'; + hash_put3("disk_io", dev, key[i], beg); + beg = end ? end + 1 : NULL; + } + dev = beg; + } + } + + else { + char delim[] = " \t\n"; + char *beg, *end; + + beg = buffer; + if ((end = strpbrk(beg, delim))) + *end = '\0'; + beg = end ? end + 1 : NULL; + if ((end = strpbrk(beg, delim))) + *end = '\0'; + while (strchr(delim, *beg)) + beg++; + hash_put1(buffer, beg); } - dev=beg; - } } - - else { - char delim[] = " \t\n"; - char *beg, *end; - - beg=buffer; - if ((end=strpbrk(beg, delim))) *end='\0'; - beg=end?end+1:NULL; - if ((end=strpbrk(beg, delim))) *end='\0'; - while (strchr(delim, *beg)) beg++; - hash_put1 (buffer, beg); - } - } - return 0; + return 0; } -static void my_proc_stat (RESULT *result, const int argc, RESULT *argv[]) +static void my_proc_stat(RESULT * result, const int argc, RESULT * argv[]) { - char *string; - double number; - - if (parse_proc_stat()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - switch (argc) { - case 1: - string=hash_get(&Stat, R2S(argv[0]), NULL); - if (string==NULL) string=""; - SetResult(&result, R_STRING, string); - break; - case 2: - number=hash_get_delta(&Stat, R2S(argv[0]), NULL, R2N(argv[1])); - SetResult(&result, R_NUMBER, &number); - break; - default: - error ("proc_stat(): wrong number of parameters"); - SetResult(&result, R_STRING, ""); - } + char *string; + double number; + + if (parse_proc_stat() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + switch (argc) { + case 1: + string = hash_get(&Stat, R2S(argv[0]), NULL); + if (string == NULL) + string = ""; + SetResult(&result, R_STRING, string); + break; + case 2: + number = hash_get_delta(&Stat, R2S(argv[0]), NULL, R2N(argv[1])); + SetResult(&result, R_NUMBER, &number); + break; + default: + error("proc_stat(): wrong number of parameters"); + SetResult(&result, R_STRING, ""); + } } -static void my_cpu (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_cpu(RESULT * result, RESULT * arg1, RESULT * arg2) { - char *key; - int delay; - double value; - double cpu_user, cpu_nice, cpu_system, cpu_idle, cpu_total; - - if (parse_proc_stat()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - key = R2S(arg1); - delay = R2N(arg2); - - cpu_user = hash_get_delta(&Stat, "cpu.user", NULL, delay); - cpu_nice = hash_get_delta(&Stat, "cpu.nice", NULL, delay); - cpu_system = hash_get_delta(&Stat, "cpu.system", NULL, delay); - cpu_idle = hash_get_delta(&Stat, "cpu.idle", NULL, delay); - - cpu_total = cpu_user+cpu_nice+cpu_system+cpu_idle; - - if (strcasecmp(key, "user" )==0) value=cpu_user; - else if (strcasecmp(key, "nice" )==0) value=cpu_nice; - else if (strcasecmp(key, "system")==0) value=cpu_system; - else if (strcasecmp(key, "idle" )==0) value=cpu_idle; - else if (strcasecmp(key, "busy" )==0) value=cpu_total-cpu_idle; - - if (cpu_total>0.0) - value = 100*value/cpu_total; - else - value=0.0; - - SetResult(&result, R_NUMBER, &value); + char *key; + int delay; + double value; + double cpu_user, cpu_nice, cpu_system, cpu_idle, cpu_total; + + if (parse_proc_stat() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + key = R2S(arg1); + delay = R2N(arg2); + + cpu_user = hash_get_delta(&Stat, "cpu.user", NULL, delay); + cpu_nice = hash_get_delta(&Stat, "cpu.nice", NULL, delay); + cpu_system = hash_get_delta(&Stat, "cpu.system", NULL, delay); + cpu_idle = hash_get_delta(&Stat, "cpu.idle", NULL, delay); + + cpu_total = cpu_user + cpu_nice + cpu_system + cpu_idle; + + if (strcasecmp(key, "user") == 0) + value = cpu_user; + else if (strcasecmp(key, "nice") == 0) + value = cpu_nice; + else if (strcasecmp(key, "system") == 0) + value = cpu_system; + else if (strcasecmp(key, "idle") == 0) + value = cpu_idle; + else if (strcasecmp(key, "busy") == 0) + value = cpu_total - cpu_idle; + + if (cpu_total > 0.0) + value = 100 * value / cpu_total; + else + value = 0.0; + + SetResult(&result, R_NUMBER, &value); } -static void my_disk (RESULT *result, RESULT *arg1, RESULT *arg2, RESULT *arg3) +static void my_disk(RESULT * result, RESULT * arg1, RESULT * arg2, RESULT * arg3) { - char *dev, *key, buffer[32]; - int delay; - double value; - - if (parse_proc_stat()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - dev = R2S(arg1); - key = R2S(arg2); - delay = R2N(arg3); - - qprintf(buffer, sizeof(buffer), "disk_io\\.%s\\.%s", dev, key); - value = hash_get_regex(&Stat, buffer, NULL, delay); - - SetResult(&result, R_NUMBER, &value); + char *dev, *key, buffer[32]; + int delay; + double value; + + if (parse_proc_stat() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + dev = R2S(arg1); + key = R2S(arg2); + delay = R2N(arg3); + + qprintf(buffer, sizeof(buffer), "disk_io\\.%s\\.%s", dev, key); + value = hash_get_regex(&Stat, buffer, NULL, delay); + + SetResult(&result, R_NUMBER, &value); } -int plugin_init_proc_stat (void) +int plugin_init_proc_stat(void) { - hash_create(&Stat); - AddFunction ("proc_stat", -1, my_proc_stat); - AddFunction ("proc_stat::cpu", 2, my_cpu); - AddFunction ("proc_stat::disk", 3, my_disk); - return 0; + hash_create(&Stat); + AddFunction("proc_stat", -1, my_proc_stat); + AddFunction("proc_stat::cpu", 2, my_cpu); + AddFunction("proc_stat::disk", 3, my_disk); + return 0; } -void plugin_exit_proc_stat(void) +void plugin_exit_proc_stat(void) { - if (stream!=NULL) { - fclose (stream); - stream=NULL; - } - hash_destroy(&Stat); + if (stream != NULL) { + fclose(stream); + stream = NULL; + } + hash_destroy(&Stat); } diff --git a/plugin_python.c b/plugin_python.c index d832808..dcd5dab 100644 --- a/plugin_python.c +++ b/plugin_python.c @@ -1,4 +1,4 @@ -/* $Id: plugin_python.c,v 1.3 2005/05/04 05:22:12 reinelt Exp $ +/* $Id: plugin_python.c,v 1.4 2005/05/08 04:32:45 reinelt Exp $ * * Python plugin * @@ -23,6 +23,9 @@ * * * $Log: plugin_python.c,v $ + * Revision 1.4 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.3 2005/05/04 05:22:12 reinelt * * replaced fprintf(stderr,... with error() * * corrected a "dangling reference" memory problem @@ -62,98 +65,95 @@ * Fixme: this function should be able to accept and receive any types * of arguments supported by the evaluator. Right now only strings are accepted. */ - -static void -pyt_exec_str(RESULT *result, const char* module, const char* function, int argc, const char* argv[]) { - PyObject *pName, *pModule, *pDict, *pFunc; - PyObject *pArgs, *pValue; - const char * rv = NULL; - int i; +static void pyt_exec_str(RESULT * result, const char *module, const char *function, int argc, const char *argv[]) +{ + + PyObject *pName, *pModule, *pDict, *pFunc; + PyObject *pArgs, *pValue; + const char *rv = NULL; + int i; - pName = PyString_FromString(module); - /* Error checking of pName left out */ + pName = PyString_FromString(module); + /* Error checking of pName left out */ - pModule = PyImport_Import(pName); - Py_DECREF(pName); + pModule = PyImport_Import(pName); + Py_DECREF(pName); - if (pModule != NULL) { - pDict = PyModule_GetDict(pModule); - /* pDict is a borrowed reference */ + if (pModule != NULL) { + pDict = PyModule_GetDict(pModule); + /* pDict is a borrowed reference */ - pFunc = PyDict_GetItemString(pDict, function); - /* pFun: Borrowed reference */ + pFunc = PyDict_GetItemString(pDict, function); + /* pFun: Borrowed reference */ - if (pFunc && PyCallable_Check(pFunc)) { - pArgs = PyTuple_New(argc); - for (i = 0; i < argc; ++i) { - pValue = PyString_FromString(argv[i]); - if (!pValue) { - Py_DECREF(pArgs); - Py_DECREF(pModule); - error("Cannot convert argument \"%s\" to python format", argv[i]); - SetResult(&result, R_STRING, ""); - return; - } - /* pValue reference stolen here: */ - PyTuple_SetItem(pArgs, i, pValue); - } - pValue = PyObject_CallObject(pFunc, pArgs); - Py_DECREF(pArgs); - if (pValue != NULL) { - rv = PyString_AsString(pValue); - SetResult(&result, R_STRING, rv); - Py_DECREF(pValue); - /* rv is now a 'dangling reference' */ - return; - } - else { - Py_DECREF(pModule); - error("Python call failed (\"%s.%s\")", module, function); - SetResult(&result, R_STRING, ""); - return; - } - /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */ - } - else { - error("Can not find python function \"%s.%s\"", module, function); + if (pFunc && PyCallable_Check(pFunc)) { + pArgs = PyTuple_New(argc); + for (i = 0; i < argc; ++i) { + pValue = PyString_FromString(argv[i]); + if (!pValue) { + Py_DECREF(pArgs); + Py_DECREF(pModule); + error("Cannot convert argument \"%s\" to python format", argv[i]); + SetResult(&result, R_STRING, ""); + return; + } + /* pValue reference stolen here: */ + PyTuple_SetItem(pArgs, i, pValue); + } + pValue = PyObject_CallObject(pFunc, pArgs); + Py_DECREF(pArgs); + if (pValue != NULL) { + rv = PyString_AsString(pValue); + SetResult(&result, R_STRING, rv); + Py_DECREF(pValue); + /* rv is now a 'dangling reference' */ + return; + } else { + Py_DECREF(pModule); + error("Python call failed (\"%s.%s\")", module, function); + SetResult(&result, R_STRING, ""); + return; + } + /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */ + } else { + error("Can not find python function \"%s.%s\"", module, function); + } + Py_DECREF(pModule); + } else { + error("Failed to load python module \"%s\"", module); } - Py_DECREF(pModule); - } - else { - error("Failed to load python module \"%s\"", module); - } - SetResult(&result, R_STRING, ""); - return; + SetResult(&result, R_STRING, ""); + return; } static int python_cleanup_responsibility = 0; - -static void my_exec (RESULT *result, RESULT *module, RESULT *function, RESULT *arg ) -{ - /* Fixme: a plugin should be able to accept any number of arguments, don't know how - to code that (yet) */ - const char* args[] = {R2S(arg)}; - pyt_exec_str(result, R2S(module), R2S(function), 1, args); + +static void my_exec(RESULT * result, RESULT * module, RESULT * function, RESULT * arg) +{ + /* Fixme: a plugin should be able to accept any number of arguments, don't know how + to code that (yet) */ + const char *args[] = { R2S(arg) }; + pyt_exec_str(result, R2S(module), R2S(function), 1, args); } -int plugin_init_python (void) +int plugin_init_python(void) { - if (!Py_IsInitialized()) { - Py_Initialize(); - python_cleanup_responsibility = 1; - } - AddFunction ("python::exec", 3, my_exec); - return 0; + if (!Py_IsInitialized()) { + Py_Initialize(); + python_cleanup_responsibility = 1; + } + AddFunction("python::exec", 3, my_exec); + return 0; } -void plugin_exit_python (void) +void plugin_exit_python(void) { - /* Make sure NOT to call Py_Finalize() When (and if) the entire lcd4linux process - * is started from inside python - */ - if (python_cleanup_responsibility) { - python_cleanup_responsibility = 0; - Py_Finalize(); - } + /* Make sure NOT to call Py_Finalize() When (and if) the entire lcd4linux process + * is started from inside python + */ + if (python_cleanup_responsibility) { + python_cleanup_responsibility = 0; + Py_Finalize(); + } } diff --git a/plugin_sample.c b/plugin_sample.c index 01aeca9..3c80e35 100644 --- a/plugin_sample.c +++ b/plugin_sample.c @@ -1,4 +1,4 @@ -/* $Id: plugin_sample.c,v 1.9 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_sample.c,v 1.10 2005/05/08 04:32:45 reinelt Exp $ * * plugin template * @@ -23,6 +23,9 @@ * * * $Log: plugin_sample.c,v $ + * Revision 1.10 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -96,22 +99,22 @@ /* multiplies the number by 2.0 */ /* Note: all local functions should be declared 'static' */ -static void my_mul2 (RESULT *result, RESULT *arg1) +static void my_mul2(RESULT * result, RESULT * arg1) { - double param; - double value; - - /* Get Parameter */ - /* R2N stands for 'Result to Number' */ - param = R2N(arg1); - - /* calculate value */ - value = param*2.0; - - /* store result */ - /* when called with R_NUMBER, it assumes the */ - /* next parameter to be a pointer to double */ - SetResult(&result, R_NUMBER, &value); + double param; + double value; + + /* Get Parameter */ + /* R2N stands for 'Result to Number' */ + param = R2N(arg1); + + /* calculate value */ + value = param * 2.0; + + /* store result */ + /* when called with R_NUMBER, it assumes the */ + /* next parameter to be a pointer to double */ + SetResult(&result, R_NUMBER, &value); } @@ -120,13 +123,13 @@ static void my_mul2 (RESULT *result, RESULT *arg1) /* multiplies the number by 3.0 */ /* same as 'mul2', but shorter */ -static void my_mul3 (RESULT *result, RESULT *arg1) +static void my_mul3(RESULT * result, RESULT * arg1) { - /* do it all in one line */ - double value = R2N(arg1) * 3.0; + /* do it all in one line */ + double value = R2N(arg1) * 3.0; - /* store result */ - SetResult(&result, R_NUMBER, &value); + /* store result */ + SetResult(&result, R_NUMBER, &value); } @@ -134,16 +137,17 @@ static void my_mul3 (RESULT *result, RESULT *arg1) /* takes two arguments, both numbers */ /* returns |a-b| */ -static void my_diff (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_diff(RESULT * result, RESULT * arg1, RESULT * arg2) { - /* do it all in one line */ - double value = R2N(arg1) - R2N(arg2); - - /* some more calculations... */ - if (value < 0) value = -value; - - /* store result */ - SetResult(&result, R_NUMBER, &value); + /* do it all in one line */ + double value = R2N(arg1) - R2N(arg2); + + /* some more calculations... */ + if (value < 0) + value = -value; + + /* store result */ + SetResult(&result, R_NUMBER, &value); } @@ -151,14 +155,14 @@ static void my_diff (RESULT *result, RESULT *arg1, RESULT *arg2) /* takes no argument! */ /* returns the answer to all questions */ -static void my_answer (RESULT *result) +static void my_answer(RESULT * result) { - /* we have to declare a variable because */ - /* SetResult needs a pointer */ - double value = 42; - - /* store result */ - SetResult(&result, R_NUMBER, &value); + /* we have to declare a variable because */ + /* SetResult needs a pointer */ + double value = 42; + + /* store result */ + SetResult(&result, R_NUMBER, &value); } @@ -166,14 +170,14 @@ static void my_answer (RESULT *result) /* takes one argument, a string */ /* returns the string length */ -static void my_length (RESULT *result, RESULT *arg1) +static void my_length(RESULT * result, RESULT * arg1) { - /* Note #1: value *must* be double! */ - /* Note #2: R2S stands for 'Result to String' */ - double value = strlen(R2S(arg1)); - - /* store result */ - SetResult(&result, R_NUMBER, &value); + /* Note #1: value *must* be double! */ + /* Note #2: R2S stands for 'Result to String' */ + double value = strlen(R2S(arg1)); + + /* store result */ + SetResult(&result, R_NUMBER, &value); } @@ -182,27 +186,27 @@ static void my_length (RESULT *result, RESULT *arg1) /* takes one argument, a string */ /* returns the string in upper case letters */ -static void my_upcase (RESULT *result, RESULT *arg1) +static void my_upcase(RESULT * result, RESULT * arg1) { - char *value, *p; - - /* create a local copy of the argument */ - /* Do *NOT* try to modify the original string! */ - value = strdup(R2S(arg1)); - - /* process the string */ - for (p = value; *p != '\0'; p++) - *p = toupper(*p); - - /* store result */ - /* when called with R_STRING, it assumes the */ - /* next parameter to be a pointer to a string */ - /* 'value' is already a char*, so use 'value', not '&value' */ - SetResult(&result, R_STRING, value); - - /* free local copy again */ - /* Note that SetResult() makes its own string copy */ - free (value); + char *value, *p; + + /* create a local copy of the argument */ + /* Do *NOT* try to modify the original string! */ + value = strdup(R2S(arg1)); + + /* process the string */ + for (p = value; *p != '\0'; p++) + *p = toupper(*p); + + /* store result */ + /* when called with R_STRING, it assumes the */ + /* next parameter to be a pointer to a string */ + /* 'value' is already a char*, so use 'value', not '&value' */ + SetResult(&result, R_STRING, value); + + /* free local copy again */ + /* Note that SetResult() makes its own string copy */ + free(value); } @@ -210,51 +214,51 @@ static void my_upcase (RESULT *result, RESULT *arg1) /* takes variable number of arguments, all strings */ /* returns all prameters concatenated */ -static void my_concat (RESULT *result, int argc, RESULT *argv[]) +static void my_concat(RESULT * result, int argc, RESULT * argv[]) { - int i, len; - char *value, *part; - - /* start with a empty string */ - value = strdup(""); - - /* process all arguments */ - for (i = 0; i < argc; i++) { - part = R2S(argv[i]); - len = strlen(value)+strlen(part); - value = realloc(value, len+1); - strcat(value, part); - } - - /* store result */ - SetResult(&result, R_STRING, value); - - /* free local string */ - free (value); + int i, len; + char *value, *part; + + /* start with a empty string */ + value = strdup(""); + + /* process all arguments */ + for (i = 0; i < argc; i++) { + part = R2S(argv[i]); + len = strlen(value) + strlen(part); + value = realloc(value, len + 1); + strcat(value, part); + } + + /* store result */ + SetResult(&result, R_STRING, value); + + /* free local string */ + free(value); } /* plugin initialization */ /* MUST NOT be declared 'static'! */ -int plugin_init_sample (void) +int plugin_init_sample(void) { - /* register all our cool functions */ - /* the second parameter is the number of arguments */ - /* -1 stands for variable argument list */ - AddFunction ("mul2", 1, my_mul2); - AddFunction ("mul3", 1, my_mul3); - AddFunction ("answer", 0, my_answer); - AddFunction ("diff", 2, my_diff); - AddFunction ("length", 1, my_length); - AddFunction ("upcase", 1, my_upcase); - AddFunction ("concat", -1, my_concat); - - return 0; + /* register all our cool functions */ + /* the second parameter is the number of arguments */ + /* -1 stands for variable argument list */ + AddFunction("mul2", 1, my_mul2); + AddFunction("mul3", 1, my_mul3); + AddFunction("answer", 0, my_answer); + AddFunction("diff", 2, my_diff); + AddFunction("length", 1, my_length); + AddFunction("upcase", 1, my_upcase); + AddFunction("concat", -1, my_concat); + + return 0; } -void plugin_exit_sample(void) +void plugin_exit_sample(void) { - /* free any allocated memory */ - /* close filedescriptors */ + /* free any allocated memory */ + /* close filedescriptors */ } diff --git a/plugin_seti.c b/plugin_seti.c index 2f46a0e..c28746b 100644 --- a/plugin_seti.c +++ b/plugin_seti.c @@ -1,4 +1,4 @@ -/* $Id: plugin_seti.c,v 1.6 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_seti.c,v 1.7 2005/05/08 04:32:45 reinelt Exp $ * * plugin for seti@home status reporting * @@ -27,6 +27,9 @@ * * * $Log: plugin_seti.c,v $ + * Revision 1.7 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.6 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -81,97 +84,107 @@ static HASH SETI; static int fatal = 0; -static int parse_seti (void) +static int parse_seti(void) { - static char fn[256] = ""; - FILE *stream; - int age; - - /* if a fatal error occured, do nothing */ - if (fatal != 0) return -1; - - /* reread every 100 msec only */ - age=hash_age(&SETI, NULL); - if (age>0 && age<=100) return 0; - - if (fn[0] == '\0') { - char *dir = cfg_get (SECTION, DIRKEY, NULL); - if (dir == NULL || *dir == '\0') { - error ("no '%s.%s' entry from %s\n", SECTION, DIRKEY, cfg_source()); - fatal = 1; - return -1; + static char fn[256] = ""; + FILE *stream; + int age; + + /* if a fatal error occured, do nothing */ + if (fatal != 0) + return -1; + + /* reread every 100 msec only */ + age = hash_age(&SETI, NULL); + if (age > 0 && age <= 100) + return 0; + + if (fn[0] == '\0') { + char *dir = cfg_get(SECTION, DIRKEY, NULL); + if (dir == NULL || *dir == '\0') { + error("no '%s.%s' entry from %s\n", SECTION, DIRKEY, cfg_source()); + fatal = 1; + return -1; + } + if (strlen(dir) > sizeof(fn) - sizeof(STATEFILE) - 2) { + error("entry '%s.%s' too long from %s!\n", SECTION, DIRKEY, cfg_source()); + fatal = 1; + free(dir); + return -1; + } + strcpy(fn, dir); + if (fn[strlen(fn) - 1] != '/') + strcat(fn, "/"); + strcat(fn, STATEFILE); + free(dir); } - if (strlen(dir) > sizeof(fn)-sizeof(STATEFILE)-2) { - error ("entry '%s.%s' too long from %s!\n", SECTION, DIRKEY, cfg_source()); - fatal = 1; - free (dir); - return -1; + + stream = fopen(fn, "r"); + if (stream == NULL) { + error("fopen(%s) failed: %s", fn, strerror(errno)); + return -1; + } + + while (!feof(stream)) { + char buffer[256]; + char *c, *key, *val; + fgets(buffer, sizeof(buffer), stream); + c = strchr(buffer, '='); + if (c == NULL) + continue; + key = buffer; + val = c + 1; + /* strip leading blanks from key */ + while (isspace(*key)) + *key++ = '\0'; + /* strip trailing blanks from key */ + do + *c = '\0'; + while (isspace(*--c)); + /* strip leading blanks from value */ + while (isspace(*val)) + *val++ = '\0'; + /* strip trailing blanks from value */ + for (c = val; *c != '\0'; c++); + while (isspace(*--c)) + *c = '\0'; + /* add entry to hash table */ + hash_put(&SETI, key, val); } - strcpy (fn, dir); - if (fn[strlen(fn)-1]!='/') - strcat (fn, "/"); - strcat (fn, STATEFILE); - free (dir); - } - - stream = fopen(fn, "r"); - if (stream == NULL) { - error ("fopen(%s) failed: %s", fn, strerror(errno)); - return -1; - } - - while (!feof(stream)) { - char buffer[256]; - char *c, *key, *val; - fgets (buffer, sizeof(buffer), stream); - c=strchr(buffer, '='); - if (c==NULL) continue; - key=buffer; val=c+1; - /* strip leading blanks from key */ - while (isspace(*key)) *key++='\0'; - /* strip trailing blanks from key */ - do *c='\0'; while (isspace(*--c)); - /* strip leading blanks from value */ - while (isspace(*val)) *val++='\0'; - /* strip trailing blanks from value */ - for (c=val; *c!='\0';c++); - while (isspace(*--c)) *c='\0'; - /* add entry to hash table */ - hash_put (&SETI, key, val); - } - - fclose (stream); - - return 0; -} - - -static void my_seti (RESULT *result, RESULT *arg1) + + fclose(stream); + + return 0; +} + + +static void my_seti(RESULT * result, RESULT * arg1) { - char *key, *val; - - if (parse_seti()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - key=R2S(arg1); - val=hash_get(&SETI, key, NULL); - if (val==NULL) val=""; - - SetResult(&result, R_STRING, val); + char *key, *val; + + if (parse_seti() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + key = R2S(arg1); + val = hash_get(&SETI, key, NULL); + if (val == NULL) + val = ""; + + SetResult(&result, R_STRING, val); } -int plugin_init_seti (void) +int plugin_init_seti(void) { - hash_create(&SETI); - AddFunction ("seti", 1, my_seti); - return 0; + hash_create(&SETI); + AddFunction("seti", 1, my_seti); + return 0; } -void plugin_exit_seti(void) +void plugin_exit_seti(void) { - hash_destroy(&SETI); + hash_destroy(&SETI); } diff --git a/plugin_statfs.c b/plugin_statfs.c index 9bf9510..d043e71 100644 --- a/plugin_statfs.c +++ b/plugin_statfs.c @@ -1,4 +1,4 @@ -/* $Id: plugin_statfs.c,v 1.1 2005/04/03 07:07:51 reinelt Exp $ +/* $Id: plugin_statfs.c,v 1.2 2005/05/08 04:32:45 reinelt Exp $ * * plugin for statfs() syscall * @@ -23,6 +23,9 @@ * * * $Log: plugin_statfs.c,v $ + * Revision 1.2 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.1 2005/04/03 07:07:51 reinelt * added statfs plugin * @@ -49,56 +52,56 @@ -static void my_statfs (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_statfs(RESULT * result, RESULT * arg1, RESULT * arg2) { - struct statfs buf; - char *path, *key; - double value; - - path = R2S(arg1); - key = R2S(arg2); - - if (statfs(path, &buf)!=0) { - error("statfs(%s) failed: %s", path, strerror(errno)); - SetResult(&result, R_STRING, ""); - return; - } + struct statfs buf; + char *path, *key; + double value; + + path = R2S(arg1); + key = R2S(arg2); + + if (statfs(path, &buf) != 0) { + error("statfs(%s) failed: %s", path, strerror(errno)); + SetResult(&result, R_STRING, ""); + return; + } - if (strcasecmp(key, "type")==0) { - value = buf.f_type; - } else if (strcasecmp(key, "bsize")==0) { - value = buf.f_bsize; - } else if (strcasecmp(key, "blocks")==0) { - value = buf.f_blocks; - } else if (strcasecmp(key, "bfree")==0) { - value = buf.f_bfree; - } else if (strcasecmp(key, "bavail")==0) { - value = buf.f_bavail; - } else if (strcasecmp(key, "files")==0) { - value = buf.f_files; - } else if (strcasecmp(key, "ffree")==0) { - value = buf.f_ffree; + if (strcasecmp(key, "type") == 0) { + value = buf.f_type; + } else if (strcasecmp(key, "bsize") == 0) { + value = buf.f_bsize; + } else if (strcasecmp(key, "blocks") == 0) { + value = buf.f_blocks; + } else if (strcasecmp(key, "bfree") == 0) { + value = buf.f_bfree; + } else if (strcasecmp(key, "bavail") == 0) { + value = buf.f_bavail; + } else if (strcasecmp(key, "files") == 0) { + value = buf.f_files; + } else if (strcasecmp(key, "ffree") == 0) { + value = buf.f_ffree; #if 0 - } else if (strcasecmp(key, "fsid")==0) { - value = buf.f_fsid; + } else if (strcasecmp(key, "fsid") == 0) { + value = buf.f_fsid; #endif - } else if (strcasecmp(key, "namelen")==0) { - value = buf.f_namelen; - } else { - error("statfs: unknown field '%s'", key); - value = -1; - } + } else if (strcasecmp(key, "namelen") == 0) { + value = buf.f_namelen; + } else { + error("statfs: unknown field '%s'", key); + value = -1; + } - SetResult(&result, R_NUMBER, &value); + SetResult(&result, R_NUMBER, &value); } -int plugin_init_statfs (void) +int plugin_init_statfs(void) { - AddFunction ("statfs", 2, my_statfs); - return 0; + AddFunction("statfs", 2, my_statfs); + return 0; } -void plugin_exit_statfs(void) +void plugin_exit_statfs(void) { } diff --git a/plugin_string.c b/plugin_string.c index c920e25..e1fd0a4 100644 --- a/plugin_string.c +++ b/plugin_string.c @@ -1,4 +1,4 @@ -/* $Id: plugin_string.c,v 1.7 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_string.c,v 1.8 2005/05/08 04:32:45 reinelt Exp $ * * string plugin * @@ -23,6 +23,9 @@ * * * $Log: plugin_string.c,v $ + * Revision 1.8 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.7 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -73,23 +76,23 @@ #include "plugin.h" -static void my_strlen (RESULT *result, RESULT *arg1) +static void my_strlen(RESULT * result, RESULT * arg1) { - double value=strlen(R2S(arg1)); - SetResult(&result, R_NUMBER, &value); + double value = strlen(R2S(arg1)); + SetResult(&result, R_NUMBER, &value); } -int plugin_init_string (void) +int plugin_init_string(void) { - /* register some basic string functions */ - AddFunction ("strlen", 1, my_strlen); + /* register some basic string functions */ + AddFunction("strlen", 1, my_strlen); - return 0; + return 0; } -void plugin_exit_string(void) +void plugin_exit_string(void) { - /* empty */ + /* empty */ } diff --git a/plugin_test.c b/plugin_test.c index 15a098b..1c4fb14 100644 --- a/plugin_test.c +++ b/plugin_test.c @@ -1,4 +1,4 @@ -/* $Id: plugin_test.c,v 1.4 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_test.c,v 1.5 2005/05/08 04:32:45 reinelt Exp $ * * Handy functions for testing displays and debugging code. * @@ -26,6 +26,9 @@ * * * $Log: plugin_test.c,v $ +* Revision 1.5 2005/05/08 04:32:45 reinelt +* CodingStyle added and applied +* * Revision 1.4 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -45,7 +48,7 @@ * */ -int plugin_init_test (void); +int plugin_init_test(void); #include "config.h" @@ -67,65 +70,64 @@ int plugin_init_test (void); * amount rdelta every time they are read. Starting value is rstart. * rbar gives the number of the test bar. */ -static void my_test_bar (RESULT *result, RESULT *rbar, RESULT *rmax, - RESULT *rstart, RESULT *rdelta) +static void my_test_bar(RESULT * result, RESULT * rbar, RESULT * rmax, RESULT * rstart, RESULT * rdelta) { - static double values[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; - static double deltas[10]; - int bar; - double max,delta,value; - - max=R2N(rmax); - delta=R2N(rdelta); - - /* the maths is just to stop double rounding errors and bad values. */ - bar=((int)floor(R2N(rbar)+0.1)) % 10; - if ( fabs(delta) > 0.1 ) { - /* don't move or init the bar if delta=0 (the widget is only browsing) */ - if (values[bar] == -1) { - /* first time called. */ - values[bar]=R2N(rstart); - deltas[bar]=delta; + static double values[10] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; + static double deltas[10]; + int bar; + double max, delta, value; + + max = R2N(rmax); + delta = R2N(rdelta); + + /* the maths is just to stop double rounding errors and bad values. */ + bar = ((int) floor(R2N(rbar) + 0.1)) % 10; + if (fabs(delta) > 0.1) { + /* don't move or init the bar if delta=0 (the widget is only browsing) */ + if (values[bar] == -1) { + /* first time called. */ + values[bar] = R2N(rstart); + deltas[bar] = delta; + }; + values[bar] += deltas[bar]; + }; + if (values[bar] < 0 || values[bar] > max) { + /* turn around. */ + deltas[bar] = -deltas[bar]; + values[bar] += deltas[bar]; }; - values[bar]+=deltas[bar]; - }; - if (values[bar] < 0 || values[bar] > max ) { - /* turn around. */ - deltas[bar]= -deltas[bar]; - values[bar]+= deltas[bar]; - }; - value=values[bar]; - SetResult(&result, R_NUMBER, &value); + value = values[bar]; + SetResult(&result, R_NUMBER, &value); } /* like above, but just switches a value between 1 and -1. Can use to test * visibility of icons. */ -static void my_test_onoff (RESULT *result, RESULT *arg1) +static void my_test_onoff(RESULT * result, RESULT * arg1) { - static int on[10] = {1,1,1,1,1,1,1,1,1,1}; - int i; - double val; - - i = ((int)floor(R2N(arg1)+0.1)) % 10; - on[i] = -on[i]; - val = (double)on[i]; - - SetResult(&result, R_NUMBER, &val); + static int on[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; + int i; + double val; + + i = ((int) floor(R2N(arg1) + 0.1)) % 10; + on[i] = -on[i]; + val = (double) on[i]; + + SetResult(&result, R_NUMBER, &val); } -int plugin_init_test (void) +int plugin_init_test(void) { - - AddFunction ("test::bar", 4, my_test_bar); - AddFunction ("test::onoff", 1, my_test_onoff); - - return 0; + + AddFunction("test::bar", 4, my_test_bar); + AddFunction("test::onoff", 1, my_test_onoff); + + return 0; } void plugin_exit_test(void) { - /* empty */ + /* empty */ } diff --git a/plugin_time.c b/plugin_time.c index 95d224c..0a01158 100644 --- a/plugin_time.c +++ b/plugin_time.c @@ -1,4 +1,4 @@ -/* $Id: plugin_time.c,v 1.4 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_time.c,v 1.5 2005/05/08 04:32:45 reinelt Exp $ * * time plugin * @@ -23,6 +23,9 @@ * * * $Log: plugin_time.c,v $ + * Revision 1.5 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.4 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -58,36 +61,36 @@ #include "plugin.h" -static void my_time (RESULT *result) +static void my_time(RESULT * result) { - double value = time(NULL); - SetResult(&result, R_NUMBER, &value); + double value = time(NULL); + SetResult(&result, R_NUMBER, &value); } -static void my_strftime (RESULT *result, RESULT *arg1, RESULT *arg2) +static void my_strftime(RESULT * result, RESULT * arg1, RESULT * arg2) { - char value[256]; - time_t t = R2N(arg2); - - value[0] = '\0'; - strftime(value, sizeof(value), R2S(arg1), localtime(&t)); + char value[256]; + time_t t = R2N(arg2); + + value[0] = '\0'; + strftime(value, sizeof(value), R2S(arg1), localtime(&t)); - SetResult(&result, R_STRING, value); + SetResult(&result, R_STRING, value); } -int plugin_init_time (void) +int plugin_init_time(void) { - /* register some basic time functions */ - AddFunction ("time", 0, my_time); - AddFunction ("strftime", 2, my_strftime); + /* register some basic time functions */ + AddFunction("time", 0, my_time); + AddFunction("strftime", 2, my_strftime); - return 0; + return 0; } -void plugin_exit_time(void) +void plugin_exit_time(void) { - /* empty */ + /* empty */ } diff --git a/plugin_uname.c b/plugin_uname.c index 1697805..ad7fa06 100644 --- a/plugin_uname.c +++ b/plugin_uname.c @@ -1,4 +1,4 @@ -/* $Id: plugin_uname.c,v 1.5 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_uname.c,v 1.6 2005/05/08 04:32:45 reinelt Exp $ * * plugin for uname() syscall * @@ -23,6 +23,9 @@ * * * $Log: plugin_uname.c,v $ + * Revision 1.6 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.5 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -68,48 +71,48 @@ -static void my_uname (RESULT *result, RESULT *arg1) +static void my_uname(RESULT * result, RESULT * arg1) { - struct utsname utsbuf; - char *key, *value; - - key=R2S(arg1); - - if (uname(&utsbuf)!=0) { - error("uname() failed: %s", strerror(errno)); - SetResult(&result, R_STRING, ""); - return; - } + struct utsname utsbuf; + char *key, *value; + + key = R2S(arg1); + + if (uname(&utsbuf) != 0) { + error("uname() failed: %s", strerror(errno)); + SetResult(&result, R_STRING, ""); + return; + } - if (strcasecmp(key, "sysname")==0) { - value=utsbuf.sysname; - } else if (strcasecmp(key, "nodename")==0) { - value=utsbuf.nodename; - } else if (strcasecmp(key, "release")==0) { - value=utsbuf.release; - } else if (strcasecmp(key, "version")==0) { - value=utsbuf.version; - } else if (strcasecmp(key, "machine")==0) { - value=utsbuf.machine; + if (strcasecmp(key, "sysname") == 0) { + value = utsbuf.sysname; + } else if (strcasecmp(key, "nodename") == 0) { + value = utsbuf.nodename; + } else if (strcasecmp(key, "release") == 0) { + value = utsbuf.release; + } else if (strcasecmp(key, "version") == 0) { + value = utsbuf.version; + } else if (strcasecmp(key, "machine") == 0) { + value = utsbuf.machine; #ifdef _GNU_SOURCE - } else if (strcasecmp(key, "domainname")==0) { - value=utsbuf.domainname; + } else if (strcasecmp(key, "domainname") == 0) { + value = utsbuf.domainname; #endif - } else { - error("uname: unknown field '%s'", key); - value=""; - } + } else { + error("uname: unknown field '%s'", key); + value = ""; + } - SetResult(&result, R_STRING, value); + SetResult(&result, R_STRING, value); } -int plugin_init_uname (void) +int plugin_init_uname(void) { - AddFunction ("uname", 1, my_uname); - return 0; + AddFunction("uname", 1, my_uname); + return 0; } -void plugin_exit_uname(void) +void plugin_exit_uname(void) { } diff --git a/plugin_uptime.c b/plugin_uptime.c index 90969d4..f15d86b 100644 --- a/plugin_uptime.c +++ b/plugin_uptime.c @@ -1,4 +1,4 @@ -/* $Id: plugin_uptime.c,v 1.5 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_uptime.c,v 1.6 2005/05/08 04:32:45 reinelt Exp $ * * plugin for uptime * @@ -23,6 +23,9 @@ * * * $Log: plugin_uptime.c,v $ + * Revision 1.6 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.5 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -73,182 +76,188 @@ static int fd = -2; -static char *itoa(char* buffer, const size_t size, unsigned int value) +static char *itoa(char *buffer, const size_t size, unsigned int value) { - char *p; - - /* sanity checks */ - if (buffer==NULL || size<2) return (NULL); - - /* p points to last char */ - p = buffer+size-1; - - /* set terminating zero */ - *p='\0'; - - do { - *--p = value%10 + '0'; - value = value/10; - } while (value!=0 && p>buffer); - - return p; -} - - -char *struptime (const unsigned int uptime, const char *format) + char *p; + + /* sanity checks */ + if (buffer == NULL || size < 2) + return (NULL); + + /* p points to last char */ + p = buffer + size - 1; + + /* set terminating zero */ + *p = '\0'; + + do { + *--p = value % 10 + '0'; + value = value / 10; + } while (value != 0 && p > buffer); + + return p; +} + + +char *struptime(const unsigned int uptime, const char *format) { - static char string[256]; - const char *src; - char *dst; - int len, size; - - src = format; - dst = string; - len = 0; - - /* leave room for terminating zero */ - size = sizeof(string) - 1; - - while (len < size) { - - if (*src == '%') { - src++; - - if (strchr ("sSmMhHd", *src) != NULL) { - char buffer[12], *s; - unsigned int value = 0; - int leading_zero = 0; - switch (*src++) { - case 's': - value = uptime; - break; - case 'S': - value = uptime % 60; - leading_zero = 1; - break; - case 'm': - value = uptime / 60; - break; - case 'M': - value = (uptime / 60) % 60; - leading_zero = 1; - break; - case 'h': - value = uptime / 60 / 60; - break; - case 'H': - value = (uptime / 60 / 60) % 24; - leading_zero = 1; - break; - case 'd': - value = uptime / 60 / 60 / 24; - break; - } - - if (leading_zero && value < 10) { - len++; - *dst++ = '0'; - } + static char string[256]; + const char *src; + char *dst; + int len, size; - s = itoa (buffer, sizeof(buffer), value); - while (len < size && *s != '\0') { - len++; - *dst++ = *s++; - } + src = format; + dst = string; + len = 0; - } else if (*src == '%') { - len++; - *dst++ = '%'; + /* leave room for terminating zero */ + size = sizeof(string) - 1; - } else { - len += 2; - *dst++ = '%'; - *dst++ = *src++; - } + while (len < size) { - } else { - len++; - *dst++ = *src; - if (*src++ == '\0') break; + if (*src == '%') { + src++; + + if (strchr("sSmMhHd", *src) != NULL) { + char buffer[12], *s; + unsigned int value = 0; + int leading_zero = 0; + switch (*src++) { + case 's': + value = uptime; + break; + case 'S': + value = uptime % 60; + leading_zero = 1; + break; + case 'm': + value = uptime / 60; + break; + case 'M': + value = (uptime / 60) % 60; + leading_zero = 1; + break; + case 'h': + value = uptime / 60 / 60; + break; + case 'H': + value = (uptime / 60 / 60) % 24; + leading_zero = 1; + break; + case 'd': + value = uptime / 60 / 60 / 24; + break; + } + + if (leading_zero && value < 10) { + len++; + *dst++ = '0'; + } + + s = itoa(buffer, sizeof(buffer), value); + while (len < size && *s != '\0') { + len++; + *dst++ = *s++; + } + + } else if (*src == '%') { + len++; + *dst++ = '%'; + + } else { + len += 2; + *dst++ = '%'; + *dst++ = *src++; + } + + } else { + len++; + *dst++ = *src; + if (*src++ == '\0') + break; + } } - } - - /* enforce terminating zero */ - if (len >= size && *(dst-1) != '\0') { - len++; - *dst = '\0'; - } - - return string; + + /* enforce terminating zero */ + if (len >= size && *(dst - 1) != '\0') { + len++; + *dst = '\0'; + } + + return string; } -double getuptime (void) +double getuptime(void) { - char buffer[36]; - int i; - - if (fd == -2) fd = open ("/proc/uptime", O_RDONLY); - if (fd < 0) return -1; + char buffer[36]; + int i; - lseek(fd, 0, SEEK_SET); + if (fd == -2) + fd = open("/proc/uptime", O_RDONLY); + if (fd < 0) + return -1; - i = read (fd, buffer, sizeof(buffer) - 1); - if (i < 0) return -1; + lseek(fd, 0, SEEK_SET); - buffer[i-1] = '\0'; - - /* ignore the 2nd value from /proc/uptime */ - return strtod(buffer, NULL); + i = read(fd, buffer, sizeof(buffer) - 1); + if (i < 0) + return -1; + + buffer[i - 1] = '\0'; + + /* ignore the 2nd value from /proc/uptime */ + return strtod(buffer, NULL); } -static void my_uptime (RESULT *result, const int argc, RESULT *argv[]) +static void my_uptime(RESULT * result, const int argc, RESULT * argv[]) { - int age; - static double uptime = 0.0; - static struct timeval last_value; - struct timeval now; - - if (argc>1) { - error ("uptime(): wrong number of parameters"); - SetResult(&result, R_STRING, ""); - return; - } - - gettimeofday(&now,NULL); - - age = (now.tv_sec - last_value.tv_sec)*1000 + (now.tv_usec - last_value.tv_usec)/1000; - /* reread every 100 msec only */ - if (fd == -2 || age == 0 || age > 100) { - uptime = getuptime(); - if (uptime < 0.0) { - error ("parse(/proc/uptime) failed!"); - SetResult(&result, R_STRING, ""); - return; + int age; + static double uptime = 0.0; + static struct timeval last_value; + struct timeval now; + + if (argc > 1) { + error("uptime(): wrong number of parameters"); + SetResult(&result, R_STRING, ""); + return; } - last_value = now; - } - - if (argc == 0) { - SetResult (&result, R_NUMBER, &uptime); - } else { - SetResult (&result, R_STRING, struptime(uptime, R2S(argv[0]))); - } - - return; - + gettimeofday(&now, NULL); + + age = (now.tv_sec - last_value.tv_sec) * 1000 + (now.tv_usec - last_value.tv_usec) / 1000; + /* reread every 100 msec only */ + if (fd == -2 || age == 0 || age > 100) { + uptime = getuptime(); + if (uptime < 0.0) { + error("parse(/proc/uptime) failed!"); + SetResult(&result, R_STRING, ""); + return; + } + + last_value = now; + } + + if (argc == 0) { + SetResult(&result, R_NUMBER, &uptime); + } else { + SetResult(&result, R_STRING, struptime(uptime, R2S(argv[0]))); + } + + return; + } -int plugin_init_uptime (void) +int plugin_init_uptime(void) { - AddFunction ("uptime", -1, my_uptime); - return 0; + AddFunction("uptime", -1, my_uptime); + return 0; } -void plugin_exit_uptime(void) +void plugin_exit_uptime(void) { - if (fd > 0) close(fd); - fd = -2; + if (fd > 0) + close(fd); + fd = -2; } diff --git a/plugin_wireless.c b/plugin_wireless.c index 8f85c5b..1823776 100755 --- a/plugin_wireless.c +++ b/plugin_wireless.c @@ -1,4 +1,4 @@ -/* $Id: plugin_wireless.c,v 1.8 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_wireless.c,v 1.9 2005/05/08 04:32:45 reinelt Exp $ * * Wireless Extension plugin * @@ -28,6 +28,9 @@ * * * $Log: plugin_wireless.c,v $ + * Revision 1.9 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.8 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -60,10 +63,10 @@ * New plugin for wireless info * */ - + /* * Exported functions: - + wifi_level wifi_noise wifi_quality @@ -74,10 +77,10 @@ wifi_op_mode wifi_sensitivity wifi_sec_mode - + All Functions take one parameter (the name of the device, like "wlan0", "ath0" and so on) - + */ #include "config.h" @@ -104,12 +107,12 @@ #endif #define HASH_TTL 100 - + /*#define KILO 1e3 */ /*#define MEGA 1e6 */ /*#define GIGA 1e9 */ - - + + #define KEY_LEVEL "level" #define KEY_QUALITY "quality" #define KEY_NOISE "noise" @@ -126,229 +129,234 @@ static HASH wireless; -static int sock=-2; - -static char *operation_mode[] = { - "Auto", - "Ad-Hoc", - "Managed", - "Master", - "Repeater", - "Secondary", - "Monitor", - "n/a" - }; - - -static void ioctl_error(const int line) { - error("IOCTL call to wireless extensions in line %d returned error", line); -} +static int sock = -2; -int do_ioctl(const int sock, /* Socket to the kernel */ - const char * ifname, /* Device name */ - const int request, /* WE ID */ - struct iwreq * pwrq) /* Fixed part of the request */ +static char *operation_mode[] = { + "Auto", + "Ad-Hoc", + "Managed", + "Master", + "Repeater", + "Secondary", + "Monitor", + "n/a" +}; + + +static void ioctl_error(const int line) { - /* Set device name */ - strncpy(pwrq->ifr_name, ifname, IFNAMSIZ); + error("IOCTL call to wireless extensions in line %d returned error", line); +} - /* Do the request */ - return(ioctl(sock, request, pwrq)); +int do_ioctl(const int sock, /* Socket to the kernel */ + const char *ifname, /* Device name */ + const int request, /* WE ID */ + struct iwreq *pwrq) +{ /* Fixed part of the request */ + /* Set device name */ + strncpy(pwrq->ifr_name, ifname, IFNAMSIZ); + + /* Do the request */ + return (ioctl(sock, request, pwrq)); } -int get_range_info(const int sock, - const char * ifname, - struct iw_range *range) +int get_range_info(const int sock, const char *ifname, struct iw_range *range) { - struct iwreq req; - char buffer[sizeof(struct iw_range) * 2]; /* Large enough */ + struct iwreq req; + char buffer[sizeof(struct iw_range) * 2]; /* Large enough */ - if (sock<=0) { - return(-1); - } + if (sock <= 0) { + return (-1); + } - /* Cleanup */ - memset(buffer, 0, sizeof(buffer)); + /* Cleanup */ + memset(buffer, 0, sizeof(buffer)); - req.u.data.pointer = (caddr_t) buffer; - req.u.data.length = sizeof(buffer); - req.u.data.flags = 0; - if(do_ioctl(sock, ifname, SIOCGIWRANGE, &req) < 0) - return(-1); + req.u.data.pointer = (caddr_t) buffer; + req.u.data.length = sizeof(buffer); + req.u.data.flags = 0; + if (do_ioctl(sock, ifname, SIOCGIWRANGE, &req) < 0) + return (-1); - /* Copy stuff at the right place, ignore extra */ - memcpy((char *) range, buffer, sizeof(struct iw_range)); + /* Copy stuff at the right place, ignore extra */ + memcpy((char *) range, buffer, sizeof(struct iw_range)); - return(0); + return (0); } -static int get_ifname(struct iwreq *preq, const char *dev) { - /* do not cache this call !!! */ - struct iwreq req; - char key_buffer[32]; - - if (sock<=0) { - return(-1); - } - - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,KEY_PROTO); - - if (!preq) preq=&req; +static int get_ifname(struct iwreq *preq, const char *dev) +{ + /* do not cache this call !!! */ + struct iwreq req; + char key_buffer[32]; + + if (sock <= 0) { + return (-1); + } - if(do_ioctl(sock, dev, SIOCGIWNAME, preq)) { - debug("%s isn't a wirelesss interface !", dev); - return -1; - } + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_PROTO); - hash_put(&wireless,key_buffer, preq->u.name); - return(0); + if (!preq) + preq = &req; + + if (do_ioctl(sock, dev, SIOCGIWNAME, preq)) { + debug("%s isn't a wirelesss interface !", dev); + return -1; + } + + hash_put(&wireless, key_buffer, preq->u.name); + return (0); } -static int get_frequency(const char* dev,const char* key) { - /* Get frequency / channel */ - struct iwreq req; - char qprintf_buffer[1024]; - char key_buffer[32]; - double freq; - int age; - - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key); - age=hash_age(&wireless, key); - - /* reread every HASH_TTL msec only */ - if (age>0 && age<=HASH_TTL) { - return(0); - } - - if (get_ifname(&req,dev)!=0) { - return(-1); - } - - req.u.data.length = 0; - req.u.data.flags = 1; /* Clear updated flag */ - - if(do_ioctl(sock, dev, SIOCGIWFREQ, &req) < 0) { - ioctl_error(__LINE__); - return -1; - } - - freq = FREQ2FLOAT(req.u.freq.m, req.u.freq.e); - - /*if( freq>= GIGA) - snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g GHz", freq / GIGA); - else - if(freq>= MEGA) - snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g MHz", freq / MEGA); - else - snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g kHz", freq / KILO); -*/ - snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g", freq); +static int get_frequency(const char *dev, const char *key) +{ + /* Get frequency / channel */ + struct iwreq req; + char qprintf_buffer[1024]; + char key_buffer[32]; + double freq; + int age; + + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); + age = hash_age(&wireless, key); + + /* reread every HASH_TTL msec only */ + if (age > 0 && age <= HASH_TTL) { + return (0); + } + + if (get_ifname(&req, dev) != 0) { + return (-1); + } + + req.u.data.length = 0; + req.u.data.flags = 1; /* Clear updated flag */ + + if (do_ioctl(sock, dev, SIOCGIWFREQ, &req) < 0) { + ioctl_error(__LINE__); + return -1; + } + + freq = FREQ2FLOAT(req.u.freq.m, req.u.freq.e); + + /*if( freq>= GIGA) + snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g GHz", freq / GIGA); + else + if(freq>= MEGA) + snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g MHz", freq / MEGA); + else + snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g kHz", freq / KILO); + */ + snprintf(qprintf_buffer, sizeof(qprintf_buffer), "%g", freq); + + hash_put(&wireless, key_buffer, qprintf_buffer); + return (0); - hash_put(&wireless, key_buffer, qprintf_buffer); - return(0); - } -static int get_essid(const char* dev, const char* key) { - /* Get ESSID */ - struct iwreq req; - char key_buffer[32]; - char essid_buffer[IW_ESSID_MAX_SIZE+1]; - int age; - - - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key); - age=hash_age(&wireless, key); - - /* reread every HASH_TTL msec only */ - if (age>0 && age<=HASH_TTL) { - return(0); - } - - if (get_ifname(&req,dev)!=0) { - return(-1); - } - - memset(essid_buffer, 0, sizeof(essid_buffer)); - req.u.essid.pointer = (caddr_t) essid_buffer; - req.u.essid.length = IW_ESSID_MAX_SIZE + 1; - req.u.essid.flags = 0; - - if(do_ioctl(sock, dev,SIOCGIWESSID, &req) < 0) { - ioctl_error(__LINE__); - return -1; - } - - hash_put(&wireless,key_buffer, essid_buffer); - return(0); - +static int get_essid(const char *dev, const char *key) +{ + /* Get ESSID */ + struct iwreq req; + char key_buffer[32]; + char essid_buffer[IW_ESSID_MAX_SIZE + 1]; + int age; + + + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); + age = hash_age(&wireless, key); + + /* reread every HASH_TTL msec only */ + if (age > 0 && age <= HASH_TTL) { + return (0); + } + + if (get_ifname(&req, dev) != 0) { + return (-1); + } + + memset(essid_buffer, 0, sizeof(essid_buffer)); + req.u.essid.pointer = (caddr_t) essid_buffer; + req.u.essid.length = IW_ESSID_MAX_SIZE + 1; + req.u.essid.flags = 0; + + if (do_ioctl(sock, dev, SIOCGIWESSID, &req) < 0) { + ioctl_error(__LINE__); + return -1; + } + + hash_put(&wireless, key_buffer, essid_buffer); + return (0); + } -static int get_op_mode(const char* dev, const char* key) { - /* Get operation mode */ - struct iwreq req; - char key_buffer[32]; - int age; - - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key); - age=hash_age(&wireless, key); - - /* reread every HASH_TTL msec only */ - if (age>0 && age<=HASH_TTL) { - return(0); - } - - if (get_ifname(&req,dev)!=0) { - return(-1); - } - - if(do_ioctl(sock, dev,SIOCGIWMODE, &req) < 0) { - ioctl_error(__LINE__); - return -1; - } - - /* Fixme: req.u.mode is unsigned and therefore never < 0 */ - /* if((req.u.mode > 6) || (req.u.mode < 0)) { */ - - if(req.u.mode > 6) { - req.u.mode=7; /* mode not available */ - } - - hash_put(&wireless,key_buffer, operation_mode[req.u.mode]); - return(0); - +static int get_op_mode(const char *dev, const char *key) +{ + /* Get operation mode */ + struct iwreq req; + char key_buffer[32]; + int age; + + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); + age = hash_age(&wireless, key); + + /* reread every HASH_TTL msec only */ + if (age > 0 && age <= HASH_TTL) { + return (0); + } + + if (get_ifname(&req, dev) != 0) { + return (-1); + } + + if (do_ioctl(sock, dev, SIOCGIWMODE, &req) < 0) { + ioctl_error(__LINE__); + return -1; + } + + /* Fixme: req.u.mode is unsigned and therefore never < 0 */ + /* if((req.u.mode > 6) || (req.u.mode < 0)) { */ + + if (req.u.mode > 6) { + req.u.mode = 7; /* mode not available */ + } + + hash_put(&wireless, key_buffer, operation_mode[req.u.mode]); + return (0); + } -static int get_bitrate(const char* dev, const char* key) { - /* Get bit rate */ - struct iwreq req; - char key_buffer[32]; - char bitrate_buffer[64]; - int age; - double bitrate=0; - - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key); - age=hash_age(&wireless, key); - - /* reread every HASH_TTL msec only */ - if (age>0 && age<=HASH_TTL) { - return(0); - } - - if (get_ifname(&req,dev)!=0) { - return(-1); - } - - if(do_ioctl(sock, dev,SIOCGIWRATE, &req) < 0) { - ioctl_error(__LINE__); - return -1; - } - - bitrate_buffer[0]='\0'; - bitrate = (double)(req.u.bitrate.value); +static int get_bitrate(const char *dev, const char *key) +{ + /* Get bit rate */ + struct iwreq req; + char key_buffer[32]; + char bitrate_buffer[64]; + int age; + double bitrate = 0; + + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); + age = hash_age(&wireless, key); + + /* reread every HASH_TTL msec only */ + if (age > 0 && age <= HASH_TTL) { + return (0); + } + + if (get_ifname(&req, dev) != 0) { + return (-1); + } + + if (do_ioctl(sock, dev, SIOCGIWRATE, &req) < 0) { + ioctl_error(__LINE__); + return -1; + } + + bitrate_buffer[0] = '\0'; + bitrate = (double) (req.u.bitrate.value); /* if( bitrate>= GIGA) snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%gGb/s", bitrate / GIGA); else @@ -356,288 +364,310 @@ static int get_bitrate(const char* dev, const char* key) { snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%gMb/s", bitrate / MEGA); else snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%gkb/s", bitrate / KILO); -*/ - snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%g", bitrate); - - hash_put(&wireless,key_buffer, bitrate_buffer); - - return(0); +*/ + snprintf(bitrate_buffer, sizeof(bitrate_buffer), "%g", bitrate); + + hash_put(&wireless, key_buffer, bitrate_buffer); + + return (0); } - -static int get_sens(const char* dev, const char* key) { - /* Get sensitivity */ - struct iwreq req; - struct iw_range range; - char key_buffer[32]; - char buffer[64]; - int age; - int has_sens =0; - int has_range =0; - - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key); - age=hash_age(&wireless, key); - - /* reread every HASH_TTL msec only */ - if (age>0 && age<=HASH_TTL) { - return(0); - } - - if (get_ifname(&req,dev)!=0) { - return(-1); - } - - if(do_ioctl(sock, dev, SIOCGIWSENS, &req) >= 0) - { - has_sens = 1; - } - - if(get_range_info(sock, dev, &range) < 0) { - memset(&range, 0, sizeof(range)); - } else { - has_range=1; - } - - if(has_range) { - if(req.u.sens.value < 0) { - qprintf(buffer, sizeof(buffer), "%d dBm", req.u.sens.value); + +static int get_sens(const char *dev, const char *key) +{ + /* Get sensitivity */ + struct iwreq req; + struct iw_range range; + char key_buffer[32]; + char buffer[64]; + int age; + int has_sens = 0; + int has_range = 0; + + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); + age = hash_age(&wireless, key); + + /* reread every HASH_TTL msec only */ + if (age > 0 && age <= HASH_TTL) { + return (0); } - else - { - qprintf(buffer, sizeof(buffer), "%d/%d", req.u.sens.value, range.sensitivity); + + if (get_ifname(&req, dev) != 0) { + return (-1); } - } else { - qprintf(buffer, sizeof(buffer), "%d", req.u.sens.value); - } - hash_put(&wireless,key_buffer, buffer); - return(0); + if (do_ioctl(sock, dev, SIOCGIWSENS, &req) >= 0) { + has_sens = 1; + } + + if (get_range_info(sock, dev, &range) < 0) { + memset(&range, 0, sizeof(range)); + } else { + has_range = 1; + } + + if (has_range) { + if (req.u.sens.value < 0) { + qprintf(buffer, sizeof(buffer), "%d dBm", req.u.sens.value); + } else { + qprintf(buffer, sizeof(buffer), "%d/%d", req.u.sens.value, range.sensitivity); + } + } else { + qprintf(buffer, sizeof(buffer), "%d", req.u.sens.value); + } + + hash_put(&wireless, key_buffer, buffer); + return (0); } - - -static int get_sec_mode(const char* dev, const char* key) { - /* Get encryption information */ - struct iwreq req; - char key_buffer[32]; - char encrypt_key[IW_ENCODING_TOKEN_MAX+1]; - int age; - int has_key=0; - int key_flags=0; - int key_size=0; - - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key); - age=hash_age(&wireless, key); - - /* reread every HASH_TTL msec only */ - if (age>0 && age<=HASH_TTL) { - return(0); - } - - if (get_ifname(&req,dev)!=0) { - return(-1); - } - - req.u.data.pointer = (caddr_t) &encrypt_key; - req.u.data.length = IW_ENCODING_TOKEN_MAX; - req.u.data.flags = 0; - - if(do_ioctl(sock, dev, SIOCGIWENCODE, &req) >= 0) - { - has_key = 1; - key_flags = req.u.data.flags; - key_size = req.u.data.length; - } else { - return(-1); - } - - /* Display encryption information */ - /*if(has_key && (key_flags & IW_ENCODE_INDEX) > 1) */ - /* printf(" [%d]", info->key_flags & IW_ENCODE_INDEX); */ - - if(has_key && (key_flags & IW_ENCODE_RESTRICTED)) - hash_put(&wireless,key_buffer, "restricted"); - else if(has_key && (key_flags & IW_ENCODE_OPEN)) - hash_put(&wireless,key_buffer, "open"); - - return(0); + + +static int get_sec_mode(const char *dev, const char *key) +{ + /* Get encryption information */ + struct iwreq req; + char key_buffer[32]; + char encrypt_key[IW_ENCODING_TOKEN_MAX + 1]; + int age; + int has_key = 0; + int key_flags = 0; + int key_size = 0; + + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); + age = hash_age(&wireless, key); + + /* reread every HASH_TTL msec only */ + if (age > 0 && age <= HASH_TTL) { + return (0); + } + + if (get_ifname(&req, dev) != 0) { + return (-1); + } + + req.u.data.pointer = (caddr_t) & encrypt_key; + req.u.data.length = IW_ENCODING_TOKEN_MAX; + req.u.data.flags = 0; + + if (do_ioctl(sock, dev, SIOCGIWENCODE, &req) >= 0) { + has_key = 1; + key_flags = req.u.data.flags; + key_size = req.u.data.length; + } else { + return (-1); + } + + /* Display encryption information */ + /*if(has_key && (key_flags & IW_ENCODE_INDEX) > 1) */ + /* printf(" [%d]", info->key_flags & IW_ENCODE_INDEX); */ + + if (has_key && (key_flags & IW_ENCODE_RESTRICTED)) + hash_put(&wireless, key_buffer, "restricted"); + else if (has_key && (key_flags & IW_ENCODE_OPEN)) + hash_put(&wireless, key_buffer, "open"); + + return (0); } static int get_stats(const char *dev, const char *key) { - struct iw_statistics stats; - struct iwreq req; - char qprintf_buffer[1024]; - char key_buffer[32]; - int age; - struct iw_range range; - - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev,key); - age=hash_age(&wireless, key); - - /* reread every HASH_TTL msec only */ - if (age>0 && age<=HASH_TTL) { - return(0); - } - - if (get_ifname(&req,dev)!=0) { - return(-1); - } - - req.u.data.pointer = (caddr_t) &stats; - req.u.data.length = 0; - req.u.data.flags = 1; /* Clear updated flag */ - - if(do_ioctl(sock, dev,SIOCGIWSTATS, &req) < 0) { - ioctl_error(__LINE__); - return -1; - } - - if(get_range_info(sock, dev, &range) < 0) - memset(&range, 0, sizeof(range)); - - if(stats.qual.level > range.max_qual.level) { - qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d", stats.qual.level-0x100); - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_LEVEL); - hash_put(&wireless,key_buffer, qprintf_buffer); - - qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d", stats.qual.noise-0x100); - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_NOISE); - hash_put(&wireless,key_buffer, qprintf_buffer); - - qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.qual,range.max_qual.qual); - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_QUALITY); - hash_put(&wireless,key_buffer, qprintf_buffer); - } else { - - qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.level, range.max_qual.level); - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_LEVEL);; - hash_put(&wireless,key_buffer, qprintf_buffer); - - qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.noise,range.max_qual.noise); - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_NOISE); - hash_put(&wireless,key_buffer, qprintf_buffer); - - qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.qual, range.max_qual.qual); - qprintf(key_buffer,sizeof(key_buffer), "%s.%s", dev, KEY_QUALITY); - hash_put(&wireless,key_buffer, qprintf_buffer); - } - - return 0; + struct iw_statistics stats; + struct iwreq req; + char qprintf_buffer[1024]; + char key_buffer[32]; + int age; + struct iw_range range; + + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); + age = hash_age(&wireless, key); + + /* reread every HASH_TTL msec only */ + if (age > 0 && age <= HASH_TTL) { + return (0); + } + + if (get_ifname(&req, dev) != 0) { + return (-1); + } + + req.u.data.pointer = (caddr_t) & stats; + req.u.data.length = 0; + req.u.data.flags = 1; /* Clear updated flag */ + + if (do_ioctl(sock, dev, SIOCGIWSTATS, &req) < 0) { + ioctl_error(__LINE__); + return -1; + } + + if (get_range_info(sock, dev, &range) < 0) + memset(&range, 0, sizeof(range)); + + if (stats.qual.level > range.max_qual.level) { + qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d", stats.qual.level - 0x100); + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_LEVEL); + hash_put(&wireless, key_buffer, qprintf_buffer); + + qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d", stats.qual.noise - 0x100); + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_NOISE); + hash_put(&wireless, key_buffer, qprintf_buffer); + + qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.qual, range.max_qual.qual); + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_QUALITY); + hash_put(&wireless, key_buffer, qprintf_buffer); + } else { + + qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.level, range.max_qual.level); + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_LEVEL);; + hash_put(&wireless, key_buffer, qprintf_buffer); + + qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.noise, range.max_qual.noise); + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_NOISE); + hash_put(&wireless, key_buffer, qprintf_buffer); + + qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.qual, range.max_qual.qual); + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_QUALITY); + hash_put(&wireless, key_buffer, qprintf_buffer); + } + + return 0; } -static int check_socket() { - - /* already handled in a previous run */ - if (sock==-3) return(-1); - - /* socket not initialized */ - if (sock==-2) sock = socket(AF_INET, SOCK_DGRAM, 0); - - /* error initilalizing socket */ - if (sock<=0) { - error("Error opening socket for reading wireless stats"); - sock=-3; - return(-1); - } - - return(0); - +static int check_socket() +{ + + /* already handled in a previous run */ + if (sock == -3) + return (-1); + + /* socket not initialized */ + if (sock == -2) + sock = socket(AF_INET, SOCK_DGRAM, 0); + + /* error initilalizing socket */ + if (sock <= 0) { + error("Error opening socket for reading wireless stats"); + sock = -3; + return (-1); + } + + return (0); + } -static void save_result(RESULT *result, const char* dev, const char* key, const int res) { - char key_buffer[64]; - char* val=NULL; - qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev,key); - - if (res<0) { - SetResult(&result, R_STRING, ""); - return; - } - - val=hash_get(&wireless,key_buffer, NULL); - - if (val) { - SetResult(&result, R_STRING, val); - } else { - SetResult(&result, R_STRING, ""); - } - - +static void save_result(RESULT * result, const char *dev, const char *key, const int res) +{ + char key_buffer[64]; + char *val = NULL; + qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); + + if (res < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + val = hash_get(&wireless, key_buffer, NULL); + + if (val) { + SetResult(&result, R_STRING, val); + } else { + SetResult(&result, R_STRING, ""); + } + + } /* *functions exported to the evaluator */ -static void wireless_quality(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_QUALITY,get_stats(dev, KEY_QUALITY)); +static void wireless_quality(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_QUALITY, get_stats(dev, KEY_QUALITY)); } -static void wireless_level(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_LEVEL,get_stats(dev, KEY_LEVEL)); +static void wireless_level(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_LEVEL, get_stats(dev, KEY_LEVEL)); } -static void wireless_noise(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_NOISE,get_stats(dev, KEY_NOISE)); +static void wireless_noise(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_NOISE, get_stats(dev, KEY_NOISE)); } -static void wireless_protocol(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_PROTO,get_ifname(NULL, dev)); +static void wireless_protocol(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_PROTO, get_ifname(NULL, dev)); } -static void wireless_frequency(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_FREQUENCY,get_frequency(dev, KEY_FREQUENCY)); +static void wireless_frequency(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_FREQUENCY, get_frequency(dev, KEY_FREQUENCY)); } -static void wireless_bitrate(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_BIT_RATE,get_bitrate(dev, KEY_BIT_RATE)); +static void wireless_bitrate(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_BIT_RATE, get_bitrate(dev, KEY_BIT_RATE)); } -static void wireless_essid(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_ESSID,get_essid(dev, KEY_ESSID)); +static void wireless_essid(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_ESSID, get_essid(dev, KEY_ESSID)); } -static void wireless_op_mode(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_OP_MODE,get_op_mode(dev, KEY_OP_MODE)); +static void wireless_op_mode(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_OP_MODE, get_op_mode(dev, KEY_OP_MODE)); } -static void wireless_sensitivity(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_SENS,get_sens(dev, KEY_SENS)); +static void wireless_sensitivity(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_SENS, get_sens(dev, KEY_SENS)); } -static void wireless_sec_mode(RESULT *result, RESULT *arg1) { - char *dev=R2S(arg1); - if (check_socket() != 0) return; - - save_result(result,dev,KEY_SEC_MODE,get_sec_mode(dev, KEY_SEC_MODE)); +static void wireless_sec_mode(RESULT * result, RESULT * arg1) +{ + char *dev = R2S(arg1); + if (check_socket() != 0) + return; + + save_result(result, dev, KEY_SEC_MODE, get_sec_mode(dev, KEY_SEC_MODE)); } /* @@ -646,24 +676,25 @@ init and cleanup int plugin_init_wireless(void) { - hash_create(&wireless); - - AddFunction ("wifi::level", 1, wireless_level); - AddFunction ("wifi::noise", 1, wireless_noise); - AddFunction ("wifi::quality", 1, wireless_quality); - AddFunction ("wifi::protocol", 1, wireless_protocol); - AddFunction ("wifi::frequency", 1, wireless_frequency); - AddFunction ("wifi::bitrate", 1, wireless_bitrate); - AddFunction ("wifi::essid", 1, wireless_essid); - AddFunction ("wifi::op_mode", 1, wireless_op_mode); - AddFunction ("wifi::sensitivity", 1, wireless_sensitivity); - AddFunction ("wifi::sec_mode", 1, wireless_sec_mode); - - return 0; + hash_create(&wireless); + + AddFunction("wifi::level", 1, wireless_level); + AddFunction("wifi::noise", 1, wireless_noise); + AddFunction("wifi::quality", 1, wireless_quality); + AddFunction("wifi::protocol", 1, wireless_protocol); + AddFunction("wifi::frequency", 1, wireless_frequency); + AddFunction("wifi::bitrate", 1, wireless_bitrate); + AddFunction("wifi::essid", 1, wireless_essid); + AddFunction("wifi::op_mode", 1, wireless_op_mode); + AddFunction("wifi::sensitivity", 1, wireless_sensitivity); + AddFunction("wifi::sec_mode", 1, wireless_sec_mode); + + return 0; } -void plugin_exit_wireless(void) +void plugin_exit_wireless(void) { - if (sock>0) close(sock); - hash_destroy(&wireless); + if (sock > 0) + close(sock); + hash_destroy(&wireless); } diff --git a/plugin_xmms.c b/plugin_xmms.c index 52ade96..b07919e 100644 --- a/plugin_xmms.c +++ b/plugin_xmms.c @@ -1,4 +1,4 @@ -/* $Id: plugin_xmms.c,v 1.13 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: plugin_xmms.c,v 1.14 2005/05/08 04:32:45 reinelt Exp $ * * XMMS-Plugin for LCD4Linux * Copyright (C) 2003 Markus Keil <markus_keil@t-online.de> @@ -21,6 +21,9 @@ * * * $Log: plugin_xmms.c,v $ + * Revision 1.14 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.13 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -115,75 +118,84 @@ static HASH xmms; -static int parse_xmms_info (void) +static int parse_xmms_info(void) { - int age; - FILE *xmms_stream; - char zeile[200]; - - /* reread every 100msec only */ - age=hash_age(&xmms, NULL); - if (age>=0 && age<=200) return 0; - /* Open Filestream for '/tmp/xmms-info' */ - xmms_stream = fopen("/tmp/xmms-info","r"); - - /* Check for File */ - if( !xmms_stream ) { - error("Error: Cannot open XMMS-Info Stream! Is XMMS started?"); - return -1; - } - - /* Read Lines from the Stream */ - while(fgets(zeile,sizeof(zeile),xmms_stream)) { - char *c, *key, *val; - c=strchr(zeile, ':'); - if (c==NULL) continue; - key=zeile; val=c+1; - /* strip leading blanks from key */ - while (isspace(*key)) *key++='\0'; - /* strip trailing blanks from key */ - do *c='\0'; while (isspace(*--c)); - /* strip leading blanks from value */ - while (isspace(*val)) *val++='\0'; - /* strip trailing blanks from value */ - for (c=val; *c!='\0';c++); - while (isspace(*--c)) *c='\0'; - hash_put (&xmms, key, val); - } - - fclose(xmms_stream); - return 0; - + int age; + FILE *xmms_stream; + char zeile[200]; + + /* reread every 100msec only */ + age = hash_age(&xmms, NULL); + if (age >= 0 && age <= 200) + return 0; + /* Open Filestream for '/tmp/xmms-info' */ + xmms_stream = fopen("/tmp/xmms-info", "r"); + + /* Check for File */ + if (!xmms_stream) { + error("Error: Cannot open XMMS-Info Stream! Is XMMS started?"); + return -1; + } + + /* Read Lines from the Stream */ + while (fgets(zeile, sizeof(zeile), xmms_stream)) { + char *c, *key, *val; + c = strchr(zeile, ':'); + if (c == NULL) + continue; + key = zeile; + val = c + 1; + /* strip leading blanks from key */ + while (isspace(*key)) + *key++ = '\0'; + /* strip trailing blanks from key */ + do + *c = '\0'; + while (isspace(*--c)); + /* strip leading blanks from value */ + while (isspace(*val)) + *val++ = '\0'; + /* strip trailing blanks from value */ + for (c = val; *c != '\0'; c++); + while (isspace(*--c)) + *c = '\0'; + hash_put(&xmms, key, val); + } + + fclose(xmms_stream); + return 0; + } -static void my_xmms (RESULT *result, RESULT *arg1) -{ - char *key, *val; - - if (parse_xmms_info()<0) { - SetResult(&result, R_STRING, ""); - return; - } - - key=R2S(arg1); - val=hash_get(&xmms, key, NULL); - if (val==NULL) val=""; - - SetResult(&result, R_STRING, val); +static void my_xmms(RESULT * result, RESULT * arg1) +{ + char *key, *val; + + if (parse_xmms_info() < 0) { + SetResult(&result, R_STRING, ""); + return; + } + + key = R2S(arg1); + val = hash_get(&xmms, key, NULL); + if (val == NULL) + val = ""; + + SetResult(&result, R_STRING, val); } -int plugin_init_xmms (void) +int plugin_init_xmms(void) { - hash_create(&xmms); + hash_create(&xmms); - /* register xmms info */ - AddFunction ("xmms", 1, my_xmms); + /* register xmms info */ + AddFunction("xmms", 1, my_xmms); - return 0; + return 0; } -void plugin_exit_xmms(void) +void plugin_exit_xmms(void) { - hash_destroy(&xmms); + hash_destroy(&xmms); } @@ -1,4 +1,4 @@ -/* $Id: qprintf.c,v 1.7 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: qprintf.c,v 1.8 2005/05/08 04:32:45 reinelt Exp $ * * simple but quick snprintf() replacement * @@ -26,6 +26,9 @@ * * * $Log: qprintf.c,v $ + * Revision 1.8 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.7 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -72,161 +75,165 @@ #include <stdarg.h> #include <string.h> -static char *itoa(char* buffer, const size_t size, int value) +static char *itoa(char *buffer, const size_t size, int value) { - char *p; - int sign; - - /* sanity checks */ - if (buffer==NULL || size<2) return (NULL); - - /* remember sign of value */ - sign = 0; - if (value < 0) { - sign = 1; - value = -value; - } - - /* p points to last char */ - p = buffer+size-1; - - /* set terminating zero */ - *p='\0'; - - do { - *--p = value%10 + '0'; - value = value/10; - } while (value!=0 && p>buffer); - - if (sign && p>buffer) - *--p = '-'; - - return p; -} - - -static char *utoa(char* buffer, const size_t size, unsigned int value) + char *p; + int sign; + + /* sanity checks */ + if (buffer == NULL || size < 2) + return (NULL); + + /* remember sign of value */ + sign = 0; + if (value < 0) { + sign = 1; + value = -value; + } + + /* p points to last char */ + p = buffer + size - 1; + + /* set terminating zero */ + *p = '\0'; + + do { + *--p = value % 10 + '0'; + value = value / 10; + } while (value != 0 && p > buffer); + + if (sign && p > buffer) + *--p = '-'; + + return p; +} + + +static char *utoa(char *buffer, const size_t size, unsigned int value) { - char *p; - - /* sanity checks */ - if (buffer==NULL || size<2) return (NULL); - - /* p points to last char */ - p = buffer+size-1; - - /* set terminating zero */ - *p='\0'; - - do { - *--p = value%10 + '0'; - value = value/10; - } while (value!=0 && p>buffer); - - return p; -} - - -static char *utox(char* buffer, const size_t size, unsigned int value) + char *p; + + /* sanity checks */ + if (buffer == NULL || size < 2) + return (NULL); + + /* p points to last char */ + p = buffer + size - 1; + + /* set terminating zero */ + *p = '\0'; + + do { + *--p = value % 10 + '0'; + value = value / 10; + } while (value != 0 && p > buffer); + + return p; +} + + +static char *utox(char *buffer, const size_t size, unsigned int value) { - char *p; - int digit; - - /* sanity checks */ - if (buffer==NULL || size<2) return (NULL); - - /* p points to last char */ - p = buffer+size-1; - - /* set terminating zero */ - *p='\0'; - - do { - digit = value%16; - value = value/16; - *--p = (digit < 10 ? '0' : 'a'-10) + digit; - } while (value!=0 && p>buffer); - - return p; -} - - -int qprintf(char *str, const size_t size, const char *format, ...) { - - va_list ap; - const char *src; - char *dst; - unsigned int len; - - src = format; - dst = str; - len = 0; - - va_start(ap, format); - - /* use size-1 for terminating zero */ - while (len < size-1) { - - if (*src=='%') { - char buf[12], *s; - int d; - unsigned int u; - switch (*++src) { - case 's': - src++; - s = va_arg(ap, char *); - while (len < size-1 && *s != '\0') { - len++; - *dst++ = *s++; - } - break; - case 'd': - src++; - d = va_arg(ap, int); - s = itoa (buf, sizeof(buf), d); - while (len < size && *s != '\0') { - len++; - *dst++ = *s++; - } - break; - case 'u': - src++; - u = va_arg(ap, unsigned int); - s = utoa (buf, sizeof(buf), u); - while (len < size-1 && *s != '\0') { - len++; - *dst++ = *s++; - } - break; - case 'x': - src++; - u = va_arg(ap, unsigned int); - s = utox (buf, sizeof(buf), u); - while (len < size-1 && *s != '\0') { - len++; - *dst++ = *s++; + char *p; + int digit; + + /* sanity checks */ + if (buffer == NULL || size < 2) + return (NULL); + + /* p points to last char */ + p = buffer + size - 1; + + /* set terminating zero */ + *p = '\0'; + + do { + digit = value % 16; + value = value / 16; + *--p = (digit < 10 ? '0' : 'a' - 10) + digit; + } while (value != 0 && p > buffer); + + return p; +} + + +int qprintf(char *str, const size_t size, const char *format, ...) +{ + + va_list ap; + const char *src; + char *dst; + unsigned int len; + + src = format; + dst = str; + len = 0; + + va_start(ap, format); + + /* use size-1 for terminating zero */ + while (len < size - 1) { + + if (*src == '%') { + char buf[12], *s; + int d; + unsigned int u; + switch (*++src) { + case 's': + src++; + s = va_arg(ap, char *); + while (len < size - 1 && *s != '\0') { + len++; + *dst++ = *s++; + } + break; + case 'd': + src++; + d = va_arg(ap, int); + s = itoa(buf, sizeof(buf), d); + while (len < size && *s != '\0') { + len++; + *dst++ = *s++; + } + break; + case 'u': + src++; + u = va_arg(ap, unsigned int); + s = utoa(buf, sizeof(buf), u); + while (len < size - 1 && *s != '\0') { + len++; + *dst++ = *s++; + } + break; + case 'x': + src++; + u = va_arg(ap, unsigned int); + s = utox(buf, sizeof(buf), u); + while (len < size - 1 && *s != '\0') { + len++; + *dst++ = *s++; + } + break; + default: + len++; + *dst++ = '%'; + } + } else { + len++; + *dst++ = *src; + if (*src++ == '\0') + break; } - break; - default: + } + + va_end(ap); + + /* enforce terminating zero */ + if (len >= size - 1 && *(dst - 1) != '\0') { len++; - *dst++ = '%'; - } - } else { - len++; - *dst++ = *src; - if (*src++ == '\0') break; + *dst = '\0'; } - } - - va_end(ap); - - /* enforce terminating zero */ - if (len>=size-1 && *(dst-1)!='\0') { - len++; - *dst='\0'; - } - - /* do not count terminating zero */ - return len-1; -} + /* do not count terminating zero */ + return len - 1; +} @@ -1,4 +1,4 @@ -/* $Id: thread.c,v 1.6 2005/01/18 06:30:23 reinelt Exp $ +/* $Id: thread.c,v 1.7 2005/05/08 04:32:45 reinelt Exp $ * * thread handling (mutex, shmem, ...) * @@ -26,6 +26,9 @@ * * * $Log: thread.c,v $ + * Revision 1.7 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.6 2005/01/18 06:30:23 reinelt * added (C) to all copyright statements * @@ -105,103 +108,103 @@ /* glibc 2.1 requires defining semun ourselves */ #ifdef _SEM_SEMUN_UNDEFINED union semun { - int val; - struct semid_ds *buf; - unsigned short int *array; - struct seminfo *__buf; + int val; + struct semid_ds *buf; + unsigned short int *array; + struct seminfo *__buf; }; #endif -int mutex_create (void) +int mutex_create(void) { - int semid; - union semun semun; - - semid = semget(IPC_PRIVATE, 1, 0); - if (semid == -1) { - error ("fatal error: semget() failed: %s", strerror(errno)); - return -1; - } - semun.val = 1; - semctl(semid, 0, SETVAL, semun); - - return semid; + int semid; + union semun semun; + + semid = semget(IPC_PRIVATE, 1, 0); + if (semid == -1) { + error("fatal error: semget() failed: %s", strerror(errno)); + return -1; + } + semun.val = 1; + semctl(semid, 0, SETVAL, semun); + + return semid; } -void mutex_lock (const int semid) +void mutex_lock(const int semid) { - struct sembuf sembuf; - sembuf.sem_num = 0; - sembuf.sem_op = -1; - sembuf.sem_flg = 0; - semop (semid, &sembuf, 1); + struct sembuf sembuf; + sembuf.sem_num = 0; + sembuf.sem_op = -1; + sembuf.sem_flg = 0; + semop(semid, &sembuf, 1); } -void mutex_unlock (const int semid) +void mutex_unlock(const int semid) { - struct sembuf sembuf; - sembuf.sem_num = 0; - sembuf.sem_op = 1; - sembuf.sem_flg = 0; - semop (semid, &sembuf, 1); + struct sembuf sembuf; + sembuf.sem_num = 0; + sembuf.sem_op = 1; + sembuf.sem_flg = 0; + semop(semid, &sembuf, 1); } -void mutex_destroy (const int semid) +void mutex_destroy(const int semid) { - union semun arg; - semctl(semid, 0, IPC_RMID, arg); + union semun arg; + semctl(semid, 0, IPC_RMID, arg); } -int shm_create (void **buffer, const int size) +int shm_create(void **buffer, const int size) { - int shmid; - - shmid = shmget(IPC_PRIVATE, size, SHM_R|SHM_W); - if (shmid==-1) { - error ("fatal error: shmget() failed: %s", strerror(errno)); - return -1; - } - - *buffer = shmat(shmid, NULL, 0); - if (*buffer == NULL) { - error ("fatal error: shmat() failed: %s", strerror(errno)); - return -1; - } - - return shmid; + int shmid; + + shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W); + if (shmid == -1) { + error("fatal error: shmget() failed: %s", strerror(errno)); + return -1; + } + + *buffer = shmat(shmid, NULL, 0); + if (*buffer == NULL) { + error("fatal error: shmat() failed: %s", strerror(errno)); + return -1; + } + + return shmid; } -void shm_destroy (const int shmid, const void *buffer) +void shm_destroy(const int shmid, const void *buffer) { - shmdt (buffer); - shmctl(shmid, IPC_RMID, NULL); + shmdt(buffer); + shmctl(shmid, IPC_RMID, NULL); } -int thread_create (const char *name, void (*thread)(void *data), void *data) +int thread_create(const char *name, void (*thread) (void *data), void *data) { - pid_t pid, ppid; - - ppid=getpid(); - - switch (pid = fork()) { - case -1: - error ("fatal error: fork(%s) failed: %s", name, strerror(errno)); - return -1; - case 0: - info ("thread %s starting...", name); - thread(data); - info ("thread %s ended.", name); - exit (0); - default: - info ("forked process %d for thread %s", pid, name); - } - - return pid; + pid_t pid, ppid; + + ppid = getpid(); + + switch (pid = fork()) { + case -1: + error("fatal error: fork(%s) failed: %s", name, strerror(errno)); + return -1; + case 0: + info("thread %s starting...", name); + thread(data); + info("thread %s ended.", name); + exit(0); + default: + info("forked process %d for thread %s", pid, name); + } + + return pid; } @@ -1,4 +1,4 @@ -/* $Id: thread.h,v 1.5 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: thread.h,v 1.6 2005/05/08 04:32:45 reinelt Exp $ * * thread handling (mutex, shmem, ...) * @@ -26,6 +26,9 @@ * * * $Log: thread.h,v $ + * Revision 1.6 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.5 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -50,14 +53,14 @@ #ifndef _THREAD_H_ #define _THREAD_H_ -int mutex_create (void); -void mutex_lock (const int semid); -void mutex_unlock (const int semid); -void mutex_destroy (const int semid); +int mutex_create(void); +void mutex_lock(const int semid); +void mutex_unlock(const int semid); +void mutex_destroy(const int semid); -int shm_create (void **buffer, const int size); -void shm_destroy (const int shmid, const void *buffer); +int shm_create(void **buffer, const int size); +void shm_destroy(const int shmid, const void *buffer); -int thread_create (const char *name, void (*thread)(void *data), void *data); +int thread_create(const char *name, void (*thread) (void *data), void *data); #endif @@ -1,4 +1,4 @@ -/* $Id: timer.c,v 1.12 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: timer.c,v 1.13 2005/05/08 04:32:45 reinelt Exp $ * * generic timer handling * @@ -21,6 +21,9 @@ * * * $Log: timer.c,v $ + * Revision 1.13 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.12 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -106,137 +109,141 @@ typedef struct TIMER { - void (*callback)(void *data); - void *data; - struct timeval when; - int interval; - int one_shot; - int active; + void (*callback) (void *data); + void *data; + struct timeval when; + int interval; + int one_shot; + int active; } TIMER; -TIMER *Timers=NULL; -int nTimers=0; +TIMER *Timers = NULL; +int nTimers = 0; -static void timer_inc (struct timeval *tv, const int msec) +static void timer_inc(struct timeval *tv, const int msec) { - tv->tv_sec += msec / 1000; - tv->tv_usec += (msec - 1000*(msec/1000)) * 1000; - - if (tv->tv_usec >= 1000000) { - tv->tv_usec -= 1000000; - tv->tv_sec++; - } + tv->tv_sec += msec / 1000; + tv->tv_usec += (msec - 1000 * (msec / 1000)) * 1000; + + if (tv->tv_usec >= 1000000) { + tv->tv_usec -= 1000000; + tv->tv_sec++; + } } -int timer_add (void (*callback)(void *data), void *data, const int interval, const int one_shot) +int timer_add(void (*callback) (void *data), void *data, const int interval, const int one_shot) { - int i; - struct timeval now; - - /* find a free slot */ - for (i=0; i<nTimers; i++) { - if (Timers[i].active==0) break; - } - - /* none found, allocate a new slot */ - if (i>=nTimers) { - nTimers++; - Timers=realloc(Timers, nTimers*sizeof(*Timers)); - } - - gettimeofday(&now, NULL); - - /* fill slot */ - Timers[i].callback = callback; - Timers[i].data = data; - Timers[i].when = now; - Timers[i].interval = interval; - Timers[i].one_shot = one_shot; - Timers[i].active = 1; - - /* if one-shot timer, don't fire now */ - if (one_shot) { - timer_inc (&Timers[i].when, interval); - } - - return 0; + int i; + struct timeval now; + + /* find a free slot */ + for (i = 0; i < nTimers; i++) { + if (Timers[i].active == 0) + break; + } + + /* none found, allocate a new slot */ + if (i >= nTimers) { + nTimers++; + Timers = realloc(Timers, nTimers * sizeof(*Timers)); + } + + gettimeofday(&now, NULL); + + /* fill slot */ + Timers[i].callback = callback; + Timers[i].data = data; + Timers[i].when = now; + Timers[i].interval = interval; + Timers[i].one_shot = one_shot; + Timers[i].active = 1; + + /* if one-shot timer, don't fire now */ + if (one_shot) { + timer_inc(&Timers[i].when, interval); + } + + return 0; } -int timer_process (struct timespec *delay) +int timer_process(struct timespec *delay) { - int i, flag, min; - struct timeval now; - - /* the current moment */ - gettimeofday(&now, NULL); - - /* sanity check */ - if (nTimers==0) { - error ("huh? not one single timer to process? dazed and confused..."); - return -1; - } - - /* process expired timers */ - flag=0; - for (i=0; i<nTimers; i++) { - if (Timers[i].active == 0) continue; - if (timercmp(&Timers[i].when, &now, <=)) { - flag=1; - /* callback */ - if (Timers[i].callback!=NULL) { - Timers[i].callback(Timers[i].data); - } - /* respawn or delete timer */ - if (Timers[i].one_shot) { - Timers[i].active=0; - } else { - Timers[i].when=now; - timer_inc (&Timers[i].when, Timers[i].interval); - } + int i, flag, min; + struct timeval now; + + /* the current moment */ + gettimeofday(&now, NULL); + + /* sanity check */ + if (nTimers == 0) { + error("huh? not one single timer to process? dazed and confused..."); + return -1; + } + + /* process expired timers */ + flag = 0; + for (i = 0; i < nTimers; i++) { + if (Timers[i].active == 0) + continue; + if (timercmp(&Timers[i].when, &now, <=)) { + flag = 1; + /* callback */ + if (Timers[i].callback != NULL) { + Timers[i].callback(Timers[i].data); + } + /* respawn or delete timer */ + if (Timers[i].one_shot) { + Timers[i].active = 0; + } else { + Timers[i].when = now; + timer_inc(&Timers[i].when, Timers[i].interval); + } + } + } + + /* find next timer */ + flag = 1; + min = -1; + for (i = 0; i < nTimers; i++) { + if (Timers[i].active == 0) + continue; + if (flag || timercmp(&Timers[i].when, &Timers[min].when, <)) { + flag = 0; + min = i; + } + } + + if (min < 0) { + error("huh? not one single timer left? dazed and confused..."); + return -1; } - } - - /* find next timer */ - flag=1; - min=-1; - for (i=0; i<nTimers; i++) { - if (Timers[i].active == 0) continue; - if (flag || timercmp(&Timers[i].when, &Timers[min].when, <)) { - flag=0; - min=i; + + /* delay until next timer event */ + delay->tv_sec = Timers[min].when.tv_sec - now.tv_sec; + delay->tv_nsec = Timers[min].when.tv_usec - now.tv_usec; + if (delay->tv_nsec < 0) { + delay->tv_sec--; + delay->tv_nsec += 1000000; } - } - - if (min<0) { - error ("huh? not one single timer left? dazed and confused..."); - return -1; - } - - /* delay until next timer event */ - delay->tv_sec = Timers[min].when.tv_sec - now.tv_sec; - delay->tv_nsec = Timers[min].when.tv_usec - now.tv_usec; - if (delay->tv_nsec<0) { - delay->tv_sec--; - delay->tv_nsec += 1000000; - } - /* nanoseconds!! */ - delay->tv_nsec *= 1000; - - return 0; - + /* nanoseconds!! */ + delay->tv_nsec *= 1000; + + return 0; + } -void timer_exit(void) { +void timer_exit(void) +{ - nTimers=0; + nTimers = 0; - if (Timers != NULL) { - free(Timers);; - Timers = NULL; - } + if (Timers != NULL) { + free(Timers);; + Timers = NULL; + } } @@ -1,4 +1,4 @@ -/* $Id: timer.h,v 1.5 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: timer.h,v 1.6 2005/05/08 04:32:45 reinelt Exp $ * * generic timer handling * @@ -23,6 +23,9 @@ * * * $Log: timer.h,v $ + * Revision 1.6 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.5 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -52,8 +55,8 @@ #ifndef _TIMER_H_ #define _TIMER_H_ -int timer_add (void(*callback)(void *data), void *data, const int interval, const int one_shot); -int timer_process (struct timespec *delay); +int timer_add(void (*callback) (void *data), void *data, const int interval, const int one_shot); +int timer_process(struct timespec *delay); void timer_exit(); #endif @@ -1,4 +1,4 @@ -/* $Id: udelay.c,v 1.19 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: udelay.c,v 1.20 2005/05/08 04:32:45 reinelt Exp $ * * short delays * @@ -23,6 +23,9 @@ * * * $Log: udelay.c,v $ + * Revision 1.20 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.19 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -158,162 +161,158 @@ unsigned long loops_per_usec; -void ndelay (const unsigned long nsec) +void ndelay(const unsigned long nsec) { - unsigned long loop=(nsec*loops_per_usec+999)/1000; - - __asm__ (".align 16\n" - "1:\tdecl %0\n" - "\tjne 1b" - : /* no result */ - :"a" (loop)); + unsigned long loop = (nsec * loops_per_usec + 999) / 1000; + + __asm__(".align 16\n" "1:\tdecl %0\n" "\tjne 1b": /* no result */ + :"a"(loop)); } /* adopted from /usr/src/linux/init/main.c */ -void udelay_calibrate (void) +void udelay_calibrate(void) { - clock_t tick; - unsigned long bit; - - loops_per_usec=1; - while (loops_per_usec<<=1) { - tick=clock(); - while (clock()==tick); - tick=clock(); - ndelay(1000000000/CLOCKS_PER_SEC); - if (clock()>tick) - break; - } - - loops_per_usec>>=1; - bit=loops_per_usec; - while (bit>>=1) { - loops_per_usec|=bit; - tick=clock(); - while (clock()==tick); - tick=clock(); - ndelay(1000000000/CLOCKS_PER_SEC); - if (clock()>tick) - loops_per_usec&=~bit; - } + clock_t tick; + unsigned long bit; + + loops_per_usec = 1; + while (loops_per_usec <<= 1) { + tick = clock(); + while (clock() == tick); + tick = clock(); + ndelay(1000000000 / CLOCKS_PER_SEC); + if (clock() > tick) + break; + } + + loops_per_usec >>= 1; + bit = loops_per_usec; + while (bit >>= 1) { + loops_per_usec |= bit; + tick = clock(); + while (clock() == tick); + tick = clock(); + ndelay(1000000000 / CLOCKS_PER_SEC); + if (clock() > tick) + loops_per_usec &= ~bit; + } } #else -static unsigned int ticks_per_usec=0; +static unsigned int ticks_per_usec = 0; -static void getCPUinfo (int *hasTSC, double *MHz) +static void getCPUinfo(int *hasTSC, double *MHz) { - int fd; - char buffer[4096], *p; - - *hasTSC=0; - *MHz=-1; - - fd=open("/proc/cpuinfo", O_RDONLY); - if (fd==-1) { - error ("udelay: open(/proc/cpuinfo) failed: %s", strerror(errno)); - return; - } - if (read (fd, &buffer, sizeof(buffer)-1)==-1) { - error ("udelay: read(/proc/cpuinfo) failed: %s", strerror(errno)); - close (fd); - return; - } - close (fd); - - p=strstr(buffer, "flags"); - if (p==NULL) { - info ("udelay: /proc/cpuinfo has no 'flags' line"); - } else { - p=strstr(p, "tsc"); - if (p==NULL) { - info ("udelay: CPU does not support Time Stamp Counter"); + int fd; + char buffer[4096], *p; + + *hasTSC = 0; + *MHz = -1; + + fd = open("/proc/cpuinfo", O_RDONLY); + if (fd == -1) { + error("udelay: open(/proc/cpuinfo) failed: %s", strerror(errno)); + return; + } + if (read(fd, &buffer, sizeof(buffer) - 1) == -1) { + error("udelay: read(/proc/cpuinfo) failed: %s", strerror(errno)); + close(fd); + return; + } + close(fd); + + p = strstr(buffer, "flags"); + if (p == NULL) { + info("udelay: /proc/cpuinfo has no 'flags' line"); } else { - info ("udelay: CPU supports Time Stamp Counter"); - *hasTSC=1; + p = strstr(p, "tsc"); + if (p == NULL) { + info("udelay: CPU does not support Time Stamp Counter"); + } else { + info("udelay: CPU supports Time Stamp Counter"); + *hasTSC = 1; + } } - } - - p=strstr(buffer, "cpu MHz"); - if (p==NULL) { - info ("udelay: /proc/cpuinfo has no 'cpu MHz' line"); - } else { - if (sscanf(p+7, " : %lf", MHz)!=1) { - error ("udelay: parse(/proc/cpuinfo) failed: unknown 'cpu MHz' format"); - *MHz=-1; + + p = strstr(buffer, "cpu MHz"); + if (p == NULL) { + info("udelay: /proc/cpuinfo has no 'cpu MHz' line"); } else { - info ("udelay: CPU runs at %f MHz", *MHz); + if (sscanf(p + 7, " : %lf", MHz) != 1) { + error("udelay: parse(/proc/cpuinfo) failed: unknown 'cpu MHz' format"); + *MHz = -1; + } else { + info("udelay: CPU runs at %f MHz", *MHz); + } } - } } -void udelay_init (void) +void udelay_init(void) { #ifdef HAVE_ASM_MSR_H - - int tsc; - double mhz; - - getCPUinfo (&tsc, &mhz); - - if (tsc && mhz>0.0) { - ticks_per_usec=ceil(mhz); - info ("udelay: using TSC delay loop, %u ticks per microsecond", ticks_per_usec); - } else + + int tsc; + double mhz; + + getCPUinfo(&tsc, &mhz); + + if (tsc && mhz > 0.0) { + ticks_per_usec = ceil(mhz); + info("udelay: using TSC delay loop, %u ticks per microsecond", ticks_per_usec); + } else #else - error ("udelay: The file 'include/asm/msr.h' was missing at compile time."); - error ("udelay: Even if your CPU supports TSC, it will not be used!"); - error ("udelay: You *really* should install msr.h and recompile LCD4linux!"); + error("udelay: The file 'include/asm/msr.h' was missing at compile time."); + error("udelay: Even if your CPU supports TSC, it will not be used!"); + error("udelay: You *really* should install msr.h and recompile LCD4linux!"); #endif - { - ticks_per_usec=0; - info ("udelay: using gettimeofday() delay loop"); - } + { + ticks_per_usec = 0; + info("udelay: using gettimeofday() delay loop"); + } } -void ndelay (const unsigned long nsec) +void ndelay(const unsigned long nsec) { #ifdef HAVE_ASM_MSR_H - if (ticks_per_usec) { + if (ticks_per_usec) { - unsigned int t1, t2; - unsigned long tsc; + unsigned int t1, t2; + unsigned long tsc; - tsc=(nsec*ticks_per_usec+999)/1000; + tsc = (nsec * ticks_per_usec + 999) / 1000; - rdtscl(t1); - do { - rep_nop(); - rdtscl(t2); - } while ((t2-t1)<tsc); - - } else + rdtscl(t1); + do { + rep_nop(); + rdtscl(t2); + } while ((t2 - t1) < tsc); + } else #endif - { - struct timeval now, end; - - gettimeofday (&end, NULL); - end.tv_usec+=(nsec+999)/1000; - while (end.tv_usec>1000000) { - end.tv_usec-=1000000; - end.tv_sec++; + { + struct timeval now, end; + + gettimeofday(&end, NULL); + end.tv_usec += (nsec + 999) / 1000; + while (end.tv_usec > 1000000) { + end.tv_usec -= 1000000; + end.tv_sec++; + } + + do { + rep_nop(); + gettimeofday(&now, NULL); + } while (now.tv_sec == end.tv_sec ? now.tv_usec < end.tv_usec : now.tv_sec < end.tv_sec); } - - do { - rep_nop(); - gettimeofday(&now, NULL); - } while (now.tv_sec==end.tv_sec?now.tv_usec<end.tv_usec:now.tv_sec<end.tv_sec); - } } #endif @@ -1,4 +1,4 @@ -/* $Id: udelay.h,v 1.9 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: udelay.h,v 1.10 2005/05/08 04:32:45 reinelt Exp $ * * short delays * @@ -23,6 +23,9 @@ * * * $Log: udelay.h,v $ + * Revision 1.10 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.9 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -71,24 +74,24 @@ /* stolen from linux/asm-i386/processor.h */ /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */ -static inline void rep_nop (void) +static inline void rep_nop(void) { - __asm__ __volatile__("rep; nop"); + __asm__ __volatile__("rep; nop"); } #ifdef USE_OLD_UDELAY extern unsigned long loops_per_usec; -void udelay_calibrate (void); +void udelay_calibrate(void); #else -void udelay_init (void); +void udelay_init(void); #endif -void ndelay (const unsigned long nsec); +void ndelay(const unsigned long nsec); #define udelay(usec) ndelay(usec*1000) @@ -1,4 +1,4 @@ -/* $Id: widget.c,v 1.18 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: widget.c,v 1.19 2005/05/08 04:32:45 reinelt Exp $ * * generic widget handling * @@ -21,6 +21,9 @@ * * * $Log: widget.c,v $ + * Revision 1.19 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.18 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -123,118 +126,122 @@ /* we use a static array of widgets and not realloc() */ #define MAX_WIDGETS 256 -static WIDGET_CLASS *Classes=NULL; -static int nClasses=0; +static WIDGET_CLASS *Classes = NULL; +static int nClasses = 0; -static WIDGET *Widgets=NULL; -static int nWidgets=0; +static WIDGET *Widgets = NULL; +static int nWidgets = 0; -static int widget_added=0; +static int widget_added = 0; -int widget_register (WIDGET_CLASS *widget) +int widget_register(WIDGET_CLASS * widget) { - int i; - - /* sanity check: disallow widget registering after at least one */ - /* widget has been added, because we use realloc here, and there may */ - /* be pointers to the old memory area */ - if (widget_added) { - error ("internal error: register_widget(%s) after add_widget()", widget->name); - return -1; - } - - for (i=0; i<nClasses; i++) { - if (strcasecmp(widget->name, Classes[i].name)==0) { - error ("internal error: widget '%s' already exists!", widget->name); - return -1; + int i; + + /* sanity check: disallow widget registering after at least one */ + /* widget has been added, because we use realloc here, and there may */ + /* be pointers to the old memory area */ + if (widget_added) { + error("internal error: register_widget(%s) after add_widget()", widget->name); + return -1; + } + + for (i = 0; i < nClasses; i++) { + if (strcasecmp(widget->name, Classes[i].name) == 0) { + error("internal error: widget '%s' already exists!", widget->name); + return -1; + } } - } - nClasses++; - Classes=realloc(Classes, nClasses*sizeof(WIDGET_CLASS)); - Classes[nClasses-1] = *widget; - - return 0; + nClasses++; + Classes = realloc(Classes, nClasses * sizeof(WIDGET_CLASS)); + Classes[nClasses - 1] = *widget; + + return 0; } -void widget_unregister(void) { - int i; - for (i=0;i<nWidgets;i++) { - Widgets[i].class->quit(&(Widgets[i])); - } - free(Widgets); +void widget_unregister(void) +{ + int i; + for (i = 0; i < nWidgets; i++) { + Widgets[i].class->quit(&(Widgets[i])); + } + free(Widgets); - free(Classes); + free(Classes); - nWidgets=0; - nClasses=0; + nWidgets = 0; + nClasses = 0; } -int widget_add (const char *name, const int row, const int col) +int widget_add(const char *name, const int row, const int col) { - int i; - char *section; - char *class; - - WIDGET_CLASS *Class; - WIDGET *Widget; - - /* prepare config section */ - /* strlen("Widget:")=7 */ - section=malloc(strlen(name)+8); - strcpy(section, "Widget:"); - strcat(section, name); - - /* get widget class */ - class=cfg_get(section, "class", NULL); - if (class==NULL || *class=='\0') { - error ("error: widget '%s' has no class!", name); - if (class) free (class); - return -1; - } - free(section); - /* lookup widget class */ - Class=NULL; - for (i=0; i<nClasses; i++) { - if (strcasecmp(class, Classes[i].name)==0) { - Class=&(Classes[i]); - break; + int i; + char *section; + char *class; + + WIDGET_CLASS *Class; + WIDGET *Widget; + + /* prepare config section */ + /* strlen("Widget:")=7 */ + section = malloc(strlen(name) + 8); + strcpy(section, "Widget:"); + strcat(section, name); + + /* get widget class */ + class = cfg_get(section, "class", NULL); + if (class == NULL || *class == '\0') { + error("error: widget '%s' has no class!", name); + if (class) + free(class); + return -1; + } + free(section); + /* lookup widget class */ + Class = NULL; + for (i = 0; i < nClasses; i++) { + if (strcasecmp(class, Classes[i].name) == 0) { + Class = &(Classes[i]); + break; + } + } + if (i == nClasses) { + error("widget '%s': class '%s' not supported", name, class); + if (class) + free(class); + return -1; } - } - if (i==nClasses) { - error ("widget '%s': class '%s' not supported", name, class); - if (class) free (class); - return -1; - } - if (class) free (class); - - /* do NOT use realloc here because there may be pointers to the old */ - /* memory area, which would point to nowhere if realloc moves the area */ - if (Widgets==NULL) { - Widgets=malloc(MAX_WIDGETS*sizeof(WIDGET)); - if (Widgets==NULL) { - error ("internal error: allocation of widget buffer failed: %s", strerror(errno)); - return -1; + if (class) + free(class); + + /* do NOT use realloc here because there may be pointers to the old */ + /* memory area, which would point to nowhere if realloc moves the area */ + if (Widgets == NULL) { + Widgets = malloc(MAX_WIDGETS * sizeof(WIDGET)); + if (Widgets == NULL) { + error("internal error: allocation of widget buffer failed: %s", strerror(errno)); + return -1; + } } - } - - /* another sanity check */ - if (nWidgets>=MAX_WIDGETS) { - error ("internal error: widget buffer full!"); - return -1; - } - - Widget=&(Widgets[nWidgets]); - nWidgets++; - - Widget->name = (char*)name; - Widget->class = Class; - Widget->row = row; - Widget->col = col; - - if (Class->init!=0) { - Class->init(Widget); - } - - return 0; + + /* another sanity check */ + if (nWidgets >= MAX_WIDGETS) { + error("internal error: widget buffer full!"); + return -1; + } + + Widget = &(Widgets[nWidgets]); + nWidgets++; + + Widget->name = (char *) name; + Widget->class = Class; + Widget->row = row; + Widget->col = col; + + if (Class->init != 0) { + Class->init(Widget); + } + + return 0; } @@ -1,4 +1,4 @@ -/* $Id: widget.h,v 1.12 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: widget.h,v 1.13 2005/05/08 04:32:45 reinelt Exp $ * * generic widget handling * @@ -23,6 +23,9 @@ * * * $Log: widget.h,v $ + * Revision 1.13 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.12 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -82,29 +85,29 @@ #define _WIDGET_H_ -struct WIDGET; /* forward declaration */ +struct WIDGET; /* forward declaration */ typedef struct WIDGET_CLASS { - char *name; - int (*init) (struct WIDGET *Self); - int (*draw) (struct WIDGET *Self); - int (*quit) (struct WIDGET *Self); + char *name; + int (*init) (struct WIDGET * Self); + int (*draw) (struct WIDGET * Self); + int (*quit) (struct WIDGET * Self); } WIDGET_CLASS; -typedef struct WIDGET{ - char *name; - WIDGET_CLASS *class; - int row; - int col; - void *data; +typedef struct WIDGET { + char *name; + WIDGET_CLASS *class; + int row; + int col; + void *data; } WIDGET; -int widget_register (WIDGET_CLASS *widget); -void widget_unregister (void); -int widget_add (const char *name, const int row, const int col); +int widget_register(WIDGET_CLASS * widget); +void widget_unregister(void); +int widget_add(const char *name, const int row, const int col); #endif diff --git a/widget_bar.c b/widget_bar.c index 7cb8956..810c69a 100644 --- a/widget_bar.c +++ b/widget_bar.c @@ -1,4 +1,4 @@ -/* $Id: widget_bar.c,v 1.15 2005/05/06 06:37:34 reinelt Exp $ +/* $Id: widget_bar.c,v 1.16 2005/05/08 04:32:45 reinelt Exp $ * * bar widget handling * @@ -21,6 +21,9 @@ * * * $Log: widget_bar.c,v $ + * Revision 1.16 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.15 2005/05/06 06:37:34 reinelt * hollow bar patch from geronet * @@ -116,181 +119,184 @@ #endif -void widget_bar_update (void *Self) +void widget_bar_update(void *Self) { - WIDGET *W = (WIDGET*)Self; - WIDGET_BAR *Bar = W->data; - RESULT result = {0, 0, 0, NULL}; - - double val1, val2; - double min, max; - - /* evaluate expressions */ - val1 = 0.0; - if (Bar->tree1 != NULL) { - Eval(Bar->tree1, &result); - val1 = R2N(&result); - DelResult(&result); - } - - val2 = val1; - if (Bar->tree2!=NULL) { - Eval(Bar->tree2, &result); - val2 = R2N(&result); - DelResult(&result); - } - - /* minimum: if expression is empty, do auto-scaling */ - if (Bar->tree_min!=NULL) { - Eval(Bar->tree_min, &result); - min = R2N(&result); - DelResult(&result); - } else { - min = Bar->min; - if (val1 < min) min = val1; - if (val2 < min) min = val2; - } - - /* maximum: if expression is empty, do auto-scaling */ - if (Bar->tree_max!=NULL) { - Eval(Bar->tree_max, &result); - max = R2N(&result); - DelResult(&result); - } else { - max = Bar->max; - if (val1 > max) max = val1; - if (val2 > max) max = val2; - } - - /* calculate bar values */ - Bar->min=min; - Bar->max=max; - if (max>min) { - Bar->val1=(val1-min)/(max-min); - Bar->val2=(val2-min)/(max-min); - } else { - Bar->val1=0.0; - Bar->val2=0.0; - } - - /* finally, draw it! */ - if (W->class->draw) - W->class->draw(W); - + WIDGET *W = (WIDGET *) Self; + WIDGET_BAR *Bar = W->data; + RESULT result = { 0, 0, 0, NULL }; + + double val1, val2; + double min, max; + + /* evaluate expressions */ + val1 = 0.0; + if (Bar->tree1 != NULL) { + Eval(Bar->tree1, &result); + val1 = R2N(&result); + DelResult(&result); + } + + val2 = val1; + if (Bar->tree2 != NULL) { + Eval(Bar->tree2, &result); + val2 = R2N(&result); + DelResult(&result); + } + + /* minimum: if expression is empty, do auto-scaling */ + if (Bar->tree_min != NULL) { + Eval(Bar->tree_min, &result); + min = R2N(&result); + DelResult(&result); + } else { + min = Bar->min; + if (val1 < min) + min = val1; + if (val2 < min) + min = val2; + } + + /* maximum: if expression is empty, do auto-scaling */ + if (Bar->tree_max != NULL) { + Eval(Bar->tree_max, &result); + max = R2N(&result); + DelResult(&result); + } else { + max = Bar->max; + if (val1 > max) + max = val1; + if (val2 > max) + max = val2; + } + + /* calculate bar values */ + Bar->min = min; + Bar->max = max; + if (max > min) { + Bar->val1 = (val1 - min) / (max - min); + Bar->val2 = (val2 - min) / (max - min); + } else { + Bar->val1 = 0.0; + Bar->val2 = 0.0; + } + + /* finally, draw it! */ + if (W->class->draw) + W->class->draw(W); + } -int widget_bar_init (WIDGET *Self) +int widget_bar_init(WIDGET * Self) { - char *section; char *c; - WIDGET_BAR *Bar; - - /* prepare config section */ - /* strlen("Widget:")=7 */ - section=malloc(strlen(Self->name)+8); - strcpy(section, "Widget:"); - strcat(section, Self->name); - - Bar=malloc(sizeof(WIDGET_BAR)); - memset (Bar, 0, sizeof(WIDGET_BAR)); - - /* get raw expressions (we evaluate them ourselves) */ - Bar->expression1 = cfg_get_raw (section, "expression", NULL); - Bar->expression2 = cfg_get_raw (section, "expression2", NULL); - - /* sanity check */ - if (Bar->expression1==NULL || *Bar->expression1=='\0') { - error ("widget %s has no expression, using '0.0'", Self->name); - Bar->expression1="0"; - } - - /* minimum and maximum value */ - Bar->expr_min = cfg_get_raw (section, "min", NULL); - Bar->expr_max = cfg_get_raw (section, "max", NULL); - - /* compile all expressions */ - Compile (Bar->expression1, &Bar->tree1); - Compile (Bar->expression2, &Bar->tree2); - Compile (Bar->expr_min, &Bar->tree_min); - Compile (Bar->expr_max, &Bar->tree_max); - - /* bar length, default 1 */ - cfg_number (section, "length", 1, 0, -1, &(Bar->length)); - - /* direction: East (default), West, North, South */ - c = cfg_get (section, "direction", "E"); - switch (toupper(*c)) { - case 'E': - Bar->direction=DIR_EAST; - break; - case 'W': - Bar->direction=DIR_WEST; - break; - case 'N': - Bar->direction=DIR_NORTH; - break; - case 'S': - Bar->direction=DIR_SOUTH; - break; - default: - error ("widget %s has unknown direction '%s', using 'East'", Self->name, c); - Bar->direction=DIR_EAST; - } - free (c); - - /* style: none (default), hollow */ - c = cfg_get (section, "style", "0"); - switch (toupper (*c)) - { + char *section; + char *c; + WIDGET_BAR *Bar; + + /* prepare config section */ + /* strlen("Widget:")=7 */ + section = malloc(strlen(Self->name) + 8); + strcpy(section, "Widget:"); + strcat(section, Self->name); + + Bar = malloc(sizeof(WIDGET_BAR)); + memset(Bar, 0, sizeof(WIDGET_BAR)); + + /* get raw expressions (we evaluate them ourselves) */ + Bar->expression1 = cfg_get_raw(section, "expression", NULL); + Bar->expression2 = cfg_get_raw(section, "expression2", NULL); + + /* sanity check */ + if (Bar->expression1 == NULL || *Bar->expression1 == '\0') { + error("widget %s has no expression, using '0.0'", Self->name); + Bar->expression1 = "0"; + } + + /* minimum and maximum value */ + Bar->expr_min = cfg_get_raw(section, "min", NULL); + Bar->expr_max = cfg_get_raw(section, "max", NULL); + + /* compile all expressions */ + Compile(Bar->expression1, &Bar->tree1); + Compile(Bar->expression2, &Bar->tree2); + Compile(Bar->expr_min, &Bar->tree_min); + Compile(Bar->expr_max, &Bar->tree_max); + + /* bar length, default 1 */ + cfg_number(section, "length", 1, 0, -1, &(Bar->length)); + + /* direction: East (default), West, North, South */ + c = cfg_get(section, "direction", "E"); + switch (toupper(*c)) { + case 'E': + Bar->direction = DIR_EAST; + break; + case 'W': + Bar->direction = DIR_WEST; + break; + case 'N': + Bar->direction = DIR_NORTH; + break; + case 'S': + Bar->direction = DIR_SOUTH; + break; + default: + error("widget %s has unknown direction '%s', using 'East'", Self->name, c); + Bar->direction = DIR_EAST; + } + free(c); + + /* style: none (default), hollow */ + c = cfg_get(section, "style", "0"); + switch (toupper(*c)) { case 'H': - Bar->style = STYLE_HOLLOW; - if (!(Bar->direction & (DIR_EAST | DIR_WEST))) - { - error ("widget %s with style \"hollow\" not implemented", Self->name); - Bar->style = 0; - } - break; + Bar->style = STYLE_HOLLOW; + if (!(Bar->direction & (DIR_EAST | DIR_WEST))) { + error("widget %s with style \"hollow\" not implemented", Self->name); + Bar->style = 0; + } + break; default: - Bar->style = 0; - } - free (c); - - /* update interval (msec), default 1 sec */ - cfg_number (section, "update", 1000, 10, -1, &(Bar->update)); - - free (section); - Self->data=Bar; - - timer_add (widget_bar_update, Self, Bar->update, 0); - - return 0; + Bar->style = 0; + } + free(c); + + /* update interval (msec), default 1 sec */ + cfg_number(section, "update", 1000, 10, -1, &(Bar->update)); + + free(section); + Self->data = Bar; + + timer_add(widget_bar_update, Self, Bar->update, 0); + + return 0; } -int widget_bar_quit (WIDGET *Self) +int widget_bar_quit(WIDGET * Self) { - if (Self ) { - if (Self->data) { - WIDGET_BAR *Bar = Self->data; - DelTree(Bar->tree1); - DelTree(Bar->tree2); - DelTree(Bar->tree_min); - DelTree(Bar->tree_max); - free(Self->data); + if (Self) { + if (Self->data) { + WIDGET_BAR *Bar = Self->data; + DelTree(Bar->tree1); + DelTree(Bar->tree2); + DelTree(Bar->tree_min); + DelTree(Bar->tree_max); + free(Self->data); + } + Self->data = NULL; } - Self->data=NULL; - } - - - return 0; - + + + return 0; + } WIDGET_CLASS Widget_Bar = { - name: "bar", - init: widget_bar_init, - draw: NULL, - quit: widget_bar_quit, + name:"bar", + init:widget_bar_init, + draw:NULL, + quit:widget_bar_quit, }; diff --git a/widget_bar.h b/widget_bar.h index 6cb31c6..898e6d2 100644 --- a/widget_bar.h +++ b/widget_bar.h @@ -1,4 +1,4 @@ -/* $Id: widget_bar.h,v 1.7 2005/05/06 06:37:34 reinelt Exp $ +/* $Id: widget_bar.h,v 1.8 2005/05/08 04:32:45 reinelt Exp $ * * bar widget handling * @@ -23,6 +23,9 @@ * * * $Log: widget_bar.h,v $ + * Revision 1.8 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.7 2005/05/06 06:37:34 reinelt * hollow bar patch from geronet * @@ -58,26 +61,26 @@ #ifndef _WIDGET_BAR_H_ #define _WIDGET_BAR_H_ -typedef enum { DIR_EAST=1, DIR_WEST=2, DIR_NORTH=4, DIR_SOUTH=8 } DIRECTION; +typedef enum { DIR_EAST = 1, DIR_WEST = 2, DIR_NORTH = 4, DIR_SOUTH = 8 } DIRECTION; typedef enum { STYLE_HOLLOW = 1, STYLE_FIRST = 2, STYLE_LAST = 4 } STYLE; typedef struct WIDGET_BAR { - char *expression1; /* expression that delivers the value */ - char *expression2; /* expression that delivers the value */ - char *expr_min; /* expression that delivers the minimum value */ - char *expr_max; /* expression that delivers the maximum value */ - void *tree1; /* pre-compiled expression that delivers the value */ - void *tree2; /* pre-compiled expression that delivers the value */ - void *tree_min; /* pre-compiled expression that delivers the minimum value */ - void *tree_max; /* pre-compiled expression that delivers the maximum value */ - DIRECTION direction; /* bar direction */ - STYLE style; /* bar style (hollow) */ - int length; /* bar length */ - int update; /* update interval (msec) */ - double val1; /* bar value, 0.0 ... 1.0 */ - double val2; /* bar value, 0.0 ... 1.0 */ - double min; /* minimum value */ - double max; /* maximum value */ + char *expression1; /* expression that delivers the value */ + char *expression2; /* expression that delivers the value */ + char *expr_min; /* expression that delivers the minimum value */ + char *expr_max; /* expression that delivers the maximum value */ + void *tree1; /* pre-compiled expression that delivers the value */ + void *tree2; /* pre-compiled expression that delivers the value */ + void *tree_min; /* pre-compiled expression that delivers the minimum value */ + void *tree_max; /* pre-compiled expression that delivers the maximum value */ + DIRECTION direction; /* bar direction */ + STYLE style; /* bar style (hollow) */ + int length; /* bar length */ + int update; /* update interval (msec) */ + double val1; /* bar value, 0.0 ... 1.0 */ + double val2; /* bar value, 0.0 ... 1.0 */ + double min; /* minimum value */ + double max; /* maximum value */ } WIDGET_BAR; diff --git a/widget_icon.c b/widget_icon.c index 4b3008b..0b7a330 100644 --- a/widget_icon.c +++ b/widget_icon.c @@ -1,4 +1,4 @@ -/* $Id: widget_icon.c,v 1.16 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: widget_icon.c,v 1.17 2005/05/08 04:32:45 reinelt Exp $ * * icon widget handling * @@ -21,6 +21,9 @@ * * * $Log: widget_icon.c,v $ + * Revision 1.17 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.16 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -124,153 +127,156 @@ /* icons always are 8 pixels high */ #define YRES 8 -static void widget_icon_read_bitmap (const char *section, WIDGET_ICON *Icon) +static void widget_icon_read_bitmap(const char *section, WIDGET_ICON * Icon) { - int row, n; - char key[15]; - char *val, *v; - unsigned char *map; - - for (row=0; row<YRES; row++) { - qprintf(key, sizeof(key), "Bitmap.Row%d", row+1); - val=cfg_get(section, key, ""); - map=Icon->bitmap+row; - n=0; - for (v=val; *v!='\0'; v++) { - if (n>=Icon->maxmap) { - Icon->maxmap++; - Icon->bitmap=realloc(Icon->bitmap, Icon->maxmap*YRES*sizeof(char)); - memset (Icon->bitmap+n*YRES, 0, YRES*sizeof(char)); - map=Icon->bitmap+n*YRES+row; - } - switch (*v) { - case '|': - n++; - map+=YRES; - break; - case '*': - (*map)<<=1; - (*map)|=1; - break; - default: - (*map)<<=1; - } + int row, n; + char key[15]; + char *val, *v; + unsigned char *map; + + for (row = 0; row < YRES; row++) { + qprintf(key, sizeof(key), "Bitmap.Row%d", row + 1); + val = cfg_get(section, key, ""); + map = Icon->bitmap + row; + n = 0; + for (v = val; *v != '\0'; v++) { + if (n >= Icon->maxmap) { + Icon->maxmap++; + Icon->bitmap = realloc(Icon->bitmap, Icon->maxmap * YRES * sizeof(char)); + memset(Icon->bitmap + n * YRES, 0, YRES * sizeof(char)); + map = Icon->bitmap + n * YRES + row; + } + switch (*v) { + case '|': + n++; + map += YRES; + break; + case '*': + (*map) <<= 1; + (*map) |= 1; + break; + default: + (*map) <<= 1; + } + } + free(val); } - free(val); - } } -void widget_icon_update (void *Self) +void widget_icon_update(void *Self) { - WIDGET *W = (WIDGET*)Self; - WIDGET_ICON *Icon = W->data; - RESULT result = {0, 0, 0, NULL}; - - /* evaluate expressions */ - Icon->speed = 100; - if (Icon->speed_tree!=NULL) { - Eval(Icon->speed_tree, &result); - Icon->speed = R2N(&result); - if (Icon->speed<10) Icon->speed=10; - DelResult(&result); - } - - Icon->visible = 1; - if (Icon->visible_tree!=NULL) { - Eval(Icon->visible_tree, &result); - Icon->visible = R2N(&result); - if (Icon->visible<1) Icon->visible=0; - DelResult(&result); - } - - /* rotate icon bitmap */ - Icon->curmap++; - if (Icon->curmap >= Icon->maxmap) - Icon->curmap = 0; - - /* finally, draw it! */ - if (W->class->draw) - W->class->draw(W); - - /* add a new one-shot timer */ - timer_add (widget_icon_update, Self, Icon->speed, 1); - + WIDGET *W = (WIDGET *) Self; + WIDGET_ICON *Icon = W->data; + RESULT result = { 0, 0, 0, NULL }; + + /* evaluate expressions */ + Icon->speed = 100; + if (Icon->speed_tree != NULL) { + Eval(Icon->speed_tree, &result); + Icon->speed = R2N(&result); + if (Icon->speed < 10) + Icon->speed = 10; + DelResult(&result); + } + + Icon->visible = 1; + if (Icon->visible_tree != NULL) { + Eval(Icon->visible_tree, &result); + Icon->visible = R2N(&result); + if (Icon->visible < 1) + Icon->visible = 0; + DelResult(&result); + } + + /* rotate icon bitmap */ + Icon->curmap++; + if (Icon->curmap >= Icon->maxmap) + Icon->curmap = 0; + + /* finally, draw it! */ + if (W->class->draw) + W->class->draw(W); + + /* add a new one-shot timer */ + timer_add(widget_icon_update, Self, Icon->speed, 1); + } -int widget_icon_init (WIDGET *Self) +int widget_icon_init(WIDGET * Self) { - char *section; - WIDGET_ICON *Icon; - - /* prepare config section */ - /* strlen("Widget:")=7 */ - section=malloc(strlen(Self->name)+8); - strcpy(section, "Widget:"); - strcat(section, Self->name); - - Icon=malloc(sizeof(WIDGET_ICON)); - memset (Icon, 0, sizeof(WIDGET_ICON)); - - /* get raw expressions (we evaluate them ourselves) */ - Icon->speed_expr = cfg_get_raw (section, "speed", NULL); - Icon->visible_expr = cfg_get_raw (section, "visible", NULL); - - /* compile'em */ - Compile (Icon->speed_expr, &Icon->speed_tree); - Compile (Icon->visible_expr, &Icon->visible_tree); - - /* sanity check */ - if (Icon->speed_expr==NULL || *Icon->speed_expr=='\0') { - error ("Icon %s has no speed, using '100'", Self->name); - Icon->speed_expr="100"; - } - - /* read bitmap */ - widget_icon_read_bitmap (section, Icon); - - free (section); - Self->data=Icon; - - /* as the speed is evaluatod on every call, we use 'one-shot'-timers. */ - /* The timer will be reactivated on every call to widget_icon_update(). */ - /* We do the initial call here... */ - Icon->prvmap=-1; - - /* reset ascii */ - Icon->ascii=-1; - - /* just do it! */ - widget_icon_update(Self); - - return 0; + char *section; + WIDGET_ICON *Icon; + + /* prepare config section */ + /* strlen("Widget:")=7 */ + section = malloc(strlen(Self->name) + 8); + strcpy(section, "Widget:"); + strcat(section, Self->name); + + Icon = malloc(sizeof(WIDGET_ICON)); + memset(Icon, 0, sizeof(WIDGET_ICON)); + + /* get raw expressions (we evaluate them ourselves) */ + Icon->speed_expr = cfg_get_raw(section, "speed", NULL); + Icon->visible_expr = cfg_get_raw(section, "visible", NULL); + + /* compile'em */ + Compile(Icon->speed_expr, &Icon->speed_tree); + Compile(Icon->visible_expr, &Icon->visible_tree); + + /* sanity check */ + if (Icon->speed_expr == NULL || *Icon->speed_expr == '\0') { + error("Icon %s has no speed, using '100'", Self->name); + Icon->speed_expr = "100"; + } + + /* read bitmap */ + widget_icon_read_bitmap(section, Icon); + + free(section); + Self->data = Icon; + + /* as the speed is evaluatod on every call, we use 'one-shot'-timers. */ + /* The timer will be reactivated on every call to widget_icon_update(). */ + /* We do the initial call here... */ + Icon->prvmap = -1; + + /* reset ascii */ + Icon->ascii = -1; + + /* just do it! */ + widget_icon_update(Self); + + return 0; } -int widget_icon_quit (WIDGET *Self) +int widget_icon_quit(WIDGET * Self) { - if (Self) { - if (Self->data) { - WIDGET_ICON *Icon = Self->data; - DelTree(Icon->speed_tree); - DelTree(Icon->visible_tree); - if (Icon->bitmap) free (Icon->bitmap); - free(Self->data); - Self->data=NULL; + if (Self) { + if (Self->data) { + WIDGET_ICON *Icon = Self->data; + DelTree(Icon->speed_tree); + DelTree(Icon->visible_tree); + if (Icon->bitmap) + free(Icon->bitmap); + free(Self->data); + Self->data = NULL; + } } - } - - return 0; - + + return 0; + } WIDGET_CLASS Widget_Icon = { - name: "icon", - init: widget_icon_init, - draw: NULL, - quit: widget_icon_quit, + name:"icon", + init:widget_icon_init, + draw:NULL, + quit:widget_icon_quit, }; diff --git a/widget_icon.h b/widget_icon.h index 4b361e2..de86798 100644 --- a/widget_icon.h +++ b/widget_icon.h @@ -1,4 +1,4 @@ -/* $Id: widget_icon.h,v 1.8 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: widget_icon.h,v 1.9 2005/05/08 04:32:45 reinelt Exp $ * * icon widget handling * @@ -23,6 +23,9 @@ * * * $Log: widget_icon.h,v $ + * Revision 1.9 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.8 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -64,17 +67,17 @@ #define _WIDGET_ICON_H_ typedef struct WIDGET_ICON { - char *speed_expr; /* expression for update interval */ - void *speed_tree; /* pre-compiled expression for update interval */ - int speed; /* update interval (msec) */ - char *visible_expr; /* expression for visibility */ - void *visible_tree; /* pre-compiled expression for visibility */ - int visible; /* icon visible? */ - int ascii; /* ascii code of icon (depends on the driver) */ - int curmap; /* current bitmap sequence */ - int prvmap; /* previous bitmap sequence */ - int maxmap; /* number of bitmap sequences */ - unsigned char *bitmap; /* bitmaps of (animated) icon */ + char *speed_expr; /* expression for update interval */ + void *speed_tree; /* pre-compiled expression for update interval */ + int speed; /* update interval (msec) */ + char *visible_expr; /* expression for visibility */ + void *visible_tree; /* pre-compiled expression for visibility */ + int visible; /* icon visible? */ + int ascii; /* ascii code of icon (depends on the driver) */ + int curmap; /* current bitmap sequence */ + int prvmap; /* previous bitmap sequence */ + int maxmap; /* number of bitmap sequences */ + unsigned char *bitmap; /* bitmaps of (animated) icon */ } WIDGET_ICON; extern WIDGET_CLASS Widget_Icon; diff --git a/widget_text.c b/widget_text.c index 4275e99..f205a59 100644 --- a/widget_text.c +++ b/widget_text.c @@ -1,4 +1,4 @@ -/* $Id: widget_text.c,v 1.21 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: widget_text.c,v 1.22 2005/05/08 04:32:45 reinelt Exp $ * * simple text widget handling * @@ -21,6 +21,9 @@ * * * $Log: widget_text.c,v $ + * Revision 1.22 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.21 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -147,317 +150,335 @@ #endif -void widget_text_scroll (void *Self) +void widget_text_scroll(void *Self) { - WIDGET *W = (WIDGET*)Self; - WIDGET_TEXT *T = W->data; - - int num, len, width, pad; - char *src, *dst; - - num = 0; - len = strlen(T->value); - width = T->width-strlen(T->preval)-strlen(T->postval); - if (width<0) width=0; - - switch (T->align) { - case ALIGN_LEFT: - pad=0; - break; - case ALIGN_CENTER: - pad=(width - len)/2; - if (pad<0) pad=0; - break; - case ALIGN_RIGHT: - pad=width - len; - if (pad<0) pad=0; - break; - case ALIGN_MARQUEE: - pad=width - T->scroll; - T->scroll++; - if (T->scroll >= width+len) T->scroll=0; - break; - default: /* not reached */ - pad=0; - } - - dst=T->buffer; - - /* process prefix */ - src=T->preval; - while (num < T->width) { - if (*src=='\0') break; - *(dst++)=*(src++); - num++; - } - - src=T->value; - - /* pad blanks on the beginning */ - while (pad > 0 && num < T->width) { - *(dst++)=' '; - num++; - pad--; - } - - /* skip src chars (marquee) */ - while (pad<0) { - src++; - pad++; - } - - /* copy content */ - while (num < T->width) { - if (*src=='\0') break; - *(dst++)=*(src++); - num++; - } - - /* pad blanks on the end */ - src=T->postval; - len=strlen(src); - while (num < T->width-len) { - *(dst++)=' '; - num++; - } - - /* process postfix */ - while (num < T->width) { - if (*src=='\0') break; - *(dst++)=*(src++); - num++; - } - - *dst='\0'; - - /* finally, draw it! */ - if (W->class->draw) - W->class->draw(W); + WIDGET *W = (WIDGET *) Self; + WIDGET_TEXT *T = W->data; + + int num, len, width, pad; + char *src, *dst; + + num = 0; + len = strlen(T->value); + width = T->width - strlen(T->preval) - strlen(T->postval); + if (width < 0) + width = 0; + + switch (T->align) { + case ALIGN_LEFT: + pad = 0; + break; + case ALIGN_CENTER: + pad = (width - len) / 2; + if (pad < 0) + pad = 0; + break; + case ALIGN_RIGHT: + pad = width - len; + if (pad < 0) + pad = 0; + break; + case ALIGN_MARQUEE: + pad = width - T->scroll; + T->scroll++; + if (T->scroll >= width + len) + T->scroll = 0; + break; + default: /* not reached */ + pad = 0; + } + + dst = T->buffer; + + /* process prefix */ + src = T->preval; + while (num < T->width) { + if (*src == '\0') + break; + *(dst++) = *(src++); + num++; + } + + src = T->value; + + /* pad blanks on the beginning */ + while (pad > 0 && num < T->width) { + *(dst++) = ' '; + num++; + pad--; + } + + /* skip src chars (marquee) */ + while (pad < 0) { + src++; + pad++; + } + + /* copy content */ + while (num < T->width) { + if (*src == '\0') + break; + *(dst++) = *(src++); + num++; + } + + /* pad blanks on the end */ + src = T->postval; + len = strlen(src); + while (num < T->width - len) { + *(dst++) = ' '; + num++; + } + + /* process postfix */ + while (num < T->width) { + if (*src == '\0') + break; + *(dst++) = *(src++); + num++; + } + + *dst = '\0'; + + /* finally, draw it! */ + if (W->class->draw) + W->class->draw(W); } -void widget_text_update (void *Self) +void widget_text_update(void *Self) { - WIDGET *W = (WIDGET*)Self; - WIDGET_TEXT *T = W->data; - RESULT result = {0, 0, 0, NULL}; - char *preval, *postval, *value; - int update; - - /* evaluate prefix */ - if (T->pretree!=NULL) { - Eval(T->pretree, &result); - preval=strdup(R2S(&result)); - DelResult (&result); - } else { - preval=strdup(""); - } - - /* evaluate postfix */ - if (T->posttree!=NULL) { - Eval(T->posttree, &result); - postval=strdup(R2S(&result)); - DelResult (&result); - } else { - postval=strdup(""); - } - - /* evaluate expression */ - Eval(T->tree, &result); - - /* string or number? */ - if (T->precision==0xC0DE) { - value=strdup(R2S(&result)); - } else { - double number=R2N(&result); - int width=T->width-strlen(preval)-strlen(postval); - int precision=T->precision; - /* print zero bytes so we can specify NULL as target */ - /* and get the length of the resulting string */ - int size=snprintf (NULL, 0, "%.*f", precision, number); - /* number does not fit into field width: try to reduce precision */ - if (width<0) width=0; - if (size>width && precision>0) { - int delta=size-width; - if (delta>precision) delta=precision; - precision-=delta; - size-=delta; - /* zero precision: omit decimal point, too */ - if (precision==0) size--; + WIDGET *W = (WIDGET *) Self; + WIDGET_TEXT *T = W->data; + RESULT result = { 0, 0, 0, NULL }; + char *preval, *postval, *value; + int update; + + /* evaluate prefix */ + if (T->pretree != NULL) { + Eval(T->pretree, &result); + preval = strdup(R2S(&result)); + DelResult(&result); + } else { + preval = strdup(""); + } + + /* evaluate postfix */ + if (T->posttree != NULL) { + Eval(T->posttree, &result); + postval = strdup(R2S(&result)); + DelResult(&result); + } else { + postval = strdup(""); } - /* number still doesn't fit: display '*****' */ - if (size>width) { - value=malloc(width+1); - memset (value, '*', width); - *(value+width)='\0'; + + /* evaluate expression */ + Eval(T->tree, &result); + + /* string or number? */ + if (T->precision == 0xC0DE) { + value = strdup(R2S(&result)); } else { - value=malloc(size+1); - snprintf (value, size+1, "%.*f", precision, number); + double number = R2N(&result); + int width = T->width - strlen(preval) - strlen(postval); + int precision = T->precision; + /* print zero bytes so we can specify NULL as target */ + /* and get the length of the resulting string */ + int size = snprintf(NULL, 0, "%.*f", precision, number); + /* number does not fit into field width: try to reduce precision */ + if (width < 0) + width = 0; + if (size > width && precision > 0) { + int delta = size - width; + if (delta > precision) + delta = precision; + precision -= delta; + size -= delta; + /* zero precision: omit decimal point, too */ + if (precision == 0) + size--; + } + /* number still doesn't fit: display '*****' */ + if (size > width) { + value = malloc(width + 1); + memset(value, '*', width); + *(value + width) = '\0'; + } else { + value = malloc(size + 1); + snprintf(value, size + 1, "%.*f", precision, number); + } } - } - - DelResult (&result); - - update=0; - - /* prefix changed? */ - if (T->preval == NULL || strcmp(T->preval, preval)!=0) { - update=1; - if (T->preval) free (T->preval); - T->preval=preval; - T->scroll=0; /* reset marquee counter */ - } else { - free (preval); - } - - /* postfix changed? */ - if (T->postval == NULL || strcmp(T->postval, postval)!=0) { - update=1; - if (T->postval) free (T->postval); - T->postval=postval; - T->scroll=0; /* reset marquee counter */ - } else { - free (postval); - } - - /* value changed? */ - if (T->value == NULL || strcmp(T->value, value)!=0) { - update=1; - if (T->value) free (T->value); - T->value=value; - T->scroll=0; /* reset marquee counter */ - } else { - free (value); - } - - /* something has changed and should be updated */ - if (update) { - /* if there's a marquee scroller active, it has its own */ - /* update callback timer, so we do nothing here; otherwise */ - /* we simply call this scroll callback directly */ - if (T->align!=ALIGN_MARQUEE) { - widget_text_scroll (Self); + + DelResult(&result); + + update = 0; + + /* prefix changed? */ + if (T->preval == NULL || strcmp(T->preval, preval) != 0) { + update = 1; + if (T->preval) + free(T->preval); + T->preval = preval; + T->scroll = 0; /* reset marquee counter */ + } else { + free(preval); + } + + /* postfix changed? */ + if (T->postval == NULL || strcmp(T->postval, postval) != 0) { + update = 1; + if (T->postval) + free(T->postval); + T->postval = postval; + T->scroll = 0; /* reset marquee counter */ + } else { + free(postval); + } + + /* value changed? */ + if (T->value == NULL || strcmp(T->value, value) != 0) { + update = 1; + if (T->value) + free(T->value); + T->value = value; + T->scroll = 0; /* reset marquee counter */ + } else { + free(value); + } + + /* something has changed and should be updated */ + if (update) { + /* if there's a marquee scroller active, it has its own */ + /* update callback timer, so we do nothing here; otherwise */ + /* we simply call this scroll callback directly */ + if (T->align != ALIGN_MARQUEE) { + widget_text_scroll(Self); + } } - } } -int widget_text_init (WIDGET *Self) +int widget_text_init(WIDGET * Self) { - char *section; char *c; - WIDGET_TEXT *Text; - - /* prepare config section */ - /* strlen("Widget:")=7 */ - section=malloc(strlen(Self->name)+8); - strcpy(section, "Widget:"); - strcat(section, Self->name); - - Text=malloc(sizeof(WIDGET_TEXT)); - memset (Text, 0, sizeof(WIDGET_TEXT)); - - /* get raw pre- and postfix (we evaluate it ourselves) */ - Text->prefix = cfg_get_raw (section, "prefix", NULL); - Text->postfix = cfg_get_raw (section, "postfix", NULL); - - /* compile pre- and postfix */ - Compile (Text->prefix, &Text->pretree); - Compile (Text->postfix, &Text->posttree); - - /* get raw expression (we evaluate it ourselves) */ - Text->expression = cfg_get_raw (section, "expression", "''"); - Compile (Text->expression, &Text->tree); - - /* field width, default 10 */ - cfg_number (section, "width", 10, 0, -1, &(Text->width)); - - /* precision: number of digits after the decimal point (default: none) */ - /* Note: this is the *maximum* precision on small values, */ - /* for larger values the precision may be reduced to fit into the field width. */ - /* The default value 0xC0DE is used to distinguish between numbers and strings: */ - /* if no precision is given, the result is always treated as a string. If a */ - /* precision is specified, the result is treated as a number. */ - cfg_number (section, "precision", 0xC0DE, 0, 80, &(Text->precision)); - - /* field alignment: Left (default), Center, Right or Marquee */ - c = cfg_get (section, "align", "L"); - switch (toupper(*c)) { - case 'L': - Text->align=ALIGN_LEFT; - break; - case 'C': - Text->align=ALIGN_CENTER; - break; - case 'R': - Text->align=ALIGN_RIGHT; - break; - case 'M': - Text->align=ALIGN_MARQUEE; - break; - default: - error ("widget %s has unknown alignment '%s', using 'Left'", section, c); - Text->align=ALIGN_LEFT; - } - free (c); - - /* update interval (msec), default 1 sec, 0 stands for never */ - cfg_number (section, "update", 1000, 0, -1, &(Text->update)); - /* limit update interval to min 10 msec */ - if (Text->update > 0 && Text->update < 10) Text->update = 10; - - /* marquee scroller speed: interval (msec), default 500msec */ - if (Text->align==ALIGN_MARQUEE) { - cfg_number (section, "speed", 500, 10, -1, &(Text->speed)); - } - - /* buffer */ - Text->buffer=malloc(Text->width+1); - - free (section); - Self->data=Text; - - /* add update timer, use one-shot if 'update' is zero */ - timer_add (widget_text_update, Self, Text->update, Text->update==0); - - /* a marquee scroller has its own timer and callback */ - if (Text->align==ALIGN_MARQUEE) { - timer_add (widget_text_scroll, Self, Text->speed, 0); - } - - return 0; + char *section; + char *c; + WIDGET_TEXT *Text; + + /* prepare config section */ + /* strlen("Widget:")=7 */ + section = malloc(strlen(Self->name) + 8); + strcpy(section, "Widget:"); + strcat(section, Self->name); + + Text = malloc(sizeof(WIDGET_TEXT)); + memset(Text, 0, sizeof(WIDGET_TEXT)); + + /* get raw pre- and postfix (we evaluate it ourselves) */ + Text->prefix = cfg_get_raw(section, "prefix", NULL); + Text->postfix = cfg_get_raw(section, "postfix", NULL); + + /* compile pre- and postfix */ + Compile(Text->prefix, &Text->pretree); + Compile(Text->postfix, &Text->posttree); + + /* get raw expression (we evaluate it ourselves) */ + Text->expression = cfg_get_raw(section, "expression", "''"); + Compile(Text->expression, &Text->tree); + + /* field width, default 10 */ + cfg_number(section, "width", 10, 0, -1, &(Text->width)); + + /* precision: number of digits after the decimal point (default: none) */ + /* Note: this is the *maximum* precision on small values, */ + /* for larger values the precision may be reduced to fit into the field width. */ + /* The default value 0xC0DE is used to distinguish between numbers and strings: */ + /* if no precision is given, the result is always treated as a string. If a */ + /* precision is specified, the result is treated as a number. */ + cfg_number(section, "precision", 0xC0DE, 0, 80, &(Text->precision)); + + /* field alignment: Left (default), Center, Right or Marquee */ + c = cfg_get(section, "align", "L"); + switch (toupper(*c)) { + case 'L': + Text->align = ALIGN_LEFT; + break; + case 'C': + Text->align = ALIGN_CENTER; + break; + case 'R': + Text->align = ALIGN_RIGHT; + break; + case 'M': + Text->align = ALIGN_MARQUEE; + break; + default: + error("widget %s has unknown alignment '%s', using 'Left'", section, c); + Text->align = ALIGN_LEFT; + } + free(c); + + /* update interval (msec), default 1 sec, 0 stands for never */ + cfg_number(section, "update", 1000, 0, -1, &(Text->update)); + /* limit update interval to min 10 msec */ + if (Text->update > 0 && Text->update < 10) + Text->update = 10; + + /* marquee scroller speed: interval (msec), default 500msec */ + if (Text->align == ALIGN_MARQUEE) { + cfg_number(section, "speed", 500, 10, -1, &(Text->speed)); + } + + /* buffer */ + Text->buffer = malloc(Text->width + 1); + + free(section); + Self->data = Text; + + /* add update timer, use one-shot if 'update' is zero */ + timer_add(widget_text_update, Self, Text->update, Text->update == 0); + + /* a marquee scroller has its own timer and callback */ + if (Text->align == ALIGN_MARQUEE) { + timer_add(widget_text_scroll, Self, Text->speed, 0); + } + + return 0; } -int widget_text_quit (WIDGET *Self) { - WIDGET_TEXT *Text; - if (Self) { - Text=Self->data; - if (Self->data) { - DelTree(Text->pretree); - DelTree(Text->posttree); - DelTree(Text->tree); - if (Text->preval) free(Text->preval); - if (Text->postval) free(Text->postval); - if (Text->value) free(Text->value); - if (Text->buffer) free(Text->buffer); - free (Self->data); - Self->data=NULL; +int widget_text_quit(WIDGET * Self) +{ + WIDGET_TEXT *Text; + if (Self) { + Text = Self->data; + if (Self->data) { + DelTree(Text->pretree); + DelTree(Text->posttree); + DelTree(Text->tree); + if (Text->preval) + free(Text->preval); + if (Text->postval) + free(Text->postval); + if (Text->value) + free(Text->value); + if (Text->buffer) + free(Text->buffer); + free(Self->data); + Self->data = NULL; + } + } - - } - return 0; - + return 0; + } WIDGET_CLASS Widget_Text = { - name: "text", - init: widget_text_init, - draw: NULL, - quit: widget_text_quit, + name:"text", + init:widget_text_init, + draw:NULL, + quit:widget_text_quit, }; - - diff --git a/widget_text.h b/widget_text.h index 6aa03f3..5708693 100644 --- a/widget_text.h +++ b/widget_text.h @@ -1,4 +1,4 @@ -/* $Id: widget_text.h,v 1.6 2005/01/18 06:30:24 reinelt Exp $ +/* $Id: widget_text.h,v 1.7 2005/05/08 04:32:45 reinelt Exp $ * * simple text widget handling * @@ -23,6 +23,9 @@ * * * $Log: widget_text.h,v $ + * Revision 1.7 2005/05/08 04:32:45 reinelt + * CodingStyle added and applied + * * Revision 1.6 2005/01/18 06:30:24 reinelt * added (C) to all copyright statements * @@ -62,22 +65,22 @@ typedef enum { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_MARQUEE } ALIGN; typedef struct WIDGET_TEXT { - char *prefix; /* expression for label on the left side */ - void *pretree; /* pre-compiled expression for label on the left side */ - char *preval; /* value for label on the left side */ - char *postfix; /* expression for label on the right side */ - void *posttree; /* pre-compiled expression for label on the right side */ - char *postval; /* value for label on the right side */ - char *expression; /* expression that delivers the value */ - void *tree; /* pre-compiled expression that delivers the value */ - char *value; /* evaluated value from expression */ - char *buffer; /* string with 'width+1' bytes allocated */ - int width; /* field width */ - int precision; /* number of digits after the decimal point */ - ALIGN align; /* alignment: L, C, R, M(arquee) */ - int update; /* update interval */ - int scroll; /* marquee starting point */ - int speed; /* marquee scrolling speed */ + char *prefix; /* expression for label on the left side */ + void *pretree; /* pre-compiled expression for label on the left side */ + char *preval; /* value for label on the left side */ + char *postfix; /* expression for label on the right side */ + void *posttree; /* pre-compiled expression for label on the right side */ + char *postval; /* value for label on the right side */ + char *expression; /* expression that delivers the value */ + void *tree; /* pre-compiled expression that delivers the value */ + char *value; /* evaluated value from expression */ + char *buffer; /* string with 'width+1' bytes allocated */ + int width; /* field width */ + int precision; /* number of digits after the decimal point */ + ALIGN align; /* alignment: L, C, R, M(arquee) */ + int update; /* update interval */ + int scroll; /* marquee starting point */ + int speed; /* marquee scrolling speed */ } WIDGET_TEXT; |