summaryrefslogtreecommitdiff
path: root/Applications/Launcher/main.cpp
blob: 9f4db8cbdd36fc86c0d4d7b7901ac5c123613e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <SharedGraphics/GraphicsBitmap.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GBoxLayout.h>
#include <LibCore/CConfigFile.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <LibCore/CUserInfo.h>

static GWindow* make_launcher_window();

void handle_sigchld(int)
{
    dbgprintf("Launcher(%d) Got SIGCHLD\n", getpid());
    int pid = waitpid(-1, nullptr, 0);
    dbgprintf("Launcher(%d) waitpid() returned %d\n", getpid(), pid);
    ASSERT(pid > 0);
}

int main(int argc, char** argv)
{
    chdir(get_current_user_home_path());
    GApplication app(argc, argv);

    signal(SIGCHLD, handle_sigchld);

    auto* launcher_window = make_launcher_window();
    launcher_window->set_should_exit_event_loop_on_close(true);
    launcher_window->show();

    return app.exec();
}

class LauncherButton final : public GButton {
public:
    LauncherButton(const String& name, const String& icon_path, const String& exec_path, GWidget* parent)
        : GButton(parent)
        , m_executable_path(exec_path)
    {
        set_tooltip(name);
        set_button_style(ButtonStyle::CoolBar);
        set_icon(GraphicsBitmap::load_from_file(icon_path));
        set_preferred_size({ 50, 50 });
        set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
        on_click = [this] (GButton&) {
            pid_t child_pid = fork();
            if (!child_pid) {
                int rc = execl(m_executable_path.characters(), m_executable_path.characters(), nullptr);
                if (rc < 0)
                    perror("execl");
            }
        };
    } virtual ~LauncherButton() { }

private:
    String m_executable_path;
};

GWindow* make_launcher_window()
{
    auto config = CConfigFile::get_for_app("Launcher");

    auto* window = new GWindow;
    window->set_title("Launcher");
    window->set_rect(50, 50, 50, config->groups().size() * 55 + 15);
    window->set_show_titlebar(false);

    auto* widget = new GWidget;
    widget->set_fill_with_background_color(true);
    widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
    widget->layout()->set_margins({ 5, 5, 5, 5 });
    window->set_main_widget(widget);

    for (auto& group : config->groups()) {
        new LauncherButton(config->read_entry(group, "Name", group),
                           config->read_entry(group, "Icon", ""),
                           config->read_entry(group, "Path", ""),
                           widget);
    }

    return window;
}