ProcessStateWidget.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "ProcessStateWidget.h"
  2. #include <LibCore/CProcessStatisticsReader.h>
  3. #include <LibCore/CTimer.h>
  4. #include <LibGUI/GBoxLayout.h>
  5. #include <LibGUI/GLabel.h>
  6. #include <unistd.h>
  7. ProcessStateWidget::ProcessStateWidget(GWidget* parent)
  8. : GWidget(parent)
  9. {
  10. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  11. set_preferred_size(0, 20);
  12. set_layout(make<GBoxLayout>(Orientation::Horizontal));
  13. auto pid_label_label = GLabel::construct("Process:", this);
  14. pid_label_label->set_font(Font::default_bold_font());
  15. m_pid_label = GLabel::construct("", this);
  16. auto state_label_label = GLabel::construct("State:", this);
  17. state_label_label->set_font(Font::default_bold_font());
  18. m_state_label = GLabel::construct("", this);
  19. // FIXME: This should show CPU% instead.
  20. auto cpu_label_label = GLabel::construct("Times scheduled:", this);
  21. cpu_label_label->set_font(Font::default_bold_font());
  22. m_cpu_label = GLabel::construct("", this);
  23. auto memory_label_label = GLabel::construct("Memory (resident):", this);
  24. memory_label_label->set_font(Font::default_bold_font());
  25. m_memory_label = GLabel::construct("", this);
  26. m_timer = CTimer::construct(500, [this] {
  27. refresh();
  28. });
  29. }
  30. ProcessStateWidget::~ProcessStateWidget()
  31. {
  32. }
  33. void ProcessStateWidget::refresh()
  34. {
  35. if (m_tty_fd == -1) {
  36. m_pid_label->set_text("(none)");
  37. m_state_label->set_text("n/a");
  38. m_cpu_label->set_text("n/a");
  39. m_memory_label->set_text("n/a");
  40. return;
  41. }
  42. pid_t pid = tcgetpgrp(m_tty_fd);
  43. auto processes = CProcessStatisticsReader::get_all();
  44. auto child_process_data = processes.get(pid);
  45. if (!child_process_data.has_value())
  46. return;
  47. auto active_process_data = processes.get(child_process_data.value().pgid);
  48. auto& data = active_process_data.value();
  49. m_pid_label->set_text(String::format("%s(%d)", data.name.characters(), pid));
  50. m_state_label->set_text(data.state);
  51. m_cpu_label->set_text(String::format("%d", data.times_scheduled));
  52. m_memory_label->set_text(String::format("%d", data.amount_resident));
  53. }
  54. void ProcessStateWidget::set_tty_fd(int tty_fd)
  55. {
  56. m_tty_fd = tty_fd;
  57. refresh();
  58. }