main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2020-2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  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 <LibGUI/Application.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGUI/Widget.h>
  29. #include <LibGUI/Window.h>
  30. #include <LibGfx/Font.h>
  31. #include <LibGfx/Palette.h>
  32. #include <stdio.h>
  33. class UserNameWidget final : public GUI::Widget {
  34. C_OBJECT(UserNameWidget)
  35. public:
  36. UserNameWidget()
  37. {
  38. m_username = getlogin();
  39. m_username_width = Gfx::Font::default_bold_font().width(m_username);
  40. }
  41. virtual ~UserNameWidget() override {}
  42. int get_width()
  43. {
  44. return m_username_width + menubar_menu_margin();
  45. }
  46. private:
  47. static int menubar_menu_margin() { return 4; }
  48. virtual void paint_event(GUI::PaintEvent& event) override
  49. {
  50. GUI::Painter painter(*this);
  51. painter.fill_rect(event.rect(), palette().window());
  52. painter.draw_text(event.rect(), m_username, Gfx::Font::default_bold_font(), Gfx::TextAlignment::Center, palette().window_text());
  53. }
  54. String m_username;
  55. int m_username_width;
  56. };
  57. int main(int argc, char** argv)
  58. {
  59. if (pledge("stdio shared_buffer rpath cpath unix fattr", nullptr) < 0) {
  60. perror("pledge");
  61. return 1;
  62. }
  63. if (unveil("/res", "r") < 0) {
  64. perror("unveil");
  65. return 1;
  66. }
  67. if (unveil("/tmp", "rwc") < 0) {
  68. perror("unveil");
  69. return 1;
  70. }
  71. if (unveil("/etc/passwd", "r") < 0) {
  72. perror("unveil");
  73. return 1;
  74. }
  75. unveil(nullptr, nullptr);
  76. GUI::Application app(argc, argv);
  77. auto window = GUI::Window::construct();
  78. window->set_title("UserName");
  79. window->set_window_type(GUI::WindowType::MenuApplet);
  80. auto& widget = window->set_main_widget<UserNameWidget>();
  81. window->resize(widget.get_width(), 16);
  82. window->show();
  83. if (pledge("stdio shared_buffer rpath", nullptr) < 0) {
  84. perror("pledge");
  85. return 1;
  86. }
  87. return app.exec();
  88. }