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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <gtk/gtk.h>
/* ------------------------------------------------------------------------ */
typedef struct gload {
GtkWidget *window;
GtkWidget *label;
GtkWidget *graph;
} gload;
/* ------------------------------------------------------------------------ */
static int gload_read()
{
char line[80];
int file, ret, val, pos;
int vals[3] = {0, 0, 0};
/* read proc file */
file = open("/proc/loadavg", O_RDONLY);
if (file < 0)
return -1;
ret = read(file, line, sizeof(line) - 1);
if (ret < 0) {
close(file);
return -1;
}
line[ret] = 0;
close(file);
/* parse load values */
for (val = 0, pos = 0;
val < 3 && pos < ret;
pos++) {
if (line[pos] == ' ') {
val++;
continue;
}
if (line[pos] == '.') {
continue;
}
if (line[pos] < '0' || line[pos] > '9') {
return -1;
}
vals[val] *= 10;
vals[val] += line[pos] - '0';
}
if (val != 3)
return -1;
fprintf(stderr, "%s: %d %d %d\n", __func__,
vals[0], vals[1], vals[2]);
return 0;
}
static gboolean gload_draw(GtkWidget *widget, cairo_t *cr, gpointer data)
{
// gload *gl = data;
GtkStyleContext *context;
guint width, height;
GdkRGBA color;
context = gtk_widget_get_style_context(widget);
width = gtk_widget_get_allocated_width(widget);
height = gtk_widget_get_allocated_height(widget);
#if 1 /* gtk doc example code */
gtk_render_background(context, cr, 0, 0, width, height);
cairo_arc(cr,
width / 2.0, height / 2.0,
MIN (width, height) / 2.0,
0, 2 * G_PI);
gtk_style_context_get_color(context,
gtk_style_context_get_state(context),
&color);
gdk_cairo_set_source_rgba(cr, &color);
cairo_fill(cr);
#endif
return FALSE;
}
static void gload_window_destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
static gload *gload_new(void)
{
struct utsname uts;
GtkWidget *vbox;
gload *gl = g_new0(gload, 1);
gl->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(gl->window), "destroy",
G_CALLBACK(gload_window_destroy), gl);
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(gl->window), vbox);
uname(&uts);
gl->label = gtk_label_new(uts.nodename);
gtk_box_pack_start(GTK_BOX(vbox), gl->label, false, false, 0);
gl->graph = gtk_drawing_area_new();
gtk_widget_set_size_request(gl->graph, 100, 70);
g_signal_connect(G_OBJECT(gl->graph), "draw",
G_CALLBACK(gload_draw), gl);
gtk_box_pack_start(GTK_BOX(vbox), gl->graph, true, true, 0);
gtk_widget_show_all(gl->window);
return gl;
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
gload_new();
gload_read();
gtk_main();
return 0;
}
|