Compare commits

...

11 Commits

Author SHA1 Message Date
13589e1db9 Fix clipboard being bugged out and not pasting or copying
Signed-off-by: Slendi <slendi@socopon.com>
2025-11-26 19:14:24 +02:00
cb2c866a35 forgor to set laeyr 💀
Signed-off-by: Slendi <slendi@socopon.com>
2025-11-26 18:24:10 +02:00
a85f4371c1 bye bye KDE blur
Signed-off-by: Slendi <slendi@socopon.com>
2025-11-26 18:12:36 +02:00
8859504fed Add config and command executor
Signed-off-by: Slendi <slendi@socopon.com>
2025-10-22 11:27:13 +03:00
237208d972 Add Waylight namespace
Signed-off-by: Slendi <slendi@socopon.com>
2025-10-17 00:20:39 +03:00
42a9de3ba3 Use in-house inotify watcher
Signed-off-by: Slendi <slendi@socopon.com>
2025-10-17 00:03:29 +03:00
6f45a3bc70 Make cppcheck happy
Signed-off-by: Slendi <slendi@socopon.com>
2025-10-16 20:36:17 +03:00
f61710010d More cache work, should be fully done for apps now
Signed-off-by: Slendi <slendi@socopon.com>
2025-10-16 20:25:06 +03:00
d368760f78 A lot
Signed-off-by: Slendi <slendi@socopon.com>
2025-10-16 19:48:02 +03:00
a67b787386 Oops forgot I disabled an if
Signed-off-by: Slendi <slendi@socopon.com>
2025-10-15 03:41:19 +03:00
86ecd128f8 Gnome moment
Signed-off-by: Slendi <slendi@socopon.com>
2025-10-15 03:41:02 +03:00
28 changed files with 10837 additions and 297 deletions

View File

@@ -1,10 +1,25 @@
cmake_minimum_required(VERSION 3.16)
project(waylight LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
find_program(cppcheck_exe NAMES cppcheck)
if (cppcheck_exe)
set(cppcheck_opts --enable=all --inline-suppr --quiet --suppressions-list=${PROJECT_SOURCE_DIR}/cppcheck.supp)
add_custom_target(run_cppcheck
COMMAND ${cppcheck_exe}
--std=c++20 --enable=all --inline-suppr --quiet
--suppressions-list=${PROJECT_SOURCE_DIR}/cppcheck.supp
--project=${CMAKE_BINARY_DIR}/compile_commands.json
--check-level=exhaustive
-i ${CMAKE_BINARY_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
endif()
find_package(PkgConfig REQUIRED)
pkg_check_modules(WAYLAND_CLIENT REQUIRED IMPORTED_TARGET wayland-client)
pkg_check_modules(WAYLAND_EGL REQUIRED IMPORTED_TARGET wayland-egl)
@@ -60,6 +75,32 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(lunasvg)
FetchContent_Declare(
SQLiteCpp
GIT_REPOSITORY https://github.com/SRombauts/SQLiteCpp.git
GIT_TAG "3.3.3"
GIT_SHALLOW 1
)
FetchContent_MakeAvailable(SQLiteCpp)
FetchContent_Declare(
cpptrace
GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git
GIT_TAG "v1.0.1"
GIT_SHALLOW 1
)
FetchContent_MakeAvailable(cpptrace)
FetchContent_Declare(
tomlplusplus
GIT_REPOSITORY https://github.com/marzer/tomlplusplus
GIT_TAG "v3.4.0"
GIT_SHALLOW 1
)
FetchContent_MakeAvailable(tomlplusplus)
add_subdirectory(vendor)
find_program(WAYLAND_SCANNER wayland-scanner REQUIRED)
pkg_get_variable(WAYLAND_PROTOCOLS_DIR wayland-protocols pkgdatadir)
@@ -112,20 +153,23 @@ set(WLR_LAYER_SHELL_XML
set(TEXT_INPUT_XML
"${WAYLAND_PROTOCOLS_DIR}/unstable/text-input/text-input-unstable-v3.xml"
)
set(WLR_DATA_CONTROL_XML
"${CMAKE_CURRENT_SOURCE_DIR}/protocols/wlr-data-control-unstable-v1.xml"
)
set(GEN_C_HEADERS
"${GEN_DIR}/xdg-shell-client-protocol.h"
"${GEN_DIR}/wlr-layer-shell-unstable-v1-client-protocol.h"
"${GEN_DIR}/ext-background-effect-v1-client-protocol.h"
"${GEN_DIR}/blur-client-protocol.h"
"${GEN_DIR}/text-input-unstable-v3-client-protocol.h"
"${GEN_DIR}/wlr-data-control-unstable-v1-client-protocol.h"
)
set(GEN_C_PRIVATES
"${GEN_DIR}/xdg-shell-protocol.c"
"${GEN_DIR}/wlr-layer-shell-unstable-v1-protocol.c"
"${GEN_DIR}/ext-background-effect-v1-protocol.c"
"${GEN_DIR}/blur-protocol.c"
"${GEN_DIR}/text-input-unstable-v3-protocol.c"
"${GEN_DIR}/wlr-data-control-unstable-v1-protocol.c"
)
add_custom_command(
@@ -141,10 +185,10 @@ add_custom_command(
# text-input-unstable-v3
COMMAND "${WAYLAND_SCANNER}" client-header "${TEXT_INPUT_XML}" "${GEN_DIR}/text-input-unstable-v3-client-protocol.h"
COMMAND "${WAYLAND_SCANNER}" private-code "${TEXT_INPUT_XML}" "${GEN_DIR}/text-input-unstable-v3-protocol.c"
# org-kde-win-blur
COMMAND "${WAYLAND_SCANNER}" client-header "${CMAKE_CURRENT_SOURCE_DIR}/blur.xml" "${GEN_DIR}/blur-client-protocol.h"
COMMAND "${WAYLAND_SCANNER}" private-code "${CMAKE_CURRENT_SOURCE_DIR}/blur.xml" "${GEN_DIR}/blur-protocol.c"
DEPENDS "${XDG_SHELL_XML}" "${WLR_LAYER_SHELL_XML}" "${TEXT_INPUT_XML}"
# wlr-data-control
COMMAND "${WAYLAND_SCANNER}" client-header "${WLR_DATA_CONTROL_XML}" "${GEN_DIR}/wlr-data-control-unstable-v1-client-protocol.h"
COMMAND "${WAYLAND_SCANNER}" private-code "${WLR_DATA_CONTROL_XML}" "${GEN_DIR}/wlr-data-control-unstable-v1-protocol.c"
DEPENDS "${XDG_SHELL_XML}" "${WLR_LAYER_SHELL_XML}" "${TEXT_INPUT_XML}" "${WLR_DATA_CONTROL_XML}"
COMMENT "Generating Wayland + wlr-layer-shell client headers and private code"
VERBATIM
)
@@ -156,7 +200,10 @@ add_custom_target(generate_protocols ALL
add_executable(waylight
${GEN_C_PRIVATES}
${CMAKE_CURRENT_SOURCE_DIR}/src/Config.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/IconRegistry.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Cache.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/InotifyWatcher.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/TextRenderer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ImGui.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/App.cpp
@@ -180,11 +227,15 @@ target_link_libraries(waylight PRIVATE
PkgConfig::FONTCONFIG
PkgConfig::HARFBUZZ
tomlplusplus::tomlplusplus
cpptrace::cpptrace
tinyfiledialogs
mINI
raylib
msdfgen::msdfgen-core
msdfgen::msdfgen-ext
lunasvg::lunasvg
SQLiteCpp
m
dl

View File

@@ -1,28 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="blur">
<copyright><![CDATA[
SPDX-FileCopyrightText: 2015 Martin Gräßlin
SPDX-FileCopyrightText: 2015 Marco Martin
SPDX-License-Identifier: LGPL-2.1-or-later
]]></copyright>
<interface name="org_kde_kwin_blur_manager" version="1">
<request name="create">
<arg name="id" type="new_id" interface="org_kde_kwin_blur"/>
<arg name="surface" type="object" interface="wl_surface"/>
</request>
<request name="unset">
<arg name="surface" type="object" interface="wl_surface"/>
</request>
</interface>
<interface name="org_kde_kwin_blur" version="1">
<request name="commit">
</request>
<request name="set_region">
<arg name="region" type="object" interface="wl_region" allow-null="true"/>
</request>
<request name="release" type="destructor">
<description summary="release the blur object"/>
</request>
</interface>
</protocol>

8
cppcheck.supp Normal file
View File

@@ -0,0 +1,8 @@
unusedFunction
shadowFunction
missingIncludeSystem
ignoredReturnValue
*:build/generated/*
*:build/_deps/*
*:vendor/*

View File

@@ -28,6 +28,9 @@
libxkbcommon
fontconfig
harfbuzz
sqlite
zenity
boost
];
buildInputs = with pkgs; [
cmake

View File

@@ -0,0 +1,278 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="wlr_data_control_unstable_v1">
<copyright>
Copyright © 2018 Simon Ser
Copyright © 2019 Ivan Molodetskikh
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that copyright notice and this permission
notice appear in supporting documentation, and that the name of
the copyright holders not be used in advertising or publicity
pertaining to distribution of the software without specific,
written prior permission. The copyright holders make no
representations about the suitability of this software for any
purpose. It is provided "as is" without express or implied
warranty.
THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
</copyright>
<description summary="control data devices">
This protocol allows a privileged client to control data devices. In
particular, the client will be able to manage the current selection and take
the role of a clipboard manager.
Warning! The protocol described in this file is experimental and
backward incompatible changes may be made. Backward compatible changes
may be added together with the corresponding interface version bump.
Backward incompatible changes are done by bumping the version number in
the protocol and interface names and resetting the interface version.
Once the protocol is to be declared stable, the 'z' prefix and the
version number in the protocol and interface names are removed and the
interface version number is reset.
</description>
<interface name="zwlr_data_control_manager_v1" version="2">
<description summary="manager to control data devices">
This interface is a manager that allows creating per-seat data device
controls.
</description>
<request name="create_data_source">
<description summary="create a new data source">
Create a new data source.
</description>
<arg name="id" type="new_id" interface="zwlr_data_control_source_v1"
summary="data source to create"/>
</request>
<request name="get_data_device">
<description summary="get a data device for a seat">
Create a data device that can be used to manage a seat's selection.
</description>
<arg name="id" type="new_id" interface="zwlr_data_control_device_v1"/>
<arg name="seat" type="object" interface="wl_seat"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy the manager">
All objects created by the manager will still remain valid, until their
appropriate destroy request has been called.
</description>
</request>
</interface>
<interface name="zwlr_data_control_device_v1" version="2">
<description summary="manage a data device for a seat">
This interface allows a client to manage a seat's selection.
When the seat is destroyed, this object becomes inert.
</description>
<request name="set_selection">
<description summary="copy data to the selection">
This request asks the compositor to set the selection to the data from
the source on behalf of the client.
The given source may not be used in any further set_selection or
set_primary_selection requests. Attempting to use a previously used
source is a protocol error.
To unset the selection, set the source to NULL.
</description>
<arg name="source" type="object" interface="zwlr_data_control_source_v1"
allow-null="true"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy this data device">
Destroys the data device object.
</description>
</request>
<event name="data_offer">
<description summary="introduce a new wlr_data_control_offer">
The data_offer event introduces a new wlr_data_control_offer object,
which will subsequently be used in either the
wlr_data_control_device.selection event (for the regular clipboard
selections) or the wlr_data_control_device.primary_selection event (for
the primary clipboard selections). Immediately following the
wlr_data_control_device.data_offer event, the new data_offer object
will send out wlr_data_control_offer.offer events to describe the MIME
types it offers.
</description>
<arg name="id" type="new_id" interface="zwlr_data_control_offer_v1"/>
</event>
<event name="selection">
<description summary="advertise new selection">
The selection event is sent out to notify the client of a new
wlr_data_control_offer for the selection for this device. The
wlr_data_control_device.data_offer and the wlr_data_control_offer.offer
events are sent out immediately before this event to introduce the data
offer object. The selection event is sent to a client when a new
selection is set. The wlr_data_control_offer is valid until a new
wlr_data_control_offer or NULL is received. The client must destroy the
previous selection wlr_data_control_offer, if any, upon receiving this
event.
The first selection event is sent upon binding the
wlr_data_control_device object.
</description>
<arg name="id" type="object" interface="zwlr_data_control_offer_v1"
allow-null="true"/>
</event>
<event name="finished">
<description summary="this data control is no longer valid">
This data control object is no longer valid and should be destroyed by
the client.
</description>
</event>
<!-- Version 2 additions -->
<event name="primary_selection" since="2">
<description summary="advertise new primary selection">
The primary_selection event is sent out to notify the client of a new
wlr_data_control_offer for the primary selection for this device. The
wlr_data_control_device.data_offer and the wlr_data_control_offer.offer
events are sent out immediately before this event to introduce the data
offer object. The primary_selection event is sent to a client when a
new primary selection is set. The wlr_data_control_offer is valid until
a new wlr_data_control_offer or NULL is received. The client must
destroy the previous primary selection wlr_data_control_offer, if any,
upon receiving this event.
If the compositor supports primary selection, the first
primary_selection event is sent upon binding the
wlr_data_control_device object.
</description>
<arg name="id" type="object" interface="zwlr_data_control_offer_v1"
allow-null="true"/>
</event>
<request name="set_primary_selection" since="2">
<description summary="copy data to the primary selection">
This request asks the compositor to set the primary selection to the
data from the source on behalf of the client.
The given source may not be used in any further set_selection or
set_primary_selection requests. Attempting to use a previously used
source is a protocol error.
To unset the primary selection, set the source to NULL.
The compositor will ignore this request if it does not support primary
selection.
</description>
<arg name="source" type="object" interface="zwlr_data_control_source_v1"
allow-null="true"/>
</request>
<enum name="error" since="2">
<entry name="used_source" value="1"
summary="source given to set_selection or set_primary_selection was already used before"/>
</enum>
</interface>
<interface name="zwlr_data_control_source_v1" version="1">
<description summary="offer to transfer data">
The wlr_data_control_source object is the source side of a
wlr_data_control_offer. It is created by the source client in a data
transfer and provides a way to describe the offered data and a way to
respond to requests to transfer the data.
</description>
<enum name="error">
<entry name="invalid_offer" value="1"
summary="offer sent after wlr_data_control_device.set_selection"/>
</enum>
<request name="offer">
<description summary="add an offered MIME type">
This request adds a MIME type to the set of MIME types advertised to
targets. Can be called several times to offer multiple types.
Calling this after wlr_data_control_device.set_selection is a protocol
error.
</description>
<arg name="mime_type" type="string"
summary="MIME type offered by the data source"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy this source">
Destroys the data source object.
</description>
</request>
<event name="send">
<description summary="send the data">
Request for data from the client. Send the data as the specified MIME
type over the passed file descriptor, then close it.
</description>
<arg name="mime_type" type="string" summary="MIME type for the data"/>
<arg name="fd" type="fd" summary="file descriptor for the data"/>
</event>
<event name="cancelled">
<description summary="selection was cancelled">
This data source is no longer valid. The data source has been replaced
by another data source.
The client should clean up and destroy this data source.
</description>
</event>
</interface>
<interface name="zwlr_data_control_offer_v1" version="1">
<description summary="offer to transfer data">
A wlr_data_control_offer represents a piece of data offered for transfer
by another client (the source client). The offer describes the different
MIME types that the data can be converted to and provides the mechanism
for transferring the data directly from the source client.
</description>
<request name="receive">
<description summary="request that the data is transferred">
To transfer the offered data, the client issues this request and
indicates the MIME type it wants to receive. The transfer happens
through the passed file descriptor (typically created with the pipe
system call). The source client writes the data in the MIME type
representation requested and then closes the file descriptor.
The receiving client reads from the read end of the pipe until EOF and
then closes its end, at which point the transfer is complete.
This request may happen multiple times for different MIME types.
</description>
<arg name="mime_type" type="string"
summary="MIME type desired by receiver"/>
<arg name="fd" type="fd" summary="file descriptor for data transfer"/>
</request>
<request name="destroy" type="destructor">
<description summary="destroy this offer">
Destroys the data offer object.
</description>
</request>
<event name="offer">
<description summary="advertise offered MIME type">
Sent immediately after creating the wlr_data_control_offer object.
One event per offered MIME type.
</description>
<arg name="mime_type" type="string" summary="offered MIME type"/>
</event>
</interface>
</protocol>

View File

@@ -8,7 +8,9 @@
#include <cstdlib>
#include <cstring>
#include <poll.h>
#include <print>
#include <pthread.h>
#include <ranges>
#include <signal.h>
#include <span>
#include <sys/mman.h>
@@ -29,9 +31,10 @@
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#undef namespace
#include "blur-client-protocol.h"
#include "ext-background-effect-v1-client-protocol.h"
namespace Waylight {
namespace {
constexpr usize MAX_SURROUNDING_BYTES = 4000;
@@ -139,6 +142,73 @@ 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);
}
{
auto const env = getenv("XDG_CONFIG_HOME");
if (env && *env) {
if (std::filesystem::exists(env)) {
m_config_home_dir = env;
}
}
if (m_config_home_dir.empty()) {
auto const home = getenv("HOME");
assert(home && *home);
m_config_home_dir = std::filesystem::path(home) / ".config";
std::filesystem::create_directories(m_config_home_dir);
}
m_config_home_dir /= "waylight";
std::filesystem::create_directories(m_config_home_dir);
m_config = Config::load(m_config_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
)
)")
.exec();
SQLite::Statement(*m_db, R"(
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.emplace(m_db);
}
App::~App()
@@ -146,9 +216,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();
@@ -164,6 +233,40 @@ App::~App()
xkb_keymap_unref(m_kbd.xkb_keymap_v);
if (m_kbd.xkb_ctx_v)
xkb_context_unref(m_kbd.xkb_ctx_v);
if (m_wayland.dd_source) {
wl_data_source_destroy(m_wayland.dd_source);
m_wayland.dd_source = nullptr;
}
if (m_wayland.dd_offer) {
wl_data_offer_destroy(m_wayland.dd_offer);
m_wayland.dd_offer = nullptr;
}
m_wl_data_offers.clear();
if (m_wayland.ddev) {
wl_data_device_destroy(m_wayland.ddev);
m_wayland.ddev = nullptr;
}
if (m_wayland.ddm) {
wl_data_device_manager_destroy(m_wayland.ddm);
m_wayland.ddm = nullptr;
}
if (m_wayland.curr_source) {
zwlr_data_control_source_v1_destroy(m_wayland.curr_source);
m_wayland.curr_source = nullptr;
}
for (auto &[offer, _] : m_data_control_offers) {
if (offer)
zwlr_data_control_offer_v1_destroy(offer);
}
m_data_control_offers.clear();
if (m_wayland.data_control_device) {
zwlr_data_control_device_v1_destroy(m_wayland.data_control_device);
m_wayland.data_control_device = nullptr;
}
if (m_wayland.data_control_mgr) {
zwlr_data_control_manager_v1_destroy(m_wayland.data_control_mgr);
m_wayland.data_control_mgr = nullptr;
}
if (m_wayland.text_input) {
zwp_text_input_v3_destroy(m_wayland.text_input);
m_wayland.text_input = nullptr;
@@ -220,6 +323,11 @@ auto App::set_visible(bool visible) -> void
auto App::init_wayland() -> void
{
m_debug_log = []() {
auto const env = getenv("WAYLIGHT_DEBUG");
return env && *env;
}();
m_wayland.display = wl_display_connect(nullptr);
if (!m_wayland.display) {
std::fprintf(stderr, "failed to connect to Wayland display\n");
@@ -383,8 +491,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;
@@ -469,93 +579,60 @@ auto App::init_wayland() -> void
app->m_ime.sent_serial = 0;
} };
auto handle_registry_global
= [](void *data, wl_registry *registry, u32 name, char const *interface,
u32 version) -> void {
auto *app { static_cast<App *>(data) };
if (std::strcmp(interface, wl_compositor_interface.name) == 0) {
app->m_wayland.compositor = static_cast<wl_compositor *>(
wl_registry_bind(registry, name, &wl_compositor_interface, 4));
} else if (std::strcmp(interface, wl_seat_interface.name) == 0) {
app->m_wayland.seat = static_cast<wl_seat *>(
wl_registry_bind(registry, name, &wl_seat_interface, 9));
static struct wl_seat_listener const seat_listener = {
.capabilities =
[](void *data, struct wl_seat *seat, u32 caps) {
auto *app { static_cast<App *>(data) };
if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
app->m_wayland.kbd = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(
app->m_wayland.kbd, &keyboard_listener, data);
app->m_ime.seat_focus = false;
}
ensure_text_input(app);
},
.name = [](void *, struct wl_seat *, char const *) {},
static auto destroy_data_control_offer
= +[](App *app, zwlr_data_control_offer_v1 *offer) {
if (!offer)
return;
zwlr_data_control_offer_v1_destroy(offer);
app->m_data_control_offers.erase(offer);
};
wl_seat_add_listener(app->m_wayland.seat, &seat_listener, data);
} else if (std::strcmp(interface, zwlr_layer_shell_v1_interface.name)
== 0) {
app->m_wayland.layer_shell = static_cast<zwlr_layer_shell_v1 *>(
wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface,
version >= 4 ? 4 : version));
} else if (std::strcmp(interface,
ext_background_effect_manager_v1_interface.name)
== 0) {
app->m_wayland.mgr
= static_cast<ext_background_effect_manager_v1 *>(
wl_registry_bind(registry, name,
&ext_background_effect_manager_v1_interface, 1));
} else if (std::strcmp(interface, "org_kde_kwin_blur_manager") == 0) {
app->m_wayland.kde_blur_mgr
= static_cast<org_kde_kwin_blur_manager *>(wl_registry_bind(
registry, name, &org_kde_kwin_blur_manager_interface, 1));
} else if (std::strcmp(
interface, zwp_text_input_manager_v3_interface.name)
== 0) {
app->m_wayland.text_input_mgr
= static_cast<zwp_text_input_manager_v3 *>(wl_registry_bind(
registry, name, &zwp_text_input_manager_v3_interface, 1));
app->m_ime.supported = true;
ensure_text_input(app);
} else if (std::strcmp(interface, wl_data_device_manager_interface.name)
== 0) {
app->m_wayland.ddm
= static_cast<wl_data_device_manager *>(wl_registry_bind(
registry, name, &wl_data_device_manager_interface,
std::min<uint32_t>(version, 3)));
if (app->m_wayland.ddm && !app->m_wayland.ddev) {
app->m_wayland.ddev = wl_data_device_manager_get_data_device(
app->m_wayland.ddm, app->m_wayland.seat);
static wl_data_device_listener const ddev_l = {
static wl_data_offer_listener const dd_offer_listener {
.offer =
[](void *data, wl_data_offer *offer, char const *mime) {
auto *app = static_cast<App *>(data);
auto &info = app->m_wl_data_offers[offer];
auto const is_text_plain = [](char const *m) {
return std::strncmp(m, "text/plain", 10) == 0;
};
auto const is_text = [](char const *m) {
return std::strncmp(m, "text/", 5) == 0;
};
if (std::strcmp(mime, "text/plain;charset=utf-8") == 0)
info.has_utf8 = true;
else if (std::strcmp(mime, "text/plain") == 0)
info.has_text = true;
if (info.first_text_mime.empty() && mime) {
if (is_text(mime) || is_text_plain(mime)
|| std::strcmp(mime, "UTF8_STRING") == 0
|| std::strcmp(mime, "STRING") == 0) {
info.first_text_mime = mime;
}
}
if (app->m_debug_log)
std::println("wl-data: offer mime {}", mime);
},
#if WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION
.source_actions = [](void *, wl_data_offer *, uint32_t) {},
.action = [](void *, wl_data_offer *, uint32_t) {},
#endif
};
static wl_data_device_listener const dd_listener {
.data_offer =
[](void *data, wl_data_device *, wl_data_offer *offer) {
auto *app = static_cast<App *>(data);
static wl_data_offer_listener const offer_l = {
.offer =
[](void *data, wl_data_offer *,
char const *mime) {
auto *app = static_cast<App *>(data);
(void)app;
(void)mime;
wl_data_offer_add_listener(offer, &dd_offer_listener, app);
if (app->m_wayland.dd_offer && app->m_wayland.dd_offer != offer)
wl_data_offer_destroy(app->m_wayland.dd_offer);
app->m_wayland.dd_offer = offer;
app->m_wl_data_offers.emplace(offer, WlDataOfferInfo {});
},
#if WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION
.source_actions
= [](void *, wl_data_offer *, uint32_t) {},
.action
= [](void *, wl_data_offer *, uint32_t) {}
#endif
};
wl_data_offer_add_listener(offer, &offer_l, app);
if (app->m_wayland.curr_offer
&& app->m_wayland.curr_offer != offer)
wl_data_offer_destroy(
app->m_wayland.curr_offer);
app->m_wayland.curr_offer = offer;
},
.enter
= [](void *, wl_data_device *, uint32_t, wl_surface *,
wl_fixed_t, wl_fixed_t, wl_data_offer *) {},
.enter = [](void *, wl_data_device *, uint32_t, wl_surface *, wl_fixed_t,
wl_fixed_t, wl_data_offer *) {},
.leave = [](void *, wl_data_device *) {},
.motion = [](void *, wl_data_device *, uint32_t, wl_fixed_t,
wl_fixed_t) {},
@@ -568,7 +645,29 @@ auto App::init_wayland() -> void
return;
}
char const *mime = "text/plain;charset=utf-8";
auto it = app->m_wl_data_offers.find(offer);
bool const has_utf8
= it != app->m_wl_data_offers.end() && it->second.has_utf8;
bool const has_text
= it != app->m_wl_data_offers.end() && it->second.has_text;
std::string const &first_text_mime
= it != app->m_wl_data_offers.end()
? it->second.first_text_mime
: std::string {};
char const *mime = nullptr;
if (has_utf8) {
mime = "text/plain;charset=utf-8";
} else if (has_text) {
mime = "text/plain";
} else if (!first_text_mime.empty()) {
mime = first_text_mime.c_str();
} else {
if (app->m_debug_log)
std::println("wl-data: no usable mime for selection");
return;
}
int fds[2];
if (pipe(fds) != 0)
return;
@@ -599,8 +698,119 @@ auto App::init_wayland() -> void
wl_data_offer *offer;
std::string data;
};
auto *ctx
= new Ctx { app, offer, std::move(data) };
auto *ctx = new Ctx { app, offer, std::move(data) };
g_main_context_invoke(
nullptr,
+[](gpointer p) -> gboolean {
auto *ctx = static_cast<Ctx *>(p);
if (!ctx->data.empty()) {
ctx->app->m_clipboard_cache
= std::move(ctx->data);
if (ctx->app->m_debug_log)
std::println("wl-data: selection received size {}",
ctx->app->m_clipboard_cache.size());
}
if (ctx->offer == ctx->app->m_wayland.dd_offer) {
wl_data_offer_destroy(ctx->offer);
ctx->app->m_wayland.dd_offer = nullptr;
}
ctx->app->m_wl_data_offers.erase(ctx->offer);
delete ctx;
return G_SOURCE_REMOVE;
},
ctx);
}).detach();
},
};
static auto ensure_data_device { +[](App *app) -> bool {
if (!app->m_wayland.ddm || !app->m_wayland.seat || app->m_wayland.ddev)
return false;
app->m_wayland.ddev
= wl_data_device_manager_get_data_device(app->m_wayland.ddm,
app->m_wayland.seat);
if (!app->m_wayland.ddev)
return false;
if (app->m_debug_log) {
std::println("wl-data: created data_device");
}
wl_data_device_add_listener(app->m_wayland.ddev, &dd_listener, app);
return true;
} };
static auto handle_selection_offer
= +[](App *app, zwlr_data_control_offer_v1 *offer) {
if (app->m_wayland.curr_offer
&& app->m_wayland.curr_offer != offer) {
destroy_data_control_offer(app, app->m_wayland.curr_offer);
}
app->m_wayland.curr_offer = offer;
if (!offer) {
if (app->m_debug_log)
std::println("data-control: selection cleared");
app->m_clipboard_cache.clear();
return;
}
auto it = app->m_data_control_offers.find(offer);
bool const has_utf8
= it != app->m_data_control_offers.end() && it->second.has_utf8;
bool const has_text
= it != app->m_data_control_offers.end() && it->second.has_text;
std::string const &first_text_mime
= it != app->m_data_control_offers.end()
? it->second.first_text_mime
: std::string {};
char const *mime = nullptr;
if (has_utf8) {
mime = "text/plain;charset=utf-8";
} else if (has_text) {
mime = "text/plain";
} else if (!first_text_mime.empty()) {
mime = first_text_mime.c_str();
} else {
if (app->m_debug_log)
std::println("data-control: no usable mime, skipping");
return;
}
if (app->m_debug_log)
std::println("data-control: receive selection mime {}", mime);
int fds[2];
if (pipe(fds) != 0)
return;
zwlr_data_control_offer_v1_receive(offer, mime, fds[1]);
wl_display_flush(app->m_wayland.display);
close(fds[1]);
int rfd = fds[0];
std::thread([app, rfd, offer]() {
std::string data;
char buf[4096];
for (;;) {
ssize_t n = read(rfd, buf, sizeof buf);
if (n > 0) {
data.append(buf, buf + n);
continue;
}
if (n < 0 && errno == EINTR)
continue;
break;
}
close(rfd);
struct Ctx {
App *app;
zwlr_data_control_offer_v1 *offer;
std::string data;
};
auto *ctx = new Ctx { app, offer, std::move(data) };
g_main_context_invoke(
nullptr,
@@ -611,19 +821,185 @@ auto App::init_wayland() -> void
= std::move(ctx->data);
if (ctx->offer
== ctx->app->m_wayland.curr_offer) {
wl_data_offer_destroy(ctx->offer);
ctx->app->m_wayland.curr_offer
= nullptr;
destroy_data_control_offer(
ctx->app, ctx->offer);
ctx->app->m_wayland.curr_offer = nullptr;
}
delete ctx;
return G_SOURCE_REMOVE;
},
ctx);
}).detach();
};
static zwlr_data_control_offer_v1_listener const data_offer_listener {
.offer =
[](void *data, zwlr_data_control_offer_v1 *offer, char const *mime) {
auto *app = static_cast<App *>(data);
auto &info = app->m_data_control_offers[offer];
auto const is_text_plain = [](char const *m) {
return std::strncmp(m, "text/plain", 10) == 0;
};
auto const is_text = [](char const *m) {
return std::strncmp(m, "text/", 5) == 0;
};
if (std::strcmp(mime, "text/plain;charset=utf-8") == 0)
info.has_utf8 = true;
else if (std::strcmp(mime, "text/plain") == 0)
info.has_text = true;
if (info.first_text_mime.empty() && mime) {
if (is_text(mime) || is_text_plain(mime)
|| std::strcmp(mime, "UTF8_STRING") == 0
|| std::strcmp(mime, "STRING") == 0) {
info.first_text_mime = mime;
}
}
if (app->m_debug_log)
std::println("data-control: offer mime {}", mime);
},
};
wl_data_device_add_listener(app->m_wayland.ddev, &ddev_l, app);
static zwlr_data_control_device_v1_listener const data_device_listener {
.data_offer =
[](void *data, zwlr_data_control_device_v1 *,
zwlr_data_control_offer_v1 *offer) {
auto *app = static_cast<App *>(data);
zwlr_data_control_offer_v1_add_listener(
offer, &data_offer_listener, app);
if (app->m_debug_log)
std::println("data-control: new offer {}", static_cast<void *>(offer));
},
.selection =
[](void *data, zwlr_data_control_device_v1 *,
zwlr_data_control_offer_v1 *offer) {
auto *app = static_cast<App *>(data);
handle_selection_offer(app, offer);
},
.finished =
[](void *data, zwlr_data_control_device_v1 *device) {
auto *app = static_cast<App *>(data);
for (auto &[offer, _] : app->m_data_control_offers) {
if (offer)
zwlr_data_control_offer_v1_destroy(offer);
}
app->m_data_control_offers.clear();
app->m_wayland.curr_offer = nullptr;
if (app->m_wayland.curr_source) {
zwlr_data_control_source_v1_destroy(app->m_wayland.curr_source);
app->m_wayland.curr_source = nullptr;
}
if (app->m_wayland.data_control_device == device)
app->m_wayland.data_control_device = nullptr;
if (app->m_debug_log)
std::println("data-control: device finished");
zwlr_data_control_device_v1_destroy(device);
},
.primary_selection =
[](void *data, zwlr_data_control_device_v1 *,
zwlr_data_control_offer_v1 *offer) {
auto *app = static_cast<App *>(data);
handle_selection_offer(app, offer);
},
};
static auto ensure_data_control_device { +[](App *app) -> bool {
if (!app->m_wayland.data_control_mgr || !app->m_wayland.seat
|| app->m_wayland.data_control_device)
return false;
app->m_wayland.data_control_device
= zwlr_data_control_manager_v1_get_data_device(
app->m_wayland.data_control_mgr, app->m_wayland.seat);
if (!app->m_wayland.data_control_device)
return false;
if (app->m_debug_log) {
std::println("data-control: created device (manager {}, seat present {})",
static_cast<void *>(app->m_wayland.data_control_mgr), true);
}
zwlr_data_control_device_v1_add_listener(
app->m_wayland.data_control_device, &data_device_listener, app);
return true;
} };
auto handle_registry_global
= [](void *data, wl_registry *registry, u32 name, char const *interface,
u32 version) -> void {
auto *app { static_cast<App *>(data) };
if (app->m_debug_log) {
std::println(
"registry: interface={} name={} version={}", interface, name, version);
}
if (std::strcmp(interface, wl_compositor_interface.name) == 0) {
app->m_wayland.compositor = static_cast<wl_compositor *>(
wl_registry_bind(registry, name, &wl_compositor_interface, 4));
} else if (std::strcmp(interface, wl_seat_interface.name) == 0) {
app->m_wayland.seat = static_cast<wl_seat *>(
wl_registry_bind(registry, name, &wl_seat_interface, 9));
static struct wl_seat_listener const seat_listener = {
.capabilities =
[](void *data, struct wl_seat *seat, u32 caps) {
auto *app { static_cast<App *>(data) };
if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
app->m_wayland.kbd = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(
app->m_wayland.kbd, &keyboard_listener, data);
app->m_ime.seat_focus = false;
}
ensure_text_input(app);
bool const created_ctrl
= ensure_data_control_device(app);
bool const created_dd = ensure_data_device(app);
if (created_ctrl || created_dd)
wl_display_roundtrip(app->m_wayland.display);
},
.name = [](void *, struct wl_seat *, char const *) {},
};
wl_seat_add_listener(app->m_wayland.seat, &seat_listener, data);
} else if (std::strcmp(interface, zwlr_layer_shell_v1_interface.name)
== 0) {
app->m_wayland.layer_shell = static_cast<zwlr_layer_shell_v1 *>(
wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface,
version >= 4 ? 4 : version));
} else if (std::strcmp(interface,
ext_background_effect_manager_v1_interface.name)
== 0) {
app->m_wayland.mgr
= static_cast<ext_background_effect_manager_v1 *>(
wl_registry_bind(registry, name,
&ext_background_effect_manager_v1_interface, 1));
} else if (std::strcmp(
interface, zwp_text_input_manager_v3_interface.name)
== 0) {
app->m_wayland.text_input_mgr
= static_cast<zwp_text_input_manager_v3 *>(wl_registry_bind(
registry, name, &zwp_text_input_manager_v3_interface, 1));
app->m_ime.supported = true;
ensure_text_input(app);
} else if (std::strcmp(interface,
zwlr_data_control_manager_v1_interface.name)
== 0) {
if (app->m_debug_log) {
std::println(
"registry: data-control name={} version={}", name, version);
}
uint32_t const bind_version = std::min<uint32_t>(version, 2);
app->m_wayland.data_control_mgr
= static_cast<zwlr_data_control_manager_v1 *>(wl_registry_bind(
registry, name, &zwlr_data_control_manager_v1_interface,
bind_version));
bool const created = ensure_data_control_device(app);
if (created)
wl_display_roundtrip(app->m_wayland.display);
} else if (std::strcmp(interface, wl_data_device_manager_interface.name)
== 0) {
app->m_wayland.ddm = static_cast<wl_data_device_manager *>(
wl_registry_bind(registry, name, &wl_data_device_manager_interface,
std::min<uint32_t>(version, 3)));
bool const created = ensure_data_device(app);
if (created)
wl_display_roundtrip(app->m_wayland.display);
}
};
@@ -638,6 +1014,14 @@ auto App::init_wayland() -> void
m_kbd.xkb_ctx_v = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
wl_display_roundtrip(m_wayland.display);
if (m_debug_log) {
std::println("data-control: mgr={} device={}",
static_cast<void *>(m_wayland.data_control_mgr),
static_cast<void *>(m_wayland.data_control_device));
std::println("wl-data: ddm={} ddev={}",
static_cast<void *>(m_wayland.ddm),
static_cast<void *>(m_wayland.ddev));
}
create_layer_surface();
}
@@ -660,6 +1044,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>();
@@ -790,10 +1181,6 @@ auto App::create_layer_surface() -> void
m_wayland.eff = ext_background_effect_manager_v1_get_background_effect(
m_wayland.mgr, m_wayland.surface);
}
if (m_wayland.kde_blur_mgr) {
m_wayland.kde_blur = org_kde_kwin_blur_manager_create(
m_wayland.kde_blur_mgr, m_wayland.surface);
}
m_wayland.layer_surface = zwlr_layer_shell_v1_get_layer_surface(
m_wayland.layer_shell, m_wayland.surface, nullptr,
@@ -804,10 +1191,6 @@ auto App::create_layer_surface() -> void
ext_background_effect_surface_v1_destroy(m_wayland.eff);
m_wayland.eff = nullptr;
}
if (m_wayland.kde_blur) {
org_kde_kwin_blur_destroy(m_wayland.kde_blur);
m_wayland.kde_blur = nullptr;
}
if (m_wayland.surface) {
wl_surface_destroy(m_wayland.surface);
m_wayland.surface = nullptr;
@@ -818,6 +1201,8 @@ auto App::create_layer_surface() -> void
zwlr_layer_surface_v1_set_anchor(m_wayland.layer_surface, 0);
zwlr_layer_surface_v1_set_size(m_wayland.layer_surface, m_win_w, m_win_h);
zwlr_layer_surface_v1_set_exclusive_zone(m_wayland.layer_surface, 0);
zwlr_layer_surface_v1_set_layer(
m_wayland.layer_surface, ZWLR_LAYER_SHELL_V1_LAYER_TOP);
if (zwlr_layer_shell_v1_get_version(m_wayland.layer_shell) >= 3) {
zwlr_layer_surface_v1_set_keyboard_interactivity(
@@ -895,11 +1280,6 @@ auto App::destroy_layer_surface() -> void
m_wayland.eff = nullptr;
}
if (m_wayland.kde_blur) {
org_kde_kwin_blur_destroy(m_wayland.kde_blur);
m_wayland.kde_blur = nullptr;
}
if (m_wayland.layer_surface) {
zwlr_layer_surface_v1_destroy(m_wayland.layer_surface);
m_wayland.layer_surface = nullptr;
@@ -945,7 +1325,7 @@ auto App::update_blur_region() -> void
{
if (!m_wayland.compositor)
return;
if (!m_wayland.eff && !m_wayland.kde_blur)
if (!m_wayland.eff)
return;
wl_region *region = wl_compositor_create_region(m_wayland.compositor);
@@ -956,8 +1336,6 @@ auto App::update_blur_region() -> void
if (m_wayland.eff)
ext_background_effect_surface_v1_set_blur_region(m_wayland.eff, region);
if (m_wayland.kde_blur)
org_kde_kwin_blur_set_region(m_wayland.kde_blur, region);
wl_region_destroy(region);
}
@@ -1061,17 +1439,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 +1478,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);
@@ -1116,19 +1494,87 @@ auto App::pump_events() -> void
}
}
auto App::clipboard(std::string_view const &str) -> void
auto App::set_clipboard(std::string_view const &str) -> void
{
m_clipboard_cache.assign(str.begin(), str.end());
if (m_wayland.data_control_mgr && m_wayland.data_control_device) {
if (m_debug_log)
std::println("data-control: set_clipboard size {}", str.size());
if (m_wayland.curr_source) {
zwlr_data_control_source_v1_destroy(m_wayland.curr_source);
m_wayland.curr_source = nullptr;
}
m_wayland.curr_source = zwlr_data_control_manager_v1_create_data_source(
m_wayland.data_control_mgr);
static zwlr_data_control_source_v1_listener const src_l = {
.send =
[](void *data, zwlr_data_control_source_v1 *, char const *,
int32_t fd) {
auto *app = static_cast<App *>(data);
int wfd = dup(fd);
close(fd);
std::pmr::string payload = app->m_clipboard_cache;
std::thread([wfd, payload = std::move(payload)]() {
size_t off = 0;
while (off < payload.size()) {
ssize_t n = write(wfd, payload.data() + off,
std::min<size_t>(
64 * 1024, payload.size() - off));
if (n > 0) {
off += (size_t)n;
continue;
}
if (n < 0 && (errno == EINTR))
continue;
if (n < 0 && (errno == EAGAIN)) {
std::this_thread::sleep_for(
std::chrono::milliseconds(1));
continue;
}
break;
}
close(wfd);
}).detach();
},
.cancelled =
[](void *data, zwlr_data_control_source_v1 *src) {
auto *app = static_cast<App *>(data);
if (app->m_wayland.curr_source == src)
app->m_wayland.curr_source = nullptr;
zwlr_data_control_source_v1_destroy(src);
},
};
zwlr_data_control_source_v1_add_listener(
m_wayland.curr_source, &src_l, this);
zwlr_data_control_source_v1_offer(
m_wayland.curr_source, "text/plain;charset=utf-8");
zwlr_data_control_source_v1_offer(m_wayland.curr_source, "text/plain");
zwlr_data_control_device_v1_set_selection(
m_wayland.data_control_device, m_wayland.curr_source);
wl_display_flush(m_wayland.display);
return;
}
if (!m_wayland.ddm || !m_wayland.ddev || !m_wayland.seat)
return;
if (m_last_serial == 0)
return;
if (m_wayland.curr_source) {
wl_data_source_destroy(m_wayland.curr_source);
m_wayland.curr_source = nullptr;
if (m_debug_log)
std::println("wl-data: set_clipboard size {}", str.size());
if (m_wayland.dd_source) {
wl_data_source_destroy(m_wayland.dd_source);
m_wayland.dd_source = nullptr;
}
m_wayland.curr_source
= wl_data_device_manager_create_data_source(m_wayland.ddm);
m_wayland.dd_source = wl_data_device_manager_create_data_source(m_wayland.ddm);
static wl_data_source_listener const src_l = {
.target = [](void *, wl_data_source *, char const *) {},
@@ -1164,8 +1610,8 @@ auto App::clipboard(std::string_view const &str) -> void
.cancelled =
[](void *data, wl_data_source *src) {
auto *app = static_cast<App *>(data);
if (app->m_wayland.curr_source == src)
app->m_wayland.curr_source = nullptr;
if (app->m_wayland.dd_source == src)
app->m_wayland.dd_source = nullptr;
wl_data_source_destroy(src);
},
#if WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION
@@ -1174,13 +1620,74 @@ auto App::clipboard(std::string_view const &str) -> void
.action = [](void *, wl_data_source *, uint32_t) {}
#endif
};
wl_data_source_add_listener(m_wayland.curr_source, &src_l, this);
wl_data_source_offer(m_wayland.curr_source, "text/plain;charset=utf-8");
wl_data_source_offer(m_wayland.curr_source, "text/plain");
m_clipboard_cache.assign(str.begin(), str.end());
wl_data_source_add_listener(m_wayland.dd_source, &src_l, this);
wl_data_source_offer(m_wayland.dd_source, "text/plain;charset=utf-8");
wl_data_source_offer(m_wayland.dd_source, "text/plain");
wl_data_device_set_selection(
m_wayland.ddev, m_wayland.curr_source, m_last_serial);
m_wayland.ddev, m_wayland.dd_source, m_last_serial);
wl_display_flush(m_wayland.display);
}
void App::execute_command(bool terminal, std::string_view const command)
{
constexpr auto resolve_cmdline { [](std::string_view const cmdline,
std::vector<std::string> &out) {
std::ranges::copy(cmdline | std::views::split(' ')
| std::views::transform(
[](auto &&s) { return std::string(s.begin(), s.end()); }),
std::back_inserter(out));
} };
std::vector<std::string> args;
if (terminal) {
resolve_cmdline(m_config.terminal_cmdline, args);
} else {
args.push_back("/bin/sh");
args.push_back("-c");
}
args.push_back(std::string(command));
if (auto const &exe { args.at(0) }; exe.at(0) != '/') {
auto const *path_env { getenv("PATH") };
if (!(path_env && *path_env)) {
path_env = "/bin";
}
auto const path { std::string(path_env) };
for (auto const &dir :
path | std::views::split(':') | std::views::transform([](auto &&s) {
return std::filesystem::path(s.begin(), s.end());
}) | std::views::filter([](auto &&p) {
return std::filesystem::is_directory(p);
})) {
auto const path = dir / exe;
if (std::filesystem::is_regular_file(path)) {
args[0] = path.string();
}
}
}
std::print("Final args: ");
for (auto const &arg : args) {
std::print("{} ", arg);
}
std::println("");
std::vector<char const *> cargs;
std::transform(args.begin(), args.end(), std::back_inserter(cargs),
[](auto &&s) { return s.c_str(); });
cargs.push_back(nullptr);
auto cargsc { const_cast<char *const *>(cargs.data()) };
auto const pid = fork();
if (pid == 0) {
setsid();
execv(args.at(0).c_str(), cargsc);
} else if (pid < 0) {
throw std::runtime_error("Failed to fork process");
}
}
} // namespace Waylight

View File

@@ -3,6 +3,7 @@
#include <cassert>
#include <filesystem>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
@@ -10,24 +11,29 @@
#include <libportal/portal.h>
#include <wayland-client-protocol.h>
extern "C" {
#include "blur-client-protocol.h"
#define namespace namespace_
#include "ext-background-effect-v1-client-protocol.h"
#include "text-input-unstable-v3-client-protocol.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "wlr-data-control-unstable-v1-client-protocol.h"
#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 "Config.hpp"
#include "IconRegistry.hpp"
#include "ImGui.hpp"
#include "TextRenderer.hpp"
#include "Theme.hpp"
#include "common.hpp"
namespace Waylight {
struct TypingBuffer : std::pmr::vector<u32> {
void push_utf8(char const *s);
};
@@ -61,15 +67,26 @@ private:
return m_themes[m_active_theme];
}
auto clipboard() const -> std::pmr::string const &
auto get_clipboard() const -> std::pmr::string const &
{
return m_clipboard_cache;
}
auto clipboard(std::string_view const &str) -> void;
auto set_clipboard(std::string_view const &str) -> void;
static void on_settings_changed(XdpSettings * /*self*/, char const *ns,
char const *key, GVariant * /*value*/, gpointer data);
struct DataControlOfferInfo {
bool has_utf8 { false };
bool has_text { false };
std::string first_text_mime;
};
struct WlDataOfferInfo {
bool has_utf8 { false };
bool has_text { false };
std::string first_text_mime;
};
struct {
wl_display *display {};
wl_registry *registry {};
@@ -81,17 +98,23 @@ private:
zwlr_layer_surface_v1 *layer_surface {};
ext_background_effect_manager_v1 *mgr {};
ext_background_effect_surface_v1 *eff {};
org_kde_kwin_blur_manager *kde_blur_mgr {};
org_kde_kwin_blur *kde_blur {};
zwp_text_input_manager_v3 *text_input_mgr {};
zwp_text_input_v3 *text_input {};
wl_data_device_manager *ddm {};
wl_data_device *ddev {};
wl_data_offer *curr_offer {};
wl_data_source *curr_source {};
wl_data_offer *dd_offer {};
wl_data_source *dd_source {};
zwlr_data_control_manager_v1 *data_control_mgr {};
zwlr_data_control_device_v1 *data_control_device {};
zwlr_data_control_offer_v1 *curr_offer {};
zwlr_data_control_source_v1 *curr_source {};
} m_wayland;
std::pmr::string m_clipboard_cache;
std::unordered_map<zwlr_data_control_offer_v1 *, DataControlOfferInfo>
m_data_control_offers;
std::unordered_map<wl_data_offer *, WlDataOfferInfo> m_wl_data_offers;
u32 m_last_serial { 0 };
bool m_debug_log { false };
struct {
EGLDisplay edpy { EGL_NO_DISPLAY };
@@ -126,13 +149,11 @@ private:
{
if (!xkb_state_v)
return false;
for (auto k : held) {
if (xkb_state_key_get_one_sym(
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)
return true;
}
return false;
== sym);
});
}
auto is_sym_pressed(xkb_keysym_t sym) const -> bool
@@ -199,9 +220,6 @@ private:
bool surrounding_dirty { false };
} m_ime;
// NOTE: Canonicalize first!
std::unordered_map<std::filesystem::path, Texture2D> m_textures;
auto get_texture(std::filesystem::path const &path) -> Texture2D const &
{
if (m_textures.contains(path)) {
@@ -216,9 +234,16 @@ private:
return m_textures[path];
}
void execute_command(bool terminal, std::string_view const command);
// NOTE: Canonicalize first!
std::unordered_map<std::filesystem::path, Texture2D> m_textures;
enum_array<Theme, ColorScheme> m_themes { make_default_themes() };
Theme m_active_theme { Theme::Light };
IconRegistry m_ir;
std::optional<Cache> m_cache;
Config m_config;
int m_win_w { 800 };
int m_win_h { 600 };
@@ -227,5 +252,10 @@ private:
Color m_accent_color { 127, 127, 255, 255 };
std::filesystem::path m_data_home_dir {};
std::filesystem::path m_config_home_dir {};
std::shared_ptr<SQLite::Database> m_db {};
int m_sfd { -1 };
};
} // namespace Waylight

435
src/Cache.cpp Normal file
View File

@@ -0,0 +1,435 @@
#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"
#include <sys/inotify.h>
namespace Waylight {
void replace_all(
std::string &str, std::string const &from, std::string const &to)
{
if (from.empty())
return;
size_t pos = 0;
while ((pos = str.find(from, pos)) != std::string::npos) {
str.replace(pos, from.size(), to);
pos += to.size();
}
}
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());
}
}
for (auto const &dir : m_app_dirs) {
m_inotify.watch_path_recursively(dir);
}
m_inotify.set_callback([this](FileWatchEvent const &event) {
auto const mask = event.mask;
if (mask & IN_Q_OVERFLOW) {
rescan();
return;
}
if (mask & IN_ISDIR) {
rescan();
return;
}
if (mask & (IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM
| IN_MOVED_TO | IN_CLOSE_WRITE)) {
if (event.path.extension() == ".desktop") {
rescan();
}
}
});
m_inotify_thread = std::thread([this]() { m_inotify.run(); });
}
Cache::~Cache()
{
m_inotify.stop();
if (m_inotify_thread.joinable())
m_inotify_thread.join();
}
void Cache::rescan()
{
m_apps.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 = [&](std::string const
&desktop_file_uri,
mINI::INIMap<std::string> const
&section) {
auto const name = section.get("Name");
auto const 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;
}();
return ApplicationCache::Action {
.name = name,
.exec = [&]() -> std::optional<std::string> {
if (section.has("Exec")) {
auto s = section.get("Exec");
if (!s.empty()) {
// Either deprecated or not used...
replace_all(s, "%f", "");
replace_all(s, "%F", "");
replace_all(s, "%u", "");
replace_all(s, "%U", "");
replace_all(s, "%d", "");
replace_all(s, "%D", "");
replace_all(s, "%n", "");
replace_all(s, "%N", "");
replace_all(s, "%v", "");
replace_all(s, "%M", "");
replace_all(s, "%c", name);
if (icon) {
if (auto const p
= std::get_if<std::filesystem::path>(
&*icon)) {
replace_all(s, "%i",
"--icon '" + p->string() + "'");
} else if (auto const n
= std::get_if<std::string>(&*icon)) {
replace_all(
s, "%i", "--icon '" + *n + "'");
} else {
replace_all(s, "%i", "");
}
} else {
replace_all(s, "%i", "");
}
replace_all(s, "%k", "'" + desktop_file_uri);
return s;
}
}
return std::nullopt;
}(),
.icon = icon,
};
};
m_apps.push_back({
.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(
std::filesystem::canonical(file.path())
.string(),
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;
}(),
});
}
}
dump();
}
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.reset();
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.reset();
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 get_apps(*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 (get_apps.executeStep()) {
ApplicationCache app {};
app.id = get_apps.getColumn(0).getInt64();
app.type = static_cast<ApplicationCache::Type>(
get_apps.getColumn(1).getInt());
app.desktop_entry_path
= std::filesystem::path(get_apps.getColumn(2).getString());
app.terminal = get_apps.getColumn(3).getInt() != 0;
app.no_display = get_apps.getColumn(4).getInt() != 0;
if (!get_apps.getColumn(5).isNull())
app.path = std::string(get_apps.getColumn(5).getString());
else
app.path.reset();
if (!get_apps.getColumn(6).isNull())
app.comment = std::string(get_apps.getColumn(6).getString());
else
app.comment.reset();
app.dbus_activatable = get_apps.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 get_actions(*m_db,
"SELECT id_app, name, exec, icon "
"FROM ApplicationActionCache "
"ORDER BY id_app");
while (get_actions.executeStep()) {
auto id_app = get_actions.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(get_actions.getColumn(1).getString());
if (!get_actions.getColumn(2).isNull())
action.exec = std::string(get_actions.getColumn(2).getString());
else
action.exec.reset();
if (!get_actions.getColumn(3).isNull()) {
auto const str = get_actions.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 Waylight

66
src/Cache.hpp Normal file
View File

@@ -0,0 +1,66 @@
#pragma once
#include <filesystem>
#include <optional>
#include <string>
#include <thread>
#include <variant>
#include <vector>
#include "InotifyWatcher.hpp"
#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);
~Cache();
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;
InotifyWatcher m_inotify;
std::thread m_inotify_thread;
};
} // namespace Waylight

47
src/Config.cpp Normal file
View File

@@ -0,0 +1,47 @@
#include "Config.hpp"
#include <filesystem>
#include <fstream>
#include <print>
#include <toml++/toml.hpp>
namespace Waylight {
void Config::write(std::filesystem::path const &path)
{
std::ofstream f(path);
if (!f) {
throw std::runtime_error("Failed to open config file for writing");
}
std::println(f, "[settings]");
std::println(f, "terminal_cmdline=\"{}\"", terminal_cmdline);
}
auto Config::load(std::filesystem::path const &config_dir_path) -> Config const
{
if (!std::filesystem::is_directory(config_dir_path))
throw std::runtime_error("Provided path is not a directory!");
Config cfg {};
std::filesystem::path path_config { config_dir_path / "config.toml" };
if (!std::filesystem::is_regular_file(path_config)) {
try {
std::filesystem::remove_all(path_config);
} catch (std::exception const &e) {
}
cfg.write(path_config);
}
std::println("Config file: {}", path_config.string());
auto const tbl { toml::parse_file(path_config.string()) };
auto const terminal_cmdline { tbl["settings"]["terminal_cmdline"].value_or(
"kitty -c") };
cfg.terminal_cmdline = terminal_cmdline;
return cfg;
}
} // namespace Waylight

17
src/Config.hpp Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include <filesystem>
#include <string>
namespace Waylight {
struct Config {
std::string terminal_cmdline { "kitty" };
void write(std::filesystem::path const &path);
static auto load(std::filesystem::path const &config_dir_path)
-> Config const;
};
} // namespace Waylight

View File

@@ -6,13 +6,15 @@
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <print>
#include <ranges>
#include <stdexcept>
#include <gio/gio.h>
#include <lunasvg.h>
#include <mini/ini.h>
namespace Waylight {
static inline auto color_to_string(Color const &c) -> std::string
{
auto const r { c.r / 255.0 }, g { c.g / 255.0 }, b { c.b / 255.0 };
@@ -70,7 +72,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;
@@ -90,6 +92,35 @@ static auto kde_get_theme() -> std::string const
return {};
}
static auto other_get_theme() -> std::string
{
char const *schema_id { "org.gnome.desktop.interface" };
char const *key { "icon-theme" };
GSettingsSchemaSource *src { g_settings_schema_source_get_default() };
if (!src)
return {};
GSettingsSchema *schema { g_settings_schema_source_lookup(
src, schema_id, TRUE) };
if (!schema)
return {};
GSettings *settings { g_settings_new_full(schema, nullptr, nullptr) };
g_settings_schema_unref(schema);
if (!settings)
return {};
gchar *cstr { g_settings_get_string(settings, key) };
g_object_unref(settings);
if (!cstr)
return {};
std::string theme { cstr };
g_free(cstr);
return theme;
}
static auto get_current_icon_theme() -> std::optional<std::string> const
{
auto de { detect_desktop_environment() };
@@ -100,6 +131,10 @@ static auto get_current_icon_theme() -> std::optional<std::string> const
if (auto const t = kde_get_theme(); !t.empty()) {
return t;
}
} else {
if (auto const t = other_get_theme(); !t.empty()) {
return t;
}
}
return std::nullopt;
@@ -293,9 +328,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;
{
@@ -313,21 +347,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
@@ -335,7 +375,7 @@ IconRegistry::IconRegistry()
return std::filesystem::is_directory(path);
})) {
try {
m_themes.push_back({ path });
m_themes.push_back(IconTheme(path));
} catch (...) {
}
}
@@ -350,14 +390,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; });
});
}
}
@@ -411,3 +445,5 @@ auto IconRegistry::lookup_cached(std::string_view const name,
throw std::runtime_error(std::format("Failed to find icon `{}`!", name));
}
} // namespace Waylight

View File

@@ -8,6 +8,8 @@
#include <raylib.h>
namespace Waylight {
struct Icon {
Icon(std::filesystem::path path, Texture2D texture, int size)
: m_path(path)
@@ -30,7 +32,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>
@@ -87,3 +89,5 @@ private:
std::unordered_map<std::string, Icon> m_cached_icons;
std::optional<std::string> m_preferred_theme;
};
} // namespace Waylight

View File

@@ -11,6 +11,8 @@
#include <raylib.h>
namespace Waylight {
namespace {
struct CodepointSpan {
@@ -121,18 +123,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 +218,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 +279,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 +542,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(),
std::back_inserter(clip2),
[](char ch) { return ch != '\n' && ch != '\r'; });
str.insert(caret_byte, clip2);
state.current_rune_idx += (int)utf8_length(clip2);
} else {
@@ -763,8 +751,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 +772,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);
@@ -803,3 +788,5 @@ auto ImGui::list_view(usize id, Rectangle bounds, usize elements,
return submitted;
}
} // namespace Waylight

View File

@@ -12,6 +12,8 @@
#include "TextRenderer.hpp"
namespace Waylight {
constexpr float DEFAULT_FONT_SIZE { 24 };
struct TextInputOptions {
@@ -31,7 +33,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;
@@ -158,3 +160,5 @@ struct ImGuiGuard {
private:
std::shared_ptr<ImGui> m_imgui { nullptr };
};
} // namespace Waylight

241
src/InotifyWatcher.cpp Normal file
View File

@@ -0,0 +1,241 @@
#include "InotifyWatcher.hpp"
#include <algorithm>
#include <array>
#include <cerrno>
#include <cstring>
#include <poll.h>
#include <stdexcept>
#include <system_error>
#include <unistd.h>
#include <utility>
#include <sys/inotify.h>
namespace Waylight {
namespace {
constexpr std::uint32_t watch_mask { IN_ATTRIB | IN_CREATE | IN_DELETE
| IN_DELETE_SELF | IN_MODIFY | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO
| IN_CLOSE_WRITE };
constexpr int poll_timeout_ms { 250 };
} // namespace
InotifyWatcher::InotifyWatcher()
: m_fd(inotify_init1(IN_CLOEXEC))
{
if (m_fd < 0) {
throw std::system_error(
errno, std::generic_category(), "inotify_init1 failed");
}
}
InotifyWatcher::~InotifyWatcher()
{
stop();
if (m_fd >= 0) {
::close(m_fd);
m_fd = -1;
}
}
void InotifyWatcher::watch_path_recursively(std::filesystem::path const &path)
{
add_watch_recursively(path);
}
void InotifyWatcher::set_callback(callback_t cb)
{
std::scoped_lock lock(m_watch_mutex);
m_callback = std::move(cb);
}
void InotifyWatcher::run()
{
if (m_running.exchange(true)) {
throw std::runtime_error("InotifyWatcher::run called while running");
}
std::array<char, 4096> buffer {};
while (m_running.load()) {
pollfd fd_set {
.fd = m_fd,
.events = POLLIN,
.revents = 0,
};
int const poll_res { ::poll(&fd_set, 1, poll_timeout_ms) };
if (!m_running.load())
break;
if (poll_res < 0) {
if (errno == EINTR)
continue;
if (errno == EBADF || errno == EINVAL)
break;
throw std::system_error(errno, std::generic_category(), "poll");
}
if (poll_res == 0)
continue;
if (fd_set.revents & (POLLERR | POLLHUP | POLLNVAL))
break;
if (!(fd_set.revents & POLLIN))
continue;
ssize_t const bytes_read { ::read(m_fd, buffer.data(), buffer.size()) };
if (bytes_read < 0) {
if (errno == EINTR || errno == EAGAIN)
continue;
if (errno == EBADF || errno == EINVAL)
break;
throw std::system_error(errno, std::generic_category(), "read");
}
ssize_t offset { 0 };
while (offset + static_cast<ssize_t>(sizeof(inotify_event))
<= bytes_read) {
auto const *event = reinterpret_cast<inotify_event const *>(
buffer.data() + offset);
std::string_view name;
if (event->len > 0) {
auto const *raw_name
= buffer.data() + offset + sizeof(inotify_event);
auto const *end
= std::find(raw_name, raw_name + event->len, '\0');
name = std::string_view(raw_name, end - raw_name);
}
dispatch_event(event->wd, event->mask, name);
offset += sizeof(inotify_event) + event->len;
}
}
m_running.store(false);
}
void InotifyWatcher::stop() { m_running.store(false); }
void InotifyWatcher::add_watch_for_path(std::filesystem::path const &path)
{
auto normalized { normalize_path(path) };
if (normalized.empty())
return;
auto const key = normalized.string();
{
std::scoped_lock lock(m_watch_mutex);
auto it = m_path_to_watch.find(key);
if (it != m_path_to_watch.end())
return;
}
int const wd { ::inotify_add_watch(m_fd, normalized.c_str(), watch_mask) };
if (wd < 0) {
if (errno == ENOENT || errno == ENOTDIR)
return;
throw std::system_error(errno, std::generic_category(),
"inotify_add_watch failed for " + normalized.string());
}
std::scoped_lock lock(m_watch_mutex);
m_watch_to_path.emplace(wd, normalized);
m_path_to_watch.emplace(key, wd);
}
void InotifyWatcher::add_watch_recursively(std::filesystem::path const &path)
{
auto normalized = normalize_path(path);
if (normalized.empty())
return;
add_watch_for_path(normalized);
std::error_code ec;
if (!std::filesystem::is_directory(normalized, ec))
return;
try {
auto const options {
std::filesystem::directory_options::follow_directory_symlink
| std::filesystem::directory_options::skip_permission_denied
};
for (auto const &entry : std::filesystem::recursive_directory_iterator(
normalized, options)) {
std::error_code inner_ec;
if (!entry.is_directory(inner_ec) || inner_ec)
continue;
add_watch_for_path(entry.path());
}
} catch (std::filesystem::filesystem_error const &) {
}
}
void InotifyWatcher::remove_watch(int wd)
{
std::filesystem::path removed_path;
{
std::scoped_lock lock(m_watch_mutex);
auto it { m_watch_to_path.find(wd) };
if (it == m_watch_to_path.end())
return;
removed_path = it->second;
m_watch_to_path.erase(it);
m_path_to_watch.erase(removed_path.string());
}
::inotify_rm_watch(m_fd, wd);
}
void InotifyWatcher::dispatch_event(
int wd, std::uint32_t mask, std::string_view name)
{
std::filesystem::path base_path;
callback_t callback_copy;
{
std::scoped_lock lock(m_watch_mutex);
auto it { m_watch_to_path.find(wd) };
if (it == m_watch_to_path.end())
return;
base_path = it->second;
callback_copy = m_callback;
}
auto event_path { base_path };
if (!name.empty())
event_path /= name;
if ((mask & IN_ISDIR)
&& (mask & (IN_CREATE | IN_MOVED_TO | IN_ATTRIB | IN_CLOSE_WRITE))) {
add_watch_recursively(event_path);
}
if (mask & (IN_DELETE_SELF | IN_MOVE_SELF | IN_IGNORED)) {
remove_watch(wd);
}
if (callback_copy) {
callback_copy(FileWatchEvent {
.path = std::move(event_path),
.mask = mask,
});
}
}
std::filesystem::path InotifyWatcher::normalize_path(
std::filesystem::path const &path)
{
std::error_code ec;
auto normalized { std::filesystem::weakly_canonical(path, ec) };
if (ec)
normalized = std::filesystem::absolute(path, ec);
if (ec)
return {};
return normalized;
}
} // namespace Waylight

57
src/InotifyWatcher.hpp Normal file
View File

@@ -0,0 +1,57 @@
#pragma once
#include <atomic>
#include <cstdint>
#include <filesystem>
#include <functional>
#include <mutex>
#include <string>
#include <string_view>
#include <unordered_map>
namespace Waylight {
struct FileWatchEvent {
std::filesystem::path path;
std::uint32_t mask;
};
class InotifyWatcher {
public:
using callback_t = std::function<void(FileWatchEvent const &)>;
InotifyWatcher();
~InotifyWatcher();
InotifyWatcher(InotifyWatcher const &) = delete;
InotifyWatcher &operator=(InotifyWatcher const &) = delete;
InotifyWatcher(InotifyWatcher &&) = delete;
InotifyWatcher &operator=(InotifyWatcher &&) = delete;
void watch_path_recursively(std::filesystem::path const &path);
void set_callback(callback_t cb);
void run();
void stop();
private:
void add_watch_for_path(std::filesystem::path const &path);
void add_watch_recursively(std::filesystem::path const &path);
void remove_watch(int wd);
void dispatch_event(int wd, std::uint32_t mask, std::string_view name);
static std::filesystem::path normalize_path(
std::filesystem::path const &path);
int m_fd;
std::atomic<bool> m_running { false };
callback_t m_callback;
std::mutex m_watch_mutex;
std::unordered_map<int, std::filesystem::path> m_watch_to_path;
std::unordered_map<std::string, int> m_path_to_watch;
};
} // namespace Waylight

View File

@@ -37,6 +37,8 @@
#include <ext/import-font.h>
#include <msdfgen.h>
namespace Waylight {
namespace {
constexpr int ATLAS_DIMENSION = 1024;
@@ -347,11 +349,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));
@@ -839,3 +841,5 @@ auto find_font_path(std::string_view path)
}
return final_path;
}
} // namespace Waylight

View File

@@ -21,12 +21,22 @@ namespace msdfgen {
class FontHandle;
}
struct FontHandle {
auto operator()() const -> auto const & { return id; }
namespace Waylight {
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;
@@ -137,3 +147,5 @@ private:
auto find_font_path(std::string_view path = "sans-serif:style=Regular")
-> std::optional<std::filesystem::path>;
} // namespace Waylight

View File

@@ -4,6 +4,8 @@
#include "enum_array.hpp"
namespace Waylight {
struct ColorScheme {
Color foreground;
Color foreground_preedit;
@@ -27,7 +29,7 @@ constexpr auto make_default_themes() -> enum_array<Theme, ColorScheme> const
.foreground_preedit = { 0, 0, 0, 255 },
.window =
{
.background = { 255, 255, 255, 100 },
.background = { 255, 255, 255, 220 },
},
};
array[Theme::Dark] = {
@@ -35,8 +37,10 @@ constexpr auto make_default_themes() -> enum_array<Theme, ColorScheme> const
.foreground_preedit = { 255, 255, 255, 255 },
.window =
{
.background = { 0, 0, 0, 100 },
.background = { 0, 0, 0, 220 },
},
};
return array;
}
} // namespace Waylight

View File

@@ -1,6 +1,7 @@
#include "App.hpp"
#include <cassert>
#include <string>
#include <EGL/egl.h>
#include <GLES3/gl3.h>
@@ -8,12 +9,14 @@
#include <rlgl.h>
#include <xkbcommon/xkbcommon.h>
#include <filesystem>
#include <optional>
namespace Waylight {
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();
@@ -49,8 +52,8 @@ auto App::tick() -> void
m_kbd.typing.clear();
}
ImGuiGuard gui_scope(m_gui, rune, m_kbd.ctrl(), m_kbd.shift(),
clipboard(),
[this](std::string_view const &str) { clipboard(str); });
get_clipboard(),
[this](std::string_view const &str) { set_clipboard(str); });
Rectangle const input_rect {
0.0f,
@@ -58,9 +61,20 @@ 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)) {
if (text_input_data == "kitty") {
execute_command(false, "kitty");
} else if (text_input_data == "nvim") {
execute_command(true, "nvim");
}
text_input_data = "";
}
update_text_input_state(text_input_data, 1, input_rect);
}
@@ -71,3 +85,5 @@ auto App::tick() -> void
eglSwapBuffers(m_gl.edpy, m_gl.esurf);
}
} // namespace Waylight

View File

@@ -1,7 +1,11 @@
#pragma once
#include <algorithm>
#include <array>
#include <cstdint>
namespace Waylight {
using u8 = std::uint8_t;
using i8 = std::int8_t;
using u16 = std::uint16_t;
@@ -15,9 +19,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 +38,7 @@ using isize = std::intptr_t;
utf8[3] = 0x80 | (cp & 0x3F);
}
return utf8;
return utf8.data();
}
} // namespace Waylight

View File

@@ -5,14 +5,17 @@
#include <stdexcept>
#include <type_traits>
#include "common.hpp"
namespace Waylight {
template<class E> struct enum_traits;
template<class E>
concept EnumLike = std::is_enum_v<E>;
template<EnumLike E>
constexpr usize enum_count_v
= static_cast<usize>(enum_traits<E>::last)
constexpr usize enum_count_v = static_cast<usize>(enum_traits<E>::last)
- static_cast<usize>(enum_traits<E>::first) + 1;
template<EnumLike E, class T> struct enum_array {
@@ -77,3 +80,5 @@ constexpr auto make_enum_array(T &&first_val, U &&...rest)
arr._data = { std::forward<T>(first_val), std::forward<U>(rest)... };
return arr;
}
} // namespace Waylight

View File

@@ -1,35 +1,31 @@
#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"
namespace Waylight {
bool signal_running();
std::optional<App> g_app {};
auto main() -> int {
if (signal_running()) {
return 0;
}
std::signal(SIGINT, [](int) {
if (g_app)
g_app->stop();
});
g_app.emplace();
g_app->run();
}
bool signal_running() {
const char *lock_path = "/tmp/waylight.lock";
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;
@@ -55,3 +51,38 @@ bool signal_running() {
dprintf(fd, "%d\n", getpid());
return false;
}
} // namespace Waylight
auto main() -> int
{
if (Waylight::signal_running()) {
return 0;
}
std::signal(SIGINT, [](int) {
if (Waylight::g_app)
Waylight::g_app->stop();
});
CPPTRACE_TRY
{
Waylight::g_app.emplace();
Waylight::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);
}
}

1
vendor/CMakeLists.txt vendored Normal file
View File

@@ -0,0 +1 @@
add_subdirectory(tinyfiledialogs)

8
vendor/tinyfiledialogs/CMakeLists.txt vendored Normal file
View File

@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.30)
project(tinyfiledialogs C)
add_library(${PROJECT_NAME} STATIC tinyfiledialogs.c)
target_include_directories(${PROJECT_NAME} PUBLIC include)

View File

@@ -0,0 +1,314 @@
/* SPDX-License-Identifier: Zlib
Copyright (c) 2014 - 2025 Guillaume Vareille http://ysengrin.com
____________________________________________________________________
| |
| 100% compatible C C++ -> You can rename tinfiledialogs.c as .cpp |
|____________________________________________________________________|
********* TINY FILE DIALOGS OFFICIAL WEBSITE IS ON SOURCEFORGE *********
_________
/ \ tinyfiledialogs.h v3.21.1 [Oct 5, 2025]
|tiny file| Unique header file created [November 9, 2014]
| dialogs |
\____ ___/ http://tinyfiledialogs.sourceforge.net
\| git clone http://git.code.sf.net/p/tinyfiledialogs/code tinyfd
____________________________________________
| |
| email: tinyfiledialogs at ysengrin.com |
|____________________________________________|
________________________________________________________________________________
| ____________________________________________________________________________ |
| | | |
| | - in tinyfiledialogs, char is UTF-8 by default (since v3.6) | |
| | | |
| | on windows: | |
| | - for UTF-16, use the wchar_t functions at the bottom of the header file | |
| | | |
| | - _wfopen() requires wchar_t | |
| | - fopen() uses char but expects ASCII or MBCS (not UTF-8) | |
| | - if you want char to be MBCS: set tinyfd_winUtf8 to 0 | |
| | | |
| | - alternatively, tinyfiledialogs provides | |
| | functions to convert between UTF-8, UTF-16 and MBCS | |
| |____________________________________________________________________________| |
|________________________________________________________________________________|
If you like tinyfiledialogs, please upvote my stackoverflow answer
https://stackoverflow.com/a/47651444
- License -
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
__________________________________________
| ______________________________________ |
| | | |
| | DO NOT USE USER INPUT IN THE DIALOGS | |
| |______________________________________| |
|__________________________________________|
*/
#ifndef TINYFILEDIALOGS_H
#define TINYFILEDIALOGS_H
#ifdef __cplusplus
extern "C" {
#endif
/******************************************************************************************************/
/**************************************** UTF-8 on Windows ********************************************/
/******************************************************************************************************/
#ifdef _WIN32
/* On windows, if you want to use UTF-8 ( instead of the UTF-16/wchar_t functions at the end of this file )
Make sure your code is really prepared for UTF-8 (on windows, functions like fopen() expect MBCS and not UTF-8) */
extern int tinyfd_winUtf8; /* on windows char strings can be 1:UTF-8(default) or 0:MBCS */
/* for MBCS change this to 0, in tinyfiledialogs.c or in your code */
/* Here are some functions to help you convert between UTF-16 UTF-8 MBSC */
char * tinyfd_utf8toMbcs(char const * aUtf8string);
char * tinyfd_utf16toMbcs(wchar_t const * aUtf16string);
wchar_t * tinyfd_mbcsTo16(char const * aMbcsString);
char * tinyfd_mbcsTo8(char const * aMbcsString);
wchar_t * tinyfd_utf8to16(char const * aUtf8string);
char * tinyfd_utf16to8(wchar_t const * aUtf16string);
#endif
/******************************************************************************************************/
/******************************************************************************************************/
/******************************************************************************************************/
/************* 3 funtions for C# (you don't need this in C or C++) : */
char const * tinyfd_getGlobalChar(char const * aCharVariableName); /* returns NULL on error */
int tinyfd_getGlobalInt(char const * aIntVariableName); /* returns -1 on error */
int tinyfd_setGlobalInt(char const * aIntVariableName, int aValue); /* returns -1 on error */
/* aCharVariableName: "tinyfd_version" "tinyfd_needs" "tinyfd_response"
aIntVariableName : "tinyfd_verbose" "tinyfd_silent" "tinyfd_allowCursesDialogs"
"tinyfd_forceConsole" "tinyfd_assumeGraphicDisplay" "tinyfd_winUtf8"
**************/
extern char tinyfd_version[8]; /* contains tinyfd current version number */
extern char tinyfd_needs[]; /* info about requirements */
extern int tinyfd_verbose; /* 0 (default) or 1 : on unix, prints the command line calls */
extern int tinyfd_silent; /* 1 (default) or 0 : on unix, hide errors and warnings from called dialogs */
/** Curses dialogs are difficult to use and counter-intuitive.
On windows they are only ascii and still uses the unix backslash ! **/
extern int tinyfd_allowCursesDialogs; /* 0 (default) or 1 */
extern int tinyfd_forceConsole; /* 0 (default) or 1 */
/* for unix & windows: 0 (graphic mode) or 1 (console mode).
0: try to use a graphic solution, if it fails then it uses console mode.
1: forces all dialogs into console mode even when an X server is present.
if enabled, it can use the package Dialog or dialog.exe.
on windows it only make sense for console applications */
/* extern int tinyfd_assumeGraphicDisplay; */ /* 0 (default) or 1 */
/* some systems don't set the environment variable DISPLAY even when a graphic display is present.
set this to 1 to tell tinyfiledialogs to assume the existence of a graphic display */
extern char tinyfd_response[1024];
/* if you pass "tinyfd_query" as aTitle,
the functions will not display the dialogs
but will return 0 for console mode, 1 for graphic mode.
tinyfd_response is then filled with the retain solution.
possible values for tinyfd_response are (all lowercase)
for graphic mode:
windows_wchar windows applescript kdialog zenity zenity3 yad matedialog
shellementary qarma shanty boxer python2-tkinter python3-tkinter python-dbus
perl-dbus gxmessage gmessage xmessage xdialog gdialog dunst
for console mode:
dialog whiptail basicinput no_solution */
void tinyfd_beep(void);
int tinyfd_notifyPopup(
char const * aTitle, /* NULL or "" */
char const * aMessage, /* NULL or "" may contain \n \t */
char const * aIconType); /* "info" "warning" "error" */
/* return has only meaning for tinyfd_query */
int tinyfd_messageBox(
char const * aTitle , /* NULL or "" */
char const * aMessage , /* NULL or "" may contain \n \t */
char const * aDialogType , /* "ok" "okcancel" "yesno" "yesnocancel" */
char const * aIconType , /* "info" "warning" "error" "question" */
int aDefaultButton ) ;
/* 0 for cancel/no , 1 for ok/yes , 2 for no in yesnocancel */
char * tinyfd_inputBox(
char const * aTitle , /* NULL or "" */
char const * aMessage , /* NULL or "" (\n and \t have no effect) */
char const * aDefaultInput ) ; /* NULL = passwordBox, "" = inputbox */
/* returns NULL on cancel */
char * tinyfd_saveFileDialog(
char const * aTitle , /* NULL or "" */
char const * aDefaultPathAndOrFile , /* NULL or "" , ends with / to set only a directory */
int aNumOfFilterPatterns , /* 0 (1 in the following example) */
char const * const * aFilterPatterns , /* NULL or char const * lFilterPatterns[1]={"*.txt"} */
char const * aSingleFilterDescription ) ; /* NULL or "text files" */
/* returns NULL on cancel */
char * tinyfd_openFileDialog(
char const * aTitle, /* NULL or "" */
char const * aDefaultPathAndOrFile, /* NULL or "" , ends with / to set only a directory */
int aNumOfFilterPatterns , /* 0 (2 in the following example) */
char const * const * aFilterPatterns, /* NULL or char const * lFilterPatterns[2]={"*.png","*.jpg"}; */
char const * aSingleFilterDescription, /* NULL or "image files" */
int aAllowMultipleSelects ) ; /* 0 or 1 */
/* in case of multiple files, the separator is | */
/* returns NULL on cancel */
char * tinyfd_selectFolderDialog(
char const * aTitle, /* NULL or "" */
char const * aDefaultPath); /* NULL or "" */
/* returns NULL on cancel */
char * tinyfd_colorChooser(
char const * aTitle, /* NULL or "" */
char const * aDefaultHexRGB, /* NULL or "" or "#FF0000" */
unsigned char const aDefaultRGB[3] , /* unsigned char lDefaultRGB[3] = { 0 , 128 , 255 }; */
unsigned char aoResultRGB[3] ) ; /* unsigned char lResultRGB[3]; */
/* aDefaultRGB is used only if aDefaultHexRGB is absent */
/* aDefaultRGB and aoResultRGB can be the same array */
/* returns NULL on cancel */
/* returns the hexcolor as a string "#FF0000" */
/* aoResultRGB also contains the result */
/************ WINDOWS ONLY SECTION ************************/
#ifdef _WIN32
/* windows only - utf-16 version */
int tinyfd_notifyPopupW(
wchar_t const * aTitle, /* NULL or L"" */
wchar_t const * aMessage, /* NULL or L"" may contain \n \t */
wchar_t const * aIconType); /* L"info" L"warning" L"error" */
/* windows only - utf-16 version */
int tinyfd_messageBoxW(
wchar_t const * aTitle, /* NULL or L"" */
wchar_t const * aMessage, /* NULL or L"" may contain \n \t */
wchar_t const * aDialogType, /* L"ok" L"okcancel" L"yesno" */
wchar_t const * aIconType, /* L"info" L"warning" L"error" L"question" */
int aDefaultButton ); /* 0 for cancel/no , 1 for ok/yes */
/* returns 0 for cancel/no , 1 for ok/yes */
/* windows only - utf-16 version */
wchar_t * tinyfd_inputBoxW(
wchar_t const * aTitle, /* NULL or L"" */
wchar_t const * aMessage, /* NULL or L"" (\n nor \t not respected) */
wchar_t const * aDefaultInput); /* NULL passwordBox, L"" inputbox */
/* windows only - utf-16 version */
wchar_t * tinyfd_saveFileDialogW(
wchar_t const * aTitle, /* NULL or L"" */
wchar_t const * aDefaultPathAndOrFile, /* NULL or L"" , ends with / to set only a directory */
int aNumOfFilterPatterns, /* 0 (1 in the following example) */
wchar_t const * const * aFilterPatterns, /* NULL or wchar_t const * lFilterPatterns[1]={L"*.txt"} */
wchar_t const * aSingleFilterDescription); /* NULL or L"text files" */
/* returns NULL on cancel */
/* windows only - utf-16 version */
wchar_t * tinyfd_openFileDialogW(
wchar_t const * aTitle, /* NULL or L"" */
wchar_t const * aDefaultPathAndOrFile, /* NULL or L"" , ends with / to set only a directory */
int aNumOfFilterPatterns , /* 0 (2 in the following example) */
wchar_t const * const * aFilterPatterns, /* NULL or wchar_t const * lFilterPatterns[2]={L"*.png","*.jpg"} */
wchar_t const * aSingleFilterDescription, /* NULL or L"image files" */
int aAllowMultipleSelects ) ; /* 0 or 1 */
/* in case of multiple files, the separator is | */
/* returns NULL on cancel */
/* windows only - utf-16 version */
wchar_t * tinyfd_selectFolderDialogW(
wchar_t const * aTitle, /* NULL or L"" */
wchar_t const * aDefaultPath); /* NULL or L"" */
/* returns NULL on cancel */
/* windows only - utf-16 version */
wchar_t * tinyfd_colorChooserW(
wchar_t const * aTitle, /* NULL or L"" */
wchar_t const * aDefaultHexRGB, /* NULL or L"#FF0000" */
unsigned char const aDefaultRGB[3], /* unsigned char lDefaultRGB[3] = { 0 , 128 , 255 }; */
unsigned char aoResultRGB[3]); /* unsigned char lResultRGB[3]; */
/* returns the hexcolor as a string L"#FF0000" */
/* aoResultRGB also contains the result */
/* aDefaultRGB is used only if aDefaultHexRGB is NULL */
/* aDefaultRGB and aoResultRGB can be the same array */
/* returns NULL on cancel */
#endif /*_WIN32 */
#ifdef __cplusplus
} /*extern "C"*/
#endif
#endif /* TINYFILEDIALOGS_H */
/*
________________________________________________________________________________
| ____________________________________________________________________________ |
| | | |
| | on windows: | |
| | - for UTF-16, use the wchar_t functions at the bottom of the header file | |
| | - _wfopen() requires wchar_t | |
| | | |
| | - in tinyfiledialogs, char is UTF-8 by default (since v3.6) | |
| | - but fopen() expects MBCS (not UTF-8) | |
| | - if you want char to be MBCS: set tinyfd_winUtf8 to 0 | |
| | | |
| | - alternatively, tinyfiledialogs provides | |
| | functions to convert between UTF-8, UTF-16 and MBCS | |
| |____________________________________________________________________________| |
|________________________________________________________________________________|
- This is not for ios nor android (it works in termux though).
- The files can be renamed with extension ".cpp" as the code is 100% compatible C C++
(just comment out << extern "C" >> in the header file)
- Windows is fully supported from XP to 10 (maybe even older versions)
- C# & LUA via dll, see files in the folder EXTRAS
- OSX supported from 10.4 to latest (maybe even older versions)
- Do not use " and ' as the dialogs will be displayed with a warning
instead of the title, message, etc...
- There's one file filter only, it may contain several patterns.
- If no filter description is provided,
the list of patterns will become the description.
- On windows link against Comdlg32.lib and Ole32.lib
(on windows the no linking claim is a lie)
- On unix: it tries command line calls, so no such need (NO LINKING).
- On unix you need one of the following:
applescript, kdialog, zenity, matedialog, shellementary, qarma, shanty, boxer,
yad, python (2 or 3)/tkinter/python-dbus (optional), Xdialog
or curses dialogs (opens terminal if running without console).
- One of those is already included on most (if not all) desktops.
- In the absence of those it will use gdialog, gxmessage or whiptail
with a textinputbox. If nothing is found, it switches to basic console input,
it opens a console if needed (requires xterm + bash).
- for curses dialogs you must set tinyfd_allowCursesDialogs=1
- You can query the type of dialog that will be used (pass "tinyfd_query" as aTitle)
- String memory is preallocated statically for all the returned values.
- File and path names are tested before return, they should be valid.
- tinyfd_forceConsole=1; at run time, forces dialogs into console mode.
- On windows, console mode only make sense for console applications.
- On windows, console mode is not implemented for wchar_T UTF-16.
- Mutiple selects are not possible in console mode.
- The package dialog must be installed to run in curses dialogs in console mode.
It is already installed on most unix systems.
- On osx, the package dialog can be installed via
http://macappstore.org/dialog or http://macports.org
- On windows, for curses dialogs console mode,
dialog.exe should be copied somewhere on your executable path.
It can be found at the bottom of the following page:
http://andrear.altervista.org/home/cdialog.php
*/

8397
vendor/tinyfiledialogs/tinyfiledialogs.c vendored Normal file

File diff suppressed because it is too large Load Diff