2019-06-07 06:48:03 -03:00
|
|
|
#include "GraphWidget.h"
|
|
|
|
|
#include "MemoryStatsWidget.h"
|
|
|
|
|
#include "ProcessTableView.h"
|
2019-04-17 23:38:31 -03:00
|
|
|
#include <LibCore/CTimer.h>
|
2019-06-07 06:48:03 -03:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-02-27 21:43:50 -03:00
|
|
|
#include <LibGUI/GApplication.h>
|
2019-06-07 06:48:03 -03:00
|
|
|
#include <LibGUI/GBoxLayout.h>
|
2019-05-05 22:21:23 -03:00
|
|
|
#include <LibGUI/GGroupBox.h>
|
2019-05-04 20:31:02 -03:00
|
|
|
#include <LibGUI/GLabel.h>
|
2019-06-07 06:48:03 -03:00
|
|
|
#include <LibGUI/GMenuBar.h>
|
|
|
|
|
#include <LibGUI/GTabWidget.h>
|
|
|
|
|
#include <LibGUI/GToolBar.h>
|
|
|
|
|
#include <LibGUI/GWidget.h>
|
|
|
|
|
#include <LibGUI/GWindow.h>
|
2019-02-27 21:43:50 -03:00
|
|
|
#include <signal.h>
|
2019-06-07 06:48:03 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
2019-02-27 21:43:50 -03:00
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
|
|
|
|
GApplication app(argc, argv);
|
|
|
|
|
|
2019-05-05 15:53:04 -03:00
|
|
|
auto* keeper = new GWidget;
|
|
|
|
|
keeper->set_layout(make<GBoxLayout>(Orientation::Vertical));
|
|
|
|
|
keeper->set_fill_with_background_color(true);
|
2019-06-30 04:23:16 -03:00
|
|
|
keeper->set_background_color(Color::WarmGray);
|
2019-05-10 17:59:39 -03:00
|
|
|
keeper->layout()->set_margins({ 4, 4, 4, 4 });
|
2019-05-05 15:53:04 -03:00
|
|
|
|
|
|
|
|
auto* tabwidget = new GTabWidget(keeper);
|
2019-05-04 20:31:02 -03:00
|
|
|
|
2019-07-27 03:42:15 -03:00
|
|
|
auto* process_table_container = new GWidget(nullptr);
|
|
|
|
|
tabwidget->add_widget("Processes", process_table_container);
|
2019-05-04 20:31:02 -03:00
|
|
|
|
2019-05-05 22:21:23 -03:00
|
|
|
auto* graphs_container = new GWidget;
|
|
|
|
|
graphs_container->set_fill_with_background_color(true);
|
2019-06-30 04:23:16 -03:00
|
|
|
graphs_container->set_background_color(Color::WarmGray);
|
2019-05-05 22:21:23 -03: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 17:39:24 -03:00
|
|
|
cpu_graph_group_box->set_preferred_size(0, 120);
|
2019-05-05 22:21:23 -03: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 06:48:03 -03:00
|
|
|
cpu_graph->text_formatter = [](int value, int) {
|
2019-05-05 22:21:23 -03: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-05 22:37:22 -03:00
|
|
|
memory_graph_group_box->layout()->set_margins({ 6, 16, 6, 6 });
|
2019-05-05 22:21:23 -03:00
|
|
|
memory_graph_group_box->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
2019-07-20 17:39:24 -03:00
|
|
|
memory_graph_group_box->set_preferred_size(0, 120);
|
2019-05-05 22:21:23 -03: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 06:48:03 -03:00
|
|
|
memory_graph->text_formatter = [](int value, int max) {
|
2019-05-05 22:21:23 -03:00
|
|
|
return String::format("%d / %d KB", value, max);
|
|
|
|
|
};
|
2019-05-05 09:39:37 -03:00
|
|
|
|
2019-05-07 12:11:48 -03:00
|
|
|
tabwidget->add_widget("Graphs", graphs_container);
|
|
|
|
|
|
2019-07-27 03:42:15 -03: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-27 21:43:50 -03:00
|
|
|
|
2019-07-27 03:42:15 -03:00
|
|
|
auto* toolbar = new GToolBar(process_table_container);
|
2019-05-10 17:59:39 -03:00
|
|
|
toolbar->set_has_frame(false);
|
2019-07-27 03:42:15 -03:00
|
|
|
auto* process_table_view = new ProcessTableView(*cpu_graph, process_table_container);
|
|
|
|
|
auto* memory_stats_widget = new MemoryStatsWidget(*memory_graph, process_table_container);
|
2019-03-10 08:13:22 -03:00
|
|
|
|
2019-04-17 23:38:31 -03:00
|
|
|
auto* refresh_timer = new CTimer(1000, [&] {
|
|
|
|
|
process_table_view->refresh();
|
|
|
|
|
memory_stats_widget->refresh();
|
|
|
|
|
});
|
2019-02-27 21:43:50 -03:00
|
|
|
|
2019-06-07 06:48:03 -03:00
|
|
|
auto kill_action = GAction::create("Kill process", GraphicsBitmap::load_from_file("/res/icons/kill16.png"), [process_table_view](const GAction&) {
|
2019-02-28 06:57:09 -03:00
|
|
|
pid_t pid = process_table_view->selected_pid();
|
2019-02-27 21:43:50 -03:00
|
|
|
if (pid != -1)
|
|
|
|
|
kill(pid, SIGKILL);
|
|
|
|
|
});
|
2019-02-28 08:06:19 -03:00
|
|
|
|
2019-06-07 06:48:03 -03:00
|
|
|
auto stop_action = GAction::create("Stop process", GraphicsBitmap::load_from_file("/res/icons/stop16.png"), [process_table_view](const GAction&) {
|
2019-02-28 08:06:19 -03:00
|
|
|
pid_t pid = process_table_view->selected_pid();
|
|
|
|
|
if (pid != -1)
|
|
|
|
|
kill(pid, SIGSTOP);
|
|
|
|
|
});
|
|
|
|
|
|
2019-06-07 06:48:03 -03:00
|
|
|
auto continue_action = GAction::create("Continue process", GraphicsBitmap::load_from_file("/res/icons/continue16.png"), [process_table_view](const GAction&) {
|
2019-02-28 09:02:55 -03:00
|
|
|
pid_t pid = process_table_view->selected_pid();
|
|
|
|
|
if (pid != -1)
|
|
|
|
|
kill(pid, SIGCONT);
|
|
|
|
|
});
|
|
|
|
|
|
2019-07-11 11:04:17 -03:00
|
|
|
toolbar->add_action(kill_action);
|
|
|
|
|
toolbar->add_action(stop_action);
|
|
|
|
|
toolbar->add_action(continue_action);
|
2019-02-27 21:43:50 -03:00
|
|
|
|
|
|
|
|
auto menubar = make<GMenuBar>();
|
2019-05-27 08:52:28 -03:00
|
|
|
auto app_menu = make<GMenu>("Process Manager");
|
2019-06-07 06:48:03 -03:00
|
|
|
app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [](const GAction&) {
|
2019-02-27 21:43:50 -03:00
|
|
|
GApplication::the().quit(0);
|
|
|
|
|
return;
|
|
|
|
|
}));
|
|
|
|
|
menubar->add_menu(move(app_menu));
|
|
|
|
|
|
2019-02-28 09:02:55 -03:00
|
|
|
auto process_menu = make<GMenu>("Process");
|
2019-06-30 03:15:18 -03:00
|
|
|
process_menu->add_action(kill_action);
|
|
|
|
|
process_menu->add_action(stop_action);
|
|
|
|
|
process_menu->add_action(continue_action);
|
2019-02-28 09:02:55 -03:00
|
|
|
menubar->add_menu(move(process_menu));
|
2019-02-27 21:43:50 -03:00
|
|
|
|
2019-06-30 03:15:18 -03: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 13:20:00 -03:00
|
|
|
(void)index;
|
2019-06-30 03:15:18 -03:00
|
|
|
process_context_menu->popup(event.screen_position());
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-17 23:38:31 -03:00
|
|
|
auto frequency_menu = make<GMenu>("Frequency");
|
2019-06-07 06:48:03 -03:00
|
|
|
frequency_menu->add_action(GAction::create("0.25 sec", [refresh_timer](auto&) {
|
2019-05-05 22:21:23 -03:00
|
|
|
refresh_timer->restart(250);
|
|
|
|
|
}));
|
2019-06-07 06:48:03 -03:00
|
|
|
frequency_menu->add_action(GAction::create("0.5 sec", [refresh_timer](auto&) {
|
2019-04-17 23:38:31 -03:00
|
|
|
refresh_timer->restart(500);
|
|
|
|
|
}));
|
2019-06-07 06:48:03 -03:00
|
|
|
frequency_menu->add_action(GAction::create("1 sec", [refresh_timer](auto&) {
|
2019-04-17 23:38:31 -03:00
|
|
|
refresh_timer->restart(1000);
|
|
|
|
|
}));
|
2019-06-07 06:48:03 -03:00
|
|
|
frequency_menu->add_action(GAction::create("3 sec", [refresh_timer](auto&) {
|
2019-04-17 23:38:31 -03:00
|
|
|
refresh_timer->restart(3000);
|
|
|
|
|
}));
|
2019-06-07 06:48:03 -03:00
|
|
|
frequency_menu->add_action(GAction::create("5 sec", [refresh_timer](auto&) {
|
2019-04-17 23:38:31 -03:00
|
|
|
refresh_timer->restart(5000);
|
|
|
|
|
}));
|
|
|
|
|
menubar->add_menu(move(frequency_menu));
|
|
|
|
|
|
2019-02-27 21:43:50 -03:00
|
|
|
auto help_menu = make<GMenu>("Help");
|
2019-06-07 06:48:03 -03:00
|
|
|
help_menu->add_action(GAction::create("About", [](const GAction&) {
|
2019-02-27 21:43:50 -03:00
|
|
|
dbgprintf("FIXME: Implement Help/About\n");
|
|
|
|
|
}));
|
|
|
|
|
menubar->add_menu(move(help_menu));
|
|
|
|
|
|
|
|
|
|
app.set_menubar(move(menubar));
|
|
|
|
|
|
|
|
|
|
auto* window = new GWindow;
|
2019-05-27 08:52:28 -03:00
|
|
|
window->set_title("Process Manager");
|
2019-04-17 09:48:55 -03:00
|
|
|
window->set_rect(20, 200, 680, 400);
|
2019-05-05 15:53:04 -03:00
|
|
|
window->set_main_widget(keeper);
|
2019-07-23 13:20:00 -03:00
|
|
|
|
2019-02-27 21:43:50 -03:00
|
|
|
window->show();
|
|
|
|
|
|
2019-04-16 12:55:27 -03:00
|
|
|
window->set_icon_path("/res/icons/16x16/app-process-manager.png");
|
|
|
|
|
|
2019-02-27 21:43:50 -03:00
|
|
|
return app.exec();
|
|
|
|
|
}
|