main.cpp 7.7 KB

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