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. class AudioWidget final : public GUI::Widget {
  32. C_OBJECT(AudioWidget)
  33. public:
  34. AudioWidget()
  35. : GUI::Widget(nullptr)
  36. {
  37. m_audio_client = make<Audio::ClientConnection>();
  38. m_audio_client->on_muted_state_change = [this](bool muted) {
  39. if (m_audio_muted == muted)
  40. return;
  41. m_audio_muted = muted;
  42. update();
  43. };
  44. m_unmuted_bitmap = Gfx::Bitmap::load_from_file("/res/icons/audio-unmuted.png");
  45. m_muted_bitmap = Gfx::Bitmap::load_from_file("/res/icons/audio-muted.png");
  46. }
  47. virtual ~AudioWidget() override {}
  48. private:
  49. virtual void mousedown_event(GUI::MouseEvent& event) override
  50. {
  51. if (event.button() != GUI::MouseButton::Left)
  52. return;
  53. m_audio_client->set_muted(!m_audio_muted);
  54. update();
  55. }
  56. virtual void paint_event(GUI::PaintEvent& event) override
  57. {
  58. GUI::Painter painter(*this);
  59. painter.add_clip_rect(event.rect());
  60. painter.clear_rect(event.rect(), Color::from_rgba(0));
  61. auto& audio_bitmap = m_audio_muted ? *m_muted_bitmap : *m_unmuted_bitmap;
  62. painter.blit({}, audio_bitmap, audio_bitmap.rect());
  63. }
  64. OwnPtr<Audio::ClientConnection> m_audio_client;
  65. RefPtr<Gfx::Bitmap> m_muted_bitmap;
  66. RefPtr<Gfx::Bitmap> m_unmuted_bitmap;
  67. bool m_audio_muted { false };
  68. };
  69. int main(int argc, char** argv)
  70. {
  71. if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
  72. perror("pledge");
  73. return 1;
  74. }
  75. GUI::Application app(argc, argv);
  76. if (pledge("stdio shared_buffer accept rpath unix", nullptr) < 0) {
  77. perror("pledge");
  78. return 1;
  79. }
  80. auto window = GUI::Window::construct();
  81. window->set_has_alpha_channel(true);
  82. window->set_title("Audio");
  83. window->set_window_type(GUI::WindowType::MenuApplet);
  84. window->resize(12, 16);
  85. auto widget = AudioWidget::construct();
  86. window->set_main_widget(widget);
  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. }