main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibAudio/ClientConnection.h>
  27. #include <LibGUI/Application.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/Widget.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibGfx/Bitmap.h>
  32. #include <LibGfx/Font.h>
  33. class AudioWidget final : public GUI::Widget {
  34. C_OBJECT(AudioWidget)
  35. public:
  36. AudioWidget()
  37. {
  38. m_audio_client = make<Audio::ClientConnection>();
  39. m_audio_client->on_muted_state_change = [this](bool muted) {
  40. if (m_audio_muted == muted)
  41. return;
  42. m_audio_muted = muted;
  43. update();
  44. };
  45. m_unmuted_bitmap = Gfx::Bitmap::load_from_file("/res/icons/audio-unmuted.png");
  46. m_muted_bitmap = Gfx::Bitmap::load_from_file("/res/icons/audio-muted.png");
  47. }
  48. virtual ~AudioWidget() override {}
  49. private:
  50. virtual void mousedown_event(GUI::MouseEvent& event) override
  51. {
  52. if (event.button() != GUI::MouseButton::Left)
  53. return;
  54. m_audio_client->set_muted(!m_audio_muted);
  55. update();
  56. }
  57. virtual void paint_event(GUI::PaintEvent& event) override
  58. {
  59. GUI::Painter painter(*this);
  60. painter.add_clip_rect(event.rect());
  61. painter.clear_rect(event.rect(), Color::from_rgba(0));
  62. auto& audio_bitmap = m_audio_muted ? *m_muted_bitmap : *m_unmuted_bitmap;
  63. painter.blit({}, audio_bitmap, audio_bitmap.rect());
  64. }
  65. OwnPtr<Audio::ClientConnection> m_audio_client;
  66. RefPtr<Gfx::Bitmap> m_muted_bitmap;
  67. RefPtr<Gfx::Bitmap> m_unmuted_bitmap;
  68. bool m_audio_muted { false };
  69. };
  70. int main(int argc, char** argv)
  71. {
  72. if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
  73. perror("pledge");
  74. return 1;
  75. }
  76. GUI::Application app(argc, argv);
  77. if (pledge("stdio shared_buffer accept rpath unix", nullptr) < 0) {
  78. perror("pledge");
  79. return 1;
  80. }
  81. auto window = GUI::Window::construct();
  82. window->set_has_alpha_channel(true);
  83. window->set_title("Audio");
  84. window->set_window_type(GUI::WindowType::MenuApplet);
  85. window->resize(12, 16);
  86. window->set_main_widget<AudioWidget>();
  87. window->show();
  88. if (unveil("/res", "r") < 0) {
  89. perror("unveil");
  90. return 1;
  91. }
  92. unveil(nullptr, nullptr);
  93. if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
  94. perror("pledge");
  95. return 1;
  96. }
  97. return app.exec();
  98. }