main.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "ShutdownDialog.h"
  27. #include <AK/LexicalPath.h>
  28. #include <AK/QuickSort.h>
  29. #include <LibCore/ConfigFile.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibCore/StandardPaths.h>
  32. #include <LibDesktop/AppFile.h>
  33. #include <LibGUI/Action.h>
  34. #include <LibGUI/ActionGroup.h>
  35. #include <LibGUI/Application.h>
  36. #include <LibGUI/FileIconProvider.h>
  37. #include <LibGUI/Icon.h>
  38. #include <LibGUI/Menu.h>
  39. #include <LibGUI/WindowServerConnection.h>
  40. #include <LibGfx/Bitmap.h>
  41. #include <serenity.h>
  42. #include <spawn.h>
  43. //#define SYSTEM_MENU_DEBUG
  44. struct AppMetadata {
  45. String executable;
  46. String name;
  47. String category;
  48. };
  49. Vector<AppMetadata> g_apps;
  50. struct ThemeMetadata {
  51. String name;
  52. String path;
  53. };
  54. Color g_menu_selection_color;
  55. Vector<ThemeMetadata> g_themes;
  56. RefPtr<GUI::Menu> g_themes_menu;
  57. GUI::ActionGroup g_themes_group;
  58. static Vector<String> discover_apps_and_categories();
  59. static NonnullRefPtr<GUI::Menu> build_system_menu();
  60. int main(int argc, char** argv)
  61. {
  62. auto app = GUI::Application::construct(argc, argv);
  63. app->set_quit_when_last_window_deleted(false);
  64. auto menu = build_system_menu();
  65. menu->realize_menu_if_needed();
  66. GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetSystemMenu>(menu->menu_id());
  67. if (pledge("stdio sendfd shared_buffer accept rpath proc exec", nullptr) < 0) {
  68. perror("pledge");
  69. return 1;
  70. }
  71. if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
  72. perror("chdir");
  73. return 1;
  74. }
  75. if (unveil("/bin", "x")) {
  76. perror("unveil");
  77. return 1;
  78. }
  79. if (unveil("/res", "r")) {
  80. perror("unveil");
  81. return 1;
  82. }
  83. unveil(nullptr, nullptr);
  84. return app->exec();
  85. }
  86. Vector<String> discover_apps_and_categories()
  87. {
  88. HashTable<String> seen_app_categories;
  89. Desktop::AppFile::for_each([&](auto af) {
  90. g_apps.append({ af->executable(), af->name(), af->category() });
  91. seen_app_categories.set(af->category());
  92. });
  93. quick_sort(g_apps, [](auto& a, auto& b) { return a.name < b.name; });
  94. Vector<String> sorted_app_categories;
  95. for (auto& category : seen_app_categories) {
  96. sorted_app_categories.append(category);
  97. }
  98. quick_sort(sorted_app_categories);
  99. return sorted_app_categories;
  100. }
  101. NonnullRefPtr<GUI::Menu> build_system_menu()
  102. {
  103. const Vector<String> sorted_app_categories = discover_apps_and_categories();
  104. auto system_menu = GUI::Menu::construct("\xE2\x9A\xA1"); // HIGH VOLTAGE SIGN
  105. // First we construct all the necessary app category submenus.
  106. HashMap<String, NonnullRefPtr<GUI::Menu>> app_category_menus;
  107. auto category_icons = Core::ConfigFile::open("/res/icons/SystemMenu.ini");
  108. for (const auto& category : sorted_app_categories) {
  109. if (app_category_menus.contains(category))
  110. continue;
  111. auto& category_menu = system_menu->add_submenu(category);
  112. auto category_icon_path = category_icons->read_entry("16x16", category);
  113. if (!category_icon_path.is_empty()) {
  114. auto icon = Gfx::Bitmap::load_from_file(category_icon_path);
  115. category_menu.set_icon(icon);
  116. }
  117. app_category_menus.set(category, category_menu);
  118. }
  119. // Then we create and insert all the app menu items into the right place.
  120. int app_identifier = 0;
  121. for (const auto& app : g_apps) {
  122. auto icon = GUI::FileIconProvider::icon_for_executable(app.executable).bitmap_for_size(16);
  123. #ifdef SYSTEM_MENU_DEBUG
  124. if (icon)
  125. dbg() << "App " << app.name << " has icon with size " << icon->size();
  126. #endif
  127. auto parent_menu = app_category_menus.get(app.category).value_or(*system_menu);
  128. parent_menu->add_action(GUI::Action::create(app.name, icon, [app_identifier](auto&) {
  129. dbg() << "Activated app with ID " << app_identifier;
  130. const auto& bin = g_apps[app_identifier].executable;
  131. pid_t child_pid;
  132. const char* argv[] = { bin.characters(), nullptr };
  133. if ((errno = posix_spawn(&child_pid, bin.characters(), nullptr, nullptr, const_cast<char**>(argv), environ))) {
  134. perror("posix_spawn");
  135. } else {
  136. if (disown(child_pid) < 0)
  137. perror("disown");
  138. }
  139. }));
  140. ++app_identifier;
  141. }
  142. system_menu->add_separator();
  143. g_themes_group.set_exclusive(true);
  144. g_themes_group.set_unchecking_allowed(false);
  145. g_themes_menu = &system_menu->add_submenu("Themes");
  146. g_themes_menu->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/themes.png"));
  147. {
  148. Core::DirIterator dt("/res/themes", Core::DirIterator::SkipDots);
  149. while (dt.has_next()) {
  150. auto theme_name = dt.next_path();
  151. auto theme_path = String::formatted("/res/themes/{}", theme_name);
  152. g_themes.append({ LexicalPath(theme_name).title(), theme_path });
  153. }
  154. quick_sort(g_themes, [](auto& a, auto& b) { return a.name < b.name; });
  155. }
  156. auto current_theme_name = GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::GetSystemTheme>()->theme_name();
  157. {
  158. int theme_identifier = 0;
  159. for (auto& theme : g_themes) {
  160. auto action = GUI::Action::create_checkable(theme.name, [theme_identifier](auto&) {
  161. auto& theme = g_themes[theme_identifier];
  162. dbg() << "Theme switched to " << theme.name << " at path " << theme.path;
  163. auto response = GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetSystemTheme>(theme.path, theme.name);
  164. ASSERT(response->success());
  165. });
  166. if (theme.name == current_theme_name)
  167. action->set_checked(true);
  168. g_themes_group.add_action(action);
  169. g_themes_menu->add_action(action);
  170. ++theme_identifier;
  171. }
  172. }
  173. system_menu->add_separator();
  174. system_menu->add_action(GUI::Action::create("Run...", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-run.png"), [](auto&) {
  175. pid_t child_pid;
  176. const char* argv[] = { "/bin/Run", nullptr };
  177. if ((errno = posix_spawn(&child_pid, "/bin/Run", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  178. perror("posix_spawn");
  179. } else {
  180. if (disown(child_pid) < 0)
  181. perror("disown");
  182. }
  183. }));
  184. system_menu->add_action(GUI::Action::create("About SerenityOS", Gfx::Bitmap::load_from_file("/res/icons/16x16/ladybug.png"), [](auto&) {
  185. pid_t child_pid;
  186. const char* argv[] = { "/bin/About", nullptr };
  187. if ((errno = posix_spawn(&child_pid, "/bin/About", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  188. perror("posix_spawn");
  189. } else {
  190. if (disown(child_pid) < 0)
  191. perror("disown");
  192. }
  193. }));
  194. system_menu->add_separator();
  195. system_menu->add_action(GUI::Action::create("Exit...", Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png"), [](auto&) {
  196. auto command = ShutdownDialog::show();
  197. if (command.size() == 0)
  198. return;
  199. pid_t child_pid;
  200. if ((errno = posix_spawn(&child_pid, command[0], nullptr, nullptr, const_cast<char**>(command.data()), environ))) {
  201. perror("posix_spawn");
  202. } else {
  203. if (disown(child_pid) < 0)
  204. perror("disown");
  205. }
  206. }));
  207. return system_menu;
  208. }