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-03-19 11:58:43 +01:00
|
|
|
#include "common/draw.h"
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-05-24 08:59:27 +02:00
|
|
|
extern awesome_t globalconf;
|
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
|
|
|
typedef struct plot_t plot_t;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
/** The plot data structure. */
|
|
|
|
struct plot_t
|
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 */
|
|
|
|
xcolor_t color_start;
|
|
|
|
/** Color at middle of graph */
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +02:00
|
|
|
xcolor_t pcolor_center;
|
2008-06-11 17:27:26 +02:00
|
|
|
/** Color at end of graph */
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +02:00
|
|
|
xcolor_t pcolor_end;
|
2008-06-11 17:27:26 +02:00
|
|
|
/** Create a vertical color gradient */
|
|
|
|
bool vertical_gradient;
|
|
|
|
/** Next and previous graph */
|
2008-06-25 17:47:51 +02:00
|
|
|
plot_t *next, *prev;
|
2008-06-11 17:27:26 +02:00
|
|
|
};
|
|
|
|
|
2008-06-14 22:55:17 +02:00
|
|
|
static void
|
2008-06-25 17:47:51 +02:00
|
|
|
plot_delete(plot_t **g)
|
2008-06-14 22:55:17 +02:00
|
|
|
{
|
|
|
|
p_delete(&(*g)->title);
|
|
|
|
p_delete(&(*g)->lines);
|
|
|
|
p_delete(&(*g)->values);
|
|
|
|
p_delete(g);
|
|
|
|
}
|
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
DO_SLIST(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 */
|
|
|
|
xcolor_t bg;
|
|
|
|
/** Border color */
|
2008-06-28 13:02:19 +02:00
|
|
|
xcolor_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-06-25 17:47:51 +02:00
|
|
|
plot_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-06-25 17:47:51 +02:00
|
|
|
plot_t *plot = p_new(plot_t, 1);
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-06-25 17:47:51 +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;
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +02:00
|
|
|
plot->color_start = globalconf.colors.fg;
|
2008-06-25 17:47:51 +02:00
|
|
|
plot->vertical_gradient = true;
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
plot_list_append(&d->plots, plot);
|
2008-05-31 14:38:27 +02:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
return plot;
|
2008-05-31 14:38:27 +02:00
|
|
|
}
|
|
|
|
|
2008-06-26 17:22:01 +02:00
|
|
|
/** Draw a graph widget.
|
|
|
|
* \param ctx The draw context.
|
|
|
|
* \param screen The screen number.
|
|
|
|
* \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-01-06 18:23:56 +01:00
|
|
|
static int
|
2008-06-02 12:18:17 +02:00
|
|
|
graph_draw(draw_context_t *ctx,
|
|
|
|
int screen __attribute__ ((unused)),
|
|
|
|
widget_node_t *w,
|
2008-06-03 16:08:33 +02:00
|
|
|
int offset,
|
2008-06-02 12:18:17 +02:00
|
|
|
int used __attribute__ ((unused)),
|
|
|
|
void *p __attribute__ ((unused)))
|
2008-01-06 18:23:56 +01:00
|
|
|
{
|
2008-06-12 16:14:13 +02:00
|
|
|
int margin_top, y;
|
2008-06-11 16:44:18 +02:00
|
|
|
graph_data_t *d = w->widget->data;
|
2008-07-02 12:25:52 +02:00
|
|
|
area_t rectangle;
|
|
|
|
vector_t color_gradient;
|
2008-06-25 17:47:51 +02:00
|
|
|
plot_t *plot;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
if(!d->plots)
|
2008-01-22 14:56:03 +01:00
|
|
|
return 0;
|
|
|
|
|
2008-06-03 16:08:33 +02:00
|
|
|
w->area.x = widget_calculate_offset(ctx->width,
|
2008-05-20 15:39:47 +02:00
|
|
|
d->width, offset,
|
|
|
|
w->widget->align);
|
|
|
|
w->area.y = 0;
|
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-06-12 14:59:06 +02:00
|
|
|
margin_top = round((ctx->height - (d->box_height + 2)) / 2) + w->area.y;
|
2008-03-12 15:19:50 +01:00
|
|
|
|
2008-04-18 00:58:42 +02:00
|
|
|
/* draw background */
|
2008-05-20 15:39:47 +02:00
|
|
|
rectangle.x = w->area.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;
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +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-06-25 17:47:51 +02:00
|
|
|
for(plot = d->plots; plot; plot = plot->next)
|
|
|
|
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,
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +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,
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +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,
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +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-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-05-20 15:39:47 +02:00
|
|
|
rectangle.x = w->area.x;
|
2008-04-18 00:58:42 +02:00
|
|
|
rectangle.y = margin_top;
|
|
|
|
rectangle.width = d->size + 2;
|
|
|
|
rectangle.height = d->box_height + 2;
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +02:00
|
|
|
draw_rectangle(ctx, rectangle, 1.0, false, &d->border_color);
|
2008-04-18 00:58:42 +02:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
w->area.width = d->width;
|
2008-06-03 16:08:33 +02:00
|
|
|
w->area.height = ctx->height;
|
2008-05-20 15:39:47 +02:00
|
|
|
return w->area.width;
|
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)
|
|
|
|
{
|
|
|
|
widget_t **widget = luaA_checkudata(L, 1, "widget");
|
|
|
|
graph_data_t *d = (*widget)->data;
|
|
|
|
float max_value;
|
|
|
|
const char *title, *buf;
|
|
|
|
size_t len;
|
|
|
|
plot_t *plot;
|
|
|
|
|
|
|
|
title = luaL_checkstring(L, 2);
|
|
|
|
luaA_checktable(L, 3);
|
|
|
|
|
|
|
|
for(plot = d->plots; plot; plot = plot->next)
|
|
|
|
if(!a_strcmp(title, plot->title))
|
|
|
|
break;
|
|
|
|
/* no plot found -> create one */
|
|
|
|
if(!plot)
|
|
|
|
plot = graph_plot_add(d, title);
|
|
|
|
|
2008-07-10 15:30:16 +02:00
|
|
|
if((buf = luaA_getopt_lstring(L, 3, "fg", NULL, &len)))
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +02:00
|
|
|
xcolor_init(&plot->color_start, globalconf.connection,
|
2008-07-10 15:30:16 +02:00
|
|
|
globalconf.default_screen, 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)))
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +02:00
|
|
|
xcolor_init(&plot->pcolor_center, globalconf.connection,
|
2008-07-10 15:30:16 +02:00
|
|
|
globalconf.default_screen, 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)))
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +02:00
|
|
|
xcolor_init(&plot->pcolor_end, globalconf.connection,
|
2008-07-10 15:30:16 +02:00
|
|
|
globalconf.default_screen, 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
|
|
|
|
|
|
|
widget_invalidate_bywidget(*widget);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
widget_t **widget = luaA_checkudata(L, 1, "widget");
|
|
|
|
graph_data_t *d = (*widget)->data;
|
|
|
|
plot_t *plot;
|
|
|
|
const char *title = luaL_checkstring(L, 2);
|
|
|
|
float value;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(plot = d->plots; plot; plot = plot->next)
|
|
|
|
if(!a_strcmp(title, plot->title))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* no plot found -> create one */
|
|
|
|
if(!plot)
|
|
|
|
plot = graph_plot_add(d, title);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
widget_invalidate_bywidget(*widget);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Index 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-25 17:47:51 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
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
|
|
|
{
|
2008-06-28 13:02:19 +02:00
|
|
|
widget_t **widget = luaA_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:
|
2008-07-02 11:04:17 +02:00
|
|
|
luaA_pushcolor(L, &d->border_color);
|
2008-06-28 13:31:33 +02:00
|
|
|
break;
|
|
|
|
case A_TK_BG:
|
2008-07-02 11:04:17 +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;
|
|
|
|
widget_t **widget = luaA_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;
|
|
|
|
plot_t *plot;
|
|
|
|
position_t pos;
|
2008-07-10 15:30:16 +02:00
|
|
|
xcolor_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);
|
|
|
|
if(width != d->width)
|
|
|
|
{
|
|
|
|
d->width = width;
|
|
|
|
d->size = d->width - 2;
|
|
|
|
for(plot = d->plots; plot; plot = plot->next)
|
|
|
|
{
|
|
|
|
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)))
|
|
|
|
{
|
|
|
|
if(xcolor_init(&color, globalconf.connection,
|
|
|
|
globalconf.default_screen, buf, len))
|
|
|
|
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)))
|
|
|
|
{
|
|
|
|
if(xcolor_init(&color, globalconf.connection,
|
|
|
|
globalconf.default_screen, buf, len))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
widget_invalidate_bywidget(*widget);
|
|
|
|
|
|
|
|
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-06-25 17:47:51 +02:00
|
|
|
plot_list_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.
|
|
|
|
* \param align The widget alignment.
|
|
|
|
* \return A graph widget.
|
|
|
|
*/
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *
|
2008-05-20 15:39:47 +02:00
|
|
|
graph_new(alignment_t align)
|
2008-01-06 18:23:56 +01:00
|
|
|
{
|
2008-04-11 11:26:37 +02:00
|
|
|
widget_t *w;
|
2008-06-11 16:44:18 +02:00
|
|
|
graph_data_t *d;
|
2008-01-06 18:23:56 +01:00
|
|
|
|
2008-04-11 11:26:37 +02:00
|
|
|
w = p_new(widget_t, 1);
|
2008-05-20 15:39:47 +02:00
|
|
|
widget_common_new(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-05-20 15:39:47 +02:00
|
|
|
w->align = align;
|
2008-06-11 16:44:18 +02:00
|
|
|
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
|
|
|
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +02:00
|
|
|
d->bg = globalconf.colors.bg;
|
|
|
|
d->border_color = globalconf.colors.fg;
|
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
|