2024-04-10 19:42:21 +00:00
|
|
|
#include <luna/Sort.h>
|
|
|
|
#include <luna/StringBuilder.h>
|
2024-09-07 14:52:31 +00:00
|
|
|
#include <os/Config.h>
|
2024-04-10 19:42:21 +00:00
|
|
|
#include <os/Directory.h>
|
2023-09-11 17:15:26 +00:00
|
|
|
#include <os/File.h>
|
2024-04-10 19:42:21 +00:00
|
|
|
#include <os/FileSystem.h>
|
2024-02-01 20:58:44 +00:00
|
|
|
#include <os/IPC.h>
|
2023-08-15 13:33:44 +00:00
|
|
|
#include <os/Process.h>
|
2024-02-01 20:58:44 +00:00
|
|
|
#include <os/ipc/Launcher.h>
|
2023-08-16 14:49:08 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/wait.h>
|
2023-08-15 10:56:55 +00:00
|
|
|
#include <ui/App.h>
|
2023-08-15 13:33:44 +00:00
|
|
|
#include <ui/Button.h>
|
2023-08-15 12:42:21 +00:00
|
|
|
#include <ui/Container.h>
|
|
|
|
#include <ui/Image.h>
|
2023-08-15 10:56:55 +00:00
|
|
|
#include <ui/Layout.h>
|
|
|
|
|
2023-09-20 17:45:01 +00:00
|
|
|
static constexpr ui::Color TASKBAR_COLOR = ui::Color::from_rgb(83, 83, 83);
|
|
|
|
|
2024-02-03 18:16:39 +00:00
|
|
|
static OwnedPtr<os::IPC::Client> launcher_client;
|
2024-02-01 20:58:44 +00:00
|
|
|
|
2023-08-16 14:49:08 +00:00
|
|
|
void sigchld_handler(int)
|
|
|
|
{
|
|
|
|
wait(nullptr);
|
|
|
|
}
|
|
|
|
|
2024-07-31 17:46:02 +00:00
|
|
|
void sigquit_handler(int)
|
2024-04-10 19:42:21 +00:00
|
|
|
{
|
|
|
|
// Reload the taskbar by exec-ing the executable, resetting everything.
|
|
|
|
StringView args[] = { "/usr/bin/taskbar" };
|
|
|
|
os::Process::exec(args[0], { args, 1 });
|
|
|
|
}
|
|
|
|
|
2024-02-01 20:58:44 +00:00
|
|
|
Result<void> create_widget_group_for_app(ui::HorizontalLayout& layout, StringView path, StringView icon)
|
2023-09-27 16:52:17 +00:00
|
|
|
{
|
2024-04-10 19:42:21 +00:00
|
|
|
auto* button = TRY(make<ui::Button>(ui::Rect { 0, 0, 50, 50 }));
|
2023-09-27 16:52:17 +00:00
|
|
|
layout.add_widget(*button);
|
|
|
|
|
2024-04-10 19:42:21 +00:00
|
|
|
auto* container = TRY(
|
|
|
|
make<ui::Container>(ui::Rect { 0, 0, 50, 50 }, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center));
|
2023-09-27 16:52:17 +00:00
|
|
|
button->set_widget(*container);
|
2024-02-01 20:58:44 +00:00
|
|
|
button->set_action([=] {
|
|
|
|
os::Launcher::LaunchDetachedRequest request;
|
|
|
|
SET_IPC_STRING(request.command, path.chars());
|
2024-02-03 18:16:39 +00:00
|
|
|
launcher_client->send_async(request);
|
2024-02-01 20:58:44 +00:00
|
|
|
});
|
2023-09-27 16:52:17 +00:00
|
|
|
|
|
|
|
auto image = TRY(ui::ImageWidget::load(icon));
|
|
|
|
container->set_widget(*image);
|
|
|
|
|
|
|
|
image.leak();
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-04-10 19:42:21 +00:00
|
|
|
struct ApplicationFile
|
|
|
|
{
|
|
|
|
String name;
|
|
|
|
String command;
|
|
|
|
String icon;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<ApplicationFile> s_app_files;
|
|
|
|
|
|
|
|
// Pretty much copied from init.cpp.
|
|
|
|
static Result<void> load_application_file(const os::Path& path)
|
|
|
|
{
|
|
|
|
os::println("[taskbar] reading app file: %s", path.name().chars());
|
|
|
|
|
2024-09-07 14:52:31 +00:00
|
|
|
auto file = TRY(os::ConfigFile::open(path));
|
2024-04-10 19:42:21 +00:00
|
|
|
|
|
|
|
ApplicationFile app_file;
|
|
|
|
|
2024-09-07 14:52:31 +00:00
|
|
|
app_file.name = TRY(String::from_string_view(file->read_string_or("Name", "")));
|
|
|
|
if (app_file.name.is_empty())
|
2024-04-10 19:42:21 +00:00
|
|
|
{
|
2024-09-07 14:52:31 +00:00
|
|
|
os::println("[taskbar] app file is missing 'Name' entry, aborting!");
|
|
|
|
return {};
|
2024-04-10 19:42:21 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 14:52:31 +00:00
|
|
|
app_file.command = TRY(String::from_string_view(file->read_string_or("Command", "")));
|
|
|
|
if (app_file.command.is_empty())
|
2024-04-10 19:42:21 +00:00
|
|
|
{
|
2024-09-07 14:52:31 +00:00
|
|
|
os::println("[taskbar] app file is missing 'Command' entry, aborting!");
|
2024-04-10 19:42:21 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-09-07 14:52:31 +00:00
|
|
|
app_file.icon = TRY(String::from_string_view(file->read_string_or("Icon", "")));
|
|
|
|
if (app_file.icon.is_empty())
|
2024-04-10 19:42:21 +00:00
|
|
|
{
|
2024-09-07 14:52:31 +00:00
|
|
|
os::println("[taskbar] app file is missing 'Icon' entry, aborting!");
|
2024-04-10 19:42:21 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
os::println("[taskbar] loaded app %s into memory", app_file.name.chars());
|
|
|
|
|
|
|
|
TRY(s_app_files.try_append(move(app_file)));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Result<void> load_app_files_from_path(StringView path)
|
|
|
|
{
|
|
|
|
os::println("[taskbar] loading app files from %s", path.chars());
|
|
|
|
|
|
|
|
auto dir = TRY(os::Directory::open(path));
|
|
|
|
|
|
|
|
auto services = TRY(dir->list_names(os::Directory::Filter::ParentAndBase));
|
|
|
|
sort(services.begin(), services.end(), String::compare);
|
|
|
|
|
|
|
|
for (const auto& entry : services) TRY(load_application_file({ dir->fd(), entry.view() }));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-02-01 20:15:31 +00:00
|
|
|
Result<int> luna_main(int, char**)
|
2023-08-15 10:56:55 +00:00
|
|
|
{
|
|
|
|
ui::App app;
|
2024-02-01 20:58:44 +00:00
|
|
|
TRY(app.init("/tmp/wsys.sock"));
|
2023-08-15 10:56:55 +00:00
|
|
|
|
2023-10-09 20:14:34 +00:00
|
|
|
TRY(os::EventLoop::the().register_signal_handler(SIGCHLD, sigchld_handler));
|
2024-07-31 17:46:02 +00:00
|
|
|
TRY(os::EventLoop::the().register_signal_handler(SIGQUIT, sigquit_handler));
|
2023-08-16 14:49:08 +00:00
|
|
|
|
2024-02-03 18:16:39 +00:00
|
|
|
launcher_client = TRY(os::IPC::Client::connect("/tmp/launch.sock", false));
|
2024-02-01 20:58:44 +00:00
|
|
|
|
2023-08-15 10:56:55 +00:00
|
|
|
ui::Rect screen = app.screen_rect();
|
|
|
|
|
|
|
|
ui::Rect bar = ui::Rect { ui::Point { 0, screen.height - 50 }, screen.width, 50 };
|
|
|
|
|
2023-09-27 16:14:32 +00:00
|
|
|
auto window = TRY(ui::Window::create(bar, ui::WindowType::System));
|
2023-08-15 10:56:55 +00:00
|
|
|
app.set_main_window(window);
|
2023-10-10 20:24:11 +00:00
|
|
|
|
2023-09-20 17:45:01 +00:00
|
|
|
window->set_background(TASKBAR_COLOR);
|
2024-02-04 12:35:50 +00:00
|
|
|
window->set_special_attributes(ui::UNFOCUSEABLE);
|
2023-08-15 10:56:55 +00:00
|
|
|
|
2023-09-27 16:52:17 +00:00
|
|
|
ui::HorizontalLayout layout(ui::Margins { 0, 0, 0, 0 }, ui::AdjustHeight::Yes, ui::AdjustWidth::No);
|
2023-08-15 10:56:55 +00:00
|
|
|
window->set_main_widget(layout);
|
|
|
|
|
2024-04-10 19:42:21 +00:00
|
|
|
load_app_files_from_path("/usr/share/applications/");
|
|
|
|
|
|
|
|
auto home = TRY(os::FileSystem::home_directory());
|
|
|
|
|
|
|
|
StringBuilder sb;
|
|
|
|
TRY(sb.add(home.view()));
|
|
|
|
TRY(sb.add("/.applications/"_sv));
|
|
|
|
auto local_app_file_dir = TRY(sb.string());
|
|
|
|
|
|
|
|
load_app_files_from_path(local_app_file_dir.view());
|
|
|
|
|
|
|
|
for (const auto& app_file : s_app_files)
|
|
|
|
{
|
|
|
|
create_widget_group_for_app(layout, app_file.command.view(), app_file.icon.view());
|
|
|
|
}
|
2023-10-09 20:05:30 +00:00
|
|
|
|
2023-08-15 10:56:55 +00:00
|
|
|
return app.run();
|
|
|
|
}
|