Repository navigation
Expand file tree
/
Copy pathgraph.c
More file actions
619 lines (543 loc) · 17.3 KB
/
Copy pathgraph.c
File metadata and controls
619 lines (543 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
* 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.
*
*/
#include <math.h>
#include "widget.h"
#include "common/tokenize.h"
typedef enum
{
Bottom_Style = 0,
Top_Style,
Line_Style
} plot_style_t;
/** The plot data structure. */
typedef struct
{
/** Grapht title of the plot sections */
char *title;
/** Represents a full graph */
float max_value;
/** Scale the graph */
bool scale;
/* markers... */
/** 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 */
plot_style_t draw_style;
/** Keeps the calculated values (line-length); */
int *lines;
/** Actual values */
float *values;
/** Color of them */
color_t color_start;
/** Color at middle of graph */
color_t pcolor_center;
/** Color at end of graph */
color_t pcolor_end;
/** Create a vertical color gradient */
bool vertical_gradient;
} plot_t;
static void
plot_delete(plot_t *g)
{
p_delete(&g->title);
p_delete(&g->lines);
p_delete(&g->values);
}
DO_ARRAY(plot_t, plot, plot_delete)
/** The private graph data structure */
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 */
color_t bg;
/** Border color */
color_t border_color;
/** 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 */
plot_array_t plots;
} graph_data_t;
/** 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)
{
plot_t plot;
p_clear(&plot, 1);
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;
xcolor_to_color(&globalconf.colors.fg, &plot.color_start);
plot_array_append(&d->plots, plot);
return &d->plots.tab[d->plots.len - 1];
}
/** 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);
}
static area_t
graph_geometry(widget_t *widget, screen_t *screen, int height, int width)
{
area_t geometry;
graph_data_t *d = widget->data;
geometry.x = geometry.y = 0;
geometry.height = height;
geometry.width = d->width;
return geometry;
}
/** 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.
* \return The widget width.
*/
static void
graph_draw(widget_t *widget, draw_context_t *ctx,
area_t geometry, wibox_t *p)
{
int margin_top, y;
graph_data_t *d = widget->data;
area_t rectangle;
vector_t color_gradient;
if(!d->plots.len)
return;
/* box = the plot inside the rectangle */
if(!d->box_height)
d->box_height = round(ctx->height * d->height) - 2;
margin_top = round((ctx->height - (d->box_height + 2)) / 2) + geometry.y;
/* draw background */
rectangle.x = geometry.x + 1;
rectangle.y = margin_top + 1;
rectangle.width = d->size;
rectangle.height = d->box_height;
draw_rectangle(ctx, rectangle, 1.0, true, &d->bg);
/* for plot drawing */
rectangle.y = margin_top + d->box_height + 1; /* bottom left corner as starting point */
rectangle.width = d->size; /* rectangle.height is not used */
draw_graph_setup(ctx); /* setup some drawing options */
/* gradient begin either left or on the right of the rectangle */
if(d->grow == Right)
color_gradient.x = rectangle.x + rectangle.width;
else
color_gradient.x = rectangle.x;
for(int i = 0; i < d->plots.len; i++)
{
plot_t *plot = &d->plots.tab[i];
switch(plot->draw_style)
{
case Top_Style:
color_gradient.y = rectangle.y - rectangle.height;
if(plot->vertical_gradient)
{
color_gradient.x_offset = 0;
color_gradient.y_offset = rectangle.height;
}
else
{
color_gradient.y_offset = 0;
if(d->grow == Right)
color_gradient.x_offset = - rectangle.width;
else
color_gradient.x_offset = rectangle.width;
}
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 */
d->draw_to[y] = d->box_height - plot->lines[y]; /* i.e. on full plot -> 0 = bottom */
}
draw_graph(ctx, rectangle , d->draw_from, d->draw_to, plot->index, d->grow, color_gradient,
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
break;
case Bottom_Style:
color_gradient.y = rectangle.y;
if(plot->vertical_gradient)
{
color_gradient.x_offset = 0;
color_gradient.y_offset = - rectangle.height;
}
else
{
color_gradient.y_offset = 0;
if(d->grow == Right)
color_gradient.x_offset = - rectangle.width;
else
color_gradient.x_offset = rectangle.width;
}
p_clear(d->draw_from, d->size);
draw_graph(ctx, rectangle, d->draw_from, plot->lines, plot->index, d->grow, color_gradient,
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
break;
case Line_Style:
color_gradient.y = rectangle.y;
if(plot->vertical_gradient)
{
color_gradient.x_offset = 0;
color_gradient.y_offset = -rectangle.height;
}
else
{
color_gradient.y_offset = 0;
if(d->grow == Right)
color_gradient.x_offset = - rectangle.width;
else
color_gradient.x_offset = rectangle.width;
}
draw_graph_line(ctx, rectangle, plot->lines, plot->index, d->grow, color_gradient,
&plot->color_start, &plot->pcolor_center, &plot->pcolor_end);
break;
}
}
/* draw border (after line-drawing, what paints 0-values to the border) */
rectangle.x = geometry.x;
rectangle.y = margin_top;
rectangle.width = d->size + 2;
rectangle.height = d->box_height + 2;
draw_rectangle(ctx, rectangle, 1.0, false, &d->border_color);
}
/** Set various plot graph properties.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \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 = luaL_checkudata(L, 1, "widget");
graph_data_t *d = widget->data;
float max_value;
const char *title, *buf;
size_t len;
plot_t *plot = NULL;
color_init_cookie_t reqs[3];
int i, reqs_nbr = -1;
title = luaL_checkstring(L, 2);
luaA_checktable(L, 3);
plot = graph_plot_get(d, title);
if((buf = luaA_getopt_lstring(L, 3, "fg", NULL, &len)))
reqs[++reqs_nbr] = color_init_unchecked(&plot->color_start, buf, len);
if((buf = luaA_getopt_lstring(L, 3, "fg_center", NULL, &len)))
reqs[++reqs_nbr] = color_init_unchecked(&plot->pcolor_center, buf, len);
if((buf = luaA_getopt_lstring(L, 3, "fg_end", NULL, &len)))
reqs[++reqs_nbr] = color_init_unchecked(&plot->pcolor_end, buf, len);
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))
{
case A_TK_BOTTOM:
plot->draw_style = Bottom_Style;
break;
case A_TK_LINE:
plot->draw_style = Line_Style;
break;
case A_TK_TOP:
plot->draw_style = Top_Style;
break;
default:
break;
}
for(i = 0; i <= reqs_nbr; i++)
color_init_reply(reqs[i]);
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.
* \luastack
* \lvalue A widget.
* \lparam A plot name.
* \lparam A data value.
*/
static int
luaA_graph_plot_data_add(lua_State *L)
{
widget_t *widget = luaL_checkudata(L, 1, "widget");
graph_data_t *d = widget->data;
plot_t *plot = NULL;
const char *title = luaL_checkstring(L, 2);
float value;
int i;
if(!d->size)
return 0;
plot = graph_plot_get(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;
}
/** Graph widget.
* \param L The Lua VM state.
* \param token The key token.
* \return The number of elements pushed on stack.
* \luastack
* \lfield plot_properties_set A function to set plot properties.
* \lfield plot_data_add A function to add data to a plot.
* \lfield height Graph height.
* \lfield widget Graph width.
* \lfield bg Background color.
* \lfield grow Direction to grow: left or right.
*/
static int
luaA_graph_index(lua_State *L, awesome_token_t token)
{
widget_t *widget = luaL_checkudata(L, 1, "widget");
graph_data_t *d = widget->data;
switch(token)
{
case A_TK_PLOT_PROPERTIES_SET:
lua_pushcfunction(L, luaA_graph_plot_properties_set);
break;
case A_TK_PLOT_DATA_ADD:
lua_pushcfunction(L, luaA_graph_plot_data_add);
break;
case A_TK_HEIGHT:
lua_pushnumber(L, d->height);
break;
case A_TK_WIDTH:
lua_pushnumber(L, d->width);
break;
case A_TK_BORDER_COLOR:
luaA_pushcolor(L, &d->border_color);
break;
case A_TK_BG:
luaA_pushcolor(L, &d->bg);
break;
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;
default:
return 0;
}
return 1;
}
/** Newindex function for graph widget.
* \param L The Lua VM state.
* \param token The key token.
* \return The number of elements pushed on stack.
*/
static int
luaA_graph_newindex(lua_State *L, awesome_token_t token)
{
size_t len;
widget_t *widget = luaL_checkudata(L, 1, "widget");
graph_data_t *d = widget->data;
const char *buf;
int width;
position_t pos;
color_t color;
switch(token)
{
case A_TK_HEIGHT:
d->height = luaL_checknumber(L, 3);
break;
case A_TK_WIDTH:
width = luaL_checknumber(L, 3);
if(width >= 2 && width != d->width)
{
d->width = width;
d->size = d->width - 2;
p_realloc(&d->draw_from, d->size);
p_realloc(&d->draw_to, d->size);
for(int i = 0; i < d->plots.len; i++)
{
plot_t *plot = &d->plots.tab[i];
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:
if((buf = luaL_checklstring(L, 3, &len)))
{
if(color_init_reply(color_init_unchecked(&color, buf, len)))
d->bg = color;
else
return 0;
}
break;
case A_TK_BORDER_COLOR:
if((buf = luaL_checklstring(L, 3, &len)))
{
if(color_init_reply(color_init_unchecked(&color, buf, len)))
d->border_color = color;
else
return 0;
}
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;
}
/** Destroy definitively a graph widget.
* \param widget Who slay.
*/
static void
graph_destructor(widget_t *widget)
{
graph_data_t *d = widget->data;
plot_array_wipe(&d->plots);
p_delete(&d->draw_from);
p_delete(&d->draw_to);
p_delete(&d);
}
/** Create a brand new graph.
* \param w The widget to initialize.
* \return The same widget.
*/
widget_t *
widget_graph(widget_t *w)
{
w->draw = graph_draw;
w->index = luaA_graph_index;
w->newindex = luaA_graph_newindex;
w->destructor = graph_destructor;
w->geometry = graph_geometry;
graph_data_t *d = w->data = p_new(graph_data_t, 1);
d->width = 80;
d->height = 0.80;
d->size = d->width - 2;
d->grow = Left;
d->draw_from = p_new(int, d->size);
d->draw_to = p_new(int, d->size);
xcolor_to_color(&globalconf.colors.bg, &d->bg);
xcolor_to_color(&globalconf.colors.fg, &d->border_color);
return w;
}
/* 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 },
};
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80