summaryrefslogtreecommitdiff
path: root/Userland/Demos/CatDog/CatDog.cpp
blob: ec6a14a81796653ac41f9e961c4bfe7f47da7acf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2021, Richard Gráčik <r.gracik@gmail.com>
 * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "CatDog.h"
#include <LibCore/ProcessStatisticsReader.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>

ErrorOr<NonnullRefPtr<CatDog>> CatDog::create()
{
    struct ImageSource {
        State state;
        StringView path;
    };

    // NOTE: The order of the elements is important. Matching is done in best-match order.
    //       So items with the more bits should be placed before items with less bits to
    //       ensure correct matching order. This also means that "Frame2" has to be first.
    static constexpr Array<ImageSource, 24> const image_sources = {
        ImageSource { State::Up | State::Right | State::Frame2, "/res/icons/catdog/nerun2.png"sv },
        { State::Up | State::Right, "/res/icons/catdog/nerun1.png"sv },
        { State::Up | State::Left | State::Frame2, "/res/icons/catdog/nwrun2.png"sv },
        { State::Up | State::Left, "/res/icons/catdog/nwrun1.png"sv },
        { State::Down | State::Right | State::Frame2, "/res/icons/catdog/serun2.png"sv },
        { State::Down | State::Right, "/res/icons/catdog/serun1.png"sv },
        { State::Down | State::Left | State::Frame2, "/res/icons/catdog/swrun2.png"sv },
        { State::Down | State::Left, "/res/icons/catdog/swrun1.png"sv },
        { State::Up | State::Frame2, "/res/icons/catdog/nrun2.png"sv },
        { State::Up, "/res/icons/catdog/nrun1.png"sv },
        { State::Down | State::Frame2, "/res/icons/catdog/srun2.png"sv },
        { State::Down, "/res/icons/catdog/srun1.png"sv },
        { State::Left | State::Frame2, "/res/icons/catdog/wrun2.png"sv },
        { State::Left, "/res/icons/catdog/wrun1.png"sv },
        { State::Right | State::Frame2, "/res/icons/catdog/erun2.png"sv },
        { State::Right, "/res/icons/catdog/erun1.png"sv },
        { State::Sleeping | State::Frame2, "/res/icons/catdog/sleep2.png"sv },
        { State::Sleeping, "/res/icons/catdog/sleep1.png"sv },
        { State::Idle | State::Artist, "/res/icons/catdog/artist.png"sv },
        { State::Idle | State::Inspector, "/res/icons/catdog/inspector.png"sv },
        { State::Idle, "/res/icons/catdog/still.png"sv },
        { State::Alert | State::Artist, "/res/icons/catdog/artist.png"sv },
        { State::Alert | State::Inspector, "/res/icons/catdog/inspector.png"sv },
        { State::Alert, "/res/icons/catdog/alert.png"sv }
    };

    auto catdog = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) CatDog));
    for (auto const& image_source : image_sources)
        TRY(catdog->m_images.try_append({ image_source.state, *TRY(Gfx::Bitmap::try_load_from_file(image_source.path)) }));

    return catdog;
}

CatDog::CatDog()
    : m_proc_all(MUST(Core::Stream::File::open("/sys/kernel/processes"sv, Core::Stream::OpenMode::Read)))
{
    m_idle_sleep_timer.start();
}

void CatDog::set_roaming(bool roaming)
{
    m_state = (roaming ? State::Idle : State::Alert) | special_application_states();
    update();
}

CatDog::State CatDog::special_application_states() const
{
    auto maybe_proc_info = Core::ProcessStatisticsReader::get_all(*m_proc_all);
    if (maybe_proc_info.is_error())
        return State::GenericCatDog;

    auto proc_info = maybe_proc_info.release_value();
    auto maybe_paint_program = proc_info.processes.first_matching([](auto& process) {
        return process.name.equals_ignoring_case("pixelpaint"sv) || process.name.equals_ignoring_case("fonteditor"sv);
    });
    if (maybe_paint_program.has_value())
        return State::Artist;

    auto maybe_inspector_program = proc_info.processes.first_matching([](auto& process) {
        return process.name.equals_ignoring_case("inspector"sv) || process.name.equals_ignoring_case("systemmonitor"sv) || process.name.equals_ignoring_case("profiler"sv);
    });
    if (maybe_inspector_program.has_value())
        return State::Inspector;

    return State::GenericCatDog;
}

bool CatDog::is_artist() const
{
    return has_flag(special_application_states(), State::Artist);
}

bool CatDog::is_inspector() const
{
    return has_flag(special_application_states(), State::Inspector);
}

void CatDog::timer_event(Core::TimerEvent&)
{
    if (has_flag(m_state, State::Alert))
        return;

    m_state = special_application_states();

    auto const size = window()->size();
    Gfx::IntPoint move;

    if (m_mouse_offset.x() < 0) {
        m_state |= State::Left;
        move.set_x(max(m_mouse_offset.x(), -size.width() / 2));
    } else if (m_mouse_offset.x() > size.width()) {
        m_state |= State::Right;
        move.set_x(min(m_mouse_offset.x(), size.width() / 2));
    }

    if (m_mouse_offset.y() < 0) {
        m_state |= State::Up;
        move.set_y(max(m_mouse_offset.y(), -size.height() / 2));
    } else if (m_mouse_offset.y() > size.height()) {
        m_state |= State::Down;
        move.set_y(min(m_mouse_offset.y(), size.height() / 2));
    }

    if (has_any_flag(m_state, State::Directions)) {
        m_idle_sleep_timer.start();
    } else {
        if (m_idle_sleep_timer.elapsed() > 5'000)
            m_state |= State::Sleeping;
        else
            m_state |= State::Idle;
    }

    window()->move_to(window()->position() + move);
    m_mouse_offset -= move;

    m_frame = m_frame == State::Frame1 ? State::Frame2 : State::Frame1;
    m_state |= m_frame;

    update();
}

Gfx::Bitmap& CatDog::bitmap_for_state() const
{
    auto const iter = m_images.find_if([&](auto const& image) { return (m_state & image.state) == image.state; });
    return iter != m_images.end() ? *iter->bitmap : *m_images[m_images.size() - 1].bitmap;
}

void CatDog::paint_event(GUI::PaintEvent& event)
{
    auto const& bmp = bitmap_for_state();
    GUI::Painter painter(*this);
    painter.clear_rect(event.rect(), Gfx::Color());
    painter.blit(Gfx::IntPoint(0, 0), bmp, bmp.rect());
}

void CatDog::track_mouse_move(Gfx::IntPoint point)
{
    if (has_flag(m_state, State::Alert))
        return;

    Gfx::IntPoint relative_offset = point - window()->position();
    if (m_mouse_offset != relative_offset) {
        m_mouse_offset = relative_offset;
        m_idle_sleep_timer.start();
    }
}

void CatDog::mousedown_event(GUI::MouseEvent& event)
{
    if (event.button() == GUI::MouseButton::Primary && on_click)
        on_click();
}

void CatDog::context_menu_event(GUI::ContextMenuEvent& event)
{
    if (on_context_menu_request)
        on_context_menu_request(event);
}