main.cpp 7.4 KB

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