Serenity Operating System
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 virtual void mousedown_event(GUI::MouseEvent& event) override
83 {
84 if (event.button() != GUI::MouseButton::Left)
85 return;
86
87 pid_t pid = fork();
88 if (pid < 0) {
89 perror("fork");
90 } else if (pid == 0) {
91 execl("/bin/Calendar", "Calendar", nullptr);
92 perror("execl");
93 ASSERT_NOT_REACHED();
94 }
95 }
96
97 void tick_clock()
98 {
99 update();
100 }
101
102 RefPtr<Core::Timer> m_timer;
103 int m_time_width;
104};
105
106int main(int argc, char** argv)
107{
108#ifdef __serenity__
109 if (pledge("stdio shared_buffer accept rpath unix cpath fattr exec proc", nullptr) < 0) {
110 perror("pledge");
111 return 1;
112 }
113#endif
114
115 GUI::Application app(argc, argv);
116
117#ifdef __serenity__
118 if (pledge("stdio shared_buffer accept rpath exec proc", nullptr) < 0) {
119 perror("pledge");
120 return 1;
121 }
122#endif
123
124 auto window = GUI::Window::construct();
125 window->set_title("Clock");
126 window->set_window_type(GUI::WindowType::MenuApplet);
127
128 auto& widget = window->set_main_widget<ClockWidget>();
129 window->resize(widget.get_width(), 16);
130 window->show();
131
132 if (unveil("/res", "r") < 0) {
133 perror("unveil");
134 return 1;
135 }
136
137 if (unveil("/bin/Calendar", "x") < 0) {
138 perror("unveil");
139 return 1;
140 }
141
142 unveil(nullptr, nullptr);
143
144 return app.exec();
145}