Serenity Operating System
at hosted 109 lines 3.3 kB view raw
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#ifdef __serenity__ 68 if (pledge("stdio shared_buffer rpath cpath unix fattr", nullptr) < 0) { 69 perror("pledge"); 70 return 1; 71 } 72#endif 73 74 if (unveil("/res", "r") < 0) { 75 perror("unveil"); 76 return 1; 77 } 78 79 if (unveil("/tmp", "rwc") < 0) { 80 perror("unveil"); 81 return 1; 82 } 83 84 if (unveil("/etc/passwd", "r") < 0) { 85 perror("unveil"); 86 return 1; 87 } 88 89 unveil(nullptr, nullptr); 90 91 GUI::Application app(argc, argv); 92 93 auto window = GUI::Window::construct(); 94 window->set_title("UserName"); 95 window->set_window_type(GUI::WindowType::MenuApplet); 96 97 auto& widget = window->set_main_widget<UserNameWidget>(); 98 window->resize(widget.get_width(), 16); 99 window->show(); 100 101#ifdef __serenity__ 102 if (pledge("stdio shared_buffer rpath", nullptr) < 0) { 103 perror("pledge"); 104 return 1; 105 } 106#endif 107 108 return app.exec(); 109}