From 0d14fd9136d7dbd731b02ce8501ff725f11b71cc Mon Sep 17 00:00:00 2001 From: jrun Date: Thu, 5 Jan 2023 14:17:55 -0500 Subject: [PATCH] add systemd support (#1253) * add systemd support motivation for this is is proper ordering of related/bound/required services to Hyprland (e.g. swaybg) that would need to have a compositor ready. this could possibly be a build-time option of course. see also: example/ files for example of services Signed-off-by: Paymon MARANDI * nix: add withSystemd flag Signed-off-by: Paymon MARANDI Co-authored-by: Mihai Fufezan Co-authored-by: Vaxerski --- CMakeLists.txt | 13 +++++++++++++ example/hyprland.service | 12 ++++++++++++ example/swaybg@.service | 13 +++++++++++++ meson.build | 10 ++++++++++ meson_options.txt | 1 + nix/default.nix | 8 ++++++-- src/Compositor.cpp | 12 ++++++++++++ src/main.cpp | 9 +++++++++ src/meson.build | 1 + subprojects/wlroots | 2 +- 10 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 example/hyprland.service create mode 100644 example/swaybg@.service diff --git a/CMakeLists.txt b/CMakeLists.txt index de35c507..f3752eaf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,19 @@ ELSE() target_link_libraries(Hyprland xcb) ENDIF(NO_XWAYLAND MATCHES true) +IF(NO_SYSTEMD MATCHES true) + message(STATUS "SYSTEMD support is disabled...") +ELSE() + message(STATUS "SYSTEMD support is requested (NO_SYSTEMD not defined) checking deps...") + pkg_check_modules(LIBSYSTEMD libsystemd) + IF(LIBSYSTEMD_FOUND) + add_definitions( -DUSES_SYSTEMD ) + target_link_libraries(Hyprland "${LIBSYSTEMD_LIBRARIES}") + ELSE() + message(WARNING "Systemd support requested but libsystemd was not found") + ENDIF(LIBSYSTEMD_FOUND) +ENDIF(NO_SYSTEMD MATCHES true) + target_compile_definitions(Hyprland PRIVATE "-DGIT_COMMIT_HASH=\"${GIT_COMMIT_HASH}\"") target_compile_definitions(Hyprland PRIVATE "-DGIT_BRANCH=\"${GIT_BRANCH}\"") target_compile_definitions(Hyprland PRIVATE "-DGIT_COMMIT_MESSAGE=\"${GIT_COMMIT_MESSAGE}\"") diff --git a/example/hyprland.service b/example/hyprland.service new file mode 100644 index 00000000..1ff479bb --- /dev/null +++ b/example/hyprland.service @@ -0,0 +1,12 @@ +; a primitive systemd --user example +[Unit] +Description = %p +BindsTo = graphical-session.target +Upholds = swaybg@333333.service + +[Service] +Type = notify +ExecStart = /usr/bin/Hyprland + +[Install] +WantedBy = default.target diff --git a/example/swaybg@.service b/example/swaybg@.service new file mode 100644 index 00000000..0636625d --- /dev/null +++ b/example/swaybg@.service @@ -0,0 +1,13 @@ +; a primitive systemd --user example +; see example/hyprland.service for more details +[Unit] +Description = %p +BindsTo = hyprland.service +Wants = hyprland.service +After = hyprland.service + +[Service] +ExecStart = /usr/bin/swaybg --color #%i + +[Install] +WantedBy = default.target diff --git a/meson.build b/meson.build index 3ca2b578..73c84abf 100644 --- a/meson.build +++ b/meson.build @@ -50,6 +50,16 @@ if not have_xwayland add_project_arguments('-DNO_XWAYLAND', language: 'cpp') endif +systemd_dep = dependency('libsystemd', required: get_option('systemd')) + +if get_option('systemd').enabled() + if systemd_dep.found() + add_project_arguments('-DUSES_SYSTEMD', language: 'cpp') + else + error('Cannot enable systemd in Hyprland: libsystemd was not found') + endif +endif + if get_option('buildtype') == 'debug' add_project_arguments('-DHYPRLAND_DEBUG', language: 'cpp') endif diff --git a/meson_options.txt b/meson_options.txt index d34c6170..e8e2b062 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1 +1,2 @@ option('xwayland', type: 'feature', value: 'auto', description: 'Enable support for X11 applications') +option('systemd', type: 'feature', value: 'auto', description: 'Enable systemd integration') diff --git a/nix/default.nix b/nix/default.nix index 042fbfe7..8cd0350f 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -16,6 +16,7 @@ mesa, mount, pciutils, + systemd, wayland, wayland-protocols, wayland-scanner, @@ -27,6 +28,7 @@ hidpiXWayland ? true, legacyRenderer ? false, nvidiaPatches ? false, + withSystemd ? true, version ? "git", }: let assertXWayland = lib.assertMsg (hidpiXWayland -> enableXWayland) '' @@ -73,7 +75,8 @@ in pciutils (wlroots.override {inherit enableXWayland hidpiXWayland nvidiaPatches;}) ] - ++ lib.optionals enableXWayland [libxcb xcbutilwm xwayland]; + ++ lib.optionals enableXWayland [libxcb xcbutilwm xwayland] + ++ lib.optionals withSystemd [systemd]; mesonBuildType = if debug @@ -83,6 +86,7 @@ in mesonFlags = builtins.concatLists [ (lib.optional (!enableXWayland) "-Dxwayland=disabled") (lib.optional legacyRenderer "-DLEGACY_RENDERER:STRING=true") + (lib.optional withSystemd "-Dsystemd=enabled") ]; patches = [ @@ -90,8 +94,8 @@ in ./meson-build.patch ]; - # Fix hardcoded paths to /usr installation postPatch = '' + # Fix hardcoded paths to /usr installation sed -i "s#/usr#$out#" src/render/OpenGL.cpp # for some reason rmdir doesn't work in a dirty tree diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 2c9ad503..b60d70d7 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -2,6 +2,9 @@ #include "helpers/Splashes.hpp" #include #include "debug/HyprCtl.hpp" +#ifdef USES_SYSTEMD +#include // for sd_notify +#endif int handleCritSignal(int signo, void* data) { Debug::log(LOG, "Hyprland received signal %d", signo); @@ -384,6 +387,15 @@ void CCompositor::startCompositor() { wlr_xcursor_manager_set_cursor_image(m_sWLRXCursorMgr, "left_ptr", m_sWLRCursor); +#ifdef USES_SYSTEMD + if (sd_booted() > 0) + // tell systemd that we are ready so it can start other bond, following, related units + sd_notify(0, "READY=1"); + else + Debug::log(LOG, "systemd integration is baked in but system itself is not booted à la systemd!"); +#endif + + // This blocks until we are done. Debug::log(LOG, "Hyprland is ready, running the event loop!"); wl_display_run(m_sWLDisplay); diff --git a/src/main.cpp b/src/main.cpp index 5b56944e..7d1353ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,9 @@ #include "config/ConfigManager.hpp" #include "init/initHelpers.hpp" #include +#ifdef USES_SYSTEMD +#include // for sd_notify +#endif int main(int argc, char** argv) { @@ -66,6 +69,12 @@ int main(int argc, char** argv) { // If we are here it means we got yote. Debug::log(LOG, "Hyprland reached the end."); +#ifdef USES_SYSTEMD + // tell systemd it destroy bound/related units + if (sd_booted() > 0) + sd_notify(0, "STOPPING=1"); +#endif + wl_display_destroy_clients(g_pCompositor->m_sWLDisplay); // kill all clients diff --git a/src/meson.build b/src/meson.build index a8fa7cfc..d7b1ec22 100644 --- a/src/meson.build +++ b/src/meson.build @@ -14,6 +14,7 @@ executable('Hyprland', src, dependency('xkbcommon'), dependency('libinput'), xcb_dep, + systemd_dep, dependency('pixman-1'), dependency('gl', 'opengl'), diff --git a/subprojects/wlroots b/subprojects/wlroots index b28a9afd..dc7cc98c 160000 --- a/subprojects/wlroots +++ b/subprojects/wlroots @@ -1 +1 @@ -Subproject commit b28a9afd4b0b86e9a66a40f6b44b69f59947b7d6 +Subproject commit dc7cc98cf21a8dc19ab8895505500e3700646af0