main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <LibCore/Timer.h>
  27. #include <LibGUI/Application.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/Widget.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibGfx/Font.h>
  32. #include <LibGfx/Palette.h>
  33. #include <stdio.h>
  34. #include <time.h>
  35. class ClockWidget final : public GUI::Widget {
  36. C_OBJECT(ClockWidget)
  37. public:
  38. ClockWidget()
  39. {
  40. m_time_width = Gfx::Font::default_bold_font().width("2222-22-22 22:22:22");
  41. m_timer = add<Core::Timer>(1000, [this] {
  42. static time_t last_update_time;
  43. time_t now = time(nullptr);
  44. if (now != last_update_time) {
  45. tick_clock();
  46. last_update_time = now;
  47. }
  48. });
  49. }
  50. virtual ~ClockWidget() override {}
  51. int get_width()
  52. {
  53. return m_time_width + menubar_menu_margin();
  54. }
  55. private:
  56. static int menubar_menu_margin() { return 2; }
  57. virtual void paint_event(GUI::PaintEvent& event) override
  58. {
  59. time_t now = time(nullptr);
  60. auto* tm = localtime(&now);
  61. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u",
  62. tm->tm_year + 1900,
  63. tm->tm_mon + 1,
  64. tm->tm_mday,
  65. tm->tm_hour,
  66. tm->tm_min,
  67. tm->tm_sec);
  68. GUI::Painter painter(*this);
  69. painter.fill_rect(event.rect(), palette().window());
  70. painter.draw_text(event.rect(), time_text, Gfx::Font::default_font(), Gfx::TextAlignment::Center, palette().window_text());
  71. }
  72. void tick_clock()
  73. {
  74. update();
  75. }
  76. RefPtr<Core::Timer> m_timer;
  77. int m_time_width;
  78. };
  79. int main(int argc, char** argv)
  80. {
  81. if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
  82. perror("pledge");
  83. return 1;
  84. }
  85. GUI::Application app(argc, argv);
  86. if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
  87. perror("pledge");
  88. return 1;
  89. }
  90. auto window = GUI::Window::construct();
  91. window->set_title("Clock");
  92. window->set_window_type(GUI::WindowType::MenuApplet);
  93. auto& widget = window->set_main_widget<ClockWidget>();
  94. window->resize(widget.get_width(), 16);
  95. window->show();
  96. if (unveil("/res", "r") < 0) {
  97. perror("unveil");
  98. return 1;
  99. }
  100. unveil(nullptr, nullptr);
  101. return app.exec();
  102. }