summaryrefslogtreecommitdiff
path: root/multiload/load-graph.c
diff options
context:
space:
mode:
authormarosg <[email protected]>2017-05-07 23:38:01 +0200
committermarosg <[email protected]>2017-05-09 17:57:18 +0200
commit3588cfe609609b5c2c818ab7f15ddf1bac9f50d3 (patch)
treec82a65d3edb8381078c149877e9a49f13b1c9e48 /multiload/load-graph.c
parentf368d64f25f3170b9697b69d1b61be2e4cf576d5 (diff)
downloadmate-applets-3588cfe609609b5c2c818ab7f15ddf1bac9f50d3.tar.bz2
mate-applets-3588cfe609609b5c2c818ab7f15ddf1bac9f50d3.tar.xz
Load graph scales automatically according to average load, draws horicontal grid lines for each load level.
Diffstat (limited to 'multiload/load-graph.c')
-rw-r--r--multiload/load-graph.c136
1 files changed, 93 insertions, 43 deletions
diff --git a/multiload/load-graph.c b/multiload/load-graph.c
index a98a81ab..5eefd36c 100644
--- a/multiload/load-graph.c
+++ b/multiload/load-graph.c
@@ -12,6 +12,7 @@
#include <gio/gio.h>
#include <mate-panel-applet.h>
#include <mate-panel-applet-gsettings.h>
+#include <math.h>
#include "global.h"
@@ -37,7 +38,7 @@ shift_right(LoadGraph *g)
/* data[i+1] = data[i] */
for(i = g->draw_width - 1; i != 0; --i)
- g->data[i] = g->data[i-1];
+ g->data[i] = g->data[i - 1];
g->data[0] = last_data;
}
@@ -47,44 +48,93 @@ shift_right(LoadGraph *g)
static void
load_graph_draw (LoadGraph *g)
{
- guint i, j;
- cairo_t *cr;
-
- /* we might get called before the configure event so that
- * g->disp->allocation may not have the correct size
- * (after the user resized the applet in the prop dialog). */
-
- if (!g->surface)
- g->surface = gdk_window_create_similar_surface (gtk_widget_get_window (g->disp),
- CAIRO_CONTENT_COLOR,
- g->draw_width, g->draw_height);
-
- cr = cairo_create (g->surface);
- cairo_set_line_width (cr, 1.0);
- cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
- cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
-
+ guint i, j, k;
+ cairo_t *cr;
+ int load;
+
+ /* we might get called before the configure event so that
+ * g->disp->allocation may not have the correct size
+ * (after the user resized the applet in the prop dialog). */
+
+ if (!g->surface)
+ g->surface = gdk_window_create_similar_surface (gtk_widget_get_window (g->disp),
+ CAIRO_CONTENT_COLOR,
+ g->draw_width, g->draw_height);
+
+ cr = cairo_create (g->surface);
+ cairo_set_line_width (cr, 1.0);
+ cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
+ cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
+
+ /* all graphs except Load go this path */
+ if (g->id != 4)
+ {
for (i = 0; i < g->draw_width; i++)
- g->pos [i] = g->draw_height - 1;
+ g->pos [i] = g->draw_height - 1;
for (j = 0; j < g->n; j++)
{
- gdk_cairo_set_source_rgba (cr, &(g->colors [j]));
+ gdk_cairo_set_source_rgba (cr, &(g->colors [j]));
+
+ for (i = 0; i < g->draw_width; i++)
+ {
+ if (g->data [i][j] != 0)
+ {
+ cairo_move_to (cr, g->draw_width - i - 0.5, g->pos[i] + 0.5);
+ cairo_line_to (cr, g->draw_width - i - 0.5, g->pos[i] - (g->data [i][j] - 0.5));
+ }
+ g->pos [i] -= g->data [i][j];
+ }
+ cairo_stroke (cr);
+ }
- for (i = 0; i < g->draw_width; i++) {
- if (g->data [i][j] != 0) {
- cairo_move_to (cr, g->draw_width - i - 0.5, g->pos[i] + 0.5);
- cairo_line_to (cr, g->draw_width - i - 0.5, g->pos[i] - (g->data [i][j] - 0.5));
+ }
+ /* this is Load graph */
+ else
+ {
+ guint maxload = 1;
+ for (i = 0; i < g->draw_width; i++)
+ {
+ g->pos [i] = g->draw_height - 1;
+ /* find maximum value */
+ if (g->data[i][0] > maxload)
+ maxload = g->data[i][0];
+ }
+ load = ceil ((double) (maxload/g->draw_height)) + 1;
+ load = MAX (load,1);
- g->pos [i] -= g->data [i][j];
- }
- }
- cairo_stroke (cr);
+ for (j = 0; j < g->n; j++)
+ {
+ gdk_cairo_set_source_rgba (cr, &(g->colors [j]));
+
+ for (i = 0; i < g->draw_width; i++)
+ {
+ cairo_move_to (cr, g->draw_width - i - 0.5, g->pos[i] + 0.5);
+ if (j == 0)
+ {
+ cairo_line_to (cr, g->draw_width - i - 0.5, g->pos[i] - ((g->data [i][j] - 0.5)/load));
+ }
+ else
+ {
+ cairo_line_to (cr, g->draw_width - i - 0.5, 0.5);
+ }
+ g->pos [i] -= g->data [i][j] / load;
+ }
+ cairo_stroke (cr);
}
- gtk_widget_queue_draw (g->disp);
+ /* draw grid lines in Load graph if needed */
+ gdk_cairo_set_source_rgba (cr, &(g->colors [2]));
+ for (k = 0; k < load - 1; k++)
+ {
+ cairo_move_to (cr, 0.5, (g->draw_height/load)*(k+1));
+ cairo_line_to (cr, g->draw_width-0.5, (g->draw_height/load)*(k+1));
+ }
+ cairo_stroke (cr);
+ }
+ gtk_widget_queue_draw (g->disp);
- cairo_destroy (cr);
+ cairo_destroy (cr);
}
/* Updates the load graph when the timeout expires */
@@ -123,7 +173,7 @@ load_graph_unalloc (LoadGraph *g)
g->pos = NULL;
g->data = NULL;
-
+
g->size = g_settings_get_int(g->multiload->settings, "size");
g->size = MAX (g->size, 10);
@@ -161,7 +211,7 @@ load_graph_configure (GtkWidget *widget, GdkEventConfigure *event,
{
GtkAllocation allocation;
LoadGraph *c = (LoadGraph *) data_ptr;
-
+
load_graph_unalloc (c);
gtk_widget_get_allocation (c->disp, &allocation);
@@ -170,7 +220,7 @@ load_graph_configure (GtkWidget *widget, GdkEventConfigure *event,
c->draw_height = allocation.height;
c->draw_width = MAX (c->draw_width, 1);
c->draw_height = MAX (c->draw_height, 1);
-
+
load_graph_alloc (c);
if (!c->surface)
@@ -246,7 +296,7 @@ load_graph_load_config (LoadGraph *g)
if (!g->colors)
g->colors = g_new0(GdkRGBA, g->n);
-
+
for (i = 0; i < g->n; i++)
{
name = g_strdup_printf ("%s-color%u", g->name, i);
@@ -261,12 +311,12 @@ load_graph_load_config (LoadGraph *g)
LoadGraph *
load_graph_new (MultiloadApplet *ma, guint n, const gchar *label,
- guint id, guint speed, guint size, gboolean visible,
+ guint id, guint speed, guint size, gboolean visible,
const gchar *name, LoadGraphDataFunc get_data)
{
LoadGraph *g;
MatePanelAppletOrient orient;
-
+
g = g_new0 (LoadGraph, 1);
g->netspeed_in = netspeed_new(g);
g->netspeed_out = netspeed_new(g);
@@ -280,11 +330,11 @@ load_graph_new (MultiloadApplet *ma, guint n, const gchar *label,
g->tooltip_update = FALSE;
g->show_frame = TRUE;
g->multiload = ma;
-
+
g->main_widget = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
g->box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
-
+
orient = mate_panel_applet_get_orient (g->multiload->applet);
switch (orient)
{
@@ -303,7 +353,7 @@ load_graph_new (MultiloadApplet *ma, guint n, const gchar *label,
default:
g_assert_not_reached ();
}
-
+
if (g->show_frame)
{
g->frame = gtk_frame_new (NULL);
@@ -333,7 +383,7 @@ load_graph_new (MultiloadApplet *ma, guint n, const gchar *label,
GDK_ENTER_NOTIFY_MASK |
GDK_LEAVE_NOTIFY_MASK |
GDK_BUTTON_PRESS_MASK);
-
+
g_signal_connect (G_OBJECT (g->disp), "draw",
G_CALLBACK (load_graph_expose), g);
g_signal_connect (G_OBJECT(g->disp), "configure_event",
@@ -346,8 +396,8 @@ load_graph_new (MultiloadApplet *ma, guint n, const gchar *label,
G_CALLBACK(load_graph_enter_cb), g);
g_signal_connect (G_OBJECT(g->disp), "leave-notify-event",
G_CALLBACK(load_graph_leave_cb), g);
-
- gtk_box_pack_start (GTK_BOX (g->box), g->disp, TRUE, TRUE, 0);
+
+ gtk_box_pack_start (GTK_BOX (g->box), g->disp, TRUE, TRUE, 0);
gtk_widget_show_all(g->box);
return g;
@@ -368,6 +418,6 @@ load_graph_stop (LoadGraph *g)
{
if (g->timer_index != -1)
g_source_remove (g->timer_index);
-
+
g->timer_index = -1;
}