main.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <SharedGraphics/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. chdir(get_current_user_home_path());
  25. GApplication app(argc, argv);
  26. signal(SIGCHLD, handle_sigchld);
  27. auto* launcher_window = make_launcher_window();
  28. launcher_window->set_should_exit_event_loop_on_close(true);
  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* window = new GWindow;
  60. window->set_title("Launcher");
  61. window->set_rect(50, 50, 50, config->groups().size() * 55 + 15);
  62. window->set_show_titlebar(false);
  63. auto* widget = new GWidget;
  64. widget->set_fill_with_background_color(true);
  65. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  66. widget->layout()->set_margins({ 5, 5, 5, 5 });
  67. window->set_main_widget(widget);
  68. for (auto& group : config->groups()) {
  69. new LauncherButton(config->read_entry(group, "Name", group),
  70. config->read_entry(group, "Icon", ""),
  71. config->read_entry(group, "Path", ""),
  72. widget);
  73. }
  74. return window;
  75. }