summaryrefslogtreecommitdiff
path: root/Userland/Applets/Audio/main.cpp
blob: 383ec6f1f715c04430a62a5e981bb3440875aa54 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
 * Copyright (c) 2021, David Isaksson <davidisaksson93@gmail.com>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Array.h>
#include <LibAudio/ConnectionFromClient.h>
#include <LibConfig/Client.h>
#include <LibCore/System.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/CheckBox.h>
#include <LibGUI/Frame.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Slider.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/Palette.h>
#include <LibMain/Main.h>

class AudioWidget final : public GUI::Widget {
    C_OBJECT_ABSTRACT(AudioWidget)

private:
    struct VolumeBitmapPair {
        int volume_threshold { 0 };
        NonnullRefPtr<Gfx::Bitmap> bitmap;
    };

public:
    static ErrorOr<NonnullRefPtr<AudioWidget>> try_create()
    {
        Array<VolumeBitmapPair, 5> volume_level_bitmaps = {
            { { 66, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-high.png")) },
                { 33, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-medium.png")) },
                { 1, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-low.png")) },
                { 0, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-zero.png")) },
                { 0, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-muted.png")) } }
        };
        auto audio_client = TRY(Audio::ConnectionFromClient::try_create());
        NonnullRefPtr<AudioWidget> audio_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) AudioWidget(move(audio_client), move(volume_level_bitmaps))));
        TRY(audio_widget->try_initialize_graphical_elements());
        return audio_widget;
    }

private:
    AudioWidget(NonnullRefPtr<Audio::ConnectionFromClient> audio_client, Array<VolumeBitmapPair, 5> volume_level_bitmaps)
        : m_audio_client(move(audio_client))
        , m_volume_level_bitmaps(move(volume_level_bitmaps))
        , m_show_percent(Config::read_bool("AudioApplet", "Applet", "ShowPercent", false))
    {
        m_audio_volume = static_cast<int>(m_audio_client->get_main_mix_volume() * 100);
        m_audio_muted = m_audio_client->is_main_mix_muted();

        m_audio_client->on_main_mix_muted_state_change = [this](bool muted) {
            if (m_audio_muted == muted)
                return;
            m_mute_box->set_checked(!m_audio_muted);
            m_slider->set_enabled(!muted);
            m_audio_muted = muted;
            update();
        };

        m_audio_client->on_main_mix_volume_change = [this](double volume) {
            m_audio_volume = static_cast<int>(round(volume * 100));
            m_slider->set_value(m_slider->max() - m_audio_volume, GUI::AllowCallback::No);
            if (!m_audio_muted)
                update();
        };
    }

    ErrorOr<void> try_initialize_graphical_elements()
    {
        m_slider_window = add<GUI::Window>(window());
        m_slider_window->set_frameless(true);
        m_slider_window->set_resizable(false);
        m_slider_window->set_minimizable(false);
        m_slider_window->on_active_input_change = [this](bool is_active_input) {
            if (!is_active_input)
                close();
        };

        m_root_container = TRY(m_slider_window->try_set_main_widget<GUI::Frame>());
        m_root_container->set_fill_with_background_color(true);
        m_root_container->set_layout<GUI::VerticalBoxLayout>();
        m_root_container->layout()->set_margins({ 4 });
        m_root_container->layout()->set_spacing(0);
        m_root_container->set_frame_shape(Gfx::FrameShape::Window);

        m_percent_box = m_root_container->add<GUI::CheckBox>("\xE2\x84\xB9");
        m_percent_box->set_tooltip(m_show_percent ? "Hide percent" : "Show percent");
        m_percent_box->set_checked(m_show_percent);
        m_percent_box->on_checked = [&](bool show_percent) {
            m_show_percent = show_percent;
            set_audio_widget_size(m_show_percent);
            m_percent_box->set_tooltip(m_show_percent ? "Hide percent" : "Show percent");
            GUI::Application::the()->hide_tooltip();

            Config::write_bool("AudioApplet", "Applet", "ShowPercent", m_show_percent);
        };

        m_slider = m_root_container->add<GUI::VerticalSlider>();
        m_slider->set_max(100);
        m_slider->set_page_step(5);
        m_slider->set_step(5);
        m_slider->set_value(m_slider->max() - m_audio_volume);
        m_slider->set_knob_size_mode(GUI::Slider::KnobSizeMode::Proportional);
        m_slider->on_change = [&](int value) {
            m_audio_volume = m_slider->max() - value;
            double volume = clamp(static_cast<double>(m_audio_volume) / m_slider->max(), 0.0, 1.0);
            m_audio_client->set_main_mix_volume(volume);
            update();
        };

        m_mute_box = m_root_container->add<GUI::CheckBox>("\xE2\x9D\x8C");
        m_mute_box->set_checked(m_audio_muted);
        m_mute_box->set_tooltip(m_audio_muted ? "Unmute" : "Mute");
        m_mute_box->on_checked = [&](bool is_muted) {
            m_mute_box->set_tooltip(is_muted ? "Unmute" : "Mute");
            m_audio_client->set_main_mix_muted(is_muted);
            GUI::Application::the()->hide_tooltip();
        };

        return {};
    };

public:
    virtual ~AudioWidget() override = default;

    void set_audio_widget_size(bool show_percent)
    {
        if (show_percent)
            window()->resize(44, 16);
        else
            window()->resize(16, 16);
    }

private:
    virtual void mousedown_event(GUI::MouseEvent& event) override
    {
        if (event.button() == GUI::MouseButton::Primary) {
            if (!m_slider_window->is_visible())
                open();
            else
                close();
            return;
        }
        if (event.button() == GUI::MouseButton::Secondary) {
            m_audio_client->set_main_mix_muted(!m_audio_muted);
            update();
        }
    }

    virtual void mousewheel_event(GUI::MouseEvent& event) override
    {
        if (m_audio_muted)
            return;
        m_slider->dispatch_event(event);
        update();
    }

    virtual void paint_event(GUI::PaintEvent& event) override
    {
        GUI::Painter painter(*this);
        painter.add_clip_rect(event.rect());
        painter.clear_rect(event.rect(), Color::from_rgba(0));

        auto& audio_bitmap = choose_bitmap_from_volume();
        painter.blit({}, audio_bitmap, audio_bitmap.rect());

        if (m_show_percent) {
            auto volume_text = m_audio_muted ? "mute" : String::formatted("{}%", m_audio_volume);
            painter.draw_text({ 16, 3, 24, 16 }, volume_text, Gfx::FontDatabase::default_fixed_width_font(), Gfx::TextAlignment::TopLeft, palette().window_text());
        }
    }

    virtual void applet_area_rect_change_event(GUI::AppletAreaRectChangeEvent&) override
    {
        reposition_slider_window();
    }

    void open()
    {
        reposition_slider_window();
        m_slider_window->show();
    }

    void close()
    {
        m_slider_window->hide();
    }

    Gfx::Bitmap& choose_bitmap_from_volume()
    {
        if (m_audio_muted)
            return *m_volume_level_bitmaps.back().bitmap;

        for (auto& pair : m_volume_level_bitmaps) {
            if (m_audio_volume >= pair.volume_threshold)
                return *pair.bitmap;
        }
        VERIFY_NOT_REACHED();
    }

    void reposition_slider_window()
    {
        constexpr auto width { 50 };
        constexpr auto height { 125 };
        constexpr auto tray_and_taskbar_padding { 6 };
        constexpr auto icon_offset { (width - 16) / 2 };
        auto applet_rect = window()->applet_rect_on_screen();
        m_slider_window->set_rect(
            applet_rect.x() - icon_offset,
            applet_rect.y() - height - tray_and_taskbar_padding,
            width,
            height);
    }

    NonnullRefPtr<Audio::ConnectionFromClient> m_audio_client;
    Array<VolumeBitmapPair, 5> m_volume_level_bitmaps;
    bool m_show_percent { false };
    bool m_audio_muted { false };
    int m_audio_volume { 100 };

    RefPtr<GUI::Slider> m_slider;
    RefPtr<GUI::Window> m_slider_window;
    RefPtr<GUI::CheckBox> m_mute_box;
    RefPtr<GUI::CheckBox> m_percent_box;
    RefPtr<GUI::Frame> m_root_container;
};

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    TRY(Core::System::pledge("stdio recvfd sendfd rpath wpath cpath unix"));

    auto app = TRY(GUI::Application::try_create(arguments));
    Config::pledge_domain("AudioApplet");
    TRY(Core::System::unveil("/tmp/portal/audio", "rw"));
    TRY(Core::System::unveil("/res", "r"));
    TRY(Core::System::unveil(nullptr, nullptr));

    auto window = TRY(GUI::Window::try_create());
    window->set_has_alpha_channel(true);
    window->set_title("Audio");
    window->set_window_type(GUI::WindowType::Applet);

    auto audio_widget = TRY(window->try_set_main_widget<AudioWidget>());
    window->show();

    // This positioning code depends on the window actually existing.
    static_cast<AudioWidget*>(window->main_widget())->set_audio_widget_size(Config::read_bool("AudioApplet", "Applet", "ShowPercent", false));

    TRY(Core::System::pledge("stdio recvfd sendfd rpath"));

    return app->exec();
}