main.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <LibCore/CConfigFile.h>
  2. #include <LibCore/CUserInfo.h>
  3. #include <LibGUI/GApplication.h>
  4. #include <LibGUI/GBoxLayout.h>
  5. #include <LibGUI/GButton.h>
  6. #include <LibGUI/GWidget.h>
  7. #include <LibGUI/GWindow.h>
  8. #include <LibDraw/GraphicsBitmap.h>
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <stdio.h>
  12. #include <sys/wait.h>
  13. #include <unistd.h>
  14. static GWindow* make_launcher_window();
  15. void handle_sigchld(int)
  16. {
  17. dbgprintf("Launcher(%d) Got SIGCHLD\n", getpid());
  18. int pid = waitpid(-1, nullptr, 0);
  19. dbgprintf("Launcher(%d) waitpid() returned %d\n", getpid(), pid);
  20. ASSERT(pid > 0);
  21. }
  22. int main(int argc, char** argv)
  23. {
  24. if (chdir(get_current_user_home_path().characters()) < 0)
  25. perror("chdir");
  26. GApplication app(argc, argv);
  27. signal(SIGCHLD, handle_sigchld);
  28. auto* launcher_window = make_launcher_window();
  29. launcher_window->show();
  30. return app.exec();
  31. }
  32. class LauncherButton final : public GButton {
  33. public:
  34. LauncherButton(const String& name, const String& icon_path, const String& exec_path, GWidget* parent)
  35. : GButton(parent)
  36. , m_executable_path(exec_path)
  37. {
  38. set_tooltip(name);
  39. set_button_style(ButtonStyle::CoolBar);
  40. set_icon(GraphicsBitmap::load_from_file(icon_path));
  41. set_preferred_size(50, 50);
  42. set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  43. on_click = [this](GButton&) {
  44. pid_t child_pid = fork();
  45. if (!child_pid) {
  46. int rc = execl(m_executable_path.characters(), m_executable_path.characters(), nullptr);
  47. if (rc < 0)
  48. perror("execl");
  49. }
  50. };
  51. }
  52. virtual ~LauncherButton() {}
  53. private:
  54. String m_executable_path;
  55. };
  56. GWindow* make_launcher_window()
  57. {
  58. auto config = CConfigFile::get_for_app("Launcher");
  59. auto vertical = config->read_bool_entry("Launcher", "Vertical", true);
  60. auto* window = new GWindow;
  61. window->set_title("Launcher");
  62. int launcher_size = (config->groups().size() - 1) * 50;
  63. window->set_rect(50, 50, vertical ? 50 : launcher_size, vertical ? launcher_size : 50);
  64. window->set_show_titlebar(false);
  65. window->set_window_type(GWindowType::Launcher);
  66. auto widget = GWidget::construct();
  67. widget->set_fill_with_background_color(true);
  68. widget->set_layout(make<GBoxLayout>(vertical ? Orientation::Vertical : Orientation::Horizontal));
  69. widget->layout()->set_spacing(0);
  70. widget->layout()->set_margins({ 5, 0, 5, 0 });
  71. window->set_main_widget(widget);
  72. for (auto& group : config->groups()) {
  73. if (group != "Launcher")
  74. new LauncherButton(config->read_entry(group, "Name", group),
  75. config->read_entry(group, "Icon", ""),
  76. config->read_entry(group, "Path", ""),
  77. widget);
  78. }
  79. return window;
  80. }