Serenity Operating System
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
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
35class UserNameWidget final : public GUI::Widget {
36 C_OBJECT(UserNameWidget)
37public:
38 UserNameWidget()
39 {
40 m_username = getlogin();
41 m_username_width = Gfx::Font::default_bold_font().width(m_username);
42 }
43
44 virtual ~UserNameWidget() override {}
45
46 int get_width()
47 {
48 return m_username_width + menubar_menu_margin();
49 }
50
51private:
52 static int menubar_menu_margin() { return 4; }
53
54 virtual void paint_event(GUI::PaintEvent& event) override
55 {
56 GUI::Painter painter(*this);
57 painter.fill_rect(event.rect(), palette().window());
58 painter.draw_text(event.rect(), m_username, Gfx::Font::default_bold_font(), Gfx::TextAlignment::Center, palette().window_text());
59 }
60
61 String m_username;
62 int m_username_width;
63};
64
65int main(int argc, char** argv)
66{
67 if (pledge("stdio shared_buffer rpath cpath unix fattr", nullptr) < 0) {
68 perror("pledge");
69 return 1;
70 }
71
72 if (unveil("/res", "r") < 0) {
73 perror("unveil");
74 return 1;
75 }
76
77 if (unveil("/tmp", "rwc") < 0) {
78 perror("unveil");
79 return 1;
80 }
81
82 if (unveil("/etc/passwd", "r") < 0) {
83 perror("unveil");
84 return 1;
85 }
86
87 unveil(nullptr, nullptr);
88
89 GUI::Application app(argc, argv);
90
91 auto window = GUI::Window::construct();
92 window->set_title("UserName");
93 window->set_window_type(GUI::WindowType::MenuApplet);
94
95 auto widget = UserNameWidget::construct();
96
97 window->resize(widget->get_width(), 16);
98 window->set_main_widget(widget);
99 window->show();
100
101 if (pledge("stdio shared_buffer rpath", nullptr) < 0) {
102 perror("pledge");
103 return 1;
104 }
105
106 return app.exec();
107}