| 
									
										
										
										
											2019-08-18 16:18:20 +03:00
										 |  |  | #include "DevicesModel.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  | #include "GraphWidget.h"
 | 
					
						
							|  |  |  | #include "MemoryStatsWidget.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 11:56:30 +02:00
										 |  |  | #include "NetworkStatisticsWidget.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-03 08:26:04 +02:00
										 |  |  | #include "ProcessFileDescriptorMapWidget.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-28 12:15:24 +02:00
										 |  |  | #include "ProcessMemoryMapWidget.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-27 09:39:43 +02:00
										 |  |  | #include "ProcessStacksWidget.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  | #include "ProcessTableView.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:31 +02:00
										 |  |  | #include <LibCore/CTimer.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-28 10:18:49 +02:00
										 |  |  | #include <LibDraw/PNGLoader.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  | #include <LibGUI/GAction.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | #include <LibGUI/GApplication.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  | #include <LibGUI/GBoxLayout.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-06 03:21:23 +02:00
										 |  |  | #include <LibGUI/GGroupBox.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-11 10:11:21 +02:00
										 |  |  | #include <LibGUI/GJsonArrayModel.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | #include <LibGUI/GLabel.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  | #include <LibGUI/GMenuBar.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-14 20:38:18 +02:00
										 |  |  | #include <LibGUI/GPainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-12 11:56:30 +02:00
										 |  |  | #include <LibGUI/GSortingProxyModel.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-27 09:39:43 +02:00
										 |  |  | #include <LibGUI/GSplitter.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  | #include <LibGUI/GTabWidget.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GToolBar.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GWidget.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GWindow.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-14 14:50:25 +03:00
										 |  |  | #include <LibPCIDB/Database.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | #include <signal.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 10:11:21 +02:00
										 |  |  | static String human_readable_size(u32 size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (size < (64 * KB)) | 
					
						
							|  |  |  |         return String::format("%u", size); | 
					
						
							|  |  |  |     if (size < MB) | 
					
						
							|  |  |  |         return String::format("%u KB", size / KB); | 
					
						
							|  |  |  |     if (size < GB) | 
					
						
							|  |  |  |         return String::format("%u MB", size / MB); | 
					
						
							|  |  |  |     return String::format("%u GB", size / GB); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static GWidget* build_file_systems_tab(); | 
					
						
							| 
									
										
										
										
											2019-08-14 14:50:25 +03:00
										 |  |  | static GWidget* build_pci_devices_tab(); | 
					
						
							| 
									
										
										
										
											2019-08-18 16:18:20 +03:00
										 |  |  | static GWidget* build_devices_tab(); | 
					
						
							| 
									
										
										
										
											2019-08-11 10:11:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | int main(int argc, char** argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GApplication app(argc, argv); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 20:53:04 +02:00
										 |  |  |     auto* keeper = new GWidget; | 
					
						
							|  |  |  |     keeper->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     keeper->set_fill_with_background_color(true); | 
					
						
							| 
									
										
										
										
											2019-06-30 09:23:16 +02:00
										 |  |  |     keeper->set_background_color(Color::WarmGray); | 
					
						
							| 
									
										
										
										
											2019-05-10 22:59:39 +02:00
										 |  |  |     keeper->layout()->set_margins({ 4, 4, 4, 4 }); | 
					
						
							| 
									
										
										
										
											2019-05-05 20:53:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 11:11:49 +02:00
										 |  |  |     auto* tabwidget = new GTabWidget(keeper); | 
					
						
							| 
									
										
										
										
											2019-07-27 09:39:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 11:11:49 +02:00
										 |  |  |     auto* process_container_splitter = new GSplitter(Orientation::Vertical, nullptr); | 
					
						
							|  |  |  |     tabwidget->add_widget("Processes", process_container_splitter); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 11:11:49 +02:00
										 |  |  |     auto* process_table_container = new GWidget(process_container_splitter); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 03:21:23 +02:00
										 |  |  |     auto* graphs_container = new GWidget; | 
					
						
							|  |  |  |     graphs_container->set_fill_with_background_color(true); | 
					
						
							| 
									
										
										
										
											2019-06-30 09:23:16 +02:00
										 |  |  |     graphs_container->set_background_color(Color::WarmGray); | 
					
						
							| 
									
										
										
										
											2019-05-06 03:21:23 +02:00
										 |  |  |     graphs_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     graphs_container->layout()->set_margins({ 4, 4, 4, 4 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto* cpu_graph_group_box = new GGroupBox("CPU usage", graphs_container); | 
					
						
							|  |  |  |     cpu_graph_group_box->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     cpu_graph_group_box->layout()->set_margins({ 6, 16, 6, 6 }); | 
					
						
							|  |  |  |     cpu_graph_group_box->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     cpu_graph_group_box->set_preferred_size(0, 120); | 
					
						
							| 
									
										
										
										
											2019-05-06 03:21:23 +02:00
										 |  |  |     auto* cpu_graph = new GraphWidget(cpu_graph_group_box); | 
					
						
							|  |  |  |     cpu_graph->set_max(100); | 
					
						
							|  |  |  |     cpu_graph->set_text_color(Color::Green); | 
					
						
							|  |  |  |     cpu_graph->set_graph_color(Color::from_rgb(0x00bb00)); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     cpu_graph->text_formatter = [](int value, int) { | 
					
						
							| 
									
										
										
										
											2019-05-06 03:21:23 +02:00
										 |  |  |         return String::format("%d%%", value); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto* memory_graph_group_box = new GGroupBox("Memory usage", graphs_container); | 
					
						
							|  |  |  |     memory_graph_group_box->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							| 
									
										
										
										
											2019-05-06 03:37:22 +02:00
										 |  |  |     memory_graph_group_box->layout()->set_margins({ 6, 16, 6, 6 }); | 
					
						
							| 
									
										
										
										
											2019-05-06 03:21:23 +02:00
										 |  |  |     memory_graph_group_box->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); | 
					
						
							| 
									
										
										
										
											2019-07-20 22:39:24 +02:00
										 |  |  |     memory_graph_group_box->set_preferred_size(0, 120); | 
					
						
							| 
									
										
										
										
											2019-05-06 03:21:23 +02:00
										 |  |  |     auto* memory_graph = new GraphWidget(memory_graph_group_box); | 
					
						
							|  |  |  |     memory_graph->set_text_color(Color::Cyan); | 
					
						
							|  |  |  |     memory_graph->set_graph_color(Color::from_rgb(0x00bbbb)); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     memory_graph->text_formatter = [](int value, int max) { | 
					
						
							| 
									
										
										
										
											2019-05-06 03:21:23 +02:00
										 |  |  |         return String::format("%d / %d KB", value, max); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-05-05 14:39:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 17:11:48 +02:00
										 |  |  |     tabwidget->add_widget("Graphs", graphs_container); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 10:11:21 +02:00
										 |  |  |     tabwidget->add_widget("File systems", build_file_systems_tab()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 14:50:25 +03:00
										 |  |  |     tabwidget->add_widget("PCI devices", build_pci_devices_tab()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 16:18:20 +03:00
										 |  |  |     tabwidget->add_widget("Devices", build_devices_tab()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-08 20:43:30 +02:00
										 |  |  |     auto* network_stats_widget = new NetworkStatisticsWidget(nullptr); | 
					
						
							|  |  |  |     tabwidget->add_widget("Network", network_stats_widget); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 08:42:15 +02:00
										 |  |  |     process_table_container->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     process_table_container->layout()->set_margins({ 4, 0, 4, 4 }); | 
					
						
							|  |  |  |     process_table_container->layout()->set_spacing(0); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 08:42:15 +02:00
										 |  |  |     auto* toolbar = new GToolBar(process_table_container); | 
					
						
							| 
									
										
										
										
											2019-05-10 22:59:39 +02:00
										 |  |  |     toolbar->set_has_frame(false); | 
					
						
							| 
									
										
										
										
											2019-07-27 08:42:15 +02:00
										 |  |  |     auto* process_table_view = new ProcessTableView(*cpu_graph, process_table_container); | 
					
						
							| 
									
										
										
										
											2019-07-27 08:43:32 +02:00
										 |  |  |     auto* memory_stats_widget = new MemoryStatsWidget(*memory_graph, graphs_container); | 
					
						
							| 
									
										
										
										
											2019-03-10 12:13:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:31 +02:00
										 |  |  |     auto* refresh_timer = new CTimer(1000, [&] { | 
					
						
							|  |  |  |         process_table_view->refresh(); | 
					
						
							|  |  |  |         memory_stats_widget->refresh(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     auto kill_action = GAction::create("Kill process", GraphicsBitmap::load_from_file("/res/icons/kill16.png"), [process_table_view](const GAction&) { | 
					
						
							| 
									
										
										
										
											2019-02-28 10:57:09 +01:00
										 |  |  |         pid_t pid = process_table_view->selected_pid(); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |         if (pid != -1) | 
					
						
							|  |  |  |             kill(pid, SIGKILL); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-02-28 12:06:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     auto stop_action = GAction::create("Stop process", GraphicsBitmap::load_from_file("/res/icons/stop16.png"), [process_table_view](const GAction&) { | 
					
						
							| 
									
										
										
										
											2019-02-28 12:06:19 +01:00
										 |  |  |         pid_t pid = process_table_view->selected_pid(); | 
					
						
							|  |  |  |         if (pid != -1) | 
					
						
							|  |  |  |             kill(pid, SIGSTOP); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     auto continue_action = GAction::create("Continue process", GraphicsBitmap::load_from_file("/res/icons/continue16.png"), [process_table_view](const GAction&) { | 
					
						
							| 
									
										
										
										
											2019-02-28 13:02:55 +01:00
										 |  |  |         pid_t pid = process_table_view->selected_pid(); | 
					
						
							|  |  |  |         if (pid != -1) | 
					
						
							|  |  |  |             kill(pid, SIGCONT); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-11 16:04:17 +02:00
										 |  |  |     toolbar->add_action(kill_action); | 
					
						
							|  |  |  |     toolbar->add_action(stop_action); | 
					
						
							|  |  |  |     toolbar->add_action(continue_action); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto menubar = make<GMenuBar>(); | 
					
						
							| 
									
										
										
										
											2019-08-14 14:06:43 +03:00
										 |  |  |     auto app_menu = make<GMenu>("System Monitor"); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [](const GAction&) { | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |         GApplication::the().quit(0); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |     menubar->add_menu(move(app_menu)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 13:02:55 +01:00
										 |  |  |     auto process_menu = make<GMenu>("Process"); | 
					
						
							| 
									
										
										
										
											2019-06-30 08:15:18 +02:00
										 |  |  |     process_menu->add_action(kill_action); | 
					
						
							|  |  |  |     process_menu->add_action(stop_action); | 
					
						
							|  |  |  |     process_menu->add_action(continue_action); | 
					
						
							| 
									
										
										
										
											2019-02-28 13:02:55 +01:00
										 |  |  |     menubar->add_menu(move(process_menu)); | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-30 08:15:18 +02:00
										 |  |  |     auto process_context_menu = make<GMenu>("Process context menu"); | 
					
						
							|  |  |  |     process_context_menu->add_action(kill_action); | 
					
						
							|  |  |  |     process_context_menu->add_action(stop_action); | 
					
						
							|  |  |  |     process_context_menu->add_action(continue_action); | 
					
						
							|  |  |  |     process_table_view->on_context_menu_request = [&](const GModelIndex& index, const GContextMenuEvent& event) { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:20:00 +02:00
										 |  |  |         (void)index; | 
					
						
							| 
									
										
										
										
											2019-06-30 08:15:18 +02:00
										 |  |  |         process_context_menu->popup(event.screen_position()); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:31 +02:00
										 |  |  |     auto frequency_menu = make<GMenu>("Frequency"); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     frequency_menu->add_action(GAction::create("0.25 sec", [refresh_timer](auto&) { | 
					
						
							| 
									
										
										
										
											2019-05-06 03:21:23 +02:00
										 |  |  |         refresh_timer->restart(250); | 
					
						
							|  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     frequency_menu->add_action(GAction::create("0.5 sec", [refresh_timer](auto&) { | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:31 +02:00
										 |  |  |         refresh_timer->restart(500); | 
					
						
							|  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     frequency_menu->add_action(GAction::create("1 sec", [refresh_timer](auto&) { | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:31 +02:00
										 |  |  |         refresh_timer->restart(1000); | 
					
						
							|  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     frequency_menu->add_action(GAction::create("3 sec", [refresh_timer](auto&) { | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:31 +02:00
										 |  |  |         refresh_timer->restart(3000); | 
					
						
							|  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     frequency_menu->add_action(GAction::create("5 sec", [refresh_timer](auto&) { | 
					
						
							| 
									
										
										
										
											2019-04-18 04:38:31 +02:00
										 |  |  |         refresh_timer->restart(5000); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |     menubar->add_menu(move(frequency_menu)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |     auto help_menu = make<GMenu>("Help"); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:48:03 +02:00
										 |  |  |     help_menu->add_action(GAction::create("About", [](const GAction&) { | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |         dbgprintf("FIXME: Implement Help/About\n"); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |     menubar->add_menu(move(help_menu)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app.set_menubar(move(menubar)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 11:11:49 +02:00
										 |  |  |     auto* process_tab_widget = new GTabWidget(process_container_splitter); | 
					
						
							| 
									
										
										
										
											2019-07-28 12:15:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-03 08:26:04 +02:00
										 |  |  |     auto* open_files_widget = new ProcessFileDescriptorMapWidget(nullptr); | 
					
						
							|  |  |  |     process_tab_widget->add_widget("Open files", open_files_widget); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-28 12:15:24 +02:00
										 |  |  |     auto* memory_map_widget = new ProcessMemoryMapWidget(nullptr); | 
					
						
							|  |  |  |     process_tab_widget->add_widget("Memory map", memory_map_widget); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 09:39:43 +02:00
										 |  |  |     auto* stacks_widget = new ProcessStacksWidget(nullptr); | 
					
						
							|  |  |  |     process_tab_widget->add_widget("Stacks", stacks_widget); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     process_table_view->on_process_selected = [&](pid_t pid) { | 
					
						
							| 
									
										
										
										
											2019-08-03 08:26:04 +02:00
										 |  |  |         open_files_widget->set_pid(pid); | 
					
						
							| 
									
										
										
										
											2019-07-27 09:39:43 +02:00
										 |  |  |         stacks_widget->set_pid(pid); | 
					
						
							| 
									
										
										
										
											2019-07-28 12:15:24 +02:00
										 |  |  |         memory_map_widget->set_pid(pid); | 
					
						
							| 
									
										
										
										
											2019-07-27 09:39:43 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |     auto* window = new GWindow; | 
					
						
							| 
									
										
										
										
											2019-08-14 14:06:43 +03:00
										 |  |  |     window->set_title("System Monitor"); | 
					
						
							| 
									
										
										
										
											2019-04-17 14:48:55 +02:00
										 |  |  |     window->set_rect(20, 200, 680, 400); | 
					
						
							| 
									
										
										
										
											2019-05-05 20:53:04 +02:00
										 |  |  |     window->set_main_widget(keeper); | 
					
						
							| 
									
										
										
										
											2019-07-23 18:20:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |     window->show(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 14:06:43 +03:00
										 |  |  |     window->set_icon(load_png("/res/icons/16x16/app-system-monitor.png")); | 
					
						
							| 
									
										
										
										
											2019-04-16 17:55:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 01:43:50 +01:00
										 |  |  |     return app.exec(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-11 10:11:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 20:38:18 +02:00
										 |  |  | class ProgressBarPaintingDelegate final : public GTableCellPaintingDelegate { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     virtual ~ProgressBarPaintingDelegate() override {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void paint(GPainter& painter, const Rect& a_rect, const GModel& model, const GModelIndex& index) override | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto rect = a_rect.shrunken(2, 2); | 
					
						
							|  |  |  |         auto percentage = model.data(index, GModel::Role::Custom).to_int(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto data = model.data(index, GModel::Role::Display); | 
					
						
							|  |  |  |         String text; | 
					
						
							|  |  |  |         if (data.is_string()) | 
					
						
							|  |  |  |             text = data.as_string(); | 
					
						
							|  |  |  |         StylePainter::paint_progress_bar(painter, rect, 0, 100, percentage, text); | 
					
						
							|  |  |  |         painter.draw_rect(rect, Color::Black); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 10:11:21 +02:00
										 |  |  | GWidget* build_file_systems_tab() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* fs_widget = new GWidget(nullptr); | 
					
						
							|  |  |  |     fs_widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     fs_widget->layout()->set_margins({ 4, 4, 4, 4 }); | 
					
						
							|  |  |  |     auto* fs_table_view = new GTableView(fs_widget); | 
					
						
							|  |  |  |     fs_table_view->set_size_columns_to_fit_content(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Vector<GJsonArrayModel::FieldSpec> df_fields; | 
					
						
							|  |  |  |     df_fields.empend("mount_point", "Mount point", TextAlignment::CenterLeft); | 
					
						
							|  |  |  |     df_fields.empend("class_name", "Class", TextAlignment::CenterLeft); | 
					
						
							| 
									
										
										
										
											2019-08-12 11:56:30 +02:00
										 |  |  |     df_fields.empend( | 
					
						
							|  |  |  |         "Size", TextAlignment::CenterRight, | 
					
						
							|  |  |  |         [](const JsonObject& object) { | 
					
						
							|  |  |  |             return human_readable_size(object.get("total_block_count").to_u32() * object.get("block_size").to_u32()); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         [](const JsonObject& object) { | 
					
						
							|  |  |  |             return object.get("total_block_count").to_u32() * object.get("block_size").to_u32(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     df_fields.empend( | 
					
						
							|  |  |  |         "Used", TextAlignment::CenterRight, | 
					
						
							|  |  |  |         [](const JsonObject& object) { | 
					
						
							|  |  |  |             auto total_blocks = object.get("total_block_count").to_u32(); | 
					
						
							|  |  |  |             auto free_blocks = object.get("free_block_count").to_u32(); | 
					
						
							|  |  |  |             auto used_blocks = total_blocks - free_blocks; | 
					
						
							|  |  |  |             return human_readable_size(used_blocks * object.get("block_size").to_u32()); }, | 
					
						
							|  |  |  |         [](const JsonObject& object) { | 
					
						
							|  |  |  |             auto total_blocks = object.get("total_block_count").to_u32(); | 
					
						
							|  |  |  |             auto free_blocks = object.get("free_block_count").to_u32(); | 
					
						
							|  |  |  |             auto used_blocks = total_blocks - free_blocks; | 
					
						
							|  |  |  |             return used_blocks * object.get("block_size").to_u32(); | 
					
						
							| 
									
										
										
										
											2019-08-14 20:38:18 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |         [](const JsonObject& object) { | 
					
						
							|  |  |  |             auto total_blocks = object.get("total_block_count").to_u32(); | 
					
						
							|  |  |  |             if (total_blocks == 0) | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             auto free_blocks = object.get("free_block_count").to_u32(); | 
					
						
							|  |  |  |             auto used_blocks = total_blocks - free_blocks; | 
					
						
							|  |  |  |             int percentage = (int)((float)used_blocks / (float)total_blocks * 100.0f); | 
					
						
							|  |  |  |             return percentage; | 
					
						
							| 
									
										
										
										
											2019-08-12 11:56:30 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  |     df_fields.empend( | 
					
						
							|  |  |  |         "Available", TextAlignment::CenterRight, | 
					
						
							|  |  |  |         [](const JsonObject& object) { | 
					
						
							|  |  |  |             return human_readable_size(object.get("free_block_count").to_u32() * object.get("block_size").to_u32()); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         [](const JsonObject& object) { | 
					
						
							|  |  |  |             return object.get("free_block_count").to_u32() * object.get("block_size").to_u32(); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-08-11 10:11:21 +02:00
										 |  |  |     df_fields.empend("Access", TextAlignment::CenterLeft, [](const JsonObject& object) { | 
					
						
							|  |  |  |         return object.get("readonly").to_bool() ? "Read-only" : "Read/Write"; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     df_fields.empend("free_block_count", "Free blocks", TextAlignment::CenterRight); | 
					
						
							|  |  |  |     df_fields.empend("total_block_count", "Total blocks", TextAlignment::CenterRight); | 
					
						
							|  |  |  |     df_fields.empend("free_inode_count", "Free inodes", TextAlignment::CenterRight); | 
					
						
							|  |  |  |     df_fields.empend("total_inode_count", "Total inodes", TextAlignment::CenterRight); | 
					
						
							|  |  |  |     df_fields.empend("block_size", "Block size", TextAlignment::CenterRight); | 
					
						
							| 
									
										
										
										
											2019-08-12 11:56:30 +02:00
										 |  |  |     fs_table_view->set_model(GSortingProxyModel::create(GJsonArrayModel::create("/proc/df", move(df_fields)))); | 
					
						
							| 
									
										
										
										
											2019-08-14 20:38:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fs_table_view->set_cell_painting_delegate(3, make<ProgressBarPaintingDelegate>()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-11 10:11:21 +02:00
										 |  |  |     fs_table_view->model()->update(); | 
					
						
							|  |  |  |     return fs_widget; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-14 14:50:25 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | GWidget* build_pci_devices_tab() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* pci_widget = new GWidget(nullptr); | 
					
						
							|  |  |  |     pci_widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     pci_widget->layout()->set_margins({ 4, 4, 4, 4 }); | 
					
						
							|  |  |  |     auto* pci_table_view = new GTableView(pci_widget); | 
					
						
							|  |  |  |     pci_table_view->set_size_columns_to_fit_content(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto db = PCIDB::Database::open(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Vector<GJsonArrayModel::FieldSpec> pci_fields; | 
					
						
							|  |  |  |     pci_fields.empend( | 
					
						
							|  |  |  |         "Address", TextAlignment::CenterLeft, | 
					
						
							|  |  |  |         [](const JsonObject& object) { | 
					
						
							|  |  |  |             auto bus = object.get("bus").to_u32(); | 
					
						
							|  |  |  |             auto slot = object.get("slot").to_u32(); | 
					
						
							|  |  |  |             auto function = object.get("function").to_u32(); | 
					
						
							|  |  |  |             return String::format("%02x:%02x.%d", bus, slot, function); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     pci_fields.empend( | 
					
						
							|  |  |  |         "Class", TextAlignment::CenterLeft, | 
					
						
							|  |  |  |         [db](const JsonObject& object) { | 
					
						
							|  |  |  |             auto class_id = object.get("class").to_u32(); | 
					
						
							|  |  |  |             String class_name = db->get_class(class_id); | 
					
						
							|  |  |  |             return class_name == "" ? String::format("%04x", class_id) : class_name; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     pci_fields.empend( | 
					
						
							|  |  |  |         "Vendor", TextAlignment::CenterLeft, | 
					
						
							|  |  |  |         [db](const JsonObject& object) { | 
					
						
							|  |  |  |             auto vendor_id = object.get("vendor_id").to_u32(); | 
					
						
							|  |  |  |             String vendor_name = db->get_vendor(vendor_id); | 
					
						
							|  |  |  |             return vendor_name == "" ? String::format("%02x", vendor_id) : vendor_name; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     pci_fields.empend( | 
					
						
							|  |  |  |         "Device", TextAlignment::CenterLeft, | 
					
						
							|  |  |  |         [db](const JsonObject& object) { | 
					
						
							|  |  |  |             auto vendor_id = object.get("vendor_id").to_u32(); | 
					
						
							|  |  |  |             auto device_id = object.get("device_id").to_u32(); | 
					
						
							|  |  |  |             String device_name = db->get_device(vendor_id, device_id); | 
					
						
							|  |  |  |             return device_name == "" ? String::format("%02x", device_id) : device_name; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     pci_fields.empend( | 
					
						
							|  |  |  |         "Revision", TextAlignment::CenterRight, | 
					
						
							|  |  |  |         [](const JsonObject& object) { | 
					
						
							|  |  |  |             auto revision_id = object.get("revision_id").to_u32(); | 
					
						
							|  |  |  |             return String::format("%02x", revision_id); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pci_table_view->set_model(GSortingProxyModel::create(GJsonArrayModel::create("/proc/pci", move(pci_fields)))); | 
					
						
							|  |  |  |     pci_table_view->model()->update(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return pci_widget; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-18 16:18:20 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | GWidget* build_devices_tab() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* devices_widget = new GWidget(nullptr); | 
					
						
							|  |  |  |     devices_widget->set_layout(make<GBoxLayout>(Orientation::Vertical)); | 
					
						
							|  |  |  |     devices_widget->layout()->set_margins({ 4, 4, 4, 4 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto* devices_table_view = new GTableView(devices_widget); | 
					
						
							|  |  |  |     devices_table_view->set_size_columns_to_fit_content(true); | 
					
						
							|  |  |  |     devices_table_view->set_model(GSortingProxyModel::create(DevicesModel::create())); | 
					
						
							|  |  |  |     devices_table_view->model()->update(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return devices_widget; | 
					
						
							|  |  |  | } |