Files
lcdproc/server/widget.c
T

362 lines
6.5 KiB
C
Raw Normal View History

/*
* widget.c
* This file is part of LCDd, the lcdproc server.
*
* This file is released under the GNU General Public License. Refer to the
* COPYING file distributed with this package.
*
* Copyright (c) 1999, William Ferrell, Scott Scriven
*
*
* Does widget management
*
*/
1999-12-09 23:15:14 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "shared/sockets.h"
2001-11-29 14:09:13 +00:00
#include "shared/report.h"
1999-12-09 23:15:14 +00:00
#include "screen.h"
#include "widget.h"
#include "render.h"
1999-12-09 23:15:14 +00:00
char *types[] = { "none",
2000-04-03 22:13:57 +00:00
"string",
"hbar",
"vbar",
"icon",
"title",
"scroller",
"frame",
"num",
2001-12-30 00:15:25 +00:00
/*"",*/
2000-04-03 22:13:57 +00:00
NULL,
};
1999-12-09 23:15:14 +00:00
static widget *widget_finder (LinkedList * list, char *id);
static int widget_remover (LinkedList * list, widget * w);
widget *
widget_create ()
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
widget *w;
1999-12-09 23:15:14 +00:00
2001-11-29 14:09:13 +00:00
report (RPT_INFO, "widget_create()");
2000-04-03 22:13:57 +00:00
w = malloc (sizeof (widget));
if (!w) {
2001-11-29 14:09:13 +00:00
report (RPT_DEBUG, "widget_create: Error allocating widget");
2000-04-03 22:13:57 +00:00
return NULL;
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
w->id = NULL;
w->type = 0;
w->x = 1;
w->y = 1;
w->wid = 0;
w->hgt = 0;
w->left = 1;
w->top = 1;
w->right = 0;
w->bottom = 0;
w->length = 1;
w->speed = 1;
w->text = NULL;
w->kids = NULL;
2000-04-03 22:13:57 +00:00
return w;
1999-12-09 23:15:14 +00:00
}
int
widget_destroy (widget * w)
1999-12-09 23:15:14 +00:00
{
LinkedList *list;
2000-04-03 22:13:57 +00:00
widget *foo;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (!w)
return -1;
2001-11-29 14:09:13 +00:00
debug (RPT_INFO, "widget_destroy(%s)", w->id);
2000-04-03 22:13:57 +00:00
if (w->id)
free (w->id);
2001-12-30 00:15:25 +00:00
/*debug(RPT_DEBUG, "widget_destroy: id...");*/
2000-04-03 22:13:57 +00:00
if (w->text)
free (w->text);
2001-12-30 00:15:25 +00:00
/*debug(RPT_DEBUG, "widget_destroy: text...");*/
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* TODO: Free kids!*/
2000-04-03 22:13:57 +00:00
if (w->kids) {
list = w->kids;
LL_Rewind (list);
do {
foo = LL_Get (list);
if (foo)
widget_destroy (foo);
} while (0 == LL_Next (list));
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
LL_Destroy (list);
w->kids = NULL;
}
2000-04-03 22:13:57 +00:00
free (w);
2001-12-30 00:15:25 +00:00
/*debug(RPT_DEBUG, "widget_destroy: widget...");*/
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}
widget *
widget_find (screen * s, char *id)
1999-12-09 23:15:14 +00:00
{
2001-12-30 00:15:25 +00:00
/*widget *w, *err;*/
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (!s)
return NULL;
if (!id)
return NULL;
1999-12-09 23:15:14 +00:00
2001-11-29 14:09:13 +00:00
debug (RPT_DEBUG, "widget_find(%s)", id);
2000-04-03 22:13:57 +00:00
return widget_finder (s->widgets, id);
1999-12-09 23:15:14 +00:00
}
widget *
widget_finder (LinkedList * list, char *id)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
widget *w, *err;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (!list)
return NULL;
if (!id)
return NULL;
1999-12-09 23:15:14 +00:00
2001-11-29 14:09:13 +00:00
debug (RPT_DEBUG, "widget_finder(%s)", id);
2000-04-03 22:13:57 +00:00
LL_Rewind (list);
do {
2001-12-30 00:15:25 +00:00
/*debug(RPT_DEBUG, "widget_finder: Iteration");*/
2000-04-03 22:13:57 +00:00
w = LL_Get (list);
if (w) {
2001-12-30 00:15:25 +00:00
/*debug(RPT_DEBUG, "widget_finder: ...");*/
2000-04-03 22:13:57 +00:00
if (0 == strcmp (w->id, id)) {
2001-11-29 14:09:13 +00:00
debug (RPT_DEBUG, "widget_finder: Found %s", id);
2000-04-03 22:13:57 +00:00
return w;
}
2001-12-30 00:15:25 +00:00
/* Search kids recursively*/
/*debug(RPT_DEBUG, "widget_finder: ...");*/
2000-04-03 22:13:57 +00:00
if (w->type == WID_FRAME) {
err = widget_finder (w->kids, id);
if (err)
return err;
}
2001-12-30 00:15:25 +00:00
/*debug(RPT_DEBUG, "widget_finder: ...");*/
2000-04-03 22:13:57 +00:00
}
2000-04-03 22:13:57 +00:00
} while (LL_Next (list) == 0);
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return NULL;
1999-12-09 23:15:14 +00:00
}
int
widget_add (screen * s, char *id, char *type, char *in, int sock)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
int i;
int valid = 0;
int wid_type = 0;
widget *w, *parent;
LinkedList *list;
1999-12-09 23:15:14 +00:00
2001-11-29 14:09:13 +00:00
report (RPT_INFO, "widget_add(%s, %s, %s)", id, type, in);
2000-04-03 22:13:57 +00:00
if (!s)
return -1;
if (!id)
return -1;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
list = s->widgets;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (0 == strcmp (id, "heartbeat")) {
2001-12-30 00:15:25 +00:00
s->heartbeat = HEARTBEAT_ON; /* was 1*/
2000-04-03 22:13:57 +00:00
return 0;
}
2001-12-30 00:15:25 +00:00
/* Make sure this screen doesn't already exist...*/
2000-04-03 22:13:57 +00:00
w = widget_find (s, id);
if (w) {
2001-12-30 00:15:25 +00:00
/* already exists*/
2000-04-03 22:13:57 +00:00
sock_send_string (sock, "huh? You already have a widget with that id#\n");
return 1;
}
2001-12-30 00:15:25 +00:00
/* Make sure the container, if any, is real*/
2000-04-03 22:13:57 +00:00
if (in) {
parent = widget_find (s, in);
if (!parent) {
2001-12-30 00:15:25 +00:00
/* no frame to use as parent*/
2000-04-03 22:13:57 +00:00
sock_send_string (sock, "huh? Frame doesn't exist\n");
return 3;
} else {
if (parent->type == WID_FRAME) {
list = parent->kids;
if (!list)
2001-11-29 14:09:13 +00:00
report (RPT_DEBUG, "widget_add: Parent has no kids");
2000-04-03 22:13:57 +00:00
} else {
2001-12-30 00:15:25 +00:00
/* no frame to use as parent*/
2000-04-03 22:13:57 +00:00
sock_send_string (sock, "huh? Not a frame\n");
return 4;
}
}
}
2001-12-30 00:15:25 +00:00
/* Make sure it's a valid widget type*/
2000-04-03 22:13:57 +00:00
for (i = 1; types[i]; i++) {
if (0 == strcmp (types[i], type)) {
valid = 1;
wid_type = i;
2001-12-30 00:15:25 +00:00
break; /* it's valid: skip out...*/
2000-04-03 22:13:57 +00:00
}
}
2000-04-03 22:13:57 +00:00
if (!valid) {
2001-12-30 00:15:25 +00:00
/* invalid widget type*/
2000-04-03 22:13:57 +00:00
sock_send_string (sock, "huh? Invalid widget type\n");
return 2;
}
1999-12-09 23:15:14 +00:00
2001-11-29 14:09:13 +00:00
debug (RPT_DEBUG, "widget_add: making widget");
2001-12-30 00:15:25 +00:00
/* Now, make it...*/
2000-04-03 22:13:57 +00:00
w = widget_create ();
if (!w) {
2001-11-29 14:09:13 +00:00
report (RPT_ERR, "widget_add: Error creating widget");
2000-04-03 22:13:57 +00:00
return -1;
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
w->id = strdup (id);
if (!w->id) {
2001-11-29 14:09:13 +00:00
report (RPT_ERR, "widget_add: Error allocating id");
2000-04-03 22:13:57 +00:00
return -1;
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
w->type = wid_type;
2001-12-30 00:15:25 +00:00
/* Set up the container's widget list...*/
2000-04-03 22:13:57 +00:00
if (w->type == WID_FRAME) {
if (!w->kids) {
w->kids = LL_new ();
if (!w->kids) {
2001-11-29 14:09:13 +00:00
report (RPT_ERR, "widget_add: error allocating kids for frame");
2000-04-03 22:13:57 +00:00
return -1;
}
}
}
2001-12-30 00:15:25 +00:00
/* TODO: Check for errors here?*/
2000-04-03 22:13:57 +00:00
LL_Push (list, (void *) w);
2000-04-03 22:13:57 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}
int
widget_remove (screen * s, char *id, int sock)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
widget *w;
LinkedList *list;
2001-11-29 14:09:13 +00:00
report (RPT_INFO, "widget_remove(%s)", id);
2000-04-03 22:13:57 +00:00
if (!s)
return -1;
if (!id)
return -1;
2000-04-03 22:13:57 +00:00
list = s->widgets;
2000-04-03 22:13:57 +00:00
if (0 == strcmp (id, "heartbeat")) {
2001-12-30 00:15:25 +00:00
s->heartbeat = HEARTBEAT_OFF; /* was 0*/
2000-04-03 22:13:57 +00:00
return 0;
}
2001-12-30 00:15:25 +00:00
/* Make sure this screen *does* exist...*/
2000-04-03 22:13:57 +00:00
w = widget_find (s, id);
if (!w) {
sock_send_string (sock, "huh? You don't have a widget with that id#\n");
2001-11-29 14:09:13 +00:00
report (RPT_ERR, "widget_remove: Error finding widget %s", id);
2000-04-03 22:13:57 +00:00
return 1;
}
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* TODO: Check for errors here?
* TODO: Make this work with frames...
* LL_Remove(list, (void *)w);
*/
1999-12-09 23:15:14 +00:00
2001-11-29 14:09:13 +00:00
2001-12-30 00:15:25 +00:00
/* TODO: Check for errors here?
* widget_destroy(w);
*/
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return widget_remover (list, w);
2001-12-30 00:15:25 +00:00
/* return 0;*/
1999-12-09 23:15:14 +00:00
}
int
widget_remover (LinkedList * list, widget * w)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
widget *foo, *bar;
int err;
1999-12-09 23:15:14 +00:00
2001-11-29 14:09:13 +00:00
debug (RPT_DEBUG, "widget_remover");
2000-04-03 22:13:57 +00:00
if (!list)
return 0;
if (!w)
return 0;
2001-12-30 00:15:25 +00:00
/* Search through the list...*/
2000-04-03 22:13:57 +00:00
LL_Rewind (list);
do {
2001-12-30 00:15:25 +00:00
/* Test each item*/
2000-04-03 22:13:57 +00:00
foo = LL_Get (list);
if (foo) {
2001-12-30 00:15:25 +00:00
/* Frames require recursion to search and/or destroy*/
2000-04-03 22:13:57 +00:00
if (foo->type == WID_FRAME) {
2001-12-30 00:15:25 +00:00
/* If removing a frame, kill all its kids, too...*/
2000-04-03 22:13:57 +00:00
if (foo == w) {
if (!foo->kids) {
2001-11-29 14:09:13 +00:00
debug (RPT_DEBUG, "widget_remover: frame has no kids");
2001-12-30 00:15:25 +00:00
} else /* Kill the kids...*/
2000-04-03 22:13:57 +00:00
{
LL_Rewind (foo->kids);
do {
bar = LL_Get (foo->kids);
err = widget_remover (foo->kids, bar);
} while (0 == LL_Next (foo->kids));
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* Then kill the parent...*/
2000-04-03 22:13:57 +00:00
LL_Remove (list, (void *) w);
widget_destroy (w);
}
2001-12-30 00:15:25 +00:00
} else /* Otherwise, search the frame recursively...*/
2000-04-03 22:13:57 +00:00
{
if (!foo->kids) {
2001-11-29 14:09:13 +00:00
debug (RPT_DEBUG, "widget_remover: frame has no kids");
2000-04-03 22:13:57 +00:00
} else
err = widget_remover (foo->kids, w);
2000-04-03 22:13:57 +00:00
}
2001-12-30 00:15:25 +00:00
} else /* If not a frame, remove it if it matches...*/
2000-04-03 22:13:57 +00:00
{
if (foo == w) {
LL_Remove (list, (void *) w);
widget_destroy (w);
}
}
}
} while (0 == LL_Next (list));
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}