2008-01-06 18:23:56 +01:00
|
|
|
/*
|
|
|
|
* graph.c - a graph widget
|
|
|
|
*
|
|
|
|
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
|
|
|
|
* Copyright © 2007-2008 Marco Candrian <mac@calmar.ws>
|
|
|
|
*
|
|
|
|
* This program 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 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-06-12 14:59:06 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
2008-01-06 18:23:56 +01:00
|
|
|
#include "widget.h"
|
2008-06-23 00:38:31 +02:00
|
|
|
#include "common/tokenize.h"
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-06-03 04:51:48 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
2008-06-12 16:10:12 +02:00
|
|
|
Bottom_Style = 0,
|
|
|
|
Top_Style,
|
2008-06-03 04:51:48 +02:00
|
|
|
Line_Style
|
2008-06-25 17:47:51 +02:00
|
|
|
} plot_style_t;
|
2008-06-03 04:51:48 +02:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** The plot data structure. */
|
2008-10-21 12:04:19 +02:00
|
|
|
typedef struct
|
2008-06-11 17:27:26 +02:00
|
|
|
{
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Grapht title of the plot sections */
|
2008-06-11 17:27:26 +02:00
|
|
|
char *title;
|
|
|
|
/** Represents a full graph */
|
|
|
|
float max_value;
|
|
|
|
/** Scale the graph */
|
|
|
|
bool scale;
|
2008-01-25 21:39:08 +01:00
|
|
|
|
|
|
|
/* markers... */
|
2008-06-11 17:27:26 +02:00
|
|
|
/** Index of current (new) value */
|
|
|
|
int index;
|
|
|
|
/** Index of the actual maximum value */
|
|
|
|
int max_index;
|
|
|
|
/** Pointer to current maximum value itself */
|
|
|
|
float current_max;
|
|
|
|
/** Draw style of according index */
|
2008-06-25 17:47:51 +02:00
|
|
|
plot_style_t draw_style;
|
2008-06-11 17:27:26 +02:00
|
|
|
/** Keeps the calculated values (line-length); */
|
|
|
|
int *lines;
|
|
|
|
/** Actual values */
|
|
|
|
float *values;
|
|
|
|
/** Color of them */
|
2009-04-17 18:58:27 +02:00
|
|
|
color_t color_start;
|
2008-06-11 17:27:26 +02:00
|
|
|
/** Color at middle of graph */
|
2009-04-17 18:58:27 +02:00
|
|
|
color_t pcolor_center;
|
2008-06-11 17:27:26 +02:00
|
|
|
/** Color at end of graph */
|
2009-04-17 18:58:27 +02:00
|
|
|
color_t pcolor_end;
|
2008-06-11 17:27:26 +02:00
|
|
|
/** Create a vertical color gradient */
|
|
|
|
bool vertical_gradient;
|
2008-10-21 12:04:19 +02:00
|
|
|
} plot_t;
|
2008-06-11 17:27:26 +02:00
|
|
|
|
2008-06-14 22:55:17 +02:00
|
|
|
static void
|
2008-10-21 12:04:19 +02:00
|
|
|
plot_delete(plot_t *g)
|
2008-06-14 22:55:17 +02:00
|
|
|
{
|
2008-10-21 12:04:19 +02:00
|
|
|
p_delete(&g->title);
|
|
|
|
p_delete(&g->lines);
|
|
|
|
p_delete(&g->values);
|
2008-06-14 22:55:17 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 12:04:19 +02:00
|
|
|
DO_ARRAY(plot_t, plot, plot_delete)
|
2008-01-25 21:39:08 +01:00
|
|
|
|
2008-06-26 17:22:01 +02:00
|
|
|
/** The private graph data structure */
|
2008-06-11 17:27:26 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/** Width of the widget */
|
|
|
|
int width;
|
|
|
|
/** Height of graph (0.0-1.0; 1.0 = height of bar) */
|
|
|
|
float height;
|
|
|
|
/** Height of the innerbox in pixels */
|
|
|
|
int box_height;
|
|
|
|
/** Size of lines-array (also innerbox-length) */
|
|
|
|
int size;
|
|
|
|
/** Background color */
|
2009-04-17 18:58:27 +02:00
|
|
|
color_t bg;
|
2008-06-11 17:27:26 +02:00
|
|
|
/** Border color */
|
2009-04-17 18:58:27 +02:00
|
|
|
color_t border_color;
|
2008-06-11 17:27:26 +02:00
|
|
|
/** Grow: Left or Right */
|
|
|
|
position_t grow;
|
|
|
|
/** Preparation/tmp array for draw_graph(); */
|
|
|
|
int *draw_from;
|
|
|
|
/** Preparation/tmp array for draw_graph(); */
|
|
|
|
int *draw_to;
|
|
|
|
/** Graph list */
|
2008-10-21 12:04:19 +02:00
|
|
|
plot_array_t plots;
|
2008-06-11 16:44:18 +02:00
|
|
|
} graph_data_t;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Add a plot to a graph.
|
|
|
|
* \param d The graph private data.
|
|
|
|
* \param title The plot title.
|
|
|
|
* \return A new plot.
|
|
|
|
*/
|
|
|
|
static plot_t *
|
|
|
|
graph_plot_add(graph_data_t *d, const char *title)
|
2008-05-31 14:38:27 +02:00
|
|
|
{
|
2008-10-21 12:04:19 +02:00
|
|
|
plot_t plot;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-10-21 15:07:41 +02:00
|
|
|
p_clear(&plot, 1);
|
|
|
|
|
2008-10-21 12:04:19 +02:00
|
|
|
plot.title = a_strdup(title);
|
|
|
|
plot.values = p_new(float, d->size);
|
|
|
|
plot.lines = p_new(int, d->size);
|
|
|
|
plot.max_value = 100.0;
|
|
|
|
plot.vertical_gradient = true;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2009-04-17 18:58:27 +02:00
|
|
|
xcolor_to_color(&globalconf.colors.fg, &plot.color_start);
|
|
|
|
|
2008-10-21 12:04:19 +02:00
|
|
|
plot_array_append(&d->plots, plot);
|
2008-10-22 10:25:07 +02:00
|
|
|
|
2008-10-21 12:04:19 +02:00
|
|
|
return &d->plots.tab[d->plots.len - 1];
|
2008-05-31 14:38:27 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 10:25:07 +02:00
|
|
|
/** Get the plot, and create one if it does not exist.
|
|
|
|
* \param d The graph private data.
|
|
|
|
* \param title The plot title.
|
|
|
|
* \return A maybe new plot.
|
|
|
|
*/
|
|
|
|
static plot_t *
|
|
|
|
graph_plot_get(graph_data_t *d, const char *title)
|
|
|
|
{
|
|
|
|
plot_t *plot;
|
|
|
|
|
|
|
|
/* check if this section is defined already */
|
|
|
|
for(int j = 0; j < d->plots.len; j++)
|
|
|
|
{
|
|
|
|
plot = &d->plots.tab[j];
|
|
|
|
if(!a_strcmp(title, plot->title))
|
|
|
|
return plot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no plot found -> create one */
|
|
|
|
return graph_plot_add(d, title);
|
|
|
|
}
|
|
|
|
|
2008-10-22 17:32:17 +02:00
|
|
|
static area_t
|
2009-04-17 16:14:09 +02:00
|
|
|
graph_geometry(widget_t *widget, screen_t *screen, int height, int width)
|
2008-10-22 17:32:17 +02:00
|
|
|
{
|
|
|
|
area_t geometry;
|
|
|
|
graph_data_t *d = widget->data;
|
|
|
|
|
|
|
|
geometry.height = height;
|
|
|
|
geometry.width = d->width;
|
|
|
|
|
|
|
|
return geometry;
|
|
|
|
}
|
|
|
|
|
2008-06-26 17:22:01 +02:00
|
|
|
/** Draw a graph widget.
|
|
|
|
* \param ctx The draw context.
|
|
|
|
* \param w The widget node we are called from.
|
|
|
|
* \param offset The offset to draw at.
|
|
|
|
* \param used The already used width.
|
|
|
|
* \param p A pointer to the object we're drawing onto.
|
2008-07-29 16:51:21 +02:00
|
|
|
* \return The widget width.
|
2008-06-26 17:22:01 +02:00
|
|
|
*/
|
2008-10-22 17:32:17 +02:00
|
|
|
static void
|
|
|
|
graph_draw(widget_t *widget, draw_context_t *ctx,
|
2009-04-17 16:14:09 +02:00
|
|
|
area_t geometry, wibox_t *p)
|
2008-01-06 18:23:56 +01:00
|
|
|
{
|
2008-06-12 16:14:13 +02:00
|
|
|
int margin_top, y;
|
2008-10-22 17:32:17 +02:00
|
|
|
graph_data_t *d = widget->data;
|
2008-07-02 12:25:52 +02:00
|
|
|
area_t rectangle;
|
|
|
|
vector_t color_gradient;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-10-21 12:04:19 +02:00
|
|
|
if(!d->plots.len)
|
2008-10-22 17:32:17 +02:00
|
|
|
return;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/* box = the plot inside the rectangle */
|
2008-06-12 14:59:06 +02:00
|
|
|
if(!d->box_height)
|
|
|
|
d->box_height = round(ctx->height * d->height) - 2;
|
2008-01-09 09:59:42 +01:00
|
|
|
|
2008-10-22 17:32:17 +02:00
|
|
|
margin_top = round((ctx->height - (d->box_height + 2)) / 2) + geometry.y;
|
2008-03-12 15:19:50 +01:00
|
|
|
|
2008-04-18 00:58:42 +02:00
|
|
|
/* draw background */
|
2008-10-22 17:32:17 +02:00
|
|
|
rectangle.x = geometry.x + 1;
|
2008-04-18 00:58:42 +02:00
|
|
|
rectangle.y = margin_top + 1;
|
2008-03-09 23:49:03 +01:00
|
|
|
rectangle.width = d->size;
|
2008-04-12 08:56:14 +02:00
|
|
|
rectangle.height = d->box_height;
|
2009-04-17 18:58:27 +02:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, true, &d->bg);
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/* for plot drawing */
|
2008-03-09 23:49:03 +01:00
|
|
|
rectangle.y = margin_top + d->box_height + 1; /* bottom left corner as starting point */
|
|
|
|
rectangle.width = d->size; /* rectangle.height is not used */
|
|
|
|
|
2008-01-31 19:40:47 +01:00
|
|
|
draw_graph_setup(ctx); /* setup some drawing options */
|
2008-01-25 21:39:08 +01:00
|
|
|
|
2008-04-14 03:38:59 +02:00
|
|
|
/* gradient begin either left or on the right of the rectangle */
|
|
|
|
if(d->grow == Right)
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x = rectangle.x + rectangle.width;
|
2008-04-14 03:38:59 +02:00
|
|
|
else
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x = rectangle.x;
|
2008-04-14 03:38:59 +02:00
|
|
|
|
2008-10-21 12:04:19 +02:00
|
|
|
for(int i = 0; i < d->plots.len; i++)
|
|
|
|
{
|
|
|
|
plot_t *plot = &d->plots.tab[i];
|
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
switch(plot->draw_style)
|
2008-03-09 23:49:03 +01:00
|
|
|
{
|
2008-06-12 16:14:13 +02:00
|
|
|
case Top_Style:
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.y = rectangle.y - rectangle.height;
|
2008-06-25 17:47:51 +02:00
|
|
|
if(plot->vertical_gradient)
|
2008-06-12 16:14:13 +02:00
|
|
|
{
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x_offset = 0;
|
|
|
|
color_gradient.y_offset = rectangle.height;
|
2008-06-12 16:14:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.y_offset = 0;
|
2008-06-25 17:47:51 +02:00
|
|
|
|
2008-06-12 16:14:13 +02:00
|
|
|
if(d->grow == Right)
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x_offset = - rectangle.width;
|
2008-06-12 16:14:13 +02:00
|
|
|
else
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x_offset = rectangle.width;
|
2008-06-12 16:14:13 +02:00
|
|
|
}
|
2008-06-25 17:47:51 +02:00
|
|
|
|
2008-06-12 16:14:13 +02:00
|
|
|
for(y = 0; y < d->size; y++)
|
|
|
|
{
|
|
|
|
/* reverse values (because drawing from top) */
|
|
|
|
d->draw_from[y] = d->box_height; /* i.e. no smaller value -> from top of box */
|
2008-06-25 17:47:51 +02:00
|
|
|
d->draw_to[y] = d->box_height - plot->lines[y]; /* i.e. on full plot -> 0 = bottom */
|
2008-06-12 16:14:13 +02:00
|
|
|
}
|
2008-07-02 12:25:52 +02:00
|
|
|
draw_graph(ctx, rectangle , d->draw_from, d->draw_to, plot->index, d->grow, color_gradient,
|
2009-04-17 18:58:27 +02:00
|
|
|
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
|
2008-06-12 16:14:13 +02:00
|
|
|
break;
|
|
|
|
case Bottom_Style:
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.y = rectangle.y;
|
2008-06-25 17:47:51 +02:00
|
|
|
if(plot->vertical_gradient)
|
2008-06-12 16:14:13 +02:00
|
|
|
{
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x_offset = 0;
|
|
|
|
color_gradient.y_offset = - rectangle.height;
|
2008-06-12 16:14:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.y_offset = 0;
|
2008-06-25 17:47:51 +02:00
|
|
|
|
2008-06-12 16:14:13 +02:00
|
|
|
if(d->grow == Right)
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x_offset = - rectangle.width;
|
2008-06-12 16:14:13 +02:00
|
|
|
else
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x_offset = rectangle.width;
|
2008-06-12 16:14:13 +02:00
|
|
|
}
|
2008-06-25 17:47:51 +02:00
|
|
|
|
2008-06-12 16:14:13 +02:00
|
|
|
p_clear(d->draw_from, d->size);
|
2008-07-02 12:25:52 +02:00
|
|
|
draw_graph(ctx, rectangle, d->draw_from, plot->lines, plot->index, d->grow, color_gradient,
|
2009-04-17 18:58:27 +02:00
|
|
|
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
|
2008-06-12 16:14:13 +02:00
|
|
|
break;
|
|
|
|
case Line_Style:
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.y = rectangle.y;
|
2008-06-25 17:47:51 +02:00
|
|
|
if(plot->vertical_gradient)
|
2008-06-12 16:14:13 +02:00
|
|
|
{
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x_offset = 0;
|
|
|
|
color_gradient.y_offset = -rectangle.height;
|
2008-06-12 16:14:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.y_offset = 0;
|
2008-06-12 16:14:13 +02:00
|
|
|
if(d->grow == Right)
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x_offset = - rectangle.width;
|
2008-06-12 16:14:13 +02:00
|
|
|
else
|
2008-07-02 12:25:52 +02:00
|
|
|
color_gradient.x_offset = rectangle.width;
|
2008-06-12 16:14:13 +02:00
|
|
|
}
|
2008-06-25 17:47:51 +02:00
|
|
|
|
2008-07-02 12:25:52 +02:00
|
|
|
draw_graph_line(ctx, rectangle, plot->lines, plot->index, d->grow, color_gradient,
|
2009-04-17 18:58:27 +02:00
|
|
|
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
|
2008-06-12 16:14:13 +02:00
|
|
|
break;
|
2008-06-03 04:51:48 +02:00
|
|
|
}
|
2008-10-21 12:04:19 +02:00
|
|
|
}
|
2008-01-25 21:39:08 +01:00
|
|
|
|
2008-04-18 00:58:42 +02:00
|
|
|
/* draw border (after line-drawing, what paints 0-values to the border) */
|
2008-10-22 17:32:17 +02:00
|
|
|
rectangle.x = geometry.x;
|
2008-04-18 00:58:42 +02:00
|
|
|
rectangle.y = margin_top;
|
|
|
|
rectangle.width = d->size + 2;
|
|
|
|
rectangle.height = d->box_height + 2;
|
2009-04-17 18:58:27 +02:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, false, &d->border_color);
|
2008-01-06 18:23:56 +01:00
|
|
|
}
|
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Set various plot graph properties.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
2008-06-26 17:13:02 +02:00
|
|
|
* \luastack
|
2008-06-25 17:47:51 +02:00
|
|
|
* \lvalue A widget.
|
|
|
|
* \lparam A plot name.
|
|
|
|
* \lparam A table with various properties set.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_graph_plot_properties_set(lua_State *L)
|
|
|
|
{
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_t *widget = luaL_checkudata(L, 1, "widget");
|
|
|
|
graph_data_t *d = widget->data;
|
2008-06-25 17:47:51 +02:00
|
|
|
float max_value;
|
|
|
|
const char *title, *buf;
|
|
|
|
size_t len;
|
2008-10-21 12:04:19 +02:00
|
|
|
plot_t *plot = NULL;
|
2009-04-17 18:58:27 +02:00
|
|
|
color_init_cookie_t reqs[3];
|
2009-01-18 11:03:13 +01:00
|
|
|
int i, reqs_nbr = -1;
|
2008-06-25 17:47:51 +02:00
|
|
|
|
|
|
|
title = luaL_checkstring(L, 2);
|
|
|
|
luaA_checktable(L, 3);
|
|
|
|
|
2008-10-22 10:25:07 +02:00
|
|
|
plot = graph_plot_get(d, title);
|
2008-06-25 17:47:51 +02:00
|
|
|
|
2008-07-10 15:30:16 +02:00
|
|
|
if((buf = luaA_getopt_lstring(L, 3, "fg", NULL, &len)))
|
2009-04-17 18:58:27 +02:00
|
|
|
reqs[++reqs_nbr] = color_init_unchecked(&plot->color_start, buf, len);
|
2008-06-28 13:30:17 +02:00
|
|
|
|
2008-07-10 15:30:16 +02:00
|
|
|
if((buf = luaA_getopt_lstring(L, 3, "fg_center", NULL, &len)))
|
2009-04-17 18:58:27 +02:00
|
|
|
reqs[++reqs_nbr] = color_init_unchecked(&plot->pcolor_center, buf, len);
|
2008-06-28 13:30:17 +02:00
|
|
|
|
2008-07-10 15:30:16 +02:00
|
|
|
if((buf = luaA_getopt_lstring(L, 3, "fg_end", NULL, &len)))
|
2009-04-17 18:58:27 +02:00
|
|
|
reqs[++reqs_nbr] = color_init_unchecked(&plot->pcolor_end, buf, len);
|
2008-06-25 17:47:51 +02:00
|
|
|
|
|
|
|
plot->vertical_gradient = luaA_getopt_boolean(L, 3, "vertical_gradient", plot->vertical_gradient);
|
|
|
|
plot->scale = luaA_getopt_boolean(L, 3, "scale", plot->scale);
|
|
|
|
|
|
|
|
max_value = luaA_getopt_number(L, 3, "max_value", plot->max_value);
|
|
|
|
if(max_value != plot->max_value)
|
|
|
|
plot->max_value = plot->current_max = max_value;
|
|
|
|
|
|
|
|
if((buf = luaA_getopt_lstring(L, 3, "style", NULL, &len)))
|
|
|
|
switch (a_tokenize(buf, len))
|
2008-06-23 00:38:31 +02:00
|
|
|
{
|
|
|
|
case A_TK_BOTTOM:
|
2008-06-25 17:47:51 +02:00
|
|
|
plot->draw_style = Bottom_Style;
|
2008-06-23 00:38:31 +02:00
|
|
|
break;
|
|
|
|
case A_TK_LINE:
|
2008-06-25 17:47:51 +02:00
|
|
|
plot->draw_style = Line_Style;
|
2008-06-23 00:38:31 +02:00
|
|
|
break;
|
|
|
|
case A_TK_TOP:
|
2008-06-25 17:47:51 +02:00
|
|
|
plot->draw_style = Top_Style;
|
2008-06-23 00:38:31 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-06-25 17:47:51 +02:00
|
|
|
|
2008-08-12 14:53:57 +02:00
|
|
|
for(i = 0; i <= reqs_nbr; i++)
|
2009-04-17 18:58:27 +02:00
|
|
|
color_init_reply(reqs[i]);
|
2008-08-12 14:53:57 +02:00
|
|
|
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_invalidate_bywidget(widget);
|
2008-06-25 17:47:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add data to a plot.
|
|
|
|
* \param l The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
2008-06-26 17:13:02 +02:00
|
|
|
* \luastack
|
2008-06-25 17:47:51 +02:00
|
|
|
* \lvalue A widget.
|
|
|
|
* \lparam A plot name.
|
|
|
|
* \lparam A data value.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_graph_plot_data_add(lua_State *L)
|
|
|
|
{
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_t *widget = luaL_checkudata(L, 1, "widget");
|
|
|
|
graph_data_t *d = widget->data;
|
2008-10-21 12:04:19 +02:00
|
|
|
plot_t *plot = NULL;
|
2008-06-25 17:47:51 +02:00
|
|
|
const char *title = luaL_checkstring(L, 2);
|
|
|
|
float value;
|
|
|
|
int i;
|
|
|
|
|
2009-03-02 17:42:55 +01:00
|
|
|
if(!d->size)
|
|
|
|
return 0;
|
|
|
|
|
2008-10-22 10:25:07 +02:00
|
|
|
plot = graph_plot_get(d, title);
|
2008-06-25 17:47:51 +02:00
|
|
|
|
|
|
|
/* assign incoming value */
|
|
|
|
value = MAX(luaL_checknumber(L, 3), 0);
|
|
|
|
|
|
|
|
if(++plot->index >= d->size) /* cycle inside the array */
|
|
|
|
plot->index = 0;
|
|
|
|
|
|
|
|
if(plot->scale) /* scale option is true */
|
|
|
|
{
|
|
|
|
plot->values[plot->index] = value;
|
|
|
|
|
|
|
|
if(value > plot->current_max) /* a new maximum value found */
|
|
|
|
{
|
|
|
|
plot->max_index = plot->index;
|
|
|
|
plot->current_max = value;
|
|
|
|
|
|
|
|
/* recalculate */
|
|
|
|
for (i = 0; i < d->size; i++)
|
|
|
|
plot->lines[i] = round(plot->values[i] * d->box_height / plot->current_max);
|
|
|
|
}
|
|
|
|
/* old max_index reached + current_max > normal, re-check/generate */
|
|
|
|
else if(plot->max_index == plot->index
|
|
|
|
&& plot->current_max > plot->max_value)
|
|
|
|
{
|
|
|
|
/* find the new max */
|
|
|
|
for(i = 0; i < d->size; i++)
|
|
|
|
if(plot->values[i] > plot->values[plot->max_index])
|
|
|
|
plot->max_index = i;
|
|
|
|
|
|
|
|
plot->current_max = MAX(plot->values[plot->max_index], plot->max_value);
|
|
|
|
|
|
|
|
/* recalculate */
|
|
|
|
for(i = 0; i < d->size; i++)
|
|
|
|
plot->lines[i] = round(plot->values[i] * d->box_height / plot->current_max);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
plot->lines[plot->index] = round(value * d->box_height / plot->current_max);
|
|
|
|
}
|
|
|
|
else /* scale option is false - limit to d->box_height */
|
|
|
|
{
|
|
|
|
if(value < plot->max_value)
|
|
|
|
plot->lines[plot->index] = round(value * d->box_height / plot->max_value);
|
|
|
|
else
|
|
|
|
plot->lines[plot->index] = d->box_height;
|
|
|
|
}
|
|
|
|
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_invalidate_bywidget(widget);
|
2008-06-25 17:47:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-28 16:03:38 +02:00
|
|
|
/** Graph widget.
|
2008-06-25 17:47:51 +02:00
|
|
|
* \param L The Lua VM state.
|
2008-07-01 15:27:41 +02:00
|
|
|
* \param token The key token.
|
2008-06-25 17:47:51 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
2008-08-29 13:48:47 +02:00
|
|
|
* \luastack
|
|
|
|
* \lfield plot_properties_set A function to set plot properties.
|
2008-10-22 10:25:07 +02:00
|
|
|
* \lfield plot_data_add A function to add data to a plot.
|
2008-08-29 13:48:47 +02:00
|
|
|
* \lfield height Graph height.
|
|
|
|
* \lfield widget Graph width.
|
|
|
|
* \lfield bg Background color.
|
|
|
|
* \lfield grow Direction to grow: left or right.
|
2008-06-25 17:47:51 +02:00
|
|
|
*/
|
|
|
|
static int
|
2008-07-01 15:27:41 +02:00
|
|
|
luaA_graph_index(lua_State *L, awesome_token_t token)
|
2008-06-25 17:47:51 +02:00
|
|
|
{
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_t *widget = luaL_checkudata(L, 1, "widget");
|
|
|
|
graph_data_t *d = widget->data;
|
2008-06-25 17:47:51 +02:00
|
|
|
|
2008-07-01 15:27:41 +02:00
|
|
|
switch(token)
|
2008-06-25 17:47:51 +02:00
|
|
|
{
|
|
|
|
case A_TK_PLOT_PROPERTIES_SET:
|
|
|
|
lua_pushcfunction(L, luaA_graph_plot_properties_set);
|
2008-06-28 13:02:19 +02:00
|
|
|
break;
|
2008-06-25 17:47:51 +02:00
|
|
|
case A_TK_PLOT_DATA_ADD:
|
|
|
|
lua_pushcfunction(L, luaA_graph_plot_data_add);
|
2008-06-28 13:02:19 +02:00
|
|
|
break;
|
|
|
|
case A_TK_HEIGHT:
|
|
|
|
lua_pushnumber(L, d->height);
|
|
|
|
break;
|
|
|
|
case A_TK_WIDTH:
|
|
|
|
lua_pushnumber(L, d->width);
|
2008-06-28 13:31:33 +02:00
|
|
|
break;
|
|
|
|
case A_TK_BORDER_COLOR:
|
2009-04-17 18:58:27 +02:00
|
|
|
luaA_pushcolor(L, &d->border_color);
|
2008-06-28 13:31:33 +02:00
|
|
|
break;
|
|
|
|
case A_TK_BG:
|
2009-04-17 18:58:27 +02:00
|
|
|
luaA_pushcolor(L, &d->bg);
|
2008-06-28 13:31:33 +02:00
|
|
|
break;
|
2008-06-28 13:02:19 +02:00
|
|
|
case A_TK_GROW:
|
|
|
|
switch(d->grow)
|
|
|
|
{
|
|
|
|
case Left:
|
|
|
|
lua_pushliteral(L, "left");
|
|
|
|
break;
|
|
|
|
case Right:
|
|
|
|
lua_pushliteral(L, "right");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2008-06-23 00:38:31 +02:00
|
|
|
default:
|
2008-06-25 17:47:51 +02:00
|
|
|
return 0;
|
2008-06-23 00:38:31 +02:00
|
|
|
}
|
2008-06-28 13:02:19 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Newindex function for graph widget.
|
|
|
|
* \param L The Lua VM state.
|
2008-07-01 15:27:41 +02:00
|
|
|
* \param token The key token.
|
2008-06-28 13:02:19 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
2008-07-01 15:27:41 +02:00
|
|
|
luaA_graph_newindex(lua_State *L, awesome_token_t token)
|
2008-06-28 13:02:19 +02:00
|
|
|
{
|
|
|
|
size_t len;
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_t *widget = luaL_checkudata(L, 1, "widget");
|
|
|
|
graph_data_t *d = widget->data;
|
2008-07-01 15:27:41 +02:00
|
|
|
const char *buf;
|
2008-06-28 13:02:19 +02:00
|
|
|
int width;
|
|
|
|
position_t pos;
|
2009-04-17 18:58:27 +02:00
|
|
|
color_t color;
|
2008-06-28 13:02:19 +02:00
|
|
|
|
2008-07-01 15:27:41 +02:00
|
|
|
switch(token)
|
2008-06-28 13:02:19 +02:00
|
|
|
{
|
|
|
|
case A_TK_HEIGHT:
|
|
|
|
d->height = luaL_checknumber(L, 3);
|
|
|
|
break;
|
|
|
|
case A_TK_WIDTH:
|
|
|
|
width = luaL_checknumber(L, 3);
|
2009-03-02 14:20:08 +01:00
|
|
|
if(width >= 2 && width != d->width)
|
2008-06-28 13:02:19 +02:00
|
|
|
{
|
|
|
|
d->width = width;
|
|
|
|
d->size = d->width - 2;
|
2009-04-15 04:36:37 +02:00
|
|
|
p_realloc(&d->draw_from, d->size);
|
|
|
|
p_realloc(&d->draw_to, d->size);
|
2008-10-21 12:04:19 +02:00
|
|
|
for(int i = 0; i < d->plots.len; i++)
|
2008-06-28 13:02:19 +02:00
|
|
|
{
|
2008-10-21 12:04:19 +02:00
|
|
|
plot_t *plot = &d->plots.tab[i];
|
2008-06-28 13:02:19 +02:00
|
|
|
p_realloc(&plot->values, d->size);
|
|
|
|
p_realloc(&plot->lines, d->size);
|
|
|
|
p_clear(plot->values, d->size);
|
|
|
|
p_clear(plot->lines, d->size);
|
|
|
|
plot->index = 0;
|
|
|
|
plot->current_max = 0;
|
|
|
|
plot->max_index = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case A_TK_BG:
|
2008-07-10 15:30:16 +02:00
|
|
|
if((buf = luaL_checklstring(L, 3, &len)))
|
|
|
|
{
|
2009-04-17 18:58:27 +02:00
|
|
|
if(color_init_reply(color_init_unchecked(&color, buf, len)))
|
2008-07-10 15:30:16 +02:00
|
|
|
d->bg = color;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-28 13:02:19 +02:00
|
|
|
break;
|
|
|
|
case A_TK_BORDER_COLOR:
|
2008-07-10 15:30:16 +02:00
|
|
|
if((buf = luaL_checklstring(L, 3, &len)))
|
|
|
|
{
|
2009-04-17 18:58:27 +02:00
|
|
|
if(color_init_reply(color_init_unchecked(&color, buf, len)))
|
2008-07-10 15:30:16 +02:00
|
|
|
d->border_color = color;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-28 13:02:19 +02:00
|
|
|
break;
|
|
|
|
case A_TK_GROW:
|
|
|
|
buf = luaL_checklstring(L, 3, &len);
|
|
|
|
switch((pos = position_fromstr(buf, len)))
|
|
|
|
{
|
|
|
|
case Left:
|
|
|
|
case Right:
|
|
|
|
d->grow = pos;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-09 15:12:17 +02:00
|
|
|
widget_invalidate_bywidget(widget);
|
2008-06-28 13:02:19 +02:00
|
|
|
|
|
|
|
return 0;
|
2008-01-06 18:23:56 +01:00
|
|
|
}
|
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Destroy definitively a graph widget.
|
|
|
|
* \param widget Who slay.
|
|
|
|
*/
|
2008-06-14 22:55:17 +02:00
|
|
|
static void
|
|
|
|
graph_destructor(widget_t *widget)
|
|
|
|
{
|
|
|
|
graph_data_t *d = widget->data;
|
|
|
|
|
2008-10-21 12:04:19 +02:00
|
|
|
plot_array_wipe(&d->plots);
|
2008-06-14 22:55:17 +02:00
|
|
|
p_delete(&d->draw_from);
|
|
|
|
p_delete(&d->draw_to);
|
|
|
|
p_delete(&d);
|
|
|
|
}
|
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** Create a brand new graph.
|
2008-11-12 14:55:30 +01:00
|
|
|
* \param w The widget to initialize.
|
|
|
|
* \return The same widget.
|
2008-06-25 17:47:51 +02:00
|
|
|
*/
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *
|
2008-11-12 14:55:30 +01:00
|
|
|
widget_graph(widget_t *w)
|
2008-01-06 18:23:56 +01:00
|
|
|
{
|
|
|
|
w->draw = graph_draw;
|
2008-06-25 17:47:51 +02:00
|
|
|
w->index = luaA_graph_index;
|
2008-06-28 13:02:19 +02:00
|
|
|
w->newindex = luaA_graph_newindex;
|
2008-06-14 22:55:17 +02:00
|
|
|
w->destructor = graph_destructor;
|
2008-10-22 17:32:17 +02:00
|
|
|
w->geometry = graph_geometry;
|
2008-11-12 14:55:30 +01:00
|
|
|
|
|
|
|
graph_data_t *d = w->data = p_new(graph_data_t, 1);
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-05-31 14:38:27 +02:00
|
|
|
d->width = 80;
|
|
|
|
d->height = 0.80;
|
2008-03-31 14:31:43 +02:00
|
|
|
d->size = d->width - 2;
|
2008-05-31 14:38:27 +02:00
|
|
|
d->grow = Left;
|
2008-01-25 21:39:08 +01:00
|
|
|
d->draw_from = p_new(int, d->size);
|
|
|
|
d->draw_to = p_new(int, d->size);
|
2008-01-09 09:59:42 +01:00
|
|
|
|
2009-04-17 18:58:27 +02:00
|
|
|
xcolor_to_color(&globalconf.colors.bg, &d->bg);
|
|
|
|
xcolor_to_color(&globalconf.colors.fg, &d->border_color);
|
2008-01-25 21:39:08 +01:00
|
|
|
|
2008-01-06 18:23:56 +01:00
|
|
|
return w;
|
|
|
|
}
|
2008-06-26 17:22:01 +02:00
|
|
|
|
2008-06-26 23:32:33 +02:00
|
|
|
/* This is used for building documentation. */
|
|
|
|
static const struct luaL_reg awesome_graph_meta[] __attribute__ ((unused)) =
|
|
|
|
{
|
|
|
|
{ "plot_properties_set", luaA_graph_plot_properties_set },
|
|
|
|
{ "plot_data_add", luaA_graph_plot_data_add },
|
|
|
|
};
|
|
|
|
|
2008-01-06 18:23:56 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|