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