~vcs-imports/gnome-system-monitor/trunk

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
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <config.h>

#include <glib/gi18n.h>
#include <glibtop.h>
#include <glibtop/close.h>
#include <glibtop/cpu.h>
#include <glibtop/sysinfo.h>
#include <signal.h>

#include "application.h"
#include "procdialogs.h"
#include "prefsdialog.h"
#include "interface.h"
#include "proctable.h"
#include "load-graph.h"
#include "settings-keys.h"
#include "argv.h"
#include "util.h"
#include "lsof.h"
#include "disks.h"

static void
cb_solaris_mode_changed (GSettings *settings, const gchar *key, gpointer data)
{
    GsmApplication *app = static_cast<GsmApplication *>(data);

    app->config.solaris_mode = g_settings_get_boolean(settings, key);
    app->cpu_graph->clear_background();
    if (app->timeout) {
        proctable_update (app);
    }
}

static void
cb_draw_stacked_changed (GSettings *settings, const gchar *key, gpointer data)
{
    GsmApplication *app = static_cast<GsmApplication *>(data);

    app->config.draw_stacked = g_settings_get_boolean(settings, key);
    app->cpu_graph->clear_background();
    load_graph_reset(app->cpu_graph);
}


static void
cb_network_in_bits_changed (GSettings *settings, const gchar *key, gpointer data)
{
    GsmApplication *app = static_cast<GsmApplication *>(data);

    app->config.network_in_bits = g_settings_get_boolean(settings, key);
    // force scale to be redrawn
    app->net_graph->clear_background();
}

static void
cb_timeouts_changed (GSettings *settings, const gchar *key, gpointer data)
{
    GsmApplication *app = static_cast<GsmApplication *>(data);

    if (strcmp (key, GSM_SETTING_PROCESS_UPDATE_INTERVAL) == 0) {
        app->config.update_interval = g_settings_get_int (settings, key);

        app->smooth_refresh->reset();
        if (app->timeout) {
            proctable_reset_timeout (app);
        }
    } else if (strcmp (key, GSM_SETTING_GRAPH_UPDATE_INTERVAL) == 0) {
        app->config.graph_update_interval = g_settings_get_int (settings, key);
        load_graph_change_speed(app->cpu_graph,
                                app->config.graph_update_interval);
        load_graph_change_speed(app->mem_graph,
                                app->config.graph_update_interval);
        load_graph_change_speed(app->net_graph,
                                app->config.graph_update_interval);
    } else if (strcmp (key, GSM_SETTING_DISKS_UPDATE_INTERVAL) == 0) {
        app->config.disks_update_interval = g_settings_get_int (settings, key);
        disks_reset_timeout (app);
    }
}

static void
apply_cpu_color_settings(GSettings *settings, gpointer data)
{
    GsmApplication *app = static_cast<GsmApplication *>(data);

    GVariant *cpu_colors_var = g_settings_get_value (settings, GSM_SETTING_CPU_COLORS);
    gsize n = g_variant_n_children(cpu_colors_var);

    gchar *color;

    // Create builder to add the new colors if user has more than the number of cores with defaults.
    GVariantBuilder builder;
    GVariant* child;
    GVariant* full;

    g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);

    for (guint i = 0; i < static_cast<guint>(app->config.num_cpus); i++) {
        if(i < n) {
            child = g_variant_get_child_value ( cpu_colors_var, i );
            g_variant_get_child( child, 1, "s", &color);
            g_variant_builder_add_value ( &builder, child);
            g_variant_unref (child);
        } else {
            color = g_strdup ("#f25915e815e8");
            g_variant_builder_add(&builder, "(us)", i, color);
        }
        gdk_rgba_parse(&app->config.cpu_color[i], color);
        g_free (color);
    }
    full = g_variant_builder_end(&builder);
    // if the user has more cores than colors stored in the gsettings, store the newly built gvariant in gsettings
    if (n < static_cast<guint>(app->config.num_cpus)) {
        g_settings_set_value(settings, GSM_SETTING_CPU_COLORS, full);
    } else {
        g_variant_unref(full);
    }

    g_variant_unref(cpu_colors_var);
}

static void
cb_color_changed (GSettings *settings, const gchar *key, gpointer data)
{
    GsmApplication *app = static_cast<GsmApplication *>(data);

    if (strcmp (key, GSM_SETTING_CPU_COLORS) == 0) {
        apply_cpu_color_settings(settings, app);
        for (int i = 0; i < app->config.num_cpus; i++) {
            if(!gdk_rgba_equal(&app->cpu_graph->colors[i], &app->config.cpu_color[i])) {
                app->cpu_graph->colors[i] = app->config.cpu_color[i];
                break;
            }
        }
        return;
    }

    gchar *color = g_settings_get_string (settings, key);
    if (strcmp (key, GSM_SETTING_MEM_COLOR) == 0) {
        gdk_rgba_parse (&app->config.mem_color, color);
        app->mem_graph->colors.at(0) = app->config.mem_color;
    } else if (strcmp (key, GSM_SETTING_SWAP_COLOR) == 0) {
        gdk_rgba_parse (&app->config.swap_color, color);
        app->mem_graph->colors.at(1) = app->config.swap_color;
    } else if (strcmp (key, GSM_SETTING_NET_IN_COLOR) == 0) {
        gdk_rgba_parse (&app->config.net_in_color, color);
        app->net_graph->colors.at(0) = app->config.net_in_color;
    } else if (strcmp (key, GSM_SETTING_NET_OUT_COLOR) == 0) {
        gdk_rgba_parse (&app->config.net_out_color, color);
        app->net_graph->colors.at(1) = app->config.net_out_color;
    }
    g_free (color);
}

void
GsmApplication::load_settings()
{
    gchar *color;
    glibtop_cpu cpu;

    settings = g_settings_new (GSM_GSETTINGS_SCHEMA);

    config.solaris_mode = g_settings_get_boolean (settings, GSM_SETTING_SOLARIS_MODE);
    g_signal_connect (settings, "changed::" GSM_SETTING_SOLARIS_MODE,
                      G_CALLBACK (cb_solaris_mode_changed), this);

    config.draw_stacked = g_settings_get_boolean (settings, GSM_SETTING_DRAW_STACKED);
    g_signal_connect (settings, "changed::" GSM_SETTING_DRAW_STACKED,
                      G_CALLBACK (cb_draw_stacked_changed), this);

    config.network_in_bits = g_settings_get_boolean (settings, GSM_SETTING_NETWORK_IN_BITS);
    g_signal_connect (settings, "changed::" GSM_SETTING_NETWORK_IN_BITS,
                      G_CALLBACK (cb_network_in_bits_changed), this);

    config.update_interval = g_settings_get_int (settings, GSM_SETTING_PROCESS_UPDATE_INTERVAL);
    g_signal_connect (settings, "changed::" GSM_SETTING_PROCESS_UPDATE_INTERVAL,
                      G_CALLBACK (cb_timeouts_changed), this);
    config.graph_update_interval = g_settings_get_int (settings, GSM_SETTING_GRAPH_UPDATE_INTERVAL);
    g_signal_connect (settings, "changed::" GSM_SETTING_GRAPH_UPDATE_INTERVAL,
                      G_CALLBACK (cb_timeouts_changed), this);
    config.disks_update_interval = g_settings_get_int (settings, GSM_SETTING_DISKS_UPDATE_INTERVAL);
    g_signal_connect (settings, "changed::" GSM_SETTING_DISKS_UPDATE_INTERVAL,
                      G_CALLBACK (cb_timeouts_changed), this);

    glibtop_get_cpu (&cpu);
    frequency = cpu.frequency;

    config.num_cpus = glibtop_get_sysinfo()->ncpu; // or server->ncpu + 1

    apply_cpu_color_settings (settings, this);
    g_signal_connect (settings, "changed::" GSM_SETTING_CPU_COLORS,
                      G_CALLBACK (cb_color_changed), this);

    color = g_settings_get_string (settings, GSM_SETTING_MEM_COLOR);
    if (!color)
        color = g_strdup ("#000000ff0082");
    g_signal_connect (settings, "changed::" GSM_SETTING_MEM_COLOR,
                      G_CALLBACK (cb_color_changed), this);
    gdk_rgba_parse (&config.mem_color, color);
    g_free (color);

    color = g_settings_get_string (settings, GSM_SETTING_SWAP_COLOR);
    if (!color)
        color = g_strdup ("#00b6000000ff");
    g_signal_connect (settings, "changed::" GSM_SETTING_SWAP_COLOR,
                      G_CALLBACK (cb_color_changed), this);
    gdk_rgba_parse (&config.swap_color, color);
    g_free (color);

    color = g_settings_get_string (settings, GSM_SETTING_NET_IN_COLOR);
    if (!color)
        color = g_strdup ("#000000f200f2");
    g_signal_connect (settings, "changed::" GSM_SETTING_NET_IN_COLOR,
                      G_CALLBACK (cb_color_changed), this);
    gdk_rgba_parse (&config.net_in_color, color);
    g_free (color);

    color = g_settings_get_string (settings, GSM_SETTING_NET_OUT_COLOR);
    if (!color)
        color = g_strdup ("#00f2000000c1");
    g_signal_connect (settings, "changed::" GSM_SETTING_NET_OUT_COLOR,
                      G_CALLBACK (cb_color_changed), this);
    gdk_rgba_parse (&config.net_out_color, color);
    g_free (color);
}


GsmApplication::GsmApplication()
    : Gtk::Application("org.gnome.SystemMonitor", Gio::APPLICATION_HANDLES_COMMAND_LINE),
      tree(NULL),
      proc_actionbar_revealer(NULL),
      popup_menu(NULL),
      disk_list(NULL),
      stack(NULL),
      refresh_button(NULL),
      process_menu_button(NULL),
      end_process_button(NULL),
      search_button(NULL),
      search_entry(NULL),
      search_bar(NULL),
      config(),
      cpu_graph(NULL),
      mem_graph(NULL),
      net_graph(NULL),
      cpu_label_fixed_width(0),
      net_label_fixed_width(0),
      selection(NULL),
      timeout(0U),
      disk_timeout(0U),
      top_of_tree(NULL),
      last_vscroll_max(0.0),
      last_vscroll_value(0.0),
      pretty_table(NULL),
      settings(NULL),
      main_window(NULL),
      frequency(0U),
      smooth_refresh(NULL),
      cpu_total_time(0ULL),
      cpu_total_time_last(0ULL)
{
    Glib::set_application_name(_("System Monitor"));
}

Glib::RefPtr<GsmApplication> GsmApplication::get ()
{
    static Glib::RefPtr<GsmApplication> singleton;

    if (!singleton) {
        singleton = Glib::RefPtr<GsmApplication>(new GsmApplication());
    }
    return singleton;
}

void GsmApplication::on_activate()
{
    gtk_window_present (GTK_WINDOW (main_window));
}

void
GsmApplication::save_config ()
{
    int width, height, xpos, ypos;
    gboolean maximized;

    gtk_window_get_size (GTK_WINDOW (main_window), &width, &height);
    gtk_window_get_position (GTK_WINDOW (main_window), &xpos, &ypos);

    maximized = gdk_window_get_state (gtk_widget_get_window (main_window)) & GDK_WINDOW_STATE_MAXIMIZED;

    g_settings_set (settings, GSM_SETTING_WINDOW_STATE, "(iiii)",
                    width, height, xpos, ypos);

    g_settings_set_boolean (settings, GSM_SETTING_MAXIMIZED, maximized);
}

int GsmApplication::on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>& command_line)
{
    int argc = 0;
    char** argv = command_line->get_arguments(argc);

    Glib::OptionContext context;
    context.set_summary(_("A simple process and system monitor."));
    context.set_ignore_unknown_options(true);
    procman::OptionGroup option_group;
    context.set_main_group(option_group);

    try {
        context.parse(argc, argv);
    } catch (const Glib::Error& ex) {
        g_error("Arguments parse error : %s", ex.what().c_str());
    }

    g_strfreev(argv);

    if (option_group.print_version) {
        g_print("%s %s\n", _("GNOME System Monitor"), VERSION);
        exit (EXIT_SUCCESS);
    }

    if (option_group.show_processes_tab)
        g_settings_set_string (settings, GSM_SETTING_CURRENT_TAB, "processes");
    else if (option_group.show_resources_tab)
        g_settings_set_string (settings, GSM_SETTING_CURRENT_TAB, "resources");
    else if (option_group.show_file_systems_tab)
        g_settings_set_string (settings, GSM_SETTING_CURRENT_TAB, "disks");
    else if (option_group.print_version)

    on_activate ();

    return 0;
}

void
GsmApplication::on_help_activate(const Glib::VariantBase&)
{
    GError* error = 0;
    if (!g_app_info_launch_default_for_uri("help:gnome-system-monitor", NULL, &error)) {
        g_warning("Could not display help : %s", error->message);
        g_error_free(error);
    }
}

void
GsmApplication::on_lsof_activate(const Glib::VariantBase&)
{
    procman_lsof(this);
}

void
GsmApplication::on_preferences_activate(const Glib::VariantBase&)
{
    create_preferences_dialog (this);
}

void
GsmApplication::on_quit_activate(const Glib::VariantBase&)
{
    shutdown ();
}

void
GsmApplication::shutdown()
{
    save_config ();

    if (timeout)
        g_source_remove (timeout);
    if (disk_timeout)
        g_source_remove (disk_timeout);

    proctable_free_table (this);
    delete smooth_refresh;
    delete pretty_table;

    glibtop_close();

    quit();
}

void GsmApplication::on_startup()
{
    Gtk::Application::on_startup();

    Glib::RefPtr<Gio::SimpleAction> action;

    action = Gio::SimpleAction::create("quit");
    action->signal_activate().connect(sigc::mem_fun(*this, &GsmApplication::on_quit_activate));
    add_action(action);

    action = Gio::SimpleAction::create("help");
    action->signal_activate().connect(sigc::mem_fun(*this, &GsmApplication::on_help_activate));
    add_action(action);

    action = Gio::SimpleAction::create("lsof");
    action->signal_activate().connect(sigc::mem_fun(*this, &GsmApplication::on_lsof_activate));
    add_action(action);

    action = Gio::SimpleAction::create("preferences");
    action->signal_activate().connect(sigc::mem_fun(*this, &GsmApplication::on_preferences_activate));
    add_action(action);

    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_resource("/org/gnome/gnome-system-monitor/data/menus.ui");

    Glib::RefPtr<Gio::Menu> menu = Glib::RefPtr<Gio::Menu>::cast_static(builder->get_object ("app-menu"));
    set_app_menu (menu);

    add_accelerator("<Primary>d", "win.show-dependencies", NULL);
    add_accelerator("<Primary>s", "win.send-signal-stop", g_variant_new_int32 (SIGSTOP));
    add_accelerator("<Primary>c", "win.send-signal-cont", g_variant_new_int32 (SIGCONT));
    add_accelerator("<Primary>e", "win.send-signal-end", g_variant_new_int32 (SIGTERM));
    add_accelerator("<Primary>k", "win.send-signal-kill", g_variant_new_int32 (SIGKILL));
    add_accelerator("<Primary>m", "win.memory-maps", NULL);
    add_accelerator("<Primary>o", "win.open-files", NULL);
    add_accelerator("<Alt>Return", "win.process-properties", NULL);
    add_accelerator("<Primary>f", "win.search", g_variant_new_boolean (TRUE));

    Gtk::Window::set_default_icon_name ("utilities-system-monitor");

    glibtop_init ();

    load_settings ();

    pretty_table = new PrettyTable();
    smooth_refresh = new SmoothRefresh(settings);

    create_main_window (this);

    add_accelerator ("<Alt>1", "win.show-page", g_variant_new_string ("processes"));
    add_accelerator ("<Alt>2", "win.show-page", g_variant_new_string ("resources"));
    add_accelerator ("<Alt>3", "win.show-page", g_variant_new_string ("disks"));
    add_accelerator ("<Primary>r", "win.refresh", NULL);

    gtk_widget_show (main_window);
}