diff options
| -rw-r--r-- | lcd4linux.conf.sample | 1 | ||||
| -rw-r--r-- | property.c | 41 | ||||
| -rw-r--r-- | property.h | 7 | ||||
| -rw-r--r-- | widget_text.c | 152 | ||||
| -rw-r--r-- | widget_text.h | 20 | 
5 files changed, 105 insertions, 116 deletions
diff --git a/lcd4linux.conf.sample b/lcd4linux.conf.sample index 0df2836..6b15dc1 100644 --- a/lcd4linux.conf.sample +++ b/lcd4linux.conf.sample @@ -457,6 +457,7 @@ Widget CPU {      prefix 'CPU '      width  9      align  'L' +    style test::onoff(7)>0?'bold':'norm'      update tick  } @@ -1,4 +1,4 @@ -/* $Id: property.c,v 1.1 2006/08/13 09:53:10 reinelt Exp $ +/* $Id: property.c,v 1.2 2006/08/13 11:38:20 reinelt Exp $   *   * dynamic properties   * @@ -23,6 +23,9 @@   *   *   * $Log: property.c,v $ + * Revision 1.2  2006/08/13 11:38:20  reinelt + * text widget uses dynamic properties + *   * Revision 1.1  2006/08/13 09:53:10  reinelt   * dynamic properties added (used by 'style' of text widget)   * @@ -38,6 +41,8 @@   * void property_free (PROPERTY *prop)   *   frees all property allocations   * + * int property_eval(PROPERTY * prop) + *   evaluates a property; returns 1 if value has changed   *   * double P2N(PROPERTY * prop)   *   returns a (already evaluated) property as number @@ -84,12 +89,38 @@ void property_load(const char *section, const char *name, const char *defval, PR  } -void property_eval(PROPERTY * prop) +int property_eval(PROPERTY * prop)  { -    if (prop->compiled != NULL) { -	DelResult(&prop->result); -	Eval(prop->compiled, &prop->result); +    RESULT old; +    int update = 1; +     +    /* this is a bit ugly: we need to remember the old value */ +     +    old.type = prop->result.type; +    old.size = prop->result.size; +    old.number = prop->result.number; +    old.string = prop->result.string != NULL ? strdup(prop->result.string) : NULL;  +     +    DelResult(&prop->result); +    Eval(prop->compiled, &prop->result); +     +    if (prop->result.type & R_NUMBER && old.type & R_NUMBER && prop->result.number == old.number) { +	update = 0;      } +     +    if (prop->result.type & R_STRING && old.type & R_STRING && prop->result.size == old.size) { +	if (prop->result.string == NULL && old.string == NULL) { +	    update = 0; +	} +	else if (prop->result.string != NULL && old.string != NULL && strcmp(prop->result.string, old.string) == 0) { +	    update = 0; +	} +    } +     +    if (old.string) +	free (old.string); + +    return update;  } @@ -1,4 +1,4 @@ -/* $Id: property.h,v 1.1 2006/08/13 09:53:10 reinelt Exp $ +/* $Id: property.h,v 1.2 2006/08/13 11:38:20 reinelt Exp $   *   * dynamic properties   * @@ -23,6 +23,9 @@   *   *   * $Log: property.h,v $ + * Revision 1.2  2006/08/13 11:38:20  reinelt + * text widget uses dynamic properties + *   * Revision 1.1  2006/08/13 09:53:10  reinelt   * dynamic properties added (used by 'style' of text widget)   * @@ -42,7 +45,7 @@ typedef struct {  void property_load(const char *section, const char *name, const char *defval, PROPERTY * prop); -void property_eval(PROPERTY * prop); +int property_eval(PROPERTY * prop);  double P2N(PROPERTY * prop);  char *P2S(PROPERTY * prop);  void property_free(PROPERTY * prop); diff --git a/widget_text.c b/widget_text.c index 4fd77d6..033d6f8 100644 --- a/widget_text.c +++ b/widget_text.c @@ -1,4 +1,4 @@ -/* $Id: widget_text.c,v 1.25 2006/08/13 09:53:10 reinelt Exp $ +/* $Id: widget_text.c,v 1.26 2006/08/13 11:38:20 reinelt Exp $   *   * simple text widget handling   * @@ -21,6 +21,9 @@   *   *   * $Log: widget_text.c,v $ + * Revision 1.26  2006/08/13 11:38:20  reinelt + * text widget uses dynamic properties + *   * Revision 1.25  2006/08/13 09:53:10  reinelt   * dynamic properties added (used by 'style' of text widget)   * @@ -165,12 +168,17 @@ void widget_text_scroll(void *Self)      WIDGET *W = (WIDGET *) Self;      WIDGET_TEXT *T = W->data; +    char *prefix = P2S(&T->prefix); +    char *postfix = P2S(&T->postfix); + +    char *string = T->string; +          int num, len, width, pad;      char *src, *dst;      num = 0; -    len = strlen(T->value); -    width = T->width - strlen(T->preval) - strlen(T->postval); +    len = strlen(string); +    width = T->width - strlen(prefix) - strlen(postfix);      if (width < 0)  	width = 0; @@ -201,7 +209,7 @@ void widget_text_scroll(void *Self)      dst = T->buffer;      /* process prefix */ -    src = T->preval; +    src = prefix;      while (num < T->width) {  	if (*src == '\0')  	    break; @@ -209,7 +217,7 @@ void widget_text_scroll(void *Self)  	num++;      } -    src = T->value; +    src = string;      /* pad blanks on the beginning */      while (pad > 0 && num < T->width) { @@ -233,7 +241,7 @@ void widget_text_scroll(void *Self)      }      /* pad blanks on the end */ -    src = T->postval; +    src = postfix;      len = strlen(src);      while (num < T->width - len) {  	*(dst++) = ' '; @@ -261,37 +269,22 @@ 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(""); -    } +    char *string; +    int update = 0; -    /* evaluate postfix */ -    if (T->posttree != NULL) { -	Eval(T->posttree, &result); -	postval = strdup(R2S(&result)); -	DelResult(&result); -    } else { -	postval = strdup(""); -    } +    /* evaluate prefix and postfix */ +    update += property_eval(&T->prefix); +    update += property_eval(&T->postfix); -    /* evaluate expression */ -    Eval(T->tree, &result); +    /* evaluate value */ +    property_eval(&T->value);      /* string or number? */ -    if (T->precision == 0xC0DE) { -	value = strdup(R2S(&result)); +    if (T->precision == 0xDEAD) { +	string = strdup(P2S(&T->value));      } else { -	double number = R2N(&result); -	int width = T->width - strlen(preval) - strlen(postval); +	double number = P2N(&T->value); +	int width = T->width - strlen(P2S(&T->prefix)) - strlen(P2S(&T->postfix));  	int precision = T->precision;  	/* print zero bytes so we can specify NULL as target  */  	/* and get the length of the resulting string */ @@ -311,57 +304,32 @@ void widget_text_update(void *Self)  	}  	/* number still doesn't fit: display '*****'  */  	if (size > width) { -	    value = malloc(width + 1); -	    memset(value, '*', width); -	    *(value + width) = '\0'; +	    string = malloc(width + 1); +	    memset(string, '*', width); +	    *(string + width) = '\0';  	} else { -	    value = malloc(size + 1); -	    snprintf(value, size + 1, "%.*f", precision, number); +	    string = malloc(size + 1); +	    snprintf(string, 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 */ +    /* did the formatted string change? */ +    if (T->string == NULL || strcmp(T->string, string) != 0) { +	update++; +	if (T->string) +	    free(T->string); +	T->string = string;      } 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); +	free(string);      }      /* text style */ -    property_eval(&T->style); - +    update += property_eval(&T->style); +          /* something has changed and should be updated */      if (update) { +	/* reset marquee counter if content has changed */ +	T->scroll = 0;  	/* 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 */ @@ -388,17 +356,11 @@ int widget_text_init(WIDGET * Self)      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); +    /* load properties */ +    property_load(section, "prefix", NULL, &Text->prefix); +    property_load(section, "expression", NULL, &Text->value); +    property_load(section, "postfix", NULL, &Text->postfix); +    property_load(section, "style", "norm", &Text->style);      /* field width, default 10 */      cfg_number(section, "width", 10, 0, -1, &(Text->width)); @@ -406,10 +368,10 @@ int widget_text_init(WIDGET * Self)      /* 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: */ +    /* The default value 0xDEAD 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)); +    cfg_number(section, "precision", 0xDEAD, 0, 80, &(Text->precision));      /* field alignment: Left (default), Center, Right or Marquee */      c = cfg_get(section, "align", "L"); @@ -432,8 +394,6 @@ int widget_text_init(WIDGET * Self)      }      free(c); -    property_load(section, "style", "norm", &Text->style); -      /* 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 */ @@ -469,18 +429,14 @@ int widget_text_quit(WIDGET * Self)      if (Self) {  	Text = Self->data;  	if (Self->data) { -	    DelTree(Text->pretree); -	    if (Text->preval) -		free(Text->preval); -	    DelTree(Text->posttree); -	    if (Text->postval) -		free(Text->postval); -	    DelTree(Text->tree); -	    if (Text->value) -		free(Text->value); +	    property_free(&Text->prefix); +	    property_free(&Text->value); +	    property_free(&Text->postfix); +	    property_free(&Text->style); +	    if (Text->string) +		free(Text->string);  	    if (Text->buffer)  		free(Text->buffer); -	    property_free(&Text->style);  	    free(Self->data);  	    Self->data = NULL;  	} diff --git a/widget_text.h b/widget_text.h index f864410..61e5b35 100644 --- a/widget_text.h +++ b/widget_text.h @@ -1,4 +1,4 @@ -/* $Id: widget_text.h,v 1.8 2006/08/13 09:53:10 reinelt Exp $ +/* $Id: widget_text.h,v 1.9 2006/08/13 11:38:20 reinelt Exp $   *   * simple text widget handling   * @@ -23,6 +23,9 @@   *   *   * $Log: widget_text.h,v $ + * Revision 1.9  2006/08/13 11:38:20  reinelt + * text widget uses dynamic properties + *   * Revision 1.8  2006/08/13 09:53:10  reinelt   * dynamic properties added (used by 'style' of text widget)   * @@ -72,20 +75,15 @@  typedef enum { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_MARQUEE } TEXT_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 */ +    PROPERTY prefix;		/* label on the left side */ +    PROPERTY postfix;		/* label on the right side */ +    PROPERTY value;             /* value of text widget */ +    PROPERTY style;		/* text style (plain/bold/slant) */ +    char *string;               /* formatted value */      char *buffer;		/* string with 'width+1' bytes allocated  */      int width;			/* field width */      int precision;		/* number of digits after the decimal point */      TEXT_ALIGN align;		/* alignment: L(eft), C(enter), R(ight), M(arquee) */ -    PROPERTY style;		/* text style (plain/bold/slant) */      int update;			/* update interval */      int scroll;			/* marquee starting point */      int speed;			/* marquee scrolling speed */  | 
