Debian Codegraph @

src/application.cpp

raw symbols SCIP
1 #include <config.h>
2
3 #include <glib/gi18n.h>
4 #include <glibtop.h>
5 #include <glibtop/close.h>
6 #include <glibtop/cpu.h>
7 #include <glibtop/sysinfo.h>
8 #include <adwaita.h>
9 #include <signal.h>
10 #include <stdlib.h>
11 #include <glib.h>
12 #include <sigc++/functors/functors.h>
13
14 #include "application.h"
15 #include "procdialogs.h"
16 #include "prefsdialog.h"
17 #include "interface.h"
18 #include "proctable.h"
19 #include "load-graph.h"
20 #include "settings-keys.h"
21 #include "util.h"
22 #include "lsof.h"
23 #include "disks.h"
24
25 extern "C" {
26 #include "gsm-resources.h"
27 }
28
29 static void
30 cb_solaris_mode_changed (Gio::Settings& settings,
31 Glib::ustring key,
32 GsmApplication*app)
33 {
34 app->config.solaris_mode = settings.get_boolean (key);
35 app->cpu_graph->clear_background ();
36 if (app->timeout)
37 proctable_update (app);
38 }
39
40 static void
41 cb_process_memory_in_iec_changed (Gio::Settings& settings,
42 Glib::ustring key,
43 GsmApplication*app)
44 {
45 app->config.process_memory_in_iec = settings.get_boolean (key);
46 app->cpu_graph->clear_background ();
47 if (app->timeout)
48 proctable_update (app);
49 }
50
51 static void
52 cb_logarithmic_scale_changed (Gio::Settings& settings,
53 Glib::ustring key,
54 GsmApplication*app)
55 {
56 app->config.logarithmic_scale = settings.get_boolean (key);
57 app->mem_graph->clear_background ();
58 load_graph_reset (app->mem_graph);
59 }
60
61 static void
62 cb_draw_stacked_changed (Gio::Settings& settings,
63 Glib::ustring key,
64 GsmApplication*app)
65 {
66 app->config.draw_stacked = settings.get_boolean (key);
67 app->cpu_graph->clear_background ();
68 load_graph_reset (app->cpu_graph);
69 }
70
71 static void
72 cb_draw_smooth_changed (Gio::Settings& settings,
73 Glib::ustring key,
74 GsmApplication*app)
75 {
76 app->config.draw_smooth = settings.get_boolean (key);
77 app->cpu_graph->clear_background ();
78 app->mem_graph->clear_background ();
79 app->net_graph->clear_background ();
80 app->disk_graph->clear_background ();
81 }
82
83 static void
84 cb_resources_memory_in_iec_changed (Gio::Settings& settings,
85 Glib::ustring key,
86 GsmApplication*app)
87 {
88 app->config.resources_memory_in_iec = settings.get_boolean (key);
89 app->cpu_graph->clear_background ();
90 if (app->timeout)
91 proctable_update (app);
92 }
93
94 static void
95 cb_network_in_bits_changed (Gio::Settings& settings,
96 Glib::ustring key,
97 GsmApplication*app)
98 {
99 app->config.network_in_bits = settings.get_boolean (key);
100 // force scale to be redrawn
101 app->net_graph->clear_background ();
102 }
103
104 static void
105 cb_network_total_in_bits_changed (Gio::Settings& settings,
106 Glib::ustring key,
107 GsmApplication*app)
108 {
109 app->config.network_total_in_bits = settings.get_boolean (key);
110 // force scale to be redrawn
111 app->net_graph->clear_background ();
112 }
113
114 static void
115 cb_timeouts_changed (Gio::Settings& settings,
116 Glib::ustring key,
117 GsmApplication*app)
118 {
119 if (key == GSM_SETTING_PROCESS_UPDATE_INTERVAL)
120 {
121 app->config.update_interval = settings.get_int (key);
122
123 app->smooth_refresh->reset ();
124 if (app->timeout)
125 proctable_reset_timeout (app);
126 }
127 else if (key == GSM_SETTING_GRAPH_UPDATE_INTERVAL)
128 {
129 app->config.graph_update_interval = settings.get_int (key);
130 load_graph_change_speed (app->cpu_graph,
131 app->config.graph_update_interval);
132 load_graph_change_speed (app->mem_graph,
133 app->config.graph_update_interval);
134 load_graph_change_speed (app->net_graph,
135 app->config.graph_update_interval);
136 load_graph_change_speed (app->disk_graph,
137 app->config.graph_update_interval);
138 }
139 }
140
141 static void
142 cb_data_points_changed (Gio::Settings& settings,
143 Glib::ustring key,
144 GsmApplication*app)
145 {
146 app->config.graph_data_points = settings.get_int (key);
147 unsigned points = app->config.graph_data_points + 2;
148
149 load_graph_change_num_points (app->cpu_graph, points);
150 load_graph_change_num_points (app->mem_graph, points);
151 load_graph_change_num_points (app->net_graph, points);
152 load_graph_change_num_points (app->disk_graph, points);
153 }
154
155 static void
156 apply_cpu_color_settings (Gio::Settings& settings,
157 GsmApplication*app)
158 {
159 GVariant *cpu_colors_var = g_settings_get_value (settings.gobj (), GSM_SETTING_CPU_COLORS);
160 gsize n = g_variant_n_children (cpu_colors_var);
161
162 gchar *color;
163
164 // Create builder to add the new colors if user has more than the number of cores with defaults.
165 GVariantBuilder builder;
166 GVariant*child;
167 GVariant*full;
168
169 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
170
171 std::vector<std::string> random_colors = procman::generate_colors (app->config.num_cpus);
172
173 for (guint i = 0; i < static_cast<guint>(app->config.num_cpus); i++)
174 {
175 if (i < n)
176 {
177 child = g_variant_get_child_value (cpu_colors_var, i);
178 g_variant_get_child (child, 1, "s", &color);
179 g_variant_builder_add_value (&builder, child);
180 g_variant_unref (child);
181 }
182 else
183 {
184 color = g_strdup (random_colors[i].c_str ());
185 g_variant_builder_add (&builder, "(us)", i, color);
186 }
187 gdk_rgba_parse (&app->config.cpu_color[i], color);
188 g_free (color);
189 }
190 full = g_variant_builder_end (&builder);
191 // if the user has more cores than colors stored in the gsettings, store the newly built gvariant in gsettings
192 if (n < static_cast<guint>(app->config.num_cpus))
193 g_settings_set_value (settings.gobj (), GSM_SETTING_CPU_COLORS, full);
194 else
195 g_variant_unref (full);
196
197 g_variant_unref (cpu_colors_var);
198 }
199
200 static void
201 cb_color_changed (Gio::Settings& settings,
202 Glib::ustring key,
203 GsmApplication*app)
204 {
205 if (key == GSM_SETTING_CPU_COLORS)
206 {
207 apply_cpu_color_settings (settings, app);
208 for (int i = 0; i < app->config.num_cpus; i++)
209 if (!gdk_rgba_equal (&app->cpu_graph->colors[i], &app->config.cpu_color[i]))
210 {
211 app->cpu_graph->colors[i] = app->config.cpu_color[i];
212 break;
213 }
214 return;
215 }
216
217 auto color = settings.get_string (key);
218
219 if (key == GSM_SETTING_MEM_COLOR)
220 {
221 gdk_rgba_parse (&app->config.mem_color, color.c_str ());
222 app->mem_graph->colors.at (0) = app->config.mem_color;
223 }
224 else if (key == GSM_SETTING_SWAP_COLOR)
225 {
226 gdk_rgba_parse (&app->config.swap_color, color.c_str ());
227 app->mem_graph->colors.at (1) = app->config.swap_color;
228 }
229 else if (key == GSM_SETTING_NET_IN_COLOR)
230 {
231 gdk_rgba_parse (&app->config.net_in_color, color.c_str ());
232 app->net_graph->colors.at (0) = app->config.net_in_color;
233 }
234 else if (key == GSM_SETTING_NET_OUT_COLOR)
235 {
236 gdk_rgba_parse (&app->config.net_out_color, color.c_str ());
237 app->net_graph->colors.at (1) = app->config.net_out_color;
238 }
239 else if (key == GSM_SETTING_DISK_READ_COLOR)
240 {
241 gdk_rgba_parse (&app->config.disk_read_color, color.c_str ());
242 app->disk_graph->colors.at (0) = app->config.disk_read_color;
243 }
244 else if (key == GSM_SETTING_DISK_WRITE_COLOR)
245 {
246 gdk_rgba_parse (&app->config.disk_write_color, color.c_str ());
247 app->disk_graph->colors.at (1) = app->config.disk_write_color;
248 }
249 }
250
251 void
252 GsmApplication::load_settings ()
253 {
254 glibtop_cpu cpu;
255
256 this->settings = Gio::Settings::create (GSM_GSETTINGS_SCHEMA);
257
258 config.current_tab = this->settings->get_string (GSM_SETTING_CURRENT_TAB);
259
260 config.solaris_mode = this->settings->get_boolean (GSM_SETTING_SOLARIS_MODE);
261 this->settings->signal_changed (GSM_SETTING_SOLARIS_MODE).connect ([this](const Glib::ustring&key) {
262 cb_solaris_mode_changed (*this->settings.operator-> (), key, this);
263 });
264
265 config.process_memory_in_iec = this->settings->get_boolean (GSM_SETTING_PROCESS_MEMORY_IN_IEC);
266 this->settings->signal_changed (GSM_SETTING_PROCESS_MEMORY_IN_IEC).connect ([this](const Glib::ustring&key) {
267 cb_process_memory_in_iec_changed (*this->settings.operator-> (), key, this);
268 });
269
270 config.logarithmic_scale = this->settings->get_boolean (GSM_SETTING_LOGARITHMIC_SCALE);
271 this->settings->signal_changed (GSM_SETTING_LOGARITHMIC_SCALE).connect ([this](const Glib::ustring&key) {
272 cb_logarithmic_scale_changed (*this->settings.operator-> (), key, this);
273 });
274
275 config.draw_stacked = this->settings->get_boolean (GSM_SETTING_DRAW_STACKED);
276 this->settings->signal_changed (GSM_SETTING_DRAW_STACKED).connect ([this](const Glib::ustring&key) {
277 cb_draw_stacked_changed (*this->settings.operator-> (), key, this);
278 });
279
280 config.draw_smooth = this->settings->get_boolean (GSM_SETTING_DRAW_SMOOTH);
281 this->settings->signal_changed (GSM_SETTING_DRAW_SMOOTH).connect ([this](const Glib::ustring&key) {
282 cb_draw_smooth_changed (*this->settings.operator-> (), key, this);
283 });
284
285 config.resources_memory_in_iec = this->settings->get_boolean (GSM_SETTING_RESOURCES_MEMORY_IN_IEC);
286 this->settings->signal_changed (GSM_SETTING_RESOURCES_MEMORY_IN_IEC).connect ([this](const Glib::ustring&key) {
287 cb_resources_memory_in_iec_changed (*this->settings.operator-> (), key, this);
288 });
289
290 config.network_in_bits = this->settings->get_boolean (GSM_SETTING_NETWORK_IN_BITS);
291 this->settings->signal_changed (GSM_SETTING_NETWORK_IN_BITS).connect ([this](const Glib::ustring&key) {
292 cb_network_in_bits_changed (*this->settings.operator-> (), key, this);
293 });
294
295 config.network_total_in_bits = this->settings->get_boolean (GSM_SETTING_NETWORK_TOTAL_IN_BITS);
296 this->settings->signal_changed (GSM_SETTING_NETWORK_TOTAL_IN_BITS).connect ([this](const Glib::ustring&key) {
297 cb_network_total_in_bits_changed (*this->settings.operator-> (), key, this);
298 });
299
300 auto cbtc = [this](const Glib::ustring&key) {
301 cb_timeouts_changed (*this->settings.operator-> (), key, this);
302 };
303
304 config.update_interval = this->settings->get_int (GSM_SETTING_PROCESS_UPDATE_INTERVAL);
305 this->settings->signal_changed (GSM_SETTING_PROCESS_UPDATE_INTERVAL).connect (cbtc);
306 config.graph_update_interval = this->settings->get_int (GSM_SETTING_GRAPH_UPDATE_INTERVAL);
307 this->settings->signal_changed (GSM_SETTING_GRAPH_UPDATE_INTERVAL).connect (cbtc);
308
309 config.graph_data_points = this->settings->get_int (GSM_SETTING_GRAPH_DATA_POINTS);
310 this->settings->signal_changed (GSM_SETTING_GRAPH_DATA_POINTS).connect ([this](const Glib::ustring&key) {
311 cb_data_points_changed (*this->settings.operator-> (), key, this);
312 });
313
314 glibtop_get_cpu (&cpu);
315 frequency = cpu.frequency;
316
317 config.num_cpus = glibtop_get_sysinfo ()->ncpu; // or server->ncpu + 1
318
319 apply_cpu_color_settings (*this->settings.operator-> (), this);
320
321 auto mem_color = this->settings->get_string (GSM_SETTING_MEM_COLOR);
322
323 gdk_rgba_parse (&config.mem_color, mem_color.empty () ? "#000000ff0082" : mem_color.c_str ());
324
325 auto swap_color = this->settings->get_string (GSM_SETTING_SWAP_COLOR);
326
327 gdk_rgba_parse (&config.swap_color, swap_color.empty () ? "#00b6000000ff" : swap_color.c_str ());
328
329 auto net_in_color = this->settings->get_string (GSM_SETTING_NET_IN_COLOR);
330
331 gdk_rgba_parse (&config.net_in_color, net_in_color.empty () ? "#000000f200f2" : net_in_color.c_str ());
332
333 auto net_out_color = this->settings->get_string (GSM_SETTING_NET_OUT_COLOR);
334
335 gdk_rgba_parse (&config.net_out_color, net_out_color.empty () ? "#00f2000000c1" : net_out_color.c_str ());
336
337 auto disk_read_color = this->settings->get_string (GSM_SETTING_DISK_READ_COLOR);
338
339 gdk_rgba_parse (&config.disk_read_color, disk_read_color.empty () ? "#000000f200f2" : disk_read_color.c_str ());
340
341 auto disk_write_color = this->settings->get_string (GSM_SETTING_DISK_WRITE_COLOR);
342
343 gdk_rgba_parse (&config.disk_write_color, disk_write_color.empty () ? "#00f2000000c1" : disk_write_color.c_str ());
344
345
346 auto cbcc = [this](const Glib::ustring&key) {
347 cb_color_changed (*this->settings.operator-> (), key, this);
348 };
349
350 for (auto k : { GSM_SETTING_CPU_COLORS, GSM_SETTING_MEM_COLOR, GSM_SETTING_SWAP_COLOR, GSM_SETTING_NET_IN_COLOR, GSM_SETTING_NET_OUT_COLOR, GSM_SETTING_DISK_READ_COLOR, GSM_SETTING_DISK_WRITE_COLOR })
351 this->settings->signal_changed (k).connect (cbcc);
352 }
353
354
355 GsmApplication::GsmApplication()
356 : Gtk::Application (APP_ID, Gio::Application::Flags::HANDLES_COMMAND_LINE),
357 settings (NULL),
358 main_window (NULL),
359 stack (NULL),
360 app_menu_button (NULL),
361
362 config (),
363 pretty_table (NULL),
364 tree (NULL),
365 proc_actionbar_revealer (NULL),
366 proc_popover_menu (NULL),
367 refresh_button (NULL),
368 end_process_button (NULL),
369 search_button (NULL),
370 search_entry (NULL),
371 search_bar (NULL),
372 top_of_tree (NULL),
373
374 last_vscroll_max (0.0),
375 last_vscroll_value (0.0),
376 timeout (0U),
377 cpu_total_time (0ULL),
378 cpu_total_time_last (0ULL),
379 frequency (0U),
380
381 cpu_graph (NULL),
382 mem_graph (NULL),
383 net_graph (NULL),
384 disk_graph (NULL),
385
386 disk_list (NULL),
387
388 selection (NULL),
389
390 smooth_refresh (NULL)
391 {
392 Glib::set_application_name (_("System Monitor"));
393 this->set_version (VERSION);
394 this->set_option_context_summary (_("A simple process and system monitor."));
395 this->add_main_option_entry (OptionType::BOOL,
396 "show-processes-tab",
397 'p',
398 _("Show the Processes tab"));
399 this->add_main_option_entry (OptionType::BOOL,
400 "show-resources-tab",
401 'r',
402 _("Show the Resources tab"));
403 this->add_main_option_entry (OptionType::BOOL,
404 "show-file-systems-tab",
405 'f',
406 _("Show the File Systems tab"));
407 }
408
409 GsmApplication&
410 GsmApplication::get ()
411 {
412 static GsmApplication singleton;
413 return singleton;
414 }
415
416 void
417 GsmApplication::on_activate ()
418 {
419 if (main_window == NULL)
420 create_main_window (this);
421
422 gtk_window_present (GTK_WINDOW (main_window));
423 }
424
425
426 int
427 GsmApplication::on_command_line (const Glib::RefPtr<Gio::ApplicationCommandLine>&command_line)
428 {
429 auto args = command_line->get_options_dict ();
430
431 const char*tab = NULL;
432 if (args->contains ("show-processes-tab")) {
433 tab = "processes";
434 } else if (args->contains ("show-resources-tab")) {
435 tab = "resources";
436 } else if (args->contains ("show-file-systems-tab")) {
437 tab = "disks";
438 }
439
440 on_activate ();
441
442 if (tab)
443 adw_view_stack_set_visible_child_name (this->stack, tab);
444
445 return 0;
446 }
447
448 void
449 GsmApplication::on_help_activate (const Glib::VariantBase&)
450 {
451 GError*error = 0;
452
453 if (!g_app_info_launch_default_for_uri ("help:gnome-system-monitor", NULL, &error))
454 {
455 g_warning ("Could not display help : %s", error->message);
456 g_error_free (error);
457 }
458 }
459
460
461 void
462 GsmApplication::on_lsof_activate (const Glib::VariantBase&)
463 {
464 GsmLsof *window = gsm_lsof_new (GTK_WINDOW (this->main_window));
465
466 gtk_window_present (GTK_WINDOW (window));
467 }
468
469
470 void
471 GsmApplication::on_preferences_activate (const Glib::VariantBase&)
472 {
473 create_preferences_dialog (this);
474 }
475
476 void
477 GsmApplication::on_quit_activate (const Glib::VariantBase&)
478 {
479 shutdown ();
480 }
481
482 void
483 GsmApplication::shutdown ()
484 {
485 if (timeout)
486 g_source_remove (timeout);
487
488 proctable_free_table (this);
489 delete smooth_refresh;
490 delete pretty_table;
491
492 glibtop_close ();
493
494 quit ();
495 }
496
497 void
498 GsmApplication::on_startup ()
499 {
500 AdwStyleManager *style_manager;
501 GtkCssProvider *css_provider;
502
503 g_resources_register (gsm_get_resource ());
504
505 Gtk::Application::on_startup ();
506
507 glibtop_init ();
508
509 style_manager = adw_style_manager_get_default ();
510 adw_style_manager_set_color_scheme (style_manager, ADW_COLOR_SCHEME_PREFER_LIGHT);
511
512 css_provider = gtk_css_provider_new ();
513 gtk_css_provider_load_from_resource (css_provider, "/org/gnome/gnome-system-monitor/style.css");
514 gtk_style_context_add_provider_for_display (gdk_display_get_default (),
515 GTK_STYLE_PROVIDER (css_provider),
516 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
517
518 Gtk::Window::set_default_icon_name (APP_ID);
519
520 using SignalFunction = void (GsmApplication::*)(const Glib::VariantBase&);
521 auto make_action = [&](const Glib::ustring& name, const SignalFunction function) {
522 auto action = Gio::SimpleAction::create (name);
523 action->signal_activate ().connect (sigc::mem_fun (*this, function));
524 return action;
525 };
526
527 add_action (make_action("quit", &GsmApplication::on_quit_activate));
528 add_action (make_action("help", &GsmApplication::on_help_activate));
529 add_action (make_action("lsof", &GsmApplication::on_lsof_activate));
530 add_action (make_action("preferences", &GsmApplication::on_preferences_activate));
531
532 Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_resource ("/org/gnome/gnome-system-monitor/data/menus.ui");
533
534 set_accel_for_action ("win.show-dependencies", "<Primary>d");
535 set_accel_for_action ("app.quit", "<Primary>q");
536 set_accel_for_action ("window.close", "<Primary>w");
537 set_accel_for_action ("win.send-signal-stop", "<Primary>s");
538 set_accel_for_action ("win.send-signal-cont", "<Primary>c");
539 set_accel_for_action ("win.send-signal-term", "<Primary>t");
540 set_accel_for_action ("win.send-signal-kill", "<Primary>k");
541 set_accel_for_action ("win.set-affinity", "<Alt>s");
542 set_accel_for_action ("win.memory-maps", "<Control>m");
543 set_accel_for_action ("win.open-files", "<Control>o");
544 set_accel_for_action ("win.process-properties", "<Alt>Return");
545 set_accel_for_action ("win.search", "<Control>f");
546 set_accel_for_action ("app.help", "F1");
547 set_accel_for_action ("win.shortcuts", "<Control>question");
548 set_accel_for_action ("app.preferences", "<Primary>comma");
549 set_accel_for_action ("win.show-page('resources')", "<Alt>1");
550 set_accel_for_action ("win.show-page('processes')", "<Alt>2");
551 set_accel_for_action ("win.show-page('disks')", "<Alt>3");
552 set_accel_for_action ("win.refresh", "<Control>r");
553
554 load_settings ();
555
556 pretty_table = new PrettyTable ();
557 smooth_refresh = new SmoothRefresh (settings);
558 }