main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "SoundPlayerWidget.h"
  27. #include <LibAudio/ClientConnection.h>
  28. #include <LibGfx/CharacterBitmap.h>
  29. #include <LibGUI/AboutDialog.h>
  30. #include <LibGUI/Action.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/FilePicker.h>
  33. #include <LibGUI/Menu.h>
  34. #include <LibGUI/MenuBar.h>
  35. #include <LibGUI/Window.h>
  36. #include <stdio.h>
  37. int main(int argc, char** argv)
  38. {
  39. if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
  40. perror("pledge");
  41. return 1;
  42. }
  43. GUI::Application app(argc, argv);
  44. if (pledge("stdio shared_buffer accept rpath unix", nullptr) < 0) {
  45. perror("pledge");
  46. return 1;
  47. }
  48. auto audio_client = Audio::ClientConnection::construct();
  49. audio_client->handshake();
  50. if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
  51. perror("pledge");
  52. return 1;
  53. }
  54. auto window = GUI::Window::construct();
  55. window->set_title("SoundPlayer");
  56. window->set_resizable(false);
  57. window->set_rect(300, 300, 350, 140);
  58. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-sound-player.png"));
  59. auto menubar = make<GUI::MenuBar>();
  60. auto app_menu = GUI::Menu::construct("SoundPlayer");
  61. auto& player = window->set_main_widget<SoundPlayerWidget>(window, audio_client);
  62. if (argc > 1) {
  63. String path = argv[1];
  64. player.open_file(path);
  65. player.manager().play();
  66. }
  67. auto hide_scope = GUI::Action::create("Hide scope", { Mod_Ctrl, Key_H }, [&](GUI::Action& action) {
  68. action.set_checked(!action.is_checked());
  69. player.hide_scope(action.is_checked());
  70. });
  71. hide_scope->set_checkable(true);
  72. app_menu->add_action(GUI::CommonActions::make_open_action([&](auto&) {
  73. Optional<String> path = GUI::FilePicker::get_open_filepath("Open wav file...");
  74. if (path.has_value()) {
  75. player.open_file(path.value());
  76. }
  77. }));
  78. app_menu->add_action(move(hide_scope));
  79. app_menu->add_separator();
  80. app_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  81. app.quit();
  82. }));
  83. auto help_menu = GUI::Menu::construct("Help");
  84. help_menu->add_action(GUI::Action::create("About", [](auto&) {
  85. GUI::AboutDialog::show("SoundPlayer", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-sound-player.png"));
  86. }));
  87. menubar->add_menu(move(app_menu));
  88. menubar->add_menu(move(help_menu));
  89. app.set_menubar(move(menubar));
  90. window->show();
  91. return app.exec();
  92. }