main.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. * Copyright (c) 2021, David Isaksson <davidisaksson93@gmail.com>
  5. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Array.h>
  10. #include <LibAudio/ConnectionToManagerServer.h>
  11. #include <LibConfig/Client.h>
  12. #include <LibCore/System.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/BoxLayout.h>
  15. #include <LibGUI/CheckBox.h>
  16. #include <LibGUI/Frame.h>
  17. #include <LibGUI/Painter.h>
  18. #include <LibGUI/Slider.h>
  19. #include <LibGUI/Widget.h>
  20. #include <LibGUI/Window.h>
  21. #include <LibGfx/Bitmap.h>
  22. #include <LibGfx/Font/FontDatabase.h>
  23. #include <LibGfx/Palette.h>
  24. #include <LibMain/Main.h>
  25. static constexpr bool audio_applet_show_percent_default = false;
  26. class AudioWidget final : public GUI::Widget {
  27. C_OBJECT_ABSTRACT(AudioWidget)
  28. private:
  29. struct VolumeBitmapPair {
  30. int volume_threshold { 0 };
  31. NonnullRefPtr<Gfx::Bitmap> bitmap;
  32. };
  33. public:
  34. static ErrorOr<NonnullRefPtr<AudioWidget>> try_create()
  35. {
  36. Array<VolumeBitmapPair, 5> volume_level_bitmaps = {
  37. { { 66, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/audio-volume-high.png"sv)) },
  38. { 33, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/audio-volume-medium.png"sv)) },
  39. { 1, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/audio-volume-low.png"sv)) },
  40. { 0, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/audio-volume-zero.png"sv)) },
  41. { 0, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/audio-volume-muted.png"sv)) } }
  42. };
  43. auto audio_client = TRY(Audio::ConnectionToManagerServer::try_create());
  44. NonnullRefPtr<AudioWidget> audio_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) AudioWidget(move(audio_client), move(volume_level_bitmaps))));
  45. TRY(audio_widget->try_initialize_graphical_elements());
  46. return audio_widget;
  47. }
  48. private:
  49. AudioWidget(NonnullRefPtr<Audio::ConnectionToManagerServer> audio_client, Array<VolumeBitmapPair, 5> volume_level_bitmaps)
  50. : m_audio_client(move(audio_client))
  51. , m_volume_level_bitmaps(move(volume_level_bitmaps))
  52. {
  53. m_audio_volume = static_cast<int>(m_audio_client->get_main_mix_volume() * 100);
  54. m_audio_muted = m_audio_client->is_main_mix_muted();
  55. m_audio_client->on_main_mix_muted_state_change = [this](bool muted) {
  56. if (m_audio_muted == muted)
  57. return;
  58. m_mute_box->set_checked(!m_audio_muted);
  59. m_slider->set_enabled(!muted);
  60. m_audio_muted = muted;
  61. update();
  62. };
  63. m_audio_client->on_main_mix_volume_change = [this](double volume) {
  64. m_audio_volume = static_cast<int>(round(volume * 100));
  65. m_slider->set_value(m_slider->max() - m_audio_volume, GUI::AllowCallback::No);
  66. if (!m_audio_muted)
  67. update();
  68. };
  69. }
  70. ErrorOr<void> try_initialize_graphical_elements()
  71. {
  72. m_slider_window = add<GUI::Window>(window());
  73. m_slider_window->set_window_type(GUI::WindowType::Popup);
  74. m_root_container = TRY(m_slider_window->set_main_widget<GUI::Frame>());
  75. m_root_container->set_fill_with_background_color(true);
  76. m_root_container->set_layout<GUI::VerticalBoxLayout>(4, 0);
  77. m_root_container->set_frame_style(Gfx::FrameStyle::Window);
  78. m_percent_box = m_root_container->add<GUI::CheckBox>("\xE2\x84\xB9"_string);
  79. m_percent_box->set_tooltip_deprecated(show_percent() ? "Hide percent" : "Show percent");
  80. m_percent_box->set_checked(show_percent());
  81. m_percent_box->on_checked = [&](bool show_percent) {
  82. set_show_percent(show_percent);
  83. GUI::Application::the()->hide_tooltip();
  84. Config::write_bool("AudioApplet"sv, "Applet"sv, "ShowPercent"sv, show_percent);
  85. };
  86. m_slider = m_root_container->add<GUI::VerticalSlider>();
  87. m_slider->set_max(100);
  88. m_slider->set_page_step(5);
  89. m_slider->set_step(5);
  90. m_slider->set_value(m_slider->max() - m_audio_volume);
  91. m_slider->set_knob_size_mode(GUI::Slider::KnobSizeMode::Proportional);
  92. m_slider->on_change = [&](int value) {
  93. m_audio_volume = m_slider->max() - value;
  94. double volume = clamp(static_cast<double>(m_audio_volume) / m_slider->max(), 0.0, 1.0);
  95. m_audio_client->set_main_mix_volume(volume);
  96. update();
  97. };
  98. m_mute_box = m_root_container->add<GUI::CheckBox>("\xE2\x9D\x8C"_string);
  99. m_mute_box->set_checked(m_audio_muted);
  100. m_mute_box->set_tooltip_deprecated(m_audio_muted ? "Unmute" : "Mute");
  101. m_mute_box->on_checked = [&](bool is_muted) {
  102. m_mute_box->set_tooltip_deprecated(is_muted ? "Unmute" : "Mute");
  103. m_audio_client->set_main_mix_muted(is_muted);
  104. GUI::Application::the()->hide_tooltip();
  105. };
  106. return {};
  107. }
  108. public:
  109. virtual ~AudioWidget() override = default;
  110. bool show_percent() const { return m_show_percent; }
  111. void set_show_percent(bool show_percent)
  112. {
  113. m_show_percent = show_percent;
  114. m_percent_box->set_checked(show_percent);
  115. m_percent_box->set_tooltip_deprecated(show_percent ? "Hide percent" : "Show percent");
  116. if (show_percent)
  117. window()->resize(44, 16);
  118. else
  119. window()->resize(16, 16);
  120. }
  121. private:
  122. virtual void mousedown_event(GUI::MouseEvent& event) override
  123. {
  124. if (event.button() == GUI::MouseButton::Primary) {
  125. if (!m_slider_window->is_visible())
  126. open();
  127. else
  128. close();
  129. return;
  130. }
  131. if (event.button() == GUI::MouseButton::Secondary) {
  132. m_audio_client->set_main_mix_muted(!m_audio_muted);
  133. update();
  134. }
  135. }
  136. virtual void mousewheel_event(GUI::MouseEvent& event) override
  137. {
  138. if (m_audio_muted)
  139. return;
  140. m_slider->dispatch_event(event);
  141. update();
  142. }
  143. virtual void paint_event(GUI::PaintEvent& event) override
  144. {
  145. GUI::Painter painter(*this);
  146. painter.add_clip_rect(event.rect());
  147. painter.clear_rect(event.rect(), Color::from_argb(0));
  148. auto& audio_bitmap = choose_bitmap_from_volume();
  149. painter.blit({}, audio_bitmap, audio_bitmap.rect());
  150. if (show_percent()) {
  151. auto volume_text = m_audio_muted ? "mute" : DeprecatedString::formatted("{}%", m_audio_volume);
  152. painter.draw_text(Gfx::IntRect { 16, 3, 24, 16 }, volume_text, Gfx::FontDatabase::default_fixed_width_font(), Gfx::TextAlignment::TopLeft, palette().window_text());
  153. }
  154. }
  155. virtual void applet_area_rect_change_event(GUI::AppletAreaRectChangeEvent&) override
  156. {
  157. reposition_slider_window();
  158. }
  159. void open()
  160. {
  161. reposition_slider_window();
  162. m_slider_window->show();
  163. }
  164. void close()
  165. {
  166. m_slider_window->hide();
  167. }
  168. Gfx::Bitmap& choose_bitmap_from_volume()
  169. {
  170. if (m_audio_muted)
  171. return *m_volume_level_bitmaps.last().bitmap;
  172. for (auto& pair : m_volume_level_bitmaps) {
  173. if (m_audio_volume >= pair.volume_threshold)
  174. return *pair.bitmap;
  175. }
  176. VERIFY_NOT_REACHED();
  177. }
  178. void reposition_slider_window()
  179. {
  180. constexpr auto width { 50 };
  181. constexpr auto height { 125 };
  182. constexpr auto tray_and_taskbar_padding { 6 };
  183. constexpr auto icon_offset { (width - 16) / 2 };
  184. auto applet_rect = window()->applet_rect_on_screen();
  185. m_slider_window->set_rect(
  186. applet_rect.x() - icon_offset,
  187. applet_rect.y() - height - tray_and_taskbar_padding,
  188. width,
  189. height);
  190. }
  191. NonnullRefPtr<Audio::ConnectionToManagerServer> m_audio_client;
  192. Array<VolumeBitmapPair, 5> m_volume_level_bitmaps;
  193. bool m_show_percent { false };
  194. bool m_audio_muted { false };
  195. int m_audio_volume { 100 };
  196. RefPtr<GUI::Slider> m_slider;
  197. RefPtr<GUI::Window> m_slider_window;
  198. RefPtr<GUI::CheckBox> m_mute_box;
  199. RefPtr<GUI::CheckBox> m_percent_box;
  200. RefPtr<GUI::Frame> m_root_container;
  201. };
  202. ErrorOr<int> serenity_main(Main::Arguments arguments)
  203. {
  204. TRY(Core::System::pledge("stdio recvfd sendfd rpath wpath cpath unix thread"));
  205. auto app = TRY(GUI::Application::create(arguments));
  206. Config::pledge_domain("AudioApplet");
  207. TRY(Core::System::unveil("/tmp/session/%sid/portal/audiomanager", "rw"));
  208. TRY(Core::System::unveil("/res", "r"));
  209. TRY(Core::System::unveil(nullptr, nullptr));
  210. auto window = GUI::Window::construct();
  211. window->set_has_alpha_channel(true);
  212. window->set_title("Audio");
  213. window->set_window_type(GUI::WindowType::Applet);
  214. auto audio_widget = TRY(window->set_main_widget<AudioWidget>());
  215. window->show();
  216. // This affects the positioning, which depends on the window actually existing.
  217. bool should_show_percent = Config::read_bool("AudioApplet"sv, "Applet"sv, "ShowPercent"sv, audio_applet_show_percent_default);
  218. audio_widget->set_show_percent(should_show_percent);
  219. TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
  220. return app->exec();
  221. }