main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "GraphWidget.h"
  2. #include "MemoryStatsWidget.h"
  3. #include "ProcessTableView.h"
  4. #include <LibCore/CTimer.h>
  5. #include <LibGUI/GAction.h>
  6. #include <LibGUI/GApplication.h>
  7. #include <LibGUI/GBoxLayout.h>
  8. #include <LibGUI/GGroupBox.h>
  9. #include <LibGUI/GLabel.h>
  10. #include <LibGUI/GMenuBar.h>
  11. #include <LibGUI/GTabWidget.h>
  12. #include <LibGUI/GToolBar.h>
  13. #include <LibGUI/GWidget.h>
  14. #include <LibGUI/GWindow.h>
  15. #include <signal.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. int main(int argc, char** argv)
  19. {
  20. GApplication app(argc, argv);
  21. auto* keeper = new GWidget;
  22. keeper->set_layout(make<GBoxLayout>(Orientation::Vertical));
  23. keeper->set_fill_with_background_color(true);
  24. keeper->set_background_color(Color::LightGray);
  25. keeper->layout()->set_margins({ 4, 4, 4, 4 });
  26. auto* tabwidget = new GTabWidget(keeper);
  27. auto* widget = new GWidget(nullptr);
  28. tabwidget->add_widget("Processes", widget);
  29. auto* graphs_container = new GWidget;
  30. graphs_container->set_fill_with_background_color(true);
  31. graphs_container->set_background_color(Color::LightGray);
  32. graphs_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
  33. graphs_container->layout()->set_margins({ 4, 4, 4, 4 });
  34. auto* cpu_graph_group_box = new GGroupBox("CPU usage", graphs_container);
  35. cpu_graph_group_box->set_layout(make<GBoxLayout>(Orientation::Vertical));
  36. cpu_graph_group_box->layout()->set_margins({ 6, 16, 6, 6 });
  37. cpu_graph_group_box->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  38. cpu_graph_group_box->set_preferred_size({ 0, 120 });
  39. auto* cpu_graph = new GraphWidget(cpu_graph_group_box);
  40. cpu_graph->set_max(100);
  41. cpu_graph->set_text_color(Color::Green);
  42. cpu_graph->set_graph_color(Color::from_rgb(0x00bb00));
  43. cpu_graph->text_formatter = [](int value, int) {
  44. return String::format("%d%%", value);
  45. };
  46. auto* memory_graph_group_box = new GGroupBox("Memory usage", graphs_container);
  47. memory_graph_group_box->set_layout(make<GBoxLayout>(Orientation::Vertical));
  48. memory_graph_group_box->layout()->set_margins({ 6, 16, 6, 6 });
  49. memory_graph_group_box->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  50. memory_graph_group_box->set_preferred_size({ 0, 120 });
  51. auto* memory_graph = new GraphWidget(memory_graph_group_box);
  52. memory_graph->set_text_color(Color::Cyan);
  53. memory_graph->set_graph_color(Color::from_rgb(0x00bbbb));
  54. memory_graph->text_formatter = [](int value, int max) {
  55. return String::format("%d / %d KB", value, max);
  56. };
  57. tabwidget->add_widget("Graphs", graphs_container);
  58. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  59. widget->layout()->set_margins({ 4, 0, 4, 4 });
  60. widget->layout()->set_spacing(0);
  61. auto* toolbar = new GToolBar(widget);
  62. toolbar->set_has_frame(false);
  63. auto* process_table_view = new ProcessTableView(*cpu_graph, widget);
  64. auto* memory_stats_widget = new MemoryStatsWidget(*memory_graph, widget);
  65. auto* refresh_timer = new CTimer(1000, [&] {
  66. process_table_view->refresh();
  67. memory_stats_widget->refresh();
  68. });
  69. auto kill_action = GAction::create("Kill process", GraphicsBitmap::load_from_file("/res/icons/kill16.png"), [process_table_view](const GAction&) {
  70. pid_t pid = process_table_view->selected_pid();
  71. if (pid != -1)
  72. kill(pid, SIGKILL);
  73. });
  74. auto stop_action = GAction::create("Stop process", GraphicsBitmap::load_from_file("/res/icons/stop16.png"), [process_table_view](const GAction&) {
  75. pid_t pid = process_table_view->selected_pid();
  76. if (pid != -1)
  77. kill(pid, SIGSTOP);
  78. });
  79. auto continue_action = GAction::create("Continue process", GraphicsBitmap::load_from_file("/res/icons/continue16.png"), [process_table_view](const GAction&) {
  80. pid_t pid = process_table_view->selected_pid();
  81. if (pid != -1)
  82. kill(pid, SIGCONT);
  83. });
  84. toolbar->add_action(kill_action.copy_ref());
  85. toolbar->add_action(stop_action.copy_ref());
  86. toolbar->add_action(continue_action.copy_ref());
  87. auto menubar = make<GMenuBar>();
  88. auto app_menu = make<GMenu>("Process Manager");
  89. app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [](const GAction&) {
  90. GApplication::the().quit(0);
  91. return;
  92. }));
  93. menubar->add_menu(move(app_menu));
  94. auto process_menu = make<GMenu>("Process");
  95. process_menu->add_action(kill_action.copy_ref());
  96. process_menu->add_action(stop_action.copy_ref());
  97. process_menu->add_action(continue_action.copy_ref());
  98. menubar->add_menu(move(process_menu));
  99. auto frequency_menu = make<GMenu>("Frequency");
  100. frequency_menu->add_action(GAction::create("0.25 sec", [refresh_timer](auto&) {
  101. refresh_timer->restart(250);
  102. }));
  103. frequency_menu->add_action(GAction::create("0.5 sec", [refresh_timer](auto&) {
  104. refresh_timer->restart(500);
  105. }));
  106. frequency_menu->add_action(GAction::create("1 sec", [refresh_timer](auto&) {
  107. refresh_timer->restart(1000);
  108. }));
  109. frequency_menu->add_action(GAction::create("3 sec", [refresh_timer](auto&) {
  110. refresh_timer->restart(3000);
  111. }));
  112. frequency_menu->add_action(GAction::create("5 sec", [refresh_timer](auto&) {
  113. refresh_timer->restart(5000);
  114. }));
  115. menubar->add_menu(move(frequency_menu));
  116. auto help_menu = make<GMenu>("Help");
  117. help_menu->add_action(GAction::create("About", [](const GAction&) {
  118. dbgprintf("FIXME: Implement Help/About\n");
  119. }));
  120. menubar->add_menu(move(help_menu));
  121. app.set_menubar(move(menubar));
  122. auto* window = new GWindow;
  123. window->set_title("Process Manager");
  124. window->set_rect(20, 200, 680, 400);
  125. window->set_main_widget(keeper);
  126. window->set_should_exit_event_loop_on_close(true);
  127. window->show();
  128. window->set_icon_path("/res/icons/16x16/app-process-manager.png");
  129. return app.exec();
  130. }