1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 01:25:07 +00:00

WindowServer+CPUGraph: Make menu applets be "regular" windows

Instead of implementing menu applets as their own thing, they are now
WSWindows of WSWindowType::MenuApplet.

This makes it much easier to work with them on the client side, since
you can just create a GWindow with the right type and you're in the
menubar doing applet stuff :^)
This commit is contained in:
Andreas Kling 2019-12-16 15:05:45 +01:00
parent 648ed76085
commit df129bbe0e
13 changed files with 97 additions and 202 deletions

View file

@ -1,87 +1,83 @@
#include <AK/CircularQueue.h>
#include <LibCore/CProcessStatisticsReader.h>
#include <LibCore/CTimer.h>
#include <LibDraw/GraphicsBitmap.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GWindowServerConnection.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
NonnullRefPtr<GraphicsBitmap> create_shared_bitmap(const Size& size)
{
ASSERT(GWindowServerConnection::the().server_pid());
ASSERT(!size.is_empty());
size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
size_t size_in_bytes = size.height() * pitch;
auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes);
ASSERT(shared_buffer);
shared_buffer->share_with(GWindowServerConnection::the().server_pid());
return GraphicsBitmap::create_with_shared_buffer(GraphicsBitmap::Format::RGBA32, *shared_buffer, size);
}
class GraphWidget final : public GWidget {
C_OBJECT(GraphWidget)
public:
GraphWidget()
: GWidget(nullptr)
{
start_timer(1000);
}
static void get_cpu_usage(unsigned& busy, unsigned& idle)
{
busy = 0;
idle = 0;
virtual ~GraphWidget() override {}
auto all_processes = CProcessStatisticsReader::get_all();
private:
virtual void timer_event(CTimerEvent&) override
{
unsigned busy;
unsigned idle;
get_cpu_usage(busy, idle);
unsigned busy_diff = busy - m_last_busy;
unsigned idle_diff = idle - m_last_idle;
m_last_busy = busy;
m_last_idle = idle;
float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
m_cpu_history.enqueue(cpu);
update();
}
for (auto& it : all_processes) {
for (auto& jt : it.value.threads) {
if (it.value.pid == 0)
idle += jt.times_scheduled;
else
busy += jt.times_scheduled;
virtual void paint_event(GPaintEvent& event) override
{
GPainter painter(*this);
painter.add_clip_rect(event.rect());
painter.fill_rect(event.rect(), Color::Black);
int i = m_cpu_history.capacity() - m_cpu_history.size();
for (auto cpu_usage : m_cpu_history) {
painter.draw_line(
{ i, rect().bottom() },
{ i, (int)(height() - (cpu_usage * (float)height())) },
Color::from_rgb(0xaa6d4b));
++i;
}
}
}
static void get_cpu_usage(unsigned& busy, unsigned& idle)
{
busy = 0;
idle = 0;
auto all_processes = CProcessStatisticsReader::get_all();
for (auto& it : all_processes) {
for (auto& jt : it.value.threads) {
if (it.value.pid == 0)
idle += jt.times_scheduled;
else
busy += jt.times_scheduled;
}
}
}
CircularQueue<float, 30> m_cpu_history;
unsigned m_last_busy { 0 };
unsigned m_last_idle { 0 };
};
int main(int argc, char** argv)
{
GApplication app(argc, argv);
Size applet_size(30, 16);
Rect applet_rect({}, applet_size);
CircularQueue<float, 30> cpu_history;
i32 applet_id = GWindowServerConnection::the().send_sync<WindowServer::CreateMenuApplet>(applet_size)->applet_id();
auto bitmap = create_shared_bitmap(applet_size);
GWindowServerConnection::the().send_sync<WindowServer::SetMenuAppletBackingStore>(applet_id, bitmap->shared_buffer_id());
unsigned last_busy = 0;
unsigned last_idle = 0;
auto repaint = [&] {
unsigned busy;
unsigned idle;
get_cpu_usage(busy, idle);
unsigned busy_diff = busy - last_busy;
unsigned idle_diff = idle - last_idle;
last_busy = busy;
last_idle = idle;
float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
cpu_history.enqueue(cpu);
GPainter painter(*bitmap);
painter.fill_rect(applet_rect, Color::Black);
int i = cpu_history.capacity() - cpu_history.size();
for (auto cpu_usage : cpu_history) {
painter.draw_line(
{ applet_rect.x() + i, applet_rect.bottom() },
{ applet_rect.x() + i, (int)(applet_rect.y() + (applet_rect.height() - (cpu_usage * (float)applet_rect.height()))) },
Color::from_rgb(0xaa6d4b));
++i;
}
GWindowServerConnection::the().send_sync<WindowServer::InvalidateMenuAppletRect>(applet_id, applet_rect);
};
repaint();
auto timer = CTimer::construct(1000, [&] {
repaint();
});
auto window = GWindow::construct();
window->set_window_type(GWindowType::MenuApplet);
window->resize(30, 16);
auto widget = GraphWidget::construct();
window->set_main_widget(widget);
window->show();
return app.exec();
}