Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
2025-10-16 19:48:02 +03:00
parent a67b787386
commit d368760f78
20 changed files with 9358 additions and 108 deletions

View File

@@ -139,6 +139,50 @@ App::App()
init_egl();
init_signal();
init_theme_portal();
{
auto const env = getenv("XDG_DATA_HOME");
if (env && *env) {
if (std::filesystem::exists(env)) {
m_data_home_dir = env;
}
}
if (m_data_home_dir.empty()) {
auto const home = getenv("HOME");
assert(home && *home);
m_data_home_dir = std::filesystem::path(home) / ".local" / "share";
std::filesystem::create_directories(m_data_home_dir);
}
m_data_home_dir /= "waylight";
std::filesystem::create_directories(m_data_home_dir);
}
m_db = std::make_shared<SQLite::Database>(m_data_home_dir / "data.db",
SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
SQLite::Statement(*m_db, R"(
CREATE TABLE IF NOT EXISTS ApplicationCache (
id INTEGER PRIMARY KEY NOT NULL,
type INTEGER NOT NULL,
desktop_entry_path TEXT NOT NULL,
terminal BOOL NOT NULL,
no_display BOOL NOT NULL,
path TEXT,
comment TEXT,
dbus_activatable BOOL NOT NULL
);
CREATE TABLE IF NOT EXISTS ApplicationActionCache (
id INTEGER PRIMARY KEY NOT NULL,
id_app INTEGER NOT NULL,
name TEXT NOT NULL,
exec TEXT,
icon TEXT,
FOREIGN KEY (id_app) REFERENCES ApplicationCache(id)
);
)")
.exec();
m_cache = Waylight::Cache(m_db);
}
App::~App()
@@ -146,9 +190,8 @@ App::~App()
if (m_sfd != -1)
close(m_sfd);
for (auto &[_, tex] : m_textures) {
for (auto &[_, tex] : m_textures)
UnloadTexture(tex);
}
destroy_layer_surface();
@@ -383,8 +426,10 @@ auto App::init_wayland() -> void
static zwp_text_input_v3_listener text_input_listener {};
{
auto ti_enter
= [](void *data, zwp_text_input_v3 *, wl_surface *surface) -> void {
auto ti_enter =
[](void *data, zwp_text_input_v3 *,
wl_surface *surface) // cppcheck-suppress constParameterPointer
-> void {
auto *app { static_cast<App *>(data) };
bool const focused_surface
= surface && surface == app->m_wayland.surface;
@@ -660,6 +705,13 @@ auto App::init_egl() -> void
ensure_egl_surface();
{
auto const *env = getenv("WAYLIGHT_DEBUG");
if (env && *env) {
SetTraceLogLevel(LOG_DEBUG);
}
}
InitWindow(m_win_w, m_win_h, "");
m_tr = std::make_shared<TextRenderer>();
@@ -1061,17 +1113,17 @@ auto App::update_text_input_state(
= std::max(1, static_cast<int32_t>(std::round(rect.width)));
int32_t const height
= std::max(1, static_cast<int32_t>(std::round(rect.height)));
bool const visible = cursor_info->visible;
bool const cur_visible = cursor_info->visible;
if (rect.x != m_ime.last_cursor_rect.x
|| rect.y != m_ime.last_cursor_rect.y
|| rect.width != m_ime.last_cursor_rect.width
|| rect.height != m_ime.last_cursor_rect.height
|| visible != m_ime.last_cursor_visible) {
|| cur_visible != m_ime.last_cursor_visible) {
zwp_text_input_v3_set_cursor_rectangle(
m_wayland.text_input, x, y, width, height);
m_ime.last_cursor_rect = rect;
m_ime.last_cursor_visible = visible;
m_ime.last_cursor_visible = cur_visible;
state_dirty = true;
}
}
@@ -1100,7 +1152,7 @@ auto App::pump_events() -> void
if (ret > 0 && (fds[0].revents & POLLIN)) {
if (prepared) {
wl_display_read_events(m_wayland.display);
prepared = false;
prepared = false; // cppcheck-suppress unreadVariable
}
} else if (prepared) {
wl_display_cancel_read(m_wayland.display);

View File

@@ -18,10 +18,12 @@ extern "C" {
#include <libportal/settings.h>
#undef namespace
}
#include <SQLiteCpp/SQLiteCpp.h>
#include <wayland-client.h>
#include <wayland-egl.h>
#include <xkbcommon/xkbcommon.h>
#include "Cache.hpp"
#include "IconRegistry.hpp"
#include "ImGui.hpp"
#include "TextRenderer.hpp"
@@ -126,13 +128,11 @@ private:
{
if (!xkb_state_v)
return false;
for (auto k : held) {
if (xkb_state_key_get_one_sym(
xkb_state_v, static_cast<xkb_keycode_t>(k + 8))
== sym)
return true;
}
return false;
return std::any_of(held.begin(), held.end(), [&](u32 const k) {
return (xkb_state_key_get_one_sym(
xkb_state_v, static_cast<xkb_keycode_t>(k + 8))
== sym);
});
}
auto is_sym_pressed(xkb_keysym_t sym) const -> bool
@@ -219,6 +219,7 @@ private:
enum_array<Theme, ColorScheme> m_themes { make_default_themes() };
Theme m_active_theme { Theme::Light };
IconRegistry m_ir;
std::optional<Waylight::Cache> m_cache;
int m_win_w { 800 };
int m_win_h { 600 };
@@ -227,5 +228,7 @@ private:
Color m_accent_color { 127, 127, 255, 255 };
std::filesystem::path m_data_home_dir {};
std::shared_ptr<SQLite::Database> m_db {};
int m_sfd { -1 };
};

345
src/Cache.cpp Normal file
View File

@@ -0,0 +1,345 @@
#include "Cache.hpp"
#include <algorithm>
#include <numeric>
#include <ranges>
#include <unordered_map>
#include <SQLiteCpp/Statement.h>
#include <SQLiteCpp/Transaction.h>
#include <mini/ini.h>
#include <raylib.h>
#include "common.hpp"
namespace Waylight {
Cache::Cache(std::shared_ptr<SQLite::Database> db)
: m_db(db)
{
{
auto const *env { getenv("XDG_DATA_DIRS") };
if (env && *env) {
std::ranges::copy(std::string_view(env) | std::views::split(':')
| std::views::transform([](auto &&s) {
return std::filesystem::path(s.begin(), s.end())
/ "applications";
})
| std::views::filter([](auto &&p) {
if (!std::filesystem::is_directory(p))
return false;
if (std::filesystem::directory_iterator(p)
== std::filesystem::directory_iterator {})
return false;
return true;
}),
std::back_inserter(m_app_dirs));
}
}
load();
auto total = std::accumulate(m_app_dirs.begin(), m_app_dirs.end(),
static_cast<usize>(0), [](usize acc, auto &&dir) {
return acc
+ std::count_if(std::filesystem::directory_iterator(dir),
std::filesystem::directory_iterator {}, [](auto &&entry) {
return entry.is_regular_file()
&& entry.path().extension() == ".desktop";
});
});
if (total != m_apps.size()) {
rescan();
}
TraceLog(LOG_DEBUG, std::format("Applications in cache:").c_str());
for (auto const &app : m_apps) {
TraceLog(LOG_DEBUG,
std::format("{}:", app.desktop_entry_path.string()).c_str());
if (app.comment)
TraceLog(
LOG_DEBUG, std::format(" - Comment: {}", *app.comment).c_str());
if (app.path)
TraceLog(LOG_DEBUG, std::format(" - Path: {}", *app.path).c_str());
TraceLog(
LOG_DEBUG, std::format(" - Terminal: {}", app.terminal).c_str());
TraceLog(
LOG_DEBUG, std::format(" - NoDisplay: {}", app.no_display).c_str());
TraceLog(LOG_DEBUG, std::format(" - Actions:").c_str());
for (auto const &action : app.actions) {
TraceLog(
LOG_DEBUG, std::format(" - Name: {}", action.name).c_str());
if (action.exec)
TraceLog(LOG_DEBUG,
std::format(" Exec: {}", *action.exec).c_str());
}
}
}
void Cache::rescan()
{
m_app_dirs.clear();
int id = 0;
for (auto const &dir : m_app_dirs) {
for (auto const &file : std::filesystem::directory_iterator(dir)) {
if (!file.is_regular_file())
continue;
if (file.path().extension() != ".desktop")
continue;
mINI::INIFile ini_file(file.path());
mINI::INIStructure ini;
ini_file.read(ini);
constexpr auto read_action =
[&](mINI::INIMap<std::string> const &section) {
return ApplicationCache::Action {
.name = section.get("Name"),
.exec = [&]() -> std::optional<std::string> {
if (section.has("Exec")) {
auto const s = section.get("Exec");
if (!s.empty())
return s;
}
return std::nullopt;
}(),
.icon = [&]() -> std::optional<std::variant<
std::filesystem::path, std::string>> {
if (section.has("Icon")) {
auto const icon_name = section.get("Icon");
if (!icon_name.empty()) {
if (icon_name[0] == '/') {
return std::filesystem::path(icon_name);
} else {
return icon_name;
}
}
}
return std::nullopt;
}(),
};
};
ApplicationCache app {
.id = id++,
.desktop_entry_path = file.path(),
.type =
[&]() {
auto const type_str { ini["Desktop Entry"].get(
"Type") };
auto type { ApplicationCache::Type::Application };
if (type_str == "Application")
type = ApplicationCache::Type::Application;
else if (type_str == "Link")
type = ApplicationCache::Type::Link;
else if (type_str == "Directory")
type = ApplicationCache::Type::Directory;
return type;
}(),
.terminal =
[&]() {
if (ini["Desktop Entry"].has("Terminal")) {
return ini["Desktop Entry"]["Terminal"] == "true"
? true
: false;
}
return false;
}(),
.no_display =
[&]() {
if (ini["Desktop Entry"].has("NoDisplay")) {
return ini["Desktop Entry"]["NoDisplay"] == "true"
? true
: false;
}
return false;
}(),
.path = [&]() -> std::optional<std::string> {
if (ini["Desktop Entry"].has("Path")) {
return ini["Desktop Entry"]["Path"];
}
return std::nullopt;
}(),
.comment = [&]() -> std::optional<std::string> {
if (ini["Desktop Entry"].has("Comment")) {
return ini["Desktop Entry"]["Comment"];
}
return std::nullopt;
}(),
.actions =
[&]() {
std::vector<ApplicationCache::Action> actions;
for (auto const &[_, v] : ini) {
try {
auto const action = read_action(v);
actions.push_back(action);
} catch (...) {
}
}
return actions;
}(),
.dbus_activatable =
[&]() {
if (ini["Desktop Entry"].has("DBusActivatable")) {
return ini["Desktop Entry"]["DBusActivatable"]
== "true"
? true
: false;
}
return false;
}(),
};
}
}
}
void Cache::dump()
{
SQLite::Transaction tx(*m_db);
SQLite::Statement(*m_db, "DELETE FROM ApplicationCache").exec();
SQLite::Statement(*m_db, "DELETE FROM ApplicationActionCache").exec();
try {
SQLite::Statement(
*m_db, "DELETE FROM sqlite_sequence WHERE name='ApplicationCache'")
.exec();
SQLite::Statement(*m_db,
"DELETE FROM sqlite_sequence WHERE name='ApplicationActionCache'")
.exec();
} catch (std::exception const &) {
}
SQLite::Statement ins_app(*m_db,
"INSERT INTO ApplicationCache(type, desktop_entry_path, terminal, "
"no_display, path, comment, dbus_activatable) VALUES (?,?,?,?,?,?,?)");
SQLite::Statement ins_act(*m_db,
"INSERT INTO ApplicationActionCache(id_app, name, exec, icon) VALUES "
"(?,?,?,?)");
for (auto &app : m_apps) {
ins_app.clearBindings();
ins_app.bind(1, static_cast<int>(app.type));
ins_app.bind(2, app.desktop_entry_path.string());
ins_app.bind(3, app.terminal ? 1 : 0);
ins_app.bind(4, app.no_display ? 1 : 0);
if (app.path)
ins_app.bind(5, *app.path);
else
ins_app.bind(5);
if (app.comment)
ins_app.bind(6, *app.comment);
else
ins_app.bind(6);
ins_app.bind(7, app.dbus_activatable ? 1 : 0);
ins_app.exec();
app.id = m_db->getLastInsertRowid();
for (auto const &action : app.actions) {
ins_act.clearBindings();
ins_act.bind(1, app.id);
ins_act.bind(2, action.name);
if (action.exec)
ins_act.bind(3, *action.exec);
else
ins_act.bind(3);
if (action.icon) {
std::string str;
if (auto const *s = std::get_if<std::string>(&*action.icon)) {
str = *s;
} else if (auto const *p
= std::get_if<std::filesystem::path>(&*action.icon)) {
str = std::filesystem::canonical(*p).string();
}
ins_act.bind(4, str);
} else {
ins_act.bind(4);
}
ins_act.exec();
}
}
tx.commit();
}
void Cache::load()
{
m_apps.clear();
SQLite::Statement getApps(*m_db,
"SELECT id, type, desktop_entry_path, terminal, no_display, path, "
"comment, dbus_activatable "
"FROM ApplicationCache");
std::unordered_map<std::int64_t, std::size_t> id_to_index;
while (getApps.executeStep()) {
ApplicationCache app {};
app.id = getApps.getColumn(0).getInt64();
app.type = static_cast<ApplicationCache::Type>(
getApps.getColumn(1).getInt());
app.desktop_entry_path
= std::filesystem::path(getApps.getColumn(2).getString());
app.terminal = getApps.getColumn(3).getInt() != 0;
app.no_display = getApps.getColumn(4).getInt() != 0;
if (!getApps.getColumn(5).isNull())
app.path = std::string(getApps.getColumn(5).getString());
else
app.path.reset();
if (!getApps.getColumn(6).isNull())
app.comment = std::string(getApps.getColumn(6).getString());
else
app.comment.reset();
app.dbus_activatable = getApps.getColumn(7).getInt() != 0;
id_to_index.emplace(app.id, m_apps.size());
m_apps.push_back(std::move(app));
}
if (m_apps.empty())
return;
SQLite::Statement getActions(*m_db,
"SELECT id_app, name, exec, icon "
"FROM ApplicationActionCache "
"ORDER BY id_app");
while (getActions.executeStep()) {
auto id_app = getActions.getColumn(0).getInt64();
auto it = id_to_index.find(id_app);
if (it == id_to_index.end())
continue;
ApplicationCache::Action action {};
action.name = std::string(getActions.getColumn(1).getString());
if (!getActions.getColumn(2).isNull())
action.exec = std::string(getActions.getColumn(2).getString());
else
action.exec.reset();
if (!getActions.getColumn(3).isNull()) {
auto const str = getActions.getColumn(3).getString();
if (str.at(0) == '/') {
action.icon
= std::filesystem::canonical(std::filesystem::path(str));
} else {
action.icon = str;
}
} else {
action.icon.reset();
}
m_apps[it->second].actions.push_back(std::move(action));
}
}
} // namespace Cache

60
src/Cache.hpp Normal file
View File

@@ -0,0 +1,60 @@
#pragma once
#include <filesystem>
#include <optional>
#include <string>
#include <variant>
#include <vector>
#include <SQLiteCpp/Database.h>
namespace Waylight {
struct ApplicationCache {
enum class Type {
Application,
Link,
Directory,
};
struct Action {
std::string name;
// May not exist if DBusActivable=true
std::optional<std::string> exec;
// Freedesktop Desktop Entry Spec 11.2 Table 3 says:
//
// If the name is an absolute path, the given file will be used.
// If the name is not an absolute path, the algorithm described in
// the Icon Theme Specification will be used to locate the icon.
//
// Thus, when deserializing, we will just check if it starts with /
// to determine type.
std::optional<std::variant<std::filesystem::path, std::string>> icon;
};
int id;
std::filesystem::path desktop_entry_path;
Type type { Type::Application };
bool terminal { false };
bool no_display { false };
std::optional<std::string> path;
std::optional<std::string> comment;
std::vector<Action> actions; // There should always be at least 1.
bool dbus_activatable {}; // Unimplemented for now.
};
struct Cache {
explicit Cache(std::shared_ptr<SQLite::Database> db);
void rescan();
void dump();
void load();
private:
std::vector<ApplicationCache> m_apps;
std::vector<std::filesystem::path> m_app_dirs;
std::shared_ptr<SQLite::Database> m_db;
};
} // namespace Cache

View File

@@ -70,7 +70,7 @@ static auto kde_get_theme() -> std::string const
home + "/.config/kdedefaults/kdeglobals",
};
for (auto p : paths) {
for (auto const &p : paths) {
std::ifstream f(p);
if (!f)
continue;
@@ -326,9 +326,8 @@ IconTheme::IconTheme(std::filesystem::path const &themes_directory_path)
}
IconRegistry::IconRegistry()
: m_preferred_theme(get_current_icon_theme())
{
m_preferred_theme = get_current_icon_theme();
std::vector<std::filesystem::path> theme_directory_paths;
{
@@ -346,21 +345,27 @@ IconRegistry::IconRegistry()
| std::views::transform([](auto &&s) {
return std::filesystem::path(s.begin(), s.end())
/ "icons";
})
| std::views::filter([](auto &&p) {
if (!std::filesystem::is_directory(p))
return false;
if (std::filesystem::directory_iterator(p)
== std::filesystem::directory_iterator {})
return false;
return true;
}),
std::back_inserter(theme_directory_paths));
}
}
{
std::filesystem::path const paths[] {
std::array<std::filesystem::path, 3> const paths {
"/usr/share/pixmaps",
"/usr/local/share/icons",
"/usr/share/icons",
};
for (auto const &path : paths) {
if (std::filesystem::exists(path))
theme_directory_paths.push_back(path);
}
std::copy_if(paths.begin(), paths.end(), theme_directory_paths.begin(),
[](auto const path) { return std::filesystem::exists(path); });
}
for (auto &&path : theme_directory_paths
@@ -368,7 +373,7 @@ IconRegistry::IconRegistry()
return std::filesystem::is_directory(path);
})) {
try {
m_themes.push_back({ path });
m_themes.push_back(IconTheme(path));
} catch (...) {
}
}
@@ -383,14 +388,8 @@ IconRegistry::IconRegistry()
std::stable_partition(
m_themes.begin(), m_themes.end(), [&](auto const &t) {
bool found { false };
for (auto const &e : t.names()) {
if (e == *m_preferred_theme) {
found = true;
break;
}
}
return found;
return std::any_of(t.names().begin(), t.names().end(),
[&](auto const &e) { return e == *m_preferred_theme; });
});
}
}

View File

@@ -30,7 +30,7 @@ private:
};
struct IconTheme {
IconTheme(std::filesystem::path const &themes_directory_path);
explicit IconTheme(std::filesystem::path const &themes_directory_path);
~IconTheme() = default;
constexpr auto inherits() const -> std::span<std::string const>

View File

@@ -121,18 +121,6 @@ auto clamp_preedit_index(int value, usize text_size) -> usize
return std::min(as_size, text_size);
}
auto slice_bytes(std::string_view text, usize begin, usize end)
-> std::string_view
{
if (begin > text.size())
begin = text.size();
if (end > text.size())
end = text.size();
if (end < begin)
end = begin;
return std::string_view(text.data() + begin, end - begin);
}
constexpr float HORIZONTAL_PADDING = 6.0f;
constexpr float VERTICAL_PADDING = 4.0f;
constexpr double CARET_BLINK_INTERVAL = 0.5;
@@ -228,9 +216,9 @@ void ImGui::ime_delete_surrounding(
if (it == m_ti_states.end())
return;
auto &state { it->second };
usize caret_byte = std::min(state.caret_byte, str.size());
usize start = before > caret_byte ? 0 : caret_byte - before;
usize end = std::min(caret_byte + after, str.size());
usize caret_byte { std::min(state.caret_byte, str.size()) };
usize start { before > caret_byte ? 0 : caret_byte - before };
usize end { std::min(caret_byte + after, str.size()) };
if (end > start) {
str.erase(start, end - start);
state.caret_byte = start;
@@ -289,10 +277,8 @@ void ImGui::ime_clear_preedit()
size_t utf8_length(std::string_view const &s)
{
size_t count = 0;
for (unsigned char c : s)
if ((c & 0xC0) != 0x80)
++count;
size_t count = std::count_if(
s.begin(), s.end(), [](auto const &c) { return (c & 0xC0) != 0x80; });
return count;
}
@@ -554,9 +540,9 @@ auto ImGui::text_input(usize id, std::pmr::string &str, Rectangle rec,
if (!options.multiline) {
std::string clip2;
clip2.reserve(m_clipboard.size());
for (auto ch : m_clipboard)
if (ch != '\n' && ch != '\r')
clip2.push_back(ch);
std::copy_if(m_clipboard.begin(), m_clipboard.end(),
clip2.begin(),
[](char ch) { return ch != '\n' && ch != '\r'; });
str.insert(caret_byte, clip2);
state.current_rune_idx += (int)utf8_length(clip2);
} else {
@@ -763,8 +749,8 @@ auto ImGui::text_input(usize id, std::pmr::string &str, Rectangle rec,
if (m_focused_id == id && state.caret_visible) {
float const caret_x = std::floor(origin + caret_offset + 0.5f);
Vector2 p0 { caret_x, std::floor(caret_top + 0.5f) };
Vector2 p1 { caret_x,
Vector2 const p0 { caret_x, std::floor(caret_top + 0.5f) };
Vector2 const p1 { caret_x,
std::floor((caret_top + caret_height) + 0.5f) };
DrawLineV(p0, p1, text_color);
}
@@ -784,15 +770,12 @@ auto ImGui::text_input(usize id, std::pmr::string &str, Rectangle rec,
auto ImGui::list_view(usize id, Rectangle bounds, usize elements,
std::function<Vector2(usize i)> draw_cb, ListViewOptions options) -> bool
{
auto &state { m_lv_states[id] };
auto const &state { m_lv_states[id] };
bool submitted { false };
bool select_next = m_next_lv_next;
m_next_lv_next = false;
bool select_previous = m_next_lv_previous;
m_next_lv_previous = false;
bool select_clear = m_next_lv_clear;
m_next_lv_clear = false;
BeginScissorMode(bounds.x, bounds.y, bounds.width, bounds.height);

View File

@@ -31,7 +31,7 @@ struct ImGui {
Color selection_text_color { WHITE };
};
ImGui(std::shared_ptr<TextRenderer> text_renderer);
explicit ImGui(std::shared_ptr<TextRenderer> text_renderer);
ImGui(ImGui const &) = delete;
auto operator=(ImGui const &) -> ImGui & = delete;

View File

@@ -347,11 +347,11 @@ TextRenderer::TextRenderer()
{
static char const msdf_vs_data[] {
#embed "base.vert"
, 0
, 0 // cppcheck-suppress syntaxError
};
static char const msdf_fs_data[] {
#embed "msdf.frag"
, 0
, 0 // cppcheck-suppress syntaxError
};
m_msdf_shader = LoadShaderFromMemory(msdf_vs_data, msdf_fs_data);
assert(IsShaderValid(m_msdf_shader));

View File

@@ -21,12 +21,20 @@ namespace msdfgen {
class FontHandle;
}
struct FontHandle {
auto operator()() const -> auto const & { return id; }
struct FontHandle { // cppcheck-supress noConstructor
FontHandle() = default;
auto operator()() const -> auto const &
{
if (id == 0xffffffff) {
throw std::runtime_error("Uninitialized FontHandle");
}
return id;
}
private:
friend struct TextRenderer;
usize id;
usize id { 0xffffffff };
};
struct FontRuntime;

View File

@@ -8,12 +8,12 @@
#include <rlgl.h>
#include <xkbcommon/xkbcommon.h>
#include <filesystem>
#include <optional>
auto App::tick() -> void
{
static std::pmr::string text_input_data {};
m_ime.bound_text = &text_input_data;
m_ime.bound_id = 1;
process_pending_text_input();
@@ -58,9 +58,14 @@ auto App::tick() -> void
static_cast<float>(GetScreenWidth()),
static_cast<float>(GetScreenHeight()),
};
auto result = m_gui->text_input(1, text_input_data, input_rect);
if (result.test(1))
;
if (auto const result
= m_gui->text_input(1, text_input_data, input_rect);
result.test(1)) {
m_ime.surrounding_dirty = true;
} else if (result.test(0)) {
text_input_data = "";
}
update_text_input_state(text_input_data, 1, input_rect);
}

View File

@@ -1,5 +1,7 @@
#pragma once
#include <algorithm>
#include <array>
#include <cstdint>
using u8 = std::uint8_t;
@@ -15,9 +17,8 @@ using isize = std::intptr_t;
[[maybe_unused]] static inline auto rune_to_string(uint32_t cp) -> char const *
{
static char utf8[5] = { 0 };
for (auto &c : utf8)
c = 0;
static std::array<char, 5> utf8 {};
std::fill(utf8.begin(), utf8.end(), 0);
if (cp < 0x80) {
utf8[0] = cp;
@@ -35,5 +36,5 @@ using isize = std::intptr_t;
utf8[3] = 0x80 | (cp & 0x3F);
}
return utf8;
return utf8.data();
}

View File

@@ -1,57 +1,84 @@
#include <algorithm>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <iostream>
#include <optional>
#include <print>
#include <signal.h>
#include <sys/file.h>
#include <sys/types.h>
#include <unistd.h>
#include <cpptrace/cpptrace.hpp>
#include <cpptrace/from_current.hpp>
#include <tinyfiledialogs.h>
#include "App.hpp"
bool signal_running();
std::optional<App> g_app{};
std::optional<App> g_app {};
auto main() -> int {
if (signal_running()) {
return 0;
}
auto main() -> int
{
if (signal_running()) {
return 0;
}
std::signal(SIGINT, [](int) {
if (g_app)
g_app->stop();
});
std::signal(SIGINT, [](int) {
if (g_app)
g_app->stop();
});
g_app.emplace();
g_app->run();
CPPTRACE_TRY
{
g_app.emplace();
g_app->run();
}
CPPTRACE_CATCH(std::exception const &e)
{
std::string what { e.what() };
std::ranges::replace(what, '"', '.');
std::ranges::replace(what, '\'', '.');
std::ranges::replace(what, '`', '.');
if (what.empty()) {
std::println(std::cerr, "Unexpected exception!");
} else {
std::println(std::cerr, "Unexpected exception! Error: {}", what);
}
cpptrace::from_current_exception().print();
tinyfd_messageBox(
"Unexpected exception", what.c_str(), "ok", "error", 1);
}
}
bool signal_running() {
const char *lock_path = "/tmp/waylight.lock";
int fd = open(lock_path, O_CREAT | O_RDWR, 0666);
if (fd == -1)
return false;
bool signal_running()
{
char const *lock_path = "/tmp/waylight.lock";
int fd = open(lock_path, O_CREAT | O_RDWR, 0666);
if (fd == -1)
return false;
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
FILE *f = fopen(lock_path, "r");
if (f) {
pid_t pid;
if (fscanf(f, "%d", &pid) == 1)
kill(pid, SIGUSR1);
fclose(f);
}
close(fd);
return true;
}
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
FILE *f = fopen(lock_path, "r");
if (f) {
pid_t pid;
if (fscanf(f, "%d", &pid) == 1)
kill(pid, SIGUSR1);
fclose(f);
}
close(fd);
return true;
}
if (ftruncate(fd, 0) == -1) {
close(fd);
unlink(lock_path);
return false;
}
if (ftruncate(fd, 0) == -1) {
close(fd);
unlink(lock_path);
return false;
}
dprintf(fd, "%d\n", getpid());
return false;
dprintf(fd, "%d\n", getpid());
return false;
}