fix issues in widget_create() in tight memory situations (Andre Guibert de Bruet)
This commit is contained in:
+71
-19
@@ -9,6 +9,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 1999, William Ferrell, Scott Scriven
|
* Copyright (c) 1999, William Ferrell, Scott Scriven
|
||||||
* 2002, Joris Robijn
|
* 2002, Joris Robijn
|
||||||
|
* 2008, Peter Marschall
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -24,16 +25,16 @@
|
|||||||
#include "drivers/lcd.h"
|
#include "drivers/lcd.h"
|
||||||
|
|
||||||
char *typenames[] = {
|
char *typenames[] = {
|
||||||
"none",
|
"none", /* WID_NONE */
|
||||||
"string",
|
"string", /* WID_STRING */
|
||||||
"hbar",
|
"hbar", /* WID_HBAR */
|
||||||
"vbar",
|
"vbar", /* WID_VBAR */
|
||||||
"icon",
|
"icon", /* WID_ICON */
|
||||||
"title",
|
"title", /* WID_TITLE */
|
||||||
"scroller",
|
"scroller", /* WID_SCROLLER */
|
||||||
"frame",
|
"frame", /* WID_FRAME */
|
||||||
"num",
|
"num", /* WID_NUM */
|
||||||
NULL,
|
NULL, /* WID_NONE */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct icontable {
|
struct icontable {
|
||||||
@@ -66,6 +67,12 @@ struct icontable {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/** Create a widget.
|
||||||
|
* \param id Widget identifier; it's name.
|
||||||
|
* \param type Widget type.
|
||||||
|
* \param screen Screen on which the widget is to be placed.
|
||||||
|
* \return Pointer to the freshly created widget.
|
||||||
|
*/
|
||||||
Widget *
|
Widget *
|
||||||
widget_create(char *id, WidgetType type, Screen *screen)
|
widget_create(char *id, WidgetType type, Screen *screen)
|
||||||
{
|
{
|
||||||
@@ -75,14 +82,15 @@ widget_create(char *id, WidgetType type, Screen *screen)
|
|||||||
|
|
||||||
/* Create it */
|
/* Create it */
|
||||||
w = malloc(sizeof(Widget));
|
w = malloc(sizeof(Widget));
|
||||||
if (!w) {
|
if (w == NULL) {
|
||||||
report(RPT_DEBUG, "%s: Error allocating", __FUNCTION__);
|
report(RPT_DEBUG, "%s: Error allocating", __FUNCTION__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
w->id = strdup(id);
|
w->id = strdup(id);
|
||||||
if (!w->id) {
|
if (w->id == NULL) {
|
||||||
report(RPT_DEBUG, "%s: Error allocating", __FUNCTION__);
|
report(RPT_DEBUG, "%s: Error allocating", __FUNCTION__);
|
||||||
|
free(w);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,8 +111,14 @@ widget_create(char *id, WidgetType type, Screen *screen)
|
|||||||
|
|
||||||
if (w->type == WID_FRAME) {
|
if (w->type == WID_FRAME) {
|
||||||
/* create a screen for the frame widget */
|
/* create a screen for the frame widget */
|
||||||
char *frame_name;
|
char *frame_name = malloc(strlen("frame_") + strlen(id) + 1);
|
||||||
frame_name = malloc(strlen("frame_") + strlen(id) + 1);
|
|
||||||
|
if (frame_name == NULL) {
|
||||||
|
report(RPT_DEBUG, "%s: Error allocating", __FUNCTION__);
|
||||||
|
free(w->id);
|
||||||
|
free(w);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
strcpy(frame_name, "frame_");
|
strcpy(frame_name, "frame_");
|
||||||
strcat(frame_name, id);
|
strcat(frame_name, id);
|
||||||
|
|
||||||
@@ -115,6 +129,12 @@ widget_create(char *id, WidgetType type, Screen *screen)
|
|||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Destroy a widget.
|
||||||
|
* \param w Widget to destroy.
|
||||||
|
* \retval <0 Error; no widget given.
|
||||||
|
* \retval 0 Success.
|
||||||
|
*/
|
||||||
int
|
int
|
||||||
widget_destroy(Widget *w)
|
widget_destroy(Widget *w)
|
||||||
{
|
{
|
||||||
@@ -123,28 +143,39 @@ widget_destroy(Widget *w)
|
|||||||
if (!w)
|
if (!w)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (w->id)
|
if (w->id != NULL) {
|
||||||
free(w->id);
|
free(w->id);
|
||||||
if (w->text)
|
w->id = NULL;
|
||||||
|
}
|
||||||
|
if (w->text != NULL) {
|
||||||
free(w->text);
|
free(w->text);
|
||||||
|
w->text = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* Free subscreen of frame widget too */
|
/* Free subscreen of frame widget too */
|
||||||
if (w->type == WID_FRAME) {
|
if (w->type == WID_FRAME) {
|
||||||
screen_destroy(w->frame_screen);
|
screen_destroy(w->frame_screen);
|
||||||
|
w->frame_screen = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(w);
|
free(w);
|
||||||
|
w = NULL;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Convert a widget type name to a widget type.
|
||||||
|
* \param typename Name of the idget type.
|
||||||
|
* \return Widget type.
|
||||||
|
*/
|
||||||
WidgetType
|
WidgetType
|
||||||
widget_typename_to_type(char *typename)
|
widget_typename_to_type(char *typename)
|
||||||
{
|
{
|
||||||
WidgetType wid_type = WID_NONE;
|
WidgetType wid_type = WID_NONE;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; typenames[i]; i++) {
|
for (i = 0; typenames[i] != NULL; i++) {
|
||||||
if (strcmp(typenames[i], typename) == 0) {
|
if (strcmp(typenames[i], typename) == 0) {
|
||||||
wid_type = i;
|
wid_type = i;
|
||||||
break; /* it's valid: skip out...*/
|
break; /* it's valid: skip out...*/
|
||||||
@@ -153,12 +184,23 @@ widget_typename_to_type(char *typename)
|
|||||||
return wid_type;
|
return wid_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Convert a widget type to the associated type name.
|
||||||
|
* \param t Widget type.
|
||||||
|
* \return Widget type's name.
|
||||||
|
*/
|
||||||
char *
|
char *
|
||||||
widget_type_to_typename(WidgetType t)
|
widget_type_to_typename(WidgetType t)
|
||||||
{
|
{
|
||||||
return typenames[t];
|
return typenames[t];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Find subordinate widgets of a widget by name.
|
||||||
|
* \param w Widget.
|
||||||
|
* \param id Name of the subiordinate widget.
|
||||||
|
* \return Pointer to the sub-widget; \c NULL if not found or on error.
|
||||||
|
*/
|
||||||
Widget *
|
Widget *
|
||||||
widget_search_subs(Widget *w, char *id)
|
widget_search_subs(Widget *w, char *id)
|
||||||
{
|
{
|
||||||
@@ -169,11 +211,16 @@ widget_search_subs(Widget *w, char *id)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Find a widget icon by type.
|
||||||
|
* \param icon Icon type.
|
||||||
|
* \return Pointer to constant string containing the icon name.
|
||||||
|
*/
|
||||||
char *widget_icon_to_iconname(int icon)
|
char *widget_icon_to_iconname(int icon)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; icontable[i].iconname; i++) {
|
for (i = 0; icontable[i].iconname != NULL; i++) {
|
||||||
if (icontable[i].icon == icon) {
|
if (icontable[i].icon == icon) {
|
||||||
return icontable[i].iconname;
|
return icontable[i].iconname;
|
||||||
}
|
}
|
||||||
@@ -182,11 +229,16 @@ char *widget_icon_to_iconname(int icon)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Find a widget icon by name.
|
||||||
|
* \param iconname Icon name.
|
||||||
|
* \return Icon type
|
||||||
|
*/
|
||||||
int widget_iconname_to_icon(char *iconname)
|
int widget_iconname_to_icon(char *iconname)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; icontable[i].iconname; i++) {
|
for (i = 0; icontable[i].iconname != NULL; i++) {
|
||||||
if (strcasecmp(icontable[i].iconname, iconname) == 0) {
|
if (strcasecmp(icontable[i].iconname, iconname) == 0) {
|
||||||
return icontable[i].icon;
|
return icontable[i].icon;
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-10
@@ -1,4 +1,5 @@
|
|||||||
/** \file server/widget.h
|
/** \file server/widget.h
|
||||||
|
* Public interface to the widget methods.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* This file is part of LCDd, the lcdproc server.
|
/* This file is part of LCDd, the lcdproc server.
|
||||||
@@ -30,17 +31,19 @@ typedef enum WidgetType {
|
|||||||
WID_NUM
|
WID_NUM
|
||||||
} WidgetType;
|
} WidgetType;
|
||||||
|
|
||||||
|
|
||||||
|
/** Widget structure */
|
||||||
typedef struct Widget {
|
typedef struct Widget {
|
||||||
char *id;
|
char *id; /**< the widget's name */
|
||||||
WidgetType type;
|
WidgetType type; /**< the widget's type */
|
||||||
Screen *screen; /* What screen is this widget in ? */
|
Screen *screen; /**< What screen is this widget in ? */
|
||||||
int x, y; /* Position */
|
int x, y; /**< Position */
|
||||||
int width, height; /* Visible size */
|
int width, height; /**< Visible size */
|
||||||
int left, top, right, bottom; /* bounding rectangle */
|
int left, top, right, bottom; /**< bounding rectangle */
|
||||||
int length; /* size or direction */
|
int length; /**< size or direction */
|
||||||
int speed; /* For scroller... */
|
int speed; /**< For scroller... */
|
||||||
char *text; /* text or binary data */
|
char *text; /**< text or binary data */
|
||||||
struct Screen *frame_screen; /* frame widget get an associated screen */
|
struct Screen *frame_screen; /**< frame widget get an associated screen */
|
||||||
//LinkedList *kids; /* Frames can contain more widgets...*/
|
//LinkedList *kids; /* Frames can contain more widgets...*/
|
||||||
} Widget;
|
} Widget;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user