main.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibAudio/ClientConnection.h>
  8. #include <LibCore/ConfigFile.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/CheckBox.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/Painter.h>
  14. #include <LibGUI/Slider.h>
  15. #include <LibGUI/Widget.h>
  16. #include <LibGUI/Window.h>
  17. #include <LibGfx/Bitmap.h>
  18. #include <LibGfx/FontDatabase.h>
  19. #include <LibGfx/Palette.h>
  20. class AudioWidget final : public GUI::Widget {
  21. C_OBJECT(AudioWidget)
  22. public:
  23. AudioWidget(NonnullRefPtr<Core::ConfigFile> config, int initial_volume, bool initial_mute_state)
  24. : m_audio_client(Audio::ClientConnection::construct())
  25. , m_config(move(config))
  26. , m_show_percent(m_config->read_bool_entry("Applet", "ShowPercent", false))
  27. , m_audio_muted(initial_mute_state)
  28. , m_audio_volume(initial_volume)
  29. {
  30. m_audio_client->on_muted_state_change = [this](bool muted) {
  31. if (m_audio_muted == muted)
  32. return;
  33. m_mute_box->set_checked(!m_audio_muted);
  34. m_slider->set_enabled(!muted);
  35. m_audio_muted = muted;
  36. update();
  37. };
  38. m_audio_client->on_main_mix_volume_change = [this](int volume) {
  39. m_audio_volume = volume;
  40. if (!m_audio_muted)
  41. update();
  42. };
  43. m_volume_level_bitmaps.append({ 66, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-high.png") });
  44. m_volume_level_bitmaps.append({ 33, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-medium.png") });
  45. m_volume_level_bitmaps.append({ 1, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-low.png") });
  46. m_volume_level_bitmaps.append({ 0, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-zero.png") });
  47. m_volume_level_bitmaps.append({ 0, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/audio-volume-muted.png") });
  48. m_slider_window = add<GUI::Window>(window());
  49. m_slider_window->set_frameless(true);
  50. m_slider_window->set_resizable(false);
  51. m_slider_window->set_minimizable(false);
  52. m_slider_window->on_active_input_change = [this](bool is_active_input) {
  53. if (!is_active_input)
  54. close();
  55. };
  56. m_root_container = m_slider_window->set_main_widget<GUI::Label>();
  57. m_root_container->set_fill_with_background_color(true);
  58. m_root_container->set_layout<GUI::VerticalBoxLayout>();
  59. m_root_container->layout()->set_margins({ 4, 0 });
  60. m_root_container->layout()->set_spacing(0);
  61. m_root_container->set_frame_thickness(2);
  62. m_root_container->set_frame_shape(Gfx::FrameShape::Container);
  63. m_root_container->set_frame_shadow(Gfx::FrameShadow::Raised);
  64. m_percent_box = m_root_container->add<GUI::CheckBox>("\xE2\x84\xB9");
  65. m_percent_box->set_fixed_size(27, 16);
  66. m_percent_box->set_tooltip(m_show_percent ? "Hide percent" : "Show percent");
  67. m_percent_box->set_checked(m_show_percent);
  68. m_percent_box->on_checked = [&](bool show_percent) {
  69. m_show_percent = show_percent;
  70. if (!m_show_percent) {
  71. window()->resize(16, 16);
  72. m_percent_box->set_tooltip("Show percent");
  73. } else {
  74. window()->resize(44, 16);
  75. m_percent_box->set_tooltip("Hide percent");
  76. }
  77. reposition_slider_window();
  78. GUI::Application::the()->hide_tooltip();
  79. m_config->write_bool_entry("Applet", "ShowPercent", m_show_percent);
  80. auto sync_success = m_config->sync();
  81. if (!sync_success)
  82. warnln("Could not write applet configuration.");
  83. };
  84. m_slider = m_root_container->add<GUI::VerticalSlider>();
  85. m_slider->set_max(20);
  86. int non_log_volume = sqrt(100 * m_audio_volume);
  87. m_slider->set_value(-(non_log_volume / 5.0f) + 20);
  88. m_slider->set_knob_size_mode(GUI::Slider::KnobSizeMode::Proportional);
  89. m_slider->on_change = [&](int value) {
  90. int volume = clamp((20 - value) * 5, 0, 100);
  91. double volume_log = ((volume / 100.0) * (volume / 100.0)) * 100.0;
  92. m_audio_client->set_main_mix_volume(static_cast<i32>(volume_log));
  93. update();
  94. };
  95. m_mute_box = m_root_container->add<GUI::CheckBox>("\xE2\x9D\x8C");
  96. m_mute_box->set_fixed_size(27, 16);
  97. m_mute_box->set_checked(m_audio_muted);
  98. m_mute_box->set_tooltip(m_audio_muted ? "Unmute" : "Mute");
  99. m_mute_box->on_checked = [&](bool is_muted) {
  100. m_mute_box->set_tooltip(is_muted ? "Unmute" : "Mute");
  101. m_audio_client->set_muted(is_muted);
  102. GUI::Application::the()->hide_tooltip();
  103. };
  104. }
  105. virtual ~AudioWidget() override { }
  106. private:
  107. virtual void mousedown_event(GUI::MouseEvent& event) override
  108. {
  109. if (event.button() == GUI::MouseButton::Left) {
  110. if (!m_slider_window->is_visible())
  111. open();
  112. else
  113. close();
  114. return;
  115. }
  116. if (event.button() == GUI::MouseButton::Right) {
  117. m_audio_client->set_muted(!m_audio_muted);
  118. update();
  119. }
  120. }
  121. virtual void mousewheel_event(GUI::MouseEvent& event) override
  122. {
  123. if (m_audio_muted)
  124. return;
  125. int new_slider_value = m_slider->value() + event.wheel_delta() / 4;
  126. m_slider->set_value(new_slider_value);
  127. update();
  128. }
  129. virtual void paint_event(GUI::PaintEvent& event) override
  130. {
  131. GUI::Painter painter(*this);
  132. painter.add_clip_rect(event.rect());
  133. painter.clear_rect(event.rect(), Color::from_rgba(0));
  134. auto& audio_bitmap = choose_bitmap_from_volume();
  135. painter.blit({}, audio_bitmap, audio_bitmap.rect());
  136. if (m_show_percent) {
  137. auto volume_text = m_audio_muted ? "mute" : String::formatted("{}%", m_audio_volume);
  138. painter.draw_text({ 16, 3, 24, 16 }, volume_text, Gfx::FontDatabase::default_fixed_width_font(), Gfx::TextAlignment::TopLeft, palette().window_text());
  139. }
  140. }
  141. void open()
  142. {
  143. reposition_slider_window();
  144. m_slider_window->show();
  145. }
  146. void close()
  147. {
  148. m_slider_window->hide();
  149. }
  150. Gfx::Bitmap& choose_bitmap_from_volume()
  151. {
  152. if (m_audio_muted)
  153. return *m_volume_level_bitmaps.last().bitmap;
  154. for (auto& pair : m_volume_level_bitmaps) {
  155. if (m_audio_volume >= pair.volume_threshold)
  156. return *pair.bitmap;
  157. }
  158. VERIFY_NOT_REACHED();
  159. }
  160. void reposition_slider_window()
  161. {
  162. auto applet_rect = window()->applet_rect_on_screen();
  163. m_slider_window->set_rect(applet_rect.x() - 20, applet_rect.y() - 106, 50, 100);
  164. }
  165. struct VolumeBitmapPair {
  166. int volume_threshold { 0 };
  167. RefPtr<Gfx::Bitmap> bitmap;
  168. };
  169. NonnullRefPtr<Audio::ClientConnection> m_audio_client;
  170. NonnullRefPtr<Core::ConfigFile> m_config;
  171. Vector<VolumeBitmapPair, 5> m_volume_level_bitmaps;
  172. bool m_show_percent { false };
  173. bool m_audio_muted { false };
  174. int m_audio_volume { 100 };
  175. RefPtr<GUI::Slider> m_slider;
  176. RefPtr<GUI::Window> m_slider_window;
  177. RefPtr<GUI::CheckBox> m_mute_box;
  178. RefPtr<GUI::CheckBox> m_percent_box;
  179. RefPtr<GUI::Label> m_root_container;
  180. };
  181. int main(int argc, char** argv)
  182. {
  183. if (pledge("stdio recvfd sendfd rpath wpath cpath unix", nullptr) < 0) {
  184. perror("pledge");
  185. return 1;
  186. }
  187. auto config = Core::ConfigFile::open_for_app("AudioApplet", Core::ConfigFile::AllowWriting::Yes);
  188. // To not upset the audio server state, we responsibly read this once.
  189. auto audio_master_config = Core::ConfigFile::open_for_app("Audio");
  190. auto app = GUI::Application::construct(argc, argv);
  191. auto window = GUI::Window::construct();
  192. window->set_has_alpha_channel(true);
  193. window->set_title("Audio");
  194. window->set_window_type(GUI::WindowType::Applet);
  195. window->set_main_widget<AudioWidget>(config, audio_master_config->read_num_entry("Master", "Volume", 100), audio_master_config->read_bool_entry("Master", "Mute", false));
  196. window->show();
  197. // This positioning code depends on the window actually existing.
  198. if (!config->read_bool_entry("Applet", "ShowPercent")) {
  199. window->resize(16, 16);
  200. } else {
  201. window->resize(44, 16);
  202. }
  203. if (unveil("/res", "r") < 0) {
  204. perror("unveil");
  205. return 1;
  206. }
  207. if (unveil(config->filename().characters(), "rwc") < 0) {
  208. perror("unveil");
  209. return 1;
  210. }
  211. unveil(nullptr, nullptr);
  212. if (pledge("stdio recvfd sendfd rpath wpath cpath", nullptr) < 0) {
  213. perror("pledge");
  214. return 1;
  215. }
  216. return app->exec();
  217. }