Serenity Operating System
at portability 215 lines 7.5 kB view raw
1/* 2 * Copyright (c) 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 <AK/FileSystemPath.h> 28#include <AK/QuickSort.h> 29#include <LibCore/ConfigFile.h> 30#include <LibCore/DirIterator.h> 31#include <LibGUI/Action.h> 32#include <LibGUI/Application.h> 33#include <LibGUI/Desktop.h> 34#include <LibGUI/Menu.h> 35#include <LibGUI/MenuBar.h> 36#include <LibGUI/MessageBox.h> 37#include <LibGUI/WindowServerConnection.h> 38#include <LibGfx/Bitmap.h> 39#include <stdio.h> 40#include <sys/utsname.h> 41#include <unistd.h> 42 43#include "PowerDialog.h" 44 45//#define SYSTEM_MENU_DEBUG 46 47struct AppMetadata { 48 String executable; 49 String name; 50 String icon_path; 51 String category; 52}; 53Vector<AppMetadata> g_apps; 54 55HashMap<String, NonnullRefPtr<GUI::Menu>> g_app_category_menus; 56 57struct ThemeMetadata { 58 String name; 59 String path; 60}; 61 62Color g_menu_selection_color; 63 64Vector<ThemeMetadata> g_themes; 65RefPtr<GUI::Menu> g_themes_menu; 66 67static NonnullRefPtr<GUI::Menu> build_system_menu(); 68 69int main(int argc, char** argv) 70{ 71 GUI::Application app(argc, argv); 72 73 auto menu = build_system_menu(); 74 menu->realize_menu_if_needed(); 75 76 GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetSystemMenu>(menu->menu_id()); 77 78 if (pledge("stdio shared_buffer accept rpath proc exec", nullptr) < 0) { 79 perror("pledge"); 80 return 1; 81 } 82 83 if (unveil("/bin", "x")) { 84 perror("unveil"); 85 return 1; 86 } 87 88 if (unveil("/res", "r")) { 89 perror("unveil"); 90 return 1; 91 } 92 93 if (unveil("/etc/PowerOptions.ini", "r")) { 94 perror("unveil"); 95 return 1; 96 } 97 98 unveil(nullptr, nullptr); 99 100 return app.exec(); 101} 102 103NonnullRefPtr<GUI::Menu> build_system_menu() 104{ 105 HashTable<String> seen_app_categories; 106 { 107 Core::DirIterator dt("/res/apps", Core::DirIterator::SkipDots); 108 while (dt.has_next()) { 109 auto af_name = dt.next_path(); 110 auto af_path = String::format("/res/apps/%s", af_name.characters()); 111 auto af = Core::ConfigFile::open(af_path); 112 if (!af->has_key("App", "Name") || !af->has_key("App", "Executable")) 113 continue; 114 auto app_name = af->read_entry("App", "Name"); 115 auto app_executable = af->read_entry("App", "Executable"); 116 auto app_category = af->read_entry("App", "Category"); 117 auto app_icon_path = af->read_entry("Icons", "16x16"); 118 g_apps.append({ app_executable, app_name, app_icon_path, app_category }); 119 seen_app_categories.set(app_category); 120 } 121 } 122 123 Vector<String> sorted_app_categories; 124 for (auto& category : seen_app_categories) 125 sorted_app_categories.append(category); 126 quick_sort(sorted_app_categories.begin(), sorted_app_categories.end(), [](auto& a, auto& b) { return a < b; }); 127 128 u8 system_menu_name[] = { 0xc3, 0xb8, 0 }; 129 auto system_menu = GUI::Menu::construct(String((const char*)system_menu_name)); 130 131 // First we construct all the necessary app category submenus. 132 for (const auto& category : sorted_app_categories) { 133 134 if (g_app_category_menus.contains(category)) 135 continue; 136 auto category_menu = GUI::Menu::construct(category); 137 system_menu->add_submenu(category_menu); 138 g_app_category_menus.set(category, move(category_menu)); 139 } 140 141 // Then we create and insert all the app menu items into the right place. 142 int app_identifier = 0; 143 for (const auto& app : g_apps) { 144 RefPtr<Gfx::Bitmap> icon; 145 if (!app.icon_path.is_empty()) 146 icon = Gfx::Bitmap::load_from_file(app.icon_path); 147 148#ifdef SYSTEM_MENU_DEBUG 149 if (icon) 150 dbg() << "App " << app.name << " has icon with size " << icon->size(); 151#endif 152 153 auto parent_menu = g_app_category_menus.get(app.category).value_or(*system_menu); 154 parent_menu->add_action(GUI::Action::create(app.name, icon.ptr(), [app_identifier](auto&) { 155 dbg() << "Activated app with ID " << app_identifier; 156 if (fork() == 0) { 157 const auto& bin = g_apps[app_identifier].executable; 158 execl(bin.characters(), bin.characters(), nullptr); 159 ASSERT_NOT_REACHED(); 160 } 161 })); 162 ++app_identifier; 163 } 164 165 system_menu->add_separator(); 166 167 g_themes_menu = GUI::Menu::construct("Themes"); 168 169 system_menu->add_submenu(*g_themes_menu); 170 171 { 172 Core::DirIterator dt("/res/themes", Core::DirIterator::SkipDots); 173 while (dt.has_next()) { 174 auto theme_name = dt.next_path(); 175 auto theme_path = String::format("/res/themes/%s", theme_name.characters()); 176 g_themes.append({ FileSystemPath(theme_name).title(), theme_path }); 177 } 178 quick_sort(g_themes.begin(), g_themes.end(), [](auto& a, auto& b) { return a.name < b.name; }); 179 } 180 181 { 182 int theme_identifier = 0; 183 for (auto& theme : g_themes) { 184 g_themes_menu->add_action(GUI::Action::create(theme.name, [theme_identifier](auto&) { 185 auto& theme = g_themes[theme_identifier]; 186 dbg() << "Theme switched to " << theme.name << " at path " << theme.path; 187 auto response = GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetSystemTheme>(theme.path, theme.name); 188 ASSERT(response->success()); 189 })); 190 ++theme_identifier; 191 } 192 } 193 194 system_menu->add_separator(); 195 system_menu->add_action(GUI::Action::create("About...", Gfx::Bitmap::load_from_file("/res/icons/16x16/ladybug.png"), [](auto&) { 196 if (fork() == 0) { 197 execl("/bin/About", "/bin/About", nullptr); 198 ASSERT_NOT_REACHED(); 199 } 200 })); 201 system_menu->add_separator(); 202 system_menu->add_action(GUI::Action::create("Exit...", [](auto&) { 203 Vector<char const*> command = PowerDialog::show(); 204 205 if (command.size() == 0) 206 return; 207 208 if (fork() == 0) { 209 execv(command[0], const_cast<char* const*>(command.data())); 210 ASSERT_NOT_REACHED(); 211 } 212 })); 213 214 return system_menu; 215}