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